//os.h Copyright (C)1989-2009 I.Pedley (CTPP) Sun 12-Apr-1998 at 08:52:11

//28.12.2009 type system revision

//version numbering////////////////////
#define VERSION         0x01000000      //default start is V1.00.00000
UL  getOSversion  (VD);                 //get live version for OS
UL  getprocversion(VD);                 //get live version for current process
VD  getprocvstring(CH* str);            //get version string for current process
VD  versionstring (CH* str,UL vno,UL zeroterminate);

//support for incremental version numbering used by project MAKE
//uses \chaos.ver file in root directory of current drive to hold advancing
//number, incremented only by  successful LINK (KC05) at present
EX  CH* vfname;                         //link to version file name
EX  UL  version;                        //global to hold incrementing version no
UL  getversion(VD);                     //version file handling
VD  setversion(VD);                     //version file handling

#define SUBVERSION      0x01020000      //SUBVERSION must match for modules
                                        //to compile link and exec together
///////////////////////////////////////

//20.10.2009 64-bit mode support
//scratch system tables:
#define mmIDT64         0x00100000
#define mmIDT64max      128
#define mmGDT64         0x00100800
#define mmGDT64max      32
#define mmPML4          0x00101000
#define mmPAGEDIR       0x00102000
#define mmPAGETABLE     0x00120000
#define mmTSS64BIT      0x00108000
#define mmTSS64STACKS   0x0010c000
#define mmTSS64STACKsiz 0x00000400
#define mmCALLBACKPTR   0x00110000

//global selector values:
#define CS32    0x10
#define DS32    0x20
#define CS16    0x30
#define DS16    0x40
#define TSS386  0x50
#define TSS64   0x60
#define CS64    0x70

//64-bit segment descriptor attributes:
//#define IDTattr 0x8e01      //64-bit interrupt gate, with stack switch
#define IDT64attr 0x8e00      //64-bit interrupt gate
#define GDT64code 0x209a      //for 64-bit code

#define NULL 0L

//flag settings for OSflags

#define SYSTEMSYMBOLS        0x00000001 //note this must match flag definition
                                        //in asm.hpp/ctp.hpp
                                        //for bootstrap compilation
#define DOSLOAD              0x00000002 //DOS is restartable via shutdown(10)
#define BIOSBOOT             0x00000004 //native ChaOS boot, used to grab copy
                                        //of boot BIOS vector table
#define NETWORKLOAD          0x00000008 //OS was loaded via network card
                                        //network boot rom workspace addresses
                                        //below although ugly, are valid
#define NETWORKRELOAD        0x00000010 //used to speed up network reboot by
                                        //skipping hardware probe

#define NETROMRESTARTFLAG   0x80006     //&1 tells boot rom to restart
                                        //&2 skips 3 second wait on restart
                                        //&4 skips ne_find() on restart
                                        //by setting NETWORKRELOAD in OSflags

#define NETCFGBUF           0x84000
#define SRVCFGBUF           0x84040
#define BDCPKTBUF           0x84080
#define RCVPKTBUF           0x840e0
#define CMDPKTBUF           0x84540
#define REPPKTBUF           0x849a0

//globals from loader,passed in registers
EX  UL  OSflags;    //in eax
EX  UL  OSbase;     //in ebx
EX  UL  OSsize;     //in edx
EX  UL  DOSpsp;     //in edi, zero if native bootstrap loaded the image

////system stack
////original stack size 8k
//#define STACKSIZE   0x2000
////increased to 10k 14.5.2000
//#define STACKSIZE   0x2400
////increased to 16k 22.10.2000
//#define STACKSIZE   0x4000
//EX  UC  STACK[STACKSIZE];
//EX  UL  stacktop;
//EX  UL  memtop;
////2.12.2000 move OS stacktop to OSbase
//stack now grows downwards just below OS image
#define STACKSIZE 0x10000
EX  UL  memtop;
EX  UL  stacktop;
EX  UL  stackbase;

//CTPP object/executable file structures

struct  TYPDEF
{
    TYPDEF* next;
    UC       type;
    UC       len;
    CH*      name;
    UL       data[1];
};

//TYPDEF type fields
#define TYPBASIC    0x00
#define TYPLIST     0x01
#define TYPARRAY    0x02
#define TYPFUNC     0x03
#define TYPSTRUCT   0x04
#define TYPNAMELIST 0x05
#define TYPUNION    0x06

