root/include/asm-i386/system.h

/* [previous][next][first][last][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. _get_base
  2. get_limit
  3. __xchg

   1 #ifndef __ASM_SYSTEM_H
   2 #define __ASM_SYSTEM_H
   3 
   4 #include <asm/segment.h>
   5 
   6 /*
   7  * Entry into gdt where to find first TSS. GDT layout:
   8  *   0 - nul
   9  *   1 - kernel code segment
  10  *   2 - kernel data segment
  11  *   3 - user code segment
  12  *   4 - user data segment
  13  * ...
  14  *   8 - TSS #0
  15  *   9 - LDT #0
  16  *  10 - TSS #1
  17  *  11 - LDT #1
  18  */
  19 #define FIRST_TSS_ENTRY 8
  20 #define FIRST_LDT_ENTRY (FIRST_TSS_ENTRY+1)
  21 #define _TSS(n) ((((unsigned long) n)<<4)+(FIRST_TSS_ENTRY<<3))
  22 #define _LDT(n) ((((unsigned long) n)<<4)+(FIRST_LDT_ENTRY<<3))
  23 #define load_TR(n) __asm__("ltr %%ax": /* no output */ :"a" (_TSS(n)))
  24 #define load_ldt(n) __asm__("lldt %%ax": /* no output */ :"a" (_LDT(n)))
  25 #define store_TR(n) \
  26 __asm__("str %%ax\n\t" \
  27         "subl %2,%%eax\n\t" \
  28         "shrl $4,%%eax" \
  29         :"=a" (n) \
  30         :"0" (0),"i" (FIRST_TSS_ENTRY<<3))
  31 
  32 /* This special macro can be used to load a debugging register */
  33 
  34 #define loaddebug(tsk,register) \
  35                 __asm__("movl %0,%%edx\n\t" \
  36                         "movl %%edx,%%db" #register "\n\t" \
  37                         : /* no output */ \
  38                         :"m" (tsk->debugreg[register]) \
  39                         :"dx");
  40 
  41 
  42 /*
  43  *      switch_to(n) should switch tasks to task nr n, first
  44  * checking that n isn't the current task, in which case it does nothing.
  45  * This also clears the TS-flag if the task we switched to has used
  46  * the math co-processor latest.
  47  *
  48  * It also reloads the debug regs if necessary..
  49  */
  50 
  51  
  52 #ifdef __SMP__
  53         /*
  54          *      Keep the lock depth straight. If we switch on an interrupt from
  55          *      kernel->user task we need to lose a depth, and if we switch the
  56          *      other way we need to gain a depth. Same layer switches come out
  57          *      the same.
  58          *
  59          *      We spot a switch in user mode because the kernel counter is the
  60          *      same as the interrupt counter depth. (We never switch during the
  61          *      message/invalidate IPI).
  62          *
  63          *      We fsave/fwait so that an exception goes off at the right time
  64          *      (as a call from the fsave or fwait in effect) rather than to
  65          *      the wrong process.
  66          */
  67 
  68 #define switch_to(prev,next) do { \
  69         cli();\
  70         if(prev->flags&PF_USEDFPU) \
  71         { \
  72                 __asm__ __volatile__("fnsave %0":"=m" (prev->tss.i387.hard)); \
  73                 __asm__ __volatile__("fwait"); \
  74                 prev->flags&=~PF_USEDFPU;        \
  75         } \
  76         prev->lock_depth=syscall_count; \
  77         kernel_counter+=next->lock_depth-prev->lock_depth; \
  78         syscall_count=next->lock_depth; \
  79 __asm__("pushl %%edx\n\t" \
  80         "movl "SYMBOL_NAME_STR(apic_reg)",%%edx\n\t" \
  81         "movl 0x20(%%edx), %%edx\n\t" \
  82         "shrl $22,%%edx\n\t" \
  83         "and  $0x3C,%%edx\n\t" \
  84         "movl %%ecx,"SYMBOL_NAME_STR(current_set)"(,%%edx)\n\t" \
  85         "popl %%edx\n\t" \
  86         "ljmp %0\n\t" \
  87         "sti\n\t" \
  88         : /* no output */ \
  89         :"m" (*(((char *)&next->tss.tr)-4)), \
  90          "c" (next)); \
  91         /* Now maybe reload the debug registers */ \
  92         if(prev->debugreg[7]){ \
  93                 loaddebug(prev,0); \
  94                 loaddebug(prev,1); \
  95                 loaddebug(prev,2); \
  96                 loaddebug(prev,3); \
  97                 loaddebug(prev,6); \
  98         } \
  99 } while (0)
 100 
 101 #else
 102 #define switch_to(prev,next) do { \
 103 __asm__("movl %2,"SYMBOL_NAME_STR(current_set)"\n\t" \
 104         "ljmp %0\n\t" \
 105         "cmpl %1,"SYMBOL_NAME_STR(last_task_used_math)"\n\t" \
 106         "jne 1f\n\t" \
 107         "clts\n" \
 108         "1:" \
 109         : /* no outputs */ \
 110         :"m" (*(((char *)&next->tss.tr)-4)), \
 111          "r" (prev), "r" (next)); \
 112         /* Now maybe reload the debug registers */ \
 113         if(prev->debugreg[7]){ \
 114                 loaddebug(prev,0); \
 115                 loaddebug(prev,1); \
 116                 loaddebug(prev,2); \
 117                 loaddebug(prev,3); \
 118                 loaddebug(prev,6); \
 119         } \
 120 } while (0)
 121 #endif
 122 
 123 #define _set_base(addr,base) \
 124 __asm__("movw %%dx,%0\n\t" \
 125         "rorl $16,%%edx\n\t" \
 126         "movb %%dl,%1\n\t" \
 127         "movb %%dh,%2" \
 128         : /* no output */ \
 129         :"m" (*((addr)+2)), \
 130          "m" (*((addr)+4)), \
 131          "m" (*((addr)+7)), \
 132          "d" (base) \
 133         :"dx")
 134 
 135 #define _set_limit(addr,limit) \
 136 __asm__("movw %%dx,%0\n\t" \
 137         "rorl $16,%%edx\n\t" \
 138         "movb %1,%%dh\n\t" \
 139         "andb $0xf0,%%dh\n\t" \
 140         "orb %%dh,%%dl\n\t" \
 141         "movb %%dl,%1" \
 142         : /* no output */ \
 143         :"m" (*(addr)), \
 144          "m" (*((addr)+6)), \
 145          "d" (limit) \
 146         :"dx")
 147 
 148 #define set_base(ldt,base) _set_base( ((char *)&(ldt)) , base )
 149 #define set_limit(ldt,limit) _set_limit( ((char *)&(ldt)) , (limit-1)>>12 )
 150 
 151 static inline unsigned long _get_base(char * addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 152 {
 153         unsigned long __base;
 154         __asm__("movb %3,%%dh\n\t"
 155                 "movb %2,%%dl\n\t"
 156                 "shll $16,%%edx\n\t"
 157                 "movw %1,%%dx"
 158                 :"=&d" (__base)
 159                 :"m" (*((addr)+2)),
 160                  "m" (*((addr)+4)),
 161                  "m" (*((addr)+7)));
 162         return __base;
 163 }
 164 
 165 #define get_base(ldt) _get_base( ((char *)&(ldt)) )
 166 
 167 static inline unsigned long get_limit(unsigned long segment)
     /* [previous][next][first][last][top][bottom][index][help] */
 168 {
 169         unsigned long __limit;
 170         __asm__("lsll %1,%0"
 171                 :"=r" (__limit):"r" (segment));
 172         return __limit+1;
 173 }
 174 
 175 #define nop() __asm__ __volatile__ ("nop")
 176 
 177 /*
 178  * Clear and set 'TS' bit respectively
 179  */
 180 #define clts() __asm__ __volatile__ ("clts")
 181 #define stts() \
 182 __asm__ __volatile__ ( \
 183         "movl %%cr0,%%eax\n\t" \
 184         "orl $8,%%eax\n\t" \
 185         "movl %%eax,%%cr0" \
 186         : /* no outputs */ \
 187         : /* no inputs */ \
 188         :"ax")
 189 
 190 
 191 #define xchg(ptr,x) ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
 192 #define tas(ptr) (xchg((ptr),1))
 193 
 194 struct __xchg_dummy { unsigned long a[100]; };
 195 #define __xg(x) ((struct __xchg_dummy *)(x))
 196 
 197 static inline unsigned long __xchg(unsigned long x, void * ptr, int size)
     /* [previous][next][first][last][top][bottom][index][help] */
 198 {
 199         switch (size) {
 200                 case 1:
 201                         __asm__("xchgb %b0,%1"
 202                                 :"=&q" (x), "=m" (*__xg(ptr))
 203                                 :"0" (x), "m" (*__xg(ptr)));
 204                         break;
 205                 case 2:
 206                         __asm__("xchgw %w0,%1"
 207                                 :"=&r" (x), "=m" (*__xg(ptr))
 208                                 :"0" (x), "m" (*__xg(ptr)));
 209                         break;
 210                 case 4:
 211                         __asm__("xchgl %0,%1"
 212                                 :"=&r" (x), "=m" (*__xg(ptr))
 213                                 :"0" (x), "m" (*__xg(ptr)));
 214                         break;
 215         }
 216         return x;
 217 }
 218 
 219 #define mb()  __asm__ __volatile__ (""   : : :"memory")
 220 #define sti() __asm__ __volatile__ ("sti": : :"memory")
 221 #define cli() __asm__ __volatile__ ("cli": : :"memory")
 222 
 223 #define save_flags(x) \
 224 __asm__ __volatile__("pushfl ; popl %0":"=g" (x): /* no input */ :"memory")
 225 
 226 #define restore_flags(x) \
 227 __asm__ __volatile__("pushl %0 ; popfl": /* no output */ :"g" (x):"memory")
 228 
 229 #define iret() __asm__ __volatile__ ("iret": : :"memory")
 230 
 231 #define _set_gate(gate_addr,type,dpl,addr) \
 232 __asm__ __volatile__ ("movw %%dx,%%ax\n\t" \
 233         "movw %2,%%dx\n\t" \
 234         "movl %%eax,%0\n\t" \
 235         "movl %%edx,%1" \
 236         :"=m" (*((long *) (gate_addr))), \
 237          "=m" (*(1+(long *) (gate_addr))) \
 238         :"i" ((short) (0x8000+(dpl<<13)+(type<<8))), \
 239          "d" ((char *) (addr)),"a" (KERNEL_CS << 16) \
 240         :"ax","dx")
 241 
 242 #define set_intr_gate(n,addr) \
 243         _set_gate(&idt[n],14,0,addr)
 244 
 245 #define set_trap_gate(n,addr) \
 246         _set_gate(&idt[n],15,0,addr)
 247 
 248 #define set_system_gate(n,addr) \
 249         _set_gate(&idt[n],15,3,addr)
 250 
 251 #define set_call_gate(a,addr) \
 252         _set_gate(a,12,3,addr)
 253 
 254 #define _set_seg_desc(gate_addr,type,dpl,base,limit) {\
 255         *((gate_addr)+1) = ((base) & 0xff000000) | \
 256                 (((base) & 0x00ff0000)>>16) | \
 257                 ((limit) & 0xf0000) | \
 258                 ((dpl)<<13) | \
 259                 (0x00408000) | \
 260                 ((type)<<8); \
 261         *(gate_addr) = (((base) & 0x0000ffff)<<16) | \
 262                 ((limit) & 0x0ffff); }
 263 
 264 #define _set_tssldt_desc(n,addr,limit,type) \
 265 __asm__ __volatile__ ("movw $" #limit ",%1\n\t" \
 266         "movw %%ax,%2\n\t" \
 267         "rorl $16,%%eax\n\t" \
 268         "movb %%al,%3\n\t" \
 269         "movb $" type ",%4\n\t" \
 270         "movb $0x00,%5\n\t" \
 271         "movb %%ah,%6\n\t" \
 272         "rorl $16,%%eax" \
 273         : /* no output */ \
 274         :"a" (addr+0xc0000000), "m" (*(n)), "m" (*(n+2)), "m" (*(n+4)), \
 275          "m" (*(n+5)), "m" (*(n+6)), "m" (*(n+7)) \
 276         )
 277 
 278 #define set_tss_desc(n,addr) _set_tssldt_desc(((char *) (n)),((int)(addr)),235,"0x89")
 279 #define set_ldt_desc(n,addr,size) \
 280         _set_tssldt_desc(((char *) (n)),((int)(addr)),((size << 3) - 1),"0x82")
 281 
 282 /*
 283  * This is the ldt that every process will get unless we need
 284  * something other than this.
 285  */
 286 extern struct desc_struct default_ldt;
 287 
 288 /*
 289  * disable hlt during certain critical i/o operations
 290  */
 291 #define HAVE_DISABLE_HLT
 292 void disable_hlt(void);
 293 void enable_hlt(void);
 294 
 295 #endif

/* [previous][next][first][last][top][bottom][index][help] */