Ticket #4224: haiku_mlock_map_locked_and_private_area.diff

File haiku_mlock_map_locked_and_private_area.diff, 6.1 KB (added by Blub, 15 years ago)

implements: B_PRIVATE_AREA, mlock(), munlock(), MAP_LOCKED to mmap() and using B_PRIVATE_AREA when using MAP_PRIVATE in mmap()

  • headers/os/kernel/OS.h

     
    7979/* area protection */
    8080#define B_READ_AREA             1
    8181#define B_WRITE_AREA            2
     82#define B_PRIVATE_AREA          0x8000
    8283
    8384extern area_id      create_area(const char *name, void **startAddress,
    8485                        uint32 addressSpec, size_t size, uint32 lock,
  • headers/posix/sys/mman.h

     
    2222#define MAP_FIXED       0x04            /* require mapping to specified addr */
    2323#define MAP_ANONYMOUS   0x08            /* no underlying object */
    2424#define MAP_ANON        MAP_ANONYMOUS
     25#define MAP_LOCKED      0x2000          /* memory is locked into RAM */
    2526
    2627/* mmap() error return code */
    2728#define MAP_FAILED      ((void*)-1)
     
    5051
    5152int     posix_madvise(void* address, size_t length, int advice);
    5253
     54int     mlock(const void *address, size_t length);
     55int     munlock(const void *address, size_t length);
     56
    5357int     shm_open(const char* name, int openMode, mode_t permissions);
    5458int     shm_unlink(const char* name);
    5559
  • headers/private/kernel/vm.h

     
    131131            int protection);
    132132status_t _user_sync_memory(void *address, size_t size, int flags);
    133133status_t _user_memory_advice(void* address, size_t size, int advice);
     134status_t _user_lock_memory(void *address, size_t size, uint32 flags);
     135status_t _user_unlock_memory(void *address, size_t size, uint32 flags);
    134136
    135137area_id _user_area_for(void *address);
    136138area_id _user_find_area(const char *name);
  • headers/private/system/syscalls.h

     
    393393extern status_t     _kern_sync_memory(void *address, size_t size, int flags);
    394394extern status_t     _kern_memory_advice(void *address, size_t size,
    395395                        int advice);
     396extern status_t     _kern_lock_memory(void *address, size_t size, uint32 flags);
     397extern status_t     _kern_unlock_memory(void *address, size_t size, uint32 flags);
    396398
    397399/* kernel port functions */
    398400extern port_id      _kern_create_port(int32 queue_length, const char *name);
  • headers/private/system/vm_defs.h

     
    2727#define B_KERNEL_STACK_AREA     0x80
    2828
    2929#define B_USER_PROTECTION \
    30     (B_READ_AREA | B_WRITE_AREA | B_EXECUTE_AREA | B_STACK_AREA)
     30    (B_READ_AREA | B_WRITE_AREA | B_EXECUTE_AREA | B_STACK_AREA | B_PRIVATE_AREA)
    3131#define B_KERNEL_PROTECTION \
    3232    (B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA | B_KERNEL_EXECUTE_AREA \
    3333    | B_KERNEL_STACK_AREA)
  • src/system/kernel/vm/vm.cpp

     
    22642264    TRACE(("_vm_map_file(fd = %d, offset = %Ld, size = %lu, mapping %ld)\n",
    22652265        fd, offset, size, mapping));
    22662266
     2267    uint32 lock = (mapping & MAP_LOCKED) ? B_FULL_LOCK : B_NO_LOCK;
     2268    mapping &= ~MAP_LOCKED;
     2269
    22672270    offset = ROUNDDOWN(offset, B_PAGE_SIZE);
    22682271    size = PAGE_ALIGN(size);
    22692272
    22702273    if (mapping == REGION_NO_PRIVATE_MAP)
    22712274        protection |= B_SHARED_AREA;
     2275    else if (mapping == REGION_PRIVATE_MAP)
     2276        protection |= B_PRIVATE_AREA;
    22722277    if (addressSpec != B_EXACT_ADDRESS)
    22732278        unmapAddressRange = false;
    22742279
    22752280    if (fd < 0) {
    22762281        uint32 flags = unmapAddressRange ? CREATE_AREA_UNMAP_ADDRESS_RANGE : 0;
    22772282        return vm_create_anonymous_area(team, name, _address, addressSpec, size,
    2278             B_NO_LOCK, protection, 0, flags, kernel);
     2283            lock, protection, 0, flags, kernel);
    22792284    }
    22802285
    22812286    // get the open flags of the FD
     
    24392444        if (status != B_OK)
    24402445            return status;
    24412446
    2442         if (!kernel && (sourceArea->protection & B_KERNEL_AREA) != 0)
     2447        if (!kernel && (sourceArea->protection & (B_KERNEL_AREA | B_PRIVATE_AREA)) != 0)
    24432448            return B_NOT_ALLOWED;
    24442449
    24452450        sourceArea->protection |= B_SHARED_AREA;
     
    64436448    // TODO: Implement!
    64446449    return B_OK;
    64456450}
     6451
     6452status_t
     6453_user_lock_memory(void* _address, size_t size, uint32 flags)
     6454{
     6455    addr_t address = (addr_t)_address;
     6456
     6457    // check params
     6458    if (size == 0 || (addr_t)address + size < (addr_t)address || (address % B_PAGE_SIZE) != 0)
     6459        return B_BAD_VALUE;
     6460
     6461    if (!IS_USER_ADDRESS(address) || !IS_USER_ADDRESS((addr_t)address + size))
     6462        return B_BAD_ADDRESS;
     6463
     6464    // lock
     6465    return lock_memory(_address, size, flags);
     6466}
     6467
     6468status_t
     6469_user_unlock_memory(void* _address, size_t size, uint32 flags)
     6470{
     6471    addr_t address = (addr_t)_address;
     6472
     6473    // check params
     6474    if (size == 0 || (addr_t)address + size < (addr_t)address || (address % B_PAGE_SIZE) != 0)
     6475        return B_BAD_VALUE;
     6476
     6477    if (!IS_USER_ADDRESS(address) || !IS_USER_ADDRESS((addr_t)address + size))
     6478        return B_BAD_ADDRESS;
     6479
     6480    // lock
     6481    return unlock_memory(_address, size, flags);
     6482}
  • src/system/libroot/posix/sys/mman.cpp

     
    110110    // translate mapping, address specification, and protection
    111111    int mapping = (flags & MAP_SHARED) != 0
    112112        ? REGION_NO_PRIVATE_MAP : REGION_PRIVATE_MAP;
     113    mapping |= (flags & MAP_LOCKED);
    113114
    114115    uint32 addressSpec = B_ANY_ADDRESS;
    115116    if ((flags & MAP_FIXED) != 0)
     
    165166
    166167
    167168int
     169mlock(const void *address, size_t length)
     170{
     171    void *_address = const_cast<void*>(address);
     172    RETURN_AND_SET_ERRNO(_kern_lock_memory(_address, length, 0));
     173}
     174
     175int
     176munlock(const void *address, size_t length)
     177{
     178    void *_address = const_cast<void*>(address);
     179    RETURN_AND_SET_ERRNO(_kern_unlock_memory(_address, length, 0));
     180}
     181
     182
     183int
    168184shm_open(const char* name, int openMode, mode_t permissions)
    169185{
    170186    char path[PATH_MAX];