JOS LAB2,Memory and Paging.PART 1

JOS LAB2,Memory and Paging.PART 2

asd posted @ Wed, 24 Nov 2010 10:17:05 -1100 in OSE6.828 , 3144 readers

在PART1说完分页机制后,我们开始讨论具体的实现代码吧 !

第一个函数:

void
i386_vm_init(void)
{
    pde_t* pgdir;
    uint32_t cr0;
    size_t n;
    //////////////////////////////////////////////////////////////////////
    // create initial page directory.
    pgdir = boot_alloc(PGSIZE, PGSIZE);
    memset(pgdir, 0, PGSIZE);
    boot_pgdir = pgdir;
    boot_cr3 = PADDR(pgdir);
    //////////////////////////////////////////////////////////////////////
    // Recursively insert PD in itself as a page table, to form
    // a virtual page table at virtual address VPT.
    // (For now, you don't have understand the greater purpose of the
    // following two lines.)

    // Permissions: kernel RW, user NONE
    pgdir[PDX(VPT)] = PADDR(pgdir)|PTE_W|PTE_P;

    // same for UVPT
    // Permissions: kernel R, user R 
    pgdir[PDX(UVPT)] = PADDR(pgdir)|PTE_U|PTE_P;

    //////////////////////////////////////////////////////////////////////
    // Allocate an array of npage 'struct Page's and store it in 'pages'.
    // The kernel uses this array to keep track of physical pages: for
    // each physical page, there is a corresponding struct Page in this
    // array.  'npage' is the number of physical pages in memory.
    // User-level programs will get read-only access to the array as well.
    // Your code goes here:

    //*****************
    //SUNUS,Nov 15,2010
    pages = boot_alloc(npage * sizeof(struct Page), PGSIZE);
    ROUNDUP(pages,PGSIZE);
    cprintf("pages is %08x\n",PADDR(pages));
    /* I don't know how to deal with the permission stuffs,so leave it ! - SUNUS*/

    //////////////////////////////////////////////////////////////////////
    // Now that we've allocated the initial kernel data structures, we set
    // up the list of free physical pages. Once we've done so, all further
    // memory management will go through the page_* functions. In
    // particular, we can now map memory using boot_map_segment or page_insert
    page_init();

    check_page_alloc();

    page_check();

    //////////////////////////////////////////////////////////////////////
    // Now we set up virtual memory 

    //////////////////////////////////////////////////////////////////////
    // Map 'pages' read-only by the user at linear address UPAGES
    // Permissions:
    //    - the new image at UPAGES -- kernel R, user R
    //      (ie. perm = PTE_U | PTE_P)
    //    - pages itself -- kernel RW, user NONE
    // Your code goes here:

    //SUNUS,Nov,23,2010
    size_t actual_ptsize;
    actual_ptsize = ROUNDUP(npage * sizeof(struct Page),PGSIZE);

    boot_map_segment(pgdir, UPAGES, actual_ptsize, PADDR(pages), (PTE_W|PTE_U));

    //////////////////////////////////////////////////////////////////////
    // Use the physical memory that 'bootstack' refers to as the kernel
    // stack.  The kernel stack grows down from virtual address KSTACKTOP.
    // We consider the entire range from [KSTACKTOP-PTSIZE, KSTACKTOP) 
    // to be the kernel stack, but break this into two pieces:
    //     * [KSTACKTOP-KSTKSIZE, KSTACKTOP) -- backed by physical memory
    //     * [KSTACKTOP-PTSIZE, KSTACKTOP-KSTKSIZE) -- not backed; so if
    //       the kernel overflows its stack, it will fault rather than
    //       overwrite memory.  Known as a "guard page".
    //     Permissions: kernel RW, user NONE
    // Your code goes here:

    //SUNUS,Nov,23,2010
    boot_map_segment(pgdir, KSTACKTOP - KSTKSIZE, KSTKSIZE, PADDR(bootstack),PTE_W|PTE_P);
    boot_map_segment(pgdir, KSTACKTOP - PTSIZE, PTSIZE - KSTKSIZE, 0, 0 ); // I don't know if it's necessary -sunus//
    //////////////////////////////////////////////////////////////////////
    // Map all of physical memory at KERNBASE. 
    // Ie.  the VA range [KERNBASE, 2^32) should map to
    //      the PA range [0, 2^32 - KERNBASE)
    // We might not have 2^32 - KERNBASE bytes of physical memory, but
    // we just set up the mapping anyway.
    // Permissions: kernel RW, user NONE
    // Your code goes here: 
    boot_map_segment(pgdir, KERNBASE, 0x0fffffff + 1, 0, PTE_W|PTE_P);
    // Check that the initial page directory has been set up correctly.
    check_boot_pgdir();

    //////////////////////////////////////////////////////////////////////
    // On x86, segmentation maps a VA to a LA (linear addr) and
    // paging maps the LA to a PA.  I.e. VA => LA => PA.  If paging is
    // turned off the LA is used as the PA.  Note: there is no way to
    // turn off segmentation.  The closest thing is to set the base
    // address to 0, so the VA => LA mapping is the identity.

    // Current mapping: VA KERNBASE+x => PA x.
    //     (segmentation base=-KERNBASE and paging is off)

    // From here on down we must maintain this VA KERNBASE + x => PA x
    // mapping, even though we are turning on paging and reconfiguring
    // segmentation.

    // Map VA 0:4MB same as VA KERNBASE, i.e. to PA 0:4MB.
    // (Limits our kernel to <4MB)
    pgdir[0] = pgdir[PDX(KERNBASE)];

    // Install page table.
    lcr3(boot_cr3);

    // Turn on paging.
    cr0 = rcr0();
    cr0 |= CR0_PE|CR0_PG|CR0_AM|CR0_WP|CR0_NE|CR0_TS|CR0_EM|CR0_MP;
    cr0 &= ~(CR0_TS|CR0_EM);
    lcr0(cr0);

    // Current mapping: KERNBASE+x => x => x.
    // (x < 4MB so uses paging pgdir[0])

    // Reload all segment registers.
    asm volatile("lgdt gdt_pd");
    asm volatile("movw %%ax,%%gs" :: "a" (GD_UD|3));
    asm volatile("movw %%ax,%%fs" :: "a" (GD_UD|3));
    asm volatile("movw %%ax,%%es" :: "a" (GD_KD));
    asm volatile("movw %%ax,%%ds" :: "a" (GD_KD));
    asm volatile("movw %%ax,%%ss" :: "a" (GD_KD));
    asm volatile("ljmp %0,$1f\n 1:\n" :: "i" (GD_KT));  // reload cs
    asm volatile("lldt %%ax" :: "a" (0));

    // Final mapping: KERNBASE+x => KERNBASE+x => x.

    // This mapping was only used after paging was turned on but
    // before the segment registers were reloaded.
    pgdir[0] = 0;

    // Flush the TLB for good measure, to kill the pgdir[0] mapping.
    lcr3(boot_cr3);
}

这是开始的对内存初始化的函数,我们先来到

    static void*
boot_alloc(uint32_t n, uint32_t align)
{
    extern char end[];
    void *v;

    // Initialize boot_freemem if this is the first time.
    // 'end' is a magic symbol automatically generated by the linker,
    // which points to the end of the kernel's bss segment -
    // i.e., the first virtual address that the linker
    // did _not_ assign to any kernel code or global variables.
    if (boot_freemem == 0)
	boot_freemem = end;

    // LAB 2: Your code here:
    //	Step 1: round boot_freemem up to be aligned properly
    //		(hint: look in types.h for some handy macros)
    //	Step 2: save current value of boot_freemem as allocated chunk
    //	Step 3: increase boot_freemem to record allocation
    //	Step 4: return allocated chunk	
    // SUNUS Nov,14,2010
    boot_freemem = ROUNDUP(boot_freemem, align);
    v = boot_freemem;
    boot_freemem += n; 
    if(PADDR(boot_freemem)  > (maxpa))
      {
	cprintf("v is %08x , mapa is %08x\n",PADDR(boot_freemem),maxpa);
	cprintf("Memory Not Enough!\n");
	return NULL;
      }
    return v;
}

这里的 ROUNDUP是向上取整函数,这里的作用是让boot_freemem按4KB内存对齐。这样可以让寻址的效率达到最大。

该函数作用则是分配n字节大小的内存,且起始地址按align方式对齐。

而分配的方式,其实则是让boot_freemem指针不断自增,从而让boot_freemem的值 之前,end之后的地址为“已使用内存”.并返回下一个可使用内存地址。maxpa为最大物理地址,在之前的函数中已经被赋值,这里先不深究。

这里来到了

page_init(void),这里注意的是对于一个页是否可用的标志是pages[x].pp->ref,pp->ref位对页面引用的计数器,若等于0,则可用

对于初始化,要注意系统的有几个页面是不可用的(系统预留或处于安全原因):

