1 /* Written 2000 by Andi Kleen */
9 #include <lwk/string.h>
12 #include <arch/segment.h>
15 // 8 byte segment descriptor
19 unsigned base1 : 8, type : 4, s : 1, dpl : 2, p : 1;
20 unsigned limit : 4, avl : 1, l : 1, d : 1, g : 1, base2 : 8;
21 } __attribute__((packed));
23 struct n_desc_struct {
27 extern struct desc_struct cpu_gdt_table[GDT_ENTRIES];
36 * Long-Mode Gate Descriptor (16-bytes)
39 uint16_t offset_low; /* [15-0] of target code segment offset */
40 uint16_t segment; /* Target code segment selector */
41 unsigned ist : 3; /* Interrupt-Stack-Table index into TSS */
43 unsigned type : 5; /* Gate descriptor type */
44 unsigned dpl : 2; /* Privilege level */
45 unsigned p : 1; /* Present bit... in use? */
46 uint16_t offset_middle; /* [31-24] of target code segment offset */
47 uint32_t offset_high; /* [63-32] of target code segment offset */
49 } __attribute__((packed));
51 #define PTR_LOW(x) ((unsigned long)(x) & 0xFFFF)
52 #define PTR_MIDDLE(x) (((unsigned long)(x) >> 16) & 0xFFFF)
53 #define PTR_HIGH(x) ((unsigned long)(x) >> 32)
60 // LDT or TSS descriptor in the GDT. 16 bytes.
64 unsigned base1 : 8, type : 5, dpl : 2, p : 1;
65 unsigned limit1 : 4, zero0 : 3, g : 1, base2 : 8;
68 } __attribute__((packed));
72 unsigned long address;
73 } __attribute__((packed)) ;
75 #define load_TR_desc() asm volatile("ltr %w0"::"r" (GDT_ENTRY_TSS*8))
76 #define load_LDT_desc() asm volatile("lldt %w0"::"r" (GDT_ENTRY_LDT*8))
77 #define clear_LDT() asm volatile("lldt %w0"::"r" (0))
80 * This is the ldt that every process will get unless we need
81 * something other than this.
83 extern struct desc_struct default_ldt[];
84 extern struct gate_struct idt_table[];
85 extern struct desc_ptr cpu_gdt_descr[];
87 /* the cpu gdt accessor */
88 #define cpu_gdt(_cpu) ((struct desc_struct *)cpu_gdt_descr[_cpu].address)
91 * Installs a Long-Mode gate descriptor.
95 void * adr, /* Address to install gate descriptor at */
96 unsigned type, /* Type of gate */
97 unsigned long func, /* The handler function for the gate */
98 unsigned dpl, /* Privilege level */
99 unsigned ist /* Interupt-Stack-Table index */
102 struct gate_struct s;
103 s.offset_low = PTR_LOW(func);
104 s.segment = __KERNEL_CS;
111 s.offset_middle = PTR_MIDDLE(func);
112 s.offset_high = PTR_HIGH(func);
113 /* does not need to be atomic because it is only done once at setup time */
118 * Installs an interrupt gate.
119 * The interrupt will execute on the normal kernel stack.
122 set_intr_gate(int nr, void *func)
124 BUG_ON((unsigned)nr > 0xFF);
125 _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, 0);
129 * Installs an interrupt gate.
130 * The interrupt will execute on the stack specified by the 'ist' argument.
132 static inline void set_intr_gate_ist(int nr, void *func, unsigned ist)
134 BUG_ON((unsigned)nr > 0xFF);
135 _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, ist);
139 * Installs a system interrupt gate.
140 * The privilege level is set to 3, meaning that user-mode can trigger it.
142 static inline void set_system_gate(int nr, void *func)
144 BUG_ON((unsigned)nr > 0xFF);
145 _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, 0);
149 * Installs a system interrupt gate.
150 * The privilege level is set to 3, meaning that user-mode can trigger it.
151 * The interrupt will execute on the stack specified by the 'ist' argument.
153 static inline void set_system_gate_ist(int nr, void *func, unsigned ist)
155 _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, ist);
158 static inline void set_tssldt_descriptor(void *ptr, unsigned long tss, unsigned type,
161 struct ldttss_desc d;
162 memset(&d,0,sizeof(d));
163 d.limit0 = size & 0xFFFF;
164 d.base0 = PTR_LOW(tss);
165 d.base1 = PTR_MIDDLE(tss) & 0xFF;
168 d.limit1 = (size >> 16) & 0xF;
169 d.base2 = (PTR_MIDDLE(tss) >> 8) & 0xFF;
170 d.base3 = PTR_HIGH(tss);
174 static inline void set_tss_desc(unsigned cpu, void *addr)
177 * sizeof(unsigned long) coming from an extra "long" at the end
178 * of the iobitmap. See tss_struct definition in processor.h
180 * -1? seg base+limit should be pointing to the address of the
183 set_tssldt_descriptor(&cpu_gdt(cpu)[GDT_ENTRY_TSS],
184 (unsigned long)addr, DESC_TSS,
185 IO_BITMAP_OFFSET + IO_BITMAP_BYTES + sizeof(unsigned long) - 1);
188 static inline void set_ldt_desc(unsigned cpu, void *addr, int size)
190 set_tssldt_descriptor(&cpu_gdt(cpu)[GDT_ENTRY_LDT], (unsigned long)addr,
191 DESC_LDT, size * 8 - 1);
194 static inline void set_seg_base(unsigned cpu, int entry, void *base)
196 struct desc_struct *d = &cpu_gdt(cpu)[entry];
197 u32 addr = (u32)(u64)base;
198 BUG_ON((u64)base >> 32);
199 d->base0 = addr & 0xffff;
200 d->base1 = (addr >> 16) & 0xff;
201 d->base2 = (addr >> 24) & 0xff;
204 #define LDT_entry_a(info) \
205 ((((info)->base_addr & 0x0000ffff) << 16) | ((info)->limit & 0x0ffff))
206 /* Don't allow setting of the lm bit. It is useless anyways because
207 64bit system calls require __USER_CS. */
208 #define LDT_entry_b(info) \
209 (((info)->base_addr & 0xff000000) | \
210 (((info)->base_addr & 0x00ff0000) >> 16) | \
211 ((info)->limit & 0xf0000) | \
212 (((info)->read_exec_only ^ 1) << 9) | \
213 ((info)->contents << 10) | \
214 (((info)->seg_not_present ^ 1) << 15) | \
215 ((info)->seg_32bit << 22) | \
216 ((info)->limit_in_pages << 23) | \
217 ((info)->useable << 20) | \
218 /* ((info)->lm << 21) | */ \
221 #define LDT_empty(info) (\
222 (info)->base_addr == 0 && \
223 (info)->limit == 0 && \
224 (info)->contents == 0 && \
225 (info)->read_exec_only == 1 && \
226 (info)->seg_32bit == 0 && \
227 (info)->limit_in_pages == 0 && \
228 (info)->seg_not_present == 1 && \
229 (info)->useable == 0 && \
233 # error update this code.
236 static inline void load_TLS(struct thread_struct *t, unsigned int cpu)
238 u64 *gdt = (u64 *)(cpu_gdt(cpu) + GDT_ENTRY_TLS_MIN);
239 gdt[0] = t->tls_array[0];
240 gdt[1] = t->tls_array[1];
241 gdt[2] = t->tls_array[2];
245 * load one particular LDT into the current CPU
247 static inline void load_LDT_nolock (mm_context_t *pc, int cpu)
249 int count = pc->size;
251 if (likely(!count)) {
256 set_ldt_desc(cpu, pc->ldt, count);
260 static inline void load_LDT(mm_context_t *pc)
263 load_LDT_nolock(pc, cpu);
267 extern struct desc_ptr idt_descr;
269 #endif /* !__ASSEMBLY__ */