view cos/kernel/klib.c @ 407:9eb1fc6aad6c

Minor improvements
author Windel Bouwman
date Fri, 20 Feb 2015 15:47:54 +0100
parents 91f91ff07ea8
children
line wrap: on
line source

#include "kernel.h"

/* Panic and shutdown functions */
void magicBochsBreak()
{
   asm volatile("xchg %bx, %bx");
}

void panic(char *msg) 
{
   printf("Kernel panic: ");
   printf(msg);
   magicBochsBreak();
   halt();
}

void reboot()
{
   // Use the keyboard to reboot:
   while ( (inb(0x64) & 0x02) == 0x02)
   {
      ;
   }
   outb(0x64, 0xFE);
}

void halt()
{
   asm volatile("cli");  // Disable interrupts.
   asm volatile("hlt");  // Halt processor
}

/* IO port helpers: */
void outb(uint16_t port, uint8_t value)
{
   asm volatile ("outb %1, %0" : : "dN" (port), "a" (value));
}

uint8_t inb(uint16_t port)
{
   uint8_t ret;
   asm volatile ("inb %1, %0" : "=a" (ret) : "dN" (port));
   return ret;
}

uint16_t inw(uint16_t port)
{
   uint16_t ret;
   asm volatile ("inw %1, %0" : "=a" (ret) : "dN" (port));
   return ret;
}

/* string functions: */
int strncmp(const char* s1, const char* s2, int size) 
{
   int i;
   for (i=0; i<size; i++) 
   {
      if (s1[i] != s2[i]) return 0;
   }
   return 1;
}

void memset(void* data, uint8_t value, uint64_t size)
{
   uint64_t i;
   uint8_t *data2 = (uint8_t*)data;
   for (i = 0; i < size; i++)
   {
      data2[i] = value;
   }
}