summaryrefslogtreecommitdiff
path: root/Kernel/CMakeLists.txt
blob: 16a5636161ca1297ad85f2c322881fd46b096e1e (plain)
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
if (ENABLE_EXTRA_KERNEL_DEBUG_SYMBOLS)
    add_compile_options(-Og)
    add_compile_options(-ggdb3)
else()
    add_compile_options(-O2)
endif()

if ("${SERENITY_ARCH}" STREQUAL "aarch64")
    set(KERNEL_ARCH aarch64)
elseif("${SERENITY_ARCH}" STREQUAL "x86_64")
    set(KERNEL_ARCH x86_64)
endif()

set(KERNEL_HEAP_SOURCES
    Heap/kmalloc.cpp
)

set(KERNEL_SOURCES
    AddressSanitizer.cpp
    Arch/PageFault.cpp
    Bus/PCI/Controller/HostController.cpp
    Bus/PCI/Controller/MemoryBackedHostBridge.cpp
    Bus/PCI/Controller/VolumeManagementDevice.cpp
    Bus/PCI/Access.cpp
    Bus/PCI/API.cpp
    Bus/PCI/Device.cpp
    Bus/PCI/DeviceIdentifier.cpp
    Bus/USB/UHCI/UHCIController.cpp
    Bus/USB/UHCI/UHCIRootHub.cpp
    Bus/USB/USBConfiguration.cpp
    Bus/USB/USBController.cpp
    Bus/USB/USBDevice.cpp
    Bus/USB/USBHub.cpp
    Bus/USB/USBManagement.cpp
    Bus/USB/USBPipe.cpp
    Bus/USB/USBTransfer.cpp
    Bus/VirtIO/Console.cpp
    Bus/VirtIO/ConsolePort.cpp
    Bus/VirtIO/Device.cpp
    Bus/VirtIO/Queue.cpp
    Bus/VirtIO/RNG.cpp
    CommandLine.cpp
    Coredump.cpp
    CrashHandler.cpp
    Credentials.cpp
    Devices/AsyncDeviceRequest.cpp
    Devices/Audio/AC97.cpp
    Devices/Audio/Channel.cpp
    Devices/Audio/IntelHDA/Codec.cpp
    Devices/Audio/IntelHDA/Controller.cpp
    Devices/Audio/IntelHDA/Format.cpp
    Devices/Audio/IntelHDA/Stream.cpp
    Devices/Audio/Management.cpp
    Devices/BlockDevice.cpp
    Devices/CharacterDevice.cpp
    Devices/ConsoleDevice.cpp
    Devices/Device.cpp
    Devices/DeviceControlDevice.cpp
    Devices/DeviceManagement.cpp
    Devices/FullDevice.cpp
    Devices/KCOVDevice.cpp
    Devices/KCOVInstance.cpp
    Devices/MemoryDevice.cpp
    Devices/NullDevice.cpp
    Devices/PCISerialDevice.cpp
    Devices/RandomDevice.cpp
    Devices/SelfTTYDevice.cpp
    Devices/SerialDevice.cpp
    Devices/ZeroDevice.cpp
    Devices/HID/HIDManagement.cpp
    Devices/HID/KeyboardDevice.cpp
    Devices/HID/MouseDevice.cpp
    Graphics/Bochs/GraphicsAdapter.cpp
    Graphics/Bochs/QEMUDisplayConnector.cpp
    Graphics/Console/BootFramebufferConsole.cpp
    Graphics/Console/GenericFramebufferConsole.cpp
    Graphics/Console/ContiguousFramebufferConsole.cpp
    Graphics/Console/VGATextModeConsole.cpp
    Graphics/DisplayConnector.cpp
    Graphics/Generic/DisplayConnector.cpp
    Graphics/GraphicsManagement.cpp
    Graphics/Intel/Auxiliary/GMBusConnector.cpp
    Graphics/Intel/Plane/DisplayPlane.cpp
    Graphics/Intel/Plane/G33DisplayPlane.cpp
    Graphics/Intel/Transcoder/AnalogDisplayTranscoder.cpp
    Graphics/Intel/Transcoder/DisplayTranscoder.cpp
    Graphics/Intel/Transcoder/PLL.cpp
    Graphics/Intel/DisplayConnectorGroup.cpp
    Graphics/Intel/NativeDisplayConnector.cpp
    Graphics/Intel/NativeGraphicsAdapter.cpp
    Graphics/VMWare/Console.cpp
    Graphics/VMWare/GraphicsAdapter.cpp
    Graphics/VMWare/DisplayConnector.cpp
    Graphics/VirtIOGPU/DisplayConnector.cpp
    Graphics/VirtIOGPU/Console.cpp
    Graphics/VirtIOGPU/GPU3DDevice.cpp
    Graphics/VirtIOGPU/GraphicsAdapter.cpp
    IOWindow.cpp
    Jail.cpp
    SanCov.cpp
    Storage/ATA/AHCI/Controller.cpp
    Storage/ATA/AHCI/Port.cpp
    Storage/ATA/AHCI/InterruptHandler.cpp
    Storage/ATA/GenericIDE/Controller.cpp
    Storage/ATA/GenericIDE/Channel.cpp
    Storage/ATA/ATAController.cpp
    Storage/ATA/ATADevice.cpp
    Storage/ATA/ATADiskDevice.cpp
    Storage/ATA/ATAPort.cpp
    Storage/NVMe/NVMeController.cpp
    Storage/NVMe/NVMeNameSpace.cpp
    Storage/NVMe/NVMeInterruptQueue.cpp
    Storage/NVMe/NVMePollQueue.cpp
    Storage/NVMe/NVMeQueue.cpp
    Storage/SD/SDHostController.cpp
    Storage/SD/SDMemoryCard.cpp
    Storage/DiskPartition.cpp
    Storage/StorageController.cpp
    Storage/StorageDevice.cpp
    Storage/StorageManagement.cpp
    DoubleBuffer.cpp
    FileSystem/AnonymousFile.cpp
    FileSystem/BlockBasedFileSystem.cpp
    FileSystem/Custody.cpp
    FileSystem/DevPtsFS/FileSystem.cpp
    FileSystem/DevPtsFS/Inode.cpp
    FileSystem/Ext2FS/FileSystem.cpp
    FileSystem/Ext2FS/Inode.cpp
    FileSystem/FATFS/FileSystem.cpp
    FileSystem/FATFS/Inode.cpp
    FileSystem/FIFO.cpp
    FileSystem/File.cpp
    FileSystem/FileBackedFileSystem.cpp
    FileSystem/FileSystem.cpp
    FileSystem/Inode.cpp
    FileSystem/InodeFile.cpp
    FileSystem/InodeMetadata.cpp
    FileSystem/InodeWatcher.cpp
    FileSystem/ISO9660FS/DirectoryIterator.cpp
    FileSystem/ISO9660FS/FileSystem.cpp
    FileSystem/ISO9660FS/Inode.cpp
    FileSystem/Mount.cpp
    FileSystem/OpenFileDescription.cpp
    FileSystem/Plan9FS/FileSystem.cpp
    FileSystem/Plan9FS/Inode.cpp
    FileSystem/Plan9FS/Message.cpp
    FileSystem/ProcFS/FileSystem.cpp
    FileSystem/ProcFS/Inode.cpp
    FileSystem/ProcFS/ProcessExposed.cpp
    FileSystem/RAMFS/FileSystem.cpp
    FileSystem/RAMFS/Inode.cpp
    FileSystem/SysFS/Component.cpp
    FileSystem/SysFS/DirectoryInode.cpp
    FileSystem/SysFS/FileSystem.cpp
    FileSystem/SysFS/Inode.cpp
    FileSystem/SysFS/LinkInode.cpp
    FileSystem/SysFS/Registry.cpp
    FileSystem/SysFS/RootDirectory.cpp
    FileSystem/SysFS/Subsystems/Bus/PCI/BusDirectory.cpp
    FileSystem/SysFS/Subsystems/Bus/PCI/DeviceAttribute.cpp
    FileSystem/SysFS/Subsystems/Bus/PCI/DeviceDirectory.cpp
    FileSystem/SysFS/Subsystems/Bus/PCI/DeviceExpansionROM.cpp
    FileSystem/SysFS/Subsystems/Bus/USB/BusDirectory.cpp
    FileSystem/SysFS/Subsystems/Bus/USB/DeviceInformation.cpp
    FileSystem/SysFS/Subsystems/Bus/Directory.cpp
    FileSystem/SysFS/Subsystems/DeviceIdentifiers/BlockDevicesDirectory.cpp
    FileSystem/SysFS/Subsystems/DeviceIdentifiers/CharacterDevicesDirectory.cpp
    FileSystem/SysFS/Subsystems/DeviceIdentifiers/DeviceComponent.cpp
    FileSystem/SysFS/Subsystems/DeviceIdentifiers/Directory.cpp
    FileSystem/SysFS/Subsystems/DeviceIdentifiers/SymbolicLinkDeviceComponent.cpp
    FileSystem/SysFS/Subsystems/Devices/Storage/DeviceAttribute.cpp
    FileSystem/SysFS/Subsystems/Devices/Storage/DeviceDirectory.cpp
    FileSystem/SysFS/Subsystems/Devices/Storage/Directory.cpp
    FileSystem/SysFS/Subsystems/Devices/Graphics/Directory.cpp
    FileSystem/SysFS/Subsystems/Devices/Graphics/DisplayConnector/Directory.cpp
    FileSystem/SysFS/Subsystems/Devices/Graphics/DisplayConnector/DeviceDirectory.cpp
    FileSystem/SysFS/Subsystems/Devices/Graphics/DisplayConnector/DeviceAttribute.cpp
    FileSystem/SysFS/Subsystems/Devices/Directory.cpp
    FileSystem/SysFS/Subsystems/Firmware/BIOS/Component.cpp
    FileSystem/SysFS/Subsystems/Firmware/BIOS/Directory.cpp
    FileSystem/SysFS/Subsystems/Firmware/Directory.cpp
    FileSystem/SysFS/Subsystems/Kernel/Interrupts.cpp
    FileSystem/SysFS/Subsystems/Kernel/Processes.cpp
    FileSystem/SysFS/Subsystems/Kernel/CPUInfo.cpp
    FileSystem/SysFS/Subsystems/Kernel/Jails.cpp
    FileSystem/SysFS/Subsystems/Kernel/Keymap.cpp
    FileSystem/SysFS/Subsystems/Kernel/Profile.cpp
    FileSystem/SysFS/Subsystems/Kernel/Directory.cpp
    FileSystem/SysFS/Subsystems/Kernel/DiskUsage.cpp
    FileSystem/SysFS/Subsystems/Kernel/Log.cpp
    FileSystem/SysFS/Subsystems/Kernel/SystemStatistics.cpp
    FileSystem/SysFS/Subsystems/Kernel/GlobalInformation.cpp
    FileSystem/SysFS/Subsystems/Kernel/MemoryStatus.cpp
    FileSystem/SysFS/Subsystems/Kernel/PowerStateSwitch.cpp
    FileSystem/SysFS/Subsystems/Kernel/Uptime.cpp
    FileSystem/SysFS/Subsystems/Kernel/Network/Adapters.cpp
    FileSystem/SysFS/Subsystems/Kernel/Network/ARP.cpp
    FileSystem/SysFS/Subsystems/Kernel/Network/Directory.cpp
    FileSystem/SysFS/Subsystems/Kernel/Network/Local.cpp
    FileSystem/SysFS/Subsystems/Kernel/Network/Route.cpp
    FileSystem/SysFS/Subsystems/Kernel/Network/TCP.cpp
    FileSystem/SysFS/Subsystems/Kernel/Network/UDP.cpp
    FileSystem/SysFS/Subsystems/Kernel/Constants/ConstantInformation.cpp
    FileSystem/SysFS/Subsystems/Kernel/Constants/Directory.cpp
    FileSystem/SysFS/Subsystems/Kernel/Variables/BooleanVariable.cpp
    FileSystem/SysFS/Subsystems/Kernel/Variables/CapsLockRemap.cpp
    FileSystem/SysFS/Subsystems/Kernel/Variables/CoredumpDirectory.cpp
    FileSystem/SysFS/Subsystems/Kernel/Variables/Directory.cpp
    FileSystem/SysFS/Subsystems/Kernel/Variables/DumpKmallocStack.cpp
    FileSystem/SysFS/Subsystems/Kernel/Variables/StringVariable.cpp
    FileSystem/SysFS/Subsystems/Kernel/Variables/UBSANDeadly.cpp
    FileSystem/VirtualFileSystem.cpp
    Firmware/BIOS.cpp
    Firmware/ACPI/Initialize.cpp
    Firmware/ACPI/Parser.cpp
    Firmware/MultiProcessor/Parser.cpp
    FutexQueue.cpp
    Interrupts/GenericInterruptHandler.cpp
    Interrupts/IRQHandler.cpp
    Interrupts/SharedIRQHandler.cpp
    Interrupts/UnhandledInterruptHandler.cpp
    KBufferBuilder.cpp
    KLexicalPath.cpp
    KString.cpp
    KSyms.cpp
    Memory/AddressSpace.cpp
    Memory/AnonymousVMObject.cpp
    Memory/InodeVMObject.cpp
    Memory/MemoryManager.cpp
    Memory/PhysicalPage.cpp
    Memory/PhysicalRegion.cpp
    Memory/PhysicalZone.cpp
    Memory/PrivateInodeVMObject.cpp
    Memory/Region.cpp
    Memory/RegionTree.cpp
    Memory/RingBuffer.cpp
    Memory/ScatterGatherList.cpp
    Memory/ScopedAddressSpaceSwitcher.cpp
    Memory/SharedFramebufferVMObject.cpp
    Memory/SharedInodeVMObject.cpp
    Memory/VMObject.cpp
    Memory/VirtualRange.cpp
    MiniStdLib.cpp
    Locking/LockRank.cpp
    Locking/Mutex.cpp
    Net/Intel/E1000ENetworkAdapter.cpp
    Net/Intel/E1000NetworkAdapter.cpp
    Net/Realtek/RTL8168NetworkAdapter.cpp
    Net/IPv4Socket.cpp
    Net/LocalSocket.cpp
    Net/LoopbackAdapter.cpp
    Net/NetworkAdapter.cpp
    Net/NetworkTask.cpp
    Net/NetworkingManagement.cpp
    Net/Routing.cpp
    Net/Socket.cpp
    Net/TCPSocket.cpp
    Net/UDPSocket.cpp
    PerformanceEventBuffer.cpp
    Process.cpp
    ProcessGroup.cpp
    ProcessList.cpp
    Random.cpp
    Scheduler.cpp
    ScopedCritical.cpp
    StdLib.cpp
    Syscall.cpp
    Syscalls/anon_create.cpp
    Syscalls/alarm.cpp
    Syscalls/beep.cpp
    Syscalls/chdir.cpp
    Syscalls/chmod.cpp
    Syscalls/chown.cpp
    Syscalls/clock.cpp
    Syscalls/debug.cpp
    Syscalls/disown.cpp
    Syscalls/dup2.cpp
    Syscalls/emuctl.cpp
    Syscalls/execve.cpp
    Syscalls/exit.cpp
    Syscalls/faccessat.cpp
    Syscalls/fallocate.cpp
    Syscalls/fcntl.cpp
    Syscalls/fork.cpp
    Syscalls/fsync.cpp
    Syscalls/ftruncate.cpp
    Syscalls/futex.cpp
    Syscalls/get_dir_entries.cpp
    Syscalls/get_stack_bounds.cpp
    Syscalls/getrandom.cpp
    Syscalls/getuid.cpp
    Syscalls/hostname.cpp
    Syscalls/ioctl.cpp
    Syscalls/jail.cpp
    Syscalls/keymap.cpp
    Syscalls/kill.cpp
    Syscalls/link.cpp
    Syscalls/lseek.cpp
    Syscalls/mkdir.cpp
    Syscalls/mknod.cpp
    Syscalls/mmap.cpp
    Syscalls/mount.cpp
    Syscalls/open.cpp
    Syscalls/perf_event.cpp
    Syscalls/pipe.cpp
    Syscalls/pledge.cpp
    Syscalls/poll.cpp
    Syscalls/prctl.cpp
    Syscalls/process.cpp
    Syscalls/profiling.cpp
    Syscalls/ptrace.cpp
    Syscalls/purge.cpp
    Syscalls/read.cpp
    Syscalls/readlink.cpp
    Syscalls/realpath.cpp
    Syscalls/rename.cpp
    Syscalls/resource.cpp
    Syscalls/rmdir.cpp
    Syscalls/sched.cpp
    Syscalls/sendfd.cpp
    Syscalls/setpgid.cpp
    Syscalls/setuid.cpp
    Syscalls/sigaction.cpp
    Syscalls/socket.cpp
    Syscalls/stat.cpp
    Syscalls/statvfs.cpp
    Syscalls/sync.cpp
    Syscalls/sysconf.cpp
    Syscalls/thread.cpp
    Syscalls/times.cpp
    Syscalls/umask.cpp
    Syscalls/uname.cpp
    Syscalls/unlink.cpp
    Syscalls/unveil.cpp
    Syscalls/utime.cpp
    Syscalls/utimensat.cpp
    Syscalls/waitid.cpp
    Syscalls/inode_watcher.cpp
    Syscalls/write.cpp
    TTY/ConsoleManagement.cpp
    TTY/MasterPTY.cpp
    TTY/PTYMultiplexer.cpp
    TTY/SlavePTY.cpp
    TTY/TTY.cpp
    TTY/VirtualConsole.cpp
    Tasks/FinalizerTask.cpp
    Tasks/SyncTask.cpp
    Thread.cpp
    ThreadBlockers.cpp
    ThreadTracer.cpp
    Time/TimeManagement.cpp
    TimerQueue.cpp
    UBSanitizer.cpp
    UserOrKernelBuffer.cpp
    WaitQueue.cpp
    WorkQueue.cpp
)