pages[0],IOPHYSMEM~EXTPHYSMEM预留的IO HOLE还有内核位置[KERNBASE~boot_freemem]

    void
page_init(void)
{

    /*Nov 16,2010 Sunus */

    int i;
    pages[0].pp_ref = 1;
    LIST_INIT(&page_free_list);
    for(i = 1 ; i < npage ; i++)
      {
    pages[i].pp_ref = 0;
     if((i >= IOPHYSMEM/PGSIZE && i < EXTPHYSMEM/PGSIZE)  /*IO HOLE*/
         || (i >= PADDR(KERNBASE)/PGSIZE && i < (size_t)ROUNDUP(PADDR(boot_freemem),PGSIZE)/PGSIZE)) /*KERNBASE-boot_freemem*/
      {
        pages[i].pp_ref = 1;
        continue;
      }
    LIST_INSERT_HEAD(&page_free_list, &pages[i], pp_link);
      }
}

 

Avatar_small
AAA said:
Sun, 01 May 2022 02:53:50 -1100

This website is my inhalation, really fantastic layout and Perfect written content. Selling Merchant Services

 

 

=========================================================================

 

 

inspiring insights you are sharing. I love the way you are sharing it. Is there any way I could get updated for more? Merchant Affiliate Program

 

 

======================================================================================

 

Thanks for the post, was an interesting read. Curious as to how you came about that solution… North American Bancard Referral Program

Avatar_small
civaget said:
Mon, 11 Dec 2023 06:21:41 -1100

While content is essential, don't overlook the technical aspects of SEO. Site speed, mobile optimization, and clean code all contribute to your 구글 상위노출 success.

Avatar_small
civaget said:
Tue, 12 Dec 2023 00:40:19 -1100 제주유흥 offers a diverse range of entertainment and relaxation options. Something for everyone.
Avatar_small
civaget said:
Tue, 12 Dec 2023 01:41:17 -1100

I booked 러시아마사지 during my business trip, and it was the best decision ever.

Avatar_small
civaget said:
Thu, 14 Dec 2023 04:58:59 -1100 Hello there, simply become aware of your weblog thru Google, and located that it is really informative. I’m going to be careful for brussels. I’ll be grateful when you continue this in future. Numerous people shall be benefited from your writing. Cheers! 해외축구중계
Avatar_small
civaget said:
Fri, 15 Dec 2023 23:05:53 -1100

Elevate your well-being at 울산오피, a wellness haven in Ulsan, known for its holistic approach, skilled therapists, and serene ambiance.

Avatar_small
civaget said:
Mon, 18 Dec 2023 06:32:51 -1100

I can't believe I ever paid for cable with 누누티비 around.

Avatar_small
civaget said:
Mon, 18 Dec 2023 20:08:05 -1100

I'm hooked on 티비위키 for its exceptional content quality.

Avatar_small
civaget said:
Mon, 18 Dec 2023 22:45:20 -1100

대구오피's spa day is a must-try. It's a holistic experience that leaves you feeling rejuvenated and ready to take on the world.

Avatar_small
civaget said:
Wed, 20 Dec 2023 08:22:03 -1100

대구휴게텔's ambiance is serene and peaceful. The massages are top-notch, making it a perfect escape from the daily grind.

Avatar_small
civaget said:
Sat, 23 Dec 2023 19:13:52 -1100

설문조사 사이트 무료 options empower individuals and businesses to reach their target audience effectively.

Avatar_small
civaget said:
Tue, 26 Dec 2023 03:06:31 -1100

Some times its a pain in the ass to read what blog owners wrote but this site is really user pleasant! . 카지노사이트

Avatar_small
civaget said:
Tue, 26 Dec 2023 03:39:47 -1100

Connect with the global sports community and witness the magic of sports worldwide through 해외스포츠중계.

Avatar_small
civaget said:
Tue, 26 Dec 2023 09:36:08 -1100

The ability to experiment with different book covers for different editions is a creative advantage. self publishing on amazon

Avatar_small
civaget said:
Sat, 30 Dec 2023 18:50:06 -1100

I must say, as a lot as I enjoyed reading what you had to say, I couldnt help but lose interest after a while. Its as if you had a wonderful grasp on the subject matter, but you forgot to include your readers. Perhaps you should think about this from far more than one angle. Or maybe you shouldnt generalise so considerably. Its better if you think about what others may have to say instead of just going for a gut reaction to the subject. Think about adjusting your own believed process and giving others who may read this the benefit of the doubt. Divine Revelations


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter