推荐给好友 上一篇 | 下一篇

Open Source Hackers' Guide Through The Galaxy Part III - Kernel

Part III - Kernel

This is the third part of our tour through the NetBSD source tree. After we have talked about the various components that build up the userland, we will concentrate on the kernel source now. It is located in /usr/src/sys, with the /sys symlink being a well-known abbreviation to reach the system's kernel source.

3]EqF N)AcLet's remember what happens when building a kernel: after editing the kernel config file located in /sys/arch/<arch>/conf and running config(8) on it, a number of files are created in /sys/arch/<arch>/compile/KERNELNAME. The header files contain data about what and how many devices to include, as well as other data for the system's configuration. Besides that, a Makefile is created, that is used to build the kernel from source. The interesting point to note here is that there is only one Makefile that will locate and compile all the needed sources and place the object files in the .../compile/KERNELNAME directory. In NetBSD, there is no recursive tree-walk of the whole source tree utilizing several Makefiles to build the various sub-trees of the kernel source. This allows building kernels for several configurations and platforms from the same source, without different builds tripping across each other.BSD爱好者乐园-~j'R&?4[&v BM9[\}

Still, the various parts of the NetBSD kernel are placed in various subdirectories that we will have a closer look at now. Under /usr/src/sys, there are:

w"^~Y\&wr'M4@

adosfs, coda, filecorefs, isofs, msdosfs, nfs, ntfs:
These are various filesystems used directly by NetBSD to access data. Some of the filesystems' primary goal is to help in exchanging data between the machine's native operating system (AmigaOS's adosfs, Acorn Computers RISC OS's filecorefs, ...), while others implement filesystems that can be found on many systems (isofs, nfs, ...).

7|0n0E.A$qo