if ("${SERENITY_ARCH}" STREQUAL "x86_64")
    set(KERNEL_SOURCES
        ${KERNEL_SOURCES}
        Arch/x86_64/init.cpp

        Arch/Processor.cpp

        Arch/x86_64/CMOS.cpp
        Arch/x86_64/DebugOutput.cpp
        Arch/x86_64/Delay.cpp

        Arch/x86_64/Hypervisor/BochsDisplayConnector.cpp
        Arch/x86_64/Hypervisor/VMWareBackdoor.cpp

        Arch/x86_64/CurrentTime.cpp

        Arch/x86_64/I8042Reboot.cpp
        Arch/x86_64/Interrupts/APIC.cpp
        Arch/x86_64/Interrupts/IOAPIC.cpp
        Arch/x86_64/Interrupts/PIC.cpp
        Arch/x86_64/Time/APICTimer.cpp
        Arch/x86_64/Time/HPET.cpp
        Arch/x86_64/Time/HPETComparator.cpp
        Arch/x86_64/Time/PIT.cpp
        Arch/x86_64/Time/RTC.cpp
        Arch/x86_64/PCSpeaker.cpp

        Arch/x86_64/ISABus/HID/PS2KeyboardDevice.cpp
        Arch/x86_64/ISABus/HID/PS2MouseDevice.cpp
        Arch/x86_64/ISABus/HID/VMWareMouseDevice.cpp
        Arch/x86_64/ISABus/I8042Controller.cpp
        Arch/x86_64/ISABus/IDEController.cpp
        Arch/x86_64/ISABus/SerialDevice.cpp
        Arch/x86_64/PCI/Controller/HostBridge.cpp
        Arch/x86_64/PCI/IDELegacyModeController.cpp
        Arch/x86_64/PCI/Initializer.cpp

        Arch/x86_64/VGA/IOArbiter.cpp

        Arch/x86_64/RTC.cpp
        Arch/x86_64/Shutdown.cpp
        Arch/x86_64/SmapDisabler.cpp

        # TODO: Share these with the aarch64 build
        Interrupts/SpuriousInterruptHandler.cpp
        kprintf.cpp
        Panic.cpp
    )

    set(KERNEL_SOURCES
        ${KERNEL_SOURCES}
        ${CMAKE_CURRENT_SOURCE_DIR}/Arch/x86_64/Boot/ap_setup.S
        ${CMAKE_CURRENT_SOURCE_DIR}/Arch/x86_64/InterruptEntry.cpp
    )

    set(KERNEL_SOURCES
        ${KERNEL_SOURCES}
        
        ${CMAKE_CURRENT_SOURCE_DIR}/Arch/x86_64/ASM_wrapper.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/Arch/x86_64/CPU.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/Arch/x86_64/CPUID.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/Arch/x86_64/InterruptManagement.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/Arch/x86_64/Interrupts.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/Arch/x86_64/PageDirectory.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/Arch/x86_64/Processor.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/Arch/x86_64/ProcessorInfo.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/Arch/x86_64/SafeMem.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/Arch/x86_64/TrapFrame.cpp
    )

    if("${SERENITY_ARCH}" STREQUAL "x86_64")
        set(KERNEL_SOURCES
            ${KERNEL_SOURCES}
            ${CMAKE_CURRENT_SOURCE_DIR}/Arch/x86_64/SyscallEntry.cpp
        )
    endif()