//TYPDEF data array layout info
#define TYPBASICTYPE      0x00
#define TYPLISTITEMS      0x00
#define TYPLISTTYPES      0x01
#define TYPARRAYDEPTH     0x00
#define TYPARRAYTYPE      0x01
#define TYPFUNCRETURNTYPE 0x00
#define TYPFUNCNAMELIST   0x01
#define TYPFUNCARGLIST    0x02
#define TYPSTRUCTITEMS      0x00
#define TYPSTRUCTNAMELIST   0x01
#define TYPSTRUCTTYPELIST   0x02
#define TYPNAMELISTITEMS    0x00
#define TYPNAMELISTNAMES    0x01
#define TYPUNIONITEMS       0x00
#define TYPUNIONNAMELIST    0x01
#define TYPUNIONTYPELIST    0x02

//'extended' structure types 16.1.02
//initially we will allocate two extra dwords at TYPDEF->data[3] and [4]
#define STRUCTEXTRA     2
//and the bit meanings of these will be to flag the presence of:
//in TYPDEF->data[3];
#define CONSTRUCTOR         0x80000000
#define COPYCONSTRUCTOR     0x40000000
#define DESTRUCTOR          0x20000000
#define BASECLASSPRESENT    0x10000000
//if TYPDEF->data[3]&BASECLASSPRESENT,
//TYPDEF->data[5] is count of base classes
//and base class types are in TYPDEF->data[6] onwards


struct  SYMBOL
{
    SYMBOL* prev;
    SYMBOL* next;
    SYMBOL* Gprev;
    SYMBOL* Gnext;
    CH*     name;                       //pascal namestr
    UL      type;
    UL      nameidx;                    //nametable index if required
    UL      edo;
    UI      seg;
    UI      cur;
    UL      len;
    UL      scope_start;
    UL      scope_end;
    UC      lnkidx;
    UC      modsub;
    UI      modidx;
    UL      referenced;
    UL      usr1;
    UL      usr2;
};
struct LINE
{
    LINE* prev;
    LINE* next;
    CH*   text;
    UI    len;
};
struct LSEQ
{
    UL    lines;
    CH*   buf;
    LINE* line;
};
struct  RELOC
{
    UC  srctype;
    UC  dsttype;
    UL  src;
    UL  dst;
    UC  size;
    UL  srcoff;
    UL  dstoff;
};
struct  LINENUM
{
    UI  line;
    UI  col;
    UL  edo;
    UL  ctr;
    UC  codebyte;
    UC  flags;
};
struct  SOURCE
{
    SOURCE* next;
    UL  idx;
    UL  date;
    UL  time;
    CH* name;
    UL  scope_start;
    UL  scope_end;
    UL  linenums;
    UL  lnctr;
    LINENUM* ln;
    LSEQ* src;
    VD* userptr;

};

//29.3.2001 new structure for k1load to pass boot trail to ChaOS session
//duplicated in boot.h (make sure definitions remain the same)
//see also makebootinfo() in k1load.c

#define BIDOSDIRVALID   0x00000001
#define biSUBDIRBOOT    0x00000002

struct BOOTINFO
{
    UL  flags;
    UC  partition;
    UC  drive;
    UL  lba;
    UC* usr;
};
//28.1.2002 new structure for storage of macro table in the CBJ file
struct MACRO
{
    CH* ident;  //ident is a pascal string
    CH* equ;    //equ is an ascii string in the source buffer, length equlen
    UL  equlen;
    UL  parms;  //macro arguments
};

//defines for CTPP file header ftype field
//type 0to 5 are CBJHEADERs
#define FTINT    0x00000000
#define FTCBJ    0x00000001
#define FTXEC    0x00000002
#define FTDRV    0x00000003
#define FTMULTI  0x00000004
#define ftNEWCBJ 0x00000005
#define ftGRFONT 0x00000006
#define ftLYR    0x00000007

