Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef __ARCH_DESC_H |
| 2 | #define __ARCH_DESC_H |
| 3 | |
| 4 | #include <asm/ldt.h> |
| 5 | #include <asm/segment.h> |
| 6 | |
| 7 | #define CPU_16BIT_STACK_SIZE 1024 |
| 8 | |
| 9 | #ifndef __ASSEMBLY__ |
| 10 | |
| 11 | #include <linux/preempt.h> |
| 12 | #include <linux/smp.h> |
| 13 | #include <linux/percpu.h> |
| 14 | |
| 15 | #include <asm/mmu.h> |
| 16 | |
| 17 | extern struct desc_struct cpu_gdt_table[GDT_ENTRIES]; |
Zachary Amsden | 251e691 | 2005-10-30 14:59:34 -0800 | [diff] [blame] | 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | DECLARE_PER_CPU(unsigned char, cpu_16bit_stack[CPU_16BIT_STACK_SIZE]); |
| 20 | |
| 21 | struct Xgt_desc_struct { |
| 22 | unsigned short size; |
| 23 | unsigned long address __attribute__((packed)); |
| 24 | unsigned short pad; |
| 25 | } __attribute__ ((packed)); |
| 26 | |
James Bottomley | 2b932f6 | 2006-02-24 13:04:14 -0800 | [diff] [blame] | 27 | extern struct Xgt_desc_struct idt_descr; |
| 28 | DECLARE_PER_CPU(struct Xgt_desc_struct, cpu_gdt_descr); |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Zachary Amsden | 7c4cb60 | 2006-01-06 00:11:47 -0800 | [diff] [blame] | 31 | static inline struct desc_struct *get_cpu_gdt_table(unsigned int cpu) |
| 32 | { |
James Bottomley | 2b932f6 | 2006-02-24 13:04:14 -0800 | [diff] [blame] | 33 | return (struct desc_struct *)per_cpu(cpu_gdt_descr, cpu).address; |
Zachary Amsden | 7c4cb60 | 2006-01-06 00:11:47 -0800 | [diff] [blame] | 34 | } |
| 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | /* |
| 37 | * This is the ldt that every process will get unless we need |
| 38 | * something other than this. |
| 39 | */ |
| 40 | extern struct desc_struct default_ldt[]; |
Rusty Russell | 522e93e | 2006-09-26 10:52:35 +0200 | [diff] [blame] | 41 | extern struct desc_struct idt_table[]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | extern void set_intr_gate(unsigned int irq, void * addr); |
| 43 | |
Rusty Russell | 522e93e | 2006-09-26 10:52:35 +0200 | [diff] [blame] | 44 | static inline void pack_descriptor(__u32 *a, __u32 *b, |
| 45 | unsigned long base, unsigned long limit, unsigned char type, unsigned char flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | { |
Rusty Russell | 522e93e | 2006-09-26 10:52:35 +0200 | [diff] [blame] | 47 | *a = ((base & 0xffff) << 16) | (limit & 0xffff); |
| 48 | *b = (base & 0xff000000) | ((base & 0xff0000) >> 16) | |
Jeremy Fitzhardinge | 2817716 | 2006-09-26 10:52:40 +0200 | [diff] [blame^] | 49 | (limit & 0x000f0000) | ((type & 0xff) << 8) | ((flags & 0xf) << 20); |
Rusty Russell | 522e93e | 2006-09-26 10:52:35 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static inline void pack_gate(__u32 *a, __u32 *b, |
| 53 | unsigned long base, unsigned short seg, unsigned char type, unsigned char flags) |
| 54 | { |
| 55 | *a = (seg << 16) | (base & 0xffff); |
| 56 | *b = (base & 0xffff0000) | ((type & 0xff) << 8) | (flags & 0xff); |
| 57 | } |
| 58 | |
| 59 | #define DESCTYPE_LDT 0x82 /* present, system, DPL-0, LDT */ |
| 60 | #define DESCTYPE_TSS 0x89 /* present, system, DPL-0, 32-bit TSS */ |
| 61 | #define DESCTYPE_TASK 0x85 /* present, system, DPL-0, task gate */ |
| 62 | #define DESCTYPE_INT 0x8e /* present, system, DPL-0, interrupt gate */ |
| 63 | #define DESCTYPE_TRAP 0x8f /* present, system, DPL-0, trap gate */ |
| 64 | #define DESCTYPE_DPL3 0x60 /* DPL-3 */ |
| 65 | #define DESCTYPE_S 0x10 /* !system */ |
| 66 | |
| 67 | #define load_TR_desc() __asm__ __volatile__("ltr %w0"::"q" (GDT_ENTRY_TSS*8)) |
| 68 | #define load_LDT_desc() __asm__ __volatile__("lldt %w0"::"q" (GDT_ENTRY_LDT*8)) |
| 69 | |
| 70 | #define load_gdt(dtr) __asm__ __volatile("lgdt %0"::"m" (*dtr)) |
| 71 | #define load_idt(dtr) __asm__ __volatile("lidt %0"::"m" (*dtr)) |
| 72 | #define load_tr(tr) __asm__ __volatile("ltr %0"::"m" (tr)) |
| 73 | #define load_ldt(ldt) __asm__ __volatile("lldt %0"::"m" (ldt)) |
| 74 | |
| 75 | #define store_gdt(dtr) __asm__ ("sgdt %0":"=m" (*dtr)) |
| 76 | #define store_idt(dtr) __asm__ ("sidt %0":"=m" (*dtr)) |
| 77 | #define store_tr(tr) __asm__ ("str %0":"=m" (tr)) |
| 78 | #define store_ldt(ldt) __asm__ ("sldt %0":"=m" (ldt)) |
| 79 | |
| 80 | #if TLS_SIZE != 24 |
| 81 | # error update this code. |
| 82 | #endif |
| 83 | |
| 84 | static inline void load_TLS(struct thread_struct *t, unsigned int cpu) |
| 85 | { |
| 86 | #define C(i) get_cpu_gdt_table(cpu)[GDT_ENTRY_TLS_MIN + i] = t->tls_array[i] |
| 87 | C(0); C(1); C(2); |
| 88 | #undef C |
| 89 | } |
| 90 | |
| 91 | static inline void write_dt_entry(void *dt, int entry, __u32 entry_a, __u32 entry_b) |
| 92 | { |
| 93 | __u32 *lp = (__u32 *)((char *)dt + entry*8); |
| 94 | *lp = entry_a; |
| 95 | *(lp+1) = entry_b; |
| 96 | } |
| 97 | |
| 98 | #define write_ldt_entry(dt, entry, a, b) write_dt_entry(dt, entry, a, b) |
| 99 | #define write_gdt_entry(dt, entry, a, b) write_dt_entry(dt, entry, a, b) |
| 100 | #define write_idt_entry(dt, entry, a, b) write_dt_entry(dt, entry, a, b) |
| 101 | |
| 102 | static inline void _set_gate(int gate, unsigned int type, void *addr, unsigned short seg) |
| 103 | { |
| 104 | __u32 a, b; |
| 105 | pack_gate(&a, &b, (unsigned long)addr, seg, type, 0); |
| 106 | write_idt_entry(idt_table, gate, a, b); |
| 107 | } |
| 108 | |
| 109 | static inline void __set_tss_desc(unsigned int cpu, unsigned int entry, const void *addr) |
| 110 | { |
| 111 | __u32 a, b; |
| 112 | pack_descriptor(&a, &b, (unsigned long)addr, |
| 113 | offsetof(struct tss_struct, __cacheline_filler) - 1, |
| 114 | DESCTYPE_TSS, 0); |
| 115 | write_gdt_entry(get_cpu_gdt_table(cpu), entry, a, b); |
| 116 | } |
| 117 | |
| 118 | static inline void set_ldt_desc(unsigned int cpu, void *addr, unsigned int entries) |
| 119 | { |
| 120 | __u32 a, b; |
| 121 | pack_descriptor(&a, &b, (unsigned long)addr, |
| 122 | entries * sizeof(struct desc_struct) - 1, |
| 123 | DESCTYPE_LDT, 0); |
| 124 | write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_LDT, a, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | #define set_tss_desc(cpu,addr) __set_tss_desc(cpu, GDT_ENTRY_TSS, addr) |
| 128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | #define LDT_entry_a(info) \ |
| 130 | ((((info)->base_addr & 0x0000ffff) << 16) | ((info)->limit & 0x0ffff)) |
| 131 | |
| 132 | #define LDT_entry_b(info) \ |
| 133 | (((info)->base_addr & 0xff000000) | \ |
| 134 | (((info)->base_addr & 0x00ff0000) >> 16) | \ |
| 135 | ((info)->limit & 0xf0000) | \ |
| 136 | (((info)->read_exec_only ^ 1) << 9) | \ |
| 137 | ((info)->contents << 10) | \ |
| 138 | (((info)->seg_not_present ^ 1) << 15) | \ |
| 139 | ((info)->seg_32bit << 22) | \ |
| 140 | ((info)->limit_in_pages << 23) | \ |
| 141 | ((info)->useable << 20) | \ |
| 142 | 0x7000) |
| 143 | |
| 144 | #define LDT_empty(info) (\ |
| 145 | (info)->base_addr == 0 && \ |
| 146 | (info)->limit == 0 && \ |
| 147 | (info)->contents == 0 && \ |
| 148 | (info)->read_exec_only == 1 && \ |
| 149 | (info)->seg_32bit == 0 && \ |
| 150 | (info)->limit_in_pages == 0 && \ |
| 151 | (info)->seg_not_present == 1 && \ |
| 152 | (info)->useable == 0 ) |
| 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | static inline void clear_LDT(void) |
| 155 | { |
| 156 | int cpu = get_cpu(); |
| 157 | |
| 158 | set_ldt_desc(cpu, &default_ldt[0], 5); |
| 159 | load_LDT_desc(); |
| 160 | put_cpu(); |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * load one particular LDT into the current CPU |
| 165 | */ |
| 166 | static inline void load_LDT_nolock(mm_context_t *pc, int cpu) |
| 167 | { |
| 168 | void *segments = pc->ldt; |
| 169 | int count = pc->size; |
| 170 | |
| 171 | if (likely(!count)) { |
| 172 | segments = &default_ldt[0]; |
| 173 | count = 5; |
| 174 | } |
| 175 | |
| 176 | set_ldt_desc(cpu, segments, count); |
| 177 | load_LDT_desc(); |
| 178 | } |
| 179 | |
| 180 | static inline void load_LDT(mm_context_t *pc) |
| 181 | { |
| 182 | int cpu = get_cpu(); |
| 183 | load_LDT_nolock(pc, cpu); |
| 184 | put_cpu(); |
| 185 | } |
| 186 | |
| 187 | static inline unsigned long get_desc_base(unsigned long *desc) |
| 188 | { |
| 189 | unsigned long base; |
| 190 | base = ((desc[0] >> 16) & 0x0000ffff) | |
| 191 | ((desc[1] << 16) & 0x00ff0000) | |
| 192 | (desc[1] & 0xff000000); |
| 193 | return base; |
| 194 | } |
| 195 | |
| 196 | #endif /* !__ASSEMBLY__ */ |
| 197 | |
| 198 | #endif |