Skip to content
Snippets Groups Projects
x86.h 2.82 KiB
Newer Older
rtm's avatar
rtm committed
static __inline uchar
rtm's avatar
rtm committed
inb(int port)
{
  uchar data;
  __asm __volatile("inb %w1,%0" : "=a" (data) : "d" (port));
  return data;
rtm's avatar
rtm committed
}

static __inline void
insl(int port, void *addr, int cnt)
{
  __asm __volatile("cld\n\trepne\n\tinsl"     :
                   "=D" (addr), "=c" (cnt)    :
                   "d" (port), "0" (addr), "1" (cnt)  :
                   "memory", "cc");
rtm's avatar
rtm committed
}

static __inline void
rtm's avatar
rtm committed
outb(int port, uchar data)
rtm's avatar
rtm committed
{
  __asm __volatile("outb %0,%w1" : : "a" (data), "d" (port));
rtm's avatar
rtm committed
}

static __inline void
rtm's avatar
rtm committed
outw(int port, ushort data)
rtm's avatar
rtm committed
{
  __asm __volatile("outw %0,%w1" : : "a" (data), "d" (port));
rtm's avatar
rtm committed
}

static __inline void
outsl(int port, const void *addr, int cnt)
{
  __asm __volatile("cld\n\trepne\n\toutsl"    :
                   "=S" (addr), "=c" (cnt)    :
                   "d" (port), "0" (addr), "1" (cnt)  :
                   "cc");
rtm's avatar
rtm committed
}

struct segdesc;
rtm's avatar
rtm committed

static __inline void
lgdt(struct segdesc *p, int size)
rtm's avatar
rtm committed
{
  volatile ushort pd[3];
  pd[0] = size-1;
  pd[1] = (uint)p;
  pd[2] = (uint)p >> 16;
rsc's avatar
 
rsc committed

  asm volatile("lgdt (%0)" : : "g" (pd));
rsc's avatar
 
rsc committed
}

struct gatedesc;

rsc's avatar
 
rsc committed
static __inline void
lidt(struct gatedesc *p, int size)
rsc's avatar
 
rsc committed
{
  volatile ushort pd[3];
  pd[0] = size-1;
  pd[1] = (uint)p;
  pd[2] = (uint)p >> 16;
  asm volatile("lidt (%0)" : : "g" (pd));
rtm's avatar
rtm committed
}

static __inline void
rtm's avatar
rtm committed
ltr(ushort sel)
rtm's avatar
rtm committed
{
  __asm __volatile("ltr %0" : : "r" (sel));
rtm's avatar
rtm committed
}

rtm's avatar
rtm committed
static __inline uint
rtm's avatar
rtm committed
read_eflags(void)
{
  uint eflags;
  __asm __volatile("pushfl; popl %0" : "=r" (eflags));
  return eflags;
rtm's avatar
rtm committed
}

static __inline void
rtm's avatar
rtm committed
write_eflags(uint eflags)
rtm's avatar
rtm committed
{
  __asm __volatile("pushl %0; popfl" : : "r" (eflags));
rtm's avatar
rtm committed
}

static __inline void
rtm's avatar
rtm committed
cpuid(uint info, uint *eaxp, uint *ebxp, uint *ecxp, uint *edxp)
rtm's avatar
rtm committed
{
  uint eax, ebx, ecx, edx;
  asm volatile("cpuid" :
               "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) :
               "a" (info));
rsc's avatar
rsc committed
  if(eaxp)
    *eaxp = eax;
rsc's avatar
rsc committed
  if(ebxp)
    *ebxp = ebx;
rsc's avatar
rsc committed
  if(ecxp)
    *ecxp = ecx;
rsc's avatar
rsc committed
  if(edxp)
rtm's avatar
rtm committed
}

rtm's avatar
rtm committed
static __inline uint
cmpxchg(uint oldval, uint newval, volatile uint* lock_addr)
rtm's avatar
rtm committed
  uint result;
  __asm__ __volatile__("lock; cmpxchgl %2, %0" :
                       "+m" (*lock_addr), "=a" (result) :
                       "r"(newval), "1"(oldval) :
                       "cc");
rsc's avatar
 
rsc committed
static __inline void
cli(void)
{
  __asm__ volatile("cli");
rsc's avatar
 
rsc committed
}

static __inline void
sti(void)
{
  __asm__ volatile("sti");
rsc's avatar
 
rsc committed
}

struct trapframe {
rsc's avatar
rsc committed
  // registers as pushed by pusha
  uint edi;
  uint esi;
  uint ebp;
rsc's avatar
rsc committed
  uint oesp;      // useless & ignored
  uint ebx;
  uint edx;
  uint ecx;
  uint eax;
rsc's avatar
rsc committed

  // rest of trap frame
  ushort es;
  ushort padding1;
  ushort ds;
  ushort padding2;
  uint trapno;
rsc's avatar
rsc committed

  // below here defined by x86 hardware
  uint err;
  uint eip;
  ushort cs;
  ushort padding3;
  uint eflags;
rsc's avatar
rsc committed

  // below here only when crossing rings, such as from user to kernel
  uint esp;
  ushort ss;
  ushort padding4;
rtm's avatar
rtm committed
};