struct  CBJHEADER
{                       //offset
    UL  id;             //0x00
    UL  ftype;          //0x04
    UL  cflags;         //0x08
    UL  hdrlen;         //0x0c
    UL  boot;           //0x10
    UL  init;           //0x14
    UL  start;          //0x18
    UL  term;           //0x1c
    UL  cbase;          //0x20
    UL  clen;           //0x24
    UL  dbase;          //0x28
    UL  dlen;           //0x2c
    UL  edobase;        //0x30
    UL  dataoff;        //0x34
    UL  bssoff;         //0x38
    UL  bsslen;         //0x3c
    UC* data;           //0x40
    UL  datalen;        //0x44
    UL  names;          //0x48
    UL  types;          //0x4c
    UL  sources;        //0x50
    UL  symbols;        //0x54
    UL  relocations;    //0x58
    CH*         ntb;    //0x5c
    TYPDEF*     typ;    //0x60
    SOURCE*     src;    //0x64
    SYMBOL*     sym;    //0x68
    RELOC*      rlc;    //0x6c
    UL  file;           //0x70
    UL  ver;            //0x74      //19.11.00 new version field
    UL  heap;           //0x78
    UL  heapsize;       //0x7c
    UL* fastreloc;      //0x80
    UL  fastrelocs;     //0x84
    UL  linear;         //0x88
    UL  externals;      //0x8c      //added 7.4.00 to retain dynlink count
    TYPDEF*     typl;   //0x90      //new fields added 9.11.99
    UL      nexttypidx; //0x94      //to support multiple type tables
    SYMBOL*     syml;   //0x98
    TYPDEF** tlookup;   //0x9c
    CH  processname[16];//0xa0      //new fields added 19.1.2000 for run-time
    UL  dynlinks;       //0xb0      //dynamic linking support in ChaOS
    UL  patched;        //0xb4      //run-time flag for symbol-table readiness
    UC* savedata;       //0xb8
    UL  spare3;         //0xbc
    MACRO* mac;         //0xc0      //added 28.1.2002 to allow storage of
    UL  macros;         //0xc4      //macro table in .CBJ file format
    UC  calign;         //0xc8      //added 1.4.02 to hold alignment info
    UC  dalign;         //0xc9      //for code and data segments
    UI  processes;      //0xca      //added 20.4.02 for multi-XEC file
    CBJHEADER* next;    //0xcc      //process count and ->next pointer

