是否可以获得 mmap 的 Linux 源代码和 MapViewOfFile 的 Windows 源代码?

发布于 2024-11-09 20:03:48 字数 670 浏览 1 评论 0原文

下午Goog,我最近下载了www.kernel.org mainline 2.6.39 Linux kernel源代码发行版。我们正在寻找 void mmap(void start , size_t length, int prot, int flags, int fd, off_t offset) 的 Linux 源代码。解压tar.bz2发行版后,我们发现了一个文件mmap.c,其中包含内存映射源代码。 但是,我们无法获取 mmap.c 中 void mmap(void start , size_t length, int prot, int flags, int fd, off_t offset) 的 Linux 源代码。 有 Linux 工程师或管理员知道我们从哪里获得 void mmap(void start , size_t length, int prot, int flags, int fd, off_t offset) 的 Linux 源代码吗? 此外,我们对 MapViewOfFile 的 Windows 源代码感兴趣。我知道这有些牵强,因为微软操作系统源代码并不在开源领域。 如果有人想知道为什么我们需要这个源代码,我们正在尝试使用 32 位架构上的缓存内存映射文件实现来优化 C++ 重复数据删除程序原型的运行时性能。我们想了解如何使用 mmap 和 MapViewOfFile 来优化原型的运行时性能?谢谢。

Goog afternoon, I recently downloaded the www.kernel.org mainline 2.6.39 Linux kernel source distribution. We are looking for the Linux source code for void mmap(void start , size_t length, int prot, int flags, int fd, off_t offset). After we decompressed the tar.bz2 distribution, we found a file mmap.c which contains memory mapping source code.
However, we could not the Linux source code for void mmap(void start , size_t length, int prot, int flags, int fd, off_t offset) in mmap.c.
Do any Linux engineers or adminstrators knwo where we obtain the Linux source code for void mmap(void start , size_t length, int prot, int flags, int fd, off_t offset)?
Also, we are interested in the Windows source code for MapViewOfFile. I know this a stretch because Microsoft OS source code is not in the open source domain.
In case anybody is wondering why we need this source code, we are trying to optimize the runtime performant of a C++ deduplicating program prototype using a cached memory mapped file implementation on a 32-bit architecture. We want to understand how to use mmap and MapViewOfFile in order the optimize the runtime performant of our prototype? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

动听の歌 2024-11-16 20:03:48

从android源代码中提取;

#include <unistd.h>
#include <errno.h>
#include <sys/mman.h>

extern void*  __mmap2(void*, size_t, int, int, int, size_t);

#define  MMAP2_SHIFT  12
void*   mmap( void*  addr,  size_t  size, int  prot, int  flags, int  fd,  long  offset )
{
    if ( offset & ((1UL << MMAP2_SHIFT)-1) ) {
    errno = EINVAL;
    return MAP_FAILED;
  }

    return __mmap2(addr, size, prot, flags, fd, (size_t)offset >> MMAP2_SHIFT);
}

来源: mmap.c

现在,实际的 __mmap2 调用已经有了程序集,因此它将取决于您的架构。这是 x86 版本:

/* autogenerated by gensyscalls.py */
#include <sys/linux-syscalls.h>

    .text
    .type __mmap2, @function
    .globl __mmap2
    .align 4

__mmap2:
    pushl   %ebx
    pushl   %ecx
    pushl   %edx
    pushl   %esi
    pushl   %edi
    pushl   %ebp
    mov     28(%esp), %ebx
    mov     32(%esp), %ecx
    mov     36(%esp), %edx
    mov     40(%esp), %esi
    mov     44(%esp), %edi
    mov     48(%esp), %ebp
    movl    $__NR_mmap2, %eax
    int     $0x80
    cmpl    $-129, %eax
    jb      1f
    negl    %eax
    pushl   %eax
    call    __set_errno
    addl    $4, %esp
    orl     $-1, %eax
1:
    popl    %ebp
    popl    %edi
    popl    %esi
    popl    %edx
    popl    %ecx
    popl    %ebx
    ret

来源:__mmap2.S

Pulling from android source code;

#include <unistd.h>
#include <errno.h>
#include <sys/mman.h>

extern void*  __mmap2(void*, size_t, int, int, int, size_t);

#define  MMAP2_SHIFT  12
void*   mmap( void*  addr,  size_t  size, int  prot, int  flags, int  fd,  long  offset )
{
    if ( offset & ((1UL << MMAP2_SHIFT)-1) ) {
    errno = EINVAL;
    return MAP_FAILED;
  }

    return __mmap2(addr, size, prot, flags, fd, (size_t)offset >> MMAP2_SHIFT);
}

Source: mmap.c

Now, the actual __mmap2 call has assembly, so it's going to depend on your arch. Here's an x86 version:

/* autogenerated by gensyscalls.py */
#include <sys/linux-syscalls.h>

    .text
    .type __mmap2, @function
    .globl __mmap2
    .align 4

__mmap2:
    pushl   %ebx
    pushl   %ecx
    pushl   %edx
    pushl   %esi
    pushl   %edi
    pushl   %ebp
    mov     28(%esp), %ebx
    mov     32(%esp), %ecx
    mov     36(%esp), %edx
    mov     40(%esp), %esi
    mov     44(%esp), %edi
    mov     48(%esp), %ebp
    movl    $__NR_mmap2, %eax
    int     $0x80
    cmpl    $-129, %eax
    jb      1f
    negl    %eax
    pushl   %eax
    call    __set_errno
    addl    $4, %esp
    orl     $-1, %eax
