View Single Post
Old 2021-09-28, 18:13   #7
retina
Undefined
 
retina's Avatar
 
"The unspeakable one"
Jun 2006
My evil lair

2·23·137 Posts
Default

Quote:
Originally Posted by retina View Post
You can freely allocate PiB of memory (so I have read, not tried it myself) ...
That last part in brackets is incorrect. I got curious so I tried it.

I allocated 2PiB of RAM on the smallest machine I could find with 4GiB installed.
Code:
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
16894 retina    20   0  0.125p   2052      4 S   0.0  0.1   0:00.10 mem_reserve_tes
16906 retina    20   0  0.125p   2052      4 S   0.0  0.1   0:00.10 mem_reserve_tes
16914 retina    20   0  0.125p   2052      4 S   0.0  0.1   0:00.10 mem_reserve_tes
16922 retina    20   0  0.125p   2052      4 S   0.0  0.1   0:00.10 mem_reserve_tes
16930 retina    20   0  0.125p   2052      4 S   0.0  0.1   0:00.10 mem_reserve_tes
16938 retina    20   0  0.125p   2052      4 S   0.0  0.1   0:00.10 mem_reserve_tes
16946 retina    20   0  0.125p   2052      4 S   0.0  0.1   0:00.11 mem_reserve_tes
16954 retina    20   0  0.125p   2052      4 S   0.0  0.1   0:00.10 mem_reserve_tes
16962 retina    20   0  0.125p   2052      4 S   0.0  0.1   0:00.10 mem_reserve_tes
16970 retina    20   0  0.125p   2052      4 S   0.0  0.1   0:00.10 mem_reserve_tes
16978 retina    20   0  0.125p   2052      4 S   0.0  0.1   0:00.10 mem_reserve_tes
16990 retina    20   0  0.125p   2052      4 S   0.0  0.1   0:00.10 mem_reserve_tes
16998 retina    20   0  0.125p   2052      4 S   0.0  0.1   0:00.10 mem_reserve_tes
17006 retina    20   0  0.125p   2052      4 S   0.0  0.1   0:00.11 mem_reserve_tes
17014 retina    20   0  0.125p   2052      4 S   0.0  0.1   0:00.10 mem_reserve_tes
17017 retina    20   0  0.125p   2052      4 S   0.0  0.1   0:00.10 mem_reserve_tes
Tasks can allocate a maximum of 127TiB each.
Code:
~ for x in {1..16} ; do ./mem_reserve_test & sleep 1 ; done
[1] 16894
7ffec0000000
[2] 16906
7ffec0000000
[3] 16914
7ffec0000000
[4] 16922
7ffec0000000
[5] 16930
7ffec0000000
[6] 16938
7ffec0000000
[7] 16946
7ffec0000000
[8] 16954
7ffec0000000
[9] 16962
7ffec0000000
[10] 16970
7ffec0000000
[11] 16978
7ffec0000000
[12] 16990
7ffec0000000
[13] 16998
7ffec0000000
[14] 17006
7ffec0000000
[15] 17014
7ffec0000000
[16] 17017
7ffec0000000
So that is the full 47 bits of address space maxed out, 16 times over.

And the test code in case anyone cares to try it.
Code:
ALLOC_START		= 1 shl 32
ALLOC_STEPS		= 1 shl 30

format elf64 executable 0 at 1 shl 16
entry main

segment executable readable

HANDLE_STD_OUTPUT	= 1

; see /usr/include/asm/unistd_64.h
SYS64_WRITE		= 1
SYS64_MMAP		= 9
SYS64_NANOSLEEP		= 35
SYS64_EXIT		= 60

MMAP_PROT_READ		= 0x1
MMAP_PROT_WRITE		= 0x2
MMAP_MAP_PRIVATE	= 0x2
MMAP_MAP_FIXED		= 0x10
MMAP_MAP_ANONYMOUS	= 0x20
MMAP_MAP_FIXED_NOREPLACE= 0x100000

main:
	mov	r15,ALLOC_START
	mov	r14,ALLOC_STEPS
    .alloc_loop:
	xor	r9,r9			;offset
	or	r8,-1			;fd
	mov	r10,MMAP_MAP_PRIVATE or MMAP_MAP_ANONYMOUS or MMAP_MAP_FIXED or MMAP_MAP_FIXED_NOREPLACE
	mov	edx,MMAP_PROT_READ or MMAP_PROT_WRITE
	mov	rsi,r14			;size
	mov	rdi,r15			;address
	mov	eax,SYS64_MMAP
	syscall
	cmp	rax,r15
	jnz	.done
	add	r15,r14
	jmp	.alloc_loop
    .done:
	mov	rax,-ALLOC_START
	add	rax,r15
	call	print_hex
	mov	al,10
	call	print_char
	mov	eax,60
	call	sleep
	mov	rax,SYS64_EXIT
	xor	edi,edi
	syscall

print_hex:
	mov	rdi,rsp
	sub	rsp,32
	dec	rdi
	mov	byte[rdi],0
	mov	ecx,16
    .next_digit:
	xor	edx,edx
	div	rcx
	xchg	rdx,rax
	lea	ebx,[eax+'a'-10]
	add	al,'0'
	cmp	al,'9'
	cmova	eax,ebx
	dec	rdi
	mov	[rdi],al
	test	rdx,rdx
	mov	rax,rdx
	jnz	.next_digit
	mov	rax,rdi
	call	print_string
	add	rsp,32
	ret

print_string:
	mov	rdi,rax
    .next_char:
	mov	al,[rdi]
	test	al,al
	jz	.done
	push	rdi
	call	print_char
	pop	rdi
	inc	rdi
	jmp	.next_char
    .done:
	ret

print_char:
	push	rax
	mov	eax,SYS64_WRITE
	mov	edi,HANDLE_STD_OUTPUT
	mov	rsi,rsp
	mov	edx,1
	syscall
	pop	rax
	ret

sleep:
	push	0 rax
	mov	eax,SYS64_NANOSLEEP
	mov	rdi,rsp
	xor	esi,esi
	syscall
	pop	rax rax
	ret
Old school assembly with no macros or other modern rubbish like calling standards or anything. You'll need fasm to compile it. It has a whopping 350 byte executable size.
retina is online now   Reply With Quote