In-Kernel Daemon insertion tools

I've got my 3-stack Forth-like language running in kernelspace, in the
manner of a kernel debugger I guess.  H3sm, Hohensee's 3-stack machine,
isn't Linux kernel code per se, but the stunts involved in getting it in
there are, and aren't specific to H3sm.

The version of H3sm used as a kernel thread is written entirely in gas/m4,
(and could be converted to build with my shasm Bash script assembler in a
matter of days/weeks, BTW) so it needs a wrapper to slow-dance with Linux.
In order to not create a lot of #include dependancy issues I just mimic
kpiod in terms of installing it in the kernel sources, using the shell of
that function as an example/wrapper, and inserting it in that source file
right after kpiod(). That was surprisingly easy. A better knowledge of
kernel code than mine would allow this to be done more maintainably. I had
started a linux/H3sm directory, but the #include issues ate my head.

Then we have namespaces. Linux has gajillions of labels in constant flux,
and H3sm has lots of Linux syscalls in it. Spamming up the H3sm labels is
a must. After getting a dummy H3sm_kthread symbol in System.map in a built
kernel by the above means, an ed script was devised. All labels in the
standalone H3sm were suffixed with "H3sm", so jz cellwisebooleanbla
becomes jz cellwisebooleanblaH3sm, main: became mainH3sm:, and just for
regularity top_loop_of_H3sm became top_loop_of_H3smH3sm. Not what I want
to look at normally, and I won't, unless I'm going into the kernel. Name
conflicts are thus astronomically unlikely. When I first wrote H3sm there
were 0 Google hits for "h3sm". For each H3sm routine that has a name in
the H3sm interpreter there's also a label with a "CFA" suffix, which
should also be orthogonal to the Linux symbol namespace.

Here's the sh/ed script to change labels. This runs on the pre-m4 source.
Tabs were expanded in posting this. Something similar could be run on the
gas phase of C output perhaps.
......................................................................
#
# convert 1.s.m4 to 1.k, m4/gas source for a Linux kernel daemon
#   or kernel thread ala kswapd
# april 2001

ed 1.s.m4 <<HERE
1
/.globl main/s/.globl main/.globl mainH3sm/
1
/.type    main,@f/s/.type    main,@function/.type  mainH3sm,@function/
1
,s/^\([a-zA-Z0-9_]*\):/\1H3sm:/
,s/\$1:/$\1H3sm:/
,s/[    ]*call \([a-zA-Z0-9_]*\)/call \1H3sm/
,s/^[   ]*ELL([         ]*\([a-zA-Z0-9_]*\)/ELL(                \1H3sm/
,s/^[   ]*YES([         ]*\([a-zA-Z0-9_]*\)/YES(                \1H3sm/
,s/^[   ]*NO([  ]*\([a-zA-Z0-9_]*\)/NO(         \1H3sm/
,s/ifnonzero[   ]*\([a-zA-Z0-9_]*\)/ifnonzero           \1H3sm/
,s/ifzero[      ]*\([a-zA-Z0-9_]*\)/ifzero              \1H3sm/
,s/ifnocarry[   ]*\([a-zA-Z0-9_]*\)/ifnocarry           \1H3sm/
,s/ifcarry[     ]*\([a-zA-Z0-9_]*\)/ifcarry             \1H3sm/
,s/jump[        ]*\([a-zA-Z0-9_]*\)/jump                \1H3sm/
w 1.k
q
HERE
....................................................................


















        Linux Kernel Internals

Tigran Aivazian tigran@veritas.com

   Kernel threads are created using kernel_thread() function which
   invokes the clone(2) system call in kernel mode. Kernel threads
   usually have no user address space, i.e. p->mm = NULL, because they
   explicitly do exit_mm(), e.g. via daemonize() function. Kernel threads
   can always access kernel address space directly. They are allocated
   pid numbers in the low range. Running at processor's ring 0 (on x86,
   that is) implies that the kernel threads enjoy all I/O privileges and
   cannot be pre-empted by the scheduler.


# me     hmmmm, cooperative multitasking.



2.5 Wait Queues

   When a process requests the kernel to do something which is currently
   impossible but that may become possible later, the process is put to
   sleep and is woken up when the request is more likely to be satisfied.
   One of the kernel mechanisms used for this is called a 'wait queue'.

   Linux implementation allows wake-on semantics using TASK_EXCLUSIVE
   flag. With waitqueues, you can either use a well-known queue and then
   simply
   sleep_on/sleep_on_timeout/interruptible_sleep_on/interruptible_sleep_o
   n_timeout, or you can define your own waitqueue and use
   add/remove_wait_queue to add and remove yourself from it and
   wake_up/wake_up_interruptible to wake up when needed.


   An example of the first usage of waitqueues is interaction between the
   page allocator (in mm/page_alloc.c:__alloc_pages()) and the kswapd
   kernel daemon (in mm/vmscan.c:kswap()), by means of wait queue
   kswapd_wait, declared in mm/vmscan.c; the kswapd daemon sleeps on this
   queue, and it is woken up whenever the page allocator needs to free up
   some pages.

   An example of autonomous waitqueue usage is interaction between user
   process requesting data via read(2) system call and kernel running in
   the interrupt context to supply the data. An interrupt handler might
   look like (simplified drivers/char/rtc_interrupt()):
       ______________________________________________________________

static DECLARE_WAIT_QUEUE_HEAD(rtc_wait);

void rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
        spin_lock(&rtc_lock);
        rtc_irq_data = CMOS_READ(RTC_INTR_FLAGS);
        spin_unlock(&rtc_lock);
        wake_up_interruptible(&rtc_wait);
}

# me     so I don't think I need this
