about summary refs log tree commit diff stats
path: root/tools/iso/kernel.soso/common.h
blob: 7f04785ef3ebaa79b7f94823c564b38a26946cd5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#ifndef COMMON_H
#define COMMON_H

#define enableInterrupts() asm volatile("sti")
#define disableInterrupts() asm volatile("cli")
#define halt() asm volatile("hlt")


typedef unsigned  long long uint64;
typedef signed    long long int64;
typedef unsigned  int       uint32;
typedef           int       int32;
typedef unsigned  short     uint16;
typedef           short     int16;
typedef unsigned  char      uint8;
typedef           char      int8;
typedef unsigned  int       size_t;

#define BOOL uint8
#define TRUE 1
#define FALSE 0
#define NULL 0

// kernel memory map
//   code
#define KERN_PAGE_DIRECTORY     0x00001000
#define KERN_BASE               0x00100000
//   4k page directories
#define KERN_PD_AREA_BEGIN      0x00800000 //  8 MB
#define KERN_PD_AREA_END        0x00C00000 // 12 MB
//   4MB unused (used to be for mini heap)
#define KERN_NOTUSED_BEGIN      0x00C00000 // 12 MB
#define KERN_NOTUSED_END        0x01000000 // 16 MB
//
#define RESERVED_AREA           0x01000000 // 16 MB
//   frame buffer
#define GFX_MEMORY              0x01000000 // 16 MB
//   heap
#define KERN_HEAP_BEGIN         0x02000000 // 32 MB
#define KERN_HEAP_END           0x40000000 //  1 GB


#define PAGE_INDEX_4K(addr)     ((addr) >> 12)
#define PAGE_INDEX_4M(addr)     ((addr) >> 22)
#define PAGING_FLAG             0x80000000  // CR0 - bit 31
#define PSE_FLAG                0x00000010  // CR4 - bit 4
#define PG_PRESENT              0x00000001  // page directory / table
#define PG_WRITE                0x00000002
#define PG_USER                 0x00000004
#define PG_4MB                  0x00000080
#define PAGESIZE_4K             0x00001000
#define PAGESIZE_4M             0x00400000
#define RAM_AS_4K_PAGES         0x100000
#define RAM_AS_4M_PAGES         1024

#define KERNELMEMORY_PAGE_COUNT 256 //(KERN_HEAP_END / PAGESIZE_4M)

#define KERN_STACK_SIZE         PAGESIZE_4K

//KERN_HEAP_END ends and this one starts
#define USER_OFFSET             0x40000000
#define USER_OFFSET_END         0xF0000000
#define USER_OFFSET_MMAP        0xF0000000
#define USER_OFFSET_MMAP_END    0xFFFFFFFF

#define USER_EXE_IMAGE          0x200000 //2MB
#define USER_ARGV_ENV_SIZE      0x10000  //65KB
#define USER_ARGV_ENV_LOC       (USER_OFFSET + (USER_EXE_IMAGE - USER_ARGV_ENV_SIZE))
//This means we support executable images up to 2MB
//And user malloc functions will start from USER_OFFSET + USER_EXE_IMAGE
//We will 65KB (0x10000) for argv and environ just before user malloc start
//So USER_EXE_IMAGE - USER_ARGV_ENV_SIZE = 0x1F0000
//That means argv and env data will start from USER_OFFSET + 0x1F0000
//Of course libc should know this numbers :)

#define USER_STACK          0xF0000000

void outb(uint16 port, uint8 value);
void outw(uint16 port, uint16 value);
uint8 inb(uint16 port);
uint16 inw(uint16 port);

#define PANIC(msg) panic(msg, __FILE__, __LINE__);
#define WARNING(msg) warning(msg, __FILE__, __LINE__);
#define ASSERT(b) ((b) ? (void)0 : panic_assert(__FILE__, __LINE__, #b))

#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))

void warning(const char *message, const char *file, uint32 line);
void panic(const char *message, const char *file, uint32 line);
void panic_assert(const char *file, uint32 line, const char *desc);

void* memset(uint8 *dest, uint8 val, uint32 len);
void* memcpy(uint8 *dest, const uint8 *src, uint32 len);
void* memmove(void* dest, const void* src, uint32 n);
int memcmp(const void* p1, const void* p2, uint32 c);

int strcmp(const char *str1, const char *str2);
int strncmp(const char *str1, const char *str2, int length);
char *strcpy(char *dest, const char *src);
char *strcpyNonNull(char *dest, const char *src);
char *strncpy(char *dest, const char *src, uint32 num);
char* strcat(char *dest, const char *src);
int strlen(const char *src);
int strFirstIndexOf(const char *src, char c);
int sprintf(char* buffer, const char *format, ...);

void printkf(const char *format, ...);

int atoi(char *str);
void itoa(char *buf, int base, int d);

uint32 rand();

uint32 readEip();
uint32 readEsp();
uint32 getCpuFlags();
BOOL isInterruptsEnabled();

void beginCriticalSection();
void endCriticalSection();

#endif // COMMON_H

// vim:expandtab:ts=2