hi my name is Arnell and today we're
going to start coating a kernel in zig
we will be targeting
x8664 for now but we may eventually Port
it to other architectures like Risk 5 or
a arch 64 so we'll keep a more or less
modular design that we can later expand
on
but today
we'll only do a Hello World
we'll be using lemonade as a bootloader
all the code is available on GitHub
let's start
first we'll be writing the build.zig
file
we'll create a kernel configuration
struct that we will eventually expand
but will be as simple as a Target
architecture for now
for some architectures we'll need to
enable or disable certain features
this is the case of x8664.
we quickly create a function that takes
an architecture as a parameter and
Returns what features should be enabled
and disabled
for x8664 we need to disable MMX SSE and
AVX while enabling software floats
now we can create our cross compilation
Target
we'll leave the default optimizations
now create a compilation step for the
kernel
the main file will be kernel slash SRC
slash main.zig
we should also set the code mode to
Kernel and Pie flag position independent
executable to true
we need our own link or script too
we now create the top level step that
will run to compile the kernel
compiling the kernel is not really
enough as you cannot do much with a
kernel
we probably want to create an ISO image
containing both the kernel and the
bootloader lemon a
we may also want to run it in Kimu so
we'll create a script for that too
the first script which we'll call
lemonade.sh will be very simple
it will get cloned eliminate bootloader
and call the make Command on it
we also make sure to delete any prior
cloning
now to build the ISO file we write
iso.sh
we'll be working on a folder called ISO
underscore root placed under Zig cache
so we make sure to create it and delete
it if it already existed
now we'll copy the kernel and some
lemonade files
after that we create EFI slash Boot and
copy some more lemonade files in there
we then create the iso using xorriso and
install lemonade on it
for the Run underscore iso.sh
we just have to call Kimu with the
appropriate Flags
we can also print a new line for visual
reasons
we can go back to build.zig now and
create steps for running the scripts we
just created
make sure to add the dependencies
between them
now that we're at it
we can also create a new step for
cleaning the working directory
and that's everything we need for the
build system
we now have to write the link or script
which we'll call linker.ld specify
underscore start as the entry point and
to find the program headers
for the sections
just make sure that they start at the
appropriate address
now this is a fun part create a
lemonade.cfg file set the timeout and
create a basic entry
this will be the lemonade configuration
file that specifies what the bootloader
entry will look like and how to load the
kernel
we're ready to write the kernel itself
we create the main.zig file
the underscore start function will be
called by lemonae as that's what we
specified in the Linker script
however as the stack doesn't contain a
valid return address
we should declare it no return and put
an endless loop in it
let's go ahead and run it
we run the command Zig build run ISO but
nothing happens
we can see the lemonade boot screen but
then it's all in black
there's really nothing wrong with it
in fact it's working perfectly
we still haven't done anything
for now just as a hello world we'll
write to Port hex E9 where some
emulators have the debug output on
we write some wrappers for the NB and
Out b assembly instructions and call
them to print hi
let's run it again
we can see high printed to the console
we didn't print it on the screen as that
requires more work but it sure is
printing on the debug con as Kimu calls
it and we can indeed see it in STD out
with that we've done the bare bones of a
64-bit kernel
don't forget the code is on GitHub
also if you've made it this far you may
want to subscribe so that you don't miss
part two
thanks for watching