1
- KnightOS Memory Layout
1
+ Kernel memory structures
2
2
3
3
Note that this describes the default settings, and that the actual layout of
4
4
memory is subject to change depending on how the kernel is configured. Userspace
5
- programs mucking around in memory manually is very strongly frowned upon.
5
+ programs mucking around in memory manually is _very strongly_ frowned upon. All
6
+ kernel data structures are subject to change.
6
7
7
- Overall Layout
8
+ Overall layout
8
9
9
10
Address Length Description
10
11
@@ -21,88 +22,85 @@ functions, including boot, filesystem access, context switching, etc.
21
22
Volatile Flash access is generally only used with interrupts disabled to prevent
22
23
other processes from ruining your day.
23
24
24
- RAM Layout
25
+ RAM layout
25
26
26
27
Right now, KnightOS only uses 0x8000 (32K) of RAM, even on devices that may have
27
28
additional RAM available. Nothing of value is kept in RAM, and it may be safely
28
29
wiped during a reboot (and in fact, the kernel does so).
29
30
30
- Note that RAM data structures are scheduled to change dramatically for kernel
31
- 0.7.0.
32
-
33
31
Address Length Description
34
32
35
- 0x8000 0x100 Thread table
36
- 0x8100 0x40 Library table
37
- 0x8140 0x40 Signal table
38
- 0x8180 0x40 File handle table
39
- 0x8200 0x100 Misc kernel state
40
- 0x8300 0x100 Kernel flash functions
41
- 0x8400 0x200 Volatile kernel garbage
42
- 0x8600 0x7A00 Userspace memory
33
+ 0x8000 0x80 Miscellaneous kernel state
34
+ 0x8080 0x80 Flash execution space
35
+ 0x8100 0x100 Volatile kernel utility space (kernelGarbage)
36
+ 0x8200 0x7D00 Heap
37
+
38
+ Misc kernel state holds several statically allocated kernel variables. Flash
39
+ execution space is reserved for the Flash driver to use to execute Flash
40
+ operations, which must be run from RAM. Volatile kernel utility space is a fixed
41
+ buffer used for various kernel operations where dynamic memory allocation cannot
42
+ fail. Finally, the heap is where `malloc` allocates memory from.
43
43
44
- Userspace memory is where `malloc` allocates memory. Volatile kernel garbage is
45
- used throughout the kernel whenever it just needs a little space to work, and is
46
- accessed with interrupts disabled. Kernel flash functions are where the Flash
47
- driver loads executable code to perform Flash manipulation from RAM. Misc kernel
48
- state is used for various things like the currently executing thread.
44
+ The process table
49
45
50
- Data Structures
46
+ The kernel supports preemptive multitasking, and to this end it keeps track of
47
+ running processes and their resources. The process table is where this state is
48
+ stored, and it is allocated in the heap and grows or shrinks and demands change.
49
+ There are some variables in misc kernel state that keep track of the process
50
+ table:
51
51
52
- Each of the tables is a fixed-length array that holds several of the following
53
- data structures.
52
+ process_table Pointer to process table in the heap
53
+ active_processes Number of active processes
54
+ current_process Index of the currently running process
55
+ last_pid The last PID allocated by the kernel
54
56
55
- Threads
57
+ At boot, the kernel allocates a process table large enough to hold 8 processes.
58
+ As more processes are started, the process table may be `realloc`d to hold
59
+ more or less process state (though never less than 8). Each process state
60
+ structure is designed like so:
56
61
57
62
Address Length Description
58
63
59
- 0x0000 0x01 Thread ID
64
+ 0x0000 0x01 Process ID (pid)
60
65
0x0001 0x02 Executable address
61
- 0x0003 0x02 Stack pointer
66
+ 0x0003 0x02 Stack address (SP)
62
67
0x0005 0x01 Flags (bitfield)
63
- 1: May be suspended
64
- 2: Is suspended
65
- 3: Color enabled
66
- 0x0006 0x02 Reserved for future use
67
-
68
- Libraries
69
-
70
- Address Length Description
71
-
72
- 0x0000 0x01 Library ID
73
- 0x0001 0x02 Library address
74
- 0x0003 0x01 Dependent threads
75
-
76
- Signals
77
-
78
- Address Length Description
79
-
80
- 0x0000 0x01 Target thread ID
81
- 0x0001 0x01 Message type
82
- 0x0002 0x02 Payload
83
-
84
- File Handles
68
+ 0: May be suspended
69
+ 1: Is suspended
70
+ 2: Color enabled (84+CSE only)
71
+ 0x0006 0x02 Pointer to file handle table
72
+ 0x0007 0x01 Number of file handles
73
+ 0x0009 0x02 Pointer to library handle table
74
+ 0x000B 0x01 Number of libraries
75
+ 0x000C 0x02 Pointer to signal table
76
+ 0x000E 0x01 Number of signal handlers
77
+
78
+ Each process has its own stack, file descriptor table, library handle table, and
79
+ signal table. These are also allocated in the heap and expand or shrink to meet
80
+ the process's needs. By default, each process is allocated a file descriptor
81
+ table 4 entries long, a library handle table 2 entries long, and no signal
82
+ table (both the pointer and length will be zero).
83
+
84
+ File handle
85
85
86
86
Address Length Description
87
87
88
- 0x0000 0x01 Flags/Owner
88
+ 0x0000 0x01 Flags (bitfield)
89
+ 0: Writable if 1
90
+ 1: EoF if 1
91
+ 2: Flushed if 1
92
+ 3: Set if final block of the file*
93
+ 4-7: Reserved for future use
89
94
0x0001 0x02 Buffer address
90
95
0x0003 0x01 Stream pointer
91
96
0x0004 0x02 Section ID
92
97
0x0006 0x01 Length of final block
93
98
0x0007 0x01 File entry page
94
99
0x0008 0x02 File entry address
95
100
0x000A 0x03 File working size
96
- 0x000D 0x01 Writable stream flags
97
- 0x000E 0x02 Reserved for future use
98
-
99
- Flags/Owner is the thread ID of the file handle's owner. It takes the format of
100
- FTExxxxx, where xxxxx is the thread ID. F is set to indicate that the stream is
101
- pointing at the final block of the file. T is set if the thread is writable. E
102
- is set if the file handle is set to EOF.
101
+ 0x000D 0x03 Reserved for future use
103
102
104
- Writable stream flags are set to xxxxxxF, where F is set if the stream has been
105
- flushed, and x is reserved for future use.
103
+ All kernel data structures are subject to change, but this one especially so.
106
104
107
105
The buffer address refers to the address of the 256-byte stream buffer, which
108
106
contains the contents of the current DAT block the stream points to. The stream
@@ -114,3 +112,18 @@ in the KFS docs.
114
112
115
113
The length of the final block is used to determine when EOF has been reached on
116
114
the final block.
115
+
116
+ Library handle
117
+
118
+ Address Length Description
119
+
120
+ 0x0000 0x02 Library ID
121
+ 0x0002 0x02 Library address
122
+
123
+ Signal handler
124
+
125
+ Address Length Description
126
+
127
+ 0x0000 0x02 Signal handler
128
+ 0x0002 0x01 Signal number
129
+ 0x0003 0x01 Reserved for future use
0 commit comments