    CBJHEADER* parent;  //0xd0      //added 9.2.03 run-time parent CBJHEADER
    CBJHEADER* child;   //0xd4
    UL  spare4[2] ;     //0xd8-0xdc
    UL  killesp;        //0xe0      //12.2.2003 *{kesp,kebp,kret} for kill()
    UL  kret;           //0xe4      //to be used to move runtime kill variables
    UL  kebp;           //0xe8      //off the stack
    UL  kesp;           //0xec
//17.7.04 variables for DRV
    UL  pcitype;        //0xf0
    UL  protocol;       //0xf4
    UL  devtype;        //0xf8
    UL  spare5;         //0xfc
//see dev.h for #defines fro DEV->protocol

//additional space added 28.11.2000 to round off header to one disk sector
//should provide enough space for new features to ChaOS already planned

//ideas for this space:
//1:revise all the above fields, & throw away those that are no longer used,
//  by replacing each in turn with new fields here! (not today)
//2:poss new fields:
//  UL  parentid;               //process which exec'd this process
//  UL  devuser,adminuser,user; //user privelege levels
//Multitasking?
//  UL  timeslices;             //timer ticks which this process has had
//  UC  slicesneeded;           //timer tick ratio required by this process,set
//  UL  slicesper;              //to inform scheduler of CPU requirements
//  UL  stackbase,stacksize;    //local stack if required
//  UL  stackptr;               //for pre-emptive multitask to resume process
//  SYSDESC32* GDT;             //not necessary for sub-tasks, as these can be
//  SYSDESC32* IDT;             //dynamically-linked, but useful for exec loader
                                //or bootstrap to init processor
//struct HOOK                   //a table of, say 16 of these structures could
//{UC intno,UC level,UL fwdptr};//keep track of interrupts hooked by this
                                //process,AND provide interrupt chaining.
                                //Instead of storing the actual vectors,
                                //this is the address of a relative 32-bit
                                //near jump inside our handler, which can
                                //be used to chain off the interrupt
                                //if the interrupt is never chained, jmp 0
                                //does nothing. Note that the relative jmp
                                //must be to the first instruction after
                                //the function prologue of the next in the
                                //chain

//first extra field a module count, to store the total number of compilation
//units in this file: set to 1 by ASM5, and used by linkers to keep track
//of total module count when combining .TMP files which themselves contain a
//number of modules.
    UL  modules;        //0x100
//19.12.00 six extra fields, placeholders for CODE and DATA segment
//selectors to be used by loaders when relocating 32-bit 16-bit modules.
//can be filled in by #asm directive to pass a value which can be used
//by STRIP to produce a binary to run at a given segment address.
//otherwise, whenever the value of CODE or DATA is taken in a module,
//a reference to EX UI __pm16CODE,__pm16DATA  etc generated. Hopefully
//just a little addition to the dynamic linking code can create links to these
//header fields, causing segment relocations to be correctly patched
    UI  pm32code;       //0x104
    UI  pm32data;       //0x106
    UI  pm16code;       //0x108
    UI  pm16data;       //0x10a
    UI  rm16code;       //0x10c
    UI  rm16data;       //0x10e
//4.1.01 extra fields to co-ordinate mode switching, for current real-mode and
//protected mode PIC masks
    UC  rmPIC1;         //0x110
    UC  rmPIC2;         //0x111
    UC  pmPIC1;         //0x112
    UC  pmPIC2;         //0x113
//7.2.01 run-time flags field, to supercede OSflags currently passed in EAX
//by loaders. Lower 5 bits reserved to be used as OSflags when ready, then:
#define rtRELOCSPATCHED 0x00000020  //to enable fastrelocs patch to be skipped
#define rtSTACKALREADY  0x00000040  //loader has created a stack frame already
#define rtBOOTINFOVALID 0x00000100  //loader has filled in CBJHEADER->bootinfo
#define rtMULTIXEC      0x00000200  //loader encountered MULTI.XEC file format
#define rtTSR           0x00001000  //module was loaded as a TSR
//8.3.03 more run-time flags to tidy up debugger, and to provide symbolic
//debugging of unlinked .CBJ modules (using RELOC table to match globals, rather
//than SYMBOL->edo match to addresses in code stream)
#define rtCODEBYTES     0x00010000  //disassemble with code byte display on left
#define rtNOLOCALS      0x00020000  //no call to checklocals() in disassembler
#define rtNOGLOBALS     0x00040000  //no call to checkglobals() in disassembler
#define rtCHECKRELOCS   0x00080000  //find globals symbols via RELOC table
#define rtSAVEDATASEG   0x00100000  //cause exec to copy data seg before init()
    UL  rtflags;        //0x114

//28.2.01 structure used in conjunction with rtBOOTINFOVALID to provide trail
//to boot image file for ChaOS cold boot
    BOOTINFO* bootinfo; //0x118
//8.3.03 placeholder for currentfunction during debugging
    SYMBOL* curfunc;    //0x11c
//5.1.06 multiple DRVs allowed in single process
    UL  drvs;           //0x120
    VD* drvtbl;         //0x124
//20.11.08 vmode support
    UI  vmreq;          //0x128
    UI  vmreqw;         //0x12a
    UI  vmreqh;         //0x12c
    UI  vmreqbpp;       //0x12e
    UI  vmcur;          //0x130
    UI  vmstart;        //0x132

    UL  padding[3];     //0x134
    UL  lh,lhsz;        //0x140     //8.1.2010 for new local heap strategy
    VD* mpstk;          //0x148     //16.1.2010 xecs for APs
    UL  mpstksiz;       //0x14c
    UL  mpespsav;       //0x150
    UL  extra[43];      //0x154
};

//21.4.2002, new structures CBJMULTI and LODREC to support
//multi-process .XEC files. This process relies on MULTI .XEC files being
//contiguous on disk
struct  LODREC
{
    CH  processname[12];    //name of command to be matched by invoke()
    UI  ldrive;             //0=boot drive, otherwise, to be filled in by system
                            //with LDRIVE index to provide access to process
    UI  ftype;
    UL  lba;                //relative to beginning of parent file on disk
                            //and adjusted when building catalog
    UL  sectors;            //length of process, in sectors on disk
    CBJHEADER* mem;         //non-zero when loaded
    UC* data;               //copy of process data, copied back in for re-exec
};
//512-byte header for FTMULTI files
struct CBJMULTI
{
    UL  id;             //0x00
    UL  ftype;          //0x04  FTMULTI=4
    UL  cflags;         //0x08
    UL  hdrlen;         //0x0c
    UL  processes;      //0x10
    UL  spare[3];       //0x14
    LODREC  lr[15];     //0x20
};

//TYPE support//////////////////////////////////////////////

//basic type bits
#define tPTRSIZE             4          //model dependent
#define tSIZE       0x00000007          //and with this to get space req for data
#define tSIGN       0x00000080
#define tREV        0x00000020          //Motorola integer format
#define tFP         0x00000040          //Floating point type
#define tSHIFTDIR   0x00000800          //decimal shift direction, 1=*10, 0=/10
#define tSHIFTCNT   0x00000700          //decimal shift count