F!r2q@`2US
ufs:
The Unix (User) File System is the base of the native filesystem used in NetBSD. Ancient (AT&T) Unix filesystems only allowed up to 14 chars long filenames, there were no symlinks etc. The problems were solved by the Berkely computer scientists implementing BSD Unix. Their filesystem implementation serves as a base for several filesystems based on it these days, using various ways of data layout on the disk.BSD爱好者乐园9~l p }:D w

The filesystems are stored in the "ufs" subdirectory, filesystems contained in there includeBSD爱好者乐园3gDyQ3?-s

  • ext2fs: Linux' ext2fs
  • lfs: Log structured filesystem
  • mfs: Memory filesystem, for things like in-core /tmp
  • ufs: The native NetBSD filesystem
  • ffs: General routines of the Berkeley Fast File System, utilized by the other UFS-based filesystems, including things like softdeps.

B!G1H ~O]R

*L-i5r8FYc4[&E'i;x
miscfs:
This directory contains further filesystems that aren'd directly related to physical storage. Instead they implement various layered filesystems for services like data translation or routines for implementing kernel features. Using the virtual filesystem operations table, it is easy to change behaviour of a operation upon certain conditions, e.g. mapping operations to deadfs on a file who's filedescriptors were revoke(2)'d.

5M hM&r y.zuThe filesystems included here are:BSD爱好者乐园;b? O8|8sMU5~x

  • deadfs: Implements operations that don't modify any data and instead return indications of invalid IO. Used to revoke(2) file descriptors.

    B*EwBkWkn

    K`'@ wgB } BJXY
  • fdesc: Maps a process' file descriptors into filesystem space, depending on the accessing process. Can be mounted on /dev/fd using mount_fdesc(8).BSD爱好者乐园'g/I&\7}8_!@"u4h*h q

    wEs!x] Jy] UP|
  • fifofs: Implements FIFOs using Unix domain sockets internally

    ;o!O-c5|!^(X,J,W

    O'Q/cy d
  • genfs: Generic filesystem functions that mostly return errors of some kind - bad filedescriptor, bad operation, or just does no operation at all. Used for implementing deadfs etc.

    2F^)}#t+V(HvXUf2mnBSD爱好者乐园T p]F a

  • kernfs: This filesystem is usually mounted under /kern and provides various informations about the running system, like kernel version, system time etc.

    D/lI;^ m}G!c

    lV9~8gXG$mg
  • nullfs: Used to "mirror" one directory tree onto another directory, providing the same tree on both mount points. Also known as loopback mount - see mount_null(8) for more information.

    :Pk,e)v bGl-Q xBSD爱好者乐园 ['A(Uj#js

  • overlay: The operation of this filesystem is similar to the null filesystem, the implementation allows using this filesystem as a base for further layered filesystems though, as all VFS operations are defined. See mount_overlay(8) for more information.

    0G0J!I7E V;T3``

    ]&Q @0Oa
  • portal: The portal filesystem provides an service that allows descriptors such as sockets to be made available in the filesystem namespace following conversion rules given in a config file. See the mount_portal(8) manpage for further information.

    'T?SMl

    U.[F&DMIw3eII
  • procfs: Similar to kernfs, this filesystem is usually mounted on /proc and allows accessing various data about processes. It is used by ps(1) and other utilities. See mount_proc(8) for more information.BSD爱好者乐园8n:r(Od?)k_D

    .S}A a8v Xb
  • specfs: Implements routines to access special devices. The filesystem provides a filesystem interface, and calls the device-specific routines depending on the device's type, major and minor number.BSD爱好者乐园#{D'dom6U

    BSD爱好者乐园d9nTatWKL

  • syncfs: Operations used to implement the ioflush kernel thread that writes out modified pages to disk.BSD爱好者乐园F Rf)d"e%Xe%_

    BSD爱好者乐园 Rs(Gmx"j Y

  • umapfs: A filesystem for re-mapping UIDs/GIDs, useful e.g. when mounting a NFS volume from a server that has a different set of UIDs/GIDs than the local machine.BSD爱好者乐园3Q_ [2n)v-K(@

    -r @"y2\'V)h:Zs
  • union: This layered filesystem allows merging two filesystems, providing a view as if they were mounted on the same mountpoint. Modifications go either to the "upper" or to the "lower" layer, which allows mounting a CDROM (read-only :), and mounting an empty but writable directory over it, making it e.g. possible to do a compile on a source expanded on the CDROM. See mount_union(8) for further details.BSD爱好者乐园 Gmp5w$f$Uv}

    e"OL+x+X3T

}qk*_2Mf0s { w J

1XX znW._{
compat:
This directory contains code for emulating binary compatibility with various non-NetBSD operating systems as well as with old NetBSD binaries. It includes:BSD爱好者乐园#xu(udby8DJB

  • aout: This subsystem is used to run native NetBSD a.out binaries on systems that made the transition to the ELF executable format. As for most emulations, the shared library loader ld.so, shared libs etc. are looked for in /etc/aout first.

    z.R9q-NM7S;{5w

    4wp*] j&myH
  • common: Various common routines used by all emulations like system call table translation routines; also contains compat code for prior NetBSD releases, see the COMPAT_* kernel options in options(4).BSD爱好者乐园+I-Z6y L Cp A

    BSD爱好者乐园0d`N}qj)~

  • freebsd: mostly a few glue routines for running FreeBSD/i386 a.out and ELF binaries; See the compat_freebsd(8) manpage for details on setting things up!BSD爱好者乐园q(}:^:c1p}*s bL

    %sqF pG1{.Do3J
  • hpux: To run native HP/UX programs on the Motorola based hp300/hp400 machines. Adjusts a fair number of calls, including terminal IO, signals, IO, etc.

    3A_a!E5p#h

    9S"S"_4[^3E2k
  • ibcs2: This code implements the Intel Binary Compatibility Suite version2 used for running SCO programs on i386, but also for general compatibility with AT&T System V.3 which is used on the VAX port. Maybe it should have been named COMPAT_SVR3 - the compat_ibcs2(8) manpage contains more data.

    ']dLO U&^)suBSD爱好者乐园#I\$[(e2U.cnCVP

  • linux: Code to run a.out and ELF Linux binaries for a number of hardware platforms, including alpha, arm32, i386, powerpc, mips, m68k, sparc and sparc64. One of the special things of the Linux emulation is that Linux uses a different system call table on each port, which makes maintaining things a bit more interesting. The code is seperated in a "common" directory that applies to all platforms, and various architecture specific directories for different CPUs. The compat_linux(8) manpage contains more information on using the system, and there are also several packages in pkgsrc that help in setting up the necessary shared libraries etc. to run Linux binaries like Netscape or Acrobat Reader.BSD爱好者乐园 DN3|PL%v'Yt5Q

    /p0LXz~ T9M
  • m68k4k: Some of the m68k ports used to use a pagesize of 4k instead of the 8k common today. This code helps in maintaining binary compatibility with old binaries that still use 4k.BSD爱好者乐园:Ua,A+@*`#cT

    s&m-O/rp$E&_?k#P
  • netbsd32: Used by 64bit systems like sparc64 to run native 32bit binaries, mapping the programs' 32bit args to the 64bit args used by LP64 systems' kernels.

    ~+F/l(TVfS

    ]x!}OI0rg
  • osf1: The compat_osf1(8) system allows running OSF/1 (AKA Digital Unix AKA Tru64) on the Alpha platform.

    &_9u Q3AoD)H&M}}#P/O

    b*o(zf\l*P
  • ossaudio: This software layer provides Open Sound System compatible ioctl calls that are then mapped to the native NetBSD audio model by this code. Enabled when compiling in support for Linux and/or FreeBSD binary compatibility.

    ,B!e5MW6b8\BSD爱好者乐园](X/I-s_RiT

  • pecoff: This subsystem allows running programs that are in the PEcoff executable format, which is found on the Microsoft Windows platform. Of course mapping system calls is a real challenge here, as the API to present to the upper layer is definitely nothing that is even remotely near to the API used on all the Unix-like compat systems, and as such there's no easy mapping of the calls to NetBSD functions. Much of the work is done by libraries in the userspace instead, which then talk to the X server etc. See the compat_pecoff(8) manpage for further details.BSD爱好者乐园0f Wa\2qK'nT

    BSD爱好者乐园8|G+h+O!H dFv%YF

  • sunos: If users still have SPARC or m68k applications built for SunOS 4.x, this emulation layer will help run them. See compat_sunos(8) for more information.BSD爱好者乐园"D4S2W2Keg.l8G

    BSD爱好者乐园r i R&O Gy1~$_Y!J$a

  • svr4: The System V compat system allows binary compatibility for several systems, e.g. Solaris (SunOS 5.x) on i386, sparc and sparc64, Amix on m68k and SCO/Xenix on i386. The compat_svr4 manpage contains further information.

    Kw,}!x,I0D!~~|

    1bTV]C0ZI
  • ultrix: For pmax and other MIPS based systems as well as VAX systems, to run Ultrix binaries. See compat_ultrix(8).BSD爱好者乐园0~+[ cm!k

    BSD爱好者乐园^V*Z9M%KU

  • vax1k: For VAX binaries that still use 1k pagesizes, this allows running them. No idea where these originate - probably very historic. :)BSD爱好者乐园8Hhu Fui@p#gQ1u

    nDYl U+xL,NV(W

V/Q5G&Q(`'v8klhk

ATb-f*jS n
conf:
The /sys/conf directory contains the main list of files to include into kernel builds as well as scripts and files used to update the OS version and compile it into the kernel. The operating system's version is stored in the "osrelease.sh" script, which is used from a number of places to determine the OS version.BSD爱好者乐园NY'k;[7K&z"dzAn

K3I;f2o s+^
crypto:
This directory contains code for various data encryption standards (arc4, blowfish, DES, Rijndael etc.) that is subject to crypto export regulations. The code is use by the IPsec kernel subsystem.

&FZ_Lz&~/frjiBSD爱好者乐园L/KG'^-JTBT

ddb:
The DDB kernel debugger that can be used to do post mortem debugging is found here. The debugger is used on all NetBSD ports.BSD爱好者乐园YtFq5J!O

%m PNi9?x U
dev:
This directory contains device drivers that use the machine independent bus_dma(9) and bus_space(9) interfaces and that work on all platforms that support the necessary bus glue routines. There are several subdirectories grouping drivers by various categories:BSD爱好者乐园&E I9kHl/E$P5^`

  • bus interface: cardbus, eisa, ieee1394, isa, isapnp, mca, pci, pcmcia, sbus, tc, usb, vme, qbus, xmiBSD爱好者乐园:wK!e_4ay

    BSD爱好者乐园KD*yOP

  • functionality: ata, i2c, i2o, mii, ofw, pckbc, raidframe, rasops, rcons, scsipi, sysmon, wscons, wsfont

    n+z r,m8OXT{

    )^/N UcT'{2E
  • general interfaces that are backed by bus-specific drivers: audio, midi, rnd

    1} F;U U8l2C h0f

    Dgx.y;vhqG z
The directory structure is mostly oriented towards the bus system that a hardware device attaches to, not towards the functionality it provides. There are no special categories for things like audio, network etc. - these are in their bus-specific directories like pci, isa etc. containing (only) the bus-specific attachment routines.BSD爱好者乐园-^#u(J~2s!B:]2f

If a chip implements some functionality like audio, network or scsi, it is often used on several cards that all have the same chip, but different bus interfaces - ISA, PCI, etc. To prevent maintaining several drivers that have identical core functionality, NetBSD drivers are seperated into bus-glue code kept in the bus-specific directories mentioned above, and the core functionality of the integrated circuit. Naming conventions help identifying e.g. network cards (if_*), but aren't implemented throughoutly, unfortunately.BSD爱好者乐园7_*e[f%?nhd

The drivers for the core functionality are stored in the "ic" subdirectory, with the file names indicating the IC's chip numbers:

% ls /sys/dev/ic
CVS                 cac.c               isp_target.c        pckbc.c
Makefile            cacreg.h            isp_target.h        pckbcvar.h
README.ncr5380sbc   cacvar.h            isp_tpublic.h       pdq.c
ac97.c              cd1190reg.h         ispmbox.h           pdq_ifsubr.c
ac97reg.h           cd1400reg.h         ispreg.h            pdqreg.h 
...

SQ0?u6q:N@~:}O

