Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - Write to physical memory from kernel module

In the kernel module, I need to handle the interrupt by writing a "zero" to address of physical memory.

First of all, I should allocate a memory by some function like "mmap", but for kernel module; for example, ioremap.

static irqreturn_t int068_interrupt(int irq, void *dev_id)
{
    unsigned int *p;
    unsigned int address;
    unsigned int memsize;

    address = 0x12345678;
    memsize = 1024;

    p = ioremap(address, memsize);
    p[0]=0;

    printk("Interrupt was handled\n");

    return IRQ_HANDLED;
}

However, the kernel crashes when interrupt comes and interrupt handler starts handling it (kernel BUG at mm/vmalloc.c:numberofline)

It seems that something wrong with my usage of ioremap, or I should use another "kernel substitute of mmap"

Please tell me, how to workaround this problem?

like image 754
Jake Badlands Avatar asked Mar 06 '13 13:03

Jake Badlands


People also ask

Can kernel access physical memory?

The physical memory can only be directly accessed in kernel-mode debugging.

How do I transfer data from kernel space to user space?

The function copy_to_user is used to copy data from the kernel address space to the address space of the user program. For example, to copy a buffer which has been allocated with kmalloc to the buffer provided by the user.

Can kernel access user memory?

Whilst a user-space program is not allowed to access kernel memory, it is possible for the kernel to access user memory. However, the kernel must never execute user-space memory and it must also never access user-space memory without explicit expectation to do so.

Can you use malloc in kernel?

The malloc () function allocates uninitialized memory in kernel address space for an object whose size is specified by size . The mallocarray () function is the same as malloc (), but allocates space for an array of nmemb objects and checks for arithmetic overflow.


1 Answers

directly from Linux ioremap.c:

If you iounmap and ioremap a region, the other CPUs will not see this change until their next context switch. Meanwhile, (eg) if an interrupt comes in on one of those other CPUs which requires the new ioremap'd region to be referenced, the CPU will reference the old region.

This strikly asks to avoid ioremap call within an interrupt service routine.

like image 61
Arno Avatar answered Oct 19 '22 22:10

Arno