1/* 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#define __TSS_INDEX(n) ((n)*64) 11#define __LDT_INDEX(n) ((n)*64) 12 13extern __u8 gdt_table[]; 14extern __u8 gdt_end[]; 15 16enum { 17 GATE_INTERRUPT = 0xE, 18 GATE_TRAP = 0xF, 19 GATE_CALL = 0xC, 20}; 21 22// 16byte gate 23struct gate_struct { 24 u16 offset_low; 25 u16 segment; 26 unsigned ist : 3, zero0 : 5, type : 5, dpl : 2, p : 1; 27 u16 offset_middle; 28 u32 offset_high; 29 u32 zero1; 30} __attribute__((packed)); 31 32// 8 byte segment descriptor 33struct desc_struct { 34 u16 limit0; 35 u16 base0; 36 unsigned base1 : 8, type : 4, s : 1, dpl : 2, p : 1; 37 unsigned limit : 4, avl : 1, l : 1, d : 1, g : 1, base2 : 8; 38} __attribute__((packed)); 39 40// LDT or TSS descriptor in the GDT. 16 bytes. 41struct ldttss_desc { 42 u16 limit0; 43 u16 base0; 44 unsigned base1 : 8, type : 5, dpl : 2, p : 1; 45 unsigned limit1 : 4, zero0 : 3, g : 1, base2 : 8; 46 u32 base3; 47 u32 zero1; 48} __attribute__((packed)); 49 50 51struct per_cpu_gdt { 52 struct ldttss_desc tss; 53 struct ldttss_desc ldt; 54} ____cacheline_aligned; 55 56extern struct per_cpu_gdt gdt_cpu_table[]; 57 58#define PTR_LOW(x) ((unsigned long)(x) & 0xFFFF) 59#define PTR_MIDDLE(x) (((unsigned long)(x) >> 16) & 0xFFFF) 60#define PTR_HIGH(x) ((unsigned long)(x) >> 32) 61 62enum { 63 DESC_TSS = 0x9, 64 DESC_LDT = 0x2, 65}; 66 67struct desc_ptr { 68 unsigned short size; 69 unsigned long address; 70} __attribute__((packed)) ; 71 72#define __GDT_HEAD_SIZE 64 73#define __CPU_DESC_INDEX(x,field) \ 74 ((x) * sizeof(struct per_cpu_gdt) + offsetof(struct per_cpu_gdt, field) + __GDT_HEAD_SIZE) 75 76#define load_TR(cpu) asm volatile("ltr %w0"::"r" (__CPU_DESC_INDEX(cpu, tss))); 77#define __load_LDT(cpu) asm volatile("lldt %w0"::"r" (__CPU_DESC_INDEX(cpu, ldt))); 78#define clear_LDT(n) asm volatile("lldt %w0"::"r" (0)) 79 80extern struct gate_struct idt_table[]; 81 82#define copy_16byte(dst,src) memcpy((dst), (src), 16) 83 84static inline void _set_gate(void *adr, unsigned type, unsigned long func, unsigned dpl, unsigned ist) 85{ 86 struct gate_struct s; 87 s.offset_low = PTR_LOW(func); 88 s.segment = __KERNEL_CS; 89 s.ist = ist; 90 s.p = 1; 91 s.dpl = dpl; 92 s.zero0 = 0; 93 s.zero1 = 0; 94 s.type = type; 95 s.offset_middle = PTR_MIDDLE(func); 96 s.offset_high = PTR_HIGH(func); 97 copy_16byte(adr, &s); 98} 99 100static inline void set_intr_gate(int nr, void *func) 101{ 102 _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, 0); 103} 104 105static inline void set_intr_gate_ist(int nr, void *func, unsigned ist) 106{ 107 _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, ist); 108} 109 110static inline void set_system_gate(int nr, void *func) 111{ 112 _set_gate(&idt_table[nr], GATE_INTERRUPT, (unsigned long) func, 3, 0); 113} 114 115static inline void set_tssldt_descriptor(struct ldttss_desc *dst, unsigned long ptr, unsigned type, 116 unsigned size) 117{ 118 memset(dst,0,16); 119 dst->limit0 = size & 0xFFFF; 120 dst->base0 = PTR_LOW(ptr); 121 dst->base1 = PTR_MIDDLE(ptr) & 0xFF; 122 dst->type = type; 123 dst->p = 1; 124 dst->limit1 = 0xF; 125 dst->base2 = (PTR_MIDDLE(ptr) >> 8) & 0xFF; 126 dst->base3 = PTR_HIGH(ptr); 127} 128 129static inline void set_tss_desc(unsigned n, void *addr) 130{ 131 set_tssldt_descriptor((void *)&gdt_table + __CPU_DESC_INDEX(n,tss), (unsigned long)addr, DESC_TSS, sizeof(struct tss_struct)); 132} 133 134static inline void set_ldt_desc(unsigned n, void *addr, int size) 135{ 136 set_tssldt_descriptor((void *)&gdt_table + __CPU_DESC_INDEX(n,ldt), (unsigned long)addr, DESC_LDT, size); 137} 138 139/* 140 * load one particular LDT into the current CPU 141 */ 142static inline void load_LDT (struct mm_struct *mm) 143{ 144 int cpu = smp_processor_id(); 145 void *segments = mm->context.segments; 146 147 if (!segments) { 148 clear_LDT(cpu); 149 return; 150 } 151 152 set_ldt_desc(cpu, segments, LDT_ENTRIES); 153 __load_LDT(cpu); 154} 155 156#endif /* !__ASSEMBLY__ */ 157 158#endif 159