1 /* $Id: section.c,v 1.14 1999/08/29 06:59:10 ea Exp $
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/mm/section.c
6 * PURPOSE: Implements section objects
7 * PROGRAMMER: David Welch (welch@mcmail.com)
12 /* INCLUDES *****************************************************************/
14 #include <ddk/ntddk.h>
15 #include <internal/mm.h>
16 #include <internal/ob.h>
17 #include <internal/io.h>
18 #include <internal/ps.h>
22 #include <internal/debug.h>
24 /* GLOBALS *******************************************************************/
26 POBJECT_TYPE MmSectionType
= NULL
;
28 /* FUNCTIONS *****************************************************************/
30 VOID
MmpDeleteSection(PVOID ObjectBody
)
34 NTSTATUS
MmpCreateSection(PVOID ObjectBody
,
37 POBJECT_ATTRIBUTES ObjectAttributes
)
41 DPRINT("MmpCreateDevice(ObjectBody %x, Parent %x, RemainingPath %w)\n",
42 ObjectBody
, Parent
, RemainingPath
);
44 if (RemainingPath
== NULL
)
46 return(STATUS_SUCCESS
);
49 if (wcschr(RemainingPath
+1, '\\') != NULL
)
51 return(STATUS_UNSUCCESSFUL
);
54 Status
= ObReferenceObjectByPointer(Parent
,
55 STANDARD_RIGHTS_REQUIRED
,
58 if (!NT_SUCCESS(Status
))
63 ObAddEntryDirectory(Parent
, ObjectBody
, RemainingPath
+1);
64 ObDereferenceObject(Parent
);
66 return(STATUS_SUCCESS
);
69 NTSTATUS
MmInitSectionImplementation(VOID
)
71 ANSI_STRING AnsiString
;
73 MmSectionType
= ExAllocatePool(NonPagedPool
,sizeof(OBJECT_TYPE
));
75 MmSectionType
->TotalObjects
= 0;
76 MmSectionType
->TotalHandles
= 0;
77 MmSectionType
->MaxObjects
= ULONG_MAX
;
78 MmSectionType
->MaxHandles
= ULONG_MAX
;
79 MmSectionType
->PagedPoolCharge
= 0;
80 MmSectionType
->NonpagedPoolCharge
= sizeof(SECTION_OBJECT
);
81 MmSectionType
->Dump
= NULL
;
82 MmSectionType
->Open
= NULL
;
83 MmSectionType
->Close
= NULL
;
84 MmSectionType
->Delete
= MmpDeleteSection
;
85 MmSectionType
->Parse
= NULL
;
86 MmSectionType
->Security
= NULL
;
87 MmSectionType
->QueryName
= NULL
;
88 MmSectionType
->OkayToClose
= NULL
;
89 MmSectionType
->Create
= MmpCreateSection
;
91 RtlInitAnsiString(&AnsiString
,"Section");
92 RtlAnsiStringToUnicodeString(&MmSectionType
->TypeName
,
94 return(STATUS_SUCCESS
);
101 OUT PHANDLE SectionHandle
,
102 IN ACCESS_MASK DesiredAccess
,
103 IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL
,
104 IN PLARGE_INTEGER MaximumSize OPTIONAL
,
105 IN ULONG SectionPageProtection OPTIONAL
,
106 IN ULONG AllocationAttributes
,
107 IN HANDLE FileHandle OPTIONAL
110 * FUNCTION: Creates a section object.
112 * SectionHandle (OUT) = Caller supplied storage for the resulting
114 * DesiredAccess = Specifies the desired access to the section can be a
115 * combination of STANDARD_RIGHTS_REQUIRED |
116 * SECTION_QUERY | SECTION_MAP_WRITE |
117 * SECTION_MAP_READ | SECTION_MAP_EXECUTE.
118 * ObjectAttribute = Initialized attributes for the object can be used
119 * to create a named section
120 * MaxiumSize = Maximizes the size of the memory section. Must be
121 * non-NULL for a page-file backed section.
122 * If value specified for a mapped file and the file is
123 * not large enough, file will be extended.
124 * SectionPageProtection = Can be a combination of PAGE_READONLY |
125 * PAGE_READWRITE | PAGE_WRITEONLY |
127 * AllocationAttributes = can be a combination of SEC_IMAGE |
129 * FileHandle = Handle to a file to create a section mapped to a file
130 * instead of a memory backed section.
134 PSECTION_OBJECT Section
;
137 DPRINT("NtCreateSection()\n");
139 Section
= ObCreateObject(SectionHandle
,
145 return(STATUS_UNSUCCESSFUL
);
148 if (MaximumSize
!= NULL
)
150 Section
->MaximumSize
= *MaximumSize
;
154 Section
->MaximumSize
.QuadPart
= 0xffffffff;
156 Section
->SectionPageProtection
= SectionPageProtection
;
157 Section
->AllocateAttributes
= AllocationAttributes
;
159 if (FileHandle
!= NULL
)
161 Status
= ObReferenceObjectByHandle(FileHandle
,
165 (PVOID
*)&Section
->FileObject
,
167 if (Status
!= STATUS_SUCCESS
)
169 DPRINT("NtCreateSection() = %x\n",Status
);
175 Section
->FileObject
= NULL
;
178 DPRINT("NtCreateSection() = STATUS_SUCCESS\n");
179 return(STATUS_SUCCESS
);
183 /**********************************************************************
204 PHANDLE SectionHandle
,
205 ACCESS_MASK DesiredAccess
,
206 POBJECT_ATTRIBUTES ObjectAttributes
214 Status
= ObReferenceObjectByName(
215 ObjectAttributes
->ObjectName
,
216 ObjectAttributes
->Attributes
,
224 if (!NT_SUCCESS(Status
))
229 Status
= ObCreateHandle(
230 PsGetCurrentProcess(),
240 /**********************************************************************
245 * Maps a view of a section into the virtual address space of a
250 * Handle of the section.
253 * Handle of the process.
256 * Desired base address (or NULL) on entry;
257 * Actual base address of the view on exit.
260 * Number of high order address bits that must be zero.
263 * Size in bytes of the initially committed section of
267 * Offset in bytes from the beginning of the section
268 * to the beginning of the view.
271 * Desired length of map (or zero to map all) on entry
272 * Actual length mapped on exit.
275 * Specified how the view is to be shared with
279 * Type of allocation for the pages.
282 * Protection for the committed region of the view.
290 HANDLE SectionHandle
,
291 HANDLE ProcessHandle
,
295 PLARGE_INTEGER SectionOffset
,
297 SECTION_INHERIT InheritDisposition
,
298 ULONG AllocationType
,
302 PSECTION_OBJECT Section
;
304 MEMORY_AREA
* Result
;
308 "NtMapViewOfSection(Section:%08lx, Process:%08lx,\n"
309 " Base:%08lx, ZeroBits:%08lx, CommitSize:%08lx,\n"
310 " SectionOffs:%08lx, *ViewSize:%08lx, InheritDisp:%08lx,\n"
311 " AllocType:%08lx, Protect:%08lx)\n",
324 DPRINT(" *Base:%08lx\n", *BaseAddress
);
326 Status
= ObReferenceObjectByHandle(
334 if (Status
!= STATUS_SUCCESS
)
336 DPRINT("ObReference failed rc=%x\n",Status
);
341 DPRINT("Section %x\n",Section
);
343 Status
= ObReferenceObjectByHandle(
345 PROCESS_VM_OPERATION
,
351 if (Status
!= STATUS_SUCCESS
)
353 ObDereferenceObject(Section
);
358 DPRINT("ViewSize %x\n",ViewSize
);
360 if ((*ViewSize
) > Section
->MaximumSize
.u
.LowPart
)
362 (*ViewSize
) = Section
->MaximumSize
.u
.LowPart
;
365 Status
= MmCreateMemoryArea(
368 MEMORY_AREA_SECTION_VIEW_COMMIT
,
374 if (!NT_SUCCESS(Status
))
376 DPRINT("NtMapViewOfSection() = %x\n",Status
);
378 ObDereferenceObject(Process
);
379 ObDereferenceObject(Section
);
383 Result
->Data
.SectionData
.Section
= Section
;
385 DPRINT("SectionOffset %x\n",SectionOffset
);
387 if (SectionOffset
== NULL
)
389 Result
->Data
.SectionData
.ViewOffset
= 0;
393 Result
->Data
.SectionData
.ViewOffset
=
394 SectionOffset
->u
.LowPart
;
397 DPRINT("*BaseAddress %x\n",*BaseAddress
);
398 ObDereferenceObject(Process
);
399 ObDereferenceObject(Section
);
401 return STATUS_SUCCESS
;
405 /**********************************************************************
407 * NtUnmapViewOfSection
424 NtUnmapViewOfSection (
425 HANDLE ProcessHandle
,
432 Status
= ObReferenceObjectByHandle(
434 PROCESS_VM_OPERATION
,
440 if (Status
!= STATUS_SUCCESS
)
444 Status
= MmFreeMemoryArea(
450 ObDereferenceObject(Process
);
459 IN HANDLE SectionHandle
,
460 IN CINT SectionInformationClass
,
461 OUT PVOID SectionInformation
,
463 OUT PULONG ResultLength
466 * FUNCTION: Queries the information of a section object.
468 * SectionHandle = Handle to the section link object
469 * SectionInformationClass = Index to a certain information structure
470 * SectionInformation (OUT)= Caller supplies storage for resulting
472 * Length = Size of the supplied storage
473 * ResultLength = Data written
478 return(STATUS_UNSUCCESSFUL
);
485 IN HANDLE SectionHandle
,
486 IN ULONG NewMaximumSize