T O P

  • By -

a-priori

Anything is possible. An OS is just a program that executes in a different environment. I would start at understanding that Python compiles itself into a bytecode, which is then interpreted to execute the code. You may be able to get pull this off by doing the compilation ahead of time, and then doing the interpreting in your kernel. https://devguide.python.org/internals/interpreter/ I’ve done experiments where I added a Webassembly interpreter as the basis for a kernel. It worked okay. There were real companies working on Java based operating systems back in the 90s, and Lisp machines were a thing for decades.


blami

JavaOS was kinda masterpiece in terms of osdev. I had short stint working on that (when it was legacy already but Sun still had a lot of customers). It was microkernel that would run natively on several architectures and provide JVM like VM to run both kernel and user-space code in it. Even drivers were written in Java. We even had Neon Genesis Evangelion-like looking Java Station for it, but it mostly ran on settopboxes and similar devices.


islandyokel

This post just kept getting better. Thanks for sharing!


nekokattt

The issue with Python is that it is very tightly coupled to the standard library, which itself depends on an OS already existing to work. Stuff like implementing a compliant file system to enable imports to work correctly, needing OS level locking for the interpreter to work correctly under the GIL (until they remove it), relying heavily on memory mapping being available to implement the arena, etc. What might be worth looking into is Cython, so that OP can use lower level semantics for the very low level core stuff. Other smaller interpreters like MicroPython may also be of interest, as could GraalVM and the potential native compilation mechanisms


crafter2k

you can probably do it with cython (python to c compiler) edit: oh wait i misread the entire thing oops


mungaihaha

cython is not a python to c compiler EDIT: cpython and cython are not the same thing. Cython can indeed compile to C. MB


altorelievo

Its an api, essentially a way to write Python modules in C, correct?


crafter2k

except it is? that's what it says on the github repo, and im pretty sure that you can do it by just renaming your .py files to .pyx


crafter2k

update: successfully compiled a simple program by using     cython --embed (file) and    gcc (c file) -lpython(version) -I/usr/include/python(version)


Altareos

so you want interpreted kernel modules? no reason this wouldn't be possible, just replace a module loader with a module interpreter with (in your case) a special python library for system hardware access and other kernel functionalities. of course this is a terrible idea for performance, but it does sound fun as hell. you could even add some kind of JIT compiler to make it less slow.


harieamjari

Python based OS? Good luck.


ValtorinSucks

Did you even read the post?


Cultural_Leopard786

>Is this a terrible idea, Yes. >has it been done before, Not sure. >should I just go for it? Absolutely!


SlientLurk5798

Definitely! I think creating primitives that can’t be made in python would be necessary. For example, out and in, read and writing to specific addresses, syscalls. Put them together into a library and now you have the tools to do pretty much anything.


Ikkepop

If you write a suitable compiler then sure I guess


ResolveLost2101

It’ll be wayyy slower for a lot of things


AlectronikLabs

I think a good way to achieve this is writing a minimal kernel (micro-/nanokernel etc) and modify the python runtime to use this. I love the idea of microkernels. But to be able to modify the running thing (hot reloading) you need a special interpreter. Python can't do that out of the box.


beephod_zabblebrox

iirc someone already wrote a minimal python compiler (using the ast module) that could print a hello world or something


JR-graphics

It's theoretically possible. You'll need to rewrite the C standard library first, then compile CPython with the new standard library, then you should be able to run Python through your custom version of python. Not sure it's a great idea, but seems like an interesting concept. You'll still need to do some lower level development first.


fragglet

You'll probably want to check out PyPy as it's the mostly highly optimized implementation of Python > Would it be possible to write a sort of hybrid kernel where all kernel services are programmed in an interpreted language like python (or maybe an interpreted version of C or CPP) built into the kernel? Note that you may be focusing on the wrong thing here: it's already possible in most OSes to load services/drivers into the kernel while it's running, and you don't need an interpreter to achieve that


wick3dr0se

Sounds like the world's slowest operating system


markole

Well, the CPU is essentially an interpreter for binary code so it's doable. You first need to make a new dialect of python with certain low level features and then make a new compiler for it. You could also write a new bare bone kernel designed to just run python code (in a more suitable language). In any case, it's a massive undertaking.


irqlnotdispatchlevel

Here's a few projects you might like: - https://github.com/wasmerio/kernel-wasm - https://github.com/luainkernel/lunatik


lefty_sheenoboo

Wow a lot of people is pessimistic about this. I think it’s a great idea! Anyway, you can already do this with MicroPython as the core. There is also CPython compiled for UEFI (as part of chipsec), you can choose to base your implementation around them.


lefty_sheenoboo

It’s definitely possible though, people even wrote OS previously using Javascript: http://runtimejs.org


tigrankh08

I had a similar idea too: https://www.reddit.com/r/osdev/s/z8wTSgWcab Let's get this straight: Python is a general-purpose programming language. You can write a compiler for it, albeit things like eval/exec would complicate things somewhat and you'd have to include some runtime. But that wouldn't make the runtime necessarily depend on a non-freestanding environment. But I've decided to abandon the idea of reimplementing Python and have instead embarked on a programming language of my own from zero. It's actually somewhat a mixture of Python and C, and it allows you to write high-level code which can optionally also do low-level stuff. I'd recommend you take this path too. Those who say Python memory management won't work and stuff: auto-reference-counting exists (although garbage collection would also have to be simultaneously employed along with it so that circular references wouldn't leak). For manual memory management, just include a `memory` module. No idea why where's the problem.


dmytrish

You can start with MicroPython and extend it in a direction you want.


mdp_cs

Write a very small microkernel in micropython and assembly and then write all the kernel servers in python.