Skip to content

Commit

Permalink
build: move init and iso generation all to cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
tyler569 committed May 9, 2024
1 parent 9070cac commit 8b1cbeb
Show file tree
Hide file tree
Showing 21 changed files with 149 additions and 168 deletions.
112 changes: 80 additions & 32 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,50 +1,90 @@
cmake_minimum_required(VERSION 3.10)

project(nightingale C ASM CXX)
include(ExternalProject)

unset(CMAKE_SYSROOT)
set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/sysroot)
set(CMAKE_SYSROOT ${CMAKE_BINARY_DIR}/sysroot)
set(CMAKE_C_STANDARD 23)
set(CMAKE_C_FLAGS_DEBUG "-g -O0 -Werror")
set(CMAKE_C_FLAGS_RELEASE "-O3")

set(limine_cfg_file "${CMAKE_SOURCE_DIR}/kernel/limine.cfg")

set(limine_dir "${CMAKE_BINARY_DIR}/limine")
set(iso_file "${CMAKE_SOURCE_DIR}/ngos.iso")
set(iso_dir "${CMAKE_BINARY_DIR}/system_root")

set(system_boot_dir "${iso_dir}/boot")
set(system_include_dir "${iso_dir}/include")
set(system_lib_dir "${iso_dir}/lib")
set(system_bin_dir "${iso_dir}/bin")

file(MAKE_DIRECTORY ${iso_dir}/boot/limine)
file(MAKE_DIRECTORY ${system_include_dir})
file(MAKE_DIRECTORY ${system_lib_dir})
file(MAKE_DIRECTORY ${system_bin_dir})

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${system_bin_dir})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${system_lib_dir})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${system_lib_dir})
set(EXECUTABLE_OUTPUT_PATH ${system_bin_dir})
set(LIBRARY_OUTPUT_PATH ${system_lib_dir})

file(COPY ${limine_cfg_file} DESTINATION ${system_boot_dir})
file(COPY ${CMAKE_SOURCE_DIR}/include DESTINATION ${iso_dir})

set(CMAKE_C_FLAGS_DEBUG "-g -O0 -Werror")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "-g")

message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")

# We do this as a manual git download as a workaround for the fact that ExternalProject_Add
# doesn't support shallow clones, or more accurately lies about its support for shallow clones.
# See: https://gitlab.kitware.com/cmake/cmake/-/issues/17770
if (NOT EXISTS ${limine_dir})
ExternalProject_Add(limine
SOURCE_DIR "${limine_dir}"
DOWNLOAD_COMMAND git clone --depth 1 --branch v7.x-binary https://github.com/limine-bootloader/limine.git ${limine_dir}
CONFIGURE_COMMAND ""
BUILD_IN_SOURCE 1
BUILD_COMMAND make
INSTALL_COMMAND ""
UPDATE_COMMAND ""
)
endif ()

project(nightingale C ASM)

execute_process(
COMMAND git describe --tags
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE NIGHTINGALE_VERSION
OUTPUT_VARIABLE nightingale_version
OUTPUT_STRIP_TRAILING_WHITESPACE
)

configure_file(include/version.h.in version.h)

