Changeset 739

Show
Ignore:
Timestamp:
07/07/08 15:23:30 (2 months ago)
Author:
mwhitworth
Message:

Add start of SysMemoryProtect system call.

Location:
Whitix/trunk/memory
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • Whitix/trunk/memory/mmap.c

    r694 r739  
    2828#include <sys.h> 
    2929#include <print.h> 
     30#include <preempt.h> 
    3031 
    3132extern struct Cache* areaCache,*mapCache; 
     
    5354        struct PhysPage* newPage; 
    5455        int ret; 
    55  
    56 //      KePrint("VmHandleNoPage(%#X)\n", address); 
    5756 
    5857        if (area->areaOps && area->areaOps->handleNoPage) 
     
    6968                        MachineHalt(); 
    7069                }else{ 
     70                        PreemptDisable(); /* Needed? */ 
    7171                        newPage=PageAlloc(); 
    7272 
    7373                        if (!newPage) 
     74                        { 
     75                                PreemptEnable(); 
    7476                                return -ENOMEM; 
     77                        } 
    7578 
    7679                        VirtMemMapPage(address,newPage->physAddr,area->protection); 
     80 
     81                        PreemptEnable(); 
    7782 
    7883                        if (area->vNode->fileOps->mMap) 
     
    154159} 
    155160 
    156 /*********************************************************************** 
    157  * 
    158  * FUNCTION:    MMapAddArea 
    159  * 
    160  * DESCRIPTION: Add an area to the process's area list. The list is 
    161  *                              sorted by start address, as this makes it much easier 
    162  *                              for MMapFindAddress. 
    163  * 
    164  * PARAMETERS:  process - process in question. 
    165  *                              area - area to be added. 
    166  * 
    167  * RETURNS:             Usual error codes. 
    168  * 
    169  ***********************************************************************/ 
    170  
    171161int MmapHandleFault(struct Process* process,DWORD address,int error) 
    172162{ 
     
    218208 ***********************************************************************/ 
    219209 
    220 static void MMapAddArea(struct Process* process,struct VMArea* area) 
    221 { 
    222         struct VMArea* curr=NULL; 
    223  
     210static void MMapAddArea(struct Process* process, struct VMArea* area) 
     211{ 
    224212        if (ListEmpty(&process->areaList)) 
    225213                ListAdd(&area->list,&process->areaList); 
    226214        else 
    227215        { 
     216                struct VMArea* curr=NULL; 
     217 
    228218                ListForEachEntry(curr,&process->areaList,list) 
    229219                        if (curr->start > area->start) 
     
    299289void MmapProcessRemove(struct Process* process) 
    300290{ 
    301         struct VMArea* curr,*curr2; 
     291        struct VMArea* curr, *curr2; 
    302292        DWORD i; 
     293        DWORD flags; 
     294 
     295        IrqSaveFlags(flags); 
    303296 
    304297        ListForEachEntrySafe(curr, curr2, &process->areaList, list) 
     
    309302                MMapRemoveArea(curr); 
    310303        } 
     304 
     305        IrqRestoreFlags(flags); 
    311306} 
    312307 
     
    365360 ***********************************************************************/ 
    366361 
    367 void MMapMerge(struct VMArea* first,struct VMArea* second,int isAnon) 
     362void MMapMerge(struct VMArea* first, struct VMArea* second) 
    368363{ 
    369364        if (first->start+first->length != second->start) 
     
    376371                return; 
    377372 
    378         if (!(isAnon)) 
    379                 /* Check offsets */ 
    380                 if (second->offset != first->offset+first->length) 
    381                         return; 
    382  
    383373        if (first->flags != second->flags) 
    384374                return; 
    385375 
     376        /* Check offsets */ 
     377        if (second->offset != first->offset+first->length) 
     378                return; 
     379 
    386380        /* Ok to merge now */ 
    387  
    388381        first->length+=second->length; 
    389  
    390382        MMapRemoveArea(second); 
    391383} 
    392384 
    393 void MMapMergeMappings(struct Process* process,struct VMArea* area,int isAnon) 
     385void MMapMergeMappings(struct Process* process, struct VMArea* area) 
    394386{ 
    395387        /* Get neighbours of this area (in terms of memory), and see whether they are  
     
    401393 
    402394        if (area->list.next != &process->areaList) 
    403                 MMapMerge(area,next,isAnon); 
     395                MMapMerge(area, next); 
    404396 
    405397        if (area->list.prev != &process->areaList) 
    406                 MMapMerge(prev,area,isAnon); 
     398                MMapMerge(prev, area); 
    407399} 
    408400 
     
    430422{ 
    431423        struct VMArea* area=(struct VMArea*)MemCacheAlloc(areaCache); 
    432         int isAnon=!vNode; 
    433424 
    434425        if (!process || !length || !area) 
     
    455446                if (NameToVNode(&vNode,DEVICES_PATH "Special/Zero",0)) 
    456447                        return 0; 
     448 
     449                flags |= MMAP_ANON; 
    457450        } 
    458451 
     
    469462 
    470463        MMapAddArea(process,area); 
    471         MMapMergeMappings(process,area,isAnon); 
     464        MMapMergeMappings(process, area); 
    472465 
    473466        return address; 
     
    603596} 
    604597 
    605 int SysMemoryProtect(DWORD address, size_t length, int protection) 
    606 { 
    607         KePrint("SysMemoryProtect(%#X, %d)\n", address, length); 
    608         return -ENOTIMPL; 
     598int MMapProtectionFixAll(struct VMArea* area, int protection) 
     599{ 
     600        area->protection=protection; 
     601        return 0; 
     602} 
     603 
     604int MMapProtectionChange(struct VMArea* area, DWORD start, DWORD end, int protection) 
     605{ 
     606        int error=-ENOTIMPL; 
     607 
     608        if (protection ==  area->protection) 
     609                return 0; 
     610 
     611        if (start == area->start) 
     612        { 
     613                if (end == area->start+area->length) 
     614                        error=MMapProtectionFixAll(area, protection); 
     615                else 
     616                        KePrint("MMapProtectionChange: TODO\n"); 
     617        }else{ 
     618                KePrint("MMapProtectionChange: TODO\n"); 
     619        } 
     620 
     621        if (error) 
     622                return error; 
     623 
     624        VirtChangeProtection(start, end, protection); 
     625 
     626        return 0; 
     627} 
     628 
     629int SysMemoryProtect(DWORD start, size_t length, int protection) 
     630{ 
     631        int error=-EINVAL; 
     632        size_t end; 
     633        size_t address; 
     634        struct VMArea* area, *next; 
     635 
     636        PreemptDisable(); 
     637 
     638        /* Can only deal with changing the protection of whole pages. */ 
     639        if (PAGE_OFFSET(start)) 
     640                goto out; 
     641 
     642        length=PAGE_ALIGN_UP(length); 
     643        end=start+length; 
     644 
     645        if (end < start) 
     646                goto out; 
     647 
     648        /* Check protection bits. */ 
     649 
     650        error=0; 
     651 
     652        /* A zero length is valid, but useless. */ 
     653        if (start == end) 
     654                goto out; 
     655 
     656        area=VmLookupAddress(current, start); 
     657 
     658        error=-EFAULT; 
     659 
     660        if (!area) 
     661                goto out; 
     662 
     663        /* The change of protection may extend over one or more mappings. */ 
     664        address=start; 
     665 
     666        while (1) 
     667        { 
     668                if ((area->start + area->length) >= end) 
     669                { 
     670                        error=MMapProtectionChange(area, address, end, protection); 
     671                        break; 
     672                } 
     673 
     674                KePrint("SysMemoryProtect: todo\n"); 
     675        } 
     676 
     677//      MMapMergeMappings(current, start, end); 
     678 
     679out: 
     680        PreemptEnable(); 
     681        return error; 
    609682} 
    610683 
  • Whitix/trunk/memory/shmem.c

    r694 r739  
    2727#include <sys.h> 
    2828#include <module.h> 
     29#include <print.h> 
    2930 
    3031#define SHMEM_MAX_DESCS         10