elseif("${SERENITY_ARCH}" STREQUAL "aarch64")
    set(RPI_SOURCES
        Arch/aarch64/RPi/DebugOutput.cpp
        Arch/aarch64/RPi/Framebuffer.cpp
        Arch/aarch64/RPi/GPIO.cpp
        Arch/aarch64/RPi/InterruptController.cpp
        Arch/aarch64/RPi/Mailbox.cpp
        Arch/aarch64/RPi/MMIO.cpp
        Arch/aarch64/RPi/SDHostController.cpp
        Arch/aarch64/RPi/Timer.cpp
        Arch/aarch64/RPi/UART.cpp
    )
    set(SOURCES_RUNNING_WITHOUT_MMU
        Arch/aarch64/Exceptions.cpp
        Arch/aarch64/MMU.cpp
        Arch/aarch64/pre_init.cpp
        )
    set(KERNEL_SOURCES
        ${KERNEL_SOURCES}
        ${RPI_SOURCES}
        ${SOURCES_RUNNING_WITHOUT_MMU}
        Arch/Processor.cpp

        Arch/aarch64/boot.S
        Arch/aarch64/BootPPMParser.cpp
        Arch/aarch64/CPUID.cpp
        Arch/aarch64/CurrentTime.cpp
        Arch/aarch64/Dummy.cpp
        Arch/aarch64/init.cpp
        Arch/aarch64/InterruptManagement.cpp
        Arch/aarch64/Interrupts.cpp
        Arch/aarch64/kprintf.cpp
        Arch/aarch64/MainIdRegister.cpp
        Arch/aarch64/PageDirectory.cpp
        Arch/aarch64/Panic.cpp
        Arch/aarch64/Processor.cpp
        Arch/aarch64/SafeMem.cpp
        Arch/aarch64/SmapDisabler.cpp
        Arch/aarch64/vector_table.S
    )

    # Otherwise linker errors e.g undefined reference to `__aarch64_cas8_acq_rel'
    add_compile_options(-mno-outline-atomics -latomic)

    # FIXME: Remove this once compiling MemoryManager.cpp doesn't give the nonnull error anymore.
    add_compile_options(-Wno-nonnull)

    # NOTE: These files cannot use a stack protector and sanitizers, as these will cause accesses to global variables to be inserted
    #       by the compiler. The CPU cannot access global variables without the MMU as the kernel is linked for a virtual address in high memory.
    set_source_files_properties(${SOURCES_RUNNING_WITHOUT_MMU} PROPERTIES COMPILE_FLAGS "-fno-stack-protector -fno-sanitize=all")
