blob: c89b58bebee24619f9ae2b5ad903b752f5e1ec12 [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>
11#include <asm/segment.h>
12#include <asm/mmu.h>
13
14// 8 byte segment descriptor
15struct desc_struct {
16 u16 limit0;
17 u16 base0;
18 unsigned base1 : 8, type : 4, s : 1, dpl : 2, p : 1;
19 unsigned limit : 4, avl : 1, l : 1, d : 1, g : 1, base2 : 8;
20} __attribute__((packed));
21
22struct n_desc_struct {
23 unsigned int a,b;
24};
25
26extern struct desc_struct cpu_gdt_table[NR_CPUS][GDT_ENTRIES];
27
28enum {
29 GATE_INTERRUPT = 0xE,
30 GATE_TRAP = 0xF,
31 GATE_CALL = 0xC,
32};
33
34// 16byte gate
35struct gate_struct {
36 u16 offset_low;
37 u16 segment;
38 unsigned ist : 3, zero0 : 5, type : 5, dpl : 2, p : 1;
39 u16 offset_middle;
40 u32 offset_high;
41 u32 zero1;
42} __attribute__((packed));
43
44#define PTR_LOW(x) ((unsigned long)(x) & 0xFFFF)
45#define PTR_MIDDLE(x) (((unsigned long)(x) >> 16) & 0xFFFF)
46#define PTR_HIGH(x) ((unsigned long)(x) >> 32)
47
48enum {
49 DESC_TSS = 0x9,
50 DESC_LDT = 0x2,
51};
52
53// LDT or TSS descriptor in the GDT. 16 bytes.
54struct ldttss_desc {
55 u16 limit0;
56 u16 base0;
57 unsigned base1 : 8, type : 5, dpl : 2, p : 1;
58 unsigned limit1 : 4, zero0 : 3, g : 1, base2 : 8;
59 u32 base3;
60 u32 zero1;
61} __attribute__((packed));
62
63struct desc_ptr {
64 unsigned short size;
65 unsigned long address;
66} __attribute__((packed)) ;
67
68#define load_TR_desc() asm volatile("ltr %w0"::"r" (GDT_ENTRY_TSS*8))
69#define load_LDT_desc() asm volatile("lldt %w0"::"r" (GDT_ENTRY_LDT*8))
70#define clear_LDT() asm volatile("lldt %w0"::"r" (0))
71
72/*
73 * This is the ldt that every process will get unless we need
74 * something other than this.
75 */
76extern struct desc_struct default_ldt[];
77extern struct gate_struct idt_table[];
Andi Kleena9401992005-07-28 21:15:30 -070078extern struct desc_ptr cpu_gdt_descr[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80static inline void _set_gate(void *adr, unsigned type, unsigned long func, unsigned dpl, unsigned ist)
81{
82 struct gate_struct s;
83 s.offset_low = PTR_LOW(func);
84 s.segment = __KERNEL_CS;
85 s.ist = ist;
86 s.p = 1;
87 s.dpl = dpl;
88 s.zero0 = 0;
89 s.zero1 = 0;
90 s.type = type;
91 s.offset_middle = PTR_MIDDLE(func);
92 s.offset_high = PTR_HIGH(func);
93 /* does not need to be atomic because it is only done once at setup time */
94 memcpy(adr, &s, 16);
95}
96
97static inline void set_intr_gate(int nr, void *func)
98{
99 _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, 0);
100}
101
102static inline void set_intr_gate_ist(int nr, void *func, unsigned ist)
103{
104 _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, ist);
105}
106
107static inline void set_system_gate(int nr, void *func)
108{
109 _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, 0);
110}
111
112static inline void set_tssldt_descriptor(void *ptr, unsigned long tss, unsigned type,
113 unsigned size)
114{
115 struct ldttss_desc d;
116 memset(&d,0,sizeof(d));
117 d.limit0 = size & 0xFFFF;
118 d.base0 = PTR_LOW(tss);
119 d.base1 = PTR_MIDDLE(tss) & 0xFF;
120 d.type = type;
121 d.p = 1;
122 d.limit1 = (size >> 16) & 0xF;
123 d.base2 = (PTR_MIDDLE(tss) >> 8) & 0xFF;
124 d.base3 = PTR_HIGH(tss);
125 memcpy(ptr, &d, 16);
126}
127
128static inline void set_tss_desc(unsigned cpu, void *addr)
129{
130 set_tssldt_descriptor(&cpu_gdt_table[cpu][GDT_ENTRY_TSS], (unsigned long)addr,
131 DESC_TSS,
132 sizeof(struct tss_struct) - 1);
133}
134
135static inline void set_ldt_desc(unsigned cpu, void *addr, int size)
136{
137 set_tssldt_descriptor(&cpu_gdt_table[cpu][GDT_ENTRY_LDT], (unsigned long)addr,
138 DESC_LDT, size * 8 - 1);
139}
140
141static inline void set_seg_base(unsigned cpu, int entry, void *base)
142{
143 struct desc_struct *d = &cpu_gdt_table[cpu][entry];
144 u32 addr = (u32)(u64)base;
145 BUG_ON((u64)base >> 32);
146 d->base0 = addr & 0xffff;
147 d->base1 = (addr >> 16) & 0xff;
148 d->base2 = (addr >> 24) & 0xff;
149}
150
151#define LDT_entry_a(info) \
152 ((((info)->base_addr & 0x0000ffff) << 16) | ((info)->limit & 0x0ffff))
153/* Don't allow setting of the lm bit. It is useless anyways because
154 64bit system calls require __USER_CS. */
155#define LDT_entry_b(info) \
156 (((info)->base_addr & 0xff000000) | \
157 (((info)->base_addr & 0x00ff0000) >> 16) | \
158 ((info)->limit & 0xf0000) | \
159 (((info)->read_exec_only ^ 1) << 9) | \
160 ((info)->contents << 10) | \
161 (((info)->seg_not_present ^ 1) << 15) | \
162 ((info)->seg_32bit << 22) | \
163 ((info)->limit_in_pages << 23) | \
164 ((info)->useable << 20) | \
165 /* ((info)->lm << 21) | */ \
166 0x7000)
167
168#define LDT_empty(info) (\
169 (info)->base_addr == 0 && \
170 (info)->limit == 0 && \
171 (info)->contents == 0 && \
172 (info)->read_exec_only == 1 && \
173 (info)->seg_32bit == 0 && \
174 (info)->limit_in_pages == 0 && \
175 (info)->seg_not_present == 1 && \
176 (info)->useable == 0 && \
177 (info)->lm == 0)
178
179#if TLS_SIZE != 24
180# error update this code.
181#endif
182
183static inline void load_TLS(struct thread_struct *t, unsigned int cpu)
184{
185 u64 *gdt = (u64 *)(cpu_gdt_table[cpu] + GDT_ENTRY_TLS_MIN);
186 gdt[0] = t->tls_array[0];
187 gdt[1] = t->tls_array[1];
188 gdt[2] = t->tls_array[2];
189}
190
191/*
192 * load one particular LDT into the current CPU
193 */
194extern inline void load_LDT_nolock (mm_context_t *pc, int cpu)
195{
196 int count = pc->size;
197
198 if (likely(!count)) {
199 clear_LDT();
200 return;
201 }
202
203 set_ldt_desc(cpu, pc->ldt, count);
204 load_LDT_desc();
205}
206
207static inline void load_LDT(mm_context_t *pc)
208{
209 int cpu = get_cpu();
210 load_LDT_nolock(pc, cpu);
211 put_cpu();
212}
213
214extern struct desc_ptr idt_descr;
215
216#endif /* !__ASSEMBLY__ */
217
218#endif