Find the answer to your Linux question:
Results 1 to 6 of 6
Hello I want to write to some specific physical address in the system memory space. I am writing a driver for it. I am using following functions in module_init: check_region() ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2009
    Posts
    8

    help regarding memory driver


    Hello

    I want to write to some specific physical address in the system memory space. I am writing a driver for it. I am using following functions

    in module_init:
    check_region() to check the availability of that specific physical address.
    request_mem_region() to request the memory resource from the kernel in case of successful check_region(). The function request_mem_region() returns success which means (as far as I assume) that the resource has been allocated to my driver.

    in write function
    If the request_region() returns success then I must be able to write to that memory address just freely.
    For example: *(char *)myphysicaladdr = some value

    But when I open the driver file from /dev and write a charater to it, segmentation fault occurs. Kindly somebody help me what should I do?

    outb() is used to write to an i/o port address. Kindly tell me is there any function to write to physical address?

    regards,
    Atif Shabir

  2. #2
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    debug your code to find out why you are getting a seg fault

    the most common cause of seg fault is dereferencing null pointer and accessing uninitialized memory

  3. #3
    Just Joined!
    Join Date
    Oct 2009
    Posts
    8

    BUG: unable to handle kernel NULL pointer dereference at 00000100

    Thank you coopstah13 for your reply.

    A piece of my source code is

    #define PHYSICAL_ADDR 0x00000100
    #define MEM_SIZE 2

    int memory_init(void)
    {
    int result;

    /*Registering device*/
    result = register_chrdev(memory_major, "my_module", &memory_fops);
    if(result < 0) {
    printk("<1> my_module: cannot obtain major number %d\n", memory_major);
    return result;
    }

    /*Allocating memory byte for the port*/
    my_port = check_region(PHYSICAL_ADDR, MEM_SIZE); //check_region returns 0 (success)
    if(my_port) {
    printk("<1>my_module: cannot reserve 0x%X\n",PHYSICAL_ADDR);
    result = my_port;
    goto fail;
    }

    alloc_result = request_mem_region(PHYSICAL_ADDR, MEM_SIZE, "my_module");
    //request_mem_region() returns 0 (success)
    if(alloc_result) {
    printk("<1>my_partport2: Requested memory region not allocated\n");
    goto fail;
    }
    printk("<1>my_module: Requested memory region allocated successfully....Inserting Module\n");
    return 0;

    fail:
    memory_exit();
    return result;
    }

    ssize_t memory_read(struct file *filp, char *buf,
    size_t count, loff_t *f_pos) {

    char my_parport_buf;

    ////////////////////////////////////////////
    my_parport_buf = *((char *)PHYSICAL_ADDR); //This statement gives the segmentation fault
    ///////////////////////////////////////////////

    copy_to_user(buf, &my_parport_buf, 1);
    if(*f_pos == 0) {
    *f_pos+=1;
    return 1;
    } else {
    return 0;
    }
    }


    When I run a C program that opens and reads one character from the file /dev/my_module, segmentation fault occurs. dmesg shows following

    BUG: unable to handle kernel NULL pointer dereference at 00000100
    IP: [<f8b6f080>] :my_parport2:memory_read+0x1c/0x66
    *pdpt = 00000000243e6001 *pde = 0000000000000000
    Oops: 0000 [#2] SMP


    Kindly tell me where my the problem may lie. Am I on the right path for writing on physical memory? or should I adopt a new path?

    regards,
    Atif Shabir

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    Code:
    my_parport_buf = *((char *)PHYSICAL_ADDR); //This statement gives the segmentation fault
    you are dereferencing a null pointer, you need to initialize PHYSICAL_ADDR

  6. #5
    Just Joined!
    Join Date
    Oct 2009
    Posts
    8
    I have #defined PHYSICAL_ADDR to be 0x00000100

    and how will I initiallize this pointer? I mean if I use kalloc(), the kernel will allocate me an address from any where in the memory. But If want to allocate specifically 0x00000100 physical address then what should I do?

  7. #6
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    Is there any reason you need to allocate at a specific memory address? What if something else is already using it? If you are just looking to write a driver that you can write something to memory from and read it from anywhere, then any address should be fine...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •