|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bootimage.c |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Skip to line: 7050 - 7100 - 7150 - 7200 - 7250 - 7300 - 7350 - 7400 - 7450 - 7500 - 7550 - 7600 - 7650 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| If you have a comment for bootimage.c, please click here. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
bootminix() (line 7647) is invoked by the boot monitor boot
command and (if options are passed to
the kernel) by the boot monitor boot -optsvar
command. bootminix() is the high-level function
that:
1) calls select_image() (line 7589) to find the desired OS image file on disk. 2) calls exec_image() (line 7348); exec_image() loads the OS image and calls minix(), which switches the system to protected mode (if the kernel was compiled for protected mode) and then jumps to the kernel. bootminix() returns when the OS returns to the boot monitor (for example, when the user issues the shutdown command). | ||
|
#define is a preprocessor command. The preprocessor replaces
all occurrences of the first string (in this case, "BIOS") with
the second string (in this case, "1") before compilation.
The string "BIOS" actually never occurs in this file after this #define. It is important to note, however, that bootimage.c is used to create boot, the boot monitor, but is not used to create the edparams utility program. | ||
| The string "nil" is used to increase readability. | ||
| 7006 |
| 7005 #define _POSIX_SOURCE 1 |
|
_POSIX_SOURCE and _MINIX are macros that are used by library routines. In fact, nearly all macros that begin with an
underscore (_) are specific to library routines.
The POSIX standard was created to improve portability between UNIX systems. The 12th paragraph of section 2.6.2, lines 01184-011200, and lines 01241-01245 in Operating Systems describe the _POSIX_SOURCE and _MINIX macros. | ||
|
A function must be either defined or declared in a file before it can
be used. Header files (files ending in .h) make the task of declaring
variables easier.
Before compilation begins, the preprocessor replaces any #include <filename.h> statement with the contents of filename.h. If the filename is enclosed in < and >, the preprocessor searches for the file in the default directory (typically /usr/include) and other directories specified by the -I option of the compiler (see line 7018). If the filename is quoted (see line 7021), the preprocessor looks for the include file in the same directory that the source file is found. (In this case, bootimage.c is the source file and is found in the /usr/src/boot directory.) As an example, strlen() (see line 7622) is declared in the file string.h (see line 7012). string.h is located in the directory /usr/include. Header files, in addition to containing function declarations, also frequently contain #defines. For example, RATIO (see line 7232) is #defined in boot.h. Since boot.h is quoted (see line 7023), the preprocessor searches for boot.h in the same directory as bootimage.c. Indeed, both files are in the /usr/src/boot directory. | ||
|
7008 #include <sys/types.h>
7009 #include <sys/stat.h> 7010 #include <stdlib.h> 7011 #include <limits.h> 7012 #include <string.h> |
| errno is declared in errno.h (see line 00230 in the book) as extern. Memory for a variable can be allocated in only one file (i.e. the variable is "defined") but the variable must be declared as extern in every other file that accesses it. However, memory is not allocated for errno in boothead.s, boot.c, bootimage.c, or rawfs.c. If you understand how memory is allocated for errno, please submit a comment to the site which will be displayed below. | |||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||
|
7014 #include <a.out.h>
7015 #include <minix/config.h> 7016 #include <minix/const.h> 7017 #include <minix/type.h> |
| If a filename is enclosed in < and >, the preprocessor searches for the file in the default directory (typically /usr/include) and other directories specified by the -I option of the compiler. In Makefile, the parent directory (..) is specified by the -I option. Makefile is found in /usr/src/boot; therefore, the parent directory is /usr/src. After the preprocessor unsuccessfully looks for const.h in the /usr/include/kernel directory, the preprocessor looks for (and finds) const.h in the /usr/src/kernel directory. Note that the preprocessor can't find const.h in /usr/src/kernel since this directory doesn't exist. | ||
|
7019 #include <kernel/type.h>
7020 #include <ibm/partition.h> 7021 #include "rawfs.h" 7022 #include "image.h" 7023 #include "boot.h" 7024 |
|
The kernel often uses "clicks" instead of bytes to describe the size
of memory objects. The click shift for the kernel is found at the
beginning of the kernel code (i.e. text) segment (see line 06053 in the
book) and is equal to 8. A size in bytes is shifted 8 bits to the
right to get the size in clicks. For example, 0x0300 bytes is equal
to 0x03 clicks.
The kernel and the boot monitor save space by converting sizes in bytes to sizes in clicks. The text and data sizes in clicks for the kernel, memory manager (mm), file system (fs), and init (plus any other server that is part of the OS image) are patched into the beginning of the kernel data (see line 7182). 2 bytes are used for each size in clicks; if byte sizes were instead used, 2 bytes would not be enough. | ||
| 7026 |
|
Different kernels have different capabilities and different requirements.
The word (2 bytes) at offset FLAGS_OFF (=4; see line 7056) into
the kernel code (see line 06055 in Operating
Systems ) holds the 8 flags that define these capabilities and requirements.
The value of this word is stored in k_flags (on line 7305).
Note that k_flags for the kernel given in the book is 0x2D ( K_I386, K_CHMEM, K_HIGH, and K_RET are set). | ||
| In boothead.s, a switch to protected mode is made if the K_I386 flag is set in k_flags. | ||
| If the kernel flag K_CLAIM is set, memory for the stack or the bss is not allocated to any process except the kernel (see line 7481). | ||
| If the kernel flag K_CHMEM is not set, memory for the stack is not allocated to any of the processes (see lines 7435 and 7497-7503). | ||
| If the K_HIGH flag is set in k_flags, mm, fs, inet (if network support is enabled) and init are loaded into extended memory (>1MB) (see line 7508). Note that your system must have extended memory if you compile a kernel with this flag set. | ||
| Various sizes need to be patched into various locations (see patch_sizes() on line 7160). However, if the kernel flag K_HDR flag is set, this patching is unnecessary and the kernel instead uses the headers to determine the sizes. | ||
| If the K_RET flag is set in k_flags, the shutdown command returns minix to the boot monitor. The return code is in boothead.s. | ||
| The code for generic INT support is in boothead.s. Generic INT support allows the kernel to switch back to real mode to make a bios function call. | ||
| The array mem[] is the memory map (or list). mem[] is initialized in boothead.s and is passed to the kernel as the environment variable memory. | ||
|
The text and data click sizes (see comments for line 7025) for the kernel,
memory manager (mm), file system (fs) and init (plus any other server that
is part of the OS image) are patched into the beginning of the kernel data
(see line 7182). 16*2*2 = 64 bytes are reserved for these sizes on
line 06478 in the book - 2 bytes for each text size and 2 bytes for each
data size. 64 bytes is enough space for the kernel sizes, mm sizes,
fs sizes, init sizes and the sizes for 12 other servers.
Note that the magic number on line 06480 (in the book) is overwritten. exec_image() (line 7348) first checks the magic number (see line 7521) and then patches the sizes (see line 7528). | ||
|
The OS is loaded into memory in the following order:
1) kernel 2) memory manager (mm) 3) file system (fs) 4) inet (if network support is enabled) 5) init | ||
| 7043 |
| struct process describes the layout of a process in memory. process[] (see line 7050) holds the memory layout for each process in the OS image file. process[] is populated by exec_image() (see lines 7448, 7450, 7458, 7463, 7465, and 7506) and then the values from process[] are used to patch sizes into the kernel data section (see patch_sizes() on line7160). | ||
| entry is the offset of the first instruction to be executed in an executable. For example, process[KERNEL].entry is the first argument in the call to minix() (see lines 7055-7056). | ||
cs is the absolute memory address of the beginning of the
code and
data is the absolute memory address of the beginning
of the data. end is the absolute memory address of the end of
the process in memory. The meaning of ds depends on whether
the A_SEP flag is set (see line 7453). (see comment on line
7212 for an explanation of the A_SEP flag.)
| ||
| 7050 }process[PROCESS_MAX ]; |
| If the kernel, the memory manager (mm), the file system (fs), the network server (inet), and init are in the OS image file and all these processes are loaded into memory, n_procs will be equal to 5. n_procs is set in exec_image() (see line 7515). | ||
|
The magic number (KERNEL_D_MAGIC = 0x526F) is at the beginning
(MAGIC_OFF = 0) of the kernel data segment (see line 06478 in
the book).
Note that MAGIC_OFF and P_SIZ_OFF (see line 7060) are both offsets into the kernel data segment and are both equal to 0. The text and data sizes (see comments for line 7040) are patched into the kernel data segment at offset P_SIZ_OFF (see line 7075). exec_image() (line 7348) first checks the magic number (see line 7521) and then patches over the magic number with the sizes (see line 7528). | ||
|
The kernel often uses "clicks" instead of bytes to describe the sizes
of different memory objects. The click shift for the kernel is found
at an offset of CLICK_OFF in the kernel code (i.e. text) segment
(see line 06053 in the book) and is equal to 8. A size in bytes is
shifted 8 bits to the right to get the click size. For example, 0x0300
bytes is equal to 0x03 clicks.
The kernel and the boot monitor save space by converting byte sizes to click sizes. The text and data click sizes for the kernel, memory manager (mm), file system (fs), and init (plus any other server that is part of the OS image) are patched into the beginning of the kernel data (see line 7182). 2 bytes are used for each click size; if byte sizes were instead used, 2 bytes would not be enough. The kernel flags are found at an offset of FLAGS_OFF in the kernel code segment (see line 06055 in the book). These flags are described in the comments for lines 7028-7035. | ||
|
7057 #define KERNEL_D_MAGIC 0x526F /*Kernel magic number. */
7058 7059 /*Offsetsof sizes to be patched into kernel and fs. */ |
| Note that MAGIC_OFF and P_SIZ_OFF (see line 7060) are both offsets into the kernel data segment and are both equal to 0. The text and data sizes (see comments for line 7040) are patched into the kernel data segment at offset P_SIZ_OFF (see line 7075). exec_image() (line 7348) first checks the magic number (see line 7521) and then patches the sizes (see line 7528). | ||
| The text and data click sizes for init are patched into the file system (fs) data segment (see line 7191). | ||
| 7062 |
| 7063 |
| between() returns TRUE if c is between a and z. | ||
| 7065 |
| OS image files are typically placed in the /minix directory. | ||
|
7067 /* Pretty print the name of the image to load. Translate '/' and '_' to
7068 * space, first letter goes uppercase. An 'r' before a digit prints as 7069 * 'revision'. E.g. 'minix/1.6.16r10'->'Minix 1.6.16 revision 10'. 7070 * The idea is that the part before the 'r'is the official Minix release 7071 * and after the 'r' you can put versionnumbers for your own changes. 7072 */ 7073 { 7074 int up= 0, c; 7075 |