syscall.c 402 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603
  1. /*
  2. * Linux syscalls
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #define _ATFILE_SOURCE
  20. #include "qemu/osdep.h"
  21. #include "qemu/cutils.h"
  22. #include "qemu/path.h"
  23. #include "qemu/memfd.h"
  24. #include "qemu/queue.h"
  25. #include <elf.h>
  26. #include <endian.h>
  27. #include <grp.h>
  28. #include <sys/ipc.h>
  29. #include <sys/msg.h>
  30. #include <sys/wait.h>
  31. #include <sys/mount.h>
  32. #include <sys/file.h>
  33. #include <sys/fsuid.h>
  34. #include <sys/personality.h>
  35. #include <sys/prctl.h>
  36. #include <sys/resource.h>
  37. #include <sys/swap.h>
  38. #include <linux/capability.h>
  39. #include <sched.h>
  40. #include <sys/timex.h>
  41. #include <sys/socket.h>
  42. #include <linux/sockios.h>
  43. #include <sys/un.h>
  44. #include <sys/uio.h>
  45. #include <poll.h>
  46. #include <sys/times.h>
  47. #include <sys/shm.h>
  48. #include <sys/sem.h>
  49. #include <sys/statfs.h>
  50. #include <utime.h>
  51. #include <sys/sysinfo.h>
  52. #include <sys/signalfd.h>
  53. //#include <sys/user.h>
  54. #include <netinet/ip.h>
  55. #include <netinet/tcp.h>
  56. #include <linux/wireless.h>
  57. #include <linux/icmp.h>
  58. #include <linux/icmpv6.h>
  59. #include <linux/errqueue.h>
  60. #include <linux/random.h>
  61. #ifdef CONFIG_TIMERFD
  62. #include <sys/timerfd.h>
  63. #endif
  64. #ifdef CONFIG_EVENTFD
  65. #include <sys/eventfd.h>
  66. #endif
  67. #ifdef CONFIG_EPOLL
  68. #include <sys/epoll.h>
  69. #endif
  70. #ifdef CONFIG_ATTR
  71. #include "qemu/xattr.h"
  72. #endif
  73. #ifdef CONFIG_SENDFILE
  74. #include <sys/sendfile.h>
  75. #endif
  76. #ifdef CONFIG_KCOV
  77. #include <sys/kcov.h>
  78. #endif
  79. #define termios host_termios
  80. #define winsize host_winsize
  81. #define termio host_termio
  82. #define sgttyb host_sgttyb /* same as target */
  83. #define tchars host_tchars /* same as target */
  84. #define ltchars host_ltchars /* same as target */
  85. #include <linux/termios.h>
  86. #include <linux/unistd.h>
  87. #include <linux/cdrom.h>
  88. #include <linux/hdreg.h>
  89. #include <linux/soundcard.h>
  90. #include <linux/kd.h>
  91. #include <linux/mtio.h>
  92. #include <linux/fs.h>
  93. #include <linux/fd.h>
  94. #if defined(CONFIG_FIEMAP)
  95. #include <linux/fiemap.h>
  96. #endif
  97. #include <linux/fb.h>
  98. #if defined(CONFIG_USBFS)
  99. #include <linux/usbdevice_fs.h>
  100. #include <linux/usb/ch9.h>
  101. #endif
  102. #include <linux/vt.h>
  103. #include <linux/dm-ioctl.h>
  104. #include <linux/reboot.h>
  105. #include <linux/route.h>
  106. #include <linux/filter.h>
  107. #include <linux/blkpg.h>
  108. #include <netpacket/packet.h>
  109. #include <linux/netlink.h>
  110. #include <linux/if_alg.h>
  111. #include <linux/rtc.h>
  112. #include <sound/asound.h>
  113. #ifdef HAVE_DRM_H
  114. #include <libdrm/drm.h>
  115. #endif
  116. #include "linux_loop.h"
  117. #include "uname.h"
  118. #include "qemu.h"
  119. #include "qemu/guest-random.h"
  120. #include "qemu/selfmap.h"
  121. #include "user/syscall-trace.h"
  122. #include "qapi/error.h"
  123. #include "fd-trans.h"
  124. #include "tcg/tcg.h"
  125. #ifndef CLONE_IO
  126. #define CLONE_IO 0x80000000 /* Clone io context */
  127. #endif
  128. /* We can't directly call the host clone syscall, because this will
  129. * badly confuse libc (breaking mutexes, for example). So we must
  130. * divide clone flags into:
  131. * * flag combinations that look like pthread_create()
  132. * * flag combinations that look like fork()
  133. * * flags we can implement within QEMU itself
  134. * * flags we can't support and will return an error for
  135. */
  136. /* For thread creation, all these flags must be present; for
  137. * fork, none must be present.
  138. */
  139. #define CLONE_THREAD_FLAGS \
  140. (CLONE_VM | CLONE_FS | CLONE_FILES | \
  141. CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM)
  142. /* These flags are ignored:
  143. * CLONE_DETACHED is now ignored by the kernel;
  144. * CLONE_IO is just an optimisation hint to the I/O scheduler
  145. */
  146. #define CLONE_IGNORED_FLAGS \
  147. (CLONE_DETACHED | CLONE_IO)
  148. /* Flags for fork which we can implement within QEMU itself */
  149. #define CLONE_OPTIONAL_FORK_FLAGS \
  150. (CLONE_SETTLS | CLONE_PARENT_SETTID | \
  151. CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID)
  152. /* Flags for thread creation which we can implement within QEMU itself */
  153. #define CLONE_OPTIONAL_THREAD_FLAGS \
  154. (CLONE_SETTLS | CLONE_PARENT_SETTID | \
  155. CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | CLONE_PARENT)
  156. #define CLONE_INVALID_FORK_FLAGS \
  157. (~(CSIGNAL | CLONE_OPTIONAL_FORK_FLAGS | CLONE_IGNORED_FLAGS))
  158. #define CLONE_INVALID_THREAD_FLAGS \
  159. (~(CSIGNAL | CLONE_THREAD_FLAGS | CLONE_OPTIONAL_THREAD_FLAGS | \
  160. CLONE_IGNORED_FLAGS))
  161. /* CLONE_VFORK is special cased early in do_fork(). The other flag bits
  162. * have almost all been allocated. We cannot support any of
  163. * CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC,
  164. * CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, CLONE_PTRACE, CLONE_UNTRACED.
  165. * The checks against the invalid thread masks above will catch these.
  166. * (The one remaining unallocated bit is 0x1000 which used to be CLONE_PID.)
  167. */
  168. /* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
  169. * once. This exercises the codepaths for restart.
  170. */
  171. //#define DEBUG_ERESTARTSYS
  172. //#include <linux/msdos_fs.h>
  173. #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
  174. #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
  175. #undef _syscall0
  176. #undef _syscall1
  177. #undef _syscall2
  178. #undef _syscall3
  179. #undef _syscall4
  180. #undef _syscall5
  181. #undef _syscall6
  182. #define _syscall0(type,name) \
  183. static type name (void) \
  184. { \
  185. return syscall(__NR_##name); \
  186. }
  187. #define _syscall1(type,name,type1,arg1) \
  188. static type name (type1 arg1) \
  189. { \
  190. return syscall(__NR_##name, arg1); \
  191. }
  192. #define _syscall2(type,name,type1,arg1,type2,arg2) \
  193. static type name (type1 arg1,type2 arg2) \
  194. { \
  195. return syscall(__NR_##name, arg1, arg2); \
  196. }
  197. #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
  198. static type name (type1 arg1,type2 arg2,type3 arg3) \
  199. { \
  200. return syscall(__NR_##name, arg1, arg2, arg3); \
  201. }
  202. #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
  203. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
  204. { \
  205. return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
  206. }
  207. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  208. type5,arg5) \
  209. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
  210. { \
  211. return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
  212. }
  213. #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  214. type5,arg5,type6,arg6) \
  215. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
  216. type6 arg6) \
  217. { \
  218. return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
  219. }
  220. #define __NR_sys_uname __NR_uname
  221. #define __NR_sys_getcwd1 __NR_getcwd
  222. #define __NR_sys_getdents __NR_getdents
  223. #define __NR_sys_getdents64 __NR_getdents64
  224. #define __NR_sys_getpriority __NR_getpriority
  225. #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
  226. #define __NR_sys_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo
  227. #define __NR_sys_syslog __NR_syslog
  228. #if defined(__NR_futex)
  229. # define __NR_sys_futex __NR_futex
  230. #endif
  231. #if defined(__NR_futex_time64)
  232. # define __NR_sys_futex_time64 __NR_futex_time64
  233. #endif
  234. #define __NR_sys_inotify_init __NR_inotify_init
  235. #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
  236. #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
  237. #define __NR_sys_statx __NR_statx
  238. #if defined(__alpha__) || defined(__x86_64__) || defined(__s390x__)
  239. #define __NR__llseek __NR_lseek
  240. #endif
  241. /* Newer kernel ports have llseek() instead of _llseek() */
  242. #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
  243. #define TARGET_NR__llseek TARGET_NR_llseek
  244. #endif
  245. #define __NR_sys_gettid __NR_gettid
  246. _syscall0(int, sys_gettid)
  247. /* For the 64-bit guest on 32-bit host case we must emulate
  248. * getdents using getdents64, because otherwise the host
  249. * might hand us back more dirent records than we can fit
  250. * into the guest buffer after structure format conversion.
  251. * Otherwise we emulate getdents with getdents if the host has it.
  252. */
  253. #if defined(__NR_getdents) && HOST_LONG_BITS >= TARGET_ABI_BITS
  254. #define EMULATE_GETDENTS_WITH_GETDENTS
  255. #endif
  256. #if defined(TARGET_NR_getdents) && defined(EMULATE_GETDENTS_WITH_GETDENTS)
  257. _syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
  258. #endif
  259. #if (defined(TARGET_NR_getdents) && \
  260. !defined(EMULATE_GETDENTS_WITH_GETDENTS)) || \
  261. (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
  262. _syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
  263. #endif
  264. #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
  265. _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
  266. loff_t *, res, uint, wh);
  267. #endif
  268. _syscall3(int, sys_rt_sigqueueinfo, pid_t, pid, int, sig, siginfo_t *, uinfo)
  269. _syscall4(int, sys_rt_tgsigqueueinfo, pid_t, pid, pid_t, tid, int, sig,
  270. siginfo_t *, uinfo)
  271. _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
  272. #ifdef __NR_exit_group
  273. _syscall1(int,exit_group,int,error_code)
  274. #endif
  275. #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
  276. _syscall1(int,set_tid_address,int *,tidptr)
  277. #endif
  278. #if defined(__NR_futex)
  279. _syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
  280. const struct timespec *,timeout,int *,uaddr2,int,val3)
  281. #endif
  282. #if defined(__NR_futex_time64)
  283. _syscall6(int,sys_futex_time64,int *,uaddr,int,op,int,val,
  284. const struct timespec *,timeout,int *,uaddr2,int,val3)
  285. #endif
  286. #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
  287. _syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
  288. unsigned long *, user_mask_ptr);
  289. #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
  290. _syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
  291. unsigned long *, user_mask_ptr);
  292. #define __NR_sys_getcpu __NR_getcpu
  293. _syscall3(int, sys_getcpu, unsigned *, cpu, unsigned *, node, void *, tcache);
  294. _syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
  295. void *, arg);
  296. _syscall2(int, capget, struct __user_cap_header_struct *, header,
  297. struct __user_cap_data_struct *, data);
  298. _syscall2(int, capset, struct __user_cap_header_struct *, header,
  299. struct __user_cap_data_struct *, data);
  300. #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
  301. _syscall2(int, ioprio_get, int, which, int, who)
  302. #endif
  303. #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
  304. _syscall3(int, ioprio_set, int, which, int, who, int, ioprio)
  305. #endif
  306. #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
  307. _syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags)
  308. #endif
  309. #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
  310. _syscall5(int, kcmp, pid_t, pid1, pid_t, pid2, int, type,
  311. unsigned long, idx1, unsigned long, idx2)
  312. #endif
  313. /*
  314. * It is assumed that struct statx is architecture independent.
  315. */
  316. #if defined(TARGET_NR_statx) && defined(__NR_statx)
  317. _syscall5(int, sys_statx, int, dirfd, const char *, pathname, int, flags,
  318. unsigned int, mask, struct target_statx *, statxbuf)
  319. #endif
  320. #if defined(TARGET_NR_membarrier) && defined(__NR_membarrier)
  321. _syscall2(int, membarrier, int, cmd, int, flags)
  322. #endif
  323. static bitmask_transtbl fcntl_flags_tbl[] = {
  324. { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
  325. { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
  326. { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
  327. { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
  328. { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
  329. { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
  330. { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
  331. { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
  332. { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
  333. { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
  334. { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
  335. { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
  336. { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
  337. #if defined(O_DIRECT)
  338. { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
  339. #endif
  340. #if defined(O_NOATIME)
  341. { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
  342. #endif
  343. #if defined(O_CLOEXEC)
  344. { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
  345. #endif
  346. #if defined(O_PATH)
  347. { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
  348. #endif
  349. #if defined(O_TMPFILE)
  350. { TARGET_O_TMPFILE, TARGET_O_TMPFILE, O_TMPFILE, O_TMPFILE },
  351. #endif
  352. /* Don't terminate the list prematurely on 64-bit host+guest. */
  353. #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
  354. { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
  355. #endif
  356. { 0, 0, 0, 0 }
  357. };
  358. _syscall2(int, sys_getcwd1, char *, buf, size_t, size)
  359. #ifdef TARGET_NR_utimensat
  360. #if defined(__NR_utimensat)
  361. #define __NR_sys_utimensat __NR_utimensat
  362. _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
  363. const struct timespec *,tsp,int,flags)
  364. #else
  365. static int sys_utimensat(int dirfd, const char *pathname,
  366. const struct timespec times[2], int flags)
  367. {
  368. errno = ENOSYS;
  369. return -1;
  370. }
  371. #endif
  372. #endif /* TARGET_NR_utimensat */
  373. #ifdef TARGET_NR_renameat2
  374. #if defined(__NR_renameat2)
  375. #define __NR_sys_renameat2 __NR_renameat2
  376. _syscall5(int, sys_renameat2, int, oldfd, const char *, old, int, newfd,
  377. const char *, new, unsigned int, flags)
  378. #else
  379. static int sys_renameat2(int oldfd, const char *old,
  380. int newfd, const char *new, int flags)
  381. {
  382. if (flags == 0) {
  383. return renameat(oldfd, old, newfd, new);
  384. }
  385. errno = ENOSYS;
  386. return -1;
  387. }
  388. #endif
  389. #endif /* TARGET_NR_renameat2 */
  390. #ifdef CONFIG_INOTIFY
  391. #include <sys/inotify.h>
  392. #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
  393. static int sys_inotify_init(void)
  394. {
  395. return (inotify_init());
  396. }
  397. #endif
  398. #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
  399. static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
  400. {
  401. return (inotify_add_watch(fd, pathname, mask));
  402. }
  403. #endif
  404. #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
  405. static int sys_inotify_rm_watch(int fd, int32_t wd)
  406. {
  407. return (inotify_rm_watch(fd, wd));
  408. }
  409. #endif
  410. #ifdef CONFIG_INOTIFY1
  411. #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
  412. static int sys_inotify_init1(int flags)
  413. {
  414. return (inotify_init1(flags));
  415. }
  416. #endif
  417. #endif
  418. #else
  419. /* Userspace can usually survive runtime without inotify */
  420. #undef TARGET_NR_inotify_init
  421. #undef TARGET_NR_inotify_init1
  422. #undef TARGET_NR_inotify_add_watch
  423. #undef TARGET_NR_inotify_rm_watch
  424. #endif /* CONFIG_INOTIFY */
  425. #if defined(TARGET_NR_prlimit64)
  426. #ifndef __NR_prlimit64
  427. # define __NR_prlimit64 -1
  428. #endif
  429. #define __NR_sys_prlimit64 __NR_prlimit64
  430. /* The glibc rlimit structure may not be that used by the underlying syscall */
  431. struct host_rlimit64 {
  432. uint64_t rlim_cur;
  433. uint64_t rlim_max;
  434. };
  435. _syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
  436. const struct host_rlimit64 *, new_limit,
  437. struct host_rlimit64 *, old_limit)
  438. #endif
  439. #if defined(TARGET_NR_timer_create)
  440. /* Maxiumum of 32 active POSIX timers allowed at any one time. */
  441. static timer_t g_posix_timers[32] = { 0, } ;
  442. static inline int next_free_host_timer(void)
  443. {
  444. int k ;
  445. /* FIXME: Does finding the next free slot require a lock? */
  446. for (k = 0; k < ARRAY_SIZE(g_posix_timers); k++) {
  447. if (g_posix_timers[k] == 0) {
  448. g_posix_timers[k] = (timer_t) 1;
  449. return k;
  450. }
  451. }
  452. return -1;
  453. }
  454. #endif
  455. /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
  456. #ifdef TARGET_ARM
  457. static inline int regpairs_aligned(void *cpu_env, int num)
  458. {
  459. return ((((CPUARMState *)cpu_env)->eabi) == 1) ;
  460. }
  461. #elif defined(TARGET_MIPS) && (TARGET_ABI_BITS == 32)
  462. static inline int regpairs_aligned(void *cpu_env, int num) { return 1; }
  463. #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
  464. /* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
  465. * of registers which translates to the same as ARM/MIPS, because we start with
  466. * r3 as arg1 */
  467. static inline int regpairs_aligned(void *cpu_env, int num) { return 1; }
  468. #elif defined(TARGET_SH4)
  469. /* SH4 doesn't align register pairs, except for p{read,write}64 */
  470. static inline int regpairs_aligned(void *cpu_env, int num)
  471. {
  472. switch (num) {
  473. case TARGET_NR_pread64:
  474. case TARGET_NR_pwrite64:
  475. return 1;
  476. default:
  477. return 0;
  478. }
  479. }
  480. #elif defined(TARGET_XTENSA)
  481. static inline int regpairs_aligned(void *cpu_env, int num) { return 1; }
  482. #else
  483. static inline int regpairs_aligned(void *cpu_env, int num) { return 0; }
  484. #endif
  485. #define ERRNO_TABLE_SIZE 1200
  486. /* target_to_host_errno_table[] is initialized from
  487. * host_to_target_errno_table[] in syscall_init(). */
  488. static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
  489. };
  490. /*
  491. * This list is the union of errno values overridden in asm-<arch>/errno.h
  492. * minus the errnos that are not actually generic to all archs.
  493. */
  494. static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
  495. [EAGAIN] = TARGET_EAGAIN,
  496. [EIDRM] = TARGET_EIDRM,
  497. [ECHRNG] = TARGET_ECHRNG,
  498. [EL2NSYNC] = TARGET_EL2NSYNC,
  499. [EL3HLT] = TARGET_EL3HLT,
  500. [EL3RST] = TARGET_EL3RST,
  501. [ELNRNG] = TARGET_ELNRNG,
  502. [EUNATCH] = TARGET_EUNATCH,
  503. [ENOCSI] = TARGET_ENOCSI,
  504. [EL2HLT] = TARGET_EL2HLT,
  505. [EDEADLK] = TARGET_EDEADLK,
  506. [ENOLCK] = TARGET_ENOLCK,
  507. [EBADE] = TARGET_EBADE,
  508. [EBADR] = TARGET_EBADR,
  509. [EXFULL] = TARGET_EXFULL,
  510. [ENOANO] = TARGET_ENOANO,
  511. [EBADRQC] = TARGET_EBADRQC,
  512. [EBADSLT] = TARGET_EBADSLT,
  513. [EBFONT] = TARGET_EBFONT,
  514. [ENOSTR] = TARGET_ENOSTR,
  515. [ENODATA] = TARGET_ENODATA,
  516. [ETIME] = TARGET_ETIME,
  517. [ENOSR] = TARGET_ENOSR,
  518. [ENONET] = TARGET_ENONET,
  519. [ENOPKG] = TARGET_ENOPKG,
  520. [EREMOTE] = TARGET_EREMOTE,
  521. [ENOLINK] = TARGET_ENOLINK,
  522. [EADV] = TARGET_EADV,
  523. [ESRMNT] = TARGET_ESRMNT,
  524. [ECOMM] = TARGET_ECOMM,
  525. [EPROTO] = TARGET_EPROTO,
  526. [EDOTDOT] = TARGET_EDOTDOT,
  527. [EMULTIHOP] = TARGET_EMULTIHOP,
  528. [EBADMSG] = TARGET_EBADMSG,
  529. [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
  530. [EOVERFLOW] = TARGET_EOVERFLOW,
  531. [ENOTUNIQ] = TARGET_ENOTUNIQ,
  532. [EBADFD] = TARGET_EBADFD,
  533. [EREMCHG] = TARGET_EREMCHG,
  534. [ELIBACC] = TARGET_ELIBACC,
  535. [ELIBBAD] = TARGET_ELIBBAD,
  536. [ELIBSCN] = TARGET_ELIBSCN,
  537. [ELIBMAX] = TARGET_ELIBMAX,
  538. [ELIBEXEC] = TARGET_ELIBEXEC,
  539. [EILSEQ] = TARGET_EILSEQ,
  540. [ENOSYS] = TARGET_ENOSYS,
  541. [ELOOP] = TARGET_ELOOP,
  542. [ERESTART] = TARGET_ERESTART,
  543. [ESTRPIPE] = TARGET_ESTRPIPE,
  544. [ENOTEMPTY] = TARGET_ENOTEMPTY,
  545. [EUSERS] = TARGET_EUSERS,
  546. [ENOTSOCK] = TARGET_ENOTSOCK,
  547. [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
  548. [EMSGSIZE] = TARGET_EMSGSIZE,
  549. [EPROTOTYPE] = TARGET_EPROTOTYPE,
  550. [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
  551. [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
  552. [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
  553. [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
  554. [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
  555. [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
  556. [EADDRINUSE] = TARGET_EADDRINUSE,
  557. [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
  558. [ENETDOWN] = TARGET_ENETDOWN,
  559. [ENETUNREACH] = TARGET_ENETUNREACH,
  560. [ENETRESET] = TARGET_ENETRESET,
  561. [ECONNABORTED] = TARGET_ECONNABORTED,
  562. [ECONNRESET] = TARGET_ECONNRESET,
  563. [ENOBUFS] = TARGET_ENOBUFS,
  564. [EISCONN] = TARGET_EISCONN,
  565. [ENOTCONN] = TARGET_ENOTCONN,
  566. [EUCLEAN] = TARGET_EUCLEAN,
  567. [ENOTNAM] = TARGET_ENOTNAM,
  568. [ENAVAIL] = TARGET_ENAVAIL,
  569. [EISNAM] = TARGET_EISNAM,
  570. [EREMOTEIO] = TARGET_EREMOTEIO,
  571. [EDQUOT] = TARGET_EDQUOT,
  572. [ESHUTDOWN] = TARGET_ESHUTDOWN,
  573. [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
  574. [ETIMEDOUT] = TARGET_ETIMEDOUT,
  575. [ECONNREFUSED] = TARGET_ECONNREFUSED,
  576. [EHOSTDOWN] = TARGET_EHOSTDOWN,
  577. [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
  578. [EALREADY] = TARGET_EALREADY,
  579. [EINPROGRESS] = TARGET_EINPROGRESS,
  580. [ESTALE] = TARGET_ESTALE,
  581. [ECANCELED] = TARGET_ECANCELED,
  582. [ENOMEDIUM] = TARGET_ENOMEDIUM,
  583. [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
  584. #ifdef ENOKEY
  585. [ENOKEY] = TARGET_ENOKEY,
  586. #endif
  587. #ifdef EKEYEXPIRED
  588. [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
  589. #endif
  590. #ifdef EKEYREVOKED
  591. [EKEYREVOKED] = TARGET_EKEYREVOKED,
  592. #endif
  593. #ifdef EKEYREJECTED
  594. [EKEYREJECTED] = TARGET_EKEYREJECTED,
  595. #endif
  596. #ifdef EOWNERDEAD
  597. [EOWNERDEAD] = TARGET_EOWNERDEAD,
  598. #endif
  599. #ifdef ENOTRECOVERABLE
  600. [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
  601. #endif
  602. #ifdef ENOMSG
  603. [ENOMSG] = TARGET_ENOMSG,
  604. #endif
  605. #ifdef ERKFILL
  606. [ERFKILL] = TARGET_ERFKILL,
  607. #endif
  608. #ifdef EHWPOISON
  609. [EHWPOISON] = TARGET_EHWPOISON,
  610. #endif
  611. };
  612. static inline int host_to_target_errno(int err)
  613. {
  614. if (err >= 0 && err < ERRNO_TABLE_SIZE &&
  615. host_to_target_errno_table[err]) {
  616. return host_to_target_errno_table[err];
  617. }
  618. return err;
  619. }
  620. static inline int target_to_host_errno(int err)
  621. {
  622. if (err >= 0 && err < ERRNO_TABLE_SIZE &&
  623. target_to_host_errno_table[err]) {
  624. return target_to_host_errno_table[err];
  625. }
  626. return err;
  627. }
  628. static inline abi_long get_errno(abi_long ret)
  629. {
  630. if (ret == -1)
  631. return -host_to_target_errno(errno);
  632. else
  633. return ret;
  634. }
  635. const char *target_strerror(int err)
  636. {
  637. if (err == TARGET_ERESTARTSYS) {
  638. return "To be restarted";
  639. }
  640. if (err == TARGET_QEMU_ESIGRETURN) {
  641. return "Successful exit from sigreturn";
  642. }
  643. if ((err >= ERRNO_TABLE_SIZE) || (err < 0)) {
  644. return NULL;
  645. }
  646. return strerror(target_to_host_errno(err));
  647. }
  648. #define safe_syscall0(type, name) \
  649. static type safe_##name(void) \
  650. { \
  651. return safe_syscall(__NR_##name); \
  652. }
  653. #define safe_syscall1(type, name, type1, arg1) \
  654. static type safe_##name(type1 arg1) \
  655. { \
  656. return safe_syscall(__NR_##name, arg1); \
  657. }
  658. #define safe_syscall2(type, name, type1, arg1, type2, arg2) \
  659. static type safe_##name(type1 arg1, type2 arg2) \
  660. { \
  661. return safe_syscall(__NR_##name, arg1, arg2); \
  662. }
  663. #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
  664. static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
  665. { \
  666. return safe_syscall(__NR_##name, arg1, arg2, arg3); \
  667. }
  668. #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
  669. type4, arg4) \
  670. static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
  671. { \
  672. return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
  673. }
  674. #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
  675. type4, arg4, type5, arg5) \
  676. static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  677. type5 arg5) \
  678. { \
  679. return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
  680. }
  681. #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
  682. type4, arg4, type5, arg5, type6, arg6) \
  683. static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  684. type5 arg5, type6 arg6) \
  685. { \
  686. return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
  687. }
  688. safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count)
  689. safe_syscall3(ssize_t, write, int, fd, const void *, buff, size_t, count)
  690. safe_syscall4(int, openat, int, dirfd, const char *, pathname, \
  691. int, flags, mode_t, mode)
  692. #if defined(TARGET_NR_wait4) || defined(TARGET_NR_waitpid)
  693. safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \
  694. struct rusage *, rusage)
  695. #endif
  696. safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \
  697. int, options, struct rusage *, rusage)
  698. safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp)
  699. #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
  700. defined(TARGET_NR_pselect6)
  701. safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \
  702. fd_set *, exceptfds, struct timespec *, timeout, void *, sig)
  703. #endif
  704. #if defined(TARGET_NR_ppoll) || defined(TARGET_NR_poll)
  705. safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds,
  706. struct timespec *, tsp, const sigset_t *, sigmask,
  707. size_t, sigsetsize)
  708. #endif
  709. safe_syscall6(int, epoll_pwait, int, epfd, struct epoll_event *, events,
  710. int, maxevents, int, timeout, const sigset_t *, sigmask,
  711. size_t, sigsetsize)
  712. #if defined(__NR_futex)
  713. safe_syscall6(int,futex,int *,uaddr,int,op,int,val, \
  714. const struct timespec *,timeout,int *,uaddr2,int,val3)
  715. #endif
  716. #if defined(__NR_futex_time64)
  717. safe_syscall6(int,futex_time64,int *,uaddr,int,op,int,val, \
  718. const struct timespec *,timeout,int *,uaddr2,int,val3)
  719. #endif
  720. safe_syscall2(int, rt_sigsuspend, sigset_t *, newset, size_t, sigsetsize)
  721. safe_syscall2(int, kill, pid_t, pid, int, sig)
  722. safe_syscall2(int, tkill, int, tid, int, sig)
  723. safe_syscall3(int, tgkill, int, tgid, int, pid, int, sig)
  724. safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt)
  725. safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt)
  726. safe_syscall5(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt,
  727. unsigned long, pos_l, unsigned long, pos_h)
  728. safe_syscall5(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, iovcnt,
  729. unsigned long, pos_l, unsigned long, pos_h)
  730. safe_syscall3(int, connect, int, fd, const struct sockaddr *, addr,
  731. socklen_t, addrlen)
  732. safe_syscall6(ssize_t, sendto, int, fd, const void *, buf, size_t, len,
  733. int, flags, const struct sockaddr *, addr, socklen_t, addrlen)
  734. safe_syscall6(ssize_t, recvfrom, int, fd, void *, buf, size_t, len,
  735. int, flags, struct sockaddr *, addr, socklen_t *, addrlen)
  736. safe_syscall3(ssize_t, sendmsg, int, fd, const struct msghdr *, msg, int, flags)
  737. safe_syscall3(ssize_t, recvmsg, int, fd, struct msghdr *, msg, int, flags)
  738. safe_syscall2(int, flock, int, fd, int, operation)
  739. #ifdef TARGET_NR_rt_sigtimedwait
  740. safe_syscall4(int, rt_sigtimedwait, const sigset_t *, these, siginfo_t *, uinfo,
  741. const struct timespec *, uts, size_t, sigsetsize)
  742. #endif
  743. safe_syscall4(int, accept4, int, fd, struct sockaddr *, addr, socklen_t *, len,
  744. int, flags)
  745. #if defined(TARGET_NR_nanosleep)
  746. safe_syscall2(int, nanosleep, const struct timespec *, req,
  747. struct timespec *, rem)
  748. #endif
  749. #ifdef TARGET_NR_clock_nanosleep
  750. safe_syscall4(int, clock_nanosleep, const clockid_t, clock, int, flags,
  751. const struct timespec *, req, struct timespec *, rem)
  752. #endif
  753. #ifdef __NR_ipc
  754. #ifdef __s390x__
  755. safe_syscall5(int, ipc, int, call, long, first, long, second, long, third,
  756. void *, ptr)
  757. #else
  758. safe_syscall6(int, ipc, int, call, long, first, long, second, long, third,
  759. void *, ptr, long, fifth)
  760. #endif
  761. #endif
  762. #ifdef __NR_msgsnd
  763. safe_syscall4(int, msgsnd, int, msgid, const void *, msgp, size_t, sz,
  764. int, flags)
  765. #endif
  766. #ifdef __NR_msgrcv
  767. safe_syscall5(int, msgrcv, int, msgid, void *, msgp, size_t, sz,
  768. long, msgtype, int, flags)
  769. #endif
  770. #ifdef __NR_semtimedop
  771. safe_syscall4(int, semtimedop, int, semid, struct sembuf *, tsops,
  772. unsigned, nsops, const struct timespec *, timeout)
  773. #endif
  774. #ifdef TARGET_NR_mq_timedsend
  775. safe_syscall5(int, mq_timedsend, int, mqdes, const char *, msg_ptr,
  776. size_t, len, unsigned, prio, const struct timespec *, timeout)
  777. #endif
  778. #ifdef TARGET_NR_mq_timedreceive
  779. safe_syscall5(int, mq_timedreceive, int, mqdes, char *, msg_ptr,
  780. size_t, len, unsigned *, prio, const struct timespec *, timeout)
  781. #endif
  782. /* We do ioctl like this rather than via safe_syscall3 to preserve the
  783. * "third argument might be integer or pointer or not present" behaviour of
  784. * the libc function.
  785. */
  786. #define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
  787. /* Similarly for fcntl. Note that callers must always:
  788. * pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK
  789. * use the flock64 struct rather than unsuffixed flock
  790. * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts.
  791. */
  792. #ifdef __NR_fcntl64
  793. #define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
  794. #else
  795. #define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
  796. #endif
  797. static inline int host_to_target_sock_type(int host_type)
  798. {
  799. int target_type;
  800. switch (host_type & 0xf /* SOCK_TYPE_MASK */) {
  801. case SOCK_DGRAM:
  802. target_type = TARGET_SOCK_DGRAM;
  803. break;
  804. case SOCK_STREAM:
  805. target_type = TARGET_SOCK_STREAM;
  806. break;
  807. default:
  808. target_type = host_type & 0xf /* SOCK_TYPE_MASK */;
  809. break;
  810. }
  811. #if defined(SOCK_CLOEXEC)
  812. if (host_type & SOCK_CLOEXEC) {
  813. target_type |= TARGET_SOCK_CLOEXEC;
  814. }
  815. #endif
  816. #if defined(SOCK_NONBLOCK)
  817. if (host_type & SOCK_NONBLOCK) {
  818. target_type |= TARGET_SOCK_NONBLOCK;
  819. }
  820. #endif
  821. return target_type;
  822. }
  823. static abi_ulong target_brk;
  824. static abi_ulong target_original_brk;
  825. static abi_ulong brk_page;
  826. void target_set_brk(abi_ulong new_brk)
  827. {
  828. target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
  829. brk_page = HOST_PAGE_ALIGN(target_brk);
  830. }
  831. //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
  832. #define DEBUGF_BRK(message, args...)
  833. /* do_brk() must return target values and target errnos. */
  834. abi_long do_brk(abi_ulong new_brk)
  835. {
  836. abi_long mapped_addr;
  837. abi_ulong new_alloc_size;
  838. DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
  839. if (!new_brk) {
  840. DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
  841. return target_brk;
  842. }
  843. if (new_brk < target_original_brk) {
  844. DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
  845. target_brk);
  846. return target_brk;
  847. }
  848. /* If the new brk is less than the highest page reserved to the
  849. * target heap allocation, set it and we're almost done... */
  850. if (new_brk <= brk_page) {
  851. /* Heap contents are initialized to zero, as for anonymous
  852. * mapped pages. */
  853. if (new_brk > target_brk) {
  854. memset(g2h(target_brk), 0, new_brk - target_brk);
  855. }
  856. target_brk = new_brk;
  857. DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
  858. return target_brk;
  859. }
  860. /* We need to allocate more memory after the brk... Note that
  861. * we don't use MAP_FIXED because that will map over the top of
  862. * any existing mapping (like the one with the host libc or qemu
  863. * itself); instead we treat "mapped but at wrong address" as
  864. * a failure and unmap again.
  865. */
  866. new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
  867. mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
  868. PROT_READ|PROT_WRITE,
  869. MAP_ANON|MAP_PRIVATE, 0, 0));
  870. if (mapped_addr == brk_page) {
  871. /* Heap contents are initialized to zero, as for anonymous
  872. * mapped pages. Technically the new pages are already
  873. * initialized to zero since they *are* anonymous mapped
  874. * pages, however we have to take care with the contents that
  875. * come from the remaining part of the previous page: it may
  876. * contains garbage data due to a previous heap usage (grown
  877. * then shrunken). */
  878. memset(g2h(target_brk), 0, brk_page - target_brk);
  879. target_brk = new_brk;
  880. brk_page = HOST_PAGE_ALIGN(target_brk);
  881. DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
  882. target_brk);
  883. return target_brk;
  884. } else if (mapped_addr != -1) {
  885. /* Mapped but at wrong address, meaning there wasn't actually
  886. * enough space for this brk.
  887. */
  888. target_munmap(mapped_addr, new_alloc_size);
  889. mapped_addr = -1;
  890. DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
  891. }
  892. else {
  893. DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
  894. }
  895. #if defined(TARGET_ALPHA)
  896. /* We (partially) emulate OSF/1 on Alpha, which requires we
  897. return a proper errno, not an unchanged brk value. */
  898. return -TARGET_ENOMEM;
  899. #endif
  900. /* For everything else, return the previous break. */
  901. return target_brk;
  902. }
  903. #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
  904. defined(TARGET_NR_pselect6)
  905. static inline abi_long copy_from_user_fdset(fd_set *fds,
  906. abi_ulong target_fds_addr,
  907. int n)
  908. {
  909. int i, nw, j, k;
  910. abi_ulong b, *target_fds;
  911. nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
  912. if (!(target_fds = lock_user(VERIFY_READ,
  913. target_fds_addr,
  914. sizeof(abi_ulong) * nw,
  915. 1)))
  916. return -TARGET_EFAULT;
  917. FD_ZERO(fds);
  918. k = 0;
  919. for (i = 0; i < nw; i++) {
  920. /* grab the abi_ulong */
  921. __get_user(b, &target_fds[i]);
  922. for (j = 0; j < TARGET_ABI_BITS; j++) {
  923. /* check the bit inside the abi_ulong */
  924. if ((b >> j) & 1)
  925. FD_SET(k, fds);
  926. k++;
  927. }
  928. }
  929. unlock_user(target_fds, target_fds_addr, 0);
  930. return 0;
  931. }
  932. static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
  933. abi_ulong target_fds_addr,
  934. int n)
  935. {
  936. if (target_fds_addr) {
  937. if (copy_from_user_fdset(fds, target_fds_addr, n))
  938. return -TARGET_EFAULT;
  939. *fds_ptr = fds;
  940. } else {
  941. *fds_ptr = NULL;
  942. }
  943. return 0;
  944. }
  945. static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
  946. const fd_set *fds,
  947. int n)
  948. {
  949. int i, nw, j, k;
  950. abi_long v;
  951. abi_ulong *target_fds;
  952. nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
  953. if (!(target_fds = lock_user(VERIFY_WRITE,
  954. target_fds_addr,
  955. sizeof(abi_ulong) * nw,
  956. 0)))
  957. return -TARGET_EFAULT;
  958. k = 0;
  959. for (i = 0; i < nw; i++) {
  960. v = 0;
  961. for (j = 0; j < TARGET_ABI_BITS; j++) {
  962. v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
  963. k++;
  964. }
  965. __put_user(v, &target_fds[i]);
  966. }
  967. unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
  968. return 0;
  969. }
  970. #endif
  971. #if defined(__alpha__)
  972. #define HOST_HZ 1024
  973. #else
  974. #define HOST_HZ 100
  975. #endif
  976. static inline abi_long host_to_target_clock_t(long ticks)
  977. {
  978. #if HOST_HZ == TARGET_HZ
  979. return ticks;
  980. #else
  981. return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
  982. #endif
  983. }
  984. static inline abi_long host_to_target_rusage(abi_ulong target_addr,
  985. const struct rusage *rusage)
  986. {
  987. struct target_rusage *target_rusage;
  988. if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
  989. return -TARGET_EFAULT;
  990. target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
  991. target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
  992. target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
  993. target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
  994. target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
  995. target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
  996. target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
  997. target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
  998. target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
  999. target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
  1000. target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
  1001. target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
  1002. target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
  1003. target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
  1004. target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
  1005. target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
  1006. target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
  1007. target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
  1008. unlock_user_struct(target_rusage, target_addr, 1);
  1009. return 0;
  1010. }
  1011. #ifdef TARGET_NR_setrlimit
  1012. static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
  1013. {
  1014. abi_ulong target_rlim_swap;
  1015. rlim_t result;
  1016. target_rlim_swap = tswapal(target_rlim);
  1017. if (target_rlim_swap == TARGET_RLIM_INFINITY)
  1018. return RLIM_INFINITY;
  1019. result = target_rlim_swap;
  1020. if (target_rlim_swap != (rlim_t)result)
  1021. return RLIM_INFINITY;
  1022. return result;
  1023. }
  1024. #endif
  1025. #if defined(TARGET_NR_getrlimit) || defined(TARGET_NR_ugetrlimit)
  1026. static inline abi_ulong host_to_target_rlim(rlim_t rlim)
  1027. {
  1028. abi_ulong target_rlim_swap;
  1029. abi_ulong result;
  1030. if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
  1031. target_rlim_swap = TARGET_RLIM_INFINITY;
  1032. else
  1033. target_rlim_swap = rlim;
  1034. result = tswapal(target_rlim_swap);
  1035. return result;
  1036. }
  1037. #endif
  1038. static inline int target_to_host_resource(int code)
  1039. {
  1040. switch (code) {
  1041. case TARGET_RLIMIT_AS:
  1042. return RLIMIT_AS;
  1043. case TARGET_RLIMIT_CORE:
  1044. return RLIMIT_CORE;
  1045. case TARGET_RLIMIT_CPU:
  1046. return RLIMIT_CPU;
  1047. case TARGET_RLIMIT_DATA:
  1048. return RLIMIT_DATA;
  1049. case TARGET_RLIMIT_FSIZE:
  1050. return RLIMIT_FSIZE;
  1051. case TARGET_RLIMIT_LOCKS:
  1052. return RLIMIT_LOCKS;
  1053. case TARGET_RLIMIT_MEMLOCK:
  1054. return RLIMIT_MEMLOCK;
  1055. case TARGET_RLIMIT_MSGQUEUE:
  1056. return RLIMIT_MSGQUEUE;
  1057. case TARGET_RLIMIT_NICE:
  1058. return RLIMIT_NICE;
  1059. case TARGET_RLIMIT_NOFILE:
  1060. return RLIMIT_NOFILE;
  1061. case TARGET_RLIMIT_NPROC:
  1062. return RLIMIT_NPROC;
  1063. case TARGET_RLIMIT_RSS:
  1064. return RLIMIT_RSS;
  1065. case TARGET_RLIMIT_RTPRIO:
  1066. return RLIMIT_RTPRIO;
  1067. case TARGET_RLIMIT_SIGPENDING:
  1068. return RLIMIT_SIGPENDING;
  1069. case TARGET_RLIMIT_STACK:
  1070. return RLIMIT_STACK;
  1071. default:
  1072. return code;
  1073. }
  1074. }
  1075. static inline abi_long copy_from_user_timeval(struct timeval *tv,
  1076. abi_ulong target_tv_addr)
  1077. {
  1078. struct target_timeval *target_tv;
  1079. if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) {
  1080. return -TARGET_EFAULT;
  1081. }
  1082. __get_user(tv->tv_sec, &target_tv->tv_sec);
  1083. __get_user(tv->tv_usec, &target_tv->tv_usec);
  1084. unlock_user_struct(target_tv, target_tv_addr, 0);
  1085. return 0;
  1086. }
  1087. static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
  1088. const struct timeval *tv)
  1089. {
  1090. struct target_timeval *target_tv;
  1091. if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
  1092. return -TARGET_EFAULT;
  1093. }
  1094. __put_user(tv->tv_sec, &target_tv->tv_sec);
  1095. __put_user(tv->tv_usec, &target_tv->tv_usec);
  1096. unlock_user_struct(target_tv, target_tv_addr, 1);
  1097. return 0;
  1098. }
  1099. static inline abi_long copy_to_user_timeval64(abi_ulong target_tv_addr,
  1100. const struct timeval *tv)
  1101. {
  1102. struct target__kernel_sock_timeval *target_tv;
  1103. if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
  1104. return -TARGET_EFAULT;
  1105. }
  1106. __put_user(tv->tv_sec, &target_tv->tv_sec);
  1107. __put_user(tv->tv_usec, &target_tv->tv_usec);
  1108. unlock_user_struct(target_tv, target_tv_addr, 1);
  1109. return 0;
  1110. }
  1111. #if defined(TARGET_NR_futex) || \
  1112. defined(TARGET_NR_rt_sigtimedwait) || \
  1113. defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6) || \
  1114. defined(TARGET_NR_nanosleep) || defined(TARGET_NR_clock_settime) || \
  1115. defined(TARGET_NR_utimensat) || defined(TARGET_NR_mq_timedsend) || \
  1116. defined(TARGET_NR_mq_timedreceive) || defined(TARGET_NR_ipc) || \
  1117. defined(TARGET_NR_semop) || defined(TARGET_NR_semtimedop)
  1118. static inline abi_long target_to_host_timespec(struct timespec *host_ts,
  1119. abi_ulong target_addr)
  1120. {
  1121. struct target_timespec *target_ts;
  1122. if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
  1123. return -TARGET_EFAULT;
  1124. }
  1125. __get_user(host_ts->tv_sec, &target_ts->tv_sec);
  1126. __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1127. unlock_user_struct(target_ts, target_addr, 0);
  1128. return 0;
  1129. }
  1130. #endif
  1131. #if defined(TARGET_NR_clock_settime64) || defined(TARGET_NR_futex_time64)
  1132. static inline abi_long target_to_host_timespec64(struct timespec *host_ts,
  1133. abi_ulong target_addr)
  1134. {
  1135. struct target__kernel_timespec *target_ts;
  1136. if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
  1137. return -TARGET_EFAULT;
  1138. }
  1139. __get_user(host_ts->tv_sec, &target_ts->tv_sec);
  1140. __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1141. unlock_user_struct(target_ts, target_addr, 0);
  1142. return 0;
  1143. }
  1144. #endif
  1145. static inline abi_long host_to_target_timespec(abi_ulong target_addr,
  1146. struct timespec *host_ts)
  1147. {
  1148. struct target_timespec *target_ts;
  1149. if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
  1150. return -TARGET_EFAULT;
  1151. }
  1152. __put_user(host_ts->tv_sec, &target_ts->tv_sec);
  1153. __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1154. unlock_user_struct(target_ts, target_addr, 1);
  1155. return 0;
  1156. }
  1157. static inline abi_long host_to_target_timespec64(abi_ulong target_addr,
  1158. struct timespec *host_ts)
  1159. {
  1160. struct target__kernel_timespec *target_ts;
  1161. if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
  1162. return -TARGET_EFAULT;
  1163. }
  1164. __put_user(host_ts->tv_sec, &target_ts->tv_sec);
  1165. __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1166. unlock_user_struct(target_ts, target_addr, 1);
  1167. return 0;
  1168. }
  1169. #if defined(TARGET_NR_gettimeofday)
  1170. static inline abi_long copy_to_user_timezone(abi_ulong target_tz_addr,
  1171. struct timezone *tz)
  1172. {
  1173. struct target_timezone *target_tz;
  1174. if (!lock_user_struct(VERIFY_WRITE, target_tz, target_tz_addr, 1)) {
  1175. return -TARGET_EFAULT;
  1176. }
  1177. __put_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
  1178. __put_user(tz->tz_dsttime, &target_tz->tz_dsttime);
  1179. unlock_user_struct(target_tz, target_tz_addr, 1);
  1180. return 0;
  1181. }
  1182. #endif
  1183. #if defined(TARGET_NR_settimeofday)
  1184. static inline abi_long copy_from_user_timezone(struct timezone *tz,
  1185. abi_ulong target_tz_addr)
  1186. {
  1187. struct target_timezone *target_tz;
  1188. if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) {
  1189. return -TARGET_EFAULT;
  1190. }
  1191. __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
  1192. __get_user(tz->tz_dsttime, &target_tz->tz_dsttime);
  1193. unlock_user_struct(target_tz, target_tz_addr, 0);
  1194. return 0;
  1195. }
  1196. #endif
  1197. #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
  1198. #include <mqueue.h>
  1199. static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
  1200. abi_ulong target_mq_attr_addr)
  1201. {
  1202. struct target_mq_attr *target_mq_attr;
  1203. if (!lock_user_struct(VERIFY_READ, target_mq_attr,
  1204. target_mq_attr_addr, 1))
  1205. return -TARGET_EFAULT;
  1206. __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
  1207. __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
  1208. __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
  1209. __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
  1210. unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
  1211. return 0;
  1212. }
  1213. static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
  1214. const struct mq_attr *attr)
  1215. {
  1216. struct target_mq_attr *target_mq_attr;
  1217. if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
  1218. target_mq_attr_addr, 0))
  1219. return -TARGET_EFAULT;
  1220. __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
  1221. __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
  1222. __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
  1223. __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
  1224. unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
  1225. return 0;
  1226. }
  1227. #endif
  1228. #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
  1229. /* do_select() must return target values and target errnos. */
  1230. static abi_long do_select(int n,
  1231. abi_ulong rfd_addr, abi_ulong wfd_addr,
  1232. abi_ulong efd_addr, abi_ulong target_tv_addr)
  1233. {
  1234. fd_set rfds, wfds, efds;
  1235. fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
  1236. struct timeval tv;
  1237. struct timespec ts, *ts_ptr;
  1238. abi_long ret;
  1239. ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
  1240. if (ret) {
  1241. return ret;
  1242. }
  1243. ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
  1244. if (ret) {
  1245. return ret;
  1246. }
  1247. ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
  1248. if (ret) {
  1249. return ret;
  1250. }
  1251. if (target_tv_addr) {
  1252. if (copy_from_user_timeval(&tv, target_tv_addr))
  1253. return -TARGET_EFAULT;
  1254. ts.tv_sec = tv.tv_sec;
  1255. ts.tv_nsec = tv.tv_usec * 1000;
  1256. ts_ptr = &ts;
  1257. } else {
  1258. ts_ptr = NULL;
  1259. }
  1260. ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
  1261. ts_ptr, NULL));
  1262. if (!is_error(ret)) {
  1263. if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
  1264. return -TARGET_EFAULT;
  1265. if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
  1266. return -TARGET_EFAULT;
  1267. if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
  1268. return -TARGET_EFAULT;
  1269. if (target_tv_addr) {
  1270. tv.tv_sec = ts.tv_sec;
  1271. tv.tv_usec = ts.tv_nsec / 1000;
  1272. if (copy_to_user_timeval(target_tv_addr, &tv)) {
  1273. return -TARGET_EFAULT;
  1274. }
  1275. }
  1276. }
  1277. return ret;
  1278. }
  1279. #if defined(TARGET_WANT_OLD_SYS_SELECT)
  1280. static abi_long do_old_select(abi_ulong arg1)
  1281. {
  1282. struct target_sel_arg_struct *sel;
  1283. abi_ulong inp, outp, exp, tvp;
  1284. long nsel;
  1285. if (!lock_user_struct(VERIFY_READ, sel, arg1, 1)) {
  1286. return -TARGET_EFAULT;
  1287. }
  1288. nsel = tswapal(sel->n);
  1289. inp = tswapal(sel->inp);
  1290. outp = tswapal(sel->outp);
  1291. exp = tswapal(sel->exp);
  1292. tvp = tswapal(sel->tvp);
  1293. unlock_user_struct(sel, arg1, 0);
  1294. return do_select(nsel, inp, outp, exp, tvp);
  1295. }
  1296. #endif
  1297. #endif
  1298. static abi_long do_pipe2(int host_pipe[], int flags)
  1299. {
  1300. #ifdef CONFIG_PIPE2
  1301. return pipe2(host_pipe, flags);
  1302. #else
  1303. return -ENOSYS;
  1304. #endif
  1305. }
  1306. static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
  1307. int flags, int is_pipe2)
  1308. {
  1309. int host_pipe[2];
  1310. abi_long ret;
  1311. ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
  1312. if (is_error(ret))
  1313. return get_errno(ret);
  1314. /* Several targets have special calling conventions for the original
  1315. pipe syscall, but didn't replicate this into the pipe2 syscall. */
  1316. if (!is_pipe2) {
  1317. #if defined(TARGET_ALPHA)
  1318. ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
  1319. return host_pipe[0];
  1320. #elif defined(TARGET_MIPS)
  1321. ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
  1322. return host_pipe[0];
  1323. #elif defined(TARGET_SH4)
  1324. ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
  1325. return host_pipe[0];
  1326. #elif defined(TARGET_SPARC)
  1327. ((CPUSPARCState*)cpu_env)->regwptr[1] = host_pipe[1];
  1328. return host_pipe[0];
  1329. #endif
  1330. }
  1331. if (put_user_s32(host_pipe[0], pipedes)
  1332. || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
  1333. return -TARGET_EFAULT;
  1334. return get_errno(ret);
  1335. }
  1336. static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
  1337. abi_ulong target_addr,
  1338. socklen_t len)
  1339. {
  1340. struct target_ip_mreqn *target_smreqn;
  1341. target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
  1342. if (!target_smreqn)
  1343. return -TARGET_EFAULT;
  1344. mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
  1345. mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
  1346. if (len == sizeof(struct target_ip_mreqn))
  1347. mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
  1348. unlock_user(target_smreqn, target_addr, 0);
  1349. return 0;
  1350. }
  1351. static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr,
  1352. abi_ulong target_addr,
  1353. socklen_t len)
  1354. {
  1355. const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
  1356. sa_family_t sa_family;
  1357. struct target_sockaddr *target_saddr;
  1358. if (fd_trans_target_to_host_addr(fd)) {
  1359. return fd_trans_target_to_host_addr(fd)(addr, target_addr, len);
  1360. }
  1361. target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
  1362. if (!target_saddr)
  1363. return -TARGET_EFAULT;
  1364. sa_family = tswap16(target_saddr->sa_family);
  1365. /* Oops. The caller might send a incomplete sun_path; sun_path
  1366. * must be terminated by \0 (see the manual page), but
  1367. * unfortunately it is quite common to specify sockaddr_un
  1368. * length as "strlen(x->sun_path)" while it should be
  1369. * "strlen(...) + 1". We'll fix that here if needed.
  1370. * Linux kernel has a similar feature.
  1371. */
  1372. if (sa_family == AF_UNIX) {
  1373. if (len < unix_maxlen && len > 0) {
  1374. char *cp = (char*)target_saddr;
  1375. if ( cp[len-1] && !cp[len] )
  1376. len++;
  1377. }
  1378. if (len > unix_maxlen)
  1379. len = unix_maxlen;
  1380. }
  1381. memcpy(addr, target_saddr, len);
  1382. addr->sa_family = sa_family;
  1383. if (sa_family == AF_NETLINK) {
  1384. struct sockaddr_nl *nladdr;
  1385. nladdr = (struct sockaddr_nl *)addr;
  1386. nladdr->nl_pid = tswap32(nladdr->nl_pid);
  1387. nladdr->nl_groups = tswap32(nladdr->nl_groups);
  1388. } else if (sa_family == AF_PACKET) {
  1389. struct target_sockaddr_ll *lladdr;
  1390. lladdr = (struct target_sockaddr_ll *)addr;
  1391. lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex);
  1392. lladdr->sll_hatype = tswap16(lladdr->sll_hatype);
  1393. }
  1394. unlock_user(target_saddr, target_addr, 0);
  1395. return 0;
  1396. }
  1397. static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
  1398. struct sockaddr *addr,
  1399. socklen_t len)
  1400. {
  1401. struct target_sockaddr *target_saddr;
  1402. if (len == 0) {
  1403. return 0;
  1404. }
  1405. assert(addr);
  1406. target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
  1407. if (!target_saddr)
  1408. return -TARGET_EFAULT;
  1409. memcpy(target_saddr, addr, len);
  1410. if (len >= offsetof(struct target_sockaddr, sa_family) +
  1411. sizeof(target_saddr->sa_family)) {
  1412. target_saddr->sa_family = tswap16(addr->sa_family);
  1413. }
  1414. if (addr->sa_family == AF_NETLINK &&
  1415. len >= sizeof(struct target_sockaddr_nl)) {
  1416. struct target_sockaddr_nl *target_nl =
  1417. (struct target_sockaddr_nl *)target_saddr;
  1418. target_nl->nl_pid = tswap32(target_nl->nl_pid);
  1419. target_nl->nl_groups = tswap32(target_nl->nl_groups);
  1420. } else if (addr->sa_family == AF_PACKET) {
  1421. struct sockaddr_ll *target_ll = (struct sockaddr_ll *)target_saddr;
  1422. target_ll->sll_ifindex = tswap32(target_ll->sll_ifindex);
  1423. target_ll->sll_hatype = tswap16(target_ll->sll_hatype);
  1424. } else if (addr->sa_family == AF_INET6 &&
  1425. len >= sizeof(struct target_sockaddr_in6)) {
  1426. struct target_sockaddr_in6 *target_in6 =
  1427. (struct target_sockaddr_in6 *)target_saddr;
  1428. target_in6->sin6_scope_id = tswap16(target_in6->sin6_scope_id);
  1429. }
  1430. unlock_user(target_saddr, target_addr, len);
  1431. return 0;
  1432. }
  1433. static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
  1434. struct target_msghdr *target_msgh)
  1435. {
  1436. struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
  1437. abi_long msg_controllen;
  1438. abi_ulong target_cmsg_addr;
  1439. struct target_cmsghdr *target_cmsg, *target_cmsg_start;
  1440. socklen_t space = 0;
  1441. msg_controllen = tswapal(target_msgh->msg_controllen);
  1442. if (msg_controllen < sizeof (struct target_cmsghdr))
  1443. goto the_end;
  1444. target_cmsg_addr = tswapal(target_msgh->msg_control);
  1445. target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
  1446. target_cmsg_start = target_cmsg;
  1447. if (!target_cmsg)
  1448. return -TARGET_EFAULT;
  1449. while (cmsg && target_cmsg) {
  1450. void *data = CMSG_DATA(cmsg);
  1451. void *target_data = TARGET_CMSG_DATA(target_cmsg);
  1452. int len = tswapal(target_cmsg->cmsg_len)
  1453. - sizeof(struct target_cmsghdr);
  1454. space += CMSG_SPACE(len);
  1455. if (space > msgh->msg_controllen) {
  1456. space -= CMSG_SPACE(len);
  1457. /* This is a QEMU bug, since we allocated the payload
  1458. * area ourselves (unlike overflow in host-to-target
  1459. * conversion, which is just the guest giving us a buffer
  1460. * that's too small). It can't happen for the payload types
  1461. * we currently support; if it becomes an issue in future
  1462. * we would need to improve our allocation strategy to
  1463. * something more intelligent than "twice the size of the
  1464. * target buffer we're reading from".
  1465. */
  1466. qemu_log_mask(LOG_UNIMP,
  1467. ("Unsupported ancillary data %d/%d: "
  1468. "unhandled msg size\n"),
  1469. tswap32(target_cmsg->cmsg_level),
  1470. tswap32(target_cmsg->cmsg_type));
  1471. break;
  1472. }
  1473. if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
  1474. cmsg->cmsg_level = SOL_SOCKET;
  1475. } else {
  1476. cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
  1477. }
  1478. cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
  1479. cmsg->cmsg_len = CMSG_LEN(len);
  1480. if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
  1481. int *fd = (int *)data;
  1482. int *target_fd = (int *)target_data;
  1483. int i, numfds = len / sizeof(int);
  1484. for (i = 0; i < numfds; i++) {
  1485. __get_user(fd[i], target_fd + i);
  1486. }
  1487. } else if (cmsg->cmsg_level == SOL_SOCKET
  1488. && cmsg->cmsg_type == SCM_CREDENTIALS) {
  1489. struct ucred *cred = (struct ucred *)data;
  1490. struct target_ucred *target_cred =
  1491. (struct target_ucred *)target_data;
  1492. __get_user(cred->pid, &target_cred->pid);
  1493. __get_user(cred->uid, &target_cred->uid);
  1494. __get_user(cred->gid, &target_cred->gid);
  1495. } else {
  1496. qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n",
  1497. cmsg->cmsg_level, cmsg->cmsg_type);
  1498. memcpy(data, target_data, len);
  1499. }
  1500. cmsg = CMSG_NXTHDR(msgh, cmsg);
  1501. target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
  1502. target_cmsg_start);
  1503. }
  1504. unlock_user(target_cmsg, target_cmsg_addr, 0);
  1505. the_end:
  1506. msgh->msg_controllen = space;
  1507. return 0;
  1508. }
  1509. static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
  1510. struct msghdr *msgh)
  1511. {
  1512. struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
  1513. abi_long msg_controllen;
  1514. abi_ulong target_cmsg_addr;
  1515. struct target_cmsghdr *target_cmsg, *target_cmsg_start;
  1516. socklen_t space = 0;
  1517. msg_controllen = tswapal(target_msgh->msg_controllen);
  1518. if (msg_controllen < sizeof (struct target_cmsghdr))
  1519. goto the_end;
  1520. target_cmsg_addr = tswapal(target_msgh->msg_control);
  1521. target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
  1522. target_cmsg_start = target_cmsg;
  1523. if (!target_cmsg)
  1524. return -TARGET_EFAULT;
  1525. while (cmsg && target_cmsg) {
  1526. void *data = CMSG_DATA(cmsg);
  1527. void *target_data = TARGET_CMSG_DATA(target_cmsg);
  1528. int len = cmsg->cmsg_len - sizeof(struct cmsghdr);
  1529. int tgt_len, tgt_space;
  1530. /* We never copy a half-header but may copy half-data;
  1531. * this is Linux's behaviour in put_cmsg(). Note that
  1532. * truncation here is a guest problem (which we report
  1533. * to the guest via the CTRUNC bit), unlike truncation
  1534. * in target_to_host_cmsg, which is a QEMU bug.
  1535. */
  1536. if (msg_controllen < sizeof(struct target_cmsghdr)) {
  1537. target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
  1538. break;
  1539. }
  1540. if (cmsg->cmsg_level == SOL_SOCKET) {
  1541. target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
  1542. } else {
  1543. target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
  1544. }
  1545. target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
  1546. /* Payload types which need a different size of payload on
  1547. * the target must adjust tgt_len here.
  1548. */
  1549. tgt_len = len;
  1550. switch (cmsg->cmsg_level) {
  1551. case SOL_SOCKET:
  1552. switch (cmsg->cmsg_type) {
  1553. case SO_TIMESTAMP:
  1554. tgt_len = sizeof(struct target_timeval);
  1555. break;
  1556. default:
  1557. break;
  1558. }
  1559. break;
  1560. default:
  1561. break;
  1562. }
  1563. if (msg_controllen < TARGET_CMSG_LEN(tgt_len)) {
  1564. target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
  1565. tgt_len = msg_controllen - sizeof(struct target_cmsghdr);
  1566. }
  1567. /* We must now copy-and-convert len bytes of payload
  1568. * into tgt_len bytes of destination space. Bear in mind
  1569. * that in both source and destination we may be dealing
  1570. * with a truncated value!
  1571. */
  1572. switch (cmsg->cmsg_level) {
  1573. case SOL_SOCKET:
  1574. switch (cmsg->cmsg_type) {
  1575. case SCM_RIGHTS:
  1576. {
  1577. int *fd = (int *)data;
  1578. int *target_fd = (int *)target_data;
  1579. int i, numfds = tgt_len / sizeof(int);
  1580. for (i = 0; i < numfds; i++) {
  1581. __put_user(fd[i], target_fd + i);
  1582. }
  1583. break;
  1584. }
  1585. case SO_TIMESTAMP:
  1586. {
  1587. struct timeval *tv = (struct timeval *)data;
  1588. struct target_timeval *target_tv =
  1589. (struct target_timeval *)target_data;
  1590. if (len != sizeof(struct timeval) ||
  1591. tgt_len != sizeof(struct target_timeval)) {
  1592. goto unimplemented;
  1593. }
  1594. /* copy struct timeval to target */
  1595. __put_user(tv->tv_sec, &target_tv->tv_sec);
  1596. __put_user(tv->tv_usec, &target_tv->tv_usec);
  1597. break;
  1598. }
  1599. case SCM_CREDENTIALS:
  1600. {
  1601. struct ucred *cred = (struct ucred *)data;
  1602. struct target_ucred *target_cred =
  1603. (struct target_ucred *)target_data;
  1604. __put_user(cred->pid, &target_cred->pid);
  1605. __put_user(cred->uid, &target_cred->uid);
  1606. __put_user(cred->gid, &target_cred->gid);
  1607. break;
  1608. }
  1609. default:
  1610. goto unimplemented;
  1611. }
  1612. break;
  1613. case SOL_IP:
  1614. switch (cmsg->cmsg_type) {
  1615. case IP_TTL:
  1616. {
  1617. uint32_t *v = (uint32_t *)data;
  1618. uint32_t *t_int = (uint32_t *)target_data;
  1619. if (len != sizeof(uint32_t) ||
  1620. tgt_len != sizeof(uint32_t)) {
  1621. goto unimplemented;
  1622. }
  1623. __put_user(*v, t_int);
  1624. break;
  1625. }
  1626. case IP_RECVERR:
  1627. {
  1628. struct errhdr_t {
  1629. struct sock_extended_err ee;
  1630. struct sockaddr_in offender;
  1631. };
  1632. struct errhdr_t *errh = (struct errhdr_t *)data;
  1633. struct errhdr_t *target_errh =
  1634. (struct errhdr_t *)target_data;
  1635. if (len != sizeof(struct errhdr_t) ||
  1636. tgt_len != sizeof(struct errhdr_t)) {
  1637. goto unimplemented;
  1638. }
  1639. __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
  1640. __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
  1641. __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
  1642. __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
  1643. __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
  1644. __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
  1645. __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
  1646. host_to_target_sockaddr((unsigned long) &target_errh->offender,
  1647. (void *) &errh->offender, sizeof(errh->offender));
  1648. break;
  1649. }
  1650. default:
  1651. goto unimplemented;
  1652. }
  1653. break;
  1654. case SOL_IPV6:
  1655. switch (cmsg->cmsg_type) {
  1656. case IPV6_HOPLIMIT:
  1657. {
  1658. uint32_t *v = (uint32_t *)data;
  1659. uint32_t *t_int = (uint32_t *)target_data;
  1660. if (len != sizeof(uint32_t) ||
  1661. tgt_len != sizeof(uint32_t)) {
  1662. goto unimplemented;
  1663. }
  1664. __put_user(*v, t_int);
  1665. break;
  1666. }
  1667. case IPV6_RECVERR:
  1668. {
  1669. struct errhdr6_t {
  1670. struct sock_extended_err ee;
  1671. struct sockaddr_in6 offender;
  1672. };
  1673. struct errhdr6_t *errh = (struct errhdr6_t *)data;
  1674. struct errhdr6_t *target_errh =
  1675. (struct errhdr6_t *)target_data;
  1676. if (len != sizeof(struct errhdr6_t) ||
  1677. tgt_len != sizeof(struct errhdr6_t)) {
  1678. goto unimplemented;
  1679. }
  1680. __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
  1681. __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
  1682. __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
  1683. __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
  1684. __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
  1685. __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
  1686. __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
  1687. host_to_target_sockaddr((unsigned long) &target_errh->offender,
  1688. (void *) &errh->offender, sizeof(errh->offender));
  1689. break;
  1690. }
  1691. default:
  1692. goto unimplemented;
  1693. }
  1694. break;
  1695. default:
  1696. unimplemented:
  1697. qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n",
  1698. cmsg->cmsg_level, cmsg->cmsg_type);
  1699. memcpy(target_data, data, MIN(len, tgt_len));
  1700. if (tgt_len > len) {
  1701. memset(target_data + len, 0, tgt_len - len);
  1702. }
  1703. }
  1704. target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(tgt_len));
  1705. tgt_space = TARGET_CMSG_SPACE(tgt_len);
  1706. if (msg_controllen < tgt_space) {
  1707. tgt_space = msg_controllen;
  1708. }
  1709. msg_controllen -= tgt_space;
  1710. space += tgt_space;
  1711. cmsg = CMSG_NXTHDR(msgh, cmsg);
  1712. target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
  1713. target_cmsg_start);
  1714. }
  1715. unlock_user(target_cmsg, target_cmsg_addr, space);
  1716. the_end:
  1717. target_msgh->msg_controllen = tswapal(space);
  1718. return 0;
  1719. }
  1720. /* do_setsockopt() Must return target values and target errnos. */
  1721. static abi_long do_setsockopt(int sockfd, int level, int optname,
  1722. abi_ulong optval_addr, socklen_t optlen)
  1723. {
  1724. abi_long ret;
  1725. int val;
  1726. struct ip_mreqn *ip_mreq;
  1727. struct ip_mreq_source *ip_mreq_source;
  1728. switch(level) {
  1729. case SOL_TCP:
  1730. /* TCP options all take an 'int' value. */
  1731. if (optlen < sizeof(uint32_t))
  1732. return -TARGET_EINVAL;
  1733. if (get_user_u32(val, optval_addr))
  1734. return -TARGET_EFAULT;
  1735. ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
  1736. break;
  1737. case SOL_IP:
  1738. switch(optname) {
  1739. case IP_TOS:
  1740. case IP_TTL:
  1741. case IP_HDRINCL:
  1742. case IP_ROUTER_ALERT:
  1743. case IP_RECVOPTS:
  1744. case IP_RETOPTS:
  1745. case IP_PKTINFO:
  1746. case IP_MTU_DISCOVER:
  1747. case IP_RECVERR:
  1748. case IP_RECVTTL:
  1749. case IP_RECVTOS:
  1750. #ifdef IP_FREEBIND
  1751. case IP_FREEBIND:
  1752. #endif
  1753. case IP_MULTICAST_TTL:
  1754. case IP_MULTICAST_LOOP:
  1755. val = 0;
  1756. if (optlen >= sizeof(uint32_t)) {
  1757. if (get_user_u32(val, optval_addr))
  1758. return -TARGET_EFAULT;
  1759. } else if (optlen >= 1) {
  1760. if (get_user_u8(val, optval_addr))
  1761. return -TARGET_EFAULT;
  1762. }
  1763. ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
  1764. break;
  1765. case IP_ADD_MEMBERSHIP:
  1766. case IP_DROP_MEMBERSHIP:
  1767. if (optlen < sizeof (struct target_ip_mreq) ||
  1768. optlen > sizeof (struct target_ip_mreqn))
  1769. return -TARGET_EINVAL;
  1770. ip_mreq = (struct ip_mreqn *) alloca(optlen);
  1771. target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
  1772. ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
  1773. break;
  1774. case IP_BLOCK_SOURCE:
  1775. case IP_UNBLOCK_SOURCE:
  1776. case IP_ADD_SOURCE_MEMBERSHIP:
  1777. case IP_DROP_SOURCE_MEMBERSHIP:
  1778. if (optlen != sizeof (struct target_ip_mreq_source))
  1779. return -TARGET_EINVAL;
  1780. ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
  1781. ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
  1782. unlock_user (ip_mreq_source, optval_addr, 0);
  1783. break;
  1784. default:
  1785. goto unimplemented;
  1786. }
  1787. break;
  1788. case SOL_IPV6:
  1789. switch (optname) {
  1790. case IPV6_MTU_DISCOVER:
  1791. case IPV6_MTU:
  1792. case IPV6_V6ONLY:
  1793. case IPV6_RECVPKTINFO:
  1794. case IPV6_UNICAST_HOPS:
  1795. case IPV6_MULTICAST_HOPS:
  1796. case IPV6_MULTICAST_LOOP:
  1797. case IPV6_RECVERR:
  1798. case IPV6_RECVHOPLIMIT:
  1799. case IPV6_2292HOPLIMIT:
  1800. case IPV6_CHECKSUM:
  1801. case IPV6_ADDRFORM:
  1802. case IPV6_2292PKTINFO:
  1803. case IPV6_RECVTCLASS:
  1804. case IPV6_RECVRTHDR:
  1805. case IPV6_2292RTHDR:
  1806. case IPV6_RECVHOPOPTS:
  1807. case IPV6_2292HOPOPTS:
  1808. case IPV6_RECVDSTOPTS:
  1809. case IPV6_2292DSTOPTS:
  1810. case IPV6_TCLASS:
  1811. #ifdef IPV6_RECVPATHMTU
  1812. case IPV6_RECVPATHMTU:
  1813. #endif
  1814. #ifdef IPV6_TRANSPARENT
  1815. case IPV6_TRANSPARENT:
  1816. #endif
  1817. #ifdef IPV6_FREEBIND
  1818. case IPV6_FREEBIND:
  1819. #endif
  1820. #ifdef IPV6_RECVORIGDSTADDR
  1821. case IPV6_RECVORIGDSTADDR:
  1822. #endif
  1823. val = 0;
  1824. if (optlen < sizeof(uint32_t)) {
  1825. return -TARGET_EINVAL;
  1826. }
  1827. if (get_user_u32(val, optval_addr)) {
  1828. return -TARGET_EFAULT;
  1829. }
  1830. ret = get_errno(setsockopt(sockfd, level, optname,
  1831. &val, sizeof(val)));
  1832. break;
  1833. case IPV6_PKTINFO:
  1834. {
  1835. struct in6_pktinfo pki;
  1836. if (optlen < sizeof(pki)) {
  1837. return -TARGET_EINVAL;
  1838. }
  1839. if (copy_from_user(&pki, optval_addr, sizeof(pki))) {
  1840. return -TARGET_EFAULT;
  1841. }
  1842. pki.ipi6_ifindex = tswap32(pki.ipi6_ifindex);
  1843. ret = get_errno(setsockopt(sockfd, level, optname,
  1844. &pki, sizeof(pki)));
  1845. break;
  1846. }
  1847. case IPV6_ADD_MEMBERSHIP:
  1848. case IPV6_DROP_MEMBERSHIP:
  1849. {
  1850. struct ipv6_mreq ipv6mreq;
  1851. if (optlen < sizeof(ipv6mreq)) {
  1852. return -TARGET_EINVAL;
  1853. }
  1854. if (copy_from_user(&ipv6mreq, optval_addr, sizeof(ipv6mreq))) {
  1855. return -TARGET_EFAULT;
  1856. }
  1857. ipv6mreq.ipv6mr_interface = tswap32(ipv6mreq.ipv6mr_interface);
  1858. ret = get_errno(setsockopt(sockfd, level, optname,
  1859. &ipv6mreq, sizeof(ipv6mreq)));
  1860. break;
  1861. }
  1862. default:
  1863. goto unimplemented;
  1864. }
  1865. break;
  1866. case SOL_ICMPV6:
  1867. switch (optname) {
  1868. case ICMPV6_FILTER:
  1869. {
  1870. struct icmp6_filter icmp6f;
  1871. if (optlen > sizeof(icmp6f)) {
  1872. optlen = sizeof(icmp6f);
  1873. }
  1874. if (copy_from_user(&icmp6f, optval_addr, optlen)) {
  1875. return -TARGET_EFAULT;
  1876. }
  1877. for (val = 0; val < 8; val++) {
  1878. icmp6f.data[val] = tswap32(icmp6f.data[val]);
  1879. }
  1880. ret = get_errno(setsockopt(sockfd, level, optname,
  1881. &icmp6f, optlen));
  1882. break;
  1883. }
  1884. default:
  1885. goto unimplemented;
  1886. }
  1887. break;
  1888. case SOL_RAW:
  1889. switch (optname) {
  1890. case ICMP_FILTER:
  1891. case IPV6_CHECKSUM:
  1892. /* those take an u32 value */
  1893. if (optlen < sizeof(uint32_t)) {
  1894. return -TARGET_EINVAL;
  1895. }
  1896. if (get_user_u32(val, optval_addr)) {
  1897. return -TARGET_EFAULT;
  1898. }
  1899. ret = get_errno(setsockopt(sockfd, level, optname,
  1900. &val, sizeof(val)));
  1901. break;
  1902. default:
  1903. goto unimplemented;
  1904. }
  1905. break;
  1906. #if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE)
  1907. case SOL_ALG:
  1908. switch (optname) {
  1909. case ALG_SET_KEY:
  1910. {
  1911. char *alg_key = g_malloc(optlen);
  1912. if (!alg_key) {
  1913. return -TARGET_ENOMEM;
  1914. }
  1915. if (copy_from_user(alg_key, optval_addr, optlen)) {
  1916. g_free(alg_key);
  1917. return -TARGET_EFAULT;
  1918. }
  1919. ret = get_errno(setsockopt(sockfd, level, optname,
  1920. alg_key, optlen));
  1921. g_free(alg_key);
  1922. break;
  1923. }
  1924. case ALG_SET_AEAD_AUTHSIZE:
  1925. {
  1926. ret = get_errno(setsockopt(sockfd, level, optname,
  1927. NULL, optlen));
  1928. break;
  1929. }
  1930. default:
  1931. goto unimplemented;
  1932. }
  1933. break;
  1934. #endif
  1935. case TARGET_SOL_SOCKET:
  1936. switch (optname) {
  1937. case TARGET_SO_RCVTIMEO:
  1938. {
  1939. struct timeval tv;
  1940. optname = SO_RCVTIMEO;
  1941. set_timeout:
  1942. if (optlen != sizeof(struct target_timeval)) {
  1943. return -TARGET_EINVAL;
  1944. }
  1945. if (copy_from_user_timeval(&tv, optval_addr)) {
  1946. return -TARGET_EFAULT;
  1947. }
  1948. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
  1949. &tv, sizeof(tv)));
  1950. return ret;
  1951. }
  1952. case TARGET_SO_SNDTIMEO:
  1953. optname = SO_SNDTIMEO;
  1954. goto set_timeout;
  1955. case TARGET_SO_ATTACH_FILTER:
  1956. {
  1957. struct target_sock_fprog *tfprog;
  1958. struct target_sock_filter *tfilter;
  1959. struct sock_fprog fprog;
  1960. struct sock_filter *filter;
  1961. int i;
  1962. if (optlen != sizeof(*tfprog)) {
  1963. return -TARGET_EINVAL;
  1964. }
  1965. if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
  1966. return -TARGET_EFAULT;
  1967. }
  1968. if (!lock_user_struct(VERIFY_READ, tfilter,
  1969. tswapal(tfprog->filter), 0)) {
  1970. unlock_user_struct(tfprog, optval_addr, 1);
  1971. return -TARGET_EFAULT;
  1972. }
  1973. fprog.len = tswap16(tfprog->len);
  1974. filter = g_try_new(struct sock_filter, fprog.len);
  1975. if (filter == NULL) {
  1976. unlock_user_struct(tfilter, tfprog->filter, 1);
  1977. unlock_user_struct(tfprog, optval_addr, 1);
  1978. return -TARGET_ENOMEM;
  1979. }
  1980. for (i = 0; i < fprog.len; i++) {
  1981. filter[i].code = tswap16(tfilter[i].code);
  1982. filter[i].jt = tfilter[i].jt;
  1983. filter[i].jf = tfilter[i].jf;
  1984. filter[i].k = tswap32(tfilter[i].k);
  1985. }
  1986. fprog.filter = filter;
  1987. ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
  1988. SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
  1989. g_free(filter);
  1990. unlock_user_struct(tfilter, tfprog->filter, 1);
  1991. unlock_user_struct(tfprog, optval_addr, 1);
  1992. return ret;
  1993. }
  1994. case TARGET_SO_BINDTODEVICE:
  1995. {
  1996. char *dev_ifname, *addr_ifname;
  1997. if (optlen > IFNAMSIZ - 1) {
  1998. optlen = IFNAMSIZ - 1;
  1999. }
  2000. dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1);
  2001. if (!dev_ifname) {
  2002. return -TARGET_EFAULT;
  2003. }
  2004. optname = SO_BINDTODEVICE;
  2005. addr_ifname = alloca(IFNAMSIZ);
  2006. memcpy(addr_ifname, dev_ifname, optlen);
  2007. addr_ifname[optlen] = 0;
  2008. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
  2009. addr_ifname, optlen));
  2010. unlock_user (dev_ifname, optval_addr, 0);
  2011. return ret;
  2012. }
  2013. case TARGET_SO_LINGER:
  2014. {
  2015. struct linger lg;
  2016. struct target_linger *tlg;
  2017. if (optlen != sizeof(struct target_linger)) {
  2018. return -TARGET_EINVAL;
  2019. }
  2020. if (!lock_user_struct(VERIFY_READ, tlg, optval_addr, 1)) {
  2021. return -TARGET_EFAULT;
  2022. }
  2023. __get_user(lg.l_onoff, &tlg->l_onoff);
  2024. __get_user(lg.l_linger, &tlg->l_linger);
  2025. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, SO_LINGER,
  2026. &lg, sizeof(lg)));
  2027. unlock_user_struct(tlg, optval_addr, 0);
  2028. return ret;
  2029. }
  2030. /* Options with 'int' argument. */
  2031. case TARGET_SO_DEBUG:
  2032. optname = SO_DEBUG;
  2033. break;
  2034. case TARGET_SO_REUSEADDR:
  2035. optname = SO_REUSEADDR;
  2036. break;
  2037. #ifdef SO_REUSEPORT
  2038. case TARGET_SO_REUSEPORT:
  2039. optname = SO_REUSEPORT;
  2040. break;
  2041. #endif
  2042. case TARGET_SO_TYPE:
  2043. optname = SO_TYPE;
  2044. break;
  2045. case TARGET_SO_ERROR:
  2046. optname = SO_ERROR;
  2047. break;
  2048. case TARGET_SO_DONTROUTE:
  2049. optname = SO_DONTROUTE;
  2050. break;
  2051. case TARGET_SO_BROADCAST:
  2052. optname = SO_BROADCAST;
  2053. break;
  2054. case TARGET_SO_SNDBUF:
  2055. optname = SO_SNDBUF;
  2056. break;
  2057. case TARGET_SO_SNDBUFFORCE:
  2058. optname = SO_SNDBUFFORCE;
  2059. break;
  2060. case TARGET_SO_RCVBUF:
  2061. optname = SO_RCVBUF;
  2062. break;
  2063. case TARGET_SO_RCVBUFFORCE:
  2064. optname = SO_RCVBUFFORCE;
  2065. break;
  2066. case TARGET_SO_KEEPALIVE:
  2067. optname = SO_KEEPALIVE;
  2068. break;
  2069. case TARGET_SO_OOBINLINE:
  2070. optname = SO_OOBINLINE;
  2071. break;
  2072. case TARGET_SO_NO_CHECK:
  2073. optname = SO_NO_CHECK;
  2074. break;
  2075. case TARGET_SO_PRIORITY:
  2076. optname = SO_PRIORITY;
  2077. break;
  2078. #ifdef SO_BSDCOMPAT
  2079. case TARGET_SO_BSDCOMPAT:
  2080. optname = SO_BSDCOMPAT;
  2081. break;
  2082. #endif
  2083. case TARGET_SO_PASSCRED:
  2084. optname = SO_PASSCRED;
  2085. break;
  2086. case TARGET_SO_PASSSEC:
  2087. optname = SO_PASSSEC;
  2088. break;
  2089. case TARGET_SO_TIMESTAMP:
  2090. optname = SO_TIMESTAMP;
  2091. break;
  2092. case TARGET_SO_RCVLOWAT:
  2093. optname = SO_RCVLOWAT;
  2094. break;
  2095. default:
  2096. goto unimplemented;
  2097. }
  2098. if (optlen < sizeof(uint32_t))
  2099. return -TARGET_EINVAL;
  2100. if (get_user_u32(val, optval_addr))
  2101. return -TARGET_EFAULT;
  2102. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
  2103. break;
  2104. #ifdef SOL_NETLINK
  2105. case SOL_NETLINK:
  2106. switch (optname) {
  2107. case NETLINK_PKTINFO:
  2108. case NETLINK_ADD_MEMBERSHIP:
  2109. case NETLINK_DROP_MEMBERSHIP:
  2110. case NETLINK_BROADCAST_ERROR:
  2111. case NETLINK_NO_ENOBUFS:
  2112. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
  2113. case NETLINK_LISTEN_ALL_NSID:
  2114. case NETLINK_CAP_ACK:
  2115. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
  2116. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
  2117. case NETLINK_EXT_ACK:
  2118. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2119. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
  2120. case NETLINK_GET_STRICT_CHK:
  2121. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2122. break;
  2123. default:
  2124. goto unimplemented;
  2125. }
  2126. val = 0;
  2127. if (optlen < sizeof(uint32_t)) {
  2128. return -TARGET_EINVAL;
  2129. }
  2130. if (get_user_u32(val, optval_addr)) {
  2131. return -TARGET_EFAULT;
  2132. }
  2133. ret = get_errno(setsockopt(sockfd, SOL_NETLINK, optname, &val,
  2134. sizeof(val)));
  2135. break;
  2136. #endif /* SOL_NETLINK */
  2137. default:
  2138. unimplemented:
  2139. qemu_log_mask(LOG_UNIMP, "Unsupported setsockopt level=%d optname=%d\n",
  2140. level, optname);
  2141. ret = -TARGET_ENOPROTOOPT;
  2142. }
  2143. return ret;
  2144. }
  2145. /* do_getsockopt() Must return target values and target errnos. */
  2146. static abi_long do_getsockopt(int sockfd, int level, int optname,
  2147. abi_ulong optval_addr, abi_ulong optlen)
  2148. {
  2149. abi_long ret;
  2150. int len, val;
  2151. socklen_t lv;
  2152. switch(level) {
  2153. case TARGET_SOL_SOCKET:
  2154. level = SOL_SOCKET;
  2155. switch (optname) {
  2156. /* These don't just return a single integer */
  2157. case TARGET_SO_PEERNAME:
  2158. goto unimplemented;
  2159. case TARGET_SO_RCVTIMEO: {
  2160. struct timeval tv;
  2161. socklen_t tvlen;
  2162. optname = SO_RCVTIMEO;
  2163. get_timeout:
  2164. if (get_user_u32(len, optlen)) {
  2165. return -TARGET_EFAULT;
  2166. }
  2167. if (len < 0) {
  2168. return -TARGET_EINVAL;
  2169. }
  2170. tvlen = sizeof(tv);
  2171. ret = get_errno(getsockopt(sockfd, level, optname,
  2172. &tv, &tvlen));
  2173. if (ret < 0) {
  2174. return ret;
  2175. }
  2176. if (len > sizeof(struct target_timeval)) {
  2177. len = sizeof(struct target_timeval);
  2178. }
  2179. if (copy_to_user_timeval(optval_addr, &tv)) {
  2180. return -TARGET_EFAULT;
  2181. }
  2182. if (put_user_u32(len, optlen)) {
  2183. return -TARGET_EFAULT;
  2184. }
  2185. break;
  2186. }
  2187. case TARGET_SO_SNDTIMEO:
  2188. optname = SO_SNDTIMEO;
  2189. goto get_timeout;
  2190. case TARGET_SO_PEERCRED: {
  2191. struct ucred cr;
  2192. socklen_t crlen;
  2193. struct target_ucred *tcr;
  2194. if (get_user_u32(len, optlen)) {
  2195. return -TARGET_EFAULT;
  2196. }
  2197. if (len < 0) {
  2198. return -TARGET_EINVAL;
  2199. }
  2200. crlen = sizeof(cr);
  2201. ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
  2202. &cr, &crlen));
  2203. if (ret < 0) {
  2204. return ret;
  2205. }
  2206. if (len > crlen) {
  2207. len = crlen;
  2208. }
  2209. if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
  2210. return -TARGET_EFAULT;
  2211. }
  2212. __put_user(cr.pid, &tcr->pid);
  2213. __put_user(cr.uid, &tcr->uid);
  2214. __put_user(cr.gid, &tcr->gid);
  2215. unlock_user_struct(tcr, optval_addr, 1);
  2216. if (put_user_u32(len, optlen)) {
  2217. return -TARGET_EFAULT;
  2218. }
  2219. break;
  2220. }
  2221. case TARGET_SO_PEERSEC: {
  2222. char *name;
  2223. if (get_user_u32(len, optlen)) {
  2224. return -TARGET_EFAULT;
  2225. }
  2226. if (len < 0) {
  2227. return -TARGET_EINVAL;
  2228. }
  2229. name = lock_user(VERIFY_WRITE, optval_addr, len, 0);
  2230. if (!name) {
  2231. return -TARGET_EFAULT;
  2232. }
  2233. lv = len;
  2234. ret = get_errno(getsockopt(sockfd, level, SO_PEERSEC,
  2235. name, &lv));
  2236. if (put_user_u32(lv, optlen)) {
  2237. ret = -TARGET_EFAULT;
  2238. }
  2239. unlock_user(name, optval_addr, lv);
  2240. break;
  2241. }
  2242. case TARGET_SO_LINGER:
  2243. {
  2244. struct linger lg;
  2245. socklen_t lglen;
  2246. struct target_linger *tlg;
  2247. if (get_user_u32(len, optlen)) {
  2248. return -TARGET_EFAULT;
  2249. }
  2250. if (len < 0) {
  2251. return -TARGET_EINVAL;
  2252. }
  2253. lglen = sizeof(lg);
  2254. ret = get_errno(getsockopt(sockfd, level, SO_LINGER,
  2255. &lg, &lglen));
  2256. if (ret < 0) {
  2257. return ret;
  2258. }
  2259. if (len > lglen) {
  2260. len = lglen;
  2261. }
  2262. if (!lock_user_struct(VERIFY_WRITE, tlg, optval_addr, 0)) {
  2263. return -TARGET_EFAULT;
  2264. }
  2265. __put_user(lg.l_onoff, &tlg->l_onoff);
  2266. __put_user(lg.l_linger, &tlg->l_linger);
  2267. unlock_user_struct(tlg, optval_addr, 1);
  2268. if (put_user_u32(len, optlen)) {
  2269. return -TARGET_EFAULT;
  2270. }
  2271. break;
  2272. }
  2273. /* Options with 'int' argument. */
  2274. case TARGET_SO_DEBUG:
  2275. optname = SO_DEBUG;
  2276. goto int_case;
  2277. case TARGET_SO_REUSEADDR:
  2278. optname = SO_REUSEADDR;
  2279. goto int_case;
  2280. #ifdef SO_REUSEPORT
  2281. case TARGET_SO_REUSEPORT:
  2282. optname = SO_REUSEPORT;
  2283. goto int_case;
  2284. #endif
  2285. case TARGET_SO_TYPE:
  2286. optname = SO_TYPE;
  2287. goto int_case;
  2288. case TARGET_SO_ERROR:
  2289. optname = SO_ERROR;
  2290. goto int_case;
  2291. case TARGET_SO_DONTROUTE:
  2292. optname = SO_DONTROUTE;
  2293. goto int_case;
  2294. case TARGET_SO_BROADCAST:
  2295. optname = SO_BROADCAST;
  2296. goto int_case;
  2297. case TARGET_SO_SNDBUF:
  2298. optname = SO_SNDBUF;
  2299. goto int_case;
  2300. case TARGET_SO_RCVBUF:
  2301. optname = SO_RCVBUF;
  2302. goto int_case;
  2303. case TARGET_SO_KEEPALIVE:
  2304. optname = SO_KEEPALIVE;
  2305. goto int_case;
  2306. case TARGET_SO_OOBINLINE:
  2307. optname = SO_OOBINLINE;
  2308. goto int_case;
  2309. case TARGET_SO_NO_CHECK:
  2310. optname = SO_NO_CHECK;
  2311. goto int_case;
  2312. case TARGET_SO_PRIORITY:
  2313. optname = SO_PRIORITY;
  2314. goto int_case;
  2315. #ifdef SO_BSDCOMPAT
  2316. case TARGET_SO_BSDCOMPAT:
  2317. optname = SO_BSDCOMPAT;
  2318. goto int_case;
  2319. #endif
  2320. case TARGET_SO_PASSCRED:
  2321. optname = SO_PASSCRED;
  2322. goto int_case;
  2323. case TARGET_SO_TIMESTAMP:
  2324. optname = SO_TIMESTAMP;
  2325. goto int_case;
  2326. case TARGET_SO_RCVLOWAT:
  2327. optname = SO_RCVLOWAT;
  2328. goto int_case;
  2329. case TARGET_SO_ACCEPTCONN:
  2330. optname = SO_ACCEPTCONN;
  2331. goto int_case;
  2332. default:
  2333. goto int_case;
  2334. }
  2335. break;
  2336. case SOL_TCP:
  2337. /* TCP options all take an 'int' value. */
  2338. int_case:
  2339. if (get_user_u32(len, optlen))
  2340. return -TARGET_EFAULT;
  2341. if (len < 0)
  2342. return -TARGET_EINVAL;
  2343. lv = sizeof(lv);
  2344. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2345. if (ret < 0)
  2346. return ret;
  2347. if (optname == SO_TYPE) {
  2348. val = host_to_target_sock_type(val);
  2349. }
  2350. if (len > lv)
  2351. len = lv;
  2352. if (len == 4) {
  2353. if (put_user_u32(val, optval_addr))
  2354. return -TARGET_EFAULT;
  2355. } else {
  2356. if (put_user_u8(val, optval_addr))
  2357. return -TARGET_EFAULT;
  2358. }
  2359. if (put_user_u32(len, optlen))
  2360. return -TARGET_EFAULT;
  2361. break;
  2362. case SOL_IP:
  2363. switch(optname) {
  2364. case IP_TOS:
  2365. case IP_TTL:
  2366. case IP_HDRINCL:
  2367. case IP_ROUTER_ALERT:
  2368. case IP_RECVOPTS:
  2369. case IP_RETOPTS:
  2370. case IP_PKTINFO:
  2371. case IP_MTU_DISCOVER:
  2372. case IP_RECVERR:
  2373. case IP_RECVTOS:
  2374. #ifdef IP_FREEBIND
  2375. case IP_FREEBIND:
  2376. #endif
  2377. case IP_MULTICAST_TTL:
  2378. case IP_MULTICAST_LOOP:
  2379. if (get_user_u32(len, optlen))
  2380. return -TARGET_EFAULT;
  2381. if (len < 0)
  2382. return -TARGET_EINVAL;
  2383. lv = sizeof(lv);
  2384. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2385. if (ret < 0)
  2386. return ret;
  2387. if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
  2388. len = 1;
  2389. if (put_user_u32(len, optlen)
  2390. || put_user_u8(val, optval_addr))
  2391. return -TARGET_EFAULT;
  2392. } else {
  2393. if (len > sizeof(int))
  2394. len = sizeof(int);
  2395. if (put_user_u32(len, optlen)
  2396. || put_user_u32(val, optval_addr))
  2397. return -TARGET_EFAULT;
  2398. }
  2399. break;
  2400. default:
  2401. ret = -TARGET_ENOPROTOOPT;
  2402. break;
  2403. }
  2404. break;
  2405. case SOL_IPV6:
  2406. switch (optname) {
  2407. case IPV6_MTU_DISCOVER:
  2408. case IPV6_MTU:
  2409. case IPV6_V6ONLY:
  2410. case IPV6_RECVPKTINFO:
  2411. case IPV6_UNICAST_HOPS:
  2412. case IPV6_MULTICAST_HOPS:
  2413. case IPV6_MULTICAST_LOOP:
  2414. case IPV6_RECVERR:
  2415. case IPV6_RECVHOPLIMIT:
  2416. case IPV6_2292HOPLIMIT:
  2417. case IPV6_CHECKSUM:
  2418. case IPV6_ADDRFORM:
  2419. case IPV6_2292PKTINFO:
  2420. case IPV6_RECVTCLASS:
  2421. case IPV6_RECVRTHDR:
  2422. case IPV6_2292RTHDR:
  2423. case IPV6_RECVHOPOPTS:
  2424. case IPV6_2292HOPOPTS:
  2425. case IPV6_RECVDSTOPTS:
  2426. case IPV6_2292DSTOPTS:
  2427. case IPV6_TCLASS:
  2428. #ifdef IPV6_RECVPATHMTU
  2429. case IPV6_RECVPATHMTU:
  2430. #endif
  2431. #ifdef IPV6_TRANSPARENT
  2432. case IPV6_TRANSPARENT:
  2433. #endif
  2434. #ifdef IPV6_FREEBIND
  2435. case IPV6_FREEBIND:
  2436. #endif
  2437. #ifdef IPV6_RECVORIGDSTADDR
  2438. case IPV6_RECVORIGDSTADDR:
  2439. #endif
  2440. if (get_user_u32(len, optlen))
  2441. return -TARGET_EFAULT;
  2442. if (len < 0)
  2443. return -TARGET_EINVAL;
  2444. lv = sizeof(lv);
  2445. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2446. if (ret < 0)
  2447. return ret;
  2448. if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
  2449. len = 1;
  2450. if (put_user_u32(len, optlen)
  2451. || put_user_u8(val, optval_addr))
  2452. return -TARGET_EFAULT;
  2453. } else {
  2454. if (len > sizeof(int))
  2455. len = sizeof(int);
  2456. if (put_user_u32(len, optlen)
  2457. || put_user_u32(val, optval_addr))
  2458. return -TARGET_EFAULT;
  2459. }
  2460. break;
  2461. default:
  2462. ret = -TARGET_ENOPROTOOPT;
  2463. break;
  2464. }
  2465. break;
  2466. #ifdef SOL_NETLINK
  2467. case SOL_NETLINK:
  2468. switch (optname) {
  2469. case NETLINK_PKTINFO:
  2470. case NETLINK_BROADCAST_ERROR:
  2471. case NETLINK_NO_ENOBUFS:
  2472. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
  2473. case NETLINK_LISTEN_ALL_NSID:
  2474. case NETLINK_CAP_ACK:
  2475. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
  2476. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
  2477. case NETLINK_EXT_ACK:
  2478. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2479. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
  2480. case NETLINK_GET_STRICT_CHK:
  2481. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2482. if (get_user_u32(len, optlen)) {
  2483. return -TARGET_EFAULT;
  2484. }
  2485. if (len != sizeof(val)) {
  2486. return -TARGET_EINVAL;
  2487. }
  2488. lv = len;
  2489. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2490. if (ret < 0) {
  2491. return ret;
  2492. }
  2493. if (put_user_u32(lv, optlen)
  2494. || put_user_u32(val, optval_addr)) {
  2495. return -TARGET_EFAULT;
  2496. }
  2497. break;
  2498. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
  2499. case NETLINK_LIST_MEMBERSHIPS:
  2500. {
  2501. uint32_t *results;
  2502. int i;
  2503. if (get_user_u32(len, optlen)) {
  2504. return -TARGET_EFAULT;
  2505. }
  2506. if (len < 0) {
  2507. return -TARGET_EINVAL;
  2508. }
  2509. results = lock_user(VERIFY_WRITE, optval_addr, len, 1);
  2510. if (!results) {
  2511. return -TARGET_EFAULT;
  2512. }
  2513. lv = len;
  2514. ret = get_errno(getsockopt(sockfd, level, optname, results, &lv));
  2515. if (ret < 0) {
  2516. unlock_user(results, optval_addr, 0);
  2517. return ret;
  2518. }
  2519. /* swap host endianess to target endianess. */
  2520. for (i = 0; i < (len / sizeof(uint32_t)); i++) {
  2521. results[i] = tswap32(results[i]);
  2522. }
  2523. if (put_user_u32(lv, optlen)) {
  2524. return -TARGET_EFAULT;
  2525. }
  2526. unlock_user(results, optval_addr, 0);
  2527. break;
  2528. }
  2529. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
  2530. default:
  2531. goto unimplemented;
  2532. }
  2533. break;
  2534. #endif /* SOL_NETLINK */
  2535. default:
  2536. unimplemented:
  2537. qemu_log_mask(LOG_UNIMP,
  2538. "getsockopt level=%d optname=%d not yet supported\n",
  2539. level, optname);
  2540. ret = -TARGET_EOPNOTSUPP;
  2541. break;
  2542. }
  2543. return ret;
  2544. }
  2545. /* Convert target low/high pair representing file offset into the host
  2546. * low/high pair. This function doesn't handle offsets bigger than 64 bits
  2547. * as the kernel doesn't handle them either.
  2548. */
  2549. static void target_to_host_low_high(abi_ulong tlow,
  2550. abi_ulong thigh,
  2551. unsigned long *hlow,
  2552. unsigned long *hhigh)
  2553. {
  2554. uint64_t off = tlow |
  2555. ((unsigned long long)thigh << TARGET_LONG_BITS / 2) <<
  2556. TARGET_LONG_BITS / 2;
  2557. *hlow = off;
  2558. *hhigh = (off >> HOST_LONG_BITS / 2) >> HOST_LONG_BITS / 2;
  2559. }
  2560. static struct iovec *lock_iovec(int type, abi_ulong target_addr,
  2561. abi_ulong count, int copy)
  2562. {
  2563. struct target_iovec *target_vec;
  2564. struct iovec *vec;
  2565. abi_ulong total_len, max_len;
  2566. int i;
  2567. int err = 0;
  2568. bool bad_address = false;
  2569. if (count == 0) {
  2570. errno = 0;
  2571. return NULL;
  2572. }
  2573. if (count > IOV_MAX) {
  2574. errno = EINVAL;
  2575. return NULL;
  2576. }
  2577. vec = g_try_new0(struct iovec, count);
  2578. if (vec == NULL) {
  2579. errno = ENOMEM;
  2580. return NULL;
  2581. }
  2582. target_vec = lock_user(VERIFY_READ, target_addr,
  2583. count * sizeof(struct target_iovec), 1);
  2584. if (target_vec == NULL) {
  2585. err = EFAULT;
  2586. goto fail2;
  2587. }
  2588. /* ??? If host page size > target page size, this will result in a
  2589. value larger than what we can actually support. */
  2590. max_len = 0x7fffffff & TARGET_PAGE_MASK;
  2591. total_len = 0;
  2592. for (i = 0; i < count; i++) {
  2593. abi_ulong base = tswapal(target_vec[i].iov_base);
  2594. abi_long len = tswapal(target_vec[i].iov_len);
  2595. if (len < 0) {
  2596. err = EINVAL;
  2597. goto fail;
  2598. } else if (len == 0) {
  2599. /* Zero length pointer is ignored. */
  2600. vec[i].iov_base = 0;
  2601. } else {
  2602. vec[i].iov_base = lock_user(type, base, len, copy);
  2603. /* If the first buffer pointer is bad, this is a fault. But
  2604. * subsequent bad buffers will result in a partial write; this
  2605. * is realized by filling the vector with null pointers and
  2606. * zero lengths. */
  2607. if (!vec[i].iov_base) {
  2608. if (i == 0) {
  2609. err = EFAULT;
  2610. goto fail;
  2611. } else {
  2612. bad_address = true;
  2613. }
  2614. }
  2615. if (bad_address) {
  2616. len = 0;
  2617. }
  2618. if (len > max_len - total_len) {
  2619. len = max_len - total_len;
  2620. }
  2621. }
  2622. vec[i].iov_len = len;
  2623. total_len += len;
  2624. }
  2625. unlock_user(target_vec, target_addr, 0);
  2626. return vec;
  2627. fail:
  2628. while (--i >= 0) {
  2629. if (tswapal(target_vec[i].iov_len) > 0) {
  2630. unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0);
  2631. }
  2632. }
  2633. unlock_user(target_vec, target_addr, 0);
  2634. fail2:
  2635. g_free(vec);
  2636. errno = err;
  2637. return NULL;
  2638. }
  2639. static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
  2640. abi_ulong count, int copy)
  2641. {
  2642. struct target_iovec *target_vec;
  2643. int i;
  2644. target_vec = lock_user(VERIFY_READ, target_addr,
  2645. count * sizeof(struct target_iovec), 1);
  2646. if (target_vec) {
  2647. for (i = 0; i < count; i++) {
  2648. abi_ulong base = tswapal(target_vec[i].iov_base);
  2649. abi_long len = tswapal(target_vec[i].iov_len);
  2650. if (len < 0) {
  2651. break;
  2652. }
  2653. unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
  2654. }
  2655. unlock_user(target_vec, target_addr, 0);
  2656. }
  2657. g_free(vec);
  2658. }
  2659. static inline int target_to_host_sock_type(int *type)
  2660. {
  2661. int host_type = 0;
  2662. int target_type = *type;
  2663. switch (target_type & TARGET_SOCK_TYPE_MASK) {
  2664. case TARGET_SOCK_DGRAM:
  2665. host_type = SOCK_DGRAM;
  2666. break;
  2667. case TARGET_SOCK_STREAM:
  2668. host_type = SOCK_STREAM;
  2669. break;
  2670. default:
  2671. host_type = target_type & TARGET_SOCK_TYPE_MASK;
  2672. break;
  2673. }
  2674. if (target_type & TARGET_SOCK_CLOEXEC) {
  2675. #if defined(SOCK_CLOEXEC)
  2676. host_type |= SOCK_CLOEXEC;
  2677. #else
  2678. return -TARGET_EINVAL;
  2679. #endif
  2680. }
  2681. if (target_type & TARGET_SOCK_NONBLOCK) {
  2682. #if defined(SOCK_NONBLOCK)
  2683. host_type |= SOCK_NONBLOCK;
  2684. #elif !defined(O_NONBLOCK)
  2685. return -TARGET_EINVAL;
  2686. #endif
  2687. }
  2688. *type = host_type;
  2689. return 0;
  2690. }
  2691. /* Try to emulate socket type flags after socket creation. */
  2692. static int sock_flags_fixup(int fd, int target_type)
  2693. {
  2694. #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
  2695. if (target_type & TARGET_SOCK_NONBLOCK) {
  2696. int flags = fcntl(fd, F_GETFL);
  2697. if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) {
  2698. close(fd);
  2699. return -TARGET_EINVAL;
  2700. }
  2701. }
  2702. #endif
  2703. return fd;
  2704. }
  2705. /* do_socket() Must return target values and target errnos. */
  2706. static abi_long do_socket(int domain, int type, int protocol)
  2707. {
  2708. int target_type = type;
  2709. int ret;
  2710. ret = target_to_host_sock_type(&type);
  2711. if (ret) {
  2712. return ret;
  2713. }
  2714. if (domain == PF_NETLINK && !(
  2715. #ifdef CONFIG_RTNETLINK
  2716. protocol == NETLINK_ROUTE ||
  2717. #endif
  2718. protocol == NETLINK_KOBJECT_UEVENT ||
  2719. protocol == NETLINK_AUDIT)) {
  2720. return -TARGET_EPROTONOSUPPORT;
  2721. }
  2722. if (domain == AF_PACKET ||
  2723. (domain == AF_INET && type == SOCK_PACKET)) {
  2724. protocol = tswap16(protocol);
  2725. }
  2726. ret = get_errno(socket(domain, type, protocol));
  2727. if (ret >= 0) {
  2728. ret = sock_flags_fixup(ret, target_type);
  2729. if (type == SOCK_PACKET) {
  2730. /* Manage an obsolete case :
  2731. * if socket type is SOCK_PACKET, bind by name
  2732. */
  2733. fd_trans_register(ret, &target_packet_trans);
  2734. } else if (domain == PF_NETLINK) {
  2735. switch (protocol) {
  2736. #ifdef CONFIG_RTNETLINK
  2737. case NETLINK_ROUTE:
  2738. fd_trans_register(ret, &target_netlink_route_trans);
  2739. break;
  2740. #endif
  2741. case NETLINK_KOBJECT_UEVENT:
  2742. /* nothing to do: messages are strings */
  2743. break;
  2744. case NETLINK_AUDIT:
  2745. fd_trans_register(ret, &target_netlink_audit_trans);
  2746. break;
  2747. default:
  2748. g_assert_not_reached();
  2749. }
  2750. }
  2751. }
  2752. return ret;
  2753. }
  2754. /* do_bind() Must return target values and target errnos. */
  2755. static abi_long do_bind(int sockfd, abi_ulong target_addr,
  2756. socklen_t addrlen)
  2757. {
  2758. void *addr;
  2759. abi_long ret;
  2760. if ((int)addrlen < 0) {
  2761. return -TARGET_EINVAL;
  2762. }
  2763. addr = alloca(addrlen+1);
  2764. ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
  2765. if (ret)
  2766. return ret;
  2767. return get_errno(bind(sockfd, addr, addrlen));
  2768. }
  2769. /* do_connect() Must return target values and target errnos. */
  2770. static abi_long do_connect(int sockfd, abi_ulong target_addr,
  2771. socklen_t addrlen)
  2772. {
  2773. void *addr;
  2774. abi_long ret;
  2775. if ((int)addrlen < 0) {
  2776. return -TARGET_EINVAL;
  2777. }
  2778. addr = alloca(addrlen+1);
  2779. ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
  2780. if (ret)
  2781. return ret;
  2782. return get_errno(safe_connect(sockfd, addr, addrlen));
  2783. }
  2784. /* do_sendrecvmsg_locked() Must return target values and target errnos. */
  2785. static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
  2786. int flags, int send)
  2787. {
  2788. abi_long ret, len;
  2789. struct msghdr msg;
  2790. abi_ulong count;
  2791. struct iovec *vec;
  2792. abi_ulong target_vec;
  2793. if (msgp->msg_name) {
  2794. msg.msg_namelen = tswap32(msgp->msg_namelen);
  2795. msg.msg_name = alloca(msg.msg_namelen+1);
  2796. ret = target_to_host_sockaddr(fd, msg.msg_name,
  2797. tswapal(msgp->msg_name),
  2798. msg.msg_namelen);
  2799. if (ret == -TARGET_EFAULT) {
  2800. /* For connected sockets msg_name and msg_namelen must
  2801. * be ignored, so returning EFAULT immediately is wrong.
  2802. * Instead, pass a bad msg_name to the host kernel, and
  2803. * let it decide whether to return EFAULT or not.
  2804. */
  2805. msg.msg_name = (void *)-1;
  2806. } else if (ret) {
  2807. goto out2;
  2808. }
  2809. } else {
  2810. msg.msg_name = NULL;
  2811. msg.msg_namelen = 0;
  2812. }
  2813. msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
  2814. msg.msg_control = alloca(msg.msg_controllen);
  2815. memset(msg.msg_control, 0, msg.msg_controllen);
  2816. msg.msg_flags = tswap32(msgp->msg_flags);
  2817. count = tswapal(msgp->msg_iovlen);
  2818. target_vec = tswapal(msgp->msg_iov);
  2819. if (count > IOV_MAX) {
  2820. /* sendrcvmsg returns a different errno for this condition than
  2821. * readv/writev, so we must catch it here before lock_iovec() does.
  2822. */
  2823. ret = -TARGET_EMSGSIZE;
  2824. goto out2;
  2825. }
  2826. vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
  2827. target_vec, count, send);
  2828. if (vec == NULL) {
  2829. ret = -host_to_target_errno(errno);
  2830. goto out2;
  2831. }
  2832. msg.msg_iovlen = count;
  2833. msg.msg_iov = vec;
  2834. if (send) {
  2835. if (fd_trans_target_to_host_data(fd)) {
  2836. void *host_msg;
  2837. host_msg = g_malloc(msg.msg_iov->iov_len);
  2838. memcpy(host_msg, msg.msg_iov->iov_base, msg.msg_iov->iov_len);
  2839. ret = fd_trans_target_to_host_data(fd)(host_msg,
  2840. msg.msg_iov->iov_len);
  2841. if (ret >= 0) {
  2842. msg.msg_iov->iov_base = host_msg;
  2843. ret = get_errno(safe_sendmsg(fd, &msg, flags));
  2844. }
  2845. g_free(host_msg);
  2846. } else {
  2847. ret = target_to_host_cmsg(&msg, msgp);
  2848. if (ret == 0) {
  2849. ret = get_errno(safe_sendmsg(fd, &msg, flags));
  2850. }
  2851. }
  2852. } else {
  2853. ret = get_errno(safe_recvmsg(fd, &msg, flags));
  2854. if (!is_error(ret)) {
  2855. len = ret;
  2856. if (fd_trans_host_to_target_data(fd)) {
  2857. ret = fd_trans_host_to_target_data(fd)(msg.msg_iov->iov_base,
  2858. MIN(msg.msg_iov->iov_len, len));
  2859. } else {
  2860. ret = host_to_target_cmsg(msgp, &msg);
  2861. }
  2862. if (!is_error(ret)) {
  2863. msgp->msg_namelen = tswap32(msg.msg_namelen);
  2864. msgp->msg_flags = tswap32(msg.msg_flags);
  2865. if (msg.msg_name != NULL && msg.msg_name != (void *)-1) {
  2866. ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
  2867. msg.msg_name, msg.msg_namelen);
  2868. if (ret) {
  2869. goto out;
  2870. }
  2871. }
  2872. ret = len;
  2873. }
  2874. }
  2875. }
  2876. out:
  2877. unlock_iovec(vec, target_vec, count, !send);
  2878. out2:
  2879. return ret;
  2880. }
  2881. static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
  2882. int flags, int send)
  2883. {
  2884. abi_long ret;
  2885. struct target_msghdr *msgp;
  2886. if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
  2887. msgp,
  2888. target_msg,
  2889. send ? 1 : 0)) {
  2890. return -TARGET_EFAULT;
  2891. }
  2892. ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
  2893. unlock_user_struct(msgp, target_msg, send ? 0 : 1);
  2894. return ret;
  2895. }
  2896. /* We don't rely on the C library to have sendmmsg/recvmmsg support,
  2897. * so it might not have this *mmsg-specific flag either.
  2898. */
  2899. #ifndef MSG_WAITFORONE
  2900. #define MSG_WAITFORONE 0x10000
  2901. #endif
  2902. static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec,
  2903. unsigned int vlen, unsigned int flags,
  2904. int send)
  2905. {
  2906. struct target_mmsghdr *mmsgp;
  2907. abi_long ret = 0;
  2908. int i;
  2909. if (vlen > UIO_MAXIOV) {
  2910. vlen = UIO_MAXIOV;
  2911. }
  2912. mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1);
  2913. if (!mmsgp) {
  2914. return -TARGET_EFAULT;
  2915. }
  2916. for (i = 0; i < vlen; i++) {
  2917. ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send);
  2918. if (is_error(ret)) {
  2919. break;
  2920. }
  2921. mmsgp[i].msg_len = tswap32(ret);
  2922. /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
  2923. if (flags & MSG_WAITFORONE) {
  2924. flags |= MSG_DONTWAIT;
  2925. }
  2926. }
  2927. unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i);
  2928. /* Return number of datagrams sent if we sent any at all;
  2929. * otherwise return the error.
  2930. */
  2931. if (i) {
  2932. return i;
  2933. }
  2934. return ret;
  2935. }
  2936. /* do_accept4() Must return target values and target errnos. */
  2937. static abi_long do_accept4(int fd, abi_ulong target_addr,
  2938. abi_ulong target_addrlen_addr, int flags)
  2939. {
  2940. socklen_t addrlen, ret_addrlen;
  2941. void *addr;
  2942. abi_long ret;
  2943. int host_flags;
  2944. host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
  2945. if (target_addr == 0) {
  2946. return get_errno(safe_accept4(fd, NULL, NULL, host_flags));
  2947. }
  2948. /* linux returns EINVAL if addrlen pointer is invalid */
  2949. if (get_user_u32(addrlen, target_addrlen_addr))
  2950. return -TARGET_EINVAL;
  2951. if ((int)addrlen < 0) {
  2952. return -TARGET_EINVAL;
  2953. }
  2954. if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
  2955. return -TARGET_EINVAL;
  2956. addr = alloca(addrlen);
  2957. ret_addrlen = addrlen;
  2958. ret = get_errno(safe_accept4(fd, addr, &ret_addrlen, host_flags));
  2959. if (!is_error(ret)) {
  2960. host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
  2961. if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
  2962. ret = -TARGET_EFAULT;
  2963. }
  2964. }
  2965. return ret;
  2966. }
  2967. /* do_getpeername() Must return target values and target errnos. */
  2968. static abi_long do_getpeername(int fd, abi_ulong target_addr,
  2969. abi_ulong target_addrlen_addr)
  2970. {
  2971. socklen_t addrlen, ret_addrlen;
  2972. void *addr;
  2973. abi_long ret;
  2974. if (get_user_u32(addrlen, target_addrlen_addr))
  2975. return -TARGET_EFAULT;
  2976. if ((int)addrlen < 0) {
  2977. return -TARGET_EINVAL;
  2978. }
  2979. if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
  2980. return -TARGET_EFAULT;
  2981. addr = alloca(addrlen);
  2982. ret_addrlen = addrlen;
  2983. ret = get_errno(getpeername(fd, addr, &ret_addrlen));
  2984. if (!is_error(ret)) {
  2985. host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
  2986. if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
  2987. ret = -TARGET_EFAULT;
  2988. }
  2989. }
  2990. return ret;
  2991. }
  2992. /* do_getsockname() Must return target values and target errnos. */
  2993. static abi_long do_getsockname(int fd, abi_ulong target_addr,
  2994. abi_ulong target_addrlen_addr)
  2995. {
  2996. socklen_t addrlen, ret_addrlen;
  2997. void *addr;
  2998. abi_long ret;
  2999. if (get_user_u32(addrlen, target_addrlen_addr))
  3000. return -TARGET_EFAULT;
  3001. if ((int)addrlen < 0) {
  3002. return -TARGET_EINVAL;
  3003. }
  3004. if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
  3005. return -TARGET_EFAULT;
  3006. addr = alloca(addrlen);
  3007. ret_addrlen = addrlen;
  3008. ret = get_errno(getsockname(fd, addr, &ret_addrlen));
  3009. if (!is_error(ret)) {
  3010. host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
  3011. if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
  3012. ret = -TARGET_EFAULT;
  3013. }
  3014. }
  3015. return ret;
  3016. }
  3017. /* do_socketpair() Must return target values and target errnos. */
  3018. static abi_long do_socketpair(int domain, int type, int protocol,
  3019. abi_ulong target_tab_addr)
  3020. {
  3021. int tab[2];
  3022. abi_long ret;
  3023. target_to_host_sock_type(&type);
  3024. ret = get_errno(socketpair(domain, type, protocol, tab));
  3025. if (!is_error(ret)) {
  3026. if (put_user_s32(tab[0], target_tab_addr)
  3027. || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
  3028. ret = -TARGET_EFAULT;
  3029. }
  3030. return ret;
  3031. }
  3032. /* do_sendto() Must return target values and target errnos. */
  3033. static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
  3034. abi_ulong target_addr, socklen_t addrlen)
  3035. {
  3036. void *addr;
  3037. void *host_msg;
  3038. void *copy_msg = NULL;
  3039. abi_long ret;
  3040. if ((int)addrlen < 0) {
  3041. return -TARGET_EINVAL;
  3042. }
  3043. host_msg = lock_user(VERIFY_READ, msg, len, 1);
  3044. if (!host_msg)
  3045. return -TARGET_EFAULT;
  3046. if (fd_trans_target_to_host_data(fd)) {
  3047. copy_msg = host_msg;
  3048. host_msg = g_malloc(len);
  3049. memcpy(host_msg, copy_msg, len);
  3050. ret = fd_trans_target_to_host_data(fd)(host_msg, len);
  3051. if (ret < 0) {
  3052. goto fail;
  3053. }
  3054. }
  3055. if (target_addr) {
  3056. addr = alloca(addrlen+1);
  3057. ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen);
  3058. if (ret) {
  3059. goto fail;
  3060. }
  3061. ret = get_errno(safe_sendto(fd, host_msg, len, flags, addr, addrlen));
  3062. } else {
  3063. ret = get_errno(safe_sendto(fd, host_msg, len, flags, NULL, 0));
  3064. }
  3065. fail:
  3066. if (copy_msg) {
  3067. g_free(host_msg);
  3068. host_msg = copy_msg;
  3069. }
  3070. unlock_user(host_msg, msg, 0);
  3071. return ret;
  3072. }
  3073. /* do_recvfrom() Must return target values and target errnos. */
  3074. static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
  3075. abi_ulong target_addr,
  3076. abi_ulong target_addrlen)
  3077. {
  3078. socklen_t addrlen, ret_addrlen;
  3079. void *addr;
  3080. void *host_msg;
  3081. abi_long ret;
  3082. host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
  3083. if (!host_msg)
  3084. return -TARGET_EFAULT;
  3085. if (target_addr) {
  3086. if (get_user_u32(addrlen, target_addrlen)) {
  3087. ret = -TARGET_EFAULT;
  3088. goto fail;
  3089. }
  3090. if ((int)addrlen < 0) {
  3091. ret = -TARGET_EINVAL;
  3092. goto fail;
  3093. }
  3094. addr = alloca(addrlen);
  3095. ret_addrlen = addrlen;
  3096. ret = get_errno(safe_recvfrom(fd, host_msg, len, flags,
  3097. addr, &ret_addrlen));
  3098. } else {
  3099. addr = NULL; /* To keep compiler quiet. */
  3100. addrlen = 0; /* To keep compiler quiet. */
  3101. ret = get_errno(safe_recvfrom(fd, host_msg, len, flags, NULL, 0));
  3102. }
  3103. if (!is_error(ret)) {
  3104. if (fd_trans_host_to_target_data(fd)) {
  3105. abi_long trans;
  3106. trans = fd_trans_host_to_target_data(fd)(host_msg, MIN(ret, len));
  3107. if (is_error(trans)) {
  3108. ret = trans;
  3109. goto fail;
  3110. }
  3111. }
  3112. if (target_addr) {
  3113. host_to_target_sockaddr(target_addr, addr,
  3114. MIN(addrlen, ret_addrlen));
  3115. if (put_user_u32(ret_addrlen, target_addrlen)) {
  3116. ret = -TARGET_EFAULT;
  3117. goto fail;
  3118. }
  3119. }
  3120. unlock_user(host_msg, msg, len);
  3121. } else {
  3122. fail:
  3123. unlock_user(host_msg, msg, 0);
  3124. }
  3125. return ret;
  3126. }
  3127. #ifdef TARGET_NR_socketcall
  3128. /* do_socketcall() must return target values and target errnos. */
  3129. static abi_long do_socketcall(int num, abi_ulong vptr)
  3130. {
  3131. static const unsigned nargs[] = { /* number of arguments per operation */
  3132. [TARGET_SYS_SOCKET] = 3, /* domain, type, protocol */
  3133. [TARGET_SYS_BIND] = 3, /* fd, addr, addrlen */
  3134. [TARGET_SYS_CONNECT] = 3, /* fd, addr, addrlen */
  3135. [TARGET_SYS_LISTEN] = 2, /* fd, backlog */
  3136. [TARGET_SYS_ACCEPT] = 3, /* fd, addr, addrlen */
  3137. [TARGET_SYS_GETSOCKNAME] = 3, /* fd, addr, addrlen */
  3138. [TARGET_SYS_GETPEERNAME] = 3, /* fd, addr, addrlen */
  3139. [TARGET_SYS_SOCKETPAIR] = 4, /* domain, type, protocol, tab */
  3140. [TARGET_SYS_SEND] = 4, /* fd, msg, len, flags */
  3141. [TARGET_SYS_RECV] = 4, /* fd, msg, len, flags */
  3142. [TARGET_SYS_SENDTO] = 6, /* fd, msg, len, flags, addr, addrlen */
  3143. [TARGET_SYS_RECVFROM] = 6, /* fd, msg, len, flags, addr, addrlen */
  3144. [TARGET_SYS_SHUTDOWN] = 2, /* fd, how */
  3145. [TARGET_SYS_SETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
  3146. [TARGET_SYS_GETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
  3147. [TARGET_SYS_SENDMSG] = 3, /* fd, msg, flags */
  3148. [TARGET_SYS_RECVMSG] = 3, /* fd, msg, flags */
  3149. [TARGET_SYS_ACCEPT4] = 4, /* fd, addr, addrlen, flags */
  3150. [TARGET_SYS_RECVMMSG] = 4, /* fd, msgvec, vlen, flags */
  3151. [TARGET_SYS_SENDMMSG] = 4, /* fd, msgvec, vlen, flags */
  3152. };
  3153. abi_long a[6]; /* max 6 args */
  3154. unsigned i;
  3155. /* check the range of the first argument num */
  3156. /* (TARGET_SYS_SENDMMSG is the highest among TARGET_SYS_xxx) */
  3157. if (num < 1 || num > TARGET_SYS_SENDMMSG) {
  3158. return -TARGET_EINVAL;
  3159. }
  3160. /* ensure we have space for args */
  3161. if (nargs[num] > ARRAY_SIZE(a)) {
  3162. return -TARGET_EINVAL;
  3163. }
  3164. /* collect the arguments in a[] according to nargs[] */
  3165. for (i = 0; i < nargs[num]; ++i) {
  3166. if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) {
  3167. return -TARGET_EFAULT;
  3168. }
  3169. }
  3170. /* now when we have the args, invoke the appropriate underlying function */
  3171. switch (num) {
  3172. case TARGET_SYS_SOCKET: /* domain, type, protocol */
  3173. return do_socket(a[0], a[1], a[2]);
  3174. case TARGET_SYS_BIND: /* sockfd, addr, addrlen */
  3175. return do_bind(a[0], a[1], a[2]);
  3176. case TARGET_SYS_CONNECT: /* sockfd, addr, addrlen */
  3177. return do_connect(a[0], a[1], a[2]);
  3178. case TARGET_SYS_LISTEN: /* sockfd, backlog */
  3179. return get_errno(listen(a[0], a[1]));
  3180. case TARGET_SYS_ACCEPT: /* sockfd, addr, addrlen */
  3181. return do_accept4(a[0], a[1], a[2], 0);
  3182. case TARGET_SYS_GETSOCKNAME: /* sockfd, addr, addrlen */
  3183. return do_getsockname(a[0], a[1], a[2]);
  3184. case TARGET_SYS_GETPEERNAME: /* sockfd, addr, addrlen */
  3185. return do_getpeername(a[0], a[1], a[2]);
  3186. case TARGET_SYS_SOCKETPAIR: /* domain, type, protocol, tab */
  3187. return do_socketpair(a[0], a[1], a[2], a[3]);
  3188. case TARGET_SYS_SEND: /* sockfd, msg, len, flags */
  3189. return do_sendto(a[0], a[1], a[2], a[3], 0, 0);
  3190. case TARGET_SYS_RECV: /* sockfd, msg, len, flags */
  3191. return do_recvfrom(a[0], a[1], a[2], a[3], 0, 0);
  3192. case TARGET_SYS_SENDTO: /* sockfd, msg, len, flags, addr, addrlen */
  3193. return do_sendto(a[0], a[1], a[2], a[3], a[4], a[5]);
  3194. case TARGET_SYS_RECVFROM: /* sockfd, msg, len, flags, addr, addrlen */
  3195. return do_recvfrom(a[0], a[1], a[2], a[3], a[4], a[5]);
  3196. case TARGET_SYS_SHUTDOWN: /* sockfd, how */
  3197. return get_errno(shutdown(a[0], a[1]));
  3198. case TARGET_SYS_SETSOCKOPT: /* sockfd, level, optname, optval, optlen */
  3199. return do_setsockopt(a[0], a[1], a[2], a[3], a[4]);
  3200. case TARGET_SYS_GETSOCKOPT: /* sockfd, level, optname, optval, optlen */
  3201. return do_getsockopt(a[0], a[1], a[2], a[3], a[4]);
  3202. case TARGET_SYS_SENDMSG: /* sockfd, msg, flags */
  3203. return do_sendrecvmsg(a[0], a[1], a[2], 1);
  3204. case TARGET_SYS_RECVMSG: /* sockfd, msg, flags */
  3205. return do_sendrecvmsg(a[0], a[1], a[2], 0);
  3206. case TARGET_SYS_ACCEPT4: /* sockfd, addr, addrlen, flags */
  3207. return do_accept4(a[0], a[1], a[2], a[3]);
  3208. case TARGET_SYS_RECVMMSG: /* sockfd, msgvec, vlen, flags */
  3209. return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 0);
  3210. case TARGET_SYS_SENDMMSG: /* sockfd, msgvec, vlen, flags */
  3211. return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 1);
  3212. default:
  3213. qemu_log_mask(LOG_UNIMP, "Unsupported socketcall: %d\n", num);
  3214. return -TARGET_EINVAL;
  3215. }
  3216. }
  3217. #endif
  3218. #define N_SHM_REGIONS 32
  3219. static struct shm_region {
  3220. abi_ulong start;
  3221. abi_ulong size;
  3222. bool in_use;
  3223. } shm_regions[N_SHM_REGIONS];
  3224. #ifndef TARGET_SEMID64_DS
  3225. /* asm-generic version of this struct */
  3226. struct target_semid64_ds
  3227. {
  3228. struct target_ipc_perm sem_perm;
  3229. abi_ulong sem_otime;
  3230. #if TARGET_ABI_BITS == 32
  3231. abi_ulong __unused1;
  3232. #endif
  3233. abi_ulong sem_ctime;
  3234. #if TARGET_ABI_BITS == 32
  3235. abi_ulong __unused2;
  3236. #endif
  3237. abi_ulong sem_nsems;
  3238. abi_ulong __unused3;
  3239. abi_ulong __unused4;
  3240. };
  3241. #endif
  3242. static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
  3243. abi_ulong target_addr)
  3244. {
  3245. struct target_ipc_perm *target_ip;
  3246. struct target_semid64_ds *target_sd;
  3247. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  3248. return -TARGET_EFAULT;
  3249. target_ip = &(target_sd->sem_perm);
  3250. host_ip->__key = tswap32(target_ip->__key);
  3251. host_ip->uid = tswap32(target_ip->uid);
  3252. host_ip->gid = tswap32(target_ip->gid);
  3253. host_ip->cuid = tswap32(target_ip->cuid);
  3254. host_ip->cgid = tswap32(target_ip->cgid);
  3255. #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
  3256. host_ip->mode = tswap32(target_ip->mode);
  3257. #else
  3258. host_ip->mode = tswap16(target_ip->mode);
  3259. #endif
  3260. #if defined(TARGET_PPC)
  3261. host_ip->__seq = tswap32(target_ip->__seq);
  3262. #else
  3263. host_ip->__seq = tswap16(target_ip->__seq);
  3264. #endif
  3265. unlock_user_struct(target_sd, target_addr, 0);
  3266. return 0;
  3267. }
  3268. static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
  3269. struct ipc_perm *host_ip)
  3270. {
  3271. struct target_ipc_perm *target_ip;
  3272. struct target_semid64_ds *target_sd;
  3273. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  3274. return -TARGET_EFAULT;
  3275. target_ip = &(target_sd->sem_perm);
  3276. target_ip->__key = tswap32(host_ip->__key);
  3277. target_ip->uid = tswap32(host_ip->uid);
  3278. target_ip->gid = tswap32(host_ip->gid);
  3279. target_ip->cuid = tswap32(host_ip->cuid);
  3280. target_ip->cgid = tswap32(host_ip->cgid);
  3281. #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
  3282. target_ip->mode = tswap32(host_ip->mode);
  3283. #else
  3284. target_ip->mode = tswap16(host_ip->mode);
  3285. #endif
  3286. #if defined(TARGET_PPC)
  3287. target_ip->__seq = tswap32(host_ip->__seq);
  3288. #else
  3289. target_ip->__seq = tswap16(host_ip->__seq);
  3290. #endif
  3291. unlock_user_struct(target_sd, target_addr, 1);
  3292. return 0;
  3293. }
  3294. static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
  3295. abi_ulong target_addr)
  3296. {
  3297. struct target_semid64_ds *target_sd;
  3298. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  3299. return -TARGET_EFAULT;
  3300. if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
  3301. return -TARGET_EFAULT;
  3302. host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
  3303. host_sd->sem_otime = tswapal(target_sd->sem_otime);
  3304. host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
  3305. unlock_user_struct(target_sd, target_addr, 0);
  3306. return 0;
  3307. }
  3308. static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
  3309. struct semid_ds *host_sd)
  3310. {
  3311. struct target_semid64_ds *target_sd;
  3312. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  3313. return -TARGET_EFAULT;
  3314. if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
  3315. return -TARGET_EFAULT;
  3316. target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
  3317. target_sd->sem_otime = tswapal(host_sd->sem_otime);
  3318. target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
  3319. unlock_user_struct(target_sd, target_addr, 1);
  3320. return 0;
  3321. }
  3322. struct target_seminfo {
  3323. int semmap;
  3324. int semmni;
  3325. int semmns;
  3326. int semmnu;
  3327. int semmsl;
  3328. int semopm;
  3329. int semume;
  3330. int semusz;
  3331. int semvmx;
  3332. int semaem;
  3333. };
  3334. static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
  3335. struct seminfo *host_seminfo)
  3336. {
  3337. struct target_seminfo *target_seminfo;
  3338. if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
  3339. return -TARGET_EFAULT;
  3340. __put_user(host_seminfo->semmap, &target_seminfo->semmap);
  3341. __put_user(host_seminfo->semmni, &target_seminfo->semmni);
  3342. __put_user(host_seminfo->semmns, &target_seminfo->semmns);
  3343. __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
  3344. __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
  3345. __put_user(host_seminfo->semopm, &target_seminfo->semopm);
  3346. __put_user(host_seminfo->semume, &target_seminfo->semume);
  3347. __put_user(host_seminfo->semusz, &target_seminfo->semusz);
  3348. __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
  3349. __put_user(host_seminfo->semaem, &target_seminfo->semaem);
  3350. unlock_user_struct(target_seminfo, target_addr, 1);
  3351. return 0;
  3352. }
  3353. union semun {
  3354. int val;
  3355. struct semid_ds *buf;
  3356. unsigned short *array;
  3357. struct seminfo *__buf;
  3358. };
  3359. union target_semun {
  3360. int val;
  3361. abi_ulong buf;
  3362. abi_ulong array;
  3363. abi_ulong __buf;
  3364. };
  3365. static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
  3366. abi_ulong target_addr)
  3367. {
  3368. int nsems;
  3369. unsigned short *array;
  3370. union semun semun;
  3371. struct semid_ds semid_ds;
  3372. int i, ret;
  3373. semun.buf = &semid_ds;
  3374. ret = semctl(semid, 0, IPC_STAT, semun);
  3375. if (ret == -1)
  3376. return get_errno(ret);
  3377. nsems = semid_ds.sem_nsems;
  3378. *host_array = g_try_new(unsigned short, nsems);
  3379. if (!*host_array) {
  3380. return -TARGET_ENOMEM;
  3381. }
  3382. array = lock_user(VERIFY_READ, target_addr,
  3383. nsems*sizeof(unsigned short), 1);
  3384. if (!array) {
  3385. g_free(*host_array);
  3386. return -TARGET_EFAULT;
  3387. }
  3388. for(i=0; i<nsems; i++) {
  3389. __get_user((*host_array)[i], &array[i]);
  3390. }
  3391. unlock_user(array, target_addr, 0);
  3392. return 0;
  3393. }
  3394. static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
  3395. unsigned short **host_array)
  3396. {
  3397. int nsems;
  3398. unsigned short *array;
  3399. union semun semun;
  3400. struct semid_ds semid_ds;
  3401. int i, ret;
  3402. semun.buf = &semid_ds;
  3403. ret = semctl(semid, 0, IPC_STAT, semun);
  3404. if (ret == -1)
  3405. return get_errno(ret);
  3406. nsems = semid_ds.sem_nsems;
  3407. array = lock_user(VERIFY_WRITE, target_addr,
  3408. nsems*sizeof(unsigned short), 0);
  3409. if (!array)
  3410. return -TARGET_EFAULT;
  3411. for(i=0; i<nsems; i++) {
  3412. __put_user((*host_array)[i], &array[i]);
  3413. }
  3414. g_free(*host_array);
  3415. unlock_user(array, target_addr, 1);
  3416. return 0;
  3417. }
  3418. static inline abi_long do_semctl(int semid, int semnum, int cmd,
  3419. abi_ulong target_arg)
  3420. {
  3421. union target_semun target_su = { .buf = target_arg };
  3422. union semun arg;
  3423. struct semid_ds dsarg;
  3424. unsigned short *array = NULL;
  3425. struct seminfo seminfo;
  3426. abi_long ret = -TARGET_EINVAL;
  3427. abi_long err;
  3428. cmd &= 0xff;
  3429. switch( cmd ) {
  3430. case GETVAL:
  3431. case SETVAL:
  3432. /* In 64 bit cross-endian situations, we will erroneously pick up
  3433. * the wrong half of the union for the "val" element. To rectify
  3434. * this, the entire 8-byte structure is byteswapped, followed by
  3435. * a swap of the 4 byte val field. In other cases, the data is
  3436. * already in proper host byte order. */
  3437. if (sizeof(target_su.val) != (sizeof(target_su.buf))) {
  3438. target_su.buf = tswapal(target_su.buf);
  3439. arg.val = tswap32(target_su.val);
  3440. } else {
  3441. arg.val = target_su.val;
  3442. }
  3443. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3444. break;
  3445. case GETALL:
  3446. case SETALL:
  3447. err = target_to_host_semarray(semid, &array, target_su.array);
  3448. if (err)
  3449. return err;
  3450. arg.array = array;
  3451. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3452. err = host_to_target_semarray(semid, target_su.array, &array);
  3453. if (err)
  3454. return err;
  3455. break;
  3456. case IPC_STAT:
  3457. case IPC_SET:
  3458. case SEM_STAT:
  3459. err = target_to_host_semid_ds(&dsarg, target_su.buf);
  3460. if (err)
  3461. return err;
  3462. arg.buf = &dsarg;
  3463. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3464. err = host_to_target_semid_ds(target_su.buf, &dsarg);
  3465. if (err)
  3466. return err;
  3467. break;
  3468. case IPC_INFO:
  3469. case SEM_INFO:
  3470. arg.__buf = &seminfo;
  3471. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3472. err = host_to_target_seminfo(target_su.__buf, &seminfo);
  3473. if (err)
  3474. return err;
  3475. break;
  3476. case IPC_RMID:
  3477. case GETPID:
  3478. case GETNCNT:
  3479. case GETZCNT:
  3480. ret = get_errno(semctl(semid, semnum, cmd, NULL));
  3481. break;
  3482. }
  3483. return ret;
  3484. }
  3485. struct target_sembuf {
  3486. unsigned short sem_num;
  3487. short sem_op;
  3488. short sem_flg;
  3489. };
  3490. static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
  3491. abi_ulong target_addr,
  3492. unsigned nsops)
  3493. {
  3494. struct target_sembuf *target_sembuf;
  3495. int i;
  3496. target_sembuf = lock_user(VERIFY_READ, target_addr,
  3497. nsops*sizeof(struct target_sembuf), 1);
  3498. if (!target_sembuf)
  3499. return -TARGET_EFAULT;
  3500. for(i=0; i<nsops; i++) {
  3501. __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
  3502. __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
  3503. __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
  3504. }
  3505. unlock_user(target_sembuf, target_addr, 0);
  3506. return 0;
  3507. }
  3508. #if defined(TARGET_NR_ipc) || defined(TARGET_NR_semop) || \
  3509. defined(TARGET_NR_semtimedop)
  3510. /*
  3511. * This macro is required to handle the s390 variants, which passes the
  3512. * arguments in a different order than default.
  3513. */
  3514. #ifdef __s390x__
  3515. #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
  3516. (__nsops), (__timeout), (__sops)
  3517. #else
  3518. #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
  3519. (__nsops), 0, (__sops), (__timeout)
  3520. #endif
  3521. static inline abi_long do_semtimedop(int semid,
  3522. abi_long ptr,
  3523. unsigned nsops,
  3524. abi_long timeout)
  3525. {
  3526. struct sembuf sops[nsops];
  3527. struct timespec ts, *pts = NULL;
  3528. abi_long ret;
  3529. if (timeout) {
  3530. pts = &ts;
  3531. if (target_to_host_timespec(pts, timeout)) {
  3532. return -TARGET_EFAULT;
  3533. }
  3534. }
  3535. if (target_to_host_sembuf(sops, ptr, nsops))
  3536. return -TARGET_EFAULT;
  3537. ret = -TARGET_ENOSYS;
  3538. #ifdef __NR_semtimedop
  3539. ret = get_errno(safe_semtimedop(semid, sops, nsops, pts));
  3540. #endif
  3541. #ifdef __NR_ipc
  3542. if (ret == -TARGET_ENOSYS) {
  3543. ret = get_errno(safe_ipc(IPCOP_semtimedop, semid,
  3544. SEMTIMEDOP_IPC_ARGS(nsops, sops, (long)pts)));
  3545. }
  3546. #endif
  3547. return ret;
  3548. }
  3549. #endif
  3550. struct target_msqid_ds
  3551. {
  3552. struct target_ipc_perm msg_perm;
  3553. abi_ulong msg_stime;
  3554. #if TARGET_ABI_BITS == 32
  3555. abi_ulong __unused1;
  3556. #endif
  3557. abi_ulong msg_rtime;
  3558. #if TARGET_ABI_BITS == 32
  3559. abi_ulong __unused2;
  3560. #endif
  3561. abi_ulong msg_ctime;
  3562. #if TARGET_ABI_BITS == 32
  3563. abi_ulong __unused3;
  3564. #endif
  3565. abi_ulong __msg_cbytes;
  3566. abi_ulong msg_qnum;
  3567. abi_ulong msg_qbytes;
  3568. abi_ulong msg_lspid;
  3569. abi_ulong msg_lrpid;
  3570. abi_ulong __unused4;
  3571. abi_ulong __unused5;
  3572. };
  3573. static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
  3574. abi_ulong target_addr)
  3575. {
  3576. struct target_msqid_ds *target_md;
  3577. if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
  3578. return -TARGET_EFAULT;
  3579. if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
  3580. return -TARGET_EFAULT;
  3581. host_md->msg_stime = tswapal(target_md->msg_stime);
  3582. host_md->msg_rtime = tswapal(target_md->msg_rtime);
  3583. host_md->msg_ctime = tswapal(target_md->msg_ctime);
  3584. host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
  3585. host_md->msg_qnum = tswapal(target_md->msg_qnum);
  3586. host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
  3587. host_md->msg_lspid = tswapal(target_md->msg_lspid);
  3588. host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
  3589. unlock_user_struct(target_md, target_addr, 0);
  3590. return 0;
  3591. }
  3592. static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
  3593. struct msqid_ds *host_md)
  3594. {
  3595. struct target_msqid_ds *target_md;
  3596. if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
  3597. return -TARGET_EFAULT;
  3598. if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
  3599. return -TARGET_EFAULT;
  3600. target_md->msg_stime = tswapal(host_md->msg_stime);
  3601. target_md->msg_rtime = tswapal(host_md->msg_rtime);
  3602. target_md->msg_ctime = tswapal(host_md->msg_ctime);
  3603. target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
  3604. target_md->msg_qnum = tswapal(host_md->msg_qnum);
  3605. target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
  3606. target_md->msg_lspid = tswapal(host_md->msg_lspid);
  3607. target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
  3608. unlock_user_struct(target_md, target_addr, 1);
  3609. return 0;
  3610. }
  3611. struct target_msginfo {
  3612. int msgpool;
  3613. int msgmap;
  3614. int msgmax;
  3615. int msgmnb;
  3616. int msgmni;
  3617. int msgssz;
  3618. int msgtql;
  3619. unsigned short int msgseg;
  3620. };
  3621. static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
  3622. struct msginfo *host_msginfo)
  3623. {
  3624. struct target_msginfo *target_msginfo;
  3625. if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
  3626. return -TARGET_EFAULT;
  3627. __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
  3628. __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
  3629. __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
  3630. __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
  3631. __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
  3632. __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
  3633. __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
  3634. __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
  3635. unlock_user_struct(target_msginfo, target_addr, 1);
  3636. return 0;
  3637. }
  3638. static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
  3639. {
  3640. struct msqid_ds dsarg;
  3641. struct msginfo msginfo;
  3642. abi_long ret = -TARGET_EINVAL;
  3643. cmd &= 0xff;
  3644. switch (cmd) {
  3645. case IPC_STAT:
  3646. case IPC_SET:
  3647. case MSG_STAT:
  3648. if (target_to_host_msqid_ds(&dsarg,ptr))
  3649. return -TARGET_EFAULT;
  3650. ret = get_errno(msgctl(msgid, cmd, &dsarg));
  3651. if (host_to_target_msqid_ds(ptr,&dsarg))
  3652. return -TARGET_EFAULT;
  3653. break;
  3654. case IPC_RMID:
  3655. ret = get_errno(msgctl(msgid, cmd, NULL));
  3656. break;
  3657. case IPC_INFO:
  3658. case MSG_INFO:
  3659. ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
  3660. if (host_to_target_msginfo(ptr, &msginfo))
  3661. return -TARGET_EFAULT;
  3662. break;
  3663. }
  3664. return ret;
  3665. }
  3666. struct target_msgbuf {
  3667. abi_long mtype;
  3668. char mtext[1];
  3669. };
  3670. static inline abi_long do_msgsnd(int msqid, abi_long msgp,
  3671. ssize_t msgsz, int msgflg)
  3672. {
  3673. struct target_msgbuf *target_mb;
  3674. struct msgbuf *host_mb;
  3675. abi_long ret = 0;
  3676. if (msgsz < 0) {
  3677. return -TARGET_EINVAL;
  3678. }
  3679. if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
  3680. return -TARGET_EFAULT;
  3681. host_mb = g_try_malloc(msgsz + sizeof(long));
  3682. if (!host_mb) {
  3683. unlock_user_struct(target_mb, msgp, 0);
  3684. return -TARGET_ENOMEM;
  3685. }
  3686. host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
  3687. memcpy(host_mb->mtext, target_mb->mtext, msgsz);
  3688. ret = -TARGET_ENOSYS;
  3689. #ifdef __NR_msgsnd
  3690. ret = get_errno(safe_msgsnd(msqid, host_mb, msgsz, msgflg));
  3691. #endif
  3692. #ifdef __NR_ipc
  3693. if (ret == -TARGET_ENOSYS) {
  3694. #ifdef __s390x__
  3695. ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
  3696. host_mb));
  3697. #else
  3698. ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
  3699. host_mb, 0));
  3700. #endif
  3701. }
  3702. #endif
  3703. g_free(host_mb);
  3704. unlock_user_struct(target_mb, msgp, 0);
  3705. return ret;
  3706. }
  3707. #ifdef __NR_ipc
  3708. #if defined(__sparc__)
  3709. /* SPARC for msgrcv it does not use the kludge on final 2 arguments. */
  3710. #define MSGRCV_ARGS(__msgp, __msgtyp) __msgp, __msgtyp
  3711. #elif defined(__s390x__)
  3712. /* The s390 sys_ipc variant has only five parameters. */
  3713. #define MSGRCV_ARGS(__msgp, __msgtyp) \
  3714. ((long int[]){(long int)__msgp, __msgtyp})
  3715. #else
  3716. #define MSGRCV_ARGS(__msgp, __msgtyp) \
  3717. ((long int[]){(long int)__msgp, __msgtyp}), 0
  3718. #endif
  3719. #endif
  3720. static inline abi_long do_msgrcv(int msqid, abi_long msgp,
  3721. ssize_t msgsz, abi_long msgtyp,
  3722. int msgflg)
  3723. {
  3724. struct target_msgbuf *target_mb;
  3725. char *target_mtext;
  3726. struct msgbuf *host_mb;
  3727. abi_long ret = 0;
  3728. if (msgsz < 0) {
  3729. return -TARGET_EINVAL;
  3730. }
  3731. if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
  3732. return -TARGET_EFAULT;
  3733. host_mb = g_try_malloc(msgsz + sizeof(long));
  3734. if (!host_mb) {
  3735. ret = -TARGET_ENOMEM;
  3736. goto end;
  3737. }
  3738. ret = -TARGET_ENOSYS;
  3739. #ifdef __NR_msgrcv
  3740. ret = get_errno(safe_msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
  3741. #endif
  3742. #ifdef __NR_ipc
  3743. if (ret == -TARGET_ENOSYS) {
  3744. ret = get_errno(safe_ipc(IPCOP_CALL(1, IPCOP_msgrcv), msqid, msgsz,
  3745. msgflg, MSGRCV_ARGS(host_mb, msgtyp)));
  3746. }
  3747. #endif
  3748. if (ret > 0) {
  3749. abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
  3750. target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
  3751. if (!target_mtext) {
  3752. ret = -TARGET_EFAULT;
  3753. goto end;
  3754. }
  3755. memcpy(target_mb->mtext, host_mb->mtext, ret);
  3756. unlock_user(target_mtext, target_mtext_addr, ret);
  3757. }
  3758. target_mb->mtype = tswapal(host_mb->mtype);
  3759. end:
  3760. if (target_mb)
  3761. unlock_user_struct(target_mb, msgp, 1);
  3762. g_free(host_mb);
  3763. return ret;
  3764. }
  3765. static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
  3766. abi_ulong target_addr)
  3767. {
  3768. struct target_shmid_ds *target_sd;
  3769. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  3770. return -TARGET_EFAULT;
  3771. if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
  3772. return -TARGET_EFAULT;
  3773. __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
  3774. __get_user(host_sd->shm_atime, &target_sd->shm_atime);
  3775. __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
  3776. __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
  3777. __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
  3778. __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
  3779. __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
  3780. unlock_user_struct(target_sd, target_addr, 0);
  3781. return 0;
  3782. }
  3783. static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
  3784. struct shmid_ds *host_sd)
  3785. {
  3786. struct target_shmid_ds *target_sd;
  3787. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  3788. return -TARGET_EFAULT;
  3789. if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
  3790. return -TARGET_EFAULT;
  3791. __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
  3792. __put_user(host_sd->shm_atime, &target_sd->shm_atime);
  3793. __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
  3794. __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
  3795. __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
  3796. __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
  3797. __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
  3798. unlock_user_struct(target_sd, target_addr, 1);
  3799. return 0;
  3800. }
  3801. struct target_shminfo {
  3802. abi_ulong shmmax;
  3803. abi_ulong shmmin;
  3804. abi_ulong shmmni;
  3805. abi_ulong shmseg;
  3806. abi_ulong shmall;
  3807. };
  3808. static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
  3809. struct shminfo *host_shminfo)
  3810. {
  3811. struct target_shminfo *target_shminfo;
  3812. if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
  3813. return -TARGET_EFAULT;
  3814. __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
  3815. __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
  3816. __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
  3817. __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
  3818. __put_user(host_shminfo->shmall, &target_shminfo->shmall);
  3819. unlock_user_struct(target_shminfo, target_addr, 1);
  3820. return 0;
  3821. }
  3822. struct target_shm_info {
  3823. int used_ids;
  3824. abi_ulong shm_tot;
  3825. abi_ulong shm_rss;
  3826. abi_ulong shm_swp;
  3827. abi_ulong swap_attempts;
  3828. abi_ulong swap_successes;
  3829. };
  3830. static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
  3831. struct shm_info *host_shm_info)
  3832. {
  3833. struct target_shm_info *target_shm_info;
  3834. if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
  3835. return -TARGET_EFAULT;
  3836. __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
  3837. __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
  3838. __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
  3839. __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
  3840. __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
  3841. __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
  3842. unlock_user_struct(target_shm_info, target_addr, 1);
  3843. return 0;
  3844. }
  3845. static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
  3846. {
  3847. struct shmid_ds dsarg;
  3848. struct shminfo shminfo;
  3849. struct shm_info shm_info;
  3850. abi_long ret = -TARGET_EINVAL;
  3851. cmd &= 0xff;
  3852. switch(cmd) {
  3853. case IPC_STAT:
  3854. case IPC_SET:
  3855. case SHM_STAT:
  3856. if (target_to_host_shmid_ds(&dsarg, buf))
  3857. return -TARGET_EFAULT;
  3858. ret = get_errno(shmctl(shmid, cmd, &dsarg));
  3859. if (host_to_target_shmid_ds(buf, &dsarg))
  3860. return -TARGET_EFAULT;
  3861. break;
  3862. case IPC_INFO:
  3863. ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
  3864. if (host_to_target_shminfo(buf, &shminfo))
  3865. return -TARGET_EFAULT;
  3866. break;
  3867. case SHM_INFO:
  3868. ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
  3869. if (host_to_target_shm_info(buf, &shm_info))
  3870. return -TARGET_EFAULT;
  3871. break;
  3872. case IPC_RMID:
  3873. case SHM_LOCK:
  3874. case SHM_UNLOCK:
  3875. ret = get_errno(shmctl(shmid, cmd, NULL));
  3876. break;
  3877. }
  3878. return ret;
  3879. }
  3880. #ifndef TARGET_FORCE_SHMLBA
  3881. /* For most architectures, SHMLBA is the same as the page size;
  3882. * some architectures have larger values, in which case they should
  3883. * define TARGET_FORCE_SHMLBA and provide a target_shmlba() function.
  3884. * This corresponds to the kernel arch code defining __ARCH_FORCE_SHMLBA
  3885. * and defining its own value for SHMLBA.
  3886. *
  3887. * The kernel also permits SHMLBA to be set by the architecture to a
  3888. * value larger than the page size without setting __ARCH_FORCE_SHMLBA;
  3889. * this means that addresses are rounded to the large size if
  3890. * SHM_RND is set but addresses not aligned to that size are not rejected
  3891. * as long as they are at least page-aligned. Since the only architecture
  3892. * which uses this is ia64 this code doesn't provide for that oddity.
  3893. */
  3894. static inline abi_ulong target_shmlba(CPUArchState *cpu_env)
  3895. {
  3896. return TARGET_PAGE_SIZE;
  3897. }
  3898. #endif
  3899. static inline abi_ulong do_shmat(CPUArchState *cpu_env,
  3900. int shmid, abi_ulong shmaddr, int shmflg)
  3901. {
  3902. abi_long raddr;
  3903. void *host_raddr;
  3904. struct shmid_ds shm_info;
  3905. int i,ret;
  3906. abi_ulong shmlba;
  3907. /* find out the length of the shared memory segment */
  3908. ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
  3909. if (is_error(ret)) {
  3910. /* can't get length, bail out */
  3911. return ret;
  3912. }
  3913. shmlba = target_shmlba(cpu_env);
  3914. if (shmaddr & (shmlba - 1)) {
  3915. if (shmflg & SHM_RND) {
  3916. shmaddr &= ~(shmlba - 1);
  3917. } else {
  3918. return -TARGET_EINVAL;
  3919. }
  3920. }
  3921. if (!guest_range_valid(shmaddr, shm_info.shm_segsz)) {
  3922. return -TARGET_EINVAL;
  3923. }
  3924. mmap_lock();
  3925. if (shmaddr)
  3926. host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
  3927. else {
  3928. abi_ulong mmap_start;
  3929. /* In order to use the host shmat, we need to honor host SHMLBA. */
  3930. mmap_start = mmap_find_vma(0, shm_info.shm_segsz, MAX(SHMLBA, shmlba));
  3931. if (mmap_start == -1) {
  3932. errno = ENOMEM;
  3933. host_raddr = (void *)-1;
  3934. } else
  3935. host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
  3936. }
  3937. if (host_raddr == (void *)-1) {
  3938. mmap_unlock();
  3939. return get_errno((long)host_raddr);
  3940. }
  3941. raddr=h2g((unsigned long)host_raddr);
  3942. page_set_flags(raddr, raddr + shm_info.shm_segsz,
  3943. PAGE_VALID | PAGE_READ |
  3944. ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
  3945. for (i = 0; i < N_SHM_REGIONS; i++) {
  3946. if (!shm_regions[i].in_use) {
  3947. shm_regions[i].in_use = true;
  3948. shm_regions[i].start = raddr;
  3949. shm_regions[i].size = shm_info.shm_segsz;
  3950. break;
  3951. }
  3952. }
  3953. mmap_unlock();
  3954. return raddr;
  3955. }
  3956. static inline abi_long do_shmdt(abi_ulong shmaddr)
  3957. {
  3958. int i;
  3959. abi_long rv;
  3960. mmap_lock();
  3961. for (i = 0; i < N_SHM_REGIONS; ++i) {
  3962. if (shm_regions[i].in_use && shm_regions[i].start == shmaddr) {
  3963. shm_regions[i].in_use = false;
  3964. page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
  3965. break;
  3966. }
  3967. }
  3968. rv = get_errno(shmdt(g2h(shmaddr)));
  3969. mmap_unlock();
  3970. return rv;
  3971. }
  3972. #ifdef TARGET_NR_ipc
  3973. /* ??? This only works with linear mappings. */
  3974. /* do_ipc() must return target values and target errnos. */
  3975. static abi_long do_ipc(CPUArchState *cpu_env,
  3976. unsigned int call, abi_long first,
  3977. abi_long second, abi_long third,
  3978. abi_long ptr, abi_long fifth)
  3979. {
  3980. int version;
  3981. abi_long ret = 0;
  3982. version = call >> 16;
  3983. call &= 0xffff;
  3984. switch (call) {
  3985. case IPCOP_semop:
  3986. ret = do_semtimedop(first, ptr, second, 0);
  3987. break;
  3988. case IPCOP_semtimedop:
  3989. /*
  3990. * The s390 sys_ipc variant has only five parameters instead of six
  3991. * (as for default variant) and the only difference is the handling of
  3992. * SEMTIMEDOP where on s390 the third parameter is used as a pointer
  3993. * to a struct timespec where the generic variant uses fifth parameter.
  3994. */
  3995. #if defined(TARGET_S390X)
  3996. ret = do_semtimedop(first, ptr, second, third);
  3997. #else
  3998. ret = do_semtimedop(first, ptr, second, fifth);
  3999. #endif
  4000. break;
  4001. case IPCOP_semget:
  4002. ret = get_errno(semget(first, second, third));
  4003. break;
  4004. case IPCOP_semctl: {
  4005. /* The semun argument to semctl is passed by value, so dereference the
  4006. * ptr argument. */
  4007. abi_ulong atptr;
  4008. get_user_ual(atptr, ptr);
  4009. ret = do_semctl(first, second, third, atptr);
  4010. break;
  4011. }
  4012. case IPCOP_msgget:
  4013. ret = get_errno(msgget(first, second));
  4014. break;
  4015. case IPCOP_msgsnd:
  4016. ret = do_msgsnd(first, ptr, second, third);
  4017. break;
  4018. case IPCOP_msgctl:
  4019. ret = do_msgctl(first, second, ptr);
  4020. break;
  4021. case IPCOP_msgrcv:
  4022. switch (version) {
  4023. case 0:
  4024. {
  4025. struct target_ipc_kludge {
  4026. abi_long msgp;
  4027. abi_long msgtyp;
  4028. } *tmp;
  4029. if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
  4030. ret = -TARGET_EFAULT;
  4031. break;
  4032. }
  4033. ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
  4034. unlock_user_struct(tmp, ptr, 0);
  4035. break;
  4036. }
  4037. default:
  4038. ret = do_msgrcv(first, ptr, second, fifth, third);
  4039. }
  4040. break;
  4041. case IPCOP_shmat:
  4042. switch (version) {
  4043. default:
  4044. {
  4045. abi_ulong raddr;
  4046. raddr = do_shmat(cpu_env, first, ptr, second);
  4047. if (is_error(raddr))
  4048. return get_errno(raddr);
  4049. if (put_user_ual(raddr, third))
  4050. return -TARGET_EFAULT;
  4051. break;
  4052. }
  4053. case 1:
  4054. ret = -TARGET_EINVAL;
  4055. break;
  4056. }
  4057. break;
  4058. case IPCOP_shmdt:
  4059. ret = do_shmdt(ptr);
  4060. break;
  4061. case IPCOP_shmget:
  4062. /* IPC_* flag values are the same on all linux platforms */
  4063. ret = get_errno(shmget(first, second, third));
  4064. break;
  4065. /* IPC_* and SHM_* command values are the same on all linux platforms */
  4066. case IPCOP_shmctl:
  4067. ret = do_shmctl(first, second, ptr);
  4068. break;
  4069. default:
  4070. qemu_log_mask(LOG_UNIMP, "Unsupported ipc call: %d (version %d)\n",
  4071. call, version);
  4072. ret = -TARGET_ENOSYS;
  4073. break;
  4074. }
  4075. return ret;
  4076. }
  4077. #endif
  4078. /* kernel structure types definitions */
  4079. #define STRUCT(name, ...) STRUCT_ ## name,
  4080. #define STRUCT_SPECIAL(name) STRUCT_ ## name,
  4081. enum {
  4082. #include "syscall_types.h"
  4083. STRUCT_MAX
  4084. };
  4085. #undef STRUCT
  4086. #undef STRUCT_SPECIAL
  4087. #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
  4088. #define STRUCT_SPECIAL(name)
  4089. #include "syscall_types.h"
  4090. #undef STRUCT
  4091. #undef STRUCT_SPECIAL
  4092. #define MAX_STRUCT_SIZE 4096
  4093. #ifdef CONFIG_FIEMAP
  4094. /* So fiemap access checks don't overflow on 32 bit systems.
  4095. * This is very slightly smaller than the limit imposed by
  4096. * the underlying kernel.
  4097. */
  4098. #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
  4099. / sizeof(struct fiemap_extent))
  4100. static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
  4101. int fd, int cmd, abi_long arg)
  4102. {
  4103. /* The parameter for this ioctl is a struct fiemap followed
  4104. * by an array of struct fiemap_extent whose size is set
  4105. * in fiemap->fm_extent_count. The array is filled in by the
  4106. * ioctl.
  4107. */
  4108. int target_size_in, target_size_out;
  4109. struct fiemap *fm;
  4110. const argtype *arg_type = ie->arg_type;
  4111. const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
  4112. void *argptr, *p;
  4113. abi_long ret;
  4114. int i, extent_size = thunk_type_size(extent_arg_type, 0);
  4115. uint32_t outbufsz;
  4116. int free_fm = 0;
  4117. assert(arg_type[0] == TYPE_PTR);
  4118. assert(ie->access == IOC_RW);
  4119. arg_type++;
  4120. target_size_in = thunk_type_size(arg_type, 0);
  4121. argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
  4122. if (!argptr) {
  4123. return -TARGET_EFAULT;
  4124. }
  4125. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4126. unlock_user(argptr, arg, 0);
  4127. fm = (struct fiemap *)buf_temp;
  4128. if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
  4129. return -TARGET_EINVAL;
  4130. }
  4131. outbufsz = sizeof (*fm) +
  4132. (sizeof(struct fiemap_extent) * fm->fm_extent_count);
  4133. if (outbufsz > MAX_STRUCT_SIZE) {
  4134. /* We can't fit all the extents into the fixed size buffer.
  4135. * Allocate one that is large enough and use it instead.
  4136. */
  4137. fm = g_try_malloc(outbufsz);
  4138. if (!fm) {
  4139. return -TARGET_ENOMEM;
  4140. }
  4141. memcpy(fm, buf_temp, sizeof(struct fiemap));
  4142. free_fm = 1;
  4143. }
  4144. ret = get_errno(safe_ioctl(fd, ie->host_cmd, fm));
  4145. if (!is_error(ret)) {
  4146. target_size_out = target_size_in;
  4147. /* An extent_count of 0 means we were only counting the extents
  4148. * so there are no structs to copy
  4149. */
  4150. if (fm->fm_extent_count != 0) {
  4151. target_size_out += fm->fm_mapped_extents * extent_size;
  4152. }
  4153. argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
  4154. if (!argptr) {
  4155. ret = -TARGET_EFAULT;
  4156. } else {
  4157. /* Convert the struct fiemap */
  4158. thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
  4159. if (fm->fm_extent_count != 0) {
  4160. p = argptr + target_size_in;
  4161. /* ...and then all the struct fiemap_extents */
  4162. for (i = 0; i < fm->fm_mapped_extents; i++) {
  4163. thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
  4164. THUNK_TARGET);
  4165. p += extent_size;
  4166. }
  4167. }
  4168. unlock_user(argptr, arg, target_size_out);
  4169. }
  4170. }
  4171. if (free_fm) {
  4172. g_free(fm);
  4173. }
  4174. return ret;
  4175. }
  4176. #endif
  4177. static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
  4178. int fd, int cmd, abi_long arg)
  4179. {
  4180. const argtype *arg_type = ie->arg_type;
  4181. int target_size;
  4182. void *argptr;
  4183. int ret;
  4184. struct ifconf *host_ifconf;
  4185. uint32_t outbufsz;
  4186. const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
  4187. int target_ifreq_size;
  4188. int nb_ifreq;
  4189. int free_buf = 0;
  4190. int i;
  4191. int target_ifc_len;
  4192. abi_long target_ifc_buf;
  4193. int host_ifc_len;
  4194. char *host_ifc_buf;
  4195. assert(arg_type[0] == TYPE_PTR);
  4196. assert(ie->access == IOC_RW);
  4197. arg_type++;
  4198. target_size = thunk_type_size(arg_type, 0);
  4199. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4200. if (!argptr)
  4201. return -TARGET_EFAULT;
  4202. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4203. unlock_user(argptr, arg, 0);
  4204. host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
  4205. target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
  4206. target_ifreq_size = thunk_type_size(ifreq_arg_type, 0);
  4207. if (target_ifc_buf != 0) {
  4208. target_ifc_len = host_ifconf->ifc_len;
  4209. nb_ifreq = target_ifc_len / target_ifreq_size;
  4210. host_ifc_len = nb_ifreq * sizeof(struct ifreq);
  4211. outbufsz = sizeof(*host_ifconf) + host_ifc_len;
  4212. if (outbufsz > MAX_STRUCT_SIZE) {
  4213. /*
  4214. * We can't fit all the extents into the fixed size buffer.
  4215. * Allocate one that is large enough and use it instead.
  4216. */
  4217. host_ifconf = malloc(outbufsz);
  4218. if (!host_ifconf) {
  4219. return -TARGET_ENOMEM;
  4220. }
  4221. memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
  4222. free_buf = 1;
  4223. }
  4224. host_ifc_buf = (char *)host_ifconf + sizeof(*host_ifconf);
  4225. host_ifconf->ifc_len = host_ifc_len;
  4226. } else {
  4227. host_ifc_buf = NULL;
  4228. }
  4229. host_ifconf->ifc_buf = host_ifc_buf;
  4230. ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_ifconf));
  4231. if (!is_error(ret)) {
  4232. /* convert host ifc_len to target ifc_len */
  4233. nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
  4234. target_ifc_len = nb_ifreq * target_ifreq_size;
  4235. host_ifconf->ifc_len = target_ifc_len;
  4236. /* restore target ifc_buf */
  4237. host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
  4238. /* copy struct ifconf to target user */
  4239. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4240. if (!argptr)
  4241. return -TARGET_EFAULT;
  4242. thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
  4243. unlock_user(argptr, arg, target_size);
  4244. if (target_ifc_buf != 0) {
  4245. /* copy ifreq[] to target user */
  4246. argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
  4247. for (i = 0; i < nb_ifreq ; i++) {
  4248. thunk_convert(argptr + i * target_ifreq_size,
  4249. host_ifc_buf + i * sizeof(struct ifreq),
  4250. ifreq_arg_type, THUNK_TARGET);
  4251. }
  4252. unlock_user(argptr, target_ifc_buf, target_ifc_len);
  4253. }
  4254. }
  4255. if (free_buf) {
  4256. free(host_ifconf);
  4257. }
  4258. return ret;
  4259. }
  4260. #if defined(CONFIG_USBFS)
  4261. #if HOST_LONG_BITS > 64
  4262. #error USBDEVFS thunks do not support >64 bit hosts yet.
  4263. #endif
  4264. struct live_urb {
  4265. uint64_t target_urb_adr;
  4266. uint64_t target_buf_adr;
  4267. char *target_buf_ptr;
  4268. struct usbdevfs_urb host_urb;
  4269. };
  4270. static GHashTable *usbdevfs_urb_hashtable(void)
  4271. {
  4272. static GHashTable *urb_hashtable;
  4273. if (!urb_hashtable) {
  4274. urb_hashtable = g_hash_table_new(g_int64_hash, g_int64_equal);
  4275. }
  4276. return urb_hashtable;
  4277. }
  4278. static void urb_hashtable_insert(struct live_urb *urb)
  4279. {
  4280. GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
  4281. g_hash_table_insert(urb_hashtable, urb, urb);
  4282. }
  4283. static struct live_urb *urb_hashtable_lookup(uint64_t target_urb_adr)
  4284. {
  4285. GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
  4286. return g_hash_table_lookup(urb_hashtable, &target_urb_adr);
  4287. }
  4288. static void urb_hashtable_remove(struct live_urb *urb)
  4289. {
  4290. GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
  4291. g_hash_table_remove(urb_hashtable, urb);
  4292. }
  4293. static abi_long
  4294. do_ioctl_usbdevfs_reapurb(const IOCTLEntry *ie, uint8_t *buf_temp,
  4295. int fd, int cmd, abi_long arg)
  4296. {
  4297. const argtype usbfsurb_arg_type[] = { MK_STRUCT(STRUCT_usbdevfs_urb) };
  4298. const argtype ptrvoid_arg_type[] = { TYPE_PTRVOID, 0, 0 };
  4299. struct live_urb *lurb;
  4300. void *argptr;
  4301. uint64_t hurb;
  4302. int target_size;
  4303. uintptr_t target_urb_adr;
  4304. abi_long ret;
  4305. target_size = thunk_type_size(usbfsurb_arg_type, THUNK_TARGET);
  4306. memset(buf_temp, 0, sizeof(uint64_t));
  4307. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4308. if (is_error(ret)) {
  4309. return ret;
  4310. }
  4311. memcpy(&hurb, buf_temp, sizeof(uint64_t));
  4312. lurb = (void *)((uintptr_t)hurb - offsetof(struct live_urb, host_urb));
  4313. if (!lurb->target_urb_adr) {
  4314. return -TARGET_EFAULT;
  4315. }
  4316. urb_hashtable_remove(lurb);
  4317. unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr,
  4318. lurb->host_urb.buffer_length);
  4319. lurb->target_buf_ptr = NULL;
  4320. /* restore the guest buffer pointer */
  4321. lurb->host_urb.buffer = (void *)(uintptr_t)lurb->target_buf_adr;
  4322. /* update the guest urb struct */
  4323. argptr = lock_user(VERIFY_WRITE, lurb->target_urb_adr, target_size, 0);
  4324. if (!argptr) {
  4325. g_free(lurb);
  4326. return -TARGET_EFAULT;
  4327. }
  4328. thunk_convert(argptr, &lurb->host_urb, usbfsurb_arg_type, THUNK_TARGET);
  4329. unlock_user(argptr, lurb->target_urb_adr, target_size);
  4330. target_size = thunk_type_size(ptrvoid_arg_type, THUNK_TARGET);
  4331. /* write back the urb handle */
  4332. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4333. if (!argptr) {
  4334. g_free(lurb);
  4335. return -TARGET_EFAULT;
  4336. }
  4337. /* GHashTable uses 64-bit keys but thunk_convert expects uintptr_t */
  4338. target_urb_adr = lurb->target_urb_adr;
  4339. thunk_convert(argptr, &target_urb_adr, ptrvoid_arg_type, THUNK_TARGET);
  4340. unlock_user(argptr, arg, target_size);
  4341. g_free(lurb);
  4342. return ret;
  4343. }
  4344. static abi_long
  4345. do_ioctl_usbdevfs_discardurb(const IOCTLEntry *ie,
  4346. uint8_t *buf_temp __attribute__((unused)),
  4347. int fd, int cmd, abi_long arg)
  4348. {
  4349. struct live_urb *lurb;
  4350. /* map target address back to host URB with metadata. */
  4351. lurb = urb_hashtable_lookup(arg);
  4352. if (!lurb) {
  4353. return -TARGET_EFAULT;
  4354. }
  4355. return get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb));
  4356. }
  4357. static abi_long
  4358. do_ioctl_usbdevfs_submiturb(const IOCTLEntry *ie, uint8_t *buf_temp,
  4359. int fd, int cmd, abi_long arg)
  4360. {
  4361. const argtype *arg_type = ie->arg_type;
  4362. int target_size;
  4363. abi_long ret;
  4364. void *argptr;
  4365. int rw_dir;
  4366. struct live_urb *lurb;
  4367. /*
  4368. * each submitted URB needs to map to a unique ID for the
  4369. * kernel, and that unique ID needs to be a pointer to
  4370. * host memory. hence, we need to malloc for each URB.
  4371. * isochronous transfers have a variable length struct.
  4372. */
  4373. arg_type++;
  4374. target_size = thunk_type_size(arg_type, THUNK_TARGET);
  4375. /* construct host copy of urb and metadata */
  4376. lurb = g_try_malloc0(sizeof(struct live_urb));
  4377. if (!lurb) {
  4378. return -TARGET_ENOMEM;
  4379. }
  4380. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4381. if (!argptr) {
  4382. g_free(lurb);
  4383. return -TARGET_EFAULT;
  4384. }
  4385. thunk_convert(&lurb->host_urb, argptr, arg_type, THUNK_HOST);
  4386. unlock_user(argptr, arg, 0);
  4387. lurb->target_urb_adr = arg;
  4388. lurb->target_buf_adr = (uintptr_t)lurb->host_urb.buffer;
  4389. /* buffer space used depends on endpoint type so lock the entire buffer */
  4390. /* control type urbs should check the buffer contents for true direction */
  4391. rw_dir = lurb->host_urb.endpoint & USB_DIR_IN ? VERIFY_WRITE : VERIFY_READ;
  4392. lurb->target_buf_ptr = lock_user(rw_dir, lurb->target_buf_adr,
  4393. lurb->host_urb.buffer_length, 1);
  4394. if (lurb->target_buf_ptr == NULL) {
  4395. g_free(lurb);
  4396. return -TARGET_EFAULT;
  4397. }
  4398. /* update buffer pointer in host copy */
  4399. lurb->host_urb.buffer = lurb->target_buf_ptr;
  4400. ret = get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb));
  4401. if (is_error(ret)) {
  4402. unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr, 0);
  4403. g_free(lurb);
  4404. } else {
  4405. urb_hashtable_insert(lurb);
  4406. }
  4407. return ret;
  4408. }
  4409. #endif /* CONFIG_USBFS */
  4410. static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
  4411. int cmd, abi_long arg)
  4412. {
  4413. void *argptr;
  4414. struct dm_ioctl *host_dm;
  4415. abi_long guest_data;
  4416. uint32_t guest_data_size;
  4417. int target_size;
  4418. const argtype *arg_type = ie->arg_type;
  4419. abi_long ret;
  4420. void *big_buf = NULL;
  4421. char *host_data;
  4422. arg_type++;
  4423. target_size = thunk_type_size(arg_type, 0);
  4424. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4425. if (!argptr) {
  4426. ret = -TARGET_EFAULT;
  4427. goto out;
  4428. }
  4429. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4430. unlock_user(argptr, arg, 0);
  4431. /* buf_temp is too small, so fetch things into a bigger buffer */
  4432. big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
  4433. memcpy(big_buf, buf_temp, target_size);
  4434. buf_temp = big_buf;
  4435. host_dm = big_buf;
  4436. guest_data = arg + host_dm->data_start;
  4437. if ((guest_data - arg) < 0) {
  4438. ret = -TARGET_EINVAL;
  4439. goto out;
  4440. }
  4441. guest_data_size = host_dm->data_size - host_dm->data_start;
  4442. host_data = (char*)host_dm + host_dm->data_start;
  4443. argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
  4444. if (!argptr) {
  4445. ret = -TARGET_EFAULT;
  4446. goto out;
  4447. }
  4448. switch (ie->host_cmd) {
  4449. case DM_REMOVE_ALL:
  4450. case DM_LIST_DEVICES:
  4451. case DM_DEV_CREATE:
  4452. case DM_DEV_REMOVE:
  4453. case DM_DEV_SUSPEND:
  4454. case DM_DEV_STATUS:
  4455. case DM_DEV_WAIT:
  4456. case DM_TABLE_STATUS:
  4457. case DM_TABLE_CLEAR:
  4458. case DM_TABLE_DEPS:
  4459. case DM_LIST_VERSIONS:
  4460. /* no input data */
  4461. break;
  4462. case DM_DEV_RENAME:
  4463. case DM_DEV_SET_GEOMETRY:
  4464. /* data contains only strings */
  4465. memcpy(host_data, argptr, guest_data_size);
  4466. break;
  4467. case DM_TARGET_MSG:
  4468. memcpy(host_data, argptr, guest_data_size);
  4469. *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
  4470. break;
  4471. case DM_TABLE_LOAD:
  4472. {
  4473. void *gspec = argptr;
  4474. void *cur_data = host_data;
  4475. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
  4476. int spec_size = thunk_type_size(arg_type, 0);
  4477. int i;
  4478. for (i = 0; i < host_dm->target_count; i++) {
  4479. struct dm_target_spec *spec = cur_data;
  4480. uint32_t next;
  4481. int slen;
  4482. thunk_convert(spec, gspec, arg_type, THUNK_HOST);
  4483. slen = strlen((char*)gspec + spec_size) + 1;
  4484. next = spec->next;
  4485. spec->next = sizeof(*spec) + slen;
  4486. strcpy((char*)&spec[1], gspec + spec_size);
  4487. gspec += next;
  4488. cur_data += spec->next;
  4489. }
  4490. break;
  4491. }
  4492. default:
  4493. ret = -TARGET_EINVAL;
  4494. unlock_user(argptr, guest_data, 0);
  4495. goto out;
  4496. }
  4497. unlock_user(argptr, guest_data, 0);
  4498. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4499. if (!is_error(ret)) {
  4500. guest_data = arg + host_dm->data_start;
  4501. guest_data_size = host_dm->data_size - host_dm->data_start;
  4502. argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
  4503. switch (ie->host_cmd) {
  4504. case DM_REMOVE_ALL:
  4505. case DM_DEV_CREATE:
  4506. case DM_DEV_REMOVE:
  4507. case DM_DEV_RENAME:
  4508. case DM_DEV_SUSPEND:
  4509. case DM_DEV_STATUS:
  4510. case DM_TABLE_LOAD:
  4511. case DM_TABLE_CLEAR:
  4512. case DM_TARGET_MSG:
  4513. case DM_DEV_SET_GEOMETRY:
  4514. /* no return data */
  4515. break;
  4516. case DM_LIST_DEVICES:
  4517. {
  4518. struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
  4519. uint32_t remaining_data = guest_data_size;
  4520. void *cur_data = argptr;
  4521. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
  4522. int nl_size = 12; /* can't use thunk_size due to alignment */
  4523. while (1) {
  4524. uint32_t next = nl->next;
  4525. if (next) {
  4526. nl->next = nl_size + (strlen(nl->name) + 1);
  4527. }
  4528. if (remaining_data < nl->next) {
  4529. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  4530. break;
  4531. }
  4532. thunk_convert(cur_data, nl, arg_type, THUNK_TARGET);
  4533. strcpy(cur_data + nl_size, nl->name);
  4534. cur_data += nl->next;
  4535. remaining_data -= nl->next;
  4536. if (!next) {
  4537. break;
  4538. }
  4539. nl = (void*)nl + next;
  4540. }
  4541. break;
  4542. }
  4543. case DM_DEV_WAIT:
  4544. case DM_TABLE_STATUS:
  4545. {
  4546. struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
  4547. void *cur_data = argptr;
  4548. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
  4549. int spec_size = thunk_type_size(arg_type, 0);
  4550. int i;
  4551. for (i = 0; i < host_dm->target_count; i++) {
  4552. uint32_t next = spec->next;
  4553. int slen = strlen((char*)&spec[1]) + 1;
  4554. spec->next = (cur_data - argptr) + spec_size + slen;
  4555. if (guest_data_size < spec->next) {
  4556. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  4557. break;
  4558. }
  4559. thunk_convert(cur_data, spec, arg_type, THUNK_TARGET);
  4560. strcpy(cur_data + spec_size, (char*)&spec[1]);
  4561. cur_data = argptr + spec->next;
  4562. spec = (void*)host_dm + host_dm->data_start + next;
  4563. }
  4564. break;
  4565. }
  4566. case DM_TABLE_DEPS:
  4567. {
  4568. void *hdata = (void*)host_dm + host_dm->data_start;
  4569. int count = *(uint32_t*)hdata;
  4570. uint64_t *hdev = hdata + 8;
  4571. uint64_t *gdev = argptr + 8;
  4572. int i;
  4573. *(uint32_t*)argptr = tswap32(count);
  4574. for (i = 0; i < count; i++) {
  4575. *gdev = tswap64(*hdev);
  4576. gdev++;
  4577. hdev++;
  4578. }
  4579. break;
  4580. }
  4581. case DM_LIST_VERSIONS:
  4582. {
  4583. struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
  4584. uint32_t remaining_data = guest_data_size;
  4585. void *cur_data = argptr;
  4586. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
  4587. int vers_size = thunk_type_size(arg_type, 0);
  4588. while (1) {
  4589. uint32_t next = vers->next;
  4590. if (next) {
  4591. vers->next = vers_size + (strlen(vers->name) + 1);
  4592. }
  4593. if (remaining_data < vers->next) {
  4594. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  4595. break;
  4596. }
  4597. thunk_convert(cur_data, vers, arg_type, THUNK_TARGET);
  4598. strcpy(cur_data + vers_size, vers->name);
  4599. cur_data += vers->next;
  4600. remaining_data -= vers->next;
  4601. if (!next) {
  4602. break;
  4603. }
  4604. vers = (void*)vers + next;
  4605. }
  4606. break;
  4607. }
  4608. default:
  4609. unlock_user(argptr, guest_data, 0);
  4610. ret = -TARGET_EINVAL;
  4611. goto out;
  4612. }
  4613. unlock_user(argptr, guest_data, guest_data_size);
  4614. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4615. if (!argptr) {
  4616. ret = -TARGET_EFAULT;
  4617. goto out;
  4618. }
  4619. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  4620. unlock_user(argptr, arg, target_size);
  4621. }
  4622. out:
  4623. g_free(big_buf);
  4624. return ret;
  4625. }
  4626. static abi_long do_ioctl_blkpg(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
  4627. int cmd, abi_long arg)
  4628. {
  4629. void *argptr;
  4630. int target_size;
  4631. const argtype *arg_type = ie->arg_type;
  4632. const argtype part_arg_type[] = { MK_STRUCT(STRUCT_blkpg_partition) };
  4633. abi_long ret;
  4634. struct blkpg_ioctl_arg *host_blkpg = (void*)buf_temp;
  4635. struct blkpg_partition host_part;
  4636. /* Read and convert blkpg */
  4637. arg_type++;
  4638. target_size = thunk_type_size(arg_type, 0);
  4639. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4640. if (!argptr) {
  4641. ret = -TARGET_EFAULT;
  4642. goto out;
  4643. }
  4644. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4645. unlock_user(argptr, arg, 0);
  4646. switch (host_blkpg->op) {
  4647. case BLKPG_ADD_PARTITION:
  4648. case BLKPG_DEL_PARTITION:
  4649. /* payload is struct blkpg_partition */
  4650. break;
  4651. default:
  4652. /* Unknown opcode */
  4653. ret = -TARGET_EINVAL;
  4654. goto out;
  4655. }
  4656. /* Read and convert blkpg->data */
  4657. arg = (abi_long)(uintptr_t)host_blkpg->data;
  4658. target_size = thunk_type_size(part_arg_type, 0);
  4659. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4660. if (!argptr) {
  4661. ret = -TARGET_EFAULT;
  4662. goto out;
  4663. }
  4664. thunk_convert(&host_part, argptr, part_arg_type, THUNK_HOST);
  4665. unlock_user(argptr, arg, 0);
  4666. /* Swizzle the data pointer to our local copy and call! */
  4667. host_blkpg->data = &host_part;
  4668. ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_blkpg));
  4669. out:
  4670. return ret;
  4671. }
  4672. static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
  4673. int fd, int cmd, abi_long arg)
  4674. {
  4675. const argtype *arg_type = ie->arg_type;
  4676. const StructEntry *se;
  4677. const argtype *field_types;
  4678. const int *dst_offsets, *src_offsets;
  4679. int target_size;
  4680. void *argptr;
  4681. abi_ulong *target_rt_dev_ptr = NULL;
  4682. unsigned long *host_rt_dev_ptr = NULL;
  4683. abi_long ret;
  4684. int i;
  4685. assert(ie->access == IOC_W);
  4686. assert(*arg_type == TYPE_PTR);
  4687. arg_type++;
  4688. assert(*arg_type == TYPE_STRUCT);
  4689. target_size = thunk_type_size(arg_type, 0);
  4690. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4691. if (!argptr) {
  4692. return -TARGET_EFAULT;
  4693. }
  4694. arg_type++;
  4695. assert(*arg_type == (int)STRUCT_rtentry);
  4696. se = struct_entries + *arg_type++;
  4697. assert(se->convert[0] == NULL);
  4698. /* convert struct here to be able to catch rt_dev string */
  4699. field_types = se->field_types;
  4700. dst_offsets = se->field_offsets[THUNK_HOST];
  4701. src_offsets = se->field_offsets[THUNK_TARGET];
  4702. for (i = 0; i < se->nb_fields; i++) {
  4703. if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) {
  4704. assert(*field_types == TYPE_PTRVOID);
  4705. target_rt_dev_ptr = (abi_ulong *)(argptr + src_offsets[i]);
  4706. host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]);
  4707. if (*target_rt_dev_ptr != 0) {
  4708. *host_rt_dev_ptr = (unsigned long)lock_user_string(
  4709. tswapal(*target_rt_dev_ptr));
  4710. if (!*host_rt_dev_ptr) {
  4711. unlock_user(argptr, arg, 0);
  4712. return -TARGET_EFAULT;
  4713. }
  4714. } else {
  4715. *host_rt_dev_ptr = 0;
  4716. }
  4717. field_types++;
  4718. continue;
  4719. }
  4720. field_types = thunk_convert(buf_temp + dst_offsets[i],
  4721. argptr + src_offsets[i],
  4722. field_types, THUNK_HOST);
  4723. }
  4724. unlock_user(argptr, arg, 0);
  4725. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4726. assert(host_rt_dev_ptr != NULL);
  4727. assert(target_rt_dev_ptr != NULL);
  4728. if (*host_rt_dev_ptr != 0) {
  4729. unlock_user((void *)*host_rt_dev_ptr,
  4730. *target_rt_dev_ptr, 0);
  4731. }
  4732. return ret;
  4733. }
  4734. static abi_long do_ioctl_kdsigaccept(const IOCTLEntry *ie, uint8_t *buf_temp,
  4735. int fd, int cmd, abi_long arg)
  4736. {
  4737. int sig = target_to_host_signal(arg);
  4738. return get_errno(safe_ioctl(fd, ie->host_cmd, sig));
  4739. }
  4740. static abi_long do_ioctl_SIOCGSTAMP(const IOCTLEntry *ie, uint8_t *buf_temp,
  4741. int fd, int cmd, abi_long arg)
  4742. {
  4743. struct timeval tv;
  4744. abi_long ret;
  4745. ret = get_errno(safe_ioctl(fd, SIOCGSTAMP, &tv));
  4746. if (is_error(ret)) {
  4747. return ret;
  4748. }
  4749. if (cmd == (int)TARGET_SIOCGSTAMP_OLD) {
  4750. if (copy_to_user_timeval(arg, &tv)) {
  4751. return -TARGET_EFAULT;
  4752. }
  4753. } else {
  4754. if (copy_to_user_timeval64(arg, &tv)) {
  4755. return -TARGET_EFAULT;
  4756. }
  4757. }
  4758. return ret;
  4759. }
  4760. static abi_long do_ioctl_SIOCGSTAMPNS(const IOCTLEntry *ie, uint8_t *buf_temp,
  4761. int fd, int cmd, abi_long arg)
  4762. {
  4763. struct timespec ts;
  4764. abi_long ret;
  4765. ret = get_errno(safe_ioctl(fd, SIOCGSTAMPNS, &ts));
  4766. if (is_error(ret)) {
  4767. return ret;
  4768. }
  4769. if (cmd == (int)TARGET_SIOCGSTAMPNS_OLD) {
  4770. if (host_to_target_timespec(arg, &ts)) {
  4771. return -TARGET_EFAULT;
  4772. }
  4773. } else{
  4774. if (host_to_target_timespec64(arg, &ts)) {
  4775. return -TARGET_EFAULT;
  4776. }
  4777. }
  4778. return ret;
  4779. }
  4780. #ifdef TIOCGPTPEER
  4781. static abi_long do_ioctl_tiocgptpeer(const IOCTLEntry *ie, uint8_t *buf_temp,
  4782. int fd, int cmd, abi_long arg)
  4783. {
  4784. int flags = target_to_host_bitmask(arg, fcntl_flags_tbl);
  4785. return get_errno(safe_ioctl(fd, ie->host_cmd, flags));
  4786. }
  4787. #endif
  4788. #ifdef HAVE_DRM_H
  4789. static void unlock_drm_version(struct drm_version *host_ver,
  4790. struct target_drm_version *target_ver,
  4791. bool copy)
  4792. {
  4793. unlock_user(host_ver->name, target_ver->name,
  4794. copy ? host_ver->name_len : 0);
  4795. unlock_user(host_ver->date, target_ver->date,
  4796. copy ? host_ver->date_len : 0);
  4797. unlock_user(host_ver->desc, target_ver->desc,
  4798. copy ? host_ver->desc_len : 0);
  4799. }
  4800. static inline abi_long target_to_host_drmversion(struct drm_version *host_ver,
  4801. struct target_drm_version *target_ver)
  4802. {
  4803. memset(host_ver, 0, sizeof(*host_ver));
  4804. __get_user(host_ver->name_len, &target_ver->name_len);
  4805. if (host_ver->name_len) {
  4806. host_ver->name = lock_user(VERIFY_WRITE, target_ver->name,
  4807. target_ver->name_len, 0);
  4808. if (!host_ver->name) {
  4809. return -EFAULT;
  4810. }
  4811. }
  4812. __get_user(host_ver->date_len, &target_ver->date_len);
  4813. if (host_ver->date_len) {
  4814. host_ver->date = lock_user(VERIFY_WRITE, target_ver->date,
  4815. target_ver->date_len, 0);
  4816. if (!host_ver->date) {
  4817. goto err;
  4818. }
  4819. }
  4820. __get_user(host_ver->desc_len, &target_ver->desc_len);
  4821. if (host_ver->desc_len) {
  4822. host_ver->desc = lock_user(VERIFY_WRITE, target_ver->desc,
  4823. target_ver->desc_len, 0);
  4824. if (!host_ver->desc) {
  4825. goto err;
  4826. }
  4827. }
  4828. return 0;
  4829. err:
  4830. unlock_drm_version(host_ver, target_ver, false);
  4831. return -EFAULT;
  4832. }
  4833. static inline void host_to_target_drmversion(
  4834. struct target_drm_version *target_ver,
  4835. struct drm_version *host_ver)
  4836. {
  4837. __put_user(host_ver->version_major, &target_ver->version_major);
  4838. __put_user(host_ver->version_minor, &target_ver->version_minor);
  4839. __put_user(host_ver->version_patchlevel, &target_ver->version_patchlevel);
  4840. __put_user(host_ver->name_len, &target_ver->name_len);
  4841. __put_user(host_ver->date_len, &target_ver->date_len);
  4842. __put_user(host_ver->desc_len, &target_ver->desc_len);
  4843. unlock_drm_version(host_ver, target_ver, true);
  4844. }
  4845. static abi_long do_ioctl_drm(const IOCTLEntry *ie, uint8_t *buf_temp,
  4846. int fd, int cmd, abi_long arg)
  4847. {
  4848. struct drm_version *ver;
  4849. struct target_drm_version *target_ver;
  4850. abi_long ret;
  4851. switch (ie->host_cmd) {
  4852. case DRM_IOCTL_VERSION:
  4853. if (!lock_user_struct(VERIFY_WRITE, target_ver, arg, 0)) {
  4854. return -TARGET_EFAULT;
  4855. }
  4856. ver = (struct drm_version *)buf_temp;
  4857. ret = target_to_host_drmversion(ver, target_ver);
  4858. if (!is_error(ret)) {
  4859. ret = get_errno(safe_ioctl(fd, ie->host_cmd, ver));
  4860. if (is_error(ret)) {
  4861. unlock_drm_version(ver, target_ver, false);
  4862. } else {
  4863. host_to_target_drmversion(target_ver, ver);
  4864. }
  4865. }
  4866. unlock_user_struct(target_ver, arg, 0);
  4867. return ret;
  4868. }
  4869. return -TARGET_ENOSYS;
  4870. }
  4871. #endif
  4872. IOCTLEntry ioctl_entries[] = {
  4873. #define IOCTL(cmd, access, ...) \
  4874. { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
  4875. #define IOCTL_SPECIAL(cmd, access, dofn, ...) \
  4876. { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
  4877. #define IOCTL_IGNORE(cmd) \
  4878. { TARGET_ ## cmd, 0, #cmd },
  4879. #include "ioctls.h"
  4880. { 0, 0, },
  4881. };
  4882. /* ??? Implement proper locking for ioctls. */
  4883. /* do_ioctl() Must return target values and target errnos. */
  4884. static abi_long do_ioctl(int fd, int cmd, abi_long arg)
  4885. {
  4886. const IOCTLEntry *ie;
  4887. const argtype *arg_type;
  4888. abi_long ret;
  4889. uint8_t buf_temp[MAX_STRUCT_SIZE];
  4890. int target_size;
  4891. void *argptr;
  4892. ie = ioctl_entries;
  4893. for(;;) {
  4894. if (ie->target_cmd == 0) {
  4895. qemu_log_mask(
  4896. LOG_UNIMP, "Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
  4897. return -TARGET_ENOSYS;
  4898. }
  4899. if (ie->target_cmd == cmd)
  4900. break;
  4901. ie++;
  4902. }
  4903. arg_type = ie->arg_type;
  4904. if (ie->do_ioctl) {
  4905. return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
  4906. } else if (!ie->host_cmd) {
  4907. /* Some architectures define BSD ioctls in their headers
  4908. that are not implemented in Linux. */
  4909. return -TARGET_ENOSYS;
  4910. }
  4911. switch(arg_type[0]) {
  4912. case TYPE_NULL:
  4913. /* no argument */
  4914. ret = get_errno(safe_ioctl(fd, ie->host_cmd));
  4915. break;
  4916. case TYPE_PTRVOID:
  4917. case TYPE_INT:
  4918. case TYPE_LONG:
  4919. case TYPE_ULONG:
  4920. ret = get_errno(safe_ioctl(fd, ie->host_cmd, arg));
  4921. break;
  4922. case TYPE_PTR:
  4923. arg_type++;
  4924. target_size = thunk_type_size(arg_type, 0);
  4925. switch(ie->access) {
  4926. case IOC_R:
  4927. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4928. if (!is_error(ret)) {
  4929. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4930. if (!argptr)
  4931. return -TARGET_EFAULT;
  4932. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  4933. unlock_user(argptr, arg, target_size);
  4934. }
  4935. break;
  4936. case IOC_W:
  4937. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4938. if (!argptr)
  4939. return -TARGET_EFAULT;
  4940. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4941. unlock_user(argptr, arg, 0);
  4942. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4943. break;
  4944. default:
  4945. case IOC_RW:
  4946. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4947. if (!argptr)
  4948. return -TARGET_EFAULT;
  4949. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4950. unlock_user(argptr, arg, 0);
  4951. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4952. if (!is_error(ret)) {
  4953. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4954. if (!argptr)
  4955. return -TARGET_EFAULT;
  4956. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  4957. unlock_user(argptr, arg, target_size);
  4958. }
  4959. break;
  4960. }
  4961. break;
  4962. default:
  4963. qemu_log_mask(LOG_UNIMP,
  4964. "Unsupported ioctl type: cmd=0x%04lx type=%d\n",
  4965. (long)cmd, arg_type[0]);
  4966. ret = -TARGET_ENOSYS;
  4967. break;
  4968. }
  4969. return ret;
  4970. }
  4971. static const bitmask_transtbl iflag_tbl[] = {
  4972. { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
  4973. { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
  4974. { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
  4975. { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
  4976. { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
  4977. { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
  4978. { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
  4979. { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
  4980. { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
  4981. { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
  4982. { TARGET_IXON, TARGET_IXON, IXON, IXON },
  4983. { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
  4984. { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
  4985. { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
  4986. { 0, 0, 0, 0 }
  4987. };
  4988. static const bitmask_transtbl oflag_tbl[] = {
  4989. { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
  4990. { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
  4991. { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
  4992. { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
  4993. { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
  4994. { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
  4995. { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
  4996. { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
  4997. { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
  4998. { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
  4999. { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
  5000. { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
  5001. { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
  5002. { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
  5003. { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
  5004. { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
  5005. { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
  5006. { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
  5007. { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
  5008. { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
  5009. { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
  5010. { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
  5011. { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
  5012. { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
  5013. { 0, 0, 0, 0 }
  5014. };
  5015. static const bitmask_transtbl cflag_tbl[] = {
  5016. { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
  5017. { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
  5018. { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
  5019. { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
  5020. { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
  5021. { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
  5022. { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
  5023. { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
  5024. { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
  5025. { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
  5026. { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
  5027. { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
  5028. { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
  5029. { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
  5030. { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
  5031. { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
  5032. { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
  5033. { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
  5034. { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
  5035. { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
  5036. { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
  5037. { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
  5038. { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
  5039. { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
  5040. { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
  5041. { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
  5042. { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
  5043. { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
  5044. { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
  5045. { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
  5046. { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
  5047. { 0, 0, 0, 0 }
  5048. };
  5049. static const bitmask_transtbl lflag_tbl[] = {
  5050. { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
  5051. { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
  5052. { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
  5053. { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
  5054. { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
  5055. { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
  5056. { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
  5057. { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
  5058. { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
  5059. { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
  5060. { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
  5061. { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
  5062. { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
  5063. { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
  5064. { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
  5065. { 0, 0, 0, 0 }
  5066. };
  5067. static void target_to_host_termios (void *dst, const void *src)
  5068. {
  5069. struct host_termios *host = dst;
  5070. const struct target_termios *target = src;
  5071. host->c_iflag =
  5072. target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
  5073. host->c_oflag =
  5074. target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
  5075. host->c_cflag =
  5076. target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
  5077. host->c_lflag =
  5078. target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
  5079. host->c_line = target->c_line;
  5080. memset(host->c_cc, 0, sizeof(host->c_cc));
  5081. host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
  5082. host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
  5083. host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
  5084. host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
  5085. host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
  5086. host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
  5087. host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
  5088. host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
  5089. host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
  5090. host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
  5091. host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
  5092. host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
  5093. host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
  5094. host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
  5095. host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
  5096. host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
  5097. host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
  5098. }
  5099. static void host_to_target_termios (void *dst, const void *src)
  5100. {
  5101. struct target_termios *target = dst;
  5102. const struct host_termios *host = src;
  5103. target->c_iflag =
  5104. tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
  5105. target->c_oflag =
  5106. tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
  5107. target->c_cflag =
  5108. tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
  5109. target->c_lflag =
  5110. tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
  5111. target->c_line = host->c_line;
  5112. memset(target->c_cc, 0, sizeof(target->c_cc));
  5113. target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
  5114. target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
  5115. target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
  5116. target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
  5117. target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
  5118. target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
  5119. target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
  5120. target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
  5121. target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
  5122. target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
  5123. target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
  5124. target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
  5125. target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
  5126. target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
  5127. target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
  5128. target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
  5129. target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
  5130. }
  5131. static const StructEntry struct_termios_def = {
  5132. .convert = { host_to_target_termios, target_to_host_termios },
  5133. .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
  5134. .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
  5135. };
  5136. static bitmask_transtbl mmap_flags_tbl[] = {
  5137. { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
  5138. { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
  5139. { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
  5140. { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS,
  5141. MAP_ANONYMOUS, MAP_ANONYMOUS },
  5142. { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN,
  5143. MAP_GROWSDOWN, MAP_GROWSDOWN },
  5144. { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE,
  5145. MAP_DENYWRITE, MAP_DENYWRITE },
  5146. { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE,
  5147. MAP_EXECUTABLE, MAP_EXECUTABLE },
  5148. { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
  5149. { TARGET_MAP_NORESERVE, TARGET_MAP_NORESERVE,
  5150. MAP_NORESERVE, MAP_NORESERVE },
  5151. { TARGET_MAP_HUGETLB, TARGET_MAP_HUGETLB, MAP_HUGETLB, MAP_HUGETLB },
  5152. /* MAP_STACK had been ignored by the kernel for quite some time.
  5153. Recognize it for the target insofar as we do not want to pass
  5154. it through to the host. */
  5155. { TARGET_MAP_STACK, TARGET_MAP_STACK, 0, 0 },
  5156. { 0, 0, 0, 0 }
  5157. };
  5158. /*
  5159. * NOTE: TARGET_ABI32 is defined for TARGET_I386 (but not for TARGET_X86_64)
  5160. * TARGET_I386 is defined if TARGET_X86_64 is defined
  5161. */
  5162. #if defined(TARGET_I386)
  5163. /* NOTE: there is really one LDT for all the threads */
  5164. static uint8_t *ldt_table;
  5165. static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
  5166. {
  5167. int size;
  5168. void *p;
  5169. if (!ldt_table)
  5170. return 0;
  5171. size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
  5172. if (size > bytecount)
  5173. size = bytecount;
  5174. p = lock_user(VERIFY_WRITE, ptr, size, 0);
  5175. if (!p)
  5176. return -TARGET_EFAULT;
  5177. /* ??? Should this by byteswapped? */
  5178. memcpy(p, ldt_table, size);
  5179. unlock_user(p, ptr, size);
  5180. return size;
  5181. }
  5182. /* XXX: add locking support */
  5183. static abi_long write_ldt(CPUX86State *env,
  5184. abi_ulong ptr, unsigned long bytecount, int oldmode)
  5185. {
  5186. struct target_modify_ldt_ldt_s ldt_info;
  5187. struct target_modify_ldt_ldt_s *target_ldt_info;
  5188. int seg_32bit, contents, read_exec_only, limit_in_pages;
  5189. int seg_not_present, useable, lm;
  5190. uint32_t *lp, entry_1, entry_2;
  5191. if (bytecount != sizeof(ldt_info))
  5192. return -TARGET_EINVAL;
  5193. if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
  5194. return -TARGET_EFAULT;
  5195. ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
  5196. ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
  5197. ldt_info.limit = tswap32(target_ldt_info->limit);
  5198. ldt_info.flags = tswap32(target_ldt_info->flags);
  5199. unlock_user_struct(target_ldt_info, ptr, 0);
  5200. if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
  5201. return -TARGET_EINVAL;
  5202. seg_32bit = ldt_info.flags & 1;
  5203. contents = (ldt_info.flags >> 1) & 3;
  5204. read_exec_only = (ldt_info.flags >> 3) & 1;
  5205. limit_in_pages = (ldt_info.flags >> 4) & 1;
  5206. seg_not_present = (ldt_info.flags >> 5) & 1;
  5207. useable = (ldt_info.flags >> 6) & 1;
  5208. #ifdef TARGET_ABI32
  5209. lm = 0;
  5210. #else
  5211. lm = (ldt_info.flags >> 7) & 1;
  5212. #endif
  5213. if (contents == 3) {
  5214. if (oldmode)
  5215. return -TARGET_EINVAL;
  5216. if (seg_not_present == 0)
  5217. return -TARGET_EINVAL;
  5218. }
  5219. /* allocate the LDT */
  5220. if (!ldt_table) {
  5221. env->ldt.base = target_mmap(0,
  5222. TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
  5223. PROT_READ|PROT_WRITE,
  5224. MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
  5225. if (env->ldt.base == -1)
  5226. return -TARGET_ENOMEM;
  5227. memset(g2h(env->ldt.base), 0,
  5228. TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
  5229. env->ldt.limit = 0xffff;
  5230. ldt_table = g2h(env->ldt.base);
  5231. }
  5232. /* NOTE: same code as Linux kernel */
  5233. /* Allow LDTs to be cleared by the user. */
  5234. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  5235. if (oldmode ||
  5236. (contents == 0 &&
  5237. read_exec_only == 1 &&
  5238. seg_32bit == 0 &&
  5239. limit_in_pages == 0 &&
  5240. seg_not_present == 1 &&
  5241. useable == 0 )) {
  5242. entry_1 = 0;
  5243. entry_2 = 0;
  5244. goto install;
  5245. }
  5246. }
  5247. entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
  5248. (ldt_info.limit & 0x0ffff);
  5249. entry_2 = (ldt_info.base_addr & 0xff000000) |
  5250. ((ldt_info.base_addr & 0x00ff0000) >> 16) |
  5251. (ldt_info.limit & 0xf0000) |
  5252. ((read_exec_only ^ 1) << 9) |
  5253. (contents << 10) |
  5254. ((seg_not_present ^ 1) << 15) |
  5255. (seg_32bit << 22) |
  5256. (limit_in_pages << 23) |
  5257. (lm << 21) |
  5258. 0x7000;
  5259. if (!oldmode)
  5260. entry_2 |= (useable << 20);
  5261. /* Install the new entry ... */
  5262. install:
  5263. lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
  5264. lp[0] = tswap32(entry_1);
  5265. lp[1] = tswap32(entry_2);
  5266. return 0;
  5267. }
  5268. /* specific and weird i386 syscalls */
  5269. static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
  5270. unsigned long bytecount)
  5271. {
  5272. abi_long ret;
  5273. switch (func) {
  5274. case 0:
  5275. ret = read_ldt(ptr, bytecount);
  5276. break;
  5277. case 1:
  5278. ret = write_ldt(env, ptr, bytecount, 1);
  5279. break;
  5280. case 0x11:
  5281. ret = write_ldt(env, ptr, bytecount, 0);
  5282. break;
  5283. default:
  5284. ret = -TARGET_ENOSYS;
  5285. break;
  5286. }
  5287. return ret;
  5288. }
  5289. #if defined(TARGET_ABI32)
  5290. abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
  5291. {
  5292. uint64_t *gdt_table = g2h(env->gdt.base);
  5293. struct target_modify_ldt_ldt_s ldt_info;
  5294. struct target_modify_ldt_ldt_s *target_ldt_info;
  5295. int seg_32bit, contents, read_exec_only, limit_in_pages;
  5296. int seg_not_present, useable, lm;
  5297. uint32_t *lp, entry_1, entry_2;
  5298. int i;
  5299. lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
  5300. if (!target_ldt_info)
  5301. return -TARGET_EFAULT;
  5302. ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
  5303. ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
  5304. ldt_info.limit = tswap32(target_ldt_info->limit);
  5305. ldt_info.flags = tswap32(target_ldt_info->flags);
  5306. if (ldt_info.entry_number == -1) {
  5307. for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
  5308. if (gdt_table[i] == 0) {
  5309. ldt_info.entry_number = i;
  5310. target_ldt_info->entry_number = tswap32(i);
  5311. break;
  5312. }
  5313. }
  5314. }
  5315. unlock_user_struct(target_ldt_info, ptr, 1);
  5316. if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
  5317. ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
  5318. return -TARGET_EINVAL;
  5319. seg_32bit = ldt_info.flags & 1;
  5320. contents = (ldt_info.flags >> 1) & 3;
  5321. read_exec_only = (ldt_info.flags >> 3) & 1;
  5322. limit_in_pages = (ldt_info.flags >> 4) & 1;
  5323. seg_not_present = (ldt_info.flags >> 5) & 1;
  5324. useable = (ldt_info.flags >> 6) & 1;
  5325. #ifdef TARGET_ABI32
  5326. lm = 0;
  5327. #else
  5328. lm = (ldt_info.flags >> 7) & 1;
  5329. #endif
  5330. if (contents == 3) {
  5331. if (seg_not_present == 0)
  5332. return -TARGET_EINVAL;
  5333. }
  5334. /* NOTE: same code as Linux kernel */
  5335. /* Allow LDTs to be cleared by the user. */
  5336. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  5337. if ((contents == 0 &&
  5338. read_exec_only == 1 &&
  5339. seg_32bit == 0 &&
  5340. limit_in_pages == 0 &&
  5341. seg_not_present == 1 &&
  5342. useable == 0 )) {
  5343. entry_1 = 0;
  5344. entry_2 = 0;
  5345. goto install;
  5346. }
  5347. }
  5348. entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
  5349. (ldt_info.limit & 0x0ffff);
  5350. entry_2 = (ldt_info.base_addr & 0xff000000) |
  5351. ((ldt_info.base_addr & 0x00ff0000) >> 16) |
  5352. (ldt_info.limit & 0xf0000) |
  5353. ((read_exec_only ^ 1) << 9) |
  5354. (contents << 10) |
  5355. ((seg_not_present ^ 1) << 15) |
  5356. (seg_32bit << 22) |
  5357. (limit_in_pages << 23) |
  5358. (useable << 20) |
  5359. (lm << 21) |
  5360. 0x7000;
  5361. /* Install the new entry ... */
  5362. install:
  5363. lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
  5364. lp[0] = tswap32(entry_1);
  5365. lp[1] = tswap32(entry_2);
  5366. return 0;
  5367. }
  5368. static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
  5369. {
  5370. struct target_modify_ldt_ldt_s *target_ldt_info;
  5371. uint64_t *gdt_table = g2h(env->gdt.base);
  5372. uint32_t base_addr, limit, flags;
  5373. int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
  5374. int seg_not_present, useable, lm;
  5375. uint32_t *lp, entry_1, entry_2;
  5376. lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
  5377. if (!target_ldt_info)
  5378. return -TARGET_EFAULT;
  5379. idx = tswap32(target_ldt_info->entry_number);
  5380. if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
  5381. idx > TARGET_GDT_ENTRY_TLS_MAX) {
  5382. unlock_user_struct(target_ldt_info, ptr, 1);
  5383. return -TARGET_EINVAL;
  5384. }
  5385. lp = (uint32_t *)(gdt_table + idx);
  5386. entry_1 = tswap32(lp[0]);
  5387. entry_2 = tswap32(lp[1]);
  5388. read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
  5389. contents = (entry_2 >> 10) & 3;
  5390. seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
  5391. seg_32bit = (entry_2 >> 22) & 1;
  5392. limit_in_pages = (entry_2 >> 23) & 1;
  5393. useable = (entry_2 >> 20) & 1;
  5394. #ifdef TARGET_ABI32
  5395. lm = 0;
  5396. #else
  5397. lm = (entry_2 >> 21) & 1;
  5398. #endif
  5399. flags = (seg_32bit << 0) | (contents << 1) |
  5400. (read_exec_only << 3) | (limit_in_pages << 4) |
  5401. (seg_not_present << 5) | (useable << 6) | (lm << 7);
  5402. limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
  5403. base_addr = (entry_1 >> 16) |
  5404. (entry_2 & 0xff000000) |
  5405. ((entry_2 & 0xff) << 16);
  5406. target_ldt_info->base_addr = tswapal(base_addr);
  5407. target_ldt_info->limit = tswap32(limit);
  5408. target_ldt_info->flags = tswap32(flags);
  5409. unlock_user_struct(target_ldt_info, ptr, 1);
  5410. return 0;
  5411. }
  5412. abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
  5413. {
  5414. return -TARGET_ENOSYS;
  5415. }
  5416. #else
  5417. abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
  5418. {
  5419. abi_long ret = 0;
  5420. abi_ulong val;
  5421. int idx;
  5422. switch(code) {
  5423. case TARGET_ARCH_SET_GS:
  5424. case TARGET_ARCH_SET_FS:
  5425. if (code == TARGET_ARCH_SET_GS)
  5426. idx = R_GS;
  5427. else
  5428. idx = R_FS;
  5429. cpu_x86_load_seg(env, idx, 0);
  5430. env->segs[idx].base = addr;
  5431. break;
  5432. case TARGET_ARCH_GET_GS:
  5433. case TARGET_ARCH_GET_FS:
  5434. if (code == TARGET_ARCH_GET_GS)
  5435. idx = R_GS;
  5436. else
  5437. idx = R_FS;
  5438. val = env->segs[idx].base;
  5439. if (put_user(val, addr, abi_ulong))
  5440. ret = -TARGET_EFAULT;
  5441. break;
  5442. default:
  5443. ret = -TARGET_EINVAL;
  5444. break;
  5445. }
  5446. return ret;
  5447. }
  5448. #endif /* defined(TARGET_ABI32 */
  5449. #endif /* defined(TARGET_I386) */
  5450. #define NEW_STACK_SIZE 0x40000
  5451. static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
  5452. typedef struct {
  5453. CPUArchState *env;
  5454. pthread_mutex_t mutex;
  5455. pthread_cond_t cond;
  5456. pthread_t thread;
  5457. uint32_t tid;
  5458. abi_ulong child_tidptr;
  5459. abi_ulong parent_tidptr;
  5460. sigset_t sigmask;
  5461. } new_thread_info;
  5462. static void *clone_func(void *arg)
  5463. {
  5464. new_thread_info *info = arg;
  5465. CPUArchState *env;
  5466. CPUState *cpu;
  5467. TaskState *ts;
  5468. rcu_register_thread();
  5469. tcg_register_thread();
  5470. env = info->env;
  5471. cpu = env_cpu(env);
  5472. thread_cpu = cpu;
  5473. ts = (TaskState *)cpu->opaque;
  5474. info->tid = sys_gettid();
  5475. task_settid(ts);
  5476. if (info->child_tidptr)
  5477. put_user_u32(info->tid, info->child_tidptr);
  5478. if (info->parent_tidptr)
  5479. put_user_u32(info->tid, info->parent_tidptr);
  5480. qemu_guest_random_seed_thread_part2(cpu->random_seed);
  5481. /* Enable signals. */
  5482. sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
  5483. /* Signal to the parent that we're ready. */
  5484. pthread_mutex_lock(&info->mutex);
  5485. pthread_cond_broadcast(&info->cond);
  5486. pthread_mutex_unlock(&info->mutex);
  5487. /* Wait until the parent has finished initializing the tls state. */
  5488. pthread_mutex_lock(&clone_lock);
  5489. pthread_mutex_unlock(&clone_lock);
  5490. cpu_loop(env);
  5491. /* never exits */
  5492. return NULL;
  5493. }
  5494. /* do_fork() Must return host values and target errnos (unlike most
  5495. do_*() functions). */
  5496. static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
  5497. abi_ulong parent_tidptr, target_ulong newtls,
  5498. abi_ulong child_tidptr)
  5499. {
  5500. CPUState *cpu = env_cpu(env);
  5501. int ret;
  5502. TaskState *ts;
  5503. CPUState *new_cpu;
  5504. CPUArchState *new_env;
  5505. sigset_t sigmask;
  5506. flags &= ~CLONE_IGNORED_FLAGS;
  5507. /* Emulate vfork() with fork() */
  5508. if (flags & CLONE_VFORK)
  5509. flags &= ~(CLONE_VFORK | CLONE_VM);
  5510. if (flags & CLONE_VM) {
  5511. TaskState *parent_ts = (TaskState *)cpu->opaque;
  5512. new_thread_info info;
  5513. pthread_attr_t attr;
  5514. if (((flags & CLONE_THREAD_FLAGS) != CLONE_THREAD_FLAGS) ||
  5515. (flags & CLONE_INVALID_THREAD_FLAGS)) {
  5516. return -TARGET_EINVAL;
  5517. }
  5518. ts = g_new0(TaskState, 1);
  5519. init_task_state(ts);
  5520. /* Grab a mutex so that thread setup appears atomic. */
  5521. pthread_mutex_lock(&clone_lock);
  5522. /* we create a new CPU instance. */
  5523. new_env = cpu_copy(env);
  5524. /* Init regs that differ from the parent. */
  5525. cpu_clone_regs_child(new_env, newsp, flags);
  5526. cpu_clone_regs_parent(env, flags);
  5527. new_cpu = env_cpu(new_env);
  5528. new_cpu->opaque = ts;
  5529. ts->bprm = parent_ts->bprm;
  5530. ts->info = parent_ts->info;
  5531. ts->signal_mask = parent_ts->signal_mask;
  5532. if (flags & CLONE_CHILD_CLEARTID) {
  5533. ts->child_tidptr = child_tidptr;
  5534. }
  5535. if (flags & CLONE_SETTLS) {
  5536. cpu_set_tls (new_env, newtls);
  5537. }
  5538. memset(&info, 0, sizeof(info));
  5539. pthread_mutex_init(&info.mutex, NULL);
  5540. pthread_mutex_lock(&info.mutex);
  5541. pthread_cond_init(&info.cond, NULL);
  5542. info.env = new_env;
  5543. if (flags & CLONE_CHILD_SETTID) {
  5544. info.child_tidptr = child_tidptr;
  5545. }
  5546. if (flags & CLONE_PARENT_SETTID) {
  5547. info.parent_tidptr = parent_tidptr;
  5548. }
  5549. ret = pthread_attr_init(&attr);
  5550. ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
  5551. ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  5552. /* It is not safe to deliver signals until the child has finished
  5553. initializing, so temporarily block all signals. */
  5554. sigfillset(&sigmask);
  5555. sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
  5556. cpu->random_seed = qemu_guest_random_seed_thread_part1();
  5557. /* If this is our first additional thread, we need to ensure we
  5558. * generate code for parallel execution and flush old translations.
  5559. */
  5560. if (!parallel_cpus) {
  5561. parallel_cpus = true;
  5562. tb_flush(cpu);
  5563. }
  5564. ret = pthread_create(&info.thread, &attr, clone_func, &info);
  5565. /* TODO: Free new CPU state if thread creation failed. */
  5566. sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
  5567. pthread_attr_destroy(&attr);
  5568. if (ret == 0) {
  5569. /* Wait for the child to initialize. */
  5570. pthread_cond_wait(&info.cond, &info.mutex);
  5571. ret = info.tid;
  5572. } else {
  5573. ret = -1;
  5574. }
  5575. pthread_mutex_unlock(&info.mutex);
  5576. pthread_cond_destroy(&info.cond);
  5577. pthread_mutex_destroy(&info.mutex);
  5578. pthread_mutex_unlock(&clone_lock);
  5579. } else {
  5580. /* if no CLONE_VM, we consider it is a fork */
  5581. if (flags & CLONE_INVALID_FORK_FLAGS) {
  5582. return -TARGET_EINVAL;
  5583. }
  5584. /* We can't support custom termination signals */
  5585. if ((flags & CSIGNAL) != TARGET_SIGCHLD) {
  5586. return -TARGET_EINVAL;
  5587. }
  5588. if (block_signals()) {
  5589. return -TARGET_ERESTARTSYS;
  5590. }
  5591. fork_start();
  5592. ret = fork();
  5593. if (ret == 0) {
  5594. /* Child Process. */
  5595. cpu_clone_regs_child(env, newsp, flags);
  5596. fork_end(1);
  5597. /* There is a race condition here. The parent process could
  5598. theoretically read the TID in the child process before the child
  5599. tid is set. This would require using either ptrace
  5600. (not implemented) or having *_tidptr to point at a shared memory
  5601. mapping. We can't repeat the spinlock hack used above because
  5602. the child process gets its own copy of the lock. */
  5603. if (flags & CLONE_CHILD_SETTID)
  5604. put_user_u32(sys_gettid(), child_tidptr);
  5605. if (flags & CLONE_PARENT_SETTID)
  5606. put_user_u32(sys_gettid(), parent_tidptr);
  5607. ts = (TaskState *)cpu->opaque;
  5608. if (flags & CLONE_SETTLS)
  5609. cpu_set_tls (env, newtls);
  5610. if (flags & CLONE_CHILD_CLEARTID)
  5611. ts->child_tidptr = child_tidptr;
  5612. } else {
  5613. cpu_clone_regs_parent(env, flags);
  5614. fork_end(0);
  5615. }
  5616. }
  5617. return ret;
  5618. }
  5619. /* warning : doesn't handle linux specific flags... */
  5620. static int target_to_host_fcntl_cmd(int cmd)
  5621. {
  5622. int ret;
  5623. switch(cmd) {
  5624. case TARGET_F_DUPFD:
  5625. case TARGET_F_GETFD:
  5626. case TARGET_F_SETFD:
  5627. case TARGET_F_GETFL:
  5628. case TARGET_F_SETFL:
  5629. case TARGET_F_OFD_GETLK:
  5630. case TARGET_F_OFD_SETLK:
  5631. case TARGET_F_OFD_SETLKW:
  5632. ret = cmd;
  5633. break;
  5634. case TARGET_F_GETLK:
  5635. ret = F_GETLK64;
  5636. break;
  5637. case TARGET_F_SETLK:
  5638. ret = F_SETLK64;
  5639. break;
  5640. case TARGET_F_SETLKW:
  5641. ret = F_SETLKW64;
  5642. break;
  5643. case TARGET_F_GETOWN:
  5644. ret = F_GETOWN;
  5645. break;
  5646. case TARGET_F_SETOWN:
  5647. ret = F_SETOWN;
  5648. break;
  5649. case TARGET_F_GETSIG:
  5650. ret = F_GETSIG;
  5651. break;
  5652. case TARGET_F_SETSIG:
  5653. ret = F_SETSIG;
  5654. break;
  5655. #if TARGET_ABI_BITS == 32
  5656. case TARGET_F_GETLK64:
  5657. ret = F_GETLK64;
  5658. break;
  5659. case TARGET_F_SETLK64:
  5660. ret = F_SETLK64;
  5661. break;
  5662. case TARGET_F_SETLKW64:
  5663. ret = F_SETLKW64;
  5664. break;
  5665. #endif
  5666. case TARGET_F_SETLEASE:
  5667. ret = F_SETLEASE;
  5668. break;
  5669. case TARGET_F_GETLEASE:
  5670. ret = F_GETLEASE;
  5671. break;
  5672. #ifdef F_DUPFD_CLOEXEC
  5673. case TARGET_F_DUPFD_CLOEXEC:
  5674. ret = F_DUPFD_CLOEXEC;
  5675. break;
  5676. #endif
  5677. case TARGET_F_NOTIFY:
  5678. ret = F_NOTIFY;
  5679. break;
  5680. #ifdef F_GETOWN_EX
  5681. case TARGET_F_GETOWN_EX:
  5682. ret = F_GETOWN_EX;
  5683. break;
  5684. #endif
  5685. #ifdef F_SETOWN_EX
  5686. case TARGET_F_SETOWN_EX:
  5687. ret = F_SETOWN_EX;
  5688. break;
  5689. #endif
  5690. #ifdef F_SETPIPE_SZ
  5691. case TARGET_F_SETPIPE_SZ:
  5692. ret = F_SETPIPE_SZ;
  5693. break;
  5694. case TARGET_F_GETPIPE_SZ:
  5695. ret = F_GETPIPE_SZ;
  5696. break;
  5697. #endif
  5698. default:
  5699. ret = -TARGET_EINVAL;
  5700. break;
  5701. }
  5702. #if defined(__powerpc64__)
  5703. /* On PPC64, glibc headers has the F_*LK* defined to 12, 13 and 14 and
  5704. * is not supported by kernel. The glibc fcntl call actually adjusts
  5705. * them to 5, 6 and 7 before making the syscall(). Since we make the
  5706. * syscall directly, adjust to what is supported by the kernel.
  5707. */
  5708. if (ret >= F_GETLK64 && ret <= F_SETLKW64) {
  5709. ret -= F_GETLK64 - 5;
  5710. }
  5711. #endif
  5712. return ret;
  5713. }
  5714. #define FLOCK_TRANSTBL \
  5715. switch (type) { \
  5716. TRANSTBL_CONVERT(F_RDLCK); \
  5717. TRANSTBL_CONVERT(F_WRLCK); \
  5718. TRANSTBL_CONVERT(F_UNLCK); \
  5719. TRANSTBL_CONVERT(F_EXLCK); \
  5720. TRANSTBL_CONVERT(F_SHLCK); \
  5721. }
  5722. static int target_to_host_flock(int type)
  5723. {
  5724. #define TRANSTBL_CONVERT(a) case TARGET_##a: return a
  5725. FLOCK_TRANSTBL
  5726. #undef TRANSTBL_CONVERT
  5727. return -TARGET_EINVAL;
  5728. }
  5729. static int host_to_target_flock(int type)
  5730. {
  5731. #define TRANSTBL_CONVERT(a) case a: return TARGET_##a
  5732. FLOCK_TRANSTBL
  5733. #undef TRANSTBL_CONVERT
  5734. /* if we don't know how to convert the value coming
  5735. * from the host we copy to the target field as-is
  5736. */
  5737. return type;
  5738. }
  5739. static inline abi_long copy_from_user_flock(struct flock64 *fl,
  5740. abi_ulong target_flock_addr)
  5741. {
  5742. struct target_flock *target_fl;
  5743. int l_type;
  5744. if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
  5745. return -TARGET_EFAULT;
  5746. }
  5747. __get_user(l_type, &target_fl->l_type);
  5748. l_type = target_to_host_flock(l_type);
  5749. if (l_type < 0) {
  5750. return l_type;
  5751. }
  5752. fl->l_type = l_type;
  5753. __get_user(fl->l_whence, &target_fl->l_whence);
  5754. __get_user(fl->l_start, &target_fl->l_start);
  5755. __get_user(fl->l_len, &target_fl->l_len);
  5756. __get_user(fl->l_pid, &target_fl->l_pid);
  5757. unlock_user_struct(target_fl, target_flock_addr, 0);
  5758. return 0;
  5759. }
  5760. static inline abi_long copy_to_user_flock(abi_ulong target_flock_addr,
  5761. const struct flock64 *fl)
  5762. {
  5763. struct target_flock *target_fl;
  5764. short l_type;
  5765. if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
  5766. return -TARGET_EFAULT;
  5767. }
  5768. l_type = host_to_target_flock(fl->l_type);
  5769. __put_user(l_type, &target_fl->l_type);
  5770. __put_user(fl->l_whence, &target_fl->l_whence);
  5771. __put_user(fl->l_start, &target_fl->l_start);
  5772. __put_user(fl->l_len, &target_fl->l_len);
  5773. __put_user(fl->l_pid, &target_fl->l_pid);
  5774. unlock_user_struct(target_fl, target_flock_addr, 1);
  5775. return 0;
  5776. }
  5777. typedef abi_long from_flock64_fn(struct flock64 *fl, abi_ulong target_addr);
  5778. typedef abi_long to_flock64_fn(abi_ulong target_addr, const struct flock64 *fl);
  5779. #if defined(TARGET_ARM) && TARGET_ABI_BITS == 32
  5780. static inline abi_long copy_from_user_oabi_flock64(struct flock64 *fl,
  5781. abi_ulong target_flock_addr)
  5782. {
  5783. struct target_oabi_flock64 *target_fl;
  5784. int l_type;
  5785. if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
  5786. return -TARGET_EFAULT;
  5787. }
  5788. __get_user(l_type, &target_fl->l_type);
  5789. l_type = target_to_host_flock(l_type);
  5790. if (l_type < 0) {
  5791. return l_type;
  5792. }
  5793. fl->l_type = l_type;
  5794. __get_user(fl->l_whence, &target_fl->l_whence);
  5795. __get_user(fl->l_start, &target_fl->l_start);
  5796. __get_user(fl->l_len, &target_fl->l_len);
  5797. __get_user(fl->l_pid, &target_fl->l_pid);
  5798. unlock_user_struct(target_fl, target_flock_addr, 0);
  5799. return 0;
  5800. }
  5801. static inline abi_long copy_to_user_oabi_flock64(abi_ulong target_flock_addr,
  5802. const struct flock64 *fl)
  5803. {
  5804. struct target_oabi_flock64 *target_fl;
  5805. short l_type;
  5806. if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
  5807. return -TARGET_EFAULT;
  5808. }
  5809. l_type = host_to_target_flock(fl->l_type);
  5810. __put_user(l_type, &target_fl->l_type);
  5811. __put_user(fl->l_whence, &target_fl->l_whence);
  5812. __put_user(fl->l_start, &target_fl->l_start);
  5813. __put_user(fl->l_len, &target_fl->l_len);
  5814. __put_user(fl->l_pid, &target_fl->l_pid);
  5815. unlock_user_struct(target_fl, target_flock_addr, 1);
  5816. return 0;
  5817. }
  5818. #endif
  5819. static inline abi_long copy_from_user_flock64(struct flock64 *fl,
  5820. abi_ulong target_flock_addr)
  5821. {
  5822. struct target_flock64 *target_fl;
  5823. int l_type;
  5824. if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
  5825. return -TARGET_EFAULT;
  5826. }
  5827. __get_user(l_type, &target_fl->l_type);
  5828. l_type = target_to_host_flock(l_type);
  5829. if (l_type < 0) {
  5830. return l_type;
  5831. }
  5832. fl->l_type = l_type;
  5833. __get_user(fl->l_whence, &target_fl->l_whence);
  5834. __get_user(fl->l_start, &target_fl->l_start);
  5835. __get_user(fl->l_len, &target_fl->l_len);
  5836. __get_user(fl->l_pid, &target_fl->l_pid);
  5837. unlock_user_struct(target_fl, target_flock_addr, 0);
  5838. return 0;
  5839. }
  5840. static inline abi_long copy_to_user_flock64(abi_ulong target_flock_addr,
  5841. const struct flock64 *fl)
  5842. {
  5843. struct target_flock64 *target_fl;
  5844. short l_type;
  5845. if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
  5846. return -TARGET_EFAULT;
  5847. }
  5848. l_type = host_to_target_flock(fl->l_type);
  5849. __put_user(l_type, &target_fl->l_type);
  5850. __put_user(fl->l_whence, &target_fl->l_whence);
  5851. __put_user(fl->l_start, &target_fl->l_start);
  5852. __put_user(fl->l_len, &target_fl->l_len);
  5853. __put_user(fl->l_pid, &target_fl->l_pid);
  5854. unlock_user_struct(target_fl, target_flock_addr, 1);
  5855. return 0;
  5856. }
  5857. static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
  5858. {
  5859. struct flock64 fl64;
  5860. #ifdef F_GETOWN_EX
  5861. struct f_owner_ex fox;
  5862. struct target_f_owner_ex *target_fox;
  5863. #endif
  5864. abi_long ret;
  5865. int host_cmd = target_to_host_fcntl_cmd(cmd);
  5866. if (host_cmd == -TARGET_EINVAL)
  5867. return host_cmd;
  5868. switch(cmd) {
  5869. case TARGET_F_GETLK:
  5870. ret = copy_from_user_flock(&fl64, arg);
  5871. if (ret) {
  5872. return ret;
  5873. }
  5874. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  5875. if (ret == 0) {
  5876. ret = copy_to_user_flock(arg, &fl64);
  5877. }
  5878. break;
  5879. case TARGET_F_SETLK:
  5880. case TARGET_F_SETLKW:
  5881. ret = copy_from_user_flock(&fl64, arg);
  5882. if (ret) {
  5883. return ret;
  5884. }
  5885. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  5886. break;
  5887. case TARGET_F_GETLK64:
  5888. case TARGET_F_OFD_GETLK:
  5889. ret = copy_from_user_flock64(&fl64, arg);
  5890. if (ret) {
  5891. return ret;
  5892. }
  5893. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  5894. if (ret == 0) {
  5895. ret = copy_to_user_flock64(arg, &fl64);
  5896. }
  5897. break;
  5898. case TARGET_F_SETLK64:
  5899. case TARGET_F_SETLKW64:
  5900. case TARGET_F_OFD_SETLK:
  5901. case TARGET_F_OFD_SETLKW:
  5902. ret = copy_from_user_flock64(&fl64, arg);
  5903. if (ret) {
  5904. return ret;
  5905. }
  5906. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  5907. break;
  5908. case TARGET_F_GETFL:
  5909. ret = get_errno(safe_fcntl(fd, host_cmd, arg));
  5910. if (ret >= 0) {
  5911. ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
  5912. }
  5913. break;
  5914. case TARGET_F_SETFL:
  5915. ret = get_errno(safe_fcntl(fd, host_cmd,
  5916. target_to_host_bitmask(arg,
  5917. fcntl_flags_tbl)));
  5918. break;
  5919. #ifdef F_GETOWN_EX
  5920. case TARGET_F_GETOWN_EX:
  5921. ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
  5922. if (ret >= 0) {
  5923. if (!lock_user_struct(VERIFY_WRITE, target_fox, arg, 0))
  5924. return -TARGET_EFAULT;
  5925. target_fox->type = tswap32(fox.type);
  5926. target_fox->pid = tswap32(fox.pid);
  5927. unlock_user_struct(target_fox, arg, 1);
  5928. }
  5929. break;
  5930. #endif
  5931. #ifdef F_SETOWN_EX
  5932. case TARGET_F_SETOWN_EX:
  5933. if (!lock_user_struct(VERIFY_READ, target_fox, arg, 1))
  5934. return -TARGET_EFAULT;
  5935. fox.type = tswap32(target_fox->type);
  5936. fox.pid = tswap32(target_fox->pid);
  5937. unlock_user_struct(target_fox, arg, 0);
  5938. ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
  5939. break;
  5940. #endif
  5941. case TARGET_F_SETOWN:
  5942. case TARGET_F_GETOWN:
  5943. case TARGET_F_SETSIG:
  5944. case TARGET_F_GETSIG:
  5945. case TARGET_F_SETLEASE:
  5946. case TARGET_F_GETLEASE:
  5947. case TARGET_F_SETPIPE_SZ:
  5948. case TARGET_F_GETPIPE_SZ:
  5949. ret = get_errno(safe_fcntl(fd, host_cmd, arg));
  5950. break;
  5951. default:
  5952. ret = get_errno(safe_fcntl(fd, cmd, arg));
  5953. break;
  5954. }
  5955. return ret;
  5956. }
  5957. #ifdef USE_UID16
  5958. static inline int high2lowuid(int uid)
  5959. {
  5960. if (uid > 65535)
  5961. return 65534;
  5962. else
  5963. return uid;
  5964. }
  5965. static inline int high2lowgid(int gid)
  5966. {
  5967. if (gid > 65535)
  5968. return 65534;
  5969. else
  5970. return gid;
  5971. }
  5972. static inline int low2highuid(int uid)
  5973. {
  5974. if ((int16_t)uid == -1)
  5975. return -1;
  5976. else
  5977. return uid;
  5978. }
  5979. static inline int low2highgid(int gid)
  5980. {
  5981. if ((int16_t)gid == -1)
  5982. return -1;
  5983. else
  5984. return gid;
  5985. }
  5986. static inline int tswapid(int id)
  5987. {
  5988. return tswap16(id);
  5989. }
  5990. #define put_user_id(x, gaddr) put_user_u16(x, gaddr)
  5991. #else /* !USE_UID16 */
  5992. static inline int high2lowuid(int uid)
  5993. {
  5994. return uid;
  5995. }
  5996. static inline int high2lowgid(int gid)
  5997. {
  5998. return gid;
  5999. }
  6000. static inline int low2highuid(int uid)
  6001. {
  6002. return uid;
  6003. }
  6004. static inline int low2highgid(int gid)
  6005. {
  6006. return gid;
  6007. }
  6008. static inline int tswapid(int id)
  6009. {
  6010. return tswap32(id);
  6011. }
  6012. #define put_user_id(x, gaddr) put_user_u32(x, gaddr)
  6013. #endif /* USE_UID16 */
  6014. /* We must do direct syscalls for setting UID/GID, because we want to
  6015. * implement the Linux system call semantics of "change only for this thread",
  6016. * not the libc/POSIX semantics of "change for all threads in process".
  6017. * (See http://ewontfix.com/17/ for more details.)
  6018. * We use the 32-bit version of the syscalls if present; if it is not
  6019. * then either the host architecture supports 32-bit UIDs natively with
  6020. * the standard syscall, or the 16-bit UID is the best we can do.
  6021. */
  6022. #ifdef __NR_setuid32
  6023. #define __NR_sys_setuid __NR_setuid32
  6024. #else
  6025. #define __NR_sys_setuid __NR_setuid
  6026. #endif
  6027. #ifdef __NR_setgid32
  6028. #define __NR_sys_setgid __NR_setgid32
  6029. #else
  6030. #define __NR_sys_setgid __NR_setgid
  6031. #endif
  6032. #ifdef __NR_setresuid32
  6033. #define __NR_sys_setresuid __NR_setresuid32
  6034. #else
  6035. #define __NR_sys_setresuid __NR_setresuid
  6036. #endif
  6037. #ifdef __NR_setresgid32
  6038. #define __NR_sys_setresgid __NR_setresgid32
  6039. #else
  6040. #define __NR_sys_setresgid __NR_setresgid
  6041. #endif
  6042. _syscall1(int, sys_setuid, uid_t, uid)
  6043. _syscall1(int, sys_setgid, gid_t, gid)
  6044. _syscall3(int, sys_setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
  6045. _syscall3(int, sys_setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
  6046. void syscall_init(void)
  6047. {
  6048. IOCTLEntry *ie;
  6049. const argtype *arg_type;
  6050. int size;
  6051. int i;
  6052. thunk_init(STRUCT_MAX);
  6053. #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
  6054. #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
  6055. #include "syscall_types.h"
  6056. #undef STRUCT
  6057. #undef STRUCT_SPECIAL
  6058. /* Build target_to_host_errno_table[] table from
  6059. * host_to_target_errno_table[]. */
  6060. for (i = 0; i < ERRNO_TABLE_SIZE; i++) {
  6061. target_to_host_errno_table[host_to_target_errno_table[i]] = i;
  6062. }
  6063. /* we patch the ioctl size if necessary. We rely on the fact that
  6064. no ioctl has all the bits at '1' in the size field */
  6065. ie = ioctl_entries;
  6066. while (ie->target_cmd != 0) {
  6067. if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
  6068. TARGET_IOC_SIZEMASK) {
  6069. arg_type = ie->arg_type;
  6070. if (arg_type[0] != TYPE_PTR) {
  6071. fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
  6072. ie->target_cmd);
  6073. exit(1);
  6074. }
  6075. arg_type++;
  6076. size = thunk_type_size(arg_type, 0);
  6077. ie->target_cmd = (ie->target_cmd &
  6078. ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
  6079. (size << TARGET_IOC_SIZESHIFT);
  6080. }
  6081. /* automatic consistency check if same arch */
  6082. #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
  6083. (defined(__x86_64__) && defined(TARGET_X86_64))
  6084. if (unlikely(ie->target_cmd != ie->host_cmd)) {
  6085. fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
  6086. ie->name, ie->target_cmd, ie->host_cmd);
  6087. }
  6088. #endif
  6089. ie++;
  6090. }
  6091. }
  6092. #ifdef TARGET_NR_truncate64
  6093. static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
  6094. abi_long arg2,
  6095. abi_long arg3,
  6096. abi_long arg4)
  6097. {
  6098. if (regpairs_aligned(cpu_env, TARGET_NR_truncate64)) {
  6099. arg2 = arg3;
  6100. arg3 = arg4;
  6101. }
  6102. return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
  6103. }
  6104. #endif
  6105. #ifdef TARGET_NR_ftruncate64
  6106. static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
  6107. abi_long arg2,
  6108. abi_long arg3,
  6109. abi_long arg4)
  6110. {
  6111. if (regpairs_aligned(cpu_env, TARGET_NR_ftruncate64)) {
  6112. arg2 = arg3;
  6113. arg3 = arg4;
  6114. }
  6115. return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
  6116. }
  6117. #endif
  6118. #if defined(TARGET_NR_timer_settime) || \
  6119. (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
  6120. static inline abi_long target_to_host_itimerspec(struct itimerspec *host_itspec,
  6121. abi_ulong target_addr)
  6122. {
  6123. struct target_itimerspec *target_itspec;
  6124. if (!lock_user_struct(VERIFY_READ, target_itspec, target_addr, 1)) {
  6125. return -TARGET_EFAULT;
  6126. }
  6127. host_itspec->it_interval.tv_sec =
  6128. tswapal(target_itspec->it_interval.tv_sec);
  6129. host_itspec->it_interval.tv_nsec =
  6130. tswapal(target_itspec->it_interval.tv_nsec);
  6131. host_itspec->it_value.tv_sec = tswapal(target_itspec->it_value.tv_sec);
  6132. host_itspec->it_value.tv_nsec = tswapal(target_itspec->it_value.tv_nsec);
  6133. unlock_user_struct(target_itspec, target_addr, 1);
  6134. return 0;
  6135. }
  6136. #endif
  6137. #if ((defined(TARGET_NR_timerfd_gettime) || \
  6138. defined(TARGET_NR_timerfd_settime)) && defined(CONFIG_TIMERFD)) || \
  6139. defined(TARGET_NR_timer_gettime) || defined(TARGET_NR_timer_settime)
  6140. static inline abi_long host_to_target_itimerspec(abi_ulong target_addr,
  6141. struct itimerspec *host_its)
  6142. {
  6143. struct target_itimerspec *target_itspec;
  6144. if (!lock_user_struct(VERIFY_WRITE, target_itspec, target_addr, 0)) {
  6145. return -TARGET_EFAULT;
  6146. }
  6147. target_itspec->it_interval.tv_sec = tswapal(host_its->it_interval.tv_sec);
  6148. target_itspec->it_interval.tv_nsec = tswapal(host_its->it_interval.tv_nsec);
  6149. target_itspec->it_value.tv_sec = tswapal(host_its->it_value.tv_sec);
  6150. target_itspec->it_value.tv_nsec = tswapal(host_its->it_value.tv_nsec);
  6151. unlock_user_struct(target_itspec, target_addr, 0);
  6152. return 0;
  6153. }
  6154. #endif
  6155. #if defined(TARGET_NR_adjtimex) || \
  6156. (defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME))
  6157. static inline abi_long target_to_host_timex(struct timex *host_tx,
  6158. abi_long target_addr)
  6159. {
  6160. struct target_timex *target_tx;
  6161. if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) {
  6162. return -TARGET_EFAULT;
  6163. }
  6164. __get_user(host_tx->modes, &target_tx->modes);
  6165. __get_user(host_tx->offset, &target_tx->offset);
  6166. __get_user(host_tx->freq, &target_tx->freq);
  6167. __get_user(host_tx->maxerror, &target_tx->maxerror);
  6168. __get_user(host_tx->esterror, &target_tx->esterror);
  6169. __get_user(host_tx->status, &target_tx->status);
  6170. __get_user(host_tx->constant, &target_tx->constant);
  6171. __get_user(host_tx->precision, &target_tx->precision);
  6172. __get_user(host_tx->tolerance, &target_tx->tolerance);
  6173. __get_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
  6174. __get_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
  6175. __get_user(host_tx->tick, &target_tx->tick);
  6176. __get_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  6177. __get_user(host_tx->jitter, &target_tx->jitter);
  6178. __get_user(host_tx->shift, &target_tx->shift);
  6179. __get_user(host_tx->stabil, &target_tx->stabil);
  6180. __get_user(host_tx->jitcnt, &target_tx->jitcnt);
  6181. __get_user(host_tx->calcnt, &target_tx->calcnt);
  6182. __get_user(host_tx->errcnt, &target_tx->errcnt);
  6183. __get_user(host_tx->stbcnt, &target_tx->stbcnt);
  6184. __get_user(host_tx->tai, &target_tx->tai);
  6185. unlock_user_struct(target_tx, target_addr, 0);
  6186. return 0;
  6187. }
  6188. static inline abi_long host_to_target_timex(abi_long target_addr,
  6189. struct timex *host_tx)
  6190. {
  6191. struct target_timex *target_tx;
  6192. if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) {
  6193. return -TARGET_EFAULT;
  6194. }
  6195. __put_user(host_tx->modes, &target_tx->modes);
  6196. __put_user(host_tx->offset, &target_tx->offset);
  6197. __put_user(host_tx->freq, &target_tx->freq);
  6198. __put_user(host_tx->maxerror, &target_tx->maxerror);
  6199. __put_user(host_tx->esterror, &target_tx->esterror);
  6200. __put_user(host_tx->status, &target_tx->status);
  6201. __put_user(host_tx->constant, &target_tx->constant);
  6202. __put_user(host_tx->precision, &target_tx->precision);
  6203. __put_user(host_tx->tolerance, &target_tx->tolerance);
  6204. __put_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
  6205. __put_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
  6206. __put_user(host_tx->tick, &target_tx->tick);
  6207. __put_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  6208. __put_user(host_tx->jitter, &target_tx->jitter);
  6209. __put_user(host_tx->shift, &target_tx->shift);
  6210. __put_user(host_tx->stabil, &target_tx->stabil);
  6211. __put_user(host_tx->jitcnt, &target_tx->jitcnt);
  6212. __put_user(host_tx->calcnt, &target_tx->calcnt);
  6213. __put_user(host_tx->errcnt, &target_tx->errcnt);
  6214. __put_user(host_tx->stbcnt, &target_tx->stbcnt);
  6215. __put_user(host_tx->tai, &target_tx->tai);
  6216. unlock_user_struct(target_tx, target_addr, 1);
  6217. return 0;
  6218. }
  6219. #endif
  6220. static inline abi_long target_to_host_sigevent(struct sigevent *host_sevp,
  6221. abi_ulong target_addr)
  6222. {
  6223. struct target_sigevent *target_sevp;
  6224. if (!lock_user_struct(VERIFY_READ, target_sevp, target_addr, 1)) {
  6225. return -TARGET_EFAULT;
  6226. }
  6227. /* This union is awkward on 64 bit systems because it has a 32 bit
  6228. * integer and a pointer in it; we follow the conversion approach
  6229. * used for handling sigval types in signal.c so the guest should get
  6230. * the correct value back even if we did a 64 bit byteswap and it's
  6231. * using the 32 bit integer.
  6232. */
  6233. host_sevp->sigev_value.sival_ptr =
  6234. (void *)(uintptr_t)tswapal(target_sevp->sigev_value.sival_ptr);
  6235. host_sevp->sigev_signo =
  6236. target_to_host_signal(tswap32(target_sevp->sigev_signo));
  6237. host_sevp->sigev_notify = tswap32(target_sevp->sigev_notify);
  6238. host_sevp->_sigev_un._tid = tswap32(target_sevp->_sigev_un._tid);
  6239. unlock_user_struct(target_sevp, target_addr, 1);
  6240. return 0;
  6241. }
  6242. #if defined(TARGET_NR_mlockall)
  6243. static inline int target_to_host_mlockall_arg(int arg)
  6244. {
  6245. int result = 0;
  6246. if (arg & TARGET_MLOCKALL_MCL_CURRENT) {
  6247. result |= MCL_CURRENT;
  6248. }
  6249. if (arg & TARGET_MLOCKALL_MCL_FUTURE) {
  6250. result |= MCL_FUTURE;
  6251. }
  6252. return result;
  6253. }
  6254. #endif
  6255. #if (defined(TARGET_NR_stat64) || defined(TARGET_NR_lstat64) || \
  6256. defined(TARGET_NR_fstat64) || defined(TARGET_NR_fstatat64) || \
  6257. defined(TARGET_NR_newfstatat))
  6258. static inline abi_long host_to_target_stat64(void *cpu_env,
  6259. abi_ulong target_addr,
  6260. struct stat *host_st)
  6261. {
  6262. #if defined(TARGET_ARM) && defined(TARGET_ABI32)
  6263. if (((CPUARMState *)cpu_env)->eabi) {
  6264. struct target_eabi_stat64 *target_st;
  6265. if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
  6266. return -TARGET_EFAULT;
  6267. memset(target_st, 0, sizeof(struct target_eabi_stat64));
  6268. __put_user(host_st->st_dev, &target_st->st_dev);
  6269. __put_user(host_st->st_ino, &target_st->st_ino);
  6270. #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
  6271. __put_user(host_st->st_ino, &target_st->__st_ino);
  6272. #endif
  6273. __put_user(host_st->st_mode, &target_st->st_mode);
  6274. __put_user(host_st->st_nlink, &target_st->st_nlink);
  6275. __put_user(host_st->st_uid, &target_st->st_uid);
  6276. __put_user(host_st->st_gid, &target_st->st_gid);
  6277. __put_user(host_st->st_rdev, &target_st->st_rdev);
  6278. __put_user(host_st->st_size, &target_st->st_size);
  6279. __put_user(host_st->st_blksize, &target_st->st_blksize);
  6280. __put_user(host_st->st_blocks, &target_st->st_blocks);
  6281. __put_user(host_st->st_atime, &target_st->target_st_atime);
  6282. __put_user(host_st->st_mtime, &target_st->target_st_mtime);
  6283. __put_user(host_st->st_ctime, &target_st->target_st_ctime);
  6284. #if _POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700
  6285. __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
  6286. __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
  6287. __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
  6288. #endif
  6289. unlock_user_struct(target_st, target_addr, 1);
  6290. } else
  6291. #endif
  6292. {
  6293. #if defined(TARGET_HAS_STRUCT_STAT64)
  6294. struct target_stat64 *target_st;
  6295. #else
  6296. struct target_stat *target_st;
  6297. #endif
  6298. if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
  6299. return -TARGET_EFAULT;
  6300. memset(target_st, 0, sizeof(*target_st));
  6301. __put_user(host_st->st_dev, &target_st->st_dev);
  6302. __put_user(host_st->st_ino, &target_st->st_ino);
  6303. #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
  6304. __put_user(host_st->st_ino, &target_st->__st_ino);
  6305. #endif
  6306. __put_user(host_st->st_mode, &target_st->st_mode);
  6307. __put_user(host_st->st_nlink, &target_st->st_nlink);
  6308. __put_user(host_st->st_uid, &target_st->st_uid);
  6309. __put_user(host_st->st_gid, &target_st->st_gid);
  6310. __put_user(host_st->st_rdev, &target_st->st_rdev);
  6311. /* XXX: better use of kernel struct */
  6312. __put_user(host_st->st_size, &target_st->st_size);
  6313. __put_user(host_st->st_blksize, &target_st->st_blksize);
  6314. __put_user(host_st->st_blocks, &target_st->st_blocks);
  6315. __put_user(host_st->st_atime, &target_st->target_st_atime);
  6316. __put_user(host_st->st_mtime, &target_st->target_st_mtime);
  6317. __put_user(host_st->st_ctime, &target_st->target_st_ctime);
  6318. #if _POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700
  6319. __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
  6320. __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
  6321. __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
  6322. #endif
  6323. unlock_user_struct(target_st, target_addr, 1);
  6324. }
  6325. return 0;
  6326. }
  6327. #endif
  6328. #if defined(TARGET_NR_statx) && defined(__NR_statx)
  6329. static inline abi_long host_to_target_statx(struct target_statx *host_stx,
  6330. abi_ulong target_addr)
  6331. {
  6332. struct target_statx *target_stx;
  6333. if (!lock_user_struct(VERIFY_WRITE, target_stx, target_addr, 0)) {
  6334. return -TARGET_EFAULT;
  6335. }
  6336. memset(target_stx, 0, sizeof(*target_stx));
  6337. __put_user(host_stx->stx_mask, &target_stx->stx_mask);
  6338. __put_user(host_stx->stx_blksize, &target_stx->stx_blksize);
  6339. __put_user(host_stx->stx_attributes, &target_stx->stx_attributes);
  6340. __put_user(host_stx->stx_nlink, &target_stx->stx_nlink);
  6341. __put_user(host_stx->stx_uid, &target_stx->stx_uid);
  6342. __put_user(host_stx->stx_gid, &target_stx->stx_gid);
  6343. __put_user(host_stx->stx_mode, &target_stx->stx_mode);
  6344. __put_user(host_stx->stx_ino, &target_stx->stx_ino);
  6345. __put_user(host_stx->stx_size, &target_stx->stx_size);
  6346. __put_user(host_stx->stx_blocks, &target_stx->stx_blocks);
  6347. __put_user(host_stx->stx_attributes_mask, &target_stx->stx_attributes_mask);
  6348. __put_user(host_stx->stx_atime.tv_sec, &target_stx->stx_atime.tv_sec);
  6349. __put_user(host_stx->stx_atime.tv_nsec, &target_stx->stx_atime.tv_nsec);
  6350. __put_user(host_stx->stx_btime.tv_sec, &target_stx->stx_btime.tv_sec);
  6351. __put_user(host_stx->stx_btime.tv_nsec, &target_stx->stx_btime.tv_nsec);
  6352. __put_user(host_stx->stx_ctime.tv_sec, &target_stx->stx_ctime.tv_sec);
  6353. __put_user(host_stx->stx_ctime.tv_nsec, &target_stx->stx_ctime.tv_nsec);
  6354. __put_user(host_stx->stx_mtime.tv_sec, &target_stx->stx_mtime.tv_sec);
  6355. __put_user(host_stx->stx_mtime.tv_nsec, &target_stx->stx_mtime.tv_nsec);
  6356. __put_user(host_stx->stx_rdev_major, &target_stx->stx_rdev_major);
  6357. __put_user(host_stx->stx_rdev_minor, &target_stx->stx_rdev_minor);
  6358. __put_user(host_stx->stx_dev_major, &target_stx->stx_dev_major);
  6359. __put_user(host_stx->stx_dev_minor, &target_stx->stx_dev_minor);
  6360. unlock_user_struct(target_stx, target_addr, 1);
  6361. return 0;
  6362. }
  6363. #endif
  6364. static int do_sys_futex(int *uaddr, int op, int val,
  6365. const struct timespec *timeout, int *uaddr2,
  6366. int val3)
  6367. {
  6368. #if HOST_LONG_BITS == 64
  6369. #if defined(__NR_futex)
  6370. /* always a 64-bit time_t, it doesn't define _time64 version */
  6371. return sys_futex(uaddr, op, val, timeout, uaddr2, val3);
  6372. #endif
  6373. #else /* HOST_LONG_BITS == 64 */
  6374. #if defined(__NR_futex_time64)
  6375. if (sizeof(timeout->tv_sec) == 8) {
  6376. /* _time64 function on 32bit arch */
  6377. return sys_futex_time64(uaddr, op, val, timeout, uaddr2, val3);
  6378. }
  6379. #endif
  6380. #if defined(__NR_futex)
  6381. /* old function on 32bit arch */
  6382. return sys_futex(uaddr, op, val, timeout, uaddr2, val3);
  6383. #endif
  6384. #endif /* HOST_LONG_BITS == 64 */
  6385. g_assert_not_reached();
  6386. }
  6387. static int do_safe_futex(int *uaddr, int op, int val,
  6388. const struct timespec *timeout, int *uaddr2,
  6389. int val3)
  6390. {
  6391. #if HOST_LONG_BITS == 64
  6392. #if defined(__NR_futex)
  6393. /* always a 64-bit time_t, it doesn't define _time64 version */
  6394. return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3));
  6395. #endif
  6396. #else /* HOST_LONG_BITS == 64 */
  6397. #if defined(__NR_futex_time64)
  6398. if (sizeof(timeout->tv_sec) == 8) {
  6399. /* _time64 function on 32bit arch */
  6400. return get_errno(safe_futex_time64(uaddr, op, val, timeout, uaddr2,
  6401. val3));
  6402. }
  6403. #endif
  6404. #if defined(__NR_futex)
  6405. /* old function on 32bit arch */
  6406. return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3));
  6407. #endif
  6408. #endif /* HOST_LONG_BITS == 64 */
  6409. return -TARGET_ENOSYS;
  6410. }
  6411. /* ??? Using host futex calls even when target atomic operations
  6412. are not really atomic probably breaks things. However implementing
  6413. futexes locally would make futexes shared between multiple processes
  6414. tricky. However they're probably useless because guest atomic
  6415. operations won't work either. */
  6416. #if defined(TARGET_NR_futex)
  6417. static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
  6418. target_ulong uaddr2, int val3)
  6419. {
  6420. struct timespec ts, *pts;
  6421. int base_op;
  6422. /* ??? We assume FUTEX_* constants are the same on both host
  6423. and target. */
  6424. #ifdef FUTEX_CMD_MASK
  6425. base_op = op & FUTEX_CMD_MASK;
  6426. #else
  6427. base_op = op;
  6428. #endif
  6429. switch (base_op) {
  6430. case FUTEX_WAIT:
  6431. case FUTEX_WAIT_BITSET:
  6432. if (timeout) {
  6433. pts = &ts;
  6434. target_to_host_timespec(pts, timeout);
  6435. } else {
  6436. pts = NULL;
  6437. }
  6438. return do_safe_futex(g2h(uaddr), op, tswap32(val), pts, NULL, val3);
  6439. case FUTEX_WAKE:
  6440. return do_safe_futex(g2h(uaddr), op, val, NULL, NULL, 0);
  6441. case FUTEX_FD:
  6442. return do_safe_futex(g2h(uaddr), op, val, NULL, NULL, 0);
  6443. case FUTEX_REQUEUE:
  6444. case FUTEX_CMP_REQUEUE:
  6445. case FUTEX_WAKE_OP:
  6446. /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
  6447. TIMEOUT parameter is interpreted as a uint32_t by the kernel.
  6448. But the prototype takes a `struct timespec *'; insert casts
  6449. to satisfy the compiler. We do not need to tswap TIMEOUT
  6450. since it's not compared to guest memory. */
  6451. pts = (struct timespec *)(uintptr_t) timeout;
  6452. return do_safe_futex(g2h(uaddr), op, val, pts, g2h(uaddr2),
  6453. (base_op == FUTEX_CMP_REQUEUE
  6454. ? tswap32(val3)
  6455. : val3));
  6456. default:
  6457. return -TARGET_ENOSYS;
  6458. }
  6459. }
  6460. #endif
  6461. #if defined(TARGET_NR_futex_time64)
  6462. static int do_futex_time64(target_ulong uaddr, int op, int val, target_ulong timeout,
  6463. target_ulong uaddr2, int val3)
  6464. {
  6465. struct timespec ts, *pts;
  6466. int base_op;
  6467. /* ??? We assume FUTEX_* constants are the same on both host
  6468. and target. */
  6469. #ifdef FUTEX_CMD_MASK
  6470. base_op = op & FUTEX_CMD_MASK;
  6471. #else
  6472. base_op = op;
  6473. #endif
  6474. switch (base_op) {
  6475. case FUTEX_WAIT:
  6476. case FUTEX_WAIT_BITSET:
  6477. if (timeout) {
  6478. pts = &ts;
  6479. target_to_host_timespec64(pts, timeout);
  6480. } else {
  6481. pts = NULL;
  6482. }
  6483. return do_safe_futex(g2h(uaddr), op, tswap32(val), pts, NULL, val3);
  6484. case FUTEX_WAKE:
  6485. return do_safe_futex(g2h(uaddr), op, val, NULL, NULL, 0);
  6486. case FUTEX_FD:
  6487. return do_safe_futex(g2h(uaddr), op, val, NULL, NULL, 0);
  6488. case FUTEX_REQUEUE:
  6489. case FUTEX_CMP_REQUEUE:
  6490. case FUTEX_WAKE_OP:
  6491. /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
  6492. TIMEOUT parameter is interpreted as a uint32_t by the kernel.
  6493. But the prototype takes a `struct timespec *'; insert casts
  6494. to satisfy the compiler. We do not need to tswap TIMEOUT
  6495. since it's not compared to guest memory. */
  6496. pts = (struct timespec *)(uintptr_t) timeout;
  6497. return do_safe_futex(g2h(uaddr), op, val, pts, g2h(uaddr2),
  6498. (base_op == FUTEX_CMP_REQUEUE
  6499. ? tswap32(val3)
  6500. : val3));
  6501. default:
  6502. return -TARGET_ENOSYS;
  6503. }
  6504. }
  6505. #endif
  6506. #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  6507. static abi_long do_name_to_handle_at(abi_long dirfd, abi_long pathname,
  6508. abi_long handle, abi_long mount_id,
  6509. abi_long flags)
  6510. {
  6511. struct file_handle *target_fh;
  6512. struct file_handle *fh;
  6513. int mid = 0;
  6514. abi_long ret;
  6515. char *name;
  6516. unsigned int size, total_size;
  6517. if (get_user_s32(size, handle)) {
  6518. return -TARGET_EFAULT;
  6519. }
  6520. name = lock_user_string(pathname);
  6521. if (!name) {
  6522. return -TARGET_EFAULT;
  6523. }
  6524. total_size = sizeof(struct file_handle) + size;
  6525. target_fh = lock_user(VERIFY_WRITE, handle, total_size, 0);
  6526. if (!target_fh) {
  6527. unlock_user(name, pathname, 0);
  6528. return -TARGET_EFAULT;
  6529. }
  6530. fh = g_malloc0(total_size);
  6531. fh->handle_bytes = size;
  6532. ret = get_errno(name_to_handle_at(dirfd, path(name), fh, &mid, flags));
  6533. unlock_user(name, pathname, 0);
  6534. /* man name_to_handle_at(2):
  6535. * Other than the use of the handle_bytes field, the caller should treat
  6536. * the file_handle structure as an opaque data type
  6537. */
  6538. memcpy(target_fh, fh, total_size);
  6539. target_fh->handle_bytes = tswap32(fh->handle_bytes);
  6540. target_fh->handle_type = tswap32(fh->handle_type);
  6541. g_free(fh);
  6542. unlock_user(target_fh, handle, total_size);
  6543. if (put_user_s32(mid, mount_id)) {
  6544. return -TARGET_EFAULT;
  6545. }
  6546. return ret;
  6547. }
  6548. #endif
  6549. #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  6550. static abi_long do_open_by_handle_at(abi_long mount_fd, abi_long handle,
  6551. abi_long flags)
  6552. {
  6553. struct file_handle *target_fh;
  6554. struct file_handle *fh;
  6555. unsigned int size, total_size;
  6556. abi_long ret;
  6557. if (get_user_s32(size, handle)) {
  6558. return -TARGET_EFAULT;
  6559. }
  6560. total_size = sizeof(struct file_handle) + size;
  6561. target_fh = lock_user(VERIFY_READ, handle, total_size, 1);
  6562. if (!target_fh) {
  6563. return -TARGET_EFAULT;
  6564. }
  6565. fh = g_memdup(target_fh, total_size);
  6566. fh->handle_bytes = size;
  6567. fh->handle_type = tswap32(target_fh->handle_type);
  6568. ret = get_errno(open_by_handle_at(mount_fd, fh,
  6569. target_to_host_bitmask(flags, fcntl_flags_tbl)));
  6570. g_free(fh);
  6571. unlock_user(target_fh, handle, total_size);
  6572. return ret;
  6573. }
  6574. #endif
  6575. #if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4)
  6576. static abi_long do_signalfd4(int fd, abi_long mask, int flags)
  6577. {
  6578. int host_flags;
  6579. target_sigset_t *target_mask;
  6580. sigset_t host_mask;
  6581. abi_long ret;
  6582. if (flags & ~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC)) {
  6583. return -TARGET_EINVAL;
  6584. }
  6585. if (!lock_user_struct(VERIFY_READ, target_mask, mask, 1)) {
  6586. return -TARGET_EFAULT;
  6587. }
  6588. target_to_host_sigset(&host_mask, target_mask);
  6589. host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
  6590. ret = get_errno(signalfd(fd, &host_mask, host_flags));
  6591. if (ret >= 0) {
  6592. fd_trans_register(ret, &target_signalfd_trans);
  6593. }
  6594. unlock_user_struct(target_mask, mask, 0);
  6595. return ret;
  6596. }
  6597. #endif
  6598. /* Map host to target signal numbers for the wait family of syscalls.
  6599. Assume all other status bits are the same. */
  6600. int host_to_target_waitstatus(int status)
  6601. {
  6602. if (WIFSIGNALED(status)) {
  6603. return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
  6604. }
  6605. if (WIFSTOPPED(status)) {
  6606. return (host_to_target_signal(WSTOPSIG(status)) << 8)
  6607. | (status & 0xff);
  6608. }
  6609. return status;
  6610. }
  6611. static int open_self_cmdline(void *cpu_env, int fd)
  6612. {
  6613. CPUState *cpu = env_cpu((CPUArchState *)cpu_env);
  6614. struct linux_binprm *bprm = ((TaskState *)cpu->opaque)->bprm;
  6615. int i;
  6616. for (i = 0; i < bprm->argc; i++) {
  6617. size_t len = strlen(bprm->argv[i]) + 1;
  6618. if (write(fd, bprm->argv[i], len) != len) {
  6619. return -1;
  6620. }
  6621. }
  6622. return 0;
  6623. }
  6624. static int open_self_maps(void *cpu_env, int fd)
  6625. {
  6626. CPUState *cpu = env_cpu((CPUArchState *)cpu_env);
  6627. TaskState *ts = cpu->opaque;
  6628. GSList *map_info = read_self_maps();
  6629. GSList *s;
  6630. int count;
  6631. for (s = map_info; s; s = g_slist_next(s)) {
  6632. MapInfo *e = (MapInfo *) s->data;
  6633. if (h2g_valid(e->start)) {
  6634. unsigned long min = e->start;
  6635. unsigned long max = e->end;
  6636. int flags = page_get_flags(h2g(min));
  6637. const char *path;
  6638. max = h2g_valid(max - 1) ?
  6639. max : (uintptr_t) g2h(GUEST_ADDR_MAX) + 1;
  6640. if (page_check_range(h2g(min), max - min, flags) == -1) {
  6641. continue;
  6642. }
  6643. if (h2g(min) == ts->info->stack_limit) {
  6644. path = "[stack]";
  6645. } else {
  6646. path = e->path;
  6647. }
  6648. count = dprintf(fd, TARGET_ABI_FMT_ptr "-" TARGET_ABI_FMT_ptr
  6649. " %c%c%c%c %08" PRIx64 " %s %"PRId64,
  6650. h2g(min), h2g(max - 1) + 1,
  6651. e->is_read ? 'r' : '-',
  6652. e->is_write ? 'w' : '-',
  6653. e->is_exec ? 'x' : '-',
  6654. e->is_priv ? 'p' : '-',
  6655. (uint64_t) e->offset, e->dev, e->inode);
  6656. if (path) {
  6657. dprintf(fd, "%*s%s\n", 73 - count, "", path);
  6658. } else {
  6659. dprintf(fd, "\n");
  6660. }
  6661. }
  6662. }
  6663. free_self_maps(map_info);
  6664. #ifdef TARGET_VSYSCALL_PAGE
  6665. /*
  6666. * We only support execution from the vsyscall page.
  6667. * This is as if CONFIG_LEGACY_VSYSCALL_XONLY=y from v5.3.
  6668. */
  6669. count = dprintf(fd, TARGET_FMT_lx "-" TARGET_FMT_lx
  6670. " --xp 00000000 00:00 0",
  6671. TARGET_VSYSCALL_PAGE, TARGET_VSYSCALL_PAGE + TARGET_PAGE_SIZE);
  6672. dprintf(fd, "%*s%s\n", 73 - count, "", "[vsyscall]");
  6673. #endif
  6674. return 0;
  6675. }
  6676. static int open_self_stat(void *cpu_env, int fd)
  6677. {
  6678. CPUState *cpu = env_cpu((CPUArchState *)cpu_env);
  6679. TaskState *ts = cpu->opaque;
  6680. g_autoptr(GString) buf = g_string_new(NULL);
  6681. int i;
  6682. for (i = 0; i < 44; i++) {
  6683. if (i == 0) {
  6684. /* pid */
  6685. g_string_printf(buf, FMT_pid " ", getpid());
  6686. } else if (i == 1) {
  6687. /* app name */
  6688. gchar *bin = g_strrstr(ts->bprm->argv[0], "/");
  6689. bin = bin ? bin + 1 : ts->bprm->argv[0];
  6690. g_string_printf(buf, "(%.15s) ", bin);
  6691. } else if (i == 27) {
  6692. /* stack bottom */
  6693. g_string_printf(buf, TARGET_ABI_FMT_ld " ", ts->info->start_stack);
  6694. } else {
  6695. /* for the rest, there is MasterCard */
  6696. g_string_printf(buf, "0%c", i == 43 ? '\n' : ' ');
  6697. }
  6698. if (write(fd, buf->str, buf->len) != buf->len) {
  6699. return -1;
  6700. }
  6701. }
  6702. return 0;
  6703. }
  6704. static int open_self_auxv(void *cpu_env, int fd)
  6705. {
  6706. CPUState *cpu = env_cpu((CPUArchState *)cpu_env);
  6707. TaskState *ts = cpu->opaque;
  6708. abi_ulong auxv = ts->info->saved_auxv;
  6709. abi_ulong len = ts->info->auxv_len;
  6710. char *ptr;
  6711. /*
  6712. * Auxiliary vector is stored in target process stack.
  6713. * read in whole auxv vector and copy it to file
  6714. */
  6715. ptr = lock_user(VERIFY_READ, auxv, len, 0);
  6716. if (ptr != NULL) {
  6717. while (len > 0) {
  6718. ssize_t r;
  6719. r = write(fd, ptr, len);
  6720. if (r <= 0) {
  6721. break;
  6722. }
  6723. len -= r;
  6724. ptr += r;
  6725. }
  6726. lseek(fd, 0, SEEK_SET);
  6727. unlock_user(ptr, auxv, len);
  6728. }
  6729. return 0;
  6730. }
  6731. static int is_proc_myself(const char *filename, const char *entry)
  6732. {
  6733. if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
  6734. filename += strlen("/proc/");
  6735. if (!strncmp(filename, "self/", strlen("self/"))) {
  6736. filename += strlen("self/");
  6737. } else if (*filename >= '1' && *filename <= '9') {
  6738. char myself[80];
  6739. snprintf(myself, sizeof(myself), "%d/", getpid());
  6740. if (!strncmp(filename, myself, strlen(myself))) {
  6741. filename += strlen(myself);
  6742. } else {
  6743. return 0;
  6744. }
  6745. } else {
  6746. return 0;
  6747. }
  6748. if (!strcmp(filename, entry)) {
  6749. return 1;
  6750. }
  6751. }
  6752. return 0;
  6753. }
  6754. #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN) || \
  6755. defined(TARGET_SPARC) || defined(TARGET_M68K) || defined(TARGET_HPPA)
  6756. static int is_proc(const char *filename, const char *entry)
  6757. {
  6758. return strcmp(filename, entry) == 0;
  6759. }
  6760. #endif
  6761. #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
  6762. static int open_net_route(void *cpu_env, int fd)
  6763. {
  6764. FILE *fp;
  6765. char *line = NULL;
  6766. size_t len = 0;
  6767. ssize_t read;
  6768. fp = fopen("/proc/net/route", "r");
  6769. if (fp == NULL) {
  6770. return -1;
  6771. }
  6772. /* read header */
  6773. read = getline(&line, &len, fp);
  6774. dprintf(fd, "%s", line);
  6775. /* read routes */
  6776. while ((read = getline(&line, &len, fp)) != -1) {
  6777. char iface[16];
  6778. uint32_t dest, gw, mask;
  6779. unsigned int flags, refcnt, use, metric, mtu, window, irtt;
  6780. int fields;
  6781. fields = sscanf(line,
  6782. "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
  6783. iface, &dest, &gw, &flags, &refcnt, &use, &metric,
  6784. &mask, &mtu, &window, &irtt);
  6785. if (fields != 11) {
  6786. continue;
  6787. }
  6788. dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
  6789. iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
  6790. metric, tswap32(mask), mtu, window, irtt);
  6791. }
  6792. free(line);
  6793. fclose(fp);
  6794. return 0;
  6795. }
  6796. #endif
  6797. #if defined(TARGET_SPARC)
  6798. static int open_cpuinfo(void *cpu_env, int fd)
  6799. {
  6800. dprintf(fd, "type\t\t: sun4u\n");
  6801. return 0;
  6802. }
  6803. #endif
  6804. #if defined(TARGET_HPPA)
  6805. static int open_cpuinfo(void *cpu_env, int fd)
  6806. {
  6807. dprintf(fd, "cpu family\t: PA-RISC 1.1e\n");
  6808. dprintf(fd, "cpu\t\t: PA7300LC (PCX-L2)\n");
  6809. dprintf(fd, "capabilities\t: os32\n");
  6810. dprintf(fd, "model\t\t: 9000/778/B160L\n");
  6811. dprintf(fd, "model name\t: Merlin L2 160 QEMU (9000/778/B160L)\n");
  6812. return 0;
  6813. }
  6814. #endif
  6815. #if defined(TARGET_M68K)
  6816. static int open_hardware(void *cpu_env, int fd)
  6817. {
  6818. dprintf(fd, "Model:\t\tqemu-m68k\n");
  6819. return 0;
  6820. }
  6821. #endif
  6822. static int do_openat(void *cpu_env, int dirfd, const char *pathname, int flags, mode_t mode)
  6823. {
  6824. struct fake_open {
  6825. const char *filename;
  6826. int (*fill)(void *cpu_env, int fd);
  6827. int (*cmp)(const char *s1, const char *s2);
  6828. };
  6829. const struct fake_open *fake_open;
  6830. static const struct fake_open fakes[] = {
  6831. { "maps", open_self_maps, is_proc_myself },
  6832. { "stat", open_self_stat, is_proc_myself },
  6833. { "auxv", open_self_auxv, is_proc_myself },
  6834. { "cmdline", open_self_cmdline, is_proc_myself },
  6835. #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
  6836. { "/proc/net/route", open_net_route, is_proc },
  6837. #endif
  6838. #if defined(TARGET_SPARC) || defined(TARGET_HPPA)
  6839. { "/proc/cpuinfo", open_cpuinfo, is_proc },
  6840. #endif
  6841. #if defined(TARGET_M68K)
  6842. { "/proc/hardware", open_hardware, is_proc },
  6843. #endif
  6844. { NULL, NULL, NULL }
  6845. };
  6846. if (is_proc_myself(pathname, "exe")) {
  6847. int execfd = qemu_getauxval(AT_EXECFD);
  6848. return execfd ? execfd : safe_openat(dirfd, exec_path, flags, mode);
  6849. }
  6850. for (fake_open = fakes; fake_open->filename; fake_open++) {
  6851. if (fake_open->cmp(pathname, fake_open->filename)) {
  6852. break;
  6853. }
  6854. }
  6855. if (fake_open->filename) {
  6856. const char *tmpdir;
  6857. char filename[PATH_MAX];
  6858. int fd, r;
  6859. /* create temporary file to map stat to */
  6860. tmpdir = getenv("TMPDIR");
  6861. if (!tmpdir)
  6862. tmpdir = "/tmp";
  6863. snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
  6864. fd = mkstemp(filename);
  6865. if (fd < 0) {
  6866. return fd;
  6867. }
  6868. unlink(filename);
  6869. if ((r = fake_open->fill(cpu_env, fd))) {
  6870. int e = errno;
  6871. close(fd);
  6872. errno = e;
  6873. return r;
  6874. }
  6875. lseek(fd, 0, SEEK_SET);
  6876. return fd;
  6877. }
  6878. return safe_openat(dirfd, path(pathname), flags, mode);
  6879. }
  6880. #define TIMER_MAGIC 0x0caf0000
  6881. #define TIMER_MAGIC_MASK 0xffff0000
  6882. /* Convert QEMU provided timer ID back to internal 16bit index format */
  6883. static target_timer_t get_timer_id(abi_long arg)
  6884. {
  6885. target_timer_t timerid = arg;
  6886. if ((timerid & TIMER_MAGIC_MASK) != TIMER_MAGIC) {
  6887. return -TARGET_EINVAL;
  6888. }
  6889. timerid &= 0xffff;
  6890. if (timerid >= ARRAY_SIZE(g_posix_timers)) {
  6891. return -TARGET_EINVAL;
  6892. }
  6893. return timerid;
  6894. }
  6895. static int target_to_host_cpu_mask(unsigned long *host_mask,
  6896. size_t host_size,
  6897. abi_ulong target_addr,
  6898. size_t target_size)
  6899. {
  6900. unsigned target_bits = sizeof(abi_ulong) * 8;
  6901. unsigned host_bits = sizeof(*host_mask) * 8;
  6902. abi_ulong *target_mask;
  6903. unsigned i, j;
  6904. assert(host_size >= target_size);
  6905. target_mask = lock_user(VERIFY_READ, target_addr, target_size, 1);
  6906. if (!target_mask) {
  6907. return -TARGET_EFAULT;
  6908. }
  6909. memset(host_mask, 0, host_size);
  6910. for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
  6911. unsigned bit = i * target_bits;
  6912. abi_ulong val;
  6913. __get_user(val, &target_mask[i]);
  6914. for (j = 0; j < target_bits; j++, bit++) {
  6915. if (val & (1UL << j)) {
  6916. host_mask[bit / host_bits] |= 1UL << (bit % host_bits);
  6917. }
  6918. }
  6919. }
  6920. unlock_user(target_mask, target_addr, 0);
  6921. return 0;
  6922. }
  6923. static int host_to_target_cpu_mask(const unsigned long *host_mask,
  6924. size_t host_size,
  6925. abi_ulong target_addr,
  6926. size_t target_size)
  6927. {
  6928. unsigned target_bits = sizeof(abi_ulong) * 8;
  6929. unsigned host_bits = sizeof(*host_mask) * 8;
  6930. abi_ulong *target_mask;
  6931. unsigned i, j;
  6932. assert(host_size >= target_size);
  6933. target_mask = lock_user(VERIFY_WRITE, target_addr, target_size, 0);
  6934. if (!target_mask) {
  6935. return -TARGET_EFAULT;
  6936. }
  6937. for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
  6938. unsigned bit = i * target_bits;
  6939. abi_ulong val = 0;
  6940. for (j = 0; j < target_bits; j++, bit++) {
  6941. if (host_mask[bit / host_bits] & (1UL << (bit % host_bits))) {
  6942. val |= 1UL << j;
  6943. }
  6944. }
  6945. __put_user(val, &target_mask[i]);
  6946. }
  6947. unlock_user(target_mask, target_addr, target_size);
  6948. return 0;
  6949. }
  6950. /* This is an internal helper for do_syscall so that it is easier
  6951. * to have a single return point, so that actions, such as logging
  6952. * of syscall results, can be performed.
  6953. * All errnos that do_syscall() returns must be -TARGET_<errcode>.
  6954. */
  6955. static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
  6956. abi_long arg2, abi_long arg3, abi_long arg4,
  6957. abi_long arg5, abi_long arg6, abi_long arg7,
  6958. abi_long arg8)
  6959. {
  6960. CPUState *cpu = env_cpu(cpu_env);
  6961. abi_long ret;
  6962. #if defined(TARGET_NR_stat) || defined(TARGET_NR_stat64) \
  6963. || defined(TARGET_NR_lstat) || defined(TARGET_NR_lstat64) \
  6964. || defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64) \
  6965. || defined(TARGET_NR_statx)
  6966. struct stat st;
  6967. #endif
  6968. #if defined(TARGET_NR_statfs) || defined(TARGET_NR_statfs64) \
  6969. || defined(TARGET_NR_fstatfs)
  6970. struct statfs stfs;
  6971. #endif
  6972. void *p;
  6973. switch(num) {
  6974. case TARGET_NR_exit:
  6975. /* In old applications this may be used to implement _exit(2).
  6976. However in threaded applictions it is used for thread termination,
  6977. and _exit_group is used for application termination.
  6978. Do thread termination if we have more then one thread. */
  6979. if (block_signals()) {
  6980. return -TARGET_ERESTARTSYS;
  6981. }
  6982. pthread_mutex_lock(&clone_lock);
  6983. if (CPU_NEXT(first_cpu)) {
  6984. TaskState *ts = cpu->opaque;
  6985. object_property_set_bool(OBJECT(cpu), "realized", false, NULL);
  6986. object_unref(OBJECT(cpu));
  6987. /*
  6988. * At this point the CPU should be unrealized and removed
  6989. * from cpu lists. We can clean-up the rest of the thread
  6990. * data without the lock held.
  6991. */
  6992. pthread_mutex_unlock(&clone_lock);
  6993. if (ts->child_tidptr) {
  6994. put_user_u32(0, ts->child_tidptr);
  6995. do_sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
  6996. NULL, NULL, 0);
  6997. }
  6998. thread_cpu = NULL;
  6999. g_free(ts);
  7000. rcu_unregister_thread();
  7001. pthread_exit(NULL);
  7002. }
  7003. pthread_mutex_unlock(&clone_lock);
  7004. preexit_cleanup(cpu_env, arg1);
  7005. _exit(arg1);
  7006. return 0; /* avoid warning */
  7007. case TARGET_NR_read:
  7008. if (arg2 == 0 && arg3 == 0) {
  7009. return get_errno(safe_read(arg1, 0, 0));
  7010. } else {
  7011. if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
  7012. return -TARGET_EFAULT;
  7013. ret = get_errno(safe_read(arg1, p, arg3));
  7014. if (ret >= 0 &&
  7015. fd_trans_host_to_target_data(arg1)) {
  7016. ret = fd_trans_host_to_target_data(arg1)(p, ret);
  7017. }
  7018. unlock_user(p, arg2, ret);
  7019. }
  7020. return ret;
  7021. case TARGET_NR_write:
  7022. if (arg2 == 0 && arg3 == 0) {
  7023. return get_errno(safe_write(arg1, 0, 0));
  7024. }
  7025. if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
  7026. return -TARGET_EFAULT;
  7027. if (fd_trans_target_to_host_data(arg1)) {
  7028. void *copy = g_malloc(arg3);
  7029. memcpy(copy, p, arg3);
  7030. ret = fd_trans_target_to_host_data(arg1)(copy, arg3);
  7031. if (ret >= 0) {
  7032. ret = get_errno(safe_write(arg1, copy, ret));
  7033. }
  7034. g_free(copy);
  7035. } else {
  7036. ret = get_errno(safe_write(arg1, p, arg3));
  7037. }
  7038. unlock_user(p, arg2, 0);
  7039. return ret;
  7040. #ifdef TARGET_NR_open
  7041. case TARGET_NR_open:
  7042. if (!(p = lock_user_string(arg1)))
  7043. return -TARGET_EFAULT;
  7044. ret = get_errno(do_openat(cpu_env, AT_FDCWD, p,
  7045. target_to_host_bitmask(arg2, fcntl_flags_tbl),
  7046. arg3));
  7047. fd_trans_unregister(ret);
  7048. unlock_user(p, arg1, 0);
  7049. return ret;
  7050. #endif
  7051. case TARGET_NR_openat:
  7052. if (!(p = lock_user_string(arg2)))
  7053. return -TARGET_EFAULT;
  7054. ret = get_errno(do_openat(cpu_env, arg1, p,
  7055. target_to_host_bitmask(arg3, fcntl_flags_tbl),
  7056. arg4));
  7057. fd_trans_unregister(ret);
  7058. unlock_user(p, arg2, 0);
  7059. return ret;
  7060. #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  7061. case TARGET_NR_name_to_handle_at:
  7062. ret = do_name_to_handle_at(arg1, arg2, arg3, arg4, arg5);
  7063. return ret;
  7064. #endif
  7065. #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  7066. case TARGET_NR_open_by_handle_at:
  7067. ret = do_open_by_handle_at(arg1, arg2, arg3);
  7068. fd_trans_unregister(ret);
  7069. return ret;
  7070. #endif
  7071. case TARGET_NR_close:
  7072. fd_trans_unregister(arg1);
  7073. return get_errno(close(arg1));
  7074. case TARGET_NR_brk:
  7075. return do_brk(arg1);
  7076. #ifdef TARGET_NR_fork
  7077. case TARGET_NR_fork:
  7078. return get_errno(do_fork(cpu_env, TARGET_SIGCHLD, 0, 0, 0, 0));
  7079. #endif
  7080. #ifdef TARGET_NR_waitpid
  7081. case TARGET_NR_waitpid:
  7082. {
  7083. int status;
  7084. ret = get_errno(safe_wait4(arg1, &status, arg3, 0));
  7085. if (!is_error(ret) && arg2 && ret
  7086. && put_user_s32(host_to_target_waitstatus(status), arg2))
  7087. return -TARGET_EFAULT;
  7088. }
  7089. return ret;
  7090. #endif
  7091. #ifdef TARGET_NR_waitid
  7092. case TARGET_NR_waitid:
  7093. {
  7094. siginfo_t info;
  7095. info.si_pid = 0;
  7096. ret = get_errno(safe_waitid(arg1, arg2, &info, arg4, NULL));
  7097. if (!is_error(ret) && arg3 && info.si_pid != 0) {
  7098. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
  7099. return -TARGET_EFAULT;
  7100. host_to_target_siginfo(p, &info);
  7101. unlock_user(p, arg3, sizeof(target_siginfo_t));
  7102. }
  7103. }
  7104. return ret;
  7105. #endif
  7106. #ifdef TARGET_NR_creat /* not on alpha */
  7107. case TARGET_NR_creat:
  7108. if (!(p = lock_user_string(arg1)))
  7109. return -TARGET_EFAULT;
  7110. ret = get_errno(creat(p, arg2));
  7111. fd_trans_unregister(ret);
  7112. unlock_user(p, arg1, 0);
  7113. return ret;
  7114. #endif
  7115. #ifdef TARGET_NR_link
  7116. case TARGET_NR_link:
  7117. {
  7118. void * p2;
  7119. p = lock_user_string(arg1);
  7120. p2 = lock_user_string(arg2);
  7121. if (!p || !p2)
  7122. ret = -TARGET_EFAULT;
  7123. else
  7124. ret = get_errno(link(p, p2));
  7125. unlock_user(p2, arg2, 0);
  7126. unlock_user(p, arg1, 0);
  7127. }
  7128. return ret;
  7129. #endif
  7130. #if defined(TARGET_NR_linkat)
  7131. case TARGET_NR_linkat:
  7132. {
  7133. void * p2 = NULL;
  7134. if (!arg2 || !arg4)
  7135. return -TARGET_EFAULT;
  7136. p = lock_user_string(arg2);
  7137. p2 = lock_user_string(arg4);
  7138. if (!p || !p2)
  7139. ret = -TARGET_EFAULT;
  7140. else
  7141. ret = get_errno(linkat(arg1, p, arg3, p2, arg5));
  7142. unlock_user(p, arg2, 0);
  7143. unlock_user(p2, arg4, 0);
  7144. }
  7145. return ret;
  7146. #endif
  7147. #ifdef TARGET_NR_unlink
  7148. case TARGET_NR_unlink:
  7149. if (!(p = lock_user_string(arg1)))
  7150. return -TARGET_EFAULT;
  7151. ret = get_errno(unlink(p));
  7152. unlock_user(p, arg1, 0);
  7153. return ret;
  7154. #endif
  7155. #if defined(TARGET_NR_unlinkat)
  7156. case TARGET_NR_unlinkat:
  7157. if (!(p = lock_user_string(arg2)))
  7158. return -TARGET_EFAULT;
  7159. ret = get_errno(unlinkat(arg1, p, arg3));
  7160. unlock_user(p, arg2, 0);
  7161. return ret;
  7162. #endif
  7163. case TARGET_NR_execve:
  7164. {
  7165. char **argp, **envp;
  7166. int argc, envc;
  7167. abi_ulong gp;
  7168. abi_ulong guest_argp;
  7169. abi_ulong guest_envp;
  7170. abi_ulong addr;
  7171. char **q;
  7172. int total_size = 0;
  7173. argc = 0;
  7174. guest_argp = arg2;
  7175. for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
  7176. if (get_user_ual(addr, gp))
  7177. return -TARGET_EFAULT;
  7178. if (!addr)
  7179. break;
  7180. argc++;
  7181. }
  7182. envc = 0;
  7183. guest_envp = arg3;
  7184. for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
  7185. if (get_user_ual(addr, gp))
  7186. return -TARGET_EFAULT;
  7187. if (!addr)
  7188. break;
  7189. envc++;
  7190. }
  7191. argp = g_new0(char *, argc + 1);
  7192. envp = g_new0(char *, envc + 1);
  7193. for (gp = guest_argp, q = argp; gp;
  7194. gp += sizeof(abi_ulong), q++) {
  7195. if (get_user_ual(addr, gp))
  7196. goto execve_efault;
  7197. if (!addr)
  7198. break;
  7199. if (!(*q = lock_user_string(addr)))
  7200. goto execve_efault;
  7201. total_size += strlen(*q) + 1;
  7202. }
  7203. *q = NULL;
  7204. for (gp = guest_envp, q = envp; gp;
  7205. gp += sizeof(abi_ulong), q++) {
  7206. if (get_user_ual(addr, gp))
  7207. goto execve_efault;
  7208. if (!addr)
  7209. break;
  7210. if (!(*q = lock_user_string(addr)))
  7211. goto execve_efault;
  7212. total_size += strlen(*q) + 1;
  7213. }
  7214. *q = NULL;
  7215. if (!(p = lock_user_string(arg1)))
  7216. goto execve_efault;
  7217. /* Although execve() is not an interruptible syscall it is
  7218. * a special case where we must use the safe_syscall wrapper:
  7219. * if we allow a signal to happen before we make the host
  7220. * syscall then we will 'lose' it, because at the point of
  7221. * execve the process leaves QEMU's control. So we use the
  7222. * safe syscall wrapper to ensure that we either take the
  7223. * signal as a guest signal, or else it does not happen
  7224. * before the execve completes and makes it the other
  7225. * program's problem.
  7226. */
  7227. ret = get_errno(safe_execve(p, argp, envp));
  7228. unlock_user(p, arg1, 0);
  7229. goto execve_end;
  7230. execve_efault:
  7231. ret = -TARGET_EFAULT;
  7232. execve_end:
  7233. for (gp = guest_argp, q = argp; *q;
  7234. gp += sizeof(abi_ulong), q++) {
  7235. if (get_user_ual(addr, gp)
  7236. || !addr)
  7237. break;
  7238. unlock_user(*q, addr, 0);
  7239. }
  7240. for (gp = guest_envp, q = envp; *q;
  7241. gp += sizeof(abi_ulong), q++) {
  7242. if (get_user_ual(addr, gp)
  7243. || !addr)
  7244. break;
  7245. unlock_user(*q, addr, 0);
  7246. }
  7247. g_free(argp);
  7248. g_free(envp);
  7249. }
  7250. return ret;
  7251. case TARGET_NR_chdir:
  7252. if (!(p = lock_user_string(arg1)))
  7253. return -TARGET_EFAULT;
  7254. ret = get_errno(chdir(p));
  7255. unlock_user(p, arg1, 0);
  7256. return ret;
  7257. #ifdef TARGET_NR_time
  7258. case TARGET_NR_time:
  7259. {
  7260. time_t host_time;
  7261. ret = get_errno(time(&host_time));
  7262. if (!is_error(ret)
  7263. && arg1
  7264. && put_user_sal(host_time, arg1))
  7265. return -TARGET_EFAULT;
  7266. }
  7267. return ret;
  7268. #endif
  7269. #ifdef TARGET_NR_mknod
  7270. case TARGET_NR_mknod:
  7271. if (!(p = lock_user_string(arg1)))
  7272. return -TARGET_EFAULT;
  7273. ret = get_errno(mknod(p, arg2, arg3));
  7274. unlock_user(p, arg1, 0);
  7275. return ret;
  7276. #endif
  7277. #if defined(TARGET_NR_mknodat)
  7278. case TARGET_NR_mknodat:
  7279. if (!(p = lock_user_string(arg2)))
  7280. return -TARGET_EFAULT;
  7281. ret = get_errno(mknodat(arg1, p, arg3, arg4));
  7282. unlock_user(p, arg2, 0);
  7283. return ret;
  7284. #endif
  7285. #ifdef TARGET_NR_chmod
  7286. case TARGET_NR_chmod:
  7287. if (!(p = lock_user_string(arg1)))
  7288. return -TARGET_EFAULT;
  7289. ret = get_errno(chmod(p, arg2));
  7290. unlock_user(p, arg1, 0);
  7291. return ret;
  7292. #endif
  7293. #ifdef TARGET_NR_lseek
  7294. case TARGET_NR_lseek:
  7295. return get_errno(lseek(arg1, arg2, arg3));
  7296. #endif
  7297. #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
  7298. /* Alpha specific */
  7299. case TARGET_NR_getxpid:
  7300. ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
  7301. return get_errno(getpid());
  7302. #endif
  7303. #ifdef TARGET_NR_getpid
  7304. case TARGET_NR_getpid:
  7305. return get_errno(getpid());
  7306. #endif
  7307. case TARGET_NR_mount:
  7308. {
  7309. /* need to look at the data field */
  7310. void *p2, *p3;
  7311. if (arg1) {
  7312. p = lock_user_string(arg1);
  7313. if (!p) {
  7314. return -TARGET_EFAULT;
  7315. }
  7316. } else {
  7317. p = NULL;
  7318. }
  7319. p2 = lock_user_string(arg2);
  7320. if (!p2) {
  7321. if (arg1) {
  7322. unlock_user(p, arg1, 0);
  7323. }
  7324. return -TARGET_EFAULT;
  7325. }
  7326. if (arg3) {
  7327. p3 = lock_user_string(arg3);
  7328. if (!p3) {
  7329. if (arg1) {
  7330. unlock_user(p, arg1, 0);
  7331. }
  7332. unlock_user(p2, arg2, 0);
  7333. return -TARGET_EFAULT;
  7334. }
  7335. } else {
  7336. p3 = NULL;
  7337. }
  7338. /* FIXME - arg5 should be locked, but it isn't clear how to
  7339. * do that since it's not guaranteed to be a NULL-terminated
  7340. * string.
  7341. */
  7342. if (!arg5) {
  7343. ret = mount(p, p2, p3, (unsigned long)arg4, NULL);
  7344. } else {
  7345. ret = mount(p, p2, p3, (unsigned long)arg4, g2h(arg5));
  7346. }
  7347. ret = get_errno(ret);
  7348. if (arg1) {
  7349. unlock_user(p, arg1, 0);
  7350. }
  7351. unlock_user(p2, arg2, 0);
  7352. if (arg3) {
  7353. unlock_user(p3, arg3, 0);
  7354. }
  7355. }
  7356. return ret;
  7357. #if defined(TARGET_NR_umount) || defined(TARGET_NR_oldumount)
  7358. #if defined(TARGET_NR_umount)
  7359. case TARGET_NR_umount:
  7360. #endif
  7361. #if defined(TARGET_NR_oldumount)
  7362. case TARGET_NR_oldumount:
  7363. #endif
  7364. if (!(p = lock_user_string(arg1)))
  7365. return -TARGET_EFAULT;
  7366. ret = get_errno(umount(p));
  7367. unlock_user(p, arg1, 0);
  7368. return ret;
  7369. #endif
  7370. #ifdef TARGET_NR_stime /* not on alpha */
  7371. case TARGET_NR_stime:
  7372. {
  7373. struct timespec ts;
  7374. ts.tv_nsec = 0;
  7375. if (get_user_sal(ts.tv_sec, arg1)) {
  7376. return -TARGET_EFAULT;
  7377. }
  7378. return get_errno(clock_settime(CLOCK_REALTIME, &ts));
  7379. }
  7380. #endif
  7381. #ifdef TARGET_NR_alarm /* not on alpha */
  7382. case TARGET_NR_alarm:
  7383. return alarm(arg1);
  7384. #endif
  7385. #ifdef TARGET_NR_pause /* not on alpha */
  7386. case TARGET_NR_pause:
  7387. if (!block_signals()) {
  7388. sigsuspend(&((TaskState *)cpu->opaque)->signal_mask);
  7389. }
  7390. return -TARGET_EINTR;
  7391. #endif
  7392. #ifdef TARGET_NR_utime
  7393. case TARGET_NR_utime:
  7394. {
  7395. struct utimbuf tbuf, *host_tbuf;
  7396. struct target_utimbuf *target_tbuf;
  7397. if (arg2) {
  7398. if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
  7399. return -TARGET_EFAULT;
  7400. tbuf.actime = tswapal(target_tbuf->actime);
  7401. tbuf.modtime = tswapal(target_tbuf->modtime);
  7402. unlock_user_struct(target_tbuf, arg2, 0);
  7403. host_tbuf = &tbuf;
  7404. } else {
  7405. host_tbuf = NULL;
  7406. }
  7407. if (!(p = lock_user_string(arg1)))
  7408. return -TARGET_EFAULT;
  7409. ret = get_errno(utime(p, host_tbuf));
  7410. unlock_user(p, arg1, 0);
  7411. }
  7412. return ret;
  7413. #endif
  7414. #ifdef TARGET_NR_utimes
  7415. case TARGET_NR_utimes:
  7416. {
  7417. struct timeval *tvp, tv[2];
  7418. if (arg2) {
  7419. if (copy_from_user_timeval(&tv[0], arg2)
  7420. || copy_from_user_timeval(&tv[1],
  7421. arg2 + sizeof(struct target_timeval)))
  7422. return -TARGET_EFAULT;
  7423. tvp = tv;
  7424. } else {
  7425. tvp = NULL;
  7426. }
  7427. if (!(p = lock_user_string(arg1)))
  7428. return -TARGET_EFAULT;
  7429. ret = get_errno(utimes(p, tvp));
  7430. unlock_user(p, arg1, 0);
  7431. }
  7432. return ret;
  7433. #endif
  7434. #if defined(TARGET_NR_futimesat)
  7435. case TARGET_NR_futimesat:
  7436. {
  7437. struct timeval *tvp, tv[2];
  7438. if (arg3) {
  7439. if (copy_from_user_timeval(&tv[0], arg3)
  7440. || copy_from_user_timeval(&tv[1],
  7441. arg3 + sizeof(struct target_timeval)))
  7442. return -TARGET_EFAULT;
  7443. tvp = tv;
  7444. } else {
  7445. tvp = NULL;
  7446. }
  7447. if (!(p = lock_user_string(arg2))) {
  7448. return -TARGET_EFAULT;
  7449. }
  7450. ret = get_errno(futimesat(arg1, path(p), tvp));
  7451. unlock_user(p, arg2, 0);
  7452. }
  7453. return ret;
  7454. #endif
  7455. #ifdef TARGET_NR_access
  7456. case TARGET_NR_access:
  7457. if (!(p = lock_user_string(arg1))) {
  7458. return -TARGET_EFAULT;
  7459. }
  7460. ret = get_errno(access(path(p), arg2));
  7461. unlock_user(p, arg1, 0);
  7462. return ret;
  7463. #endif
  7464. #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
  7465. case TARGET_NR_faccessat:
  7466. if (!(p = lock_user_string(arg2))) {
  7467. return -TARGET_EFAULT;
  7468. }
  7469. ret = get_errno(faccessat(arg1, p, arg3, 0));
  7470. unlock_user(p, arg2, 0);
  7471. return ret;
  7472. #endif
  7473. #ifdef TARGET_NR_nice /* not on alpha */
  7474. case TARGET_NR_nice:
  7475. return get_errno(nice(arg1));
  7476. #endif
  7477. case TARGET_NR_sync:
  7478. sync();
  7479. return 0;
  7480. #if defined(TARGET_NR_syncfs) && defined(CONFIG_SYNCFS)
  7481. case TARGET_NR_syncfs:
  7482. return get_errno(syncfs(arg1));
  7483. #endif
  7484. case TARGET_NR_kill:
  7485. return get_errno(safe_kill(arg1, target_to_host_signal(arg2)));
  7486. #ifdef TARGET_NR_rename
  7487. case TARGET_NR_rename:
  7488. {
  7489. void *p2;
  7490. p = lock_user_string(arg1);
  7491. p2 = lock_user_string(arg2);
  7492. if (!p || !p2)
  7493. ret = -TARGET_EFAULT;
  7494. else
  7495. ret = get_errno(rename(p, p2));
  7496. unlock_user(p2, arg2, 0);
  7497. unlock_user(p, arg1, 0);
  7498. }
  7499. return ret;
  7500. #endif
  7501. #if defined(TARGET_NR_renameat)
  7502. case TARGET_NR_renameat:
  7503. {
  7504. void *p2;
  7505. p = lock_user_string(arg2);
  7506. p2 = lock_user_string(arg4);
  7507. if (!p || !p2)
  7508. ret = -TARGET_EFAULT;
  7509. else
  7510. ret = get_errno(renameat(arg1, p, arg3, p2));
  7511. unlock_user(p2, arg4, 0);
  7512. unlock_user(p, arg2, 0);
  7513. }
  7514. return ret;
  7515. #endif
  7516. #if defined(TARGET_NR_renameat2)
  7517. case TARGET_NR_renameat2:
  7518. {
  7519. void *p2;
  7520. p = lock_user_string(arg2);
  7521. p2 = lock_user_string(arg4);
  7522. if (!p || !p2) {
  7523. ret = -TARGET_EFAULT;
  7524. } else {
  7525. ret = get_errno(sys_renameat2(arg1, p, arg3, p2, arg5));
  7526. }
  7527. unlock_user(p2, arg4, 0);
  7528. unlock_user(p, arg2, 0);
  7529. }
  7530. return ret;
  7531. #endif
  7532. #ifdef TARGET_NR_mkdir
  7533. case TARGET_NR_mkdir:
  7534. if (!(p = lock_user_string(arg1)))
  7535. return -TARGET_EFAULT;
  7536. ret = get_errno(mkdir(p, arg2));
  7537. unlock_user(p, arg1, 0);
  7538. return ret;
  7539. #endif
  7540. #if defined(TARGET_NR_mkdirat)
  7541. case TARGET_NR_mkdirat:
  7542. if (!(p = lock_user_string(arg2)))
  7543. return -TARGET_EFAULT;
  7544. ret = get_errno(mkdirat(arg1, p, arg3));
  7545. unlock_user(p, arg2, 0);
  7546. return ret;
  7547. #endif
  7548. #ifdef TARGET_NR_rmdir
  7549. case TARGET_NR_rmdir:
  7550. if (!(p = lock_user_string(arg1)))
  7551. return -TARGET_EFAULT;
  7552. ret = get_errno(rmdir(p));
  7553. unlock_user(p, arg1, 0);
  7554. return ret;
  7555. #endif
  7556. case TARGET_NR_dup:
  7557. ret = get_errno(dup(arg1));
  7558. if (ret >= 0) {
  7559. fd_trans_dup(arg1, ret);
  7560. }
  7561. return ret;
  7562. #ifdef TARGET_NR_pipe
  7563. case TARGET_NR_pipe:
  7564. return do_pipe(cpu_env, arg1, 0, 0);
  7565. #endif
  7566. #ifdef TARGET_NR_pipe2
  7567. case TARGET_NR_pipe2:
  7568. return do_pipe(cpu_env, arg1,
  7569. target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
  7570. #endif
  7571. case TARGET_NR_times:
  7572. {
  7573. struct target_tms *tmsp;
  7574. struct tms tms;
  7575. ret = get_errno(times(&tms));
  7576. if (arg1) {
  7577. tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
  7578. if (!tmsp)
  7579. return -TARGET_EFAULT;
  7580. tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
  7581. tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
  7582. tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
  7583. tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
  7584. }
  7585. if (!is_error(ret))
  7586. ret = host_to_target_clock_t(ret);
  7587. }
  7588. return ret;
  7589. case TARGET_NR_acct:
  7590. if (arg1 == 0) {
  7591. ret = get_errno(acct(NULL));
  7592. } else {
  7593. if (!(p = lock_user_string(arg1))) {
  7594. return -TARGET_EFAULT;
  7595. }
  7596. ret = get_errno(acct(path(p)));
  7597. unlock_user(p, arg1, 0);
  7598. }
  7599. return ret;
  7600. #ifdef TARGET_NR_umount2
  7601. case TARGET_NR_umount2:
  7602. if (!(p = lock_user_string(arg1)))
  7603. return -TARGET_EFAULT;
  7604. ret = get_errno(umount2(p, arg2));
  7605. unlock_user(p, arg1, 0);
  7606. return ret;
  7607. #endif
  7608. case TARGET_NR_ioctl:
  7609. return do_ioctl(arg1, arg2, arg3);
  7610. #ifdef TARGET_NR_fcntl
  7611. case TARGET_NR_fcntl:
  7612. return do_fcntl(arg1, arg2, arg3);
  7613. #endif
  7614. case TARGET_NR_setpgid:
  7615. return get_errno(setpgid(arg1, arg2));
  7616. case TARGET_NR_umask:
  7617. return get_errno(umask(arg1));
  7618. case TARGET_NR_chroot:
  7619. if (!(p = lock_user_string(arg1)))
  7620. return -TARGET_EFAULT;
  7621. ret = get_errno(chroot(p));
  7622. unlock_user(p, arg1, 0);
  7623. return ret;
  7624. #ifdef TARGET_NR_dup2
  7625. case TARGET_NR_dup2:
  7626. ret = get_errno(dup2(arg1, arg2));
  7627. if (ret >= 0) {
  7628. fd_trans_dup(arg1, arg2);
  7629. }
  7630. return ret;
  7631. #endif
  7632. #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
  7633. case TARGET_NR_dup3:
  7634. {
  7635. int host_flags;
  7636. if ((arg3 & ~TARGET_O_CLOEXEC) != 0) {
  7637. return -EINVAL;
  7638. }
  7639. host_flags = target_to_host_bitmask(arg3, fcntl_flags_tbl);
  7640. ret = get_errno(dup3(arg1, arg2, host_flags));
  7641. if (ret >= 0) {
  7642. fd_trans_dup(arg1, arg2);
  7643. }
  7644. return ret;
  7645. }
  7646. #endif
  7647. #ifdef TARGET_NR_getppid /* not on alpha */
  7648. case TARGET_NR_getppid:
  7649. return get_errno(getppid());
  7650. #endif
  7651. #ifdef TARGET_NR_getpgrp
  7652. case TARGET_NR_getpgrp:
  7653. return get_errno(getpgrp());
  7654. #endif
  7655. case TARGET_NR_setsid:
  7656. return get_errno(setsid());
  7657. #ifdef TARGET_NR_sigaction
  7658. case TARGET_NR_sigaction:
  7659. {
  7660. #if defined(TARGET_ALPHA)
  7661. struct target_sigaction act, oact, *pact = 0;
  7662. struct target_old_sigaction *old_act;
  7663. if (arg2) {
  7664. if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
  7665. return -TARGET_EFAULT;
  7666. act._sa_handler = old_act->_sa_handler;
  7667. target_siginitset(&act.sa_mask, old_act->sa_mask);
  7668. act.sa_flags = old_act->sa_flags;
  7669. act.sa_restorer = 0;
  7670. unlock_user_struct(old_act, arg2, 0);
  7671. pact = &act;
  7672. }
  7673. ret = get_errno(do_sigaction(arg1, pact, &oact));
  7674. if (!is_error(ret) && arg3) {
  7675. if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
  7676. return -TARGET_EFAULT;
  7677. old_act->_sa_handler = oact._sa_handler;
  7678. old_act->sa_mask = oact.sa_mask.sig[0];
  7679. old_act->sa_flags = oact.sa_flags;
  7680. unlock_user_struct(old_act, arg3, 1);
  7681. }
  7682. #elif defined(TARGET_MIPS)
  7683. struct target_sigaction act, oact, *pact, *old_act;
  7684. if (arg2) {
  7685. if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
  7686. return -TARGET_EFAULT;
  7687. act._sa_handler = old_act->_sa_handler;
  7688. target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
  7689. act.sa_flags = old_act->sa_flags;
  7690. unlock_user_struct(old_act, arg2, 0);
  7691. pact = &act;
  7692. } else {
  7693. pact = NULL;
  7694. }
  7695. ret = get_errno(do_sigaction(arg1, pact, &oact));
  7696. if (!is_error(ret) && arg3) {
  7697. if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
  7698. return -TARGET_EFAULT;
  7699. old_act->_sa_handler = oact._sa_handler;
  7700. old_act->sa_flags = oact.sa_flags;
  7701. old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
  7702. old_act->sa_mask.sig[1] = 0;
  7703. old_act->sa_mask.sig[2] = 0;
  7704. old_act->sa_mask.sig[3] = 0;
  7705. unlock_user_struct(old_act, arg3, 1);
  7706. }
  7707. #else
  7708. struct target_old_sigaction *old_act;
  7709. struct target_sigaction act, oact, *pact;
  7710. if (arg2) {
  7711. if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
  7712. return -TARGET_EFAULT;
  7713. act._sa_handler = old_act->_sa_handler;
  7714. target_siginitset(&act.sa_mask, old_act->sa_mask);
  7715. act.sa_flags = old_act->sa_flags;
  7716. act.sa_restorer = old_act->sa_restorer;
  7717. #ifdef TARGET_ARCH_HAS_KA_RESTORER
  7718. act.ka_restorer = 0;
  7719. #endif
  7720. unlock_user_struct(old_act, arg2, 0);
  7721. pact = &act;
  7722. } else {
  7723. pact = NULL;
  7724. }
  7725. ret = get_errno(do_sigaction(arg1, pact, &oact));
  7726. if (!is_error(ret) && arg3) {
  7727. if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
  7728. return -TARGET_EFAULT;
  7729. old_act->_sa_handler = oact._sa_handler;
  7730. old_act->sa_mask = oact.sa_mask.sig[0];
  7731. old_act->sa_flags = oact.sa_flags;
  7732. old_act->sa_restorer = oact.sa_restorer;
  7733. unlock_user_struct(old_act, arg3, 1);
  7734. }
  7735. #endif
  7736. }
  7737. return ret;
  7738. #endif
  7739. case TARGET_NR_rt_sigaction:
  7740. {
  7741. #if defined(TARGET_ALPHA)
  7742. /* For Alpha and SPARC this is a 5 argument syscall, with
  7743. * a 'restorer' parameter which must be copied into the
  7744. * sa_restorer field of the sigaction struct.
  7745. * For Alpha that 'restorer' is arg5; for SPARC it is arg4,
  7746. * and arg5 is the sigsetsize.
  7747. * Alpha also has a separate rt_sigaction struct that it uses
  7748. * here; SPARC uses the usual sigaction struct.
  7749. */
  7750. struct target_rt_sigaction *rt_act;
  7751. struct target_sigaction act, oact, *pact = 0;
  7752. if (arg4 != sizeof(target_sigset_t)) {
  7753. return -TARGET_EINVAL;
  7754. }
  7755. if (arg2) {
  7756. if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
  7757. return -TARGET_EFAULT;
  7758. act._sa_handler = rt_act->_sa_handler;
  7759. act.sa_mask = rt_act->sa_mask;
  7760. act.sa_flags = rt_act->sa_flags;
  7761. act.sa_restorer = arg5;
  7762. unlock_user_struct(rt_act, arg2, 0);
  7763. pact = &act;
  7764. }
  7765. ret = get_errno(do_sigaction(arg1, pact, &oact));
  7766. if (!is_error(ret) && arg3) {
  7767. if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
  7768. return -TARGET_EFAULT;
  7769. rt_act->_sa_handler = oact._sa_handler;
  7770. rt_act->sa_mask = oact.sa_mask;
  7771. rt_act->sa_flags = oact.sa_flags;
  7772. unlock_user_struct(rt_act, arg3, 1);
  7773. }
  7774. #else
  7775. #ifdef TARGET_SPARC
  7776. target_ulong restorer = arg4;
  7777. target_ulong sigsetsize = arg5;
  7778. #else
  7779. target_ulong sigsetsize = arg4;
  7780. #endif
  7781. struct target_sigaction *act;
  7782. struct target_sigaction *oact;
  7783. if (sigsetsize != sizeof(target_sigset_t)) {
  7784. return -TARGET_EINVAL;
  7785. }
  7786. if (arg2) {
  7787. if (!lock_user_struct(VERIFY_READ, act, arg2, 1)) {
  7788. return -TARGET_EFAULT;
  7789. }
  7790. #ifdef TARGET_ARCH_HAS_KA_RESTORER
  7791. act->ka_restorer = restorer;
  7792. #endif
  7793. } else {
  7794. act = NULL;
  7795. }
  7796. if (arg3) {
  7797. if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
  7798. ret = -TARGET_EFAULT;
  7799. goto rt_sigaction_fail;
  7800. }
  7801. } else
  7802. oact = NULL;
  7803. ret = get_errno(do_sigaction(arg1, act, oact));
  7804. rt_sigaction_fail:
  7805. if (act)
  7806. unlock_user_struct(act, arg2, 0);
  7807. if (oact)
  7808. unlock_user_struct(oact, arg3, 1);
  7809. #endif
  7810. }
  7811. return ret;
  7812. #ifdef TARGET_NR_sgetmask /* not on alpha */
  7813. case TARGET_NR_sgetmask:
  7814. {
  7815. sigset_t cur_set;
  7816. abi_ulong target_set;
  7817. ret = do_sigprocmask(0, NULL, &cur_set);
  7818. if (!ret) {
  7819. host_to_target_old_sigset(&target_set, &cur_set);
  7820. ret = target_set;
  7821. }
  7822. }
  7823. return ret;
  7824. #endif
  7825. #ifdef TARGET_NR_ssetmask /* not on alpha */
  7826. case TARGET_NR_ssetmask:
  7827. {
  7828. sigset_t set, oset;
  7829. abi_ulong target_set = arg1;
  7830. target_to_host_old_sigset(&set, &target_set);
  7831. ret = do_sigprocmask(SIG_SETMASK, &set, &oset);
  7832. if (!ret) {
  7833. host_to_target_old_sigset(&target_set, &oset);
  7834. ret = target_set;
  7835. }
  7836. }
  7837. return ret;
  7838. #endif
  7839. #ifdef TARGET_NR_sigprocmask
  7840. case TARGET_NR_sigprocmask:
  7841. {
  7842. #if defined(TARGET_ALPHA)
  7843. sigset_t set, oldset;
  7844. abi_ulong mask;
  7845. int how;
  7846. switch (arg1) {
  7847. case TARGET_SIG_BLOCK:
  7848. how = SIG_BLOCK;
  7849. break;
  7850. case TARGET_SIG_UNBLOCK:
  7851. how = SIG_UNBLOCK;
  7852. break;
  7853. case TARGET_SIG_SETMASK:
  7854. how = SIG_SETMASK;
  7855. break;
  7856. default:
  7857. return -TARGET_EINVAL;
  7858. }
  7859. mask = arg2;
  7860. target_to_host_old_sigset(&set, &mask);
  7861. ret = do_sigprocmask(how, &set, &oldset);
  7862. if (!is_error(ret)) {
  7863. host_to_target_old_sigset(&mask, &oldset);
  7864. ret = mask;
  7865. ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0; /* force no error */
  7866. }
  7867. #else
  7868. sigset_t set, oldset, *set_ptr;
  7869. int how;
  7870. if (arg2) {
  7871. switch (arg1) {
  7872. case TARGET_SIG_BLOCK:
  7873. how = SIG_BLOCK;
  7874. break;
  7875. case TARGET_SIG_UNBLOCK:
  7876. how = SIG_UNBLOCK;
  7877. break;
  7878. case TARGET_SIG_SETMASK:
  7879. how = SIG_SETMASK;
  7880. break;
  7881. default:
  7882. return -TARGET_EINVAL;
  7883. }
  7884. if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
  7885. return -TARGET_EFAULT;
  7886. target_to_host_old_sigset(&set, p);
  7887. unlock_user(p, arg2, 0);
  7888. set_ptr = &set;
  7889. } else {
  7890. how = 0;
  7891. set_ptr = NULL;
  7892. }
  7893. ret = do_sigprocmask(how, set_ptr, &oldset);
  7894. if (!is_error(ret) && arg3) {
  7895. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
  7896. return -TARGET_EFAULT;
  7897. host_to_target_old_sigset(p, &oldset);
  7898. unlock_user(p, arg3, sizeof(target_sigset_t));
  7899. }
  7900. #endif
  7901. }
  7902. return ret;
  7903. #endif
  7904. case TARGET_NR_rt_sigprocmask:
  7905. {
  7906. int how = arg1;
  7907. sigset_t set, oldset, *set_ptr;
  7908. if (arg4 != sizeof(target_sigset_t)) {
  7909. return -TARGET_EINVAL;
  7910. }
  7911. if (arg2) {
  7912. switch(how) {
  7913. case TARGET_SIG_BLOCK:
  7914. how = SIG_BLOCK;
  7915. break;
  7916. case TARGET_SIG_UNBLOCK:
  7917. how = SIG_UNBLOCK;
  7918. break;
  7919. case TARGET_SIG_SETMASK:
  7920. how = SIG_SETMASK;
  7921. break;
  7922. default:
  7923. return -TARGET_EINVAL;
  7924. }
  7925. if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
  7926. return -TARGET_EFAULT;
  7927. target_to_host_sigset(&set, p);
  7928. unlock_user(p, arg2, 0);
  7929. set_ptr = &set;
  7930. } else {
  7931. how = 0;
  7932. set_ptr = NULL;
  7933. }
  7934. ret = do_sigprocmask(how, set_ptr, &oldset);
  7935. if (!is_error(ret) && arg3) {
  7936. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
  7937. return -TARGET_EFAULT;
  7938. host_to_target_sigset(p, &oldset);
  7939. unlock_user(p, arg3, sizeof(target_sigset_t));
  7940. }
  7941. }
  7942. return ret;
  7943. #ifdef TARGET_NR_sigpending
  7944. case TARGET_NR_sigpending:
  7945. {
  7946. sigset_t set;
  7947. ret = get_errno(sigpending(&set));
  7948. if (!is_error(ret)) {
  7949. if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
  7950. return -TARGET_EFAULT;
  7951. host_to_target_old_sigset(p, &set);
  7952. unlock_user(p, arg1, sizeof(target_sigset_t));
  7953. }
  7954. }
  7955. return ret;
  7956. #endif
  7957. case TARGET_NR_rt_sigpending:
  7958. {
  7959. sigset_t set;
  7960. /* Yes, this check is >, not != like most. We follow the kernel's
  7961. * logic and it does it like this because it implements
  7962. * NR_sigpending through the same code path, and in that case
  7963. * the old_sigset_t is smaller in size.
  7964. */
  7965. if (arg2 > sizeof(target_sigset_t)) {
  7966. return -TARGET_EINVAL;
  7967. }
  7968. ret = get_errno(sigpending(&set));
  7969. if (!is_error(ret)) {
  7970. if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
  7971. return -TARGET_EFAULT;
  7972. host_to_target_sigset(p, &set);
  7973. unlock_user(p, arg1, sizeof(target_sigset_t));
  7974. }
  7975. }
  7976. return ret;
  7977. #ifdef TARGET_NR_sigsuspend
  7978. case TARGET_NR_sigsuspend:
  7979. {
  7980. TaskState *ts = cpu->opaque;
  7981. #if defined(TARGET_ALPHA)
  7982. abi_ulong mask = arg1;
  7983. target_to_host_old_sigset(&ts->sigsuspend_mask, &mask);
  7984. #else
  7985. if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
  7986. return -TARGET_EFAULT;
  7987. target_to_host_old_sigset(&ts->sigsuspend_mask, p);
  7988. unlock_user(p, arg1, 0);
  7989. #endif
  7990. ret = get_errno(safe_rt_sigsuspend(&ts->sigsuspend_mask,
  7991. SIGSET_T_SIZE));
  7992. if (ret != -TARGET_ERESTARTSYS) {
  7993. ts->in_sigsuspend = 1;
  7994. }
  7995. }
  7996. return ret;
  7997. #endif
  7998. case TARGET_NR_rt_sigsuspend:
  7999. {
  8000. TaskState *ts = cpu->opaque;
  8001. if (arg2 != sizeof(target_sigset_t)) {
  8002. return -TARGET_EINVAL;
  8003. }
  8004. if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
  8005. return -TARGET_EFAULT;
  8006. target_to_host_sigset(&ts->sigsuspend_mask, p);
  8007. unlock_user(p, arg1, 0);
  8008. ret = get_errno(safe_rt_sigsuspend(&ts->sigsuspend_mask,
  8009. SIGSET_T_SIZE));
  8010. if (ret != -TARGET_ERESTARTSYS) {
  8011. ts->in_sigsuspend = 1;
  8012. }
  8013. }
  8014. return ret;
  8015. #ifdef TARGET_NR_rt_sigtimedwait
  8016. case TARGET_NR_rt_sigtimedwait:
  8017. {
  8018. sigset_t set;
  8019. struct timespec uts, *puts;
  8020. siginfo_t uinfo;
  8021. if (arg4 != sizeof(target_sigset_t)) {
  8022. return -TARGET_EINVAL;
  8023. }
  8024. if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
  8025. return -TARGET_EFAULT;
  8026. target_to_host_sigset(&set, p);
  8027. unlock_user(p, arg1, 0);
  8028. if (arg3) {
  8029. puts = &uts;
  8030. if (target_to_host_timespec(puts, arg3)) {
  8031. return -TARGET_EFAULT;
  8032. }
  8033. } else {
  8034. puts = NULL;
  8035. }
  8036. ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts,
  8037. SIGSET_T_SIZE));
  8038. if (!is_error(ret)) {
  8039. if (arg2) {
  8040. p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t),
  8041. 0);
  8042. if (!p) {
  8043. return -TARGET_EFAULT;
  8044. }
  8045. host_to_target_siginfo(p, &uinfo);
  8046. unlock_user(p, arg2, sizeof(target_siginfo_t));
  8047. }
  8048. ret = host_to_target_signal(ret);
  8049. }
  8050. }
  8051. return ret;
  8052. #endif
  8053. case TARGET_NR_rt_sigqueueinfo:
  8054. {
  8055. siginfo_t uinfo;
  8056. p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1);
  8057. if (!p) {
  8058. return -TARGET_EFAULT;
  8059. }
  8060. target_to_host_siginfo(&uinfo, p);
  8061. unlock_user(p, arg3, 0);
  8062. ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
  8063. }
  8064. return ret;
  8065. case TARGET_NR_rt_tgsigqueueinfo:
  8066. {
  8067. siginfo_t uinfo;
  8068. p = lock_user(VERIFY_READ, arg4, sizeof(target_siginfo_t), 1);
  8069. if (!p) {
  8070. return -TARGET_EFAULT;
  8071. }
  8072. target_to_host_siginfo(&uinfo, p);
  8073. unlock_user(p, arg4, 0);
  8074. ret = get_errno(sys_rt_tgsigqueueinfo(arg1, arg2, arg3, &uinfo));
  8075. }
  8076. return ret;
  8077. #ifdef TARGET_NR_sigreturn
  8078. case TARGET_NR_sigreturn:
  8079. if (block_signals()) {
  8080. return -TARGET_ERESTARTSYS;
  8081. }
  8082. return do_sigreturn(cpu_env);
  8083. #endif
  8084. case TARGET_NR_rt_sigreturn:
  8085. if (block_signals()) {
  8086. return -TARGET_ERESTARTSYS;
  8087. }
  8088. return do_rt_sigreturn(cpu_env);
  8089. case TARGET_NR_sethostname:
  8090. if (!(p = lock_user_string(arg1)))
  8091. return -TARGET_EFAULT;
  8092. ret = get_errno(sethostname(p, arg2));
  8093. unlock_user(p, arg1, 0);
  8094. return ret;
  8095. #ifdef TARGET_NR_setrlimit
  8096. case TARGET_NR_setrlimit:
  8097. {
  8098. int resource = target_to_host_resource(arg1);
  8099. struct target_rlimit *target_rlim;
  8100. struct rlimit rlim;
  8101. if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
  8102. return -TARGET_EFAULT;
  8103. rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
  8104. rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
  8105. unlock_user_struct(target_rlim, arg2, 0);
  8106. /*
  8107. * If we just passed through resource limit settings for memory then
  8108. * they would also apply to QEMU's own allocations, and QEMU will
  8109. * crash or hang or die if its allocations fail. Ideally we would
  8110. * track the guest allocations in QEMU and apply the limits ourselves.
  8111. * For now, just tell the guest the call succeeded but don't actually
  8112. * limit anything.
  8113. */
  8114. if (resource != RLIMIT_AS &&
  8115. resource != RLIMIT_DATA &&
  8116. resource != RLIMIT_STACK) {
  8117. return get_errno(setrlimit(resource, &rlim));
  8118. } else {
  8119. return 0;
  8120. }
  8121. }
  8122. #endif
  8123. #ifdef TARGET_NR_getrlimit
  8124. case TARGET_NR_getrlimit:
  8125. {
  8126. int resource = target_to_host_resource(arg1);
  8127. struct target_rlimit *target_rlim;
  8128. struct rlimit rlim;
  8129. ret = get_errno(getrlimit(resource, &rlim));
  8130. if (!is_error(ret)) {
  8131. if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
  8132. return -TARGET_EFAULT;
  8133. target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
  8134. target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
  8135. unlock_user_struct(target_rlim, arg2, 1);
  8136. }
  8137. }
  8138. return ret;
  8139. #endif
  8140. case TARGET_NR_getrusage:
  8141. {
  8142. struct rusage rusage;
  8143. ret = get_errno(getrusage(arg1, &rusage));
  8144. if (!is_error(ret)) {
  8145. ret = host_to_target_rusage(arg2, &rusage);
  8146. }
  8147. }
  8148. return ret;
  8149. #if defined(TARGET_NR_gettimeofday)
  8150. case TARGET_NR_gettimeofday:
  8151. {
  8152. struct timeval tv;
  8153. struct timezone tz;
  8154. ret = get_errno(gettimeofday(&tv, &tz));
  8155. if (!is_error(ret)) {
  8156. if (arg1 && copy_to_user_timeval(arg1, &tv)) {
  8157. return -TARGET_EFAULT;
  8158. }
  8159. if (arg2 && copy_to_user_timezone(arg2, &tz)) {
  8160. return -TARGET_EFAULT;
  8161. }
  8162. }
  8163. }
  8164. return ret;
  8165. #endif
  8166. #if defined(TARGET_NR_settimeofday)
  8167. case TARGET_NR_settimeofday:
  8168. {
  8169. struct timeval tv, *ptv = NULL;
  8170. struct timezone tz, *ptz = NULL;
  8171. if (arg1) {
  8172. if (copy_from_user_timeval(&tv, arg1)) {
  8173. return -TARGET_EFAULT;
  8174. }
  8175. ptv = &tv;
  8176. }
  8177. if (arg2) {
  8178. if (copy_from_user_timezone(&tz, arg2)) {
  8179. return -TARGET_EFAULT;
  8180. }
  8181. ptz = &tz;
  8182. }
  8183. return get_errno(settimeofday(ptv, ptz));
  8184. }
  8185. #endif
  8186. #if defined(TARGET_NR_select)
  8187. case TARGET_NR_select:
  8188. #if defined(TARGET_WANT_NI_OLD_SELECT)
  8189. /* some architectures used to have old_select here
  8190. * but now ENOSYS it.
  8191. */
  8192. ret = -TARGET_ENOSYS;
  8193. #elif defined(TARGET_WANT_OLD_SYS_SELECT)
  8194. ret = do_old_select(arg1);
  8195. #else
  8196. ret = do_select(arg1, arg2, arg3, arg4, arg5);
  8197. #endif
  8198. return ret;
  8199. #endif
  8200. #ifdef TARGET_NR_pselect6
  8201. case TARGET_NR_pselect6:
  8202. {
  8203. abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
  8204. fd_set rfds, wfds, efds;
  8205. fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
  8206. struct timespec ts, *ts_ptr;
  8207. /*
  8208. * The 6th arg is actually two args smashed together,
  8209. * so we cannot use the C library.
  8210. */
  8211. sigset_t set;
  8212. struct {
  8213. sigset_t *set;
  8214. size_t size;
  8215. } sig, *sig_ptr;
  8216. abi_ulong arg_sigset, arg_sigsize, *arg7;
  8217. target_sigset_t *target_sigset;
  8218. n = arg1;
  8219. rfd_addr = arg2;
  8220. wfd_addr = arg3;
  8221. efd_addr = arg4;
  8222. ts_addr = arg5;
  8223. ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
  8224. if (ret) {
  8225. return ret;
  8226. }
  8227. ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
  8228. if (ret) {
  8229. return ret;
  8230. }
  8231. ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
  8232. if (ret) {
  8233. return ret;
  8234. }
  8235. /*
  8236. * This takes a timespec, and not a timeval, so we cannot
  8237. * use the do_select() helper ...
  8238. */
  8239. if (ts_addr) {
  8240. if (target_to_host_timespec(&ts, ts_addr)) {
  8241. return -TARGET_EFAULT;
  8242. }
  8243. ts_ptr = &ts;
  8244. } else {
  8245. ts_ptr = NULL;
  8246. }
  8247. /* Extract the two packed args for the sigset */
  8248. if (arg6) {
  8249. sig_ptr = &sig;
  8250. sig.size = SIGSET_T_SIZE;
  8251. arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
  8252. if (!arg7) {
  8253. return -TARGET_EFAULT;
  8254. }
  8255. arg_sigset = tswapal(arg7[0]);
  8256. arg_sigsize = tswapal(arg7[1]);
  8257. unlock_user(arg7, arg6, 0);
  8258. if (arg_sigset) {
  8259. sig.set = &set;
  8260. if (arg_sigsize != sizeof(*target_sigset)) {
  8261. /* Like the kernel, we enforce correct size sigsets */
  8262. return -TARGET_EINVAL;
  8263. }
  8264. target_sigset = lock_user(VERIFY_READ, arg_sigset,
  8265. sizeof(*target_sigset), 1);
  8266. if (!target_sigset) {
  8267. return -TARGET_EFAULT;
  8268. }
  8269. target_to_host_sigset(&set, target_sigset);
  8270. unlock_user(target_sigset, arg_sigset, 0);
  8271. } else {
  8272. sig.set = NULL;
  8273. }
  8274. } else {
  8275. sig_ptr = NULL;
  8276. }
  8277. ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
  8278. ts_ptr, sig_ptr));
  8279. if (!is_error(ret)) {
  8280. if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
  8281. return -TARGET_EFAULT;
  8282. if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
  8283. return -TARGET_EFAULT;
  8284. if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
  8285. return -TARGET_EFAULT;
  8286. if (ts_addr && host_to_target_timespec(ts_addr, &ts))
  8287. return -TARGET_EFAULT;
  8288. }
  8289. }
  8290. return ret;
  8291. #endif
  8292. #ifdef TARGET_NR_symlink
  8293. case TARGET_NR_symlink:
  8294. {
  8295. void *p2;
  8296. p = lock_user_string(arg1);
  8297. p2 = lock_user_string(arg2);
  8298. if (!p || !p2)
  8299. ret = -TARGET_EFAULT;
  8300. else
  8301. ret = get_errno(symlink(p, p2));
  8302. unlock_user(p2, arg2, 0);
  8303. unlock_user(p, arg1, 0);
  8304. }
  8305. return ret;
  8306. #endif
  8307. #if defined(TARGET_NR_symlinkat)
  8308. case TARGET_NR_symlinkat:
  8309. {
  8310. void *p2;
  8311. p = lock_user_string(arg1);
  8312. p2 = lock_user_string(arg3);
  8313. if (!p || !p2)
  8314. ret = -TARGET_EFAULT;
  8315. else
  8316. ret = get_errno(symlinkat(p, arg2, p2));
  8317. unlock_user(p2, arg3, 0);
  8318. unlock_user(p, arg1, 0);
  8319. }
  8320. return ret;
  8321. #endif
  8322. #ifdef TARGET_NR_readlink
  8323. case TARGET_NR_readlink:
  8324. {
  8325. void *p2;
  8326. p = lock_user_string(arg1);
  8327. p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  8328. if (!p || !p2) {
  8329. ret = -TARGET_EFAULT;
  8330. } else if (!arg3) {
  8331. /* Short circuit this for the magic exe check. */
  8332. ret = -TARGET_EINVAL;
  8333. } else if (is_proc_myself((const char *)p, "exe")) {
  8334. char real[PATH_MAX], *temp;
  8335. temp = realpath(exec_path, real);
  8336. /* Return value is # of bytes that we wrote to the buffer. */
  8337. if (temp == NULL) {
  8338. ret = get_errno(-1);
  8339. } else {
  8340. /* Don't worry about sign mismatch as earlier mapping
  8341. * logic would have thrown a bad address error. */
  8342. ret = MIN(strlen(real), arg3);
  8343. /* We cannot NUL terminate the string. */
  8344. memcpy(p2, real, ret);
  8345. }
  8346. } else {
  8347. ret = get_errno(readlink(path(p), p2, arg3));
  8348. }
  8349. unlock_user(p2, arg2, ret);
  8350. unlock_user(p, arg1, 0);
  8351. }
  8352. return ret;
  8353. #endif
  8354. #if defined(TARGET_NR_readlinkat)
  8355. case TARGET_NR_readlinkat:
  8356. {
  8357. void *p2;
  8358. p = lock_user_string(arg2);
  8359. p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  8360. if (!p || !p2) {
  8361. ret = -TARGET_EFAULT;
  8362. } else if (is_proc_myself((const char *)p, "exe")) {
  8363. char real[PATH_MAX], *temp;
  8364. temp = realpath(exec_path, real);
  8365. ret = temp == NULL ? get_errno(-1) : strlen(real) ;
  8366. snprintf((char *)p2, arg4, "%s", real);
  8367. } else {
  8368. ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
  8369. }
  8370. unlock_user(p2, arg3, ret);
  8371. unlock_user(p, arg2, 0);
  8372. }
  8373. return ret;
  8374. #endif
  8375. #ifdef TARGET_NR_swapon
  8376. case TARGET_NR_swapon:
  8377. if (!(p = lock_user_string(arg1)))
  8378. return -TARGET_EFAULT;
  8379. ret = get_errno(swapon(p, arg2));
  8380. unlock_user(p, arg1, 0);
  8381. return ret;
  8382. #endif
  8383. case TARGET_NR_reboot:
  8384. if (arg3 == LINUX_REBOOT_CMD_RESTART2) {
  8385. /* arg4 must be ignored in all other cases */
  8386. p = lock_user_string(arg4);
  8387. if (!p) {
  8388. return -TARGET_EFAULT;
  8389. }
  8390. ret = get_errno(reboot(arg1, arg2, arg3, p));
  8391. unlock_user(p, arg4, 0);
  8392. } else {
  8393. ret = get_errno(reboot(arg1, arg2, arg3, NULL));
  8394. }
  8395. return ret;
  8396. #ifdef TARGET_NR_mmap
  8397. case TARGET_NR_mmap:
  8398. #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
  8399. (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
  8400. defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
  8401. || defined(TARGET_S390X)
  8402. {
  8403. abi_ulong *v;
  8404. abi_ulong v1, v2, v3, v4, v5, v6;
  8405. if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
  8406. return -TARGET_EFAULT;
  8407. v1 = tswapal(v[0]);
  8408. v2 = tswapal(v[1]);
  8409. v3 = tswapal(v[2]);
  8410. v4 = tswapal(v[3]);
  8411. v5 = tswapal(v[4]);
  8412. v6 = tswapal(v[5]);
  8413. unlock_user(v, arg1, 0);
  8414. ret = get_errno(target_mmap(v1, v2, v3,
  8415. target_to_host_bitmask(v4, mmap_flags_tbl),
  8416. v5, v6));
  8417. }
  8418. #else
  8419. ret = get_errno(target_mmap(arg1, arg2, arg3,
  8420. target_to_host_bitmask(arg4, mmap_flags_tbl),
  8421. arg5,
  8422. arg6));
  8423. #endif
  8424. return ret;
  8425. #endif
  8426. #ifdef TARGET_NR_mmap2
  8427. case TARGET_NR_mmap2:
  8428. #ifndef MMAP_SHIFT
  8429. #define MMAP_SHIFT 12
  8430. #endif
  8431. ret = target_mmap(arg1, arg2, arg3,
  8432. target_to_host_bitmask(arg4, mmap_flags_tbl),
  8433. arg5, arg6 << MMAP_SHIFT);
  8434. return get_errno(ret);
  8435. #endif
  8436. case TARGET_NR_munmap:
  8437. return get_errno(target_munmap(arg1, arg2));
  8438. case TARGET_NR_mprotect:
  8439. {
  8440. TaskState *ts = cpu->opaque;
  8441. /* Special hack to detect libc making the stack executable. */
  8442. if ((arg3 & PROT_GROWSDOWN)
  8443. && arg1 >= ts->info->stack_limit
  8444. && arg1 <= ts->info->start_stack) {
  8445. arg3 &= ~PROT_GROWSDOWN;
  8446. arg2 = arg2 + arg1 - ts->info->stack_limit;
  8447. arg1 = ts->info->stack_limit;
  8448. }
  8449. }
  8450. return get_errno(target_mprotect(arg1, arg2, arg3));
  8451. #ifdef TARGET_NR_mremap
  8452. case TARGET_NR_mremap:
  8453. return get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
  8454. #endif
  8455. /* ??? msync/mlock/munlock are broken for softmmu. */
  8456. #ifdef TARGET_NR_msync
  8457. case TARGET_NR_msync:
  8458. return get_errno(msync(g2h(arg1), arg2, arg3));
  8459. #endif
  8460. #ifdef TARGET_NR_mlock
  8461. case TARGET_NR_mlock:
  8462. return get_errno(mlock(g2h(arg1), arg2));
  8463. #endif
  8464. #ifdef TARGET_NR_munlock
  8465. case TARGET_NR_munlock:
  8466. return get_errno(munlock(g2h(arg1), arg2));
  8467. #endif
  8468. #ifdef TARGET_NR_mlockall
  8469. case TARGET_NR_mlockall:
  8470. return get_errno(mlockall(target_to_host_mlockall_arg(arg1)));
  8471. #endif
  8472. #ifdef TARGET_NR_munlockall
  8473. case TARGET_NR_munlockall:
  8474. return get_errno(munlockall());
  8475. #endif
  8476. #ifdef TARGET_NR_truncate
  8477. case TARGET_NR_truncate:
  8478. if (!(p = lock_user_string(arg1)))
  8479. return -TARGET_EFAULT;
  8480. ret = get_errno(truncate(p, arg2));
  8481. unlock_user(p, arg1, 0);
  8482. return ret;
  8483. #endif
  8484. #ifdef TARGET_NR_ftruncate
  8485. case TARGET_NR_ftruncate:
  8486. return get_errno(ftruncate(arg1, arg2));
  8487. #endif
  8488. case TARGET_NR_fchmod:
  8489. return get_errno(fchmod(arg1, arg2));
  8490. #if defined(TARGET_NR_fchmodat)
  8491. case TARGET_NR_fchmodat:
  8492. if (!(p = lock_user_string(arg2)))
  8493. return -TARGET_EFAULT;
  8494. ret = get_errno(fchmodat(arg1, p, arg3, 0));
  8495. unlock_user(p, arg2, 0);
  8496. return ret;
  8497. #endif
  8498. case TARGET_NR_getpriority:
  8499. /* Note that negative values are valid for getpriority, so we must
  8500. differentiate based on errno settings. */
  8501. errno = 0;
  8502. ret = getpriority(arg1, arg2);
  8503. if (ret == -1 && errno != 0) {
  8504. return -host_to_target_errno(errno);
  8505. }
  8506. #ifdef TARGET_ALPHA
  8507. /* Return value is the unbiased priority. Signal no error. */
  8508. ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0;
  8509. #else
  8510. /* Return value is a biased priority to avoid negative numbers. */
  8511. ret = 20 - ret;
  8512. #endif
  8513. return ret;
  8514. case TARGET_NR_setpriority:
  8515. return get_errno(setpriority(arg1, arg2, arg3));
  8516. #ifdef TARGET_NR_statfs
  8517. case TARGET_NR_statfs:
  8518. if (!(p = lock_user_string(arg1))) {
  8519. return -TARGET_EFAULT;
  8520. }
  8521. ret = get_errno(statfs(path(p), &stfs));
  8522. unlock_user(p, arg1, 0);
  8523. convert_statfs:
  8524. if (!is_error(ret)) {
  8525. struct target_statfs *target_stfs;
  8526. if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
  8527. return -TARGET_EFAULT;
  8528. __put_user(stfs.f_type, &target_stfs->f_type);
  8529. __put_user(stfs.f_bsize, &target_stfs->f_bsize);
  8530. __put_user(stfs.f_blocks, &target_stfs->f_blocks);
  8531. __put_user(stfs.f_bfree, &target_stfs->f_bfree);
  8532. __put_user(stfs.f_bavail, &target_stfs->f_bavail);
  8533. __put_user(stfs.f_files, &target_stfs->f_files);
  8534. __put_user(stfs.f_ffree, &target_stfs->f_ffree);
  8535. __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
  8536. __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
  8537. __put_user(stfs.f_namelen, &target_stfs->f_namelen);
  8538. __put_user(stfs.f_frsize, &target_stfs->f_frsize);
  8539. #ifdef _STATFS_F_FLAGS
  8540. __put_user(stfs.f_flags, &target_stfs->f_flags);
  8541. #else
  8542. __put_user(0, &target_stfs->f_flags);
  8543. #endif
  8544. memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
  8545. unlock_user_struct(target_stfs, arg2, 1);
  8546. }
  8547. return ret;
  8548. #endif
  8549. #ifdef TARGET_NR_fstatfs
  8550. case TARGET_NR_fstatfs:
  8551. ret = get_errno(fstatfs(arg1, &stfs));
  8552. goto convert_statfs;
  8553. #endif
  8554. #ifdef TARGET_NR_statfs64
  8555. case TARGET_NR_statfs64:
  8556. if (!(p = lock_user_string(arg1))) {
  8557. return -TARGET_EFAULT;
  8558. }
  8559. ret = get_errno(statfs(path(p), &stfs));
  8560. unlock_user(p, arg1, 0);
  8561. convert_statfs64:
  8562. if (!is_error(ret)) {
  8563. struct target_statfs64 *target_stfs;
  8564. if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
  8565. return -TARGET_EFAULT;
  8566. __put_user(stfs.f_type, &target_stfs->f_type);
  8567. __put_user(stfs.f_bsize, &target_stfs->f_bsize);
  8568. __put_user(stfs.f_blocks, &target_stfs->f_blocks);
  8569. __put_user(stfs.f_bfree, &target_stfs->f_bfree);
  8570. __put_user(stfs.f_bavail, &target_stfs->f_bavail);
  8571. __put_user(stfs.f_files, &target_stfs->f_files);
  8572. __put_user(stfs.f_ffree, &target_stfs->f_ffree);
  8573. __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
  8574. __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
  8575. __put_user(stfs.f_namelen, &target_stfs->f_namelen);
  8576. __put_user(stfs.f_frsize, &target_stfs->f_frsize);
  8577. memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
  8578. unlock_user_struct(target_stfs, arg3, 1);
  8579. }
  8580. return ret;
  8581. case TARGET_NR_fstatfs64:
  8582. ret = get_errno(fstatfs(arg1, &stfs));
  8583. goto convert_statfs64;
  8584. #endif
  8585. #ifdef TARGET_NR_socketcall
  8586. case TARGET_NR_socketcall:
  8587. return do_socketcall(arg1, arg2);
  8588. #endif
  8589. #ifdef TARGET_NR_accept
  8590. case TARGET_NR_accept:
  8591. return do_accept4(arg1, arg2, arg3, 0);
  8592. #endif
  8593. #ifdef TARGET_NR_accept4
  8594. case TARGET_NR_accept4:
  8595. return do_accept4(arg1, arg2, arg3, arg4);
  8596. #endif
  8597. #ifdef TARGET_NR_bind
  8598. case TARGET_NR_bind:
  8599. return do_bind(arg1, arg2, arg3);
  8600. #endif
  8601. #ifdef TARGET_NR_connect
  8602. case TARGET_NR_connect:
  8603. return do_connect(arg1, arg2, arg3);
  8604. #endif
  8605. #ifdef TARGET_NR_getpeername
  8606. case TARGET_NR_getpeername:
  8607. return do_getpeername(arg1, arg2, arg3);
  8608. #endif
  8609. #ifdef TARGET_NR_getsockname
  8610. case TARGET_NR_getsockname:
  8611. return do_getsockname(arg1, arg2, arg3);
  8612. #endif
  8613. #ifdef TARGET_NR_getsockopt
  8614. case TARGET_NR_getsockopt:
  8615. return do_getsockopt(arg1, arg2, arg3, arg4, arg5);
  8616. #endif
  8617. #ifdef TARGET_NR_listen
  8618. case TARGET_NR_listen:
  8619. return get_errno(listen(arg1, arg2));
  8620. #endif
  8621. #ifdef TARGET_NR_recv
  8622. case TARGET_NR_recv:
  8623. return do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
  8624. #endif
  8625. #ifdef TARGET_NR_recvfrom
  8626. case TARGET_NR_recvfrom:
  8627. return do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
  8628. #endif
  8629. #ifdef TARGET_NR_recvmsg
  8630. case TARGET_NR_recvmsg:
  8631. return do_sendrecvmsg(arg1, arg2, arg3, 0);
  8632. #endif
  8633. #ifdef TARGET_NR_send
  8634. case TARGET_NR_send:
  8635. return do_sendto(arg1, arg2, arg3, arg4, 0, 0);
  8636. #endif
  8637. #ifdef TARGET_NR_sendmsg
  8638. case TARGET_NR_sendmsg:
  8639. return do_sendrecvmsg(arg1, arg2, arg3, 1);
  8640. #endif
  8641. #ifdef TARGET_NR_sendmmsg
  8642. case TARGET_NR_sendmmsg:
  8643. return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 1);
  8644. #endif
  8645. #ifdef TARGET_NR_recvmmsg
  8646. case TARGET_NR_recvmmsg:
  8647. return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 0);
  8648. #endif
  8649. #ifdef TARGET_NR_sendto
  8650. case TARGET_NR_sendto:
  8651. return do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
  8652. #endif
  8653. #ifdef TARGET_NR_shutdown
  8654. case TARGET_NR_shutdown:
  8655. return get_errno(shutdown(arg1, arg2));
  8656. #endif
  8657. #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
  8658. case TARGET_NR_getrandom:
  8659. p = lock_user(VERIFY_WRITE, arg1, arg2, 0);
  8660. if (!p) {
  8661. return -TARGET_EFAULT;
  8662. }
  8663. ret = get_errno(getrandom(p, arg2, arg3));
  8664. unlock_user(p, arg1, ret);
  8665. return ret;
  8666. #endif
  8667. #ifdef TARGET_NR_socket
  8668. case TARGET_NR_socket:
  8669. return do_socket(arg1, arg2, arg3);
  8670. #endif
  8671. #ifdef TARGET_NR_socketpair
  8672. case TARGET_NR_socketpair:
  8673. return do_socketpair(arg1, arg2, arg3, arg4);
  8674. #endif
  8675. #ifdef TARGET_NR_setsockopt
  8676. case TARGET_NR_setsockopt:
  8677. return do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
  8678. #endif
  8679. #if defined(TARGET_NR_syslog)
  8680. case TARGET_NR_syslog:
  8681. {
  8682. int len = arg2;
  8683. switch (arg1) {
  8684. case TARGET_SYSLOG_ACTION_CLOSE: /* Close log */
  8685. case TARGET_SYSLOG_ACTION_OPEN: /* Open log */
  8686. case TARGET_SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
  8687. case TARGET_SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging */
  8688. case TARGET_SYSLOG_ACTION_CONSOLE_ON: /* Enable logging */
  8689. case TARGET_SYSLOG_ACTION_CONSOLE_LEVEL: /* Set messages level */
  8690. case TARGET_SYSLOG_ACTION_SIZE_UNREAD: /* Number of chars */
  8691. case TARGET_SYSLOG_ACTION_SIZE_BUFFER: /* Size of the buffer */
  8692. return get_errno(sys_syslog((int)arg1, NULL, (int)arg3));
  8693. case TARGET_SYSLOG_ACTION_READ: /* Read from log */
  8694. case TARGET_SYSLOG_ACTION_READ_CLEAR: /* Read/clear msgs */
  8695. case TARGET_SYSLOG_ACTION_READ_ALL: /* Read last messages */
  8696. {
  8697. if (len < 0) {
  8698. return -TARGET_EINVAL;
  8699. }
  8700. if (len == 0) {
  8701. return 0;
  8702. }
  8703. p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  8704. if (!p) {
  8705. return -TARGET_EFAULT;
  8706. }
  8707. ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
  8708. unlock_user(p, arg2, arg3);
  8709. }
  8710. return ret;
  8711. default:
  8712. return -TARGET_EINVAL;
  8713. }
  8714. }
  8715. break;
  8716. #endif
  8717. case TARGET_NR_setitimer:
  8718. {
  8719. struct itimerval value, ovalue, *pvalue;
  8720. if (arg2) {
  8721. pvalue = &value;
  8722. if (copy_from_user_timeval(&pvalue->it_interval, arg2)
  8723. || copy_from_user_timeval(&pvalue->it_value,
  8724. arg2 + sizeof(struct target_timeval)))
  8725. return -TARGET_EFAULT;
  8726. } else {
  8727. pvalue = NULL;
  8728. }
  8729. ret = get_errno(setitimer(arg1, pvalue, &ovalue));
  8730. if (!is_error(ret) && arg3) {
  8731. if (copy_to_user_timeval(arg3,
  8732. &ovalue.it_interval)
  8733. || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
  8734. &ovalue.it_value))
  8735. return -TARGET_EFAULT;
  8736. }
  8737. }
  8738. return ret;
  8739. case TARGET_NR_getitimer:
  8740. {
  8741. struct itimerval value;
  8742. ret = get_errno(getitimer(arg1, &value));
  8743. if (!is_error(ret) && arg2) {
  8744. if (copy_to_user_timeval(arg2,
  8745. &value.it_interval)
  8746. || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
  8747. &value.it_value))
  8748. return -TARGET_EFAULT;
  8749. }
  8750. }
  8751. return ret;
  8752. #ifdef TARGET_NR_stat
  8753. case TARGET_NR_stat:
  8754. if (!(p = lock_user_string(arg1))) {
  8755. return -TARGET_EFAULT;
  8756. }
  8757. ret = get_errno(stat(path(p), &st));
  8758. unlock_user(p, arg1, 0);
  8759. goto do_stat;
  8760. #endif
  8761. #ifdef TARGET_NR_lstat
  8762. case TARGET_NR_lstat:
  8763. if (!(p = lock_user_string(arg1))) {
  8764. return -TARGET_EFAULT;
  8765. }
  8766. ret = get_errno(lstat(path(p), &st));
  8767. unlock_user(p, arg1, 0);
  8768. goto do_stat;
  8769. #endif
  8770. #ifdef TARGET_NR_fstat
  8771. case TARGET_NR_fstat:
  8772. {
  8773. ret = get_errno(fstat(arg1, &st));
  8774. #if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat)
  8775. do_stat:
  8776. #endif
  8777. if (!is_error(ret)) {
  8778. struct target_stat *target_st;
  8779. if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
  8780. return -TARGET_EFAULT;
  8781. memset(target_st, 0, sizeof(*target_st));
  8782. __put_user(st.st_dev, &target_st->st_dev);
  8783. __put_user(st.st_ino, &target_st->st_ino);
  8784. __put_user(st.st_mode, &target_st->st_mode);
  8785. __put_user(st.st_uid, &target_st->st_uid);
  8786. __put_user(st.st_gid, &target_st->st_gid);
  8787. __put_user(st.st_nlink, &target_st->st_nlink);
  8788. __put_user(st.st_rdev, &target_st->st_rdev);
  8789. __put_user(st.st_size, &target_st->st_size);
  8790. __put_user(st.st_blksize, &target_st->st_blksize);
  8791. __put_user(st.st_blocks, &target_st->st_blocks);
  8792. __put_user(st.st_atime, &target_st->target_st_atime);
  8793. __put_user(st.st_mtime, &target_st->target_st_mtime);
  8794. __put_user(st.st_ctime, &target_st->target_st_ctime);
  8795. #if (_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700) && \
  8796. defined(TARGET_STAT_HAVE_NSEC)
  8797. __put_user(st.st_atim.tv_nsec,
  8798. &target_st->target_st_atime_nsec);
  8799. __put_user(st.st_mtim.tv_nsec,
  8800. &target_st->target_st_mtime_nsec);
  8801. __put_user(st.st_ctim.tv_nsec,
  8802. &target_st->target_st_ctime_nsec);
  8803. #endif
  8804. unlock_user_struct(target_st, arg2, 1);
  8805. }
  8806. }
  8807. return ret;
  8808. #endif
  8809. case TARGET_NR_vhangup:
  8810. return get_errno(vhangup());
  8811. #ifdef TARGET_NR_syscall
  8812. case TARGET_NR_syscall:
  8813. return do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
  8814. arg6, arg7, arg8, 0);
  8815. #endif
  8816. #if defined(TARGET_NR_wait4)
  8817. case TARGET_NR_wait4:
  8818. {
  8819. int status;
  8820. abi_long status_ptr = arg2;
  8821. struct rusage rusage, *rusage_ptr;
  8822. abi_ulong target_rusage = arg4;
  8823. abi_long rusage_err;
  8824. if (target_rusage)
  8825. rusage_ptr = &rusage;
  8826. else
  8827. rusage_ptr = NULL;
  8828. ret = get_errno(safe_wait4(arg1, &status, arg3, rusage_ptr));
  8829. if (!is_error(ret)) {
  8830. if (status_ptr && ret) {
  8831. status = host_to_target_waitstatus(status);
  8832. if (put_user_s32(status, status_ptr))
  8833. return -TARGET_EFAULT;
  8834. }
  8835. if (target_rusage) {
  8836. rusage_err = host_to_target_rusage(target_rusage, &rusage);
  8837. if (rusage_err) {
  8838. ret = rusage_err;
  8839. }
  8840. }
  8841. }
  8842. }
  8843. return ret;
  8844. #endif
  8845. #ifdef TARGET_NR_swapoff
  8846. case TARGET_NR_swapoff:
  8847. if (!(p = lock_user_string(arg1)))
  8848. return -TARGET_EFAULT;
  8849. ret = get_errno(swapoff(p));
  8850. unlock_user(p, arg1, 0);
  8851. return ret;
  8852. #endif
  8853. case TARGET_NR_sysinfo:
  8854. {
  8855. struct target_sysinfo *target_value;
  8856. struct sysinfo value;
  8857. ret = get_errno(sysinfo(&value));
  8858. if (!is_error(ret) && arg1)
  8859. {
  8860. if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
  8861. return -TARGET_EFAULT;
  8862. __put_user(value.uptime, &target_value->uptime);
  8863. __put_user(value.loads[0], &target_value->loads[0]);
  8864. __put_user(value.loads[1], &target_value->loads[1]);
  8865. __put_user(value.loads[2], &target_value->loads[2]);
  8866. __put_user(value.totalram, &target_value->totalram);
  8867. __put_user(value.freeram, &target_value->freeram);
  8868. __put_user(value.sharedram, &target_value->sharedram);
  8869. __put_user(value.bufferram, &target_value->bufferram);
  8870. __put_user(value.totalswap, &target_value->totalswap);
  8871. __put_user(value.freeswap, &target_value->freeswap);
  8872. __put_user(value.procs, &target_value->procs);
  8873. __put_user(value.totalhigh, &target_value->totalhigh);
  8874. __put_user(value.freehigh, &target_value->freehigh);
  8875. __put_user(value.mem_unit, &target_value->mem_unit);
  8876. unlock_user_struct(target_value, arg1, 1);
  8877. }
  8878. }
  8879. return ret;
  8880. #ifdef TARGET_NR_ipc
  8881. case TARGET_NR_ipc:
  8882. return do_ipc(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6);
  8883. #endif
  8884. #ifdef TARGET_NR_semget
  8885. case TARGET_NR_semget:
  8886. return get_errno(semget(arg1, arg2, arg3));
  8887. #endif
  8888. #ifdef TARGET_NR_semop
  8889. case TARGET_NR_semop:
  8890. return do_semtimedop(arg1, arg2, arg3, 0);
  8891. #endif
  8892. #ifdef TARGET_NR_semtimedop
  8893. case TARGET_NR_semtimedop:
  8894. return do_semtimedop(arg1, arg2, arg3, arg4);
  8895. #endif
  8896. #ifdef TARGET_NR_semctl
  8897. case TARGET_NR_semctl:
  8898. return do_semctl(arg1, arg2, arg3, arg4);
  8899. #endif
  8900. #ifdef TARGET_NR_msgctl
  8901. case TARGET_NR_msgctl:
  8902. return do_msgctl(arg1, arg2, arg3);
  8903. #endif
  8904. #ifdef TARGET_NR_msgget
  8905. case TARGET_NR_msgget:
  8906. return get_errno(msgget(arg1, arg2));
  8907. #endif
  8908. #ifdef TARGET_NR_msgrcv
  8909. case TARGET_NR_msgrcv:
  8910. return do_msgrcv(arg1, arg2, arg3, arg4, arg5);
  8911. #endif
  8912. #ifdef TARGET_NR_msgsnd
  8913. case TARGET_NR_msgsnd:
  8914. return do_msgsnd(arg1, arg2, arg3, arg4);
  8915. #endif
  8916. #ifdef TARGET_NR_shmget
  8917. case TARGET_NR_shmget:
  8918. return get_errno(shmget(arg1, arg2, arg3));
  8919. #endif
  8920. #ifdef TARGET_NR_shmctl
  8921. case TARGET_NR_shmctl:
  8922. return do_shmctl(arg1, arg2, arg3);
  8923. #endif
  8924. #ifdef TARGET_NR_shmat
  8925. case TARGET_NR_shmat:
  8926. return do_shmat(cpu_env, arg1, arg2, arg3);
  8927. #endif
  8928. #ifdef TARGET_NR_shmdt
  8929. case TARGET_NR_shmdt:
  8930. return do_shmdt(arg1);
  8931. #endif
  8932. case TARGET_NR_fsync:
  8933. return get_errno(fsync(arg1));
  8934. case TARGET_NR_clone:
  8935. /* Linux manages to have three different orderings for its
  8936. * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
  8937. * match the kernel's CONFIG_CLONE_* settings.
  8938. * Microblaze is further special in that it uses a sixth
  8939. * implicit argument to clone for the TLS pointer.
  8940. */
  8941. #if defined(TARGET_MICROBLAZE)
  8942. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5));
  8943. #elif defined(TARGET_CLONE_BACKWARDS)
  8944. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
  8945. #elif defined(TARGET_CLONE_BACKWARDS2)
  8946. ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
  8947. #else
  8948. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
  8949. #endif
  8950. return ret;
  8951. #ifdef __NR_exit_group
  8952. /* new thread calls */
  8953. case TARGET_NR_exit_group:
  8954. preexit_cleanup(cpu_env, arg1);
  8955. return get_errno(exit_group(arg1));
  8956. #endif
  8957. case TARGET_NR_setdomainname:
  8958. if (!(p = lock_user_string(arg1)))
  8959. return -TARGET_EFAULT;
  8960. ret = get_errno(setdomainname(p, arg2));
  8961. unlock_user(p, arg1, 0);
  8962. return ret;
  8963. case TARGET_NR_uname:
  8964. /* no need to transcode because we use the linux syscall */
  8965. {
  8966. struct new_utsname * buf;
  8967. if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
  8968. return -TARGET_EFAULT;
  8969. ret = get_errno(sys_uname(buf));
  8970. if (!is_error(ret)) {
  8971. /* Overwrite the native machine name with whatever is being
  8972. emulated. */
  8973. g_strlcpy(buf->machine, cpu_to_uname_machine(cpu_env),
  8974. sizeof(buf->machine));
  8975. /* Allow the user to override the reported release. */
  8976. if (qemu_uname_release && *qemu_uname_release) {
  8977. g_strlcpy(buf->release, qemu_uname_release,
  8978. sizeof(buf->release));
  8979. }
  8980. }
  8981. unlock_user_struct(buf, arg1, 1);
  8982. }
  8983. return ret;
  8984. #ifdef TARGET_I386
  8985. case TARGET_NR_modify_ldt:
  8986. return do_modify_ldt(cpu_env, arg1, arg2, arg3);
  8987. #if !defined(TARGET_X86_64)
  8988. case TARGET_NR_vm86:
  8989. return do_vm86(cpu_env, arg1, arg2);
  8990. #endif
  8991. #endif
  8992. #if defined(TARGET_NR_adjtimex)
  8993. case TARGET_NR_adjtimex:
  8994. {
  8995. struct timex host_buf;
  8996. if (target_to_host_timex(&host_buf, arg1) != 0) {
  8997. return -TARGET_EFAULT;
  8998. }
  8999. ret = get_errno(adjtimex(&host_buf));
  9000. if (!is_error(ret)) {
  9001. if (host_to_target_timex(arg1, &host_buf) != 0) {
  9002. return -TARGET_EFAULT;
  9003. }
  9004. }
  9005. }
  9006. return ret;
  9007. #endif
  9008. #if defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME)
  9009. case TARGET_NR_clock_adjtime:
  9010. {
  9011. struct timex htx, *phtx = &htx;
  9012. if (target_to_host_timex(phtx, arg2) != 0) {
  9013. return -TARGET_EFAULT;
  9014. }
  9015. ret = get_errno(clock_adjtime(arg1, phtx));
  9016. if (!is_error(ret) && phtx) {
  9017. if (host_to_target_timex(arg2, phtx) != 0) {
  9018. return -TARGET_EFAULT;
  9019. }
  9020. }
  9021. }
  9022. return ret;
  9023. #endif
  9024. case TARGET_NR_getpgid:
  9025. return get_errno(getpgid(arg1));
  9026. case TARGET_NR_fchdir:
  9027. return get_errno(fchdir(arg1));
  9028. case TARGET_NR_personality:
  9029. return get_errno(personality(arg1));
  9030. #ifdef TARGET_NR__llseek /* Not on alpha */
  9031. case TARGET_NR__llseek:
  9032. {
  9033. int64_t res;
  9034. #if !defined(__NR_llseek)
  9035. res = lseek(arg1, ((uint64_t)arg2 << 32) | (abi_ulong)arg3, arg5);
  9036. if (res == -1) {
  9037. ret = get_errno(res);
  9038. } else {
  9039. ret = 0;
  9040. }
  9041. #else
  9042. ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
  9043. #endif
  9044. if ((ret == 0) && put_user_s64(res, arg4)) {
  9045. return -TARGET_EFAULT;
  9046. }
  9047. }
  9048. return ret;
  9049. #endif
  9050. #ifdef TARGET_NR_getdents
  9051. case TARGET_NR_getdents:
  9052. #ifdef EMULATE_GETDENTS_WITH_GETDENTS
  9053. #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
  9054. {
  9055. struct target_dirent *target_dirp;
  9056. struct linux_dirent *dirp;
  9057. abi_long count = arg3;
  9058. dirp = g_try_malloc(count);
  9059. if (!dirp) {
  9060. return -TARGET_ENOMEM;
  9061. }
  9062. ret = get_errno(sys_getdents(arg1, dirp, count));
  9063. if (!is_error(ret)) {
  9064. struct linux_dirent *de;
  9065. struct target_dirent *tde;
  9066. int len = ret;
  9067. int reclen, treclen;
  9068. int count1, tnamelen;
  9069. count1 = 0;
  9070. de = dirp;
  9071. if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
  9072. return -TARGET_EFAULT;
  9073. tde = target_dirp;
  9074. while (len > 0) {
  9075. reclen = de->d_reclen;
  9076. tnamelen = reclen - offsetof(struct linux_dirent, d_name);
  9077. assert(tnamelen >= 0);
  9078. treclen = tnamelen + offsetof(struct target_dirent, d_name);
  9079. assert(count1 + treclen <= count);
  9080. tde->d_reclen = tswap16(treclen);
  9081. tde->d_ino = tswapal(de->d_ino);
  9082. tde->d_off = tswapal(de->d_off);
  9083. memcpy(tde->d_name, de->d_name, tnamelen);
  9084. de = (struct linux_dirent *)((char *)de + reclen);
  9085. len -= reclen;
  9086. tde = (struct target_dirent *)((char *)tde + treclen);
  9087. count1 += treclen;
  9088. }
  9089. ret = count1;
  9090. unlock_user(target_dirp, arg2, ret);
  9091. }
  9092. g_free(dirp);
  9093. }
  9094. #else
  9095. {
  9096. struct linux_dirent *dirp;
  9097. abi_long count = arg3;
  9098. if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
  9099. return -TARGET_EFAULT;
  9100. ret = get_errno(sys_getdents(arg1, dirp, count));
  9101. if (!is_error(ret)) {
  9102. struct linux_dirent *de;
  9103. int len = ret;
  9104. int reclen;
  9105. de = dirp;
  9106. while (len > 0) {
  9107. reclen = de->d_reclen;
  9108. if (reclen > len)
  9109. break;
  9110. de->d_reclen = tswap16(reclen);
  9111. tswapls(&de->d_ino);
  9112. tswapls(&de->d_off);
  9113. de = (struct linux_dirent *)((char *)de + reclen);
  9114. len -= reclen;
  9115. }
  9116. }
  9117. unlock_user(dirp, arg2, ret);
  9118. }
  9119. #endif
  9120. #else
  9121. /* Implement getdents in terms of getdents64 */
  9122. {
  9123. struct linux_dirent64 *dirp;
  9124. abi_long count = arg3;
  9125. dirp = lock_user(VERIFY_WRITE, arg2, count, 0);
  9126. if (!dirp) {
  9127. return -TARGET_EFAULT;
  9128. }
  9129. ret = get_errno(sys_getdents64(arg1, dirp, count));
  9130. if (!is_error(ret)) {
  9131. /* Convert the dirent64 structs to target dirent. We do this
  9132. * in-place, since we can guarantee that a target_dirent is no
  9133. * larger than a dirent64; however this means we have to be
  9134. * careful to read everything before writing in the new format.
  9135. */
  9136. struct linux_dirent64 *de;
  9137. struct target_dirent *tde;
  9138. int len = ret;
  9139. int tlen = 0;
  9140. de = dirp;
  9141. tde = (struct target_dirent *)dirp;
  9142. while (len > 0) {
  9143. int namelen, treclen;
  9144. int reclen = de->d_reclen;
  9145. uint64_t ino = de->d_ino;
  9146. int64_t off = de->d_off;
  9147. uint8_t type = de->d_type;
  9148. namelen = strlen(de->d_name);
  9149. treclen = offsetof(struct target_dirent, d_name)
  9150. + namelen + 2;
  9151. treclen = QEMU_ALIGN_UP(treclen, sizeof(abi_long));
  9152. memmove(tde->d_name, de->d_name, namelen + 1);
  9153. tde->d_ino = tswapal(ino);
  9154. tde->d_off = tswapal(off);
  9155. tde->d_reclen = tswap16(treclen);
  9156. /* The target_dirent type is in what was formerly a padding
  9157. * byte at the end of the structure:
  9158. */
  9159. *(((char *)tde) + treclen - 1) = type;
  9160. de = (struct linux_dirent64 *)((char *)de + reclen);
  9161. tde = (struct target_dirent *)((char *)tde + treclen);
  9162. len -= reclen;
  9163. tlen += treclen;
  9164. }
  9165. ret = tlen;
  9166. }
  9167. unlock_user(dirp, arg2, ret);
  9168. }
  9169. #endif
  9170. return ret;
  9171. #endif /* TARGET_NR_getdents */
  9172. #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
  9173. case TARGET_NR_getdents64:
  9174. {
  9175. struct linux_dirent64 *dirp;
  9176. abi_long count = arg3;
  9177. if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
  9178. return -TARGET_EFAULT;
  9179. ret = get_errno(sys_getdents64(arg1, dirp, count));
  9180. if (!is_error(ret)) {
  9181. struct linux_dirent64 *de;
  9182. int len = ret;
  9183. int reclen;
  9184. de = dirp;
  9185. while (len > 0) {
  9186. reclen = de->d_reclen;
  9187. if (reclen > len)
  9188. break;
  9189. de->d_reclen = tswap16(reclen);
  9190. tswap64s((uint64_t *)&de->d_ino);
  9191. tswap64s((uint64_t *)&de->d_off);
  9192. de = (struct linux_dirent64 *)((char *)de + reclen);
  9193. len -= reclen;
  9194. }
  9195. }
  9196. unlock_user(dirp, arg2, ret);
  9197. }
  9198. return ret;
  9199. #endif /* TARGET_NR_getdents64 */
  9200. #if defined(TARGET_NR__newselect)
  9201. case TARGET_NR__newselect:
  9202. return do_select(arg1, arg2, arg3, arg4, arg5);
  9203. #endif
  9204. #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
  9205. # ifdef TARGET_NR_poll
  9206. case TARGET_NR_poll:
  9207. # endif
  9208. # ifdef TARGET_NR_ppoll
  9209. case TARGET_NR_ppoll:
  9210. # endif
  9211. {
  9212. struct target_pollfd *target_pfd;
  9213. unsigned int nfds = arg2;
  9214. struct pollfd *pfd;
  9215. unsigned int i;
  9216. pfd = NULL;
  9217. target_pfd = NULL;
  9218. if (nfds) {
  9219. if (nfds > (INT_MAX / sizeof(struct target_pollfd))) {
  9220. return -TARGET_EINVAL;
  9221. }
  9222. target_pfd = lock_user(VERIFY_WRITE, arg1,
  9223. sizeof(struct target_pollfd) * nfds, 1);
  9224. if (!target_pfd) {
  9225. return -TARGET_EFAULT;
  9226. }
  9227. pfd = alloca(sizeof(struct pollfd) * nfds);
  9228. for (i = 0; i < nfds; i++) {
  9229. pfd[i].fd = tswap32(target_pfd[i].fd);
  9230. pfd[i].events = tswap16(target_pfd[i].events);
  9231. }
  9232. }
  9233. switch (num) {
  9234. # ifdef TARGET_NR_ppoll
  9235. case TARGET_NR_ppoll:
  9236. {
  9237. struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
  9238. target_sigset_t *target_set;
  9239. sigset_t _set, *set = &_set;
  9240. if (arg3) {
  9241. if (target_to_host_timespec(timeout_ts, arg3)) {
  9242. unlock_user(target_pfd, arg1, 0);
  9243. return -TARGET_EFAULT;
  9244. }
  9245. } else {
  9246. timeout_ts = NULL;
  9247. }
  9248. if (arg4) {
  9249. if (arg5 != sizeof(target_sigset_t)) {
  9250. unlock_user(target_pfd, arg1, 0);
  9251. return -TARGET_EINVAL;
  9252. }
  9253. target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1);
  9254. if (!target_set) {
  9255. unlock_user(target_pfd, arg1, 0);
  9256. return -TARGET_EFAULT;
  9257. }
  9258. target_to_host_sigset(set, target_set);
  9259. } else {
  9260. set = NULL;
  9261. }
  9262. ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts,
  9263. set, SIGSET_T_SIZE));
  9264. if (!is_error(ret) && arg3) {
  9265. host_to_target_timespec(arg3, timeout_ts);
  9266. }
  9267. if (arg4) {
  9268. unlock_user(target_set, arg4, 0);
  9269. }
  9270. break;
  9271. }
  9272. # endif
  9273. # ifdef TARGET_NR_poll
  9274. case TARGET_NR_poll:
  9275. {
  9276. struct timespec ts, *pts;
  9277. if (arg3 >= 0) {
  9278. /* Convert ms to secs, ns */
  9279. ts.tv_sec = arg3 / 1000;
  9280. ts.tv_nsec = (arg3 % 1000) * 1000000LL;
  9281. pts = &ts;
  9282. } else {
  9283. /* -ve poll() timeout means "infinite" */
  9284. pts = NULL;
  9285. }
  9286. ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0));
  9287. break;
  9288. }
  9289. # endif
  9290. default:
  9291. g_assert_not_reached();
  9292. }
  9293. if (!is_error(ret)) {
  9294. for(i = 0; i < nfds; i++) {
  9295. target_pfd[i].revents = tswap16(pfd[i].revents);
  9296. }
  9297. }
  9298. unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
  9299. }
  9300. return ret;
  9301. #endif
  9302. case TARGET_NR_flock:
  9303. /* NOTE: the flock constant seems to be the same for every
  9304. Linux platform */
  9305. return get_errno(safe_flock(arg1, arg2));
  9306. case TARGET_NR_readv:
  9307. {
  9308. struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
  9309. if (vec != NULL) {
  9310. ret = get_errno(safe_readv(arg1, vec, arg3));
  9311. unlock_iovec(vec, arg2, arg3, 1);
  9312. } else {
  9313. ret = -host_to_target_errno(errno);
  9314. }
  9315. }
  9316. return ret;
  9317. case TARGET_NR_writev:
  9318. {
  9319. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  9320. if (vec != NULL) {
  9321. ret = get_errno(safe_writev(arg1, vec, arg3));
  9322. unlock_iovec(vec, arg2, arg3, 0);
  9323. } else {
  9324. ret = -host_to_target_errno(errno);
  9325. }
  9326. }
  9327. return ret;
  9328. #if defined(TARGET_NR_preadv)
  9329. case TARGET_NR_preadv:
  9330. {
  9331. struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
  9332. if (vec != NULL) {
  9333. unsigned long low, high;
  9334. target_to_host_low_high(arg4, arg5, &low, &high);
  9335. ret = get_errno(safe_preadv(arg1, vec, arg3, low, high));
  9336. unlock_iovec(vec, arg2, arg3, 1);
  9337. } else {
  9338. ret = -host_to_target_errno(errno);
  9339. }
  9340. }
  9341. return ret;
  9342. #endif
  9343. #if defined(TARGET_NR_pwritev)
  9344. case TARGET_NR_pwritev:
  9345. {
  9346. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  9347. if (vec != NULL) {
  9348. unsigned long low, high;
  9349. target_to_host_low_high(arg4, arg5, &low, &high);
  9350. ret = get_errno(safe_pwritev(arg1, vec, arg3, low, high));
  9351. unlock_iovec(vec, arg2, arg3, 0);
  9352. } else {
  9353. ret = -host_to_target_errno(errno);
  9354. }
  9355. }
  9356. return ret;
  9357. #endif
  9358. case TARGET_NR_getsid:
  9359. return get_errno(getsid(arg1));
  9360. #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
  9361. case TARGET_NR_fdatasync:
  9362. return get_errno(fdatasync(arg1));
  9363. #endif
  9364. #ifdef TARGET_NR__sysctl
  9365. case TARGET_NR__sysctl:
  9366. /* We don't implement this, but ENOTDIR is always a safe
  9367. return value. */
  9368. return -TARGET_ENOTDIR;
  9369. #endif
  9370. case TARGET_NR_sched_getaffinity:
  9371. {
  9372. unsigned int mask_size;
  9373. unsigned long *mask;
  9374. /*
  9375. * sched_getaffinity needs multiples of ulong, so need to take
  9376. * care of mismatches between target ulong and host ulong sizes.
  9377. */
  9378. if (arg2 & (sizeof(abi_ulong) - 1)) {
  9379. return -TARGET_EINVAL;
  9380. }
  9381. mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
  9382. mask = alloca(mask_size);
  9383. memset(mask, 0, mask_size);
  9384. ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
  9385. if (!is_error(ret)) {
  9386. if (ret > arg2) {
  9387. /* More data returned than the caller's buffer will fit.
  9388. * This only happens if sizeof(abi_long) < sizeof(long)
  9389. * and the caller passed us a buffer holding an odd number
  9390. * of abi_longs. If the host kernel is actually using the
  9391. * extra 4 bytes then fail EINVAL; otherwise we can just
  9392. * ignore them and only copy the interesting part.
  9393. */
  9394. int numcpus = sysconf(_SC_NPROCESSORS_CONF);
  9395. if (numcpus > arg2 * 8) {
  9396. return -TARGET_EINVAL;
  9397. }
  9398. ret = arg2;
  9399. }
  9400. if (host_to_target_cpu_mask(mask, mask_size, arg3, ret)) {
  9401. return -TARGET_EFAULT;
  9402. }
  9403. }
  9404. }
  9405. return ret;
  9406. case TARGET_NR_sched_setaffinity:
  9407. {
  9408. unsigned int mask_size;
  9409. unsigned long *mask;
  9410. /*
  9411. * sched_setaffinity needs multiples of ulong, so need to take
  9412. * care of mismatches between target ulong and host ulong sizes.
  9413. */
  9414. if (arg2 & (sizeof(abi_ulong) - 1)) {
  9415. return -TARGET_EINVAL;
  9416. }
  9417. mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
  9418. mask = alloca(mask_size);
  9419. ret = target_to_host_cpu_mask(mask, mask_size, arg3, arg2);
  9420. if (ret) {
  9421. return ret;
  9422. }
  9423. return get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
  9424. }
  9425. case TARGET_NR_getcpu:
  9426. {
  9427. unsigned cpu, node;
  9428. ret = get_errno(sys_getcpu(arg1 ? &cpu : NULL,
  9429. arg2 ? &node : NULL,
  9430. NULL));
  9431. if (is_error(ret)) {
  9432. return ret;
  9433. }
  9434. if (arg1 && put_user_u32(cpu, arg1)) {
  9435. return -TARGET_EFAULT;
  9436. }
  9437. if (arg2 && put_user_u32(node, arg2)) {
  9438. return -TARGET_EFAULT;
  9439. }
  9440. }
  9441. return ret;
  9442. case TARGET_NR_sched_setparam:
  9443. {
  9444. struct sched_param *target_schp;
  9445. struct sched_param schp;
  9446. if (arg2 == 0) {
  9447. return -TARGET_EINVAL;
  9448. }
  9449. if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
  9450. return -TARGET_EFAULT;
  9451. schp.sched_priority = tswap32(target_schp->sched_priority);
  9452. unlock_user_struct(target_schp, arg2, 0);
  9453. return get_errno(sched_setparam(arg1, &schp));
  9454. }
  9455. case TARGET_NR_sched_getparam:
  9456. {
  9457. struct sched_param *target_schp;
  9458. struct sched_param schp;
  9459. if (arg2 == 0) {
  9460. return -TARGET_EINVAL;
  9461. }
  9462. ret = get_errno(sched_getparam(arg1, &schp));
  9463. if (!is_error(ret)) {
  9464. if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
  9465. return -TARGET_EFAULT;
  9466. target_schp->sched_priority = tswap32(schp.sched_priority);
  9467. unlock_user_struct(target_schp, arg2, 1);
  9468. }
  9469. }
  9470. return ret;
  9471. case TARGET_NR_sched_setscheduler:
  9472. {
  9473. struct sched_param *target_schp;
  9474. struct sched_param schp;
  9475. if (arg3 == 0) {
  9476. return -TARGET_EINVAL;
  9477. }
  9478. if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
  9479. return -TARGET_EFAULT;
  9480. schp.sched_priority = tswap32(target_schp->sched_priority);
  9481. unlock_user_struct(target_schp, arg3, 0);
  9482. return get_errno(sched_setscheduler(arg1, arg2, &schp));
  9483. }
  9484. case TARGET_NR_sched_getscheduler:
  9485. return get_errno(sched_getscheduler(arg1));
  9486. case TARGET_NR_sched_yield:
  9487. return get_errno(sched_yield());
  9488. case TARGET_NR_sched_get_priority_max:
  9489. return get_errno(sched_get_priority_max(arg1));
  9490. case TARGET_NR_sched_get_priority_min:
  9491. return get_errno(sched_get_priority_min(arg1));
  9492. #ifdef TARGET_NR_sched_rr_get_interval
  9493. case TARGET_NR_sched_rr_get_interval:
  9494. {
  9495. struct timespec ts;
  9496. ret = get_errno(sched_rr_get_interval(arg1, &ts));
  9497. if (!is_error(ret)) {
  9498. ret = host_to_target_timespec(arg2, &ts);
  9499. }
  9500. }
  9501. return ret;
  9502. #endif
  9503. #if defined(TARGET_NR_nanosleep)
  9504. case TARGET_NR_nanosleep:
  9505. {
  9506. struct timespec req, rem;
  9507. target_to_host_timespec(&req, arg1);
  9508. ret = get_errno(safe_nanosleep(&req, &rem));
  9509. if (is_error(ret) && arg2) {
  9510. host_to_target_timespec(arg2, &rem);
  9511. }
  9512. }
  9513. return ret;
  9514. #endif
  9515. case TARGET_NR_prctl:
  9516. switch (arg1) {
  9517. case PR_GET_PDEATHSIG:
  9518. {
  9519. int deathsig;
  9520. ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
  9521. if (!is_error(ret) && arg2
  9522. && put_user_ual(deathsig, arg2)) {
  9523. return -TARGET_EFAULT;
  9524. }
  9525. return ret;
  9526. }
  9527. #ifdef PR_GET_NAME
  9528. case PR_GET_NAME:
  9529. {
  9530. void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
  9531. if (!name) {
  9532. return -TARGET_EFAULT;
  9533. }
  9534. ret = get_errno(prctl(arg1, (unsigned long)name,
  9535. arg3, arg4, arg5));
  9536. unlock_user(name, arg2, 16);
  9537. return ret;
  9538. }
  9539. case PR_SET_NAME:
  9540. {
  9541. void *name = lock_user(VERIFY_READ, arg2, 16, 1);
  9542. if (!name) {
  9543. return -TARGET_EFAULT;
  9544. }
  9545. ret = get_errno(prctl(arg1, (unsigned long)name,
  9546. arg3, arg4, arg5));
  9547. unlock_user(name, arg2, 0);
  9548. return ret;
  9549. }
  9550. #endif
  9551. #ifdef TARGET_MIPS
  9552. case TARGET_PR_GET_FP_MODE:
  9553. {
  9554. CPUMIPSState *env = ((CPUMIPSState *)cpu_env);
  9555. ret = 0;
  9556. if (env->CP0_Status & (1 << CP0St_FR)) {
  9557. ret |= TARGET_PR_FP_MODE_FR;
  9558. }
  9559. if (env->CP0_Config5 & (1 << CP0C5_FRE)) {
  9560. ret |= TARGET_PR_FP_MODE_FRE;
  9561. }
  9562. return ret;
  9563. }
  9564. case TARGET_PR_SET_FP_MODE:
  9565. {
  9566. CPUMIPSState *env = ((CPUMIPSState *)cpu_env);
  9567. bool old_fr = env->CP0_Status & (1 << CP0St_FR);
  9568. bool old_fre = env->CP0_Config5 & (1 << CP0C5_FRE);
  9569. bool new_fr = arg2 & TARGET_PR_FP_MODE_FR;
  9570. bool new_fre = arg2 & TARGET_PR_FP_MODE_FRE;
  9571. const unsigned int known_bits = TARGET_PR_FP_MODE_FR |
  9572. TARGET_PR_FP_MODE_FRE;
  9573. /* If nothing to change, return right away, successfully. */
  9574. if (old_fr == new_fr && old_fre == new_fre) {
  9575. return 0;
  9576. }
  9577. /* Check the value is valid */
  9578. if (arg2 & ~known_bits) {
  9579. return -TARGET_EOPNOTSUPP;
  9580. }
  9581. /* Setting FRE without FR is not supported. */
  9582. if (new_fre && !new_fr) {
  9583. return -TARGET_EOPNOTSUPP;
  9584. }
  9585. if (new_fr && !(env->active_fpu.fcr0 & (1 << FCR0_F64))) {
  9586. /* FR1 is not supported */
  9587. return -TARGET_EOPNOTSUPP;
  9588. }
  9589. if (!new_fr && (env->active_fpu.fcr0 & (1 << FCR0_F64))
  9590. && !(env->CP0_Status_rw_bitmask & (1 << CP0St_FR))) {
  9591. /* cannot set FR=0 */
  9592. return -TARGET_EOPNOTSUPP;
  9593. }
  9594. if (new_fre && !(env->active_fpu.fcr0 & (1 << FCR0_FREP))) {
  9595. /* Cannot set FRE=1 */
  9596. return -TARGET_EOPNOTSUPP;
  9597. }
  9598. int i;
  9599. fpr_t *fpr = env->active_fpu.fpr;
  9600. for (i = 0; i < 32 ; i += 2) {
  9601. if (!old_fr && new_fr) {
  9602. fpr[i].w[!FP_ENDIAN_IDX] = fpr[i + 1].w[FP_ENDIAN_IDX];
  9603. } else if (old_fr && !new_fr) {
  9604. fpr[i + 1].w[FP_ENDIAN_IDX] = fpr[i].w[!FP_ENDIAN_IDX];
  9605. }
  9606. }
  9607. if (new_fr) {
  9608. env->CP0_Status |= (1 << CP0St_FR);
  9609. env->hflags |= MIPS_HFLAG_F64;
  9610. } else {
  9611. env->CP0_Status &= ~(1 << CP0St_FR);
  9612. env->hflags &= ~MIPS_HFLAG_F64;
  9613. }
  9614. if (new_fre) {
  9615. env->CP0_Config5 |= (1 << CP0C5_FRE);
  9616. if (env->active_fpu.fcr0 & (1 << FCR0_FREP)) {
  9617. env->hflags |= MIPS_HFLAG_FRE;
  9618. }
  9619. } else {
  9620. env->CP0_Config5 &= ~(1 << CP0C5_FRE);
  9621. env->hflags &= ~MIPS_HFLAG_FRE;
  9622. }
  9623. return 0;
  9624. }
  9625. #endif /* MIPS */
  9626. #ifdef TARGET_AARCH64
  9627. case TARGET_PR_SVE_SET_VL:
  9628. /*
  9629. * We cannot support either PR_SVE_SET_VL_ONEXEC or
  9630. * PR_SVE_VL_INHERIT. Note the kernel definition
  9631. * of sve_vl_valid allows for VQ=512, i.e. VL=8192,
  9632. * even though the current architectural maximum is VQ=16.
  9633. */
  9634. ret = -TARGET_EINVAL;
  9635. if (cpu_isar_feature(aa64_sve, env_archcpu(cpu_env))
  9636. && arg2 >= 0 && arg2 <= 512 * 16 && !(arg2 & 15)) {
  9637. CPUARMState *env = cpu_env;
  9638. ARMCPU *cpu = env_archcpu(env);
  9639. uint32_t vq, old_vq;
  9640. old_vq = (env->vfp.zcr_el[1] & 0xf) + 1;
  9641. vq = MAX(arg2 / 16, 1);
  9642. vq = MIN(vq, cpu->sve_max_vq);
  9643. if (vq < old_vq) {
  9644. aarch64_sve_narrow_vq(env, vq);
  9645. }
  9646. env->vfp.zcr_el[1] = vq - 1;
  9647. arm_rebuild_hflags(env);
  9648. ret = vq * 16;
  9649. }
  9650. return ret;
  9651. case TARGET_PR_SVE_GET_VL:
  9652. ret = -TARGET_EINVAL;
  9653. {
  9654. ARMCPU *cpu = env_archcpu(cpu_env);
  9655. if (cpu_isar_feature(aa64_sve, cpu)) {
  9656. ret = ((cpu->env.vfp.zcr_el[1] & 0xf) + 1) * 16;
  9657. }
  9658. }
  9659. return ret;
  9660. case TARGET_PR_PAC_RESET_KEYS:
  9661. {
  9662. CPUARMState *env = cpu_env;
  9663. ARMCPU *cpu = env_archcpu(env);
  9664. if (arg3 || arg4 || arg5) {
  9665. return -TARGET_EINVAL;
  9666. }
  9667. if (cpu_isar_feature(aa64_pauth, cpu)) {
  9668. int all = (TARGET_PR_PAC_APIAKEY | TARGET_PR_PAC_APIBKEY |
  9669. TARGET_PR_PAC_APDAKEY | TARGET_PR_PAC_APDBKEY |
  9670. TARGET_PR_PAC_APGAKEY);
  9671. int ret = 0;
  9672. Error *err = NULL;
  9673. if (arg2 == 0) {
  9674. arg2 = all;
  9675. } else if (arg2 & ~all) {
  9676. return -TARGET_EINVAL;
  9677. }
  9678. if (arg2 & TARGET_PR_PAC_APIAKEY) {
  9679. ret |= qemu_guest_getrandom(&env->keys.apia,
  9680. sizeof(ARMPACKey), &err);
  9681. }
  9682. if (arg2 & TARGET_PR_PAC_APIBKEY) {
  9683. ret |= qemu_guest_getrandom(&env->keys.apib,
  9684. sizeof(ARMPACKey), &err);
  9685. }
  9686. if (arg2 & TARGET_PR_PAC_APDAKEY) {
  9687. ret |= qemu_guest_getrandom(&env->keys.apda,
  9688. sizeof(ARMPACKey), &err);
  9689. }
  9690. if (arg2 & TARGET_PR_PAC_APDBKEY) {
  9691. ret |= qemu_guest_getrandom(&env->keys.apdb,
  9692. sizeof(ARMPACKey), &err);
  9693. }
  9694. if (arg2 & TARGET_PR_PAC_APGAKEY) {
  9695. ret |= qemu_guest_getrandom(&env->keys.apga,
  9696. sizeof(ARMPACKey), &err);
  9697. }
  9698. if (ret != 0) {
  9699. /*
  9700. * Some unknown failure in the crypto. The best
  9701. * we can do is log it and fail the syscall.
  9702. * The real syscall cannot fail this way.
  9703. */
  9704. qemu_log_mask(LOG_UNIMP,
  9705. "PR_PAC_RESET_KEYS: Crypto failure: %s",
  9706. error_get_pretty(err));
  9707. error_free(err);
  9708. return -TARGET_EIO;
  9709. }
  9710. return 0;
  9711. }
  9712. }
  9713. return -TARGET_EINVAL;
  9714. #endif /* AARCH64 */
  9715. case PR_GET_SECCOMP:
  9716. case PR_SET_SECCOMP:
  9717. /* Disable seccomp to prevent the target disabling syscalls we
  9718. * need. */
  9719. return -TARGET_EINVAL;
  9720. default:
  9721. /* Most prctl options have no pointer arguments */
  9722. return get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
  9723. }
  9724. break;
  9725. #ifdef TARGET_NR_arch_prctl
  9726. case TARGET_NR_arch_prctl:
  9727. return do_arch_prctl(cpu_env, arg1, arg2);
  9728. #endif
  9729. #ifdef TARGET_NR_pread64
  9730. case TARGET_NR_pread64:
  9731. if (regpairs_aligned(cpu_env, num)) {
  9732. arg4 = arg5;
  9733. arg5 = arg6;
  9734. }
  9735. if (arg2 == 0 && arg3 == 0) {
  9736. /* Special-case NULL buffer and zero length, which should succeed */
  9737. p = 0;
  9738. } else {
  9739. p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  9740. if (!p) {
  9741. return -TARGET_EFAULT;
  9742. }
  9743. }
  9744. ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
  9745. unlock_user(p, arg2, ret);
  9746. return ret;
  9747. case TARGET_NR_pwrite64:
  9748. if (regpairs_aligned(cpu_env, num)) {
  9749. arg4 = arg5;
  9750. arg5 = arg6;
  9751. }
  9752. if (arg2 == 0 && arg3 == 0) {
  9753. /* Special-case NULL buffer and zero length, which should succeed */
  9754. p = 0;
  9755. } else {
  9756. p = lock_user(VERIFY_READ, arg2, arg3, 1);
  9757. if (!p) {
  9758. return -TARGET_EFAULT;
  9759. }
  9760. }
  9761. ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
  9762. unlock_user(p, arg2, 0);
  9763. return ret;
  9764. #endif
  9765. case TARGET_NR_getcwd:
  9766. if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
  9767. return -TARGET_EFAULT;
  9768. ret = get_errno(sys_getcwd1(p, arg2));
  9769. unlock_user(p, arg1, ret);
  9770. return ret;
  9771. case TARGET_NR_capget:
  9772. case TARGET_NR_capset:
  9773. {
  9774. struct target_user_cap_header *target_header;
  9775. struct target_user_cap_data *target_data = NULL;
  9776. struct __user_cap_header_struct header;
  9777. struct __user_cap_data_struct data[2];
  9778. struct __user_cap_data_struct *dataptr = NULL;
  9779. int i, target_datalen;
  9780. int data_items = 1;
  9781. if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) {
  9782. return -TARGET_EFAULT;
  9783. }
  9784. header.version = tswap32(target_header->version);
  9785. header.pid = tswap32(target_header->pid);
  9786. if (header.version != _LINUX_CAPABILITY_VERSION) {
  9787. /* Version 2 and up takes pointer to two user_data structs */
  9788. data_items = 2;
  9789. }
  9790. target_datalen = sizeof(*target_data) * data_items;
  9791. if (arg2) {
  9792. if (num == TARGET_NR_capget) {
  9793. target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0);
  9794. } else {
  9795. target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1);
  9796. }
  9797. if (!target_data) {
  9798. unlock_user_struct(target_header, arg1, 0);
  9799. return -TARGET_EFAULT;
  9800. }
  9801. if (num == TARGET_NR_capset) {
  9802. for (i = 0; i < data_items; i++) {
  9803. data[i].effective = tswap32(target_data[i].effective);
  9804. data[i].permitted = tswap32(target_data[i].permitted);
  9805. data[i].inheritable = tswap32(target_data[i].inheritable);
  9806. }
  9807. }
  9808. dataptr = data;
  9809. }
  9810. if (num == TARGET_NR_capget) {
  9811. ret = get_errno(capget(&header, dataptr));
  9812. } else {
  9813. ret = get_errno(capset(&header, dataptr));
  9814. }
  9815. /* The kernel always updates version for both capget and capset */
  9816. target_header->version = tswap32(header.version);
  9817. unlock_user_struct(target_header, arg1, 1);
  9818. if (arg2) {
  9819. if (num == TARGET_NR_capget) {
  9820. for (i = 0; i < data_items; i++) {
  9821. target_data[i].effective = tswap32(data[i].effective);
  9822. target_data[i].permitted = tswap32(data[i].permitted);
  9823. target_data[i].inheritable = tswap32(data[i].inheritable);
  9824. }
  9825. unlock_user(target_data, arg2, target_datalen);
  9826. } else {
  9827. unlock_user(target_data, arg2, 0);
  9828. }
  9829. }
  9830. return ret;
  9831. }
  9832. case TARGET_NR_sigaltstack:
  9833. return do_sigaltstack(arg1, arg2,
  9834. get_sp_from_cpustate((CPUArchState *)cpu_env));
  9835. #ifdef CONFIG_SENDFILE
  9836. #ifdef TARGET_NR_sendfile
  9837. case TARGET_NR_sendfile:
  9838. {
  9839. off_t *offp = NULL;
  9840. off_t off;
  9841. if (arg3) {
  9842. ret = get_user_sal(off, arg3);
  9843. if (is_error(ret)) {
  9844. return ret;
  9845. }
  9846. offp = &off;
  9847. }
  9848. ret = get_errno(sendfile(arg1, arg2, offp, arg4));
  9849. if (!is_error(ret) && arg3) {
  9850. abi_long ret2 = put_user_sal(off, arg3);
  9851. if (is_error(ret2)) {
  9852. ret = ret2;
  9853. }
  9854. }
  9855. return ret;
  9856. }
  9857. #endif
  9858. #ifdef TARGET_NR_sendfile64
  9859. case TARGET_NR_sendfile64:
  9860. {
  9861. off_t *offp = NULL;
  9862. off_t off;
  9863. if (arg3) {
  9864. ret = get_user_s64(off, arg3);
  9865. if (is_error(ret)) {
  9866. return ret;
  9867. }
  9868. offp = &off;
  9869. }
  9870. ret = get_errno(sendfile(arg1, arg2, offp, arg4));
  9871. if (!is_error(ret) && arg3) {
  9872. abi_long ret2 = put_user_s64(off, arg3);
  9873. if (is_error(ret2)) {
  9874. ret = ret2;
  9875. }
  9876. }
  9877. return ret;
  9878. }
  9879. #endif
  9880. #endif
  9881. #ifdef TARGET_NR_vfork
  9882. case TARGET_NR_vfork:
  9883. return get_errno(do_fork(cpu_env,
  9884. CLONE_VFORK | CLONE_VM | TARGET_SIGCHLD,
  9885. 0, 0, 0, 0));
  9886. #endif
  9887. #ifdef TARGET_NR_ugetrlimit
  9888. case TARGET_NR_ugetrlimit:
  9889. {
  9890. struct rlimit rlim;
  9891. int resource = target_to_host_resource(arg1);
  9892. ret = get_errno(getrlimit(resource, &rlim));
  9893. if (!is_error(ret)) {
  9894. struct target_rlimit *target_rlim;
  9895. if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
  9896. return -TARGET_EFAULT;
  9897. target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
  9898. target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
  9899. unlock_user_struct(target_rlim, arg2, 1);
  9900. }
  9901. return ret;
  9902. }
  9903. #endif
  9904. #ifdef TARGET_NR_truncate64
  9905. case TARGET_NR_truncate64:
  9906. if (!(p = lock_user_string(arg1)))
  9907. return -TARGET_EFAULT;
  9908. ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
  9909. unlock_user(p, arg1, 0);
  9910. return ret;
  9911. #endif
  9912. #ifdef TARGET_NR_ftruncate64
  9913. case TARGET_NR_ftruncate64:
  9914. return target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
  9915. #endif
  9916. #ifdef TARGET_NR_stat64
  9917. case TARGET_NR_stat64:
  9918. if (!(p = lock_user_string(arg1))) {
  9919. return -TARGET_EFAULT;
  9920. }
  9921. ret = get_errno(stat(path(p), &st));
  9922. unlock_user(p, arg1, 0);
  9923. if (!is_error(ret))
  9924. ret = host_to_target_stat64(cpu_env, arg2, &st);
  9925. return ret;
  9926. #endif
  9927. #ifdef TARGET_NR_lstat64
  9928. case TARGET_NR_lstat64:
  9929. if (!(p = lock_user_string(arg1))) {
  9930. return -TARGET_EFAULT;
  9931. }
  9932. ret = get_errno(lstat(path(p), &st));
  9933. unlock_user(p, arg1, 0);
  9934. if (!is_error(ret))
  9935. ret = host_to_target_stat64(cpu_env, arg2, &st);
  9936. return ret;
  9937. #endif
  9938. #ifdef TARGET_NR_fstat64
  9939. case TARGET_NR_fstat64:
  9940. ret = get_errno(fstat(arg1, &st));
  9941. if (!is_error(ret))
  9942. ret = host_to_target_stat64(cpu_env, arg2, &st);
  9943. return ret;
  9944. #endif
  9945. #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
  9946. #ifdef TARGET_NR_fstatat64
  9947. case TARGET_NR_fstatat64:
  9948. #endif
  9949. #ifdef TARGET_NR_newfstatat
  9950. case TARGET_NR_newfstatat:
  9951. #endif
  9952. if (!(p = lock_user_string(arg2))) {
  9953. return -TARGET_EFAULT;
  9954. }
  9955. ret = get_errno(fstatat(arg1, path(p), &st, arg4));
  9956. unlock_user(p, arg2, 0);
  9957. if (!is_error(ret))
  9958. ret = host_to_target_stat64(cpu_env, arg3, &st);
  9959. return ret;
  9960. #endif
  9961. #if defined(TARGET_NR_statx)
  9962. case TARGET_NR_statx:
  9963. {
  9964. struct target_statx *target_stx;
  9965. int dirfd = arg1;
  9966. int flags = arg3;
  9967. p = lock_user_string(arg2);
  9968. if (p == NULL) {
  9969. return -TARGET_EFAULT;
  9970. }
  9971. #if defined(__NR_statx)
  9972. {
  9973. /*
  9974. * It is assumed that struct statx is architecture independent.
  9975. */
  9976. struct target_statx host_stx;
  9977. int mask = arg4;
  9978. ret = get_errno(sys_statx(dirfd, p, flags, mask, &host_stx));
  9979. if (!is_error(ret)) {
  9980. if (host_to_target_statx(&host_stx, arg5) != 0) {
  9981. unlock_user(p, arg2, 0);
  9982. return -TARGET_EFAULT;
  9983. }
  9984. }
  9985. if (ret != -TARGET_ENOSYS) {
  9986. unlock_user(p, arg2, 0);
  9987. return ret;
  9988. }
  9989. }
  9990. #endif
  9991. ret = get_errno(fstatat(dirfd, path(p), &st, flags));
  9992. unlock_user(p, arg2, 0);
  9993. if (!is_error(ret)) {
  9994. if (!lock_user_struct(VERIFY_WRITE, target_stx, arg5, 0)) {
  9995. return -TARGET_EFAULT;
  9996. }
  9997. memset(target_stx, 0, sizeof(*target_stx));
  9998. __put_user(major(st.st_dev), &target_stx->stx_dev_major);
  9999. __put_user(minor(st.st_dev), &target_stx->stx_dev_minor);
  10000. __put_user(st.st_ino, &target_stx->stx_ino);
  10001. __put_user(st.st_mode, &target_stx->stx_mode);
  10002. __put_user(st.st_uid, &target_stx->stx_uid);
  10003. __put_user(st.st_gid, &target_stx->stx_gid);
  10004. __put_user(st.st_nlink, &target_stx->stx_nlink);
  10005. __put_user(major(st.st_rdev), &target_stx->stx_rdev_major);
  10006. __put_user(minor(st.st_rdev), &target_stx->stx_rdev_minor);
  10007. __put_user(st.st_size, &target_stx->stx_size);
  10008. __put_user(st.st_blksize, &target_stx->stx_blksize);
  10009. __put_user(st.st_blocks, &target_stx->stx_blocks);
  10010. __put_user(st.st_atime, &target_stx->stx_atime.tv_sec);
  10011. __put_user(st.st_mtime, &target_stx->stx_mtime.tv_sec);
  10012. __put_user(st.st_ctime, &target_stx->stx_ctime.tv_sec);
  10013. unlock_user_struct(target_stx, arg5, 1);
  10014. }
  10015. }
  10016. return ret;
  10017. #endif
  10018. #ifdef TARGET_NR_lchown
  10019. case TARGET_NR_lchown:
  10020. if (!(p = lock_user_string(arg1)))
  10021. return -TARGET_EFAULT;
  10022. ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
  10023. unlock_user(p, arg1, 0);
  10024. return ret;
  10025. #endif
  10026. #ifdef TARGET_NR_getuid
  10027. case TARGET_NR_getuid:
  10028. return get_errno(high2lowuid(getuid()));
  10029. #endif
  10030. #ifdef TARGET_NR_getgid
  10031. case TARGET_NR_getgid:
  10032. return get_errno(high2lowgid(getgid()));
  10033. #endif
  10034. #ifdef TARGET_NR_geteuid
  10035. case TARGET_NR_geteuid:
  10036. return get_errno(high2lowuid(geteuid()));
  10037. #endif
  10038. #ifdef TARGET_NR_getegid
  10039. case TARGET_NR_getegid:
  10040. return get_errno(high2lowgid(getegid()));
  10041. #endif
  10042. case TARGET_NR_setreuid:
  10043. return get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
  10044. case TARGET_NR_setregid:
  10045. return get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
  10046. case TARGET_NR_getgroups:
  10047. {
  10048. int gidsetsize = arg1;
  10049. target_id *target_grouplist;
  10050. gid_t *grouplist;
  10051. int i;
  10052. grouplist = alloca(gidsetsize * sizeof(gid_t));
  10053. ret = get_errno(getgroups(gidsetsize, grouplist));
  10054. if (gidsetsize == 0)
  10055. return ret;
  10056. if (!is_error(ret)) {
  10057. target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * sizeof(target_id), 0);
  10058. if (!target_grouplist)
  10059. return -TARGET_EFAULT;
  10060. for(i = 0;i < ret; i++)
  10061. target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
  10062. unlock_user(target_grouplist, arg2, gidsetsize * sizeof(target_id));
  10063. }
  10064. }
  10065. return ret;
  10066. case TARGET_NR_setgroups:
  10067. {
  10068. int gidsetsize = arg1;
  10069. target_id *target_grouplist;
  10070. gid_t *grouplist = NULL;
  10071. int i;
  10072. if (gidsetsize) {
  10073. grouplist = alloca(gidsetsize * sizeof(gid_t));
  10074. target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * sizeof(target_id), 1);
  10075. if (!target_grouplist) {
  10076. return -TARGET_EFAULT;
  10077. }
  10078. for (i = 0; i < gidsetsize; i++) {
  10079. grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
  10080. }
  10081. unlock_user(target_grouplist, arg2, 0);
  10082. }
  10083. return get_errno(setgroups(gidsetsize, grouplist));
  10084. }
  10085. case TARGET_NR_fchown:
  10086. return get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
  10087. #if defined(TARGET_NR_fchownat)
  10088. case TARGET_NR_fchownat:
  10089. if (!(p = lock_user_string(arg2)))
  10090. return -TARGET_EFAULT;
  10091. ret = get_errno(fchownat(arg1, p, low2highuid(arg3),
  10092. low2highgid(arg4), arg5));
  10093. unlock_user(p, arg2, 0);
  10094. return ret;
  10095. #endif
  10096. #ifdef TARGET_NR_setresuid
  10097. case TARGET_NR_setresuid:
  10098. return get_errno(sys_setresuid(low2highuid(arg1),
  10099. low2highuid(arg2),
  10100. low2highuid(arg3)));
  10101. #endif
  10102. #ifdef TARGET_NR_getresuid
  10103. case TARGET_NR_getresuid:
  10104. {
  10105. uid_t ruid, euid, suid;
  10106. ret = get_errno(getresuid(&ruid, &euid, &suid));
  10107. if (!is_error(ret)) {
  10108. if (put_user_id(high2lowuid(ruid), arg1)
  10109. || put_user_id(high2lowuid(euid), arg2)
  10110. || put_user_id(high2lowuid(suid), arg3))
  10111. return -TARGET_EFAULT;
  10112. }
  10113. }
  10114. return ret;
  10115. #endif
  10116. #ifdef TARGET_NR_getresgid
  10117. case TARGET_NR_setresgid:
  10118. return get_errno(sys_setresgid(low2highgid(arg1),
  10119. low2highgid(arg2),
  10120. low2highgid(arg3)));
  10121. #endif
  10122. #ifdef TARGET_NR_getresgid
  10123. case TARGET_NR_getresgid:
  10124. {
  10125. gid_t rgid, egid, sgid;
  10126. ret = get_errno(getresgid(&rgid, &egid, &sgid));
  10127. if (!is_error(ret)) {
  10128. if (put_user_id(high2lowgid(rgid), arg1)
  10129. || put_user_id(high2lowgid(egid), arg2)
  10130. || put_user_id(high2lowgid(sgid), arg3))
  10131. return -TARGET_EFAULT;
  10132. }
  10133. }
  10134. return ret;
  10135. #endif
  10136. #ifdef TARGET_NR_chown
  10137. case TARGET_NR_chown:
  10138. if (!(p = lock_user_string(arg1)))
  10139. return -TARGET_EFAULT;
  10140. ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
  10141. unlock_user(p, arg1, 0);
  10142. return ret;
  10143. #endif
  10144. case TARGET_NR_setuid:
  10145. return get_errno(sys_setuid(low2highuid(arg1)));
  10146. case TARGET_NR_setgid:
  10147. return get_errno(sys_setgid(low2highgid(arg1)));
  10148. case TARGET_NR_setfsuid:
  10149. return get_errno(setfsuid(arg1));
  10150. case TARGET_NR_setfsgid:
  10151. return get_errno(setfsgid(arg1));
  10152. #ifdef TARGET_NR_lchown32
  10153. case TARGET_NR_lchown32:
  10154. if (!(p = lock_user_string(arg1)))
  10155. return -TARGET_EFAULT;
  10156. ret = get_errno(lchown(p, arg2, arg3));
  10157. unlock_user(p, arg1, 0);
  10158. return ret;
  10159. #endif
  10160. #ifdef TARGET_NR_getuid32
  10161. case TARGET_NR_getuid32:
  10162. return get_errno(getuid());
  10163. #endif
  10164. #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
  10165. /* Alpha specific */
  10166. case TARGET_NR_getxuid:
  10167. {
  10168. uid_t euid;
  10169. euid=geteuid();
  10170. ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
  10171. }
  10172. return get_errno(getuid());
  10173. #endif
  10174. #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
  10175. /* Alpha specific */
  10176. case TARGET_NR_getxgid:
  10177. {
  10178. uid_t egid;
  10179. egid=getegid();
  10180. ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
  10181. }
  10182. return get_errno(getgid());
  10183. #endif
  10184. #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
  10185. /* Alpha specific */
  10186. case TARGET_NR_osf_getsysinfo:
  10187. ret = -TARGET_EOPNOTSUPP;
  10188. switch (arg1) {
  10189. case TARGET_GSI_IEEE_FP_CONTROL:
  10190. {
  10191. uint64_t fpcr = cpu_alpha_load_fpcr(cpu_env);
  10192. uint64_t swcr = ((CPUAlphaState *)cpu_env)->swcr;
  10193. swcr &= ~SWCR_STATUS_MASK;
  10194. swcr |= (fpcr >> 35) & SWCR_STATUS_MASK;
  10195. if (put_user_u64 (swcr, arg2))
  10196. return -TARGET_EFAULT;
  10197. ret = 0;
  10198. }
  10199. break;
  10200. /* case GSI_IEEE_STATE_AT_SIGNAL:
  10201. -- Not implemented in linux kernel.
  10202. case GSI_UACPROC:
  10203. -- Retrieves current unaligned access state; not much used.
  10204. case GSI_PROC_TYPE:
  10205. -- Retrieves implver information; surely not used.
  10206. case GSI_GET_HWRPB:
  10207. -- Grabs a copy of the HWRPB; surely not used.
  10208. */
  10209. }
  10210. return ret;
  10211. #endif
  10212. #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
  10213. /* Alpha specific */
  10214. case TARGET_NR_osf_setsysinfo:
  10215. ret = -TARGET_EOPNOTSUPP;
  10216. switch (arg1) {
  10217. case TARGET_SSI_IEEE_FP_CONTROL:
  10218. {
  10219. uint64_t swcr, fpcr;
  10220. if (get_user_u64 (swcr, arg2)) {
  10221. return -TARGET_EFAULT;
  10222. }
  10223. /*
  10224. * The kernel calls swcr_update_status to update the
  10225. * status bits from the fpcr at every point that it
  10226. * could be queried. Therefore, we store the status
  10227. * bits only in FPCR.
  10228. */
  10229. ((CPUAlphaState *)cpu_env)->swcr
  10230. = swcr & (SWCR_TRAP_ENABLE_MASK | SWCR_MAP_MASK);
  10231. fpcr = cpu_alpha_load_fpcr(cpu_env);
  10232. fpcr &= ((uint64_t)FPCR_DYN_MASK << 32);
  10233. fpcr |= alpha_ieee_swcr_to_fpcr(swcr);
  10234. cpu_alpha_store_fpcr(cpu_env, fpcr);
  10235. ret = 0;
  10236. }
  10237. break;
  10238. case TARGET_SSI_IEEE_RAISE_EXCEPTION:
  10239. {
  10240. uint64_t exc, fpcr, fex;
  10241. if (get_user_u64(exc, arg2)) {
  10242. return -TARGET_EFAULT;
  10243. }
  10244. exc &= SWCR_STATUS_MASK;
  10245. fpcr = cpu_alpha_load_fpcr(cpu_env);
  10246. /* Old exceptions are not signaled. */
  10247. fex = alpha_ieee_fpcr_to_swcr(fpcr);
  10248. fex = exc & ~fex;
  10249. fex >>= SWCR_STATUS_TO_EXCSUM_SHIFT;
  10250. fex &= ((CPUArchState *)cpu_env)->swcr;
  10251. /* Update the hardware fpcr. */
  10252. fpcr |= alpha_ieee_swcr_to_fpcr(exc);
  10253. cpu_alpha_store_fpcr(cpu_env, fpcr);
  10254. if (fex) {
  10255. int si_code = TARGET_FPE_FLTUNK;
  10256. target_siginfo_t info;
  10257. if (fex & SWCR_TRAP_ENABLE_DNO) {
  10258. si_code = TARGET_FPE_FLTUND;
  10259. }
  10260. if (fex & SWCR_TRAP_ENABLE_INE) {
  10261. si_code = TARGET_FPE_FLTRES;
  10262. }
  10263. if (fex & SWCR_TRAP_ENABLE_UNF) {
  10264. si_code = TARGET_FPE_FLTUND;
  10265. }
  10266. if (fex & SWCR_TRAP_ENABLE_OVF) {
  10267. si_code = TARGET_FPE_FLTOVF;
  10268. }
  10269. if (fex & SWCR_TRAP_ENABLE_DZE) {
  10270. si_code = TARGET_FPE_FLTDIV;
  10271. }
  10272. if (fex & SWCR_TRAP_ENABLE_INV) {
  10273. si_code = TARGET_FPE_FLTINV;
  10274. }
  10275. info.si_signo = SIGFPE;
  10276. info.si_errno = 0;
  10277. info.si_code = si_code;
  10278. info._sifields._sigfault._addr
  10279. = ((CPUArchState *)cpu_env)->pc;
  10280. queue_signal((CPUArchState *)cpu_env, info.si_signo,
  10281. QEMU_SI_FAULT, &info);
  10282. }
  10283. ret = 0;
  10284. }
  10285. break;
  10286. /* case SSI_NVPAIRS:
  10287. -- Used with SSIN_UACPROC to enable unaligned accesses.
  10288. case SSI_IEEE_STATE_AT_SIGNAL:
  10289. case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
  10290. -- Not implemented in linux kernel
  10291. */
  10292. }
  10293. return ret;
  10294. #endif
  10295. #ifdef TARGET_NR_osf_sigprocmask
  10296. /* Alpha specific. */
  10297. case TARGET_NR_osf_sigprocmask:
  10298. {
  10299. abi_ulong mask;
  10300. int how;
  10301. sigset_t set, oldset;
  10302. switch(arg1) {
  10303. case TARGET_SIG_BLOCK:
  10304. how = SIG_BLOCK;
  10305. break;
  10306. case TARGET_SIG_UNBLOCK:
  10307. how = SIG_UNBLOCK;
  10308. break;
  10309. case TARGET_SIG_SETMASK:
  10310. how = SIG_SETMASK;
  10311. break;
  10312. default:
  10313. return -TARGET_EINVAL;
  10314. }
  10315. mask = arg2;
  10316. target_to_host_old_sigset(&set, &mask);
  10317. ret = do_sigprocmask(how, &set, &oldset);
  10318. if (!ret) {
  10319. host_to_target_old_sigset(&mask, &oldset);
  10320. ret = mask;
  10321. }
  10322. }
  10323. return ret;
  10324. #endif
  10325. #ifdef TARGET_NR_getgid32
  10326. case TARGET_NR_getgid32:
  10327. return get_errno(getgid());
  10328. #endif
  10329. #ifdef TARGET_NR_geteuid32
  10330. case TARGET_NR_geteuid32:
  10331. return get_errno(geteuid());
  10332. #endif
  10333. #ifdef TARGET_NR_getegid32
  10334. case TARGET_NR_getegid32:
  10335. return get_errno(getegid());
  10336. #endif
  10337. #ifdef TARGET_NR_setreuid32
  10338. case TARGET_NR_setreuid32:
  10339. return get_errno(setreuid(arg1, arg2));
  10340. #endif
  10341. #ifdef TARGET_NR_setregid32
  10342. case TARGET_NR_setregid32:
  10343. return get_errno(setregid(arg1, arg2));
  10344. #endif
  10345. #ifdef TARGET_NR_getgroups32
  10346. case TARGET_NR_getgroups32:
  10347. {
  10348. int gidsetsize = arg1;
  10349. uint32_t *target_grouplist;
  10350. gid_t *grouplist;
  10351. int i;
  10352. grouplist = alloca(gidsetsize * sizeof(gid_t));
  10353. ret = get_errno(getgroups(gidsetsize, grouplist));
  10354. if (gidsetsize == 0)
  10355. return ret;
  10356. if (!is_error(ret)) {
  10357. target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
  10358. if (!target_grouplist) {
  10359. return -TARGET_EFAULT;
  10360. }
  10361. for(i = 0;i < ret; i++)
  10362. target_grouplist[i] = tswap32(grouplist[i]);
  10363. unlock_user(target_grouplist, arg2, gidsetsize * 4);
  10364. }
  10365. }
  10366. return ret;
  10367. #endif
  10368. #ifdef TARGET_NR_setgroups32
  10369. case TARGET_NR_setgroups32:
  10370. {
  10371. int gidsetsize = arg1;
  10372. uint32_t *target_grouplist;
  10373. gid_t *grouplist;
  10374. int i;
  10375. grouplist = alloca(gidsetsize * sizeof(gid_t));
  10376. target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
  10377. if (!target_grouplist) {
  10378. return -TARGET_EFAULT;
  10379. }
  10380. for(i = 0;i < gidsetsize; i++)
  10381. grouplist[i] = tswap32(target_grouplist[i]);
  10382. unlock_user(target_grouplist, arg2, 0);
  10383. return get_errno(setgroups(gidsetsize, grouplist));
  10384. }
  10385. #endif
  10386. #ifdef TARGET_NR_fchown32
  10387. case TARGET_NR_fchown32:
  10388. return get_errno(fchown(arg1, arg2, arg3));
  10389. #endif
  10390. #ifdef TARGET_NR_setresuid32
  10391. case TARGET_NR_setresuid32:
  10392. return get_errno(sys_setresuid(arg1, arg2, arg3));
  10393. #endif
  10394. #ifdef TARGET_NR_getresuid32
  10395. case TARGET_NR_getresuid32:
  10396. {
  10397. uid_t ruid, euid, suid;
  10398. ret = get_errno(getresuid(&ruid, &euid, &suid));
  10399. if (!is_error(ret)) {
  10400. if (put_user_u32(ruid, arg1)
  10401. || put_user_u32(euid, arg2)
  10402. || put_user_u32(suid, arg3))
  10403. return -TARGET_EFAULT;
  10404. }
  10405. }
  10406. return ret;
  10407. #endif
  10408. #ifdef TARGET_NR_setresgid32
  10409. case TARGET_NR_setresgid32:
  10410. return get_errno(sys_setresgid(arg1, arg2, arg3));
  10411. #endif
  10412. #ifdef TARGET_NR_getresgid32
  10413. case TARGET_NR_getresgid32:
  10414. {
  10415. gid_t rgid, egid, sgid;
  10416. ret = get_errno(getresgid(&rgid, &egid, &sgid));
  10417. if (!is_error(ret)) {
  10418. if (put_user_u32(rgid, arg1)
  10419. || put_user_u32(egid, arg2)
  10420. || put_user_u32(sgid, arg3))
  10421. return -TARGET_EFAULT;
  10422. }
  10423. }
  10424. return ret;
  10425. #endif
  10426. #ifdef TARGET_NR_chown32
  10427. case TARGET_NR_chown32:
  10428. if (!(p = lock_user_string(arg1)))
  10429. return -TARGET_EFAULT;
  10430. ret = get_errno(chown(p, arg2, arg3));
  10431. unlock_user(p, arg1, 0);
  10432. return ret;
  10433. #endif
  10434. #ifdef TARGET_NR_setuid32
  10435. case TARGET_NR_setuid32:
  10436. return get_errno(sys_setuid(arg1));
  10437. #endif
  10438. #ifdef TARGET_NR_setgid32
  10439. case TARGET_NR_setgid32:
  10440. return get_errno(sys_setgid(arg1));
  10441. #endif
  10442. #ifdef TARGET_NR_setfsuid32
  10443. case TARGET_NR_setfsuid32:
  10444. return get_errno(setfsuid(arg1));
  10445. #endif
  10446. #ifdef TARGET_NR_setfsgid32
  10447. case TARGET_NR_setfsgid32:
  10448. return get_errno(setfsgid(arg1));
  10449. #endif
  10450. #ifdef TARGET_NR_mincore
  10451. case TARGET_NR_mincore:
  10452. {
  10453. void *a = lock_user(VERIFY_READ, arg1, arg2, 0);
  10454. if (!a) {
  10455. return -TARGET_ENOMEM;
  10456. }
  10457. p = lock_user_string(arg3);
  10458. if (!p) {
  10459. ret = -TARGET_EFAULT;
  10460. } else {
  10461. ret = get_errno(mincore(a, arg2, p));
  10462. unlock_user(p, arg3, ret);
  10463. }
  10464. unlock_user(a, arg1, 0);
  10465. }
  10466. return ret;
  10467. #endif
  10468. #ifdef TARGET_NR_arm_fadvise64_64
  10469. case TARGET_NR_arm_fadvise64_64:
  10470. /* arm_fadvise64_64 looks like fadvise64_64 but
  10471. * with different argument order: fd, advice, offset, len
  10472. * rather than the usual fd, offset, len, advice.
  10473. * Note that offset and len are both 64-bit so appear as
  10474. * pairs of 32-bit registers.
  10475. */
  10476. ret = posix_fadvise(arg1, target_offset64(arg3, arg4),
  10477. target_offset64(arg5, arg6), arg2);
  10478. return -host_to_target_errno(ret);
  10479. #endif
  10480. #if TARGET_ABI_BITS == 32
  10481. #ifdef TARGET_NR_fadvise64_64
  10482. case TARGET_NR_fadvise64_64:
  10483. #if defined(TARGET_PPC) || defined(TARGET_XTENSA)
  10484. /* 6 args: fd, advice, offset (high, low), len (high, low) */
  10485. ret = arg2;
  10486. arg2 = arg3;
  10487. arg3 = arg4;
  10488. arg4 = arg5;
  10489. arg5 = arg6;
  10490. arg6 = ret;
  10491. #else
  10492. /* 6 args: fd, offset (high, low), len (high, low), advice */
  10493. if (regpairs_aligned(cpu_env, num)) {
  10494. /* offset is in (3,4), len in (5,6) and advice in 7 */
  10495. arg2 = arg3;
  10496. arg3 = arg4;
  10497. arg4 = arg5;
  10498. arg5 = arg6;
  10499. arg6 = arg7;
  10500. }
  10501. #endif
  10502. ret = posix_fadvise(arg1, target_offset64(arg2, arg3),
  10503. target_offset64(arg4, arg5), arg6);
  10504. return -host_to_target_errno(ret);
  10505. #endif
  10506. #ifdef TARGET_NR_fadvise64
  10507. case TARGET_NR_fadvise64:
  10508. /* 5 args: fd, offset (high, low), len, advice */
  10509. if (regpairs_aligned(cpu_env, num)) {
  10510. /* offset is in (3,4), len in 5 and advice in 6 */
  10511. arg2 = arg3;
  10512. arg3 = arg4;
  10513. arg4 = arg5;
  10514. arg5 = arg6;
  10515. }
  10516. ret = posix_fadvise(arg1, target_offset64(arg2, arg3), arg4, arg5);
  10517. return -host_to_target_errno(ret);
  10518. #endif
  10519. #else /* not a 32-bit ABI */
  10520. #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_fadvise64)
  10521. #ifdef TARGET_NR_fadvise64_64
  10522. case TARGET_NR_fadvise64_64:
  10523. #endif
  10524. #ifdef TARGET_NR_fadvise64
  10525. case TARGET_NR_fadvise64:
  10526. #endif
  10527. #ifdef TARGET_S390X
  10528. switch (arg4) {
  10529. case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
  10530. case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
  10531. case 6: arg4 = POSIX_FADV_DONTNEED; break;
  10532. case 7: arg4 = POSIX_FADV_NOREUSE; break;
  10533. default: break;
  10534. }
  10535. #endif
  10536. return -host_to_target_errno(posix_fadvise(arg1, arg2, arg3, arg4));
  10537. #endif
  10538. #endif /* end of 64-bit ABI fadvise handling */
  10539. #ifdef TARGET_NR_madvise
  10540. case TARGET_NR_madvise:
  10541. /* A straight passthrough may not be safe because qemu sometimes
  10542. turns private file-backed mappings into anonymous mappings.
  10543. This will break MADV_DONTNEED.
  10544. This is a hint, so ignoring and returning success is ok. */
  10545. return 0;
  10546. #endif
  10547. #ifdef TARGET_NR_fcntl64
  10548. case TARGET_NR_fcntl64:
  10549. {
  10550. int cmd;
  10551. struct flock64 fl;
  10552. from_flock64_fn *copyfrom = copy_from_user_flock64;
  10553. to_flock64_fn *copyto = copy_to_user_flock64;
  10554. #ifdef TARGET_ARM
  10555. if (!((CPUARMState *)cpu_env)->eabi) {
  10556. copyfrom = copy_from_user_oabi_flock64;
  10557. copyto = copy_to_user_oabi_flock64;
  10558. }
  10559. #endif
  10560. cmd = target_to_host_fcntl_cmd(arg2);
  10561. if (cmd == -TARGET_EINVAL) {
  10562. return cmd;
  10563. }
  10564. switch(arg2) {
  10565. case TARGET_F_GETLK64:
  10566. ret = copyfrom(&fl, arg3);
  10567. if (ret) {
  10568. break;
  10569. }
  10570. ret = get_errno(safe_fcntl(arg1, cmd, &fl));
  10571. if (ret == 0) {
  10572. ret = copyto(arg3, &fl);
  10573. }
  10574. break;
  10575. case TARGET_F_SETLK64:
  10576. case TARGET_F_SETLKW64:
  10577. ret = copyfrom(&fl, arg3);
  10578. if (ret) {
  10579. break;
  10580. }
  10581. ret = get_errno(safe_fcntl(arg1, cmd, &fl));
  10582. break;
  10583. default:
  10584. ret = do_fcntl(arg1, arg2, arg3);
  10585. break;
  10586. }
  10587. return ret;
  10588. }
  10589. #endif
  10590. #ifdef TARGET_NR_cacheflush
  10591. case TARGET_NR_cacheflush:
  10592. /* self-modifying code is handled automatically, so nothing needed */
  10593. return 0;
  10594. #endif
  10595. #ifdef TARGET_NR_getpagesize
  10596. case TARGET_NR_getpagesize:
  10597. return TARGET_PAGE_SIZE;
  10598. #endif
  10599. case TARGET_NR_gettid:
  10600. return get_errno(sys_gettid());
  10601. #ifdef TARGET_NR_readahead
  10602. case TARGET_NR_readahead:
  10603. #if TARGET_ABI_BITS == 32
  10604. if (regpairs_aligned(cpu_env, num)) {
  10605. arg2 = arg3;
  10606. arg3 = arg4;
  10607. arg4 = arg5;
  10608. }
  10609. ret = get_errno(readahead(arg1, target_offset64(arg2, arg3) , arg4));
  10610. #else
  10611. ret = get_errno(readahead(arg1, arg2, arg3));
  10612. #endif
  10613. return ret;
  10614. #endif
  10615. #ifdef CONFIG_ATTR
  10616. #ifdef TARGET_NR_setxattr
  10617. case TARGET_NR_listxattr:
  10618. case TARGET_NR_llistxattr:
  10619. {
  10620. void *p, *b = 0;
  10621. if (arg2) {
  10622. b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  10623. if (!b) {
  10624. return -TARGET_EFAULT;
  10625. }
  10626. }
  10627. p = lock_user_string(arg1);
  10628. if (p) {
  10629. if (num == TARGET_NR_listxattr) {
  10630. ret = get_errno(listxattr(p, b, arg3));
  10631. } else {
  10632. ret = get_errno(llistxattr(p, b, arg3));
  10633. }
  10634. } else {
  10635. ret = -TARGET_EFAULT;
  10636. }
  10637. unlock_user(p, arg1, 0);
  10638. unlock_user(b, arg2, arg3);
  10639. return ret;
  10640. }
  10641. case TARGET_NR_flistxattr:
  10642. {
  10643. void *b = 0;
  10644. if (arg2) {
  10645. b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  10646. if (!b) {
  10647. return -TARGET_EFAULT;
  10648. }
  10649. }
  10650. ret = get_errno(flistxattr(arg1, b, arg3));
  10651. unlock_user(b, arg2, arg3);
  10652. return ret;
  10653. }
  10654. case TARGET_NR_setxattr:
  10655. case TARGET_NR_lsetxattr:
  10656. {
  10657. void *p, *n, *v = 0;
  10658. if (arg3) {
  10659. v = lock_user(VERIFY_READ, arg3, arg4, 1);
  10660. if (!v) {
  10661. return -TARGET_EFAULT;
  10662. }
  10663. }
  10664. p = lock_user_string(arg1);
  10665. n = lock_user_string(arg2);
  10666. if (p && n) {
  10667. if (num == TARGET_NR_setxattr) {
  10668. ret = get_errno(setxattr(p, n, v, arg4, arg5));
  10669. } else {
  10670. ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
  10671. }
  10672. } else {
  10673. ret = -TARGET_EFAULT;
  10674. }
  10675. unlock_user(p, arg1, 0);
  10676. unlock_user(n, arg2, 0);
  10677. unlock_user(v, arg3, 0);
  10678. }
  10679. return ret;
  10680. case TARGET_NR_fsetxattr:
  10681. {
  10682. void *n, *v = 0;
  10683. if (arg3) {
  10684. v = lock_user(VERIFY_READ, arg3, arg4, 1);
  10685. if (!v) {
  10686. return -TARGET_EFAULT;
  10687. }
  10688. }
  10689. n = lock_user_string(arg2);
  10690. if (n) {
  10691. ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
  10692. } else {
  10693. ret = -TARGET_EFAULT;
  10694. }
  10695. unlock_user(n, arg2, 0);
  10696. unlock_user(v, arg3, 0);
  10697. }
  10698. return ret;
  10699. case TARGET_NR_getxattr:
  10700. case TARGET_NR_lgetxattr:
  10701. {
  10702. void *p, *n, *v = 0;
  10703. if (arg3) {
  10704. v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  10705. if (!v) {
  10706. return -TARGET_EFAULT;
  10707. }
  10708. }
  10709. p = lock_user_string(arg1);
  10710. n = lock_user_string(arg2);
  10711. if (p && n) {
  10712. if (num == TARGET_NR_getxattr) {
  10713. ret = get_errno(getxattr(p, n, v, arg4));
  10714. } else {
  10715. ret = get_errno(lgetxattr(p, n, v, arg4));
  10716. }
  10717. } else {
  10718. ret = -TARGET_EFAULT;
  10719. }
  10720. unlock_user(p, arg1, 0);
  10721. unlock_user(n, arg2, 0);
  10722. unlock_user(v, arg3, arg4);
  10723. }
  10724. return ret;
  10725. case TARGET_NR_fgetxattr:
  10726. {
  10727. void *n, *v = 0;
  10728. if (arg3) {
  10729. v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  10730. if (!v) {
  10731. return -TARGET_EFAULT;
  10732. }
  10733. }
  10734. n = lock_user_string(arg2);
  10735. if (n) {
  10736. ret = get_errno(fgetxattr(arg1, n, v, arg4));
  10737. } else {
  10738. ret = -TARGET_EFAULT;
  10739. }
  10740. unlock_user(n, arg2, 0);
  10741. unlock_user(v, arg3, arg4);
  10742. }
  10743. return ret;
  10744. case TARGET_NR_removexattr:
  10745. case TARGET_NR_lremovexattr:
  10746. {
  10747. void *p, *n;
  10748. p = lock_user_string(arg1);
  10749. n = lock_user_string(arg2);
  10750. if (p && n) {
  10751. if (num == TARGET_NR_removexattr) {
  10752. ret = get_errno(removexattr(p, n));
  10753. } else {
  10754. ret = get_errno(lremovexattr(p, n));
  10755. }
  10756. } else {
  10757. ret = -TARGET_EFAULT;
  10758. }
  10759. unlock_user(p, arg1, 0);
  10760. unlock_user(n, arg2, 0);
  10761. }
  10762. return ret;
  10763. case TARGET_NR_fremovexattr:
  10764. {
  10765. void *n;
  10766. n = lock_user_string(arg2);
  10767. if (n) {
  10768. ret = get_errno(fremovexattr(arg1, n));
  10769. } else {
  10770. ret = -TARGET_EFAULT;
  10771. }
  10772. unlock_user(n, arg2, 0);
  10773. }
  10774. return ret;
  10775. #endif
  10776. #endif /* CONFIG_ATTR */
  10777. #ifdef TARGET_NR_set_thread_area
  10778. case TARGET_NR_set_thread_area:
  10779. #if defined(TARGET_MIPS)
  10780. ((CPUMIPSState *) cpu_env)->active_tc.CP0_UserLocal = arg1;
  10781. return 0;
  10782. #elif defined(TARGET_CRIS)
  10783. if (arg1 & 0xff)
  10784. ret = -TARGET_EINVAL;
  10785. else {
  10786. ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
  10787. ret = 0;
  10788. }
  10789. return ret;
  10790. #elif defined(TARGET_I386) && defined(TARGET_ABI32)
  10791. return do_set_thread_area(cpu_env, arg1);
  10792. #elif defined(TARGET_M68K)
  10793. {
  10794. TaskState *ts = cpu->opaque;
  10795. ts->tp_value = arg1;
  10796. return 0;
  10797. }
  10798. #else
  10799. return -TARGET_ENOSYS;
  10800. #endif
  10801. #endif
  10802. #ifdef TARGET_NR_get_thread_area
  10803. case TARGET_NR_get_thread_area:
  10804. #if defined(TARGET_I386) && defined(TARGET_ABI32)
  10805. return do_get_thread_area(cpu_env, arg1);
  10806. #elif defined(TARGET_M68K)
  10807. {
  10808. TaskState *ts = cpu->opaque;
  10809. return ts->tp_value;
  10810. }
  10811. #else
  10812. return -TARGET_ENOSYS;
  10813. #endif
  10814. #endif
  10815. #ifdef TARGET_NR_getdomainname
  10816. case TARGET_NR_getdomainname:
  10817. return -TARGET_ENOSYS;
  10818. #endif
  10819. #ifdef TARGET_NR_clock_settime
  10820. case TARGET_NR_clock_settime:
  10821. {
  10822. struct timespec ts;
  10823. ret = target_to_host_timespec(&ts, arg2);
  10824. if (!is_error(ret)) {
  10825. ret = get_errno(clock_settime(arg1, &ts));
  10826. }
  10827. return ret;
  10828. }
  10829. #endif
  10830. #ifdef TARGET_NR_clock_settime64
  10831. case TARGET_NR_clock_settime64:
  10832. {
  10833. struct timespec ts;
  10834. ret = target_to_host_timespec64(&ts, arg2);
  10835. if (!is_error(ret)) {
  10836. ret = get_errno(clock_settime(arg1, &ts));
  10837. }
  10838. return ret;
  10839. }
  10840. #endif
  10841. #ifdef TARGET_NR_clock_gettime
  10842. case TARGET_NR_clock_gettime:
  10843. {
  10844. struct timespec ts;
  10845. ret = get_errno(clock_gettime(arg1, &ts));
  10846. if (!is_error(ret)) {
  10847. ret = host_to_target_timespec(arg2, &ts);
  10848. }
  10849. return ret;
  10850. }
  10851. #endif
  10852. #ifdef TARGET_NR_clock_gettime64
  10853. case TARGET_NR_clock_gettime64:
  10854. {
  10855. struct timespec ts;
  10856. ret = get_errno(clock_gettime(arg1, &ts));
  10857. if (!is_error(ret)) {
  10858. ret = host_to_target_timespec64(arg2, &ts);
  10859. }
  10860. return ret;
  10861. }
  10862. #endif
  10863. #ifdef TARGET_NR_clock_getres
  10864. case TARGET_NR_clock_getres:
  10865. {
  10866. struct timespec ts;
  10867. ret = get_errno(clock_getres(arg1, &ts));
  10868. if (!is_error(ret)) {
  10869. host_to_target_timespec(arg2, &ts);
  10870. }
  10871. return ret;
  10872. }
  10873. #endif
  10874. #ifdef TARGET_NR_clock_nanosleep
  10875. case TARGET_NR_clock_nanosleep:
  10876. {
  10877. struct timespec ts;
  10878. target_to_host_timespec(&ts, arg3);
  10879. ret = get_errno(safe_clock_nanosleep(arg1, arg2,
  10880. &ts, arg4 ? &ts : NULL));
  10881. /*
  10882. * if the call is interrupted by a signal handler, it fails
  10883. * with error -TARGET_EINTR and if arg4 is not NULL and arg2 is not
  10884. * TIMER_ABSTIME, it returns the remaining unslept time in arg4.
  10885. */
  10886. if (ret == -TARGET_EINTR && arg4 && arg2 != TIMER_ABSTIME) {
  10887. host_to_target_timespec(arg4, &ts);
  10888. }
  10889. return ret;
  10890. }
  10891. #endif
  10892. #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
  10893. case TARGET_NR_set_tid_address:
  10894. return get_errno(set_tid_address((int *)g2h(arg1)));
  10895. #endif
  10896. case TARGET_NR_tkill:
  10897. return get_errno(safe_tkill((int)arg1, target_to_host_signal(arg2)));
  10898. case TARGET_NR_tgkill:
  10899. return get_errno(safe_tgkill((int)arg1, (int)arg2,
  10900. target_to_host_signal(arg3)));
  10901. #ifdef TARGET_NR_set_robust_list
  10902. case TARGET_NR_set_robust_list:
  10903. case TARGET_NR_get_robust_list:
  10904. /* The ABI for supporting robust futexes has userspace pass
  10905. * the kernel a pointer to a linked list which is updated by
  10906. * userspace after the syscall; the list is walked by the kernel
  10907. * when the thread exits. Since the linked list in QEMU guest
  10908. * memory isn't a valid linked list for the host and we have
  10909. * no way to reliably intercept the thread-death event, we can't
  10910. * support these. Silently return ENOSYS so that guest userspace
  10911. * falls back to a non-robust futex implementation (which should
  10912. * be OK except in the corner case of the guest crashing while
  10913. * holding a mutex that is shared with another process via
  10914. * shared memory).
  10915. */
  10916. return -TARGET_ENOSYS;
  10917. #endif
  10918. #if defined(TARGET_NR_utimensat)
  10919. case TARGET_NR_utimensat:
  10920. {
  10921. struct timespec *tsp, ts[2];
  10922. if (!arg3) {
  10923. tsp = NULL;
  10924. } else {
  10925. target_to_host_timespec(ts, arg3);
  10926. target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
  10927. tsp = ts;
  10928. }
  10929. if (!arg2)
  10930. ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
  10931. else {
  10932. if (!(p = lock_user_string(arg2))) {
  10933. return -TARGET_EFAULT;
  10934. }
  10935. ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
  10936. unlock_user(p, arg2, 0);
  10937. }
  10938. }
  10939. return ret;
  10940. #endif
  10941. #ifdef TARGET_NR_futex
  10942. case TARGET_NR_futex:
  10943. return do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
  10944. #endif
  10945. #ifdef TARGET_NR_futex_time64
  10946. case TARGET_NR_futex_time64:
  10947. return do_futex_time64(arg1, arg2, arg3, arg4, arg5, arg6);
  10948. #endif
  10949. #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
  10950. case TARGET_NR_inotify_init:
  10951. ret = get_errno(sys_inotify_init());
  10952. if (ret >= 0) {
  10953. fd_trans_register(ret, &target_inotify_trans);
  10954. }
  10955. return ret;
  10956. #endif
  10957. #ifdef CONFIG_INOTIFY1
  10958. #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
  10959. case TARGET_NR_inotify_init1:
  10960. ret = get_errno(sys_inotify_init1(target_to_host_bitmask(arg1,
  10961. fcntl_flags_tbl)));
  10962. if (ret >= 0) {
  10963. fd_trans_register(ret, &target_inotify_trans);
  10964. }
  10965. return ret;
  10966. #endif
  10967. #endif
  10968. #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
  10969. case TARGET_NR_inotify_add_watch:
  10970. p = lock_user_string(arg2);
  10971. ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
  10972. unlock_user(p, arg2, 0);
  10973. return ret;
  10974. #endif
  10975. #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
  10976. case TARGET_NR_inotify_rm_watch:
  10977. return get_errno(sys_inotify_rm_watch(arg1, arg2));
  10978. #endif
  10979. #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
  10980. case TARGET_NR_mq_open:
  10981. {
  10982. struct mq_attr posix_mq_attr;
  10983. struct mq_attr *pposix_mq_attr;
  10984. int host_flags;
  10985. host_flags = target_to_host_bitmask(arg2, fcntl_flags_tbl);
  10986. pposix_mq_attr = NULL;
  10987. if (arg4) {
  10988. if (copy_from_user_mq_attr(&posix_mq_attr, arg4) != 0) {
  10989. return -TARGET_EFAULT;
  10990. }
  10991. pposix_mq_attr = &posix_mq_attr;
  10992. }
  10993. p = lock_user_string(arg1 - 1);
  10994. if (!p) {
  10995. return -TARGET_EFAULT;
  10996. }
  10997. ret = get_errno(mq_open(p, host_flags, arg3, pposix_mq_attr));
  10998. unlock_user (p, arg1, 0);
  10999. }
  11000. return ret;
  11001. case TARGET_NR_mq_unlink:
  11002. p = lock_user_string(arg1 - 1);
  11003. if (!p) {
  11004. return -TARGET_EFAULT;
  11005. }
  11006. ret = get_errno(mq_unlink(p));
  11007. unlock_user (p, arg1, 0);
  11008. return ret;
  11009. #ifdef TARGET_NR_mq_timedsend
  11010. case TARGET_NR_mq_timedsend:
  11011. {
  11012. struct timespec ts;
  11013. p = lock_user (VERIFY_READ, arg2, arg3, 1);
  11014. if (arg5 != 0) {
  11015. target_to_host_timespec(&ts, arg5);
  11016. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts));
  11017. host_to_target_timespec(arg5, &ts);
  11018. } else {
  11019. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL));
  11020. }
  11021. unlock_user (p, arg2, arg3);
  11022. }
  11023. return ret;
  11024. #endif
  11025. #ifdef TARGET_NR_mq_timedreceive
  11026. case TARGET_NR_mq_timedreceive:
  11027. {
  11028. struct timespec ts;
  11029. unsigned int prio;
  11030. p = lock_user (VERIFY_READ, arg2, arg3, 1);
  11031. if (arg5 != 0) {
  11032. target_to_host_timespec(&ts, arg5);
  11033. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  11034. &prio, &ts));
  11035. host_to_target_timespec(arg5, &ts);
  11036. } else {
  11037. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  11038. &prio, NULL));
  11039. }
  11040. unlock_user (p, arg2, arg3);
  11041. if (arg4 != 0)
  11042. put_user_u32(prio, arg4);
  11043. }
  11044. return ret;
  11045. #endif
  11046. /* Not implemented for now... */
  11047. /* case TARGET_NR_mq_notify: */
  11048. /* break; */
  11049. case TARGET_NR_mq_getsetattr:
  11050. {
  11051. struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
  11052. ret = 0;
  11053. if (arg2 != 0) {
  11054. copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
  11055. ret = get_errno(mq_setattr(arg1, &posix_mq_attr_in,
  11056. &posix_mq_attr_out));
  11057. } else if (arg3 != 0) {
  11058. ret = get_errno(mq_getattr(arg1, &posix_mq_attr_out));
  11059. }
  11060. if (ret == 0 && arg3 != 0) {
  11061. copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
  11062. }
  11063. }
  11064. return ret;
  11065. #endif
  11066. #ifdef CONFIG_SPLICE
  11067. #ifdef TARGET_NR_tee
  11068. case TARGET_NR_tee:
  11069. {
  11070. ret = get_errno(tee(arg1,arg2,arg3,arg4));
  11071. }
  11072. return ret;
  11073. #endif
  11074. #ifdef TARGET_NR_splice
  11075. case TARGET_NR_splice:
  11076. {
  11077. loff_t loff_in, loff_out;
  11078. loff_t *ploff_in = NULL, *ploff_out = NULL;
  11079. if (arg2) {
  11080. if (get_user_u64(loff_in, arg2)) {
  11081. return -TARGET_EFAULT;
  11082. }
  11083. ploff_in = &loff_in;
  11084. }
  11085. if (arg4) {
  11086. if (get_user_u64(loff_out, arg4)) {
  11087. return -TARGET_EFAULT;
  11088. }
  11089. ploff_out = &loff_out;
  11090. }
  11091. ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
  11092. if (arg2) {
  11093. if (put_user_u64(loff_in, arg2)) {
  11094. return -TARGET_EFAULT;
  11095. }
  11096. }
  11097. if (arg4) {
  11098. if (put_user_u64(loff_out, arg4)) {
  11099. return -TARGET_EFAULT;
  11100. }
  11101. }
  11102. }
  11103. return ret;
  11104. #endif
  11105. #ifdef TARGET_NR_vmsplice
  11106. case TARGET_NR_vmsplice:
  11107. {
  11108. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  11109. if (vec != NULL) {
  11110. ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
  11111. unlock_iovec(vec, arg2, arg3, 0);
  11112. } else {
  11113. ret = -host_to_target_errno(errno);
  11114. }
  11115. }
  11116. return ret;
  11117. #endif
  11118. #endif /* CONFIG_SPLICE */
  11119. #ifdef CONFIG_EVENTFD
  11120. #if defined(TARGET_NR_eventfd)
  11121. case TARGET_NR_eventfd:
  11122. ret = get_errno(eventfd(arg1, 0));
  11123. if (ret >= 0) {
  11124. fd_trans_register(ret, &target_eventfd_trans);
  11125. }
  11126. return ret;
  11127. #endif
  11128. #if defined(TARGET_NR_eventfd2)
  11129. case TARGET_NR_eventfd2:
  11130. {
  11131. int host_flags = arg2 & (~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC));
  11132. if (arg2 & TARGET_O_NONBLOCK) {
  11133. host_flags |= O_NONBLOCK;
  11134. }
  11135. if (arg2 & TARGET_O_CLOEXEC) {
  11136. host_flags |= O_CLOEXEC;
  11137. }
  11138. ret = get_errno(eventfd(arg1, host_flags));
  11139. if (ret >= 0) {
  11140. fd_trans_register(ret, &target_eventfd_trans);
  11141. }
  11142. return ret;
  11143. }
  11144. #endif
  11145. #endif /* CONFIG_EVENTFD */
  11146. #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
  11147. case TARGET_NR_fallocate:
  11148. #if TARGET_ABI_BITS == 32
  11149. ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
  11150. target_offset64(arg5, arg6)));
  11151. #else
  11152. ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
  11153. #endif
  11154. return ret;
  11155. #endif
  11156. #if defined(CONFIG_SYNC_FILE_RANGE)
  11157. #if defined(TARGET_NR_sync_file_range)
  11158. case TARGET_NR_sync_file_range:
  11159. #if TARGET_ABI_BITS == 32
  11160. #if defined(TARGET_MIPS)
  11161. ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
  11162. target_offset64(arg5, arg6), arg7));
  11163. #else
  11164. ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
  11165. target_offset64(arg4, arg5), arg6));
  11166. #endif /* !TARGET_MIPS */
  11167. #else
  11168. ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
  11169. #endif
  11170. return ret;
  11171. #endif
  11172. #if defined(TARGET_NR_sync_file_range2) || \
  11173. defined(TARGET_NR_arm_sync_file_range)
  11174. #if defined(TARGET_NR_sync_file_range2)
  11175. case TARGET_NR_sync_file_range2:
  11176. #endif
  11177. #if defined(TARGET_NR_arm_sync_file_range)
  11178. case TARGET_NR_arm_sync_file_range:
  11179. #endif
  11180. /* This is like sync_file_range but the arguments are reordered */
  11181. #if TARGET_ABI_BITS == 32
  11182. ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
  11183. target_offset64(arg5, arg6), arg2));
  11184. #else
  11185. ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
  11186. #endif
  11187. return ret;
  11188. #endif
  11189. #endif
  11190. #if defined(TARGET_NR_signalfd4)
  11191. case TARGET_NR_signalfd4:
  11192. return do_signalfd4(arg1, arg2, arg4);
  11193. #endif
  11194. #if defined(TARGET_NR_signalfd)
  11195. case TARGET_NR_signalfd:
  11196. return do_signalfd4(arg1, arg2, 0);
  11197. #endif
  11198. #if defined(CONFIG_EPOLL)
  11199. #if defined(TARGET_NR_epoll_create)
  11200. case TARGET_NR_epoll_create:
  11201. return get_errno(epoll_create(arg1));
  11202. #endif
  11203. #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
  11204. case TARGET_NR_epoll_create1:
  11205. return get_errno(epoll_create1(target_to_host_bitmask(arg1, fcntl_flags_tbl)));
  11206. #endif
  11207. #if defined(TARGET_NR_epoll_ctl)
  11208. case TARGET_NR_epoll_ctl:
  11209. {
  11210. struct epoll_event ep;
  11211. struct epoll_event *epp = 0;
  11212. if (arg4) {
  11213. struct target_epoll_event *target_ep;
  11214. if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
  11215. return -TARGET_EFAULT;
  11216. }
  11217. ep.events = tswap32(target_ep->events);
  11218. /* The epoll_data_t union is just opaque data to the kernel,
  11219. * so we transfer all 64 bits across and need not worry what
  11220. * actual data type it is.
  11221. */
  11222. ep.data.u64 = tswap64(target_ep->data.u64);
  11223. unlock_user_struct(target_ep, arg4, 0);
  11224. epp = &ep;
  11225. }
  11226. return get_errno(epoll_ctl(arg1, arg2, arg3, epp));
  11227. }
  11228. #endif
  11229. #if defined(TARGET_NR_epoll_wait) || defined(TARGET_NR_epoll_pwait)
  11230. #if defined(TARGET_NR_epoll_wait)
  11231. case TARGET_NR_epoll_wait:
  11232. #endif
  11233. #if defined(TARGET_NR_epoll_pwait)
  11234. case TARGET_NR_epoll_pwait:
  11235. #endif
  11236. {
  11237. struct target_epoll_event *target_ep;
  11238. struct epoll_event *ep;
  11239. int epfd = arg1;
  11240. int maxevents = arg3;
  11241. int timeout = arg4;
  11242. if (maxevents <= 0 || maxevents > TARGET_EP_MAX_EVENTS) {
  11243. return -TARGET_EINVAL;
  11244. }
  11245. target_ep = lock_user(VERIFY_WRITE, arg2,
  11246. maxevents * sizeof(struct target_epoll_event), 1);
  11247. if (!target_ep) {
  11248. return -TARGET_EFAULT;
  11249. }
  11250. ep = g_try_new(struct epoll_event, maxevents);
  11251. if (!ep) {
  11252. unlock_user(target_ep, arg2, 0);
  11253. return -TARGET_ENOMEM;
  11254. }
  11255. switch (num) {
  11256. #if defined(TARGET_NR_epoll_pwait)
  11257. case TARGET_NR_epoll_pwait:
  11258. {
  11259. target_sigset_t *target_set;
  11260. sigset_t _set, *set = &_set;
  11261. if (arg5) {
  11262. if (arg6 != sizeof(target_sigset_t)) {
  11263. ret = -TARGET_EINVAL;
  11264. break;
  11265. }
  11266. target_set = lock_user(VERIFY_READ, arg5,
  11267. sizeof(target_sigset_t), 1);
  11268. if (!target_set) {
  11269. ret = -TARGET_EFAULT;
  11270. break;
  11271. }
  11272. target_to_host_sigset(set, target_set);
  11273. unlock_user(target_set, arg5, 0);
  11274. } else {
  11275. set = NULL;
  11276. }
  11277. ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
  11278. set, SIGSET_T_SIZE));
  11279. break;
  11280. }
  11281. #endif
  11282. #if defined(TARGET_NR_epoll_wait)
  11283. case TARGET_NR_epoll_wait:
  11284. ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
  11285. NULL, 0));
  11286. break;
  11287. #endif
  11288. default:
  11289. ret = -TARGET_ENOSYS;
  11290. }
  11291. if (!is_error(ret)) {
  11292. int i;
  11293. for (i = 0; i < ret; i++) {
  11294. target_ep[i].events = tswap32(ep[i].events);
  11295. target_ep[i].data.u64 = tswap64(ep[i].data.u64);
  11296. }
  11297. unlock_user(target_ep, arg2,
  11298. ret * sizeof(struct target_epoll_event));
  11299. } else {
  11300. unlock_user(target_ep, arg2, 0);
  11301. }
  11302. g_free(ep);
  11303. return ret;
  11304. }
  11305. #endif
  11306. #endif
  11307. #ifdef TARGET_NR_prlimit64
  11308. case TARGET_NR_prlimit64:
  11309. {
  11310. /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
  11311. struct target_rlimit64 *target_rnew, *target_rold;
  11312. struct host_rlimit64 rnew, rold, *rnewp = 0;
  11313. int resource = target_to_host_resource(arg2);
  11314. if (arg3 && (resource != RLIMIT_AS &&
  11315. resource != RLIMIT_DATA &&
  11316. resource != RLIMIT_STACK)) {
  11317. if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
  11318. return -TARGET_EFAULT;
  11319. }
  11320. rnew.rlim_cur = tswap64(target_rnew->rlim_cur);
  11321. rnew.rlim_max = tswap64(target_rnew->rlim_max);
  11322. unlock_user_struct(target_rnew, arg3, 0);
  11323. rnewp = &rnew;
  11324. }
  11325. ret = get_errno(sys_prlimit64(arg1, resource, rnewp, arg4 ? &rold : 0));
  11326. if (!is_error(ret) && arg4) {
  11327. if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
  11328. return -TARGET_EFAULT;
  11329. }
  11330. target_rold->rlim_cur = tswap64(rold.rlim_cur);
  11331. target_rold->rlim_max = tswap64(rold.rlim_max);
  11332. unlock_user_struct(target_rold, arg4, 1);
  11333. }
  11334. return ret;
  11335. }
  11336. #endif
  11337. #ifdef TARGET_NR_gethostname
  11338. case TARGET_NR_gethostname:
  11339. {
  11340. char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
  11341. if (name) {
  11342. ret = get_errno(gethostname(name, arg2));
  11343. unlock_user(name, arg1, arg2);
  11344. } else {
  11345. ret = -TARGET_EFAULT;
  11346. }
  11347. return ret;
  11348. }
  11349. #endif
  11350. #ifdef TARGET_NR_atomic_cmpxchg_32
  11351. case TARGET_NR_atomic_cmpxchg_32:
  11352. {
  11353. /* should use start_exclusive from main.c */
  11354. abi_ulong mem_value;
  11355. if (get_user_u32(mem_value, arg6)) {
  11356. target_siginfo_t info;
  11357. info.si_signo = SIGSEGV;
  11358. info.si_errno = 0;
  11359. info.si_code = TARGET_SEGV_MAPERR;
  11360. info._sifields._sigfault._addr = arg6;
  11361. queue_signal((CPUArchState *)cpu_env, info.si_signo,
  11362. QEMU_SI_FAULT, &info);
  11363. ret = 0xdeadbeef;
  11364. }
  11365. if (mem_value == arg2)
  11366. put_user_u32(arg1, arg6);
  11367. return mem_value;
  11368. }
  11369. #endif
  11370. #ifdef TARGET_NR_atomic_barrier
  11371. case TARGET_NR_atomic_barrier:
  11372. /* Like the kernel implementation and the
  11373. qemu arm barrier, no-op this? */
  11374. return 0;
  11375. #endif
  11376. #ifdef TARGET_NR_timer_create
  11377. case TARGET_NR_timer_create:
  11378. {
  11379. /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
  11380. struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL;
  11381. int clkid = arg1;
  11382. int timer_index = next_free_host_timer();
  11383. if (timer_index < 0) {
  11384. ret = -TARGET_EAGAIN;
  11385. } else {
  11386. timer_t *phtimer = g_posix_timers + timer_index;
  11387. if (arg2) {
  11388. phost_sevp = &host_sevp;
  11389. ret = target_to_host_sigevent(phost_sevp, arg2);
  11390. if (ret != 0) {
  11391. return ret;
  11392. }
  11393. }
  11394. ret = get_errno(timer_create(clkid, phost_sevp, phtimer));
  11395. if (ret) {
  11396. phtimer = NULL;
  11397. } else {
  11398. if (put_user(TIMER_MAGIC | timer_index, arg3, target_timer_t)) {
  11399. return -TARGET_EFAULT;
  11400. }
  11401. }
  11402. }
  11403. return ret;
  11404. }
  11405. #endif
  11406. #ifdef TARGET_NR_timer_settime
  11407. case TARGET_NR_timer_settime:
  11408. {
  11409. /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
  11410. * struct itimerspec * old_value */
  11411. target_timer_t timerid = get_timer_id(arg1);
  11412. if (timerid < 0) {
  11413. ret = timerid;
  11414. } else if (arg3 == 0) {
  11415. ret = -TARGET_EINVAL;
  11416. } else {
  11417. timer_t htimer = g_posix_timers[timerid];
  11418. struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
  11419. if (target_to_host_itimerspec(&hspec_new, arg3)) {
  11420. return -TARGET_EFAULT;
  11421. }
  11422. ret = get_errno(
  11423. timer_settime(htimer, arg2, &hspec_new, &hspec_old));
  11424. if (arg4 && host_to_target_itimerspec(arg4, &hspec_old)) {
  11425. return -TARGET_EFAULT;
  11426. }
  11427. }
  11428. return ret;
  11429. }
  11430. #endif
  11431. #ifdef TARGET_NR_timer_gettime
  11432. case TARGET_NR_timer_gettime:
  11433. {
  11434. /* args: timer_t timerid, struct itimerspec *curr_value */
  11435. target_timer_t timerid = get_timer_id(arg1);
  11436. if (timerid < 0) {
  11437. ret = timerid;
  11438. } else if (!arg2) {
  11439. ret = -TARGET_EFAULT;
  11440. } else {
  11441. timer_t htimer = g_posix_timers[timerid];
  11442. struct itimerspec hspec;
  11443. ret = get_errno(timer_gettime(htimer, &hspec));
  11444. if (host_to_target_itimerspec(arg2, &hspec)) {
  11445. ret = -TARGET_EFAULT;
  11446. }
  11447. }
  11448. return ret;
  11449. }
  11450. #endif
  11451. #ifdef TARGET_NR_timer_getoverrun
  11452. case TARGET_NR_timer_getoverrun:
  11453. {
  11454. /* args: timer_t timerid */
  11455. target_timer_t timerid = get_timer_id(arg1);
  11456. if (timerid < 0) {
  11457. ret = timerid;
  11458. } else {
  11459. timer_t htimer = g_posix_timers[timerid];
  11460. ret = get_errno(timer_getoverrun(htimer));
  11461. }
  11462. return ret;
  11463. }
  11464. #endif
  11465. #ifdef TARGET_NR_timer_delete
  11466. case TARGET_NR_timer_delete:
  11467. {
  11468. /* args: timer_t timerid */
  11469. target_timer_t timerid = get_timer_id(arg1);
  11470. if (timerid < 0) {
  11471. ret = timerid;
  11472. } else {
  11473. timer_t htimer = g_posix_timers[timerid];
  11474. ret = get_errno(timer_delete(htimer));
  11475. g_posix_timers[timerid] = 0;
  11476. }
  11477. return ret;
  11478. }
  11479. #endif
  11480. #if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
  11481. case TARGET_NR_timerfd_create:
  11482. return get_errno(timerfd_create(arg1,
  11483. target_to_host_bitmask(arg2, fcntl_flags_tbl)));
  11484. #endif
  11485. #if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
  11486. case TARGET_NR_timerfd_gettime:
  11487. {
  11488. struct itimerspec its_curr;
  11489. ret = get_errno(timerfd_gettime(arg1, &its_curr));
  11490. if (arg2 && host_to_target_itimerspec(arg2, &its_curr)) {
  11491. return -TARGET_EFAULT;
  11492. }
  11493. }
  11494. return ret;
  11495. #endif
  11496. #if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
  11497. case TARGET_NR_timerfd_settime:
  11498. {
  11499. struct itimerspec its_new, its_old, *p_new;
  11500. if (arg3) {
  11501. if (target_to_host_itimerspec(&its_new, arg3)) {
  11502. return -TARGET_EFAULT;
  11503. }
  11504. p_new = &its_new;
  11505. } else {
  11506. p_new = NULL;
  11507. }
  11508. ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
  11509. if (arg4 && host_to_target_itimerspec(arg4, &its_old)) {
  11510. return -TARGET_EFAULT;
  11511. }
  11512. }
  11513. return ret;
  11514. #endif
  11515. #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
  11516. case TARGET_NR_ioprio_get:
  11517. return get_errno(ioprio_get(arg1, arg2));
  11518. #endif
  11519. #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
  11520. case TARGET_NR_ioprio_set:
  11521. return get_errno(ioprio_set(arg1, arg2, arg3));
  11522. #endif
  11523. #if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
  11524. case TARGET_NR_setns:
  11525. return get_errno(setns(arg1, arg2));
  11526. #endif
  11527. #if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
  11528. case TARGET_NR_unshare:
  11529. return get_errno(unshare(arg1));
  11530. #endif
  11531. #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
  11532. case TARGET_NR_kcmp:
  11533. return get_errno(kcmp(arg1, arg2, arg3, arg4, arg5));
  11534. #endif
  11535. #ifdef TARGET_NR_swapcontext
  11536. case TARGET_NR_swapcontext:
  11537. /* PowerPC specific. */
  11538. return do_swapcontext(cpu_env, arg1, arg2, arg3);
  11539. #endif
  11540. #ifdef TARGET_NR_memfd_create
  11541. case TARGET_NR_memfd_create:
  11542. p = lock_user_string(arg1);
  11543. if (!p) {
  11544. return -TARGET_EFAULT;
  11545. }
  11546. ret = get_errno(memfd_create(p, arg2));
  11547. fd_trans_unregister(ret);
  11548. unlock_user(p, arg1, 0);
  11549. return ret;
  11550. #endif
  11551. #if defined TARGET_NR_membarrier && defined __NR_membarrier
  11552. case TARGET_NR_membarrier:
  11553. return get_errno(membarrier(arg1, arg2));
  11554. #endif
  11555. default:
  11556. qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num);
  11557. return -TARGET_ENOSYS;
  11558. }
  11559. return ret;
  11560. }
  11561. abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
  11562. abi_long arg2, abi_long arg3, abi_long arg4,
  11563. abi_long arg5, abi_long arg6, abi_long arg7,
  11564. abi_long arg8)
  11565. {
  11566. CPUState *cpu = env_cpu(cpu_env);
  11567. abi_long ret;
  11568. #ifdef DEBUG_ERESTARTSYS
  11569. /* Debug-only code for exercising the syscall-restart code paths
  11570. * in the per-architecture cpu main loops: restart every syscall
  11571. * the guest makes once before letting it through.
  11572. */
  11573. {
  11574. static bool flag;
  11575. flag = !flag;
  11576. if (flag) {
  11577. return -TARGET_ERESTARTSYS;
  11578. }
  11579. }
  11580. #endif
  11581. record_syscall_start(cpu, num, arg1,
  11582. arg2, arg3, arg4, arg5, arg6, arg7, arg8);
  11583. if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
  11584. print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
  11585. }
  11586. ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4,
  11587. arg5, arg6, arg7, arg8);
  11588. if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
  11589. print_syscall_ret(num, ret, arg1, arg2, arg3, arg4, arg5, arg6);
  11590. }
  11591. record_syscall_return(cpu, num, ret);
  11592. return ret;
  11593. }