Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Written 2000 by Andi Kleen */ |
| 2 | #ifndef __ARCH_DESC_H |
| 3 | #define __ARCH_DESC_H |
| 4 | |
| 5 | #include <linux/threads.h> |
| 6 | #include <asm/ldt.h> |
| 7 | |
| 8 | #ifndef __ASSEMBLY__ |
| 9 | |
| 10 | #include <linux/string.h> |
Andrew Morton | 73a0b53 | 2005-09-16 19:27:55 -0700 | [diff] [blame] | 11 | #include <linux/smp.h> |
Avi Kivity | 249e83f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 12 | #include <asm/desc_defs.h> |
Andrew Morton | 73a0b53 | 2005-09-16 19:27:55 -0700 | [diff] [blame] | 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <asm/segment.h> |
| 15 | #include <asm/mmu.h> |
| 16 | |
Ravikiran G Thirumalai | c11efdf | 2006-01-11 22:43:57 +0100 | [diff] [blame] | 17 | extern struct desc_struct cpu_gdt_table[GDT_ENTRIES]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #define load_TR_desc() asm volatile("ltr %w0"::"r" (GDT_ENTRY_TSS*8)) |
| 20 | #define load_LDT_desc() asm volatile("lldt %w0"::"r" (GDT_ENTRY_LDT*8)) |
| 21 | #define clear_LDT() asm volatile("lldt %w0"::"r" (0)) |
| 22 | |
Glauber de Oliveira Costa | 9d1c6e7 | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 23 | static inline unsigned long __store_tr(void) |
| 24 | { |
| 25 | unsigned long tr; |
| 26 | |
| 27 | asm volatile ("str %w0":"=r" (tr)); |
| 28 | return tr; |
| 29 | } |
| 30 | |
| 31 | #define store_tr(tr) (tr) = __store_tr() |
| 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | /* |
| 34 | * This is the ldt that every process will get unless we need |
| 35 | * something other than this. |
| 36 | */ |
| 37 | extern struct desc_struct default_ldt[]; |
| 38 | extern struct gate_struct idt_table[]; |
Andi Kleen | a940199 | 2005-07-28 21:15:30 -0700 | [diff] [blame] | 39 | extern struct desc_ptr cpu_gdt_descr[]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Ravikiran G Thirumalai | c11efdf | 2006-01-11 22:43:57 +0100 | [diff] [blame] | 41 | /* the cpu gdt accessor */ |
| 42 | #define cpu_gdt(_cpu) ((struct desc_struct *)cpu_gdt_descr[_cpu].address) |
| 43 | |
Glauber de Oliveira Costa | 9d1c6e7 | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 44 | static inline void load_gdt(const struct desc_ptr *ptr) |
| 45 | { |
| 46 | asm volatile("lgdt %w0"::"m" (*ptr)); |
| 47 | } |
| 48 | |
| 49 | static inline void store_gdt(struct desc_ptr *ptr) |
| 50 | { |
| 51 | asm("sgdt %w0":"=m" (*ptr)); |
| 52 | } |
| 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | static inline void _set_gate(void *adr, unsigned type, unsigned long func, unsigned dpl, unsigned ist) |
| 55 | { |
| 56 | struct gate_struct s; |
| 57 | s.offset_low = PTR_LOW(func); |
| 58 | s.segment = __KERNEL_CS; |
| 59 | s.ist = ist; |
| 60 | s.p = 1; |
| 61 | s.dpl = dpl; |
| 62 | s.zero0 = 0; |
| 63 | s.zero1 = 0; |
| 64 | s.type = type; |
| 65 | s.offset_middle = PTR_MIDDLE(func); |
| 66 | s.offset_high = PTR_HIGH(func); |
| 67 | /* does not need to be atomic because it is only done once at setup time */ |
| 68 | memcpy(adr, &s, 16); |
| 69 | } |
| 70 | |
| 71 | static inline void set_intr_gate(int nr, void *func) |
| 72 | { |
James Cleverdon | 6004e1b | 2005-11-05 17:25:53 +0100 | [diff] [blame] | 73 | BUG_ON((unsigned)nr > 0xFF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, 0); |
| 75 | } |
| 76 | |
| 77 | static inline void set_intr_gate_ist(int nr, void *func, unsigned ist) |
| 78 | { |
James Cleverdon | 6004e1b | 2005-11-05 17:25:53 +0100 | [diff] [blame] | 79 | BUG_ON((unsigned)nr > 0xFF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, ist); |
| 81 | } |
| 82 | |
| 83 | static inline void set_system_gate(int nr, void *func) |
| 84 | { |
James Cleverdon | 6004e1b | 2005-11-05 17:25:53 +0100 | [diff] [blame] | 85 | BUG_ON((unsigned)nr > 0xFF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, 0); |
| 87 | } |
| 88 | |
Jan Beulich | b556b35 | 2006-01-11 22:43:00 +0100 | [diff] [blame] | 89 | static inline void set_system_gate_ist(int nr, void *func, unsigned ist) |
| 90 | { |
| 91 | _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, ist); |
| 92 | } |
| 93 | |
Glauber de Oliveira Costa | 9d1c6e7 | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 94 | static inline void load_idt(const struct desc_ptr *ptr) |
| 95 | { |
| 96 | asm volatile("lidt %w0"::"m" (*ptr)); |
| 97 | } |
| 98 | |
| 99 | static inline void store_idt(struct desc_ptr *dtr) |
| 100 | { |
| 101 | asm("sidt %w0":"=m" (*dtr)); |
| 102 | } |
| 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | static inline void set_tssldt_descriptor(void *ptr, unsigned long tss, unsigned type, |
| 105 | unsigned size) |
| 106 | { |
| 107 | struct ldttss_desc d; |
| 108 | memset(&d,0,sizeof(d)); |
| 109 | d.limit0 = size & 0xFFFF; |
| 110 | d.base0 = PTR_LOW(tss); |
| 111 | d.base1 = PTR_MIDDLE(tss) & 0xFF; |
| 112 | d.type = type; |
| 113 | d.p = 1; |
| 114 | d.limit1 = (size >> 16) & 0xF; |
| 115 | d.base2 = (PTR_MIDDLE(tss) >> 8) & 0xFF; |
| 116 | d.base3 = PTR_HIGH(tss); |
| 117 | memcpy(ptr, &d, 16); |
| 118 | } |
| 119 | |
| 120 | static inline void set_tss_desc(unsigned cpu, void *addr) |
| 121 | { |
Siddha, Suresh B | 4793635 | 2005-11-13 16:06:21 -0800 | [diff] [blame] | 122 | /* |
| 123 | * sizeof(unsigned long) coming from an extra "long" at the end |
| 124 | * of the iobitmap. See tss_struct definition in processor.h |
| 125 | * |
| 126 | * -1? seg base+limit should be pointing to the address of the |
| 127 | * last valid byte |
| 128 | */ |
Ravikiran G Thirumalai | c11efdf | 2006-01-11 22:43:57 +0100 | [diff] [blame] | 129 | set_tssldt_descriptor(&cpu_gdt(cpu)[GDT_ENTRY_TSS], |
Siddha, Suresh B | 4793635 | 2005-11-13 16:06:21 -0800 | [diff] [blame] | 130 | (unsigned long)addr, DESC_TSS, |
| 131 | IO_BITMAP_OFFSET + IO_BITMAP_BYTES + sizeof(unsigned long) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static inline void set_ldt_desc(unsigned cpu, void *addr, int size) |
| 135 | { |
Ravikiran G Thirumalai | c11efdf | 2006-01-11 22:43:57 +0100 | [diff] [blame] | 136 | set_tssldt_descriptor(&cpu_gdt(cpu)[GDT_ENTRY_LDT], (unsigned long)addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | DESC_LDT, size * 8 - 1); |
| 138 | } |
| 139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | #define LDT_entry_a(info) \ |
| 141 | ((((info)->base_addr & 0x0000ffff) << 16) | ((info)->limit & 0x0ffff)) |
| 142 | /* Don't allow setting of the lm bit. It is useless anyways because |
| 143 | 64bit system calls require __USER_CS. */ |
| 144 | #define LDT_entry_b(info) \ |
| 145 | (((info)->base_addr & 0xff000000) | \ |
| 146 | (((info)->base_addr & 0x00ff0000) >> 16) | \ |
| 147 | ((info)->limit & 0xf0000) | \ |
| 148 | (((info)->read_exec_only ^ 1) << 9) | \ |
| 149 | ((info)->contents << 10) | \ |
| 150 | (((info)->seg_not_present ^ 1) << 15) | \ |
| 151 | ((info)->seg_32bit << 22) | \ |
| 152 | ((info)->limit_in_pages << 23) | \ |
| 153 | ((info)->useable << 20) | \ |
| 154 | /* ((info)->lm << 21) | */ \ |
| 155 | 0x7000) |
| 156 | |
| 157 | #define LDT_empty(info) (\ |
| 158 | (info)->base_addr == 0 && \ |
| 159 | (info)->limit == 0 && \ |
| 160 | (info)->contents == 0 && \ |
| 161 | (info)->read_exec_only == 1 && \ |
| 162 | (info)->seg_32bit == 0 && \ |
| 163 | (info)->limit_in_pages == 0 && \ |
| 164 | (info)->seg_not_present == 1 && \ |
| 165 | (info)->useable == 0 && \ |
| 166 | (info)->lm == 0) |
| 167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | static inline void load_TLS(struct thread_struct *t, unsigned int cpu) |
| 169 | { |
Rusty Russell | eab0c72 | 2007-05-02 19:27:09 +0200 | [diff] [blame] | 170 | unsigned int i; |
Ravikiran G Thirumalai | c11efdf | 2006-01-11 22:43:57 +0100 | [diff] [blame] | 171 | u64 *gdt = (u64 *)(cpu_gdt(cpu) + GDT_ENTRY_TLS_MIN); |
Rusty Russell | eab0c72 | 2007-05-02 19:27:09 +0200 | [diff] [blame] | 172 | |
| 173 | for (i = 0; i < GDT_ENTRY_TLS_ENTRIES; i++) |
| 174 | gdt[i] = t->tls_array[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /* |
| 178 | * load one particular LDT into the current CPU |
| 179 | */ |
Adrian Bunk | 9c0aa0f | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 180 | static inline void load_LDT_nolock (mm_context_t *pc, int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | { |
| 182 | int count = pc->size; |
| 183 | |
| 184 | if (likely(!count)) { |
| 185 | clear_LDT(); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | set_ldt_desc(cpu, pc->ldt, count); |
| 190 | load_LDT_desc(); |
| 191 | } |
| 192 | |
| 193 | static inline void load_LDT(mm_context_t *pc) |
| 194 | { |
| 195 | int cpu = get_cpu(); |
| 196 | load_LDT_nolock(pc, cpu); |
| 197 | put_cpu(); |
| 198 | } |
| 199 | |
| 200 | extern struct desc_ptr idt_descr; |
| 201 | |
| 202 | #endif /* !__ASSEMBLY__ */ |
| 203 | |
| 204 | #endif |