blob: 2090cd223505848fd65a34966a57c8d9b45017ad [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;
Thomas Gleixner38e9e812017-08-28 08:47:41 +020026 desc->limit1 = (info->limit & 0xf0000) >> 16;
Ingo Molnar9a3865b2011-05-27 09:29:32 +020027 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 Costa507f90c2008-01-30 13:31:14 +010087static inline void pack_gate(gate_desc *gate, unsigned type, unsigned long func,
88 unsigned dpl, unsigned ist, unsigned seg)
89{
Thomas Gleixner64b163f2017-08-28 08:47:37 +020090 gate->offset_low = (u16) func;
91 gate->bits.p = 1;
92 gate->bits.dpl = dpl;
93 gate->bits.zero = 0;
94 gate->bits.type = type;
95 gate->offset_middle = (u16) (func >> 16);
96#ifdef CONFIG_X86_64
Ingo Molnar9a3865b2011-05-27 09:29:32 +020097 gate->segment = __KERNEL_CS;
Thomas Gleixner64b163f2017-08-28 08:47:37 +020098 gate->bits.ist = ist;
99 gate->reserved = 0;
100 gate->offset_high = (u32) (func >> 32);
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100101#else
Thomas Gleixner64b163f2017-08-28 08:47:37 +0200102 gate->segment = seg;
103 gate->bits.ist = 0;
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100104#endif
Thomas Gleixner64b163f2017-08-28 08:47:37 +0200105}
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100106
Glauber de Oliveira Costa746ff602008-01-30 13:31:27 +0100107static inline int desc_empty(const void *ptr)
108{
109 const u32 *desc = ptr;
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200110
Glauber de Oliveira Costa746ff602008-01-30 13:31:27 +0100111 return !(desc[0] | desc[1]);
112}
113
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100114#ifdef CONFIG_PARAVIRT
115#include <asm/paravirt.h>
116#else
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200117#define load_TR_desc() native_load_tr_desc()
118#define load_gdt(dtr) native_load_gdt(dtr)
119#define load_idt(dtr) native_load_idt(dtr)
120#define load_tr(tr) asm volatile("ltr %0"::"m" (tr))
121#define load_ldt(ldt) asm volatile("lldt %0"::"m" (ldt))
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100122
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200123#define store_gdt(dtr) native_store_gdt(dtr)
124#define store_idt(dtr) native_store_idt(dtr)
125#define store_tr(tr) (tr = native_store_tr())
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100126
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200127#define load_TLS(t, cpu) native_load_tls(t, cpu)
128#define set_ldt native_set_ldt
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100129
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200130#define write_ldt_entry(dt, entry, desc) native_write_ldt_entry(dt, entry, desc)
131#define write_gdt_entry(dt, entry, desc, type) native_write_gdt_entry(dt, entry, desc, type)
132#define write_idt_entry(dt, entry, g) native_write_idt_entry(dt, entry, g)
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700133
134static inline void paravirt_alloc_ldt(struct desc_struct *ldt, unsigned entries)
135{
136}
137
138static inline void paravirt_free_ldt(struct desc_struct *ldt, unsigned entries)
139{
140}
141#endif /* CONFIG_PARAVIRT */
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100142
Jaswinder Singh Rajput8229d752009-03-11 19:13:49 +0530143#define store_ldt(ldt) asm("sldt %0" : "=m"(ldt))
144
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200145static inline void native_write_idt_entry(gate_desc *idt, int entry, const gate_desc *gate)
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100146{
147 memcpy(&idt[entry], gate, sizeof(*gate));
148}
149
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200150static inline void native_write_ldt_entry(struct desc_struct *ldt, int entry, const void *desc)
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100151{
152 memcpy(&ldt[entry], desc, 8);
153}
154
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200155static inline void
156native_write_gdt_entry(struct desc_struct *gdt, int entry, const void *desc, int type)
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100157{
158 unsigned int size;
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200159
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100160 switch (type) {
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200161 case DESC_TSS: size = sizeof(tss_desc); break;
162 case DESC_LDT: size = sizeof(ldt_desc); break;
163 default: size = sizeof(*gdt); break;
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100164 }
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200165
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100166 memcpy(&gdt[entry], desc, size);
167}
168
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100169static inline void pack_descriptor(struct desc_struct *desc, unsigned long base,
170 unsigned long limit, unsigned char type,
171 unsigned char flags)
172{
Thomas Gleixner38e9e812017-08-28 08:47:41 +0200173 desc->limit0 = (u16) limit;
174 desc->base0 = (u16) base;
175 desc->base1 = (base >> 16) & 0xFF;
176 desc->type = type & 0x0F;
177 desc->s = 0;
178 desc->dpl = 0;
179 desc->p = 1;
180 desc->limit1 = (limit >> 16) & 0xF;
181 desc->avl = (flags >> 0) & 0x01;
182 desc->l = (flags >> 1) & 0x01;
183 desc->d = (flags >> 2) & 0x01;
184 desc->g = (flags >> 3) & 0x01;
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100185}
186
Thomas Gleixner64b163f2017-08-28 08:47:37 +0200187static inline void set_tssldt_descriptor(void *d, unsigned long addr,
188 unsigned type, unsigned size)
Glauber de Oliveira Costac81c6ca2008-01-30 13:31:14 +0100189{
190#ifdef CONFIG_X86_64
Glauber de Oliveira Costaf6e0eba2008-01-30 13:31:20 +0100191 struct ldttss_desc64 *desc = d;
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200192
Glauber de Oliveira Costaf6e0eba2008-01-30 13:31:20 +0100193 memset(desc, 0, sizeof(*desc));
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200194
195 desc->limit0 = size & 0xFFFF;
Thomas Gleixner64b163f2017-08-28 08:47:37 +0200196 desc->base0 = (u16) addr;
197 desc->base1 = (addr >> 16) & 0xFF;
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200198 desc->type = type;
199 desc->p = 1;
200 desc->limit1 = (size >> 16) & 0xF;
Thomas Gleixner64b163f2017-08-28 08:47:37 +0200201 desc->base2 = (addr >> 24) & 0xFF;
202 desc->base3 = (u32) (addr >> 32);
Glauber de Oliveira Costac81c6ca2008-01-30 13:31:14 +0100203#else
Thomas Gleixner38e9e812017-08-28 08:47:41 +0200204 pack_descriptor((struct desc_struct *)d, addr, size, type, 0);
Glauber de Oliveira Costac81c6ca2008-01-30 13:31:14 +0100205#endif
206}
207
208static inline void __set_tss_desc(unsigned cpu, unsigned int entry, void *addr)
209{
Thomas Garnier69218e42017-03-14 10:05:07 -0700210 struct desc_struct *d = get_cpu_gdt_rw(cpu);
Glauber de Oliveira Costac81c6ca2008-01-30 13:31:14 +0100211 tss_desc tss;
212
Glauber de Oliveira Costaf6e0eba2008-01-30 13:31:20 +0100213 set_tssldt_descriptor(&tss, (unsigned long)addr, DESC_TSS,
Andy Lutomirski4f53ab12017-02-20 08:56:09 -0800214 __KERNEL_TSS_LIMIT);
Glauber de Oliveira Costac81c6ca2008-01-30 13:31:14 +0100215 write_gdt_entry(d, entry, &tss, DESC_TSS);
216}
217
218#define set_tss_desc(cpu, addr) __set_tss_desc(cpu, GDT_ENTRY_TSS, addr)
219
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100220static inline void native_set_ldt(const void *addr, unsigned int entries)
221{
222 if (likely(entries == 0))
Joe Perchesc1773a12008-03-23 01:01:58 -0700223 asm volatile("lldt %w0"::"q" (0));
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100224 else {
225 unsigned cpu = smp_processor_id();
226 ldt_desc ldt;
227
Michael Karcher5ac37f82008-07-11 18:04:46 +0200228 set_tssldt_descriptor(&ldt, (unsigned long)addr, DESC_LDT,
229 entries * LDT_ENTRY_SIZE - 1);
Thomas Garnier69218e42017-03-14 10:05:07 -0700230 write_gdt_entry(get_cpu_gdt_rw(cpu), GDT_ENTRY_LDT,
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100231 &ldt, DESC_LDT);
Joe Perchesc1773a12008-03-23 01:01:58 -0700232 asm volatile("lldt %w0"::"q" (GDT_ENTRY_LDT*8));
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100233 }
234}
235
Thomas Garnier45fc8752017-03-14 10:05:08 -0700236static inline void native_load_gdt(const struct desc_ptr *dtr)
237{
238 asm volatile("lgdt %0"::"m" (*dtr));
239}
240
241static inline void native_load_idt(const struct desc_ptr *dtr)
242{
243 asm volatile("lidt %0"::"m" (*dtr));
244}
245
246static inline void native_store_gdt(struct desc_ptr *dtr)
247{
248 asm volatile("sgdt %0":"=m" (*dtr));
249}
250
251static inline void native_store_idt(struct desc_ptr *dtr)
252{
253 asm volatile("sidt %0":"=m" (*dtr));
254}
255
256/*
257 * The LTR instruction marks the TSS GDT entry as busy. On 64-bit, the GDT is
258 * a read-only remapping. To prevent a page fault, the GDT is switched to the
259 * original writeable version when needed.
260 */
261#ifdef CONFIG_X86_64
262static inline void native_load_tr_desc(void)
263{
264 struct desc_ptr gdt;
265 int cpu = raw_smp_processor_id();
266 bool restore = 0;
267 struct desc_struct *fixmap_gdt;
268
269 native_store_gdt(&gdt);
270 fixmap_gdt = get_cpu_gdt_ro(cpu);
271
272 /*
273 * If the current GDT is the read-only fixmap, swap to the original
274 * writeable version. Swap back at the end.
275 */
276 if (gdt.address == (unsigned long)fixmap_gdt) {
277 load_direct_gdt(cpu);
278 restore = 1;
279 }
280 asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS*8));
281 if (restore)
282 load_fixmap_gdt(cpu);
283}
284#else
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100285static inline void native_load_tr_desc(void)
286{
287 asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS*8));
288}
Thomas Garnier45fc8752017-03-14 10:05:08 -0700289#endif
290
291static inline unsigned long native_store_tr(void)
292{
293 unsigned long tr;
294
295 asm volatile("str %0":"=r" (tr));
296
297 return tr;
298}
299
300static inline void native_load_tls(struct thread_struct *t, unsigned int cpu)
301{
302 struct desc_struct *gdt = get_cpu_gdt_rw(cpu);
303 unsigned int i;
304
305 for (i = 0; i < GDT_ENTRY_TLS_ENTRIES; i++)
306 gdt[GDT_ENTRY_TLS_MIN + i] = t->tls_array[i];
307}
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100308
Andy Lutomirskib7ceaec2017-02-22 07:36:16 -0800309DECLARE_PER_CPU(bool, __tss_limit_invalid);
310
Andy Lutomirskib7ffc442017-02-20 08:56:14 -0800311static inline void force_reload_TR(void)
312{
Thomas Garnier69218e42017-03-14 10:05:07 -0700313 struct desc_struct *d = get_current_gdt_rw();
Andy Lutomirskib7ffc442017-02-20 08:56:14 -0800314 tss_desc tss;
315
316 memcpy(&tss, &d[GDT_ENTRY_TSS], sizeof(tss_desc));
317
318 /*
319 * LTR requires an available TSS, and the TSS is currently
320 * busy. Make it be available so that LTR will work.
321 */
322 tss.type = DESC_TSS;
323 write_gdt_entry(d, GDT_ENTRY_TSS, &tss, DESC_TSS);
324
325 load_TR_desc();
Andy Lutomirskib7ceaec2017-02-22 07:36:16 -0800326 this_cpu_write(__tss_limit_invalid, false);
Andy Lutomirskib7ffc442017-02-20 08:56:14 -0800327}
328
Andy Lutomirskib7ceaec2017-02-22 07:36:16 -0800329/*
330 * Call this if you need the TSS limit to be correct, which should be the case
331 * if and only if you have TIF_IO_BITMAP set or you're switching to a task
332 * with TIF_IO_BITMAP set.
333 */
334static inline void refresh_tss_limit(void)
Andy Lutomirskib7ffc442017-02-20 08:56:14 -0800335{
336 DEBUG_LOCKS_WARN_ON(preemptible());
337
Andy Lutomirskib7ceaec2017-02-22 07:36:16 -0800338 if (unlikely(this_cpu_read(__tss_limit_invalid)))
Andy Lutomirskib7ffc442017-02-20 08:56:14 -0800339 force_reload_TR();
Andy Lutomirskib7ffc442017-02-20 08:56:14 -0800340}
341
342/*
343 * If you do something evil that corrupts the cached TSS limit (I'm looking
344 * at you, VMX exits), call this function.
345 *
346 * The optimization here is that the TSS limit only matters for Linux if the
347 * IO bitmap is in use. If the TSS limit gets forced to its minimum value,
348 * everything works except that IO bitmap will be ignored and all CPL 3 IO
349 * instructions will #GP, which is exactly what we want for normal tasks.
350 */
351static inline void invalidate_tss_limit(void)
352{
353 DEBUG_LOCKS_WARN_ON(preemptible());
354
355 if (unlikely(test_thread_flag(TIF_IO_BITMAP)))
356 force_reload_TR();
357 else
Andy Lutomirskib7ceaec2017-02-22 07:36:16 -0800358 this_cpu_write(__tss_limit_invalid, true);
Andy Lutomirskib7ffc442017-02-20 08:56:14 -0800359}
360
Andy Lutomirskie30ab182015-01-22 11:27:58 -0800361/* This intentionally ignores lm, since 32-bit apps don't have that field. */
362#define LDT_empty(info) \
Joe Perchesc1773a12008-03-23 01:01:58 -0700363 ((info)->base_addr == 0 && \
364 (info)->limit == 0 && \
365 (info)->contents == 0 && \
366 (info)->read_exec_only == 1 && \
367 (info)->seg_32bit == 0 && \
368 (info)->limit_in_pages == 0 && \
369 (info)->seg_not_present == 1 && \
370 (info)->useable == 0)
Glauber de Oliveira Costa881c2972008-01-30 13:31:14 +0100371
Andy Lutomirski3669ef92015-01-22 11:27:59 -0800372/* Lots of programs expect an all-zero user_desc to mean "no segment at all". */
373static inline bool LDT_zero(const struct user_desc *info)
374{
375 return (info->base_addr == 0 &&
376 info->limit == 0 &&
377 info->contents == 0 &&
378 info->read_exec_only == 0 &&
379 info->seg_32bit == 0 &&
380 info->limit_in_pages == 0 &&
381 info->seg_not_present == 0 &&
382 info->useable == 0);
383}
384
Glauber de Oliveira Costa881c2972008-01-30 13:31:14 +0100385static inline void clear_LDT(void)
386{
387 set_ldt(NULL, 0);
388}
389
Roland McGrath1bd57182008-01-30 13:31:51 +0100390static inline unsigned long get_desc_base(const struct desc_struct *desc)
Glauber de Oliveira Costacc697852008-01-30 13:31:14 +0100391{
Chris Lalancette2c759102009-11-05 11:47:08 +0100392 return (unsigned)(desc->base0 | ((desc->base1) << 16) | ((desc->base2) << 24));
Glauber de Oliveira Costacc697852008-01-30 13:31:14 +0100393}
Roland McGrath1bd57182008-01-30 13:31:51 +0100394
Akinobu Mita57594742009-07-19 00:11:06 +0900395static inline void set_desc_base(struct desc_struct *desc, unsigned long base)
396{
397 desc->base0 = base & 0xffff;
398 desc->base1 = (base >> 16) & 0xff;
399 desc->base2 = (base >> 24) & 0xff;
400}
401
Roland McGrath1bd57182008-01-30 13:31:51 +0100402static inline unsigned long get_desc_limit(const struct desc_struct *desc)
403{
Thomas Gleixner38e9e812017-08-28 08:47:41 +0200404 return desc->limit0 | (desc->limit1 << 16);
Roland McGrath1bd57182008-01-30 13:31:51 +0100405}
406
Akinobu Mita57594742009-07-19 00:11:06 +0900407static inline void set_desc_limit(struct desc_struct *desc, unsigned long limit)
408{
409 desc->limit0 = limit & 0xffff;
Thomas Gleixner38e9e812017-08-28 08:47:41 +0200410 desc->limit1 = (limit >> 16) & 0xf;
Akinobu Mita57594742009-07-19 00:11:06 +0900411}
412
Steven Rostedt228bdaa2011-12-09 03:02:19 -0500413#ifdef CONFIG_X86_64
414static inline void set_nmi_gate(int gate, void *addr)
415{
416 gate_desc s;
417
418 pack_gate(&s, GATE_INTERRUPT, (unsigned long)addr, 0, 0, __KERNEL_CS);
Seiji Aguchi629f4f92013-06-20 11:45:44 -0400419 write_idt_entry(debug_idt_table, gate, &s);
Steven Rostedt228bdaa2011-12-09 03:02:19 -0500420}
421#endif
422
Thomas Gleixner4b9a8dc2017-08-28 08:47:31 +0200423static inline void _set_gate(int gate, unsigned type, const void *addr,
Joe Perchesc1773a12008-03-23 01:01:58 -0700424 unsigned dpl, unsigned ist, unsigned seg)
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +0100425{
426 gate_desc s;
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200427
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +0100428 pack_gate(&s, type, (unsigned long)addr, dpl, ist, seg);
429 /*
430 * does not need to be atomic because it is only done once at
431 * setup time
432 */
433 write_idt_entry(idt_table, gate, &s);
434}
435
Thomas Gleixner4b9a8dc2017-08-28 08:47:31 +0200436static inline void set_intr_gate(unsigned int n, const void *addr)
437{
438 BUG_ON(n > 0xFF);
439 _set_gate(n, GATE_INTERRUPT, addr, 0, 0, __KERNEL_CS);
440}
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +0100441
Yinghai Lub77b8812008-12-19 15:23:44 -0800442extern unsigned long used_vectors[];
Alan Mayer305b92a2008-04-15 15:36:56 -0500443
444static inline void alloc_system_vector(int vector)
445{
Thomas Gleixner05161b92017-08-28 08:47:18 +0200446 BUG_ON(vector < FIRST_SYSTEM_VECTOR);
Yinghai Lub77b8812008-12-19 15:23:44 -0800447 if (!test_bit(vector, used_vectors)) {
448 set_bit(vector, used_vectors);
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200449 } else {
Alan Mayer305b92a2008-04-15 15:36:56 -0500450 BUG();
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200451 }
Alan Mayer305b92a2008-04-15 15:36:56 -0500452}
453
Seiji Aguchicf910e82013-06-20 11:46:53 -0400454#define alloc_intr_gate(n, addr) \
455 do { \
456 alloc_system_vector(n); \
Seiji Aguchi959c0712013-10-30 16:36:08 -0400457 set_intr_gate(n, addr); \
Seiji Aguchicf910e82013-06-20 11:46:53 -0400458 } while (0)
459
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +0100460/*
461 * This routine sets up an interrupt gate at directory privilege level 3.
462 */
463static inline void set_system_intr_gate(unsigned int n, void *addr)
464{
465 BUG_ON((unsigned)n > 0xFF);
466 _set_gate(n, GATE_INTERRUPT, addr, 0x3, 0, __KERNEL_CS);
467}
468
Alexander van Heukelum699d2932008-10-03 22:00:32 +0200469static inline void set_system_trap_gate(unsigned int n, void *addr)
470{
471 BUG_ON((unsigned)n > 0xFF);
472 _set_gate(n, GATE_TRAP, addr, 0x3, 0, __KERNEL_CS);
473}
474
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +0100475static inline void set_trap_gate(unsigned int n, void *addr)
476{
477 BUG_ON((unsigned)n > 0xFF);
478 _set_gate(n, GATE_TRAP, addr, 0, 0, __KERNEL_CS);
479}
480
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +0100481static inline void set_task_gate(unsigned int n, unsigned int gdt_entry)
482{
483 BUG_ON((unsigned)n > 0xFF);
484 _set_gate(n, GATE_TASK, (void *)0, 0, 0, (gdt_entry<<3));
485}
486
487static inline void set_intr_gate_ist(int n, void *addr, unsigned ist)
488{
489 BUG_ON((unsigned)n > 0xFF);
490 _set_gate(n, GATE_INTERRUPT, addr, 0, ist, __KERNEL_CS);
491}
492
Alexander van Heukelum699d2932008-10-03 22:00:32 +0200493static inline void set_system_intr_gate_ist(int n, void *addr, unsigned ist)
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +0100494{
495 BUG_ON((unsigned)n > 0xFF);
496 _set_gate(n, GATE_INTERRUPT, addr, 0x3, ist, __KERNEL_CS);
497}
Glauber de Oliveira Costacc697852008-01-30 13:31:14 +0100498
Seiji Aguchi629f4f92013-06-20 11:45:44 -0400499#ifdef CONFIG_X86_64
500DECLARE_PER_CPU(u32, debug_idt_ctr);
501static inline bool is_debug_idt_enabled(void)
502{
503 if (this_cpu_read(debug_idt_ctr))
504 return true;
505
506 return false;
507}
508
509static inline void load_debug_idt(void)
510{
511 load_idt((const struct desc_ptr *)&debug_idt_descr);
512}
513#else
514static inline bool is_debug_idt_enabled(void)
515{
516 return false;
517}
518
519static inline void load_debug_idt(void)
520{
521}
522#endif
523
524/*
Steven Rostedt (Red Hat)2b4bc782013-06-22 13:16:19 -0400525 * The load_current_idt() must be called with interrupts disabled
Seiji Aguchi629f4f92013-06-20 11:45:44 -0400526 * 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 -0400527 * descriptor. It's also called when a CPU is being initialized, and
528 * that doesn't need to disable interrupts, as nothing should be
529 * bothering the CPU then.
Seiji Aguchi629f4f92013-06-20 11:45:44 -0400530 */
531static inline void load_current_idt(void)
532{
Seiji Aguchi629f4f92013-06-20 11:45:44 -0400533 if (is_debug_idt_enabled())
534 load_debug_idt();
535 else
536 load_idt((const struct desc_ptr *)&idt_descr);
Seiji Aguchi629f4f92013-06-20 11:45:44 -0400537}
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700538#endif /* _ASM_X86_DESC_H */