Subject: Rebuilding the .95a kernel
From: [email-masked] (I Reid)
Date: 11 Apr 92 00:56:07 GMT

Over the past few days I have been trying to answer a number of
questions about building ps0.95 so I decided to write what I hope will
be the definitive answer to 2 questions:

1. How do I rebuild the linux 0.95a kernel
2. How do I compile and install ps (095 version)

As usual, I welcome all and any comments to [email-masked] and I
will try to reply to them if the message warrants it.

============================================================================
1. Rebuilding the 0.95a kernel
==============================

Full source code for the linux kernel is available at the archive sites
and it is useful to know how to rebuild the kernel from this code. This
is very easy to do.

You will need - the kernel source for the latest version (0.95a)
              - a C compiler (Gcc 1.40 (newgcc in the archives) or gcc 2.0)
              - 1.5 - 2 Mb of free disk space

The standard place for the linux source tree is in /usr/src/linux so
if you don't have a /usr/src directory you will need to create one
with mkdir. Once you have done this move the tar file of the kernel
source (probably called linux-0.95a.tar) to /usr/src and type

tar -xvf linux-0.95a.tar

You should see a whole load of file names flashing by on the screen.
This is good. When tar finishes you should have a directory called
/usr/src/linux and this will have a number of subdirectories. You will
probably want to hang on to the original tar in case you want an
original source tree but you can compress it up if you like.... i.e.
you don't need it again for this.

If you are using gcc 1.40 you will have to make two two byte changes
to get the source to compile properly. In the files linux/kernel/Makefile
and linux/kernel/chr_drv/Makefile you will find a bit that looks like this
quite near the top

# gcc2 doesn't have these:
# GCC_OPT = -fcombine-regs

Gcc2 may not have these but gcc 1.40 does and it will die saying
something like this

gcc -Wall -O -fstrength-reduce -fomit-frame-pointer   -finline-functions -nostdi
nc -I../include  -c -o fork.o fork.c
Program  got fatal signal 143.
*** Error code 1
 
Stop.
*** Error code 1
 
Stop.
 
if you try to compile it. The way to fix this is to uncomment a line i.e.

# GCC_OPT = -fcombine-regs 

becomes

GCC_OPT = -fcombine-regs

The other changes you might want to make are in 

============================================================================
1. linux/kernel/blk_drv/hd.c where there is the line

static int reset = 1;

If you have a lot of problems with the HD timeout message then change
this to read

static int reset = 0;

