Time to have a look at initial memory initializations. First we need to initialize BSS (more details: http://en.wikipedia.org/wiki/.bss), statically-allocated variables. We do it in next way:

1memset(edata, 0, end-edata);

From Plan9 manual: “The loaded image has several symbols inserted by the loader: etext is the address of the end of the text segment; bdata is the address of the beginning of the data segment; edata is the address of the end of the data segment; and end is the address of the end of the bss segment, and of the program.”

When it is done, we init with zeros also Mach obejct:

1memset(m, 0, sizeof(Mach)); 

Also let’s move pl011 stuff into pl011.c and add signatures into fns.h:

1void    pl011_putc(int);
2void    pl011_puts(char *);
3void    pl011_serputs(char *, int);

Specify amount of Mach objects:

1conf.nmach = 1; 

Specify a function for serial console writing:

1serwrite = &pl011;_serputs;

Next is confinit() call to initialize Conf object:

 1extern int main_pool_pcnt;
 2extern int heap_pool_pcnt;
 3extern int image_pool_pcnt;
 4
 5void
 6confinit(void)
 7{
 8    ulong base;
 9    conf.topofmem = 128*MB;
10
11    base = PGROUND((ulong)end);
12    conf.base0 = base;
13
14    conf.npage1 = 0;
15    conf.npage0 = (conf.topofmem - base)/BY2PG;
16    conf.npage = conf.npage0 + conf.npage1;
17    conf.ialloc = (((conf.npage*(main_pool_pcnt))/100)/2)*BY2PG;
18
19    conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
20    conf.nmach = 1;
21
22    print("Conf: top=%lud, npage0=%lud, ialloc=%lud, nproc=%lud\n",
23            conf.topofmem, conf.npage0,
24            conf.ialloc, conf.nproc);
25}

Plus we need to add ulong npage; and ulong topofmem; into Conf structure and definition #define PGROUND(s) ROUND(s, BY2PG) into mem.h Three variable: int main_pool_pcnt; int heap_pool_pcnt; int image_pool_pcnt; we define in rpi config file:

1code
2    int kernel_pool_pcnt = 10;
3    int main_pool_pcnt = 40;
4    int heap_pool_pcnt = 20;
5    int image_pool_pcnt = 40;

Finally we can initialize memory pools:

1static void
2poolsizeinit(void)
3{
4    ulong nb;
5    nb = conf.npage*BY2PG;
6    poolsize(mainmem, (nb*main_pool_pcnt)/100, 0);
7    poolsize(heapmem, (nb*heap_pool_pcnt)/100, 0);
8    poolsize(imagmem, (nb*image_pool_pcnt)/100, 1);
9}

And in main():

1xinit();
2poolinit();
3poolsizeinit();

As memory pools are ready then malloc() is functional we can do much more in next labs.

FILES: