Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 5 Jul 2001 14:59:17 +0200
From:      Bernd Walter <ticso@mail.cicely.de>
To:        "Eugene L. Vorokov" <vel@bugz.infotecs.ru>
Cc:        freebsd-hackers@FreeBSD.ORG
Subject:   Re: kernel panic when trying to use init's address space
Message-ID:  <20010705145917.A7717@cicely20.cicely.de>
In-Reply-To: <200107051251.f65CpMp03726@bugz.infotecs.ru>; from vel@bugz.infotecs.ru on Thu, Jul 05, 2001 at 04:51:22PM %2B0400
References:  <200107051251.f65CpMp03726@bugz.infotecs.ru>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, Jul 05, 2001 at 04:51:22PM +0400, Eugene L. Vorokov wrote:
> Hello,
> 
> Some time ago I was asking about I/O in kernel mode when I don't have
> struct proc to use syscalls. Actually I just wanted my kld to read it's
> config file on load. Terry told me it's tricky, and I was thinking
> about possible workarounds. I decided to try the following: look for
> some process, get it's struct proc, allocate memory in it's address
> space using mmap() syscall and then use open() and read() syscalls,
> passing that struct proc to them. I first decided to look for init
> process for this, since it always exists. So it looked like that:
> 
>  struct proc *p; register_t save; char *buf;
>  struct mmap_args mem; int res;
> 
>  for (p = allproc.lh_first;
>       p && (strcmp(p->p_comm, "init"));
>       p = p->p_list.le_next);

If yhou don't care whicvh process you can just do:
struct proc *p = &proc0;

>  if (!p)
>   return -1;
>  save = p->p_retval[0];
>  mem.addr = NULL;
>  mem.len = size;   
>  mem.prot = PROT_READ | PROT_WRITE;
>  mem.flags = MAP_ANON;
>  mem.fd = -1;
>  mem.pad = 0;
>  mem.pos = 0;       
>  res = mmap(p, &mem);
>  if (res)
>   {
>    p->p_retval[0] = save;
>    return -1;
>   }
>  buf = (char *)p->p_retval[0];
>  p->p_retval[0] = save;
>  *buf = 0; 
> 
> However at this point kernel panics with page fault. I really don't
> understand why could it be ...
> 
> Of course, I've found another workaround. I recalled that kldload
> program is still active when my module loads, so I started looking
> for it instead of init. It works just fine, I'm able to allocate
> memory, use it and finally read my config file. But I'm curious,
> why doesn't it work with init ? What's so special in init from this
> point of view ?

You are mmaping into the address space for the process you use the
struct proc from.
As long as it's this programm that is curproc everything is fine.
That means you are called from that process such in kldload or
interrupted that proccess.
What you need is to use the address space that is common to all
proccess while working in kernel mode not into the proccess specific.
I don't know if it is possible to mmap into that space.

-- 
B.Walter              COSMO-Project         http://www.cosmo-project.de
ticso@cicely.de         Usergroup           info@cosmo-project.de


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010705145917.A7717>