1 /* Written 2000 by Andi Kleen */
5 #include <linux/threads.h>
10 #include <linux/string.h>
11 #include <linux/smp.h>
13 #include <asm/segment.h>
16 // 8 byte segment descriptor
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
));
24 struct n_desc_struct
{
28 extern struct desc_struct cpu_gdt_table
[NR_CPUS
][GDT_ENTRIES
];
40 unsigned ist
: 3, zero0
: 5, type
: 5, dpl
: 2, p
: 1;
44 } __attribute__((packed
));
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)
55 // LDT or TSS descriptor in the GDT. 16 bytes.
59 unsigned base1
: 8, type
: 5, dpl
: 2, p
: 1;
60 unsigned limit1
: 4, zero0
: 3, g
: 1, base2
: 8;
63 } __attribute__((packed
));
67 unsigned long address
;
68 } __attribute__((packed
)) ;
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))
75 * This is the ldt that every process will get unless we need
76 * something other than this.
78 extern struct desc_struct default_ldt
[];
79 extern struct gate_struct idt_table
[];
80 extern struct desc_ptr cpu_gdt_descr
[];
82 static inline void _set_gate(void *adr
, unsigned type
, unsigned long func
, unsigned dpl
, unsigned ist
)
85 s
.offset_low
= PTR_LOW(func
);
86 s
.segment
= __KERNEL_CS
;
93 s
.offset_middle
= PTR_MIDDLE(func
);
94 s
.offset_high
= PTR_HIGH(func
);
95 /* does not need to be atomic because it is only done once at setup time */
99 static inline void set_intr_gate(int nr
, void *func
)
101 BUG_ON((unsigned)nr
> 0xFF);
102 _set_gate(&idt_table
[nr
], GATE_INTERRUPT
, (unsigned long) func
, 0, 0);
105 static inline void set_intr_gate_ist(int nr
, void *func
, unsigned ist
)
107 BUG_ON((unsigned)nr
> 0xFF);
108 _set_gate(&idt_table
[nr
], GATE_INTERRUPT
, (unsigned long) func
, 0, ist
);
111 static inline void set_system_gate(int nr
, void *func
)
113 BUG_ON((unsigned)nr
> 0xFF);
114 _set_gate(&idt_table
[nr
], GATE_INTERRUPT
, (unsigned long) func
, 3, 0);
117 static inline void set_tssldt_descriptor(void *ptr
, unsigned long tss
, unsigned type
,
120 struct ldttss_desc d
;
121 memset(&d
,0,sizeof(d
));
122 d
.limit0
= size
& 0xFFFF;
123 d
.base0
= PTR_LOW(tss
);
124 d
.base1
= PTR_MIDDLE(tss
) & 0xFF;
127 d
.limit1
= (size
>> 16) & 0xF;
128 d
.base2
= (PTR_MIDDLE(tss
) >> 8) & 0xFF;
129 d
.base3
= PTR_HIGH(tss
);
133 static inline void set_tss_desc(unsigned cpu
, void *addr
)
135 set_tssldt_descriptor(&cpu_gdt_table
[cpu
][GDT_ENTRY_TSS
], (unsigned long)addr
,
137 sizeof(struct tss_struct
) - 1);
140 static inline void set_ldt_desc(unsigned cpu
, void *addr
, int size
)
142 set_tssldt_descriptor(&cpu_gdt_table
[cpu
][GDT_ENTRY_LDT
], (unsigned long)addr
,
143 DESC_LDT
, size
* 8 - 1);
146 static inline void set_seg_base(unsigned cpu
, int entry
, void *base
)
148 struct desc_struct
*d
= &cpu_gdt_table
[cpu
][entry
];
149 u32 addr
= (u32
)(u64
)base
;
150 BUG_ON((u64
)base
>> 32);
151 d
->base0
= addr
& 0xffff;
152 d
->base1
= (addr
>> 16) & 0xff;
153 d
->base2
= (addr
>> 24) & 0xff;
156 #define LDT_entry_a(info) \
157 ((((info)->base_addr & 0x0000ffff) << 16) | ((info)->limit & 0x0ffff))
158 /* Don't allow setting of the lm bit. It is useless anyways because
159 64bit system calls require __USER_CS. */
160 #define LDT_entry_b(info) \
161 (((info)->base_addr & 0xff000000) | \
162 (((info)->base_addr & 0x00ff0000) >> 16) | \
163 ((info)->limit & 0xf0000) | \
164 (((info)->read_exec_only ^ 1) << 9) | \
165 ((info)->contents << 10) | \
166 (((info)->seg_not_present ^ 1) << 15) | \
167 ((info)->seg_32bit << 22) | \
168 ((info)->limit_in_pages << 23) | \
169 ((info)->useable << 20) | \
170 /* ((info)->lm << 21) | */ \
173 #define LDT_empty(info) (\
174 (info)->base_addr == 0 && \
175 (info)->limit == 0 && \
176 (info)->contents == 0 && \
177 (info)->read_exec_only == 1 && \
178 (info)->seg_32bit == 0 && \
179 (info)->limit_in_pages == 0 && \
180 (info)->seg_not_present == 1 && \
181 (info)->useable == 0 && \
185 # error update this code.
188 static inline void load_TLS(struct thread_struct
*t
, unsigned int cpu
)
190 u64
*gdt
= (u64
*)(cpu_gdt_table
[cpu
] + GDT_ENTRY_TLS_MIN
);
191 gdt
[0] = t
->tls_array
[0];
192 gdt
[1] = t
->tls_array
[1];
193 gdt
[2] = t
->tls_array
[2];
197 * load one particular LDT into the current CPU
199 static inline void load_LDT_nolock (mm_context_t
*pc
, int cpu
)
201 int count
= pc
->size
;
203 if (likely(!count
)) {
208 set_ldt_desc(cpu
, pc
->ldt
, count
);
212 static inline void load_LDT(mm_context_t
*pc
)
215 load_LDT_nolock(pc
, cpu
);
219 extern struct desc_ptr idt_descr
;
221 #endif /* !__ASSEMBLY__ */