This is a TEMPORARY hack so don't forget about it! Do NOT do this
before mkfs'ing your filesystem 'cos errors won't be handled properly
with this change (I'm told).
============================================================================

============================================================================
2. linux/kernel/chr_drv/keyboard.S where there is a bit that says

/* KBD_FINNISH for Finnish keyboards
 * KBD_US for US-type
 * KBD_GR for German keyboards
 * KBD_FR for Frech keyboard
 * KBD_UK for British extended keyboard
 * KBD_DK for Danish keyboard
 */
#define KBD_FINNISH

you should change this to whatever is appropriate for you e.g.

#define KBD_UK for me
============================================================================

============================================================================
3. linux/Makefile where there is a bit that says

# ROOT_DEV specifies the default root-device when making the image.
# This can be either FLOPPY, /dev/xxxx or empty, in which case the
# default of FLOPPY is used by 'build'.
#
ROOT_DEV=/dev/hdb1

Change this to be whatever your root device is (the device you did
mkfs -c for). For me this would be /dev/hda2 'cos I have a dos
partition on /dev/hda1
============================================================================

Ok, now cd to /usr/src/linux and type

make all

If your compiler is set up properly you should see many lines like

gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions
 -nostdinc -I../include  -c -o fork.o fork.c

(with different source file names obviously). This should continue for
quite a while (7-8 minutes on my heavily cached 386-33) so time for a
quick cup of tea and a chat with your flatmates.

N.b. don't worry if it seems to hang while compiling floppy.c.... give
it a few seconds and it should burst back into life.... it's thinking :-)

If it gets all the way through there will be a file called Image
created in /usr/src/linux. This is the new bootimage and you can put
it onto a floppy in /dev/PS0 (a 3.5" 1.44Mb floppy on my machine) by
typing

dd bs=8192 if=Image of=/dev/PS0

You can do this automatically by typing make disk instead of make all
(above) if you wish.

Congratulations! You have just rebuilt the linux kernel and created a
new boot floppy. Time to sync, reboot your machine and test it. I
hope you didn't overwrite your original bootimage; 'cos if you did then
what are going to use to boot up if the new bootimage is faulty?? Ah,
the value of forward planning :-)

/***********************************************************/

2. How to compile and install the ps and free commands in Linux v0.95a
===================================================================

You will need: - source for the 0.95a kernel
               - source for ps
               - patch
               
I'm assuming that you are happy rebuilding the kernel (as above) 'cos
you need to be able to do this to install ps.

Take the tar file containg the ps source (say ps.tar) and place it in
/usr/src/linux (or wherever you keep your kernel source. Type tar xvf
ps.tar (or whatever) to create a directory ps (within /usr/src/linux)
to contain the ps source and kernel patches.

Read the file /usr/src/linux/ps/README (especially the bit marked
installation). Cd to /usr/src/linux and type the following

patch -p0 <ps/patches >patch.result 2>&1

Now have a look at the file patch.result in the current directory.
Most of it should indicate success, but at the end is a bit that reads

Hmm...  The next patch looks like a new-style context diff to me...
The text leading up to this was:
==========================
|*** mm/swap.c.Org      Fri Mar  6 16:43:18 1992
|--- mm/swap.c  Tue Mar 10 19:03:19 1992
==========================
Patching file mm/swap.c using Plan A...
Hunk #1 failed at 156.
Hunk #2 succeeded at 173 with fuzz 1 (offset -3 lines).
Hunk #3 failed at 182.
2 out of 3 hunks failed--saving rejects to mm/swap.c#

Make a note of these details and cd to /usr/src/linux/mm. There you
will find a file called swap.c# where the rejects have been saved. It
looks like this

===================================================================
***************
*** 156,160 ****
        static int page_entry = -1;
        int counter = VM_PAGES;
!       int pg_table = 0;
  
  repeat:
--- 156,160 ----
        static int page_entry = -1;
        int counter = VM_PAGES;
!       int pg_table = 0, d_entry;
  
  repeat:
***************
*** 181,186 ****
                        goto repeat;
                }
!               if (try_to_swap_out(page_entry + (unsigned long *) pg_table))
                        return 1;
        }
        printk("Out of swap-memory\n\r");
--- 182,189 ----
                        goto repeat;
                }
!               if (try_to_swap_out(page_entry + (unsigned long *) pg_table)) {
!                       --task[d_entry >> 4]->rss;
                        return 1;
+               }
        }
        printk("Out of swap-memory\n\r");
===================================================================

Taking the first patch first. This means that patch tried to
find lines 156-160 in swap.c (which it thinks look like this)

        static int page_entry = -1;
        int counter = VM_PAGES;
!       int pg_table = 0;
  
  repeat:

and replace them with

        static int page_entry = -1;
        int counter = VM_PAGES;
!       int pg_table = 0, d_entry;
  
  repeat:

Notice the exclamation mark indicating the changed line. What you
need to is open swap.c with an editor, find the line with int pg_table
= 0; (line 158 in my unaltered copy) and add the d_entry bit.

The second one is more slightly more tricky. Patch thinks lines
181-186 look like this

                        goto repeat;
                }
!               if (try_to_swap_out(page_entry + (unsigned long *) pg_table))
                        return 1;
        }
        printk("Out of swap-memory\n\r");

Unfortunately they don't so you need to find this bit yourself (look
at line 187 for a match for the exclamation marked line above) and
then edit it to incorporate the patch (below)

                        goto repeat;
                }
!               if (try_to_swap_out(page_entry + (unsigned long *) pg_table)) {
!                       --task[d_entry >> 4]->rss;
                        return 1;
+               }
        }
        printk("Out of swap-memory\n\r");

In this case you need to alter lines 187-189 to read

        if (try_to_swap_out(page_entry + (unsigned long *) pg_table)) {
                --task[d_entry >> 4]->rss;
                return 1;
        }

