Glauber de Oliveira Costa | 80fbb69 | 2008-01-30 13:31:13 +0100 | [diff] [blame] | 1 | #ifndef _ASM_DESC_H_ |
| 2 | #define _ASM_DESC_H_ |
| 3 | |
| 4 | #ifndef __ASSEMBLY__ |
| 5 | #include <asm/desc_defs.h> |
| 6 | #include <asm/ldt.h> |
Glauber de Oliveira Costa | 881c297 | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 7 | #include <asm/mmu.h> |
Glauber de Oliveira Costa | 54cd0ea | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 8 | #include <linux/smp.h> |
Glauber de Oliveira Costa | 80fbb69 | 2008-01-30 13:31:13 +0100 | [diff] [blame] | 9 | |
| 10 | static inline void fill_ldt(struct desc_struct *desc, struct user_desc *info) |
| 11 | { |
| 12 | desc->limit0 = info->limit & 0x0ffff; |
| 13 | desc->base0 = info->base_addr & 0x0000ffff; |
| 14 | |
| 15 | desc->base1 = (info->base_addr & 0x00ff0000) >> 16; |
| 16 | desc->type = (info->read_exec_only ^ 1) << 1; |
| 17 | desc->type |= info->contents << 2; |
| 18 | desc->s = 1; |
| 19 | desc->dpl = 0x3; |
| 20 | desc->p = info->seg_not_present ^ 1; |
| 21 | desc->limit = (info->limit & 0xf0000) >> 16; |
| 22 | desc->avl = info->useable; |
| 23 | desc->d = info->seg_32bit; |
| 24 | desc->g = info->limit_in_pages; |
| 25 | desc->base2 = (info->base_addr & 0xff000000) >> 24; |
| 26 | } |
| 27 | |
Glauber de Oliveira Costa | 881c297 | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 28 | extern struct desc_ptr idt_descr; |
| 29 | extern gate_desc idt_table[]; |
Glauber de Oliveira Costa | 80fbb69 | 2008-01-30 13:31:13 +0100 | [diff] [blame] | 30 | |
Glauber de Oliveira Costa | 54cd0ea | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 31 | #ifdef CONFIG_X86_64 |
| 32 | extern struct desc_struct cpu_gdt_table[GDT_ENTRIES]; |
| 33 | extern struct desc_ptr cpu_gdt_descr[]; |
| 34 | /* the cpu gdt accessor */ |
| 35 | #define get_cpu_gdt_table(x) ((struct desc_struct *)cpu_gdt_descr[x].address) |
Glauber de Oliveira Costa | 507f90c | 2008-01-30 13:31:14 +0100 | [diff] [blame^] | 36 | |
| 37 | static inline void pack_gate(gate_desc *gate, unsigned type, unsigned long func, |
| 38 | unsigned dpl, unsigned ist, unsigned seg) |
| 39 | { |
| 40 | gate->offset_low = PTR_LOW(func); |
| 41 | gate->segment = __KERNEL_CS; |
| 42 | gate->ist = ist; |
| 43 | gate->p = 1; |
| 44 | gate->dpl = dpl; |
| 45 | gate->zero0 = 0; |
| 46 | gate->zero1 = 0; |
| 47 | gate->type = type; |
| 48 | gate->offset_middle = PTR_MIDDLE(func); |
| 49 | gate->offset_high = PTR_HIGH(func); |
| 50 | } |
| 51 | |
Glauber de Oliveira Costa | 54cd0ea | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 52 | #else |
| 53 | struct gdt_page { |
| 54 | struct desc_struct gdt[GDT_ENTRIES]; |
| 55 | } __attribute__((aligned(PAGE_SIZE))); |
| 56 | DECLARE_PER_CPU(struct gdt_page, gdt_page); |
| 57 | |
| 58 | static inline struct desc_struct *get_cpu_gdt_table(unsigned int cpu) |
| 59 | { |
| 60 | return per_cpu(gdt_page, cpu).gdt; |
| 61 | } |
Glauber de Oliveira Costa | 507f90c | 2008-01-30 13:31:14 +0100 | [diff] [blame^] | 62 | |
| 63 | static inline void pack_gate(gate_desc *gate, unsigned char type, |
| 64 | unsigned long base, unsigned dpl, unsigned flags, unsigned short seg) |
| 65 | |
| 66 | { |
| 67 | gate->a = (seg << 16) | (base & 0xffff); |
| 68 | gate->b = (base & 0xffff0000) | |
| 69 | (((0x80 | type | (dpl << 5)) & 0xff) << 8); |
| 70 | } |
| 71 | |
Glauber de Oliveira Costa | 54cd0ea | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 72 | #endif |
| 73 | |
| 74 | #ifdef CONFIG_PARAVIRT |
| 75 | #include <asm/paravirt.h> |
| 76 | #else |
| 77 | #define load_TR_desc() native_load_tr_desc() |
| 78 | #define load_gdt(dtr) native_load_gdt(dtr) |
| 79 | #define load_idt(dtr) native_load_idt(dtr) |
| 80 | #define load_tr(tr) __asm__ __volatile("ltr %0"::"m" (tr)) |
| 81 | #define load_ldt(ldt) __asm__ __volatile("lldt %0"::"m" (ldt)) |
| 82 | |
| 83 | #define store_gdt(dtr) native_store_gdt(dtr) |
| 84 | #define store_idt(dtr) native_store_idt(dtr) |
| 85 | #define store_tr(tr) (tr = native_store_tr()) |
| 86 | #define store_ldt(ldt) __asm__ ("sldt %0":"=m" (ldt)) |
| 87 | |
| 88 | #define load_TLS(t, cpu) native_load_tls(t, cpu) |
| 89 | #define set_ldt native_set_ldt |
| 90 | |
| 91 | #define write_ldt_entry(dt, entry, desc) \ |
| 92 | native_write_ldt_entry(dt, entry, desc) |
| 93 | #define write_gdt_entry(dt, entry, desc, type) \ |
| 94 | native_write_gdt_entry(dt, entry, desc, type) |
| 95 | #define write_idt_entry(dt, entry, g) native_write_idt_entry(dt, entry, g) |
| 96 | #endif |
| 97 | |
| 98 | static inline void native_write_idt_entry(gate_desc *idt, int entry, |
| 99 | const gate_desc *gate) |
| 100 | { |
| 101 | memcpy(&idt[entry], gate, sizeof(*gate)); |
| 102 | } |
| 103 | |
| 104 | static inline void native_write_ldt_entry(struct desc_struct *ldt, int entry, |
| 105 | const void *desc) |
| 106 | { |
| 107 | memcpy(&ldt[entry], desc, 8); |
| 108 | } |
| 109 | |
| 110 | static inline void native_write_gdt_entry(struct desc_struct *gdt, int entry, |
| 111 | const void *desc, int type) |
| 112 | { |
| 113 | unsigned int size; |
| 114 | switch (type) { |
| 115 | case DESC_TSS: |
| 116 | size = sizeof(tss_desc); |
| 117 | break; |
| 118 | case DESC_LDT: |
| 119 | size = sizeof(ldt_desc); |
| 120 | break; |
| 121 | default: |
| 122 | size = sizeof(struct desc_struct); |
| 123 | break; |
| 124 | } |
| 125 | memcpy(&gdt[entry], desc, size); |
| 126 | } |
| 127 | |
| 128 | static inline void set_tssldt_descriptor(struct ldttss_desc64 *d, |
| 129 | unsigned long tss, unsigned type, |
| 130 | unsigned size) |
| 131 | { |
| 132 | memset(d, 0, sizeof(*d)); |
| 133 | d->limit0 = size & 0xFFFF; |
| 134 | d->base0 = PTR_LOW(tss); |
| 135 | d->base1 = PTR_MIDDLE(tss) & 0xFF; |
| 136 | d->type = type; |
| 137 | d->p = 1; |
| 138 | d->limit1 = (size >> 16) & 0xF; |
| 139 | d->base2 = (PTR_MIDDLE(tss) >> 8) & 0xFF; |
| 140 | d->base3 = PTR_HIGH(tss); |
| 141 | } |
| 142 | |
| 143 | static inline void pack_descriptor(struct desc_struct *desc, unsigned long base, |
| 144 | unsigned long limit, unsigned char type, |
| 145 | unsigned char flags) |
| 146 | { |
| 147 | desc->a = ((base & 0xffff) << 16) | (limit & 0xffff); |
| 148 | desc->b = (base & 0xff000000) | ((base & 0xff0000) >> 16) | |
| 149 | (limit & 0x000f0000) | ((type & 0xff) << 8) | |
| 150 | ((flags & 0xf) << 20); |
| 151 | desc->p = 1; |
| 152 | } |
| 153 | |
| 154 | static inline void pack_ldt(ldt_desc *ldt, unsigned long addr, |
| 155 | unsigned size) |
| 156 | { |
| 157 | |
| 158 | #ifdef CONFIG_X86_64 |
| 159 | set_tssldt_descriptor(ldt, |
| 160 | addr, DESC_LDT, size); |
| 161 | #else |
| 162 | pack_descriptor(ldt, (unsigned long)addr, |
| 163 | size, |
| 164 | 0x80 | DESC_LDT, 0); |
| 165 | #endif |
| 166 | } |
| 167 | |
| 168 | static inline void native_set_ldt(const void *addr, unsigned int entries) |
| 169 | { |
| 170 | if (likely(entries == 0)) |
| 171 | __asm__ __volatile__("lldt %w0"::"q" (0)); |
| 172 | else { |
| 173 | unsigned cpu = smp_processor_id(); |
| 174 | ldt_desc ldt; |
| 175 | |
| 176 | pack_ldt(&ldt, (unsigned long)addr, |
| 177 | entries * sizeof(ldt) - 1); |
| 178 | write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_LDT, |
| 179 | &ldt, DESC_LDT); |
| 180 | __asm__ __volatile__("lldt %w0"::"q" (GDT_ENTRY_LDT*8)); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | static inline void native_load_tr_desc(void) |
| 185 | { |
| 186 | asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS*8)); |
| 187 | } |
| 188 | |
| 189 | static inline void native_load_gdt(const struct desc_ptr *dtr) |
| 190 | { |
| 191 | asm volatile("lgdt %0"::"m" (*dtr)); |
| 192 | } |
| 193 | |
| 194 | static inline void native_load_idt(const struct desc_ptr *dtr) |
| 195 | { |
| 196 | asm volatile("lidt %0"::"m" (*dtr)); |
| 197 | } |
| 198 | |
| 199 | static inline void native_store_gdt(struct desc_ptr *dtr) |
| 200 | { |
| 201 | asm volatile("sgdt %0":"=m" (*dtr)); |
| 202 | } |
| 203 | |
| 204 | static inline void native_store_idt(struct desc_ptr *dtr) |
| 205 | { |
| 206 | asm volatile("sidt %0":"=m" (*dtr)); |
| 207 | } |
| 208 | |
| 209 | static inline unsigned long native_store_tr(void) |
| 210 | { |
| 211 | unsigned long tr; |
| 212 | asm volatile("str %0":"=r" (tr)); |
| 213 | return tr; |
| 214 | } |
| 215 | |
| 216 | static inline void native_load_tls(struct thread_struct *t, unsigned int cpu) |
| 217 | { |
| 218 | unsigned int i; |
| 219 | struct desc_struct *gdt = get_cpu_gdt_table(cpu); |
| 220 | |
| 221 | for (i = 0; i < GDT_ENTRY_TLS_ENTRIES; i++) |
| 222 | gdt[GDT_ENTRY_TLS_MIN + i] = t->tls_array[i]; |
| 223 | } |
| 224 | |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 225 | #ifdef CONFIG_X86_32 |
| 226 | # include "desc_32.h" |
| 227 | #else |
| 228 | # include "desc_64.h" |
| 229 | #endif |
Glauber de Oliveira Costa | 80fbb69 | 2008-01-30 13:31:13 +0100 | [diff] [blame] | 230 | |
Glauber de Oliveira Costa | 881c297 | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 231 | #define _LDT_empty(info) (\ |
| 232 | (info)->base_addr == 0 && \ |
| 233 | (info)->limit == 0 && \ |
| 234 | (info)->contents == 0 && \ |
| 235 | (info)->read_exec_only == 1 && \ |
| 236 | (info)->seg_32bit == 0 && \ |
| 237 | (info)->limit_in_pages == 0 && \ |
| 238 | (info)->seg_not_present == 1 && \ |
| 239 | (info)->useable == 0) |
| 240 | |
| 241 | #ifdef CONFIG_X86_64 |
| 242 | #define LDT_empty(info) (_LDT_empty(info) && ((info)->lm == 0)) |
| 243 | #else |
| 244 | #define LDT_empty(info) (_LDT_empty(info)) |
| 245 | #endif |
| 246 | |
| 247 | static inline void clear_LDT(void) |
| 248 | { |
| 249 | set_ldt(NULL, 0); |
| 250 | } |
| 251 | |
| 252 | /* |
| 253 | * load one particular LDT into the current CPU |
| 254 | */ |
| 255 | static inline void load_LDT_nolock(mm_context_t *pc) |
| 256 | { |
| 257 | set_ldt(pc->ldt, pc->size); |
| 258 | } |
| 259 | |
| 260 | static inline void load_LDT(mm_context_t *pc) |
| 261 | { |
| 262 | preempt_disable(); |
| 263 | load_LDT_nolock(pc); |
| 264 | preempt_enable(); |
| 265 | } |
| 266 | |
Glauber de Oliveira Costa | cc69785 | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 267 | static inline unsigned long get_desc_base(struct desc_struct *desc) |
| 268 | { |
| 269 | return desc->base0 | ((desc->base1) << 16) | ((desc->base2) << 24); |
| 270 | } |
Glauber de Oliveira Costa | 507f90c | 2008-01-30 13:31:14 +0100 | [diff] [blame^] | 271 | static inline void _set_gate(int gate, unsigned type, void *addr, |
| 272 | unsigned dpl, unsigned ist, unsigned seg) |
| 273 | { |
| 274 | gate_desc s; |
| 275 | pack_gate(&s, type, (unsigned long)addr, dpl, ist, seg); |
| 276 | /* |
| 277 | * does not need to be atomic because it is only done once at |
| 278 | * setup time |
| 279 | */ |
| 280 | write_idt_entry(idt_table, gate, &s); |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * This needs to use 'idt_table' rather than 'idt', and |
| 285 | * thus use the _nonmapped_ version of the IDT, as the |
| 286 | * Pentium F0 0F bugfix can have resulted in the mapped |
| 287 | * IDT being write-protected. |
| 288 | */ |
| 289 | static inline void set_intr_gate(unsigned int n, void *addr) |
| 290 | { |
| 291 | BUG_ON((unsigned)n > 0xFF); |
| 292 | _set_gate(n, GATE_INTERRUPT, addr, 0, 0, __KERNEL_CS); |
| 293 | } |
| 294 | |
| 295 | /* |
| 296 | * This routine sets up an interrupt gate at directory privilege level 3. |
| 297 | */ |
| 298 | static inline void set_system_intr_gate(unsigned int n, void *addr) |
| 299 | { |
| 300 | BUG_ON((unsigned)n > 0xFF); |
| 301 | _set_gate(n, GATE_INTERRUPT, addr, 0x3, 0, __KERNEL_CS); |
| 302 | } |
| 303 | |
| 304 | static inline void set_trap_gate(unsigned int n, void *addr) |
| 305 | { |
| 306 | BUG_ON((unsigned)n > 0xFF); |
| 307 | _set_gate(n, GATE_TRAP, addr, 0, 0, __KERNEL_CS); |
| 308 | } |
| 309 | |
| 310 | static inline void set_system_gate(unsigned int n, void *addr) |
| 311 | { |
| 312 | BUG_ON((unsigned)n > 0xFF); |
| 313 | #ifdef CONFIG_X86_32 |
| 314 | _set_gate(n, GATE_TRAP, addr, 0x3, 0, __KERNEL_CS); |
| 315 | #else |
| 316 | _set_gate(n, GATE_INTERRUPT, addr, 0x3, 0, __KERNEL_CS); |
| 317 | #endif |
| 318 | } |
| 319 | |
| 320 | static inline void set_task_gate(unsigned int n, unsigned int gdt_entry) |
| 321 | { |
| 322 | BUG_ON((unsigned)n > 0xFF); |
| 323 | _set_gate(n, GATE_TASK, (void *)0, 0, 0, (gdt_entry<<3)); |
| 324 | } |
| 325 | |
| 326 | static inline void set_intr_gate_ist(int n, void *addr, unsigned ist) |
| 327 | { |
| 328 | BUG_ON((unsigned)n > 0xFF); |
| 329 | _set_gate(n, GATE_INTERRUPT, addr, 0, ist, __KERNEL_CS); |
| 330 | } |
| 331 | |
| 332 | static inline void set_system_gate_ist(int n, void *addr, unsigned ist) |
| 333 | { |
| 334 | BUG_ON((unsigned)n > 0xFF); |
| 335 | _set_gate(n, GATE_INTERRUPT, addr, 0x3, ist, __KERNEL_CS); |
| 336 | } |
Glauber de Oliveira Costa | cc69785 | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 337 | |
Glauber de Oliveira Costa | 881c297 | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 338 | #else |
| 339 | /* |
| 340 | * GET_DESC_BASE reads the descriptor base of the specified segment. |
| 341 | * |
| 342 | * Args: |
| 343 | * idx - descriptor index |
| 344 | * gdt - GDT pointer |
| 345 | * base - 32bit register to which the base will be written |
| 346 | * lo_w - lo word of the "base" register |
| 347 | * lo_b - lo byte of the "base" register |
| 348 | * hi_b - hi byte of the low word of the "base" register |
| 349 | * |
| 350 | * Example: |
| 351 | * GET_DESC_BASE(GDT_ENTRY_ESPFIX_SS, %ebx, %eax, %ax, %al, %ah) |
| 352 | * Will read the base address of GDT_ENTRY_ESPFIX_SS and put it into %eax. |
| 353 | */ |
| 354 | #define GET_DESC_BASE(idx, gdt, base, lo_w, lo_b, hi_b) \ |
| 355 | movb idx*8+4(gdt), lo_b; \ |
| 356 | movb idx*8+7(gdt), hi_b; \ |
| 357 | shll $16, base; \ |
| 358 | movw idx*8+2(gdt), lo_w; |
| 359 | |
| 360 | |
| 361 | #endif /* __ASSEMBLY__ */ |
| 362 | |
Glauber de Oliveira Costa | 80fbb69 | 2008-01-30 13:31:13 +0100 | [diff] [blame] | 363 | #endif |