1:
    popl    %ebp
    popl    %edi
    popl    %esi
    popl    %edx
    popl    %ecx
    popl    %ebx
    ret

Source: __mmap2.S

∞觅青森が 2024-11-16 20:03:48

要获得 Win32 MapViewOfFile 实现,您必须支付昂贵的订阅费、签署合法的保密协议等。linux

mmap 是公开可读的。但是,您应该知道有两个部分:glibc 中的 mmap 函数,以及内核中匹配的系统调用,所有有趣的部分都在其中。您显示的签名适用于 glibc 函数,不要期望系统调用具有完全相同的参数。

但是您可以“了解如何使用 mmapMapViewOfFile”,而无需阅读实现。

To get the Win32 MapViewOfFile implementation, you will have to pay an expensive subscription, sign legal nondisclosure agreements, etc.

linux mmap is publicly readable. However, you should know that there are two parts: The mmap function in glibc, and the matching syscall in the kernel, where all the interesting bits are. The signature you showed is for the glibc function, don't expect the syscall to have the exact same parameters.

But you can "understand how to use mmap and MapViewOfFile" without reading the implementation.

梦里人 2024-11-16 20:03:48

对于 MapViewOfFile,我会检查 Winbase.h,但它可能只是声明,否则您必须转向逆向工程,这在大多数国家/地区被认为是非法的。

我在此处找到了一篇关于 MMAP 及其工作原理的广泛文章。也许这有帮助。

For MapViewOfFile, I'd check Winbase.h, but it might just be the declarations, else you'd have to turn to reverse engineering, which is considered illegal in most countries.

I have found an extensive article here on MMAP and how it works. Maybe that helps.

小霸王臭丫头 2024-11-16 20:03:48

我的直接猜测是,试图找出如何更好地从源代码到函数使用这些功能可能(充其量)是一种非常迂回的方式,可以得到很多东西。特别是,您可能需要查看更多/其他代码才能获得大部分内容。当您认真思考时,mmap/MapViewOfFile 中的代码本身可能起的作用很小(最多),而其他代码(例如文件系统驱动程序和文件缓存中的代码)可能更有意义。

当你认真思考时,mmap 和 MapViewOfFile 的工作相对简单:设置页面描述符,将一系列虚拟地址映射到某个文件的某些部分。

在您尝试访问这些页面之一之前,不会发生更多其他情况。这将触发“不存在”故障。故障处理程序将使用I/O子系统从磁盘读入相应的数据,并返回让原始指令执行。仍然没有多少非常有趣的事情。

至少从优化的角度来看,事情变得有趣的地方在于 I/O 子系统内部。例如,这可以跟踪页面错误的历史记录,并使用它来预测可能很快需要哪些页面(如果是这样,则在错误发生之前发出对预测页面的读取)。

然而,在最好的情况下,mmap/MapViewOfFile 的源代码根本不会直接引导到您可能关心的 I/O 子系统部分(事实上,它们在这方面可能几乎完全无用) 。

My immediate guess would be that trying to figure out how to use these better from the source code to the functions is likely to be (at best) a very roundabout way to get much of anywhere. In particular, you're probably going to have to look at quite a lot more/other code to get much of anywhere. When you get down to it, the code in mmap/MapViewOfFile itself is likely to be of minimal help (at best) and other code (such as in the file-system driver and file cache) is likely to be a lot more meaningful.

When you get down to it, mmap and MapViewOfFile have relatively simple jobs: set up page descriptors that map a range of virtual addresses to some part(s) of some file.

Not much else/more happens until you attempt to access one of those pages. That'll trigger a "not present" fault. The fault handler will use the I/O subsystem to read in the corresponding data from the disk, and the return to let the original instruction execute. Still not much that's very interesting.

At least from the viewpoint of optimization, the point that things get interesting is inside the I/O subsystem. That may (for example) keep track of the history of page faults, and use that to predict what pages are likely to be needed soon (and if so, issue reads for the predicted pages before the fault happens).

At best, however, the source code to mmap/MapViewOfFile isn't going to lead at all directly to the parts of the I/O subsystem you probably care about (in fact, they'll probably be almost entirely useless in this respect).

a√萤火虫的光℡ 2024-11-16 20:03:48

有趣,但我也需要 mmap 源代码...:)

到目前为止我只能在 NetBSD 上找到答案。
基于此评论

  • libc函数位于src/lib/libc/sys/mmap.c
  • 对应的内核函数在src/sys/uvm/uvm_map.c

    int uvm_map(struct vm_map *map, vaddr_t startp / IN/OUT */, vsize_t 大小,
    struct uvm_object *uobj, voff_t uoffset, vsize_talign, uvm_flag_t flags)

实际链接对应于 NetBSD 版本 8,但您可以将其替换为任何其他版本。

问候。

Funny, but I needed mmap source, too... :)

So far I could find the answer only on NetBSD.
Based on this comment:

  • The libc function is in src/lib/libc/sys/mmap.c
  • The corresponding kernel function is in src/sys/uvm/uvm_map.c:

    int uvm_map(struct vm_map *map, vaddr_t startp / IN/OUT */, vsize_t size,
    struct uvm_object *uobj, voff_t uoffset, vsize_t align, uvm_flag_t flags)

The actual links correspond to NetBSD release 8, but you can replace it with any other release.

Regards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文