1 #ifndef _ASM_X86_DESC_H
2 #define _ASM_X86_DESC_H
4 #include <asm/desc_defs.h>
7 #include <asm/fixmap.h>
10 #include <linux/percpu.h>
12 static inline void fill_ldt(struct desc_struct
*desc
, const struct user_desc
*info
)
14 desc
->limit0
= info
->limit
& 0x0ffff;
16 desc
->base0
= (info
->base_addr
& 0x0000ffff);
17 desc
->base1
= (info
->base_addr
& 0x00ff0000) >> 16;
19 desc
->type
= (info
->read_exec_only
^ 1) << 1;
20 desc
->type
|= info
->contents
<< 2;
24 desc
->p
= info
->seg_not_present
^ 1;
25 desc
->limit
= (info
->limit
& 0xf0000) >> 16;
26 desc
->avl
= info
->useable
;
27 desc
->d
= info
->seg_32bit
;
28 desc
->g
= info
->limit_in_pages
;
30 desc
->base2
= (info
->base_addr
& 0xff000000) >> 24;
32 * Don't allow setting of the lm bit. It would confuse
33 * user_64bit_mode and would get overridden by sysret anyway.
38 extern struct desc_ptr idt_descr
;
39 extern gate_desc idt_table
[];
40 extern const struct desc_ptr debug_idt_descr
;
41 extern gate_desc debug_idt_table
[];
42 extern pgprot_t pg_fixmap_gdt_flags
;
45 struct desc_struct gdt
[GDT_ENTRIES
];
46 } __attribute__((aligned(PAGE_SIZE
)));
48 DECLARE_PER_CPU_PAGE_ALIGNED(struct gdt_page
, gdt_page
);
50 /* Provide the original GDT */
51 static inline struct desc_struct
*get_cpu_gdt_rw(unsigned int cpu
)
53 return per_cpu(gdt_page
, cpu
).gdt
;
56 /* Provide the current original GDT */
57 static inline struct desc_struct
*get_current_gdt_rw(void)
59 return this_cpu_ptr(&gdt_page
)->gdt
;
62 /* Get the fixmap index for a specific processor */
63 static inline unsigned int get_cpu_gdt_ro_index(int cpu
)
65 return FIX_GDT_REMAP_BEGIN
+ cpu
;
68 /* Provide the fixmap address of the remapped GDT */
69 static inline struct desc_struct
*get_cpu_gdt_ro(int cpu
)
71 unsigned int idx
= get_cpu_gdt_ro_index(cpu
);
72 return (struct desc_struct
*)__fix_to_virt(idx
);
75 /* Provide the current read-only GDT */
76 static inline struct desc_struct
*get_current_gdt_ro(void)
78 return get_cpu_gdt_ro(smp_processor_id());
81 /* Provide the physical address of the GDT page. */
82 static inline phys_addr_t
get_cpu_gdt_paddr(unsigned int cpu
)
84 return per_cpu_ptr_to_phys(get_cpu_gdt_rw(cpu
));
89 static inline void pack_gate(gate_desc
*gate
, unsigned type
, unsigned long func
,
90 unsigned dpl
, unsigned ist
, unsigned seg
)
92 gate
->offset_low
= PTR_LOW(func
);
93 gate
->segment
= __KERNEL_CS
;
100 gate
->offset_middle
= PTR_MIDDLE(func
);
101 gate
->offset_high
= PTR_HIGH(func
);
105 static inline void pack_gate(gate_desc
*gate
, unsigned char type
,
106 unsigned long base
, unsigned dpl
, unsigned flags
,
109 gate
->a
= (seg
<< 16) | (base
& 0xffff);
110 gate
->b
= (base
& 0xffff0000) | (((0x80 | type
| (dpl
<< 5)) & 0xff) << 8);
115 static inline int desc_empty(const void *ptr
)
117 const u32
*desc
= ptr
;
119 return !(desc
[0] | desc
[1]);
122 #ifdef CONFIG_PARAVIRT
123 #include <asm/paravirt.h>
125 #define load_TR_desc() native_load_tr_desc()
126 #define load_gdt(dtr) native_load_gdt(dtr)
127 #define load_idt(dtr) native_load_idt(dtr)
128 #define load_tr(tr) asm volatile("ltr %0"::"m" (tr))
129 #define load_ldt(ldt) asm volatile("lldt %0"::"m" (ldt))
131 #define store_gdt(dtr) native_store_gdt(dtr)
132 #define store_idt(dtr) native_store_idt(dtr)
133 #define store_tr(tr) (tr = native_store_tr())
135 #define load_TLS(t, cpu) native_load_tls(t, cpu)
136 #define set_ldt native_set_ldt
138 #define write_ldt_entry(dt, entry, desc) native_write_ldt_entry(dt, entry, desc)
139 #define write_gdt_entry(dt, entry, desc, type) native_write_gdt_entry(dt, entry, desc, type)
140 #define write_idt_entry(dt, entry, g) native_write_idt_entry(dt, entry, g)
142 static inline void paravirt_alloc_ldt(struct desc_struct
*ldt
, unsigned entries
)
146 static inline void paravirt_free_ldt(struct desc_struct
*ldt
, unsigned entries
)
149 #endif /* CONFIG_PARAVIRT */
151 #define store_ldt(ldt) asm("sldt %0" : "=m"(ldt))
153 static inline void native_write_idt_entry(gate_desc
*idt
, int entry
, const gate_desc
*gate
)
155 memcpy(&idt
[entry
], gate
, sizeof(*gate
));
158 static inline void native_write_ldt_entry(struct desc_struct
*ldt
, int entry
, const void *desc
)
160 memcpy(&ldt
[entry
], desc
, 8);
164 native_write_gdt_entry(struct desc_struct
*gdt
, int entry
, const void *desc
, int type
)
169 case DESC_TSS
: size
= sizeof(tss_desc
); break;
170 case DESC_LDT
: size
= sizeof(ldt_desc
); break;
171 default: size
= sizeof(*gdt
); break;
174 memcpy(&gdt
[entry
], desc
, size
);
177 static inline void pack_descriptor(struct desc_struct
*desc
, unsigned long base
,
178 unsigned long limit
, unsigned char type
,
181 desc
->a
= ((base
& 0xffff) << 16) | (limit
& 0xffff);
182 desc
->b
= (base
& 0xff000000) | ((base
& 0xff0000) >> 16) |
183 (limit
& 0x000f0000) | ((type
& 0xff) << 8) |
184 ((flags
& 0xf) << 20);
189 static inline void set_tssldt_descriptor(void *d
, unsigned long addr
, unsigned type
, unsigned size
)
192 struct ldttss_desc64
*desc
= d
;
194 memset(desc
, 0, sizeof(*desc
));
196 desc
->limit0
= size
& 0xFFFF;
197 desc
->base0
= PTR_LOW(addr
);
198 desc
->base1
= PTR_MIDDLE(addr
) & 0xFF;
201 desc
->limit1
= (size
>> 16) & 0xF;
202 desc
->base2
= (PTR_MIDDLE(addr
) >> 8) & 0xFF;
203 desc
->base3
= PTR_HIGH(addr
);
205 pack_descriptor((struct desc_struct
*)d
, addr
, size
, 0x80 | type
, 0);
209 static inline void __set_tss_desc(unsigned cpu
, unsigned int entry
, void *addr
)
211 struct desc_struct
*d
= get_cpu_gdt_rw(cpu
);
214 set_tssldt_descriptor(&tss
, (unsigned long)addr
, DESC_TSS
,
216 write_gdt_entry(d
, entry
, &tss
, DESC_TSS
);
219 #define set_tss_desc(cpu, addr) __set_tss_desc(cpu, GDT_ENTRY_TSS, addr)
221 static inline void native_set_ldt(const void *addr
, unsigned int entries
)
223 if (likely(entries
== 0))
224 asm volatile("lldt %w0"::"q" (0));
226 unsigned cpu
= smp_processor_id();
229 set_tssldt_descriptor(&ldt
, (unsigned long)addr
, DESC_LDT
,
230 entries
* LDT_ENTRY_SIZE
- 1);
231 write_gdt_entry(get_cpu_gdt_rw(cpu
), GDT_ENTRY_LDT
,
233 asm volatile("lldt %w0"::"q" (GDT_ENTRY_LDT
*8));
237 static inline void native_load_gdt(const struct desc_ptr
*dtr
)
239 asm volatile("lgdt %0"::"m" (*dtr
));
242 static inline void native_load_idt(const struct desc_ptr
*dtr
)
244 asm volatile("lidt %0"::"m" (*dtr
));
247 static inline void native_store_gdt(struct desc_ptr
*dtr
)
249 asm volatile("sgdt %0":"=m" (*dtr
));
252 static inline void native_store_idt(struct desc_ptr
*dtr
)
254 asm volatile("sidt %0":"=m" (*dtr
));
258 * The LTR instruction marks the TSS GDT entry as busy. On 64-bit, the GDT is
259 * a read-only remapping. To prevent a page fault, the GDT is switched to the
260 * original writeable version when needed.
263 static inline void native_load_tr_desc(void)
266 int cpu
= raw_smp_processor_id();
268 struct desc_struct
*fixmap_gdt
;
270 native_store_gdt(&gdt
);
271 fixmap_gdt
= get_cpu_gdt_ro(cpu
);
274 * If the current GDT is the read-only fixmap, swap to the original
275 * writeable version. Swap back at the end.
277 if (gdt
.address
== (unsigned long)fixmap_gdt
) {
278 load_direct_gdt(cpu
);
281 asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS
*8));
283 load_fixmap_gdt(cpu
);
286 static inline void native_load_tr_desc(void)
288 asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS
*8));
292 static inline unsigned long native_store_tr(void)
296 asm volatile("str %0":"=r" (tr
));
301 static inline void native_load_tls(struct thread_struct
*t
, unsigned int cpu
)
303 struct desc_struct
*gdt
= get_cpu_gdt_rw(cpu
);
306 for (i
= 0; i
< GDT_ENTRY_TLS_ENTRIES
; i
++)
307 gdt
[GDT_ENTRY_TLS_MIN
+ i
] = t
->tls_array
[i
];
310 DECLARE_PER_CPU(bool, __tss_limit_invalid
);
312 static inline void force_reload_TR(void)
314 struct desc_struct
*d
= get_current_gdt_rw();
317 memcpy(&tss
, &d
[GDT_ENTRY_TSS
], sizeof(tss_desc
));
320 * LTR requires an available TSS, and the TSS is currently
321 * busy. Make it be available so that LTR will work.
324 write_gdt_entry(d
, GDT_ENTRY_TSS
, &tss
, DESC_TSS
);
327 this_cpu_write(__tss_limit_invalid
, false);
331 * Call this if you need the TSS limit to be correct, which should be the case
332 * if and only if you have TIF_IO_BITMAP set or you're switching to a task
333 * with TIF_IO_BITMAP set.
335 static inline void refresh_tss_limit(void)
337 DEBUG_LOCKS_WARN_ON(preemptible());
339 if (unlikely(this_cpu_read(__tss_limit_invalid
)))
344 * If you do something evil that corrupts the cached TSS limit (I'm looking
345 * at you, VMX exits), call this function.
347 * The optimization here is that the TSS limit only matters for Linux if the
348 * IO bitmap is in use. If the TSS limit gets forced to its minimum value,
349 * everything works except that IO bitmap will be ignored and all CPL 3 IO
350 * instructions will #GP, which is exactly what we want for normal tasks.
352 static inline void invalidate_tss_limit(void)
354 DEBUG_LOCKS_WARN_ON(preemptible());
356 if (unlikely(test_thread_flag(TIF_IO_BITMAP
)))
359 this_cpu_write(__tss_limit_invalid
, true);
362 /* This intentionally ignores lm, since 32-bit apps don't have that field. */
363 #define LDT_empty(info) \
364 ((info)->base_addr == 0 && \
365 (info)->limit == 0 && \
366 (info)->contents == 0 && \
367 (info)->read_exec_only == 1 && \
368 (info)->seg_32bit == 0 && \
369 (info)->limit_in_pages == 0 && \
370 (info)->seg_not_present == 1 && \
371 (info)->useable == 0)
373 /* Lots of programs expect an all-zero user_desc to mean "no segment at all". */
374 static inline bool LDT_zero(const struct user_desc
*info
)
376 return (info
->base_addr
== 0 &&
378 info
->contents
== 0 &&
379 info
->read_exec_only
== 0 &&
380 info
->seg_32bit
== 0 &&
381 info
->limit_in_pages
== 0 &&
382 info
->seg_not_present
== 0 &&
386 static inline void clear_LDT(void)
391 static inline unsigned long get_desc_base(const struct desc_struct
*desc
)
393 return (unsigned)(desc
->base0
| ((desc
->base1
) << 16) | ((desc
->base2
) << 24));
396 static inline void set_desc_base(struct desc_struct
*desc
, unsigned long base
)
398 desc
->base0
= base
& 0xffff;
399 desc
->base1
= (base
>> 16) & 0xff;
400 desc
->base2
= (base
>> 24) & 0xff;
403 static inline unsigned long get_desc_limit(const struct desc_struct
*desc
)
405 return desc
->limit0
| (desc
->limit
<< 16);
408 static inline void set_desc_limit(struct desc_struct
*desc
, unsigned long limit
)
410 desc
->limit0
= limit
& 0xffff;
411 desc
->limit
= (limit
>> 16) & 0xf;
415 static inline void set_nmi_gate(int gate
, void *addr
)
419 pack_gate(&s
, GATE_INTERRUPT
, (unsigned long)addr
, 0, 0, __KERNEL_CS
);
420 write_idt_entry(debug_idt_table
, gate
, &s
);
424 #ifdef CONFIG_TRACING
425 extern struct desc_ptr trace_idt_descr
;
426 extern gate_desc trace_idt_table
[];
427 static inline void write_trace_idt_entry(int entry
, const gate_desc
*gate
)
429 write_idt_entry(trace_idt_table
, entry
, gate
);
432 static inline void _trace_set_gate(int gate
, unsigned type
, void *addr
,
433 unsigned dpl
, unsigned ist
, unsigned seg
)
437 pack_gate(&s
, type
, (unsigned long)addr
, dpl
, ist
, seg
);
439 * does not need to be atomic because it is only done once at
442 write_trace_idt_entry(gate
, &s
);
445 static inline void write_trace_idt_entry(int entry
, const gate_desc
*gate
)
449 #define _trace_set_gate(gate, type, addr, dpl, ist, seg)
452 static inline void _set_gate(int gate
, unsigned type
, void *addr
,
453 unsigned dpl
, unsigned ist
, unsigned seg
)
457 pack_gate(&s
, type
, (unsigned long)addr
, dpl
, ist
, seg
);
459 * does not need to be atomic because it is only done once at
462 write_idt_entry(idt_table
, gate
, &s
);
463 write_trace_idt_entry(gate
, &s
);
467 * This needs to use 'idt_table' rather than 'idt', and
468 * thus use the _nonmapped_ version of the IDT, as the
469 * Pentium F0 0F bugfix can have resulted in the mapped
470 * IDT being write-protected.
472 #define set_intr_gate_notrace(n, addr) \
474 BUG_ON((unsigned)n > 0xFF); \
475 _set_gate(n, GATE_INTERRUPT, (void *)addr, 0, 0, \
479 #define set_intr_gate(n, addr) \
481 set_intr_gate_notrace(n, addr); \
482 _trace_set_gate(n, GATE_INTERRUPT, (void *)trace_##addr,\
483 0, 0, __KERNEL_CS); \
486 extern int first_system_vector
;
487 /* used_vectors is BITMAP for irq is not managed by percpu vector_irq */
488 extern unsigned long used_vectors
[];
490 static inline void alloc_system_vector(int vector
)
492 if (!test_bit(vector
, used_vectors
)) {
493 set_bit(vector
, used_vectors
);
494 if (first_system_vector
> vector
)
495 first_system_vector
= vector
;
501 #define alloc_intr_gate(n, addr) \
503 alloc_system_vector(n); \
504 set_intr_gate(n, addr); \
508 * This routine sets up an interrupt gate at directory privilege level 3.
510 static inline void set_system_intr_gate(unsigned int n
, void *addr
)
512 BUG_ON((unsigned)n
> 0xFF);
513 _set_gate(n
, GATE_INTERRUPT
, addr
, 0x3, 0, __KERNEL_CS
);
516 static inline void set_system_trap_gate(unsigned int n
, void *addr
)
518 BUG_ON((unsigned)n
> 0xFF);
519 _set_gate(n
, GATE_TRAP
, addr
, 0x3, 0, __KERNEL_CS
);
522 static inline void set_trap_gate(unsigned int n
, void *addr
)
524 BUG_ON((unsigned)n
> 0xFF);
525 _set_gate(n
, GATE_TRAP
, addr
, 0, 0, __KERNEL_CS
);
528 static inline void set_task_gate(unsigned int n
, unsigned int gdt_entry
)
530 BUG_ON((unsigned)n
> 0xFF);
531 _set_gate(n
, GATE_TASK
, (void *)0, 0, 0, (gdt_entry
<<3));
534 static inline void set_intr_gate_ist(int n
, void *addr
, unsigned ist
)
536 BUG_ON((unsigned)n
> 0xFF);
537 _set_gate(n
, GATE_INTERRUPT
, addr
, 0, ist
, __KERNEL_CS
);
540 static inline void set_system_intr_gate_ist(int n
, void *addr
, unsigned ist
)
542 BUG_ON((unsigned)n
> 0xFF);
543 _set_gate(n
, GATE_INTERRUPT
, addr
, 0x3, ist
, __KERNEL_CS
);
547 DECLARE_PER_CPU(u32
, debug_idt_ctr
);
548 static inline bool is_debug_idt_enabled(void)
550 if (this_cpu_read(debug_idt_ctr
))
556 static inline void load_debug_idt(void)
558 load_idt((const struct desc_ptr
*)&debug_idt_descr
);
561 static inline bool is_debug_idt_enabled(void)
566 static inline void load_debug_idt(void)
571 #ifdef CONFIG_TRACING
572 extern atomic_t trace_idt_ctr
;
573 static inline bool is_trace_idt_enabled(void)
575 if (atomic_read(&trace_idt_ctr
))
581 static inline void load_trace_idt(void)
583 load_idt((const struct desc_ptr
*)&trace_idt_descr
);
586 static inline bool is_trace_idt_enabled(void)
591 static inline void load_trace_idt(void)
597 * The load_current_idt() must be called with interrupts disabled
598 * to avoid races. That way the IDT will always be set back to the expected
599 * descriptor. It's also called when a CPU is being initialized, and
600 * that doesn't need to disable interrupts, as nothing should be
601 * bothering the CPU then.
603 static inline void load_current_idt(void)
605 if (is_debug_idt_enabled())
607 else if (is_trace_idt_enabled())
610 load_idt((const struct desc_ptr
*)&idt_descr
);
612 #endif /* _ASM_X86_DESC_H */