linux - 在 Ubuntu 10.04(64 位机器)上访问物理内存时 PC 完全死机

标签 linux memory linux-kernel ubuntu-10.04

我正在尝试使用这个使用 mmap() 的程序访问我的 PC 的物理内存-

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/mman.h>

#define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
  __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)

#define MAP_SIZE 4096UL
#define MAP_MASK (MAP_SIZE - 1)

int main(int argc, char **argv) {
    int fd;
    void *map_base, *virt_addr;
    unsigned long read_result, writeval;
    off_t target;
    int access_type = 'w';

    if(argc < 2) {
        fprintf(stderr, "\nUsage:\t%s { address } [ type [ data ] ]\n"
            "\taddress : memory address to act upon\n"
            "\ttype    : access operation type : [b]yte, [h]alfword, [w]ord\n"
            "\tdata    : data to be written\n\n",
            argv[0]);
        exit(1);
    }
    target = strtoul(argv[1], 0, 0);

    if(argc > 2)
        access_type = tolower(argv[2][0]);


    if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL;
    printf("/dev/mem opened.\n");
    fflush(stdout);

    /* Map one page */
    map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);
    if(map_base == (void *) -1) FATAL;
    printf("Memory mapped at address %p.\n", map_base);
    fflush(stdout);

    virt_addr = map_base + (target & MAP_MASK);
    switch(access_type) {
        case 'b':
            read_result = *((unsigned char *) virt_addr);
            break;
        case 'h':
            read_result = *((unsigned short *) virt_addr);
            break;
        case 'w':
            read_result = *((unsigned long *) virt_addr);
            break;
        default:
            fprintf(stderr, "Illegal data type '%c'.\n", access_type);
            exit(2);
    }
    printf("Value at address 0x%X (%p): 0x%X\n", target, virt_addr, read_result);
    fflush(stdout);

    if(argc > 3) {
        writeval = strtoul(argv[3], 0, 0);
        switch(access_type) {
            case 'b':
                *((unsigned char *) virt_addr) = writeval;
                read_result = *((unsigned char *) virt_addr);
                break;
            case 'h':
                *((unsigned short *) virt_addr) = writeval;
                read_result = *((unsigned short *) virt_addr);
                break;
            case 'w':
                *((unsigned long *) virt_addr) = writeval;
                read_result = *((unsigned long *) virt_addr);
                break;
        }
        printf("Written 0x%X; readback 0x%X\n", writeval, read_result);
        fflush(stdout);
    }

    if(munmap(map_base, MAP_SIZE) == -1) FATAL;
    close(fd);
    return 0;
}

当我以 sudo ./a.out 0xfdff8000 运行此程序时,我的系统只是挂起。鼠标、键盘、显示器一切都定格了!重启是唯一的选择。我在/proc/iomem 中检查了 0xfdff8000。它对应于 ICH HD 音频。我不确定这意味着什么。 kmsg、dmesg 和/var/log/messages 也没有给出任何提示!

最佳答案

您正在从芯片组的 IO 内存中的某个地方读取一个单词。我不能确切地告诉你为什么这会卡住你的系统,但是在不知道地址上有什么硬件寄存器或者不知道对有问题的硬件进行编程的正确方法的情况下偷看和戳入 IO 内存很可能会卡住你的系统。

关于linux - 在 Ubuntu 10.04(64 位机器)上访问物理内存时 PC 完全死机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6148061/

相关文章:

linux-kernel - 如何在 Rust 中调用 "ioctl"?并连接 Linux "tun"驱动程序

c - 不同的字符串如何具有相同的地址

java - tomcat 中的两个 war 文件与每个 tomcat 中的每个 war 文件 - 性能比较

c - 处理来自task_struct 的条目。为什么有些任务->下一个有0。如何转到下一个任务比?

json - 如何使用 Jq 将 JSON 文件的对象转换为对象数组?

python - 如何在python中读取大型压缩文件而不将其全部加载到内存中

linux - 在 Linux 上写 IO 中断?

c - 如何在Linux中的C编程中使用其IP地址获取远程主机的MAC地址

linux - 我如何防止 X11 为 tty7 设置 opost?

php - 文件包含从 Linux 到 Windows