当我们替换时,8位内存访问的行为如何用正常的内存分配替换/ dev / mem映射 [英] How 8 bit memory access behaves when we replace replace the /dev/mem mapping with a normal memory allocation

查看:65
本文介绍了当我们替换时,8位内存访问的行为如何用正常的内存分配替换/ dev / mem映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码,它给出了分段错误(核心)

This is the code and it gives segmentation fault (core)

static uint32_t map_size = 0x08000000;
static uint32_t map_base = 0x18000000;
static uint32_t map_addr = 0x00000000;
static uint64_t cycle_count = 0x1000000;


static char *dev_mem = "/dev/mem";

int main(int argc, char **argv) {
int fd;
uint8_t *buf;
if ((fd = open(dev_mem, O_RDWR | O_SYNC)) == -1) {
    printf("can't open /dev/mem .\n");
    exit(EXIT_FAILURE);
}

buf = (uint8_t *) malloc(sizeof(uint8_t));
if (buf == 0) {
    printf("Can't be mapped. \n");
    exit(EXIT_FAILURE);
} else
    map_addr = (long unsigned) buf;

uint8_t sum = 0;

while (cycle_count-- > 0)
    sum += *buf++;

printf("%u\n", sum);
close(fd);
exit(EXIT_SUCCESS);


return 0;
  }





我的尝试:



因此,我观察了各种位字的执行时间如何表现,下面是读取8位字的程序示例。我想检查当我们用正常内存分配(即malloc / calloc)替换/ dev / mem映射时它的行为。但是我的代码开始给出分段错误(Core dump)。任何帮助?



我是新来的,很抱歉,如果错误是愚蠢的。



What I have tried:

So, I was observing how execution time behaves for various bit word, below is example of a program that read 8 bits word. I wanted to check how it behaves when we replace the /dev/mem mapping with a 'normal' memory allocation (i.e by malloc/calloc). But my code started giving segmentation fault (Core dump). Any help?

I am new to this, so sorry if the errors are silly.

推荐答案

仔细研究这段代码的内容。故障原因对我来说很明显。首先,打开的文件/ dev / mem不用于任何内容。删除对它的所有引用,您将看到代码仍然编译。问题是你正在分配一个大小为1字节的缓冲区(sizeof(uint8_t)),假设它是8位,为它指定一个指针,然后尝试访问该缓冲区的0x1000000字节。可能在没有故障的情况下运行的唯一方法是在具有不受保护的扁平地址空间的处理器上。缓冲区必须至少为0x1000000字节才能使代码正确运行。



实际上,在我看来,你试图将文件映射到内存中你的应用空间。查找函数MapViewOfFile和CreateFileMapping来执行此操作。
Study carefully what is going in this code. The reason for the fault is obvious to me. To begin, the file that is opened, /dev/mem, is not used for anything. Remove all references to it and you will see the code still compiles. The problem is you are allocating a buffer of size one byte (sizeof(uint8_t)), assuming that is 8 bits, assigning a pointer to it, and then trying to access 0x1000000 bytes of that buffer. The only way that might possibly run without faulting is on a processor with an unprotected, flat address space. The buffer has to be a minimum of 0x1000000 bytes for the code to run correctly.

Actually, it appears to me that you are attempting to map a file into the memory space of your app. Look up the functions MapViewOfFile and CreateFileMapping to do this.


您必须知道如何在(Linux)系统上管理内存。当调用 malloc()时,将检查应用程序堆上是否有足够的空间来满足请求。如果没有,则通过从系统请求更多内存来增加程序堆。实际的堆由操作系统分配给您的应用程序,以便它可以对该内存执行任何操作。但访问未分配给您的应用程序的内存受到保护并导致分段错误。



这就是解决方案1中所描述的:您正在分配缓冲区1个字节,但访问0x100000 = 1 MB。



虽然访问该1字节缓冲区的第一个字节通常是有效的,因为堆上可能有一些剩余(未使用的)内存,但是一旦你的<$ $就会出现分段错误c $ c> buf 指针离开应用程序堆(更精确:留下操作系统分配给应用程序的内存范围)。
You have to know how memory is managed on your (Linux) system. When calling malloc(), it will be checked if there is enough space on the heap of the application to satisfy the request. If not, the heap of your program will be increased by requesting more memory from the system. The actual heap is assigned to your application by the operating system so that it can do anything with that memory. But accessing memory not assigned to your application is guarded and results in a segmentation fault.

That is what happens here as described in solution 1: you are allocating a buffer of 1 byte but accessing 0x100000 = 1 MB.

While accessing the first bytes of that 1 byte buffer usually works because there might be some remaining (unsused) memory on the heap, the segmentation fault occurs once your buf pointer leaves the heap of your application (more precise: leaves the range of memory that has been assigned to your application by the operating system).


这篇关于当我们替换时,8位内存访问的行为如何用正常的内存分配替换/ dev / mem映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