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>
15 extern struct desc_struct cpu_gdt_table[GDT_ENTRIES];
17 #define load_TR_desc() asm volatile("ltr %w0"::"r" (GDT_ENTRY_TSS*8))
18 #define load_LDT_desc() asm volatile("lldt %w0"::"r" (GDT_ENTRY_LDT*8))
20 static inline unsigned long __store_tr(void)
24 asm volatile ("str %w0":"=r" (tr));
28 #define store_tr(tr) (tr) = __store_tr()
30 extern struct desc_ptr cpu_gdt_descr[];
32 static inline void write_ldt_entry(struct desc_struct *ldt,
35 memcpy(&ldt[entry], ptr, 8);
38 /* the cpu gdt accessor */
39 #define get_cpu_gdt_table(x) ((struct desc_struct *)cpu_gdt_descr[x].address)
41 static inline void load_gdt(const struct desc_ptr *ptr)
43 asm volatile("lgdt %w0"::"m" (*ptr));
46 static inline void store_gdt(struct desc_ptr *ptr)
48 asm("sgdt %w0":"=m" (*ptr));
51 static inline void _set_gate(void *adr, unsigned type, unsigned long func,
52 unsigned dpl, unsigned ist)
56 s.offset_low = PTR_LOW(func);
57 s.segment = __KERNEL_CS;
64 s.offset_middle = PTR_MIDDLE(func);
65 s.offset_high = PTR_HIGH(func);
67 * does not need to be atomic because it is only done once at
73 static inline void set_intr_gate(int nr, void *func)
75 BUG_ON((unsigned)nr > 0xFF);
76 _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, 0);
79 static inline void set_intr_gate_ist(int nr, void *func, unsigned ist)
81 BUG_ON((unsigned)nr > 0xFF);
82 _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 0, ist);
85 static inline void set_system_gate(int nr, void *func)
87 BUG_ON((unsigned)nr > 0xFF);
88 _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, 0);
91 static inline void set_system_gate_ist(int nr, void *func, unsigned ist)
93 _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, ist);
96 static inline void load_idt(const struct desc_ptr *ptr)
98 asm volatile("lidt %w0"::"m" (*ptr));
101 static inline void store_idt(struct desc_ptr *dtr)
103 asm("sidt %w0":"=m" (*dtr));
106 static inline void set_tssldt_descriptor(void *ptr, unsigned long tss,
107 unsigned type, unsigned size)
109 struct ldttss_desc64 d;
111 memset(&d, 0, sizeof(d));
112 d.limit0 = size & 0xFFFF;
113 d.base0 = PTR_LOW(tss);
114 d.base1 = PTR_MIDDLE(tss) & 0xFF;
117 d.limit1 = (size >> 16) & 0xF;
118 d.base2 = (PTR_MIDDLE(tss) >> 8) & 0xFF;
119 d.base3 = PTR_HIGH(tss);
123 static inline void set_tss_desc(unsigned cpu, void *addr)
126 * sizeof(unsigned long) coming from an extra "long" at the end
127 * of the iobitmap. See tss_struct definition in processor.h
129 * -1? seg base+limit should be pointing to the address of the
132 set_tssldt_descriptor(&get_cpu_gdt_table(cpu)[GDT_ENTRY_TSS],
133 (unsigned long)addr, DESC_TSS,
134 IO_BITMAP_OFFSET + IO_BITMAP_BYTES + sizeof(unsigned long) - 1);
137 static inline void set_ldt(void *addr, int entries)
139 if (likely(entries == 0))
140 __asm__ __volatile__("lldt %w0"::"q" (0));
142 unsigned cpu = smp_processor_id();
144 set_tssldt_descriptor(&get_cpu_gdt_table(cpu)[GDT_ENTRY_LDT],
145 (unsigned long)addr, DESC_LDT, entries * 8 - 1);
146 __asm__ __volatile__("lldt %w0"::"q" (GDT_ENTRY_LDT*8));
150 static inline void load_TLS(struct thread_struct *t, unsigned int cpu)
153 u64 *gdt = (u64 *)(get_cpu_gdt_table(cpu) + GDT_ENTRY_TLS_MIN);
155 for (i = 0; i < GDT_ENTRY_TLS_ENTRIES; i++)
156 gdt[i] = t->tls_array[i];
159 static inline unsigned long get_desc_base(const void *ptr)
161 const u32 *desc = ptr;
163 base = ((desc[0] >> 16) & 0x0000ffff) |
164 ((desc[1] << 16) & 0x00ff0000) |
165 (desc[1] & 0xff000000);
168 #endif /* !__ASSEMBLY__ */