blob: a0398f780ca11782f699e955373c8c689cfbad3e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __ARCH_DESC_H
2#define __ARCH_DESC_H
3
4#include <asm/ldt.h>
5#include <asm/segment.h>
6
7#define CPU_16BIT_STACK_SIZE 1024
8
9#ifndef __ASSEMBLY__
10
11#include <linux/preempt.h>
12#include <linux/smp.h>
13#include <linux/percpu.h>
14
15#include <asm/mmu.h>
16
17extern struct desc_struct cpu_gdt_table[GDT_ENTRIES];
Zachary Amsden251e6912005-10-30 14:59:34 -080018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019DECLARE_PER_CPU(unsigned char, cpu_16bit_stack[CPU_16BIT_STACK_SIZE]);
20
21struct Xgt_desc_struct {
22 unsigned short size;
23 unsigned long address __attribute__((packed));
24 unsigned short pad;
25} __attribute__ ((packed));
26
James Bottomley2b932f62006-02-24 13:04:14 -080027extern struct Xgt_desc_struct idt_descr;
28DECLARE_PER_CPU(struct Xgt_desc_struct, cpu_gdt_descr);
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Zachary Amsden7c4cb602006-01-06 00:11:47 -080031static inline struct desc_struct *get_cpu_gdt_table(unsigned int cpu)
32{
James Bottomley2b932f62006-02-24 13:04:14 -080033 return (struct desc_struct *)per_cpu(cpu_gdt_descr, cpu).address;
Zachary Amsden7c4cb602006-01-06 00:11:47 -080034}
35
Rusty Russell522e93e2006-09-26 10:52:35 +020036extern struct desc_struct idt_table[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070037extern void set_intr_gate(unsigned int irq, void * addr);
38
Rusty Russell522e93e2006-09-26 10:52:35 +020039static inline void pack_descriptor(__u32 *a, __u32 *b,
40 unsigned long base, unsigned long limit, unsigned char type, unsigned char flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Rusty Russell522e93e2006-09-26 10:52:35 +020042 *a = ((base & 0xffff) << 16) | (limit & 0xffff);
43 *b = (base & 0xff000000) | ((base & 0xff0000) >> 16) |
Jeremy Fitzhardinge28177162006-09-26 10:52:40 +020044 (limit & 0x000f0000) | ((type & 0xff) << 8) | ((flags & 0xf) << 20);
Rusty Russell522e93e2006-09-26 10:52:35 +020045}
46
47static inline void pack_gate(__u32 *a, __u32 *b,
48 unsigned long base, unsigned short seg, unsigned char type, unsigned char flags)
49{
50 *a = (seg << 16) | (base & 0xffff);
51 *b = (base & 0xffff0000) | ((type & 0xff) << 8) | (flags & 0xff);
52}
53
54#define DESCTYPE_LDT 0x82 /* present, system, DPL-0, LDT */
55#define DESCTYPE_TSS 0x89 /* present, system, DPL-0, 32-bit TSS */
56#define DESCTYPE_TASK 0x85 /* present, system, DPL-0, task gate */
57#define DESCTYPE_INT 0x8e /* present, system, DPL-0, interrupt gate */
58#define DESCTYPE_TRAP 0x8f /* present, system, DPL-0, trap gate */
59#define DESCTYPE_DPL3 0x60 /* DPL-3 */
60#define DESCTYPE_S 0x10 /* !system */
61
62#define load_TR_desc() __asm__ __volatile__("ltr %w0"::"q" (GDT_ENTRY_TSS*8))
Rusty Russell522e93e2006-09-26 10:52:35 +020063
64#define load_gdt(dtr) __asm__ __volatile("lgdt %0"::"m" (*dtr))
65#define load_idt(dtr) __asm__ __volatile("lidt %0"::"m" (*dtr))
66#define load_tr(tr) __asm__ __volatile("ltr %0"::"m" (tr))
67#define load_ldt(ldt) __asm__ __volatile("lldt %0"::"m" (ldt))
68
69#define store_gdt(dtr) __asm__ ("sgdt %0":"=m" (*dtr))
70#define store_idt(dtr) __asm__ ("sidt %0":"=m" (*dtr))
71#define store_tr(tr) __asm__ ("str %0":"=m" (tr))
72#define store_ldt(ldt) __asm__ ("sldt %0":"=m" (ldt))
73
74#if TLS_SIZE != 24
75# error update this code.
76#endif
77
78static inline void load_TLS(struct thread_struct *t, unsigned int cpu)
79{
80#define C(i) get_cpu_gdt_table(cpu)[GDT_ENTRY_TLS_MIN + i] = t->tls_array[i]
81 C(0); C(1); C(2);
82#undef C
83}
84
85static inline void write_dt_entry(void *dt, int entry, __u32 entry_a, __u32 entry_b)
86{
87 __u32 *lp = (__u32 *)((char *)dt + entry*8);
88 *lp = entry_a;
89 *(lp+1) = entry_b;
90}
91
92#define write_ldt_entry(dt, entry, a, b) write_dt_entry(dt, entry, a, b)
93#define write_gdt_entry(dt, entry, a, b) write_dt_entry(dt, entry, a, b)
94#define write_idt_entry(dt, entry, a, b) write_dt_entry(dt, entry, a, b)
95
96static inline void _set_gate(int gate, unsigned int type, void *addr, unsigned short seg)
97{
98 __u32 a, b;
99 pack_gate(&a, &b, (unsigned long)addr, seg, type, 0);
100 write_idt_entry(idt_table, gate, a, b);
101}
102
103static inline void __set_tss_desc(unsigned int cpu, unsigned int entry, const void *addr)
104{
105 __u32 a, b;
106 pack_descriptor(&a, &b, (unsigned long)addr,
107 offsetof(struct tss_struct, __cacheline_filler) - 1,
108 DESCTYPE_TSS, 0);
109 write_gdt_entry(get_cpu_gdt_table(cpu), entry, a, b);
110}
111
Jeremy Fitzhardingee5e3a042006-12-07 02:14:01 +0100112static inline void set_ldt(void *addr, unsigned int entries)
Rusty Russell522e93e2006-09-26 10:52:35 +0200113{
Jeremy Fitzhardingee5e3a042006-12-07 02:14:01 +0100114 if (likely(entries == 0))
115 __asm__ __volatile__("lldt %w0"::"q" (0));
116 else {
117 unsigned cpu = smp_processor_id();
118 __u32 a, b;
119
120 pack_descriptor(&a, &b, (unsigned long)addr,
121 entries * sizeof(struct desc_struct) - 1,
122 DESCTYPE_LDT, 0);
123 write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_LDT, a, b);
124 __asm__ __volatile__("lldt %w0"::"q" (GDT_ENTRY_LDT*8));
125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128#define set_tss_desc(cpu,addr) __set_tss_desc(cpu, GDT_ENTRY_TSS, addr)
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#define LDT_entry_a(info) \
131 ((((info)->base_addr & 0x0000ffff) << 16) | ((info)->limit & 0x0ffff))
132
133#define LDT_entry_b(info) \
134 (((info)->base_addr & 0xff000000) | \
135 (((info)->base_addr & 0x00ff0000) >> 16) | \
136 ((info)->limit & 0xf0000) | \
137 (((info)->read_exec_only ^ 1) << 9) | \
138 ((info)->contents << 10) | \
139 (((info)->seg_not_present ^ 1) << 15) | \
140 ((info)->seg_32bit << 22) | \
141 ((info)->limit_in_pages << 23) | \
142 ((info)->useable << 20) | \
143 0x7000)
144
145#define LDT_empty(info) (\
146 (info)->base_addr == 0 && \
147 (info)->limit == 0 && \
148 (info)->contents == 0 && \
149 (info)->read_exec_only == 1 && \
150 (info)->seg_32bit == 0 && \
151 (info)->limit_in_pages == 0 && \
152 (info)->seg_not_present == 1 && \
153 (info)->useable == 0 )
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static inline void clear_LDT(void)
156{
Jeremy Fitzhardingee5e3a042006-12-07 02:14:01 +0100157 set_ldt(NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
160/*
161 * load one particular LDT into the current CPU
162 */
Jeremy Fitzhardingee5e3a042006-12-07 02:14:01 +0100163static inline void load_LDT_nolock(mm_context_t *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Jeremy Fitzhardingee5e3a042006-12-07 02:14:01 +0100165 set_ldt(pc->ldt, pc->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
168static inline void load_LDT(mm_context_t *pc)
169{
Jeremy Fitzhardingee5e3a042006-12-07 02:14:01 +0100170 preempt_disable();
171 load_LDT_nolock(pc);
172 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175static inline unsigned long get_desc_base(unsigned long *desc)
176{
177 unsigned long base;
178 base = ((desc[0] >> 16) & 0x0000ffff) |
179 ((desc[1] << 16) & 0x00ff0000) |
180 (desc[1] & 0xff000000);
181 return base;
182}
183
184#endif /* !__ASSEMBLY__ */
185
186#endif