VirtualBox

Changeset 20525 in vbox for trunk


Ignore:
Timestamp:
Jun 13, 2009 8:13:33 PM (15 years ago)
Author:
vboxsync
Message:

iprt/memobj.h: Added RTR0MemObjProtect, only implemented for darwin.

Location:
trunk
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/memobj.h

    r20374 r20525  
    300300RTR0DECL(int) RTR0MemObjMapUser(PRTR0MEMOBJ pMemObj, RTR0MEMOBJ MemObjToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process);
    301301
     302/**
     303 * Change the page level protection of one or more pages in a memory object.
     304 *
     305 * @returns IPRT status code.
     306 * @retval  VERR_NOT_SUPPORTED if the OS doesn't provide any way to manipulate
     307 *          page level protection. The caller must handle this status code
     308 *          gracefully. (Note that it may also occur if the implementation is
     309 *          missing, in which case just go ahead and implement it.)
     310 *
     311 * @param   hMemObj         Memory object handle.
     312 * @param   offSub          Offset into the memory object. Must be page aligned.
     313 * @param   cbSub           Number of bytes to change the protection of. Must be
     314 *                          page aligned.
     315 * @param   fProt           Combination of RTMEM_PROT_* flags.
     316 */
     317RTR0DECL(int) RTR0MemObjProtect(RTR0MEMOBJ hMemObj, size_t offSub, size_t cbSub, uint32_t fProt);
     318
    302319#endif /* IN_RING0 */
    303320
  • trunk/src/VBox/Runtime/include/internal/memobj.h

    r20374 r20525  
    9090
    9191
     92/** @name RTR0MEMOBJINTERNAL::fFlags
     93 * @{ */
     94/** Page level protection was changed. */
     95#define RTR0MEMOBJ_FLAGS_PROT_CHANGED       RT_BIT_32(0)
     96/** @} */
     97
     98
    9299typedef struct RTR0MEMOBJINTERNAL *PRTR0MEMOBJINTERNAL;
    93100typedef struct RTR0MEMOBJINTERNAL **PPRTR0MEMOBJINTERNAL;
     
    111118    /** The type of allocation. */
    112119    RTR0MEMOBJTYPE  enmType;
     120    /** Flags, RTR0MEMOBJ_FLAGS_*. */
     121    uint32_t        fFlags;
    113122    /** The size of the memory allocated, pinned down, or mapped. */
    114123    size_t          cb;
     
    222231    {
    223232        case RTR0MEMOBJTYPE_MAPPING:
     233            return true;
     234
     235        default:
     236            return false;
     237    }
     238}
     239
     240
     241/**
     242 * Checks page level protection can be changed on this object.
     243 *
     244 * @returns true / false.
     245 * @param   pMem        The ring-0 memory object handle.
     246 */
     247DECLINLINE(bool) rtR0MemObjIsProtectable(PRTR0MEMOBJINTERNAL pMem)
     248{
     249    switch (pMem->enmType)
     250    {
     251        case RTR0MEMOBJTYPE_MAPPING:
     252        case RTR0MEMOBJTYPE_PAGE:
     253        case RTR0MEMOBJTYPE_LOW:
     254        case RTR0MEMOBJTYPE_CONT:
    224255            return true;
    225256
     
    411442
    412443/**
     444 * Change the page level protection of one or more pages in a memory object.
     445 *
     446 * @returns IPRT status code.
     447 * @retval  VERR_NOT_SUPPORTED see RTR0MemObjProtect.
     448 *
     449 * @param   pMem            The memory object.
     450 * @param   offSub          Offset into the memory object. Page aligned.
     451 * @param   cbSub           Number of bytes to change the protection of. Page
     452 *                          aligned.
     453 * @param   fProt           Combination of RTMEM_PROT_* flags.
     454 */
     455int rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt);
     456
     457/**
    413458 * Get the physical address of an page in the memory object.
    414459 *
  • trunk/src/VBox/Runtime/r0drv/darwin/memobj-r0drv-darwin.cpp

    r20507 r20525  
    9292    }
    9393}
     94
     95
     96/**
     97 * Gets the virtual memory map the specified object is mapped into.
     98 *
     99 * @returns VM map handle on success, NULL if no map.
     100 * @param   pMem                The memory object.
     101 */
     102DECLINLINE(vm_map_t) rtR0MemObjDarwinGetMap(PRTR0MEMOBJINTERNAL pMem)
     103{
     104    switch (pMem->enmType)
     105    {
     106        case RTR0MEMOBJTYPE_PAGE:
     107        case RTR0MEMOBJTYPE_LOW:
     108        case RTR0MEMOBJTYPE_CONT:
     109            return kernel_map;
     110
     111        case RTR0MEMOBJTYPE_PHYS:
     112        case RTR0MEMOBJTYPE_PHYS_NC:
     113            return NULL; /* pretend these have no mapping atm. */
     114
     115        case RTR0MEMOBJTYPE_LOCK:
     116            return pMem->u.Lock.R0Process == NIL_RTR0PROCESS
     117                ? kernel_map
     118                : get_task_map((task_t)pMem->u.Lock.R0Process);
     119
     120        case RTR0MEMOBJTYPE_RES_VIRT:
     121            return pMem->u.ResVirt.R0Process == NIL_RTR0PROCESS
     122                ? kernel_map
     123                : get_task_map((task_t)pMem->u.ResVirt.R0Process);
     124
     125        case RTR0MEMOBJTYPE_MAPPING:
     126            return pMem->u.Mapping.R0Process == NIL_RTR0PROCESS
     127                ? kernel_map
     128                : get_task_map((task_t)pMem->u.Mapping.R0Process);
     129
     130        default:
     131            return NULL;
     132    }
     133}
     134
     135#if 0 /* not necessary after all*/
     136/* My vm_map mockup. */
     137struct my_vm_map
     138{
     139    struct { char pad[8]; } lock;
     140    struct my_vm_map_header
     141    {
     142        struct vm_map_links
     143        {
     144            void            *prev;
     145            void            *next;
     146            vm_map_offset_t start;
     147            vm_map_offset_t end;
     148        }                   links;
     149        int                 nentries;
     150        boolean_t           entries_pageable;
     151    }                       hdr;
     152    pmap_t                  pmap;
     153    vm_map_size_t           size;
     154};
     155
     156
     157/**
     158 * Gets the minimum map address, this is similar to get_map_min.
     159 *
     160 * @returns The start address of the map.
     161 * @param   pMap                The map.
     162 */
     163static vm_map_offset_t rtR0MemObjDarwinGetMapMin(vm_map_t pMap)
     164{
     165    /* lazy discovery of the correct offset. The apple guys is a wonderfully secretive bunch. */
     166    static int32_t volatile s_offAdjust = INT32_MAX;
     167    int32_t                 off         = s_offAdjust;
     168    if (off == INT32_MAX)
     169    {
     170        for (off = 0; ; off += sizeof(pmap_t))
     171        {
     172            if (*(pmap_t *)((uint8_t *)kernel_map + off) == kernel_pmap)
     173                break;
     174            AssertReturn(off <= RT_MAX(RT_OFFSETOF(struct my_vm_map, pmap) * 4, 1024), 0x1000);
     175        }
     176        ASMAtomicWriteS32(&s_offAdjust, off - RT_OFFSETOF(struct my_vm_map, pmap));
     177    }
     178
     179    /* calculate it. */
     180    struct my_vm_map *pMyMap = (struct my_vm_map *)((uint8_t *)pMap + off);
     181    return pMyMap->hdr.links.start;
     182}
     183#endif /* unused */
    94184
    95185#ifdef RT_STRICT
     
    813903
    814904
     905int rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
     906{
     907    /* Get the map for the object. */
     908    vm_map_t pVmMap = rtR0MemObjDarwinGetMap(pMem);
     909    if (!pVmMap)
     910        return VERR_NOT_SUPPORTED;
     911
     912    /* Convert the protection. */
     913    vm_prot_t fMachProt;
     914    switch (fProt)
     915    {
     916        case RTMEM_PROT_NONE:
     917            fMachProt = VM_PROT_NONE;
     918            break;
     919        case RTMEM_PROT_READ:
     920            fMachProt = VM_PROT_READ;
     921            break;
     922        case RTMEM_PROT_READ | RTMEM_PROT_WRITE:
     923            fMachProt = VM_PROT_READ | VM_PROT_WRITE;
     924            break;
     925        case RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
     926            fMachProt = VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
     927            break;
     928        case RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
     929            fMachProt = VM_PROT_WRITE | VM_PROT_EXECUTE;
     930            break;
     931        case RTMEM_PROT_EXEC:
     932            fMachProt = VM_PROT_EXECUTE;
     933            break;
     934        default:
     935            AssertFailedReturn(VERR_INVALID_PARAMETER);
     936    }
     937
     938    /* do the job. */
     939    vm_offset_t Start = (uintptr_t)pMem->pv + offSub;
     940    kern_return_t krc = vm_protect(pVmMap,
     941                                   Start,
     942                                   cbSub,
     943                                   false,
     944                                   fMachProt);
     945    if (krc != KERN_SUCCESS)
     946        return RTErrConvertFromDarwinKern(krc);
     947    return VINF_SUCCESS;
     948}
     949
     950
    815951RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
    816952{
  • trunk/src/VBox/Runtime/r0drv/darwin/the-darwin-kernel.h

    r20374 r20525  
    8888
    8989RT_C_DECLS_BEGIN
     90
    9091/* mach/vm_types.h */
    9192typedef struct pmap *pmap_t;
     
    116117/* osfmk/i386/cpu_number.h */
    117118extern int cpu_number(void);
     119
     120/* osfmk/vm/vm_user.c */
     121extern kern_return_t vm_protect(vm_map_t, vm_offset_t, vm_size_t, boolean_t, vm_prot_t);
    118122
    119123/* i386/machine_routines.h */
  • trunk/src/VBox/Runtime/r0drv/freebsd/memobj-r0drv-freebsd.c

    r19673 r20525  
    682682
    683683
     684int rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
     685{
     686    NOREF(pMem);
     687    NOREF(offSub);
     688    NOREF(cbSub);
     689    NOREF(fProt);
     690    return VERR_NOT_SUPPORTED;
     691}
     692
     693
    684694RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
    685695{
  • trunk/src/VBox/Runtime/r0drv/linux/memobj-r0drv-linux.c

    r18522 r20525  
    3939#include <iprt/assert.h>
    4040#include <iprt/log.h>
     41#include <iprt/process.h>
    4142#include <iprt/string.h>
    42 #include <iprt/process.h>
    4343#include "internal/memobj.h"
    4444
     
    11661166
    11671167
     1168int rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
     1169{
     1170    NOREF(pMem);
     1171    NOREF(offSub);
     1172    NOREF(cbSub);
     1173    NOREF(fProt);
     1174    return VERR_NOT_SUPPORTED;
     1175}
     1176
     1177
    11681178RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
    11691179{
  • trunk/src/VBox/Runtime/r0drv/memobj-r0drv.cpp

    r15718 r20525  
    3535#define LOG_GROUP RTLOGGROUP_DEFAULT ///@todo RTLOGGROUP_MEM
    3636#include <iprt/memobj.h>
     37
    3738#include <iprt/alloc.h>
    38 #include <iprt/process.h>
     39#include <iprt/asm.h>
    3940#include <iprt/assert.h>
    4041#include <iprt/err.h>
    4142#include <iprt/log.h>
    4243#include <iprt/param.h>
     44#include <iprt/process.h>
     45
    4346#include "internal/memobj.h"
    4447
     
    7275        pNew->cbSelf    = (uint32_t)cbSelf;
    7376        pNew->enmType   = enmType;
     77        pNew->fFlags    = 0;
    7478        pNew->cb        = cb;
    7579        pNew->pv        = pv;
     
    9094    if (pMem)
    9195    {
    92         pMem->u32Magic++;
     96        ASMAtomicUoWriteU32(&pMem->u32Magic, ~RTR0MEMOBJ_MAGIC);
    9397        pMem->enmType = RTR0MEMOBJTYPE_END;
    9498        RTMemFree(pMem);
     
    818822}
    819823
     824
     825RTR0DECL(int) RTR0MemObjProtect(RTR0MEMOBJ hMemObj, size_t offSub, size_t cbSub, uint32_t fProt)
     826{
     827    PRTR0MEMOBJINTERNAL pMemObj;
     828    int                 rc;
     829
     830    /* sanity checks. */
     831    pMemObj = (PRTR0MEMOBJINTERNAL)hMemObj;
     832    AssertPtrReturn(pMemObj, VERR_INVALID_HANDLE);
     833    AssertReturn(pMemObj->u32Magic == RTR0MEMOBJ_MAGIC, VERR_INVALID_HANDLE);
     834    AssertReturn(pMemObj->enmType > RTR0MEMOBJTYPE_INVALID && pMemObj->enmType < RTR0MEMOBJTYPE_END, VERR_INVALID_HANDLE);
     835    AssertReturn(rtR0MemObjIsProtectable(pMemObj), VERR_INVALID_PARAMETER);
     836    AssertReturn(!(offSub & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
     837    AssertReturn(offSub < pMemObj->cb, VERR_INVALID_PARAMETER);
     838    AssertReturn(!(cbSub  & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
     839    AssertReturn(cbSub <= pMemObj->cb, VERR_INVALID_PARAMETER);
     840    AssertReturn(offSub + cbSub <= pMemObj->cb, VERR_INVALID_PARAMETER);
     841    AssertReturn(!(fProt & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC)), VERR_INVALID_PARAMETER);
     842
     843    /* do the job */
     844    rc = rtR0MemObjNativeProtect(pMemObj, offSub, cbSub, fProt);
     845    if (RT_SUCCESS(rc))
     846        pMemObj->fFlags |= RTR0MEMOBJ_FLAGS_PROT_CHANGED; /* record it */
     847
     848    return rc;
     849}
  • trunk/src/VBox/Runtime/r0drv/nt/memobj-r0drv-nt.cpp

    r14824 r20525  
    761761
    762762
     763int rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
     764{
     765    NOREF(pMem);
     766    NOREF(offSub);
     767    NOREF(cbSub);
     768    NOREF(fProt);
     769    return VERR_NOT_SUPPORTED;
     770}
     771
     772
    763773RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
    764774{
  • trunk/src/VBox/Runtime/r0drv/os2/memobj-r0drv-os2.cpp

    r14824 r20525  
    468468
    469469
     470int rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
     471{
     472    NOREF(pMem);
     473    NOREF(offSub);
     474    NOREF(cbSub);
     475    NOREF(fProt);
     476    return VERR_NOT_SUPPORTED;
     477}
     478
     479
    470480RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
    471481{
  • trunk/src/VBox/Runtime/r0drv/solaris/memobj-r0drv-solaris.c

    r16722 r20525  
    401401
    402402
     403int rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
     404{
     405    NOREF(pMem);
     406    NOREF(offSub);
     407    NOREF(cbSub);
     408    NOREF(fProt);
     409    return VERR_NOT_SUPPORTED;
     410}
     411
     412
    403413RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
    404414{
  • trunk/src/VBox/Runtime/r0drv/solaris/vbi/memobj-r0drv-solaris.c

    r20477 r20525  
    323323
    324324
     325int rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
     326{
     327    NOREF(pMem);
     328    NOREF(offSub);
     329    NOREF(cbSub);
     330    NOREF(fProt);
     331    return VERR_NOT_SUPPORTED;
     332}
     333
     334
    325335RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
    326336{
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette