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