blob: 6cf2ac2bfde747111ce5e3b46f69811b22f6978a [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
15extern struct desc_struct cpu_gdt_table[GDT_ENTRIES];
Zachary Amsden251e6912005-10-30 14:59:34 -080016
Linus Torvalds1da177e2005-04-16 15:20:36 -070017struct Xgt_desc_struct {
18 unsigned short size;
19 unsigned long address __attribute__((packed));
20 unsigned short pad;
21} __attribute__ ((packed));
22
James Bottomley2b932f62006-02-24 13:04:14 -080023extern struct Xgt_desc_struct idt_descr;
24DECLARE_PER_CPU(struct Xgt_desc_struct, cpu_gdt_descr);
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Zachary Amsden7c4cb602006-01-06 00:11:47 -080027static inline struct desc_struct *get_cpu_gdt_table(unsigned int cpu)
28{
James Bottomley2b932f62006-02-24 13:04:14 -080029 return (struct desc_struct *)per_cpu(cpu_gdt_descr, cpu).address;
Zachary Amsden7c4cb602006-01-06 00:11:47 -080030}
31
Rusty Russell522e93e2006-09-26 10:52:35 +020032extern struct desc_struct idt_table[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070033extern void set_intr_gate(unsigned int irq, void * addr);
34
Rusty Russell522e93e2006-09-26 10:52:35 +020035static inline void pack_descriptor(__u32 *a, __u32 *b,
36 unsigned long base, unsigned long limit, unsigned char type, unsigned char flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Rusty Russell522e93e2006-09-26 10:52:35 +020038 *a = ((base & 0xffff) << 16) | (limit & 0xffff);
39 *b = (base & 0xff000000) | ((base & 0xff0000) >> 16) |
Jeremy Fitzhardinge28177162006-09-26 10:52:40 +020040 (limit & 0x000f0000) | ((type & 0xff) << 8) | ((flags & 0xf) << 20);
Rusty Russell522e93e2006-09-26 10:52:35 +020041}
42
43static inline void pack_gate(__u32 *a, __u32 *b,
44 unsigned long base, unsigned short seg, unsigned char type, unsigned char flags)
45{
46 *a = (seg << 16) | (base & 0xffff);
47 *b = (base & 0xffff0000) | ((type & 0xff) << 8) | (flags & 0xff);
48}
49
50#define DESCTYPE_LDT 0x82 /* present, system, DPL-0, LDT */
51#define DESCTYPE_TSS 0x89 /* present, system, DPL-0, 32-bit TSS */
52#define DESCTYPE_TASK 0x85 /* present, system, DPL-0, task gate */
53#define DESCTYPE_INT 0x8e /* present, system, DPL-0, interrupt gate */
54#define DESCTYPE_TRAP 0x8f /* present, system, DPL-0, trap gate */
55#define DESCTYPE_DPL3 0x60 /* DPL-3 */
56#define DESCTYPE_S 0x10 /* !system */
57
58#define load_TR_desc() __asm__ __volatile__("ltr %w0"::"q" (GDT_ENTRY_TSS*8))
Rusty Russell522e93e2006-09-26 10:52:35 +020059
60#define load_gdt(dtr) __asm__ __volatile("lgdt %0"::"m" (*dtr))
61#define load_idt(dtr) __asm__ __volatile("lidt %0"::"m" (*dtr))
62#define load_tr(tr) __asm__ __volatile("ltr %0"::"m" (tr))
63#define load_ldt(ldt) __asm__ __volatile("lldt %0"::"m" (ldt))
64
65#define store_gdt(dtr) __asm__ ("sgdt %0":"=m" (*dtr))
66#define store_idt(dtr) __asm__ ("sidt %0":"=m" (*dtr))
67#define store_tr(tr) __asm__ ("str %0":"=m" (tr))
68#define store_ldt(ldt) __asm__ ("sldt %0":"=m" (ldt))
69
70#if TLS_SIZE != 24
71# error update this code.
72#endif
73
74static inline void load_TLS(struct thread_struct *t, unsigned int cpu)
75{
76#define C(i) get_cpu_gdt_table(cpu)[GDT_ENTRY_TLS_MIN + i] = t->tls_array[i]
77 C(0); C(1); C(2);
78#undef C
79}
80
81static inline void write_dt_entry(void *dt, int entry, __u32 entry_a, __u32 entry_b)
82{
83 __u32 *lp = (__u32 *)((char *)dt + entry*8);
84 *lp = entry_a;
85 *(lp+1) = entry_b;
86}
87
88#define write_ldt_entry(dt, entry, a, b) write_dt_entry(dt, entry, a, b)
89#define write_gdt_entry(dt, entry, a, b) write_dt_entry(dt, entry, a, b)
90#define write_idt_entry(dt, entry, a, b) write_dt_entry(dt, entry, a, b)
91
92static inline void _set_gate(int gate, unsigned int type, void *addr, unsigned short seg)
93{
94 __u32 a, b;
95 pack_gate(&a, &b, (unsigned long)addr, seg, type, 0);
96 write_idt_entry(idt_table, gate, a, b);
97}
98
99static inline void __set_tss_desc(unsigned int cpu, unsigned int entry, const void *addr)
100{
101 __u32 a, b;
102 pack_descriptor(&a, &b, (unsigned long)addr,
103 offsetof(struct tss_struct, __cacheline_filler) - 1,
104 DESCTYPE_TSS, 0);
105 write_gdt_entry(get_cpu_gdt_table(cpu), entry, a, b);
106}
107
Jeremy Fitzhardingee5e3a042006-12-07 02:14:01 +0100108static inline void set_ldt(void *addr, unsigned int entries)
Rusty Russell522e93e2006-09-26 10:52:35 +0200109{
Jeremy Fitzhardingee5e3a042006-12-07 02:14:01 +0100110 if (likely(entries == 0))
111 __asm__ __volatile__("lldt %w0"::"q" (0));
112 else {
113 unsigned cpu = smp_processor_id();
114 __u32 a, b;
115
116 pack_descriptor(&a, &b, (unsigned long)addr,
117 entries * sizeof(struct desc_struct) - 1,
118 DESCTYPE_LDT, 0);
119 write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_LDT, a, b);
120 __asm__ __volatile__("lldt %w0"::"q" (GDT_ENTRY_LDT*8));
121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124#define set_tss_desc(cpu,addr) __set_tss_desc(cpu, GDT_ENTRY_TSS, addr)
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#define LDT_entry_a(info) \
127 ((((info)->base_addr & 0x0000ffff) << 16) | ((info)->limit & 0x0ffff))
128
129#define LDT_entry_b(info) \
130 (((info)->base_addr & 0xff000000) | \
131 (((info)->base_addr & 0x00ff0000) >> 16) | \
132 ((info)->limit & 0xf0000) | \
133 (((info)->read_exec_only ^ 1) << 9) | \
134 ((info)->contents << 10) | \
135 (((info)->seg_not_present ^ 1) << 15) | \
136 ((info)->seg_32bit << 22) | \
137 ((info)->limit_in_pages << 23) | \
138 ((info)->useable << 20) | \
139 0x7000)
140
141#define LDT_empty(info) (\
142 (info)->base_addr == 0 && \
143 (info)->limit == 0 && \
144 (info)->contents == 0 && \
145 (info)->read_exec_only == 1 && \
146 (info)->seg_32bit == 0 && \
147 (info)->limit_in_pages == 0 && \
148 (info)->seg_not_present == 1 && \
149 (info)->useable == 0 )
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static inline void clear_LDT(void)
152{
Jeremy Fitzhardingee5e3a042006-12-07 02:14:01 +0100153 set_ldt(NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
156/*
157 * load one particular LDT into the current CPU
158 */
Jeremy Fitzhardingee5e3a042006-12-07 02:14:01 +0100159static inline void load_LDT_nolock(mm_context_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Jeremy Fitzhardingee5e3a042006-12-07 02:14:01 +0100161 set_ldt(pc->ldt, pc->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
164static inline void load_LDT(mm_context_t *pc)
165{
Jeremy Fitzhardingee5e3a042006-12-07 02:14:01 +0100166 preempt_disable();
167 load_LDT_nolock(pc);
168 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171static inline unsigned long get_desc_base(unsigned long *desc)
172{
173 unsigned long base;
174 base = ((desc[0] >> 16) & 0x0000ffff) |
175 ((desc[1] << 16) & 0x00ff0000) |
176 (desc[1] & 0xff000000);
177 return base;
178}
179
Stas Sergeevbe44d2a2006-12-07 02:14:01 +0100180#else /* __ASSEMBLY__ */
181
182/*
183 * GET_DESC_BASE reads the descriptor base of the specified segment.
184 *
185 * Args:
186 * idx - descriptor index
187 * gdt - GDT pointer
188 * base - 32bit register to which the base will be written
189 * lo_w - lo word of the "base" register
190 * lo_b - lo byte of the "base" register
191 * hi_b - hi byte of the low word of the "base" register
192 *
193 * Example:
194 * GET_DESC_BASE(GDT_ENTRY_ESPFIX_SS, %ebx, %eax, %ax, %al, %ah)
195 * Will read the base address of GDT_ENTRY_ESPFIX_SS and put it into %eax.
196 */
197#define GET_DESC_BASE(idx, gdt, base, lo_w, lo_b, hi_b) \
198 movb idx*8+4(gdt), lo_b; \
199 movb idx*8+7(gdt), hi_b; \
200 shll $16, base; \
201 movw idx*8+2(gdt), lo_w;
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203#endif /* !__ASSEMBLY__ */
204
205#endif