On Tue, Feb 12, 2013 at 5:34 PM, jordon <open...@sirjorj.com> wrote:
> Today I was looking into some of the more simple devices to see how they
> are implemented.  I figured the basic text ones (zero, random, null,
> etc) would be a nice place to start.  I went to /usr/src/sys/dev to look
> for them, but I couldn't find them.  Where is the source of these basic
> devices?  Are they actual drivers like network drivers or are they some
> completely different devices?

Devices != drivers.  Indeed, network drivers generally do not present
devices in /dev/.  That was one of the (early) criticisms of the BSD
networking model: it created a new namespace for interface names.

Anyway, if you're looking for the code behind a device file (a block
device or character device file in the filesystem), you start by
looking up its major number in the arrays in the file
/usr/src/sys/arch/${ARCH}/${ARCH}/conf.c.  For example, /dev/zero is a
character device with major 2, and
/usr/src/sys/arch/amd64/amd64/conf.c has this:

struct cdevsw   cdevsw[] =
{
        cdev_cn_init(1,cn),             /* 0: virtual console */
        cdev_ctty_init(1,ctty),         /* 1: controlling terminal */
        cdev_mm_init(1,mm),             /* 2: /dev/{null,mem,kmem,...} */
...

cdev_mm_init() is a macro in <sys/conf.h>, and if you stare there and
work out the cpp expansion, you'll eventually look for functions
mmopen(), mmclose(), mmrw(), etc.  For amd64, those are implemented in
/usr/src/sys/arch/amd64/amd64/mem.c, where the code figures out which
of the minor devices is involved in a given call by examining the
'dev' argument to the function.


Philip Guenther

Reply via email to