can python do some kernel stuff?

Grant Edwards grante at visi.com
Wed Jun 4 18:24:11 CEST 2008


On 2008-06-04, Ivan Illarionov <ivan.illarionov at gmail.com> wrote:

>> Didn't somebody once demonstrate how to put a VM into kernel
>> space so that you could write kernel code in Python?  Maybe it
>> was just a discussion about how it could be done in theory.
>> 
>> There have been a few JVM-in-hardware projects, so I suppose
>> you could use Jython to write kernel code for those machines.
>
> I can't understand why somebody might want to do kernel stuff
> in Python.

    we choose to put Python in kernel-space and do the other
    things, not because they are easy, but because they are
    hard, because that goal will serve to organize and measure
    the best of our energies and skills...

;)
    
> It's a *high level language*. It's for high level stuff.

There is a lot of high-level stuff in the kernel.  Performance
expectations and resource availability don't make python
and the PVM a very practical choice.

> OTOH Python can be easily extended to get as close to the kernel as 
> anyone may ever need.
>
> I decided to change my "hello world" post to use Linux system call 
> instead of printf. 
>
> #include <Python.h>
>
> PyObject* 
> hello(PyObject* self)
> {
> #if defined(__GNUC__) && defined(__i386__) && defined(__linux__)
>     const char * hello_str = "Hello world (Linux system call)!\n";
>     int hello_len = 33;
>     asm __volatile__(
>         "push   %%ebx;"
>         "movl   $4,  %%eax;" /* The system call for write (sys_write) */
>         "movl   $1,  %%ebx;" /* File descriptor 1 - standard output */
>         "int    $0x80;"
>         "pop    %%ebx;"
>         : /* no outputs */
>         : "c" (hello_str), "d" (hello_len) /* input */
>         );
> #else
>     printf("Hello world!\n");
> #endif
>     Py_RETURN_NONE;
> }
>
> static PyMethodDef functions[] = {
>     {"hello",    (PyCFunction)hello, METH_NOARGS},
>     {NULL, NULL, 0, NULL},
> };
>
> DL_EXPORT(void)
> init_hello(void)
> {
>     Py_InitModule("_hello", functions);
> }
>
> Python 2.5.2 (r252:60911, Apr 21 2008, 11:12:42) 
> [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> from _hello import hello
>>>> hello()
> Hello world (Linux system call)!
>
> I'm in the mood to write "Hello world" programs today ;)

That's cool.  A generic Linux syscall wrapper for Python
wouldn't be hard to do.  Though I'm not sure what the practical
uses would be.

-- 
Grant Edwards                   grante             Yow! OVER the underpass!
                                  at               UNDER the overpass!
                               visi.com            Around the FUTURE and
                                                   BEYOND REPAIR!!



More information about the Python-list mailing list