123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609 |
- /*++
- Copyright (c) 2013 Minoca Corp. All Rights Reserved
- Module Name:
- osbase.c
- Abstract:
- This module implements the Operating System Base interface.
- Author:
- Evan Green 25-Feb-2013
- Environment:
- User mode
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include "osbasep.h"
- //
- // ---------------------------------------------------------------- Definitions
- //
- #define OS_MOUNT_ALLOCATION_TAG 0x744D734F // 'tMsO'
- #define OS_VERSION_ALLOCATION_TAG 0x73726556 // 'sreV'
- #define OS_CURRENT_DIRECTORY_ALLOCATION_TAG 0x6443734F // 'dCsO'
- //
- // Defines the number of times the get mount points routine should try to
- // collect mount points.
- //
- #define OS_GET_MOUNT_POINTS_TRY_COUNT 5
- //
- // Defines the initial size of the mount points buffer.
- //
- #define OS_GET_MOUNT_POINTS_BUFFER_SIZE_GUESS 4096
- //
- // Defines the number of times the get current directory routine should try
- // to collect the current directory string.
- //
- #define OS_GET_CURRENT_DIRECTORY_TRY_COUNT 5
- //
- // Defines the initial size of the current directory buffer.
- //
- #define OS_GET_CURRENT_DIRECTORY_BUFFER_SIZE_GUESS 256
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- PVOID
- OspSetSignalHandler (
- PVOID SignalHandlerRoutine
- );
- KSTATUS
- OspGetSetFileInformation (
- HANDLE Directory,
- PSTR Path,
- ULONG PathSize,
- BOOL FollowLink,
- PSET_FILE_INFORMATION Request
- );
- KSTATUS
- OspMountOrUnmount (
- PSTR MountPointPath,
- ULONG MountPointPathSize,
- PSTR TargetPath,
- ULONG TargetPathSize,
- ULONG Flags
- );
- NO_RETURN
- VOID
- OspExitThread (
- PVOID UnmapAddress,
- UINTN UnmapSize
- );
- //
- // -------------------------------------------------------------------- Globals
- //
- //
- // Store a boolean indicating whether this library is initialized or not to be
- // moderately defensive against people accidentally calling this routine
- // multiple times.
- //
- BOOL OsLibraryInitialized = FALSE;
- //
- // Store a pointer to the signal handling routine.
- //
- volatile PSIGNAL_HANDLER_ROUTINE OsSignalHandler = NULL;
- //
- // Store pointers to the system version information if it has been requested
- // already.
- //
- PSTR OsProductName;
- PSTR OsBuildString;
- ULONG OsSystemVersionStringsSize;
- //
- // ------------------------------------------------------------------ Functions
- //
- OS_API
- VOID
- OsInitializeLibrary (
- PPROCESS_ENVIRONMENT Environment
- )
- /*++
- Routine Description:
- This routine initializes the base OS library. It needs to be called only
- once, when the library is first loaded.
- Arguments:
- Environment - Supplies a pointer to the environment information.
- Return Value:
- None.
- --*/
- {
- if (OsLibraryInitialized != FALSE) {
- return;
- }
- OsEnvironment = Environment;
- OsLibraryInitialized = TRUE;
- OspSetUpSystemCalls();
- OspInitializeMemory();
- OspInitializeImageSupport();
- OspInitializeThreadSupport();
- //
- // Register the signal handler to start receiving signals.
- //
- OspSetSignalHandler(OspSignalHandler);
- return;
- }
- OS_API
- VOID
- OsTestSystemCall (
- VOID
- )
- /*++
- Routine Description:
- This routine performs a meaningless system call.
- Arguments:
- None.
- Return Value:
- None.
- --*/
- {
- OsSystemCall(SystemCallInvalid, NULL);
- return;
- }
- OS_API
- KSTATUS
- OsOpen (
- HANDLE Directory,
- PSTR Path,
- ULONG PathLength,
- ULONG Flags,
- FILE_PERMISSIONS CreatePermissions,
- PHANDLE Handle
- )
- /*++
- Routine Description:
- This routine opens a file or other I/O object.
- Arguments:
- Directory - Supplies an optional handle to a directory to start the
- search from if the supplied path is relative. Supply INVALID_HANDLE
- here to to use the current directory for relative paths.
- Path - Supplies a pointer to a string containing the path of the object
- to open.
- PathLength - Supplies the length of the path buffer, in bytes, including
- the null terminator.
- Flags - Supplies flags associated with the open operation. See
- SYS_OPEN_FLAG_* definitions.
- CreatePermissions - Supplies the permissions for create operations.
- Handle - Supplies a pointer where a handle will be returned on success.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_OPEN Parameters;
- Parameters.Directory = Directory;
- Parameters.Path = Path;
- Parameters.PathBufferLength = PathLength;
- Parameters.Flags = Flags;
- Parameters.CreatePermissions = CreatePermissions & FILE_PERMISSION_MASK;
- OsSystemCall(SystemCallOpen, &Parameters);
- *Handle = Parameters.Handle;
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsOpenDevice (
- DEVICE_ID DeviceId,
- ULONG Flags,
- PHANDLE Handle
- )
- /*++
- Routine Description:
- This routine opens a device directly.
- Arguments:
- DeviceId - Supplies the identifier of the device to open.
- Flags - Supplies flags associated with the open operation. See
- SYS_OPEN_FLAG_* definitions.
- Handle - Supplies a pointer where a handle will be returned on success.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_OPEN_DEVICE Parameters;
- Parameters.DeviceId = DeviceId;
- Parameters.Flags = Flags;
- OsSystemCall(SystemCallOpenDevice, &Parameters);
- *Handle = Parameters.Handle;
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsClose (
- HANDLE Handle
- )
- /*++
- Routine Description:
- This routine closes an I/O handle.
- Arguments:
- Handle - Supplies a pointer to the open handle.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_CLOSE Parameters;
- Parameters.Handle = Handle;
- OsSystemCall(SystemCallClose, &Parameters);
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsPerformIo (
- HANDLE Handle,
- IO_OFFSET Offset,
- UINTN Size,
- ULONG Flags,
- ULONG TimeoutInMilliseconds,
- PVOID Buffer,
- PUINTN BytesCompleted
- )
- /*++
- Routine Description:
- This routine performs I/O on an open handle.
- Arguments:
- Handle - Supplies a pointer to the opened I/O handle.
- Offset - Supplies the offset into the file to read from or write to. Set
- this to IO_OFFSET_NONE to do I/O at the current file position or for
- handles that are not seekable.
- Size - Supplies the number of bytes to transfer.
- Flags - Supplies a bitfield of flags. See SYS_IO_FLAG_* definitions.
- TimeoutInMilliseconds - Supplies the number of milliseconds that the I/O
- operation should be waited on before timing out. Use
- SYS_WAIT_TIME_INDEFINITE to wait forever on the I/O.
- Buffer - Supplies a pointer to the buffer containing the data to write or
- where the read data should be returned, depending on the operation.
- BytesCompleted - Supplies a pointer where the number of bytes completed
- will be returned.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_PERFORM_IO Parameters;
- Parameters.Handle = Handle;
- Parameters.Buffer = Buffer;
- Parameters.Flags = Flags;
- Parameters.TimeoutInMilliseconds = TimeoutInMilliseconds;
- Parameters.Offset = Offset;
- Parameters.Size = Size;
- OsSystemCall(SystemCallPerformIo, &Parameters);
- *BytesCompleted = Parameters.Size;
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsPerformVectoredIo (
- HANDLE Handle,
- IO_OFFSET Offset,
- UINTN Size,
- ULONG Flags,
- ULONG TimeoutInMilliseconds,
- PIO_VECTOR VectorArray,
- UINTN VectorCount,
- PUINTN BytesCompleted
- )
- /*++
- Routine Description:
- This routine performs I/O on an open handle.
- Arguments:
- Handle - Supplies a pointer to the opened I/O handle.
- Offset - Supplies the offset into the file to read from or write to. Set
- this to IO_OFFSET_NONE to do I/O at the current file position or for
- handles that are not seekable.
- Size - Supplies the number of bytes to transfer.
- Flags - Supplies a bitfield of flags. See SYS_IO_FLAG_* definitions.
- TimeoutInMilliseconds - Supplies the number of milliseconds that the I/O
- operation should be waited on before timing out. Use
- SYS_WAIT_TIME_INDEFINITE to wait forever on the I/O.
- VectorArray - Supplies an array of I/O vector structures to do I/O to/from.
- VectorCount - Supplies the number of elements in the vector array.
- BytesCompleted - Supplies a pointer where the number of bytes completed
- will be returned.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_PERFORM_VECTORED_IO Parameters;
- Parameters.Handle = Handle;
- Parameters.Flags = Flags;
- Parameters.TimeoutInMilliseconds = TimeoutInMilliseconds;
- Parameters.Offset = Offset;
- Parameters.Size = Size;
- Parameters.VectorArray = VectorArray;
- Parameters.VectorCount = VectorCount;
- OsSystemCall(SystemCallPerformVectoredIo, &Parameters);
- *BytesCompleted = Parameters.Size;
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsFlush (
- HANDLE Handle,
- ULONG Flags
- )
- /*++
- Routine Description:
- This routine flushes handle data to its backing device. If the flags
- specify that all data is to be flushed, then a handle is not required.
- Arguments:
- Handle - Supplies an open I/O handle. This parameter is not required if
- SYS_FLUSH_FLAG_ALL is set.
- Flags - Supplies a bitfield of flags. See SYS_FLUSH_FLAG_* definitions.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_FLUSH Parameters;
- Parameters.Handle = Handle;
- Parameters.Flags = Flags;
- OsSystemCall(SystemCallFlush, &Parameters);
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsCreatePipe (
- HANDLE Directory,
- PSTR Path,
- ULONG PathLength,
- ULONG Flags,
- FILE_PERMISSIONS Permissions,
- PHANDLE ReadHandle,
- PHANDLE WriteHandle
- )
- /*++
- Routine Description:
- This routine creates a pipe.
- Arguments:
- Directory - Supplies an optional handle to a directory to start the
- search from if the supplied path is relative. Supply INVALID_HANDLE
- here to to use the current directory for relative paths.
- Path - Supplies an optional pointer to the path to create the pipe at.
- PathLength - Supplies the length of the path buffer in bytes, including
- the null terminator character.
- Flags - Supplies a bitfield of flags governing the behavior of the new pipe
- descriptors. Only SYS_OPEN_FLAG_CLOSE_ON_EXECUTE and
- SYS_OPEN_FLAG_NON_BLOCKING are permitted.
- Permissions - Supplies the initial permissions to set on the pipe.
- ReadHandle - Supplies a pointer where the handle to the read end of the
- pipe will be returned on success. Handles are only returned if a
- NULL path was passed in.
- WriteHandle - Supplies a pointer where the handle to the write end of the
- pipe will be returned on success. Handles are only returned in a NULL
- path was passed in.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_CREATE_PIPE Parameters;
- Parameters.Directory = Directory;
- Parameters.Path = Path;
- Parameters.PathLength = PathLength;
- Parameters.OpenFlags = Flags;
- Parameters.Permissions = Permissions;
- OsSystemCall(SystemCallCreatePipe, &Parameters);
- if (Path == NULL) {
- *ReadHandle = Parameters.ReadHandle;
- *WriteHandle = Parameters.WriteHandle;
- }
- return Parameters.Status;
- }
- OS_API
- VOID
- OsExitThread (
- PVOID UnmapAddress,
- UINTN UnmapSize
- )
- /*++
- Routine Description:
- This routine terminates the current thread, and optionally attempts to
- unmap a region of memory on its way out. Usually this is the stack of the
- thread that is exiting.
- Arguments:
- UnmapAddress - Supplies an optional pointer to a region of memory to unmap
- as the thread exits. Supply NULL to skip unmapping.
- UnmapSize - Supplies the size of the region to unmap in bytes. This must be
- aligned to the page size. If it is not, the unmap simply won't happen.
- Supply 0 to skip the unmap and just exit the thread. If -1 is supplied,
- this routine returns. This value can be used to warm up the PLT entry,
- since lazy binding cannot take place after the thread's control block
- has been destroyed.
- Return Value:
- This routine does not return, unless the magic size is passed in.
- --*/
- {
- if (UnmapSize == -1UL) {
- return;
- }
- OspExitThread(UnmapAddress, UnmapSize);
- }
- OS_API
- KSTATUS
- OsCreateThread (
- PSTR ThreadName,
- ULONG ThreadNameBufferLength,
- PTHREAD_ENTRY_ROUTINE ThreadRoutine,
- PVOID Parameter,
- PVOID StackBase,
- ULONG StackSize,
- PVOID ThreadPointer,
- PTHREAD_ID ThreadId
- )
- /*++
- Routine Description:
- This routine creates a new thread.
- Arguments:
- ThreadName - Supplies an optional pointer to the thread name.
- ThreadNameBufferLength - Supplies the size of the thread name buffer,
- including the null terminator.
- ThreadRoutine - Supplies a pointer to the funtion that should be run on the
- new thread.
- Parameter - Supplies a pointer that will be passed directly to the thread
- routine.
- StackBase - Supplies an optional pointer to the stack base address. If
- supplied, the kernel will not add a guard region or automatically
- expand the stack.
- StackSize - Supplies the size of the new thread's stack. Supply 0 to use
- the system default.
- ThreadPointer - Supplies the thread pointer to set for the new thread.
- ThreadId - Supplies an optional pointer where the ID of the new thread will
- be returned on success.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_CREATE_THREAD Parameters;
- Parameters.Name = ThreadName;
- Parameters.NameBufferLength = ThreadNameBufferLength;
- Parameters.ThreadRoutine = ThreadRoutine;
- Parameters.Parameter = Parameter;
- Parameters.StackBase = StackBase;
- Parameters.StackSize = StackSize;
- Parameters.ThreadPointer = ThreadPointer;
- Parameters.ThreadId = ThreadId;
- OsSystemCall(SystemCallCreateThread, &Parameters);
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsForkProcess (
- PPROCESS_ID NewProcessId
- )
- /*++
- Routine Description:
- This routine forks the current process into two separate processes. The
- child process begins executing in the middle of this function.
- Arguments:
- NewProcessId - Supplies a pointer that on success contains the process ID
- of the child process in the parent, and 0 in the child. This value
- contains -1 if the new process failed to spawn.
- Return Value:
- STATUS_SUCCESS in both the parent and child on success.
- Other status codes are returned to the parent if the child failed to spawn.
- --*/
- {
- SYSTEM_CALL_FORK_PROCESS Parameters;
- //
- // If the call is successful, the child's parameter structure will not be
- // modified. Set the parameters up with the return values for the child
- // before making the call so that when the child returns, it looks like the
- // kernel filled those values in.
- //
- Parameters.Status = STATUS_SUCCESS;
- Parameters.ProcessId = 0;
- OspSystemCallFull(SystemCallForkProcess, &Parameters);
- *NewProcessId = Parameters.ProcessId;
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsExecuteImage (
- PPROCESS_ENVIRONMENT Environment
- )
- /*++
- Routine Description:
- This routine replaces the currently running process with the given binary
- image.
- Arguments:
- Environment - Supplies a pointer to the environment to execute, which
- includes the image name, parameters, and environment variables.
- Return Value:
- If this routine succeeds, it will not return, as the process will be
- replaced with the new executable. If the process could not be started,
- a failing status code will be returned to the caller.
- --*/
- {
- SYSTEM_CALL_EXECUTE_IMAGE Parameters;
- RtlCopyMemory(&(Parameters.Environment),
- Environment,
- sizeof(PROCESS_ENVIRONMENT));
- return OspSystemCallFull(SystemCallExecuteImage, &Parameters);
- }
- OS_API
- KSTATUS
- OsGetSystemVersion (
- PSYSTEM_VERSION_INFORMATION VersionInformation,
- BOOL WantStrings
- )
- /*++
- Routine Description:
- This routine gets the system version information.
- Arguments:
- VersionInformation - Supplies a pointer where the system version
- information will be returned. The caller should not attempt to modify
- or free the strings pointed to by members of this structure.
- WantStrings - Supplies a boolean indicating if the build strings are
- desired or just the major/minor version information.
- Return Value:
- Status code.
- --*/
- {
- UINTN BufferSize;
- ULONG EncodedVersion;
- PVOID HeapAllocation;
- PSYSTEM_VERSION_INFORMATION LocalInformation;
- KSTATUS Status;
- PUSER_SHARED_DATA UserSharedData;
- Status = STATUS_SUCCESS;
- //
- // Get the build strings from the kernel if needed.
- //
- if ((WantStrings != FALSE) && (OsProductName == NULL)) {
- BufferSize = 0;
- Status = OsGetSetSystemInformation(SystemInformationKe,
- KeInformationSystemVersion,
- NULL,
- &BufferSize,
- FALSE);
- ASSERT(Status == STATUS_BUFFER_TOO_SMALL);
- OsSystemVersionStringsSize = BufferSize;
- HeapAllocation = OsHeapAllocate(BufferSize, OS_VERSION_ALLOCATION_TAG);
- if (HeapAllocation == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto GetSystemVersionEnd;
- }
- Status = OsGetSetSystemInformation(SystemInformationKe,
- KeInformationSystemVersion,
- HeapAllocation,
- &BufferSize,
- FALSE);
- if (!KSUCCESS(Status)) {
- goto GetSystemVersionEnd;
- }
- LocalInformation = HeapAllocation;
- if (LocalInformation->BuildString != NULL) {
- LocalInformation->BuildString =
- (PVOID)((UINTN)LocalInformation +
- (UINTN)(LocalInformation->BuildString));
- }
- if (LocalInformation->ProductName != NULL) {
- LocalInformation->ProductName =
- (PVOID)((UINTN)LocalInformation +
- (UINTN)(LocalInformation->ProductName));
- }
- OsBuildString = LocalInformation->BuildString;
- OsProductName = LocalInformation->ProductName;
- }
- //
- // Fill in the caller's structure.
- //
- UserSharedData = OspGetUserSharedData();
- EncodedVersion = UserSharedData->EncodedSystemVersion;
- VersionInformation->MajorVersion = DECODE_MAJOR_VERSION(EncodedVersion);
- VersionInformation->MinorVersion = DECODE_MINOR_VERSION(EncodedVersion);
- VersionInformation->Revision = DECODE_VERSION_REVISION(EncodedVersion);
- VersionInformation->SerialVersion = UserSharedData->SystemVersionSerial;
- VersionInformation->ReleaseLevel = DECODE_VERSION_RELEASE(EncodedVersion);
- VersionInformation->DebugLevel = DECODE_VERSION_DEBUG(EncodedVersion);
- VersionInformation->BuildTime.Seconds = UserSharedData->BuildTime;
- VersionInformation->BuildTime.Nanoseconds = 0;
- VersionInformation->ProductName = NULL;
- VersionInformation->BuildString = NULL;
- //
- // Copy the strings as well if requested.
- //
- if (WantStrings != FALSE) {
- VersionInformation->ProductName = OsProductName;
- VersionInformation->BuildString = OsBuildString;
- }
- Status = STATUS_SUCCESS;
- GetSystemVersionEnd:
- return Status;
- }
- OS_API
- KSTATUS
- OsGetCurrentDirectory (
- BOOL Root,
- PSTR *Buffer,
- PUINTN BufferSize
- )
- /*++
- Routine Description:
- This routine retrieves a pointer to a null terminated string containing the
- path to the current working directory or the current root directory.
- Arguments:
- Root - Supplies a boolean indicating whether caller would like the current
- working directory (FALSE) or the path to the current root directory
- (TRUE). If the caller does not have permission to escape roots, or
- does not currently have an altered root directory, then / is returned.
- Buffer - Supplies a pointer that receives a pointer to a buffer that
- contains a null terminated string for the path to the current
- directory.
- BufferSize - Supplies a pointer that receives the size of the buffer, in
- bytes.
- Return Value:
- Status code.
- --*/
- {
- PSTR CurrentDirectory;
- UINTN CurrentDirectorySize;
- ULONG Index;
- SYSTEM_CALL_GET_CURRENT_DIRECTORY Parameters;
- KSTATUS Status;
- CurrentDirectorySize = OS_GET_CURRENT_DIRECTORY_BUFFER_SIZE_GUESS;
- for (Index = 0; Index < OS_GET_CURRENT_DIRECTORY_TRY_COUNT; Index += 1) {
- CurrentDirectory = OsHeapAllocate(CurrentDirectorySize,
- OS_CURRENT_DIRECTORY_ALLOCATION_TAG);
- if (CurrentDirectory == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto GetCurrentDirectoryEnd;
- }
- Parameters.Root = Root;
- Parameters.Buffer = CurrentDirectory;
- Parameters.BufferSize = CurrentDirectorySize;
- OsSystemCall(SystemCallGetCurrentDirectory, &Parameters);
- //
- // Exit on any status besides a buffer too small result.
- //
- if (Parameters.Status != STATUS_BUFFER_TOO_SMALL) {
- break;
- }
- //
- // If the buffer is too small. Double the expected size just in case
- // another thread changes directories.
- //
- CurrentDirectorySize = Parameters.BufferSize * 2;
- OsHeapFree(CurrentDirectory);
- }
- Status = Parameters.Status;
- GetCurrentDirectoryEnd:
- if (KSUCCESS(Status)) {
- *Buffer = Parameters.Buffer;
- *BufferSize = Parameters.BufferSize;
- } else {
- OsHeapFree(CurrentDirectory);
- }
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsChangeDirectory (
- BOOL Root,
- PSTR Path,
- ULONG PathSize
- )
- /*++
- Routine Description:
- This routine sets the current working directory or current root directory.
- Arguments:
- Root - Supplies a boolean indicating whether to change the current working
- directory (FALSE) or the current root directory (TRUE). If attempting
- to change the root, the caller must have permission to change the root,
- must be running a single thread, and must not have any other handles
- to directories open.
- Path - Supplies a pointer to the path of the new working directory. If
- trying to escape the root, supply NULL here. The caller must have
- sufficient privileges to escape a root.
- PathSize - Supplies the size of the path directory string in bytes
- including the null terminator.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_CHANGE_DIRECTORY Parameters;
- Parameters.Root = Root;
- Parameters.Buffer = Path;
- Parameters.BufferLength = PathSize;
- Parameters.Handle = INVALID_HANDLE;
- OsSystemCall(SystemCallChangeDirectory, &Parameters);
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsChangeDirectoryHandle (
- BOOL Root,
- HANDLE Handle
- )
- /*++
- Routine Description:
- This routine sets the current working directory or root directory to the
- same directory opened with the given file handle.
- Arguments:
- Root - Supplies a boolean indicating whether to change the current working
- directory (FALSE) or the current root directory (TRUE). If attempting
- to change the root, the caller must have permission to change the root,
- must be running a single thread, and must not have any other handles
- to directories open.
- Handle - Supplies an open handle to a directory to change the current
- working directroy to. Supply INVALID_HANDLE here to escape the root.
- The caller must have sufficient privileges to escape a root.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_CHANGE_DIRECTORY Parameters;
- Parameters.Root = Root;
- Parameters.Buffer = NULL;
- Parameters.BufferLength = 0;
- Parameters.Handle = Handle;
- OsSystemCall(SystemCallChangeDirectory, &Parameters);
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsPoll (
- PSIGNAL_SET SignalMask,
- PPOLL_DESCRIPTOR Descriptors,
- ULONG DescriptorCount,
- ULONG TimeoutInMilliseconds,
- PULONG DescriptorsSelected
- )
- /*++
- Routine Description:
- This routine polls several I/O handles.
- Arguments:
- SignalMask - Supplies an optional pointer to a mask to set for the
- duration of the wait.
- Descriptors - Supplies a pointer to an array of poll descriptor structures
- describing the descriptors and events to wait on.
- DescriptorCount - Supplies the number of descriptors in the array.
- TimeoutInMilliseconds - Supplies the number of milliseconds to wait before
- giving up.
- DescriptorsSelected - Supplies a pointer where the number of descriptors
- that had activity will be returned on success.
- Return Value:
- STATUS_SUCCESS if one or more descriptors is ready for action.
- STATUS_INSUFFICIENT_RESOURCES if memory could not be allocated for the
- operation.
- STATUS_INTERRUPTED if a signal was caught during the wait.
- STATUS_TIMEOUT if no descriptors were ready in the given amount of time.
- --*/
- {
- SYSTEM_CALL_POLL Poll;
- Poll.SignalMask = SignalMask;
- Poll.Descriptors = Descriptors;
- Poll.DescriptorCount = DescriptorCount;
- Poll.TimeoutInMilliseconds = TimeoutInMilliseconds;
- OsSystemCall(SystemCallPoll, &Poll);
- *DescriptorsSelected = Poll.DescriptorsSelected;
- return Poll.Status;
- }
- OS_API
- PSIGNAL_HANDLER_ROUTINE
- OsSetSignalHandler (
- PSIGNAL_HANDLER_ROUTINE NewHandler
- )
- /*++
- Routine Description:
- This routine sets the signal handler routine called whenever a signal is
- delivered by the kernel.
- Arguments:
- NewHandler - Supplies a pointer to the new handler routine to use.
- Return Value:
- Returns a pointer to the old handler, or NULL if no other signal handlers
- were registered.
- --*/
- {
- PSIGNAL_HANDLER_ROUTINE OldHandler;
- OldHandler = (PVOID)RtlAtomicExchange((PVOID)&OsSignalHandler,
- (UINTN)NewHandler);
- return OldHandler;
- }
- OS_API
- KSTATUS
- OsSendSignal (
- SIGNAL_TARGET_TYPE TargetType,
- ULONG TargetId,
- ULONG SignalNumber,
- SHORT SignalCode,
- UINTN SignalParameter
- )
- /*++
- Routine Description:
- This routine sends a signal to a process, process group or thread.
- Arguments:
- TargetType - Supplies the target type to which the signal is being sent. It
- can be either a process, process group, or thread.
- TargetId - Supplies the ID for the signal's target process, process group,
- or thread.
- SignalNumber - Supplies the signal number to send.
- SignalCode - Supplies the signal code to send. See SIGNAL_CODE_*
- definitions.
- SignalParameter - Supplies a parameter to send with the signal if the
- signal is in the real time signal range.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_SEND_SIGNAL SendSignal;
- SendSignal.TargetType = TargetType;
- SendSignal.TargetId = TargetId;
- SendSignal.SignalNumber = SignalNumber;
- SendSignal.SignalCode = SignalCode;
- SendSignal.SignalParameter = SignalParameter;
- //
- // Use the full system call because it makes debugging easier when
- // something like an abort gets forwarded to the kernel mode debugger.
- //
- OspSystemCallFull(SystemCallSendSignal, &SendSignal);
- return SendSignal.Status;
- }
- OS_API
- KSTATUS
- OsGetProcessId (
- PROCESS_ID_TYPE ProcessIdType,
- PPROCESS_ID ProcessId
- )
- /*++
- Routine Description:
- This routine gets an identifier associated with the process, such as the
- process ID, thread ID, parent process ID, process group ID, and session ID.
- Arguments:
- ProcessIdType - Supplies the type of ID to get.
- ProcessId - Supplies a pointer that on input contains the process ID
- argument if the operation takes a parameter. On successful output,
- returns the desired ID. Supply zero to use the calling process ID.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_GET_SET_PROCESS_ID Parameters;
- //
- // TODO: These values should be read once and cached, which means changing
- // the system call interface and then invalidating on fork.
- //
- Parameters.ProcessIdType = ProcessIdType;
- Parameters.ProcessId = *ProcessId;
- Parameters.NewValue = 0;
- Parameters.Set = FALSE;
- OsSystemCall(SystemCallGetSetProcessId, &Parameters);
- *ProcessId = Parameters.ProcessId;
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsSetProcessId (
- PROCESS_ID_TYPE ProcessIdType,
- PROCESS_ID ProcessId,
- PROCESS_ID NewValue
- )
- /*++
- Routine Description:
- This routine sets an identifier associated with the process, such as the
- process group ID or session ID.
- Arguments:
- ProcessIdType - Supplies the type of ID to set. Not all types can be set.
- ProcessId - Supplies the ID of the process to change. Supply 0 to use the
- current process.
- NewValue - Supplies the new value to set.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_GET_SET_PROCESS_ID Parameters;
- Parameters.ProcessIdType = ProcessIdType;
- Parameters.ProcessId = ProcessId;
- Parameters.NewValue = NewValue;
- Parameters.Set = TRUE;
- OsSystemCall(SystemCallGetSetProcessId, &Parameters);
- return Parameters.Status;
- }
- OS_API
- SIGNAL_SET
- OsSetSignalBehavior (
- SIGNAL_MASK_TYPE MaskType,
- SIGNAL_MASK_OPERATION Operation,
- PSIGNAL_SET NewMask
- )
- /*++
- Routine Description:
- This routine sets signal behavior, either for the current thread in the
- case of the blocked signals, or for the process for other signal masks.
- Arguments:
- MaskType - Supplies the type of mask to change.
- Operation - Supplies the interaction between the new mask and the previous
- signal mask.
- NewMask - Supplies a pointer to the mask of signals that are affected by
- this operation.
- Return Value:
- Returns the original signal mask before this function was called.
- --*/
- {
- SYSTEM_CALL_SET_SIGNAL_BEHAVIOR SetSignalBehavior;
- SetSignalBehavior.MaskType = MaskType;
- SetSignalBehavior.Operation = Operation;
- if (NewMask != NULL) {
- SetSignalBehavior.SignalSet = *NewMask;
- } else {
- INITIALIZE_SIGNAL_SET(SetSignalBehavior.SignalSet);
- }
- OsSystemCall(SystemCallSetSignalBehavior, &SetSignalBehavior);
- return SetSignalBehavior.SignalSet;
- }
- OS_API
- KSTATUS
- OsWaitForChildProcess (
- ULONG Flags,
- PPROCESS_ID ChildPid,
- PULONG Reason,
- PUINTN ChildExitValue,
- PRESOURCE_USAGE ChildResourceUsage
- )
- /*++
- Routine Description:
- This routine is called to suspend execution of the current thread until
- a child process completes.
- Arguments:
- Flags - Supplies a bitfield of flags governing the behavior of the wait.
- See SYSTEM_CALL_WAIT_FLAG_* definitions.
- ChildPid - Supplies a pointer that on input supplies the child process ID
- parameter. This parameter can be one of the following:
- If -1 is supplied, any child signal will be pulled off and returned.
- If a number greater than 0 is supplied, only the specific process ID
- will be pulled off and returned.
- If 0 is supplied, any child process whose process group ID is equal to
- that of the calling process will be pulled.
- If a number less than zero (but not -1) is supplied, then any process
- whose process group ID is equal to the absolute value of this parameter
- will be dequeued and returned.
- On output, this parameter will contain the process ID of the child that
- generated the signal activity, and the child signal will be discarded.
- If the wait for child parameter is set to FALSE, then this parameter
- is ignored. If a non-child signal cause the routine to return, then
- the value at this parameter is undefined.
- Reason - Supplies a pointer where the reason for the child event will be
- returned. See CHILD_SIGNAL_REASON_* definitions.
- ChildExitValue - Supplies a pointer where the child exit value (or signal
- that caused the event) will be returned.
- ChildResourceUsage - Supplies an optional pointer where the resource usage
- of the child will be returned on success.
- Return Value:
- STATUS_SUCCESS if the wait was successfully satisfied. If the
- SYSTEM_CALL_WAIT_FLAG_RETURN_IMMEDIATELY flag is set and there are no
- children ready to be reaped, then STATUS_SUCCESS is returned and the child
- PID is returned as -1.
- STATUS_INTERRUPTED if the wait was interrupted by a signal.
- STATUS_NO_ELIGIBLE_CHILDREN if no eligible children could be reaped.
- --*/
- {
- SYSTEM_CALL_WAIT_FOR_CHILD Parameters;
- Parameters.Flags = Flags;
- Parameters.ChildPid = -1;
- if (ChildPid != NULL) {
- Parameters.ChildPid = *ChildPid;
- }
- Parameters.ResourceUsage = ChildResourceUsage;
- OsSystemCall(SystemCallWaitForChildProcess, &Parameters);
- if (ChildPid != NULL) {
- *ChildPid = Parameters.ChildPid;
- }
- if (Reason != NULL) {
- *Reason = Parameters.Reason;
- }
- if (ChildExitValue != NULL) {
- *ChildExitValue = Parameters.ChildExitValue;
- }
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsSuspendExecution (
- SIGNAL_MASK_OPERATION SignalOperation,
- PSIGNAL_SET SignalSet,
- PSIGNAL_PARAMETERS SignalParameters,
- ULONG TimeoutInMilliseconds
- )
- /*++
- Routine Description:
- This routine suspends execution of the current thread until a signal comes
- in. The current thread's blocked signal mask can be changed for the
- duration of the call by providing an operation and a signal set.
- Arguments:
- SignalOperation - Supplies the operation to perform with the signal set for
- the duration of the call: set, clear, overwrite or none.
- SignalSet - Supplies a pointer to the signal set to apply for the duration
- of this system call as dictated by the signal operation.
- SignalParameters - Supplies an optional pointer where the signal
- information for the signal that occurred will be returned.
- TimeoutInMilliseconds - Supplies the timeout of the operation in
- milliseconds.
- Return Value:
- STATUS_SUCCESS if a signal arrived.
- STATUS_INTERRUPTED on a clear signal operation if a signal that is not in
- the given set arrived.
- STATUS_TIMEOUT if no signal arrived before the given timeout expires.
- STATUS_INVALID_PARAMETER if no signal set is supplied for an operation
- other than SignalMaskOperationNone.
- --*/
- {
- SYSTEM_CALL_SUSPEND_EXECUTION Parameters;
- if (SignalSet == NULL) {
- if (SignalOperation != SignalMaskOperationNone) {
- return STATUS_INVALID_PARAMETER;
- }
- } else {
- Parameters.SignalSet = *SignalSet;
- }
- Parameters.SignalOperation = SignalOperation;
- Parameters.SignalParameters = SignalParameters;
- Parameters.TimeoutInMilliseconds = TimeoutInMilliseconds;
- OsSystemCall(SystemCallSuspendExecution, &Parameters);
- return Parameters.Status;
- }
- OS_API
- NO_RETURN
- VOID
- OsExitProcess (
- UINTN Status
- )
- /*++
- Routine Description:
- This routine terminates the current process and any threads that may be
- running in it.
- Arguments:
- Status - Supplies the exit status, returned to the parent in the wait
- calls. Conventionally 0 indicates success, and non-zero indicates
- failure. The C library only recieves the first eight bits of the return
- status, portable applications should not set bits beyond that.
- Return Value:
- This routine does not return.
- --*/
- {
- SYSTEM_CALL_EXIT_PROCESS Parameters;
- Parameters.Status = Status;
- OsSystemCall(SystemCallExitProcess, &Parameters);
- while (TRUE) {
- ASSERT(FALSE);
- }
- }
- OS_API
- KSTATUS
- OsFileControl (
- HANDLE Handle,
- FILE_CONTROL_COMMAND Command,
- PFILE_CONTROL_PARAMETERS_UNION Parameters
- )
- /*++
- Routine Description:
- This routine performs a file control operation on the given handle.
- Arguments:
- Handle - Supplies the file handle to operate on.
- Command - Supplies the command to perform.
- Parameters - Supplies an optional pointer to any additional parameters
- needed by the command.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_FILE_CONTROL FileControl;
- FileControl.File = Handle;
- FileControl.Command = Command;
- FileControl.Parameters = Parameters;
- OsSystemCall(SystemCallFileControl, &FileControl);
- return FileControl.Status;
- }
- OS_API
- KSTATUS
- OsGetFileInformation (
- HANDLE Directory,
- PSTR Path,
- ULONG PathLength,
- BOOL FollowLink,
- PFILE_PROPERTIES Properties
- )
- /*++
- Routine Description:
- This routine gets the file properties for a given file.
- Arguments:
- Directory - Supplies an optional handle to a directory to start the
- search from if the supplied path is relative. Supply INVALID_HANDLE
- here to to use the current directory for relative paths.
- Path - Supplies a pointer to the string containing the file path to get
- properties for.
- PathLength - Supplies the length of the path string in bytes including the
- null terminator.
- FollowLink - Supplies a boolean indicating what to do if the file path
- points to a symbolic link. If set to TRUE, the file information set or
- returned will be for the file the link points to. If FALSE, the call
- will set or get information for the link itself.
- Properties - Supplies a pointer where the file properties will be returned
- on success.
- Return Value:
- Status code.
- --*/
- {
- SET_FILE_INFORMATION Request;
- KSTATUS Status;
- Request.FieldsToSet = 0;
- Status = OspGetSetFileInformation(Directory,
- Path,
- PathLength,
- FollowLink,
- &Request);
- if (KSUCCESS(Status)) {
- RtlCopyMemory(Properties,
- &(Request.FileProperties),
- sizeof(FILE_PROPERTIES));
- }
- return Status;
- }
- OS_API
- KSTATUS
- OsSetFileInformation (
- HANDLE Directory,
- PSTR Path,
- ULONG PathLength,
- BOOL FollowLink,
- PSET_FILE_INFORMATION Request
- )
- /*++
- Routine Description:
- This routine sets the file properties for a given file.
- Arguments:
- Directory - Supplies an optional handle to a directory to start the
- search from if the supplied path is relative. Supply INVALID_HANDLE
- here to to use the current directory for relative paths.
- Path - Supplies a pointer to the string containing the file path to set
- properties for.
- PathLength - Supplies the length of the path string in bytes including the
- null terminator.
- FollowLink - Supplies a boolean indicating what to do if the file path
- points to a symbolic link. If set to TRUE, the file information set or
- returned will be for the file the link points to. If FALSE, the call
- will set or get information for the link itself.
- Request - Supplies a pointer to the set file information request.
- Return Value:
- Status code.
- --*/
- {
- KSTATUS Status;
- Status = OspGetSetFileInformation(Directory,
- Path,
- PathLength,
- FollowLink,
- Request);
- return Status;
- }
- OS_API
- VOID
- OsDebugPrint (
- PSTR String,
- ULONG StringSize
- )
- /*++
- Routine Description:
- This routine prints a message to the debug console. No formatting is
- provided.
- Arguments:
- String - Supplies a pointer to the string to print.
- StringSize - Supplies the size of the string in bytes including the null
- terminator.
- Return Value:
- None.
- --*/
- {
- OsDebug(DebugCommandPrint, 0, NULL, String, StringSize, 0);
- return;
- }
- OS_API
- KSTATUS
- OsDebug (
- DEBUG_COMMAND_TYPE Command,
- PROCESS_ID Process,
- PVOID Address,
- PVOID Data,
- ULONG Size,
- ULONG SignalToDeliver
- )
- /*++
- Routine Description:
- This routine sends a debug command to a process.
- Arguments:
- Command - Supplies the command to send.
- Process - Supplies the process ID to send the command to.
- Address - Supplies the address parameter of the command, usually the
- address in the target to read from or write to.
- Data - Supplies the data parameter, usually the buffer containing the data
- to write or the buffer where the read data will be returned.
- Size - Supplies the size of the buffer, in bytes.
- SignalToDeliver - Supplies the signal number to deliver to the debugged
- process for step and continue commands. For other commands, this
- parameter is ignored.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_DEBUG Request;
- Request.Command.Command = Command;
- Request.Process = Process;
- Request.Command.U.Address = Address;
- Request.Command.Data = Data;
- Request.Command.Size = Size;
- Request.Command.SignalToDeliver = SignalToDeliver;
- OsSystemCall(SystemCallDebug, &Request);
- return Request.Command.Status;
- }
- OS_API
- KSTATUS
- OsSeek (
- HANDLE Handle,
- SEEK_COMMAND SeekCommand,
- IO_OFFSET Offset,
- PIO_OFFSET NewOffset
- )
- /*++
- Routine Description:
- This routine seeks to the given position in a file. This routine is only
- relevant for normal file or block based devices.
- Arguments:
- Handle - Supplies the open file handle.
- SeekCommand - Supplies the reference point for the seek offset. Usual
- reference points are the beginning of the file, current file position,
- and the end of the file.
- Offset - Supplies the offset from the reference point to move in bytes.
- NewOffset - Supplies an optional pointer where the file position after the
- move will be returned on success.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_SEEK Request;
- Request.Handle = Handle;
- Request.Command = SeekCommand;
- Request.Offset = Offset;
- OsSystemCall(SystemCallSeek, &Request);
- if (NewOffset != NULL) {
- *NewOffset = Request.Offset;
- }
- return Request.Status;
- }
- OS_API
- KSTATUS
- OsCreateSymbolicLink (
- HANDLE Directory,
- PSTR Path,
- ULONG PathSize,
- PSTR LinkDestinationBuffer,
- ULONG LinkDestinationBufferSize
- )
- /*++
- Routine Description:
- This routine creates a symbolic link.
- Arguments:
- Directory - Supplies an optional handle to a directory to start the
- search from if the supplied path is relative. Supply INVALID_HANDLE
- here to to use the current directory for relative paths.
- Path - Supplies a pointer to the symbolic link path.
- PathSize - Supplies the size of the symbolic link pointer in bytes
- including the null terminator.
- LinkDestinationBuffer - Supplies a pointer to a string containing the link's
- target path, the location the link points to.
- LinkDestinationBufferSize - Supplies the size of the link destination
- buffer in bytes, NOT including the null terminator.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_CREATE_SYMBOLIC_LINK Parameters;
- Parameters.Directory = Directory;
- Parameters.Path = Path;
- Parameters.PathSize = PathSize;
- Parameters.LinkDestinationBuffer = LinkDestinationBuffer;
- Parameters.LinkDestinationBufferSize = LinkDestinationBufferSize;
- OsSystemCall(SystemCallCreateSymbolicLink, &Parameters);
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsReadSymbolicLink (
- HANDLE Directory,
- PSTR Path,
- ULONG PathSize,
- PSTR LinkDestinationBuffer,
- ULONG LinkDestinationBufferSize,
- PULONG LinkDestinationSize
- )
- /*++
- Routine Description:
- This routine reads the destination path of a symbolic link.
- Arguments:
- Directory - Supplies an optional handle to a directory to start the
- search from if the supplied path is relative. Supply INVALID_HANDLE
- here to to use the current directory for relative paths.
- Path - Supplies a pointer to the symbolic link path.
- PathSize - Supplies the size of the symbolic link pointer in bytes
- including the null terminator.
- LinkDestinationBuffer - Supplies a pointer to a buffer where the
- destination of the link will be returned. A null terminator byte is not
- written.
- LinkDestinationBufferSize - Supplies the size of the link destination
- buffer in bytes.
- LinkDestinationSize - Supplies a pointer where the actual size of the
- link destination (including the null terminator) will be returned on
- either success or a STATUS_BUFFER_TOO_SMALL case. On failure, 0 will be
- returned here.
- Return Value:
- STATUS_SUCCESS on success.
- STATUS_BUFFER_TOO_SMALL if the link destination buffer was not large
- enough to store the link destination.
- Other errors on other failures.
- --*/
- {
- SYSTEM_CALL_READ_SYMBOLIC_LINK Parameters;
- Parameters.Directory = Directory;
- Parameters.Path = Path;
- Parameters.PathSize = PathSize;
- Parameters.LinkDestinationBuffer = LinkDestinationBuffer;
- Parameters.LinkDestinationBufferSize = LinkDestinationBufferSize;
- OsSystemCall(SystemCallReadSymbolicLink, &Parameters);
- *LinkDestinationSize = Parameters.LinkDestinationSize;
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsCreateHardLink (
- HANDLE ExistingFileDirectory,
- PSTR ExistingFile,
- ULONG ExistingFileSize,
- HANDLE LinkDirectory,
- PSTR LinkPath,
- ULONG LinkPathSize,
- BOOL FollowExistingFileLinks
- )
- /*++
- Routine Description:
- This routine creates a hard link.
- Arguments:
- ExistingFileDirectory - Supplies an optional handle to the directory to
- start path traversal from if the specified existing file path is
- relative. Supply INVALID_HANDLE here to use the current directory for
- relative paths.
- ExistingFile - Supplies a pointer to the path of the existing file to
- create the link from.
- ExistingFileSize - Supplies the size of the existing file path buffer in
- bytes, including the null terminator.
- LinkDirectory - Supplies an optional handle to the directory to start path
- traversal from if the specified new link path is relative. Supply
- INVALID_HANDLE here to use the current directory for relative paths.
- LinkPath - Supplies a pointer to a string containing the destination path
- of the new link.
- LinkPathSize - Supplies the size of the link path buffer in bytes.
- FollowExistingFileLinks - Supplies a boolean indicating that if the
- existing file path exists and is a symbolic link, the new link shall be
- for the target of that link (TRUE) rather than the link itself (FALSE).
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_CREATE_HARD_LINK Parameters;
- Parameters.ExistingFileDirectory = ExistingFileDirectory;
- Parameters.ExistingFilePath = ExistingFile;
- Parameters.ExistingFilePathSize = ExistingFileSize;
- Parameters.NewLinkDirectory = LinkDirectory;
- Parameters.NewLinkPath = LinkPath;
- Parameters.NewLinkPathSize = LinkPathSize;
- Parameters.FollowLinks = FollowExistingFileLinks;
- OsSystemCall(SystemCallCreateHardLink, &Parameters);
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsDelete (
- HANDLE Directory,
- PSTR Path,
- ULONG PathSize,
- ULONG Flags
- )
- /*++
- Routine Description:
- This routine attempts to delete the object at the given path. If the path
- points to a directory, the directory must be empty. If the path points to
- a file, the hard link count on the file is decremented. If the hard link
- count reaches zero and no processes have the file open, the contents of the
- file are destroyed. If processes have open handles to the file, the
- destruction of the file contents are deferred until the last handle to the
- old file is closed. If the path points to a symbolic link, the link itself
- is removed and not the destination. The removal of the entry from the
- directory is immediate.
- Arguments:
- Directory - Supplies an optional handle to a directory to start the
- search from if the supplied path is relative. Supply INVALID_HANDLE
- here to to use the current directory for relative paths.
- Path - Supplies a pointer to the path to delete.
- PathSize - Supplies the length of the path buffer in bytes, including the
- null terminator.
- Flags - Supplies flags associated with the delete operation. See
- SYS_DELETE_FLAG_* definitions.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_DELETE Parameters;
- Parameters.Directory = Directory;
- Parameters.Path = Path;
- Parameters.PathSize = PathSize;
- Parameters.Flags = Flags;
- OsSystemCall(SystemCallDelete, &Parameters);
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsRename (
- HANDLE SourceDirectory,
- PSTR SourcePath,
- ULONG SourcePathSize,
- HANDLE DestinationDirectory,
- PSTR DestinationPath,
- ULONG DestinationPathSize
- )
- /*++
- Routine Description:
- This routine attempts to rename the object at the given path. This routine
- operates on symbolic links themselves, not the destinations of symbolic
- links. If the source and destination paths are equal, this routine will do
- nothing and return successfully. If the source path is not a directory, the
- destination path must not be a directory. If the destination file exists,
- it will be deleted. The caller must have write access in both the old and
- new directories. If the source path is a directory, the destination path
- must not exist or be an empty directory. The destination path must not have
- a path prefix of the source (ie it's illegal to move /my/path into
- /my/path/stuff).
- Arguments:
- SourceDirectory - Supplies an optional handle to the directory to start
- source path searches from. If the source path is absolute, this value
- is ignored. If this is INVALID_HANDLE, then source path searches will
- start from the current working directory.
- SourcePath - Supplies a pointer to the path of the file to rename.
- SourcePathSize - Supplies the length of the source path buffer in bytes,
- including the null terminator.
- DestinationDirectory - Supplies an optional handle to the directory to
- start destination path searches from. If the destination path is
- absolute, this value is ignored. If this is INVALID_HANDLE, then
- destination path searches will start from the current working directory.
- DestinationPath - Supplies a pointer to the path to rename the file to.
- DestinationPathSize - Supplies the size of the destination path buffer in
- bytes, including the null terminator.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_RENAME Parameters;
- Parameters.SourceDirectory = SourceDirectory;
- Parameters.SourcePath = SourcePath;
- Parameters.SourcePathSize = SourcePathSize;
- Parameters.DestinationDirectory = DestinationDirectory;
- Parameters.DestinationPath = DestinationPath;
- Parameters.DestinationPathSize = DestinationPathSize;
- OsSystemCall(SystemCallRename, &Parameters);
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsUserControl (
- HANDLE Handle,
- ULONG RequestCode,
- PVOID Context,
- UINTN ContextSize
- )
- /*++
- Routine Description:
- This routine sends a user I/O request to the given file/device/etc.
- Arguments:
- Handle - Supplies the open file handle to send the request to.
- RequestCode - Supplies the request to send. For device handles, this is the
- minor code of the IRP.
- Context - Supplies a pointer to the context buffer allocated by the
- caller for the request.
- ContextSize - Supplies the size of the supplied context buffer.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_USER_CONTROL Parameters;
- Parameters.Handle = Handle;
- Parameters.RequestCode = RequestCode;
- Parameters.Context = Context;
- Parameters.ContextSize = ContextSize;
- OsSystemCall(SystemCallUserControl, &Parameters);
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsMount (
- PSTR MountPointPath,
- ULONG MountPointPathSize,
- PSTR TargetPath,
- ULONG TargetPathSize,
- ULONG Flags
- )
- /*++
- Routine Description:
- This routine attempts to mount the given target at the given mount point.
- Arguments:
- MountPointPath - Supplies a pointer to a string containing the path to the
- mount point where the target is to be mounted.
- MountPointPathSize - Supplies the size of the mount point path string in
- bytes, including the null terminator.
- TargetPath - Supplies a pointer to a string containing the path to the
- target file, directory, volume, or device that is to be mounted.
- TargetPathSize - Supplies the size of the target path string in bytes,
- including the null terminator.
- Flags - Supplies the flags associated with the mount operation. See
- SYS_MOUNT_FLAG_* for definitions.
- Return Value:
- Status code.
- --*/
- {
- KSTATUS Status;
- Flags &= ~SYS_MOUNT_FLAG_UNMOUNT;
- Status = OspMountOrUnmount(MountPointPath,
- MountPointPathSize,
- TargetPath,
- TargetPathSize,
- Flags);
- return Status;
- }
- OS_API
- KSTATUS
- OsUnmount (
- PSTR MountPointPath,
- ULONG MountPointPathSize,
- ULONG Flags
- )
- /*++
- Routine Description:
- This routine attempts to unmount the given target. If the target is not a
- mount point, or the user does not have access to the mount point, then the
- routine will return the appropriate error. Otherwise, it will remove the
- mount point based on the supplied flags.
- Arguments:
- MountPointPath - Supplies a pointer to a string containing the path to the
- mount point that is to be unmounted.
- MountPointPathSize - Supplies the size of the mount point path string in
- bytes, including the null terminator.
- Flags - Supplies the flags associated with the mount operation. See
- SYS_MOUNT_FLAG_* for definitions.
- Return Value:
- Status code.
- --*/
- {
- KSTATUS Status;
- Flags |= SYS_MOUNT_FLAG_UNMOUNT;
- Status = OspMountOrUnmount(MountPointPath,
- MountPointPathSize,
- NULL,
- 0,
- Flags);
- return Status;
- }
- OS_API
- KSTATUS
- OsGetMountPoints (
- PVOID *Buffer,
- PUINTN BufferSize
- )
- /*++
- Routine Description:
- This routine returns the list of mount points currently in the system. It
- only returns the mounts that are visible to the calling process. The caller
- is responsible for releasing the buffer.
- Arguments:
- Buffer - Supplies a pointer that receives a buffer of mount point data.
- BufferSize - Supplies a pointer that receives the size of the mount point
- data.
- Return Value:
- Status code.
- --*/
- {
- ULONG Index;
- PVOID MountPoints;
- UINTN MountPointsSize;
- KSTATUS Status;
- MountPointsSize = OS_GET_MOUNT_POINTS_BUFFER_SIZE_GUESS;
- for (Index = 0; Index < OS_GET_MOUNT_POINTS_TRY_COUNT; Index += 1) {
- MountPoints = OsHeapAllocate(MountPointsSize, OS_MOUNT_ALLOCATION_TAG);
- if (MountPoints == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto GetMountPointsEnd;
- }
- Status = OsGetSetSystemInformation(SystemInformationIo,
- IoInformationMountPoints,
- MountPoints,
- &MountPointsSize,
- FALSE);
- //
- // Exit on any status besides a buffer too small result.
- //
- if (Status != STATUS_BUFFER_TOO_SMALL) {
- break;
- }
- //
- // If the buffer is too small. Double the expected size just in case
- // something else sneaks in and try again.
- //
- MountPointsSize *= 2;
- OsHeapFree(MountPoints);
- }
- GetMountPointsEnd:
- if (KSUCCESS(Status)) {
- *Buffer = MountPoints;
- *BufferSize = MountPointsSize;
- } else {
- OsHeapFree(MountPoints);
- }
- return Status;
- }
- OS_API
- KSTATUS
- OsGetEffectiveAccess (
- HANDLE Directory,
- PSTR Path,
- ULONG PathSize,
- ULONG DesiredFlags,
- BOOL UseRealIds,
- PULONG EffectiveAccess
- )
- /*++
- Routine Description:
- This routine determines the effective access for the given path.
- Arguments:
- Directory - Supplies an optional handle to a directory to start the
- search from if the supplied path is relative. Supply INVALID_HANDLE
- here to to use the current directory for relative paths.
- Path - Supplies a pointer to a string containing the path to the object
- to query access of.
- PathSize - Supplies the size of the supplied path buffer in bytes including
- the null terminator.
- DesiredFlags - Supplies the bitfield of flags the user is interested in.
- See EFFECTIVE_ACCESS_* definitions.
- UseRealIds - Supplies a boolean indicating whether the access check should
- use the real user and group IDs (TRUE) or the effective user and group
- IDs (FALSE).
- EffectiveAccess - Supplies a pointer where the effective access permission
- bits are returned. Only bits set in the desired flags parameter will be
- potentially set. All other bits will be set to 0. See
- EFFECTIVE_ACCESS_* definitions.
- Return Value:
- Status code indicating if the request completed successfully.
- --*/
- {
- SYSTEM_CALL_GET_EFFECTIVE_ACCESS Parameters;
- Parameters.Directory = Directory;
- Parameters.FilePath = Path;
- Parameters.FilePathSize = PathSize;
- Parameters.UseRealIds = UseRealIds;
- Parameters.DesiredFlags = DesiredFlags;
- Parameters.EffectiveAccess = 0;
- OsSystemCall(SystemCallGetEffectiveAccess, &Parameters);
- *EffectiveAccess = Parameters.EffectiveAccess;
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsLoadDriver (
- PSTR Path,
- ULONG PathSize
- )
- /*++
- Routine Description:
- This routine loads the given driver into kernel address space.
- Arguments:
- Path - Supplies a pointer to a string containing the path to the driver.
- PathSize - Supplies the size of the supplied path buffer in bytes including
- the null terminator.
- Return Value:
- Status code indicating if the request completed successfully.
- --*/
- {
- SYSTEM_CALL_LOAD_DRIVER Parameters;
- Parameters.DriverName = Path;
- Parameters.DriverNameSize = PathSize;
- OsSystemCall(SystemCallLoadDriver, &Parameters);
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsLocateDeviceInformation (
- PUUID Uuid,
- PDEVICE_ID DeviceId,
- PDEVICE_INFORMATION_RESULT Results,
- PULONG ResultCount
- )
- /*++
- Routine Description:
- This routine returns instances of devices enumerating information. Callers
- can get all devices enumerating the given information type, or all
- information types enumerated by a given device. This routine must be called
- at low level.
- Arguments:
- Uuid - Supplies an optional pointer to the information identifier to
- filter on. If NULL, any information type will match.
- DeviceId - Supplies an optional pointer to the device ID to match against.
- If NULL, then any device will match.
- Results - Supplies a pointer to a caller allocated buffer where the
- results will be returned.
- ResultCount - Supplies a pointer that upon input contains the size of the
- buffer in information result elements. On output, returns the number
- of elements in the query, even if the provided buffer was too small.
- Do note however that the number of results can change between two
- successive searches.
- Return Value:
- STATUS_SUCCESS on success.
- STATUS_BUFFER_TOO_SMALL if the provided buffer was not large enough to
- contain all the results. The result count will contain the required number
- of elements to contain the results.
- --*/
- {
- SYSTEM_CALL_LOCATE_DEVICE_INFORMATION Request;
- RtlZeroMemory(&Request, sizeof(SYSTEM_CALL_LOCATE_DEVICE_INFORMATION));
- if (Uuid != NULL) {
- Request.ByUuid = TRUE;
- RtlCopyMemory(&(Request.Uuid), Uuid, sizeof(UUID));
- }
- if (DeviceId != NULL) {
- Request.ByDeviceId = TRUE;
- Request.DeviceId = *DeviceId;
- }
- Request.Results = Results;
- Request.ResultCount = *ResultCount;
- OsSystemCall(SystemCallLocateDeviceInformation, &Request);
- *ResultCount = Request.ResultCount;
- return Request.Status;
- }
- OS_API
- KSTATUS
- OsGetSetDeviceInformation (
- DEVICE_ID DeviceId,
- PUUID Uuid,
- PVOID Data,
- PUINTN DataSize,
- BOOL Set
- )
- /*++
- Routine Description:
- This routine gets or sets device information.
- Arguments:
- DeviceId - Supplies the device ID of the device to get or set information
- for.
- Uuid - Supplies a pointer to the identifier of the device information type
- to get or set.
- Data - Supplies a pointer to the data buffer that either contains the
- information to set or will contain the information to get on success.
- DataSize - Supplies a pointer that on input contains the size of the
- data buffer. On output contains the actual size of the data.
- Set - Supplies a boolean indicating whether to get information (FALSE) or
- set information (TRUE).
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_GET_SET_DEVICE_INFORMATION Request;
- RtlCopyMemory(&(Request.Uuid), Uuid, sizeof(UUID));
- Request.DeviceId = DeviceId;
- Request.Data = Data;
- Request.DataSize = *DataSize;
- Request.Set = Set;
- OsSystemCall(SystemCallGetSetDeviceInformation, &Request);
- *DataSize = Request.DataSize;
- return Request.Status;
- }
- OS_API
- KSTATUS
- OsGetSetSystemInformation (
- SYSTEM_INFORMATION_SUBSYSTEM Subsystem,
- UINTN InformationType,
- PVOID Data,
- PUINTN DataSize,
- BOOL Set
- )
- /*++
- Routine Description:
- This routine gets or sets system information.
- Arguments:
- Subsystem - Supplies the subsystem to query or set information of.
- InformationType - Supplies the information type, which is specific to
- the subsystem. The type of this value is generally
- <subsystem>_INFORMATION_TYPE (eg IO_INFORMATION_TYPE).
- Data - Supplies a pointer to the data buffer where the data is either
- returned for a get operation or given for a set operation.
- DataSize - Supplies a pointer that on input contains the size of the
- data buffer. On output, contains the required size of the data buffer.
- Set - Supplies a boolean indicating if this is a get operation (FALSE) or
- a set operation (TRUE).
- Return Value:
- STATUS_SUCCESS if the information was successfully queried or set.
- STATUS_BUFFER_TOO_SMALL if the buffer size specified was too small. The
- required buffer size will be returned in the data size parameter.
- STATUS_DATA_LENGTH_MISMATCH if the buffer size was not correct. The
- correct buffer size will be returned in the data size parameter.
- STATUS_INVALID_PARAMETER if the given subsystem or information type is
- not known.
- Other status codes on other failures.
- --*/
- {
- SYSTEM_CALL_GET_SET_SYSTEM_INFORMATION Request;
- Request.Subsystem = Subsystem;
- Request.InformationType = InformationType;
- Request.Data = Data;
- Request.DataSize = *DataSize;
- Request.Set = Set;
- OsSystemCall(SystemCallGetSetSystemInformation, &Request);
- *DataSize = Request.DataSize;
- return Request.Status;
- }
- OS_API
- KSTATUS
- OsResetSystem (
- SYSTEM_RESET_TYPE ResetType
- )
- /*++
- Routine Description:
- This routine attempts to reboot the system.
- Arguments:
- ResetType - Supplies the desired system reset type. If the given type is
- not supported and a cold reset is, then a cold reset will be
- performed.
- Return Value:
- STATUS_SUCCESS if the reset request was successfully queued. The process
- should expect to receive a termination signal shortly, followed by a
- kill signal shortly after that.
- STATUS_INVALID_PARAMETER if the given reset type is not valid.
- STATUS_INSUFFICIENT_RESOURCES if an allocation failure in the kernel
- prevented queuing of the reset system work item.
- --*/
- {
- SYSTEM_CALL_RESET_SYSTEM Request;
- Request.ResetType = ResetType;
- OsSystemCall(SystemCallResetSystem, &Request);
- return Request.Status;
- }
- OS_API
- PVOID
- OsSetProgramBreak (
- PVOID NewBreak
- )
- /*++
- Routine Description:
- This routine gets or sets the application program break for the process.
- Arguments:
- NewBreak - Supplies an optional pointer to the new break to set. If this
- is less than the original break, then no change is made. Set to NULL
- to simply get the current program break.
- Return Value:
- Returns the current program break, which is either the new value set or
- the previous value.
- --*/
- {
- SYSTEM_CALL_SET_BREAK Request;
- Request.Break = NewBreak;
- OsSystemCall(SystemCallSetBreak, &Request);
- return Request.Break;
- }
- OS_API
- KSTATUS
- OsMemoryMap (
- HANDLE Handle,
- IO_OFFSET Offset,
- UINTN Size,
- ULONG Flags,
- PVOID *Address
- )
- /*++
- Routine Description:
- This routine maps the specified object starting at the given offset for the
- requested size, in bytes. A suggested address can optionally be supplied.
- Arguments:
- Handle - Supplies a pointer to an opened I/O handle.
- Offset - Supplies the offset into the I/O object where the mapping should
- begin.
- Size - Supplies the size of the mapping region, in bytes.
- Flags - Supplies a bitfield of flags. See SYS_MAP_FLAG_* for definitions.
- Address - Supplies a pointer that receives the address of the mapped
- region. The caller can optionally specify a suggested address using
- this paramter.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_MAP_UNMAP_MEMORY Parameters;
- Parameters.Map = TRUE;
- Parameters.Flags = Flags;
- Parameters.Handle = Handle;
- Parameters.Address = *Address;
- Parameters.Offset = Offset;
- Parameters.Size = Size;
- OsSystemCall(SystemCallMapOrUnmapMemory, &Parameters);
- if (KSUCCESS(Parameters.Status)) {
- *Address = Parameters.Address;
- }
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsMemoryUnmap (
- PVOID Address,
- UINTN Size
- )
- /*++
- Routine Description:
- This routine unmaps the specified region from the current process' address
- space.
- Arguments:
- Address - Supplies the starting address of the memory region to unmap.
- Size - Supplies the size of the region to unmap, in bytes.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_MAP_UNMAP_MEMORY Parameters;
- Parameters.Map = FALSE;
- Parameters.Address = Address;
- Parameters.Size = Size;
- OsSystemCall(SystemCallMapOrUnmapMemory, &Parameters);
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsSetMemoryProtection (
- PVOID Address,
- UINTN Size,
- ULONG NewAttributes
- )
- /*++
- Routine Description:
- This routine set the memory protection attributes for the given region.
- Arguments:
- Address - Supplies the starting address (inclusive) to change the memory
- protection for. This must be aligned to a page boundary.
- Size - Supplies the length, in bytes, of the region to change attributes
- for.
- NewAttributes - Supplies the new attributes to set. See SYS_MAP_FLAG_*
- definitions.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_SET_MEMORY_PROTECTION Parameters;
- Parameters.Address = Address;
- Parameters.Size = Size;
- Parameters.NewAttributes = NewAttributes;
- OsSystemCall(SystemCallSetMemoryProtection, &Parameters);
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsMemoryFlush (
- PVOID Address,
- ULONGLONG Size,
- ULONG Flags
- )
- /*++
- Routine Description:
- This routine flushes a region of the current process' mapped memory to
- permament storage, if the region has a backing image.
- Arguments:
- Address - Supplies the starting address of the memory region to synchronize.
- Size - Supplies the size of the region to synchronize, in bytes.
- Flags - Supplies a bitfield of flags. See SYS_MAP_SYNC_FLAG_* for
- definitions.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_FLUSH_MEMORY Parameters;
- Parameters.Address = Address;
- Parameters.Size = Size;
- Parameters.Flags = Flags;
- OsSystemCall(SystemCallFlushMemory, &Parameters);
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsSetThreadIdentity (
- ULONG FieldsToSet,
- PTHREAD_IDENTITY Identity
- )
- /*++
- Routine Description:
- This routine gets or sets a thread's identity.
- Arguments:
- FieldsToSet - Supplies a bitfield indicating which identity fields to set.
- Supply zero to simply get the current thread identity. See
- THREAD_IDENTITY_FIELD_* definitions.
- Identity - Supplies a pointer that on input contains the thread identity
- fields to set. On successful output, will contain the complete new
- thread identity.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_SET_THREAD_IDENTITY Parameters;
- Parameters.Request.FieldsToSet = FieldsToSet;
- if (FieldsToSet != 0) {
- RtlCopyMemory(&(Parameters.Request.Identity),
- Identity,
- sizeof(THREAD_IDENTITY));
- }
- OsSystemCall(SystemCallSetThreadIdentity, &Parameters);
- if (KSUCCESS(Parameters.Status)) {
- RtlCopyMemory(Identity,
- &(Parameters.Request.Identity),
- sizeof(THREAD_IDENTITY));
- }
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsSetThreadPermissions (
- ULONG FieldsToSet,
- PTHREAD_PERMISSIONS Permissions
- )
- /*++
- Routine Description:
- This routine gets or sets a thread's permission masks.
- Arguments:
- FieldsToSet - Supplies a bitfield indicating which permission sets or
- fields to modify. Supply zero to simply get the current thread
- permission sets. See THREAD_PERMISSION_FIELD_* definitions.
- Permissions - Supplies a pointer that on input contains the thread
- permission masks to set. On successful output, will contain the
- complete new set of permission masks.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_SET_THREAD_PERMISSIONS Parameters;
- Parameters.Request.FieldsToSet = FieldsToSet;
- if (FieldsToSet != 0) {
- RtlCopyMemory(&(Parameters.Request.Permissions),
- Permissions,
- sizeof(THREAD_PERMISSIONS));
- }
- OsSystemCall(SystemCallSetThreadPermissions, &Parameters);
- if (KSUCCESS(Parameters.Status)) {
- RtlCopyMemory(Permissions,
- &(Parameters.Request.Permissions),
- sizeof(THREAD_PERMISSIONS));
- }
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsSetSupplementaryGroups (
- BOOL Set,
- PGROUP_ID Groups,
- PUINTN Count
- )
- /*++
- Routine Description:
- This routine gets or sets a thread's set of supplementary groups. To set
- the supplementary groups, the thread must have the set group ID permission.
- Arguments:
- Set - Supplies a boolean indicating whether to set the new groups (TRUE) or
- just get the current list of supplementary groups.
- Groups - Supplies a pointer that recieves the supplementary groups for a
- get operation or contains the new group IDs to set for a set operation.
- Count - Supplies a pointer that on input contains the number of elements
- in the given buffer. On output, contains the number of valid elements.
- If STATUS_BUFFER_TOO_SMALL is returned, the number of elements needed
- will be retunred.
- Return Value:
- STATUS_SUCCESS on success.
- STATUS_ACCESS_VIOLATION if the given groups buffer is invalid.
- STATUS_PERMISSION_DENIED if the caller does not have the set group ID
- permission.
- STATUS_INSUFFICIENT_RESOURCES if an internal kernel allocation failed.
- STATUS_INVALID_PARAMETER if the count was too big.
- STATUS_BUFFER_TOO_SMALL if the given buffer was not big enough to contain
- all the current supplementary groups. In this case, count contains the
- number of elements needed.
- --*/
- {
- SYSTEM_CALL_SET_SUPPLEMENTARY_GROUPS Parameters;
- Parameters.Set = Set;
- Parameters.Groups = Groups;
- Parameters.Count = *Count;
- OsSystemCall(SystemCallSetSupplementaryGroups, &Parameters);
- *Count = Parameters.Count;
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsSetResourceLimit (
- RESOURCE_LIMIT_TYPE Type,
- PRESOURCE_LIMIT NewValue,
- PRESOURCE_LIMIT OldValue
- )
- /*++
- Routine Description:
- This routine gets or sets the current resource limit value for a given type.
- Arguments:
- Type - Supplies the resource limit type to get the limit for.
- NewValue - Supplies an optional pointer to the new limit to set. If this is
- NULL, then a new value is not set.
- OldValue - Supplies an optional pointer where the previous limit will be
- returned.
- Return Value:
- STATUS_SUCCESS on success.
- STATUS_INVALID_PARAMETER if the resource type is not valid or the current
- value was greater than the max.
- STATUS_PERMISSION_DENIED if the caller is trying to raise the max/hard
- limit and does not have the resources permission.
- --*/
- {
- SYSTEM_CALL_SET_RESOURCE_LIMIT Parameters;
- Parameters.Type = Type;
- if (NewValue != NULL) {
- Parameters.Set = TRUE;
- Parameters.Value.Current = NewValue->Current;
- Parameters.Value.Max = NewValue->Max;
- } else {
- Parameters.Set = FALSE;
- }
- OsSystemCall(SystemCallSetResourceLimit, &Parameters);
- if (OldValue != NULL) {
- OldValue->Current = Parameters.Value.Current;
- OldValue->Max = Parameters.Value.Max;
- }
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsCreateTerminal (
- HANDLE MasterDirectory,
- HANDLE SlaveDirectory,
- PSTR MasterPath,
- UINTN MasterPathLength,
- PSTR SlavePath,
- UINTN SlavePathLength,
- ULONG MasterOpenFlags,
- FILE_PERMISSIONS MasterCreatePermissions,
- FILE_PERMISSIONS SlaveCreatePermissions,
- PHANDLE MasterHandle
- )
- /*++
- Routine Description:
- This routine creates a new pseudo-terminal master and slave at the given
- paths.
- Arguments:
- MasterDirectory - Supplies an optional handle to a directory for relative
- paths when creating the master. Supply INVALID_HANDLE to use the
- current working directory.
- SlaveDirectory - Supplies an optional handle to a directory for relative
- paths when creating the slave. Supply INVALID_HANDLE to use the
- current working directory.
- MasterPath - Supplies an optional pointer to the path to create for the
- master.
- MasterPathLength - Supplies the length of the master side path buffer in
- bytes, including the null terminator.
- SlavePath - Supplies an optional pointer to the path to create for the
- master.
- SlavePathLength - Supplies the length of the slave side path buffer in
- bytes, including the null terminator.
- MasterOpenFlags - Supplies the open flags to use when opening the master.
- Only read, write, and "no controlling terminal" flags are honored.
- MasterCreatePermissions - Supplies the permissions to apply to the created
- master side.
- SlaveCreatePermissions - Supplies the permission to apply to the created
- slave side.
- MasterHandle - Supplies a pointer where the opened handle to the master
- will be returned on success.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_CREATE_TERMINAL Parameters;
- Parameters.MasterDirectory = MasterDirectory;
- Parameters.SlaveDirectory = SlaveDirectory;
- Parameters.MasterPath = MasterPath;
- Parameters.MasterPathLength = MasterPathLength;
- Parameters.SlavePath = SlavePath;
- Parameters.SlavePathLength = SlavePathLength;
- Parameters.MasterOpenFlags = MasterOpenFlags;
- Parameters.MasterCreatePermissions = MasterCreatePermissions;
- Parameters.SlaveCreatePermissions = SlaveCreatePermissions;
- OsSystemCall(SystemCallCreateTerminal, &Parameters);
- *MasterHandle = INVALID_HANDLE;
- if (KSUCCESS(Parameters.Status)) {
- *MasterHandle = Parameters.MasterHandle;
- }
- return Parameters.Status;
- }
- OS_API
- KSTATUS
- OsGetFilePath (
- HANDLE Handle,
- PSTR Path,
- PUINTN PathSize
- )
- /*++
- Routine Description:
- This routine returns the file path for the given handle, if possible.
- Arguments:
- Handle - Supplies the open handle to get the file path of.
- Path - Supplies a pointer where the path will be returned on success.
- PathSize - Supplies a pointer that on input contains the size of the path
- buffer. On output, returns the needed size of the path buffer, even
- if the supplied buffer is too small.
- Return Value:
- STATUS_SUCCESS on success.
- STATUS_INVALID_HANDLE if the given handle is not valid.
- STATUS_PATH_NOT_FOUND if no path exists for the given handle.
- STATUS_BUFFER_TOO_SMALL if the supplied path buffer was not large enough to
- contain the complete path. In this case the path size returned is the size
- needed.
- STATUS_ACCESS_VIOLATION if the path buffer was invalid.
- --*/
- {
- FILE_CONTROL_PARAMETERS_UNION Parameters;
- KSTATUS Status;
- Parameters.FilePath.Path = Path;
- Parameters.FilePath.PathSize = *PathSize;
- Status = OsFileControl(Handle, FileControlCommandGetPath, &Parameters);
- *PathSize = Parameters.FilePath.PathSize;
- return Status;
- }
- OS_API
- VOID
- OsSetThreadIdPointer (
- PTHREAD_ID Pointer
- )
- /*++
- Routine Description:
- This routine sets the thread ID pointer in the kernel. If this value is
- non-null when the thread exits, then zero will be written to this address,
- and a UserLockWake operation will be performed to wake up one thread.
- Arguments:
- Pointer - Supplies the new user mode pointer to the thread ID that will be
- cleared when the thread exits. Supply NULL to clear the thread ID
- pointer in the kernel. If this is non-null, the kernel will write the
- thread ID into this region.
- Return Value:
- None.
- --*/
- {
- SYSTEM_CALL_SET_THREAD_ID_POINTER Parameters;
- Parameters.Pointer = Pointer;
- OsSystemCall(SystemCallSetThreadIdPointer, &Parameters);
- return;
- }
- OS_API
- FILE_PERMISSIONS
- OsSetUmask (
- FILE_PERMISSIONS NewMask
- )
- /*++
- Routine Description:
- This routine sets file permission mask for the current process. Bits set
- in this mask will be automatically cleared out of the permissions of any
- file or directory created.
- Arguments:
- NewMask - Supplies the new mask to set.
- Return Value:
- Returns the previously set mask.
- --*/
- {
- SYSTEM_CALL_SET_UMASK Parameters;
- Parameters.Mask = NewMask;
- OsSystemCall(SystemCallSetUmask, &Parameters);
- return Parameters.Mask;
- }
- OS_API
- KSTATUS
- OsDuplicateHandle (
- HANDLE ExistingHandle,
- PHANDLE NewHandle,
- ULONG Flags
- )
- /*++
- Routine Description:
- This routine duplicates a given handle at a new handle.
- Arguments:
- ExistingHandle - Supplies the handle to duplicate.
- NewHandle - Supplies a pointer that contains the destination handle value
- for the new handle. If this is INVALID_HANDLE, then the duplicated
- handle will be the lowest available handle value, and will be returned
- here. If this is not INVALID_HANDLE, then the previous handle at that
- location will be closed. If the new handle equals the existing handle,
- failure is returned.
- Flags - Supplies open flags to be set on the new handle. Only
- SYS_OPEN_FLAG_CLOSE_ON_EXECUTE is permitted. If not set, the new handle
- will have the close on execute flag cleared.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_DUPLICATE_HANDLE Parameters;
- Parameters.OldHandle = ExistingHandle;
- Parameters.NewHandle = *NewHandle;
- Parameters.OpenFlags = Flags;
- OsSystemCall(SystemCallDuplicateHandle, &Parameters);
- *NewHandle = Parameters.NewHandle;
- return Parameters.Status;
- }
- VOID
- OspProcessSignal (
- PSIGNAL_PARAMETERS Parameters,
- PSIGNAL_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine processes a signal sent via the kernel.
- Arguments:
- Parameters - Supplies a pointer to the signal parameters from the kernel.
- Context - Supplies a pointer to the signal context from the kernel.
- Return Value:
- None.
- --*/
- {
- BOOL RestartAllowed;
- PSIGNAL_HANDLER_ROUTINE SignalHandler;
- RestartAllowed = FALSE;
- SignalHandler = OsSignalHandler;
- if (SignalHandler != NULL) {
- RestartAllowed = SignalHandler(Parameters);
- }
- //
- // Clear the restart flag if it's set but the handler does not allow
- // restarts.
- //
- if (((Context->Flags & SIGNAL_CONTEXT_FLAG_RESTART) != 0) &&
- (RestartAllowed == FALSE)) {
- Context->Flags &= ~SIGNAL_CONTEXT_FLAG_RESTART;
- }
- return;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
- PVOID
- OspSetSignalHandler (
- PVOID SignalHandlerRoutine
- )
- /*++
- Routine Description:
- This routine sets the signal handler routine for the given thread.
- Arguments:
- SignalHandlerRoutine - Supplies a pointer to the signal handler routine to
- invoke when signals occur. The two parameters to the routine will be
- the signal number and the optional parameter, both of which will be
- passed via registers.
- Return Value:
- Returns a pointer to the original signal handler registered, or NULL if no
- signal handler was previously registered.
- --*/
- {
- SYSTEM_CALL_SET_SIGNAL_HANDLER Parameters;
- Parameters.SignalHandler = SignalHandlerRoutine;
- OsSystemCall(SystemCallSetSignalHandler, &Parameters);
- return Parameters.SignalHandler;
- }
- KSTATUS
- OspGetSetFileInformation (
- HANDLE Directory,
- PSTR Path,
- ULONG PathSize,
- BOOL FollowLink,
- PSET_FILE_INFORMATION Request
- )
- /*++
- Routine Description:
- This routine gets or sets the file properties for a given file.
- Arguments:
- Directory - Supplies an optional handle to a directory to start the
- search from if the supplied path is relative. Supply INVALID_HANDLE
- here to to use the current directory for relative paths (usually the
- expected default).
- Path - Supplies a pointer to the string containing the file path to get or
- set properties for.
- PathSize - Supplies the size of the path string in bytes including the
- null terminator.
- FollowLink - Supplies a boolean indicating what to do if the file path
- points to a symbolic link. If set to TRUE, the file information set or
- returned will be for the file the link points to. If FALSE, the call
- will set or get information for the link itself.
- Request - Supplies a pointer to the get or set file properties request.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_GET_SET_FILE_INFORMATION Parameters;
- Parameters.Request.FieldsToSet = 0;
- if (Request->FieldsToSet != 0) {
- RtlCopyMemory(&(Parameters.Request),
- Request,
- sizeof(SET_FILE_INFORMATION));
- }
- Parameters.Directory = Directory;
- Parameters.FilePath = Path;
- Parameters.FilePathSize = PathSize;
- Parameters.FollowLink = FollowLink;
- OsSystemCall(SystemCallGetSetFileInformation, &Parameters);
- if (Request->FieldsToSet == 0) {
- RtlCopyMemory(Request,
- &(Parameters.Request),
- sizeof(SET_FILE_INFORMATION));
- }
- return Parameters.Status;
- }
- KSTATUS
- OspMountOrUnmount (
- PSTR MountPointPath,
- ULONG MountPointPathSize,
- PSTR TargetPath,
- ULONG TargetPathSize,
- ULONG Flags
- )
- /*++
- Routine Description:
- This routine mounts the target at the given mount point or unmounts the
- mount point.
- Arguments:
- MountPointPath - Supplies a pointer to a string containing the path to the
- mount point where the target is to be mounted.
- MountPointPathSize - Supplies the size of the mount point path string in
- bytes, including the null terminator.
- TargetPath - Supplies a pointer to a string containing the path to the
- target file, directory, volume, or device that is to be mounted.
- TargetPathSize - Supplies the size of the target path string in bytes,
- including the null terminator.
- Flags - Supplies the flags associated with the mount operation. See
- SYS_MOUNT_FLAG_* for definitions.
- Return Value:
- Status code.
- --*/
- {
- SYSTEM_CALL_MOUNT_UNMOUNT Parameters;
- //
- // Make the system call with the mount/unmount information.
- //
- Parameters.MountPointPath = MountPointPath;
- Parameters.MountPointPathSize = MountPointPathSize;
- Parameters.TargetPath = TargetPath;
- Parameters.TargetPathSize = TargetPathSize;
- Parameters.Flags = Flags;
- OsSystemCall(SystemCallMountOrUnmount, &Parameters);
- return Parameters.Status;
- }
- NO_RETURN
- VOID
- OspExitThread (
- PVOID UnmapAddress,
- UINTN UnmapSize
- )
- /*++
- Routine Description:
- This routine terminates the current thread, and optionally attempts to
- unmap a region of memory on its way out. Usually this is the stack of the
- thread that is exiting.
- Arguments:
- UnmapAddress - Supplies an optional pointer to a region of memory to unmap
- as the thread exits. Supply NULL to skip unmapping.
- UnmapSize - Supplies the size of the region to unmap in bytes. This must be
- aligned to the page size. If it is not, the unmap simply won't happen.
- Supply 0 to skip the unmap and just exit the thread.
- Return Value:
- This routine does not return.
- --*/
- {
- SYSTEM_CALL_EXIT_THREAD Parameters;
- Parameters.UnmapAddress = UnmapAddress;
- Parameters.UnmapSize = UnmapSize;
- OsSystemCall(SystemCallExitThread, &Parameters);
- while (TRUE) {
- ASSERT(FALSE);
- }
- }
|