blob: 54b2314f2ddf0f9e2cd4aab445be9a3cd15b8b8c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __ARCH_DESC_H
2#define __ARCH_DESC_H
3
4#include <asm/ldt.h>
5#include <asm/segment.h>
Glauber de Oliveira Costa010d4f82008-01-30 13:31:12 +01006#include <asm/desc_defs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#ifndef __ASSEMBLY__
9
10#include <linux/preempt.h>
11#include <linux/smp.h>
12#include <linux/percpu.h>
13
14#include <asm/mmu.h>
15
Jeremy Fitzhardinge7a61d352007-05-02 19:27:15 +020016struct gdt_page
17{
18 struct desc_struct gdt[GDT_ENTRIES];
19} __attribute__((aligned(PAGE_SIZE)));
20DECLARE_PER_CPU(struct gdt_page, gdt_page);
21
Zachary Amsden7c4cb602006-01-06 00:11:47 -080022static inline struct desc_struct *get_cpu_gdt_table(unsigned int cpu)
23{
Jeremy Fitzhardinge7a61d352007-05-02 19:27:15 +020024 return per_cpu(gdt_page, cpu).gdt;
Zachary Amsden7c4cb602006-01-06 00:11:47 -080025}
26
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +010027extern struct desc_ptr idt_descr;
Glauber de Oliveira Costa010d4f82008-01-30 13:31:12 +010028extern gate_desc idt_table[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070029extern void set_intr_gate(unsigned int irq, void * addr);
30
Rusty Russell522e93e2006-09-26 10:52:35 +020031static inline void pack_descriptor(__u32 *a, __u32 *b,
32 unsigned long base, unsigned long limit, unsigned char type, unsigned char flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Rusty Russell522e93e2006-09-26 10:52:35 +020034 *a = ((base & 0xffff) << 16) | (limit & 0xffff);
35 *b = (base & 0xff000000) | ((base & 0xff0000) >> 16) |
Jeremy Fitzhardinge28177162006-09-26 10:52:40 +020036 (limit & 0x000f0000) | ((type & 0xff) << 8) | ((flags & 0xf) << 20);
Rusty Russell522e93e2006-09-26 10:52:35 +020037}
38
Glauber de Oliveira Costa010d4f82008-01-30 13:31:12 +010039static inline void pack_gate(gate_desc *gate,
Rusty Russell522e93e2006-09-26 10:52:35 +020040 unsigned long base, unsigned short seg, unsigned char type, unsigned char flags)
41{
Glauber de Oliveira Costa010d4f82008-01-30 13:31:12 +010042 gate->a = (seg << 16) | (base & 0xffff);
43 gate->b = (base & 0xffff0000) | ((type & 0xff) << 8) | (flags & 0xff);
Rusty Russell522e93e2006-09-26 10:52:35 +020044}
45
46#define DESCTYPE_LDT 0x82 /* present, system, DPL-0, LDT */
47#define DESCTYPE_TSS 0x89 /* present, system, DPL-0, 32-bit TSS */
48#define DESCTYPE_TASK 0x85 /* present, system, DPL-0, task gate */
49#define DESCTYPE_INT 0x8e /* present, system, DPL-0, interrupt gate */
50#define DESCTYPE_TRAP 0x8f /* present, system, DPL-0, trap gate */
51#define DESCTYPE_DPL3 0x60 /* DPL-3 */
52#define DESCTYPE_S 0x10 /* !system */
53
Rusty Russelld3561b72006-12-07 02:14:07 +010054#ifdef CONFIG_PARAVIRT
55#include <asm/paravirt.h>
56#else
Rusty Russell90a0a062007-05-02 19:27:10 +020057#define load_TR_desc() native_load_tr_desc()
58#define load_gdt(dtr) native_load_gdt(dtr)
59#define load_idt(dtr) native_load_idt(dtr)
Rusty Russell522e93e2006-09-26 10:52:35 +020060#define load_tr(tr) __asm__ __volatile("ltr %0"::"m" (tr))
61#define load_ldt(ldt) __asm__ __volatile("lldt %0"::"m" (ldt))
62
Rusty Russell90a0a062007-05-02 19:27:10 +020063#define store_gdt(dtr) native_store_gdt(dtr)
64#define store_idt(dtr) native_store_idt(dtr)
65#define store_tr(tr) (tr = native_store_tr())
Rusty Russell522e93e2006-09-26 10:52:35 +020066#define store_ldt(ldt) __asm__ ("sldt %0":"=m" (ldt))
67
Rusty Russell90a0a062007-05-02 19:27:10 +020068#define load_TLS(t, cpu) native_load_tls(t, cpu)
69#define set_ldt native_set_ldt
Rusty Russell522e93e2006-09-26 10:52:35 +020070
Rusty Russell139ec7c2006-12-07 02:14:08 +010071#define write_ldt_entry(dt, entry, a, b) write_dt_entry(dt, entry, a, b)
72#define write_gdt_entry(dt, entry, a, b) write_dt_entry(dt, entry, a, b)
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +010073#define write_idt_entry(dt, entry, g) native_write_idt_entry(dt, entry, g)
Rusty Russell90a0a062007-05-02 19:27:10 +020074#endif
Rusty Russell139ec7c2006-12-07 02:14:08 +010075
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +010076static inline void native_write_idt_entry(gate_desc *idt, int entry,
77 const gate_desc *gate)
78{
79 memcpy(&idt[entry], gate, sizeof(*gate));
80}
81
Rusty Russell90a0a062007-05-02 19:27:10 +020082static inline void write_dt_entry(struct desc_struct *dt,
83 int entry, u32 entry_low, u32 entry_high)
Rusty Russell522e93e2006-09-26 10:52:35 +020084{
Rusty Russell90a0a062007-05-02 19:27:10 +020085 dt[entry].a = entry_low;
86 dt[entry].b = entry_high;
Rusty Russell522e93e2006-09-26 10:52:35 +020087}
88
Rusty Russell90a0a062007-05-02 19:27:10 +020089static inline void native_set_ldt(const void *addr, unsigned int entries)
Rusty Russell522e93e2006-09-26 10:52:35 +020090{
Jeremy Fitzhardingee5e3a042006-12-07 02:14:01 +010091 if (likely(entries == 0))
92 __asm__ __volatile__("lldt %w0"::"q" (0));
93 else {
94 unsigned cpu = smp_processor_id();
95 __u32 a, b;
96
97 pack_descriptor(&a, &b, (unsigned long)addr,
98 entries * sizeof(struct desc_struct) - 1,
99 DESCTYPE_LDT, 0);
100 write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_LDT, a, b);
101 __asm__ __volatile__("lldt %w0"::"q" (GDT_ENTRY_LDT*8));
102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Rusty Russell90a0a062007-05-02 19:27:10 +0200105
106static inline void native_load_tr_desc(void)
107{
108 asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS*8));
109}
110
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100111static inline void native_load_gdt(const struct desc_ptr *dtr)
Rusty Russell90a0a062007-05-02 19:27:10 +0200112{
113 asm volatile("lgdt %0"::"m" (*dtr));
114}
115
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100116static inline void native_load_idt(const struct desc_ptr *dtr)
Rusty Russell90a0a062007-05-02 19:27:10 +0200117{
118 asm volatile("lidt %0"::"m" (*dtr));
119}
120
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100121static inline void native_store_gdt(struct desc_ptr *dtr)
Rusty Russell90a0a062007-05-02 19:27:10 +0200122{
123 asm ("sgdt %0":"=m" (*dtr));
124}
125
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100126static inline void native_store_idt(struct desc_ptr *dtr)
Rusty Russell90a0a062007-05-02 19:27:10 +0200127{
128 asm ("sidt %0":"=m" (*dtr));
129}
130
131static inline unsigned long native_store_tr(void)
132{
133 unsigned long tr;
134 asm ("str %0":"=r" (tr));
135 return tr;
136}
137
138static inline void native_load_tls(struct thread_struct *t, unsigned int cpu)
139{
140 unsigned int i;
141 struct desc_struct *gdt = get_cpu_gdt_table(cpu);
142
143 for (i = 0; i < GDT_ENTRY_TLS_ENTRIES; i++)
144 gdt[GDT_ENTRY_TLS_MIN + i] = t->tls_array[i];
145}
146
Rusty Russell139ec7c2006-12-07 02:14:08 +0100147static inline void _set_gate(int gate, unsigned int type, void *addr, unsigned short seg)
148{
Glauber de Oliveira Costa010d4f82008-01-30 13:31:12 +0100149 gate_desc g;
150 pack_gate(&g, (unsigned long)addr, seg, type, 0);
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +0100151 write_idt_entry(idt_table, gate, &g);
Rusty Russell139ec7c2006-12-07 02:14:08 +0100152}
153
154static inline void __set_tss_desc(unsigned int cpu, unsigned int entry, const void *addr)
155{
156 __u32 a, b;
157 pack_descriptor(&a, &b, (unsigned long)addr,
158 offsetof(struct tss_struct, __cacheline_filler) - 1,
159 DESCTYPE_TSS, 0);
160 write_gdt_entry(get_cpu_gdt_table(cpu), entry, a, b);
161}
162
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#define set_tss_desc(cpu,addr) __set_tss_desc(cpu, GDT_ENTRY_TSS, addr)
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#define LDT_entry_a(info) \
167 ((((info)->base_addr & 0x0000ffff) << 16) | ((info)->limit & 0x0ffff))
168
169#define LDT_entry_b(info) \
170 (((info)->base_addr & 0xff000000) | \
171 (((info)->base_addr & 0x00ff0000) >> 16) | \
172 ((info)->limit & 0xf0000) | \
173 (((info)->read_exec_only ^ 1) << 9) | \
174 ((info)->contents << 10) | \
175 (((info)->seg_not_present ^ 1) << 15) | \
176 ((info)->seg_32bit << 22) | \
177 ((info)->limit_in_pages << 23) | \
178 ((info)->useable << 20) | \
179 0x7000)
180
181#define LDT_empty(info) (\
182 (info)->base_addr == 0 && \
183 (info)->limit == 0 && \
184 (info)->contents == 0 && \
185 (info)->read_exec_only == 1 && \
186 (info)->seg_32bit == 0 && \
187 (info)->limit_in_pages == 0 && \
188 (info)->seg_not_present == 1 && \
189 (info)->useable == 0 )
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191static inline void clear_LDT(void)
192{
Jeremy Fitzhardingee5e3a042006-12-07 02:14:01 +0100193 set_ldt(NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196/*
197 * load one particular LDT into the current CPU
198 */
Jeremy Fitzhardingee5e3a042006-12-07 02:14:01 +0100199static inline void load_LDT_nolock(mm_context_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Jeremy Fitzhardingee5e3a042006-12-07 02:14:01 +0100201 set_ldt(pc->ldt, pc->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
204static inline void load_LDT(mm_context_t *pc)
205{
Jeremy Fitzhardingee5e3a042006-12-07 02:14:01 +0100206 preempt_disable();
207 load_LDT_nolock(pc);
208 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
211static inline unsigned long get_desc_base(unsigned long *desc)
212{
213 unsigned long base;
214 base = ((desc[0] >> 16) & 0x0000ffff) |
215 ((desc[1] << 16) & 0x00ff0000) |
216 (desc[1] & 0xff000000);
217 return base;
218}
219
Stas Sergeevbe44d2a2006-12-07 02:14:01 +0100220#else /* __ASSEMBLY__ */
221
222/*
223 * GET_DESC_BASE reads the descriptor base of the specified segment.
224 *
225 * Args:
226 * idx - descriptor index
227 * gdt - GDT pointer
228 * base - 32bit register to which the base will be written
229 * lo_w - lo word of the "base" register
230 * lo_b - lo byte of the "base" register
231 * hi_b - hi byte of the low word of the "base" register
232 *
233 * Example:
234 * GET_DESC_BASE(GDT_ENTRY_ESPFIX_SS, %ebx, %eax, %ax, %al, %ah)
235 * Will read the base address of GDT_ENTRY_ESPFIX_SS and put it into %eax.
236 */
237#define GET_DESC_BASE(idx, gdt, base, lo_w, lo_b, hi_b) \
238 movb idx*8+4(gdt), lo_b; \
239 movb idx*8+7(gdt), hi_b; \
240 shll $16, base; \
241 movw idx*8+2(gdt), lo_w;
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243#endif /* !__ASSEMBLY__ */
244
245#endif