Commit | Line | Data |
---|---|---|
1da177e4 LT |
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> | |
73a0b538 | 11 | #include <linux/smp.h> |
249e83fe | 12 | #include <asm/desc_defs.h> |
73a0b538 | 13 | |
1da177e4 LT |
14 | #include <asm/segment.h> |
15 | #include <asm/mmu.h> | |
16 | ||
c11efdf9 | 17 | extern struct desc_struct cpu_gdt_table[GDT_ENTRIES]; |
1da177e4 | 18 | |
1da177e4 LT |
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[]; | |
a940199f | 29 | extern struct desc_ptr cpu_gdt_descr[]; |
1da177e4 | 30 | |
c11efdf9 RT |
31 | /* the cpu gdt accessor */ |
32 | #define cpu_gdt(_cpu) ((struct desc_struct *)cpu_gdt_descr[_cpu].address) | |
33 | ||
1da177e4 LT |
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 | { | |
6004e1b7 | 53 | BUG_ON((unsigned)nr > 0xFF); |
1da177e4 LT |
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 | { | |
6004e1b7 | 59 | BUG_ON((unsigned)nr > 0xFF); |
1da177e4 LT |
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 | { | |
6004e1b7 | 65 | BUG_ON((unsigned)nr > 0xFF); |
1da177e4 LT |
66 | _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, 0); |
67 | } | |
68 | ||
b556b35e JB |
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 | ||
1da177e4 LT |
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 | { | |
47936357 SS |
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 | */ | |
c11efdf9 | 99 | set_tssldt_descriptor(&cpu_gdt(cpu)[GDT_ENTRY_TSS], |
47936357 SS |
100 | (unsigned long)addr, DESC_TSS, |
101 | IO_BITMAP_OFFSET + IO_BITMAP_BYTES + sizeof(unsigned long) - 1); | |
1da177e4 LT |
102 | } |
103 | ||
104 | static inline void set_ldt_desc(unsigned cpu, void *addr, int size) | |
105 | { | |
c11efdf9 | 106 | set_tssldt_descriptor(&cpu_gdt(cpu)[GDT_ENTRY_LDT], (unsigned long)addr, |
1da177e4 LT |
107 | DESC_LDT, size * 8 - 1); |
108 | } | |
109 | ||
1da177e4 LT |
110 | #define LDT_entry_a(info) \ |
111 | ((((info)->base_addr & 0x0000ffff) << 16) | ((info)->limit & 0x0ffff)) | |
112 | /* Don't allow setting of the lm bit. It is useless anyways because | |
113 | 64bit system calls require __USER_CS. */ | |
114 | #define LDT_entry_b(info) \ | |
115 | (((info)->base_addr & 0xff000000) | \ | |
116 | (((info)->base_addr & 0x00ff0000) >> 16) | \ | |
117 | ((info)->limit & 0xf0000) | \ | |
118 | (((info)->read_exec_only ^ 1) << 9) | \ | |
119 | ((info)->contents << 10) | \ | |
120 | (((info)->seg_not_present ^ 1) << 15) | \ | |
121 | ((info)->seg_32bit << 22) | \ | |
122 | ((info)->limit_in_pages << 23) | \ | |
123 | ((info)->useable << 20) | \ | |
124 | /* ((info)->lm << 21) | */ \ | |
125 | 0x7000) | |
126 | ||
127 | #define LDT_empty(info) (\ | |
128 | (info)->base_addr == 0 && \ | |
129 | (info)->limit == 0 && \ | |
130 | (info)->contents == 0 && \ | |
131 | (info)->read_exec_only == 1 && \ | |
132 | (info)->seg_32bit == 0 && \ | |
133 | (info)->limit_in_pages == 0 && \ | |
134 | (info)->seg_not_present == 1 && \ | |
135 | (info)->useable == 0 && \ | |
136 | (info)->lm == 0) | |
137 | ||
1da177e4 LT |
138 | static inline void load_TLS(struct thread_struct *t, unsigned int cpu) |
139 | { | |
eab0c72a | 140 | unsigned int i; |
c11efdf9 | 141 | u64 *gdt = (u64 *)(cpu_gdt(cpu) + GDT_ENTRY_TLS_MIN); |
eab0c72a RR |
142 | |
143 | for (i = 0; i < GDT_ENTRY_TLS_ENTRIES; i++) | |
144 | gdt[i] = t->tls_array[i]; | |
1da177e4 LT |
145 | } |
146 | ||
147 | /* | |
148 | * load one particular LDT into the current CPU | |
149 | */ | |
9c0aa0f9 | 150 | static inline void load_LDT_nolock (mm_context_t *pc, int cpu) |
1da177e4 LT |
151 | { |
152 | int count = pc->size; | |
153 | ||
154 | if (likely(!count)) { | |
155 | clear_LDT(); | |
156 | return; | |
157 | } | |
158 | ||
159 | set_ldt_desc(cpu, pc->ldt, count); | |
160 | load_LDT_desc(); | |
161 | } | |
162 | ||
163 | static inline void load_LDT(mm_context_t *pc) | |
164 | { | |
165 | int cpu = get_cpu(); | |
166 | load_LDT_nolock(pc, cpu); | |
167 | put_cpu(); | |
168 | } | |
169 | ||
170 | extern struct desc_ptr idt_descr; | |
171 | ||
172 | #endif /* !__ASSEMBLY__ */ | |
173 | ||
174 | #endif |