H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 1 | #ifndef _ASM_X86_DESC_H |
| 2 | #define _ASM_X86_DESC_H |
Glauber de Oliveira Costa | 80fbb69 | 2008-01-30 13:31:13 +0100 | [diff] [blame] | 3 | |
Glauber de Oliveira Costa | 80fbb69 | 2008-01-30 13:31:13 +0100 | [diff] [blame] | 4 | #include <asm/desc_defs.h> |
| 5 | #include <asm/ldt.h> |
Glauber de Oliveira Costa | 881c297 | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 6 | #include <asm/mmu.h> |
Thomas Garnier | 69218e4 | 2017-03-14 10:05:07 -0700 | [diff] [blame] | 7 | #include <asm/fixmap.h> |
Thomas Gleixner | 05161b9 | 2017-08-28 08:47:18 +0200 | [diff] [blame^] | 8 | #include <asm/irq_vectors.h> |
Ingo Molnar | 9a3865b | 2011-05-27 09:29:32 +0200 | [diff] [blame] | 9 | |
Glauber de Oliveira Costa | 54cd0ea | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 10 | #include <linux/smp.h> |
Alex Shi | c6ae41e | 2012-05-11 15:35:27 +0800 | [diff] [blame] | 11 | #include <linux/percpu.h> |
Glauber de Oliveira Costa | 80fbb69 | 2008-01-30 13:31:13 +0100 | [diff] [blame] | 12 | |
Ingo Molnar | 9a3865b | 2011-05-27 09:29:32 +0200 | [diff] [blame] | 13 | static inline void fill_ldt(struct desc_struct *desc, const struct user_desc *info) |
Glauber de Oliveira Costa | 80fbb69 | 2008-01-30 13:31:13 +0100 | [diff] [blame] | 14 | { |
Ingo Molnar | 9a3865b | 2011-05-27 09:29:32 +0200 | [diff] [blame] | 15 | desc->limit0 = info->limit & 0x0ffff; |
Glauber de Oliveira Costa | 80fbb69 | 2008-01-30 13:31:13 +0100 | [diff] [blame] | 16 | |
Ingo Molnar | 9a3865b | 2011-05-27 09:29:32 +0200 | [diff] [blame] | 17 | 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 Fitzhardinge | 64f53a0 | 2008-07-27 08:42:32 -0700 | [diff] [blame] | 32 | /* |
Andy Lutomirski | 318f5a2 | 2011-08-03 09:31:53 -0400 | [diff] [blame] | 33 | * Don't allow setting of the lm bit. It would confuse |
| 34 | * user_64bit_mode and would get overridden by sysret anyway. |
Jeremy Fitzhardinge | 64f53a0 | 2008-07-27 08:42:32 -0700 | [diff] [blame] | 35 | */ |
Ingo Molnar | 9a3865b | 2011-05-27 09:29:32 +0200 | [diff] [blame] | 36 | desc->l = 0; |
Glauber de Oliveira Costa | 80fbb69 | 2008-01-30 13:31:13 +0100 | [diff] [blame] | 37 | } |
| 38 | |
Glauber de Oliveira Costa | 881c297 | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 39 | extern struct desc_ptr idt_descr; |
| 40 | extern gate_desc idt_table[]; |
Kees Cook | 404f6aa | 2016-08-08 16:29:06 -0700 | [diff] [blame] | 41 | extern const struct desc_ptr debug_idt_descr; |
Seiji Aguchi | 629f4f9 | 2013-06-20 11:45:44 -0400 | [diff] [blame] | 42 | extern gate_desc debug_idt_table[]; |
Glauber de Oliveira Costa | 80fbb69 | 2008-01-30 13:31:13 +0100 | [diff] [blame] | 43 | |
Glauber Costa | a939098 | 2008-05-28 16:19:53 -0700 | [diff] [blame] | 44 | struct gdt_page { |
| 45 | struct desc_struct gdt[GDT_ENTRIES]; |
| 46 | } __attribute__((aligned(PAGE_SIZE))); |
Ingo Molnar | 9a3865b | 2011-05-27 09:29:32 +0200 | [diff] [blame] | 47 | |
David Howells | 9b8de74 | 2009-04-21 23:00:24 +0100 | [diff] [blame] | 48 | DECLARE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page); |
Glauber Costa | a939098 | 2008-05-28 16:19:53 -0700 | [diff] [blame] | 49 | |
Thomas Garnier | 69218e4 | 2017-03-14 10:05:07 -0700 | [diff] [blame] | 50 | /* Provide the original GDT */ |
| 51 | static inline struct desc_struct *get_cpu_gdt_rw(unsigned int cpu) |
Glauber Costa | a939098 | 2008-05-28 16:19:53 -0700 | [diff] [blame] | 52 | { |
| 53 | return per_cpu(gdt_page, cpu).gdt; |
| 54 | } |
| 55 | |
Thomas Garnier | 69218e4 | 2017-03-14 10:05:07 -0700 | [diff] [blame] | 56 | /* Provide the current original GDT */ |
| 57 | static inline struct desc_struct *get_current_gdt_rw(void) |
| 58 | { |
| 59 | return this_cpu_ptr(&gdt_page)->gdt; |
| 60 | } |
| 61 | |
Thomas Garnier | 69218e4 | 2017-03-14 10:05:07 -0700 | [diff] [blame] | 62 | /* Get the fixmap index for a specific processor */ |
| 63 | static 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 */ |
| 69 | static 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 Garnier | 69218e4 | 2017-03-14 10:05:07 -0700 | [diff] [blame] | 75 | /* Provide the current read-only GDT */ |
| 76 | static inline struct desc_struct *get_current_gdt_ro(void) |
| 77 | { |
| 78 | return get_cpu_gdt_ro(smp_processor_id()); |
| 79 | } |
| 80 | |
Andy Lutomirski | aa4ea6755 | 2017-03-22 14:32:30 -0700 | [diff] [blame] | 81 | /* Provide the physical address of the GDT page. */ |
| 82 | static 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 Costa | 54cd0ea | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 87 | #ifdef CONFIG_X86_64 |
Glauber de Oliveira Costa | 507f90c | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 88 | |
| 89 | static inline void pack_gate(gate_desc *gate, unsigned type, unsigned long func, |
| 90 | unsigned dpl, unsigned ist, unsigned seg) |
| 91 | { |
Ingo Molnar | 9a3865b | 2011-05-27 09:29:32 +0200 | [diff] [blame] | 92 | 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 Costa | 507f90c | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Glauber de Oliveira Costa | 54cd0ea | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 104 | #else |
Glauber de Oliveira Costa | 507f90c | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 105 | static inline void pack_gate(gate_desc *gate, unsigned char type, |
Joe Perches | c1773a1 | 2008-03-23 01:01:58 -0700 | [diff] [blame] | 106 | unsigned long base, unsigned dpl, unsigned flags, |
| 107 | unsigned short seg) |
Glauber de Oliveira Costa | 507f90c | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 108 | { |
| 109 | gate->a = (seg << 16) | (base & 0xffff); |
Ingo Molnar | 9a3865b | 2011-05-27 09:29:32 +0200 | [diff] [blame] | 110 | gate->b = (base & 0xffff0000) | (((0x80 | type | (dpl << 5)) & 0xff) << 8); |
Glauber de Oliveira Costa | 507f90c | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 111 | } |
| 112 | |
Glauber de Oliveira Costa | 54cd0ea | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 113 | #endif |
| 114 | |
Glauber de Oliveira Costa | 746ff60 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 115 | static inline int desc_empty(const void *ptr) |
| 116 | { |
| 117 | const u32 *desc = ptr; |
Ingo Molnar | 9a3865b | 2011-05-27 09:29:32 +0200 | [diff] [blame] | 118 | |
Glauber de Oliveira Costa | 746ff60 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 119 | return !(desc[0] | desc[1]); |
| 120 | } |
| 121 | |
Glauber de Oliveira Costa | 54cd0ea | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 122 | #ifdef CONFIG_PARAVIRT |
| 123 | #include <asm/paravirt.h> |
| 124 | #else |
Ingo Molnar | 9a3865b | 2011-05-27 09:29:32 +0200 | [diff] [blame] | 125 | #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 Costa | 54cd0ea | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 130 | |
Ingo Molnar | 9a3865b | 2011-05-27 09:29:32 +0200 | [diff] [blame] | 131 | #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 Costa | 54cd0ea | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 134 | |
Ingo Molnar | 9a3865b | 2011-05-27 09:29:32 +0200 | [diff] [blame] | 135 | #define load_TLS(t, cpu) native_load_tls(t, cpu) |
| 136 | #define set_ldt native_set_ldt |
Glauber de Oliveira Costa | 54cd0ea | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 137 | |
Ingo Molnar | 9a3865b | 2011-05-27 09:29:32 +0200 | [diff] [blame] | 138 | #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 Fitzhardinge | 38ffbe6 | 2008-07-23 14:21:18 -0700 | [diff] [blame] | 141 | |
| 142 | static inline void paravirt_alloc_ldt(struct desc_struct *ldt, unsigned entries) |
| 143 | { |
| 144 | } |
| 145 | |
| 146 | static inline void paravirt_free_ldt(struct desc_struct *ldt, unsigned entries) |
| 147 | { |
| 148 | } |
| 149 | #endif /* CONFIG_PARAVIRT */ |
Glauber de Oliveira Costa | 54cd0ea | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 150 | |
Jaswinder Singh Rajput | 8229d75 | 2009-03-11 19:13:49 +0530 | [diff] [blame] | 151 | #define store_ldt(ldt) asm("sldt %0" : "=m"(ldt)) |
| 152 | |
Ingo Molnar | 9a3865b | 2011-05-27 09:29:32 +0200 | [diff] [blame] | 153 | static inline void native_write_idt_entry(gate_desc *idt, int entry, const gate_desc *gate) |
Glauber de Oliveira Costa | 54cd0ea | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 154 | { |
| 155 | memcpy(&idt[entry], gate, sizeof(*gate)); |
| 156 | } |
| 157 | |
Ingo Molnar | 9a3865b | 2011-05-27 09:29:32 +0200 | [diff] [blame] | 158 | static inline void native_write_ldt_entry(struct desc_struct *ldt, int entry, const void *desc) |
Glauber de Oliveira Costa | 54cd0ea | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 159 | { |
| 160 | memcpy(&ldt[entry], desc, 8); |
| 161 | } |
| 162 | |
Ingo Molnar | 9a3865b | 2011-05-27 09:29:32 +0200 | [diff] [blame] | 163 | static inline void |
| 164 | native_write_gdt_entry(struct desc_struct *gdt, int entry, const void *desc, int type) |
Glauber de Oliveira Costa | 54cd0ea | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 165 | { |
| 166 | unsigned int size; |
Ingo Molnar | 9a3865b | 2011-05-27 09:29:32 +0200 | [diff] [blame] | 167 | |
Glauber de Oliveira Costa | 54cd0ea | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 168 | switch (type) { |
Ingo Molnar | 9a3865b | 2011-05-27 09:29:32 +0200 | [diff] [blame] | 169 | 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 Costa | 54cd0ea | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 172 | } |
Ingo Molnar | 9a3865b | 2011-05-27 09:29:32 +0200 | [diff] [blame] | 173 | |
Glauber de Oliveira Costa | 54cd0ea | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 174 | memcpy(&gdt[entry], desc, size); |
| 175 | } |
| 176 | |
Glauber de Oliveira Costa | 54cd0ea | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 177 | static 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 Perches | c1773a1 | 2008-03-23 01:01:58 -0700 | [diff] [blame] | 183 | (limit & 0x000f0000) | ((type & 0xff) << 8) | |
| 184 | ((flags & 0xf) << 20); |
Glauber de Oliveira Costa | 54cd0ea | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 185 | desc->p = 1; |
| 186 | } |
| 187 | |
Glauber de Oliveira Costa | 54cd0ea | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 188 | |
Ingo Molnar | 9a3865b | 2011-05-27 09:29:32 +0200 | [diff] [blame] | 189 | static inline void set_tssldt_descriptor(void *d, unsigned long addr, unsigned type, unsigned size) |
Glauber de Oliveira Costa | c81c6ca | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 190 | { |
| 191 | #ifdef CONFIG_X86_64 |
Glauber de Oliveira Costa | f6e0eba | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 192 | struct ldttss_desc64 *desc = d; |
Ingo Molnar | 9a3865b | 2011-05-27 09:29:32 +0200 | [diff] [blame] | 193 | |
Glauber de Oliveira Costa | f6e0eba | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 194 | memset(desc, 0, sizeof(*desc)); |
Ingo Molnar | 9a3865b | 2011-05-27 09:29:32 +0200 | [diff] [blame] | 195 | |
| 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 Costa | c81c6ca | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 204 | #else |
Glauber de Oliveira Costa | f6e0eba | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 205 | pack_descriptor((struct desc_struct *)d, addr, size, 0x80 | type, 0); |
Glauber de Oliveira Costa | c81c6ca | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 206 | #endif |
| 207 | } |
| 208 | |
| 209 | static inline void __set_tss_desc(unsigned cpu, unsigned int entry, void *addr) |
| 210 | { |
Thomas Garnier | 69218e4 | 2017-03-14 10:05:07 -0700 | [diff] [blame] | 211 | struct desc_struct *d = get_cpu_gdt_rw(cpu); |
Glauber de Oliveira Costa | c81c6ca | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 212 | tss_desc tss; |
| 213 | |
Glauber de Oliveira Costa | f6e0eba | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 214 | set_tssldt_descriptor(&tss, (unsigned long)addr, DESC_TSS, |
Andy Lutomirski | 4f53ab1 | 2017-02-20 08:56:09 -0800 | [diff] [blame] | 215 | __KERNEL_TSS_LIMIT); |
Glauber de Oliveira Costa | c81c6ca | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 216 | 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 Costa | 54cd0ea | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 221 | static inline void native_set_ldt(const void *addr, unsigned int entries) |
| 222 | { |
| 223 | if (likely(entries == 0)) |
Joe Perches | c1773a1 | 2008-03-23 01:01:58 -0700 | [diff] [blame] | 224 | asm volatile("lldt %w0"::"q" (0)); |
Glauber de Oliveira Costa | 54cd0ea | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 225 | else { |
| 226 | unsigned cpu = smp_processor_id(); |
| 227 | ldt_desc ldt; |
| 228 | |
Michael Karcher | 5ac37f8 | 2008-07-11 18:04:46 +0200 | [diff] [blame] | 229 | set_tssldt_descriptor(&ldt, (unsigned long)addr, DESC_LDT, |
| 230 | entries * LDT_ENTRY_SIZE - 1); |
Thomas Garnier | 69218e4 | 2017-03-14 10:05:07 -0700 | [diff] [blame] | 231 | write_gdt_entry(get_cpu_gdt_rw(cpu), GDT_ENTRY_LDT, |
Glauber de Oliveira Costa | 54cd0ea | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 232 | &ldt, DESC_LDT); |
Joe Perches | c1773a1 | 2008-03-23 01:01:58 -0700 | [diff] [blame] | 233 | asm volatile("lldt %w0"::"q" (GDT_ENTRY_LDT*8)); |
Glauber de Oliveira Costa | 54cd0ea | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 237 | static inline void native_load_gdt(const struct desc_ptr *dtr) |
| 238 | { |
| 239 | asm volatile("lgdt %0"::"m" (*dtr)); |
| 240 | } |
| 241 | |
| 242 | static inline void native_load_idt(const struct desc_ptr *dtr) |
| 243 | { |
| 244 | asm volatile("lidt %0"::"m" (*dtr)); |
| 245 | } |
| 246 | |
| 247 | static inline void native_store_gdt(struct desc_ptr *dtr) |
| 248 | { |
| 249 | asm volatile("sgdt %0":"=m" (*dtr)); |
| 250 | } |
| 251 | |
| 252 | static 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 |
| 263 | static 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 Costa | 54cd0ea | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 286 | static inline void native_load_tr_desc(void) |
| 287 | { |
| 288 | asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS*8)); |
| 289 | } |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 290 | #endif |
| 291 | |
| 292 | static 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 | |
| 301 | static 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 Costa | 54cd0ea | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 309 | |
Andy Lutomirski | b7ceaec | 2017-02-22 07:36:16 -0800 | [diff] [blame] | 310 | DECLARE_PER_CPU(bool, __tss_limit_invalid); |
| 311 | |
Andy Lutomirski | b7ffc44 | 2017-02-20 08:56:14 -0800 | [diff] [blame] | 312 | static inline void force_reload_TR(void) |
| 313 | { |
Thomas Garnier | 69218e4 | 2017-03-14 10:05:07 -0700 | [diff] [blame] | 314 | struct desc_struct *d = get_current_gdt_rw(); |
Andy Lutomirski | b7ffc44 | 2017-02-20 08:56:14 -0800 | [diff] [blame] | 315 | 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 Lutomirski | b7ceaec | 2017-02-22 07:36:16 -0800 | [diff] [blame] | 327 | this_cpu_write(__tss_limit_invalid, false); |
Andy Lutomirski | b7ffc44 | 2017-02-20 08:56:14 -0800 | [diff] [blame] | 328 | } |
| 329 | |
Andy Lutomirski | b7ceaec | 2017-02-22 07:36:16 -0800 | [diff] [blame] | 330 | /* |
| 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 | */ |
| 335 | static inline void refresh_tss_limit(void) |
Andy Lutomirski | b7ffc44 | 2017-02-20 08:56:14 -0800 | [diff] [blame] | 336 | { |
| 337 | DEBUG_LOCKS_WARN_ON(preemptible()); |
| 338 | |
Andy Lutomirski | b7ceaec | 2017-02-22 07:36:16 -0800 | [diff] [blame] | 339 | if (unlikely(this_cpu_read(__tss_limit_invalid))) |
Andy Lutomirski | b7ffc44 | 2017-02-20 08:56:14 -0800 | [diff] [blame] | 340 | force_reload_TR(); |
Andy Lutomirski | b7ffc44 | 2017-02-20 08:56:14 -0800 | [diff] [blame] | 341 | } |
| 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 | */ |
| 352 | static 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 Lutomirski | b7ceaec | 2017-02-22 07:36:16 -0800 | [diff] [blame] | 359 | this_cpu_write(__tss_limit_invalid, true); |
Andy Lutomirski | b7ffc44 | 2017-02-20 08:56:14 -0800 | [diff] [blame] | 360 | } |
| 361 | |
Andy Lutomirski | e30ab18 | 2015-01-22 11:27:58 -0800 | [diff] [blame] | 362 | /* This intentionally ignores lm, since 32-bit apps don't have that field. */ |
| 363 | #define LDT_empty(info) \ |
Joe Perches | c1773a1 | 2008-03-23 01:01:58 -0700 | [diff] [blame] | 364 | ((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 Costa | 881c297 | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 372 | |
Andy Lutomirski | 3669ef9 | 2015-01-22 11:27:59 -0800 | [diff] [blame] | 373 | /* Lots of programs expect an all-zero user_desc to mean "no segment at all". */ |
| 374 | static 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 Costa | 881c297 | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 386 | static inline void clear_LDT(void) |
| 387 | { |
| 388 | set_ldt(NULL, 0); |
| 389 | } |
| 390 | |
Roland McGrath | 1bd5718 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 391 | static inline unsigned long get_desc_base(const struct desc_struct *desc) |
Glauber de Oliveira Costa | cc69785 | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 392 | { |
Chris Lalancette | 2c75910 | 2009-11-05 11:47:08 +0100 | [diff] [blame] | 393 | return (unsigned)(desc->base0 | ((desc->base1) << 16) | ((desc->base2) << 24)); |
Glauber de Oliveira Costa | cc69785 | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 394 | } |
Roland McGrath | 1bd5718 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 395 | |
Akinobu Mita | 5759474 | 2009-07-19 00:11:06 +0900 | [diff] [blame] | 396 | static 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 McGrath | 1bd5718 | 2008-01-30 13:31:51 +0100 | [diff] [blame] | 403 | static inline unsigned long get_desc_limit(const struct desc_struct *desc) |
| 404 | { |
| 405 | return desc->limit0 | (desc->limit << 16); |
| 406 | } |
| 407 | |
Akinobu Mita | 5759474 | 2009-07-19 00:11:06 +0900 | [diff] [blame] | 408 | static 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 Rostedt | 228bdaa | 2011-12-09 03:02:19 -0500 | [diff] [blame] | 414 | #ifdef CONFIG_X86_64 |
| 415 | static 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 Aguchi | 629f4f9 | 2013-06-20 11:45:44 -0400 | [diff] [blame] | 420 | write_idt_entry(debug_idt_table, gate, &s); |
Steven Rostedt | 228bdaa | 2011-12-09 03:02:19 -0500 | [diff] [blame] | 421 | } |
| 422 | #endif |
| 423 | |
Seiji Aguchi | cf910e8 | 2013-06-20 11:46:53 -0400 | [diff] [blame] | 424 | #ifdef CONFIG_TRACING |
| 425 | extern struct desc_ptr trace_idt_descr; |
| 426 | extern gate_desc trace_idt_table[]; |
| 427 | static inline void write_trace_idt_entry(int entry, const gate_desc *gate) |
| 428 | { |
| 429 | write_idt_entry(trace_idt_table, entry, gate); |
| 430 | } |
Seiji Aguchi | 25c74b1 | 2013-10-30 16:37:00 -0400 | [diff] [blame] | 431 | |
| 432 | static 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 Aguchi | cf910e8 | 2013-06-20 11:46:53 -0400 | [diff] [blame] | 444 | #else |
| 445 | static inline void write_trace_idt_entry(int entry, const gate_desc *gate) |
| 446 | { |
| 447 | } |
Seiji Aguchi | 25c74b1 | 2013-10-30 16:37:00 -0400 | [diff] [blame] | 448 | |
| 449 | #define _trace_set_gate(gate, type, addr, dpl, ist, seg) |
Seiji Aguchi | cf910e8 | 2013-06-20 11:46:53 -0400 | [diff] [blame] | 450 | #endif |
| 451 | |
Glauber de Oliveira Costa | 507f90c | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 452 | static inline void _set_gate(int gate, unsigned type, void *addr, |
Joe Perches | c1773a1 | 2008-03-23 01:01:58 -0700 | [diff] [blame] | 453 | unsigned dpl, unsigned ist, unsigned seg) |
Glauber de Oliveira Costa | 507f90c | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 454 | { |
| 455 | gate_desc s; |
Ingo Molnar | 9a3865b | 2011-05-27 09:29:32 +0200 | [diff] [blame] | 456 | |
Glauber de Oliveira Costa | 507f90c | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 457 | 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 Aguchi | cf910e8 | 2013-06-20 11:46:53 -0400 | [diff] [blame] | 463 | write_trace_idt_entry(gate, &s); |
Glauber de Oliveira Costa | 507f90c | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 464 | } |
| 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 Nan | 5eca745 | 2015-02-27 12:19:49 +0800 | [diff] [blame] | 472 | #define set_intr_gate_notrace(n, addr) \ |
Seiji Aguchi | 25c74b1 | 2013-10-30 16:37:00 -0400 | [diff] [blame] | 473 | do { \ |
| 474 | BUG_ON((unsigned)n > 0xFF); \ |
| 475 | _set_gate(n, GATE_INTERRUPT, (void *)addr, 0, 0, \ |
| 476 | __KERNEL_CS); \ |
Wang Nan | 5eca745 | 2015-02-27 12:19:49 +0800 | [diff] [blame] | 477 | } while (0) |
| 478 | |
| 479 | #define set_intr_gate(n, addr) \ |
| 480 | do { \ |
| 481 | set_intr_gate_notrace(n, addr); \ |
Seiji Aguchi | 25c74b1 | 2013-10-30 16:37:00 -0400 | [diff] [blame] | 482 | _trace_set_gate(n, GATE_INTERRUPT, (void *)trace_##addr,\ |
| 483 | 0, 0, __KERNEL_CS); \ |
| 484 | } while (0) |
Glauber de Oliveira Costa | 507f90c | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 485 | |
Yinghai Lu | b77b881 | 2008-12-19 15:23:44 -0800 | [diff] [blame] | 486 | /* used_vectors is BITMAP for irq is not managed by percpu vector_irq */ |
| 487 | extern unsigned long used_vectors[]; |
Alan Mayer | 305b92a | 2008-04-15 15:36:56 -0500 | [diff] [blame] | 488 | |
| 489 | static inline void alloc_system_vector(int vector) |
| 490 | { |
Thomas Gleixner | 05161b9 | 2017-08-28 08:47:18 +0200 | [diff] [blame^] | 491 | BUG_ON(vector < FIRST_SYSTEM_VECTOR); |
Yinghai Lu | b77b881 | 2008-12-19 15:23:44 -0800 | [diff] [blame] | 492 | if (!test_bit(vector, used_vectors)) { |
| 493 | set_bit(vector, used_vectors); |
Ingo Molnar | 9a3865b | 2011-05-27 09:29:32 +0200 | [diff] [blame] | 494 | } else { |
Alan Mayer | 305b92a | 2008-04-15 15:36:56 -0500 | [diff] [blame] | 495 | BUG(); |
Ingo Molnar | 9a3865b | 2011-05-27 09:29:32 +0200 | [diff] [blame] | 496 | } |
Alan Mayer | 305b92a | 2008-04-15 15:36:56 -0500 | [diff] [blame] | 497 | } |
| 498 | |
Seiji Aguchi | cf910e8 | 2013-06-20 11:46:53 -0400 | [diff] [blame] | 499 | #define alloc_intr_gate(n, addr) \ |
| 500 | do { \ |
| 501 | alloc_system_vector(n); \ |
Seiji Aguchi | 959c071 | 2013-10-30 16:36:08 -0400 | [diff] [blame] | 502 | set_intr_gate(n, addr); \ |
Seiji Aguchi | cf910e8 | 2013-06-20 11:46:53 -0400 | [diff] [blame] | 503 | } while (0) |
| 504 | |
Glauber de Oliveira Costa | 507f90c | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 505 | /* |
| 506 | * This routine sets up an interrupt gate at directory privilege level 3. |
| 507 | */ |
| 508 | static 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 Heukelum | 699d293 | 2008-10-03 22:00:32 +0200 | [diff] [blame] | 514 | static 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 Costa | 507f90c | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 520 | static 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 Costa | 507f90c | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 526 | static 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 | |
| 532 | static 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 Heukelum | 699d293 | 2008-10-03 22:00:32 +0200 | [diff] [blame] | 538 | static inline void set_system_intr_gate_ist(int n, void *addr, unsigned ist) |
Glauber de Oliveira Costa | 507f90c | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 539 | { |
| 540 | BUG_ON((unsigned)n > 0xFF); |
| 541 | _set_gate(n, GATE_INTERRUPT, addr, 0x3, ist, __KERNEL_CS); |
| 542 | } |
Glauber de Oliveira Costa | cc69785 | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 543 | |
Seiji Aguchi | 629f4f9 | 2013-06-20 11:45:44 -0400 | [diff] [blame] | 544 | #ifdef CONFIG_X86_64 |
| 545 | DECLARE_PER_CPU(u32, debug_idt_ctr); |
| 546 | static 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 | |
| 554 | static inline void load_debug_idt(void) |
| 555 | { |
| 556 | load_idt((const struct desc_ptr *)&debug_idt_descr); |
| 557 | } |
| 558 | #else |
| 559 | static inline bool is_debug_idt_enabled(void) |
| 560 | { |
| 561 | return false; |
| 562 | } |
| 563 | |
| 564 | static inline void load_debug_idt(void) |
| 565 | { |
| 566 | } |
| 567 | #endif |
| 568 | |
Seiji Aguchi | cf910e8 | 2013-06-20 11:46:53 -0400 | [diff] [blame] | 569 | #ifdef CONFIG_TRACING |
| 570 | extern atomic_t trace_idt_ctr; |
| 571 | static inline bool is_trace_idt_enabled(void) |
| 572 | { |
| 573 | if (atomic_read(&trace_idt_ctr)) |
| 574 | return true; |
| 575 | |
| 576 | return false; |
| 577 | } |
| 578 | |
| 579 | static inline void load_trace_idt(void) |
| 580 | { |
| 581 | load_idt((const struct desc_ptr *)&trace_idt_descr); |
| 582 | } |
| 583 | #else |
| 584 | static inline bool is_trace_idt_enabled(void) |
| 585 | { |
| 586 | return false; |
| 587 | } |
| 588 | |
| 589 | static inline void load_trace_idt(void) |
| 590 | { |
| 591 | } |
| 592 | #endif |
| 593 | |
Seiji Aguchi | 629f4f9 | 2013-06-20 11:45:44 -0400 | [diff] [blame] | 594 | /* |
Steven Rostedt (Red Hat) | 2b4bc78 | 2013-06-22 13:16:19 -0400 | [diff] [blame] | 595 | * The load_current_idt() must be called with interrupts disabled |
Seiji Aguchi | 629f4f9 | 2013-06-20 11:45:44 -0400 | [diff] [blame] | 596 | * to avoid races. That way the IDT will always be set back to the expected |
Steven Rostedt (Red Hat) | 2b4bc78 | 2013-06-22 13:16:19 -0400 | [diff] [blame] | 597 | * 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 Aguchi | 629f4f9 | 2013-06-20 11:45:44 -0400 | [diff] [blame] | 600 | */ |
| 601 | static inline void load_current_idt(void) |
| 602 | { |
Seiji Aguchi | 629f4f9 | 2013-06-20 11:45:44 -0400 | [diff] [blame] | 603 | if (is_debug_idt_enabled()) |
| 604 | load_debug_idt(); |
Seiji Aguchi | cf910e8 | 2013-06-20 11:46:53 -0400 | [diff] [blame] | 605 | else if (is_trace_idt_enabled()) |
| 606 | load_trace_idt(); |
Seiji Aguchi | 629f4f9 | 2013-06-20 11:45:44 -0400 | [diff] [blame] | 607 | else |
| 608 | load_idt((const struct desc_ptr *)&idt_descr); |
Seiji Aguchi | 629f4f9 | 2013-06-20 11:45:44 -0400 | [diff] [blame] | 609 | } |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 610 | #endif /* _ASM_X86_DESC_H */ |