This commit is contained in:
Pavel 2023-03-26 19:35:55 +03:00
parent cb16305f5c
commit ee590973fc
5 changed files with 82 additions and 23 deletions

View File

@ -5,6 +5,9 @@ unit sys_mmap;
interface interface
uses
vm_mmap;
const const
PROT_NONE =$00; // no permissions PROT_NONE =$00; // no permissions
PROT_READ =$01; // pages can be read PROT_READ =$01; // pages can be read
@ -71,6 +74,10 @@ const
MADV_CORE = 9; // revert to including pages in a core file MADV_CORE = 9; // revert to including pages in a core file
MADV_PROTECT =10; // protect process from pageout kill MADV_PROTECT =10; // protect process from pageout kill
type
p_query_memory_prot=vm_mmap.p_query_memory_prot;
t_query_memory_prot=vm_mmap.t_query_memory_prot;
function mmap(_addr :Pointer; function mmap(_addr :Pointer;
_len :QWORD; _len :QWORD;
_prot :Integer; _prot :Integer;
@ -82,11 +89,11 @@ function munmap(addr:Pointer;len:QWORD):Integer;
function mprotect(addr:Pointer;len:QWORD;prot:Integer):Integer; function mprotect(addr:Pointer;len:QWORD;prot:Integer):Integer;
function madvise(addr:Pointer;len:QWORD;behav:Integer):Integer; function madvise(addr:Pointer;len:QWORD;behav:Integer):Integer;
function mname(addr:Pointer;len:QWORD;name:PChar):Integer; function mname(addr:Pointer;len:QWORD;name:PChar):Integer;
function query_memory_protection(addr:Pointer;len:QWORD;info:p_query_memory_prot):Integer;
implementation implementation
uses uses
vm_mmap,
trap, trap,
thr_error; thr_error;
@ -130,6 +137,13 @@ asm
call cerror call cerror
end; end;
function query_memory_protection(addr:Pointer;len:QWORD;info:p_query_memory_prot):Integer; assembler; nostackframe;
asm
movq sys_query_memory_protection,%rax
call fast_syscall
call cerror
end;
end. end.

View File

@ -61,6 +61,7 @@ function _thread(parameter:pointer):ptrint;
var var
td:p_kthread; td:p_kthread;
p:Pointer; p:Pointer;
qr:t_query_memory_prot;
begin begin
Result:=0; Result:=0;
NtWaitForSingleObject(event,false,nil); NtWaitForSingleObject(event,false,nil);
@ -83,6 +84,9 @@ begin
p:=mmap(Pointer($700000000),16*1024,PROT_CPU_ALL,MAP_ANON or MAP_FIXED,-1,0); p:=mmap(Pointer($700000000),16*1024,PROT_CPU_ALL,MAP_ANON or MAP_FIXED,-1,0);
Writeln(HexStr(p)); Writeln(HexStr(p));
Result:=query_memory_protection(Pointer($700000000),16*1024,@qr);
Writeln(Result);
p:=mmap(Pointer($700000000+16*1024),16*1024,PROT_CPU_ALL,MAP_ANON {or MAP_VOID} or MAP_FIXED,-1,0); p:=mmap(Pointer($700000000+16*1024),16*1024,PROT_CPU_ALL,MAP_ANON {or MAP_VOID} or MAP_FIXED,-1,0);
Writeln(HexStr(p)); Writeln(HexStr(p));

View File

@ -5,6 +5,15 @@ unit vm_mmap;
interface interface
type
p_query_memory_prot=^t_query_memory_prot;
t_query_memory_prot=packed record
start:Pointer;
__end:Pointer;
prot :Integer;
eflags:Integer;
end;
function sys_mmap(_addr :Pointer; function sys_mmap(_addr :Pointer;
_len :QWORD; _len :QWORD;
_prot :Integer; _prot :Integer;
@ -16,6 +25,7 @@ function sys_munmap(addr:Pointer;len:QWORD):Integer;
function sys_mprotect(addr:Pointer;len:QWORD;prot:Integer):Integer; function sys_mprotect(addr:Pointer;len:QWORD;prot:Integer):Integer;
function sys_madvise(addr:Pointer;len:QWORD;behav:Integer):Integer; function sys_madvise(addr:Pointer;len:QWORD;behav:Integer):Integer;
function sys_mname(addr:Pointer;len:QWORD;name:PChar):Integer; function sys_mname(addr:Pointer;len:QWORD;name:PChar):Integer;
function sys_query_memory_protection(addr:Pointer;len:QWORD;info:p_query_memory_prot):Integer;
implementation implementation
@ -616,7 +626,36 @@ begin
vm_map_set_name(@g_vmspace.vm_map,start,__end,@_name); vm_map_set_name(@g_vmspace.vm_map,start,__end,@_name);
end; end;
function sys_query_memory_protection(addr:Pointer;len:QWORD;info:p_query_memory_prot):Integer;
var
map:vm_map_t;
_addr:vm_offset_t;
__end:vm_offset_t;
entry:vm_map_entry_t;
data:t_query_memory_prot;
begin
Result:=EINVAL;
_addr:=trunc_page(vm_offset_t(addr));
map:=@g_vmspace.vm_map;
__end:=vm_map_max(map);
if (_addr<__end) or (_addr=__end) then
begin
vm_map_lock(map);
if not vm_map_lookup_entry(map,_addr,@entry) then
begin
vm_map_unlock(map);
Result:=EACCES;
end else
begin
data.start:=Pointer(entry^.start);
data.__end:=Pointer(entry^.__end);
data.prot:=(entry^.max_protection and entry^.protection);
data.eflags:=entry^.eflags;
vm_map_unlock(map);
Result:=copyout(@data,info,SizeOf(t_query_memory_prot));
end;
end;
end;
end. end.

View File

@ -60,8 +60,6 @@ const
OBJPR_NOTMAPPED=$2; // Don't unmap pages. OBJPR_NOTMAPPED=$2; // Don't unmap pages.
OBJPR_NOTWIRED =$4; // Don't remove wired pages. OBJPR_NOTWIRED =$4; // Don't remove wired pages.
//function vm_object_is_merges (_object:vm_object_t):Boolean;
procedure vm_object_reference (_object:vm_object_t); procedure vm_object_reference (_object:vm_object_t);
function vm_object_allocate (t:objtype_t;size:vm_pindex_t):vm_object_t; function vm_object_allocate (t:objtype_t;size:vm_pindex_t):vm_object_t;
procedure vm_object_deallocate(_object:vm_object_t); procedure vm_object_deallocate(_object:vm_object_t);
@ -158,19 +156,6 @@ begin
FreeMem(_object); FreeMem(_object);
end; end;
{
function vm_object_is_merges(_object:vm_object_t):Boolean;
begin
if (_object=nil) then
begin
Result:=True;
end else
begin
Result:=(obj_type(_object^.otype)=OBJT_DMEM);
end;
end;
}
procedure vm_object_reference(_object:vm_object_t); procedure vm_object_reference(_object:vm_object_t);
begin begin
if (_object=nil) then Exit; if (_object=nil) then Exit;

View File

@ -222,6 +222,22 @@ begin
base_old:=base_old+VM_MIN_GPU_ADDRESS; base_old:=base_old+VM_MIN_GPU_ADDRESS;
end; end;
//set old to readonly
r:=NtProtectVirtualMemory(
NtCurrentProcess,
@base_old,
@size,
PAGE_READONLY,
nil
);
if (r<>0) then
begin
Writeln('failed NtProtectVirtualMemory:',r);
Assert(false,'pmap_protect');
end;
//alloc new
r:=NtAllocateVirtualMemory( r:=NtAllocateVirtualMemory(
NtCurrentProcess, NtCurrentProcess,
@base_new, @base_new,
@ -237,6 +253,10 @@ begin
Assert(false,'pmap_protect'); Assert(false,'pmap_protect');
end; end;
//move data
Move(base_old^,base_new^,size);
//free old
r:=NtFreeVirtualMemory( r:=NtFreeVirtualMemory(
NtCurrentProcess, NtCurrentProcess,
@base_old, @base_old,
@ -279,15 +299,12 @@ var
begin begin
Writeln('pmap_madv_free:',HexStr(start,11),':',HexStr(__end,11),':',HexStr(prot,2)); Writeln('pmap_madv_free:',HexStr(start,11),':',HexStr(__end,11),':',HexStr(prot,2));
//dont reset gpu mem
if is_gpu(prot) then Exit;
base:=Pointer(trunc_page(start)); base:=Pointer(trunc_page(start));
size:=trunc_page(__end-start); size:=trunc_page(__end-start);
if is_gpu(prot) then
begin
//shift
base:=base+VM_MIN_GPU_ADDRESS;
end;
r:=NtAllocateVirtualMemory( r:=NtAllocateVirtualMemory(
NtCurrentProcess, NtCurrentProcess,
@base, @base,