//inbuilt types
#define tVD 0
#define tUC 1
#define tCH (1|tSIGN)
#define tSC (1|tSIGN)
#define tUI 2
#define tSI (2|tSIGN)
#define tUm (2|tREV)
#define tSm (2|tREV|tSIGN)
#define tUL 4
#define tSL (4|tSIGN)
#define tUM (4|tREV)
#define tSM (4|tREV|tSIGN)
#define tDP 6
#define tDB (8|tFP)
#define tFL (4|tFP)
#define tSLV1 (4|0x100|tSIGN)
#define tSLV2 (4|0x200|tSIGN)
#define tSLV3 (4|0x300|tSIGN)
#define tELLIPSIS   (1|tREV)

#define tNULL   tREV                //runtime only, replaced by DEFAULTDATATYPE

#define tUSER       0x00002000          //type is in TYPDEF table
#define mUSER       0x00001fff          //mask for TYPDEF index
#define tPTR        0x00008000          //this is ptr to type
#define tFAR        0x00004000          //this is far modifier
#define mPTR        0x0000c000
#define tREF        0x10000000          //this is reference to type
#define mSIZE       0x0000000f          //mask to obtain size of basic types

#define tSTATIC     0x00010000
#define tEXTERN     0x00020000
#define tLOCAL      0x00040000
#define tGLOBAL     0x00080000
#define tCODE       0x00100000
#define tDATA       0x00200000
#define tCONST      0x00400000
#define tBSS        0x00800000
#define tCALL32     0x00800000
#define tCDECL      0x00400000
#define tPASCAL     0x00800000
#define tINTERRUPT  0x00c00000
#define tEXCEPTION  0x80c00000
#define mCLASS      0x08ff0000
#define mFUNC       0x00c00000
#define tFUNC       0x01000000
#define tSCOPE      0x04000000
#define tREGISTER   0x02000000
#define tSYSTEM     0x08000000

#define tPROCESSOR  0x80000000      //see \i\ctp.htm for tPROCESSOR subtypes
                                    //used by compiler and inline assembler

//CBJHEADER->cflags referred to by ChaOS    //Important general note on flags:
#define EMBEDDEDSOURCE          0x02000000  //flags are OR'ed to set, AND NOT'd
//CBJHEADER->patched flags                  //to clear...so notice here
#define HEADERPATCHED           0x00000001  //don't define a flag value 0x80 -
#define TYPESPATCHED            0x00000002  //our inline assembler compiles this
#define SOURCESPATCHED          0x00000004  //as OR sym,Ib - if sym is WORD or
#define SYMBOLSPATCHED          0x00000008  //DWORD, then Ib is expanded as a
#define RELOCATIONSPATCHED      0x00000010  //SIGNED value, and sym is OR'ed
#define HEAPPATCHED             0x00000020  //with 0xff80 or 0xffffff80!!!!!!
#define DYNAMICLINKSPATCHED     0x00000040
#define FASTRELOCSPATCHED       0x00000100
#define MACROSPATCHED           0x00000200

//environment string management  see cli.ctp
struct  ENV
{
    ENV*    prev;
    ENV*    next;
    CH*     str;
    CH*     equ;
    UL      active;
};
ENV* findenvironmentstring(CH* str);

//process.ctp
#define MAXPROCESSES    20
EX  CBJHEADER* cbcur;
EX  CBJHEADER* process[MAXPROCESSES];
EX  UL         processes;

CBJHEADER* thread(VD);
VD  setthread(CBJHEADER* c);

//31/1/01 embryo structure to (try to) identify machine-specific parameters,
//initially to store virgin BIOS vector table needed by ChaOS fast
//reboot procedures
//the idea is, do a CRC32 of the memory area 0xf0000->0xfffff (the Main BIOS
//ROM chip) and this will give us a unique 32-bit value identifying the
//machine we are currently running on; convert this value to an 8-character
//ASCII file name, and use this file to store machine specific information.
//ChaOS running on a moveable hard disk could store machine-specific data
//for each different BIOS ID encountered.
//See getmachineID() in \z\chaos\part.ctp for the way the machine data is
//collected, and boot() in \z\chaos\part.ctp for the way this info is used.

struct MACHINE
{
    UL  mainbioscrc;
    UL  allbioscrc;
    UL  biosvect[0x100];
};