/*
  cpu.h, Copyright  (c) by Lukas Ruf, lr@lpr.ch,
  Swiss Federal Institute of Technology,
  Computer Engineering and Networks Laboratory.
 
  TOPSY -- A Teachable Operating System.
           Implementation of a tiny and simple
           micro kernel for teaching purposes.
 
  For further information, please visit http://www.tik.ee.ethz.ch/~topsy
 
  This software is provided under the terms of the GNU General Public Licence.
 
  A full copy of the GNU GPL is provided in the file COPYING found in the 
  development root of Topsy.
*/
/*
	File:                  $Source: /home/ruf/cvs_Topsy/cvs/Topsy/Topsy/ia32/cpu.h,v $
 	Author(s):             Lukas Ruf, lr@lpr.ch
 	Affiliation:           ETH Zuerich, TIK
 	Version:               $Revision: 3038 $
 	Last Date of Change:   $Date: 2007-06-05 00:18:19 +0200 (Die, 05 Jun 2007) $      by: $Author: sryffel $
*/

#ifndef __CPU_H__
#define __CPU_H__

/* CoreLoad.h is included to make RM Addresses available to kernel:
      Load Address of:
      - Kernel
      - User
      - Driver (if so :-}) */
#include "CoreLoad.h"

#define PHYS_BASE     0
/* If you modify thes values don't forget to have a look at DisplayContext() in
   IO/ia32/Debug.c and restoreContext() in Threads/ia32/TMHalAsm.S because they
   check the stack boundaries CJ 20010226 */
#define KUSEG_BASE    0x00000000  /* @0000 */
#define KDATASEG_BASE 0x00000000  /* @0000: Kernel Data Segment */
#define KDATASEG_SIZE 0xFFFFFFFF  /* 4GB */
#define K1SEG_BASE    0x00000000  /* @0000: i386-specific Data Region   */
#define K1SEG_SIZE    0x00020000  /* 128KB: Ctrl Data Region */
#define K0SEG_BASE    0x00000000  /* @0000: Kernel Code Segemnt */
#define K0SEG_SIZE    0x000A0000  /* 640KB */
#define K2SEG_BASE    0x000A0000  /* @640KB: Hardware IO Region */
#define K2SEG_SIZE    0x00060000  /* 360KB: */

#define STEALALLOC    0x00800000  /* 8Mb: XXX */


/* Virtual Memory Address Base of the Kernel */
#define KERNEL_VMA_BASE   0x020000 /* @128KB: Virtual Memory Address of Kernel */  
#define USER_VMA_BASE     0x200000 /* @2MB  : Virtual Memory Address of User   */  

/* User Address relative to Kernel Data Segment */
#define UADDR_BASE    (KUSEG_BASE - K0SEG_BASE)

#define UADDR_START   0x40        /* define the user start address right after
the binary file header:
64B for binary file
information should be enough, I hope */
/* User Load Address relative to Kernel Data Segment */
#define USERBIN_ADDR  ((USERSEG * 16) + USEROFS - K0SEG_BASE)   /* address of the user.bin in topsyimage */
#define ULOAD_ADDR    ((USERSEG * 16) + USEROFS - K0SEG_BASE)
/* Driver Load Address relative to Kernel Data Segment */
#define DLOAD_ADDR    ((DRIVSEG * 16) + DRIVOFS - K0SEG_BASE)

#define ISA_KUSEG(x)    ((unsigned)(x)  >= KUSEG_BASE)
#define ISA_K0SEG(x)    (((unsigned)(x) >= K0SEG_BASE) && \
         ((unsigned)(x) <  K2SEG_BASE))
#define ISA_K1SEG(x)    (((unsigned)(x) >= K1SEG_BASE) && \
         ((unsigned)(x) <  K0SEG_BASE))
#define ISA_K2SEG(x)    (((unsigned)(x)  >= K2SEG_BASE) && \
         ((unsigned)(x) <  KUSEG_BASE))


#define K0SEG_TO_PHYS(x)  ((unsigned)(x) + K0SEG_BASE)
#define PHYS_TO_K0SEG(x)  ((unsigned)(x) - K0SEG_BASE)

#define K1SEG_TO_PHYS(x)  ((unsigned)(x) + K1SEG_BASE)
#define PHYS_TO_K1SEG(x)  ((unsigned)(x) - K1SEG_BASE)

#define K0SEG_TO_K1SEG(x) ((unsigned)(x) + K0SEG_BASE)
#define K1SEG_TO_K0SEG(x) ((unsigned)(x) - K0SEG_BASE)

#define U_TO_K(x)         ((unsigned)(x)  + UADDR_BASE)
#define K_TO_U(x)         ((unsigned)(x)  - UADDR_BASE)

#define PAGEBOUNDARY(x)   ((((x) >> 12) + 1) << 12)

#endif



