-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmeson.build
More file actions
400 lines (361 loc) · 12 KB
/
meson.build
File metadata and controls
400 lines (361 loc) · 12 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
project('kernel', 'c', 'cpp', version: '0.0.1', default_options: [
'c_std=c11',
'cpp_std=c++20',
'warning_level=2',
'cpp_rtti=false',
'cpp_eh=none',
])
build_resources = meson.source_root()/'build-resources'
# required program for generating the kernel
gdb = find_program('gdb')
objcopy = find_program('objcopy')
qemu = find_program('qemu-system-x86_64')
gen_dbg = executable('gen-dbg',
build_resources/'gen_dbg.cpp',
cpp_args: [
],
dependencies: dependency('fmt'),
override_options: [
'cpp_eh=default'
]
)
strip_debug = find_program(build_resources/'strip_debug.sh')
generate_image = find_program(build_resources/'generate_image.sh')
git_version_info = find_program(build_resources/'git_version_info.sh')
mcopy = find_program('mcopy')
parted = find_program('parted')
kernel_src = [
'kernel/src/arch/x86/idt/idt.cpp',
'kernel/src/arch/x86/idt/handlers/handle_ud.cpp',
'kernel/src/arch/x86/idt/handlers/handle_np.cpp',
'kernel/src/arch/x86/idt/handlers/handle_page_fault.cpp',
'kernel/src/arch/x86/idt/handlers/handle_gp.cpp',
'kernel/src/arch/x86/idt/handlers/handle_overflow.cpp',
'kernel/src/arch/x86/idt/handlers/handle_db.cpp',
'kernel/src/arch/x86/idt/handlers/handle_div_by_zero.cpp',
'kernel/src/arch/x86/idt/handlers/handle_stack.cpp',
'kernel/src/arch/x86/idt/handlers/handle_double_fault.cpp',
'kernel/src/arch/x86/idt/handlers/handle_bad_tss.cpp',
'kernel/src/arch/x86/idt/handlers/handle_bounds.cpp',
'kernel/src/arch/x86/idt/handlers/handle_breakpoint.cpp',
'kernel/src/arch/x86/idt/handlers/handle_timer.cpp',
'kernel/src/arch/x86/idt/handlers/handle_nm.cpp',
'kernel/src/arch/x86/idt/idt.S',
'kernel/src/arch/x86/debug/kinit_dump.cpp',
'kernel/src/arch/x86/debug/panic.cpp',
'kernel/src/arch/x86/debug/symbols.cpp',
'kernel/src/arch/x86/asm/return_to_context.cpp',
'kernel/src/arch/x86/asm/return_to_context.S',
'kernel/src/arch/x86/pci/pci_scan.cpp',
'kernel/src/arch/x86/sync/spinlock.cpp',
'kernel/src/arch/x86/apic/apic.cpp',
'kernel/src/arch/x86/kinit/kinit.cpp',
'kernel/src/arch/x86/process/process.cpp',
'kernel/src/arch/x86/process/save_ctx_for_reschedule.S',
'kernel/src/arch/x86/process/scheduler/scheduler.cpp',
'kernel/src/arch/x86/klog/klog.cpp',
'kernel/src/arch/x86/tty/tty.cpp',
'kernel/src/arch/x86/mm/pmm.cpp',
'kernel/src/arch/x86/mm/malloc.cpp',
'kernel/src/arch/x86/mm/init.cpp',
# 'kernel/src/arch/x86/mm/slab.cpp',
'kernel/src/arch/x86/mm/paging/paging.cpp',
'kernel/src/arch/x86/mm/vmm.cpp',
'kernel/src/arch/x86/cpuid/cpuid.cpp',
'kernel/src/arch/x86/gdt/gdt.cpp',
# 'kernel/src/arch/x86/acpi/lai.cpp',
'kernel/src/arch/x86/user/fd/console.cpp',
'kernel/src/arch/x86/user/syscall/syscall_entry.S',
'kernel/src/arch/x86/user/elf_load.cpp',
'kernel/src/arch/x86/smp/smp.cpp',
'kernel/src/arch/x86/smp/tls.cpp',
'kernel/external/terminal/limine_term_ccompat.cpp',
]
lib_src = [
'lib/src/cstring.cpp',
'lib/src/new.cpp',
'lib/src/backtrace.cpp',
'lib/src/printf.cpp',
'libimpl/cxxabi.cpp',
'libimpl/implement.cpp',
]
common_src = [
'kernel/external/terminal/backends/framebuffer.c',
'kernel/external/terminal/term.c',
'kernel/external/terminal/limine_term_ccompat.cpp',
]
opt_to_config = {
'PRE_ALLOCATE_PAGES': 'preallocate_pages',
'KLOG_PAGES': 'klog_size_pages',
'CPUID_FEATURE_SIZE': 'cpuid_feature_buffer_size',
'DEBUG_LOG_MMAP': 'debug_log_mmap',
'DEBUG_LOG_CPUID': 'debug_log_cpuid',
'DEBUG_LOG_ACPI': 'debug_log_acpi',
'DEBUG_LOG_PCI': 'debug_log_pci',
'DEBUG_SPINLOCK_DEP': 'debug_spinlock_dep',
'SLAB_MIN_ORDER': 'slab_min_order',
'SLAB_MAX_ORDER': 'slab_max_order',
'SLAB_SIZE_ORDER': 'slab_size_order',
'VMM_SIZE': 'vmm_size',
'HAS_ASAN': 'kernel_asan',
'HAS_UBSAN': 'kernel_ubsan'
}
qemu_base_flags = [
'-bios', build_resources/'OVMF.fd', # Make sure this path points to **your** ovmf
'-hda', meson.build_root()/'kernel.img', # Kernel image to run
'-no-reboot', # Prevent rebooting on exception
'-monitor', 'stdio', # Monitor output to stdio
'-d', 'int,cpu_reset', # Generate interrupt logging
'-D', 'qemu.log', # QEMU logging output
'-no-shutdown',
'-s', # Enable debugging port (same as -gdb tcp::1234)
'-S', # Freeze CPU on start
'-M', 'smm=off' # No SMM interrupts
]
execution_targets = {
'emu': {
'exec': {
'smp': ['-smp', '2', '-m', '8G'],
'very-smp': ['-smp', '8', '-m', '8G'],
'nosmp': ['-smp', '1', '-m', '8G'],
},
'debugger': {
'gef': [
'gef-remote --qemu-user --qemu-binary kernel_stripped.elf localhost 1234',
'b _start',
'c',
],
'@default': [
'target remote localhost:1234',
'b _start',
'c',
]
}
},
'kvm': {
'exec': {
'smp': ['-enable-kvm', '-cpu', 'host', '-smp', '2', '-m', '8G'],
'very-smp': ['-enable-kvm', '-cpu', 'host', '-smp', '8', '-m', '8G'],
'nosmp': ['-enable-kvm', '-cpu', 'host', '-smp', '1', '-m', '8G']
},
'debugger': {
'gef': [
'gef-remote --qemu-user --qemu-binary kernel_stripped.elf localhost 1234',
'hbreak _start',
'c',
],
'@default': [
'target remote localhost:1234',
'hbreak _start',
'c',
]
}
},
}
kernel_derivatives = {
'kernel_stripped': {
'name': 'kernel_stripped.elf',
'args': [ strip_debug ]
},
'kernel_dbg': {
'name': 'kernel_dbg.elf',
'args': [ objcopy, '--only-keep-debug' ]
},
'kernel_syms': {
'name': 'kernel.syms',
'args': [ gen_dbg ]
}
}
kernel_build_args = [
'-ffreestanding',
'-fno-PIC',
'-fno-exceptions',
'-fno-omit-frame-pointer',
'-fno-strict-aliasing',
'-fcf-protection=none',
'-femulated-tls',
'-fno-stack-protector',
'-fno-threadsafe-statics',
'-mno-red-zone',
'-nostdinc',
'-Wno-ignored-attributes',
'-mno-sse',
'-mno-sse2',
'-mno-mmx',
'-mno-80387',
'-Wno-unused-parameter',
]
ubsan = get_option('kernel_ubsan')
asan = get_option('kernel_asan')
ubsan_extra = ['-fno-sanitize=function', '-fsanitize-undefined-strip-path-components=1']
asan_extra = []
ubsan_src = 'kernel/src/arch/x86/san/ubsan.cpp'
asan_src = 'kernel/src/arch/x86/san/asan.cpp'
if ubsan and asan
kernel_build_args += ['-fsanitize=kernel-address,undefined'] + ubsan_extra + asan_extra
kernel_src += [ubsan_src, asan_src]
elif ubsan
kernel_build_args += ['-fsanitize=undefined'] + ubsan_extra
kernel_src += [ubsan_src]
elif asan
kernel_build_args += ['-fsanitize=kernel-address'] + asan_extra
kernel_src += [asan_src]
endif
# options configuration
if get_option('slab_min_order') >= get_option('slab_max_order')
error('config: slab_min_order cannot >= slab_max_order')
endif
if get_option('slab_max_order') >= get_option('slab_size_order')
error('config: slab_max_order cannot >= slab_size_order')
endif
if get_option('preallocate_pages') < 0x500
warning('config: pre-allocating more than 0x500 pages is recommended')
endif
# config
conf = configuration_data()
conf.set('VERSION', meson.project_version())
conf.set('ARCH_NAME', 'x86_64')
conf.set('ARCH_PATH', 'x86')
conf.set('ARCH_PAGE_SIZE', '4096')
conf.set('COMPILER_C_ID', meson.get_compiler('c').get_id())
conf.set('COMPILER_C_VER', meson.get_compiler('c').version())
conf.set('COMPILER_CXX_ID', meson.get_compiler('cpp').get_id())
conf.set('COMPILER_CXX_VER', meson.get_compiler('cpp').version())
foreach key, opt : opt_to_config
conf.set(key, get_option(opt).to_string())
endforeach
conf.set('HHDM_START', '0xffff800000000000')
conf.set('PFN_START', '0xffff900000000000')
conf.set('SCROLLBACK_START', '0xffff910000000000')
conf.set('HEAP_START', '0xffff920000000000')
conf.set('SLAB_START', '0xffff930000000000')
conf.set('KLOG_START', '0xffff940000000000')
conf.set('VMM_START', '0xffffa00000000000')
conf.set('HEAP_SIZE', '0x10000000000')
conf.set('KLOG_SHIFT', '17')
comp_id = meson.get_compiler('cpp').get_id()
if comp_id == 'gcc'
conf.set('COMPILER_GCC', true)
conf.set('COMPILER_CLANG', false)
elif comp_id == 'clang'
conf.set('COMPILER_GCC', false)
conf.set('COMPILER_CLANG', true)
else
error('unknown cc', comp_id)
endif
configure_file(input : 'config.h.in',
output : 'config.h.in',
configuration : conf)
configuration_inc = include_directories(
'.',
'kernel/include/arch/x86',
'kernel/include',
'kernel/external',
'kernel/external/lai',
'lib',
'lib/include',
is_system: true
)
config_output = custom_target('config_output',
input: 'config.h.in',
output: 'config.h',
command: [
git_version_info,
meson.build_root()/'config.h.in',
meson.build_root()/'config.h'
],
)
lds = meson.source_root()/'kernel/src/arch/x86/kernel.lds'
kernel = executable('kernel.elf',
config_output,
[kernel_src, lib_src, common_src],
link_depends: lds,
link_args: [
'-T' + lds,
'-static',
'-nostdlib',
'-m64',
'-femulated-tls'
],
cpp_args: [
'-m64',
'-march=x86-64',
'-mcmodel=kernel',
kernel_build_args
],
c_args: [
'-m64',
'-march=x86-64',
'-mcmodel=kernel',
kernel_build_args
],
include_directories : configuration_inc
)
foreach name, args : kernel_derivatives
set_variable(name.replace('-', '_'), custom_target(name,
output: args.get('name'),
input: kernel,
command: [
args.get('args'),
'@INPUT@',
'@OUTPUT@'
]
))
endforeach
kernel_img = custom_target('kernel-img',
output: 'kernel.img',
input: [ kernel_stripped, kernel_syms ],
command: [
generate_image,
'@OUTPUT@',
build_resources/'kernel-template.img',
'@INPUT0@',
'@INPUT1@'
]
)
qemu_targets = {}
foreach type, value : execution_targets
# extract execution modes
message('registering targets for: ', type)
foreach name, flags : value.get('exec')
real_name = 'run-' + type + '-' + name
message('registering target: ', real_name)
message('flags: ', qemu_base_flags, flags)
qemu_targets += {
real_name: run_target(real_name,
depends: [kernel_img],
command: [
qemu,
qemu_base_flags,
flags
]
)
}
endforeach
message('registering debugging for: ', type)
foreach debug_type, flags : value.get('debugger')
debugger_name = ''
if debug_type != '@default'
message('registering debugging type ', debug_type,' for: ', type)
debugger_name = 'attach-' + type + '-' + debug_type
else
message('registering default dbeugging type for: ', type)
debugger_name = 'attach-' + type
endif
gdb_flags = [ ]
foreach flag : flags
gdb_flags += [ '-ex', flag ]
endforeach
message('flags: ', gdb_flags)
qemu_targets += {
debugger_name: run_target(debugger_name,
depends: [kernel_dbg],
command: [
gdb,
kernel_dbg,
gdb_flags
]
)
}
endforeach
endforeach