blob: eb7723a467908b5f86cb0e2fc2dd24168c8e523d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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>
Andrew Morton73a0b532005-09-16 19:27:55 -070011#include <linux/smp.h>
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/segment.h>
14#include <asm/mmu.h>
15
16// 8 byte segment descriptor
17struct desc_struct {
18 u16 limit0;
19 u16 base0;
20 unsigned base1 : 8, type : 4, s : 1, dpl : 2, p : 1;
21 unsigned limit : 4, avl : 1, l : 1, d : 1, g : 1, base2 : 8;
22} __attribute__((packed));
23
24struct n_desc_struct {
25 unsigned int a,b;
26};
27
Ravikiran G Thirumalaic11efdf2006-01-11 22:43:57 +010028extern struct desc_struct cpu_gdt_table[GDT_ENTRIES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30enum {
31 GATE_INTERRUPT = 0xE,
32 GATE_TRAP = 0xF,
33 GATE_CALL = 0xC,
34};
35
36// 16byte gate
37struct gate_struct {
38 u16 offset_low;
39 u16 segment;
40 unsigned ist : 3, zero0 : 5, type : 5, dpl : 2, p : 1;
41 u16 offset_middle;
42 u32 offset_high;
43 u32 zero1;
44} __attribute__((packed));
45
46#define PTR_LOW(x) ((unsigned long)(x) & 0xFFFF)
47#define PTR_MIDDLE(x) (((unsigned long)(x) >> 16) & 0xFFFF)
48#define PTR_HIGH(x) ((unsigned long)(x) >> 32)
49
50enum {
51 DESC_TSS = 0x9,
52 DESC_LDT = 0x2,
53};
54
55// LDT or TSS descriptor in the GDT. 16 bytes.
56struct ldttss_desc {
57 u16 limit0;
58 u16 base0;
59 unsigned base1 : 8, type : 5, dpl : 2, p : 1;
60 unsigned limit1 : 4, zero0 : 3, g : 1, base2 : 8;
61 u32 base3;
62 u32 zero1;
63} __attribute__((packed));
64
65struct desc_ptr {
66 unsigned short size;
67 unsigned long address;
68} __attribute__((packed)) ;
69
70#define load_TR_desc() asm volatile("ltr %w0"::"r" (GDT_ENTRY_TSS*8))
71#define load_LDT_desc() asm volatile("lldt %w0"::"r" (GDT_ENTRY_LDT*8))
72#define clear_LDT() asm volatile("lldt %w0"::"r" (0))
73
74/*
75 * This is the ldt that every process will get unless we need
76 * something other than this.
77 */
78extern struct desc_struct default_ldt[];
79extern struct gate_struct idt_table[];
Andi Kleena9401992005-07-28 21:15:30 -070080extern struct desc_ptr cpu_gdt_descr[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Ravikiran G Thirumalaic11efdf2006-01-11 22:43:57 +010082/* the cpu gdt accessor */
83#define cpu_gdt(_cpu) ((struct desc_struct *)cpu_gdt_descr[_cpu].address)
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static inline void _set_gate(void *adr, unsigned type, unsigned long func, unsigned dpl, unsigned ist)
86{
87 struct gate_struct s;
88 s.offset_low = PTR_LOW(func);
89 s.segment = __KERNEL_CS;
90 s.ist = ist;
91 s.p = 1;
92 s.dpl = dpl;
93 s.zero0 = 0;
94 s.zero1 = 0;
95 s.type = type;
96 s.offset_middle = PTR_MIDDLE(func);
97 s.offset_high = PTR_HIGH(func);
98 /* does not need to be atomic because it is only done once at setup time */
99 memcpy(adr, &s, 16);
100}
101
102static inline void set_intr_gate(int nr, void *func)
103{
James Cleverdon6004e1b2005-11-05 17:25:53 +0100104 BUG_ON((unsigned)nr > 0xFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, 0);
106}
107
108static inline void set_intr_gate_ist(int nr, void *func, unsigned ist)
109{
James Cleverdon6004e1b2005-11-05 17:25:53 +0100110 BUG_ON((unsigned)nr > 0xFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, ist);
112}
113
114static inline void set_system_gate(int nr, void *func)
115{
James Cleverdon6004e1b2005-11-05 17:25:53 +0100116 BUG_ON((unsigned)nr > 0xFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, 0);
118}
119
Jan Beulichb556b352006-01-11 22:43:00 +0100120static inline void set_system_gate_ist(int nr, void *func, unsigned ist)
121{
122 _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, ist);
123}
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static inline void set_tssldt_descriptor(void *ptr, unsigned long tss, unsigned type,
126 unsigned size)
127{
128 struct ldttss_desc d;
129 memset(&d,0,sizeof(d));
130 d.limit0 = size & 0xFFFF;
131 d.base0 = PTR_LOW(tss);
132 d.base1 = PTR_MIDDLE(tss) & 0xFF;
133 d.type = type;
134 d.p = 1;
135 d.limit1 = (size >> 16) & 0xF;
136 d.base2 = (PTR_MIDDLE(tss) >> 8) & 0xFF;
137 d.base3 = PTR_HIGH(tss);
138 memcpy(ptr, &d, 16);
139}
140
141static inline void set_tss_desc(unsigned cpu, void *addr)
142{
Siddha, Suresh B47936352005-11-13 16:06:21 -0800143 /*
144 * sizeof(unsigned long) coming from an extra "long" at the end
145 * of the iobitmap. See tss_struct definition in processor.h
146 *
147 * -1? seg base+limit should be pointing to the address of the
148 * last valid byte
149 */
Ravikiran G Thirumalaic11efdf2006-01-11 22:43:57 +0100150 set_tssldt_descriptor(&cpu_gdt(cpu)[GDT_ENTRY_TSS],
Siddha, Suresh B47936352005-11-13 16:06:21 -0800151 (unsigned long)addr, DESC_TSS,
152 IO_BITMAP_OFFSET + IO_BITMAP_BYTES + sizeof(unsigned long) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
155static inline void set_ldt_desc(unsigned cpu, void *addr, int size)
156{
Ravikiran G Thirumalaic11efdf2006-01-11 22:43:57 +0100157 set_tssldt_descriptor(&cpu_gdt(cpu)[GDT_ENTRY_LDT], (unsigned long)addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 DESC_LDT, size * 8 - 1);
159}
160
161static inline void set_seg_base(unsigned cpu, int entry, void *base)
162{
Ravikiran G Thirumalaic11efdf2006-01-11 22:43:57 +0100163 struct desc_struct *d = &cpu_gdt(cpu)[entry];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 u32 addr = (u32)(u64)base;
165 BUG_ON((u64)base >> 32);
166 d->base0 = addr & 0xffff;
167 d->base1 = (addr >> 16) & 0xff;
168 d->base2 = (addr >> 24) & 0xff;
169}
170
171#define LDT_entry_a(info) \
172 ((((info)->base_addr & 0x0000ffff) << 16) | ((info)->limit & 0x0ffff))
173/* Don't allow setting of the lm bit. It is useless anyways because
174 64bit system calls require __USER_CS. */
175#define LDT_entry_b(info) \
176 (((info)->base_addr & 0xff000000) | \
177 (((info)->base_addr & 0x00ff0000) >> 16) | \
178 ((info)->limit & 0xf0000) | \
179 (((info)->read_exec_only ^ 1) << 9) | \
180 ((info)->contents << 10) | \
181 (((info)->seg_not_present ^ 1) << 15) | \
182 ((info)->seg_32bit << 22) | \
183 ((info)->limit_in_pages << 23) | \
184 ((info)->useable << 20) | \
185 /* ((info)->lm << 21) | */ \
186 0x7000)
187
188#define LDT_empty(info) (\
189 (info)->base_addr == 0 && \
190 (info)->limit == 0 && \
191 (info)->contents == 0 && \
192 (info)->read_exec_only == 1 && \
193 (info)->seg_32bit == 0 && \
194 (info)->limit_in_pages == 0 && \
195 (info)->seg_not_present == 1 && \
196 (info)->useable == 0 && \
197 (info)->lm == 0)
198
199#if TLS_SIZE != 24
200# error update this code.
201#endif
202
203static inline void load_TLS(struct thread_struct *t, unsigned int cpu)
204{
Ravikiran G Thirumalaic11efdf2006-01-11 22:43:57 +0100205 u64 *gdt = (u64 *)(cpu_gdt(cpu) + GDT_ENTRY_TLS_MIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 gdt[0] = t->tls_array[0];
207 gdt[1] = t->tls_array[1];
208 gdt[2] = t->tls_array[2];
209}
210
211/*
212 * load one particular LDT into the current CPU
213 */
Adrian Bunk9c0aa0f2005-09-12 18:49:24 +0200214static inline void load_LDT_nolock (mm_context_t *pc, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 int count = pc->size;
217
218 if (likely(!count)) {
219 clear_LDT();
220 return;
221 }
222
223 set_ldt_desc(cpu, pc->ldt, count);
224 load_LDT_desc();
225}
226
227static inline void load_LDT(mm_context_t *pc)
228{
229 int cpu = get_cpu();
230 load_LDT_nolock(pc, cpu);
231 put_cpu();
232}
233
234extern struct desc_ptr idt_descr;
235
236#endif /* !__ASSEMBLY__ */
237
238#endif