blob: a75ae6b978607554a365a03dcbdeff146658f58e [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>
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#ifndef __ASSEMBLY__
8
9#include <linux/preempt.h>
10#include <linux/smp.h>
11#include <linux/percpu.h>
12
13#include <asm/mmu.h>
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015struct Xgt_desc_struct {
16 unsigned short size;
17 unsigned long address __attribute__((packed));
18 unsigned short pad;
19} __attribute__ ((packed));
20
James Bottomley2b932f62006-02-24 13:04:14 -080021extern struct Xgt_desc_struct idt_descr;
22DECLARE_PER_CPU(struct Xgt_desc_struct, cpu_gdt_descr);
Rusty Russellae1ee112007-05-02 19:27:10 +020023DECLARE_PER_CPU(struct desc_struct, cpu_gdt[GDT_ENTRIES]);
Rusty Russell2a57ff12007-02-13 13:26:26 +010024extern struct Xgt_desc_struct early_gdt_descr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Zachary Amsden7c4cb602006-01-06 00:11:47 -080026static inline struct desc_struct *get_cpu_gdt_table(unsigned int cpu)
27{
James Bottomley2b932f62006-02-24 13:04:14 -080028 return (struct desc_struct *)per_cpu(cpu_gdt_descr, cpu).address;
Zachary Amsden7c4cb602006-01-06 00:11:47 -080029}
30
Rusty Russell522e93e2006-09-26 10:52:35 +020031extern struct desc_struct idt_table[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070032extern void set_intr_gate(unsigned int irq, void * addr);
33
Rusty Russell522e93e2006-09-26 10:52:35 +020034static inline void pack_descriptor(__u32 *a, __u32 *b,
35 unsigned long base, unsigned long limit, unsigned char type, unsigned char flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Rusty Russell522e93e2006-09-26 10:52:35 +020037 *a = ((base & 0xffff) << 16) | (limit & 0xffff);
38 *b = (base & 0xff000000) | ((base & 0xff0000) >> 16) |
Jeremy Fitzhardinge28177162006-09-26 10:52:40 +020039 (limit & 0x000f0000) | ((type & 0xff) << 8) | ((flags & 0xf) << 20);
Rusty Russell522e93e2006-09-26 10:52:35 +020040}
41
42static inline void pack_gate(__u32 *a, __u32 *b,
43 unsigned long base, unsigned short seg, unsigned char type, unsigned char flags)
44{
45 *a = (seg << 16) | (base & 0xffff);
46 *b = (base & 0xffff0000) | ((type & 0xff) << 8) | (flags & 0xff);
47}
48
49#define DESCTYPE_LDT 0x82 /* present, system, DPL-0, LDT */
50#define DESCTYPE_TSS 0x89 /* present, system, DPL-0, 32-bit TSS */
51#define DESCTYPE_TASK 0x85 /* present, system, DPL-0, task gate */
52#define DESCTYPE_INT 0x8e /* present, system, DPL-0, interrupt gate */
53#define DESCTYPE_TRAP 0x8f /* present, system, DPL-0, trap gate */
54#define DESCTYPE_DPL3 0x60 /* DPL-3 */
55#define DESCTYPE_S 0x10 /* !system */
56
Rusty Russelld3561b72006-12-07 02:14:07 +010057#ifdef CONFIG_PARAVIRT
58#include <asm/paravirt.h>
59#else
Rusty Russell522e93e2006-09-26 10:52:35 +020060#define load_TR_desc() __asm__ __volatile__("ltr %w0"::"q" (GDT_ENTRY_TSS*8))
Rusty Russell522e93e2006-09-26 10:52:35 +020061
62#define load_gdt(dtr) __asm__ __volatile("lgdt %0"::"m" (*dtr))
63#define load_idt(dtr) __asm__ __volatile("lidt %0"::"m" (*dtr))
64#define load_tr(tr) __asm__ __volatile("ltr %0"::"m" (tr))
65#define load_ldt(ldt) __asm__ __volatile("lldt %0"::"m" (ldt))
66
67#define store_gdt(dtr) __asm__ ("sgdt %0":"=m" (*dtr))
68#define store_idt(dtr) __asm__ ("sidt %0":"=m" (*dtr))
69#define store_tr(tr) __asm__ ("str %0":"=m" (tr))
70#define store_ldt(ldt) __asm__ ("sldt %0":"=m" (ldt))
71
72#if TLS_SIZE != 24
73# error update this code.
74#endif
75
76static inline void load_TLS(struct thread_struct *t, unsigned int cpu)
77{
78#define C(i) get_cpu_gdt_table(cpu)[GDT_ENTRY_TLS_MIN + i] = t->tls_array[i]
79 C(0); C(1); C(2);
80#undef C
81}
82
Rusty Russell139ec7c2006-12-07 02:14:08 +010083#define write_ldt_entry(dt, entry, a, b) write_dt_entry(dt, entry, a, b)
84#define write_gdt_entry(dt, entry, a, b) write_dt_entry(dt, entry, a, b)
85#define write_idt_entry(dt, entry, a, b) write_dt_entry(dt, entry, a, b)
86
Rusty Russell522e93e2006-09-26 10:52:35 +020087static inline void write_dt_entry(void *dt, int entry, __u32 entry_a, __u32 entry_b)
88{
89 __u32 *lp = (__u32 *)((char *)dt + entry*8);
90 *lp = entry_a;
91 *(lp+1) = entry_b;
92}
93
Rusty Russelld3561b72006-12-07 02:14:07 +010094#define set_ldt native_set_ldt
95#endif /* CONFIG_PARAVIRT */
96
97static inline fastcall void native_set_ldt(const void *addr,
98 unsigned int entries)
Rusty Russell522e93e2006-09-26 10:52:35 +020099{
Jeremy Fitzhardingee5e3a042006-12-07 02:14:01 +0100100 if (likely(entries == 0))
101 __asm__ __volatile__("lldt %w0"::"q" (0));
102 else {
103 unsigned cpu = smp_processor_id();
104 __u32 a, b;
105
106 pack_descriptor(&a, &b, (unsigned long)addr,
107 entries * sizeof(struct desc_struct) - 1,
108 DESCTYPE_LDT, 0);
109 write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_LDT, a, b);
110 __asm__ __volatile__("lldt %w0"::"q" (GDT_ENTRY_LDT*8));
111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Rusty Russell139ec7c2006-12-07 02:14:08 +0100114static inline void _set_gate(int gate, unsigned int type, void *addr, unsigned short seg)
115{
116 __u32 a, b;
117 pack_gate(&a, &b, (unsigned long)addr, seg, type, 0);
118 write_idt_entry(idt_table, gate, a, b);
119}
120
121static inline void __set_tss_desc(unsigned int cpu, unsigned int entry, const void *addr)
122{
123 __u32 a, b;
124 pack_descriptor(&a, &b, (unsigned long)addr,
125 offsetof(struct tss_struct, __cacheline_filler) - 1,
126 DESCTYPE_TSS, 0);
127 write_gdt_entry(get_cpu_gdt_table(cpu), entry, a, b);
128}
129
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#define set_tss_desc(cpu,addr) __set_tss_desc(cpu, GDT_ENTRY_TSS, addr)
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#define LDT_entry_a(info) \
134 ((((info)->base_addr & 0x0000ffff) << 16) | ((info)->limit & 0x0ffff))
135
136#define LDT_entry_b(info) \
137 (((info)->base_addr & 0xff000000) | \
138 (((info)->base_addr & 0x00ff0000) >> 16) | \
139 ((info)->limit & 0xf0000) | \
140 (((info)->read_exec_only ^ 1) << 9) | \
141 ((info)->contents << 10) | \
142 (((info)->seg_not_present ^ 1) << 15) | \
143 ((info)->seg_32bit << 22) | \
144 ((info)->limit_in_pages << 23) | \
145 ((info)->useable << 20) | \
146 0x7000)
147
148#define LDT_empty(info) (\
149 (info)->base_addr == 0 && \
150 (info)->limit == 0 && \
151 (info)->contents == 0 && \
152 (info)->read_exec_only == 1 && \
153 (info)->seg_32bit == 0 && \
154 (info)->limit_in_pages == 0 && \
155 (info)->seg_not_present == 1 && \
156 (info)->useable == 0 )
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static inline void clear_LDT(void)
159{
Jeremy Fitzhardingee5e3a042006-12-07 02:14:01 +0100160 set_ldt(NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
163/*
164 * load one particular LDT into the current CPU
165 */
Jeremy Fitzhardingee5e3a042006-12-07 02:14:01 +0100166static inline void load_LDT_nolock(mm_context_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Jeremy Fitzhardingee5e3a042006-12-07 02:14:01 +0100168 set_ldt(pc->ldt, pc->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171static inline void load_LDT(mm_context_t *pc)
172{
Jeremy Fitzhardingee5e3a042006-12-07 02:14:01 +0100173 preempt_disable();
174 load_LDT_nolock(pc);
175 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178static inline unsigned long get_desc_base(unsigned long *desc)
179{
180 unsigned long base;
181 base = ((desc[0] >> 16) & 0x0000ffff) |
182 ((desc[1] << 16) & 0x00ff0000) |
183 (desc[1] & 0xff000000);
184 return base;
185}
186
Stas Sergeevbe44d2a2006-12-07 02:14:01 +0100187#else /* __ASSEMBLY__ */
188
189/*
190 * GET_DESC_BASE reads the descriptor base of the specified segment.
191 *
192 * Args:
193 * idx - descriptor index
194 * gdt - GDT pointer
195 * base - 32bit register to which the base will be written
196 * lo_w - lo word of the "base" register
197 * lo_b - lo byte of the "base" register
198 * hi_b - hi byte of the low word of the "base" register
199 *
200 * Example:
201 * GET_DESC_BASE(GDT_ENTRY_ESPFIX_SS, %ebx, %eax, %ax, %al, %ah)
202 * Will read the base address of GDT_ENTRY_ESPFIX_SS and put it into %eax.
203 */
204#define GET_DESC_BASE(idx, gdt, base, lo_w, lo_b, hi_b) \
205 movb idx*8+4(gdt), lo_b; \
206 movb idx*8+7(gdt), hi_b; \
207 shll $16, base; \
208 movw idx*8+2(gdt), lo_w;
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210#endif /* !__ASSEMBLY__ */
211
212#endif