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