fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / include / asm-x86_64 / mach-xen / asm / desc.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 extern struct desc_ptr idt_descr, cpu_gdt_descr[NR_CPUS];
20
21 #define load_TR_desc() asm volatile("ltr %w0"::"r" (GDT_ENTRY_TSS*8))
22 #define load_LDT_desc() asm volatile("lldt %w0"::"r" (GDT_ENTRY_LDT*8))
23
24 static inline void clear_LDT(void)
25 {
26         int cpu = get_cpu();
27
28         /*
29          * NB. We load the default_ldt for lcall7/27 handling on demand, as
30          * it slows down context switching. Noone uses it anyway.
31          */
32         cpu = cpu;              /* XXX avoid compiler warning */
33         xen_set_ldt(0UL, 0);
34         put_cpu();
35 }
36
37 /*
38  * This is the ldt that every process will get unless we need
39  * something other than this.
40  */
41 extern struct desc_struct default_ldt[];
42 #ifndef CONFIG_X86_NO_IDT
43 extern struct gate_struct idt_table[]; 
44 #endif
45 extern struct desc_ptr cpu_gdt_descr[];
46
47 /* the cpu gdt accessor */
48 #define cpu_gdt(_cpu) ((struct desc_struct *)cpu_gdt_descr[_cpu].address)
49
50 static inline void _set_gate(void *adr, unsigned type, unsigned long func, unsigned dpl, unsigned ist)  
51 {
52         struct gate_struct s;   
53         s.offset_low = PTR_LOW(func); 
54         s.segment = __KERNEL_CS;
55         s.ist = ist; 
56         s.p = 1;
57         s.dpl = dpl; 
58         s.zero0 = 0;
59         s.zero1 = 0; 
60         s.type = type; 
61         s.offset_middle = PTR_MIDDLE(func); 
62         s.offset_high = PTR_HIGH(func); 
63         /* does not need to be atomic because it is only done once at setup time */ 
64         memcpy(adr, &s, 16); 
65
66
67 #ifndef CONFIG_X86_NO_IDT
68 static inline void set_intr_gate(int nr, void *func) 
69
70         BUG_ON((unsigned)nr > 0xFF);
71         _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, 0); 
72
73
74 static inline void set_intr_gate_ist(int nr, void *func, unsigned ist) 
75
76         BUG_ON((unsigned)nr > 0xFF);
77         _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, ist); 
78
79
80 static inline void set_system_gate(int nr, void *func) 
81
82         BUG_ON((unsigned)nr > 0xFF);
83         _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, 0); 
84
85
86 static inline void set_system_gate_ist(int nr, void *func, unsigned ist)
87 {
88         _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, ist);
89 }
90 #endif
91
92 static inline void set_tssldt_descriptor(void *ptr, unsigned long tss, unsigned type, 
93                                          unsigned size) 
94
95         struct ldttss_desc d;
96         memset(&d,0,sizeof(d)); 
97         d.limit0 = size & 0xFFFF;
98         d.base0 = PTR_LOW(tss); 
99         d.base1 = PTR_MIDDLE(tss) & 0xFF; 
100         d.type = type;
101         d.p = 1; 
102         d.limit1 = (size >> 16) & 0xF;
103         d.base2 = (PTR_MIDDLE(tss) >> 8) & 0xFF; 
104         d.base3 = PTR_HIGH(tss); 
105         memcpy(ptr, &d, 16); 
106 }
107
108 #ifndef CONFIG_X86_NO_TSS
109 static inline void set_tss_desc(unsigned cpu, void *addr)
110
111         /*
112          * sizeof(unsigned long) coming from an extra "long" at the end
113          * of the iobitmap. See tss_struct definition in processor.h
114          *
115          * -1? seg base+limit should be pointing to the address of the
116          * last valid byte
117          */
118         set_tssldt_descriptor(&cpu_gdt(cpu)[GDT_ENTRY_TSS],
119                 (unsigned long)addr, DESC_TSS,
120                 IO_BITMAP_OFFSET + IO_BITMAP_BYTES + sizeof(unsigned long) - 1);
121
122 #endif
123
124 static inline void set_ldt_desc(unsigned cpu, void *addr, int size)
125
126         set_tssldt_descriptor(&cpu_gdt(cpu)[GDT_ENTRY_LDT], (unsigned long)addr,
127                               DESC_LDT, size * 8 - 1);
128 }
129
130 static inline void set_seg_base(unsigned cpu, int entry, void *base)
131
132         struct desc_struct *d = &cpu_gdt(cpu)[entry];
133         u32 addr = (u32)(u64)base;
134         BUG_ON((u64)base >> 32); 
135         d->base0 = addr & 0xffff;
136         d->base1 = (addr >> 16) & 0xff;
137         d->base2 = (addr >> 24) & 0xff;
138
139
140 #define LDT_entry_a(info) \
141         ((((info)->base_addr & 0x0000ffff) << 16) | ((info)->limit & 0x0ffff))
142 /* Don't allow setting of the lm bit. It is useless anyways because 
143    64bit system calls require __USER_CS. */ 
144 #define LDT_entry_b(info) \
145         (((info)->base_addr & 0xff000000) | \
146         (((info)->base_addr & 0x00ff0000) >> 16) | \
147         ((info)->limit & 0xf0000) | \
148         (((info)->read_exec_only ^ 1) << 9) | \
149         ((info)->contents << 10) | \
150         (((info)->seg_not_present ^ 1) << 15) | \
151         ((info)->seg_32bit << 22) | \
152         ((info)->limit_in_pages << 23) | \
153         ((info)->useable << 20) | \
154         /* ((info)->lm << 21) | */ \
155         0x7000)
156
157 #define LDT_empty(info) (\
158         (info)->base_addr       == 0    && \
159         (info)->limit           == 0    && \
160         (info)->contents        == 0    && \
161         (info)->read_exec_only  == 1    && \
162         (info)->seg_32bit       == 0    && \
163         (info)->limit_in_pages  == 0    && \
164         (info)->seg_not_present == 1    && \
165         (info)->useable         == 0    && \
166         (info)->lm              == 0)
167
168 #if TLS_SIZE != 24
169 # error update this code.
170 #endif
171
172 static inline void load_TLS(struct thread_struct *t, unsigned int cpu)
173 {
174 #if 0
175         u64 *gdt = (u64 *)(cpu_gdt(cpu) + GDT_ENTRY_TLS_MIN);
176         gdt[0] = t->tls_array[0];
177         gdt[1] = t->tls_array[1];
178         gdt[2] = t->tls_array[2];
179 #endif
180 #define C(i) \
181         HYPERVISOR_update_descriptor(virt_to_machine(&cpu_gdt(cpu)[GDT_ENTRY_TLS_MIN + i]), t->tls_array[i])
182
183         C(0); C(1); C(2);
184 #undef C
185
186
187 /*
188  * load one particular LDT into the current CPU
189  */
190 static inline void load_LDT_nolock (mm_context_t *pc, int cpu)
191 {
192         void *segments = pc->ldt;
193         int count = pc->size;
194
195         if (likely(!count))
196                 segments = NULL;
197
198         xen_set_ldt((unsigned long)segments, count);
199 }
200
201 static inline void load_LDT(mm_context_t *pc)
202 {
203         int cpu = get_cpu();
204         load_LDT_nolock(pc, cpu);
205         put_cpu();
206 }
207
208 extern struct desc_ptr idt_descr;
209
210 #endif /* !__ASSEMBLY__ */
211
212 #endif