endif()

set(AK_SOURCES
    ../AK/DOSPackedTime.cpp
    ../AK/GenericLexer.cpp
    ../AK/Hex.cpp
    ../AK/MemoryStream.cpp
    ../AK/Stream.cpp
    ../AK/StringBuilder.cpp
    ../AK/StringUtils.cpp
    ../AK/StringView.cpp
    ../AK/Time.cpp
    ../AK/Error.cpp
    ../AK/Format.cpp
    ../AK/UUID.cpp
)

set(EDID_SOURCES
    ../Userland/Libraries/LibEDID/DMT.cpp
    ../Userland/Libraries/LibEDID/EDID.cpp
    ../Userland/Libraries/LibEDID/VIC.cpp
)

set(ELF_SOURCES
    ../Userland/Libraries/LibELF/Image.cpp
    ../Userland/Libraries/LibELF/Validation.cpp
)

add_custom_command(
    COMMAND "${SerenityOS_SOURCE_DIR}/Kernel/generate-version-file.sh" "${CMAKE_CURRENT_BINARY_DIR}/Version.h.tmp"
    COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${CMAKE_CURRENT_BINARY_DIR}/Version.h.tmp" "${CMAKE_CURRENT_BINARY_DIR}/Version.h"
    COMMAND "${CMAKE_COMMAND}" -E remove "${CMAKE_CURRENT_BINARY_DIR}/Version.h.tmp"
    WORKING_DIRECTORY "${SerenityOS_SOURCE_DIR}"
    COMMENT "Generating SerenityOS version information"
    OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/Version.h"
    VERBATIM
)
add_custom_target(generate_version_header DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/Version.h")
set(GENERATED_SOURCES "${CMAKE_CURRENT_BINARY_DIR}/Version.h")

generate_state_machine(../Userland/Libraries/LibVT/StateMachine.txt ../Userland/Libraries/LibVT/EscapeSequenceStateMachine.h)

set(VT_SOURCES
    ../Userland/Libraries/LibVT/Terminal.cpp
    ../Userland/Libraries/LibVT/Line.cpp
    ../Userland/Libraries/LibVT/EscapeSequenceParser.cpp
)

set(CRYPTO_SOURCES
    ../Userland/Libraries/LibCrypto/Cipher/AES.cpp
    ../Userland/Libraries/LibCrypto/Hash/SHA2.cpp
)

set(PARTITION_SOURCES
    ../Userland/Libraries/LibPartition/DiskPartitionMetadata.cpp
    ../Userland/Libraries/LibPartition/EBRPartitionTable.cpp
    ../Userland/Libraries/LibPartition/GUIDPartitionTable.cpp
    ../Userland/Libraries/LibPartition/MBRPartitionTable.cpp
    ../Userland/Libraries/LibPartition/PartitionTable.cpp
)

set(SOURCES
    ${KERNEL_SOURCES}
    ${GENERATED_SOURCES}
    ${AK_SOURCES}
    ${EDID_SOURCES}
    ${ELF_SOURCES}
    ${VT_SOURCES}
    ${CRYPTO_SOURCES}
    ${PARTITION_SOURCES}
)

add_compile_options(-fsigned-char)
add_compile_options(-Wno-unknown-warning-option -Wvla -Wnull-dereference)
add_compile_options(-fno-rtti -ffreestanding -fbuiltin)

if ("${SERENITY_ARCH}" STREQUAL "x86_64")
    add_compile_options(-mno-80387 -mno-mmx -mno-sse -mno-sse2)
elseif("${SERENITY_ARCH}" STREQUAL "aarch64")
    add_compile_options(-mgeneral-regs-only)
endif()

add_compile_options(-fno-asynchronous-unwind-tables)
add_compile_options(-fstack-protector-strong)
add_compile_options(-fno-exceptions)
add_compile_options(-nostdlib)

# Auto initialize trivial types on the stack, we use "pattern" as 
# it's the only option portable across compilers going forward.
#
# This is designed to help avoid uninitialized variables bugs and
# information disclosures coming from the kernel stack. 
#
# FIXME: It appears to conflict with something during the boot of the
# aarch64 kernel, we should investigate and remove this special case.
if (NOT "${SERENITY_ARCH}" STREQUAL "aarch64")
    add_compile_options(-ftrivial-auto-var-init=pattern)
endif()

if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    # Apply any flags that are only available on >= GCC 11.1
    if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "11.1")
        # Zero any registers used within a function on return (to reduce data lifetime and ROP gadgets).
        add_compile_options(-fzero-call-used-regs=used-gpr)
    endif()
    link_directories(${TOOLCHAIN_ROOT}/${SERENITY_ARCH}-pc-serenity/lib)
    link_directories(${TOOLCHAIN_ROOT}/lib/gcc/${SERENITY_ARCH}-pc-serenity/${GCC_VERSION}/)

    set(TARGET_STRING "")


    # Prevent naively implemented string functions (like strlen) from being "optimized" into a call to themselves.
    set_source_files_properties(MiniStdLib.cpp
        PROPERTIES COMPILE_FLAGS "-fno-tree-loop-distribution -fno-tree-loop-distribute-patterns")

    add_link_options(LINKER:-z,pack-relative-relocs)
