Muli Ben-Yehuda's journal

June 9, 2004

why user programs can’t access the kernel’s mapping, cont

Filed under: Uncategorized — Muli Ben-Yehuda @ 2:58 PM

Yesterday I wrote about “the story of a page
fault”
, ending with “What I am not absolutely convinced about is
why the fault happens in the first place. The options are that it gets
a fault because the kernel is mapped, but the protection on the ptes
is such that only code running in ring 0 (i.e. the kernel) can access
them, or that the kernel is not mapped while we are running in user
space, and is only mapped in when we context switch to kernel
space. The former makes a lot more sense, but I haven’t yet hunted
down the code that does it.”

Well, I found it 🙂

in 2.6.7-rc3, arch/i386/mm/pgtable.c, pgd_ctor():

void pgd_ctor(void *pgd, kmem_cache_t *cache, unsigned long unused)
{
	unsigned long flags;

	if (PTRS_PER_PMD == 1)
		spin_lock_irqsave(&pgd_lock, flags);

	/* muli: copy into the user's pgd the portion of the swapper_pg_dir that maps the kernel */ 
	memcpy((pgd_t *)pgd + USER_PTRS_PER_PGD,
			swapper_pg_dir + USER_PTRS_PER_PGD,
			(PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));

	if (PTRS_PER_PMD > 1)
		return;

	pgd_list_add(pgd);
	spin_unlock_irqrestore(&pgd_lock, flags);

	/* muli: set the rest to 0 */ 
	memset(pgd, 0, USER_PTRS_PER_PGD*sizeof(pgd_t));
}

The way I found it is to go through mingo’s 4:4 split patch,
which obviously needs to change this bit, since the kernel is no
longer mapped in each process’s address space.

Advertisements

2 Comments »

  1. Yes, this is called for newly created processes.
    BTW, as far as I remember from the 2.4.x implementation – the PGDs of the vmalloc area are also copied from swapper_pg_dir, but it occurs on-demand during page faults
    in that range.
    I really like mingo’s 4:4 patch. It reminds me what I did in coLinux. Who needs them long-lasting TLBs anyway? 😉

    Comment by da_x — June 11, 2004 @ 1:16 AM | Reply

    • don’t tell Linus… I seem to recall the phrase “4:4 horrors” coming up on lkml a couple of times 😉

      Comment by mulix — June 13, 2004 @ 9:43 AM | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: