NaOS icon indicating copy to clipboard operation
NaOS copied to clipboard

A 64bit (x86-64) Simple Operating System Writing By C++.

NaOS

Nano Operating system

A 64bit (arch x86-64) simple operating system writing by c++. (:sunglasses:)
NaOS runs on Intel / AMD modern processors.

BSD-3-Clause development on linux

Features

View Features .

Preview

preview1

Quick Start

Requirements

  • GNU Binutils (version 2.38 tested)
  • GNU EFI lib pacman -S gnu-efi
  • GCC or Clang (GCC 12.1.1 tested)
  • CMake 3.3+
  • ninja and meson are required to build libc
  • Python 3 (For running utility)
  • An emulator or virtual machine such as Bochs, QEMU, Virtual Box, hyper-v and VMware Workstation (For running OS)
  • Grub2, fdisk / gdisk, udisks2 (For making runnable raw disk file and mounting raw disk without root privilege)

Optional

  • OVMF (UEFI firmware) for QEMU if boot from UEFI environment

1. Download source code

Clone this repo

git clone https://url/path/to/repo
git submodule update --init

2. Compile

  • GCC
cd /path/to/repo

# build libc first
cd naos/libc/mlibc/
cat > cross_file.txt << EOF
[host_machine]
system = 'naos'
cpu_family = 'x86_64'
cpu = 'i686'
endian = 'little'

[binaries]
c = '/usr/bin/x86_64-pc-linux-gnu-gcc' 
cpp = '/usr/bin/x86_64-pc-linux-gnu-g++'
ar = '/usr/bin/ar'
strip = '/usr/bin/strip'

[properties]
needs_exe_wrapper = true
EOF

meson setup build --cross-file cross_file.txt
cd build && ninja

cd /path/to/repo
# build kernel & nanobox binary
mkdir build
cd build
# CMAKE_BUILD_TYPE: Debug\Release
cmake -DCMAKE_BUILD_TYPE=Debug -DFREELIBCXX_TEST=OFF ..
make -j
  • Or Clang/LLVM (Not tested yet)
cd /path/to/repo

# build libc first
cd naos/libc/mlibc/
cat > cross_file.txt << EOF
[host_machine]
system = 'naos'
cpu_family = 'x86_64'
cpu = 'i686'
endian = 'little'

[binaries]
c = '/usr/bin/clang'
c_ld = '/usr/bin/lld'
cpp = '/usr/bin/clang++'
cpp_ld = '/usr/bin/lld'
ar = '/usr/bin/ar'
strip = '/usr/bin/strip'

[properties]
needs_exe_wrapper = true
EOF
meson setup build --cross-file cross_file.txt
cd build && ninja 

cd /path/to/repo
# build kernel & nanobox binary
mkdir build
cd build

# CMAKE_BUILD_TYPE: Debug\Release
cmake -DCMAKE_BUILD_TYPE=Release -DFREELIBCXX_TEST=OFF  -DCMAKE_CXX_COMPILER="/usr/bin/clang++" -DCMAKE_C_COMPILER="/usr/bin/clang" -DCMAKE_CXX_FLAGS="-fuse-ld=lld" -DCMAKE_C_FLAGS="-fuse-ld=lld" ..

make -j

3. Creating artifact

NaOS requires multiboot2 loader, which is supported by GRUB. multiboot2 (spec).

A. ISO image target

To get kernel iso image:

cur=`pwd`
cp ${cur}/build/bin/system/kernel ${cur}/run/iso/kernel
cp ${cur}/build/bin/system/rfsimg ${cur}/run/iso/rfsimg
grub-mkrescue -o ./run/image/naos.iso ./run/iso

B. Disk target

dd if=/dev/zero of=run/image/disk.img bs=1024 count=140240
Option1. Legacy mode

Make partitions by gdisk ./run/image/disk.img.
Example of disk partition (MBR):

Partition number Type (Gdisk) Code FS Content Size
1 Grub 8300 FAT32 Grub data 70MiB
2 Root 8300 FAT32 NaOS data 70MiB

Then mount disk and make filesystem

python ./util/disk.py mount
mkfs.fat -F32 /dev/loopxp1
mkfs.fat -F32 /dev/loopxp2

Install (MBR):

sudo grub-install --boot-directory=/run/media/user/xxx --targe=i386-pc run/image/disk.img

vim grub.cfg (MBR):

root=(hd0,msdos1)
set default=0
set timeout=1
menuentry "NaOS multiboot2" {
    insmod all_video
    insmod part_msdos
    insmod fat
    multiboot2 /boot/kernel
    module2 /boot/rfsimg
    boot
}
python ./util/disk.py umount
Option2. UEFI mode

Install OVMF and configure OVMF_CODE.fd in util/run.py.

dd if=/dev/zero of=run/image/disk.img bs=1024 count=140240

Make partitions by gdisk ./run/image/disk.img.
Example of disk partitions (UEFI):

Partition number Type (Gdisk) Code FS Content Size
1 ESP EF00 FAT32 Grub EFI loader 70MiB
2 Root 8300 FAT32 Grub and NaOS data 70MiB
python ./util/disk.py mount
mkfs.fat -F32 /dev/loopxp1
mkfs.fat -F32 /dev/loopxp2

Install (UEFI):

sudo grub-install --boot-directory=/run/media/user/root_xxx  --efi-directory=/run/media/user/esp_xxx  --targe=x86_64-efi run/image/disk.img

vim /run/media/user/root_xxx/grub/grub.cfg (UEFI):

root=(hd0,gpt2)
set default=0
set timeout=1
menuentry "NaOS multiboot2" {
    insmod all_video
    insmod part_gpt
    insmod part_msdos
    insmod fat
    insmod ext2
    multiboot2 /boot/kernel
    module2 /boot/rfsimg
    boot
}
python ./util/disk.py umount

The raw disk file should be in the path run/image/disk.img.

References:

Make a disk
Install the grub. (archlinux wiki)
OSDev

4. Run

After make success, the following files will be generated

build
├── bin # Binary executable files without debug info
│   ├── rfsroot # root file system image files (the root folder when kernel loading)
│   └── system
│       ├── rfsimg # root file system image
│       └── kernel # kernel binary file
└── debug # Binary executable files with debug info which can be used by debugger like gdb/lldb ...

make sure to mount disk by python util/disk.py mount before running the emulator. then

# Run emulator
python util/run.py q
# or uefi
python util/run.py q --uefi

The kernel log will be generated in run/kernel_out.log,

tail -f run/kernel_out.log

5. Debug

The command python util/gen_debug_asm.py kernel will generate kernel disassembly if needed. (e.g. Debug in bochs)

Repo Tree

NaOS
├── build # build target directory
├── naos
│   ├── includes
│   │   └── kernel
│   └── src
│       ├── kernel
│       │   ├── arch # arch x86_64 specification source code
│       │   ├── common # kernel data
│       │   ├── dev # driver interface & drivers
│       │   ├── fs # file subsystem 
│       │   ├── io # io subsystem
│       │   ├── mm # memory subsystem
│       │   ├── module # module support code
│       │   ├── schedulers # round robin scheduler and completely fair scheduler
│       │   ├── syscall # syscall entries
│       │   ├── task # ELF loader and built-in task
│       │   └── util # util functions: memcpy, strcpy, cxxlib, formatter, containers
│       └── usr
│           ├── init # the userland init program 
│           └── bin # nanobox program, like busybox
├── run
│   ├── fakeroot # the files in fake root path will be overwritten to the real root path
│   ├── cfg # include emulator config file: bochsrc
│   └── image
│       └── disk.img # raw disk image to startup OS
└── util # Python tools

References

License

BSD-3-Clause © Kadds


          _____                    _____                   _______                   _____          
         /\    \                  /\    \                 /::\    \                 /\    \         
        /::\____\                /::\    \               /::::\    \               /::\    \        
       /::::|   |               /::::\    \             /::::::\    \             /::::\    \       
      /:::::|   |              /::::::\    \           /::::::::\    \           /::::::\    \      
     /::::::|   |             /:::/\:::\    \         /:::/~~\:::\    \         /:::/\:::\    \     
    /:::/|::|   |            /:::/__\:::\    \       /:::/    \:::\    \       /:::/__\:::\    \    
   /:::/ |::|   |           /::::\   \:::\    \     /:::/    / \:::\    \      \:::\   \:::\    \   
  /:::/  |::|   | _____    /::::::\   \:::\    \   /:::/____/   \:::\____\   ___\:::\   \:::\    \  
 /:::/   |::|   |/\    \  /:::/\:::\   \:::\    \ |:::|    |     |:::|    | /\   \:::\   \:::\    \ 
/:: /    |::|   /::\____\/:::/  \:::\   \:::\____\|:::|____|     |:::|    |/::\   \:::\   \:::\____\
\::/    /|::|  /:::/    /\::/    \:::\  /:::/    / \:::\    \   /:::/    / \:::\   \:::\   \::/    /
 \/____/ |::| /:::/    /  \/____/ \:::\/:::/    /   \:::\    \ /:::/    /   \:::\   \:::\   \/____/ 
         |::|/:::/    /            \::::::/    /     \:::\    /:::/    /     \:::\   \:::\    \     
         |::::::/    /              \::::/    /       \:::\__/:::/    /       \:::\   \:::\____\    
         |:::::/    /               /:::/    /         \::::::::/    /         \:::\  /:::/    /    
         |::::/    /               /:::/    /           \::::::/    /           \:::\/:::/    /     
         /:::/    /               /:::/    /             \::::/    /             \::::::/    /      
        /:::/    /               /:::/    /               \::/____/               \::::/    /       
        \::/    /                \::/    /                 ~~                      \::/    /        
         \/____/                  \/____/                                           \/____/