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