instead of 

        if (try_to_swap_out(page_entry + (unsigned long *) pg_table))
                return 1;

and ignore the goto repeat; line 'cos it's not in the original.

Can you see the sense in this? It's really pretty easy once you get
the hang of it.

Ok, you've patched in all the patches (some automatically and some by
hand. Now cd /usr/src/linux and type

make clean
make all

This should remove any out of date files and build a new bootimage.

You should sync and reboot with the new bootimage to make sure that
everything is ok. This is exactly the same as above.

NB: You must reboot and be running the new kernel for the rest of this
to work properly.

Next, having rebooted and satisfied yourself that everything is
alright you type cd /usr/src/linux/ps and type

make ps

If it complains about not finding cc then add a line saying CC = gcc to
the start of the makefile or create a link to gcc and call it cc i.e.

ln -s /usr/bin/gcc /usr/bin/cc

You will need to edit the makefile if your linux source is not
in/usr/src/linux- see ps/README for details.

Last, but not least.... type

ps U /usr/src/linux/tools/system

to build the ps-database.

"Every time you boot a new kernel you have to do a 'ps U' to update
the psdatabase, after doing this you can remove the system file or
do a make clean.

The pathname to the system binary is stored in the psdatabase, so
you only have to specify it if you have moved your source tree or
if you are creating the psdatabase for the first time. The psdatabase
is always '/etc/psdatabase'.

You can install ps suid root or sgid kmem, read access to /dev/kmem
and /dev/mem is needed." - quoted from ps/README

Where you install is up to you.... I run single user so I can do what
I like but I think the recommended place is /usr/bin.

Hope this clears everything up.

Iain
============================================================


1,,
To: [email-masked]
Subject: [[email-masked]: Compiling kerenel, installing ps: a solution]
To: [email-masked]
From: [email-masked] (I Reid)
Subject: Compiling kerenel, installing ps: a solution
Date: 28 Mar 92 21:05:48 GMT

*** EOOH ***
To: [email-masked]
Subject: [[email-masked]: Compiling kerenel, installing ps: a solution]
To: [email-masked]
From: [email-masked] (I Reid)
Subject: Compiling kerenel, installing ps: a solution
Date: 28 Mar 92 21:05:48 GMT

Over the past few days I have been trying to answer a number of
questions about building ps0.95 so I decided to write what I hope will
be the definitive answer to 2 questions:

1. How do I rebuild the linux 0.95a kernel
2. How do I compile and install ps (095 version)

As usual, I welcome all and any comments to [email-masked] and I
will try to reply to them if the message warrants it.

============================================================================
1. Rebuilding the 0.95a kernel
==============================

Full source code for the linux kernel is available at the archive sites
and it is useful to know how to rebuild the kernel from this code. This
is very easy to do.

You will need - the kernel source for the latest version (0.95a)
              - a C compiler (Gcc 1.40 (newgcc in the archives) or gcc 2.0)
              - 1.5 - 2 Mb of free disk space

[Note: I have downloaded gcc 2.0 but haven't installed it yet ('cos I
found my data transfer rate was 304 Kb/sec for my hard drive and
decided to investigate. I have now poked it up to a more healthy 742
Kb/sec but the cure involved reformatting, forgetting to backup
something important etc etc, reinstalling, fixing things etc etc). I
know that all this works for gcc 1.40. If 2.0 has problems anywhere
then please let me know.]

The standard place for the linux source tree is in /usr/src/linux so
if you don't have a /usr/src directory you will need to create one
with mkdir. Once you have done this move the tar file of the kernel
source (probably called linux-0.95a.tar) to /usr/src and type

tar -xvf linux-0.95a.tar

You should see a whole load of file names flashing by on the screen.
This is good. When tar finishes you should have a directory called
/usr/src/linux and this will have a number of subdirectories. You will
probably want to hang on to the original tar in case you want an
original source tree but you can compress it up if you like.... i.e.
you don't need it again for this.

If you are using gcc 1.40 you will have to make two two byte changes
to get the source to compile properly. In the files linux/kernel/Makefile
and linux/kernel/chr_drv/Makefile you will find a bit that looks like this
quite near the top

