x86: fixup numa 64 namespace
[powerpc.git] / include / asm-x86 / desc_64.h
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>
11 #include <linux/smp.h>
12 #include <asm/desc_defs.h>
13
14 #include <asm/segment.h>
15 #include <asm/mmu.h>
16
17 extern struct desc_struct cpu_gdt_table[GDT_ENTRIES];
18
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 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
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[];
39 extern struct desc_ptr cpu_gdt_descr[];
40
41 static inline void write_ldt_entry(struct desc_struct *ldt,
42                                    int entry, u32 entry_low, u32 entry_high)
43 {
44         __u32 *lp = (__u32 *)((entry << 3) + (char *)ldt);
45
46         lp[0] = entry_low;
47         lp[1] = entry_high;
48 }
49
50 /* the cpu gdt accessor */
51 #define cpu_gdt(_cpu) ((struct desc_struct *)cpu_gdt_descr[_cpu].address)
52
53 static inline void load_gdt(const struct desc_ptr *ptr)
54 {
55         asm volatile("lgdt %w0"::"m" (*ptr));
56 }
57
58 static inline void store_gdt(struct desc_ptr *ptr)
59 {
60        asm("sgdt %w0":"=m" (*ptr));
61 }
62
63 static inline void _set_gate(void *adr, unsigned type, unsigned long func,
64                              unsigned dpl, unsigned ist)
65 {
66         struct gate_struct s;
67
68         s.offset_low = PTR_LOW(func);
69         s.segment = __KERNEL_CS;
70         s.ist = ist;
71         s.p = 1;
72         s.dpl = dpl;
73         s.zero0 = 0;
74         s.zero1 = 0;
75         s.type = type;
76         s.offset_middle = PTR_MIDDLE(func);
77         s.offset_high = PTR_HIGH(func);
78         /*
79          * does not need to be atomic because it is only done once at
80          * setup time
81          */
82         memcpy(adr, &s, 16);
83 }
84
85 static inline void set_intr_gate(int nr, void *func)
86 {
87         BUG_ON((unsigned)nr > 0xFF);
88         _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, 0);
89 }
90
91 static inline void set_intr_gate_ist(int nr, void *func, unsigned ist)
92 {
93         BUG_ON((unsigned)nr > 0xFF);
94         _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, ist);
95 }
96
97 static inline void set_system_gate(int nr, void *func)
98 {
99         BUG_ON((unsigned)nr > 0xFF);
100         _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, 0);
101 }
102
103 static inline void set_system_gate_ist(int nr, void *func, unsigned ist)
104 {
105         _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, ist);
106 }
107
108 static inline void load_idt(const struct desc_ptr *ptr)
109 {
110         asm volatile("lidt %w0"::"m" (*ptr));
111 }
112
113 static inline void store_idt(struct desc_ptr *dtr)
114 {
115        asm("sidt %w0":"=m" (*dtr));
116 }
117
118 static inline void set_tssldt_descriptor(void *ptr, unsigned long tss,
119                                          unsigned type, unsigned size)
120 {
121         struct ldttss_desc d;
122
123         memset(&d, 0, sizeof(d));
124         d.limit0 = size & 0xFFFF;
125         d.base0 = PTR_LOW(tss);
126         d.base1 = PTR_MIDDLE(tss) & 0xFF;
127         d.type = type;
128         d.p = 1;
129         d.limit1 = (size >> 16) & 0xF;
130         d.base2 = (PTR_MIDDLE(tss) >> 8) & 0xFF;
131         d.base3 = PTR_HIGH(tss);
132         memcpy(ptr, &d, 16);
133 }
134
135 static inline void set_tss_desc(unsigned cpu, void *addr)
136 {
137         /*
138          * sizeof(unsigned long) coming from an extra "long" at the end
139          * of the iobitmap. See tss_struct definition in processor.h
140          *
141          * -1? seg base+limit should be pointing to the address of the
142          * last valid byte
143          */
144         set_tssldt_descriptor(&cpu_gdt(cpu)[GDT_ENTRY_TSS],
145                 (unsigned long)addr, DESC_TSS,
146                 IO_BITMAP_OFFSET + IO_BITMAP_BYTES + sizeof(unsigned long) - 1);
147 }
148
149 static inline void set_ldt_desc(unsigned cpu, void *addr, int size)
150 {
151         set_tssldt_descriptor(&cpu_gdt(cpu)[GDT_ENTRY_LDT], (unsigned long)addr,
152                               DESC_LDT, size * 8 - 1);
153 }
154
155 #define LDT_entry_a(info) \
156         ((((info)->base_addr & 0x0000ffff) << 16) | ((info)->limit & 0x0ffff))
157 /* Don't allow setting of the lm bit. It is useless anyways because
158    64bit system calls require __USER_CS. */
159 #define LDT_entry_b(info) \
160         (((info)->base_addr & 0xff000000) | \
161         (((info)->base_addr & 0x00ff0000) >> 16) | \
162         ((info)->limit & 0xf0000) | \
163         (((info)->read_exec_only ^ 1) << 9) | \
164         ((info)->contents << 10) | \
165         (((info)->seg_not_present ^ 1) << 15) | \
166         ((info)->seg_32bit << 22) | \
167         ((info)->limit_in_pages << 23) | \
168         ((info)->useable << 20) | \
169         /* ((info)->lm << 21) | */ \
170         0x7000)
171
172 #define LDT_empty(info) (\
173         (info)->base_addr       == 0    && \
174         (info)->limit           == 0    && \
175         (info)->contents        == 0    && \
176         (info)->read_exec_only  == 1    && \
177         (info)->seg_32bit       == 0    && \
178         (info)->limit_in_pages  == 0    && \
179         (info)->seg_not_present == 1    && \
180         (info)->useable         == 0    && \
181         (info)->lm              == 0)
182
183 static inline void load_TLS(struct thread_struct *t, unsigned int cpu)
184 {
185         unsigned int i;
186         u64 *gdt = (u64 *)(cpu_gdt(cpu) + GDT_ENTRY_TLS_MIN);
187
188         for (i = 0; i < GDT_ENTRY_TLS_ENTRIES; i++)
189                 gdt[i] = t->tls_array[i];
190 }
191
192 /*
193  * load one particular LDT into the current CPU
194  */
195 static inline void load_LDT_nolock(mm_context_t *pc, int cpu)
196 {
197         int count = pc->size;
198
199         if (likely(!count)) {
200                 clear_LDT();
201                 return;
202         }
203
204         set_ldt_desc(cpu, pc->ldt, count);
205         load_LDT_desc();
206 }
207
208 static inline void load_LDT(mm_context_t *pc)
209 {
210         int cpu = get_cpu();
211
212         load_LDT_nolock(pc, cpu);
213         put_cpu();
214 }
215
216 extern struct desc_ptr idt_descr;
217
218 #endif /* !__ASSEMBLY__ */
219
220 #endif