else() # Assume Clang
    add_compile_options(-Waddress-of-packed-member)
    add_compile_options(-faligned-allocation)

    # We need this in order to pick up the #define __serenity__, otherwise we end up including unistd.h into the linker script
    set(TARGET_STRING "--target=${CMAKE_CXX_COMPILER_TARGET}")

    add_link_options(LINKER:--build-id=none LINKER:--pack-dyn-relocs=relr)
endif()

macro (set_new_alignment alignment)
    if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
        add_compile_options(-faligned-new=${alignment})
    elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang$")
        add_compile_options(-fnew-alignment=${alignment})
    endif()
endmacro()

if ("${SERENITY_ARCH}" STREQUAL "x86_64")
    add_compile_options(-mcmodel=large -mno-red-zone)
    set_new_alignment(8)
endif()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-pie")

# Kernel Coverage (KCOV) is an API to collect and expose program counters of
# kernel code that has been run to user space. It's rather slow and likely not
# secure to run in production builds. Useful for coverage guided fuzzing.
if (ENABLE_KERNEL_COVERAGE_COLLECTION)
    add_definitions(-DENABLE_KERNEL_COVERAGE_COLLECTION)
    add_compile_options(-fsanitize-coverage=trace-pc)
    set(KCOV_EXCLUDED_SOURCES
        # Make sure we don't instrument any code called from __sanitizer_cov_trace_pc
        # otherwise we'll end up with recursive calls to that function.
        ../AK/Error.cpp
        ../AK/Format.cpp
        ../AK/StringBuilder.cpp
        ../Kernel/Arch/x86_64/Processor.cpp
        ../Kernel/Devices/KCOVDevice.cpp
        ../Kernel/Devices/KCOVInstance.cpp
        ../Kernel/FileSystem/File.cpp
        ../Kernel/FileSystem/OpenFileDescription.cpp
        ../Kernel/init.cpp
        ../Kernel/SanCov.cpp
        # GCC assumes that the caller saves registers for functions according
        # to the System V ABI and happily inserts coverage calls into the
        # function prologue for all functions. This assumption is not true for
        # interrupt handlers because their calling convention is not compatible
        # with the System V ABI.
        ../Kernel/Arch/x86_64/Interrupts.cpp
        ../Kernel/Syscall.cpp
    )
    set_source_files_properties(${KCOV_EXCLUDED_SOURCES} PROPERTIES COMPILE_FLAGS "-fno-sanitize-coverage=trace-pc")
