blob: a7f36ab1c07d484b0061099db52003207427697f [file] [log] [blame]
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001#ifndef _ASM_X86_DESC_H
2#define _ASM_X86_DESC_H
Glauber de Oliveira Costa80fbb692008-01-30 13:31:13 +01003
Glauber de Oliveira Costa80fbb692008-01-30 13:31:13 +01004#include <asm/desc_defs.h>
5#include <asm/ldt.h>
Glauber de Oliveira Costa881c2972008-01-30 13:31:14 +01006#include <asm/mmu.h>
Thomas Garnier69218e42017-03-14 10:05:07 -07007#include <asm/fixmap.h>
Thomas Gleixner05161b92017-08-28 08:47:18 +02008#include <asm/irq_vectors.h>
Ingo Molnar9a3865b2011-05-27 09:29:32 +02009
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +010010#include <linux/smp.h>
Alex Shic6ae41e2012-05-11 15:35:27 +080011#include <linux/percpu.h>
Glauber de Oliveira Costa80fbb692008-01-30 13:31:13 +010012
Ingo Molnar9a3865b2011-05-27 09:29:32 +020013static inline void fill_ldt(struct desc_struct *desc, const struct user_desc *info)
Glauber de Oliveira Costa80fbb692008-01-30 13:31:13 +010014{
Ingo Molnar9a3865b2011-05-27 09:29:32 +020015 desc->limit0 = info->limit & 0x0ffff;
Glauber de Oliveira Costa80fbb692008-01-30 13:31:13 +010016
Ingo Molnar9a3865b2011-05-27 09:29:32 +020017 desc->base0 = (info->base_addr & 0x0000ffff);
18 desc->base1 = (info->base_addr & 0x00ff0000) >> 16;
19
20 desc->type = (info->read_exec_only ^ 1) << 1;
21 desc->type |= info->contents << 2;
22
23 desc->s = 1;
24 desc->dpl = 0x3;
25 desc->p = info->seg_not_present ^ 1;
26 desc->limit = (info->limit & 0xf0000) >> 16;
27 desc->avl = info->useable;
28 desc->d = info->seg_32bit;
29 desc->g = info->limit_in_pages;
30
31 desc->base2 = (info->base_addr & 0xff000000) >> 24;
Jeremy Fitzhardinge64f53a02008-07-27 08:42:32 -070032 /*
Andy Lutomirski318f5a22011-08-03 09:31:53 -040033 * Don't allow setting of the lm bit. It would confuse
34 * user_64bit_mode and would get overridden by sysret anyway.
Jeremy Fitzhardinge64f53a02008-07-27 08:42:32 -070035 */
Ingo Molnar9a3865b2011-05-27 09:29:32 +020036 desc->l = 0;
Glauber de Oliveira Costa80fbb692008-01-30 13:31:13 +010037}
38
Glauber de Oliveira Costa881c2972008-01-30 13:31:14 +010039extern struct desc_ptr idt_descr;
40extern gate_desc idt_table[];
Kees Cook404f6aa2016-08-08 16:29:06 -070041extern const struct desc_ptr debug_idt_descr;
Seiji Aguchi629f4f92013-06-20 11:45:44 -040042extern gate_desc debug_idt_table[];
Glauber de Oliveira Costa80fbb692008-01-30 13:31:13 +010043
Glauber Costaa9390982008-05-28 16:19:53 -070044struct gdt_page {
45 struct desc_struct gdt[GDT_ENTRIES];
46} __attribute__((aligned(PAGE_SIZE)));
Ingo Molnar9a3865b2011-05-27 09:29:32 +020047
David Howells9b8de742009-04-21 23:00:24 +010048DECLARE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page);
Glauber Costaa9390982008-05-28 16:19:53 -070049
Thomas Garnier69218e42017-03-14 10:05:07 -070050/* Provide the original GDT */
51static inline struct desc_struct *get_cpu_gdt_rw(unsigned int cpu)
Glauber Costaa9390982008-05-28 16:19:53 -070052{
53 return per_cpu(gdt_page, cpu).gdt;
54}
55
Thomas Garnier69218e42017-03-14 10:05:07 -070056/* Provide the current original GDT */
57static inline struct desc_struct *get_current_gdt_rw(void)
58{
59 return this_cpu_ptr(&gdt_page)->gdt;
60}
61
Thomas Garnier69218e42017-03-14 10:05:07 -070062/* Get the fixmap index for a specific processor */
63static inline unsigned int get_cpu_gdt_ro_index(int cpu)
64{
65 return FIX_GDT_REMAP_BEGIN + cpu;
66}
67
68/* Provide the fixmap address of the remapped GDT */
69static inline struct desc_struct *get_cpu_gdt_ro(int cpu)
70{
71 unsigned int idx = get_cpu_gdt_ro_index(cpu);
72 return (struct desc_struct *)__fix_to_virt(idx);
73}
74
Thomas Garnier69218e42017-03-14 10:05:07 -070075/* Provide the current read-only GDT */
76static inline struct desc_struct *get_current_gdt_ro(void)
77{
78 return get_cpu_gdt_ro(smp_processor_id());
79}
80
Andy Lutomirskiaa4ea67552017-03-22 14:32:30 -070081/* Provide the physical address of the GDT page. */
82static inline phys_addr_t get_cpu_gdt_paddr(unsigned int cpu)
83{
84 return per_cpu_ptr_to_phys(get_cpu_gdt_rw(cpu));
85}
86
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +010087#ifdef CONFIG_X86_64
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +010088
89static inline void pack_gate(gate_desc *gate, unsigned type, unsigned long func,
90 unsigned dpl, unsigned ist, unsigned seg)
91{
Ingo Molnar9a3865b2011-05-27 09:29:32 +020092 gate->offset_low = PTR_LOW(func);
93 gate->segment = __KERNEL_CS;
94 gate->ist = ist;
95 gate->p = 1;
96 gate->dpl = dpl;
97 gate->zero0 = 0;
98 gate->zero1 = 0;
99 gate->type = type;
100 gate->offset_middle = PTR_MIDDLE(func);
101 gate->offset_high = PTR_HIGH(func);
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +0100102}
103
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100104#else
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +0100105static inline void pack_gate(gate_desc *gate, unsigned char type,
Joe Perchesc1773a12008-03-23 01:01:58 -0700106 unsigned long base, unsigned dpl, unsigned flags,
107 unsigned short seg)
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +0100108{
109 gate->a = (seg << 16) | (base & 0xffff);
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200110 gate->b = (base & 0xffff0000) | (((0x80 | type | (dpl << 5)) & 0xff) << 8);
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +0100111}
112
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100113#endif
114
Glauber de Oliveira Costa746ff602008-01-30 13:31:27 +0100115static inline int desc_empty(const void *ptr)
116{
117 const u32 *desc = ptr;
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200118
Glauber de Oliveira Costa746ff602008-01-30 13:31:27 +0100119 return !(desc[0] | desc[1]);
120}
121
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100122#ifdef CONFIG_PARAVIRT
123#include <asm/paravirt.h>
124#else
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200125#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))
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100130
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200131#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())
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100134
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200135#define load_TLS(t, cpu) native_load_tls(t, cpu)
136#define set_ldt native_set_ldt
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100137
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200138#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)
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700141
142static inline void paravirt_alloc_ldt(struct desc_struct *ldt, unsigned entries)
143{
144}
145
146static inline void paravirt_free_ldt(struct desc_struct *ldt, unsigned entries)
147{
148}
149#endif /* CONFIG_PARAVIRT */
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100150
Jaswinder Singh Rajput8229d752009-03-11 19:13:49 +0530151#define store_ldt(ldt) asm("sldt %0" : "=m"(ldt))
152
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200153static inline void native_write_idt_entry(gate_desc *idt, int entry, const gate_desc *gate)
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100154{
155 memcpy(&idt[entry], gate, sizeof(*gate));
156}
157
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200158static inline void native_write_ldt_entry(struct desc_struct *ldt, int entry, const void *desc)
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100159{
160 memcpy(&ldt[entry], desc, 8);
161}
162
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200163static inline void
164native_write_gdt_entry(struct desc_struct *gdt, int entry, const void *desc, int type)
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100165{
166 unsigned int size;
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200167
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100168 switch (type) {
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200169 case DESC_TSS: size = sizeof(tss_desc); break;
170 case DESC_LDT: size = sizeof(ldt_desc); break;
171 default: size = sizeof(*gdt); break;
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100172 }
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200173
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100174 memcpy(&gdt[entry], desc, size);
175}
176
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100177static inline void pack_descriptor(struct desc_struct *desc, unsigned long base,
178 unsigned long limit, unsigned char type,
179 unsigned char flags)
180{
181 desc->a = ((base & 0xffff) << 16) | (limit & 0xffff);
182 desc->b = (base & 0xff000000) | ((base & 0xff0000) >> 16) |
Joe Perchesc1773a12008-03-23 01:01:58 -0700183 (limit & 0x000f0000) | ((type & 0xff) << 8) |
184 ((flags & 0xf) << 20);
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100185 desc->p = 1;
186}
187
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100188
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200189static inline void set_tssldt_descriptor(void *d, unsigned long addr, unsigned type, unsigned size)
Glauber de Oliveira Costac81c6ca2008-01-30 13:31:14 +0100190{
191#ifdef CONFIG_X86_64
Glauber de Oliveira Costaf6e0eba2008-01-30 13:31:20 +0100192 struct ldttss_desc64 *desc = d;
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200193
Glauber de Oliveira Costaf6e0eba2008-01-30 13:31:20 +0100194 memset(desc, 0, sizeof(*desc));
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200195
196 desc->limit0 = size & 0xFFFF;
197 desc->base0 = PTR_LOW(addr);
198 desc->base1 = PTR_MIDDLE(addr) & 0xFF;
199 desc->type = type;
200 desc->p = 1;
201 desc->limit1 = (size >> 16) & 0xF;
202 desc->base2 = (PTR_MIDDLE(addr) >> 8) & 0xFF;
203 desc->base3 = PTR_HIGH(addr);
Glauber de Oliveira Costac81c6ca2008-01-30 13:31:14 +0100204#else
Glauber de Oliveira Costaf6e0eba2008-01-30 13:31:20 +0100205 pack_descriptor((struct desc_struct *)d, addr, size, 0x80 | type, 0);
Glauber de Oliveira Costac81c6ca2008-01-30 13:31:14 +0100206#endif
207}
208
209static inline void __set_tss_desc(unsigned cpu, unsigned int entry, void *addr)
210{
Thomas Garnier69218e42017-03-14 10:05:07 -0700211 struct desc_struct *d = get_cpu_gdt_rw(cpu);
Glauber de Oliveira Costac81c6ca2008-01-30 13:31:14 +0100212 tss_desc tss;
213
Glauber de Oliveira Costaf6e0eba2008-01-30 13:31:20 +0100214 set_tssldt_descriptor(&tss, (unsigned long)addr, DESC_TSS,
Andy Lutomirski4f53ab12017-02-20 08:56:09 -0800215 __KERNEL_TSS_LIMIT);
Glauber de Oliveira Costac81c6ca2008-01-30 13:31:14 +0100216 write_gdt_entry(d, entry, &tss, DESC_TSS);
217}
218
219#define set_tss_desc(cpu, addr) __set_tss_desc(cpu, GDT_ENTRY_TSS, addr)
220
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100221static inline void native_set_ldt(const void *addr, unsigned int entries)
222{
223 if (likely(entries == 0))
Joe Perchesc1773a12008-03-23 01:01:58 -0700224 asm volatile("lldt %w0"::"q" (0));
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100225 else {
226 unsigned cpu = smp_processor_id();
227 ldt_desc ldt;
228
Michael Karcher5ac37f82008-07-11 18:04:46 +0200229 set_tssldt_descriptor(&ldt, (unsigned long)addr, DESC_LDT,
230 entries * LDT_ENTRY_SIZE - 1);
Thomas Garnier69218e42017-03-14 10:05:07 -0700231 write_gdt_entry(get_cpu_gdt_rw(cpu), GDT_ENTRY_LDT,
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100232 &ldt, DESC_LDT);
Joe Perchesc1773a12008-03-23 01:01:58 -0700233 asm volatile("lldt %w0"::"q" (GDT_ENTRY_LDT*8));
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100234 }
235}
236
Thomas Garnier45fc8752017-03-14 10:05:08 -0700237static inline void native_load_gdt(const struct desc_ptr *dtr)
238{
239 asm volatile("lgdt %0"::"m" (*dtr));
240}
241
242static inline void native_load_idt(const struct desc_ptr *dtr)
243{
244 asm volatile("lidt %0"::"m" (*dtr));
245}
246
247static inline void native_store_gdt(struct desc_ptr *dtr)
248{
249 asm volatile("sgdt %0":"=m" (*dtr));
250}
251
252static inline void native_store_idt(struct desc_ptr *dtr)
253{
254 asm volatile("sidt %0":"=m" (*dtr));
255}
256
257/*
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.
261 */
262#ifdef CONFIG_X86_64
263static inline void native_load_tr_desc(void)
264{
265 struct desc_ptr gdt;
266 int cpu = raw_smp_processor_id();
267 bool restore = 0;
268 struct desc_struct *fixmap_gdt;
269
270 native_store_gdt(&gdt);
271 fixmap_gdt = get_cpu_gdt_ro(cpu);
272
273 /*
274 * If the current GDT is the read-only fixmap, swap to the original
275 * writeable version. Swap back at the end.
276 */
277 if (gdt.address == (unsigned long)fixmap_gdt) {
278 load_direct_gdt(cpu);
279 restore = 1;
280 }
281 asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS*8));
282 if (restore)
283 load_fixmap_gdt(cpu);
284}
285#else
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100286static inline void native_load_tr_desc(void)
287{
288 asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS*8));
289}
Thomas Garnier45fc8752017-03-14 10:05:08 -0700290#endif
291
292static inline unsigned long native_store_tr(void)
293{
294 unsigned long tr;
295
296 asm volatile("str %0":"=r" (tr));
297
298 return tr;
299}
300
301static inline void native_load_tls(struct thread_struct *t, unsigned int cpu)
302{
303 struct desc_struct *gdt = get_cpu_gdt_rw(cpu);
304 unsigned int i;
305
306 for (i = 0; i < GDT_ENTRY_TLS_ENTRIES; i++)
307 gdt[GDT_ENTRY_TLS_MIN + i] = t->tls_array[i];
308}
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100309
Andy Lutomirskib7ceaec2017-02-22 07:36:16 -0800310DECLARE_PER_CPU(bool, __tss_limit_invalid);
311
Andy Lutomirskib7ffc442017-02-20 08:56:14 -0800312static inline void force_reload_TR(void)
313{
Thomas Garnier69218e42017-03-14 10:05:07 -0700314 struct desc_struct *d = get_current_gdt_rw();
Andy Lutomirskib7ffc442017-02-20 08:56:14 -0800315 tss_desc tss;
316
317 memcpy(&tss, &d[GDT_ENTRY_TSS], sizeof(tss_desc));
318
319 /*
320 * LTR requires an available TSS, and the TSS is currently
321 * busy. Make it be available so that LTR will work.
322 */
323 tss.type = DESC_TSS;
324 write_gdt_entry(d, GDT_ENTRY_TSS, &tss, DESC_TSS);
325
326 load_TR_desc();
Andy Lutomirskib7ceaec2017-02-22 07:36:16 -0800327 this_cpu_write(__tss_limit_invalid, false);
Andy Lutomirskib7ffc442017-02-20 08:56:14 -0800328}
329
Andy Lutomirskib7ceaec2017-02-22 07:36:16 -0800330/*
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.
334 */
335static inline void refresh_tss_limit(void)
Andy Lutomirskib7ffc442017-02-20 08:56:14 -0800336{
337 DEBUG_LOCKS_WARN_ON(preemptible());
338
Andy Lutomirskib7ceaec2017-02-22 07:36:16 -0800339 if (unlikely(this_cpu_read(__tss_limit_invalid)))
Andy Lutomirskib7ffc442017-02-20 08:56:14 -0800340 force_reload_TR();
Andy Lutomirskib7ffc442017-02-20 08:56:14 -0800341}
342
343/*
344 * If you do something evil that corrupts the cached TSS limit (I'm looking
345 * at you, VMX exits), call this function.
346 *
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.
351 */
352static inline void invalidate_tss_limit(void)
353{
354 DEBUG_LOCKS_WARN_ON(preemptible());
355
356 if (unlikely(test_thread_flag(TIF_IO_BITMAP)))
357 force_reload_TR();
358 else
Andy Lutomirskib7ceaec2017-02-22 07:36:16 -0800359 this_cpu_write(__tss_limit_invalid, true);
Andy Lutomirskib7ffc442017-02-20 08:56:14 -0800360}
361
Andy Lutomirskie30ab182015-01-22 11:27:58 -0800362/* This intentionally ignores lm, since 32-bit apps don't have that field. */
363#define LDT_empty(info) \
Joe Perchesc1773a12008-03-23 01:01:58 -0700364 ((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)
Glauber de Oliveira Costa881c2972008-01-30 13:31:14 +0100372
Andy Lutomirski3669ef92015-01-22 11:27:59 -0800373/* Lots of programs expect an all-zero user_desc to mean "no segment at all". */
374static inline bool LDT_zero(const struct user_desc *info)
375{
376 return (info->base_addr == 0 &&
377 info->limit == 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 &&
383 info->useable == 0);
384}
385
Glauber de Oliveira Costa881c2972008-01-30 13:31:14 +0100386static inline void clear_LDT(void)
387{
388 set_ldt(NULL, 0);
389}
390
Roland McGrath1bd57182008-01-30 13:31:51 +0100391static inline unsigned long get_desc_base(const struct desc_struct *desc)
Glauber de Oliveira Costacc697852008-01-30 13:31:14 +0100392{
Chris Lalancette2c759102009-11-05 11:47:08 +0100393 return (unsigned)(desc->base0 | ((desc->base1) << 16) | ((desc->base2) << 24));
Glauber de Oliveira Costacc697852008-01-30 13:31:14 +0100394}
Roland McGrath1bd57182008-01-30 13:31:51 +0100395
Akinobu Mita57594742009-07-19 00:11:06 +0900396static inline void set_desc_base(struct desc_struct *desc, unsigned long base)
397{
398 desc->base0 = base & 0xffff;
399 desc->base1 = (base >> 16) & 0xff;
400 desc->base2 = (base >> 24) & 0xff;
401}
402
Roland McGrath1bd57182008-01-30 13:31:51 +0100403static inline unsigned long get_desc_limit(const struct desc_struct *desc)
404{
405 return desc->limit0 | (desc->limit << 16);
406}
407
Akinobu Mita57594742009-07-19 00:11:06 +0900408static inline void set_desc_limit(struct desc_struct *desc, unsigned long limit)
409{
410 desc->limit0 = limit & 0xffff;
411 desc->limit = (limit >> 16) & 0xf;
412}
413
Steven Rostedt228bdaa2011-12-09 03:02:19 -0500414#ifdef CONFIG_X86_64
415static inline void set_nmi_gate(int gate, void *addr)
416{
417 gate_desc s;
418
419 pack_gate(&s, GATE_INTERRUPT, (unsigned long)addr, 0, 0, __KERNEL_CS);
Seiji Aguchi629f4f92013-06-20 11:45:44 -0400420 write_idt_entry(debug_idt_table, gate, &s);
Steven Rostedt228bdaa2011-12-09 03:02:19 -0500421}
422#endif
423
Seiji Aguchicf910e82013-06-20 11:46:53 -0400424#ifdef CONFIG_TRACING
425extern struct desc_ptr trace_idt_descr;
426extern gate_desc trace_idt_table[];
427static inline void write_trace_idt_entry(int entry, const gate_desc *gate)
428{
429 write_idt_entry(trace_idt_table, entry, gate);
430}
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400431
432static inline void _trace_set_gate(int gate, unsigned type, void *addr,
433 unsigned dpl, unsigned ist, unsigned seg)
434{
435 gate_desc s;
436
437 pack_gate(&s, type, (unsigned long)addr, dpl, ist, seg);
438 /*
439 * does not need to be atomic because it is only done once at
440 * setup time
441 */
442 write_trace_idt_entry(gate, &s);
443}
Seiji Aguchicf910e82013-06-20 11:46:53 -0400444#else
445static inline void write_trace_idt_entry(int entry, const gate_desc *gate)
446{
447}
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400448
449#define _trace_set_gate(gate, type, addr, dpl, ist, seg)
Seiji Aguchicf910e82013-06-20 11:46:53 -0400450#endif
451
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +0100452static inline void _set_gate(int gate, unsigned type, void *addr,
Joe Perchesc1773a12008-03-23 01:01:58 -0700453 unsigned dpl, unsigned ist, unsigned seg)
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +0100454{
455 gate_desc s;
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200456
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +0100457 pack_gate(&s, type, (unsigned long)addr, dpl, ist, seg);
458 /*
459 * does not need to be atomic because it is only done once at
460 * setup time
461 */
462 write_idt_entry(idt_table, gate, &s);
Seiji Aguchicf910e82013-06-20 11:46:53 -0400463 write_trace_idt_entry(gate, &s);
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +0100464}
465
466/*
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.
471 */
Wang Nan5eca7452015-02-27 12:19:49 +0800472#define set_intr_gate_notrace(n, addr) \
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400473 do { \
474 BUG_ON((unsigned)n > 0xFF); \
475 _set_gate(n, GATE_INTERRUPT, (void *)addr, 0, 0, \
476 __KERNEL_CS); \
Wang Nan5eca7452015-02-27 12:19:49 +0800477 } while (0)
478
479#define set_intr_gate(n, addr) \
480 do { \
481 set_intr_gate_notrace(n, addr); \
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400482 _trace_set_gate(n, GATE_INTERRUPT, (void *)trace_##addr,\
483 0, 0, __KERNEL_CS); \
484 } while (0)
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +0100485
Yinghai Lub77b8812008-12-19 15:23:44 -0800486/* used_vectors is BITMAP for irq is not managed by percpu vector_irq */
487extern unsigned long used_vectors[];
Alan Mayer305b92a2008-04-15 15:36:56 -0500488
489static inline void alloc_system_vector(int vector)
490{
Thomas Gleixner05161b92017-08-28 08:47:18 +0200491 BUG_ON(vector < FIRST_SYSTEM_VECTOR);
Yinghai Lub77b8812008-12-19 15:23:44 -0800492 if (!test_bit(vector, used_vectors)) {
493 set_bit(vector, used_vectors);
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200494 } else {
Alan Mayer305b92a2008-04-15 15:36:56 -0500495 BUG();
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200496 }
Alan Mayer305b92a2008-04-15 15:36:56 -0500497}
498
Seiji Aguchicf910e82013-06-20 11:46:53 -0400499#define alloc_intr_gate(n, addr) \
500 do { \
501 alloc_system_vector(n); \
Seiji Aguchi959c0712013-10-30 16:36:08 -0400502 set_intr_gate(n, addr); \
Seiji Aguchicf910e82013-06-20 11:46:53 -0400503 } while (0)
504
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +0100505/*
506 * This routine sets up an interrupt gate at directory privilege level 3.
507 */
508static inline void set_system_intr_gate(unsigned int n, void *addr)
509{
510 BUG_ON((unsigned)n > 0xFF);
511 _set_gate(n, GATE_INTERRUPT, addr, 0x3, 0, __KERNEL_CS);
512}
513
Alexander van Heukelum699d2932008-10-03 22:00:32 +0200514static inline void set_system_trap_gate(unsigned int n, void *addr)
515{
516 BUG_ON((unsigned)n > 0xFF);
517 _set_gate(n, GATE_TRAP, addr, 0x3, 0, __KERNEL_CS);
518}
519
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +0100520static inline void set_trap_gate(unsigned int n, void *addr)
521{
522 BUG_ON((unsigned)n > 0xFF);
523 _set_gate(n, GATE_TRAP, addr, 0, 0, __KERNEL_CS);
524}
525
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +0100526static inline void set_task_gate(unsigned int n, unsigned int gdt_entry)
527{
528 BUG_ON((unsigned)n > 0xFF);
529 _set_gate(n, GATE_TASK, (void *)0, 0, 0, (gdt_entry<<3));
530}
531
532static inline void set_intr_gate_ist(int n, void *addr, unsigned ist)
533{
534 BUG_ON((unsigned)n > 0xFF);
535 _set_gate(n, GATE_INTERRUPT, addr, 0, ist, __KERNEL_CS);
536}
537
Alexander van Heukelum699d2932008-10-03 22:00:32 +0200538static inline void set_system_intr_gate_ist(int n, void *addr, unsigned ist)
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +0100539{
540 BUG_ON((unsigned)n > 0xFF);
541 _set_gate(n, GATE_INTERRUPT, addr, 0x3, ist, __KERNEL_CS);
542}
Glauber de Oliveira Costacc697852008-01-30 13:31:14 +0100543
Seiji Aguchi629f4f92013-06-20 11:45:44 -0400544#ifdef CONFIG_X86_64
545DECLARE_PER_CPU(u32, debug_idt_ctr);
546static inline bool is_debug_idt_enabled(void)
547{
548 if (this_cpu_read(debug_idt_ctr))
549 return true;
550
551 return false;
552}
553
554static inline void load_debug_idt(void)
555{
556 load_idt((const struct desc_ptr *)&debug_idt_descr);
557}
558#else
559static inline bool is_debug_idt_enabled(void)
560{
561 return false;
562}
563
564static inline void load_debug_idt(void)
565{
566}
567#endif
568
Seiji Aguchicf910e82013-06-20 11:46:53 -0400569#ifdef CONFIG_TRACING
570extern atomic_t trace_idt_ctr;
571static inline bool is_trace_idt_enabled(void)
572{
573 if (atomic_read(&trace_idt_ctr))
574 return true;
575
576 return false;
577}
578
579static inline void load_trace_idt(void)
580{
581 load_idt((const struct desc_ptr *)&trace_idt_descr);
582}
583#else
584static inline bool is_trace_idt_enabled(void)
585{
586 return false;
587}
588
589static inline void load_trace_idt(void)
590{
591}
592#endif
593
Seiji Aguchi629f4f92013-06-20 11:45:44 -0400594/*
Steven Rostedt (Red Hat)2b4bc782013-06-22 13:16:19 -0400595 * The load_current_idt() must be called with interrupts disabled
Seiji Aguchi629f4f92013-06-20 11:45:44 -0400596 * to avoid races. That way the IDT will always be set back to the expected
Steven Rostedt (Red Hat)2b4bc782013-06-22 13:16:19 -0400597 * descriptor. It's also called when a CPU is being initialized, and
598 * that doesn't need to disable interrupts, as nothing should be
599 * bothering the CPU then.
Seiji Aguchi629f4f92013-06-20 11:45:44 -0400600 */
601static inline void load_current_idt(void)
602{
Seiji Aguchi629f4f92013-06-20 11:45:44 -0400603 if (is_debug_idt_enabled())
604 load_debug_idt();
Seiji Aguchicf910e82013-06-20 11:46:53 -0400605 else if (is_trace_idt_enabled())
606 load_trace_idt();
Seiji Aguchi629f4f92013-06-20 11:45:44 -0400607 else
608 load_idt((const struct desc_ptr *)&idt_descr);
Seiji Aguchi629f4f92013-06-20 11:45:44 -0400609}
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700610#endif /* _ASM_X86_DESC_H */