1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_DESC_H
3 #define _ASM_X86_DESC_H
5 #include <asm/desc_defs.h>
8 #include <asm/fixmap.h>
9 #include <asm/irq_vectors.h>
11 #include <linux/smp.h>
12 #include <linux/percpu.h>
14 static inline void fill_ldt(struct desc_struct
*desc
, const struct user_desc
*info
)
16 desc
->limit0
= info
->limit
& 0x0ffff;
18 desc
->base0
= (info
->base_addr
& 0x0000ffff);
19 desc
->base1
= (info
->base_addr
& 0x00ff0000) >> 16;
21 desc
->type
= (info
->read_exec_only
^ 1) << 1;
22 desc
->type
|= info
->contents
<< 2;
26 desc
->p
= info
->seg_not_present
^ 1;
27 desc
->limit1
= (info
->limit
& 0xf0000) >> 16;
28 desc
->avl
= info
->useable
;
29 desc
->d
= info
->seg_32bit
;
30 desc
->g
= info
->limit_in_pages
;
32 desc
->base2
= (info
->base_addr
& 0xff000000) >> 24;
34 * Don't allow setting of the lm bit. It would confuse
35 * user_64bit_mode and would get overridden by sysret anyway.
40 extern struct desc_ptr idt_descr
;
41 extern gate_desc idt_table
[];
42 extern const struct desc_ptr debug_idt_descr
;
43 extern gate_desc debug_idt_table
[];
46 struct desc_struct gdt
[GDT_ENTRIES
];
47 } __attribute__((aligned(PAGE_SIZE
)));
49 DECLARE_PER_CPU_PAGE_ALIGNED(struct gdt_page
, gdt_page
);
51 /* Provide the original GDT */
52 static inline struct desc_struct
*get_cpu_gdt_rw(unsigned int cpu
)
54 return per_cpu(gdt_page
, cpu
).gdt
;
57 /* Provide the current original GDT */
58 static inline struct desc_struct
*get_current_gdt_rw(void)
60 return this_cpu_ptr(&gdt_page
)->gdt
;
63 /* Provide the fixmap address of the remapped GDT */
64 static inline struct desc_struct
*get_cpu_gdt_ro(int cpu
)
66 return (struct desc_struct
*)&get_cpu_entry_area(cpu
)->gdt
;
69 /* Provide the current read-only GDT */
70 static inline struct desc_struct
*get_current_gdt_ro(void)
72 return get_cpu_gdt_ro(smp_processor_id());
75 /* Provide the physical address of the GDT page. */
76 static inline phys_addr_t
get_cpu_gdt_paddr(unsigned int cpu
)
78 return per_cpu_ptr_to_phys(get_cpu_gdt_rw(cpu
));
81 static inline void pack_gate(gate_desc
*gate
, unsigned type
, unsigned long func
,
82 unsigned dpl
, unsigned ist
, unsigned seg
)
84 gate
->offset_low
= (u16
) func
;
88 gate
->bits
.type
= type
;
89 gate
->offset_middle
= (u16
) (func
>> 16);
91 gate
->segment
= __KERNEL_CS
;
94 gate
->offset_high
= (u32
) (func
>> 32);
101 static inline int desc_empty(const void *ptr
)
103 const u32
*desc
= ptr
;
105 return !(desc
[0] | desc
[1]);
108 #ifdef CONFIG_PARAVIRT
109 #include <asm/paravirt.h>
111 #define load_TR_desc() native_load_tr_desc()
112 #define load_gdt(dtr) native_load_gdt(dtr)
113 #define load_idt(dtr) native_load_idt(dtr)
114 #define load_tr(tr) asm volatile("ltr %0"::"m" (tr))
115 #define load_ldt(ldt) asm volatile("lldt %0"::"m" (ldt))
117 #define store_gdt(dtr) native_store_gdt(dtr)
118 #define store_tr(tr) (tr = native_store_tr())
120 #define load_TLS(t, cpu) native_load_tls(t, cpu)
121 #define set_ldt native_set_ldt
123 #define write_ldt_entry(dt, entry, desc) native_write_ldt_entry(dt, entry, desc)
124 #define write_gdt_entry(dt, entry, desc, type) native_write_gdt_entry(dt, entry, desc, type)
125 #define write_idt_entry(dt, entry, g) native_write_idt_entry(dt, entry, g)
127 static inline void paravirt_alloc_ldt(struct desc_struct
*ldt
, unsigned entries
)
131 static inline void paravirt_free_ldt(struct desc_struct
*ldt
, unsigned entries
)
134 #endif /* CONFIG_PARAVIRT */
136 #define store_ldt(ldt) asm("sldt %0" : "=m"(ldt))
138 static inline void native_write_idt_entry(gate_desc
*idt
, int entry
, const gate_desc
*gate
)
140 memcpy(&idt
[entry
], gate
, sizeof(*gate
));
143 static inline void native_write_ldt_entry(struct desc_struct
*ldt
, int entry
, const void *desc
)
145 memcpy(&ldt
[entry
], desc
, 8);
149 native_write_gdt_entry(struct desc_struct
*gdt
, int entry
, const void *desc
, int type
)
154 case DESC_TSS
: size
= sizeof(tss_desc
); break;
155 case DESC_LDT
: size
= sizeof(ldt_desc
); break;
156 default: size
= sizeof(*gdt
); break;
159 memcpy(&gdt
[entry
], desc
, size
);
162 static inline void set_tssldt_descriptor(void *d
, unsigned long addr
,
163 unsigned type
, unsigned size
)
165 struct ldttss_desc
*desc
= d
;
167 memset(desc
, 0, sizeof(*desc
));
169 desc
->limit0
= (u16
) size
;
170 desc
->base0
= (u16
) addr
;
171 desc
->base1
= (addr
>> 16) & 0xFF;
174 desc
->limit1
= (size
>> 16) & 0xF;
175 desc
->base2
= (addr
>> 24) & 0xFF;
177 desc
->base3
= (u32
) (addr
>> 32);
181 static inline void __set_tss_desc(unsigned cpu
, unsigned int entry
, struct x86_hw_tss
*addr
)
183 struct desc_struct
*d
= get_cpu_gdt_rw(cpu
);
186 set_tssldt_descriptor(&tss
, (unsigned long)addr
, DESC_TSS
,
188 write_gdt_entry(d
, entry
, &tss
, DESC_TSS
);
191 #define set_tss_desc(cpu, addr) __set_tss_desc(cpu, GDT_ENTRY_TSS, addr)
193 static inline void native_set_ldt(const void *addr
, unsigned int entries
)
195 if (likely(entries
== 0))
196 asm volatile("lldt %w0"::"q" (0));
198 unsigned cpu
= smp_processor_id();
201 set_tssldt_descriptor(&ldt
, (unsigned long)addr
, DESC_LDT
,
202 entries
* LDT_ENTRY_SIZE
- 1);
203 write_gdt_entry(get_cpu_gdt_rw(cpu
), GDT_ENTRY_LDT
,
205 asm volatile("lldt %w0"::"q" (GDT_ENTRY_LDT
*8));
209 static inline void native_load_gdt(const struct desc_ptr
*dtr
)
211 asm volatile("lgdt %0"::"m" (*dtr
));
214 static inline void native_load_idt(const struct desc_ptr
*dtr
)
216 asm volatile("lidt %0"::"m" (*dtr
));
219 static inline void native_store_gdt(struct desc_ptr
*dtr
)
221 asm volatile("sgdt %0":"=m" (*dtr
));
224 static inline void store_idt(struct desc_ptr
*dtr
)
226 asm volatile("sidt %0":"=m" (*dtr
));
230 * The LTR instruction marks the TSS GDT entry as busy. On 64-bit, the GDT is
231 * a read-only remapping. To prevent a page fault, the GDT is switched to the
232 * original writeable version when needed.
235 static inline void native_load_tr_desc(void)
238 int cpu
= raw_smp_processor_id();
240 struct desc_struct
*fixmap_gdt
;
242 native_store_gdt(&gdt
);
243 fixmap_gdt
= get_cpu_gdt_ro(cpu
);
246 * If the current GDT is the read-only fixmap, swap to the original
247 * writeable version. Swap back at the end.
249 if (gdt
.address
== (unsigned long)fixmap_gdt
) {
250 load_direct_gdt(cpu
);
253 asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS
*8));
255 load_fixmap_gdt(cpu
);
258 static inline void native_load_tr_desc(void)
260 asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS
*8));
264 static inline unsigned long native_store_tr(void)
268 asm volatile("str %0":"=r" (tr
));
273 static inline void native_load_tls(struct thread_struct
*t
, unsigned int cpu
)
275 struct desc_struct
*gdt
= get_cpu_gdt_rw(cpu
);
278 for (i
= 0; i
< GDT_ENTRY_TLS_ENTRIES
; i
++)
279 gdt
[GDT_ENTRY_TLS_MIN
+ i
] = t
->tls_array
[i
];
282 DECLARE_PER_CPU(bool, __tss_limit_invalid
);
284 static inline void force_reload_TR(void)
286 struct desc_struct
*d
= get_current_gdt_rw();
289 memcpy(&tss
, &d
[GDT_ENTRY_TSS
], sizeof(tss_desc
));
292 * LTR requires an available TSS, and the TSS is currently
293 * busy. Make it be available so that LTR will work.
296 write_gdt_entry(d
, GDT_ENTRY_TSS
, &tss
, DESC_TSS
);
299 this_cpu_write(__tss_limit_invalid
, false);
303 * Call this if you need the TSS limit to be correct, which should be the case
304 * if and only if you have TIF_IO_BITMAP set or you're switching to a task
305 * with TIF_IO_BITMAP set.
307 static inline void refresh_tss_limit(void)
309 DEBUG_LOCKS_WARN_ON(preemptible());
311 if (unlikely(this_cpu_read(__tss_limit_invalid
)))
316 * If you do something evil that corrupts the cached TSS limit (I'm looking
317 * at you, VMX exits), call this function.
319 * The optimization here is that the TSS limit only matters for Linux if the
320 * IO bitmap is in use. If the TSS limit gets forced to its minimum value,
321 * everything works except that IO bitmap will be ignored and all CPL 3 IO
322 * instructions will #GP, which is exactly what we want for normal tasks.
324 static inline void invalidate_tss_limit(void)
326 DEBUG_LOCKS_WARN_ON(preemptible());
328 if (unlikely(test_thread_flag(TIF_IO_BITMAP
)))
331 this_cpu_write(__tss_limit_invalid
, true);
334 /* This intentionally ignores lm, since 32-bit apps don't have that field. */
335 #define LDT_empty(info) \
336 ((info)->base_addr == 0 && \
337 (info)->limit == 0 && \
338 (info)->contents == 0 && \
339 (info)->read_exec_only == 1 && \
340 (info)->seg_32bit == 0 && \
341 (info)->limit_in_pages == 0 && \
342 (info)->seg_not_present == 1 && \
343 (info)->useable == 0)
345 /* Lots of programs expect an all-zero user_desc to mean "no segment at all". */
346 static inline bool LDT_zero(const struct user_desc
*info
)
348 return (info
->base_addr
== 0 &&
350 info
->contents
== 0 &&
351 info
->read_exec_only
== 0 &&
352 info
->seg_32bit
== 0 &&
353 info
->limit_in_pages
== 0 &&
354 info
->seg_not_present
== 0 &&
358 static inline void clear_LDT(void)
363 static inline unsigned long get_desc_base(const struct desc_struct
*desc
)
365 return (unsigned)(desc
->base0
| ((desc
->base1
) << 16) | ((desc
->base2
) << 24));
368 static inline void set_desc_base(struct desc_struct
*desc
, unsigned long base
)
370 desc
->base0
= base
& 0xffff;
371 desc
->base1
= (base
>> 16) & 0xff;
372 desc
->base2
= (base
>> 24) & 0xff;
375 static inline unsigned long get_desc_limit(const struct desc_struct
*desc
)
377 return desc
->limit0
| (desc
->limit1
<< 16);
380 static inline void set_desc_limit(struct desc_struct
*desc
, unsigned long limit
)
382 desc
->limit0
= limit
& 0xffff;
383 desc
->limit1
= (limit
>> 16) & 0xf;
386 void update_intr_gate(unsigned int n
, const void *addr
);
387 void alloc_intr_gate(unsigned int n
, const void *addr
);
389 extern unsigned long used_vectors
[];
392 DECLARE_PER_CPU(u32
, debug_idt_ctr
);
393 static inline bool is_debug_idt_enabled(void)
395 if (this_cpu_read(debug_idt_ctr
))
401 static inline void load_debug_idt(void)
403 load_idt((const struct desc_ptr
*)&debug_idt_descr
);
406 static inline bool is_debug_idt_enabled(void)
411 static inline void load_debug_idt(void)
417 * The load_current_idt() must be called with interrupts disabled
418 * to avoid races. That way the IDT will always be set back to the expected
419 * descriptor. It's also called when a CPU is being initialized, and
420 * that doesn't need to disable interrupts, as nothing should be
421 * bothering the CPU then.
423 static inline void load_current_idt(void)
425 if (is_debug_idt_enabled())
428 load_idt((const struct desc_ptr
*)&idt_descr
);
431 extern void idt_setup_early_handler(void);
432 extern void idt_setup_early_traps(void);
433 extern void idt_setup_traps(void);
434 extern void idt_setup_apic_and_irq_gates(void);
437 extern void idt_setup_early_pf(void);
438 extern void idt_setup_ist_traps(void);
439 extern void idt_setup_debugidt_traps(void);
441 static inline void idt_setup_early_pf(void) { }
442 static inline void idt_setup_ist_traps(void) { }
443 static inline void idt_setup_debugidt_traps(void) { }
446 extern void idt_invalidate(void *addr
);
448 #endif /* _ASM_X86_DESC_H */