blob: 108a9e8940199638089f063d832a4fb353f468d2 [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
Thomas Gleixner64b163f2017-08-28 08:47:37 +0200169static inline void set_tssldt_descriptor(void *d, unsigned long addr,
170 unsigned type, unsigned size)
Glauber de Oliveira Costac81c6ca2008-01-30 13:31:14 +0100171{
Thomas Gleixner87cc0372017-08-28 08:47:42 +0200172 struct ldttss_desc *desc = d;
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200173
Glauber de Oliveira Costaf6e0eba2008-01-30 13:31:20 +0100174 memset(desc, 0, sizeof(*desc));
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200175
Thomas Gleixner87cc0372017-08-28 08:47:42 +0200176 desc->limit0 = (u16) size;
Thomas Gleixner64b163f2017-08-28 08:47:37 +0200177 desc->base0 = (u16) addr;
178 desc->base1 = (addr >> 16) & 0xFF;
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200179 desc->type = type;
180 desc->p = 1;
181 desc->limit1 = (size >> 16) & 0xF;
Thomas Gleixner64b163f2017-08-28 08:47:37 +0200182 desc->base2 = (addr >> 24) & 0xFF;
Thomas Gleixner87cc0372017-08-28 08:47:42 +0200183#ifdef CONFIG_X86_64
Thomas Gleixner64b163f2017-08-28 08:47:37 +0200184 desc->base3 = (u32) (addr >> 32);
Glauber de Oliveira Costac81c6ca2008-01-30 13:31:14 +0100185#endif
186}
187
188static inline void __set_tss_desc(unsigned cpu, unsigned int entry, void *addr)
189{
Thomas Garnier69218e42017-03-14 10:05:07 -0700190 struct desc_struct *d = get_cpu_gdt_rw(cpu);
Glauber de Oliveira Costac81c6ca2008-01-30 13:31:14 +0100191 tss_desc tss;
192
Glauber de Oliveira Costaf6e0eba2008-01-30 13:31:20 +0100193 set_tssldt_descriptor(&tss, (unsigned long)addr, DESC_TSS,
Andy Lutomirski4f53ab12017-02-20 08:56:09 -0800194 __KERNEL_TSS_LIMIT);
Glauber de Oliveira Costac81c6ca2008-01-30 13:31:14 +0100195 write_gdt_entry(d, entry, &tss, DESC_TSS);
196}
197
198#define set_tss_desc(cpu, addr) __set_tss_desc(cpu, GDT_ENTRY_TSS, addr)
199
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100200static inline void native_set_ldt(const void *addr, unsigned int entries)
201{
202 if (likely(entries == 0))
Joe Perchesc1773a12008-03-23 01:01:58 -0700203 asm volatile("lldt %w0"::"q" (0));
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100204 else {
205 unsigned cpu = smp_processor_id();
206 ldt_desc ldt;
207
Michael Karcher5ac37f82008-07-11 18:04:46 +0200208 set_tssldt_descriptor(&ldt, (unsigned long)addr, DESC_LDT,
209 entries * LDT_ENTRY_SIZE - 1);
Thomas Garnier69218e42017-03-14 10:05:07 -0700210 write_gdt_entry(get_cpu_gdt_rw(cpu), GDT_ENTRY_LDT,
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100211 &ldt, DESC_LDT);
Joe Perchesc1773a12008-03-23 01:01:58 -0700212 asm volatile("lldt %w0"::"q" (GDT_ENTRY_LDT*8));
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100213 }
214}
215
Thomas Garnier45fc8752017-03-14 10:05:08 -0700216static inline void native_load_gdt(const struct desc_ptr *dtr)
217{
218 asm volatile("lgdt %0"::"m" (*dtr));
219}
220
221static inline void native_load_idt(const struct desc_ptr *dtr)
222{
223 asm volatile("lidt %0"::"m" (*dtr));
224}
225
226static inline void native_store_gdt(struct desc_ptr *dtr)
227{
228 asm volatile("sgdt %0":"=m" (*dtr));
229}
230
231static inline void native_store_idt(struct desc_ptr *dtr)
232{
233 asm volatile("sidt %0":"=m" (*dtr));
234}
235
236/*
237 * The LTR instruction marks the TSS GDT entry as busy. On 64-bit, the GDT is
238 * a read-only remapping. To prevent a page fault, the GDT is switched to the
239 * original writeable version when needed.
240 */
241#ifdef CONFIG_X86_64
242static inline void native_load_tr_desc(void)
243{
244 struct desc_ptr gdt;
245 int cpu = raw_smp_processor_id();
246 bool restore = 0;
247 struct desc_struct *fixmap_gdt;
248
249 native_store_gdt(&gdt);
250 fixmap_gdt = get_cpu_gdt_ro(cpu);
251
252 /*
253 * If the current GDT is the read-only fixmap, swap to the original
254 * writeable version. Swap back at the end.
255 */
256 if (gdt.address == (unsigned long)fixmap_gdt) {
257 load_direct_gdt(cpu);
258 restore = 1;
259 }
260 asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS*8));
261 if (restore)
262 load_fixmap_gdt(cpu);
263}
264#else
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100265static inline void native_load_tr_desc(void)
266{
267 asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS*8));
268}
Thomas Garnier45fc8752017-03-14 10:05:08 -0700269#endif
270
271static inline unsigned long native_store_tr(void)
272{
273 unsigned long tr;
274
275 asm volatile("str %0":"=r" (tr));
276
277 return tr;
278}
279
280static inline void native_load_tls(struct thread_struct *t, unsigned int cpu)
281{
282 struct desc_struct *gdt = get_cpu_gdt_rw(cpu);
283 unsigned int i;
284
285 for (i = 0; i < GDT_ENTRY_TLS_ENTRIES; i++)
286 gdt[GDT_ENTRY_TLS_MIN + i] = t->tls_array[i];
287}
Glauber de Oliveira Costa54cd0ea2008-01-30 13:31:14 +0100288
Andy Lutomirskib7ceaec2017-02-22 07:36:16 -0800289DECLARE_PER_CPU(bool, __tss_limit_invalid);
290
Andy Lutomirskib7ffc442017-02-20 08:56:14 -0800291static inline void force_reload_TR(void)
292{
Thomas Garnier69218e42017-03-14 10:05:07 -0700293 struct desc_struct *d = get_current_gdt_rw();
Andy Lutomirskib7ffc442017-02-20 08:56:14 -0800294 tss_desc tss;
295
296 memcpy(&tss, &d[GDT_ENTRY_TSS], sizeof(tss_desc));
297
298 /*
299 * LTR requires an available TSS, and the TSS is currently
300 * busy. Make it be available so that LTR will work.
301 */
302 tss.type = DESC_TSS;
303 write_gdt_entry(d, GDT_ENTRY_TSS, &tss, DESC_TSS);
304
305 load_TR_desc();
Andy Lutomirskib7ceaec2017-02-22 07:36:16 -0800306 this_cpu_write(__tss_limit_invalid, false);
Andy Lutomirskib7ffc442017-02-20 08:56:14 -0800307}
308
Andy Lutomirskib7ceaec2017-02-22 07:36:16 -0800309/*
310 * Call this if you need the TSS limit to be correct, which should be the case
311 * if and only if you have TIF_IO_BITMAP set or you're switching to a task
312 * with TIF_IO_BITMAP set.
313 */
314static inline void refresh_tss_limit(void)
Andy Lutomirskib7ffc442017-02-20 08:56:14 -0800315{
316 DEBUG_LOCKS_WARN_ON(preemptible());
317
Andy Lutomirskib7ceaec2017-02-22 07:36:16 -0800318 if (unlikely(this_cpu_read(__tss_limit_invalid)))
Andy Lutomirskib7ffc442017-02-20 08:56:14 -0800319 force_reload_TR();
Andy Lutomirskib7ffc442017-02-20 08:56:14 -0800320}
321
322/*
323 * If you do something evil that corrupts the cached TSS limit (I'm looking
324 * at you, VMX exits), call this function.
325 *
326 * The optimization here is that the TSS limit only matters for Linux if the
327 * IO bitmap is in use. If the TSS limit gets forced to its minimum value,
328 * everything works except that IO bitmap will be ignored and all CPL 3 IO
329 * instructions will #GP, which is exactly what we want for normal tasks.
330 */
331static inline void invalidate_tss_limit(void)
332{
333 DEBUG_LOCKS_WARN_ON(preemptible());
334
335 if (unlikely(test_thread_flag(TIF_IO_BITMAP)))
336 force_reload_TR();
337 else
Andy Lutomirskib7ceaec2017-02-22 07:36:16 -0800338 this_cpu_write(__tss_limit_invalid, true);
Andy Lutomirskib7ffc442017-02-20 08:56:14 -0800339}
340
Andy Lutomirskie30ab182015-01-22 11:27:58 -0800341/* This intentionally ignores lm, since 32-bit apps don't have that field. */
342#define LDT_empty(info) \
Joe Perchesc1773a12008-03-23 01:01:58 -0700343 ((info)->base_addr == 0 && \
344 (info)->limit == 0 && \
345 (info)->contents == 0 && \
346 (info)->read_exec_only == 1 && \
347 (info)->seg_32bit == 0 && \
348 (info)->limit_in_pages == 0 && \
349 (info)->seg_not_present == 1 && \
350 (info)->useable == 0)
Glauber de Oliveira Costa881c2972008-01-30 13:31:14 +0100351
Andy Lutomirski3669ef92015-01-22 11:27:59 -0800352/* Lots of programs expect an all-zero user_desc to mean "no segment at all". */
353static inline bool LDT_zero(const struct user_desc *info)
354{
355 return (info->base_addr == 0 &&
356 info->limit == 0 &&
357 info->contents == 0 &&
358 info->read_exec_only == 0 &&
359 info->seg_32bit == 0 &&
360 info->limit_in_pages == 0 &&
361 info->seg_not_present == 0 &&
362 info->useable == 0);
363}
364
Glauber de Oliveira Costa881c2972008-01-30 13:31:14 +0100365static inline void clear_LDT(void)
366{
367 set_ldt(NULL, 0);
368}
369
Roland McGrath1bd57182008-01-30 13:31:51 +0100370static inline unsigned long get_desc_base(const struct desc_struct *desc)
Glauber de Oliveira Costacc697852008-01-30 13:31:14 +0100371{
Chris Lalancette2c759102009-11-05 11:47:08 +0100372 return (unsigned)(desc->base0 | ((desc->base1) << 16) | ((desc->base2) << 24));
Glauber de Oliveira Costacc697852008-01-30 13:31:14 +0100373}
Roland McGrath1bd57182008-01-30 13:31:51 +0100374
Akinobu Mita57594742009-07-19 00:11:06 +0900375static inline void set_desc_base(struct desc_struct *desc, unsigned long base)
376{
377 desc->base0 = base & 0xffff;
378 desc->base1 = (base >> 16) & 0xff;
379 desc->base2 = (base >> 24) & 0xff;
380}
381
Roland McGrath1bd57182008-01-30 13:31:51 +0100382static inline unsigned long get_desc_limit(const struct desc_struct *desc)
383{
Thomas Gleixner38e9e812017-08-28 08:47:41 +0200384 return desc->limit0 | (desc->limit1 << 16);
Roland McGrath1bd57182008-01-30 13:31:51 +0100385}
386
Akinobu Mita57594742009-07-19 00:11:06 +0900387static inline void set_desc_limit(struct desc_struct *desc, unsigned long limit)
388{
389 desc->limit0 = limit & 0xffff;
Thomas Gleixner38e9e812017-08-28 08:47:41 +0200390 desc->limit1 = (limit >> 16) & 0xf;
Akinobu Mita57594742009-07-19 00:11:06 +0900391}
392
Steven Rostedt228bdaa2011-12-09 03:02:19 -0500393#ifdef CONFIG_X86_64
394static inline void set_nmi_gate(int gate, void *addr)
395{
396 gate_desc s;
397
398 pack_gate(&s, GATE_INTERRUPT, (unsigned long)addr, 0, 0, __KERNEL_CS);
Seiji Aguchi629f4f92013-06-20 11:45:44 -0400399 write_idt_entry(debug_idt_table, gate, &s);
Steven Rostedt228bdaa2011-12-09 03:02:19 -0500400}
401#endif
402
Thomas Gleixner4b9a8dc2017-08-28 08:47:31 +0200403static inline void _set_gate(int gate, unsigned type, const void *addr,
Joe Perchesc1773a12008-03-23 01:01:58 -0700404 unsigned dpl, unsigned ist, unsigned seg)
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +0100405{
406 gate_desc s;
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200407
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +0100408 pack_gate(&s, type, (unsigned long)addr, dpl, ist, seg);
409 /*
410 * does not need to be atomic because it is only done once at
411 * setup time
412 */
413 write_idt_entry(idt_table, gate, &s);
414}
415
Thomas Gleixner4b9a8dc2017-08-28 08:47:31 +0200416static inline void set_intr_gate(unsigned int n, const void *addr)
417{
418 BUG_ON(n > 0xFF);
419 _set_gate(n, GATE_INTERRUPT, addr, 0, 0, __KERNEL_CS);
420}
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +0100421
Yinghai Lub77b8812008-12-19 15:23:44 -0800422extern unsigned long used_vectors[];
Alan Mayer305b92a2008-04-15 15:36:56 -0500423
424static inline void alloc_system_vector(int vector)
425{
Thomas Gleixner05161b92017-08-28 08:47:18 +0200426 BUG_ON(vector < FIRST_SYSTEM_VECTOR);
Yinghai Lub77b8812008-12-19 15:23:44 -0800427 if (!test_bit(vector, used_vectors)) {
428 set_bit(vector, used_vectors);
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200429 } else {
Alan Mayer305b92a2008-04-15 15:36:56 -0500430 BUG();
Ingo Molnar9a3865b2011-05-27 09:29:32 +0200431 }
Alan Mayer305b92a2008-04-15 15:36:56 -0500432}
433
Seiji Aguchicf910e82013-06-20 11:46:53 -0400434#define alloc_intr_gate(n, addr) \
435 do { \
436 alloc_system_vector(n); \
Seiji Aguchi959c0712013-10-30 16:36:08 -0400437 set_intr_gate(n, addr); \
Seiji Aguchicf910e82013-06-20 11:46:53 -0400438 } while (0)
439
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +0100440/*
441 * This routine sets up an interrupt gate at directory privilege level 3.
442 */
443static inline void set_system_intr_gate(unsigned int n, void *addr)
444{
445 BUG_ON((unsigned)n > 0xFF);
446 _set_gate(n, GATE_INTERRUPT, addr, 0x3, 0, __KERNEL_CS);
447}
448
Alexander van Heukelum699d2932008-10-03 22:00:32 +0200449static inline void set_system_trap_gate(unsigned int n, void *addr)
450{
451 BUG_ON((unsigned)n > 0xFF);
452 _set_gate(n, GATE_TRAP, addr, 0x3, 0, __KERNEL_CS);
453}
454
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +0100455static inline void set_trap_gate(unsigned int n, void *addr)
456{
457 BUG_ON((unsigned)n > 0xFF);
458 _set_gate(n, GATE_TRAP, addr, 0, 0, __KERNEL_CS);
459}
460
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +0100461static inline void set_task_gate(unsigned int n, unsigned int gdt_entry)
462{
463 BUG_ON((unsigned)n > 0xFF);
464 _set_gate(n, GATE_TASK, (void *)0, 0, 0, (gdt_entry<<3));
465}
466
467static inline void set_intr_gate_ist(int n, void *addr, unsigned ist)
468{
469 BUG_ON((unsigned)n > 0xFF);
470 _set_gate(n, GATE_INTERRUPT, addr, 0, ist, __KERNEL_CS);
471}
472
Alexander van Heukelum699d2932008-10-03 22:00:32 +0200473static inline void set_system_intr_gate_ist(int n, void *addr, unsigned ist)
Glauber de Oliveira Costa507f90c2008-01-30 13:31:14 +0100474{
475 BUG_ON((unsigned)n > 0xFF);
476 _set_gate(n, GATE_INTERRUPT, addr, 0x3, ist, __KERNEL_CS);
477}
Glauber de Oliveira Costacc697852008-01-30 13:31:14 +0100478
Seiji Aguchi629f4f92013-06-20 11:45:44 -0400479#ifdef CONFIG_X86_64
480DECLARE_PER_CPU(u32, debug_idt_ctr);
481static inline bool is_debug_idt_enabled(void)
482{
483 if (this_cpu_read(debug_idt_ctr))
484 return true;
485
486 return false;
487}
488
489static inline void load_debug_idt(void)
490{
491 load_idt((const struct desc_ptr *)&debug_idt_descr);
492}
493#else
494static inline bool is_debug_idt_enabled(void)
495{
496 return false;
497}
498
499static inline void load_debug_idt(void)
500{
501}
502#endif
503
504/*
Steven Rostedt (Red Hat)2b4bc782013-06-22 13:16:19 -0400505 * The load_current_idt() must be called with interrupts disabled
Seiji Aguchi629f4f92013-06-20 11:45:44 -0400506 * 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 -0400507 * descriptor. It's also called when a CPU is being initialized, and
508 * that doesn't need to disable interrupts, as nothing should be
509 * bothering the CPU then.
Seiji Aguchi629f4f92013-06-20 11:45:44 -0400510 */
511static inline void load_current_idt(void)
512{
Seiji Aguchi629f4f92013-06-20 11:45:44 -0400513 if (is_debug_idt_enabled())
514 load_debug_idt();
515 else
516 load_idt((const struct desc_ptr *)&idt_descr);
Seiji Aguchi629f4f92013-06-20 11:45:44 -0400517}
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700518#endif /* _ASM_X86_DESC_H */