Kernel VM allocation

Hello,

I'm new to freebsdFreeBSD kernel programming. I'm currently trying to allocate kernel RWX pages, but without any success.
I did this:


Code:
 void* addr = 0;
  u32 size = PAGE_SIZE;
  u32 maxprot = PROT_WRITE | PROT_READ | PROT_EXEC;
  u32 prot = maxprot;
  int docow = 0;
  int fspace = VMFS_ANY_SPACE;
  int ret = vm_map_find(kernel_map, 0, 0, &addr, size, fspace, prot, maxprot, docow);

vm_map_find returns 0, addr is also valid, but when i tried to access the page, with a read for example, it causes a page_fault (that's normal since the page has never been accessed before)
But then in vm_fault, a new page fault occurs and crashes the system.

Did I miss something here?
Does anyone have another way to allocate a kernel RWX page ?

Thanks
 
Not sure why you need to do this when you can use the kernel malloc(9) to allocate memory. What exactly are you trying to accomplish? Memory allocation?

I've never really used this function, but I'll give it a stab. Instead of using 0 for the object, use NULL. It's bad practice to use 0 in place of NULL. According to vm_map_find(9), it does not return 0. It returns one of the following three values: KERN_SUCCESS, KERN_NO_SPACE, and KERN_INVALID_ADDRESS. Check for one of those instead of using a numeric constant.

What version of FBSD are you running? The man page for 10.2 shows 10 arguments, you have 9.

Furthermore, page faults in kernel mode are illegal and will panic the system. This is why I use the kernel malloc as it does all this for you. Not sure why you want to be able to execute the memory region, but since you have kernel_map, once you use malloc, you should be able to use vm_map_madvise(9) to set flags as needed. The problem is that I believe that start and end must be on a page boundary, which is not guaranteed with malloc.
 
Instead of using 0 for the object, use NULL. It's bad practice to use 0 in place of NULL.

This was good advice in pre-ANSI C but with ANSI C literal zero 0 is guaranteed to be equal to NULL when assigned to a pointer or compared with any pointer. In other words, it does not matter how exotic the actual representation of the NULL pointer is internally, on the C language level literal 0 is always converted automatically to the correct internal representation.
 
Stylistically some people prefer to use NULL over 0 with pointers because you can then spot the places where such assignments happen more easily, I'm not one of them but style(9) seem to say that NULL should be used in FreeBSD code.
 
Back
Top