# gcc2 doesn't have these:
# GCC_OPT = -fcombine-regs

Gcc2 may not have these but gcc 1.40 does and it will die saying
something like this

gcc -Wall -O -fstrength-reduce -fomit-frame-pointer   -finline-functions -nostdi
nc -I../include  -c -o fork.o fork.c
Program  got fatal signal 143.
*** Error code 1
 
Stop.
*** Error code 1
 
Stop.
 
if you try to compile it. The way to fix this is to uncomment a line i.e.

# GCC_OPT = -fcombine-regs 

becomes

GCC_OPT = -fcombine-regs

The other changes you might want to make are in 

============================================================================
1. linux/kernel/blk_drv/hd.c where there is the line

static int reset = 1;

If you have a lot of problems with the HD timeout message then change
this to read

static int reset = 0;

This is a TEMPORARY hack so don't forget about it! Do NOT do this
before mkfs'ing your filesystem 'cos errors won't be handled properly
with this change (I'm told).
============================================================================

============================================================================
2. linux/kernel/chr_drv/keyboard.S where there is a bit that says

/* KBD_FINNISH for Finnish keyboards
 * KBD_US for US-type
 * KBD_GR for German keyboards
 * KBD_FR for Frech keyboard
 * KBD_UK for British extended keyboard
 * KBD_DK for Danish keyboard
 */
#define KBD_FINNISH

you should change this to whatever is appropriate for you e.g.

#define KBD_UK for me
============================================================================

============================================================================
3. linux/Makefile where there is a bit that says

# ROOT_DEV specifies the default root-device when making the image.
# This can be either FLOPPY, /dev/xxxx or empty, in which case the
# default of FLOPPY is used by 'build'.
#
ROOT_DEV=/dev/hdb1

Change this to be whatever your root device is (the device you did
mkfs -c for). For me this would be /dev/hda2 'cos I have a dos
partition on /dev/hda1
============================================================================

Ok, now cd to /usr/src/linux and type

make all

If your compiler is set up properly you should see many lines like

gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions
 -nostdinc -I../include  -c -o fork.o fork.c

(with different source file names obviously). This should continue for
quite a while (7-8 minutes on my cached 386-33) so time for a quick cup of
tea and a chat with your flatmates.

N.b. don't worry if it seems to hang while compiling floppy.c.... give
it a few seconds and it should burst back into life.... it's thinking :-)

If it gets all the way through there will be a file called Image
created in /usr/src/linux. This is the new bootimage and you can put
it onto a floppy in /dev/PS0 (a 3.5" 1.44Mb floppy on my machine) by
typing

dd bs=8192 if=Image of=/dev/PS0

You can do this automatically by typing make disk instead of make all
(above) if you wish.

Congratulations! You have just rebuilt the linux kernel and created a
new boot floppy. Time to sync, reboot your machine and test it. I
hope you didn't overwrite your original bootimage; 'cos if you did then
what are going to use to boot up if the new bootimage is faulty?? Ah,
the value of forward planning :-)

/***********************************************************/

2. How to compile and install the ps and free commands in Linux v0.95a
===================================================================

You will need: - source for the 0.95a kernel
               - source for ps
               - patch
               
I'm assumning that you are happy rebuilding the kernel (as above) 'cos
you need to be able to do this to install ps.

Take the tar file containg the ps source (say ps.tar) and place it in
/usr/src/linux (or wherever you keep your kernel source. Type tar xvf
ps.tar (or whatever) to create a directory ps (within /usr/src/linux)
to contain the ps source and kernel patches.

Read the file /usr/src/linux/ps/README (especially the bit marked
installation). Cd to /usr/src/linux and type the following

patch -p0 <ps/patches >patch.result 2>&1

Now have a look at the file patch.result in the current directory.
Most of it should indicate success, but at the end is a bit that reads

Hmm...  The next patch looks like a new-style context diff to me...
The text leading up to this was:
==========================
|*** mm/swap.c.Org      Fri Mar  6 16:43:18 1992
|--- mm/swap.c  Tue Mar 10 19:03:19 1992
==========================
Patching file mm/swap.c using Plan A...
Hunk #1 failed at 156.
Hunk #2 succeeded at 173 with fuzz 1 (offset -3 lines).
Hunk #3 failed at 182.
2 out of 3 hunks failed--saving rejects to mm/swap.c#

Make a note of these details and cd to /usr/src/linux/mm. There you
will find a file called swap.c# where the rejects have been saved. It
looks like this

===================================================================
***************
*** 156,160 ****
        static int page_entry = -1;
        int counter = VM_PAGES;
!       int pg_table = 0;
  
  repeat:
--- 156,160 ----
        static int page_entry = -1;
        int counter = VM_PAGES;
!       int pg_table = 0, d_entry;
  
  repeat:
***************
*** 181,186 ****
                        goto repeat;
                }
!               if (try_to_swap_out(page_entry + (unsigned long *) pg_table))
                        return 1;
        }
        printk("Out of swap-memory\n\r");
