summaryrefslogtreecommitdiff
path: root/src/pal/src/include/pal/map.hpp
blob: a2d8e2b748b64925d44da454c10f356364dc54e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*++



Module Name:

    include/pal/map.hpp

Abstract:

    Header file for file mapping functions.



--*/

#ifndef _PAL_MAP_H_
#define _PAL_MAP_H_

#include "corunix.hpp"
#include <sys/param.h>

extern "C"
{
#include "list.h"

#ifndef NO_INO
#define NO_INO ((ino_t)-1)
#endif

    /*++
    Function :
        MapInitialize

        Initialize the critical sections.

    Return value:
        TRUE  if initialization succeeded
        FALSE otherwise        
    --*/
    BOOL MAPInitialize( void );

    /*++
    Function :
        MapCleanup

        Deletes the critical sections.

    --*/
    void MAPCleanup( void );

    /*++
    Function :
        MAPGetRegionInfo

        Parameters: 
        lpAddress: pointer to the starting memory location, not necessary
                   to be rounded to the page location

        lpBuffer: if this function finds information about the specified address,
                  the information is stored in this struct

        Note: This function is to be used in virtual.c
              
        Returns TRUE if this function finds information about the specified address
    --*/

    BOOL MAPGetRegionInfo(LPVOID lpAddress, PMEMORY_BASIC_INFORMATION lpBuffer);

    /*++
        MAPMapPEFile -

        Map a PE format file into memory like Windows LoadLibrary() would do.
        Doesn't apply base relocations if the function is relocated.

        There are two scenarios:

        - image could be preloaded and then MAPMapPEFile is called for it
        - image is loaded with MAPMapPEFile

        In the first scenario, hFile and lpPreloadedBase are supposed to be NULL, and szPath and size - non-NULL.
        In the second scenario, hFile and lpPreloadedBase are supposed to be non-NULL, and szPath and size - NULL.

        See coreclr_preload_assembly for further details.

    Parameters:
        IN hFile - file to map
        IN szPath - path to mapped file
        OUT size - mapped virtual size
        IN lpPreloadedBase - previously loaded base

    Return value:
        non-NULL - the base address of the mapped image
        NULL - error, with last error set.
    --*/

    void * MAPMapPEFile(HANDLE hFile, LPCSTR szPath, SIZE_T *size, LPVOID lpPreloadedBase);

    /*++
        MAPApplyBaseRelocationsPreloadedPEFile -

        Apply base relocations to preloaded image

    Parameters:
        IN lpMappedImage - base address of preloaded image
        IN virtualSize - virtual size of preloaded image

    Return value:
        true - if relocations were applied successfully
        false - otherwise
    --*/

    bool MAPApplyBaseRelocationsPreloadedPEFile(LPVOID lpMappedImage, SIZE_T virtualSize);

    /*++
        MAPUnmapPreloadedPEFile -

        Unmap a PE file

    Parameters:
        IN lpMappedImage - address of mapped file
        IN size - virtual size

    Return value:
        returns TRUE if successful, FALSE otherwise
    --*/

    BOOL MAPUnmapPreloadedPEFile(LPVOID lpMappedImage, SIZE_T size);

    /*++
    Function :
        MAPUnmapPEFile - unmap a PE file, and remove it from the recorded list of PE files mapped

        returns TRUE if successful, FALSE otherwise
    --*/
    BOOL MAPUnmapPEFile(LPCVOID lpAddress);

    /*++
    Function :
        MAPMarkSectionAsNotNeeded - mark a section as NotNeeded
        returns TRUE if successful, FALSE otherwise
    --*/
    BOOL MAPMarkSectionAsNotNeeded(LPCVOID lpAddress);
}

namespace CorUnix
{
    extern CObjectType otFileMapping;

#if ONE_SHARED_MAPPING_PER_FILEREGION_PER_PROCESS
    typedef struct _NativeMapHolder
    {
        Volatile<LONG> ref_count;
        LPVOID address;
        SIZE_T size;
        SIZE_T offset; /* for future use */
    } NativeMapHolder;
#endif

    /* Process specific information. This 
    structure is not stored in shared memory.*/
    typedef struct _MVL
    {
        LIST_ENTRY Link;
        
        //
        // Each MVL entry holds a reference to its parent file
        // mapping object.
        //
        
        IPalObject *pFileMapping;
        
#if ONE_SHARED_MAPPING_PER_FILEREGION_PER_PROCESS
        NativeMapHolder * pNMHolder; /* Ref-counted holder for memory mapping */
        dev_t   MappedFileDevNum;           /* ID of device containing the file to be mapped */
        ino_t   MappedFileInodeNum;         /* Inode number of file to be mapped.
                                               These two fields are used used to uniquely 
                                               identify files on systems that do not allow 
                                               more than one shared mmapping per region of 
                                               physical file, per process */
#endif
        LPVOID lpAddress;           /* The pointer to the mapped memory. */
        SIZE_T NumberOfBytesToMap;  /* Number of bytes to map. */
        DWORD dwDesiredAccess;      /* Desired access. */
        LPVOID lpPEBaseAddress;     /* If this mapping is part of a PE file mapping, this is the
                                       base address pointer of the PE file (used to find all
                                       parts of the PE file mapping to allow PE file unload).
                                       Otherwise, it is NULL. */
    } MAPPED_VIEW_LIST, * PMAPPED_VIEW_LIST;

    class CFileMappingImmutableData
    {
    public:
        CHAR *lpFileName;
        UINT MaxSize;               // The max size of the file mapping object
        DWORD flProtect;            // Protection desired for the file view
        BOOL bPALCreatedTempFile;   // TRUE if it's a PAL created file
        DWORD dwDesiredAccessWhenOpened;  // FILE_MAP_WRITE etc
    };

    class CFileMappingProcessLocalData 
    {
    public:
        INT     UnixFd;                     /* File descriptor. */
        
#if ONE_SHARED_MAPPING_PER_FILEREGION_PER_PROCESS
        dev_t   MappedFileDevNum;           /* ID of device containing the file to be mapped */
        ino_t   MappedFileInodeNum;         /* Inode number of file to be mapped.
                                               These two fields are used used to uniquely 
                                               identify files on systems that do not allow 
                                               more than one shared mmapping per region of 
                                               physical file, per process */
#endif
    };

    PAL_ERROR
    InternalCreateFileMapping(
        CPalThread *pThread,
        HANDLE hFile,
        LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
        DWORD flProtect,
        DWORD dwMaximumSizeHigh,
        DWORD dwMaximumSizeLow,
        LPCWSTR lpName,
        HANDLE *phMapping
        );

    PAL_ERROR
    InternalMapViewOfFile(
        CPalThread *pThread,
        HANDLE hFileMappingObject,
        DWORD dwDesiredAccess,
        DWORD dwFileOffsetHigh,
        DWORD dwFileOffsetLow,
        SIZE_T dwNumberOfBytesToMap,
        LPVOID *ppvBaseAddress
        );

    PAL_ERROR
    InternalUnmapViewOfFile(
        CPalThread *pThread,
        LPCVOID lpBaseAddress
        );

}

#endif /* _PAL_MAP_H_ */