How to use hugetlb for the text of a program ?

dada1
2003-08-13T09:12:03Z

Hi all

I'm trying to use a 4Mo page on a i686 to map the text portion of a
statically linked program.
I used a link script to make the text mapped to 0x00400000
The datas start at next 4M boundary (0x00800000)

architecture: i386, flags 0x00000112:
EXEC_P, HAS_SYMS, D_PAGED
start address 0x00400100

Program Header:
LOAD off 0x00000000 vaddr 0x00400000 paddr 0x00400000 align 2**12
filesz 0x00080ebc memsz 0x00080ebc flags r-x
LOAD off 0x00081000 vaddr 0x00800000 paddr 0x00800000 align 2**12
filesz 0x00005994 memsz 0x00047870 flags rw-
NOTE off 0x00000094 vaddr 0x00400094 paddr 0x00400094 align 2**2
filesz 0x00000020 memsz 0x00000020 flags r--

Next, I tried to use the following function in a hope to copy the text into
a single 4Mo page :

#include <sys/mman.h>
#include <fcntl.h>

#define HUGESZ 0x400000 /* size of one 4M page*/
#define TEXTSTART (char*)0x00400000 /* see ldscript */

void hugerelocate()
{
int fd = open("/huge/textfile", O_RDWR | O_CREAT, 0644) ;
char *ptr ;
extern int _etext ;
if (fd == -1) return ;
ftruncate(fd, 0) ;
ftruncate(fd, HUGESZ) ;
ptr = mmap((char *)0x10000000, HUGESZ, PROT_READ|PROT_WRITE, MAP_SHARED, fd,
0) ;
if (ptr == (char *)1) { close(fd);return;}
memcpy(ptr, TEXTSTART, (char *)&_etext - TEXTSTART) ;
msync(ptr, HUGESZ, MS_SYNC) ;
mmap(TEXTSTART, HUGESZ, PROT_READ|PROT_EXEC,
MAP_SHARED|MAP_FIXED|MAP_POPULATE, fd, 0) ;
close(fd) ;
munmap(ptr, HUGESZ) ;
}

The msync() call produces this kernel message (linux-2.6.0-test3)
mm/msync.c:52: bad pmd 108000e7.

The problem I have is the last mmap(), trying to replace the program text by
the 4M page, just kills the program.

If I try to use a regular file (not on a hugetlbfs), the program is killed
to.

I tried other MAP_??? flags without success.

Do you know if what I'm trying to do is possible ? ie is a hugetlb page OK
with PROT_EXEC ?

Alternatives :

- I was thinking to write a special loader to load the program, but I dont
know how to cope with the brk()

- A combination of a linker/kernel new feature :
ELF tagged program to ask the kernel to use 4Mo pages if possible to
load the text of a program from the executable file, instead of mapping it
(only for superuser users)

Thanks

Eric Dumazet