--- 182,189 ----
                        goto repeat;
                }
!               if (try_to_swap_out(page_entry + (unsigned long *) pg_table)) {
!                       --task[d_entry >> 4]->rss;
                        return 1;
+               }
        }
        printk("Out of swap-memory\n\r");
===================================================================

Taking the first patch first. This means that patch tried to
find lines 156-160 in swap.c (which it thinks look like this)

        static int page_entry = -1;
        int counter = VM_PAGES;
!       int pg_table = 0;
  
  repeat:

and replace them with

        static int page_entry = -1;
        int counter = VM_PAGES;
!       int pg_table = 0, d_entry;
  
  repeat:

Notice the exclamation mark indicating the changed line. What you
need to is open swap.c with an editor, find the line with int pg_table
= 0; (line 158 in my unaltered copy) and add the d_entry bit.

The second one is more slightly more tricky. Patch thinks lines
181-186 look like this

                        goto repeat;
                }
!               if (try_to_swap_out(page_entry + (unsigned long *) pg_table))
                        return 1;
        }
        printk("Out of swap-memory\n\r");

Unfortunately they don't so you need to find this bit yourself (look
at line 187 for a match for the exclamation marked line above) and
then edit it to incorporate the patch (below)

                        goto repeat;
                }
!               if (try_to_swap_out(page_entry + (unsigned long *) pg_table)) {
!                       --task[d_entry >> 4]->rss;
                        return 1;
+               }
        }
        printk("Out of swap-memory\n\r");

In this case you need to alter lines 187-189 to read

        if (try_to_swap_out(page_entry + (unsigned long *) pg_table)) {
                --task[d_entry >> 4]->rss;
                return 1;
        }

instead of 

        if (try_to_swap_out(page_entry + (unsigned long *) pg_table))
                return 1;

and ignore the goto repeat; line 'cos it's not in the original.

Can you see the sense in this? It's really pretty easy once you get
the hang of it.

Ok, you've patched in all the patches (some automatically and some by
hand. Now cd /usr/src/linux and type

make clean all

This should remove any out of date files and build a new bootimage.

You should sync and reboot with the new bootimage to make sure that
everything is ok. This is execatly the same as above.

Next, having rebooted and satisfied yourself that everything is
alright you type cd /usr/src/linux/ps and type

NB: You must reboot and be running the new kernel for the rest of this
to work properly.

make ps

If it complains about not finding cc then add a line saying CC = gcc to
the start of the makefile. You will need to edit the makefile if your
linux source is not in /usr/src/linux- see ps/README for details.

Last, but not least.... type

ps U /usr/src/linux/tools/system

to build the ps-database.

"Every time you boot a new kernel you have to do a 'ps U' to update
the psdatabase, after doing this you can remove the system file or
do a make clean.

The pathname to the system binary is stored in the psdatabase, so
you only have to specify it if you have moved your source tree or
if you are creating the psdatabase for the first time. The psdatabase
is always '/etc/psdatabase'.

You can install ps suid root or sgid kmem, read access to /dev/kmem
and /dev/mem is needed." - quoted from ps/README

Where you install is up to you.... I run single user so I can do what
I like but I think the recommended place is /usr/bin.

Hope this clears everything up.

Iain