list(APPEND AUTOGENERATED_HEADERS
${CMAKE_BINARY_DIR}/autogenerated_errnos.h
${CMAKE_BINARY_DIR}/autogenerated_syscall_consts.h
${CMAKE_BINARY_DIR}/autogenerated_syscalls_kernel.h
${CMAKE_BINARY_DIR}/autogenerated_syscalls_user.h
${CMAKE_BINARY_DIR}/autogenerated_errnos.c
${CMAKE_BINARY_DIR}/autogenerated_syscall_names.c
${CMAKE_BINARY_DIR}/autogenerated_syscalls_kernel.c
${CMAKE_BINARY_DIR}/autogenerated_syscalls_user.c
configure_file(include/version.h.in ${system_include_dir}/version.h)

list(APPEND nightingale_autogenerated_headers
${system_include_dir}/autogenerated_errnos.h
${system_include_dir}/autogenerated_syscall_consts.h
${system_include_dir}/autogenerated_syscalls_kernel.h
${system_include_dir}/autogenerated_syscalls_user.h
${system_include_dir}/autogenerated_errnos.c
${system_include_dir}/autogenerated_syscall_names.c
${system_include_dir}/autogenerated_syscalls_kernel.c
${system_include_dir}/autogenerated_syscalls_user.c
)

add_custom_command(
OUTPUT ${AUTOGENERATED_HEADERS}
COMMAND ${CMAKE_SOURCE_DIR}/script/generate_syscalls.rb ${CMAKE_BINARY_DIR}
OUTPUT ${nightingale_autogenerated_headers}
COMMAND ${CMAKE_SOURCE_DIR}/script/generate_syscalls.rb ${system_include_dir}
DEPENDS ${CMAKE_SOURCE_DIR}/script/generate_syscalls.rb
DEPENDS interface/SYSCALLS interface/ERRNOS
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)

set_source_files_properties(${AUTOGENERATED_HEADERS} PROPERTIES GENERATED 1)

add_custom_target(generate_headers DEPENDS ${AUTOGENERATED_HEADERS})
add_custom_target(generate_headers DEPENDS ${nightingale_autogenerated_headers})

add_compile_options(
-Wall
-Wextra
-g
-O2
-fcolor-diagnostics
-ffreestanding
-fstrict-aliasing
-nostdlib
Expand All @@ -55,18 +95,14 @@ add_compile_options(
-Wno-deprecated-non-prototype
)

if ($ENV{ENABLE_WERROR})
add_compile_options(-Werror)
endif ()

add_compile_definitions(
__nightingale__=1
_NG_SOURCE=1
)

set(CMAKE_EXE_LINKER_FLAGS "-static")

include_directories(${CMAKE_BINARY_DIR})
include_directories(${system_include_dir})
include_directories(include)

add_subdirectory(kernel)
Expand All @@ -78,5 +114,17 @@ add_dependencies(nightingale_kernel generate_headers)
add_dependencies(c generate_headers)
add_dependencies(elf generate_headers)

install(DIRECTORY include DESTINATION ${CMAKE_INSTALL_PREFIX}/usr)
install(FILES ${AUTOGENERATED_HEADERS} DESTINATION ${CMAKE_INSTALL_PREFIX}/usr/include)
add_custom_command(
DEPENDS limine nightingale_kernel initrd kernel_modules "${limine_cfg_file}"
OUTPUT ${iso_file}
COMMAND cp ${limine_dir}/limine-bios.sys ${iso_dir}/boot/limine
COMMAND cp ${limine_dir}/limine-bios-cd.bin ${iso_dir}/boot/limine
COMMAND cp ${limine_dir}/limine-uefi-cd.bin ${iso_dir}/boot/limine
COMMAND xorriso -as mkisofs -b boot/limine/limine-bios-cd.bin
-no-emul-boot -boot-load-size 4 --boot-info-table
--efi-boot boot/limine/limine-uefi-cd.bin
-efi-boot-part --efi-boot-image --protective-msdos-label
"${iso_dir}" -o "${iso_file}"
COMMAND ${limine_dir}/limine bios-install ${iso_file}
)
add_custom_target(iso ALL DEPENDS ${iso_file})
2 changes: 1 addition & 1 deletion bt.bash → bt.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

file="${1-build/kernel/nightingale_kernel}"
file="${1-build/system_root/boot/nightingale_kernel}"
addr2line_binary="llvm-addr2line"

if [ ! -f "$file" ]; then
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions include/ng/fs/dentry.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ struct dentry {

extern struct dentry *global_root_dentry;

inline struct vnode *dentry_vnode(struct dentry *dentry) {
static inline struct vnode *dentry_vnode(struct dentry *dentry) {
if (dentry->mounted_file_system) {
return dentry->mounted_file_system->root->vnode;
} else {
return dentry->vnode;
}
}

inline struct file_system *dentry_file_system(struct dentry *dentry) {
static inline struct file_system *dentry_file_system(struct dentry *dentry) {
if (dentry->mounted_file_system) {
return dentry->mounted_file_system;
} else {
Expand Down
6 changes: 3 additions & 3 deletions include/ng/fs/vnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ int open_file_clone(struct file *file);
int close_file(struct file *file);
void maybe_delete_vnode(struct vnode *vnode);

inline void access_vnode(struct vnode *i) { i->atime = time_now(); }
inline void modify_vnode(struct vnode *i) { i->mtime = time_now(); }
inline void change_vnode(struct vnode *i) { i->ctime = time_now(); }
static inline void access_vnode(struct vnode *i) { i->atime = time_now(); }
static inline void modify_vnode(struct vnode *i) { i->mtime = time_now(); }
static inline void change_vnode(struct vnode *i) { i->ctime = time_now(); }

END_DECLS
2 changes: 1 addition & 1 deletion include/ng/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ struct thread {
// running_addr().
#define running_thread ((void)0, this_cpu->running)
#define running_process ((void)0, running_thread->proc)
inline struct thread *running_addr() { return this_cpu->running; }
static inline struct thread *running_addr() { return this_cpu->running; }

void return_from_interrupt();
void set_kernel_stack(void *);
Expand Down
42 changes: 21 additions & 21 deletions include/ng/x86/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ BEGIN_DECLS
bool supports_feature(enum x86_feature feature);
[[noreturn]] void longjump_kcode(uintptr_t ip, uintptr_t sp);

inline uint64_t rdtsc() { return __builtin_ia32_rdtsc(); }
static inline uint64_t rdtsc() { return __builtin_ia32_rdtsc(); }

inline uintptr_t cr3() {
static inline uintptr_t cr3() {
uintptr_t cr3 = 0;
asm volatile("mov %%cr3, %0" : "=a"(cr3));
return cr3;
Expand All @@ -60,25 +60,25 @@ enum {
_RDX,
};

inline void cpuid(uint32_t a, uint32_t c, uint32_t out[4]) {
static inline void cpuid(uint32_t a, uint32_t c, uint32_t out[4]) {
asm("cpuid"
: "=a"(out[_RAX]), "=b"(out[_RBX]), "=c"(out[_RCX]), "=d"(out[_RDX])
: "0"(a), "2"(c));
}

inline int cpunum() {
static inline int cpunum() {
uint32_t out[4];
cpuid(1, 0, out);
return out[_RBX] >> 24;
}

inline int cpu_id() {
static inline int cpu_id() {
uint32_t out[4];
cpuid(1, 0, out);
return out[_RBX] >> 24;
}

inline void enable_bits_cr4(uintptr_t bitmap) {
static inline void enable_bits_cr4(uintptr_t bitmap) {
uintptr_t tmp;
asm volatile("mov %%cr4, %%rax\n\t"
"or %0, %%rax\n\t"
Expand All @@ -88,80 +88,80 @@ inline void enable_bits_cr4(uintptr_t bitmap) {
: "rax");
}

inline uint8_t inb(port_addr_t port) {
static inline uint8_t inb(port_addr_t port) {
uint8_t result;
asm volatile("inb %1, %0" : "=a"(result) : "Nd"(port));
return result;
}

inline void outb(port_addr_t port, uint8_t data) {
static inline void outb(port_addr_t port, uint8_t data) {
asm volatile("outb %0, %1" : : "a"(data), "Nd"(port));
}

inline uint16_t inw(port_addr_t port) {
static inline uint16_t inw(port_addr_t port) {
uint16_t result;
asm volatile("inw %1, %0" : "=a"(result) : "Nd"(port));
return result;
}

inline void outw(port_addr_t port, uint16_t data) {
static inline void outw(port_addr_t port, uint16_t data) {
asm volatile("outw %0, %1" : : "a"(data), "Nd"(port));
}

inline uint32_t ind(port_addr_t port) {
static inline uint32_t ind(port_addr_t port) {
uint32_t result;
asm volatile("inl %1, %0" : "=a"(result) : "Nd"(port));
return result;
}

inline void outd(port_addr_t port, uint32_t data) {
static inline void outd(port_addr_t port, uint32_t data) {
asm volatile("outl %0, %1" : : "a"(data), "Nd"(port));
}

inline void set_vm_root(uintptr_t address) {
static inline void set_vm_root(uintptr_t address) {
asm volatile("mov %0, %%cr3" : : "r"(address) : "memory");
}

inline void invlpg(uintptr_t address) {
static inline void invlpg(uintptr_t address) {
asm volatile("invlpg (%0)" : : "b"(address) : "memory");
}

inline void flush_tlb() {
static inline void flush_tlb() {
long temp = 0;
asm volatile("mov %%cr3, %0 \n\t"
"mov %0, %%cr3 \n\t"
: "=r"(temp)
: "0"(temp));
}

inline uint64_t rdmsr(uint32_t msr_id) {
static inline uint64_t rdmsr(uint32_t msr_id) {
uint32_t a, d;
asm volatile("rdmsr" : "=a"(a), "=d"(d) : "c"(msr_id));
return ((uint64_t)d << 32) + a;
}

inline void wrmsr(uint32_t msr_id, uint64_t value) {
static inline void wrmsr(uint32_t msr_id, uint64_t value) {
uint32_t a = value, d = value >> 32;
asm volatile("wrmsr" : : "c"(msr_id), "a"(a), "d"(d));
}

inline void set_tls_base(void *tlsbase) {
static inline void set_tls_base(void *tlsbase) {
extern int have_fsgsbase;
if (have_fsgsbase)
asm volatile("wrfsbase %0" ::"r"(tlsbase));
else
wrmsr(0xC0000100, (uintptr_t)tlsbase);
}

inline void set_gs_base(void *gsbase) {
static inline void set_gs_base(void *gsbase) {
extern int have_fsgsbase;
if (have_fsgsbase)
asm volatile("wrgsbase %0" ::"r"(gsbase));
else
wrmsr(0xC0000101, (uintptr_t)gsbase);
}

inline void *get_gs_base() {
static inline void *get_gs_base() {
extern int have_fsgsbase;
void *gsbase;
if (have_fsgsbase)
Expand All @@ -171,7 +171,7 @@ inline void *get_gs_base() {
return gsbase;
}

inline uintptr_t dr6() {
static inline uintptr_t dr6() {
uintptr_t result;
asm volatile("mov %%dr6, %0\n\t" : "=r"(result));
return result;
Expand Down
2 changes: 1 addition & 1 deletion include/version.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
#ifndef _VERSION_H
#define _VERSION_H

#cmakedefine NIGHTINGALE_VERSION "${NIGHTINGALE_VERSION}"
#define NIGHTINGALE_VERSION "${nightingale_version}"

#endif
4 changes: 4 additions & 0 deletions kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ else ()
message(FATAL_ERROR "Unsupported architecture: ${CMAKE_SYSTEM_PROCESSOR}")
endif ()

set_target_properties(nightingale_kernel PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${system_boot_dir}"
)

set_target_properties(nightingale_kernel PROPERTIES
LINK_DEPENDS "${NG_KERNEL_LINK_SCRIPT}"
)
Expand Down
21 changes: 0 additions & 21 deletions kernel/arch/x86/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,3 @@ bool supports_feature(enum x86_feature feature) {
return false;
}
}

extern inline uint64_t rdtsc();
extern inline uintptr_t cr3();
extern inline int cpunum();
extern inline void cpuid(uint32_t a, uint32_t c, uint32_t out[4]);
extern inline void enable_bits_cr4(uintptr_t bitmap);
extern inline void set_tls_base(void *tlsbase);
extern inline uint8_t inb(port_addr_t port);
extern inline void outb(port_addr_t port, uint8_t data);
extern inline uint16_t inw(port_addr_t port);
extern inline void outw(port_addr_t port, uint16_t data);
extern inline uint32_t ind(port_addr_t port);
extern inline void outd(port_addr_t port, uint32_t data);
extern inline void set_vm_root(uintptr_t address);
extern inline void invlpg(uintptr_t address);
extern inline void flush_tlb();
extern inline uint64_t rdmsr(uint32_t msr_id);
extern inline void wrmsr(uint32_t msr_id, uint64_t value);
extern inline void set_tls_base(void *);
extern inline void set_gs_base(void *);
extern inline uintptr_t dr6();
Loading

0 comments on commit 8b1cbeb

Please sign in to comment.