+w$e i:b kC m`O
ipkdb:
An IP-based debugger interface to a remote machine. Another way to debug the NetBSD besides the DDB kernel debugger and gdb, which can be used for debugging both userland and kernel.BSD爱好者乐园#{Pn m6Np2TsJ!Jq

N+Q+q5ei9H)t
kern:
This directory contains the core kernel code including a number of facilities:

v/OT2CO.eC5`

  • loaders for executables in various formats (a.out, EOF, COFF, scripts ...)

    'X} EW"x6Z+tso5A

    'Ns@6w#Lo
  • process and (kernel) thread management

    } q8m/gZt#W

    .`%{+\b-v$L
  • signal delivery and handling

    HNT T\&uZ1G?BSD爱好者乐园 y&}"l ]SE:N

  • terminal IO subsystem

    :{+h(}olh VJ?PBSD爱好者乐园&v/P9ir9T

  • sockets and other interprocess comunication primitives

    g'oB T I

    9f/Daq#G)ix;PZ7]9e
  • virtual filesystem layer, providing the framework used by the filesystems in /sys/miscfs.

    @ ?q"w@~8KQ?BSD爱好者乐园pU5YA D@3X

  • many auxiliary routines used from all placesBSD爱好者乐园+Wd;w4Z8}.Z$F

    BSD爱好者乐园,SvJ.b6[d0k

I vR5Z V:S Tx

5[p{U$k9Ed
lib:
Throughout the NetBSD kernel, there are many tasks that are used from many places, and that are stored within a few libraries that are used only in the kernel:BSD爱好者乐园LsjG5ET {

  • libkern: This is basically what libc is for the userland, with functions used for providing various arithmetic operations that can't be inlined by gcc as well as string/memory copy/comparison operations.

    0b9rL5m1_J M&y-wR eBSD爱好者乐园;~ m:Y8a nQ$n

  • libsa: The StandAlone library provides functions used for loading the kernel, when there's no operating system running yet and thus many of the services provided by the NetBSD operating system are not available. The library includes code for netbooting (rarp, RPC, NFS), locating/loading the kernel from an UFS, LFS, ISO 9660 or tar-structured media, memory management and others.

    &d Z7t"]/`{~4ffBSD爱好者乐园'aMSiQws[#u

  • libz: In-kernel decompression library for loading gzip compressed kernels.BSD爱好者乐园+O FB i H+ShJ3J4j

    5gpP.I'N.x

dvB#T7E]e,PBSD爱好者乐园7EF jV k+c

stand:
This directory contains source for several standalone programs that aren't used by NetBSD currently.

,DsDgfCMK ?

tm*F*A%Wv1x0?.G
lkm:
NetBSD supports loadable kernel modules, and the sources are in this directory. LKMs include a floppy driver for mac68k, various binary emulations, IPfilter logging and several filesystems.BSD爱好者乐园Y5p V[Q

0pxQ(v2M
net:
NetBSD's networking framework contains many routines that are independent of a special protocol, and that are used by several networking protocols/stacks. The components are included in this directory, functions include packet filtering (BPF), access routines for all hardware cards (arcnet, ATM, ethernet, fddi, IEEE 802.11, PPP, token ring etc.) that hand device access to drivers in the /sys/dev directory, routing code etc.BSD爱好者乐园/G5^kfq"Ymd!Aq

BSD爱好者乐园 v] x2|yd$|~#V

netatalk:
The code in this directory implements the kernel part of the Appletalk protocol stack. The userland part is not included in NetBSD, it can be installed from pkgsrc/net/netatalk(-sun).

1_ sZ9e\0J;D3HL/TBSD爱好者乐园1w,A5D1y M-JM*gl

netccitt, netiso:
Not in widespread use these days, NetBSD compes with an ISO/OSI protocol stack which is located in these directories.

5Gb"tey}

u#tx5o` KVB
netinet:
Internet stuff - the NetBSD TCP/IP (v4) stack. Documentation on this is available in section 9 of the NetBSD manual pages as well as in Richard Steven's "TCP/IP Illustrated" books.

`NU#D Pk k"k7V s;s"n

'Zm1Q i*I9FI2x
netinet6:
Internet, next generation - this directory contains the KAME IPv6 stack that is shipping with NetBSD. See http://www.kame.net/ for further information.

HXE[q

jUuoAGg(Es
netkey:
Key management for IPsec - see the ipsec(4) manpage for more details.BSD爱好者乐园5lw?$j^V J

$^U`%f2xymn
netnatm:
The code in this directory implements native mode ATM to transport other protocols like IP.

rx FjG+M8Mq

8Ap(Hi.A0[Z5`
netns:
NetBSD has support for the Xerox network service protocol, which can be found in this directory. Not in widespread use any more today, the protocol is described in the first edition of Richard Stevens' "TCP/IP Network Programming" book.

3~ d'k0h.sM7A:mcafBSD爱好者乐园&t3L\3y]&N

sys:
This directory contains only header files that get installed into /usr/include/sys.

7H9u{$\!f6[

.e,{ q.w7@ST2S
uvm:
The code in this directory implements NetBSD' New Virtual Memory system that replaced the old Mach-based VM system some time ago. See the uvm(4) manpage for more information.

^D1A&AFz%W

;L-o8Mi)Hd;^
vm:
This directory has only the header files of the old Mach based virtual memory system left, for use with various programs. The VM system itself is not used any longer.BSD爱好者乐园Z ^g-u o

.?P3Ou;q+mGF#J
arch:
Code specific to one hardware platform is collected under this directory. Directories are present for each port as well as for CPU-specific functions that are shared by several ports that use the same CPU, avoiding redundancy.

^ k%Fkn Lg,o%Ov%QPort-specific directories contain several subdirectories, with the following ones being present for all ports:

1P*R\ IU)sW

  • conf: contains kernel config files, a list of files specific to the port and a template for the Makefile used to build a kernel

    RGU~s by"_(M/{u

    \9tS5Jn"_sW Z
  • compile: This directory is initially empty, it gets populated by config(8) with directories that contain a Makefile and headerfiles to build a kernel.BSD爱好者乐园xD Sl;i

    pq o|)I
  • <port>: Port-specific functions, CPU/MMU/CPU initialisation code, etc. - all the machine specific code that cannot be shared across various hardware architectures.BSD爱好者乐园 c4B3cg&V;O Q8R

    _F c"W1AU
  • include: machine specific include files that describe the CPU and MMU layout, data formats used by the FPU, limits, etc.

    /i!|7_s+Y(GGR1A

    $n:`BfFmm0o2Sd
  • stand: This directory contains sources for loading the kernel into the system - usually it contains code for bootblocks, secondary stage bootloaders, netboot miniroots and other facilities used to boot the system.

    bAg6b s7Fw E6v#_[

    m#o*I9Tk P1M3yF"\
BSD爱好者乐园(E K%[ Q,l#N

Further directories may exist in the arch specific directories that contain bus-specific/non-machine independent device drivers which don't fit into /sys/dev as they work on one port only. Ideally, a port only uses machine independent drivers, of course.BSD爱好者乐园|7GD_ mV

BSD爱好者乐园!Mus#jtz3h*?5U

We have now described all the important directories that are available in the NetBSD source tree. To get used to the directory structure, it is recommented that you browse the directories and have a look at the various files to fully explore things.
[版权声明]BSD爱好者乐园站内文章,如来源不是互联网,则均系原创或翻译之作,可随意转载,或以此为基础进行演译,但务必以链接形式注明原始出处和作者信息,否则属于侵权行为。另对本站转载他处文章,俱有说明,如有侵权请联系本人,本人将会在第一时间删除侵权文章。
TAG: Guide Hackers kernel Kernel
 

评分:0

我来说两句

seccode