[net.bugs.2bsd] ramdisk driver for 2.9

keith@motel6.UUCP (Keith Packard) (11/18/85)

I have just completed a driver that makes a segment of memory into a 
pseudo disk drive.  It was rather simple to do but I thought that
someone might like to see just how it was done.  The device driver
itself is very short and straight forward.  The remaining modifications
were also fairly straight forward if you know where to put the hooks in.

The description that follows is about the 2.9 distribution that I
received from berkeley about a year or so ago.  It can be applied to
the bostic 2.9 distribution although I am still working on getting
that system running (I'm bringing up the networking code on my
11/73 - more details about that when it works) and don't have that
code on-line (I'm hacking on that on another computer).

The allocation of the block of memory occurs in the startup code along
with the allocation for disk buffers and clists.  The constant NRAM
specifies how many 512-byte blocks to allocate for the ramdisk and is
defined in "ram.h":

********ram.h*********
/*
 *	number of blocks for a ram disk
 */

# define	NRAM	512

********EOF***********

accordingly, include ram.h in machdep.c and add a declaration at the
top of startup:

****machdep.c*****
  /*
   * Machine dependent startup code
   */
  startup()
  {
  	register memaddr i, freebase;
+ #if NRAM > 0
+ 	unsigned	ramstart;
+ #endif
  	extern	end;

*****

then put in the allocation code:

*****

  #ifdef	UCB_CLIST
  #define	C	(nclist * sizeof(struct cblock))
  	if ((clststrt = malloc(coremap, btoc(C))) == 0)
  		panic("clists");
  	maxmem -= btoc(C);
  	clstaddr = ((ubadr_t) clststrt) << 6;
  #undef	C
  #else
  	clstaddr = (ubadr_t) &cfree;
  #endif	UCB_CLIST
  
+ #if	NRAM > 0
+ #define C	(NRAM << 3)
+ 	if ((ramstart = malloc (coremap, C)) == 0)
+ 		panic ("ramdisk");
+ 	maxmem -= C;
+ 	raminit (ramstart);
+ #undef 	C
+ #endif	NRAM
  	
  #if	defined(PROFILE) && !defined(ENABLE34)
  	maxmem -= msprof();
  #endif	defined(PROFILE) && !defined(ENABLE34)

******

Then fix c.c to include a ramdisk entry.  Your c.c will probably
be different as I have also written a driver for my DTC/Xebec
winchester controller which precedes the ramdisk entries.

******* c.c ******

+ #include	"ram.h"
+ #if	NRAM > 0
+ int	ramstrategy(), ramread(), ramwrite();
+ extern	struct buf	ramtab;
+ #define ramopen		nulldev
+ #define ramclose	nulldev
+ #define _ramtab		&ramtab
+ #else
+ #define ramopen		nodev
+ #define ramclose	nodev
+ #define ramstrategy	nodev
+ #define ramread		nodev
+ #define ramwrite	nodev
+ #define _ramtab		((struct buf *) NULL)
+ #endif NRAM

  #include	"rl.h"
  #if	NRL > 0
  int	rlstrategy(), rlread(), rlwrite();
  extern	struct	buf	rltab;
  #define	rlopen		nulldev
  #define	rlclose		nulldev
  #define	_rltab		&rltab
  #else
  #define	rlopen		nodev
  #define	rlclose		nodev
  #define	rlstrategy	nodev
  #define	rlread		nodev
  #define	rlwrite		nodev
  #define	_rltab		((struct buf *) NULL)
  #endif	NRL

********** bdevsw ***********

  #if NRX > 0
  	rxopen,		rxclose,	rxstrategy,
  	nulldev,	_rxtab,		/* rx = 10 */
  #else
  	rx2open,	rx2close,	rx2strategy,
  	nulldev,	_rx2tab,	/* rx2 = 10 */
  #endif
  
  	xeopen,		xeclose,	xestrategy,
  	nulldev,	_xetab,		/* xe = 11 */
  	
+ 	ramopen,	ramclose,	ramstrategy,
+ 	nulldev,	_ramtab,	/* ram = 12 */
  };
************ cdevsw *************
  
  #if NRX > 0
  	rxopen,		rxclose,	rxread,		rxwrite,
  	rxioctl,	nulldev,	0,		/* rx = 22 */
  #else
  	rx2open,	rx2close,	rx2read,	rx2write,
  	rx2ioctl,	nulldev,	0,		/* rx2 = 22 */
  #endif
  
  	xeopen,		xeclose,	xeread,		xewrite,
  	nodev,		nulldev,	0,		/* xe = 23 */
  
+ 	ramopen,	ramclose,	ramread,	ramwrite,
+ 	nodev,		nulldev,	0,		/* ram = 24 */
  };

**********************

Now install the driver in ../dev/ram.c:

*************** ram.c ************
/*
 *	SCCS id	@(#)ram.c	2.1 (motel6)	11/18/85
 */

/*
 *  ram disk driver
 */

#include "ram.h"
#if	NRAM > 0
#include "param.h"
#include <sys/buf.h>
#include <sys/dir.h>
#include <sys/user.h>
#include <sys/systm.h>
#include <sys/conf.h>

