Reimplenting Linux Kernel in Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Hobbs

    #16
    Re: Reimplenting Linux Kernel in Python

    Maboroshi <nobody@hotmail .com> wrote:[color=blue]
    > My question has to do with python and linux - I was interested in
    > finding out what it would take to reimplement the Linux Kernel in python
    > basically just taking the source code from linux and rewriting it in
    > python Would this idea make sense to do - if so what would be the
    > benefits of doing this and in what way would this not be a good idea[/color]

    I had once given serious thought to what it would take to write an OS only
    in a high-level language. (Standard ML was my language of choice at the
    time.) The short answer is, no, it can't be done with Python as it currently
    stands.

    First, you would need a Python compiler that can compile Python programs to
    machine code. Otherwise, what would you write your interpreter in? Python?
    What would you write the interpreter's interpreter in? It can't be Python
    all the way down. (Turtles are a different story, though. ;)

    Second, in order to communicate with hardware, you would need modifications
    to the Python language so that you can read and write bytes to specific
    memory locations. You would also need to be able to signal interrupts. Plus,
    you would also need to be able get the exact memory location of functions,
    in order register interrupt handlers.


    -- Michael Hobbs

    Comment

    • Grant Edwards

      #17
      Re: Reimplenting Linux Kernel in Python

      On 2004-10-14, Cliff Wells <clifford.wells @comcast.net> wrote:[color=blue]
      > On Thu, 2004-10-14 at 01:44 +0000, Grant Edwards wrote:[color=green]
      >> On 2004-10-14, Carlos Ribeiro <carribeiro@gma il.com> wrote:
      >>[color=darkred]
      >> >>> Besides that, Linux is Posix compliant and emulates Unix
      >> >>> system calls to a large extend, and that also makes C a
      >> >>> natural choice.
      >> >>
      >> >> Not sure I follow on that point. Any low level language that
      >> >> can deal with a C "struct" and byte-addressible memory would be
      >> >> fine.
      >> >
      >> > Well, Unix itself was written in C,[/color]
      >>
      >> Originally it wasn't, but recent versions are.[/color]
      >
      > 'Recent' meaning since 1974? ;)[/color]

      Yup. I admit that was sub-optimal phrasing. I should have
      said something more like "The first version wasn't but the rest
      were."

      --
      Grant Edwards grante Yow! Did you find a
      at DIGITAL WATCH in YOUR box
      visi.com of VELVEETA??

      Comment

      • Grant Edwards

        #18
        Re: Reimplenting Linux Kernel in Python

        On 2004-10-14, Michael Hobbs <mike@hobbshous e.org> wrote:
        [color=blue][color=green]
        >> My question has to do with python and linux - I was interested in
        >> finding out what it would take to reimplement the Linux Kernel in python
        >> basically just taking the source code from linux and rewriting it in
        >> python Would this idea make sense to do - if so what would be the
        >> benefits of doing this and in what way would this not be a good idea[/color]
        >
        > I had once given serious thought to what it would take to write an OS only
        > in a high-level language. (Standard ML was my language of choice at the
        > time.) The short answer is, no, it can't be done with Python as it currently
        > stands.
        >
        > First, you would need a Python compiler that can compile Python programs to
        > machine code. Otherwise, what would you write your interpreter in? Python?[/color]

        Silicon. ;)

        --
        Grant Edwards grante Yow! Vote for ME
        at -- I'm well-tapered,
        visi.com half-cocked, ill-conceived
        and TAX-DEFERRED!

        Comment

        • Jeremy Bowers

          #19
          Re: Reimplenting Linux Kernel in Python

          On Thu, 14 Oct 2004 15:32:49 +0000, Grant Edwards wrote:
          [color=blue]
          > On 2004-10-14, Michael Hobbs <mike@hobbshous e.org> wrote:[color=green]
          >> First, you would need a Python compiler that can compile Python programs to
          >> machine code. Otherwise, what would you write your interpreter in? Python?[/color]
          >
          > Silicon. ;)[/color]

          "Forgive him, Father Intel, for he knows not for what he asks."

          (OK, I think you do, but I couldn't resist. :-) )


          Comment

          • Carlos Ribeiro

            #20
            Re: Reimplenting Linux Kernel in Python

            On 14 Oct 2004 01:44:29 GMT, Grant Edwards <grante@visi.co m> wrote:[color=blue][color=green]
            > > This does not meant that other languages cant be used; it only
            > > means that C is a natural choice.[/color]
            >
            > I still don't see why.
            >[color=green]
            > > Of course, you can provide a C-like interface for a Python program
            > > (for example). But a OS written in pure Python would make a much
            > > better use of the language if it provides a Pythonic interface. I
            > > believe we can agree on this point, dont you think?[/color]
            >
            > Not really -- the interace between the kernel and user-space
            > isn't particulary C-oriented. IIRC on the x86 platform, you
            > make a system call by loading values into registers and
            > executing an interrupt instruction. You can't even make a
            > Linux system call on Linux using standard C. You have to use
            > assembly language wrappers that convert between the system call
            > API and the C API. It would be just as easy to make those
            > assembly language wrappers convert to a Pascal or Python API.[/color]

            Now I see your point. It remembers me a little bit about the
            discussions between GNU/Stallman and Linux/Linus on what makes a OS.
            The kernel is one thing, the OS is another beast. I still think that a
            language such as Python can be used to write the OS -- in the sense
            where the name Linux is usually called upon (even if technically
            incorrect, as the GNU/Stallman camp would quickly point out); it
            includes high level APIs, much above the low-level kernel calls . But
            the kernel itself is the hard part for a high level language.

            As for C being a natural choice as far as the kernel part is
            concerned... it may be because it compiles to efficient code, or
            because it facilitates integration with assembler, or because it can
            efficiently uses registers... Who knows? But the main reason is
            probably simple: it's because everyone is just used to it.

            --
            Carlos Ribeiro
            Consultoria em Projetos
            blog: http://rascunhosrotos.blogspot.com
            blog: http://pythonnotes.blogspot.com
            mail: carribeiro@gmai l.com
            mail: carribeiro@yaho o.com

            Comment

            • Caleb Hattingh

              #21
              Re: Reimplenting Linux Kernel in Python

              That's a very sarcastic reply.

              The question, really, is what the effect would be of writing an OS in a
              HLL (particularly on speed of development, bug-fixes, and feature
              addition).

              I think that is worth asking.

              Granted, as a minimum Python would need to compile to native machine code
              (or interface with a really detailed native virtual machine) but I expect
              (hope!) that is going to get done anyway.


              On Thu, 14 Oct 2004 08:53:54 -0400, Neal D. Becker <ndbecker2@veri zon.net>
              wrote:
              [color=blue]
              > Maboroshi wrote:
              >[color=green]
              >> Hello
              >>
              >> My question has to do with python and linux - I was interested in
              >> finding out what it would take to reimplement the Linux Kernel in python
              >> basically just taking the source code from linux and rewriting it in
              >> python Would this idea make sense to do - if so what would be the
              >> benefits of doing this and in what way would this not be a good idea
              >>
              >> Cheers[/color]
              >
              > Yeah, that's a great idea. Go for it. Let me know when you're done.
              >
              >[/color]

              Comment

              • Grant Edwards

                #22
                Re: Reimplenting Linux Kernel in Python

                On 2004-10-14, Carlos Ribeiro <carribeiro@gma il.com> wrote:
                [color=blue]
                > As for C being a natural choice as far as the kernel part is
                > concerned... it may be because it compiles to efficient code,
                > or because it facilitates integration with assembler, or
                > because it can efficiently uses registers...[/color]

                All of those.
                [color=blue]
                > Who knows? But the main reason is probably simple: it's
                > because everyone is just used to it.[/color]

                Exactly. Any number of "low level", statically typed,
                compiled, languages would work equally as well. Modula-2 (not
                sure about M-3, the GC stuff maybe a bit iffy in a kernel),
                Ada, BLISS, PL/I, Pascal, etc.

                --
                Grant Edwards grante Yow! I feel like I'm
                at in a Toilet Bowl with a
                visi.com thumbtack in my forehead!!

                Comment

                • Mike Meyer

                  #23
                  Re: Reimplenting Linux Kernel in Python

                  mike@hobbshouse .org (Michael Hobbs) writes:
                  [color=blue]
                  > Maboroshi <nobody@hotmail .com> wrote:[color=green]
                  >> My question has to do with python and linux - I was interested in
                  >> finding out what it would take to reimplement the Linux Kernel in python
                  >> basically just taking the source code from linux and rewriting it in
                  >> python Would this idea make sense to do - if so what would be the
                  >> benefits of doing this and in what way would this not be a good idea[/color]
                  > I had once given serious thought to what it would take to write an OS only
                  > in a high-level language. (Standard ML was my language of choice at the
                  > time.) The short answer is, no, it can't be done with Python as it currently
                  > stands.[/color]

                  But it can be done. It *has* been done.
                  [color=blue]
                  > First, you would need a Python compiler that can compile Python programs to
                  > machine code. Otherwise, what would you write your interpreter in? Python?
                  > What would you write the interpreter's interpreter in? It can't be Python
                  > all the way down. (Turtles are a different story, though. ;)[/color]

                  If you've got a Python compiler that generates machine code, you most
                  certainly *can* write your Python interpreter in Python. That's the
                  way most LISP systems are built.
                  [color=blue]
                  > Second, in order to communicate with hardware, you would need modifications
                  > to the Python language so that you can read and write bytes to specific
                  > memory locations. You would also need to be able to signal interrupts. Plus,
                  > you would also need to be able get the exact memory location of functions,
                  > in order register interrupt handlers.[/color]

                  Actually, you don't need those modifications to Python. What you need
                  is a module written in something other than Python that lets you do
                  all those things. All unix systems I'm aware of require code in
                  something other than C to let them do things that C can't do. Nothing
                  wrong about this - it's just the nature of the beast.

                  <mike
                  --
                  Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
                  Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

                  Comment

                  • Michael Hobbs

                    #24
                    Re: Reimplenting Linux Kernel in Python

                    Mike Meyer <mwm@mired.or g> wrote:[color=blue]
                    > mike@hobbshouse .org (Michael Hobbs) writes:[color=green]
                    >> I had once given serious thought to what it would take to write an OS
                    >> only in a high-level language. (Standard ML was my language of choice
                    >> at the time.) The short answer is, no, it can't be done with Python as
                    >> it currently stands.[/color]
                    > But it can be done. It *has* been done.
                    > [snip]
                    > Actually, you don't need those modifications to Python. What you need
                    > is a module written in something other than Python that lets you do
                    > all those things.[/color]

                    Sorry, I should have emphasized the word *only*. I'm talking about writing
                    an OS *only* with Python. No C, no assembly, just Python. Writing an OS
                    without assembly would be impossible unless the compiler can somehow map
                    required processor opcodes to a function or statement in the higher-level
                    language. Python does not currently have a proper compiler, much less
                    functions or statements that would, say, put the processor into protected
                    mode.

                    -- Michael Hobbs

                    Comment

                    • Cliff Wells

                      #25
                      Re: Reimplenting Linux Kernel in Python

                      On Thu, 2004-10-14 at 15:31 +0000, Grant Edwards wrote:[color=blue]
                      > On 2004-10-14, Cliff Wells <clifford.wells @comcast.net> wrote:[color=green]
                      > > On Thu, 2004-10-14 at 01:44 +0000, Grant Edwards wrote:[color=darkred]
                      > >> On 2004-10-14, Carlos Ribeiro <carribeiro@gma il.com> wrote:
                      > >>
                      > >> > Well, Unix itself was written in C,
                      > >>
                      > >> Originally it wasn't, but recent versions are.[/color]
                      > >
                      > > 'Recent' meaning since 1974? ;)[/color]
                      >
                      > Yup. I admit that was sub-optimal phrasing. I should have
                      > said something more like "The first version wasn't but the rest
                      > were."[/color]

                      And I thought you were just showing your age ;)

                      --
                      Cliff Wells <clifford.wells @comcast.net>

                      Comment

                      • Grant Edwards

                        #26
                        Re: Reimplenting Linux Kernel in Python

                        On 2004-10-15, Cliff Wells <clifford.wells @comcast.net> wrote:
                        [color=blue][color=green][color=darkred]
                        >>>> Originally it wasn't, but recent versions are.
                        >>>
                        >>> 'Recent' meaning since 1974? ;)[/color]
                        >>
                        >> Yup. I admit that was sub-optimal phrasing. I should have
                        >> said something more like "The first version wasn't but the rest
                        >> were."[/color]
                        >
                        > And I thought you were just showing your age ;)[/color]

                        Nah, I'm not quite that old -- my Unix experiece only goes back
                        20 years to the days of v7 on a PDP11.

                        --
                        Grant Edwards grante Yow! I Know A Joke!!
                        at
                        visi.com

                        Comment

                        • Bengt Richter

                          #27
                          Re: Reimplenting Linux Kernel in Python

                          On Fri, 15 Oct 2004 00:37:39 -0000, mike@hobbshouse .org (Michael Hobbs) wrote:
                          [color=blue]
                          >Mike Meyer <mwm@mired.or g> wrote:[color=green]
                          >> mike@hobbshouse .org (Michael Hobbs) writes:[color=darkred]
                          >>> I had once given serious thought to what it would take to write an OS
                          >>> only in a high-level language. (Standard ML was my language of choice
                          >>> at the time.) The short answer is, no, it can't be done with Python as
                          >>> it currently stands.[/color]
                          >> But it can be done. It *has* been done.
                          >> [snip]
                          >> Actually, you don't need those modifications to Python. What you need
                          >> is a module written in something other than Python that lets you do
                          >> all those things.[/color]
                          >
                          >Sorry, I should have emphasized the word *only*. I'm talking about writing
                          >an OS *only* with Python. No C, no assembly, just Python. Writing an OS
                          >without assembly would be impossible unless the compiler can somehow map
                          >required processor opcodes to a function or statement in the higher-level
                          >language. Python does not currently have a proper compiler, much less
                          >functions or statements that would, say, put the processor into protected
                          >mode.
                          >[/color]
                          OTOH, it's a bootstrapping process. You can start by putting together
                          a python string that contains an image of the boot block on a bootable floppy,
                          and with suitable permissions you can write it to an actual floppy.

                          Next you can create a python string that is the image of a boot loader, that
                          the boot block will know where to find and read into memory using BIOS, if you
                          put it in a certain place on the floppy. And then you can get the loader to load
                          your OS image, which is a big bunch of bytes. You might want to structure the floppy
                          info for access as a primitive (non-patent-infringing ;-/) file system.
                          Maybe you will want to do a bootable CD instead ;-)

                          Anyway, CPU instructions are just bytes. As a start, it might be interesting to try to build
                          a dll extension in an mmap array and import it (be prepared for lots of crashes ;-).

                          Obviously mmap etc depend on the OS, but if you had prepared a suitable bootable OS
                          with a primitive file system and mmap functionality, and a userland python VM, ... well,
                          it's involved, but you can get there. I'd bet pypy will eventually be at the center of
                          something like that, with maybe a monolithic boot image in flash, and BIOS to boot it.
                          At some point, on a suitable platform, it will be able to self-host its own development, I'd bet.

                          Regards,
                          Bengt Richter

                          Comment

                          • Alex Martelli

                            #28
                            Re: Reimplenting Linux Kernel in Python

                            Phil Frost <indigo@bitglue .com> wrote:
                            ...[color=blue]
                            > I do agree though that there is little point in rewriting Linux in
                            > Python. The advantage in Python is its expressiveness, but by rewriting
                            > an existing system you gain nothing.[/color]

                            By rewriting any existing system in a higher-level language, you may
                            gain the benefits of higher-level languages -- more concise and readable
                            expression, enabling maintenance, modification and enrichment.

                            This is certainly the advantage we're after with the pypy project, which
                            is based on rewriting the Python interpreter in Python. Google for
                            pypy, you'll find lots of materials about it on the net. If we can
                            shrink the interpreter and built-in modules from about 350K lines of
                            code (based on a rough find/xargs cat/wc on the 2.4 release) down to
                            about 30k (and we're on target for that -- pypy isn't anywhere like
                            ready, but already does a lot), the benefits in maintenance, further
                            future modification, enabling experimentation s, etc, should follow.

                            Re Linux, we just got a fascinating talk at CodeJam Italia, last week,
                            about the new virtual memory twists that are likely going to be in
                            kernel 2.6.9. Part of it was this Indian mathematician appearing out of
                            nowhere with a wonderful new algorithm designed exactly to solve some
                            subtle problems in the new ("pagetable-less", pardon the
                            oversimplificat ion!-) architecture for mmap on huge-memory machines.
                            This is wonderful, BUT, if the source could shrink by a factor of 10 or
                            more, making them hugely more accessible, think of how much more such
                            useful, nay, precious, contributions, that might elicit -- and how much
                            easier it would be for contributors to experiment and play around with a
                            huge variety of ideas...

                            The obvious problem might be performance. For that, all of us in the
                            pypy project (except the few others who can actually follow what's going
                            on;-) rely on wizard Armin Rigo, of psyco fame -- the idea is that the
                            pypy framework will "easily" (ha!-) generate machine code (on the fly or
                            in advance) for the (runtime-restricted) subset of Python which is used
                            in "interprete r level"... leading in the end to a _faster_ virtual
                            machine, wrt current pure-C Python, one competitive with CPython+psyco
                            or even beating it (thanks to advances in "symbolic execution" and type
                            annotation which are already enabled by using a higher-level language,
                            rather than C, for the interpreter/VM/execution framework).

                            pypy still needs to be completed, and proven in the field. But I'm
                            offering it as an example of the potential benefits of rewriting
                            existing software in higher-level languages...


                            Alex

                            Comment

                            • Mike Meyer

                              #29
                              Re: Reimplenting Linux Kernel in Python

                              Grant Edwards <grante@visi.co m> writes:[color=blue]
                              > Exactly. Any number of "low level", statically typed,
                              > compiled, languages would work equally as well. Modula-2 (not
                              > sure about M-3, the GC stuff maybe a bit iffy in a kernel),
                              > Ada, BLISS, PL/I, Pascal, etc.[/color]

                              Haven't operating systems been written in most of those languages? For
                              a sufficiently loose definition of OS, of course.

                              <mike
                              --
                              Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
                              Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

                              Comment

                              • Grant Edwards

                                #30
                                Re: Reimplenting Linux Kernel in Python

                                On 2004-10-15, Mike Meyer <mwm@mired.or g> wrote:[color=blue]
                                > Grant Edwards <grante@visi.co m> writes:[color=green]
                                >> Exactly. Any number of "low level", statically typed,
                                >> compiled, languages would work equally as well. Modula-2 (not
                                >> sure about M-3, the GC stuff maybe a bit iffy in a kernel),
                                >> Ada, BLISS, PL/I, Pascal, etc.[/color]
                                >
                                > Haven't operating systems been written in most of those languages?[/color]

                                Sure. They weren't Unix, but a kernel's a kernel. :)
                                [color=blue]
                                > For a sufficiently loose definition of OS, of course.[/color]

                                VMS was mostly in BLISS, I believe. I think MacOS (and maybe
                                parts of MS-Windows way back when) were written in Pascal. I'm
                                sure IBM must have written at least one OS in PL/I (whether it
                                escaped into the wild or not, I don't know). I _think_ Intel
                                wrote iRMX in PL/M (a PL/I derivitive). I can't think of any
                                OSes written in Modula-[23] off the top of my head.

                                --
                                Grant Edwards grante Yow! My FAVORITE group
                                at is "QUESTION MARK & THE
                                visi.com MYSTERIANS"...

                                Comment

                                Working...