elseif (ENABLE_USERSPACE_COVERAGE_COLLECTION)
    # Disable checking open() pledges and the veil for coverage data when building userspace with coverage
    # so that binaries can write out coverage data even with pledges/veil
    add_compile_definitions(SKIP_PATH_VALIDATION_FOR_COVERAGE_INSTRUMENTATION)
endif()

if (ENABLE_KERNEL_UNDEFINED_SANITIZER)
    # Kernel Undefined Behavior Sanitizer (KUBSAN)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
endif()

# Kernel Address Sanitize (KASAN) implementation is still a work in progress, this option
# is not currently meant to be used, besides when developing Kernel ASAN support.
#
if (ENABLE_KERNEL_ADDRESS_SANITIZER)
    add_compile_options(-fsanitize=kernel-address)
    add_link_options(-fsanitize=kernel-address)
endif()

if ("${SERENITY_ARCH}" STREQUAL "aarch64")
    add_compile_options(-fno-threadsafe-statics)

    # Unaligned memory access will cause a trap, so to make sure the compiler doesn't generate
    # those unaligned accesses, this flag is added.
    add_compile_options(-mstrict-align -Wno-cast-align)
endif()

add_compile_definitions(KERNEL)
add_link_options(LINKER:-z,notext)

add_library(kernel_heap STATIC ${KERNEL_HEAP_SOURCES})

