Process.cpp 146 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Demangle.h>
  27. #include <AK/FileSystemPath.h>
  28. #include <AK/ScopeGuard.h>
  29. #include <AK/StdLibExtras.h>
  30. #include <AK/StringBuilder.h>
  31. #include <AK/Time.h>
  32. #include <AK/Types.h>
  33. #include <Kernel/Arch/i386/CPU.h>
  34. #include <Kernel/Arch/i386/PIT.h>
  35. #include <Kernel/Console.h>
  36. #include <Kernel/Devices/KeyboardDevice.h>
  37. #include <Kernel/Devices/NullDevice.h>
  38. #include <Kernel/Devices/PCSpeaker.h>
  39. #include <Kernel/Devices/RandomDevice.h>
  40. #include <Kernel/FileSystem/Custody.h>
  41. #include <Kernel/FileSystem/DevPtsFS.h>
  42. #include <Kernel/FileSystem/Ext2FileSystem.h>
  43. #include <Kernel/FileSystem/FIFO.h>
  44. #include <Kernel/FileSystem/FileDescription.h>
  45. #include <Kernel/FileSystem/InodeWatcher.h>
  46. #include <Kernel/FileSystem/ProcFS.h>
  47. #include <Kernel/FileSystem/TmpFS.h>
  48. #include <Kernel/FileSystem/VirtualFileSystem.h>
  49. #include <Kernel/Heap/kmalloc.h>
  50. #include <Kernel/IO.h>
  51. #include <Kernel/KBufferBuilder.h>
  52. #include <Kernel/KSyms.h>
  53. #include <Kernel/KernelInfoPage.h>
  54. #include <Kernel/Module.h>
  55. #include <Kernel/Multiboot.h>
  56. #include <Kernel/Net/Socket.h>
  57. #include <Kernel/Process.h>
  58. #include <Kernel/ProcessTracer.h>
  59. #include <Kernel/Profiling.h>
  60. #include <Kernel/RTC.h>
  61. #include <Kernel/Random.h>
  62. #include <Kernel/Scheduler.h>
  63. #include <Kernel/SharedBuffer.h>
  64. #include <Kernel/StdLib.h>
  65. #include <Kernel/Syscall.h>
  66. #include <Kernel/TTY/MasterPTY.h>
  67. #include <Kernel/Thread.h>
  68. #include <Kernel/VM/InodeVMObject.h>
  69. #include <Kernel/VM/PurgeableVMObject.h>
  70. #include <LibC/errno_numbers.h>
  71. #include <LibC/limits.h>
  72. #include <LibC/signal_numbers.h>
  73. #include <LibELF/ELFLoader.h>
  74. //#define DEBUG_POLL_SELECT
  75. //#define DEBUG_IO
  76. //#define TASK_DEBUG
  77. //#define FORK_DEBUG
  78. //#define EXEC_DEBUG
  79. //#define SIGNAL_DEBUG
  80. //#define SHARED_BUFFER_DEBUG
  81. static void create_signal_trampolines();
  82. static void create_kernel_info_page();
  83. static pid_t next_pid;
  84. InlineLinkedList<Process>* g_processes;
  85. static String* s_hostname;
  86. static Lock* s_hostname_lock;
  87. static VirtualAddress s_info_page_address_for_userspace;
  88. static VirtualAddress s_info_page_address_for_kernel;
  89. VirtualAddress g_return_to_ring3_from_signal_trampoline;
  90. HashMap<String, OwnPtr<Module>>* g_modules;
  91. pid_t Process::allocate_pid()
  92. {
  93. InterruptDisabler disabler;
  94. return next_pid++;
  95. }
  96. void Process::initialize()
  97. {
  98. g_modules = new HashMap<String, OwnPtr<Module>>;
  99. next_pid = 0;
  100. g_processes = new InlineLinkedList<Process>;
  101. s_hostname = new String("courage");
  102. s_hostname_lock = new Lock;
  103. create_signal_trampolines();
  104. create_kernel_info_page();
  105. }
  106. void Process::update_info_page_timestamp(const timeval& tv)
  107. {
  108. auto* info_page = (KernelInfoPage*)s_info_page_address_for_kernel.as_ptr();
  109. info_page->serial++;
  110. const_cast<timeval&>(info_page->now) = tv;
  111. }
  112. Vector<pid_t> Process::all_pids()
  113. {
  114. Vector<pid_t> pids;
  115. InterruptDisabler disabler;
  116. pids.ensure_capacity((int)g_processes->size_slow());
  117. for (auto& process : *g_processes)
  118. pids.append(process.pid());
  119. return pids;
  120. }
  121. Vector<Process*> Process::all_processes()
  122. {
  123. Vector<Process*> processes;
  124. InterruptDisabler disabler;
  125. processes.ensure_capacity((int)g_processes->size_slow());
  126. for (auto& process : *g_processes)
  127. processes.append(&process);
  128. return processes;
  129. }
  130. bool Process::in_group(gid_t gid) const
  131. {
  132. return m_gid == gid || m_extra_gids.contains(gid);
  133. }
  134. Range Process::allocate_range(VirtualAddress vaddr, size_t size)
  135. {
  136. vaddr.mask(PAGE_MASK);
  137. size = PAGE_ROUND_UP(size);
  138. if (vaddr.is_null())
  139. return page_directory().range_allocator().allocate_anywhere(size);
  140. return page_directory().range_allocator().allocate_specific(vaddr, size);
  141. }
  142. static unsigned prot_to_region_access_flags(int prot)
  143. {
  144. unsigned access = 0;
  145. if (prot & PROT_READ)
  146. access |= Region::Access::Read;
  147. if (prot & PROT_WRITE)
  148. access |= Region::Access::Write;
  149. if (prot & PROT_EXEC)
  150. access |= Region::Access::Execute;
  151. return access;
  152. }
  153. Region& Process::allocate_split_region(const Region& source_region, const Range& range, size_t offset_in_vmobject)
  154. {
  155. auto& region = add_region(Region::create_user_accessible(range, source_region.vmobject(), offset_in_vmobject, source_region.name(), source_region.access()));
  156. region.set_mmap(source_region.is_mmap());
  157. region.set_stack(source_region.is_stack());
  158. size_t page_offset_in_source_region = (offset_in_vmobject - source_region.offset_in_vmobject()) / PAGE_SIZE;
  159. for (size_t i = 0; i < region.page_count(); ++i) {
  160. if (source_region.should_cow(page_offset_in_source_region + i))
  161. region.set_should_cow(i, true);
  162. }
  163. return region;
  164. }
  165. Region* Process::allocate_region(VirtualAddress vaddr, size_t size, const String& name, int prot, bool commit)
  166. {
  167. auto range = allocate_range(vaddr, size);
  168. if (!range.is_valid())
  169. return nullptr;
  170. auto& region = add_region(Region::create_user_accessible(range, name, prot_to_region_access_flags(prot)));
  171. region.map(page_directory());
  172. if (commit)
  173. region.commit();
  174. return &region;
  175. }
  176. Region* Process::allocate_file_backed_region(VirtualAddress vaddr, size_t size, NonnullRefPtr<Inode> inode, const String& name, int prot)
  177. {
  178. auto range = allocate_range(vaddr, size);
  179. if (!range.is_valid())
  180. return nullptr;
  181. auto& region = add_region(Region::create_user_accessible(range, inode, name, prot_to_region_access_flags(prot)));
  182. region.map(page_directory());
  183. return &region;
  184. }
  185. Region* Process::allocate_region_with_vmobject(VirtualAddress vaddr, size_t size, NonnullRefPtr<VMObject> vmobject, size_t offset_in_vmobject, const String& name, int prot, bool user_accessible)
  186. {
  187. size_t end_in_vmobject = offset_in_vmobject + size;
  188. if (end_in_vmobject < offset_in_vmobject) {
  189. dbgprintf("allocate_region_with_vmobject: Overflow (offset + size)\n");
  190. return nullptr;
  191. }
  192. if (offset_in_vmobject >= vmobject->size()) {
  193. dbgprintf("allocate_region_with_vmobject: Attempt to allocate a region with an offset past the end of its VMObject.\n");
  194. return nullptr;
  195. }
  196. if (end_in_vmobject > vmobject->size()) {
  197. dbgprintf("allocate_region_with_vmobject: Attempt to allocate a region with an end past the end of its VMObject.\n");
  198. return nullptr;
  199. }
  200. auto range = allocate_range(vaddr, size);
  201. if (!range.is_valid())
  202. return nullptr;
  203. offset_in_vmobject &= PAGE_MASK;
  204. Region* region;
  205. if (user_accessible)
  206. region = &add_region(Region::create_user_accessible(range, move(vmobject), offset_in_vmobject, name, prot_to_region_access_flags(prot)));
  207. else
  208. region = &add_region(Region::create_kernel_only(range, move(vmobject), offset_in_vmobject, name, prot_to_region_access_flags(prot)));
  209. region->map(page_directory());
  210. return region;
  211. }
  212. bool Process::deallocate_region(Region& region)
  213. {
  214. InterruptDisabler disabler;
  215. if (m_region_lookup_cache.region == &region)
  216. m_region_lookup_cache.region = nullptr;
  217. for (int i = 0; i < m_regions.size(); ++i) {
  218. if (&m_regions[i] == &region) {
  219. m_regions.remove(i);
  220. return true;
  221. }
  222. }
  223. return false;
  224. }
  225. Region* Process::region_from_range(const Range& range)
  226. {
  227. if (m_region_lookup_cache.range == range && m_region_lookup_cache.region)
  228. return m_region_lookup_cache.region;
  229. size_t size = PAGE_ROUND_UP(range.size());
  230. for (auto& region : m_regions) {
  231. if (region.vaddr() == range.base() && region.size() == size) {
  232. m_region_lookup_cache.range = range;
  233. m_region_lookup_cache.region = &region;
  234. return &region;
  235. }
  236. }
  237. return nullptr;
  238. }
  239. Region* Process::region_containing(const Range& range)
  240. {
  241. for (auto& region : m_regions) {
  242. if (region.contains(range))
  243. return &region;
  244. }
  245. return nullptr;
  246. }
  247. int Process::sys$set_mmap_name(const Syscall::SC_set_mmap_name_params* user_params)
  248. {
  249. REQUIRE_PROMISE(stdio);
  250. Syscall::SC_set_mmap_name_params params;
  251. if (!validate_read_and_copy_typed(&params, user_params))
  252. return -EFAULT;
  253. if (params.name.length > PATH_MAX)
  254. return -ENAMETOOLONG;
  255. auto name = validate_and_copy_string_from_user(params.name);
  256. if (name.is_null())
  257. return -EFAULT;
  258. auto* region = region_from_range({ VirtualAddress(params.addr), params.size });
  259. if (!region)
  260. return -EINVAL;
  261. if (!region->is_mmap())
  262. return -EPERM;
  263. region->set_name(name);
  264. return 0;
  265. }
  266. static bool validate_mmap_prot(int prot, bool map_stack)
  267. {
  268. bool readable = prot & PROT_READ;
  269. bool writable = prot & PROT_WRITE;
  270. bool executable = prot & PROT_EXEC;
  271. if (writable && executable)
  272. return false;
  273. if (map_stack) {
  274. if (executable)
  275. return false;
  276. if (!readable || !writable)
  277. return false;
  278. }
  279. return true;
  280. }
  281. static bool validate_inode_mmap_prot(const Process& process, int prot, const Inode& inode)
  282. {
  283. auto metadata = inode.metadata();
  284. if ((prot & PROT_WRITE) && !metadata.may_write(process))
  285. return false;
  286. if ((prot & PROT_READ) && !metadata.may_read(process))
  287. return false;
  288. InterruptDisabler disabler;
  289. if (inode.vmobject()) {
  290. if ((prot & PROT_EXEC) && inode.vmobject()->writable_mappings())
  291. return false;
  292. if ((prot & PROT_WRITE) && inode.vmobject()->executable_mappings())
  293. return false;
  294. }
  295. return true;
  296. }
  297. // Carve out a virtual address range from a region and return the two regions on either side
  298. Vector<Region*, 2> Process::split_region_around_range(const Region& source_region, const Range& desired_range)
  299. {
  300. Range old_region_range = source_region.range();
  301. auto remaining_ranges_after_unmap = old_region_range.carve(desired_range);
  302. ASSERT(!remaining_ranges_after_unmap.is_empty());
  303. auto make_replacement_region = [&](const Range& new_range) -> Region& {
  304. ASSERT(new_range.base() >= old_region_range.base());
  305. ASSERT(new_range.end() <= old_region_range.end());
  306. size_t new_range_offset_in_vmobject = source_region.offset_in_vmobject() + (new_range.base().get() - old_region_range.base().get());
  307. return allocate_split_region(source_region, new_range, new_range_offset_in_vmobject);
  308. };
  309. Vector<Region*, 2> new_regions;
  310. for (auto& new_range : remaining_ranges_after_unmap) {
  311. new_regions.unchecked_append(&make_replacement_region(new_range));
  312. }
  313. return new_regions;
  314. }
  315. void* Process::sys$mmap(const Syscall::SC_mmap_params* user_params)
  316. {
  317. REQUIRE_PROMISE(stdio);
  318. Syscall::SC_mmap_params params;
  319. if (!validate_read_and_copy_typed(&params, user_params))
  320. return (void*)-EFAULT;
  321. void* addr = (void*)params.addr;
  322. size_t size = params.size;
  323. int prot = params.prot;
  324. int flags = params.flags;
  325. int fd = params.fd;
  326. int offset = params.offset;
  327. if (!is_user_range(VirtualAddress(addr), size))
  328. return (void*)-EFAULT;
  329. String name;
  330. if (params.name.characters) {
  331. if (params.name.length > PATH_MAX)
  332. return (void*)-ENAMETOOLONG;
  333. name = validate_and_copy_string_from_user(params.name);
  334. if (name.is_null())
  335. return (void*)-EFAULT;
  336. }
  337. if (size == 0)
  338. return (void*)-EINVAL;
  339. if ((uintptr_t)addr & ~PAGE_MASK)
  340. return (void*)-EINVAL;
  341. bool map_shared = flags & MAP_SHARED;
  342. bool map_anonymous = flags & MAP_ANONYMOUS;
  343. bool map_purgeable = flags & MAP_PURGEABLE;
  344. bool map_private = flags & MAP_PRIVATE;
  345. bool map_stack = flags & MAP_STACK;
  346. bool map_fixed = flags & MAP_FIXED;
  347. if (map_shared && map_private)
  348. return (void*)-EINVAL;
  349. if (!map_shared && !map_private)
  350. return (void*)-EINVAL;
  351. if (!validate_mmap_prot(prot, map_stack))
  352. return (void*)-EINVAL;
  353. if (map_stack && (!map_private || !map_anonymous))
  354. return (void*)-EINVAL;
  355. Region* region = nullptr;
  356. if (map_purgeable) {
  357. auto vmobject = PurgeableVMObject::create_with_size(size);
  358. region = allocate_region_with_vmobject(VirtualAddress(addr), size, vmobject, 0, !name.is_null() ? name : "mmap (purgeable)", prot);
  359. if (!region && (!map_fixed && addr != 0))
  360. region = allocate_region_with_vmobject({}, size, vmobject, 0, !name.is_null() ? name : "mmap (purgeable)", prot);
  361. } else if (map_anonymous) {
  362. region = allocate_region(VirtualAddress(addr), size, !name.is_null() ? name : "mmap", prot, false);
  363. if (!region && (!map_fixed && addr != 0))
  364. region = allocate_region({}, size, !name.is_null() ? name : "mmap", prot, false);
  365. } else {
  366. if (offset < 0)
  367. return (void*)-EINVAL;
  368. if (static_cast<size_t>(offset) & ~PAGE_MASK)
  369. return (void*)-EINVAL;
  370. // FIXME: Implement MAP_PRIVATE for FileDescription-backed mmap
  371. if (map_private)
  372. return (void*)-ENOTSUP;
  373. auto description = file_description(fd);
  374. if (!description)
  375. return (void*)-EBADF;
  376. if (description->is_directory())
  377. return (void*)-ENODEV;
  378. if ((prot & PROT_READ) && !description->is_readable())
  379. return (void*)-EACCES;
  380. if ((prot & PROT_WRITE) && !description->is_writable())
  381. return (void*)-EACCES;
  382. if (description->inode()) {
  383. if (!validate_inode_mmap_prot(*this, prot, *description->inode()))
  384. return (void*)-EACCES;
  385. }
  386. auto region_or_error = description->mmap(*this, VirtualAddress(addr), static_cast<size_t>(offset), size, prot);
  387. if (region_or_error.is_error()) {
  388. // Fail if MAP_FIXED or address is 0, retry otherwise
  389. if (map_fixed || addr == 0)
  390. return (void*)(int)region_or_error.error();
  391. region_or_error = description->mmap(*this, {}, static_cast<size_t>(offset), size, prot);
  392. }
  393. if (region_or_error.is_error())
  394. return (void*)(int)region_or_error.error();
  395. region = region_or_error.value();
  396. }
  397. if (!region)
  398. return (void*)-ENOMEM;
  399. region->set_mmap(true);
  400. if (map_shared)
  401. region->set_shared(true);
  402. if (map_stack)
  403. region->set_stack(true);
  404. if (!name.is_null())
  405. region->set_name(name);
  406. return region->vaddr().as_ptr();
  407. }
  408. int Process::sys$munmap(void* addr, size_t size)
  409. {
  410. REQUIRE_PROMISE(stdio);
  411. if (!is_user_range(VirtualAddress(addr), size))
  412. return -EFAULT;
  413. Range range_to_unmap { VirtualAddress(addr), size };
  414. if (auto* whole_region = region_from_range(range_to_unmap)) {
  415. if (!whole_region->is_mmap())
  416. return -EPERM;
  417. bool success = deallocate_region(*whole_region);
  418. ASSERT(success);
  419. return 0;
  420. }
  421. if (auto* old_region = region_containing(range_to_unmap)) {
  422. if (!old_region->is_mmap())
  423. return -EPERM;
  424. auto new_regions = split_region_around_range(*old_region, range_to_unmap);
  425. // We manually unmap the old region here, specifying that we *don't* want the VM deallocated.
  426. old_region->unmap(Region::ShouldDeallocateVirtualMemoryRange::No);
  427. deallocate_region(*old_region);
  428. // Instead we give back the unwanted VM manually.
  429. page_directory().range_allocator().deallocate(range_to_unmap);
  430. // And finally we map the new region(s) using our page directory (they were just allocated and don't have one).
  431. for (auto* new_region : new_regions) {
  432. new_region->map(page_directory());
  433. }
  434. return 0;
  435. }
  436. // FIXME: We should also support munmap() across multiple regions. (#175)
  437. return -EINVAL;
  438. }
  439. int Process::sys$mprotect(void* addr, size_t size, int prot)
  440. {
  441. REQUIRE_PROMISE(stdio);
  442. if (!is_user_range(VirtualAddress(addr), size))
  443. return -EFAULT;
  444. Range range_to_mprotect = { VirtualAddress(addr), size };
  445. if (auto* whole_region = region_from_range(range_to_mprotect)) {
  446. if (!whole_region->is_mmap())
  447. return -EPERM;
  448. if (!validate_mmap_prot(prot, whole_region->is_stack()))
  449. return -EINVAL;
  450. if (whole_region->access() == prot_to_region_access_flags(prot))
  451. return 0;
  452. if (whole_region->vmobject().is_inode()
  453. && !validate_inode_mmap_prot(*this, prot, static_cast<const InodeVMObject&>(whole_region->vmobject()).inode())) {
  454. return -EACCES;
  455. }
  456. whole_region->set_readable(prot & PROT_READ);
  457. whole_region->set_writable(prot & PROT_WRITE);
  458. whole_region->set_executable(prot & PROT_EXEC);
  459. whole_region->remap();
  460. return 0;
  461. }
  462. // Check if we can carve out the desired range from an existing region
  463. if (auto* old_region = region_containing(range_to_mprotect)) {
  464. if (!old_region->is_mmap())
  465. return -EPERM;
  466. if (!validate_mmap_prot(prot, old_region->is_stack()))
  467. return -EINVAL;
  468. if (old_region->access() == prot_to_region_access_flags(prot))
  469. return 0;
  470. if (old_region->vmobject().is_inode()
  471. && !validate_inode_mmap_prot(*this, prot, static_cast<const InodeVMObject&>(old_region->vmobject()).inode())) {
  472. return -EACCES;
  473. }
  474. // This vector is the region(s) adjacent to our range.
  475. // We need to allocate a new region for the range we wanted to change permission bits on.
  476. auto adjacent_regions = split_region_around_range(*old_region, range_to_mprotect);
  477. size_t new_range_offset_in_vmobject = old_region->offset_in_vmobject() + (range_to_mprotect.base().get() - old_region->range().base().get());
  478. auto& new_region = allocate_split_region(*old_region, range_to_mprotect, new_range_offset_in_vmobject);
  479. new_region.set_readable(prot & PROT_READ);
  480. new_region.set_writable(prot & PROT_WRITE);
  481. new_region.set_executable(prot & PROT_EXEC);
  482. // Unmap the old region here, specifying that we *don't* want the VM deallocated.
  483. old_region->unmap(Region::ShouldDeallocateVirtualMemoryRange::No);
  484. deallocate_region(*old_region);
  485. // Map the new regions using our page directory (they were just allocated and don't have one).
  486. for (auto* adjacent_region : adjacent_regions) {
  487. adjacent_region->map(page_directory());
  488. }
  489. new_region.map(page_directory());
  490. return 0;
  491. }
  492. // FIXME: We should also support mprotect() across multiple regions. (#175) (#964)
  493. return -EINVAL;
  494. }
  495. int Process::sys$madvise(void* address, size_t size, int advice)
  496. {
  497. REQUIRE_PROMISE(stdio);
  498. if (!is_user_range(VirtualAddress(address), size))
  499. return -EFAULT;
  500. auto* region = region_from_range({ VirtualAddress(address), size });
  501. if (!region)
  502. return -EINVAL;
  503. if (!region->is_mmap())
  504. return -EPERM;
  505. if ((advice & MADV_SET_VOLATILE) && (advice & MADV_SET_NONVOLATILE))
  506. return -EINVAL;
  507. if (advice & MADV_SET_VOLATILE) {
  508. if (!region->vmobject().is_purgeable())
  509. return -EPERM;
  510. auto& vmobject = static_cast<PurgeableVMObject&>(region->vmobject());
  511. vmobject.set_volatile(true);
  512. return 0;
  513. }
  514. if (advice & MADV_SET_NONVOLATILE) {
  515. if (!region->vmobject().is_purgeable())
  516. return -EPERM;
  517. auto& vmobject = static_cast<PurgeableVMObject&>(region->vmobject());
  518. if (!vmobject.is_volatile())
  519. return 0;
  520. vmobject.set_volatile(false);
  521. bool was_purged = vmobject.was_purged();
  522. vmobject.set_was_purged(false);
  523. return was_purged ? 1 : 0;
  524. }
  525. if (advice & MADV_GET_VOLATILE) {
  526. if (!region->vmobject().is_purgeable())
  527. return -EPERM;
  528. auto& vmobject = static_cast<PurgeableVMObject&>(region->vmobject());
  529. return vmobject.is_volatile() ? 0 : 1;
  530. }
  531. return -EINVAL;
  532. }
  533. int Process::sys$purge(int mode)
  534. {
  535. REQUIRE_NO_PROMISES;
  536. if (!is_superuser())
  537. return -EPERM;
  538. int purged_page_count = 0;
  539. if (mode & PURGE_ALL_VOLATILE) {
  540. NonnullRefPtrVector<PurgeableVMObject> vmobjects;
  541. {
  542. InterruptDisabler disabler;
  543. MM.for_each_vmobject([&](auto& vmobject) {
  544. if (vmobject.is_purgeable())
  545. vmobjects.append(static_cast<PurgeableVMObject&>(vmobject));
  546. return IterationDecision::Continue;
  547. });
  548. }
  549. for (auto& vmobject : vmobjects) {
  550. purged_page_count += vmobject.purge();
  551. }
  552. }
  553. if (mode & PURGE_ALL_CLEAN_INODE) {
  554. NonnullRefPtrVector<InodeVMObject> vmobjects;
  555. {
  556. InterruptDisabler disabler;
  557. MM.for_each_vmobject([&](auto& vmobject) {
  558. if (vmobject.is_inode())
  559. vmobjects.append(static_cast<InodeVMObject&>(vmobject));
  560. return IterationDecision::Continue;
  561. });
  562. }
  563. for (auto& vmobject : vmobjects) {
  564. purged_page_count += vmobject.release_all_clean_pages();
  565. }
  566. }
  567. return purged_page_count;
  568. }
  569. int Process::sys$gethostname(char* buffer, ssize_t size)
  570. {
  571. REQUIRE_PROMISE(stdio);
  572. if (size < 0)
  573. return -EINVAL;
  574. if (!validate_write(buffer, size))
  575. return -EFAULT;
  576. LOCKER(*s_hostname_lock);
  577. if ((size_t)size < (s_hostname->length() + 1))
  578. return -ENAMETOOLONG;
  579. copy_to_user(buffer, s_hostname->characters(), s_hostname->length() + 1);
  580. return 0;
  581. }
  582. pid_t Process::sys$fork(RegisterDump& regs)
  583. {
  584. REQUIRE_PROMISE(proc);
  585. Thread* child_first_thread = nullptr;
  586. auto* child = new Process(child_first_thread, m_name, m_uid, m_gid, m_pid, m_ring, m_cwd, m_executable, m_tty, this);
  587. child->m_root_directory = m_root_directory;
  588. child->m_root_directory_relative_to_global_root = m_root_directory_relative_to_global_root;
  589. child->m_promises = m_promises;
  590. child->m_execpromises = m_execpromises;
  591. child->m_veil_state = m_veil_state;
  592. child->m_unveiled_paths = m_unveiled_paths;
  593. child->m_fds = m_fds;
  594. child->m_sid = m_sid;
  595. child->m_pgid = m_pgid;
  596. child->m_umask = m_umask;
  597. #ifdef FORK_DEBUG
  598. dbgprintf("fork: child=%p\n", child);
  599. #endif
  600. for (auto& region : m_regions) {
  601. #ifdef FORK_DEBUG
  602. dbg() << "fork: cloning Region{" << &region << "} '" << region.name() << "' @ " << region.vaddr();
  603. #endif
  604. auto& child_region = child->add_region(region.clone());
  605. child_region.map(child->page_directory());
  606. if (&region == m_master_tls_region)
  607. child->m_master_tls_region = &child_region;
  608. }
  609. child->m_extra_gids = m_extra_gids;
  610. auto& child_tss = child_first_thread->m_tss;
  611. child_tss.eax = 0; // fork() returns 0 in the child :^)
  612. child_tss.ebx = regs.ebx;
  613. child_tss.ecx = regs.ecx;
  614. child_tss.edx = regs.edx;
  615. child_tss.ebp = regs.ebp;
  616. child_tss.esp = regs.userspace_esp;
  617. child_tss.esi = regs.esi;
  618. child_tss.edi = regs.edi;
  619. child_tss.eflags = regs.eflags;
  620. child_tss.eip = regs.eip;
  621. child_tss.cs = regs.cs;
  622. child_tss.ds = regs.ds;
  623. child_tss.es = regs.es;
  624. child_tss.fs = regs.fs;
  625. child_tss.gs = regs.gs;
  626. child_tss.ss = regs.userspace_ss;
  627. #ifdef FORK_DEBUG
  628. dbgprintf("fork: child will begin executing at %w:%x with stack %w:%x, kstack %w:%x\n", child_tss.cs, child_tss.eip, child_tss.ss, child_tss.esp, child_tss.ss0, child_tss.esp0);
  629. #endif
  630. {
  631. InterruptDisabler disabler;
  632. g_processes->prepend(child);
  633. }
  634. #ifdef TASK_DEBUG
  635. kprintf("Process %u (%s) forked from %u @ %p\n", child->pid(), child->name().characters(), m_pid, child_tss.eip);
  636. #endif
  637. child_first_thread->set_state(Thread::State::Skip1SchedulerPass);
  638. return child->pid();
  639. }
  640. int Process::do_exec(NonnullRefPtr<FileDescription> main_program_description, Vector<String> arguments, Vector<String> environment, RefPtr<FileDescription> interpreter_description)
  641. {
  642. ASSERT(is_ring3());
  643. auto path = main_program_description->absolute_path();
  644. dbg() << "do_exec(" << path << ")";
  645. // FIXME(Thread): Kill any threads the moment we commit to the exec().
  646. if (thread_count() != 1) {
  647. dbgprintf("Gonna die because I have many threads! These are the threads:\n");
  648. for_each_thread([](Thread& thread) {
  649. dbgprintf("Thread{%p}: TID=%d, PID=%d\n", &thread, thread.tid(), thread.pid());
  650. return IterationDecision::Continue;
  651. });
  652. ASSERT(thread_count() == 1);
  653. ASSERT_NOT_REACHED();
  654. }
  655. size_t total_blob_size = 0;
  656. for (auto& a : arguments)
  657. total_blob_size += a.length() + 1;
  658. for (auto& e : environment)
  659. total_blob_size += e.length() + 1;
  660. size_t total_meta_size = sizeof(char*) * (arguments.size() + 1) + sizeof(char*) * (environment.size() + 1);
  661. // FIXME: How much stack space does process startup need?
  662. if ((total_blob_size + total_meta_size) >= Thread::default_userspace_stack_size)
  663. return -E2BIG;
  664. auto parts = path.split('/');
  665. if (parts.is_empty())
  666. return -ENOENT;
  667. RefPtr<InodeVMObject> vmobject;
  668. if (interpreter_description) {
  669. vmobject = InodeVMObject::create_with_inode(*interpreter_description->inode());
  670. } else {
  671. vmobject = InodeVMObject::create_with_inode(*main_program_description->inode());
  672. }
  673. if (static_cast<const InodeVMObject&>(*vmobject).writable_mappings()) {
  674. dbg() << "Refusing to execute a write-mapped program";
  675. return -ETXTBSY;
  676. }
  677. auto old_page_directory = move(m_page_directory);
  678. auto old_regions = move(m_regions);
  679. m_page_directory = PageDirectory::create_for_userspace(*this);
  680. #ifdef MM_DEBUG
  681. dbgprintf("Process %u exec: PD=%x created\n", pid(), m_page_directory.ptr());
  682. #endif
  683. MM.enter_process_paging_scope(*this);
  684. Region* region { nullptr };
  685. InodeMetadata loader_metadata;
  686. // FIXME: Hoooo boy this is a hack if I ever saw one.
  687. // This is the 'random' offset we're giving to our ET_DYN exectuables to start as.
  688. // It also happens to be the static Virtual Addresss offset every static exectuable gets :)
  689. // Without this, some assumptions by the ELF loading hooks below are severely broken.
  690. // 0x08000000 is a verified random number chosen by random dice roll https://xkcd.com/221/
  691. u32 totally_random_offset = interpreter_description ? 0x08000000 : 0;
  692. // FIXME: We should be able to load both the PT_INTERP interpreter and the main program... once the RTLD is smart enough
  693. if (interpreter_description) {
  694. loader_metadata = interpreter_description->metadata();
  695. region = allocate_region_with_vmobject(VirtualAddress(), loader_metadata.size, *vmobject, 0, interpreter_description->absolute_path(), PROT_READ, false);
  696. // we don't need the interpreter file desciption after we've loaded (or not) it into memory
  697. interpreter_description = nullptr;
  698. } else {
  699. loader_metadata = main_program_description->metadata();
  700. region = allocate_region_with_vmobject(VirtualAddress(), loader_metadata.size, *vmobject, 0, main_program_description->absolute_path(), PROT_READ, false);
  701. }
  702. ASSERT(region);
  703. Region* master_tls_region { nullptr };
  704. size_t master_tls_size = 0;
  705. size_t master_tls_alignment = 0;
  706. u32 entry_eip = 0;
  707. OwnPtr<ELFLoader> loader;
  708. {
  709. ArmedScopeGuard rollback_regions_guard([&]() {
  710. ASSERT(&current->process() == this);
  711. m_page_directory = move(old_page_directory);
  712. m_regions = move(old_regions);
  713. MM.enter_process_paging_scope(*this);
  714. });
  715. loader = make<ELFLoader>(region->vaddr().as_ptr(), loader_metadata.size);
  716. // Load the correct executable -- either interp or main program.
  717. // FIXME: Once we actually load both interp and main, we'll need to be more clever about this.
  718. // In that case, both will be ET_DYN objects, so they'll both be completely relocatable.
  719. // That means, we can put them literally anywhere in User VM space (ASLR anyone?).
  720. // ALSO FIXME: Reminder to really really fix that 'totally random offset' business.
  721. loader->map_section_hook = [&](VirtualAddress vaddr, size_t size, size_t alignment, size_t offset_in_image, bool is_readable, bool is_writable, bool is_executable, const String& name) -> u8* {
  722. ASSERT(size);
  723. ASSERT(alignment == PAGE_SIZE);
  724. int prot = 0;
  725. if (is_readable)
  726. prot |= PROT_READ;
  727. if (is_writable)
  728. prot |= PROT_WRITE;
  729. if (is_executable)
  730. prot |= PROT_EXEC;
  731. if (auto* region = allocate_region_with_vmobject(vaddr.offset(totally_random_offset), size, *vmobject, offset_in_image, String(name), prot))
  732. return region->vaddr().as_ptr();
  733. return nullptr;
  734. };
  735. loader->alloc_section_hook = [&](VirtualAddress vaddr, size_t size, size_t alignment, bool is_readable, bool is_writable, const String& name) -> u8* {
  736. ASSERT(size);
  737. ASSERT(alignment == PAGE_SIZE);
  738. int prot = 0;
  739. if (is_readable)
  740. prot |= PROT_READ;
  741. if (is_writable)
  742. prot |= PROT_WRITE;
  743. if (auto* region = allocate_region(vaddr.offset(totally_random_offset), size, String(name), prot))
  744. return region->vaddr().as_ptr();
  745. return nullptr;
  746. };
  747. // FIXME: Move TLS region allocation to userspace: LibC and the dynamic loader.
  748. // LibC if we end up with a statically linked executable, and the
  749. // dynamic loader so that it can create new TLS blocks for each shared libarary
  750. // that gets loaded as part of DT_NEEDED processing, and via dlopen()
  751. // If that doesn't happen quickly, at least pass the location of the TLS region
  752. // some ELF Auxilliary Vector so the loader can use it/create new ones as necessary.
  753. loader->tls_section_hook = [&](size_t size, size_t alignment) {
  754. ASSERT(size);
  755. master_tls_region = allocate_region({}, size, String(), PROT_READ | PROT_WRITE);
  756. master_tls_size = size;
  757. master_tls_alignment = alignment;
  758. return master_tls_region->vaddr().as_ptr();
  759. };
  760. bool success = loader->load();
  761. if (!success) {
  762. kprintf("do_exec: Failure loading %s\n", path.characters());
  763. return -ENOEXEC;
  764. }
  765. // FIXME: Validate that this virtual address is within executable region,
  766. // instead of just non-null. You could totally have a DSO with entry point of
  767. // the beginning of the text segement.
  768. if (!loader->entry().offset(totally_random_offset).get()) {
  769. kprintf("do_exec: Failure loading %s, entry pointer is invalid! (%p)\n", path.characters(), loader->entry().offset(totally_random_offset).get());
  770. return -ENOEXEC;
  771. }
  772. rollback_regions_guard.disarm();
  773. // NOTE: At this point, we've committed to the new executable.
  774. entry_eip = loader->entry().offset(totally_random_offset).get();
  775. #ifdef EXEC_DEBUG
  776. kprintf("Memory layout after ELF load:");
  777. dump_regions();
  778. #endif
  779. }
  780. m_elf_loader = move(loader);
  781. m_executable = main_program_description->custody();
  782. m_promises = m_execpromises;
  783. m_veil_state = VeilState::None;
  784. m_unveiled_paths.clear();
  785. // Copy of the master TLS region that we will clone for new threads
  786. m_master_tls_region = master_tls_region;
  787. auto main_program_metadata = main_program_description->metadata();
  788. if (!(main_program_description->custody()->mount_flags() & MS_NOSUID)) {
  789. if (main_program_metadata.is_setuid())
  790. m_euid = main_program_metadata.uid;
  791. if (main_program_metadata.is_setgid())
  792. m_egid = main_program_metadata.gid;
  793. }
  794. current->set_default_signal_dispositions();
  795. current->m_signal_mask = 0;
  796. current->m_pending_signals = 0;
  797. m_futex_queues.clear();
  798. for (int i = 0; i < m_fds.size(); ++i) {
  799. auto& daf = m_fds[i];
  800. if (daf.description && daf.flags & FD_CLOEXEC) {
  801. daf.description->close();
  802. daf = {};
  803. }
  804. }
  805. // FIXME: Should we just make a new Thread here instead?
  806. Thread* new_main_thread = nullptr;
  807. if (&current->process() == this) {
  808. new_main_thread = current;
  809. } else {
  810. for_each_thread([&](auto& thread) {
  811. new_main_thread = &thread;
  812. return IterationDecision::Break;
  813. });
  814. }
  815. ASSERT(new_main_thread);
  816. // NOTE: We create the new stack before disabling interrupts since it will zero-fault
  817. // and we don't want to deal with faults after this point.
  818. u32 new_userspace_esp = new_main_thread->make_userspace_stack_for_main_thread(move(arguments), move(environment));
  819. // We cli() manually here because we don't want to get interrupted between do_exec() and Schedule::yield().
  820. // The reason is that the task redirection we've set up above will be clobbered by the timer IRQ.
  821. // If we used an InterruptDisabler that sti()'d on exit, we might timer tick'd too soon in exec().
  822. if (&current->process() == this)
  823. cli();
  824. // NOTE: Be careful to not trigger any page faults below!
  825. Scheduler::prepare_to_modify_tss(*new_main_thread);
  826. m_name = parts.take_last();
  827. new_main_thread->set_name(m_name);
  828. auto& tss = new_main_thread->m_tss;
  829. u32 old_esp0 = tss.esp0;
  830. m_master_tls_size = master_tls_size;
  831. m_master_tls_alignment = master_tls_alignment;
  832. new_main_thread->make_thread_specific_region({});
  833. memset(&tss, 0, sizeof(TSS32));
  834. tss.iomapbase = sizeof(TSS32);
  835. tss.eflags = 0x0202;
  836. tss.eip = entry_eip;
  837. tss.cs = 0x1b;
  838. tss.ds = 0x23;
  839. tss.es = 0x23;
  840. tss.fs = 0x23;
  841. tss.gs = thread_specific_selector() | 3;
  842. tss.ss = 0x23;
  843. tss.cr3 = page_directory().cr3();
  844. tss.esp = new_userspace_esp;
  845. tss.ss0 = 0x10;
  846. tss.esp0 = old_esp0;
  847. tss.ss2 = m_pid;
  848. #ifdef TASK_DEBUG
  849. kprintf("Process %u (%s) exec'd %s @ %p\n", pid(), name().characters(), path.characters(), tss.eip);
  850. #endif
  851. new_main_thread->set_state(Thread::State::Skip1SchedulerPass);
  852. big_lock().force_unlock_if_locked();
  853. return 0;
  854. }
  855. static KResultOr<Vector<String>> find_shebang_interpreter_for_executable(const char first_page[], int nread)
  856. {
  857. int word_start = 2;
  858. int word_length = 0;
  859. if (nread > 2 && first_page[0] == '#' && first_page[1] == '!') {
  860. Vector<String> interpreter_words;
  861. for (int i = 2; i < nread; ++i) {
  862. if (first_page[i] == '\n') {
  863. break;
  864. }
  865. if (first_page[i] != ' ') {
  866. ++word_length;
  867. }
  868. if (first_page[i] == ' ') {
  869. if (word_length > 0) {
  870. interpreter_words.append(String(&first_page[word_start], word_length));
  871. }
  872. word_length = 0;
  873. word_start = i + 1;
  874. }
  875. }
  876. if (word_length > 0)
  877. interpreter_words.append(String(&first_page[word_start], word_length));
  878. if (!interpreter_words.is_empty())
  879. return interpreter_words;
  880. }
  881. return KResult(-ENOEXEC);
  882. }
  883. KResultOr<NonnullRefPtr<FileDescription>> Process::find_elf_interpreter_for_executable(const String& path, char (&first_page)[PAGE_SIZE], int nread, size_t file_size)
  884. {
  885. if (nread < (int)sizeof(Elf32_Ehdr))
  886. return KResult(-ENOEXEC);
  887. auto elf_header = (Elf32_Ehdr*)first_page;
  888. if (!ELFImage::validate_elf_header(*elf_header, file_size)) {
  889. dbgprintf("%s(%d) exec(%s): File has invalid ELF header\n", m_name.characters(), m_pid, path.characters());
  890. return KResult(-ENOEXEC);
  891. }
  892. // Not using KResultOr here because we'll want to do the same thing in userspace in the RTLD
  893. String interpreter_path;
  894. if (!ELFImage::validate_program_headers(*elf_header, file_size, (u8*)first_page, nread, interpreter_path)) {
  895. dbgprintf("%s(%d) exec(%s): File has invalid ELF Program headers\n", m_name.characters(), m_pid, path.characters());
  896. return KResult(-ENOEXEC);
  897. }
  898. if (!interpreter_path.is_empty()) {
  899. // Programs with an interpreter better be relocatable executables or we don't know what to do...
  900. if (elf_header->e_type != ET_DYN)
  901. return KResult(-ENOEXEC);
  902. dbgprintf("%s(%d) exec(%s): Using program interpreter %s\n", m_name.characters(), m_pid, path.characters(), interpreter_path.characters());
  903. auto interp_result = VFS::the().open(interpreter_path, O_EXEC, 0, current_directory());
  904. if (interp_result.is_error()) {
  905. dbgprintf("%s(%d) exec(%s): Unable to open program interpreter %s\n", m_name.characters(), m_pid, path.characters(), interpreter_path.characters());
  906. return interp_result.error();
  907. }
  908. auto interpreter_description = interp_result.value();
  909. auto interp_metadata = interpreter_description->metadata();
  910. ASSERT(interpreter_description->inode());
  911. // Validate the program interpreter as a valid elf binary.
  912. // If your program interpreter is a #! file or something, it's time to stop playing games :)
  913. if (interp_metadata.size < (int)sizeof(Elf32_Ehdr))
  914. return KResult(-ENOEXEC);
  915. memset(first_page, 0, sizeof(first_page));
  916. nread = interpreter_description->read((u8*)&first_page, sizeof(first_page));
  917. if (nread < (int)sizeof(Elf32_Ehdr))
  918. return KResult(-ENOEXEC);
  919. elf_header = (Elf32_Ehdr*)first_page;
  920. if (!ELFImage::validate_elf_header(*elf_header, interp_metadata.size)) {
  921. dbgprintf("%s(%d) exec(%s): Interpreter (%s) has invalid ELF header\n", m_name.characters(), m_pid, path.characters(), interpreter_description->absolute_path().characters());
  922. return KResult(-ENOEXEC);
  923. }
  924. // Not using KResultOr here because we'll want to do the same thing in userspace in the RTLD
  925. String interpreter_interpreter_path;
  926. if (!ELFImage::validate_program_headers(*elf_header, interp_metadata.size, (u8*)first_page, nread, interpreter_interpreter_path)) {
  927. dbgprintf("%s(%d) exec(%s): Interpreter (%s) has invalid ELF Program headers\n", m_name.characters(), m_pid, path.characters(), interpreter_description->absolute_path().characters());
  928. return KResult(-ENOEXEC);
  929. }
  930. if (!interpreter_interpreter_path.is_empty()) {
  931. dbgprintf("%s(%d) exec(%s): Interpreter (%s) has its own interpreter (%s)! No thank you!\n",
  932. m_name.characters(), m_pid, path.characters(), interpreter_description->absolute_path().characters(), interpreter_interpreter_path.characters());
  933. return KResult(-ELOOP);
  934. }
  935. return interpreter_description;
  936. }
  937. if (elf_header->e_type != ET_EXEC) {
  938. // We can't exec an ET_REL, that's just an object file from the compiler
  939. // If it's ET_DYN with no PT_INTERP, then we can't load it properly either
  940. return KResult(-ENOEXEC);
  941. }
  942. // No interpreter, but, path refers to a valid elf image
  943. return KResult(KSuccess);
  944. }
  945. int Process::exec(String path, Vector<String> arguments, Vector<String> environment, int recursion_depth)
  946. {
  947. if (recursion_depth > 2) {
  948. dbgprintf("%s(%d) exec(%s): SHENANIGANS! recursed too far trying to find #! interpreter\n", m_name.characters(), m_pid, path.characters());
  949. return -ELOOP;
  950. }
  951. // Open the file to check what kind of binary format it is
  952. // Currently supported formats:
  953. // - #! interpreted file
  954. // - ELF32
  955. // * ET_EXEC binary that just gets loaded
  956. // * ET_DYN binary that requires a program interpreter
  957. //
  958. auto result = VFS::the().open(path, O_EXEC, 0, current_directory());
  959. if (result.is_error())
  960. return result.error();
  961. auto description = result.value();
  962. auto metadata = description->metadata();
  963. // Always gonna need at least 3 bytes. these are for #!X
  964. if (metadata.size < 3)
  965. return -ENOEXEC;
  966. ASSERT(description->inode());
  967. // Read the first page of the program into memory so we can validate the binfmt of it
  968. char first_page[PAGE_SIZE];
  969. int nread = description->read((u8*)&first_page, sizeof(first_page));
  970. // 1) #! interpreted file
  971. auto shebang_result = find_shebang_interpreter_for_executable(first_page, nread);
  972. if (!shebang_result.is_error()) {
  973. Vector<String> new_arguments(shebang_result.value());
  974. new_arguments.append(path);
  975. arguments.remove(0);
  976. new_arguments.append(move(arguments));
  977. return exec(shebang_result.value().first(), move(new_arguments), move(environment), ++recursion_depth);
  978. }
  979. // #2) ELF32 for i386
  980. auto elf_result = find_elf_interpreter_for_executable(path, first_page, nread, metadata.size);
  981. RefPtr<FileDescription> interpreter_description;
  982. // We're getting either an interpreter, an error, or KSuccess (i.e. no interpreter but file checks out)
  983. if (!elf_result.is_error())
  984. interpreter_description = elf_result.value();
  985. else if (elf_result.error().is_error())
  986. return elf_result.error();
  987. // The bulk of exec() is done by do_exec(), which ensures that all locals
  988. // are cleaned up by the time we yield-teleport below.
  989. int rc = do_exec(move(description), move(arguments), move(environment), move(interpreter_description));
  990. if (rc < 0)
  991. return rc;
  992. if (&current->process() == this) {
  993. Scheduler::yield();
  994. ASSERT_NOT_REACHED();
  995. }
  996. return 0;
  997. }
  998. int Process::sys$execve(const Syscall::SC_execve_params* user_params)
  999. {
  1000. REQUIRE_PROMISE(exec);
  1001. // NOTE: Be extremely careful with allocating any kernel memory in exec().
  1002. // On success, the kernel stack will be lost.
  1003. Syscall::SC_execve_params params;
  1004. if (!validate_read_and_copy_typed(&params, user_params))
  1005. return -EFAULT;
  1006. if (params.arguments.length > ARG_MAX || params.environment.length > ARG_MAX)
  1007. return -E2BIG;
  1008. String path;
  1009. {
  1010. auto path_arg = get_syscall_path_argument(params.path);
  1011. if (path_arg.is_error())
  1012. return path_arg.error();
  1013. path = path_arg.value();
  1014. }
  1015. auto copy_user_strings = [&](const auto& list, auto& output) {
  1016. if (!list.length)
  1017. return true;
  1018. if (!validate_read_typed(list.strings, list.length))
  1019. return false;
  1020. Vector<Syscall::StringArgument, 32> strings;
  1021. strings.resize(list.length);
  1022. copy_from_user(strings.data(), list.strings, list.length * sizeof(Syscall::StringArgument));
  1023. for (size_t i = 0; i < list.length; ++i) {
  1024. auto string = validate_and_copy_string_from_user(strings[i]);
  1025. if (string.is_null())
  1026. return false;
  1027. output.append(move(string));
  1028. }
  1029. return true;
  1030. };
  1031. Vector<String> arguments;
  1032. if (!copy_user_strings(params.arguments, arguments))
  1033. return -EFAULT;
  1034. Vector<String> environment;
  1035. if (!copy_user_strings(params.environment, environment))
  1036. return -EFAULT;
  1037. int rc = exec(move(path), move(arguments), move(environment));
  1038. ASSERT(rc < 0); // We should never continue after a successful exec!
  1039. return rc;
  1040. }
  1041. Process* Process::create_user_process(Thread*& first_thread, const String& path, uid_t uid, gid_t gid, pid_t parent_pid, int& error, Vector<String>&& arguments, Vector<String>&& environment, TTY* tty)
  1042. {
  1043. // FIXME: Don't split() the path twice (sys$spawn also does it...)
  1044. auto parts = path.split('/');
  1045. if (arguments.is_empty()) {
  1046. arguments.append(parts.last());
  1047. }
  1048. RefPtr<Custody> cwd;
  1049. RefPtr<Custody> root;
  1050. {
  1051. InterruptDisabler disabler;
  1052. if (auto* parent = Process::from_pid(parent_pid)) {
  1053. cwd = parent->m_cwd;
  1054. root = parent->m_root_directory;
  1055. }
  1056. }
  1057. if (!cwd)
  1058. cwd = VFS::the().root_custody();
  1059. if (!root)
  1060. root = VFS::the().root_custody();
  1061. auto* process = new Process(first_thread, parts.take_last(), uid, gid, parent_pid, Ring3, move(cwd), nullptr, tty);
  1062. process->m_fds.resize(m_max_open_file_descriptors);
  1063. auto& device_to_use_as_tty = tty ? (CharacterDevice&)*tty : NullDevice::the();
  1064. auto description = device_to_use_as_tty.open(O_RDWR).value();
  1065. process->m_fds[0].set(*description);
  1066. process->m_fds[1].set(*description);
  1067. process->m_fds[2].set(*description);
  1068. error = process->exec(path, move(arguments), move(environment));
  1069. if (error != 0) {
  1070. delete process;
  1071. return nullptr;
  1072. }
  1073. {
  1074. InterruptDisabler disabler;
  1075. g_processes->prepend(process);
  1076. }
  1077. #ifdef TASK_DEBUG
  1078. kprintf("Process %u (%s) spawned @ %p\n", process->pid(), process->name().characters(), first_thread->tss().eip);
  1079. #endif
  1080. error = 0;
  1081. return process;
  1082. }
  1083. Process* Process::create_kernel_process(Thread*& first_thread, String&& name, void (*e)())
  1084. {
  1085. auto* process = new Process(first_thread, move(name), (uid_t)0, (gid_t)0, (pid_t)0, Ring0);
  1086. first_thread->tss().eip = (uintptr_t)e;
  1087. if (process->pid() != 0) {
  1088. InterruptDisabler disabler;
  1089. g_processes->prepend(process);
  1090. #ifdef TASK_DEBUG
  1091. kprintf("Kernel process %u (%s) spawned @ %p\n", process->pid(), process->name().characters(), first_thread->tss().eip);
  1092. #endif
  1093. }
  1094. first_thread->set_state(Thread::State::Runnable);
  1095. return process;
  1096. }
  1097. Process::Process(Thread*& first_thread, const String& name, uid_t uid, gid_t gid, pid_t ppid, RingLevel ring, RefPtr<Custody> cwd, RefPtr<Custody> executable, TTY* tty, Process* fork_parent)
  1098. : m_name(move(name))
  1099. , m_pid(allocate_pid())
  1100. , m_uid(uid)
  1101. , m_gid(gid)
  1102. , m_euid(uid)
  1103. , m_egid(gid)
  1104. , m_ring(ring)
  1105. , m_executable(move(executable))
  1106. , m_cwd(move(cwd))
  1107. , m_tty(tty)
  1108. , m_ppid(ppid)
  1109. {
  1110. dbg() << "Created new process " << m_name << "(" << m_pid << ")";
  1111. m_page_directory = PageDirectory::create_for_userspace(*this, fork_parent ? &fork_parent->page_directory().range_allocator() : nullptr);
  1112. #ifdef MM_DEBUG
  1113. dbgprintf("Process %u ctor: PD=%x created\n", pid(), m_page_directory.ptr());
  1114. #endif
  1115. if (fork_parent) {
  1116. // NOTE: fork() doesn't clone all threads; the thread that called fork() becomes the only thread in the new process.
  1117. first_thread = current->clone(*this);
  1118. } else {
  1119. // NOTE: This non-forked code path is only taken when the kernel creates a process "manually" (at boot.)
  1120. first_thread = new Thread(*this);
  1121. }
  1122. }
  1123. Process::~Process()
  1124. {
  1125. ASSERT(thread_count() == 0);
  1126. }
  1127. void Process::dump_regions()
  1128. {
  1129. kprintf("Process %s(%u) regions:\n", name().characters(), pid());
  1130. kprintf("BEGIN END SIZE ACCESS NAME\n");
  1131. for (auto& region : m_regions) {
  1132. kprintf("%08x -- %08x %08x %c%c%c%c%c%c %s\n",
  1133. region.vaddr().get(),
  1134. region.vaddr().offset(region.size() - 1).get(),
  1135. region.size(),
  1136. region.is_readable() ? 'R' : ' ',
  1137. region.is_writable() ? 'W' : ' ',
  1138. region.is_executable() ? 'X' : ' ',
  1139. region.is_shared() ? 'S' : ' ',
  1140. region.is_stack() ? 'T' : ' ',
  1141. region.vmobject().is_purgeable() ? 'P' : ' ',
  1142. region.name().characters());
  1143. }
  1144. MM.dump_kernel_regions();
  1145. }
  1146. void Process::sys$exit(int status)
  1147. {
  1148. cli();
  1149. #ifdef TASK_DEBUG
  1150. kprintf("sys$exit: %s(%u) exit with status %d\n", name().characters(), pid(), status);
  1151. #endif
  1152. if (status != 0)
  1153. dump_backtrace();
  1154. m_termination_status = status;
  1155. m_termination_signal = 0;
  1156. die();
  1157. current->die_if_needed();
  1158. ASSERT_NOT_REACHED();
  1159. }
  1160. void signal_trampoline_dummy(void)
  1161. {
  1162. // The trampoline preserves the current eax, pushes the signal code and
  1163. // then calls the signal handler. We do this because, when interrupting a
  1164. // blocking syscall, that syscall may return some special error code in eax;
  1165. // This error code would likely be overwritten by the signal handler, so it's
  1166. // neccessary to preserve it here.
  1167. asm(
  1168. ".intel_syntax noprefix\n"
  1169. "asm_signal_trampoline:\n"
  1170. "push ebp\n"
  1171. "mov ebp, esp\n"
  1172. "push eax\n" // we have to store eax 'cause it might be the return value from a syscall
  1173. "sub esp, 4\n" // align the stack to 16 bytes
  1174. "mov eax, [ebp+12]\n" // push the signal code
  1175. "push eax\n"
  1176. "call [ebp+8]\n" // call the signal handler
  1177. "add esp, 8\n"
  1178. "mov eax, %P0\n"
  1179. "int 0x82\n" // sigreturn syscall
  1180. "asm_signal_trampoline_end:\n"
  1181. ".att_syntax" ::"i"(Syscall::SC_sigreturn));
  1182. }
  1183. extern "C" void asm_signal_trampoline(void);
  1184. extern "C" void asm_signal_trampoline_end(void);
  1185. void create_signal_trampolines()
  1186. {
  1187. InterruptDisabler disabler;
  1188. // NOTE: We leak this region.
  1189. auto* trampoline_region = MM.allocate_user_accessible_kernel_region(PAGE_SIZE, "Signal trampolines", Region::Access::Read | Region::Access::Write | Region::Access::Execute, false).leak_ptr();
  1190. g_return_to_ring3_from_signal_trampoline = trampoline_region->vaddr();
  1191. u8* trampoline = (u8*)asm_signal_trampoline;
  1192. u8* trampoline_end = (u8*)asm_signal_trampoline_end;
  1193. size_t trampoline_size = trampoline_end - trampoline;
  1194. {
  1195. SmapDisabler disabler;
  1196. u8* code_ptr = (u8*)trampoline_region->vaddr().as_ptr();
  1197. memcpy(code_ptr, trampoline, trampoline_size);
  1198. }
  1199. trampoline_region->set_writable(false);
  1200. trampoline_region->remap();
  1201. }
  1202. void create_kernel_info_page()
  1203. {
  1204. auto* info_page_region_for_userspace = MM.allocate_user_accessible_kernel_region(PAGE_SIZE, "Kernel info page", Region::Access::Read).leak_ptr();
  1205. auto* info_page_region_for_kernel = MM.allocate_kernel_region_with_vmobject(info_page_region_for_userspace->vmobject(), PAGE_SIZE, "Kernel info page", Region::Access::Read | Region::Access::Write).leak_ptr();
  1206. s_info_page_address_for_userspace = info_page_region_for_userspace->vaddr();
  1207. s_info_page_address_for_kernel = info_page_region_for_kernel->vaddr();
  1208. memset(s_info_page_address_for_kernel.as_ptr(), 0, PAGE_SIZE);
  1209. }
  1210. int Process::sys$sigreturn(RegisterDump& registers)
  1211. {
  1212. REQUIRE_PROMISE(stdio);
  1213. SmapDisabler disabler;
  1214. //Here, we restore the state pushed by dispatch signal and asm_signal_trampoline.
  1215. u32* stack_ptr = (u32*)registers.userspace_esp;
  1216. u32 smuggled_eax = *stack_ptr;
  1217. //pop the stored eax, ebp, return address, handler and signal code
  1218. stack_ptr += 5;
  1219. current->m_signal_mask = *stack_ptr;
  1220. stack_ptr++;
  1221. //pop edi, esi, ebp, esp, ebx, edx, ecx and eax
  1222. memcpy(&registers.edi, stack_ptr, 8 * sizeof(uintptr_t));
  1223. stack_ptr += 8;
  1224. registers.eip = *stack_ptr;
  1225. stack_ptr++;
  1226. registers.eflags = *stack_ptr;
  1227. stack_ptr++;
  1228. registers.userspace_esp = registers.esp;
  1229. return smuggled_eax;
  1230. }
  1231. void Process::crash(int signal, u32 eip)
  1232. {
  1233. ASSERT_INTERRUPTS_DISABLED();
  1234. ASSERT(!is_dead());
  1235. ASSERT(&current->process() == this);
  1236. if (eip >= 0xc0000000 && ksyms_ready) {
  1237. auto* ksym = ksymbolicate(eip);
  1238. dbgprintf("\033[31;1m%p %s +%d\033[0m\n", eip, ksym ? demangle(ksym->name).characters() : "(k?)", ksym ? eip - ksym->address : 0);
  1239. } else if (m_elf_loader) {
  1240. dbgprintf("\033[31;1m%p %s\033[0m\n", eip, m_elf_loader->symbolicate(eip).characters());
  1241. } else {
  1242. dbgprintf("\033[31;1m%p (?)\033[0m\n", eip);
  1243. }
  1244. dump_backtrace();
  1245. m_termination_signal = signal;
  1246. dump_regions();
  1247. ASSERT(is_ring3());
  1248. die();
  1249. // We can not return from here, as there is nowhere
  1250. // to unwind to, so die right away.
  1251. current->die_if_needed();
  1252. ASSERT_NOT_REACHED();
  1253. }
  1254. Process* Process::from_pid(pid_t pid)
  1255. {
  1256. ASSERT_INTERRUPTS_DISABLED();
  1257. for (auto& process : *g_processes) {
  1258. if (process.pid() == pid)
  1259. return &process;
  1260. }
  1261. return nullptr;
  1262. }
  1263. RefPtr<FileDescription> Process::file_description(int fd) const
  1264. {
  1265. if (fd < 0)
  1266. return nullptr;
  1267. if (fd < m_fds.size())
  1268. return m_fds[fd].description.ptr();
  1269. return nullptr;
  1270. }
  1271. int Process::fd_flags(int fd) const
  1272. {
  1273. if (fd < 0)
  1274. return -1;
  1275. if (fd < m_fds.size())
  1276. return m_fds[fd].flags;
  1277. return -1;
  1278. }
  1279. ssize_t Process::sys$get_dir_entries(int fd, void* buffer, ssize_t size)
  1280. {
  1281. REQUIRE_PROMISE(stdio);
  1282. if (size < 0)
  1283. return -EINVAL;
  1284. if (!validate_write(buffer, size))
  1285. return -EFAULT;
  1286. auto description = file_description(fd);
  1287. if (!description)
  1288. return -EBADF;
  1289. return description->get_dir_entries((u8*)buffer, size);
  1290. }
  1291. int Process::sys$lseek(int fd, off_t offset, int whence)
  1292. {
  1293. REQUIRE_PROMISE(stdio);
  1294. auto description = file_description(fd);
  1295. if (!description)
  1296. return -EBADF;
  1297. return description->seek(offset, whence);
  1298. }
  1299. int Process::sys$ttyname_r(int fd, char* buffer, ssize_t size)
  1300. {
  1301. REQUIRE_PROMISE(tty);
  1302. if (size < 0)
  1303. return -EINVAL;
  1304. if (!validate_write(buffer, size))
  1305. return -EFAULT;
  1306. auto description = file_description(fd);
  1307. if (!description)
  1308. return -EBADF;
  1309. if (!description->is_tty())
  1310. return -ENOTTY;
  1311. String tty_name = description->tty()->tty_name();
  1312. if ((size_t)size < tty_name.length() + 1)
  1313. return -ERANGE;
  1314. copy_to_user(buffer, tty_name.characters(), tty_name.length() + 1);
  1315. return 0;
  1316. }
  1317. int Process::sys$ptsname_r(int fd, char* buffer, ssize_t size)
  1318. {
  1319. REQUIRE_PROMISE(tty);
  1320. if (size < 0)
  1321. return -EINVAL;
  1322. if (!validate_write(buffer, size))
  1323. return -EFAULT;
  1324. auto description = file_description(fd);
  1325. if (!description)
  1326. return -EBADF;
  1327. auto* master_pty = description->master_pty();
  1328. if (!master_pty)
  1329. return -ENOTTY;
  1330. auto pts_name = master_pty->pts_name();
  1331. if ((size_t)size < pts_name.length() + 1)
  1332. return -ERANGE;
  1333. copy_to_user(buffer, pts_name.characters(), pts_name.length() + 1);
  1334. return 0;
  1335. }
  1336. ssize_t Process::sys$writev(int fd, const struct iovec* iov, int iov_count)
  1337. {
  1338. REQUIRE_PROMISE(stdio);
  1339. if (iov_count < 0)
  1340. return -EINVAL;
  1341. if (!validate_read_typed(iov, iov_count))
  1342. return -EFAULT;
  1343. u64 total_length = 0;
  1344. Vector<iovec, 32> vecs;
  1345. vecs.resize(iov_count);
  1346. copy_from_user(vecs.data(), iov, iov_count * sizeof(iovec));
  1347. for (auto& vec : vecs) {
  1348. if (!validate_read(vec.iov_base, vec.iov_len))
  1349. return -EFAULT;
  1350. total_length += vec.iov_len;
  1351. if (total_length > INT32_MAX)
  1352. return -EINVAL;
  1353. }
  1354. auto description = file_description(fd);
  1355. if (!description)
  1356. return -EBADF;
  1357. if (!description->is_writable())
  1358. return -EBADF;
  1359. int nwritten = 0;
  1360. for (auto& vec : vecs) {
  1361. int rc = do_write(*description, (const u8*)vec.iov_base, vec.iov_len);
  1362. if (rc < 0) {
  1363. if (nwritten == 0)
  1364. return rc;
  1365. return nwritten;
  1366. }
  1367. nwritten += rc;
  1368. }
  1369. return nwritten;
  1370. }
  1371. ssize_t Process::do_write(FileDescription& description, const u8* data, int data_size)
  1372. {
  1373. ssize_t nwritten = 0;
  1374. if (!description.is_blocking()) {
  1375. if (!description.can_write())
  1376. return -EAGAIN;
  1377. }
  1378. if (description.should_append()) {
  1379. #ifdef IO_DEBUG
  1380. dbgprintf("seeking to end (O_APPEND)\n");
  1381. #endif
  1382. description.seek(0, SEEK_END);
  1383. }
  1384. while (nwritten < data_size) {
  1385. #ifdef IO_DEBUG
  1386. dbgprintf("while %u < %u\n", nwritten, size);
  1387. #endif
  1388. if (!description.can_write()) {
  1389. #ifdef IO_DEBUG
  1390. dbgprintf("block write on %d\n", fd);
  1391. #endif
  1392. if (current->block<Thread::WriteBlocker>(description) != Thread::BlockResult::WokeNormally) {
  1393. if (nwritten == 0)
  1394. return -EINTR;
  1395. }
  1396. }
  1397. ssize_t rc = description.write(data + nwritten, data_size - nwritten);
  1398. #ifdef IO_DEBUG
  1399. dbgprintf(" -> write returned %d\n", rc);
  1400. #endif
  1401. if (rc < 0) {
  1402. // FIXME: Support returning partial nwritten with errno.
  1403. ASSERT(nwritten == 0);
  1404. return rc;
  1405. }
  1406. if (rc == 0)
  1407. break;
  1408. nwritten += rc;
  1409. }
  1410. return nwritten;
  1411. }
  1412. ssize_t Process::sys$write(int fd, const u8* data, ssize_t size)
  1413. {
  1414. REQUIRE_PROMISE(stdio);
  1415. if (size < 0)
  1416. return -EINVAL;
  1417. if (size == 0)
  1418. return 0;
  1419. if (!validate_read(data, size))
  1420. return -EFAULT;
  1421. #ifdef DEBUG_IO
  1422. dbgprintf("%s(%u): sys$write(%d, %p, %u)\n", name().characters(), pid(), fd, data, size);
  1423. #endif
  1424. auto description = file_description(fd);
  1425. if (!description)
  1426. return -EBADF;
  1427. if (!description->is_writable())
  1428. return -EBADF;
  1429. return do_write(*description, data, size);
  1430. }
  1431. ssize_t Process::sys$read(int fd, u8* buffer, ssize_t size)
  1432. {
  1433. REQUIRE_PROMISE(stdio);
  1434. if (size < 0)
  1435. return -EINVAL;
  1436. if (size == 0)
  1437. return 0;
  1438. if (!validate_write(buffer, size))
  1439. return -EFAULT;
  1440. #ifdef DEBUG_IO
  1441. dbgprintf("%s(%u) sys$read(%d, %p, %u)\n", name().characters(), pid(), fd, buffer, size);
  1442. #endif
  1443. auto description = file_description(fd);
  1444. if (!description)
  1445. return -EBADF;
  1446. if (!description->is_readable())
  1447. return -EBADF;
  1448. if (description->is_directory())
  1449. return -EISDIR;
  1450. if (description->is_blocking()) {
  1451. if (!description->can_read()) {
  1452. if (current->block<Thread::ReadBlocker>(*description) != Thread::BlockResult::WokeNormally)
  1453. return -EINTR;
  1454. if (!description->can_read())
  1455. return -EAGAIN;
  1456. }
  1457. }
  1458. return description->read(buffer, size);
  1459. }
  1460. int Process::sys$close(int fd)
  1461. {
  1462. REQUIRE_PROMISE(stdio);
  1463. auto description = file_description(fd);
  1464. #ifdef DEBUG_IO
  1465. dbgprintf("%s(%u) sys$close(%d) %p\n", name().characters(), pid(), fd, description.ptr());
  1466. #endif
  1467. if (!description)
  1468. return -EBADF;
  1469. int rc = description->close();
  1470. m_fds[fd] = {};
  1471. return rc;
  1472. }
  1473. int Process::sys$utime(const char* user_path, size_t path_length, const utimbuf* user_buf)
  1474. {
  1475. REQUIRE_PROMISE(fattr);
  1476. if (user_buf && !validate_read_typed(user_buf))
  1477. return -EFAULT;
  1478. auto path = get_syscall_path_argument(user_path, path_length);
  1479. if (path.is_error())
  1480. return path.error();
  1481. utimbuf buf;
  1482. if (user_buf) {
  1483. copy_from_user(&buf, user_buf);
  1484. } else {
  1485. auto now = kgettimeofday();
  1486. buf = { now.tv_sec, now.tv_sec };
  1487. }
  1488. return VFS::the().utime(path.value(), current_directory(), buf.actime, buf.modtime);
  1489. }
  1490. int Process::sys$access(const char* user_path, size_t path_length, int mode)
  1491. {
  1492. REQUIRE_PROMISE(rpath);
  1493. auto path = get_syscall_path_argument(user_path, path_length);
  1494. if (path.is_error())
  1495. return path.error();
  1496. return VFS::the().access(path.value(), mode, current_directory());
  1497. }
  1498. int Process::sys$fcntl(int fd, int cmd, u32 arg)
  1499. {
  1500. REQUIRE_PROMISE(stdio);
  1501. #ifdef DEBUG_IO
  1502. dbgprintf("sys$fcntl: fd=%d, cmd=%d, arg=%u\n", fd, cmd, arg);
  1503. #endif
  1504. auto description = file_description(fd);
  1505. if (!description)
  1506. return -EBADF;
  1507. // NOTE: The FD flags are not shared between FileDescription objects.
  1508. // This means that dup() doesn't copy the FD_CLOEXEC flag!
  1509. switch (cmd) {
  1510. case F_DUPFD: {
  1511. int arg_fd = (int)arg;
  1512. if (arg_fd < 0)
  1513. return -EINVAL;
  1514. int new_fd = alloc_fd(arg_fd);
  1515. if (new_fd < 0)
  1516. return new_fd;
  1517. m_fds[new_fd].set(*description);
  1518. return new_fd;
  1519. }
  1520. case F_GETFD:
  1521. return m_fds[fd].flags;
  1522. case F_SETFD:
  1523. m_fds[fd].flags = arg;
  1524. break;
  1525. case F_GETFL:
  1526. return description->file_flags();
  1527. case F_SETFL:
  1528. description->set_file_flags(arg);
  1529. break;
  1530. default:
  1531. ASSERT_NOT_REACHED();
  1532. }
  1533. return 0;
  1534. }
  1535. int Process::sys$fstat(int fd, stat* statbuf)
  1536. {
  1537. REQUIRE_PROMISE(stdio);
  1538. if (!validate_write_typed(statbuf))
  1539. return -EFAULT;
  1540. auto description = file_description(fd);
  1541. if (!description)
  1542. return -EBADF;
  1543. return description->fstat(*statbuf);
  1544. }
  1545. int Process::sys$lstat(const char* user_path, size_t path_length, stat* user_statbuf)
  1546. {
  1547. REQUIRE_PROMISE(rpath);
  1548. if (!validate_write_typed(user_statbuf))
  1549. return -EFAULT;
  1550. auto path = get_syscall_path_argument(user_path, path_length);
  1551. if (path.is_error())
  1552. return path.error();
  1553. auto metadata_or_error = VFS::the().lookup_metadata(path.value(), current_directory(), O_NOFOLLOW_NOERROR);
  1554. if (metadata_or_error.is_error())
  1555. return metadata_or_error.error();
  1556. stat statbuf;
  1557. auto result = metadata_or_error.value().stat(statbuf);
  1558. if (result.is_error())
  1559. return result;
  1560. copy_to_user(user_statbuf, &statbuf);
  1561. return 0;
  1562. }
  1563. int Process::sys$stat(const char* user_path, size_t path_length, stat* user_statbuf)
  1564. {
  1565. REQUIRE_PROMISE(rpath);
  1566. if (!validate_write_typed(user_statbuf))
  1567. return -EFAULT;
  1568. auto path = get_syscall_path_argument(user_path, path_length);
  1569. if (path.is_error())
  1570. return path.error();
  1571. auto metadata_or_error = VFS::the().lookup_metadata(path.value(), current_directory());
  1572. if (metadata_or_error.is_error())
  1573. return metadata_or_error.error();
  1574. stat statbuf;
  1575. auto result = metadata_or_error.value().stat(statbuf);
  1576. if (result.is_error())
  1577. return result;
  1578. copy_to_user(user_statbuf, &statbuf);
  1579. return 0;
  1580. }
  1581. template<typename DataType, typename SizeType>
  1582. bool Process::validate(const Syscall::MutableBufferArgument<DataType, SizeType>& buffer)
  1583. {
  1584. return validate_write(buffer.data, buffer.size);
  1585. }
  1586. template<typename DataType, typename SizeType>
  1587. bool Process::validate(const Syscall::ImmutableBufferArgument<DataType, SizeType>& buffer)
  1588. {
  1589. return validate_read(buffer.data, buffer.size);
  1590. }
  1591. String Process::validate_and_copy_string_from_user(const char* user_characters, size_t user_length) const
  1592. {
  1593. if (user_length == 0)
  1594. return String::empty();
  1595. if (!user_characters)
  1596. return {};
  1597. if (!validate_read(user_characters, user_length))
  1598. return {};
  1599. SmapDisabler disabler;
  1600. size_t measured_length = strnlen(user_characters, user_length);
  1601. return String(user_characters, measured_length);
  1602. }
  1603. String Process::validate_and_copy_string_from_user(const Syscall::StringArgument& string) const
  1604. {
  1605. return validate_and_copy_string_from_user(string.characters, string.length);
  1606. }
  1607. int Process::sys$readlink(const Syscall::SC_readlink_params* user_params)
  1608. {
  1609. REQUIRE_PROMISE(rpath);
  1610. Syscall::SC_readlink_params params;
  1611. if (!validate_read_and_copy_typed(&params, user_params))
  1612. return -EFAULT;
  1613. if (!validate(params.buffer))
  1614. return -EFAULT;
  1615. auto path = get_syscall_path_argument(params.path);
  1616. if (path.is_error())
  1617. return path.error();
  1618. auto result = VFS::the().open(path.value(), O_RDONLY | O_NOFOLLOW_NOERROR, 0, current_directory());
  1619. if (result.is_error())
  1620. return result.error();
  1621. auto description = result.value();
  1622. if (!description->metadata().is_symlink())
  1623. return -EINVAL;
  1624. auto contents = description->read_entire_file();
  1625. if (!contents)
  1626. return -EIO; // FIXME: Get a more detailed error from VFS.
  1627. auto link_target = String::copy(contents);
  1628. if (link_target.length() + 1 > params.buffer.size)
  1629. return -ENAMETOOLONG;
  1630. copy_to_user(params.buffer.data, link_target.characters(), link_target.length() + 1);
  1631. return link_target.length() + 1;
  1632. }
  1633. int Process::sys$chdir(const char* user_path, size_t path_length)
  1634. {
  1635. REQUIRE_PROMISE(rpath);
  1636. auto path = get_syscall_path_argument(user_path, path_length);
  1637. if (path.is_error())
  1638. return path.error();
  1639. auto directory_or_error = VFS::the().open_directory(path.value(), current_directory());
  1640. if (directory_or_error.is_error())
  1641. return directory_or_error.error();
  1642. m_cwd = *directory_or_error.value();
  1643. return 0;
  1644. }
  1645. int Process::sys$fchdir(int fd)
  1646. {
  1647. REQUIRE_PROMISE(stdio);
  1648. auto description = file_description(fd);
  1649. if (!description)
  1650. return -EBADF;
  1651. if (!description->is_directory())
  1652. return -ENOTDIR;
  1653. if (!description->metadata().may_execute(*this))
  1654. return -EACCES;
  1655. m_cwd = description->custody();
  1656. return 0;
  1657. }
  1658. int Process::sys$getcwd(char* buffer, ssize_t size)
  1659. {
  1660. REQUIRE_PROMISE(rpath);
  1661. if (size < 0)
  1662. return -EINVAL;
  1663. if (!validate_write(buffer, size))
  1664. return -EFAULT;
  1665. auto path = current_directory().absolute_path();
  1666. if ((size_t)size < path.length() + 1)
  1667. return -ERANGE;
  1668. copy_to_user(buffer, path.characters(), path.length() + 1);
  1669. return 0;
  1670. }
  1671. int Process::number_of_open_file_descriptors() const
  1672. {
  1673. int count = 0;
  1674. for (auto& description : m_fds) {
  1675. if (description)
  1676. ++count;
  1677. }
  1678. return count;
  1679. }
  1680. int Process::sys$open(const Syscall::SC_open_params* user_params)
  1681. {
  1682. Syscall::SC_open_params params;
  1683. if (!validate_read_and_copy_typed(&params, user_params))
  1684. return -EFAULT;
  1685. int dirfd = params.dirfd;
  1686. int options = params.options;
  1687. u16 mode = params.mode;
  1688. if (options & O_NOFOLLOW_NOERROR)
  1689. return -EINVAL;
  1690. if (options & O_UNLINK_INTERNAL)
  1691. return -EINVAL;
  1692. if (options & O_WRONLY)
  1693. REQUIRE_PROMISE(wpath);
  1694. else if (options & O_RDONLY)
  1695. REQUIRE_PROMISE(rpath);
  1696. if (options & O_CREAT)
  1697. REQUIRE_PROMISE(cpath);
  1698. // Ignore everything except permission bits.
  1699. mode &= 04777;
  1700. auto path = get_syscall_path_argument(params.path);
  1701. if (path.is_error())
  1702. return path.error();
  1703. #ifdef DEBUG_IO
  1704. dbgprintf("%s(%u) sys$open(%d, \"%s\")\n", dirfd, name().characters(), pid(), path.value().characters());
  1705. #endif
  1706. int fd = alloc_fd();
  1707. if (fd < 0)
  1708. return fd;
  1709. RefPtr<Custody> base;
  1710. if (dirfd == AT_FDCWD) {
  1711. base = current_directory();
  1712. } else {
  1713. auto base_description = file_description(dirfd);
  1714. if (!base_description)
  1715. return -EBADF;
  1716. if (!base_description->is_directory())
  1717. return -ENOTDIR;
  1718. if (!base_description->custody())
  1719. return -EINVAL;
  1720. base = base_description->custody();
  1721. }
  1722. auto result = VFS::the().open(path.value(), options, mode & ~umask(), *base);
  1723. if (result.is_error())
  1724. return result.error();
  1725. auto description = result.value();
  1726. u32 fd_flags = (options & O_CLOEXEC) ? FD_CLOEXEC : 0;
  1727. m_fds[fd].set(move(description), fd_flags);
  1728. return fd;
  1729. }
  1730. int Process::alloc_fd(int first_candidate_fd)
  1731. {
  1732. for (int i = first_candidate_fd; i < (int)m_max_open_file_descriptors; ++i) {
  1733. if (!m_fds[i])
  1734. return i;
  1735. }
  1736. return -EMFILE;
  1737. }
  1738. int Process::sys$pipe(int pipefd[2], int flags)
  1739. {
  1740. REQUIRE_PROMISE(stdio);
  1741. if (!validate_write_typed(pipefd))
  1742. return -EFAULT;
  1743. if (number_of_open_file_descriptors() + 2 > max_open_file_descriptors())
  1744. return -EMFILE;
  1745. // Reject flags other than O_CLOEXEC.
  1746. if ((flags & O_CLOEXEC) != flags)
  1747. return -EINVAL;
  1748. u32 fd_flags = (flags & O_CLOEXEC) ? FD_CLOEXEC : 0;
  1749. auto fifo = FIFO::create(m_uid);
  1750. int reader_fd = alloc_fd();
  1751. m_fds[reader_fd].set(fifo->open_direction(FIFO::Direction::Reader), fd_flags);
  1752. m_fds[reader_fd].description->set_readable(true);
  1753. copy_to_user(&pipefd[0], &reader_fd);
  1754. int writer_fd = alloc_fd();
  1755. m_fds[writer_fd].set(fifo->open_direction(FIFO::Direction::Writer), fd_flags);
  1756. m_fds[writer_fd].description->set_writable(true);
  1757. copy_to_user(&pipefd[1], &writer_fd);
  1758. return 0;
  1759. }
  1760. int Process::sys$killpg(int pgrp, int signum)
  1761. {
  1762. REQUIRE_PROMISE(proc);
  1763. if (signum < 1 || signum >= 32)
  1764. return -EINVAL;
  1765. if (pgrp < 0)
  1766. return -EINVAL;
  1767. InterruptDisabler disabler;
  1768. return do_killpg(pgrp, signum);
  1769. }
  1770. int Process::sys$setuid(uid_t uid)
  1771. {
  1772. REQUIRE_PROMISE(id);
  1773. if (uid != m_uid && !is_superuser())
  1774. return -EPERM;
  1775. m_uid = uid;
  1776. m_euid = uid;
  1777. return 0;
  1778. }
  1779. int Process::sys$setgid(gid_t gid)
  1780. {
  1781. REQUIRE_PROMISE(id);
  1782. if (gid != m_gid && !is_superuser())
  1783. return -EPERM;
  1784. m_gid = gid;
  1785. m_egid = gid;
  1786. return 0;
  1787. }
  1788. unsigned Process::sys$alarm(unsigned seconds)
  1789. {
  1790. REQUIRE_PROMISE(stdio);
  1791. unsigned previous_alarm_remaining = 0;
  1792. if (m_alarm_deadline && m_alarm_deadline > g_uptime) {
  1793. previous_alarm_remaining = (m_alarm_deadline - g_uptime) / TICKS_PER_SECOND;
  1794. }
  1795. if (!seconds) {
  1796. m_alarm_deadline = 0;
  1797. return previous_alarm_remaining;
  1798. }
  1799. m_alarm_deadline = g_uptime + seconds * TICKS_PER_SECOND;
  1800. return previous_alarm_remaining;
  1801. }
  1802. int Process::sys$uname(utsname* buf)
  1803. {
  1804. REQUIRE_PROMISE(stdio);
  1805. if (!validate_write_typed(buf))
  1806. return -EFAULT;
  1807. LOCKER(*s_hostname_lock);
  1808. if (s_hostname->length() + 1 > sizeof(utsname::nodename))
  1809. return -ENAMETOOLONG;
  1810. copy_to_user(buf->sysname, "SerenityOS", 11);
  1811. copy_to_user(buf->release, "1.0-dev", 8);
  1812. copy_to_user(buf->version, "FIXME", 6);
  1813. copy_to_user(buf->machine, "i686", 5);
  1814. copy_to_user(buf->nodename, s_hostname->characters(), s_hostname->length() + 1);
  1815. return 0;
  1816. }
  1817. KResult Process::do_kill(Process& process, int signal)
  1818. {
  1819. // FIXME: Allow sending SIGCONT to everyone in the process group.
  1820. // FIXME: Should setuid processes have some special treatment here?
  1821. if (!is_superuser() && m_euid != process.m_uid && m_uid != process.m_uid)
  1822. return KResult(-EPERM);
  1823. if (process.is_ring0() && signal == SIGKILL) {
  1824. kprintf("%s(%u) attempted to send SIGKILL to ring 0 process %s(%u)\n", name().characters(), m_pid, process.name().characters(), process.pid());
  1825. return KResult(-EPERM);
  1826. }
  1827. if (signal != 0)
  1828. process.send_signal(signal, this);
  1829. return KSuccess;
  1830. }
  1831. KResult Process::do_killpg(pid_t pgrp, int signal)
  1832. {
  1833. ASSERT(pgrp >= 0);
  1834. // Send the signal to all processes in the given group.
  1835. if (pgrp == 0) {
  1836. // Send the signal to our own pgrp.
  1837. pgrp = pgid();
  1838. }
  1839. bool group_was_empty = true;
  1840. bool any_succeeded = false;
  1841. KResult error = KSuccess;
  1842. Process::for_each_in_pgrp(pgrp, [&](auto& process) {
  1843. group_was_empty = false;
  1844. KResult res = do_kill(process, signal);
  1845. if (res.is_success())
  1846. any_succeeded = true;
  1847. else
  1848. error = res;
  1849. return IterationDecision::Continue;
  1850. });
  1851. if (group_was_empty)
  1852. return KResult(-ESRCH);
  1853. if (any_succeeded)
  1854. return KSuccess;
  1855. return error;
  1856. }
  1857. int Process::sys$kill(pid_t pid, int signal)
  1858. {
  1859. if (pid == m_pid)
  1860. REQUIRE_PROMISE(stdio);
  1861. else
  1862. REQUIRE_PROMISE(proc);
  1863. if (signal < 0 || signal >= 32)
  1864. return -EINVAL;
  1865. if (pid <= 0)
  1866. return do_killpg(-pid, signal);
  1867. if (pid == -1) {
  1868. // FIXME: Send to all processes.
  1869. return -ENOTIMPL;
  1870. }
  1871. if (pid == m_pid) {
  1872. if (signal == 0)
  1873. return 0;
  1874. if (!current->should_ignore_signal(signal)) {
  1875. current->send_signal(signal, this);
  1876. (void)current->block<Thread::SemiPermanentBlocker>(Thread::SemiPermanentBlocker::Reason::Signal);
  1877. }
  1878. return 0;
  1879. }
  1880. InterruptDisabler disabler;
  1881. auto* peer = Process::from_pid(pid);
  1882. if (!peer)
  1883. return -ESRCH;
  1884. return do_kill(*peer, signal);
  1885. }
  1886. int Process::sys$usleep(useconds_t usec)
  1887. {
  1888. REQUIRE_PROMISE(stdio);
  1889. if (!usec)
  1890. return 0;
  1891. u64 wakeup_time = current->sleep(usec / 1000);
  1892. if (wakeup_time > g_uptime)
  1893. return -EINTR;
  1894. return 0;
  1895. }
  1896. int Process::sys$sleep(unsigned seconds)
  1897. {
  1898. REQUIRE_PROMISE(stdio);
  1899. if (!seconds)
  1900. return 0;
  1901. u64 wakeup_time = current->sleep(seconds * TICKS_PER_SECOND);
  1902. if (wakeup_time > g_uptime) {
  1903. u32 ticks_left_until_original_wakeup_time = wakeup_time - g_uptime;
  1904. return ticks_left_until_original_wakeup_time / TICKS_PER_SECOND;
  1905. }
  1906. return 0;
  1907. }
  1908. timeval kgettimeofday()
  1909. {
  1910. return const_cast<const timeval&>(((KernelInfoPage*)s_info_page_address_for_kernel.as_ptr())->now);
  1911. }
  1912. void kgettimeofday(timeval& tv)
  1913. {
  1914. tv = kgettimeofday();
  1915. }
  1916. int Process::sys$gettimeofday(timeval* tv)
  1917. {
  1918. REQUIRE_PROMISE(stdio);
  1919. if (!validate_write_typed(tv))
  1920. return -EFAULT;
  1921. *tv = kgettimeofday();
  1922. return 0;
  1923. }
  1924. uid_t Process::sys$getuid()
  1925. {
  1926. REQUIRE_PROMISE(stdio);
  1927. return m_uid;
  1928. }
  1929. gid_t Process::sys$getgid()
  1930. {
  1931. REQUIRE_PROMISE(stdio);
  1932. return m_gid;
  1933. }
  1934. uid_t Process::sys$geteuid()
  1935. {
  1936. REQUIRE_PROMISE(stdio);
  1937. return m_euid;
  1938. }
  1939. gid_t Process::sys$getegid()
  1940. {
  1941. REQUIRE_PROMISE(stdio);
  1942. return m_egid;
  1943. }
  1944. pid_t Process::sys$getpid()
  1945. {
  1946. REQUIRE_PROMISE(stdio);
  1947. return m_pid;
  1948. }
  1949. pid_t Process::sys$getppid()
  1950. {
  1951. REQUIRE_PROMISE(stdio);
  1952. return m_ppid;
  1953. }
  1954. mode_t Process::sys$umask(mode_t mask)
  1955. {
  1956. REQUIRE_PROMISE(stdio);
  1957. auto old_mask = m_umask;
  1958. m_umask = mask & 0777;
  1959. return old_mask;
  1960. }
  1961. int Process::reap(Process& process)
  1962. {
  1963. int exit_status;
  1964. {
  1965. InterruptDisabler disabler;
  1966. exit_status = (process.m_termination_status << 8) | process.m_termination_signal;
  1967. if (process.ppid()) {
  1968. auto* parent = Process::from_pid(process.ppid());
  1969. if (parent) {
  1970. parent->m_ticks_in_user_for_dead_children += process.m_ticks_in_user + process.m_ticks_in_user_for_dead_children;
  1971. parent->m_ticks_in_kernel_for_dead_children += process.m_ticks_in_kernel + process.m_ticks_in_kernel_for_dead_children;
  1972. }
  1973. }
  1974. dbg() << "Reaping process " << process;
  1975. ASSERT(process.is_dead());
  1976. g_processes->remove(&process);
  1977. }
  1978. delete &process;
  1979. return exit_status;
  1980. }
  1981. pid_t Process::sys$waitpid(pid_t waitee, int* wstatus, int options)
  1982. {
  1983. REQUIRE_PROMISE(stdio);
  1984. dbg() << "sys$waitpid(" << waitee << ", " << wstatus << ", " << options << ")";
  1985. if (!options) {
  1986. // FIXME: This can't be right.. can it? Figure out how this should actually work.
  1987. options = WEXITED;
  1988. }
  1989. if (wstatus && !validate_write_typed(wstatus))
  1990. return -EFAULT;
  1991. int exit_status = 0;
  1992. {
  1993. InterruptDisabler disabler;
  1994. if (waitee != -1 && !Process::from_pid(waitee))
  1995. return -ECHILD;
  1996. }
  1997. if (options & WNOHANG) {
  1998. // FIXME: Figure out what WNOHANG should do with stopped children.
  1999. if (waitee == -1) {
  2000. pid_t reaped_pid = 0;
  2001. InterruptDisabler disabler;
  2002. for_each_child([&reaped_pid, &exit_status](Process& process) {
  2003. if (process.is_dead()) {
  2004. reaped_pid = process.pid();
  2005. exit_status = reap(process);
  2006. }
  2007. return IterationDecision::Continue;
  2008. });
  2009. return reaped_pid;
  2010. } else {
  2011. ASSERT(waitee > 0); // FIXME: Implement other PID specs.
  2012. InterruptDisabler disabler;
  2013. auto* waitee_process = Process::from_pid(waitee);
  2014. if (!waitee_process)
  2015. return -ECHILD;
  2016. if (waitee_process->is_dead()) {
  2017. exit_status = reap(*waitee_process);
  2018. return waitee;
  2019. }
  2020. return 0;
  2021. }
  2022. }
  2023. pid_t waitee_pid = waitee;
  2024. if (current->block<Thread::WaitBlocker>(options, waitee_pid) != Thread::BlockResult::WokeNormally)
  2025. return -EINTR;
  2026. InterruptDisabler disabler;
  2027. // NOTE: If waitee was -1, m_waitee_pid will have been filled in by the scheduler.
  2028. Process* waitee_process = Process::from_pid(waitee_pid);
  2029. if (!waitee_process)
  2030. return -ECHILD;
  2031. ASSERT(waitee_process);
  2032. if (waitee_process->is_dead()) {
  2033. exit_status = reap(*waitee_process);
  2034. } else {
  2035. auto* waitee_thread = Thread::from_tid(waitee_pid);
  2036. if (!waitee_thread)
  2037. return -ECHILD;
  2038. ASSERT(waitee_thread->state() == Thread::State::Stopped);
  2039. exit_status = (waitee_thread->m_stop_signal << 8) | 0x7f;
  2040. }
  2041. if (wstatus)
  2042. copy_to_user(wstatus, &exit_status);
  2043. return waitee_pid;
  2044. }
  2045. bool Process::validate_read_from_kernel(VirtualAddress vaddr, size_t size) const
  2046. {
  2047. if (vaddr.is_null())
  2048. return false;
  2049. return MM.validate_kernel_read(*this, vaddr, size);
  2050. }
  2051. bool Process::validate_read(const void* address, size_t size) const
  2052. {
  2053. if (!size)
  2054. return false;
  2055. return MM.validate_user_read(*this, VirtualAddress(address), size);
  2056. }
  2057. bool Process::validate_write(void* address, size_t size) const
  2058. {
  2059. if (!size)
  2060. return false;
  2061. return MM.validate_user_write(*this, VirtualAddress(address), size);
  2062. }
  2063. pid_t Process::sys$getsid(pid_t pid)
  2064. {
  2065. REQUIRE_PROMISE(stdio);
  2066. if (pid == 0)
  2067. return m_sid;
  2068. InterruptDisabler disabler;
  2069. auto* process = Process::from_pid(pid);
  2070. if (!process)
  2071. return -ESRCH;
  2072. if (m_sid != process->m_sid)
  2073. return -EPERM;
  2074. return process->m_sid;
  2075. }
  2076. pid_t Process::sys$setsid()
  2077. {
  2078. REQUIRE_PROMISE(proc);
  2079. InterruptDisabler disabler;
  2080. bool found_process_with_same_pgid_as_my_pid = false;
  2081. Process::for_each_in_pgrp(pid(), [&](auto&) {
  2082. found_process_with_same_pgid_as_my_pid = true;
  2083. return IterationDecision::Break;
  2084. });
  2085. if (found_process_with_same_pgid_as_my_pid)
  2086. return -EPERM;
  2087. m_sid = m_pid;
  2088. m_pgid = m_pid;
  2089. m_tty = nullptr;
  2090. return m_sid;
  2091. }
  2092. pid_t Process::sys$getpgid(pid_t pid)
  2093. {
  2094. REQUIRE_PROMISE(stdio);
  2095. if (pid == 0)
  2096. return m_pgid;
  2097. InterruptDisabler disabler; // FIXME: Use a ProcessHandle
  2098. auto* process = Process::from_pid(pid);
  2099. if (!process)
  2100. return -ESRCH;
  2101. return process->m_pgid;
  2102. }
  2103. pid_t Process::sys$getpgrp()
  2104. {
  2105. REQUIRE_PROMISE(stdio);
  2106. return m_pgid;
  2107. }
  2108. static pid_t get_sid_from_pgid(pid_t pgid)
  2109. {
  2110. InterruptDisabler disabler;
  2111. auto* group_leader = Process::from_pid(pgid);
  2112. if (!group_leader)
  2113. return -1;
  2114. return group_leader->sid();
  2115. }
  2116. int Process::sys$setpgid(pid_t specified_pid, pid_t specified_pgid)
  2117. {
  2118. REQUIRE_PROMISE(proc);
  2119. InterruptDisabler disabler; // FIXME: Use a ProcessHandle
  2120. pid_t pid = specified_pid ? specified_pid : m_pid;
  2121. if (specified_pgid < 0) {
  2122. // The value of the pgid argument is less than 0, or is not a value supported by the implementation.
  2123. return -EINVAL;
  2124. }
  2125. auto* process = Process::from_pid(pid);
  2126. if (!process)
  2127. return -ESRCH;
  2128. if (process != this && process->ppid() != m_pid) {
  2129. // The value of the pid argument does not match the process ID
  2130. // of the calling process or of a child process of the calling process.
  2131. return -ESRCH;
  2132. }
  2133. if (process->pid() == process->sid()) {
  2134. // The process indicated by the pid argument is a session leader.
  2135. return -EPERM;
  2136. }
  2137. if (process->ppid() == m_pid && process->sid() != sid()) {
  2138. // The value of the pid argument matches the process ID of a child
  2139. // process of the calling process and the child process is not in
  2140. // the same session as the calling process.
  2141. return -EPERM;
  2142. }
  2143. pid_t new_pgid = specified_pgid ? specified_pgid : process->m_pid;
  2144. pid_t current_sid = get_sid_from_pgid(process->m_pgid);
  2145. pid_t new_sid = get_sid_from_pgid(new_pgid);
  2146. if (current_sid != new_sid) {
  2147. // Can't move a process between sessions.
  2148. return -EPERM;
  2149. }
  2150. // FIXME: There are more EPERM conditions to check for here..
  2151. process->m_pgid = new_pgid;
  2152. return 0;
  2153. }
  2154. int Process::sys$ioctl(int fd, unsigned request, unsigned arg)
  2155. {
  2156. auto description = file_description(fd);
  2157. if (!description)
  2158. return -EBADF;
  2159. SmapDisabler disabler;
  2160. return description->file().ioctl(*description, request, arg);
  2161. }
  2162. int Process::sys$getdtablesize()
  2163. {
  2164. REQUIRE_PROMISE(stdio);
  2165. return m_max_open_file_descriptors;
  2166. }
  2167. int Process::sys$dup(int old_fd)
  2168. {
  2169. REQUIRE_PROMISE(stdio);
  2170. auto description = file_description(old_fd);
  2171. if (!description)
  2172. return -EBADF;
  2173. int new_fd = alloc_fd();
  2174. if (new_fd < 0)
  2175. return new_fd;
  2176. m_fds[new_fd].set(*description);
  2177. return new_fd;
  2178. }
  2179. int Process::sys$dup2(int old_fd, int new_fd)
  2180. {
  2181. REQUIRE_PROMISE(stdio);
  2182. auto description = file_description(old_fd);
  2183. if (!description)
  2184. return -EBADF;
  2185. if (new_fd < 0 || new_fd >= m_max_open_file_descriptors)
  2186. return -EINVAL;
  2187. m_fds[new_fd].set(*description);
  2188. return new_fd;
  2189. }
  2190. int Process::sys$sigprocmask(int how, const sigset_t* set, sigset_t* old_set)
  2191. {
  2192. REQUIRE_PROMISE(stdio);
  2193. if (old_set) {
  2194. if (!validate_write_typed(old_set))
  2195. return -EFAULT;
  2196. copy_to_user(old_set, &current->m_signal_mask);
  2197. }
  2198. if (set) {
  2199. if (!validate_read_typed(set))
  2200. return -EFAULT;
  2201. sigset_t set_value;
  2202. copy_from_user(&set_value, set);
  2203. switch (how) {
  2204. case SIG_BLOCK:
  2205. current->m_signal_mask &= ~set_value;
  2206. break;
  2207. case SIG_UNBLOCK:
  2208. current->m_signal_mask |= set_value;
  2209. break;
  2210. case SIG_SETMASK:
  2211. current->m_signal_mask = set_value;
  2212. break;
  2213. default:
  2214. return -EINVAL;
  2215. }
  2216. }
  2217. return 0;
  2218. }
  2219. int Process::sys$sigpending(sigset_t* set)
  2220. {
  2221. REQUIRE_PROMISE(stdio);
  2222. if (!validate_write_typed(set))
  2223. return -EFAULT;
  2224. copy_to_user(set, &current->m_pending_signals);
  2225. return 0;
  2226. }
  2227. int Process::sys$sigaction(int signum, const sigaction* act, sigaction* old_act)
  2228. {
  2229. REQUIRE_PROMISE(stdio);
  2230. if (signum < 1 || signum >= 32 || signum == SIGKILL || signum == SIGSTOP)
  2231. return -EINVAL;
  2232. if (!validate_read_typed(act))
  2233. return -EFAULT;
  2234. InterruptDisabler disabler; // FIXME: This should use a narrower lock. Maybe a way to ignore signals temporarily?
  2235. auto& action = current->m_signal_action_data[signum];
  2236. if (old_act) {
  2237. if (!validate_write_typed(old_act))
  2238. return -EFAULT;
  2239. copy_to_user(&old_act->sa_flags, &action.flags);
  2240. copy_to_user(&old_act->sa_sigaction, &action.handler_or_sigaction, sizeof(action.handler_or_sigaction));
  2241. }
  2242. copy_from_user(&action.flags, &act->sa_flags);
  2243. copy_from_user(&action.handler_or_sigaction, &act->sa_sigaction, sizeof(action.handler_or_sigaction));
  2244. return 0;
  2245. }
  2246. int Process::sys$getgroups(ssize_t count, gid_t* gids)
  2247. {
  2248. REQUIRE_PROMISE(stdio);
  2249. if (count < 0)
  2250. return -EINVAL;
  2251. if (!count)
  2252. return m_extra_gids.size();
  2253. if (count != (int)m_extra_gids.size())
  2254. return -EINVAL;
  2255. if (!validate_write_typed(gids, m_extra_gids.size()))
  2256. return -EFAULT;
  2257. size_t i = 0;
  2258. SmapDisabler disabler;
  2259. for (auto gid : m_extra_gids)
  2260. gids[i++] = gid;
  2261. return 0;
  2262. }
  2263. int Process::sys$setgroups(ssize_t count, const gid_t* gids)
  2264. {
  2265. REQUIRE_PROMISE(id);
  2266. if (count < 0)
  2267. return -EINVAL;
  2268. if (!is_superuser())
  2269. return -EPERM;
  2270. if (count && !validate_read(gids, count))
  2271. return -EFAULT;
  2272. m_extra_gids.clear();
  2273. SmapDisabler disabler;
  2274. for (int i = 0; i < count; ++i) {
  2275. if (gids[i] == m_gid)
  2276. continue;
  2277. m_extra_gids.set(gids[i]);
  2278. }
  2279. return 0;
  2280. }
  2281. int Process::sys$mkdir(const char* user_path, size_t path_length, mode_t mode)
  2282. {
  2283. REQUIRE_PROMISE(cpath);
  2284. auto path = get_syscall_path_argument(user_path, path_length);
  2285. if (path.is_error())
  2286. return path.error();
  2287. return VFS::the().mkdir(path.value(), mode & ~umask(), current_directory());
  2288. }
  2289. int Process::sys$realpath(const Syscall::SC_realpath_params* user_params)
  2290. {
  2291. REQUIRE_PROMISE(rpath);
  2292. Syscall::SC_realpath_params params;
  2293. if (!validate_read_and_copy_typed(&params, user_params))
  2294. return -EFAULT;
  2295. if (!validate_write(params.buffer.data, params.buffer.size))
  2296. return -EFAULT;
  2297. auto path = get_syscall_path_argument(params.path);
  2298. if (path.is_error())
  2299. return path.error();
  2300. auto custody_or_error = VFS::the().resolve_path(path.value(), current_directory());
  2301. if (custody_or_error.is_error())
  2302. return custody_or_error.error();
  2303. auto& custody = custody_or_error.value();
  2304. auto absolute_path = custody->absolute_path();
  2305. if (absolute_path.length() + 1 > params.buffer.size)
  2306. return -ENAMETOOLONG;
  2307. copy_to_user(params.buffer.data, absolute_path.characters(), absolute_path.length() + 1);
  2308. return 0;
  2309. };
  2310. clock_t Process::sys$times(tms* times)
  2311. {
  2312. REQUIRE_PROMISE(stdio);
  2313. if (!validate_write_typed(times))
  2314. return -EFAULT;
  2315. copy_to_user(&times->tms_utime, &m_ticks_in_user);
  2316. copy_to_user(&times->tms_stime, &m_ticks_in_kernel);
  2317. copy_to_user(&times->tms_cutime, &m_ticks_in_user_for_dead_children);
  2318. copy_to_user(&times->tms_cstime, &m_ticks_in_kernel_for_dead_children);
  2319. return g_uptime & 0x7fffffff;
  2320. }
  2321. int Process::sys$select(const Syscall::SC_select_params* params)
  2322. {
  2323. REQUIRE_PROMISE(stdio);
  2324. // FIXME: Return -EINVAL if timeout is invalid.
  2325. if (!validate_read_typed(params))
  2326. return -EFAULT;
  2327. SmapDisabler disabler;
  2328. int nfds = params->nfds;
  2329. fd_set* readfds = params->readfds;
  2330. fd_set* writefds = params->writefds;
  2331. fd_set* exceptfds = params->exceptfds;
  2332. timeval* timeout = params->timeout;
  2333. if (writefds && !validate_write_typed(writefds))
  2334. return -EFAULT;
  2335. if (readfds && !validate_write_typed(readfds))
  2336. return -EFAULT;
  2337. if (exceptfds && !validate_write_typed(exceptfds))
  2338. return -EFAULT;
  2339. if (timeout && !validate_read_typed(timeout))
  2340. return -EFAULT;
  2341. if (nfds < 0)
  2342. return -EINVAL;
  2343. timeval computed_timeout;
  2344. bool select_has_timeout = false;
  2345. if (timeout && (timeout->tv_sec || timeout->tv_usec)) {
  2346. timeval_add(kgettimeofday(), *timeout, computed_timeout);
  2347. select_has_timeout = true;
  2348. }
  2349. Thread::SelectBlocker::FDVector rfds;
  2350. Thread::SelectBlocker::FDVector wfds;
  2351. Thread::SelectBlocker::FDVector efds;
  2352. auto transfer_fds = [&](auto* fds, auto& vector) -> int {
  2353. vector.clear_with_capacity();
  2354. if (!fds)
  2355. return 0;
  2356. for (int fd = 0; fd < nfds; ++fd) {
  2357. if (FD_ISSET(fd, fds)) {
  2358. if (!file_description(fd)) {
  2359. dbg() << "sys$select: Bad fd number " << fd;
  2360. return -EBADF;
  2361. }
  2362. vector.append(fd);
  2363. }
  2364. }
  2365. return 0;
  2366. };
  2367. if (int error = transfer_fds(writefds, wfds))
  2368. return error;
  2369. if (int error = transfer_fds(readfds, rfds))
  2370. return error;
  2371. if (int error = transfer_fds(exceptfds, efds))
  2372. return error;
  2373. #if defined(DEBUG_IO) || defined(DEBUG_POLL_SELECT)
  2374. dbgprintf("%s<%u> selecting on (read:%u, write:%u), timeout=%p\n", name().characters(), pid(), rfds.size(), wfds.size(), timeout);
  2375. #endif
  2376. if (!timeout || select_has_timeout) {
  2377. if (current->block<Thread::SelectBlocker>(computed_timeout, select_has_timeout, rfds, wfds, efds) != Thread::BlockResult::WokeNormally)
  2378. return -EINTR;
  2379. }
  2380. int marked_fd_count = 0;
  2381. auto mark_fds = [&](auto* fds, auto& vector, auto should_mark) {
  2382. if (!fds)
  2383. return;
  2384. FD_ZERO(fds);
  2385. for (int fd : vector) {
  2386. if (auto description = file_description(fd); description && should_mark(*description)) {
  2387. FD_SET(fd, fds);
  2388. ++marked_fd_count;
  2389. }
  2390. }
  2391. };
  2392. mark_fds(readfds, rfds, [](auto& description) { return description.can_read(); });
  2393. mark_fds(writefds, wfds, [](auto& description) { return description.can_write(); });
  2394. // FIXME: We should also mark exceptfds as appropriate.
  2395. return marked_fd_count;
  2396. }
  2397. int Process::sys$poll(pollfd* fds, int nfds, int timeout)
  2398. {
  2399. REQUIRE_PROMISE(stdio);
  2400. if (!validate_read_typed(fds))
  2401. return -EFAULT;
  2402. SmapDisabler disabler;
  2403. Thread::SelectBlocker::FDVector rfds;
  2404. Thread::SelectBlocker::FDVector wfds;
  2405. for (int i = 0; i < nfds; ++i) {
  2406. if (fds[i].events & POLLIN)
  2407. rfds.append(fds[i].fd);
  2408. if (fds[i].events & POLLOUT)
  2409. wfds.append(fds[i].fd);
  2410. }
  2411. timeval actual_timeout;
  2412. bool has_timeout = false;
  2413. if (timeout >= 0) {
  2414. // poll is in ms, we want s/us.
  2415. struct timeval tvtimeout;
  2416. tvtimeout.tv_sec = 0;
  2417. while (timeout >= 1000) {
  2418. tvtimeout.tv_sec += 1;
  2419. timeout -= 1000;
  2420. }
  2421. tvtimeout.tv_usec = timeout * 1000;
  2422. timeval_add(kgettimeofday(), tvtimeout, actual_timeout);
  2423. has_timeout = true;
  2424. }
  2425. #if defined(DEBUG_IO) || defined(DEBUG_POLL_SELECT)
  2426. dbgprintf("%s<%u> polling on (read:%u, write:%u), timeout=%d\n", name().characters(), pid(), rfds.size(), wfds.size(), timeout);
  2427. #endif
  2428. if (has_timeout || timeout < 0) {
  2429. if (current->block<Thread::SelectBlocker>(actual_timeout, has_timeout, rfds, wfds, Thread::SelectBlocker::FDVector()) != Thread::BlockResult::WokeNormally)
  2430. return -EINTR;
  2431. }
  2432. int fds_with_revents = 0;
  2433. for (int i = 0; i < nfds; ++i) {
  2434. auto description = file_description(fds[i].fd);
  2435. if (!description) {
  2436. fds[i].revents = POLLNVAL;
  2437. continue;
  2438. }
  2439. fds[i].revents = 0;
  2440. if (fds[i].events & POLLIN && description->can_read())
  2441. fds[i].revents |= POLLIN;
  2442. if (fds[i].events & POLLOUT && description->can_write())
  2443. fds[i].revents |= POLLOUT;
  2444. if (fds[i].revents)
  2445. ++fds_with_revents;
  2446. }
  2447. return fds_with_revents;
  2448. }
  2449. Custody& Process::current_directory()
  2450. {
  2451. if (!m_cwd)
  2452. m_cwd = VFS::the().root_custody();
  2453. return *m_cwd;
  2454. }
  2455. int Process::sys$link(const Syscall::SC_link_params* user_params)
  2456. {
  2457. REQUIRE_PROMISE(cpath);
  2458. Syscall::SC_link_params params;
  2459. if (!validate_read_and_copy_typed(&params, user_params))
  2460. return -EFAULT;
  2461. auto old_path = validate_and_copy_string_from_user(params.old_path);
  2462. auto new_path = validate_and_copy_string_from_user(params.new_path);
  2463. if (old_path.is_null() || new_path.is_null())
  2464. return -EFAULT;
  2465. return VFS::the().link(old_path, new_path, current_directory());
  2466. }
  2467. int Process::sys$unlink(const char* user_path, size_t path_length)
  2468. {
  2469. REQUIRE_PROMISE(cpath);
  2470. if (!validate_read(user_path, path_length))
  2471. return -EFAULT;
  2472. auto path = get_syscall_path_argument(user_path, path_length);
  2473. if (path.is_error())
  2474. return path.error();
  2475. return VFS::the().unlink(path.value(), current_directory());
  2476. }
  2477. int Process::sys$symlink(const Syscall::SC_symlink_params* user_params)
  2478. {
  2479. REQUIRE_PROMISE(cpath);
  2480. Syscall::SC_symlink_params params;
  2481. if (!validate_read_and_copy_typed(&params, user_params))
  2482. return -EFAULT;
  2483. auto target = get_syscall_path_argument(params.target);
  2484. if (target.is_error())
  2485. return target.error();
  2486. auto linkpath = get_syscall_path_argument(params.linkpath);
  2487. if (linkpath.is_error())
  2488. return linkpath.error();
  2489. return VFS::the().symlink(target.value(), linkpath.value(), current_directory());
  2490. }
  2491. KResultOr<String> Process::get_syscall_path_argument(const char* user_path, size_t path_length) const
  2492. {
  2493. if (path_length == 0)
  2494. return KResult(-EINVAL);
  2495. if (path_length > PATH_MAX)
  2496. return KResult(-ENAMETOOLONG);
  2497. if (!validate_read(user_path, path_length))
  2498. return KResult(-EFAULT);
  2499. return copy_string_from_user(user_path, path_length);
  2500. }
  2501. KResultOr<String> Process::get_syscall_path_argument(const Syscall::StringArgument& path) const
  2502. {
  2503. return get_syscall_path_argument(path.characters, path.length);
  2504. }
  2505. int Process::sys$rmdir(const char* user_path, size_t path_length)
  2506. {
  2507. REQUIRE_PROMISE(cpath);
  2508. auto path = get_syscall_path_argument(user_path, path_length);
  2509. if (path.is_error())
  2510. return path.error();
  2511. return VFS::the().rmdir(path.value(), current_directory());
  2512. }
  2513. int Process::sys$chmod(const char* user_path, size_t path_length, mode_t mode)
  2514. {
  2515. REQUIRE_PROMISE(fattr);
  2516. auto path = get_syscall_path_argument(user_path, path_length);
  2517. if (path.is_error())
  2518. return path.error();
  2519. return VFS::the().chmod(path.value(), mode, current_directory());
  2520. }
  2521. int Process::sys$fchmod(int fd, mode_t mode)
  2522. {
  2523. REQUIRE_PROMISE(fattr);
  2524. auto description = file_description(fd);
  2525. if (!description)
  2526. return -EBADF;
  2527. return description->chmod(mode);
  2528. }
  2529. int Process::sys$fchown(int fd, uid_t uid, gid_t gid)
  2530. {
  2531. REQUIRE_PROMISE(chown);
  2532. auto description = file_description(fd);
  2533. if (!description)
  2534. return -EBADF;
  2535. return description->chown(uid, gid);
  2536. }
  2537. int Process::sys$chown(const Syscall::SC_chown_params* user_params)
  2538. {
  2539. REQUIRE_PROMISE(chown);
  2540. Syscall::SC_chown_params params;
  2541. if (!validate_read_and_copy_typed(&params, user_params))
  2542. return -EFAULT;
  2543. auto path = get_syscall_path_argument(params.path);
  2544. if (path.is_error())
  2545. return path.error();
  2546. return VFS::the().chown(path.value(), params.uid, params.gid, current_directory());
  2547. }
  2548. void Process::finalize()
  2549. {
  2550. ASSERT(current == g_finalizer);
  2551. dbg() << "Finalizing process " << *this;
  2552. m_fds.clear();
  2553. m_tty = nullptr;
  2554. m_executable = nullptr;
  2555. m_cwd = nullptr;
  2556. m_root_directory = nullptr;
  2557. m_root_directory_relative_to_global_root = nullptr;
  2558. m_elf_loader = nullptr;
  2559. disown_all_shared_buffers();
  2560. {
  2561. InterruptDisabler disabler;
  2562. if (auto* parent_thread = Thread::from_tid(m_ppid)) {
  2563. if (parent_thread->m_signal_action_data[SIGCHLD].flags & SA_NOCLDWAIT) {
  2564. // NOTE: If the parent doesn't care about this process, let it go.
  2565. m_ppid = 0;
  2566. } else {
  2567. parent_thread->send_signal(SIGCHLD, this);
  2568. }
  2569. }
  2570. }
  2571. m_dead = true;
  2572. }
  2573. void Process::die()
  2574. {
  2575. // Let go of the TTY, otherwise a slave PTY may keep the master PTY from
  2576. // getting an EOF when the last process using the slave PTY dies.
  2577. // If the master PTY owner relies on an EOF to know when to wait() on a
  2578. // slave owner, we have to allow the PTY pair to be torn down.
  2579. m_tty = nullptr;
  2580. if (m_tracer)
  2581. m_tracer->set_dead();
  2582. {
  2583. // Tell the threads to unwind and die.
  2584. InterruptDisabler disabler;
  2585. for_each_thread([](Thread& thread) {
  2586. thread.set_should_die();
  2587. return IterationDecision::Continue;
  2588. });
  2589. }
  2590. }
  2591. size_t Process::amount_dirty_private() const
  2592. {
  2593. // FIXME: This gets a bit more complicated for Regions sharing the same underlying VMObject.
  2594. // The main issue I'm thinking of is when the VMObject has physical pages that none of the Regions are mapping.
  2595. // That's probably a situation that needs to be looked at in general.
  2596. size_t amount = 0;
  2597. for (auto& region : m_regions) {
  2598. if (!region.is_shared())
  2599. amount += region.amount_dirty();
  2600. }
  2601. return amount;
  2602. }
  2603. size_t Process::amount_clean_inode() const
  2604. {
  2605. HashTable<const InodeVMObject*> vmobjects;
  2606. for (auto& region : m_regions) {
  2607. if (region.vmobject().is_inode())
  2608. vmobjects.set(&static_cast<const InodeVMObject&>(region.vmobject()));
  2609. }
  2610. size_t amount = 0;
  2611. for (auto& vmobject : vmobjects)
  2612. amount += vmobject->amount_clean();
  2613. return amount;
  2614. }
  2615. size_t Process::amount_virtual() const
  2616. {
  2617. size_t amount = 0;
  2618. for (auto& region : m_regions) {
  2619. amount += region.size();
  2620. }
  2621. return amount;
  2622. }
  2623. size_t Process::amount_resident() const
  2624. {
  2625. // FIXME: This will double count if multiple regions use the same physical page.
  2626. size_t amount = 0;
  2627. for (auto& region : m_regions) {
  2628. amount += region.amount_resident();
  2629. }
  2630. return amount;
  2631. }
  2632. size_t Process::amount_shared() const
  2633. {
  2634. // FIXME: This will double count if multiple regions use the same physical page.
  2635. // FIXME: It doesn't work at the moment, since it relies on PhysicalPage ref counts,
  2636. // and each PhysicalPage is only reffed by its VMObject. This needs to be refactored
  2637. // so that every Region contributes +1 ref to each of its PhysicalPages.
  2638. size_t amount = 0;
  2639. for (auto& region : m_regions) {
  2640. amount += region.amount_shared();
  2641. }
  2642. return amount;
  2643. }
  2644. size_t Process::amount_purgeable_volatile() const
  2645. {
  2646. size_t amount = 0;
  2647. for (auto& region : m_regions) {
  2648. if (region.vmobject().is_purgeable() && static_cast<const PurgeableVMObject&>(region.vmobject()).is_volatile())
  2649. amount += region.amount_resident();
  2650. }
  2651. return amount;
  2652. }
  2653. size_t Process::amount_purgeable_nonvolatile() const
  2654. {
  2655. size_t amount = 0;
  2656. for (auto& region : m_regions) {
  2657. if (region.vmobject().is_purgeable() && !static_cast<const PurgeableVMObject&>(region.vmobject()).is_volatile())
  2658. amount += region.amount_resident();
  2659. }
  2660. return amount;
  2661. }
  2662. #define REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(domain) \
  2663. do { \
  2664. if (domain == AF_INET) \
  2665. REQUIRE_PROMISE(inet); \
  2666. else if (domain == AF_LOCAL) \
  2667. REQUIRE_PROMISE(unix); \
  2668. } while (0)
  2669. int Process::sys$socket(int domain, int type, int protocol)
  2670. {
  2671. REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(domain);
  2672. if ((type & SOCK_TYPE_MASK) == SOCK_RAW && !is_superuser())
  2673. return -EACCES;
  2674. int fd = alloc_fd();
  2675. if (fd < 0)
  2676. return fd;
  2677. auto result = Socket::create(domain, type, protocol);
  2678. if (result.is_error())
  2679. return result.error();
  2680. auto description = FileDescription::create(*result.value());
  2681. description->set_readable(true);
  2682. description->set_writable(true);
  2683. unsigned flags = 0;
  2684. if (type & SOCK_CLOEXEC)
  2685. flags |= FD_CLOEXEC;
  2686. if (type & SOCK_NONBLOCK)
  2687. description->set_blocking(false);
  2688. m_fds[fd].set(move(description), flags);
  2689. return fd;
  2690. }
  2691. int Process::sys$bind(int sockfd, const sockaddr* address, socklen_t address_length)
  2692. {
  2693. if (!validate_read(address, address_length))
  2694. return -EFAULT;
  2695. auto description = file_description(sockfd);
  2696. if (!description)
  2697. return -EBADF;
  2698. if (!description->is_socket())
  2699. return -ENOTSOCK;
  2700. auto& socket = *description->socket();
  2701. REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(socket.domain());
  2702. return socket.bind(address, address_length);
  2703. }
  2704. int Process::sys$listen(int sockfd, int backlog)
  2705. {
  2706. auto description = file_description(sockfd);
  2707. if (!description)
  2708. return -EBADF;
  2709. if (!description->is_socket())
  2710. return -ENOTSOCK;
  2711. auto& socket = *description->socket();
  2712. REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(socket.domain());
  2713. if (socket.is_connected())
  2714. return -EINVAL;
  2715. return socket.listen(backlog);
  2716. }
  2717. int Process::sys$accept(int accepting_socket_fd, sockaddr* address, socklen_t* address_size)
  2718. {
  2719. REQUIRE_PROMISE(accept);
  2720. if (!validate_write_typed(address_size))
  2721. return -EFAULT;
  2722. SmapDisabler disabler;
  2723. if (!validate_write(address, *address_size))
  2724. return -EFAULT;
  2725. int accepted_socket_fd = alloc_fd();
  2726. if (accepted_socket_fd < 0)
  2727. return accepted_socket_fd;
  2728. auto accepting_socket_description = file_description(accepting_socket_fd);
  2729. if (!accepting_socket_description)
  2730. return -EBADF;
  2731. if (!accepting_socket_description->is_socket())
  2732. return -ENOTSOCK;
  2733. auto& socket = *accepting_socket_description->socket();
  2734. if (!socket.can_accept()) {
  2735. if (accepting_socket_description->is_blocking()) {
  2736. if (current->block<Thread::AcceptBlocker>(*accepting_socket_description) != Thread::BlockResult::WokeNormally)
  2737. return -EINTR;
  2738. } else {
  2739. return -EAGAIN;
  2740. }
  2741. }
  2742. auto accepted_socket = socket.accept();
  2743. ASSERT(accepted_socket);
  2744. bool success = accepted_socket->get_peer_address(address, address_size);
  2745. ASSERT(success);
  2746. auto accepted_socket_description = FileDescription::create(*accepted_socket);
  2747. accepted_socket_description->set_readable(true);
  2748. accepted_socket_description->set_writable(true);
  2749. // NOTE: The accepted socket inherits fd flags from the accepting socket.
  2750. // I'm not sure if this matches other systems but it makes sense to me.
  2751. accepted_socket_description->set_blocking(accepting_socket_description->is_blocking());
  2752. m_fds[accepted_socket_fd].set(move(accepted_socket_description), m_fds[accepting_socket_fd].flags);
  2753. // NOTE: Moving this state to Completed is what causes connect() to unblock on the client side.
  2754. accepted_socket->set_setup_state(Socket::SetupState::Completed);
  2755. return accepted_socket_fd;
  2756. }
  2757. int Process::sys$connect(int sockfd, const sockaddr* address, socklen_t address_size)
  2758. {
  2759. if (!validate_read(address, address_size))
  2760. return -EFAULT;
  2761. int fd = alloc_fd();
  2762. if (fd < 0)
  2763. return fd;
  2764. auto description = file_description(sockfd);
  2765. if (!description)
  2766. return -EBADF;
  2767. if (!description->is_socket())
  2768. return -ENOTSOCK;
  2769. auto& socket = *description->socket();
  2770. REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(socket.domain());
  2771. SmapDisabler disabler;
  2772. return socket.connect(*description, address, address_size, description->is_blocking() ? ShouldBlock::Yes : ShouldBlock::No);
  2773. }
  2774. ssize_t Process::sys$sendto(const Syscall::SC_sendto_params* user_params)
  2775. {
  2776. REQUIRE_PROMISE(stdio);
  2777. Syscall::SC_sendto_params params;
  2778. if (!validate_read_and_copy_typed(&params, user_params))
  2779. return -EFAULT;
  2780. int flags = params.flags;
  2781. const sockaddr* addr = params.addr;
  2782. socklen_t addr_length = params.addr_length;
  2783. if (!validate(params.data))
  2784. return -EFAULT;
  2785. if (addr && !validate_read(addr, addr_length))
  2786. return -EFAULT;
  2787. auto description = file_description(params.sockfd);
  2788. if (!description)
  2789. return -EBADF;
  2790. if (!description->is_socket())
  2791. return -ENOTSOCK;
  2792. SmapDisabler disabler;
  2793. auto& socket = *description->socket();
  2794. return socket.sendto(*description, params.data.data, params.data.size, flags, addr, addr_length);
  2795. }
  2796. ssize_t Process::sys$recvfrom(const Syscall::SC_recvfrom_params* user_params)
  2797. {
  2798. REQUIRE_PROMISE(stdio);
  2799. Syscall::SC_recvfrom_params params;
  2800. if (!validate_read_and_copy_typed(&params, user_params))
  2801. return -EFAULT;
  2802. int flags = params.flags;
  2803. sockaddr* addr = params.addr;
  2804. socklen_t* addr_length = params.addr_length;
  2805. SmapDisabler disabler;
  2806. if (!validate(params.buffer))
  2807. return -EFAULT;
  2808. if (addr_length) {
  2809. if (!validate_write_typed(addr_length))
  2810. return -EFAULT;
  2811. if (!validate_write(addr, *addr_length))
  2812. return -EFAULT;
  2813. } else if (addr) {
  2814. return -EINVAL;
  2815. }
  2816. auto description = file_description(params.sockfd);
  2817. if (!description)
  2818. return -EBADF;
  2819. if (!description->is_socket())
  2820. return -ENOTSOCK;
  2821. auto& socket = *description->socket();
  2822. bool original_blocking = description->is_blocking();
  2823. if (flags & MSG_DONTWAIT)
  2824. description->set_blocking(false);
  2825. auto nrecv = socket.recvfrom(*description, params.buffer.data, params.buffer.size, flags, addr, addr_length);
  2826. if (flags & MSG_DONTWAIT)
  2827. description->set_blocking(original_blocking);
  2828. return nrecv;
  2829. }
  2830. template<bool sockname, typename Params>
  2831. int Process::get_sock_or_peer_name(const Params& params)
  2832. {
  2833. socklen_t addrlen_value;
  2834. if (!validate_read_and_copy_typed(&addrlen_value, params.addrlen))
  2835. return -EFAULT;
  2836. if (addrlen_value <= 0)
  2837. return -EINVAL;
  2838. if (!validate_write(params.addr, addrlen_value))
  2839. return -EFAULT;
  2840. if (!validate_write_typed(params.addrlen))
  2841. return -EFAULT;
  2842. auto description = file_description(params.sockfd);
  2843. if (!description)
  2844. return -EBADF;
  2845. if (!description->is_socket())
  2846. return -ENOTSOCK;
  2847. auto& socket = *description->socket();
  2848. REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(socket.domain());
  2849. u8 address_buffer[sizeof(sockaddr_un)];
  2850. addrlen_value = min(sizeof(sockaddr_un), static_cast<size_t>(addrlen_value));
  2851. if (!socket.get_local_address((sockaddr*)address_buffer, &addrlen_value))
  2852. return -EINVAL;
  2853. copy_to_user(params.addr, address_buffer, addrlen_value);
  2854. return 0;
  2855. }
  2856. int Process::sys$getsockname(const Syscall::SC_getsockname_params* user_params)
  2857. {
  2858. Syscall::SC_getsockname_params params;
  2859. if (!validate_read_and_copy_typed(&params, user_params))
  2860. return -EFAULT;
  2861. return get_sock_or_peer_name<true>(params);
  2862. }
  2863. int Process::sys$getpeername(const Syscall::SC_getpeername_params* user_params)
  2864. {
  2865. Syscall::SC_getpeername_params params;
  2866. if (!validate_read_and_copy_typed(&params, user_params))
  2867. return -EFAULT;
  2868. return get_sock_or_peer_name<false>(params);
  2869. }
  2870. int Process::sys$sched_setparam(int tid, const struct sched_param* param)
  2871. {
  2872. REQUIRE_PROMISE(proc);
  2873. if (!validate_read_typed(param))
  2874. return -EFAULT;
  2875. int desired_priority;
  2876. copy_from_user(&desired_priority, &param->sched_priority);
  2877. InterruptDisabler disabler;
  2878. auto* peer = current;
  2879. if (tid != 0)
  2880. peer = Thread::from_tid(tid);
  2881. if (!peer)
  2882. return -ESRCH;
  2883. if (!is_superuser() && m_euid != peer->process().m_uid && m_uid != peer->process().m_uid)
  2884. return -EPERM;
  2885. if (desired_priority < THREAD_PRIORITY_MIN || desired_priority > THREAD_PRIORITY_MAX)
  2886. return -EINVAL;
  2887. peer->set_priority((u32)desired_priority);
  2888. return 0;
  2889. }
  2890. int Process::sys$sched_getparam(pid_t pid, struct sched_param* param)
  2891. {
  2892. REQUIRE_PROMISE(proc);
  2893. if (!validate_write_typed(param))
  2894. return -EFAULT;
  2895. InterruptDisabler disabler;
  2896. auto* peer = current;
  2897. if (pid != 0)
  2898. peer = Thread::from_tid(pid);
  2899. if (!peer)
  2900. return -ESRCH;
  2901. if (!is_superuser() && m_euid != peer->process().m_uid && m_uid != peer->process().m_uid)
  2902. return -EPERM;
  2903. int priority = peer->priority();
  2904. copy_to_user(&param->sched_priority, &priority);
  2905. return 0;
  2906. }
  2907. int Process::sys$getsockopt(const Syscall::SC_getsockopt_params* params)
  2908. {
  2909. if (!validate_read_typed(params))
  2910. return -EFAULT;
  2911. SmapDisabler disabler;
  2912. int sockfd = params->sockfd;
  2913. int level = params->level;
  2914. int option = params->option;
  2915. void* value = params->value;
  2916. socklen_t* value_size = params->value_size;
  2917. if (!validate_write_typed(value_size))
  2918. return -EFAULT;
  2919. if (!validate_write(value, *value_size))
  2920. return -EFAULT;
  2921. auto description = file_description(sockfd);
  2922. if (!description)
  2923. return -EBADF;
  2924. if (!description->is_socket())
  2925. return -ENOTSOCK;
  2926. auto& socket = *description->socket();
  2927. if (has_promised(Pledge::accept) && socket.is_local() && level == SOL_SOCKET && option == SO_PEERCRED) {
  2928. // We make an exception for SOL_SOCKET::SO_PEERCRED on local sockets if you've pledged "accept"
  2929. } else {
  2930. REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(socket.domain());
  2931. }
  2932. return socket.getsockopt(*description, level, option, value, value_size);
  2933. }
  2934. int Process::sys$setsockopt(const Syscall::SC_setsockopt_params* params)
  2935. {
  2936. if (!validate_read_typed(params))
  2937. return -EFAULT;
  2938. SmapDisabler disabler;
  2939. int sockfd = params->sockfd;
  2940. int level = params->level;
  2941. int option = params->option;
  2942. const void* value = params->value;
  2943. socklen_t value_size = params->value_size;
  2944. if (!validate_read(value, value_size))
  2945. return -EFAULT;
  2946. auto description = file_description(sockfd);
  2947. if (!description)
  2948. return -EBADF;
  2949. if (!description->is_socket())
  2950. return -ENOTSOCK;
  2951. auto& socket = *description->socket();
  2952. REQUIRE_PROMISE_FOR_SOCKET_DOMAIN(socket.domain());
  2953. return socket.setsockopt(level, option, value, value_size);
  2954. }
  2955. void Process::disown_all_shared_buffers()
  2956. {
  2957. LOCKER(shared_buffers().lock());
  2958. Vector<SharedBuffer*, 32> buffers_to_disown;
  2959. for (auto& it : shared_buffers().resource())
  2960. buffers_to_disown.append(it.value.ptr());
  2961. for (auto* shared_buffer : buffers_to_disown)
  2962. shared_buffer->disown(m_pid);
  2963. }
  2964. int Process::sys$create_shared_buffer(int size, void** buffer)
  2965. {
  2966. REQUIRE_PROMISE(shared_buffer);
  2967. if (!size || size < 0)
  2968. return -EINVAL;
  2969. size = PAGE_ROUND_UP(size);
  2970. if (!validate_write_typed(buffer))
  2971. return -EFAULT;
  2972. LOCKER(shared_buffers().lock());
  2973. static int s_next_shared_buffer_id;
  2974. int shared_buffer_id = ++s_next_shared_buffer_id;
  2975. auto shared_buffer = make<SharedBuffer>(shared_buffer_id, size);
  2976. shared_buffer->share_with(m_pid);
  2977. void* address = shared_buffer->ref_for_process_and_get_address(*this);
  2978. {
  2979. SmapDisabler disabler;
  2980. *buffer = address;
  2981. }
  2982. ASSERT((int)shared_buffer->size() >= size);
  2983. #ifdef SHARED_BUFFER_DEBUG
  2984. kprintf("%s(%u): Created shared buffer %d @ %p (%u bytes, vmobject is %u)\n", name().characters(), pid(), shared_buffer_id, *buffer, size, shared_buffer->size());
  2985. #endif
  2986. shared_buffers().resource().set(shared_buffer_id, move(shared_buffer));
  2987. return shared_buffer_id;
  2988. }
  2989. int Process::sys$share_buffer_with(int shared_buffer_id, pid_t peer_pid)
  2990. {
  2991. REQUIRE_PROMISE(shared_buffer);
  2992. if (!peer_pid || peer_pid < 0 || peer_pid == m_pid)
  2993. return -EINVAL;
  2994. LOCKER(shared_buffers().lock());
  2995. auto it = shared_buffers().resource().find(shared_buffer_id);
  2996. if (it == shared_buffers().resource().end())
  2997. return -EINVAL;
  2998. auto& shared_buffer = *(*it).value;
  2999. if (!shared_buffer.is_shared_with(m_pid))
  3000. return -EPERM;
  3001. {
  3002. InterruptDisabler disabler;
  3003. auto* peer = Process::from_pid(peer_pid);
  3004. if (!peer)
  3005. return -ESRCH;
  3006. }
  3007. shared_buffer.share_with(peer_pid);
  3008. return 0;
  3009. }
  3010. int Process::sys$share_buffer_globally(int shared_buffer_id)
  3011. {
  3012. REQUIRE_PROMISE(shared_buffer);
  3013. LOCKER(shared_buffers().lock());
  3014. auto it = shared_buffers().resource().find(shared_buffer_id);
  3015. if (it == shared_buffers().resource().end())
  3016. return -EINVAL;
  3017. auto& shared_buffer = *(*it).value;
  3018. if (!shared_buffer.is_shared_with(m_pid))
  3019. return -EPERM;
  3020. shared_buffer.share_globally();
  3021. return 0;
  3022. }
  3023. int Process::sys$release_shared_buffer(int shared_buffer_id)
  3024. {
  3025. REQUIRE_PROMISE(shared_buffer);
  3026. LOCKER(shared_buffers().lock());
  3027. auto it = shared_buffers().resource().find(shared_buffer_id);
  3028. if (it == shared_buffers().resource().end())
  3029. return -EINVAL;
  3030. auto& shared_buffer = *(*it).value;
  3031. if (!shared_buffer.is_shared_with(m_pid))
  3032. return -EPERM;
  3033. #ifdef SHARED_BUFFER_DEBUG
  3034. kprintf("%s(%u): Releasing shared buffer %d, buffer count: %u\n", name().characters(), pid(), shared_buffer_id, shared_buffers().resource().size());
  3035. #endif
  3036. shared_buffer.deref_for_process(*this);
  3037. return 0;
  3038. }
  3039. void* Process::sys$get_shared_buffer(int shared_buffer_id)
  3040. {
  3041. REQUIRE_PROMISE(shared_buffer);
  3042. LOCKER(shared_buffers().lock());
  3043. auto it = shared_buffers().resource().find(shared_buffer_id);
  3044. if (it == shared_buffers().resource().end())
  3045. return (void*)-EINVAL;
  3046. auto& shared_buffer = *(*it).value;
  3047. if (!shared_buffer.is_shared_with(m_pid))
  3048. return (void*)-EPERM;
  3049. #ifdef SHARED_BUFFER_DEBUG
  3050. kprintf("%s(%u): Retaining shared buffer %d, buffer count: %u\n", name().characters(), pid(), shared_buffer_id, shared_buffers().resource().size());
  3051. #endif
  3052. return shared_buffer.ref_for_process_and_get_address(*this);
  3053. }
  3054. int Process::sys$seal_shared_buffer(int shared_buffer_id)
  3055. {
  3056. REQUIRE_PROMISE(shared_buffer);
  3057. LOCKER(shared_buffers().lock());
  3058. auto it = shared_buffers().resource().find(shared_buffer_id);
  3059. if (it == shared_buffers().resource().end())
  3060. return -EINVAL;
  3061. auto& shared_buffer = *(*it).value;
  3062. if (!shared_buffer.is_shared_with(m_pid))
  3063. return -EPERM;
  3064. #ifdef SHARED_BUFFER_DEBUG
  3065. kprintf("%s(%u): Sealing shared buffer %d\n", name().characters(), pid(), shared_buffer_id);
  3066. #endif
  3067. shared_buffer.seal();
  3068. return 0;
  3069. }
  3070. int Process::sys$get_shared_buffer_size(int shared_buffer_id)
  3071. {
  3072. REQUIRE_PROMISE(shared_buffer);
  3073. LOCKER(shared_buffers().lock());
  3074. auto it = shared_buffers().resource().find(shared_buffer_id);
  3075. if (it == shared_buffers().resource().end())
  3076. return -EINVAL;
  3077. auto& shared_buffer = *(*it).value;
  3078. if (!shared_buffer.is_shared_with(m_pid))
  3079. return -EPERM;
  3080. #ifdef SHARED_BUFFER_DEBUG
  3081. kprintf("%s(%u): Get shared buffer %d size: %u\n", name().characters(), pid(), shared_buffer_id, shared_buffers().resource().size());
  3082. #endif
  3083. return shared_buffer.size();
  3084. }
  3085. int Process::sys$set_shared_buffer_volatile(int shared_buffer_id, bool state)
  3086. {
  3087. REQUIRE_PROMISE(shared_buffer);
  3088. LOCKER(shared_buffers().lock());
  3089. auto it = shared_buffers().resource().find(shared_buffer_id);
  3090. if (it == shared_buffers().resource().end())
  3091. return -EINVAL;
  3092. auto& shared_buffer = *(*it).value;
  3093. if (!shared_buffer.is_shared_with(m_pid))
  3094. return -EPERM;
  3095. #ifdef SHARED_BUFFER_DEBUG
  3096. kprintf("%s(%u): Set shared buffer %d volatile: %u\n", name().characters(), pid(), shared_buffer_id, state);
  3097. #endif
  3098. if (!state) {
  3099. bool was_purged = shared_buffer.vmobject().was_purged();
  3100. shared_buffer.vmobject().set_volatile(state);
  3101. shared_buffer.vmobject().set_was_purged(false);
  3102. return was_purged ? 1 : 0;
  3103. }
  3104. shared_buffer.vmobject().set_volatile(true);
  3105. return 0;
  3106. }
  3107. void Process::terminate_due_to_signal(u8 signal)
  3108. {
  3109. ASSERT_INTERRUPTS_DISABLED();
  3110. ASSERT(signal < 32);
  3111. dbgprintf("terminate_due_to_signal %s(%u) <- %u\n", name().characters(), pid(), signal);
  3112. m_termination_status = 0;
  3113. m_termination_signal = signal;
  3114. die();
  3115. }
  3116. void Process::send_signal(u8 signal, Process* sender)
  3117. {
  3118. InterruptDisabler disabler;
  3119. auto* thread = Thread::from_tid(m_pid);
  3120. if (!thread)
  3121. thread = &any_thread();
  3122. thread->send_signal(signal, sender);
  3123. }
  3124. int Process::sys$create_thread(void* (*entry)(void*), void* argument, const Syscall::SC_create_thread_params* user_params)
  3125. {
  3126. REQUIRE_PROMISE(thread);
  3127. if (!validate_read((const void*)entry, sizeof(void*)))
  3128. return -EFAULT;
  3129. Syscall::SC_create_thread_params params;
  3130. if (!validate_read_and_copy_typed(&params, user_params))
  3131. return -EFAULT;
  3132. unsigned detach_state = params.m_detach_state;
  3133. int schedule_priority = params.m_schedule_priority;
  3134. void* stack_location = params.m_stack_location;
  3135. unsigned stack_size = params.m_stack_size;
  3136. if (!validate_write(stack_location, stack_size))
  3137. return -EFAULT;
  3138. u32 user_stack_address = reinterpret_cast<u32>(stack_location) + stack_size;
  3139. if (!MM.validate_user_stack(*this, VirtualAddress(user_stack_address - 4)))
  3140. return -EFAULT;
  3141. // FIXME: return EAGAIN if Thread::all_threads().size() is greater than PTHREAD_THREADS_MAX
  3142. int requested_thread_priority = schedule_priority;
  3143. if (requested_thread_priority < THREAD_PRIORITY_MIN || requested_thread_priority > THREAD_PRIORITY_MAX)
  3144. return -EINVAL;
  3145. bool is_thread_joinable = (0 == detach_state);
  3146. // FIXME: Do something with guard pages?
  3147. auto* thread = new Thread(*this);
  3148. // We know this thread is not the main_thread,
  3149. // So give it a unique name until the user calls $set_thread_name on it
  3150. // length + 4 to give space for our extra junk at the end
  3151. StringBuilder builder(m_name.length() + 4);
  3152. builder.append(m_name);
  3153. builder.appendf("[%d]", thread->tid());
  3154. thread->set_name(builder.to_string());
  3155. thread->set_priority(requested_thread_priority);
  3156. thread->set_joinable(is_thread_joinable);
  3157. auto& tss = thread->tss();
  3158. tss.eip = (uintptr_t)entry;
  3159. tss.eflags = 0x0202;
  3160. tss.cr3 = page_directory().cr3();
  3161. tss.esp = user_stack_address;
  3162. // NOTE: The stack needs to be 16-byte aligned.
  3163. thread->push_value_on_stack((uintptr_t)argument);
  3164. thread->push_value_on_stack(0);
  3165. thread->make_thread_specific_region({});
  3166. thread->set_state(Thread::State::Runnable);
  3167. return thread->tid();
  3168. }
  3169. void Process::sys$exit_thread(void* exit_value)
  3170. {
  3171. REQUIRE_PROMISE(thread);
  3172. cli();
  3173. current->m_exit_value = exit_value;
  3174. current->set_should_die();
  3175. big_lock().force_unlock_if_locked();
  3176. current->die_if_needed();
  3177. ASSERT_NOT_REACHED();
  3178. }
  3179. int Process::sys$detach_thread(int tid)
  3180. {
  3181. REQUIRE_PROMISE(thread);
  3182. InterruptDisabler disabler;
  3183. auto* thread = Thread::from_tid(tid);
  3184. if (!thread || thread->pid() != pid())
  3185. return -ESRCH;
  3186. if (!thread->is_joinable())
  3187. return -EINVAL;
  3188. thread->set_joinable(false);
  3189. return 0;
  3190. }
  3191. int Process::sys$join_thread(int tid, void** exit_value)
  3192. {
  3193. REQUIRE_PROMISE(thread);
  3194. if (exit_value && !validate_write_typed(exit_value))
  3195. return -EFAULT;
  3196. InterruptDisabler disabler;
  3197. auto* thread = Thread::from_tid(tid);
  3198. if (!thread || thread->pid() != pid())
  3199. return -ESRCH;
  3200. if (thread == current)
  3201. return -EDEADLK;
  3202. if (thread->m_joinee == current)
  3203. return -EDEADLK;
  3204. ASSERT(thread->m_joiner != current);
  3205. if (thread->m_joiner)
  3206. return -EINVAL;
  3207. if (!thread->is_joinable())
  3208. return -EINVAL;
  3209. void* joinee_exit_value = nullptr;
  3210. // NOTE: pthread_join() cannot be interrupted by signals. Only by death.
  3211. for (;;) {
  3212. auto result = current->block<Thread::JoinBlocker>(*thread, joinee_exit_value);
  3213. if (result == Thread::BlockResult::InterruptedByDeath) {
  3214. // NOTE: This cleans things up so that Thread::finalize() won't
  3215. // get confused about a missing joiner when finalizing the joinee.
  3216. InterruptDisabler disabler;
  3217. current->m_joinee->m_joiner = nullptr;
  3218. current->m_joinee = nullptr;
  3219. return 0;
  3220. }
  3221. }
  3222. // NOTE: 'thread' is very possibly deleted at this point. Clear it just to be safe.
  3223. thread = nullptr;
  3224. if (exit_value)
  3225. copy_to_user(exit_value, &joinee_exit_value);
  3226. return 0;
  3227. }
  3228. int Process::sys$set_thread_name(int tid, const char* user_name, size_t user_name_length)
  3229. {
  3230. REQUIRE_PROMISE(thread);
  3231. auto name = validate_and_copy_string_from_user(user_name, user_name_length);
  3232. if (name.is_null())
  3233. return -EFAULT;
  3234. const size_t max_thread_name_size = 64;
  3235. if (name.length() > max_thread_name_size)
  3236. return -EINVAL;
  3237. InterruptDisabler disabler;
  3238. auto* thread = Thread::from_tid(tid);
  3239. if (!thread || thread->pid() != pid())
  3240. return -ESRCH;
  3241. thread->set_name(name);
  3242. return 0;
  3243. }
  3244. int Process::sys$get_thread_name(int tid, char* buffer, size_t buffer_size)
  3245. {
  3246. REQUIRE_PROMISE(thread);
  3247. if (buffer_size == 0)
  3248. return -EINVAL;
  3249. if (!validate_write(buffer, buffer_size))
  3250. return -EFAULT;
  3251. InterruptDisabler disabler;
  3252. auto* thread = Thread::from_tid(tid);
  3253. if (!thread || thread->pid() != pid())
  3254. return -ESRCH;
  3255. if (thread->name().length() + 1 > (size_t)buffer_size)
  3256. return -ENAMETOOLONG;
  3257. copy_to_user(buffer, thread->name().characters(), thread->name().length() + 1);
  3258. return 0;
  3259. }
  3260. int Process::sys$gettid()
  3261. {
  3262. REQUIRE_PROMISE(stdio);
  3263. return current->tid();
  3264. }
  3265. int Process::sys$donate(int tid)
  3266. {
  3267. REQUIRE_PROMISE(stdio);
  3268. if (tid < 0)
  3269. return -EINVAL;
  3270. InterruptDisabler disabler;
  3271. auto* thread = Thread::from_tid(tid);
  3272. if (!thread || thread->pid() != pid())
  3273. return -ESRCH;
  3274. Scheduler::donate_to(thread, "sys$donate");
  3275. return 0;
  3276. }
  3277. int Process::sys$rename(const Syscall::SC_rename_params* user_params)
  3278. {
  3279. REQUIRE_PROMISE(cpath);
  3280. Syscall::SC_rename_params params;
  3281. if (!validate_read_and_copy_typed(&params, user_params))
  3282. return -EFAULT;
  3283. auto old_path = get_syscall_path_argument(params.old_path);
  3284. if (old_path.is_error())
  3285. return old_path.error();
  3286. auto new_path = get_syscall_path_argument(params.new_path);
  3287. if (new_path.is_error())
  3288. return new_path.error();
  3289. return VFS::the().rename(old_path.value(), new_path.value(), current_directory());
  3290. }
  3291. int Process::sys$ftruncate(int fd, off_t length)
  3292. {
  3293. REQUIRE_PROMISE(stdio);
  3294. if (length < 0)
  3295. return -EINVAL;
  3296. auto description = file_description(fd);
  3297. if (!description)
  3298. return -EBADF;
  3299. if (!description->is_writable())
  3300. return -EBADF;
  3301. return description->truncate(length);
  3302. }
  3303. int Process::sys$watch_file(const char* user_path, size_t path_length)
  3304. {
  3305. REQUIRE_PROMISE(rpath);
  3306. auto path = get_syscall_path_argument(user_path, path_length);
  3307. if (path.is_error())
  3308. return path.error();
  3309. auto custody_or_error = VFS::the().resolve_path(path.value(), current_directory());
  3310. if (custody_or_error.is_error())
  3311. return custody_or_error.error();
  3312. auto& custody = custody_or_error.value();
  3313. auto& inode = custody->inode();
  3314. if (!inode.fs().supports_watchers())
  3315. return -ENOTSUP;
  3316. int fd = alloc_fd();
  3317. if (fd < 0)
  3318. return fd;
  3319. m_fds[fd].set(FileDescription::create(*InodeWatcher::create(inode)));
  3320. m_fds[fd].description->set_readable(true);
  3321. return fd;
  3322. }
  3323. int Process::sys$systrace(pid_t pid)
  3324. {
  3325. REQUIRE_PROMISE(proc);
  3326. InterruptDisabler disabler;
  3327. auto* peer = Process::from_pid(pid);
  3328. if (!peer)
  3329. return -ESRCH;
  3330. if (peer->uid() != m_euid)
  3331. return -EACCES;
  3332. int fd = alloc_fd();
  3333. if (fd < 0)
  3334. return fd;
  3335. auto description = FileDescription::create(peer->ensure_tracer());
  3336. description->set_readable(true);
  3337. m_fds[fd].set(move(description), 0);
  3338. return fd;
  3339. }
  3340. int Process::sys$halt()
  3341. {
  3342. if (!is_superuser())
  3343. return -EPERM;
  3344. REQUIRE_NO_PROMISES;
  3345. dbgprintf("acquiring FS locks...\n");
  3346. FS::lock_all();
  3347. dbgprintf("syncing mounted filesystems...\n");
  3348. FS::sync();
  3349. dbgprintf("attempting system shutdown...\n");
  3350. IO::out16(0x604, 0x2000);
  3351. return ESUCCESS;
  3352. }
  3353. int Process::sys$reboot()
  3354. {
  3355. if (!is_superuser())
  3356. return -EPERM;
  3357. REQUIRE_NO_PROMISES;
  3358. dbgprintf("acquiring FS locks...\n");
  3359. FS::lock_all();
  3360. dbgprintf("syncing mounted filesystems...\n");
  3361. FS::sync();
  3362. dbgprintf("attempting reboot via KB Controller...\n");
  3363. IO::out8(0x64, 0xFE);
  3364. return ESUCCESS;
  3365. }
  3366. int Process::sys$mount(const Syscall::SC_mount_params* user_params)
  3367. {
  3368. if (!is_superuser())
  3369. return -EPERM;
  3370. REQUIRE_NO_PROMISES;
  3371. Syscall::SC_mount_params params;
  3372. if (!validate_read_and_copy_typed(&params, user_params))
  3373. return -EFAULT;
  3374. auto source = validate_and_copy_string_from_user(params.source);
  3375. auto target = validate_and_copy_string_from_user(params.target);
  3376. auto fs_type = validate_and_copy_string_from_user(params.fs_type);
  3377. if (source.is_null() || target.is_null() || fs_type.is_null())
  3378. return -EFAULT;
  3379. dbg() << "mount " << fs_type << ": source " << source << " @ " << target;
  3380. auto custody_or_error = VFS::the().resolve_path(target, current_directory());
  3381. if (custody_or_error.is_error())
  3382. return custody_or_error.error();
  3383. auto& target_custody = custody_or_error.value();
  3384. RefPtr<FS> fs;
  3385. if (params.flags & MS_BIND) {
  3386. // We're doing a bind mount.
  3387. auto source_or_error = VFS::the().resolve_path(source, current_directory());
  3388. if (source_or_error.is_error())
  3389. return source_or_error.error();
  3390. auto& source_custody = source_or_error.value();
  3391. return VFS::the().bind_mount(source_custody, target_custody, params.flags);
  3392. }
  3393. if (fs_type == "ext2" || fs_type == "Ext2FS") {
  3394. auto source_or_error = VFS::the().open(source, O_RDWR, 0, current_directory());
  3395. if (source_or_error.is_error())
  3396. return source_or_error.error();
  3397. auto* device = source_or_error.value()->device();
  3398. if (!device || !device->is_disk_device()) {
  3399. dbg() << "mount: this is not a DiskDevice";
  3400. return -ENODEV;
  3401. }
  3402. auto& disk_device = static_cast<DiskDevice&>(*device);
  3403. dbg() << "mount: attempting to mount " << disk_device.absolute_path() << " on " << target;
  3404. fs = Ext2FS::create(disk_device);
  3405. } else if (fs_type == "proc" || fs_type == "ProcFS") {
  3406. fs = ProcFS::create();
  3407. } else if (fs_type == "devpts" || fs_type == "DevPtsFS") {
  3408. fs = DevPtsFS::create();
  3409. } else if (fs_type == "tmp" || fs_type == "TmpFS") {
  3410. fs = TmpFS::create();
  3411. } else {
  3412. return -ENODEV;
  3413. }
  3414. if (!fs->initialize()) {
  3415. dbg() << "mount: failed to initialize " << fs_type << " filesystem on " << source;
  3416. return -ENODEV;
  3417. }
  3418. auto result = VFS::the().mount(fs.release_nonnull(), target_custody, params.flags);
  3419. dbg() << "mount: successfully mounted " << source << " on " << target;
  3420. return result;
  3421. }
  3422. int Process::sys$umount(const char* user_mountpoint, size_t mountpoint_length)
  3423. {
  3424. if (!is_superuser())
  3425. return -EPERM;
  3426. REQUIRE_NO_PROMISES;
  3427. if (!validate_read(user_mountpoint, mountpoint_length))
  3428. return -EFAULT;
  3429. auto mountpoint = get_syscall_path_argument(user_mountpoint, mountpoint_length);
  3430. if (mountpoint.is_error())
  3431. return mountpoint.error();
  3432. auto metadata_or_error = VFS::the().lookup_metadata(mountpoint.value(), current_directory());
  3433. if (metadata_or_error.is_error())
  3434. return metadata_or_error.error();
  3435. auto guest_inode_id = metadata_or_error.value().inode;
  3436. return VFS::the().unmount(guest_inode_id);
  3437. }
  3438. ProcessTracer& Process::ensure_tracer()
  3439. {
  3440. if (!m_tracer)
  3441. m_tracer = ProcessTracer::create(m_pid);
  3442. return *m_tracer;
  3443. }
  3444. void Process::FileDescriptionAndFlags::clear()
  3445. {
  3446. description = nullptr;
  3447. flags = 0;
  3448. }
  3449. void Process::FileDescriptionAndFlags::set(NonnullRefPtr<FileDescription>&& d, u32 f)
  3450. {
  3451. description = move(d);
  3452. flags = f;
  3453. }
  3454. int Process::sys$mknod(const Syscall::SC_mknod_params* user_params)
  3455. {
  3456. REQUIRE_PROMISE(dpath);
  3457. Syscall::SC_mknod_params params;
  3458. if (!validate_read_and_copy_typed(&params, user_params))
  3459. return -EFAULT;
  3460. if (!is_superuser() && !is_regular_file(params.mode) && !is_fifo(params.mode) && !is_socket(params.mode))
  3461. return -EPERM;
  3462. auto path = get_syscall_path_argument(params.path);
  3463. if (path.is_error())
  3464. return path.error();
  3465. return VFS::the().mknod(path.value(), params.mode & ~umask(), params.dev, current_directory());
  3466. }
  3467. int Process::sys$dump_backtrace()
  3468. {
  3469. dump_backtrace();
  3470. return 0;
  3471. }
  3472. int Process::sys$dbgputch(u8 ch)
  3473. {
  3474. IO::out8(0xe9, ch);
  3475. return 0;
  3476. }
  3477. int Process::sys$dbgputstr(const u8* characters, int length)
  3478. {
  3479. if (!length)
  3480. return 0;
  3481. if (!validate_read(characters, length))
  3482. return -EFAULT;
  3483. SmapDisabler disabler;
  3484. for (int i = 0; i < length; ++i)
  3485. IO::out8(0xe9, characters[i]);
  3486. return 0;
  3487. }
  3488. KBuffer Process::backtrace(ProcessInspectionHandle& handle) const
  3489. {
  3490. KBufferBuilder builder;
  3491. for_each_thread([&](Thread& thread) {
  3492. builder.appendf("Thread %d (%s):\n", thread.tid(), thread.name().characters());
  3493. builder.append(thread.backtrace(handle));
  3494. return IterationDecision::Continue;
  3495. });
  3496. return builder.build();
  3497. }
  3498. int Process::sys$set_process_icon(int icon_id)
  3499. {
  3500. REQUIRE_PROMISE(shared_buffer);
  3501. LOCKER(shared_buffers().lock());
  3502. auto it = shared_buffers().resource().find(icon_id);
  3503. if (it == shared_buffers().resource().end())
  3504. return -EINVAL;
  3505. auto& shared_buffer = *(*it).value;
  3506. if (!shared_buffer.is_shared_with(m_pid))
  3507. return -EPERM;
  3508. m_icon_id = icon_id;
  3509. return 0;
  3510. }
  3511. int Process::sys$get_process_name(char* buffer, int buffer_size)
  3512. {
  3513. REQUIRE_PROMISE(stdio);
  3514. if (buffer_size <= 0)
  3515. return -EINVAL;
  3516. if (!validate_write(buffer, buffer_size))
  3517. return -EFAULT;
  3518. if (m_name.length() + 1 > (size_t)buffer_size)
  3519. return -ENAMETOOLONG;
  3520. copy_to_user(buffer, m_name.characters(), m_name.length() + 1);
  3521. return 0;
  3522. }
  3523. // We don't use the flag yet, but we could use it for distinguishing
  3524. // random source like Linux, unlike the OpenBSD equivalent. However, if we
  3525. // do, we should be able of the caveats that Linux has dealt with.
  3526. int Process::sys$getrandom(void* buffer, size_t buffer_size, unsigned int flags __attribute__((unused)))
  3527. {
  3528. REQUIRE_PROMISE(stdio);
  3529. if (buffer_size <= 0)
  3530. return -EINVAL;
  3531. if (!validate_write(buffer, buffer_size))
  3532. return -EFAULT;
  3533. SmapDisabler disabler;
  3534. get_good_random_bytes((u8*)buffer, buffer_size);
  3535. return 0;
  3536. }
  3537. int Process::sys$setkeymap(const Syscall::SC_setkeymap_params* user_params)
  3538. {
  3539. if (!is_superuser())
  3540. return -EPERM;
  3541. REQUIRE_NO_PROMISES;
  3542. Syscall::SC_setkeymap_params params;
  3543. if (!validate_read_and_copy_typed(&params, user_params))
  3544. return -EFAULT;
  3545. const char* map = params.map;
  3546. const char* shift_map = params.shift_map;
  3547. const char* alt_map = params.alt_map;
  3548. const char* altgr_map = params.altgr_map;
  3549. if (!validate_read(map, 0x80))
  3550. return -EFAULT;
  3551. if (!validate_read(shift_map, 0x80))
  3552. return -EFAULT;
  3553. if (!validate_read(alt_map, 0x80))
  3554. return -EFAULT;
  3555. if (!validate_read(altgr_map, 0x80))
  3556. return -EFAULT;
  3557. SmapDisabler disabler;
  3558. KeyboardDevice::the().set_maps(map, shift_map, alt_map, altgr_map);
  3559. return 0;
  3560. }
  3561. int Process::sys$clock_gettime(clockid_t clock_id, timespec* ts)
  3562. {
  3563. REQUIRE_PROMISE(stdio);
  3564. if (!validate_write_typed(ts))
  3565. return -EFAULT;
  3566. SmapDisabler disabler;
  3567. switch (clock_id) {
  3568. case CLOCK_MONOTONIC:
  3569. ts->tv_sec = g_uptime / TICKS_PER_SECOND;
  3570. ts->tv_nsec = (g_uptime % TICKS_PER_SECOND) * 1000000;
  3571. break;
  3572. default:
  3573. return -EINVAL;
  3574. }
  3575. return 0;
  3576. }
  3577. int Process::sys$clock_nanosleep(const Syscall::SC_clock_nanosleep_params* user_params)
  3578. {
  3579. REQUIRE_PROMISE(stdio);
  3580. Syscall::SC_clock_nanosleep_params params;
  3581. if (!validate_read_and_copy_typed(&params, user_params))
  3582. return -EFAULT;
  3583. if (params.requested_sleep && !validate_read_typed(params.requested_sleep))
  3584. return -EFAULT;
  3585. timespec requested_sleep;
  3586. copy_from_user(&requested_sleep, params.requested_sleep);
  3587. if (params.remaining_sleep && !validate_write_typed(params.remaining_sleep))
  3588. return -EFAULT;
  3589. bool is_absolute = params.flags & TIMER_ABSTIME;
  3590. switch (params.clock_id) {
  3591. case CLOCK_MONOTONIC: {
  3592. u64 wakeup_time;
  3593. if (is_absolute) {
  3594. u64 time_to_wake = (requested_sleep.tv_sec * 1000 + requested_sleep.tv_nsec / 1000000);
  3595. wakeup_time = current->sleep_until(time_to_wake);
  3596. } else {
  3597. u32 ticks_to_sleep = (requested_sleep.tv_sec * 1000 + requested_sleep.tv_nsec / 1000000);
  3598. if (!ticks_to_sleep)
  3599. return 0;
  3600. wakeup_time = current->sleep(ticks_to_sleep);
  3601. }
  3602. if (wakeup_time > g_uptime) {
  3603. u32 ticks_left = wakeup_time - g_uptime;
  3604. if (!is_absolute && params.remaining_sleep) {
  3605. timespec remaining_sleep;
  3606. memset(&remaining_sleep, 0, sizeof(timespec));
  3607. remaining_sleep.tv_sec = ticks_left / TICKS_PER_SECOND;
  3608. ticks_left -= remaining_sleep.tv_sec * TICKS_PER_SECOND;
  3609. remaining_sleep.tv_nsec = ticks_left * 1000000;
  3610. copy_to_user(params.remaining_sleep, &remaining_sleep);
  3611. }
  3612. return -EINTR;
  3613. }
  3614. return 0;
  3615. }
  3616. default:
  3617. return -EINVAL;
  3618. }
  3619. }
  3620. int Process::sys$sync()
  3621. {
  3622. REQUIRE_PROMISE(stdio);
  3623. VFS::the().sync();
  3624. return 0;
  3625. }
  3626. int Process::sys$yield()
  3627. {
  3628. REQUIRE_PROMISE(stdio);
  3629. current->yield_without_holding_big_lock();
  3630. return 0;
  3631. }
  3632. int Process::sys$beep()
  3633. {
  3634. PCSpeaker::tone_on(440);
  3635. u64 wakeup_time = current->sleep(100);
  3636. PCSpeaker::tone_off();
  3637. if (wakeup_time > g_uptime)
  3638. return -EINTR;
  3639. return 0;
  3640. }
  3641. int Process::sys$module_load(const char* user_path, size_t path_length)
  3642. {
  3643. if (!is_superuser())
  3644. return -EPERM;
  3645. REQUIRE_NO_PROMISES;
  3646. auto path = get_syscall_path_argument(user_path, path_length);
  3647. if (path.is_error())
  3648. return path.error();
  3649. auto description_or_error = VFS::the().open(path.value(), O_RDONLY, 0, current_directory());
  3650. if (description_or_error.is_error())
  3651. return description_or_error.error();
  3652. auto& description = description_or_error.value();
  3653. auto payload = description->read_entire_file();
  3654. auto storage = KBuffer::create_with_size(payload.size());
  3655. memcpy(storage.data(), payload.data(), payload.size());
  3656. payload.clear();
  3657. auto elf_image = make<ELFImage>(storage.data(), storage.size());
  3658. if (!elf_image->parse())
  3659. return -ENOEXEC;
  3660. HashMap<String, u8*> section_storage_by_name;
  3661. auto module = make<Module>();
  3662. elf_image->for_each_section_of_type(SHT_PROGBITS, [&](const ELFImage::Section& section) {
  3663. auto section_storage = KBuffer::copy(section.raw_data(), section.size(), Region::Access::Read | Region::Access::Write | Region::Access::Execute);
  3664. section_storage_by_name.set(section.name(), section_storage.data());
  3665. module->sections.append(move(section_storage));
  3666. return IterationDecision::Continue;
  3667. });
  3668. bool missing_symbols = false;
  3669. elf_image->for_each_section_of_type(SHT_PROGBITS, [&](const ELFImage::Section& section) {
  3670. auto* section_storage = section_storage_by_name.get(section.name()).value_or(nullptr);
  3671. ASSERT(section_storage);
  3672. section.relocations().for_each_relocation([&](const ELFImage::Relocation& relocation) {
  3673. auto& patch_ptr = *reinterpret_cast<ptrdiff_t*>(section_storage + relocation.offset());
  3674. switch (relocation.type()) {
  3675. case R_386_PC32: {
  3676. // PC-relative relocation
  3677. dbg() << "PC-relative relocation: " << relocation.symbol().name();
  3678. u32 symbol_address = address_for_kernel_symbol(relocation.symbol().name());
  3679. if (symbol_address == 0)
  3680. missing_symbols = true;
  3681. dbg() << " Symbol address: " << (void*)symbol_address;
  3682. ptrdiff_t relative_offset = (char*)symbol_address - ((char*)&patch_ptr + 4);
  3683. patch_ptr = relative_offset;
  3684. break;
  3685. }
  3686. case R_386_32: // Absolute relocation
  3687. dbg() << "Absolute relocation: '" << relocation.symbol().name() << "' value:" << relocation.symbol().value() << ", index:" << relocation.symbol_index();
  3688. if (relocation.symbol().bind() == STB_LOCAL) {
  3689. auto* section_storage_containing_symbol = section_storage_by_name.get(relocation.symbol().section().name()).value_or(nullptr);
  3690. ASSERT(section_storage_containing_symbol);
  3691. u32 symbol_address = (ptrdiff_t)(section_storage_containing_symbol + relocation.symbol().value());
  3692. if (symbol_address == 0)
  3693. missing_symbols = true;
  3694. dbg() << " Symbol address: " << (void*)symbol_address;
  3695. patch_ptr += symbol_address;
  3696. } else if (relocation.symbol().bind() == STB_GLOBAL) {
  3697. u32 symbol_address = address_for_kernel_symbol(relocation.symbol().name());
  3698. if (symbol_address == 0)
  3699. missing_symbols = true;
  3700. dbg() << " Symbol address: " << (void*)symbol_address;
  3701. patch_ptr += symbol_address;
  3702. } else {
  3703. ASSERT_NOT_REACHED();
  3704. }
  3705. break;
  3706. }
  3707. return IterationDecision::Continue;
  3708. });
  3709. return IterationDecision::Continue;
  3710. });
  3711. if (missing_symbols)
  3712. return -EINVAL;
  3713. auto* text_base = section_storage_by_name.get(".text").value_or(nullptr);
  3714. if (!text_base) {
  3715. dbg() << "No .text section found in module!";
  3716. return -EINVAL;
  3717. }
  3718. elf_image->for_each_symbol([&](const ELFImage::Symbol& symbol) {
  3719. dbg() << " - " << symbol.type() << " '" << symbol.name() << "' @ " << (void*)symbol.value() << ", size=" << symbol.size();
  3720. if (symbol.name() == "module_init") {
  3721. module->module_init = (ModuleInitPtr)(text_base + symbol.value());
  3722. } else if (symbol.name() == "module_fini") {
  3723. module->module_fini = (ModuleFiniPtr)(text_base + symbol.value());
  3724. } else if (symbol.name() == "module_name") {
  3725. const u8* storage = section_storage_by_name.get(symbol.section().name()).value_or(nullptr);
  3726. if (storage)
  3727. module->name = String((const char*)(storage + symbol.value()));
  3728. }
  3729. return IterationDecision::Continue;
  3730. });
  3731. if (!module->module_init)
  3732. return -EINVAL;
  3733. if (g_modules->contains(module->name)) {
  3734. dbg() << "a module with the name " << module->name << " is already loaded; please unload it first";
  3735. return -EEXIST;
  3736. }
  3737. module->module_init();
  3738. auto name = module->name;
  3739. g_modules->set(name, move(module));
  3740. return 0;
  3741. }
  3742. int Process::sys$module_unload(const char* user_name, size_t name_length)
  3743. {
  3744. if (!is_superuser())
  3745. return -EPERM;
  3746. REQUIRE_NO_PROMISES;
  3747. auto module_name = validate_and_copy_string_from_user(user_name, name_length);
  3748. if (module_name.is_null())
  3749. return -EFAULT;
  3750. auto it = g_modules->find(module_name);
  3751. if (it == g_modules->end())
  3752. return -ENOENT;
  3753. if (it->value->module_fini)
  3754. it->value->module_fini();
  3755. g_modules->remove(it);
  3756. return 0;
  3757. }
  3758. int Process::sys$profiling_enable(pid_t pid)
  3759. {
  3760. REQUIRE_NO_PROMISES;
  3761. InterruptDisabler disabler;
  3762. auto* process = Process::from_pid(pid);
  3763. if (!process)
  3764. return -ESRCH;
  3765. if (!is_superuser() && process->uid() != m_uid)
  3766. return -EPERM;
  3767. Profiling::start(*process);
  3768. process->set_profiling(true);
  3769. return 0;
  3770. }
  3771. int Process::sys$profiling_disable(pid_t pid)
  3772. {
  3773. InterruptDisabler disabler;
  3774. auto* process = Process::from_pid(pid);
  3775. if (!process)
  3776. return -ESRCH;
  3777. if (!is_superuser() && process->uid() != m_uid)
  3778. return -EPERM;
  3779. process->set_profiling(false);
  3780. Profiling::stop();
  3781. return 0;
  3782. }
  3783. void* Process::sys$get_kernel_info_page()
  3784. {
  3785. REQUIRE_PROMISE(stdio);
  3786. return s_info_page_address_for_userspace.as_ptr();
  3787. }
  3788. Thread& Process::any_thread()
  3789. {
  3790. Thread* found_thread = nullptr;
  3791. for_each_thread([&](auto& thread) {
  3792. found_thread = &thread;
  3793. return IterationDecision::Break;
  3794. });
  3795. ASSERT(found_thread);
  3796. return *found_thread;
  3797. }
  3798. WaitQueue& Process::futex_queue(i32* userspace_address)
  3799. {
  3800. auto& queue = m_futex_queues.ensure((uintptr_t)userspace_address);
  3801. if (!queue)
  3802. queue = make<WaitQueue>();
  3803. return *queue;
  3804. }
  3805. int Process::sys$futex(const Syscall::SC_futex_params* user_params)
  3806. {
  3807. REQUIRE_PROMISE(thread);
  3808. Syscall::SC_futex_params params;
  3809. if (!validate_read_and_copy_typed(&params, user_params))
  3810. return -EFAULT;
  3811. i32* userspace_address = params.userspace_address;
  3812. int futex_op = params.futex_op;
  3813. i32 value = params.val;
  3814. const timespec* user_timeout = params.timeout;
  3815. if (!validate_read_typed(userspace_address))
  3816. return -EFAULT;
  3817. if (user_timeout && !validate_read_typed(user_timeout))
  3818. return -EFAULT;
  3819. timespec timeout { 0, 0 };
  3820. if (user_timeout)
  3821. copy_from_user(&timeout, user_timeout);
  3822. i32 user_value;
  3823. switch (futex_op) {
  3824. case FUTEX_WAIT:
  3825. copy_from_user(&user_value, userspace_address);
  3826. if (user_value != value)
  3827. return -EAGAIN;
  3828. // FIXME: This is supposed to be interruptible by a signal, but right now WaitQueue cannot be interrupted.
  3829. // FIXME: Support timeout!
  3830. current->wait_on(futex_queue(userspace_address));
  3831. break;
  3832. case FUTEX_WAKE:
  3833. if (value == 0)
  3834. return 0;
  3835. if (value == 1) {
  3836. futex_queue(userspace_address).wake_one();
  3837. } else {
  3838. // FIXME: Wake exactly (value) waiters.
  3839. futex_queue(userspace_address).wake_all();
  3840. }
  3841. break;
  3842. }
  3843. return 0;
  3844. }
  3845. int Process::sys$set_thread_boost(int tid, int amount)
  3846. {
  3847. REQUIRE_PROMISE(proc);
  3848. if (amount < 0 || amount > 20)
  3849. return -EINVAL;
  3850. InterruptDisabler disabler;
  3851. auto* thread = Thread::from_tid(tid);
  3852. if (!thread)
  3853. return -ESRCH;
  3854. if (thread->state() == Thread::State::Dead || thread->state() == Thread::State::Dying)
  3855. return -ESRCH;
  3856. if (!is_superuser() && thread->process().uid() != euid())
  3857. return -EPERM;
  3858. thread->set_priority_boost(amount);
  3859. return 0;
  3860. }
  3861. int Process::sys$set_process_boost(pid_t pid, int amount)
  3862. {
  3863. REQUIRE_PROMISE(proc);
  3864. if (amount < 0 || amount > 20)
  3865. return -EINVAL;
  3866. InterruptDisabler disabler;
  3867. auto* process = Process::from_pid(pid);
  3868. if (!process || process->is_dead())
  3869. return -ESRCH;
  3870. if (!is_superuser() && process->uid() != euid())
  3871. return -EPERM;
  3872. process->m_priority_boost = amount;
  3873. return 0;
  3874. }
  3875. int Process::sys$chroot(const char* user_path, size_t path_length, int mount_flags)
  3876. {
  3877. if (!is_superuser())
  3878. return -EPERM;
  3879. REQUIRE_PROMISE(chroot);
  3880. auto path = get_syscall_path_argument(user_path, path_length);
  3881. if (path.is_error())
  3882. return path.error();
  3883. auto directory_or_error = VFS::the().open_directory(path.value(), current_directory());
  3884. if (directory_or_error.is_error())
  3885. return directory_or_error.error();
  3886. auto directory = directory_or_error.value();
  3887. m_root_directory_relative_to_global_root = directory;
  3888. int chroot_mount_flags = mount_flags == -1 ? directory->mount_flags() : mount_flags;
  3889. set_root_directory(Custody::create(nullptr, "", directory->inode(), chroot_mount_flags));
  3890. return 0;
  3891. }
  3892. Custody& Process::root_directory()
  3893. {
  3894. if (!m_root_directory)
  3895. m_root_directory = VFS::the().root_custody();
  3896. return *m_root_directory;
  3897. }
  3898. Custody& Process::root_directory_relative_to_global_root()
  3899. {
  3900. if (!m_root_directory_relative_to_global_root)
  3901. m_root_directory_relative_to_global_root = root_directory();
  3902. return *m_root_directory_relative_to_global_root;
  3903. }
  3904. void Process::set_root_directory(const Custody& root)
  3905. {
  3906. m_root_directory = root;
  3907. }
  3908. int Process::sys$pledge(const Syscall::SC_pledge_params* user_params)
  3909. {
  3910. Syscall::SC_pledge_params params;
  3911. if (!validate_read_and_copy_typed(&params, user_params))
  3912. return -EFAULT;
  3913. if (params.promises.length > 1024 || params.execpromises.length > 1024)
  3914. return -E2BIG;
  3915. String promises;
  3916. if (params.promises.characters) {
  3917. promises = validate_and_copy_string_from_user(params.promises);
  3918. if (promises.is_null())
  3919. return -EFAULT;
  3920. }
  3921. String execpromises;
  3922. if (params.execpromises.characters) {
  3923. execpromises = validate_and_copy_string_from_user(params.execpromises);
  3924. if (execpromises.is_null())
  3925. return -EFAULT;
  3926. }
  3927. auto parse_pledge = [&](auto& pledge_spec, u32& mask) {
  3928. auto parts = pledge_spec.split_view(' ');
  3929. for (auto& part : parts) {
  3930. #define __ENUMERATE_PLEDGE_PROMISE(x) \
  3931. if (part == #x) { \
  3932. mask |= (1u << (u32)Pledge::x); \
  3933. continue; \
  3934. }
  3935. ENUMERATE_PLEDGE_PROMISES
  3936. #undef __ENUMERATE_PLEDGE_PROMISE
  3937. if (part == "dns") {
  3938. // "dns" is an alias for "unix" since DNS queries go via LookupServer
  3939. mask |= (1u << (u32)Pledge::unix);
  3940. continue;
  3941. }
  3942. return false;
  3943. }
  3944. return true;
  3945. };
  3946. if (!promises.is_null()) {
  3947. u32 new_promises = 0;
  3948. if (!parse_pledge(promises, new_promises))
  3949. return -EINVAL;
  3950. if (m_promises && (!new_promises || new_promises & ~m_promises))
  3951. return -EPERM;
  3952. m_promises = new_promises;
  3953. }
  3954. if (!execpromises.is_null()) {
  3955. u32 new_execpromises = 0;
  3956. if (!parse_pledge(execpromises, new_execpromises))
  3957. return -EINVAL;
  3958. if (m_execpromises && (!new_execpromises || new_execpromises & ~m_execpromises))
  3959. return -EPERM;
  3960. m_execpromises = new_execpromises;
  3961. }
  3962. return 0;
  3963. }
  3964. Region& Process::add_region(NonnullOwnPtr<Region> region)
  3965. {
  3966. auto* ptr = region.ptr();
  3967. m_regions.append(move(region));
  3968. return *ptr;
  3969. }
  3970. int Process::sys$unveil(const Syscall::SC_unveil_params* user_params)
  3971. {
  3972. Syscall::SC_unveil_params params;
  3973. if (!validate_read_and_copy_typed(&params, user_params))
  3974. return -EFAULT;
  3975. if (!params.path.characters && !params.permissions.characters) {
  3976. m_veil_state = VeilState::Locked;
  3977. return 0;
  3978. }
  3979. if (m_veil_state == VeilState::Locked)
  3980. return -EPERM;
  3981. if (!params.path.characters || !params.permissions.characters)
  3982. return -EINVAL;
  3983. if (params.permissions.length > 4)
  3984. return -EINVAL;
  3985. auto path = get_syscall_path_argument(params.path);
  3986. if (path.is_error())
  3987. return path.error();
  3988. if (path.value().is_empty() || path.value().characters()[0] != '/')
  3989. return -EINVAL;
  3990. auto permissions = validate_and_copy_string_from_user(params.permissions);
  3991. if (permissions.is_null())
  3992. return -EFAULT;
  3993. unsigned new_permissions = 0;
  3994. for (size_t i = 0; i < permissions.length(); ++i) {
  3995. switch (permissions[i]) {
  3996. case 'r':
  3997. new_permissions |= UnveiledPath::Access::Read;
  3998. break;
  3999. case 'w':
  4000. new_permissions |= UnveiledPath::Access::Write;
  4001. break;
  4002. case 'x':
  4003. new_permissions |= UnveiledPath::Access::Execute;
  4004. break;
  4005. case 'c':
  4006. new_permissions |= UnveiledPath::Access::CreateOrRemove;
  4007. break;
  4008. default:
  4009. return -EINVAL;
  4010. }
  4011. }
  4012. for (int i = 0; i < m_unveiled_paths.size(); ++i) {
  4013. auto& unveiled_path = m_unveiled_paths[i];
  4014. if (unveiled_path.path == path.value()) {
  4015. if (new_permissions & ~unveiled_path.permissions)
  4016. return -EPERM;
  4017. unveiled_path.permissions = new_permissions;
  4018. return 0;
  4019. }
  4020. }
  4021. m_unveiled_paths.append({ path.value(), new_permissions });
  4022. ASSERT(m_veil_state != VeilState::Locked);
  4023. m_veil_state = VeilState::Dropped;
  4024. return 0;
  4025. }