This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 15k traffic Daily!!!

My RISC-V OSDev journey, so far


GitHub repository: https://github.com/DonaldKellett/marvelos

It has been simply over a month since I began writing an working system kernel from scratch for the RISC-V structure, particularly the QEMU RISC-V virt board. Properly, form of. I have been following The Adventures of OS carefully, porting the Rust code to C alongside the best way, making incremental enhancements to the code and re-organizing the mission construction as I see match. On the time of writing (2022-10-19), I’ve applied a round-robin scheduler that juggles around 3 copies of the same user process hardcoded within the kernel ad infinitum, which is one thing, however nonetheless some methods earlier than I can load and execute an precise person program from disk, and positively an extended option to go earlier than the system turns into usable, similar to spawning an interactive command line shell.

On this article, I will current my preliminary motivations and targets for embarking on this mission, background data I discovered indispensable, stuff I realized whereas engaged on the mission and tips that could sources alongside the best way. When you’re additionally desirous about RISC-V and working techniques from a sensible perspective, and undecided on whether or not to offer OSDev a attempt, then this text is for you!



Motivations and Targets

The primary causes I made a decision to embark on this journey are:

  • To study extra in regards to the RISC-V structure from a programmer’s perspective
  • To find out how an OS kernel works, inside out, from a sensible standpoint
  • To have the ability to brag to my colleagues that I managed to write down an OS kernel from scratch



Background data

I discovered the next areas of information indispensable for getting began with OSDev on RISC-V:

  • A strong theoretical understanding of laptop structure and working techniques, by programs I took in my undergraduate Laptop Science curriculum. Another is to seek for associated undergraduate textbooks usually accessible on-line for free of charge and self-study them at your personal tempo:
  • Familiarity with the Linux command line: finest when you’ve got no less than 2 years {of professional} expertise working with the Linux command line, or alternatively, examine for and go a performance-based Linux examination similar to LFCS like I did (RHCSA will even do)
  • A strong grasp of no less than one techniques programming language similar to C, C++ or Rust. Specifically, if going for Rust, studying and finishing most, if not all the workout routines in The Rust Programming Language is strongly advisable
  • Familiarity with compiling software program from supply and construct techniques. If not already accustomed to an current construct system similar to Make or Ninja, one of the best ways to familiarize your self with them is to easily construct and set up a bunch of software program from supply, similar to giving Linux From Scratch (LFS) a go – by the point you handle to finish LFS, you may actually have the ability to recite configure; make; make set up blindfolded 😉



Stuff I realized

Other than the particular matters offered inside “The Adventures of OS” which I’ve “accomplished” as much as and together with chapter 8 on the time of writing, listed below are a few of the important aims I’ve realized and/or received the prospect to observe alongside the best way:

  • The roles of stack, body and world pointers, and find out how to initialize them in meeting so we are able to leap right into a higher-level language like C or Rust early on. RISC-V from scratch 2: Hardware layouts, linker scripts, and C runtimes comprises an in depth rationalization of this, amongst a number of different issues
  • How applications, whether or not a userspace program or an OS kernel, are usually specified by reminiscence, and find out how to assemble a linker script accordingly. Once more, “RISC-V from scratch 2” proved tremendously helpful for my preliminary understanding, although it does this by modifying an auto-generated linker script from ld --verbose. That is high quality as a primary step to get your Good day World kernel up and working, however may devolve into an impediment as you try and develop your OS kernel additional. In any case, it is strongly advisable to rewrite the complete linker script from scratch as quickly as doable in order to have full management over it and an entire understanding of the way it works. Some sources I consulted to understand this transition have been:
  • How one can automate constructing and working the mission utilizing make; specifically, leveraging variables within the Makefile to elegantly apply the identical command line choices for compiling every file within the codebase – as a result of belief me, you may want a ton of command-line choices 😉 For this, I based mostly my preliminary Makefile on that present in the source code for “The Adventures of OS”, e.g. this
  • The device tree specification, find out how to generate a DTS file with QEMU and find out how to learn it. As soon as once more, “RISC-V from scratch 2” supplies a pleasant introduction, and the remainder will be realized by studying the official specification, which actually is not that lengthy – you may undergo the entire thing in a day should you keep centered. Understanding DTS is essential for determining why, for instance, to energy off the QEMU RISC-V virt board, it’s essential to write the value 0x5555 as a 32-bit integer to the memory address 0x100000
  • Why and find out how to manage a mission at this scale into a number of recordsdata grouped into subdirectories based mostly on their particular person capabilities and classes; for instance, I positioned the code associated to paging and digital reminiscence beneath src/mm/, that associated to processes and scheduling beneath src/course of/, and many others.
  • I received the prospect to correctly discover macros in C; specifically, function-like macros and variadic macros, although I admit I may need abused them in some locations like handling the platform-level interrupt controller (PLIC) (-:
  • How one can format code with automated instruments like GNU indent so the coding fashion is constant all through the codebase and eliminates bizarre indentation as a result of unintended mixing of tabs and areas and many others. (ewwwww!!!) I additionally received to observe Linux instructions like discover to seek for all supply code recordsdata and apply formatting to them robotically, sed to work round a problem with binary literals and integrating these instructions into my Makefile as a format goal so I haven’t got to memorize and kind these instructions manually
  • Maybe most significantly, how helpful a debugger like GDB could possibly be and the way it could possibly be a life-saver, if you know the way to make use of one correctly. I’ve by no means been a fan of debuggers and have been a proponent of printf debugging for so long as I’ve been programming. As Brian Kernighan once concluded in “Unix for Beginners”, “The simplest debugging software remains to be cautious thought, coupled with judiciously positioned print statements.” And which may be true of most programming situations, the place it’s trivial to insert printf statements (or equal) across the offending code and see what will get printed out nearly instantly by working the code as soon as once more. However when your kernel unexpectedly hangs in the midst of some operation for one in all numerous doable causes, you could not have the posh to examine the system state by printing stuff to the console, because you may not even know the place this system execution has jumped to! In that case, the one believable method to find out what precisely occurred is to hook your system as much as the debugger and step by the code line by line, instruction by instruction, inspecting values that you just may not in any other case have been capable of print to the console alongside the best way (such because the values of particular registers controlling whether or not you may print any stuff to the console in any respect!). “RISC-V from scratch 2” covers the very fundamentals of utilizing GDB, and a GDB quick reference could possibly be helpful for extra superior GDB utilization (or simply Google stuff similar to “find out how to print the corresponding perform for an instruction handle in GDB” alongside the best way)



Conclusion

Writing an OS kernel from scratch isn’t any straightforward job – a strong basis in laptop science is indispensable, it’s essential to be comfy with the command line and proficient in no less than one techniques programming language, care have to be taken to prepare the mission in a sustainable method, and many others.

Nonetheless, with the required background and the help of well-written tutorials usually accessible on-line for free of charge, plus the dedication to unravel each little element concerned and the persistence to maintain engaged on the mission little by little, function by function, it’s possible to rise up to a usable system (for some definition of “usable”) inside an affordable timeframe, similar to a number of months or years.

When you occur to share an curiosity in RISC-V and working techniques from a sensible perspective and undecided on whether or not to make the leap, this text may hopefully function a reference for making an knowledgeable determination on whether or not OSDev is for you. Lastly, recall that the biggest OSDev group with essentially the most complete number of sources is over on the OSDev wiki, so do give {that a} look should you determine to embark in your journey.

Keep tuned for extra articles by @donaldsebleung 😉

The Article was Inspired from tech community site.
Contact us if this is inspired from your article and we will give you credit for it for serving the community.

This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 10k Tech related traffic daily !!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?