x86: modify get_desc_base
[linux-2.6-microblaze.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
13 #include <asm/segment.h>
14
15 extern struct desc_struct cpu_gdt_table[GDT_ENTRIES];
16
17 #define load_TR_desc() asm volatile("ltr %w0"::"r" (GDT_ENTRY_TSS*8))
18 #define load_LDT_desc() asm volatile("lldt %w0"::"r" (GDT_ENTRY_LDT*8))
19
20 static inline unsigned long __store_tr(void)
21 {
22        unsigned long tr;
23
24        asm volatile ("str %w0":"=r" (tr));
25        return tr;
26 }
27
28 #define store_tr(tr) (tr) = __store_tr()
29
30 extern struct desc_ptr cpu_gdt_descr[];
31
32 static inline void write_ldt_entry(struct desc_struct *ldt,
33                                    int entry, void *ptr)
34 {
35         memcpy(&ldt[entry], ptr, 8);
36 }
37
38 /* the cpu gdt accessor */
39 #define get_cpu_gdt_table(x) ((struct desc_struct *)cpu_gdt_descr[x].address)
40
41 static inline void load_gdt(const struct desc_ptr *ptr)
42 {
43         asm volatile("lgdt %w0"::"m" (*ptr));
44 }
45
46 static inline void store_gdt(struct desc_ptr *ptr)
47 {
48        asm("sgdt %w0":"=m" (*ptr));
49 }
50
51 static inline void _set_gate(void *adr, unsigned type, unsigned long func,
52                              unsigned dpl, unsigned ist)
53 {
54         gate_desc s;
55
56         s.offset_low = PTR_LOW(func);
57         s.segment = __KERNEL_CS;
58         s.ist = ist;
59         s.p = 1;
60         s.dpl = dpl;
61         s.zero0 = 0;
62         s.zero1 = 0;
63         s.type = type;
64         s.offset_middle = PTR_MIDDLE(func);
65         s.offset_high = PTR_HIGH(func);
66         /*
67          * does not need to be atomic because it is only done once at
68          * setup time
69          */
70         memcpy(adr, &s, 16);
71 }
72
73 static inline void set_intr_gate(int nr, void *func)
74 {
75         BUG_ON((unsigned)nr > 0xFF);
76         _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, 0);
77 }
78
79 static inline void set_intr_gate_ist(int nr, void *func, unsigned ist)
80 {
81         BUG_ON((unsigned)nr > 0xFF);
82         _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, ist);
83 }
84
85 static inline void set_system_gate(int nr, void *func)
86 {
87         BUG_ON((unsigned)nr > 0xFF);
88         _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, 0);
89 }
90
91 static inline void set_system_gate_ist(int nr, void *func, unsigned ist)
92 {
93         _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, ist);
94 }
95
96 static inline void load_idt(const struct desc_ptr *ptr)
97 {
98         asm volatile("lidt %w0"::"m" (*ptr));
99 }
100
101 static inline void store_idt(struct desc_ptr *dtr)
102 {
103        asm("sidt %w0":"=m" (*dtr));
104 }
105
106 static inline void set_tssldt_descriptor(void *ptr, unsigned long tss,
107                                          unsigned type, unsigned size)
108 {
109         struct ldttss_desc64 d;
110
111         memset(&d, 0, sizeof(d));
112         d.limit0 = size & 0xFFFF;
113         d.base0 = PTR_LOW(tss);
114         d.base1 = PTR_MIDDLE(tss) & 0xFF;
115         d.type = type;
116         d.p = 1;
117         d.limit1 = (size >> 16) & 0xF;
118         d.base2 = (PTR_MIDDLE(tss) >> 8) & 0xFF;
119         d.base3 = PTR_HIGH(tss);
120         memcpy(ptr, &d, 16);
121 }
122
123 static inline void set_tss_desc(unsigned cpu, void *addr)
124 {
125         /*
126          * sizeof(unsigned long) coming from an extra "long" at the end
127          * of the iobitmap. See tss_struct definition in processor.h
128          *
129          * -1? seg base+limit should be pointing to the address of the
130          * last valid byte
131          */
132         set_tssldt_descriptor(&get_cpu_gdt_table(cpu)[GDT_ENTRY_TSS],
133                 (unsigned long)addr, DESC_TSS,
134                 IO_BITMAP_OFFSET + IO_BITMAP_BYTES + sizeof(unsigned long) - 1);
135 }
136
137 static inline void set_ldt(void *addr, int entries)
138 {
139         if (likely(entries == 0))
140                 __asm__ __volatile__("lldt %w0"::"q" (0));
141         else {
142                 unsigned cpu = smp_processor_id();
143
144                 set_tssldt_descriptor(&get_cpu_gdt_table(cpu)[GDT_ENTRY_LDT],
145                              (unsigned long)addr, DESC_LDT, entries * 8 - 1);
146                 __asm__ __volatile__("lldt %w0"::"q" (GDT_ENTRY_LDT*8));
147         }
148 }
149
150 static inline void load_TLS(struct thread_struct *t, unsigned int cpu)
151 {
152         unsigned int i;
153         struct desc_struct *gdt = (get_cpu_gdt_table(cpu) + GDT_ENTRY_TLS_MIN);
154
155         for (i = 0; i < GDT_ENTRY_TLS_ENTRIES; i++)
156                 gdt[i] = t->tls_array[i];
157 }
158
159 #endif /* !__ASSEMBLY__ */
160
161 #endif