-
Notifications
You must be signed in to change notification settings - Fork 1
/
demos.py
216 lines (175 loc) · 5.17 KB
/
demos.py
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
## @file
## @brief @ref unikernel OS model
import config
from metaL import *
## @defgroup demos demos
## @ingroup os
## @brief @ref unikernel OS model
## @{
MODULE = anyModule('demos')
TITLE = Title('`unikernel` OS model in metaL/py')
MODULE << TITLE
ABOUT = String('''
It's an operating system model treated as a demo of writing a language-powered
OS in Python, which was mentioned in https://t.me/osdev channel a few weeks ago.
It is not something more than just a fun toy, not targets for any practical use
or Linux killer.
On the other side, I don't see a lot of projects on implementing hobby OS based
on some language interpreter, compiler embedded into the OS kernel, or
standalone interactive development system, as it was popular in the 80th.
So, in this demo, I'm going to mix a bytecode interpreter, a few bare-metal
drivers written in C and assembly, and the method of concept programming in
Python. Also, it should run in a *guest OS* mode as generic application over
mainstream OS such as Linux.
* hw: i386/QEMU
* powered by `metaL`
''')
MODULE['about'] = ABOUT
## `~/metaL/$MODULE` target directory for code generation
diroot = MODULE['dir']
## README
MODULE['github'] = 'metaL/blob/master/demos.py'
readme = README(MODULE)
diroot // readme
# readme // ('github: %s/%s/blob/master/%s.py' %
# (GITHUB.val, vm.val, MODULE.val))
readme.sync()
## file masks will be ignored by `git` version manager
gitignore = diroot['gitignore']
gitignore.sync()
gitignore // '\n*.dump\n*.kernel'
gitignore.sync()
## `Makefile` for target project build/run
mk = diroot['mk']
diroot // mk
mksection = Section(MODULE)
mk // mksection
mktools = Section('backend tools: TCC magic')
mksection // mktools
if config.TCC:
mktools // 'CFLAGS = -m32 -O0'
mktools // 'CC = tcc'
mktools // 'AS = tcc'
else:
mktools // 'CFLAGS = -m32 -O0 -fno-pic -mtune=i386'
mktools // 'CC = gcc $(OPT)'
mktools // 'AS = gcc $(OPT)'
mktools // 'LD = ld'
mktools // 'OBJDUMP = LANG=C objdump'
mksection // 'H += multiboot.h $(MODULE).h'
mksection // 'OBJ += multiboot.o $(MODULE).o'
mksection // '\n.PHONY: all\nall: $(MODULE).kernel'
mksection // '\tqemu-system-i386 -m 2M -kernel $<'
objdump = '$(OBJDUMP) -xdas $@ > $@.dump'
mksection // '\n$(MODULE).kernel: $(OBJ) multiboot.ld Makefile'
mksection // ('\t$(LD) -nostdlib -T multiboot.ld -o $@ $(OBJ) && ' + objdump + '\n')
mkrules = Section('macro rules')
mksection // mkrules
compiler = ('%%.o: %%.%s $(H) Makefile\n\t$(CC) $(CFLAGS) -o $@ -c $< && ' + objdump)
mkrules // (compiler % 's')
mkrules // (compiler % 'c')
mk.sync()
## Debian Linux packages install
apt = diroot['apt']
apt // 'binutils qemu-system-i386'
if config.TCC:
apt // 'tcc'
else:
apt // 'gcc-multilib'
apt.sync()
ldfile = File('multiboot.ld')
diroot // ldfile
ldfile // '''
OUTPUT_FORMAT("elf32-i386")
OUTPUT_ARCH(i386)
SECTIONS
{
.text : { *(.multiboot) *(.text*) }
.data : { *(.data) }
.bss : { *(.bss) }
/DISCARD/ : { *(.comment*) *(.note*) *(.eh_frame) *(.got) }
}
'''
ldfile.sync()
header = cFile('%s.h'%MODULE.val)
diroot // header
header // '''
#ifndef _H_DEMOS
#define _H_DEMOS
#ifdef __TINYC__
#include <stddef.h>
#else
#include <stdint.h>
#endif
#include "multiboot.h"
extern void init(void);
extern void main(void);
// extern uint8_t *VGA;// = (uint8_t*) 0xB8000;
struct _VGA {
uint8_t ch;
uint8_t attr;
} __attribute__((packed));
extern struct _VGA *VGA;
extern uint16_t cursor;
#endif // _H_DEMOS
'''
header.sync()
bootheader = '''
// Multiboot Specification version 0.6.96
// https://www.gnu.org/software/grub/manual/multiboot/multiboot.html
// https://github.com/dom96/nimkernel/blob/master/boot.s
'''
incheader = ('#include "%s.h"' % MODULE.val)
booth = cFile('multiboot.h')
diroot // booth
booth // bootheader
booth // incheader
booth // '''
#ifndef _H_MULTIBOOT
#define _H_MULTIBOOT
#define MULTIBOOT_PAGE_ALIGN (1<<0) // align to page boundaries
#define MULTIBOOT_MEMORY_INFO (1<<1) // provide memory map
#define MULTIBOOT_HEADER_MAGIC 0x1BADB002
#define MULTIBOOT_BOOTLOADER_MAGIC 0x2BADB002
#define MULTIBOOT_FLAGS (MULTIBOOT_PAGE_ALIGN)//|MULTIBOOT_MEMORY_INFO)
#define MULTIBOOT_CHECKSUM (-(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_FLAGS))
struct MULTIBOOT {
uint32_t magic;
uint32_t flags;
uint32_t checksum;
} __attribute__((packed));
#endif // _H_MULTIBOOT
'''
booth.sync()
bootc = cFile('multiboot.c')
diroot // bootc
bootc // bootheader
bootc // incheader
bootc // '''
struct MULTIBOOT multiboot __attribute__((section(".multiboot")))
= {MULTIBOOT_HEADER_MAGIC,MULTIBOOT_FLAGS,MULTIBOOT_CHECKSUM};
void init() { main(); for(;;); }
'''
bootc.sync()
kernel = cFile('%s.c'%MODULE.val)
diroot // kernel
kernel // incheader
kernel // '''
struct _VGA *VGA = (struct _VGA *) 0xB8000;
uint16_t cursor = 0;
void main(void) {
VGA[cursor ].ch = 'H';
VGA[cursor++].attr = 0x1F;
VGA[cursor ].ch = 'E';
VGA[cursor++].attr = 0x1F;
VGA[cursor ].ch = 'L';
VGA[cursor++].attr = 0x1F;
VGA[cursor ].ch = 'L';
VGA[cursor++].attr = 0x1F;
VGA[cursor ].ch = 'O';
VGA[cursor++].attr = 0x1F;
}
'''
kernel.sync()
print(vm)
## @}