add_executable(Kernel ${SOURCES})
add_dependencies(Kernel generate_EscapeSequenceStateMachine.h generate_version_header)

if (NOT "${SERENITY_ARCH}" STREQUAL "aarch64")
    add_custom_command(
        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/linker.ld
        COMMAND "${CMAKE_CXX_COMPILER}" ${TARGET_STRING} -E -P -x c -I${CMAKE_CURRENT_SOURCE_DIR}/.. "${CMAKE_CURRENT_SOURCE_DIR}/Arch/x86_64/linker.ld" -o "${CMAKE_CURRENT_BINARY_DIR}/linker.ld"
        MAIN_DEPENDENCY "Arch/x86_64/linker.ld"
        COMMENT "Preprocessing linker.ld"
        VERBATIM
    )

    add_custom_target(generate_kernel_linker_script DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/linker.ld)
    target_link_options(Kernel PRIVATE LINKER:-T ${CMAKE_CURRENT_BINARY_DIR}/linker.ld -nostdlib -nodefaultlibs)
    set_target_properties(Kernel PROPERTIES LINK_DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/linker.ld")
else()
    target_link_options(Kernel PRIVATE LINKER:-T ${CMAKE_CURRENT_SOURCE_DIR}/Arch/aarch64/linker.ld -nostdlib LINKER:--no-pie)
    set_target_properties(Kernel PROPERTIES LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Arch/aarch64/linker.ld)
endif()

if (ENABLE_KERNEL_LTO)
    include(CheckIPOSupported)
    check_ipo_supported()
    add_definitions(-DENABLE_KERNEL_LTO)
    set_property(TARGET Kernel PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
    if (NOT "${SERENITY_ARCH}" STREQUAL "aarch64")
        set_property(TARGET kernel_heap PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
    endif()
endif()

if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    target_link_libraries(Kernel PRIVATE kernel_heap gcc)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang$")
    target_link_libraries(Kernel PRIVATE kernel_heap clang_rt.builtins)
endif()

add_custom_command(
        TARGET Kernel POST_BUILD
        COMMAND "${CMAKE_COMMAND}" -E env NM=${CMAKE_NM} sh ${CMAKE_CURRENT_SOURCE_DIR}/mkmap.sh
        COMMAND "${CMAKE_COMMAND}" -E env OBJCOPY=${CMAKE_OBJCOPY} sh ${CMAKE_CURRENT_SOURCE_DIR}/embedmap.sh
        COMMAND ${CMAKE_OBJCOPY} --only-keep-debug Kernel Kernel.debug
        COMMAND ${CMAKE_OBJCOPY} --strip-debug Kernel
        COMMAND ${CMAKE_OBJCOPY} --add-gnu-debuglink=Kernel.debug Kernel
        BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/kernel.map
)

install(FILES "${CMAKE_CURRENT_BINARY_DIR}/Kernel" DESTINATION boot)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/Kernel.debug" DESTINATION boot)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/kernel.map" DESTINATION res)

if ("${SERENITY_ARCH}" STREQUAL "aarch64")
    embed_resource(Kernel serenity_boot_logo "Arch/aarch64/SerenityLogoRGB.ppm")
    add_custom_command(
            TARGET Kernel POST_BUILD
            COMMAND ${CMAKE_OBJCOPY} -O binary Kernel kernel8.img
            BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/kernel8.img
    )
endif()

serenity_install_headers(Kernel)
serenity_install_sources(Kernel)

# aarch64 does not need a Prekernel
if (NOT "${SERENITY_ARCH}" STREQUAL "aarch64")
    add_subdirectory(Prekernel)
endif()