struct buf	rrambuf;

struct buf	ramtab;
/*
 *	starting click of ram buffer
 */
unsigned	ramclick;
# define bltocl(b)	(((b)<<3) + ramclick)

ramattach(addr, unit)
char *addr;
{
	return(1);
}

raminit (base)
unsigned base;
{
	ramclick = base;
}

ramstrategy(bp)
register struct	buf *bp;
{
	int	ramc, bufc, count;

	if (!ramclick) {
		goto bad;
	}
	if(bp->b_blkno + (bp->b_bcount>>BSHIFT) >= NRAM) {
		if((bp->b_blkno == NRAM) && (bp->b_flags & B_READ))
			bp->b_resid = bp->b_bcount;
		else {
bad:			;
			bp->b_error = ENXIO;
			bp->b_flags |= B_ERROR;
		}
		iodone(bp);
		return;
	}
	/*
	 *	just copy the whole block using
	 *	the l.s copy routine, if possible
	 */
	if ((bp->b_un.b_addr & 63) || (bp->b_bcount & 63)) {
		goto bad;
	}
	bufc = ((bp->b_un.b_addr >> 6) & 01777);
	bufc += bp->b_xmem << 10;
	ramc = bltocl (bp->b_blkno);
	count = bp->b_bcount >> 6;
	if (bp->b_flags & B_READ)
		copy (ramc, bufc, count);
	else
		copy (bufc, ramc, count);
	iodone (bp);
}

ramread(dev)
dev_t	dev;
{
	physio(ramstrategy, &rrambuf, dev, B_READ);
}

ramwrite(dev)
dev_t	dev;
{
	physio(ramstrategy, &rrambuf, dev, B_WRITE);
}
#endif	NRAM

***************EOF****************

that should be it.  make a new kernel, boot it and make the
devices in /dev.  Note that the raw device does not support
completely general I/O - it is very possible to break things
doing raw i/o.  I suggest never using the raw device.

keith packard
tektronix!reed!motel6!keith

bb@wjh12.UUCP (brent e byer) (11/29/85)

The ramdisk driver described worked by allocating its space
at BOOT time which makes it easy to implement but much less
useful than one which could do so dynamically, say, at XXXopen()
time.

Consider:  You very well may need all the RAM you've got during
	prime time, but when the `cheese' are away and just you &
	I are there to play, we should not have to re-boot just
	to get that silicon amphet.  Also, there may come a time
	during normals' hours when something MUST finish more
	quickly.

To Kernel-Hacks:  With price of ram plummeting, all of you should
	cobble up something of this sort for your next release.
	Your sales hacks may be able to sell more add-on memory
	(iff they stop being so damn greedy with the price$ !!!),
	[ and, we nightfolk may actually see our processes finish
	  before dawn. ]

PS:	All programs which need /tmp-type space should check for
	an ENV variable to allow better use of this new feature.
PPS:	When you do the allocation, PLS take it off the top; yes,
	shuffle if you need to.

Thanks,
  Brent Byer  {decvax!genrad  harvard}!wjh12!bb
		ihnp4!ihesa!textware!brent

keith@motel6.UUCP (Keith Packard) (12/02/85)

In article <658@wjh12.UUCP> bb@wjh12.UUCP (brent e byer) writes:
>The ramdisk driver described worked by allocating its space
>at BOOT time which makes it easy to implement but much less
>useful than one which could do so dynamically, say, at XXXopen()
>time.
>
>Consider:  You very well may need all the RAM you've got during
>	prime time, but when the `cheese' are away and just you &
>	I are there to play, we should not have to re-boot just
>	to get that silicon amphet.  Also, there may come a time
>	during normals' hours when something MUST finish more
>	quickly.
>
>Thanks,
>  Brent Byer  {decvax!genrad  harvard}!wjh12!bb
>		ihnp4!ihesa!textware!brent

Actually, when I wrote the ramdisk driver, all I could think of
was how nice it would be to have version 8 unix running.  Then I
could design my *own* filesystem structure - completely dynamic with
disk backing store.

If I also cobbled up the memory allocation routines to always return 1k
chunks, I could use up *all* of memory with temporary file space.  And
have enough tmp space, even when memory was tight.

The best fix to the current allocation scheme is to have a process
do a system call, requesting that an appropriate amount of memory
be set aside for the ramdisk.  Then, the process would hang; waiting
for the scheduler to swap enough processes out to make room for the
ram disk.  This would be similar to the current method for sbrk, but
the memory would be allocated to the system and not freed until another
ramdisk allocation was issued.  This is, perhaps, the best way.

The major reasons this was not done is that it requires major modifications
to the scheduler and the addition of yasc (yet another system call).
In other words, I didn't have the time to invest in it.

It should also be fixed so that the raw device works correctly.  This
is quite possible as a routine already exists that will copy
bytes from any address to any address - unfortunately it is rather
broken in my version of 2.9 because it was a part of the networking
code that was distributed in a non-working state.  In Keith Bostic's
distribution I expect that this routine is fixed.

Keith Packard
tektronix!reed!motel6!keith