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>
10 #include <asm/cpu_entry_area.h>
12 #include <linux/smp.h>
13 #include <linux/percpu.h>
15 static inline void fill_ldt(struct desc_struct
*desc
, const struct user_desc
*info
)
17 desc
->limit0
= info
->limit
& 0x0ffff;
19 desc
->base0
= (info
->base_addr
& 0x0000ffff);
20 desc
->base1
= (info
->base_addr
& 0x00ff0000) >> 16;
22 desc
->type
= (info
->read_exec_only
^ 1) << 1;
23 desc
->type
|= info
->contents
<< 2;
27 desc
->p
= info
->seg_not_present
^ 1;
28 desc
->limit1
= (info
->limit
& 0xf0000) >> 16;
29 desc
->avl
= info
->useable
;
30 desc
->d
= info
->seg_32bit
;
31 desc
->g
= info
->limit_in_pages
;
33 desc
->base2
= (info
->base_addr
& 0xff000000) >> 24;
35 * Don't allow setting of the lm bit. It would confuse
36 * user_64bit_mode and would get overridden by sysret anyway.
41 extern struct desc_ptr idt_descr
;
42 extern gate_desc idt_table
[];
43 extern const struct desc_ptr debug_idt_descr
;
44 extern gate_desc debug_idt_table
[];
47 struct desc_struct gdt
[GDT_ENTRIES
];
48 } __attribute__((aligned(PAGE_SIZE
)));
50 DECLARE_PER_CPU_PAGE_ALIGNED(struct gdt_page
, gdt_page
);
52 /* Provide the original GDT */
53 static inline struct desc_struct
*get_cpu_gdt_rw(unsigned int cpu
)
55 return per_cpu(gdt_page
, cpu
).gdt
;
58 /* Provide the current original GDT */
59 static inline struct desc_struct
*get_current_gdt_rw(void)
61 return this_cpu_ptr(&gdt_page
)->gdt
;
64 /* Provide the fixmap address of the remapped GDT */
65 static inline struct desc_struct
*get_cpu_gdt_ro(int cpu
)
67 return (struct desc_struct
*)&get_cpu_entry_area(cpu
)->gdt
;
70 /* Provide the current read-only GDT */
71 static inline struct desc_struct
*get_current_gdt_ro(void)
73 return get_cpu_gdt_ro(smp_processor_id());
76 /* Provide the physical address of the GDT page. */
77 static inline phys_addr_t
get_cpu_gdt_paddr(unsigned int cpu
)
79 return per_cpu_ptr_to_phys(get_cpu_gdt_rw(cpu
));
82 static inline void pack_gate(gate_desc
*gate
, unsigned type
, unsigned long func
,
83 unsigned dpl
, unsigned ist
, unsigned seg
)
85 gate
->offset_low
= (u16
) func
;
89 gate
->bits
.type
= type
;
90 gate
->offset_middle
= (u16
) (func
>> 16);
92 gate
->segment
= __KERNEL_CS
;
95 gate
->offset_high
= (u32
) (func
>> 32);
102 static inline int desc_empty(const void *ptr
)
104 const u32
*desc
= ptr
;
106 return !(desc
[0] | desc
[1]);
109 #ifdef CONFIG_PARAVIRT
110 #include <asm/paravirt.h>
112 #define load_TR_desc() native_load_tr_desc()
113 #define load_gdt(dtr) native_load_gdt(dtr)
114 #define load_idt(dtr) native_load_idt(dtr)
115 #define load_tr(tr) asm volatile("ltr %0"::"m" (tr))
116 #define load_ldt(ldt) asm volatile("lldt %0"::"m" (ldt))
118 #define store_gdt(dtr) native_store_gdt(dtr)
119 #define store_tr(tr) (tr = native_store_tr())
121 #define load_TLS(t, cpu) native_load_tls(t, cpu)
122 #define set_ldt native_set_ldt
124 #define write_ldt_entry(dt, entry, desc) native_write_ldt_entry(dt, entry, desc)
125 #define write_gdt_entry(dt, entry, desc, type) native_write_gdt_entry(dt, entry, desc, type)
126 #define write_idt_entry(dt, entry, g) native_write_idt_entry(dt, entry, g)
128 static inline void paravirt_alloc_ldt(struct desc_struct
*ldt
, unsigned entries
)
132 static inline void paravirt_free_ldt(struct desc_struct
*ldt
, unsigned entries
)
135 #endif /* CONFIG_PARAVIRT */
137 #define store_ldt(ldt) asm("sldt %0" : "=m"(ldt))
139 static inline void native_write_idt_entry(gate_desc
*idt
, int entry
, const gate_desc
*gate
)
141 memcpy(&idt
[entry
], gate
, sizeof(*gate
));
144 static inline void native_write_ldt_entry(struct desc_struct
*ldt
, int entry
, const void *desc
)
146 memcpy(&ldt
[entry
], desc
, 8);
150 native_write_gdt_entry(struct desc_struct
*gdt
, int entry
, const void *desc
, int type
)
155 case DESC_TSS
: size
= sizeof(tss_desc
); break;
156 case DESC_LDT
: size
= sizeof(ldt_desc
); break;
157 default: size
= sizeof(*gdt
); break;
160 memcpy(&gdt
[entry
], desc
, size
);
163 static inline void set_tssldt_descriptor(void *d
, unsigned long addr
,
164 unsigned type
, unsigned size
)
166 struct ldttss_desc
*desc
= d
;
168 memset(desc
, 0, sizeof(*desc
));
170 desc
->limit0
= (u16
) size
;
171 desc
->base0
= (u16
) addr
;
172 desc
->base1
= (addr
>> 16) & 0xFF;
175 desc
->limit1
= (size
>> 16) & 0xF;
176 desc
->base2
= (addr
>> 24) & 0xFF;
178 desc
->base3
= (u32
) (addr
>> 32);
182 static inline void __set_tss_desc(unsigned cpu
, unsigned int entry
, struct x86_hw_tss
*addr
)
184 struct desc_struct
*d
= get_cpu_gdt_rw(cpu
);
187 set_tssldt_descriptor(&tss
, (unsigned long)addr
, DESC_TSS
,
189 write_gdt_entry(d
, entry
, &tss
, DESC_TSS
);
192 #define set_tss_desc(cpu, addr) __set_tss_desc(cpu, GDT_ENTRY_TSS, addr)
194 static inline void native_set_ldt(const void *addr
, unsigned int entries
)
196 if (likely(entries
== 0))
197 asm volatile("lldt %w0"::"q" (0));
199 unsigned cpu
= smp_processor_id();
202 set_tssldt_descriptor(&ldt
, (unsigned long)addr
, DESC_LDT
,
203 entries
* LDT_ENTRY_SIZE
- 1);
204 write_gdt_entry(get_cpu_gdt_rw(cpu
), GDT_ENTRY_LDT
,
206 asm volatile("lldt %w0"::"q" (GDT_ENTRY_LDT
*8));
210 static inline void native_load_gdt(const struct desc_ptr
*dtr
)
212 asm volatile("lgdt %0"::"m" (*dtr
));
215 static inline void native_load_idt(const struct desc_ptr
*dtr
)
217 asm volatile("lidt %0"::"m" (*dtr
));
220 static inline void native_store_gdt(struct desc_ptr
*dtr
)
222 asm volatile("sgdt %0":"=m" (*dtr
));
225 static inline void store_idt(struct desc_ptr
*dtr
)
227 asm volatile("sidt %0":"=m" (*dtr
));
231 * The LTR instruction marks the TSS GDT entry as busy. On 64-bit, the GDT is
232 * a read-only remapping. To prevent a page fault, the GDT is switched to the
233 * original writeable version when needed.
236 static inline void native_load_tr_desc(void)
239 int cpu
= raw_smp_processor_id();
241 struct desc_struct
*fixmap_gdt
;
243 native_store_gdt(&gdt
);
244 fixmap_gdt
= get_cpu_gdt_ro(cpu
);
247 * If the current GDT is the read-only fixmap, swap to the original
248 * writeable version. Swap back at the end.
250 if (gdt
.address
== (unsigned long)fixmap_gdt
) {
251 load_direct_gdt(cpu
);
254 asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS
*8));
256 load_fixmap_gdt(cpu
);
259 static inline void native_load_tr_desc(void)
261 asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS
*8));
265 static inline unsigned long native_store_tr(void)
269 asm volatile("str %0":"=r" (tr
));
274 static inline void native_load_tls(struct thread_struct
*t
, unsigned int cpu
)
276 struct desc_struct
*gdt
= get_cpu_gdt_rw(cpu
);
279 for (i
= 0; i
< GDT_ENTRY_TLS_ENTRIES
; i
++)
280 gdt
[GDT_ENTRY_TLS_MIN
+ i
] = t
->tls_array
[i
];
283 DECLARE_PER_CPU(bool, __tss_limit_invalid
);
285 static inline void force_reload_TR(void)
287 struct desc_struct
*d
= get_current_gdt_rw();
290 memcpy(&tss
, &d
[GDT_ENTRY_TSS
], sizeof(tss_desc
));
293 * LTR requires an available TSS, and the TSS is currently
294 * busy. Make it be available so that LTR will work.
297 write_gdt_entry(d
, GDT_ENTRY_TSS
, &tss
, DESC_TSS
);
300 this_cpu_write(__tss_limit_invalid
, false);
304 * Call this if you need the TSS limit to be correct, which should be the case
305 * if and only if you have TIF_IO_BITMAP set or you're switching to a task
306 * with TIF_IO_BITMAP set.
308 static inline void refresh_tss_limit(void)
310 DEBUG_LOCKS_WARN_ON(preemptible());
312 if (unlikely(this_cpu_read(__tss_limit_invalid
)))
317 * If you do something evil that corrupts the cached TSS limit (I'm looking
318 * at you, VMX exits), call this function.
320 * The optimization here is that the TSS limit only matters for Linux if the
321 * IO bitmap is in use. If the TSS limit gets forced to its minimum value,
322 * everything works except that IO bitmap will be ignored and all CPL 3 IO
323 * instructions will #GP, which is exactly what we want for normal tasks.
325 static inline void invalidate_tss_limit(void)
327 DEBUG_LOCKS_WARN_ON(preemptible());
329 if (unlikely(test_thread_flag(TIF_IO_BITMAP
)))
332 this_cpu_write(__tss_limit_invalid
, true);
335 /* This intentionally ignores lm, since 32-bit apps don't have that field. */
336 #define LDT_empty(info) \
337 ((info)->base_addr == 0 && \
338 (info)->limit == 0 && \
339 (info)->contents == 0 && \
340 (info)->read_exec_only == 1 && \
341 (info)->seg_32bit == 0 && \
342 (info)->limit_in_pages == 0 && \
343 (info)->seg_not_present == 1 && \
344 (info)->useable == 0)
346 /* Lots of programs expect an all-zero user_desc to mean "no segment at all". */
347 static inline bool LDT_zero(const struct user_desc
*info
)
349 return (info
->base_addr
== 0 &&
351 info
->contents
== 0 &&
352 info
->read_exec_only
== 0 &&
353 info
->seg_32bit
== 0 &&
354 info
->limit_in_pages
== 0 &&
355 info
->seg_not_present
== 0 &&
359 static inline void clear_LDT(void)
364 static inline unsigned long get_desc_base(const struct desc_struct
*desc
)
366 return (unsigned)(desc
->base0
| ((desc
->base1
) << 16) | ((desc
->base2
) << 24));
369 static inline void set_desc_base(struct desc_struct
*desc
, unsigned long base
)
371 desc
->base0
= base
& 0xffff;
372 desc
->base1
= (base
>> 16) & 0xff;
373 desc
->base2
= (base
>> 24) & 0xff;
376 static inline unsigned long get_desc_limit(const struct desc_struct
*desc
)
378 return desc
->limit0
| (desc
->limit1
<< 16);
381 static inline void set_desc_limit(struct desc_struct
*desc
, unsigned long limit
)
383 desc
->limit0
= limit
& 0xffff;
384 desc
->limit1
= (limit
>> 16) & 0xf;
387 void update_intr_gate(unsigned int n
, const void *addr
);
388 void alloc_intr_gate(unsigned int n
, const void *addr
);
390 extern unsigned long system_vectors
[];
393 DECLARE_PER_CPU(u32
, debug_idt_ctr
);
394 static inline bool is_debug_idt_enabled(void)
396 if (this_cpu_read(debug_idt_ctr
))
402 static inline void load_debug_idt(void)
404 load_idt((const struct desc_ptr
*)&debug_idt_descr
);
407 static inline bool is_debug_idt_enabled(void)
412 static inline void load_debug_idt(void)
418 * The load_current_idt() must be called with interrupts disabled
419 * to avoid races. That way the IDT will always be set back to the expected
420 * descriptor. It's also called when a CPU is being initialized, and
421 * that doesn't need to disable interrupts, as nothing should be
422 * bothering the CPU then.
424 static inline void load_current_idt(void)
426 if (is_debug_idt_enabled())
429 load_idt((const struct desc_ptr
*)&idt_descr
);
432 extern void idt_setup_early_handler(void);
433 extern void idt_setup_early_traps(void);
434 extern void idt_setup_traps(void);
435 extern void idt_setup_apic_and_irq_gates(void);
438 extern void idt_setup_early_pf(void);
439 extern void idt_setup_ist_traps(void);
440 extern void idt_setup_debugidt_traps(void);
442 static inline void idt_setup_early_pf(void) { }
443 static inline void idt_setup_ist_traps(void) { }
444 static inline void idt_setup_debugidt_traps(void) { }
447 extern void idt_invalidate(void *addr
);
449 #endif /* _ASM_X86_DESC_H */