
How to create an OS from scratch – tutorial - zabana
https://github.com/cfenollosa/os-tutorial
======
justinian
I've seen a number of "build an OS" tutorials pop up recently, and that's
awesome! Building an OS will definitely help you understand how to write
better user-space programs, and it's fun.

One thing I wish is that more of them would feature UEFI instead of BIOS, or
at least come with a warning, as Intel plans to drop support for legacy BIOS
in 2020.

Going through UEFI is actually easier anyway, IMHO. ... Maybe I should stop
complaining and write that tutorial myself instead. Hmm.

~~~
Immortalin
If you are writing an OS, I highly recommend the Pure64 bootloader from Return
Infinity. It's much more straightforward to use compared to GRUB which imposes
a lot of conventions on how you structure things.

httpss://github.com/ReturnInfinity/Pure64

~~~
majewsky
Working link:
[https://github.com/ReturnInfinity/Pure64](https://github.com/ReturnInfinity/Pure64)

------
jhinra
“Once you pass college, excessive theory is worse than no theory because it
makes things seem more difficult than they really are.”

About every third minute I’ve changed my mind about this quote. I think that
means it’s a good quote.

~~~
SmellyGeekBoy
As someone from a computer science background who's recently been getting into
electronics in a really big way, this quote really struck a chord with me.

------
jeffreyrogers
Writing an OS is a fun exercise and it makes you appreciate all the effort
that goes into making something like Linux usable and relatively robust and
performant. It's also interesting to look at how Linux (or other open source
OS) implements some of these features once you've tried implementing them on
your own. You'll find that much of Linux is understandable once you know what
problem the code in question is trying to solve.

------
carlesfe
Hi, author here. I’m humbled by the kind words. This is a project I started
when I was in between jobs and unfortunately it’s kind of stalled because I
had difficulties implementing the file system. Really want to get back to it
some time.

Anyway, feel free to leave your comments, I appreciate them a lot.

~~~
LaneRendell
Just shot you an email to ask you to do that! Thanks for this resource. Good
work :)

~~~
carlesfe
Glad you liked it! Thanks for the kind words

------
fokker
These kinds of posts are the reason I come to HN hoping to find. Amazing work!
I'm going to come back and read some chapters over the weekend.

------
da_murvel
I'm currently traversing through Linux From Scratch,
[http://www.linuxfromscratch.org/lfs/](http://www.linuxfromscratch.org/lfs/).
Not necessarily because I wanted to build a Linux OS but because it was the
best guide I could find. But at a first glance this looks promising to me, so
I'd definitely check it out and try out a few "chapters" when time comes.

------
deytempo
I legit thought they wrote an OS in the scratch programming language for a
second lol

------
simula67
Can anyone recommend good resources on writing a compiler from scratch ?

~~~
GeorgeTirebiter
I think the best first book is: James E. Hendrix, The Small-C Handbook, Reston
1984, ISBN 0-8359-7012-4

He explains every detail, goes through every routine, and you can see exactly
what the compiler does. It's 'Small-C' so it doesn't have any fancy features,
like floats or structs. But the basic machinery is there, and one can add
features as desired. It does compile itself. The compiler outputs x86
assembly, which must be assembled and linked with standard programs.

The original Small-C was done by Ron Cain, and produced 8080 code. I like Mr.
Hendrix' better because I have lots of x86 around (I specifically wanted it
for my HP-200 LX palmtop, which I still have and works great!); and because
the book is fantastic.

It's fully understandable in a small quantity of time. And, the back end is
re-targetable, so you could, say, output to ARM or RISC-V or whatever.

I spoke with Mr. Hendrix some years ago, maybe 10 or so, and asked if he would
consider putting the compiler into the public domain. He did.

~~~
bogomipz
Thanks for this suggestion.

In case anyone else is interested, there is a freely available ISO of the the
book available from Dr Dobbs Journal. From the README in the ISO:

>"Welcome to Dr. Dobb's Small-C Resource CD-ROM, the definitive collection of
Small-C related information and source code. This CD-ROM includes the full
text to James Hendrix's book "A Small-C Compiler: Language, Usage, Theory, and
Design," selected articles on Small-C from "Dr. Dobb's Journal" magazine, and
Small-C implementations for a number of processor platforms, including the
8080, Z80, 6502, and others."

It's available here:

[http://www.drdobbs.com/developer-network-small-c-compiler-
bo...](http://www.drdobbs.com/developer-network-small-c-compiler-
book/184415519)

------
3rdAccount
I'll be the guy to mention Forth as it is the only system I know of that a
single person can fully understand and build up a fully functional and REPL
interactive system using just assembly and a microcontroller. Most Forthers
are doing embedded work though and don't need everything that comes with an
OS.

------
bigmit37
Does any one have other resources I can look into as well. I’m still a
relatively new programmer but I really want to get a deeper understanding of
how OSes work. I’m assuming I need to use either C/C++ or Rust? I only Python
and am currently learning C++.

~~~
charlysl
Stanford CS140e - An Experimental Course on Operating Systems [1]: _students
implement a simple, clean operating system (virtual memory, processes, file
system) on a Raspberry Pi 3 in the Rust programming language and use the
result to run a variety of devices_

MIT 6.828 - Operating System Engineering [2]: _This course studies fundamental
design and implementation ideas in the engineering of operating systems.
Lectures are based on a study of UNIX and research papers. Topics include
virtual memory, threads, context switches, kernels, interrupts, system calls,
interprocess communication, coordination, and the interaction between software
and hardware. Individual laboratory assignments involve implementation of a
small operating system in C, with some x86 assembly_

[1]
[https://web.stanford.edu/class/cs140e/about/](https://web.stanford.edu/class/cs140e/about/)

[2]
[https://pdos.csail.mit.edu/6.828/2018/schedule.html](https://pdos.csail.mit.edu/6.828/2018/schedule.html)

~~~
movedx
Thanks for these links. Very useful indeed. I've been looking for something to
get me into C and Rust, so I can learn the lower level stuff, and get me away
from higher level web technologies (which I play with all day, everyday.)

~~~
charlysl
You are welcome.

Please note that the MIT 6.828 is really tough but will turn you into a bad-
ass C hacker.

It relies on MIT 6.004 - Computation Structures [1] (has edx mooc [2,3,4]) and
MIT 6.033 - Computer Systems Engineering [5] as pre-requisites given that they
teach you the fundamentals of computer architecture (digital abstraction,
gates, circuits, processors, assembler, memory, cashes, pipelining, virtual
memory, interrupts/io, buses, what have you) and system design (basically,
managing complexity); they are not programming courses, but I recommend them
if you have the time and interest, they are an awesome and principled way of
learning the core concepts that you really need to have down before dabbling
with OSs.

[1] [https://ocw.mit.edu/courses/electrical-engineering-and-
compu...](https://ocw.mit.edu/courses/electrical-engineering-and-computer-
science/6-004-computation-structures-spring-2009/)

[2]
[https://courses.edx.org/courses/course-v1:MITx+6.004.1x_3+3T...](https://courses.edx.org/courses/course-v1:MITx+6.004.1x_3+3T2016/course/)

[3]
[https://courses.edx.org/courses/course-v1:MITx+6.004.2x+3T20...](https://courses.edx.org/courses/course-v1:MITx+6.004.2x+3T2015/course/)

[4]
[https://courses.edx.org/courses/course-v1:MITx+6.004.3x+2T20...](https://courses.edx.org/courses/course-v1:MITx+6.004.3x+2T2016/course/)

[5] [https://ocw.mit.edu/courses/electrical-engineering-and-
compu...](https://ocw.mit.edu/courses/electrical-engineering-and-computer-
science/6-033-computer-system-engineering-spring-2009/)

~~~
bigmit37
Thank you so much for these resources.

------
giancarlostoro
I've been wanting to do this but with a Pi or any SBC board with Linux from
Scratch, but not sure how I would even approach LFS on a Pi.

~~~
mikestaszel
I'm in extremely early stages of writing up some documentation for how to
build a Linux distro for a Raspberry Pi 3, gauging interest and figuring out
pain points. Shoot me an email [my username]@gmail if you've got ideas or
requests!

~~~
codetrotter
That sounds interesting. I think your guide would be extra helpful if it
focused on building a distro that has as much of the system loaded into memory
as possible to minimize amount of reads from the SD card, and which is
configured with a read-only file system and never even tries to write to the
SD card. That would be very useful. SD card corruption is a pain!

------
LaneRendell
Does anyone else have a good list of similar tutorials? I'm highly interested
in this type of implementation from scratch approach.

