2
0

syscall.c 273 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844
  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 <stdlib.h>
  21. #include <stdio.h>
  22. #include <stdarg.h>
  23. #include <string.h>
  24. #include <elf.h>
  25. #include <endian.h>
  26. #include <errno.h>
  27. #include <unistd.h>
  28. #include <fcntl.h>
  29. #include <time.h>
  30. #include <limits.h>
  31. #include <sys/types.h>
  32. #include <sys/ipc.h>
  33. #include <sys/msg.h>
  34. #include <sys/wait.h>
  35. #include <sys/time.h>
  36. #include <sys/stat.h>
  37. #include <sys/mount.h>
  38. #include <sys/prctl.h>
  39. #include <sys/resource.h>
  40. #include <sys/mman.h>
  41. #include <sys/swap.h>
  42. #include <signal.h>
  43. #include <sched.h>
  44. #ifdef __ia64__
  45. int __clone2(int (*fn)(void *), void *child_stack_base,
  46. size_t stack_size, int flags, void *arg, ...);
  47. #endif
  48. #include <sys/socket.h>
  49. #include <sys/un.h>
  50. #include <sys/uio.h>
  51. #include <sys/poll.h>
  52. #include <sys/times.h>
  53. #include <sys/shm.h>
  54. #include <sys/sem.h>
  55. #include <sys/statfs.h>
  56. #include <utime.h>
  57. #include <sys/sysinfo.h>
  58. #include <sys/utsname.h>
  59. //#include <sys/user.h>
  60. #include <netinet/ip.h>
  61. #include <netinet/tcp.h>
  62. #include <linux/wireless.h>
  63. #include <linux/icmp.h>
  64. #include "qemu-common.h"
  65. #ifdef TARGET_GPROF
  66. #include <sys/gmon.h>
  67. #endif
  68. #ifdef CONFIG_EVENTFD
  69. #include <sys/eventfd.h>
  70. #endif
  71. #ifdef CONFIG_EPOLL
  72. #include <sys/epoll.h>
  73. #endif
  74. #ifdef CONFIG_ATTR
  75. #include "qemu-xattr.h"
  76. #endif
  77. #define termios host_termios
  78. #define winsize host_winsize
  79. #define termio host_termio
  80. #define sgttyb host_sgttyb /* same as target */
  81. #define tchars host_tchars /* same as target */
  82. #define ltchars host_ltchars /* same as target */
  83. #include <linux/termios.h>
  84. #include <linux/unistd.h>
  85. #include <linux/utsname.h>
  86. #include <linux/cdrom.h>
  87. #include <linux/hdreg.h>
  88. #include <linux/soundcard.h>
  89. #include <linux/kd.h>
  90. #include <linux/mtio.h>
  91. #include <linux/fs.h>
  92. #if defined(CONFIG_FIEMAP)
  93. #include <linux/fiemap.h>
  94. #endif
  95. #include <linux/fb.h>
  96. #include <linux/vt.h>
  97. #include <linux/dm-ioctl.h>
  98. #include "linux_loop.h"
  99. #include "cpu-uname.h"
  100. #include "qemu.h"
  101. #if defined(CONFIG_USE_NPTL)
  102. #define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
  103. CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
  104. #else
  105. /* XXX: Hardcode the above values. */
  106. #define CLONE_NPTL_FLAGS2 0
  107. #endif
  108. //#define DEBUG
  109. //#include <linux/msdos_fs.h>
  110. #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
  111. #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
  112. #undef _syscall0
  113. #undef _syscall1
  114. #undef _syscall2
  115. #undef _syscall3
  116. #undef _syscall4
  117. #undef _syscall5
  118. #undef _syscall6
  119. #define _syscall0(type,name) \
  120. static type name (void) \
  121. { \
  122. return syscall(__NR_##name); \
  123. }
  124. #define _syscall1(type,name,type1,arg1) \
  125. static type name (type1 arg1) \
  126. { \
  127. return syscall(__NR_##name, arg1); \
  128. }
  129. #define _syscall2(type,name,type1,arg1,type2,arg2) \
  130. static type name (type1 arg1,type2 arg2) \
  131. { \
  132. return syscall(__NR_##name, arg1, arg2); \
  133. }
  134. #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
  135. static type name (type1 arg1,type2 arg2,type3 arg3) \
  136. { \
  137. return syscall(__NR_##name, arg1, arg2, arg3); \
  138. }
  139. #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
  140. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
  141. { \
  142. return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
  143. }
  144. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  145. type5,arg5) \
  146. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
  147. { \
  148. return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
  149. }
  150. #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  151. type5,arg5,type6,arg6) \
  152. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
  153. type6 arg6) \
  154. { \
  155. return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
  156. }
  157. #define __NR_sys_uname __NR_uname
  158. #define __NR_sys_faccessat __NR_faccessat
  159. #define __NR_sys_fchmodat __NR_fchmodat
  160. #define __NR_sys_fchownat __NR_fchownat
  161. #define __NR_sys_fstatat64 __NR_fstatat64
  162. #define __NR_sys_futimesat __NR_futimesat
  163. #define __NR_sys_getcwd1 __NR_getcwd
  164. #define __NR_sys_getdents __NR_getdents
  165. #define __NR_sys_getdents64 __NR_getdents64
  166. #define __NR_sys_getpriority __NR_getpriority
  167. #define __NR_sys_linkat __NR_linkat
  168. #define __NR_sys_mkdirat __NR_mkdirat
  169. #define __NR_sys_mknodat __NR_mknodat
  170. #define __NR_sys_newfstatat __NR_newfstatat
  171. #define __NR_sys_openat __NR_openat
  172. #define __NR_sys_readlinkat __NR_readlinkat
  173. #define __NR_sys_renameat __NR_renameat
  174. #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
  175. #define __NR_sys_symlinkat __NR_symlinkat
  176. #define __NR_sys_syslog __NR_syslog
  177. #define __NR_sys_tgkill __NR_tgkill
  178. #define __NR_sys_tkill __NR_tkill
  179. #define __NR_sys_unlinkat __NR_unlinkat
  180. #define __NR_sys_utimensat __NR_utimensat
  181. #define __NR_sys_futex __NR_futex
  182. #define __NR_sys_inotify_init __NR_inotify_init
  183. #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
  184. #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
  185. #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) || \
  186. defined(__s390x__)
  187. #define __NR__llseek __NR_lseek
  188. #endif
  189. #ifdef __NR_gettid
  190. _syscall0(int, gettid)
  191. #else
  192. /* This is a replacement for the host gettid() and must return a host
  193. errno. */
  194. static int gettid(void) {
  195. return -ENOSYS;
  196. }
  197. #endif
  198. _syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
  199. #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
  200. _syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
  201. #endif
  202. #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
  203. _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
  204. loff_t *, res, uint, wh);
  205. #endif
  206. _syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
  207. _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
  208. #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
  209. _syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
  210. #endif
  211. #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
  212. _syscall2(int,sys_tkill,int,tid,int,sig)
  213. #endif
  214. #ifdef __NR_exit_group
  215. _syscall1(int,exit_group,int,error_code)
  216. #endif
  217. #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
  218. _syscall1(int,set_tid_address,int *,tidptr)
  219. #endif
  220. #if defined(CONFIG_USE_NPTL)
  221. #if defined(TARGET_NR_futex) && defined(__NR_futex)
  222. _syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
  223. const struct timespec *,timeout,int *,uaddr2,int,val3)
  224. #endif
  225. #endif
  226. #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
  227. _syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
  228. unsigned long *, user_mask_ptr);
  229. #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
  230. _syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
  231. unsigned long *, user_mask_ptr);
  232. _syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
  233. void *, arg);
  234. static bitmask_transtbl fcntl_flags_tbl[] = {
  235. { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
  236. { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
  237. { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
  238. { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
  239. { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
  240. { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
  241. { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
  242. { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
  243. { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
  244. { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
  245. { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
  246. { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
  247. { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
  248. #if defined(O_DIRECT)
  249. { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
  250. #endif
  251. #if defined(O_NOATIME)
  252. { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
  253. #endif
  254. #if defined(O_CLOEXEC)
  255. { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
  256. #endif
  257. #if defined(O_PATH)
  258. { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
  259. #endif
  260. /* Don't terminate the list prematurely on 64-bit host+guest. */
  261. #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
  262. { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
  263. #endif
  264. { 0, 0, 0, 0 }
  265. };
  266. #define COPY_UTSNAME_FIELD(dest, src) \
  267. do { \
  268. /* __NEW_UTS_LEN doesn't include terminating null */ \
  269. (void) strncpy((dest), (src), __NEW_UTS_LEN); \
  270. (dest)[__NEW_UTS_LEN] = '\0'; \
  271. } while (0)
  272. static int sys_uname(struct new_utsname *buf)
  273. {
  274. struct utsname uts_buf;
  275. if (uname(&uts_buf) < 0)
  276. return (-1);
  277. /*
  278. * Just in case these have some differences, we
  279. * translate utsname to new_utsname (which is the
  280. * struct linux kernel uses).
  281. */
  282. memset(buf, 0, sizeof(*buf));
  283. COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
  284. COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
  285. COPY_UTSNAME_FIELD(buf->release, uts_buf.release);
  286. COPY_UTSNAME_FIELD(buf->version, uts_buf.version);
  287. COPY_UTSNAME_FIELD(buf->machine, uts_buf.machine);
  288. #ifdef _GNU_SOURCE
  289. COPY_UTSNAME_FIELD(buf->domainname, uts_buf.domainname);
  290. #endif
  291. return (0);
  292. #undef COPY_UTSNAME_FIELD
  293. }
  294. static int sys_getcwd1(char *buf, size_t size)
  295. {
  296. if (getcwd(buf, size) == NULL) {
  297. /* getcwd() sets errno */
  298. return (-1);
  299. }
  300. return strlen(buf)+1;
  301. }
  302. #ifdef CONFIG_ATFILE
  303. /*
  304. * Host system seems to have atfile syscall stubs available. We
  305. * now enable them one by one as specified by target syscall_nr.h.
  306. */
  307. #ifdef TARGET_NR_faccessat
  308. static int sys_faccessat(int dirfd, const char *pathname, int mode)
  309. {
  310. return (faccessat(dirfd, pathname, mode, 0));
  311. }
  312. #endif
  313. #ifdef TARGET_NR_fchmodat
  314. static int sys_fchmodat(int dirfd, const char *pathname, mode_t mode)
  315. {
  316. return (fchmodat(dirfd, pathname, mode, 0));
  317. }
  318. #endif
  319. #if defined(TARGET_NR_fchownat)
  320. static int sys_fchownat(int dirfd, const char *pathname, uid_t owner,
  321. gid_t group, int flags)
  322. {
  323. return (fchownat(dirfd, pathname, owner, group, flags));
  324. }
  325. #endif
  326. #ifdef __NR_fstatat64
  327. static int sys_fstatat64(int dirfd, const char *pathname, struct stat *buf,
  328. int flags)
  329. {
  330. return (fstatat(dirfd, pathname, buf, flags));
  331. }
  332. #endif
  333. #ifdef __NR_newfstatat
  334. static int sys_newfstatat(int dirfd, const char *pathname, struct stat *buf,
  335. int flags)
  336. {
  337. return (fstatat(dirfd, pathname, buf, flags));
  338. }
  339. #endif
  340. #ifdef TARGET_NR_futimesat
  341. static int sys_futimesat(int dirfd, const char *pathname,
  342. const struct timeval times[2])
  343. {
  344. return (futimesat(dirfd, pathname, times));
  345. }
  346. #endif
  347. #ifdef TARGET_NR_linkat
  348. static int sys_linkat(int olddirfd, const char *oldpath,
  349. int newdirfd, const char *newpath, int flags)
  350. {
  351. return (linkat(olddirfd, oldpath, newdirfd, newpath, flags));
  352. }
  353. #endif
  354. #ifdef TARGET_NR_mkdirat
  355. static int sys_mkdirat(int dirfd, const char *pathname, mode_t mode)
  356. {
  357. return (mkdirat(dirfd, pathname, mode));
  358. }
  359. #endif
  360. #ifdef TARGET_NR_mknodat
  361. static int sys_mknodat(int dirfd, const char *pathname, mode_t mode,
  362. dev_t dev)
  363. {
  364. return (mknodat(dirfd, pathname, mode, dev));
  365. }
  366. #endif
  367. #ifdef TARGET_NR_openat
  368. static int sys_openat(int dirfd, const char *pathname, int flags, mode_t mode)
  369. {
  370. /*
  371. * open(2) has extra parameter 'mode' when called with
  372. * flag O_CREAT.
  373. */
  374. if ((flags & O_CREAT) != 0) {
  375. return (openat(dirfd, pathname, flags, mode));
  376. }
  377. return (openat(dirfd, pathname, flags));
  378. }
  379. #endif
  380. #ifdef TARGET_NR_readlinkat
  381. static int sys_readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz)
  382. {
  383. return (readlinkat(dirfd, pathname, buf, bufsiz));
  384. }
  385. #endif
  386. #ifdef TARGET_NR_renameat
  387. static int sys_renameat(int olddirfd, const char *oldpath,
  388. int newdirfd, const char *newpath)
  389. {
  390. return (renameat(olddirfd, oldpath, newdirfd, newpath));
  391. }
  392. #endif
  393. #ifdef TARGET_NR_symlinkat
  394. static int sys_symlinkat(const char *oldpath, int newdirfd, const char *newpath)
  395. {
  396. return (symlinkat(oldpath, newdirfd, newpath));
  397. }
  398. #endif
  399. #ifdef TARGET_NR_unlinkat
  400. static int sys_unlinkat(int dirfd, const char *pathname, int flags)
  401. {
  402. return (unlinkat(dirfd, pathname, flags));
  403. }
  404. #endif
  405. #else /* !CONFIG_ATFILE */
  406. /*
  407. * Try direct syscalls instead
  408. */
  409. #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
  410. _syscall3(int,sys_faccessat,int,dirfd,const char *,pathname,int,mode)
  411. #endif
  412. #if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
  413. _syscall3(int,sys_fchmodat,int,dirfd,const char *,pathname, mode_t,mode)
  414. #endif
  415. #if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
  416. _syscall5(int,sys_fchownat,int,dirfd,const char *,pathname,
  417. uid_t,owner,gid_t,group,int,flags)
  418. #endif
  419. #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
  420. defined(__NR_fstatat64)
  421. _syscall4(int,sys_fstatat64,int,dirfd,const char *,pathname,
  422. struct stat *,buf,int,flags)
  423. #endif
  424. #if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
  425. _syscall3(int,sys_futimesat,int,dirfd,const char *,pathname,
  426. const struct timeval *,times)
  427. #endif
  428. #if (defined(TARGET_NR_newfstatat) || defined(TARGET_NR_fstatat64) ) && \
  429. defined(__NR_newfstatat)
  430. _syscall4(int,sys_newfstatat,int,dirfd,const char *,pathname,
  431. struct stat *,buf,int,flags)
  432. #endif
  433. #if defined(TARGET_NR_linkat) && defined(__NR_linkat)
  434. _syscall5(int,sys_linkat,int,olddirfd,const char *,oldpath,
  435. int,newdirfd,const char *,newpath,int,flags)
  436. #endif
  437. #if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
  438. _syscall3(int,sys_mkdirat,int,dirfd,const char *,pathname,mode_t,mode)
  439. #endif
  440. #if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
  441. _syscall4(int,sys_mknodat,int,dirfd,const char *,pathname,
  442. mode_t,mode,dev_t,dev)
  443. #endif
  444. #if defined(TARGET_NR_openat) && defined(__NR_openat)
  445. _syscall4(int,sys_openat,int,dirfd,const char *,pathname,int,flags,mode_t,mode)
  446. #endif
  447. #if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
  448. _syscall4(int,sys_readlinkat,int,dirfd,const char *,pathname,
  449. char *,buf,size_t,bufsize)
  450. #endif
  451. #if defined(TARGET_NR_renameat) && defined(__NR_renameat)
  452. _syscall4(int,sys_renameat,int,olddirfd,const char *,oldpath,
  453. int,newdirfd,const char *,newpath)
  454. #endif
  455. #if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
  456. _syscall3(int,sys_symlinkat,const char *,oldpath,
  457. int,newdirfd,const char *,newpath)
  458. #endif
  459. #if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
  460. _syscall3(int,sys_unlinkat,int,dirfd,const char *,pathname,int,flags)
  461. #endif
  462. #endif /* CONFIG_ATFILE */
  463. #ifdef CONFIG_UTIMENSAT
  464. static int sys_utimensat(int dirfd, const char *pathname,
  465. const struct timespec times[2], int flags)
  466. {
  467. if (pathname == NULL)
  468. return futimens(dirfd, times);
  469. else
  470. return utimensat(dirfd, pathname, times, flags);
  471. }
  472. #else
  473. #if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
  474. _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
  475. const struct timespec *,tsp,int,flags)
  476. #endif
  477. #endif /* CONFIG_UTIMENSAT */
  478. #ifdef CONFIG_INOTIFY
  479. #include <sys/inotify.h>
  480. #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
  481. static int sys_inotify_init(void)
  482. {
  483. return (inotify_init());
  484. }
  485. #endif
  486. #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
  487. static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
  488. {
  489. return (inotify_add_watch(fd, pathname, mask));
  490. }
  491. #endif
  492. #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
  493. static int sys_inotify_rm_watch(int fd, int32_t wd)
  494. {
  495. return (inotify_rm_watch(fd, wd));
  496. }
  497. #endif
  498. #ifdef CONFIG_INOTIFY1
  499. #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
  500. static int sys_inotify_init1(int flags)
  501. {
  502. return (inotify_init1(flags));
  503. }
  504. #endif
  505. #endif
  506. #else
  507. /* Userspace can usually survive runtime without inotify */
  508. #undef TARGET_NR_inotify_init
  509. #undef TARGET_NR_inotify_init1
  510. #undef TARGET_NR_inotify_add_watch
  511. #undef TARGET_NR_inotify_rm_watch
  512. #endif /* CONFIG_INOTIFY */
  513. #if defined(TARGET_NR_ppoll)
  514. #ifndef __NR_ppoll
  515. # define __NR_ppoll -1
  516. #endif
  517. #define __NR_sys_ppoll __NR_ppoll
  518. _syscall5(int, sys_ppoll, struct pollfd *, fds, nfds_t, nfds,
  519. struct timespec *, timeout, const __sigset_t *, sigmask,
  520. size_t, sigsetsize)
  521. #endif
  522. #if defined(TARGET_NR_pselect6)
  523. #ifndef __NR_pselect6
  524. # define __NR_pselect6 -1
  525. #endif
  526. #define __NR_sys_pselect6 __NR_pselect6
  527. _syscall6(int, sys_pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds,
  528. fd_set *, exceptfds, struct timespec *, timeout, void *, sig);
  529. #endif
  530. #if defined(TARGET_NR_prlimit64)
  531. #ifndef __NR_prlimit64
  532. # define __NR_prlimit64 -1
  533. #endif
  534. #define __NR_sys_prlimit64 __NR_prlimit64
  535. /* The glibc rlimit structure may not be that used by the underlying syscall */
  536. struct host_rlimit64 {
  537. uint64_t rlim_cur;
  538. uint64_t rlim_max;
  539. };
  540. _syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
  541. const struct host_rlimit64 *, new_limit,
  542. struct host_rlimit64 *, old_limit)
  543. #endif
  544. extern int personality(int);
  545. extern int flock(int, int);
  546. extern int setfsuid(int);
  547. extern int setfsgid(int);
  548. extern int setgroups(int, gid_t *);
  549. /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
  550. #ifdef TARGET_ARM
  551. static inline int regpairs_aligned(void *cpu_env) {
  552. return ((((CPUARMState *)cpu_env)->eabi) == 1) ;
  553. }
  554. #elif defined(TARGET_MIPS)
  555. static inline int regpairs_aligned(void *cpu_env) { return 1; }
  556. #else
  557. static inline int regpairs_aligned(void *cpu_env) { return 0; }
  558. #endif
  559. #define ERRNO_TABLE_SIZE 1200
  560. /* target_to_host_errno_table[] is initialized from
  561. * host_to_target_errno_table[] in syscall_init(). */
  562. static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
  563. };
  564. /*
  565. * This list is the union of errno values overridden in asm-<arch>/errno.h
  566. * minus the errnos that are not actually generic to all archs.
  567. */
  568. static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
  569. [EIDRM] = TARGET_EIDRM,
  570. [ECHRNG] = TARGET_ECHRNG,
  571. [EL2NSYNC] = TARGET_EL2NSYNC,
  572. [EL3HLT] = TARGET_EL3HLT,
  573. [EL3RST] = TARGET_EL3RST,
  574. [ELNRNG] = TARGET_ELNRNG,
  575. [EUNATCH] = TARGET_EUNATCH,
  576. [ENOCSI] = TARGET_ENOCSI,
  577. [EL2HLT] = TARGET_EL2HLT,
  578. [EDEADLK] = TARGET_EDEADLK,
  579. [ENOLCK] = TARGET_ENOLCK,
  580. [EBADE] = TARGET_EBADE,
  581. [EBADR] = TARGET_EBADR,
  582. [EXFULL] = TARGET_EXFULL,
  583. [ENOANO] = TARGET_ENOANO,
  584. [EBADRQC] = TARGET_EBADRQC,
  585. [EBADSLT] = TARGET_EBADSLT,
  586. [EBFONT] = TARGET_EBFONT,
  587. [ENOSTR] = TARGET_ENOSTR,
  588. [ENODATA] = TARGET_ENODATA,
  589. [ETIME] = TARGET_ETIME,
  590. [ENOSR] = TARGET_ENOSR,
  591. [ENONET] = TARGET_ENONET,
  592. [ENOPKG] = TARGET_ENOPKG,
  593. [EREMOTE] = TARGET_EREMOTE,
  594. [ENOLINK] = TARGET_ENOLINK,
  595. [EADV] = TARGET_EADV,
  596. [ESRMNT] = TARGET_ESRMNT,
  597. [ECOMM] = TARGET_ECOMM,
  598. [EPROTO] = TARGET_EPROTO,
  599. [EDOTDOT] = TARGET_EDOTDOT,
  600. [EMULTIHOP] = TARGET_EMULTIHOP,
  601. [EBADMSG] = TARGET_EBADMSG,
  602. [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
  603. [EOVERFLOW] = TARGET_EOVERFLOW,
  604. [ENOTUNIQ] = TARGET_ENOTUNIQ,
  605. [EBADFD] = TARGET_EBADFD,
  606. [EREMCHG] = TARGET_EREMCHG,
  607. [ELIBACC] = TARGET_ELIBACC,
  608. [ELIBBAD] = TARGET_ELIBBAD,
  609. [ELIBSCN] = TARGET_ELIBSCN,
  610. [ELIBMAX] = TARGET_ELIBMAX,
  611. [ELIBEXEC] = TARGET_ELIBEXEC,
  612. [EILSEQ] = TARGET_EILSEQ,
  613. [ENOSYS] = TARGET_ENOSYS,
  614. [ELOOP] = TARGET_ELOOP,
  615. [ERESTART] = TARGET_ERESTART,
  616. [ESTRPIPE] = TARGET_ESTRPIPE,
  617. [ENOTEMPTY] = TARGET_ENOTEMPTY,
  618. [EUSERS] = TARGET_EUSERS,
  619. [ENOTSOCK] = TARGET_ENOTSOCK,
  620. [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
  621. [EMSGSIZE] = TARGET_EMSGSIZE,
  622. [EPROTOTYPE] = TARGET_EPROTOTYPE,
  623. [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
  624. [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
  625. [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
  626. [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
  627. [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
  628. [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
  629. [EADDRINUSE] = TARGET_EADDRINUSE,
  630. [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
  631. [ENETDOWN] = TARGET_ENETDOWN,
  632. [ENETUNREACH] = TARGET_ENETUNREACH,
  633. [ENETRESET] = TARGET_ENETRESET,
  634. [ECONNABORTED] = TARGET_ECONNABORTED,
  635. [ECONNRESET] = TARGET_ECONNRESET,
  636. [ENOBUFS] = TARGET_ENOBUFS,
  637. [EISCONN] = TARGET_EISCONN,
  638. [ENOTCONN] = TARGET_ENOTCONN,
  639. [EUCLEAN] = TARGET_EUCLEAN,
  640. [ENOTNAM] = TARGET_ENOTNAM,
  641. [ENAVAIL] = TARGET_ENAVAIL,
  642. [EISNAM] = TARGET_EISNAM,
  643. [EREMOTEIO] = TARGET_EREMOTEIO,
  644. [ESHUTDOWN] = TARGET_ESHUTDOWN,
  645. [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
  646. [ETIMEDOUT] = TARGET_ETIMEDOUT,
  647. [ECONNREFUSED] = TARGET_ECONNREFUSED,
  648. [EHOSTDOWN] = TARGET_EHOSTDOWN,
  649. [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
  650. [EALREADY] = TARGET_EALREADY,
  651. [EINPROGRESS] = TARGET_EINPROGRESS,
  652. [ESTALE] = TARGET_ESTALE,
  653. [ECANCELED] = TARGET_ECANCELED,
  654. [ENOMEDIUM] = TARGET_ENOMEDIUM,
  655. [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
  656. #ifdef ENOKEY
  657. [ENOKEY] = TARGET_ENOKEY,
  658. #endif
  659. #ifdef EKEYEXPIRED
  660. [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
  661. #endif
  662. #ifdef EKEYREVOKED
  663. [EKEYREVOKED] = TARGET_EKEYREVOKED,
  664. #endif
  665. #ifdef EKEYREJECTED
  666. [EKEYREJECTED] = TARGET_EKEYREJECTED,
  667. #endif
  668. #ifdef EOWNERDEAD
  669. [EOWNERDEAD] = TARGET_EOWNERDEAD,
  670. #endif
  671. #ifdef ENOTRECOVERABLE
  672. [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
  673. #endif
  674. };
  675. static inline int host_to_target_errno(int err)
  676. {
  677. if(host_to_target_errno_table[err])
  678. return host_to_target_errno_table[err];
  679. return err;
  680. }
  681. static inline int target_to_host_errno(int err)
  682. {
  683. if (target_to_host_errno_table[err])
  684. return target_to_host_errno_table[err];
  685. return err;
  686. }
  687. static inline abi_long get_errno(abi_long ret)
  688. {
  689. if (ret == -1)
  690. return -host_to_target_errno(errno);
  691. else
  692. return ret;
  693. }
  694. static inline int is_error(abi_long ret)
  695. {
  696. return (abi_ulong)ret >= (abi_ulong)(-4096);
  697. }
  698. char *target_strerror(int err)
  699. {
  700. if ((err >= ERRNO_TABLE_SIZE) || (err < 0)) {
  701. return NULL;
  702. }
  703. return strerror(target_to_host_errno(err));
  704. }
  705. static abi_ulong target_brk;
  706. static abi_ulong target_original_brk;
  707. static abi_ulong brk_page;
  708. void target_set_brk(abi_ulong new_brk)
  709. {
  710. target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
  711. brk_page = HOST_PAGE_ALIGN(target_brk);
  712. }
  713. //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
  714. #define DEBUGF_BRK(message, args...)
  715. /* do_brk() must return target values and target errnos. */
  716. abi_long do_brk(abi_ulong new_brk)
  717. {
  718. abi_long mapped_addr;
  719. int new_alloc_size;
  720. DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
  721. if (!new_brk) {
  722. DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
  723. return target_brk;
  724. }
  725. if (new_brk < target_original_brk) {
  726. DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
  727. target_brk);
  728. return target_brk;
  729. }
  730. /* If the new brk is less than the highest page reserved to the
  731. * target heap allocation, set it and we're almost done... */
  732. if (new_brk <= brk_page) {
  733. /* Heap contents are initialized to zero, as for anonymous
  734. * mapped pages. */
  735. if (new_brk > target_brk) {
  736. memset(g2h(target_brk), 0, new_brk - target_brk);
  737. }
  738. target_brk = new_brk;
  739. DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
  740. return target_brk;
  741. }
  742. /* We need to allocate more memory after the brk... Note that
  743. * we don't use MAP_FIXED because that will map over the top of
  744. * any existing mapping (like the one with the host libc or qemu
  745. * itself); instead we treat "mapped but at wrong address" as
  746. * a failure and unmap again.
  747. */
  748. new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
  749. mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
  750. PROT_READ|PROT_WRITE,
  751. MAP_ANON|MAP_PRIVATE, 0, 0));
  752. if (mapped_addr == brk_page) {
  753. /* Heap contents are initialized to zero, as for anonymous
  754. * mapped pages. Technically the new pages are already
  755. * initialized to zero since they *are* anonymous mapped
  756. * pages, however we have to take care with the contents that
  757. * come from the remaining part of the previous page: it may
  758. * contains garbage data due to a previous heap usage (grown
  759. * then shrunken). */
  760. memset(g2h(target_brk), 0, brk_page - target_brk);
  761. target_brk = new_brk;
  762. brk_page = HOST_PAGE_ALIGN(target_brk);
  763. DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
  764. target_brk);
  765. return target_brk;
  766. } else if (mapped_addr != -1) {
  767. /* Mapped but at wrong address, meaning there wasn't actually
  768. * enough space for this brk.
  769. */
  770. target_munmap(mapped_addr, new_alloc_size);
  771. mapped_addr = -1;
  772. DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
  773. }
  774. else {
  775. DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
  776. }
  777. #if defined(TARGET_ALPHA)
  778. /* We (partially) emulate OSF/1 on Alpha, which requires we
  779. return a proper errno, not an unchanged brk value. */
  780. return -TARGET_ENOMEM;
  781. #endif
  782. /* For everything else, return the previous break. */
  783. return target_brk;
  784. }
  785. static inline abi_long copy_from_user_fdset(fd_set *fds,
  786. abi_ulong target_fds_addr,
  787. int n)
  788. {
  789. int i, nw, j, k;
  790. abi_ulong b, *target_fds;
  791. nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
  792. if (!(target_fds = lock_user(VERIFY_READ,
  793. target_fds_addr,
  794. sizeof(abi_ulong) * nw,
  795. 1)))
  796. return -TARGET_EFAULT;
  797. FD_ZERO(fds);
  798. k = 0;
  799. for (i = 0; i < nw; i++) {
  800. /* grab the abi_ulong */
  801. __get_user(b, &target_fds[i]);
  802. for (j = 0; j < TARGET_ABI_BITS; j++) {
  803. /* check the bit inside the abi_ulong */
  804. if ((b >> j) & 1)
  805. FD_SET(k, fds);
  806. k++;
  807. }
  808. }
  809. unlock_user(target_fds, target_fds_addr, 0);
  810. return 0;
  811. }
  812. static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
  813. abi_ulong target_fds_addr,
  814. int n)
  815. {
  816. if (target_fds_addr) {
  817. if (copy_from_user_fdset(fds, target_fds_addr, n))
  818. return -TARGET_EFAULT;
  819. *fds_ptr = fds;
  820. } else {
  821. *fds_ptr = NULL;
  822. }
  823. return 0;
  824. }
  825. static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
  826. const fd_set *fds,
  827. int n)
  828. {
  829. int i, nw, j, k;
  830. abi_long v;
  831. abi_ulong *target_fds;
  832. nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
  833. if (!(target_fds = lock_user(VERIFY_WRITE,
  834. target_fds_addr,
  835. sizeof(abi_ulong) * nw,
  836. 0)))
  837. return -TARGET_EFAULT;
  838. k = 0;
  839. for (i = 0; i < nw; i++) {
  840. v = 0;
  841. for (j = 0; j < TARGET_ABI_BITS; j++) {
  842. v |= ((FD_ISSET(k, fds) != 0) << j);
  843. k++;
  844. }
  845. __put_user(v, &target_fds[i]);
  846. }
  847. unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
  848. return 0;
  849. }
  850. #if defined(__alpha__)
  851. #define HOST_HZ 1024
  852. #else
  853. #define HOST_HZ 100
  854. #endif
  855. static inline abi_long host_to_target_clock_t(long ticks)
  856. {
  857. #if HOST_HZ == TARGET_HZ
  858. return ticks;
  859. #else
  860. return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
  861. #endif
  862. }
  863. static inline abi_long host_to_target_rusage(abi_ulong target_addr,
  864. const struct rusage *rusage)
  865. {
  866. struct target_rusage *target_rusage;
  867. if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
  868. return -TARGET_EFAULT;
  869. target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
  870. target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
  871. target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
  872. target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
  873. target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
  874. target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
  875. target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
  876. target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
  877. target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
  878. target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
  879. target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
  880. target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
  881. target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
  882. target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
  883. target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
  884. target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
  885. target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
  886. target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
  887. unlock_user_struct(target_rusage, target_addr, 1);
  888. return 0;
  889. }
  890. static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
  891. {
  892. abi_ulong target_rlim_swap;
  893. rlim_t result;
  894. target_rlim_swap = tswapal(target_rlim);
  895. if (target_rlim_swap == TARGET_RLIM_INFINITY)
  896. return RLIM_INFINITY;
  897. result = target_rlim_swap;
  898. if (target_rlim_swap != (rlim_t)result)
  899. return RLIM_INFINITY;
  900. return result;
  901. }
  902. static inline abi_ulong host_to_target_rlim(rlim_t rlim)
  903. {
  904. abi_ulong target_rlim_swap;
  905. abi_ulong result;
  906. if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
  907. target_rlim_swap = TARGET_RLIM_INFINITY;
  908. else
  909. target_rlim_swap = rlim;
  910. result = tswapal(target_rlim_swap);
  911. return result;
  912. }
  913. static inline int target_to_host_resource(int code)
  914. {
  915. switch (code) {
  916. case TARGET_RLIMIT_AS:
  917. return RLIMIT_AS;
  918. case TARGET_RLIMIT_CORE:
  919. return RLIMIT_CORE;
  920. case TARGET_RLIMIT_CPU:
  921. return RLIMIT_CPU;
  922. case TARGET_RLIMIT_DATA:
  923. return RLIMIT_DATA;
  924. case TARGET_RLIMIT_FSIZE:
  925. return RLIMIT_FSIZE;
  926. case TARGET_RLIMIT_LOCKS:
  927. return RLIMIT_LOCKS;
  928. case TARGET_RLIMIT_MEMLOCK:
  929. return RLIMIT_MEMLOCK;
  930. case TARGET_RLIMIT_MSGQUEUE:
  931. return RLIMIT_MSGQUEUE;
  932. case TARGET_RLIMIT_NICE:
  933. return RLIMIT_NICE;
  934. case TARGET_RLIMIT_NOFILE:
  935. return RLIMIT_NOFILE;
  936. case TARGET_RLIMIT_NPROC:
  937. return RLIMIT_NPROC;
  938. case TARGET_RLIMIT_RSS:
  939. return RLIMIT_RSS;
  940. case TARGET_RLIMIT_RTPRIO:
  941. return RLIMIT_RTPRIO;
  942. case TARGET_RLIMIT_SIGPENDING:
  943. return RLIMIT_SIGPENDING;
  944. case TARGET_RLIMIT_STACK:
  945. return RLIMIT_STACK;
  946. default:
  947. return code;
  948. }
  949. }
  950. static inline abi_long copy_from_user_timeval(struct timeval *tv,
  951. abi_ulong target_tv_addr)
  952. {
  953. struct target_timeval *target_tv;
  954. if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
  955. return -TARGET_EFAULT;
  956. __get_user(tv->tv_sec, &target_tv->tv_sec);
  957. __get_user(tv->tv_usec, &target_tv->tv_usec);
  958. unlock_user_struct(target_tv, target_tv_addr, 0);
  959. return 0;
  960. }
  961. static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
  962. const struct timeval *tv)
  963. {
  964. struct target_timeval *target_tv;
  965. if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
  966. return -TARGET_EFAULT;
  967. __put_user(tv->tv_sec, &target_tv->tv_sec);
  968. __put_user(tv->tv_usec, &target_tv->tv_usec);
  969. unlock_user_struct(target_tv, target_tv_addr, 1);
  970. return 0;
  971. }
  972. #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
  973. #include <mqueue.h>
  974. static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
  975. abi_ulong target_mq_attr_addr)
  976. {
  977. struct target_mq_attr *target_mq_attr;
  978. if (!lock_user_struct(VERIFY_READ, target_mq_attr,
  979. target_mq_attr_addr, 1))
  980. return -TARGET_EFAULT;
  981. __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
  982. __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
  983. __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
  984. __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
  985. unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
  986. return 0;
  987. }
  988. static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
  989. const struct mq_attr *attr)
  990. {
  991. struct target_mq_attr *target_mq_attr;
  992. if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
  993. target_mq_attr_addr, 0))
  994. return -TARGET_EFAULT;
  995. __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
  996. __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
  997. __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
  998. __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
  999. unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
  1000. return 0;
  1001. }
  1002. #endif
  1003. #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
  1004. /* do_select() must return target values and target errnos. */
  1005. static abi_long do_select(int n,
  1006. abi_ulong rfd_addr, abi_ulong wfd_addr,
  1007. abi_ulong efd_addr, abi_ulong target_tv_addr)
  1008. {
  1009. fd_set rfds, wfds, efds;
  1010. fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
  1011. struct timeval tv, *tv_ptr;
  1012. abi_long ret;
  1013. ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
  1014. if (ret) {
  1015. return ret;
  1016. }
  1017. ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
  1018. if (ret) {
  1019. return ret;
  1020. }
  1021. ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
  1022. if (ret) {
  1023. return ret;
  1024. }
  1025. if (target_tv_addr) {
  1026. if (copy_from_user_timeval(&tv, target_tv_addr))
  1027. return -TARGET_EFAULT;
  1028. tv_ptr = &tv;
  1029. } else {
  1030. tv_ptr = NULL;
  1031. }
  1032. ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
  1033. if (!is_error(ret)) {
  1034. if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
  1035. return -TARGET_EFAULT;
  1036. if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
  1037. return -TARGET_EFAULT;
  1038. if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
  1039. return -TARGET_EFAULT;
  1040. if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
  1041. return -TARGET_EFAULT;
  1042. }
  1043. return ret;
  1044. }
  1045. #endif
  1046. static abi_long do_pipe2(int host_pipe[], int flags)
  1047. {
  1048. #ifdef CONFIG_PIPE2
  1049. return pipe2(host_pipe, flags);
  1050. #else
  1051. return -ENOSYS;
  1052. #endif
  1053. }
  1054. static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
  1055. int flags, int is_pipe2)
  1056. {
  1057. int host_pipe[2];
  1058. abi_long ret;
  1059. ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
  1060. if (is_error(ret))
  1061. return get_errno(ret);
  1062. /* Several targets have special calling conventions for the original
  1063. pipe syscall, but didn't replicate this into the pipe2 syscall. */
  1064. if (!is_pipe2) {
  1065. #if defined(TARGET_ALPHA)
  1066. ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
  1067. return host_pipe[0];
  1068. #elif defined(TARGET_MIPS)
  1069. ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
  1070. return host_pipe[0];
  1071. #elif defined(TARGET_SH4)
  1072. ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
  1073. return host_pipe[0];
  1074. #endif
  1075. }
  1076. if (put_user_s32(host_pipe[0], pipedes)
  1077. || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
  1078. return -TARGET_EFAULT;
  1079. return get_errno(ret);
  1080. }
  1081. static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
  1082. abi_ulong target_addr,
  1083. socklen_t len)
  1084. {
  1085. struct target_ip_mreqn *target_smreqn;
  1086. target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
  1087. if (!target_smreqn)
  1088. return -TARGET_EFAULT;
  1089. mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
  1090. mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
  1091. if (len == sizeof(struct target_ip_mreqn))
  1092. mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
  1093. unlock_user(target_smreqn, target_addr, 0);
  1094. return 0;
  1095. }
  1096. static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
  1097. abi_ulong target_addr,
  1098. socklen_t len)
  1099. {
  1100. const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
  1101. sa_family_t sa_family;
  1102. struct target_sockaddr *target_saddr;
  1103. target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
  1104. if (!target_saddr)
  1105. return -TARGET_EFAULT;
  1106. sa_family = tswap16(target_saddr->sa_family);
  1107. /* Oops. The caller might send a incomplete sun_path; sun_path
  1108. * must be terminated by \0 (see the manual page), but
  1109. * unfortunately it is quite common to specify sockaddr_un
  1110. * length as "strlen(x->sun_path)" while it should be
  1111. * "strlen(...) + 1". We'll fix that here if needed.
  1112. * Linux kernel has a similar feature.
  1113. */
  1114. if (sa_family == AF_UNIX) {
  1115. if (len < unix_maxlen && len > 0) {
  1116. char *cp = (char*)target_saddr;
  1117. if ( cp[len-1] && !cp[len] )
  1118. len++;
  1119. }
  1120. if (len > unix_maxlen)
  1121. len = unix_maxlen;
  1122. }
  1123. memcpy(addr, target_saddr, len);
  1124. addr->sa_family = sa_family;
  1125. unlock_user(target_saddr, target_addr, 0);
  1126. return 0;
  1127. }
  1128. static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
  1129. struct sockaddr *addr,
  1130. socklen_t len)
  1131. {
  1132. struct target_sockaddr *target_saddr;
  1133. target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
  1134. if (!target_saddr)
  1135. return -TARGET_EFAULT;
  1136. memcpy(target_saddr, addr, len);
  1137. target_saddr->sa_family = tswap16(addr->sa_family);
  1138. unlock_user(target_saddr, target_addr, len);
  1139. return 0;
  1140. }
  1141. static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
  1142. struct target_msghdr *target_msgh)
  1143. {
  1144. struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
  1145. abi_long msg_controllen;
  1146. abi_ulong target_cmsg_addr;
  1147. struct target_cmsghdr *target_cmsg;
  1148. socklen_t space = 0;
  1149. msg_controllen = tswapal(target_msgh->msg_controllen);
  1150. if (msg_controllen < sizeof (struct target_cmsghdr))
  1151. goto the_end;
  1152. target_cmsg_addr = tswapal(target_msgh->msg_control);
  1153. target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
  1154. if (!target_cmsg)
  1155. return -TARGET_EFAULT;
  1156. while (cmsg && target_cmsg) {
  1157. void *data = CMSG_DATA(cmsg);
  1158. void *target_data = TARGET_CMSG_DATA(target_cmsg);
  1159. int len = tswapal(target_cmsg->cmsg_len)
  1160. - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
  1161. space += CMSG_SPACE(len);
  1162. if (space > msgh->msg_controllen) {
  1163. space -= CMSG_SPACE(len);
  1164. gemu_log("Host cmsg overflow\n");
  1165. break;
  1166. }
  1167. cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
  1168. cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
  1169. cmsg->cmsg_len = CMSG_LEN(len);
  1170. if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
  1171. gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
  1172. memcpy(data, target_data, len);
  1173. } else {
  1174. int *fd = (int *)data;
  1175. int *target_fd = (int *)target_data;
  1176. int i, numfds = len / sizeof(int);
  1177. for (i = 0; i < numfds; i++)
  1178. fd[i] = tswap32(target_fd[i]);
  1179. }
  1180. cmsg = CMSG_NXTHDR(msgh, cmsg);
  1181. target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
  1182. }
  1183. unlock_user(target_cmsg, target_cmsg_addr, 0);
  1184. the_end:
  1185. msgh->msg_controllen = space;
  1186. return 0;
  1187. }
  1188. static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
  1189. struct msghdr *msgh)
  1190. {
  1191. struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
  1192. abi_long msg_controllen;
  1193. abi_ulong target_cmsg_addr;
  1194. struct target_cmsghdr *target_cmsg;
  1195. socklen_t space = 0;
  1196. msg_controllen = tswapal(target_msgh->msg_controllen);
  1197. if (msg_controllen < sizeof (struct target_cmsghdr))
  1198. goto the_end;
  1199. target_cmsg_addr = tswapal(target_msgh->msg_control);
  1200. target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
  1201. if (!target_cmsg)
  1202. return -TARGET_EFAULT;
  1203. while (cmsg && target_cmsg) {
  1204. void *data = CMSG_DATA(cmsg);
  1205. void *target_data = TARGET_CMSG_DATA(target_cmsg);
  1206. int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
  1207. space += TARGET_CMSG_SPACE(len);
  1208. if (space > msg_controllen) {
  1209. space -= TARGET_CMSG_SPACE(len);
  1210. gemu_log("Target cmsg overflow\n");
  1211. break;
  1212. }
  1213. target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
  1214. target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
  1215. target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(len));
  1216. if ((cmsg->cmsg_level == TARGET_SOL_SOCKET) &&
  1217. (cmsg->cmsg_type == SCM_RIGHTS)) {
  1218. int *fd = (int *)data;
  1219. int *target_fd = (int *)target_data;
  1220. int i, numfds = len / sizeof(int);
  1221. for (i = 0; i < numfds; i++)
  1222. target_fd[i] = tswap32(fd[i]);
  1223. } else if ((cmsg->cmsg_level == TARGET_SOL_SOCKET) &&
  1224. (cmsg->cmsg_type == SO_TIMESTAMP) &&
  1225. (len == sizeof(struct timeval))) {
  1226. /* copy struct timeval to target */
  1227. struct timeval *tv = (struct timeval *)data;
  1228. struct target_timeval *target_tv =
  1229. (struct target_timeval *)target_data;
  1230. target_tv->tv_sec = tswapal(tv->tv_sec);
  1231. target_tv->tv_usec = tswapal(tv->tv_usec);
  1232. } else {
  1233. gemu_log("Unsupported ancillary data: %d/%d\n",
  1234. cmsg->cmsg_level, cmsg->cmsg_type);
  1235. memcpy(target_data, data, len);
  1236. }
  1237. cmsg = CMSG_NXTHDR(msgh, cmsg);
  1238. target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
  1239. }
  1240. unlock_user(target_cmsg, target_cmsg_addr, space);
  1241. the_end:
  1242. target_msgh->msg_controllen = tswapal(space);
  1243. return 0;
  1244. }
  1245. /* do_setsockopt() Must return target values and target errnos. */
  1246. static abi_long do_setsockopt(int sockfd, int level, int optname,
  1247. abi_ulong optval_addr, socklen_t optlen)
  1248. {
  1249. abi_long ret;
  1250. int val;
  1251. struct ip_mreqn *ip_mreq;
  1252. struct ip_mreq_source *ip_mreq_source;
  1253. switch(level) {
  1254. case SOL_TCP:
  1255. /* TCP options all take an 'int' value. */
  1256. if (optlen < sizeof(uint32_t))
  1257. return -TARGET_EINVAL;
  1258. if (get_user_u32(val, optval_addr))
  1259. return -TARGET_EFAULT;
  1260. ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
  1261. break;
  1262. case SOL_IP:
  1263. switch(optname) {
  1264. case IP_TOS:
  1265. case IP_TTL:
  1266. case IP_HDRINCL:
  1267. case IP_ROUTER_ALERT:
  1268. case IP_RECVOPTS:
  1269. case IP_RETOPTS:
  1270. case IP_PKTINFO:
  1271. case IP_MTU_DISCOVER:
  1272. case IP_RECVERR:
  1273. case IP_RECVTOS:
  1274. #ifdef IP_FREEBIND
  1275. case IP_FREEBIND:
  1276. #endif
  1277. case IP_MULTICAST_TTL:
  1278. case IP_MULTICAST_LOOP:
  1279. val = 0;
  1280. if (optlen >= sizeof(uint32_t)) {
  1281. if (get_user_u32(val, optval_addr))
  1282. return -TARGET_EFAULT;
  1283. } else if (optlen >= 1) {
  1284. if (get_user_u8(val, optval_addr))
  1285. return -TARGET_EFAULT;
  1286. }
  1287. ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
  1288. break;
  1289. case IP_ADD_MEMBERSHIP:
  1290. case IP_DROP_MEMBERSHIP:
  1291. if (optlen < sizeof (struct target_ip_mreq) ||
  1292. optlen > sizeof (struct target_ip_mreqn))
  1293. return -TARGET_EINVAL;
  1294. ip_mreq = (struct ip_mreqn *) alloca(optlen);
  1295. target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
  1296. ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
  1297. break;
  1298. case IP_BLOCK_SOURCE:
  1299. case IP_UNBLOCK_SOURCE:
  1300. case IP_ADD_SOURCE_MEMBERSHIP:
  1301. case IP_DROP_SOURCE_MEMBERSHIP:
  1302. if (optlen != sizeof (struct target_ip_mreq_source))
  1303. return -TARGET_EINVAL;
  1304. ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
  1305. ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
  1306. unlock_user (ip_mreq_source, optval_addr, 0);
  1307. break;
  1308. default:
  1309. goto unimplemented;
  1310. }
  1311. break;
  1312. case SOL_RAW:
  1313. switch (optname) {
  1314. case ICMP_FILTER:
  1315. /* struct icmp_filter takes an u32 value */
  1316. if (optlen < sizeof(uint32_t)) {
  1317. return -TARGET_EINVAL;
  1318. }
  1319. if (get_user_u32(val, optval_addr)) {
  1320. return -TARGET_EFAULT;
  1321. }
  1322. ret = get_errno(setsockopt(sockfd, level, optname,
  1323. &val, sizeof(val)));
  1324. break;
  1325. default:
  1326. goto unimplemented;
  1327. }
  1328. break;
  1329. case TARGET_SOL_SOCKET:
  1330. switch (optname) {
  1331. /* Options with 'int' argument. */
  1332. case TARGET_SO_DEBUG:
  1333. optname = SO_DEBUG;
  1334. break;
  1335. case TARGET_SO_REUSEADDR:
  1336. optname = SO_REUSEADDR;
  1337. break;
  1338. case TARGET_SO_TYPE:
  1339. optname = SO_TYPE;
  1340. break;
  1341. case TARGET_SO_ERROR:
  1342. optname = SO_ERROR;
  1343. break;
  1344. case TARGET_SO_DONTROUTE:
  1345. optname = SO_DONTROUTE;
  1346. break;
  1347. case TARGET_SO_BROADCAST:
  1348. optname = SO_BROADCAST;
  1349. break;
  1350. case TARGET_SO_SNDBUF:
  1351. optname = SO_SNDBUF;
  1352. break;
  1353. case TARGET_SO_RCVBUF:
  1354. optname = SO_RCVBUF;
  1355. break;
  1356. case TARGET_SO_KEEPALIVE:
  1357. optname = SO_KEEPALIVE;
  1358. break;
  1359. case TARGET_SO_OOBINLINE:
  1360. optname = SO_OOBINLINE;
  1361. break;
  1362. case TARGET_SO_NO_CHECK:
  1363. optname = SO_NO_CHECK;
  1364. break;
  1365. case TARGET_SO_PRIORITY:
  1366. optname = SO_PRIORITY;
  1367. break;
  1368. #ifdef SO_BSDCOMPAT
  1369. case TARGET_SO_BSDCOMPAT:
  1370. optname = SO_BSDCOMPAT;
  1371. break;
  1372. #endif
  1373. case TARGET_SO_PASSCRED:
  1374. optname = SO_PASSCRED;
  1375. break;
  1376. case TARGET_SO_TIMESTAMP:
  1377. optname = SO_TIMESTAMP;
  1378. break;
  1379. case TARGET_SO_RCVLOWAT:
  1380. optname = SO_RCVLOWAT;
  1381. break;
  1382. case TARGET_SO_RCVTIMEO:
  1383. optname = SO_RCVTIMEO;
  1384. break;
  1385. case TARGET_SO_SNDTIMEO:
  1386. optname = SO_SNDTIMEO;
  1387. break;
  1388. break;
  1389. default:
  1390. goto unimplemented;
  1391. }
  1392. if (optlen < sizeof(uint32_t))
  1393. return -TARGET_EINVAL;
  1394. if (get_user_u32(val, optval_addr))
  1395. return -TARGET_EFAULT;
  1396. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
  1397. break;
  1398. default:
  1399. unimplemented:
  1400. gemu_log("Unsupported setsockopt level=%d optname=%d\n", level, optname);
  1401. ret = -TARGET_ENOPROTOOPT;
  1402. }
  1403. return ret;
  1404. }
  1405. /* do_getsockopt() Must return target values and target errnos. */
  1406. static abi_long do_getsockopt(int sockfd, int level, int optname,
  1407. abi_ulong optval_addr, abi_ulong optlen)
  1408. {
  1409. abi_long ret;
  1410. int len, val;
  1411. socklen_t lv;
  1412. switch(level) {
  1413. case TARGET_SOL_SOCKET:
  1414. level = SOL_SOCKET;
  1415. switch (optname) {
  1416. /* These don't just return a single integer */
  1417. case TARGET_SO_LINGER:
  1418. case TARGET_SO_RCVTIMEO:
  1419. case TARGET_SO_SNDTIMEO:
  1420. case TARGET_SO_PEERNAME:
  1421. goto unimplemented;
  1422. case TARGET_SO_PEERCRED: {
  1423. struct ucred cr;
  1424. socklen_t crlen;
  1425. struct target_ucred *tcr;
  1426. if (get_user_u32(len, optlen)) {
  1427. return -TARGET_EFAULT;
  1428. }
  1429. if (len < 0) {
  1430. return -TARGET_EINVAL;
  1431. }
  1432. crlen = sizeof(cr);
  1433. ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
  1434. &cr, &crlen));
  1435. if (ret < 0) {
  1436. return ret;
  1437. }
  1438. if (len > crlen) {
  1439. len = crlen;
  1440. }
  1441. if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
  1442. return -TARGET_EFAULT;
  1443. }
  1444. __put_user(cr.pid, &tcr->pid);
  1445. __put_user(cr.uid, &tcr->uid);
  1446. __put_user(cr.gid, &tcr->gid);
  1447. unlock_user_struct(tcr, optval_addr, 1);
  1448. if (put_user_u32(len, optlen)) {
  1449. return -TARGET_EFAULT;
  1450. }
  1451. break;
  1452. }
  1453. /* Options with 'int' argument. */
  1454. case TARGET_SO_DEBUG:
  1455. optname = SO_DEBUG;
  1456. goto int_case;
  1457. case TARGET_SO_REUSEADDR:
  1458. optname = SO_REUSEADDR;
  1459. goto int_case;
  1460. case TARGET_SO_TYPE:
  1461. optname = SO_TYPE;
  1462. goto int_case;
  1463. case TARGET_SO_ERROR:
  1464. optname = SO_ERROR;
  1465. goto int_case;
  1466. case TARGET_SO_DONTROUTE:
  1467. optname = SO_DONTROUTE;
  1468. goto int_case;
  1469. case TARGET_SO_BROADCAST:
  1470. optname = SO_BROADCAST;
  1471. goto int_case;
  1472. case TARGET_SO_SNDBUF:
  1473. optname = SO_SNDBUF;
  1474. goto int_case;
  1475. case TARGET_SO_RCVBUF:
  1476. optname = SO_RCVBUF;
  1477. goto int_case;
  1478. case TARGET_SO_KEEPALIVE:
  1479. optname = SO_KEEPALIVE;
  1480. goto int_case;
  1481. case TARGET_SO_OOBINLINE:
  1482. optname = SO_OOBINLINE;
  1483. goto int_case;
  1484. case TARGET_SO_NO_CHECK:
  1485. optname = SO_NO_CHECK;
  1486. goto int_case;
  1487. case TARGET_SO_PRIORITY:
  1488. optname = SO_PRIORITY;
  1489. goto int_case;
  1490. #ifdef SO_BSDCOMPAT
  1491. case TARGET_SO_BSDCOMPAT:
  1492. optname = SO_BSDCOMPAT;
  1493. goto int_case;
  1494. #endif
  1495. case TARGET_SO_PASSCRED:
  1496. optname = SO_PASSCRED;
  1497. goto int_case;
  1498. case TARGET_SO_TIMESTAMP:
  1499. optname = SO_TIMESTAMP;
  1500. goto int_case;
  1501. case TARGET_SO_RCVLOWAT:
  1502. optname = SO_RCVLOWAT;
  1503. goto int_case;
  1504. default:
  1505. goto int_case;
  1506. }
  1507. break;
  1508. case SOL_TCP:
  1509. /* TCP options all take an 'int' value. */
  1510. int_case:
  1511. if (get_user_u32(len, optlen))
  1512. return -TARGET_EFAULT;
  1513. if (len < 0)
  1514. return -TARGET_EINVAL;
  1515. lv = sizeof(lv);
  1516. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  1517. if (ret < 0)
  1518. return ret;
  1519. if (len > lv)
  1520. len = lv;
  1521. if (len == 4) {
  1522. if (put_user_u32(val, optval_addr))
  1523. return -TARGET_EFAULT;
  1524. } else {
  1525. if (put_user_u8(val, optval_addr))
  1526. return -TARGET_EFAULT;
  1527. }
  1528. if (put_user_u32(len, optlen))
  1529. return -TARGET_EFAULT;
  1530. break;
  1531. case SOL_IP:
  1532. switch(optname) {
  1533. case IP_TOS:
  1534. case IP_TTL:
  1535. case IP_HDRINCL:
  1536. case IP_ROUTER_ALERT:
  1537. case IP_RECVOPTS:
  1538. case IP_RETOPTS:
  1539. case IP_PKTINFO:
  1540. case IP_MTU_DISCOVER:
  1541. case IP_RECVERR:
  1542. case IP_RECVTOS:
  1543. #ifdef IP_FREEBIND
  1544. case IP_FREEBIND:
  1545. #endif
  1546. case IP_MULTICAST_TTL:
  1547. case IP_MULTICAST_LOOP:
  1548. if (get_user_u32(len, optlen))
  1549. return -TARGET_EFAULT;
  1550. if (len < 0)
  1551. return -TARGET_EINVAL;
  1552. lv = sizeof(lv);
  1553. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  1554. if (ret < 0)
  1555. return ret;
  1556. if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
  1557. len = 1;
  1558. if (put_user_u32(len, optlen)
  1559. || put_user_u8(val, optval_addr))
  1560. return -TARGET_EFAULT;
  1561. } else {
  1562. if (len > sizeof(int))
  1563. len = sizeof(int);
  1564. if (put_user_u32(len, optlen)
  1565. || put_user_u32(val, optval_addr))
  1566. return -TARGET_EFAULT;
  1567. }
  1568. break;
  1569. default:
  1570. ret = -TARGET_ENOPROTOOPT;
  1571. break;
  1572. }
  1573. break;
  1574. default:
  1575. unimplemented:
  1576. gemu_log("getsockopt level=%d optname=%d not yet supported\n",
  1577. level, optname);
  1578. ret = -TARGET_EOPNOTSUPP;
  1579. break;
  1580. }
  1581. return ret;
  1582. }
  1583. /* FIXME
  1584. * lock_iovec()/unlock_iovec() have a return code of 0 for success where
  1585. * other lock functions have a return code of 0 for failure.
  1586. */
  1587. static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
  1588. int count, int copy)
  1589. {
  1590. struct target_iovec *target_vec;
  1591. abi_ulong base;
  1592. int i;
  1593. target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
  1594. if (!target_vec)
  1595. return -TARGET_EFAULT;
  1596. for(i = 0;i < count; i++) {
  1597. base = tswapal(target_vec[i].iov_base);
  1598. vec[i].iov_len = tswapal(target_vec[i].iov_len);
  1599. if (vec[i].iov_len != 0) {
  1600. vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
  1601. /* Don't check lock_user return value. We must call writev even
  1602. if a element has invalid base address. */
  1603. } else {
  1604. /* zero length pointer is ignored */
  1605. vec[i].iov_base = NULL;
  1606. }
  1607. }
  1608. unlock_user (target_vec, target_addr, 0);
  1609. return 0;
  1610. }
  1611. static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
  1612. int count, int copy)
  1613. {
  1614. struct target_iovec *target_vec;
  1615. abi_ulong base;
  1616. int i;
  1617. target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
  1618. if (!target_vec)
  1619. return -TARGET_EFAULT;
  1620. for(i = 0;i < count; i++) {
  1621. if (target_vec[i].iov_base) {
  1622. base = tswapal(target_vec[i].iov_base);
  1623. unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
  1624. }
  1625. }
  1626. unlock_user (target_vec, target_addr, 0);
  1627. return 0;
  1628. }
  1629. /* do_socket() Must return target values and target errnos. */
  1630. static abi_long do_socket(int domain, int type, int protocol)
  1631. {
  1632. #if defined(TARGET_MIPS)
  1633. switch(type) {
  1634. case TARGET_SOCK_DGRAM:
  1635. type = SOCK_DGRAM;
  1636. break;
  1637. case TARGET_SOCK_STREAM:
  1638. type = SOCK_STREAM;
  1639. break;
  1640. case TARGET_SOCK_RAW:
  1641. type = SOCK_RAW;
  1642. break;
  1643. case TARGET_SOCK_RDM:
  1644. type = SOCK_RDM;
  1645. break;
  1646. case TARGET_SOCK_SEQPACKET:
  1647. type = SOCK_SEQPACKET;
  1648. break;
  1649. case TARGET_SOCK_PACKET:
  1650. type = SOCK_PACKET;
  1651. break;
  1652. }
  1653. #endif
  1654. if (domain == PF_NETLINK)
  1655. return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
  1656. return get_errno(socket(domain, type, protocol));
  1657. }
  1658. /* do_bind() Must return target values and target errnos. */
  1659. static abi_long do_bind(int sockfd, abi_ulong target_addr,
  1660. socklen_t addrlen)
  1661. {
  1662. void *addr;
  1663. abi_long ret;
  1664. if ((int)addrlen < 0) {
  1665. return -TARGET_EINVAL;
  1666. }
  1667. addr = alloca(addrlen+1);
  1668. ret = target_to_host_sockaddr(addr, target_addr, addrlen);
  1669. if (ret)
  1670. return ret;
  1671. return get_errno(bind(sockfd, addr, addrlen));
  1672. }
  1673. /* do_connect() Must return target values and target errnos. */
  1674. static abi_long do_connect(int sockfd, abi_ulong target_addr,
  1675. socklen_t addrlen)
  1676. {
  1677. void *addr;
  1678. abi_long ret;
  1679. if ((int)addrlen < 0) {
  1680. return -TARGET_EINVAL;
  1681. }
  1682. addr = alloca(addrlen);
  1683. ret = target_to_host_sockaddr(addr, target_addr, addrlen);
  1684. if (ret)
  1685. return ret;
  1686. return get_errno(connect(sockfd, addr, addrlen));
  1687. }
  1688. /* do_sendrecvmsg() Must return target values and target errnos. */
  1689. static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
  1690. int flags, int send)
  1691. {
  1692. abi_long ret, len;
  1693. struct target_msghdr *msgp;
  1694. struct msghdr msg;
  1695. int count;
  1696. struct iovec *vec;
  1697. abi_ulong target_vec;
  1698. /* FIXME */
  1699. if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
  1700. msgp,
  1701. target_msg,
  1702. send ? 1 : 0))
  1703. return -TARGET_EFAULT;
  1704. if (msgp->msg_name) {
  1705. msg.msg_namelen = tswap32(msgp->msg_namelen);
  1706. msg.msg_name = alloca(msg.msg_namelen);
  1707. ret = target_to_host_sockaddr(msg.msg_name, tswapal(msgp->msg_name),
  1708. msg.msg_namelen);
  1709. if (ret) {
  1710. unlock_user_struct(msgp, target_msg, send ? 0 : 1);
  1711. return ret;
  1712. }
  1713. } else {
  1714. msg.msg_name = NULL;
  1715. msg.msg_namelen = 0;
  1716. }
  1717. msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
  1718. msg.msg_control = alloca(msg.msg_controllen);
  1719. msg.msg_flags = tswap32(msgp->msg_flags);
  1720. count = tswapal(msgp->msg_iovlen);
  1721. vec = alloca(count * sizeof(struct iovec));
  1722. target_vec = tswapal(msgp->msg_iov);
  1723. lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
  1724. msg.msg_iovlen = count;
  1725. msg.msg_iov = vec;
  1726. if (send) {
  1727. ret = target_to_host_cmsg(&msg, msgp);
  1728. if (ret == 0)
  1729. ret = get_errno(sendmsg(fd, &msg, flags));
  1730. } else {
  1731. ret = get_errno(recvmsg(fd, &msg, flags));
  1732. if (!is_error(ret)) {
  1733. len = ret;
  1734. ret = host_to_target_cmsg(msgp, &msg);
  1735. if (!is_error(ret)) {
  1736. msgp->msg_namelen = tswap32(msg.msg_namelen);
  1737. if (msg.msg_name != NULL) {
  1738. ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
  1739. msg.msg_name, msg.msg_namelen);
  1740. if (ret) {
  1741. goto out;
  1742. }
  1743. }
  1744. ret = len;
  1745. }
  1746. }
  1747. }
  1748. out:
  1749. unlock_iovec(vec, target_vec, count, !send);
  1750. unlock_user_struct(msgp, target_msg, send ? 0 : 1);
  1751. return ret;
  1752. }
  1753. /* do_accept() Must return target values and target errnos. */
  1754. static abi_long do_accept(int fd, abi_ulong target_addr,
  1755. abi_ulong target_addrlen_addr)
  1756. {
  1757. socklen_t addrlen;
  1758. void *addr;
  1759. abi_long ret;
  1760. if (target_addr == 0)
  1761. return get_errno(accept(fd, NULL, NULL));
  1762. /* linux returns EINVAL if addrlen pointer is invalid */
  1763. if (get_user_u32(addrlen, target_addrlen_addr))
  1764. return -TARGET_EINVAL;
  1765. if ((int)addrlen < 0) {
  1766. return -TARGET_EINVAL;
  1767. }
  1768. if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
  1769. return -TARGET_EINVAL;
  1770. addr = alloca(addrlen);
  1771. ret = get_errno(accept(fd, addr, &addrlen));
  1772. if (!is_error(ret)) {
  1773. host_to_target_sockaddr(target_addr, addr, addrlen);
  1774. if (put_user_u32(addrlen, target_addrlen_addr))
  1775. ret = -TARGET_EFAULT;
  1776. }
  1777. return ret;
  1778. }
  1779. /* do_getpeername() Must return target values and target errnos. */
  1780. static abi_long do_getpeername(int fd, abi_ulong target_addr,
  1781. abi_ulong target_addrlen_addr)
  1782. {
  1783. socklen_t addrlen;
  1784. void *addr;
  1785. abi_long ret;
  1786. if (get_user_u32(addrlen, target_addrlen_addr))
  1787. return -TARGET_EFAULT;
  1788. if ((int)addrlen < 0) {
  1789. return -TARGET_EINVAL;
  1790. }
  1791. if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
  1792. return -TARGET_EFAULT;
  1793. addr = alloca(addrlen);
  1794. ret = get_errno(getpeername(fd, addr, &addrlen));
  1795. if (!is_error(ret)) {
  1796. host_to_target_sockaddr(target_addr, addr, addrlen);
  1797. if (put_user_u32(addrlen, target_addrlen_addr))
  1798. ret = -TARGET_EFAULT;
  1799. }
  1800. return ret;
  1801. }
  1802. /* do_getsockname() Must return target values and target errnos. */
  1803. static abi_long do_getsockname(int fd, abi_ulong target_addr,
  1804. abi_ulong target_addrlen_addr)
  1805. {
  1806. socklen_t addrlen;
  1807. void *addr;
  1808. abi_long ret;
  1809. if (get_user_u32(addrlen, target_addrlen_addr))
  1810. return -TARGET_EFAULT;
  1811. if ((int)addrlen < 0) {
  1812. return -TARGET_EINVAL;
  1813. }
  1814. if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
  1815. return -TARGET_EFAULT;
  1816. addr = alloca(addrlen);
  1817. ret = get_errno(getsockname(fd, addr, &addrlen));
  1818. if (!is_error(ret)) {
  1819. host_to_target_sockaddr(target_addr, addr, addrlen);
  1820. if (put_user_u32(addrlen, target_addrlen_addr))
  1821. ret = -TARGET_EFAULT;
  1822. }
  1823. return ret;
  1824. }
  1825. /* do_socketpair() Must return target values and target errnos. */
  1826. static abi_long do_socketpair(int domain, int type, int protocol,
  1827. abi_ulong target_tab_addr)
  1828. {
  1829. int tab[2];
  1830. abi_long ret;
  1831. ret = get_errno(socketpair(domain, type, protocol, tab));
  1832. if (!is_error(ret)) {
  1833. if (put_user_s32(tab[0], target_tab_addr)
  1834. || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
  1835. ret = -TARGET_EFAULT;
  1836. }
  1837. return ret;
  1838. }
  1839. /* do_sendto() Must return target values and target errnos. */
  1840. static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
  1841. abi_ulong target_addr, socklen_t addrlen)
  1842. {
  1843. void *addr;
  1844. void *host_msg;
  1845. abi_long ret;
  1846. if ((int)addrlen < 0) {
  1847. return -TARGET_EINVAL;
  1848. }
  1849. host_msg = lock_user(VERIFY_READ, msg, len, 1);
  1850. if (!host_msg)
  1851. return -TARGET_EFAULT;
  1852. if (target_addr) {
  1853. addr = alloca(addrlen);
  1854. ret = target_to_host_sockaddr(addr, target_addr, addrlen);
  1855. if (ret) {
  1856. unlock_user(host_msg, msg, 0);
  1857. return ret;
  1858. }
  1859. ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
  1860. } else {
  1861. ret = get_errno(send(fd, host_msg, len, flags));
  1862. }
  1863. unlock_user(host_msg, msg, 0);
  1864. return ret;
  1865. }
  1866. /* do_recvfrom() Must return target values and target errnos. */
  1867. static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
  1868. abi_ulong target_addr,
  1869. abi_ulong target_addrlen)
  1870. {
  1871. socklen_t addrlen;
  1872. void *addr;
  1873. void *host_msg;
  1874. abi_long ret;
  1875. host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
  1876. if (!host_msg)
  1877. return -TARGET_EFAULT;
  1878. if (target_addr) {
  1879. if (get_user_u32(addrlen, target_addrlen)) {
  1880. ret = -TARGET_EFAULT;
  1881. goto fail;
  1882. }
  1883. if ((int)addrlen < 0) {
  1884. ret = -TARGET_EINVAL;
  1885. goto fail;
  1886. }
  1887. addr = alloca(addrlen);
  1888. ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
  1889. } else {
  1890. addr = NULL; /* To keep compiler quiet. */
  1891. ret = get_errno(qemu_recv(fd, host_msg, len, flags));
  1892. }
  1893. if (!is_error(ret)) {
  1894. if (target_addr) {
  1895. host_to_target_sockaddr(target_addr, addr, addrlen);
  1896. if (put_user_u32(addrlen, target_addrlen)) {
  1897. ret = -TARGET_EFAULT;
  1898. goto fail;
  1899. }
  1900. }
  1901. unlock_user(host_msg, msg, len);
  1902. } else {
  1903. fail:
  1904. unlock_user(host_msg, msg, 0);
  1905. }
  1906. return ret;
  1907. }
  1908. #ifdef TARGET_NR_socketcall
  1909. /* do_socketcall() Must return target values and target errnos. */
  1910. static abi_long do_socketcall(int num, abi_ulong vptr)
  1911. {
  1912. abi_long ret;
  1913. const int n = sizeof(abi_ulong);
  1914. switch(num) {
  1915. case SOCKOP_socket:
  1916. {
  1917. abi_ulong domain, type, protocol;
  1918. if (get_user_ual(domain, vptr)
  1919. || get_user_ual(type, vptr + n)
  1920. || get_user_ual(protocol, vptr + 2 * n))
  1921. return -TARGET_EFAULT;
  1922. ret = do_socket(domain, type, protocol);
  1923. }
  1924. break;
  1925. case SOCKOP_bind:
  1926. {
  1927. abi_ulong sockfd;
  1928. abi_ulong target_addr;
  1929. socklen_t addrlen;
  1930. if (get_user_ual(sockfd, vptr)
  1931. || get_user_ual(target_addr, vptr + n)
  1932. || get_user_ual(addrlen, vptr + 2 * n))
  1933. return -TARGET_EFAULT;
  1934. ret = do_bind(sockfd, target_addr, addrlen);
  1935. }
  1936. break;
  1937. case SOCKOP_connect:
  1938. {
  1939. abi_ulong sockfd;
  1940. abi_ulong target_addr;
  1941. socklen_t addrlen;
  1942. if (get_user_ual(sockfd, vptr)
  1943. || get_user_ual(target_addr, vptr + n)
  1944. || get_user_ual(addrlen, vptr + 2 * n))
  1945. return -TARGET_EFAULT;
  1946. ret = do_connect(sockfd, target_addr, addrlen);
  1947. }
  1948. break;
  1949. case SOCKOP_listen:
  1950. {
  1951. abi_ulong sockfd, backlog;
  1952. if (get_user_ual(sockfd, vptr)
  1953. || get_user_ual(backlog, vptr + n))
  1954. return -TARGET_EFAULT;
  1955. ret = get_errno(listen(sockfd, backlog));
  1956. }
  1957. break;
  1958. case SOCKOP_accept:
  1959. {
  1960. abi_ulong sockfd;
  1961. abi_ulong target_addr, target_addrlen;
  1962. if (get_user_ual(sockfd, vptr)
  1963. || get_user_ual(target_addr, vptr + n)
  1964. || get_user_ual(target_addrlen, vptr + 2 * n))
  1965. return -TARGET_EFAULT;
  1966. ret = do_accept(sockfd, target_addr, target_addrlen);
  1967. }
  1968. break;
  1969. case SOCKOP_getsockname:
  1970. {
  1971. abi_ulong sockfd;
  1972. abi_ulong target_addr, target_addrlen;
  1973. if (get_user_ual(sockfd, vptr)
  1974. || get_user_ual(target_addr, vptr + n)
  1975. || get_user_ual(target_addrlen, vptr + 2 * n))
  1976. return -TARGET_EFAULT;
  1977. ret = do_getsockname(sockfd, target_addr, target_addrlen);
  1978. }
  1979. break;
  1980. case SOCKOP_getpeername:
  1981. {
  1982. abi_ulong sockfd;
  1983. abi_ulong target_addr, target_addrlen;
  1984. if (get_user_ual(sockfd, vptr)
  1985. || get_user_ual(target_addr, vptr + n)
  1986. || get_user_ual(target_addrlen, vptr + 2 * n))
  1987. return -TARGET_EFAULT;
  1988. ret = do_getpeername(sockfd, target_addr, target_addrlen);
  1989. }
  1990. break;
  1991. case SOCKOP_socketpair:
  1992. {
  1993. abi_ulong domain, type, protocol;
  1994. abi_ulong tab;
  1995. if (get_user_ual(domain, vptr)
  1996. || get_user_ual(type, vptr + n)
  1997. || get_user_ual(protocol, vptr + 2 * n)
  1998. || get_user_ual(tab, vptr + 3 * n))
  1999. return -TARGET_EFAULT;
  2000. ret = do_socketpair(domain, type, protocol, tab);
  2001. }
  2002. break;
  2003. case SOCKOP_send:
  2004. {
  2005. abi_ulong sockfd;
  2006. abi_ulong msg;
  2007. size_t len;
  2008. abi_ulong flags;
  2009. if (get_user_ual(sockfd, vptr)
  2010. || get_user_ual(msg, vptr + n)
  2011. || get_user_ual(len, vptr + 2 * n)
  2012. || get_user_ual(flags, vptr + 3 * n))
  2013. return -TARGET_EFAULT;
  2014. ret = do_sendto(sockfd, msg, len, flags, 0, 0);
  2015. }
  2016. break;
  2017. case SOCKOP_recv:
  2018. {
  2019. abi_ulong sockfd;
  2020. abi_ulong msg;
  2021. size_t len;
  2022. abi_ulong flags;
  2023. if (get_user_ual(sockfd, vptr)
  2024. || get_user_ual(msg, vptr + n)
  2025. || get_user_ual(len, vptr + 2 * n)
  2026. || get_user_ual(flags, vptr + 3 * n))
  2027. return -TARGET_EFAULT;
  2028. ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
  2029. }
  2030. break;
  2031. case SOCKOP_sendto:
  2032. {
  2033. abi_ulong sockfd;
  2034. abi_ulong msg;
  2035. size_t len;
  2036. abi_ulong flags;
  2037. abi_ulong addr;
  2038. socklen_t addrlen;
  2039. if (get_user_ual(sockfd, vptr)
  2040. || get_user_ual(msg, vptr + n)
  2041. || get_user_ual(len, vptr + 2 * n)
  2042. || get_user_ual(flags, vptr + 3 * n)
  2043. || get_user_ual(addr, vptr + 4 * n)
  2044. || get_user_ual(addrlen, vptr + 5 * n))
  2045. return -TARGET_EFAULT;
  2046. ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
  2047. }
  2048. break;
  2049. case SOCKOP_recvfrom:
  2050. {
  2051. abi_ulong sockfd;
  2052. abi_ulong msg;
  2053. size_t len;
  2054. abi_ulong flags;
  2055. abi_ulong addr;
  2056. socklen_t addrlen;
  2057. if (get_user_ual(sockfd, vptr)
  2058. || get_user_ual(msg, vptr + n)
  2059. || get_user_ual(len, vptr + 2 * n)
  2060. || get_user_ual(flags, vptr + 3 * n)
  2061. || get_user_ual(addr, vptr + 4 * n)
  2062. || get_user_ual(addrlen, vptr + 5 * n))
  2063. return -TARGET_EFAULT;
  2064. ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
  2065. }
  2066. break;
  2067. case SOCKOP_shutdown:
  2068. {
  2069. abi_ulong sockfd, how;
  2070. if (get_user_ual(sockfd, vptr)
  2071. || get_user_ual(how, vptr + n))
  2072. return -TARGET_EFAULT;
  2073. ret = get_errno(shutdown(sockfd, how));
  2074. }
  2075. break;
  2076. case SOCKOP_sendmsg:
  2077. case SOCKOP_recvmsg:
  2078. {
  2079. abi_ulong fd;
  2080. abi_ulong target_msg;
  2081. abi_ulong flags;
  2082. if (get_user_ual(fd, vptr)
  2083. || get_user_ual(target_msg, vptr + n)
  2084. || get_user_ual(flags, vptr + 2 * n))
  2085. return -TARGET_EFAULT;
  2086. ret = do_sendrecvmsg(fd, target_msg, flags,
  2087. (num == SOCKOP_sendmsg));
  2088. }
  2089. break;
  2090. case SOCKOP_setsockopt:
  2091. {
  2092. abi_ulong sockfd;
  2093. abi_ulong level;
  2094. abi_ulong optname;
  2095. abi_ulong optval;
  2096. socklen_t optlen;
  2097. if (get_user_ual(sockfd, vptr)
  2098. || get_user_ual(level, vptr + n)
  2099. || get_user_ual(optname, vptr + 2 * n)
  2100. || get_user_ual(optval, vptr + 3 * n)
  2101. || get_user_ual(optlen, vptr + 4 * n))
  2102. return -TARGET_EFAULT;
  2103. ret = do_setsockopt(sockfd, level, optname, optval, optlen);
  2104. }
  2105. break;
  2106. case SOCKOP_getsockopt:
  2107. {
  2108. abi_ulong sockfd;
  2109. abi_ulong level;
  2110. abi_ulong optname;
  2111. abi_ulong optval;
  2112. socklen_t optlen;
  2113. if (get_user_ual(sockfd, vptr)
  2114. || get_user_ual(level, vptr + n)
  2115. || get_user_ual(optname, vptr + 2 * n)
  2116. || get_user_ual(optval, vptr + 3 * n)
  2117. || get_user_ual(optlen, vptr + 4 * n))
  2118. return -TARGET_EFAULT;
  2119. ret = do_getsockopt(sockfd, level, optname, optval, optlen);
  2120. }
  2121. break;
  2122. default:
  2123. gemu_log("Unsupported socketcall: %d\n", num);
  2124. ret = -TARGET_ENOSYS;
  2125. break;
  2126. }
  2127. return ret;
  2128. }
  2129. #endif
  2130. #define N_SHM_REGIONS 32
  2131. static struct shm_region {
  2132. abi_ulong start;
  2133. abi_ulong size;
  2134. } shm_regions[N_SHM_REGIONS];
  2135. struct target_ipc_perm
  2136. {
  2137. abi_long __key;
  2138. abi_ulong uid;
  2139. abi_ulong gid;
  2140. abi_ulong cuid;
  2141. abi_ulong cgid;
  2142. unsigned short int mode;
  2143. unsigned short int __pad1;
  2144. unsigned short int __seq;
  2145. unsigned short int __pad2;
  2146. abi_ulong __unused1;
  2147. abi_ulong __unused2;
  2148. };
  2149. struct target_semid_ds
  2150. {
  2151. struct target_ipc_perm sem_perm;
  2152. abi_ulong sem_otime;
  2153. abi_ulong __unused1;
  2154. abi_ulong sem_ctime;
  2155. abi_ulong __unused2;
  2156. abi_ulong sem_nsems;
  2157. abi_ulong __unused3;
  2158. abi_ulong __unused4;
  2159. };
  2160. static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
  2161. abi_ulong target_addr)
  2162. {
  2163. struct target_ipc_perm *target_ip;
  2164. struct target_semid_ds *target_sd;
  2165. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  2166. return -TARGET_EFAULT;
  2167. target_ip = &(target_sd->sem_perm);
  2168. host_ip->__key = tswapal(target_ip->__key);
  2169. host_ip->uid = tswapal(target_ip->uid);
  2170. host_ip->gid = tswapal(target_ip->gid);
  2171. host_ip->cuid = tswapal(target_ip->cuid);
  2172. host_ip->cgid = tswapal(target_ip->cgid);
  2173. host_ip->mode = tswap16(target_ip->mode);
  2174. unlock_user_struct(target_sd, target_addr, 0);
  2175. return 0;
  2176. }
  2177. static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
  2178. struct ipc_perm *host_ip)
  2179. {
  2180. struct target_ipc_perm *target_ip;
  2181. struct target_semid_ds *target_sd;
  2182. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  2183. return -TARGET_EFAULT;
  2184. target_ip = &(target_sd->sem_perm);
  2185. target_ip->__key = tswapal(host_ip->__key);
  2186. target_ip->uid = tswapal(host_ip->uid);
  2187. target_ip->gid = tswapal(host_ip->gid);
  2188. target_ip->cuid = tswapal(host_ip->cuid);
  2189. target_ip->cgid = tswapal(host_ip->cgid);
  2190. target_ip->mode = tswap16(host_ip->mode);
  2191. unlock_user_struct(target_sd, target_addr, 1);
  2192. return 0;
  2193. }
  2194. static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
  2195. abi_ulong target_addr)
  2196. {
  2197. struct target_semid_ds *target_sd;
  2198. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  2199. return -TARGET_EFAULT;
  2200. if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
  2201. return -TARGET_EFAULT;
  2202. host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
  2203. host_sd->sem_otime = tswapal(target_sd->sem_otime);
  2204. host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
  2205. unlock_user_struct(target_sd, target_addr, 0);
  2206. return 0;
  2207. }
  2208. static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
  2209. struct semid_ds *host_sd)
  2210. {
  2211. struct target_semid_ds *target_sd;
  2212. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  2213. return -TARGET_EFAULT;
  2214. if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
  2215. return -TARGET_EFAULT;
  2216. target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
  2217. target_sd->sem_otime = tswapal(host_sd->sem_otime);
  2218. target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
  2219. unlock_user_struct(target_sd, target_addr, 1);
  2220. return 0;
  2221. }
  2222. struct target_seminfo {
  2223. int semmap;
  2224. int semmni;
  2225. int semmns;
  2226. int semmnu;
  2227. int semmsl;
  2228. int semopm;
  2229. int semume;
  2230. int semusz;
  2231. int semvmx;
  2232. int semaem;
  2233. };
  2234. static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
  2235. struct seminfo *host_seminfo)
  2236. {
  2237. struct target_seminfo *target_seminfo;
  2238. if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
  2239. return -TARGET_EFAULT;
  2240. __put_user(host_seminfo->semmap, &target_seminfo->semmap);
  2241. __put_user(host_seminfo->semmni, &target_seminfo->semmni);
  2242. __put_user(host_seminfo->semmns, &target_seminfo->semmns);
  2243. __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
  2244. __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
  2245. __put_user(host_seminfo->semopm, &target_seminfo->semopm);
  2246. __put_user(host_seminfo->semume, &target_seminfo->semume);
  2247. __put_user(host_seminfo->semusz, &target_seminfo->semusz);
  2248. __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
  2249. __put_user(host_seminfo->semaem, &target_seminfo->semaem);
  2250. unlock_user_struct(target_seminfo, target_addr, 1);
  2251. return 0;
  2252. }
  2253. union semun {
  2254. int val;
  2255. struct semid_ds *buf;
  2256. unsigned short *array;
  2257. struct seminfo *__buf;
  2258. };
  2259. union target_semun {
  2260. int val;
  2261. abi_ulong buf;
  2262. abi_ulong array;
  2263. abi_ulong __buf;
  2264. };
  2265. static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
  2266. abi_ulong target_addr)
  2267. {
  2268. int nsems;
  2269. unsigned short *array;
  2270. union semun semun;
  2271. struct semid_ds semid_ds;
  2272. int i, ret;
  2273. semun.buf = &semid_ds;
  2274. ret = semctl(semid, 0, IPC_STAT, semun);
  2275. if (ret == -1)
  2276. return get_errno(ret);
  2277. nsems = semid_ds.sem_nsems;
  2278. *host_array = malloc(nsems*sizeof(unsigned short));
  2279. array = lock_user(VERIFY_READ, target_addr,
  2280. nsems*sizeof(unsigned short), 1);
  2281. if (!array)
  2282. return -TARGET_EFAULT;
  2283. for(i=0; i<nsems; i++) {
  2284. __get_user((*host_array)[i], &array[i]);
  2285. }
  2286. unlock_user(array, target_addr, 0);
  2287. return 0;
  2288. }
  2289. static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
  2290. unsigned short **host_array)
  2291. {
  2292. int nsems;
  2293. unsigned short *array;
  2294. union semun semun;
  2295. struct semid_ds semid_ds;
  2296. int i, ret;
  2297. semun.buf = &semid_ds;
  2298. ret = semctl(semid, 0, IPC_STAT, semun);
  2299. if (ret == -1)
  2300. return get_errno(ret);
  2301. nsems = semid_ds.sem_nsems;
  2302. array = lock_user(VERIFY_WRITE, target_addr,
  2303. nsems*sizeof(unsigned short), 0);
  2304. if (!array)
  2305. return -TARGET_EFAULT;
  2306. for(i=0; i<nsems; i++) {
  2307. __put_user((*host_array)[i], &array[i]);
  2308. }
  2309. free(*host_array);
  2310. unlock_user(array, target_addr, 1);
  2311. return 0;
  2312. }
  2313. static inline abi_long do_semctl(int semid, int semnum, int cmd,
  2314. union target_semun target_su)
  2315. {
  2316. union semun arg;
  2317. struct semid_ds dsarg;
  2318. unsigned short *array = NULL;
  2319. struct seminfo seminfo;
  2320. abi_long ret = -TARGET_EINVAL;
  2321. abi_long err;
  2322. cmd &= 0xff;
  2323. switch( cmd ) {
  2324. case GETVAL:
  2325. case SETVAL:
  2326. arg.val = tswap32(target_su.val);
  2327. ret = get_errno(semctl(semid, semnum, cmd, arg));
  2328. target_su.val = tswap32(arg.val);
  2329. break;
  2330. case GETALL:
  2331. case SETALL:
  2332. err = target_to_host_semarray(semid, &array, target_su.array);
  2333. if (err)
  2334. return err;
  2335. arg.array = array;
  2336. ret = get_errno(semctl(semid, semnum, cmd, arg));
  2337. err = host_to_target_semarray(semid, target_su.array, &array);
  2338. if (err)
  2339. return err;
  2340. break;
  2341. case IPC_STAT:
  2342. case IPC_SET:
  2343. case SEM_STAT:
  2344. err = target_to_host_semid_ds(&dsarg, target_su.buf);
  2345. if (err)
  2346. return err;
  2347. arg.buf = &dsarg;
  2348. ret = get_errno(semctl(semid, semnum, cmd, arg));
  2349. err = host_to_target_semid_ds(target_su.buf, &dsarg);
  2350. if (err)
  2351. return err;
  2352. break;
  2353. case IPC_INFO:
  2354. case SEM_INFO:
  2355. arg.__buf = &seminfo;
  2356. ret = get_errno(semctl(semid, semnum, cmd, arg));
  2357. err = host_to_target_seminfo(target_su.__buf, &seminfo);
  2358. if (err)
  2359. return err;
  2360. break;
  2361. case IPC_RMID:
  2362. case GETPID:
  2363. case GETNCNT:
  2364. case GETZCNT:
  2365. ret = get_errno(semctl(semid, semnum, cmd, NULL));
  2366. break;
  2367. }
  2368. return ret;
  2369. }
  2370. struct target_sembuf {
  2371. unsigned short sem_num;
  2372. short sem_op;
  2373. short sem_flg;
  2374. };
  2375. static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
  2376. abi_ulong target_addr,
  2377. unsigned nsops)
  2378. {
  2379. struct target_sembuf *target_sembuf;
  2380. int i;
  2381. target_sembuf = lock_user(VERIFY_READ, target_addr,
  2382. nsops*sizeof(struct target_sembuf), 1);
  2383. if (!target_sembuf)
  2384. return -TARGET_EFAULT;
  2385. for(i=0; i<nsops; i++) {
  2386. __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
  2387. __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
  2388. __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
  2389. }
  2390. unlock_user(target_sembuf, target_addr, 0);
  2391. return 0;
  2392. }
  2393. static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
  2394. {
  2395. struct sembuf sops[nsops];
  2396. if (target_to_host_sembuf(sops, ptr, nsops))
  2397. return -TARGET_EFAULT;
  2398. return semop(semid, sops, nsops);
  2399. }
  2400. struct target_msqid_ds
  2401. {
  2402. struct target_ipc_perm msg_perm;
  2403. abi_ulong msg_stime;
  2404. #if TARGET_ABI_BITS == 32
  2405. abi_ulong __unused1;
  2406. #endif
  2407. abi_ulong msg_rtime;
  2408. #if TARGET_ABI_BITS == 32
  2409. abi_ulong __unused2;
  2410. #endif
  2411. abi_ulong msg_ctime;
  2412. #if TARGET_ABI_BITS == 32
  2413. abi_ulong __unused3;
  2414. #endif
  2415. abi_ulong __msg_cbytes;
  2416. abi_ulong msg_qnum;
  2417. abi_ulong msg_qbytes;
  2418. abi_ulong msg_lspid;
  2419. abi_ulong msg_lrpid;
  2420. abi_ulong __unused4;
  2421. abi_ulong __unused5;
  2422. };
  2423. static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
  2424. abi_ulong target_addr)
  2425. {
  2426. struct target_msqid_ds *target_md;
  2427. if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
  2428. return -TARGET_EFAULT;
  2429. if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
  2430. return -TARGET_EFAULT;
  2431. host_md->msg_stime = tswapal(target_md->msg_stime);
  2432. host_md->msg_rtime = tswapal(target_md->msg_rtime);
  2433. host_md->msg_ctime = tswapal(target_md->msg_ctime);
  2434. host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
  2435. host_md->msg_qnum = tswapal(target_md->msg_qnum);
  2436. host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
  2437. host_md->msg_lspid = tswapal(target_md->msg_lspid);
  2438. host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
  2439. unlock_user_struct(target_md, target_addr, 0);
  2440. return 0;
  2441. }
  2442. static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
  2443. struct msqid_ds *host_md)
  2444. {
  2445. struct target_msqid_ds *target_md;
  2446. if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
  2447. return -TARGET_EFAULT;
  2448. if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
  2449. return -TARGET_EFAULT;
  2450. target_md->msg_stime = tswapal(host_md->msg_stime);
  2451. target_md->msg_rtime = tswapal(host_md->msg_rtime);
  2452. target_md->msg_ctime = tswapal(host_md->msg_ctime);
  2453. target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
  2454. target_md->msg_qnum = tswapal(host_md->msg_qnum);
  2455. target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
  2456. target_md->msg_lspid = tswapal(host_md->msg_lspid);
  2457. target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
  2458. unlock_user_struct(target_md, target_addr, 1);
  2459. return 0;
  2460. }
  2461. struct target_msginfo {
  2462. int msgpool;
  2463. int msgmap;
  2464. int msgmax;
  2465. int msgmnb;
  2466. int msgmni;
  2467. int msgssz;
  2468. int msgtql;
  2469. unsigned short int msgseg;
  2470. };
  2471. static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
  2472. struct msginfo *host_msginfo)
  2473. {
  2474. struct target_msginfo *target_msginfo;
  2475. if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
  2476. return -TARGET_EFAULT;
  2477. __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
  2478. __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
  2479. __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
  2480. __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
  2481. __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
  2482. __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
  2483. __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
  2484. __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
  2485. unlock_user_struct(target_msginfo, target_addr, 1);
  2486. return 0;
  2487. }
  2488. static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
  2489. {
  2490. struct msqid_ds dsarg;
  2491. struct msginfo msginfo;
  2492. abi_long ret = -TARGET_EINVAL;
  2493. cmd &= 0xff;
  2494. switch (cmd) {
  2495. case IPC_STAT:
  2496. case IPC_SET:
  2497. case MSG_STAT:
  2498. if (target_to_host_msqid_ds(&dsarg,ptr))
  2499. return -TARGET_EFAULT;
  2500. ret = get_errno(msgctl(msgid, cmd, &dsarg));
  2501. if (host_to_target_msqid_ds(ptr,&dsarg))
  2502. return -TARGET_EFAULT;
  2503. break;
  2504. case IPC_RMID:
  2505. ret = get_errno(msgctl(msgid, cmd, NULL));
  2506. break;
  2507. case IPC_INFO:
  2508. case MSG_INFO:
  2509. ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
  2510. if (host_to_target_msginfo(ptr, &msginfo))
  2511. return -TARGET_EFAULT;
  2512. break;
  2513. }
  2514. return ret;
  2515. }
  2516. struct target_msgbuf {
  2517. abi_long mtype;
  2518. char mtext[1];
  2519. };
  2520. static inline abi_long do_msgsnd(int msqid, abi_long msgp,
  2521. unsigned int msgsz, int msgflg)
  2522. {
  2523. struct target_msgbuf *target_mb;
  2524. struct msgbuf *host_mb;
  2525. abi_long ret = 0;
  2526. if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
  2527. return -TARGET_EFAULT;
  2528. host_mb = malloc(msgsz+sizeof(long));
  2529. host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
  2530. memcpy(host_mb->mtext, target_mb->mtext, msgsz);
  2531. ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
  2532. free(host_mb);
  2533. unlock_user_struct(target_mb, msgp, 0);
  2534. return ret;
  2535. }
  2536. static inline abi_long do_msgrcv(int msqid, abi_long msgp,
  2537. unsigned int msgsz, abi_long msgtyp,
  2538. int msgflg)
  2539. {
  2540. struct target_msgbuf *target_mb;
  2541. char *target_mtext;
  2542. struct msgbuf *host_mb;
  2543. abi_long ret = 0;
  2544. if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
  2545. return -TARGET_EFAULT;
  2546. host_mb = g_malloc(msgsz+sizeof(long));
  2547. ret = get_errno(msgrcv(msqid, host_mb, msgsz, tswapal(msgtyp), msgflg));
  2548. if (ret > 0) {
  2549. abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
  2550. target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
  2551. if (!target_mtext) {
  2552. ret = -TARGET_EFAULT;
  2553. goto end;
  2554. }
  2555. memcpy(target_mb->mtext, host_mb->mtext, ret);
  2556. unlock_user(target_mtext, target_mtext_addr, ret);
  2557. }
  2558. target_mb->mtype = tswapal(host_mb->mtype);
  2559. end:
  2560. if (target_mb)
  2561. unlock_user_struct(target_mb, msgp, 1);
  2562. g_free(host_mb);
  2563. return ret;
  2564. }
  2565. struct target_shmid_ds
  2566. {
  2567. struct target_ipc_perm shm_perm;
  2568. abi_ulong shm_segsz;
  2569. abi_ulong shm_atime;
  2570. #if TARGET_ABI_BITS == 32
  2571. abi_ulong __unused1;
  2572. #endif
  2573. abi_ulong shm_dtime;
  2574. #if TARGET_ABI_BITS == 32
  2575. abi_ulong __unused2;
  2576. #endif
  2577. abi_ulong shm_ctime;
  2578. #if TARGET_ABI_BITS == 32
  2579. abi_ulong __unused3;
  2580. #endif
  2581. int shm_cpid;
  2582. int shm_lpid;
  2583. abi_ulong shm_nattch;
  2584. unsigned long int __unused4;
  2585. unsigned long int __unused5;
  2586. };
  2587. static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
  2588. abi_ulong target_addr)
  2589. {
  2590. struct target_shmid_ds *target_sd;
  2591. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  2592. return -TARGET_EFAULT;
  2593. if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
  2594. return -TARGET_EFAULT;
  2595. __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
  2596. __get_user(host_sd->shm_atime, &target_sd->shm_atime);
  2597. __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
  2598. __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
  2599. __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
  2600. __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
  2601. __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
  2602. unlock_user_struct(target_sd, target_addr, 0);
  2603. return 0;
  2604. }
  2605. static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
  2606. struct shmid_ds *host_sd)
  2607. {
  2608. struct target_shmid_ds *target_sd;
  2609. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  2610. return -TARGET_EFAULT;
  2611. if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
  2612. return -TARGET_EFAULT;
  2613. __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
  2614. __put_user(host_sd->shm_atime, &target_sd->shm_atime);
  2615. __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
  2616. __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
  2617. __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
  2618. __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
  2619. __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
  2620. unlock_user_struct(target_sd, target_addr, 1);
  2621. return 0;
  2622. }
  2623. struct target_shminfo {
  2624. abi_ulong shmmax;
  2625. abi_ulong shmmin;
  2626. abi_ulong shmmni;
  2627. abi_ulong shmseg;
  2628. abi_ulong shmall;
  2629. };
  2630. static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
  2631. struct shminfo *host_shminfo)
  2632. {
  2633. struct target_shminfo *target_shminfo;
  2634. if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
  2635. return -TARGET_EFAULT;
  2636. __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
  2637. __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
  2638. __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
  2639. __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
  2640. __put_user(host_shminfo->shmall, &target_shminfo->shmall);
  2641. unlock_user_struct(target_shminfo, target_addr, 1);
  2642. return 0;
  2643. }
  2644. struct target_shm_info {
  2645. int used_ids;
  2646. abi_ulong shm_tot;
  2647. abi_ulong shm_rss;
  2648. abi_ulong shm_swp;
  2649. abi_ulong swap_attempts;
  2650. abi_ulong swap_successes;
  2651. };
  2652. static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
  2653. struct shm_info *host_shm_info)
  2654. {
  2655. struct target_shm_info *target_shm_info;
  2656. if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
  2657. return -TARGET_EFAULT;
  2658. __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
  2659. __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
  2660. __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
  2661. __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
  2662. __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
  2663. __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
  2664. unlock_user_struct(target_shm_info, target_addr, 1);
  2665. return 0;
  2666. }
  2667. static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
  2668. {
  2669. struct shmid_ds dsarg;
  2670. struct shminfo shminfo;
  2671. struct shm_info shm_info;
  2672. abi_long ret = -TARGET_EINVAL;
  2673. cmd &= 0xff;
  2674. switch(cmd) {
  2675. case IPC_STAT:
  2676. case IPC_SET:
  2677. case SHM_STAT:
  2678. if (target_to_host_shmid_ds(&dsarg, buf))
  2679. return -TARGET_EFAULT;
  2680. ret = get_errno(shmctl(shmid, cmd, &dsarg));
  2681. if (host_to_target_shmid_ds(buf, &dsarg))
  2682. return -TARGET_EFAULT;
  2683. break;
  2684. case IPC_INFO:
  2685. ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
  2686. if (host_to_target_shminfo(buf, &shminfo))
  2687. return -TARGET_EFAULT;
  2688. break;
  2689. case SHM_INFO:
  2690. ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
  2691. if (host_to_target_shm_info(buf, &shm_info))
  2692. return -TARGET_EFAULT;
  2693. break;
  2694. case IPC_RMID:
  2695. case SHM_LOCK:
  2696. case SHM_UNLOCK:
  2697. ret = get_errno(shmctl(shmid, cmd, NULL));
  2698. break;
  2699. }
  2700. return ret;
  2701. }
  2702. static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg)
  2703. {
  2704. abi_long raddr;
  2705. void *host_raddr;
  2706. struct shmid_ds shm_info;
  2707. int i,ret;
  2708. /* find out the length of the shared memory segment */
  2709. ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
  2710. if (is_error(ret)) {
  2711. /* can't get length, bail out */
  2712. return ret;
  2713. }
  2714. mmap_lock();
  2715. if (shmaddr)
  2716. host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
  2717. else {
  2718. abi_ulong mmap_start;
  2719. mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
  2720. if (mmap_start == -1) {
  2721. errno = ENOMEM;
  2722. host_raddr = (void *)-1;
  2723. } else
  2724. host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
  2725. }
  2726. if (host_raddr == (void *)-1) {
  2727. mmap_unlock();
  2728. return get_errno((long)host_raddr);
  2729. }
  2730. raddr=h2g((unsigned long)host_raddr);
  2731. page_set_flags(raddr, raddr + shm_info.shm_segsz,
  2732. PAGE_VALID | PAGE_READ |
  2733. ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
  2734. for (i = 0; i < N_SHM_REGIONS; i++) {
  2735. if (shm_regions[i].start == 0) {
  2736. shm_regions[i].start = raddr;
  2737. shm_regions[i].size = shm_info.shm_segsz;
  2738. break;
  2739. }
  2740. }
  2741. mmap_unlock();
  2742. return raddr;
  2743. }
  2744. static inline abi_long do_shmdt(abi_ulong shmaddr)
  2745. {
  2746. int i;
  2747. for (i = 0; i < N_SHM_REGIONS; ++i) {
  2748. if (shm_regions[i].start == shmaddr) {
  2749. shm_regions[i].start = 0;
  2750. page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
  2751. break;
  2752. }
  2753. }
  2754. return get_errno(shmdt(g2h(shmaddr)));
  2755. }
  2756. #ifdef TARGET_NR_ipc
  2757. /* ??? This only works with linear mappings. */
  2758. /* do_ipc() must return target values and target errnos. */
  2759. static abi_long do_ipc(unsigned int call, int first,
  2760. int second, int third,
  2761. abi_long ptr, abi_long fifth)
  2762. {
  2763. int version;
  2764. abi_long ret = 0;
  2765. version = call >> 16;
  2766. call &= 0xffff;
  2767. switch (call) {
  2768. case IPCOP_semop:
  2769. ret = do_semop(first, ptr, second);
  2770. break;
  2771. case IPCOP_semget:
  2772. ret = get_errno(semget(first, second, third));
  2773. break;
  2774. case IPCOP_semctl:
  2775. ret = do_semctl(first, second, third, (union target_semun)(abi_ulong) ptr);
  2776. break;
  2777. case IPCOP_msgget:
  2778. ret = get_errno(msgget(first, second));
  2779. break;
  2780. case IPCOP_msgsnd:
  2781. ret = do_msgsnd(first, ptr, second, third);
  2782. break;
  2783. case IPCOP_msgctl:
  2784. ret = do_msgctl(first, second, ptr);
  2785. break;
  2786. case IPCOP_msgrcv:
  2787. switch (version) {
  2788. case 0:
  2789. {
  2790. struct target_ipc_kludge {
  2791. abi_long msgp;
  2792. abi_long msgtyp;
  2793. } *tmp;
  2794. if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
  2795. ret = -TARGET_EFAULT;
  2796. break;
  2797. }
  2798. ret = do_msgrcv(first, tmp->msgp, second, tmp->msgtyp, third);
  2799. unlock_user_struct(tmp, ptr, 0);
  2800. break;
  2801. }
  2802. default:
  2803. ret = do_msgrcv(first, ptr, second, fifth, third);
  2804. }
  2805. break;
  2806. case IPCOP_shmat:
  2807. switch (version) {
  2808. default:
  2809. {
  2810. abi_ulong raddr;
  2811. raddr = do_shmat(first, ptr, second);
  2812. if (is_error(raddr))
  2813. return get_errno(raddr);
  2814. if (put_user_ual(raddr, third))
  2815. return -TARGET_EFAULT;
  2816. break;
  2817. }
  2818. case 1:
  2819. ret = -TARGET_EINVAL;
  2820. break;
  2821. }
  2822. break;
  2823. case IPCOP_shmdt:
  2824. ret = do_shmdt(ptr);
  2825. break;
  2826. case IPCOP_shmget:
  2827. /* IPC_* flag values are the same on all linux platforms */
  2828. ret = get_errno(shmget(first, second, third));
  2829. break;
  2830. /* IPC_* and SHM_* command values are the same on all linux platforms */
  2831. case IPCOP_shmctl:
  2832. ret = do_shmctl(first, second, third);
  2833. break;
  2834. default:
  2835. gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
  2836. ret = -TARGET_ENOSYS;
  2837. break;
  2838. }
  2839. return ret;
  2840. }
  2841. #endif
  2842. /* kernel structure types definitions */
  2843. #define STRUCT(name, ...) STRUCT_ ## name,
  2844. #define STRUCT_SPECIAL(name) STRUCT_ ## name,
  2845. enum {
  2846. #include "syscall_types.h"
  2847. };
  2848. #undef STRUCT
  2849. #undef STRUCT_SPECIAL
  2850. #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
  2851. #define STRUCT_SPECIAL(name)
  2852. #include "syscall_types.h"
  2853. #undef STRUCT
  2854. #undef STRUCT_SPECIAL
  2855. typedef struct IOCTLEntry IOCTLEntry;
  2856. typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
  2857. int fd, abi_long cmd, abi_long arg);
  2858. struct IOCTLEntry {
  2859. unsigned int target_cmd;
  2860. unsigned int host_cmd;
  2861. const char *name;
  2862. int access;
  2863. do_ioctl_fn *do_ioctl;
  2864. const argtype arg_type[5];
  2865. };
  2866. #define IOC_R 0x0001
  2867. #define IOC_W 0x0002
  2868. #define IOC_RW (IOC_R | IOC_W)
  2869. #define MAX_STRUCT_SIZE 4096
  2870. #ifdef CONFIG_FIEMAP
  2871. /* So fiemap access checks don't overflow on 32 bit systems.
  2872. * This is very slightly smaller than the limit imposed by
  2873. * the underlying kernel.
  2874. */
  2875. #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
  2876. / sizeof(struct fiemap_extent))
  2877. static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
  2878. int fd, abi_long cmd, abi_long arg)
  2879. {
  2880. /* The parameter for this ioctl is a struct fiemap followed
  2881. * by an array of struct fiemap_extent whose size is set
  2882. * in fiemap->fm_extent_count. The array is filled in by the
  2883. * ioctl.
  2884. */
  2885. int target_size_in, target_size_out;
  2886. struct fiemap *fm;
  2887. const argtype *arg_type = ie->arg_type;
  2888. const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
  2889. void *argptr, *p;
  2890. abi_long ret;
  2891. int i, extent_size = thunk_type_size(extent_arg_type, 0);
  2892. uint32_t outbufsz;
  2893. int free_fm = 0;
  2894. assert(arg_type[0] == TYPE_PTR);
  2895. assert(ie->access == IOC_RW);
  2896. arg_type++;
  2897. target_size_in = thunk_type_size(arg_type, 0);
  2898. argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
  2899. if (!argptr) {
  2900. return -TARGET_EFAULT;
  2901. }
  2902. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  2903. unlock_user(argptr, arg, 0);
  2904. fm = (struct fiemap *)buf_temp;
  2905. if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
  2906. return -TARGET_EINVAL;
  2907. }
  2908. outbufsz = sizeof (*fm) +
  2909. (sizeof(struct fiemap_extent) * fm->fm_extent_count);
  2910. if (outbufsz > MAX_STRUCT_SIZE) {
  2911. /* We can't fit all the extents into the fixed size buffer.
  2912. * Allocate one that is large enough and use it instead.
  2913. */
  2914. fm = malloc(outbufsz);
  2915. if (!fm) {
  2916. return -TARGET_ENOMEM;
  2917. }
  2918. memcpy(fm, buf_temp, sizeof(struct fiemap));
  2919. free_fm = 1;
  2920. }
  2921. ret = get_errno(ioctl(fd, ie->host_cmd, fm));
  2922. if (!is_error(ret)) {
  2923. target_size_out = target_size_in;
  2924. /* An extent_count of 0 means we were only counting the extents
  2925. * so there are no structs to copy
  2926. */
  2927. if (fm->fm_extent_count != 0) {
  2928. target_size_out += fm->fm_mapped_extents * extent_size;
  2929. }
  2930. argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
  2931. if (!argptr) {
  2932. ret = -TARGET_EFAULT;
  2933. } else {
  2934. /* Convert the struct fiemap */
  2935. thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
  2936. if (fm->fm_extent_count != 0) {
  2937. p = argptr + target_size_in;
  2938. /* ...and then all the struct fiemap_extents */
  2939. for (i = 0; i < fm->fm_mapped_extents; i++) {
  2940. thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
  2941. THUNK_TARGET);
  2942. p += extent_size;
  2943. }
  2944. }
  2945. unlock_user(argptr, arg, target_size_out);
  2946. }
  2947. }
  2948. if (free_fm) {
  2949. free(fm);
  2950. }
  2951. return ret;
  2952. }
  2953. #endif
  2954. static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
  2955. int fd, abi_long cmd, abi_long arg)
  2956. {
  2957. const argtype *arg_type = ie->arg_type;
  2958. int target_size;
  2959. void *argptr;
  2960. int ret;
  2961. struct ifconf *host_ifconf;
  2962. uint32_t outbufsz;
  2963. const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
  2964. int target_ifreq_size;
  2965. int nb_ifreq;
  2966. int free_buf = 0;
  2967. int i;
  2968. int target_ifc_len;
  2969. abi_long target_ifc_buf;
  2970. int host_ifc_len;
  2971. char *host_ifc_buf;
  2972. assert(arg_type[0] == TYPE_PTR);
  2973. assert(ie->access == IOC_RW);
  2974. arg_type++;
  2975. target_size = thunk_type_size(arg_type, 0);
  2976. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  2977. if (!argptr)
  2978. return -TARGET_EFAULT;
  2979. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  2980. unlock_user(argptr, arg, 0);
  2981. host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
  2982. target_ifc_len = host_ifconf->ifc_len;
  2983. target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
  2984. target_ifreq_size = thunk_type_size(ifreq_arg_type, 0);
  2985. nb_ifreq = target_ifc_len / target_ifreq_size;
  2986. host_ifc_len = nb_ifreq * sizeof(struct ifreq);
  2987. outbufsz = sizeof(*host_ifconf) + host_ifc_len;
  2988. if (outbufsz > MAX_STRUCT_SIZE) {
  2989. /* We can't fit all the extents into the fixed size buffer.
  2990. * Allocate one that is large enough and use it instead.
  2991. */
  2992. host_ifconf = malloc(outbufsz);
  2993. if (!host_ifconf) {
  2994. return -TARGET_ENOMEM;
  2995. }
  2996. memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
  2997. free_buf = 1;
  2998. }
  2999. host_ifc_buf = (char*)host_ifconf + sizeof(*host_ifconf);
  3000. host_ifconf->ifc_len = host_ifc_len;
  3001. host_ifconf->ifc_buf = host_ifc_buf;
  3002. ret = get_errno(ioctl(fd, ie->host_cmd, host_ifconf));
  3003. if (!is_error(ret)) {
  3004. /* convert host ifc_len to target ifc_len */
  3005. nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
  3006. target_ifc_len = nb_ifreq * target_ifreq_size;
  3007. host_ifconf->ifc_len = target_ifc_len;
  3008. /* restore target ifc_buf */
  3009. host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
  3010. /* copy struct ifconf to target user */
  3011. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  3012. if (!argptr)
  3013. return -TARGET_EFAULT;
  3014. thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
  3015. unlock_user(argptr, arg, target_size);
  3016. /* copy ifreq[] to target user */
  3017. argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
  3018. for (i = 0; i < nb_ifreq ; i++) {
  3019. thunk_convert(argptr + i * target_ifreq_size,
  3020. host_ifc_buf + i * sizeof(struct ifreq),
  3021. ifreq_arg_type, THUNK_TARGET);
  3022. }
  3023. unlock_user(argptr, target_ifc_buf, target_ifc_len);
  3024. }
  3025. if (free_buf) {
  3026. free(host_ifconf);
  3027. }
  3028. return ret;
  3029. }
  3030. static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
  3031. abi_long cmd, abi_long arg)
  3032. {
  3033. void *argptr;
  3034. struct dm_ioctl *host_dm;
  3035. abi_long guest_data;
  3036. uint32_t guest_data_size;
  3037. int target_size;
  3038. const argtype *arg_type = ie->arg_type;
  3039. abi_long ret;
  3040. void *big_buf = NULL;
  3041. char *host_data;
  3042. arg_type++;
  3043. target_size = thunk_type_size(arg_type, 0);
  3044. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  3045. if (!argptr) {
  3046. ret = -TARGET_EFAULT;
  3047. goto out;
  3048. }
  3049. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  3050. unlock_user(argptr, arg, 0);
  3051. /* buf_temp is too small, so fetch things into a bigger buffer */
  3052. big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
  3053. memcpy(big_buf, buf_temp, target_size);
  3054. buf_temp = big_buf;
  3055. host_dm = big_buf;
  3056. guest_data = arg + host_dm->data_start;
  3057. if ((guest_data - arg) < 0) {
  3058. ret = -EINVAL;
  3059. goto out;
  3060. }
  3061. guest_data_size = host_dm->data_size - host_dm->data_start;
  3062. host_data = (char*)host_dm + host_dm->data_start;
  3063. argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
  3064. switch (ie->host_cmd) {
  3065. case DM_REMOVE_ALL:
  3066. case DM_LIST_DEVICES:
  3067. case DM_DEV_CREATE:
  3068. case DM_DEV_REMOVE:
  3069. case DM_DEV_SUSPEND:
  3070. case DM_DEV_STATUS:
  3071. case DM_DEV_WAIT:
  3072. case DM_TABLE_STATUS:
  3073. case DM_TABLE_CLEAR:
  3074. case DM_TABLE_DEPS:
  3075. case DM_LIST_VERSIONS:
  3076. /* no input data */
  3077. break;
  3078. case DM_DEV_RENAME:
  3079. case DM_DEV_SET_GEOMETRY:
  3080. /* data contains only strings */
  3081. memcpy(host_data, argptr, guest_data_size);
  3082. break;
  3083. case DM_TARGET_MSG:
  3084. memcpy(host_data, argptr, guest_data_size);
  3085. *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
  3086. break;
  3087. case DM_TABLE_LOAD:
  3088. {
  3089. void *gspec = argptr;
  3090. void *cur_data = host_data;
  3091. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
  3092. int spec_size = thunk_type_size(arg_type, 0);
  3093. int i;
  3094. for (i = 0; i < host_dm->target_count; i++) {
  3095. struct dm_target_spec *spec = cur_data;
  3096. uint32_t next;
  3097. int slen;
  3098. thunk_convert(spec, gspec, arg_type, THUNK_HOST);
  3099. slen = strlen((char*)gspec + spec_size) + 1;
  3100. next = spec->next;
  3101. spec->next = sizeof(*spec) + slen;
  3102. strcpy((char*)&spec[1], gspec + spec_size);
  3103. gspec += next;
  3104. cur_data += spec->next;
  3105. }
  3106. break;
  3107. }
  3108. default:
  3109. ret = -TARGET_EINVAL;
  3110. goto out;
  3111. }
  3112. unlock_user(argptr, guest_data, 0);
  3113. ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
  3114. if (!is_error(ret)) {
  3115. guest_data = arg + host_dm->data_start;
  3116. guest_data_size = host_dm->data_size - host_dm->data_start;
  3117. argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
  3118. switch (ie->host_cmd) {
  3119. case DM_REMOVE_ALL:
  3120. case DM_DEV_CREATE:
  3121. case DM_DEV_REMOVE:
  3122. case DM_DEV_RENAME:
  3123. case DM_DEV_SUSPEND:
  3124. case DM_DEV_STATUS:
  3125. case DM_TABLE_LOAD:
  3126. case DM_TABLE_CLEAR:
  3127. case DM_TARGET_MSG:
  3128. case DM_DEV_SET_GEOMETRY:
  3129. /* no return data */
  3130. break;
  3131. case DM_LIST_DEVICES:
  3132. {
  3133. struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
  3134. uint32_t remaining_data = guest_data_size;
  3135. void *cur_data = argptr;
  3136. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
  3137. int nl_size = 12; /* can't use thunk_size due to alignment */
  3138. while (1) {
  3139. uint32_t next = nl->next;
  3140. if (next) {
  3141. nl->next = nl_size + (strlen(nl->name) + 1);
  3142. }
  3143. if (remaining_data < nl->next) {
  3144. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  3145. break;
  3146. }
  3147. thunk_convert(cur_data, nl, arg_type, THUNK_TARGET);
  3148. strcpy(cur_data + nl_size, nl->name);
  3149. cur_data += nl->next;
  3150. remaining_data -= nl->next;
  3151. if (!next) {
  3152. break;
  3153. }
  3154. nl = (void*)nl + next;
  3155. }
  3156. break;
  3157. }
  3158. case DM_DEV_WAIT:
  3159. case DM_TABLE_STATUS:
  3160. {
  3161. struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
  3162. void *cur_data = argptr;
  3163. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
  3164. int spec_size = thunk_type_size(arg_type, 0);
  3165. int i;
  3166. for (i = 0; i < host_dm->target_count; i++) {
  3167. uint32_t next = spec->next;
  3168. int slen = strlen((char*)&spec[1]) + 1;
  3169. spec->next = (cur_data - argptr) + spec_size + slen;
  3170. if (guest_data_size < spec->next) {
  3171. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  3172. break;
  3173. }
  3174. thunk_convert(cur_data, spec, arg_type, THUNK_TARGET);
  3175. strcpy(cur_data + spec_size, (char*)&spec[1]);
  3176. cur_data = argptr + spec->next;
  3177. spec = (void*)host_dm + host_dm->data_start + next;
  3178. }
  3179. break;
  3180. }
  3181. case DM_TABLE_DEPS:
  3182. {
  3183. void *hdata = (void*)host_dm + host_dm->data_start;
  3184. int count = *(uint32_t*)hdata;
  3185. uint64_t *hdev = hdata + 8;
  3186. uint64_t *gdev = argptr + 8;
  3187. int i;
  3188. *(uint32_t*)argptr = tswap32(count);
  3189. for (i = 0; i < count; i++) {
  3190. *gdev = tswap64(*hdev);
  3191. gdev++;
  3192. hdev++;
  3193. }
  3194. break;
  3195. }
  3196. case DM_LIST_VERSIONS:
  3197. {
  3198. struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
  3199. uint32_t remaining_data = guest_data_size;
  3200. void *cur_data = argptr;
  3201. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
  3202. int vers_size = thunk_type_size(arg_type, 0);
  3203. while (1) {
  3204. uint32_t next = vers->next;
  3205. if (next) {
  3206. vers->next = vers_size + (strlen(vers->name) + 1);
  3207. }
  3208. if (remaining_data < vers->next) {
  3209. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  3210. break;
  3211. }
  3212. thunk_convert(cur_data, vers, arg_type, THUNK_TARGET);
  3213. strcpy(cur_data + vers_size, vers->name);
  3214. cur_data += vers->next;
  3215. remaining_data -= vers->next;
  3216. if (!next) {
  3217. break;
  3218. }
  3219. vers = (void*)vers + next;
  3220. }
  3221. break;
  3222. }
  3223. default:
  3224. ret = -TARGET_EINVAL;
  3225. goto out;
  3226. }
  3227. unlock_user(argptr, guest_data, guest_data_size);
  3228. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  3229. if (!argptr) {
  3230. ret = -TARGET_EFAULT;
  3231. goto out;
  3232. }
  3233. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  3234. unlock_user(argptr, arg, target_size);
  3235. }
  3236. out:
  3237. g_free(big_buf);
  3238. return ret;
  3239. }
  3240. static IOCTLEntry ioctl_entries[] = {
  3241. #define IOCTL(cmd, access, ...) \
  3242. { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
  3243. #define IOCTL_SPECIAL(cmd, access, dofn, ...) \
  3244. { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
  3245. #include "ioctls.h"
  3246. { 0, 0, },
  3247. };
  3248. /* ??? Implement proper locking for ioctls. */
  3249. /* do_ioctl() Must return target values and target errnos. */
  3250. static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
  3251. {
  3252. const IOCTLEntry *ie;
  3253. const argtype *arg_type;
  3254. abi_long ret;
  3255. uint8_t buf_temp[MAX_STRUCT_SIZE];
  3256. int target_size;
  3257. void *argptr;
  3258. ie = ioctl_entries;
  3259. for(;;) {
  3260. if (ie->target_cmd == 0) {
  3261. gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
  3262. return -TARGET_ENOSYS;
  3263. }
  3264. if (ie->target_cmd == cmd)
  3265. break;
  3266. ie++;
  3267. }
  3268. arg_type = ie->arg_type;
  3269. #if defined(DEBUG)
  3270. gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
  3271. #endif
  3272. if (ie->do_ioctl) {
  3273. return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
  3274. }
  3275. switch(arg_type[0]) {
  3276. case TYPE_NULL:
  3277. /* no argument */
  3278. ret = get_errno(ioctl(fd, ie->host_cmd));
  3279. break;
  3280. case TYPE_PTRVOID:
  3281. case TYPE_INT:
  3282. /* int argment */
  3283. ret = get_errno(ioctl(fd, ie->host_cmd, arg));
  3284. break;
  3285. case TYPE_PTR:
  3286. arg_type++;
  3287. target_size = thunk_type_size(arg_type, 0);
  3288. switch(ie->access) {
  3289. case IOC_R:
  3290. ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
  3291. if (!is_error(ret)) {
  3292. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  3293. if (!argptr)
  3294. return -TARGET_EFAULT;
  3295. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  3296. unlock_user(argptr, arg, target_size);
  3297. }
  3298. break;
  3299. case IOC_W:
  3300. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  3301. if (!argptr)
  3302. return -TARGET_EFAULT;
  3303. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  3304. unlock_user(argptr, arg, 0);
  3305. ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
  3306. break;
  3307. default:
  3308. case IOC_RW:
  3309. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  3310. if (!argptr)
  3311. return -TARGET_EFAULT;
  3312. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  3313. unlock_user(argptr, arg, 0);
  3314. ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
  3315. if (!is_error(ret)) {
  3316. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  3317. if (!argptr)
  3318. return -TARGET_EFAULT;
  3319. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  3320. unlock_user(argptr, arg, target_size);
  3321. }
  3322. break;
  3323. }
  3324. break;
  3325. default:
  3326. gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
  3327. (long)cmd, arg_type[0]);
  3328. ret = -TARGET_ENOSYS;
  3329. break;
  3330. }
  3331. return ret;
  3332. }
  3333. static const bitmask_transtbl iflag_tbl[] = {
  3334. { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
  3335. { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
  3336. { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
  3337. { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
  3338. { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
  3339. { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
  3340. { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
  3341. { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
  3342. { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
  3343. { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
  3344. { TARGET_IXON, TARGET_IXON, IXON, IXON },
  3345. { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
  3346. { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
  3347. { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
  3348. { 0, 0, 0, 0 }
  3349. };
  3350. static const bitmask_transtbl oflag_tbl[] = {
  3351. { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
  3352. { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
  3353. { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
  3354. { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
  3355. { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
  3356. { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
  3357. { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
  3358. { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
  3359. { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
  3360. { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
  3361. { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
  3362. { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
  3363. { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
  3364. { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
  3365. { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
  3366. { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
  3367. { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
  3368. { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
  3369. { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
  3370. { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
  3371. { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
  3372. { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
  3373. { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
  3374. { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
  3375. { 0, 0, 0, 0 }
  3376. };
  3377. static const bitmask_transtbl cflag_tbl[] = {
  3378. { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
  3379. { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
  3380. { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
  3381. { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
  3382. { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
  3383. { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
  3384. { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
  3385. { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
  3386. { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
  3387. { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
  3388. { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
  3389. { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
  3390. { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
  3391. { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
  3392. { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
  3393. { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
  3394. { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
  3395. { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
  3396. { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
  3397. { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
  3398. { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
  3399. { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
  3400. { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
  3401. { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
  3402. { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
  3403. { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
  3404. { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
  3405. { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
  3406. { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
  3407. { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
  3408. { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
  3409. { 0, 0, 0, 0 }
  3410. };
  3411. static const bitmask_transtbl lflag_tbl[] = {
  3412. { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
  3413. { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
  3414. { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
  3415. { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
  3416. { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
  3417. { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
  3418. { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
  3419. { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
  3420. { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
  3421. { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
  3422. { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
  3423. { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
  3424. { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
  3425. { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
  3426. { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
  3427. { 0, 0, 0, 0 }
  3428. };
  3429. static void target_to_host_termios (void *dst, const void *src)
  3430. {
  3431. struct host_termios *host = dst;
  3432. const struct target_termios *target = src;
  3433. host->c_iflag =
  3434. target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
  3435. host->c_oflag =
  3436. target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
  3437. host->c_cflag =
  3438. target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
  3439. host->c_lflag =
  3440. target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
  3441. host->c_line = target->c_line;
  3442. memset(host->c_cc, 0, sizeof(host->c_cc));
  3443. host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
  3444. host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
  3445. host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
  3446. host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
  3447. host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
  3448. host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
  3449. host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
  3450. host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
  3451. host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
  3452. host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
  3453. host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
  3454. host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
  3455. host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
  3456. host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
  3457. host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
  3458. host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
  3459. host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
  3460. }
  3461. static void host_to_target_termios (void *dst, const void *src)
  3462. {
  3463. struct target_termios *target = dst;
  3464. const struct host_termios *host = src;
  3465. target->c_iflag =
  3466. tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
  3467. target->c_oflag =
  3468. tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
  3469. target->c_cflag =
  3470. tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
  3471. target->c_lflag =
  3472. tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
  3473. target->c_line = host->c_line;
  3474. memset(target->c_cc, 0, sizeof(target->c_cc));
  3475. target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
  3476. target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
  3477. target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
  3478. target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
  3479. target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
  3480. target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
  3481. target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
  3482. target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
  3483. target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
  3484. target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
  3485. target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
  3486. target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
  3487. target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
  3488. target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
  3489. target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
  3490. target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
  3491. target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
  3492. }
  3493. static const StructEntry struct_termios_def = {
  3494. .convert = { host_to_target_termios, target_to_host_termios },
  3495. .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
  3496. .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
  3497. };
  3498. static bitmask_transtbl mmap_flags_tbl[] = {
  3499. { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
  3500. { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
  3501. { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
  3502. { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
  3503. { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
  3504. { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
  3505. { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
  3506. { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
  3507. { 0, 0, 0, 0 }
  3508. };
  3509. #if defined(TARGET_I386)
  3510. /* NOTE: there is really one LDT for all the threads */
  3511. static uint8_t *ldt_table;
  3512. static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
  3513. {
  3514. int size;
  3515. void *p;
  3516. if (!ldt_table)
  3517. return 0;
  3518. size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
  3519. if (size > bytecount)
  3520. size = bytecount;
  3521. p = lock_user(VERIFY_WRITE, ptr, size, 0);
  3522. if (!p)
  3523. return -TARGET_EFAULT;
  3524. /* ??? Should this by byteswapped? */
  3525. memcpy(p, ldt_table, size);
  3526. unlock_user(p, ptr, size);
  3527. return size;
  3528. }
  3529. /* XXX: add locking support */
  3530. static abi_long write_ldt(CPUX86State *env,
  3531. abi_ulong ptr, unsigned long bytecount, int oldmode)
  3532. {
  3533. struct target_modify_ldt_ldt_s ldt_info;
  3534. struct target_modify_ldt_ldt_s *target_ldt_info;
  3535. int seg_32bit, contents, read_exec_only, limit_in_pages;
  3536. int seg_not_present, useable, lm;
  3537. uint32_t *lp, entry_1, entry_2;
  3538. if (bytecount != sizeof(ldt_info))
  3539. return -TARGET_EINVAL;
  3540. if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
  3541. return -TARGET_EFAULT;
  3542. ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
  3543. ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
  3544. ldt_info.limit = tswap32(target_ldt_info->limit);
  3545. ldt_info.flags = tswap32(target_ldt_info->flags);
  3546. unlock_user_struct(target_ldt_info, ptr, 0);
  3547. if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
  3548. return -TARGET_EINVAL;
  3549. seg_32bit = ldt_info.flags & 1;
  3550. contents = (ldt_info.flags >> 1) & 3;
  3551. read_exec_only = (ldt_info.flags >> 3) & 1;
  3552. limit_in_pages = (ldt_info.flags >> 4) & 1;
  3553. seg_not_present = (ldt_info.flags >> 5) & 1;
  3554. useable = (ldt_info.flags >> 6) & 1;
  3555. #ifdef TARGET_ABI32
  3556. lm = 0;
  3557. #else
  3558. lm = (ldt_info.flags >> 7) & 1;
  3559. #endif
  3560. if (contents == 3) {
  3561. if (oldmode)
  3562. return -TARGET_EINVAL;
  3563. if (seg_not_present == 0)
  3564. return -TARGET_EINVAL;
  3565. }
  3566. /* allocate the LDT */
  3567. if (!ldt_table) {
  3568. env->ldt.base = target_mmap(0,
  3569. TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
  3570. PROT_READ|PROT_WRITE,
  3571. MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
  3572. if (env->ldt.base == -1)
  3573. return -TARGET_ENOMEM;
  3574. memset(g2h(env->ldt.base), 0,
  3575. TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
  3576. env->ldt.limit = 0xffff;
  3577. ldt_table = g2h(env->ldt.base);
  3578. }
  3579. /* NOTE: same code as Linux kernel */
  3580. /* Allow LDTs to be cleared by the user. */
  3581. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  3582. if (oldmode ||
  3583. (contents == 0 &&
  3584. read_exec_only == 1 &&
  3585. seg_32bit == 0 &&
  3586. limit_in_pages == 0 &&
  3587. seg_not_present == 1 &&
  3588. useable == 0 )) {
  3589. entry_1 = 0;
  3590. entry_2 = 0;
  3591. goto install;
  3592. }
  3593. }
  3594. entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
  3595. (ldt_info.limit & 0x0ffff);
  3596. entry_2 = (ldt_info.base_addr & 0xff000000) |
  3597. ((ldt_info.base_addr & 0x00ff0000) >> 16) |
  3598. (ldt_info.limit & 0xf0000) |
  3599. ((read_exec_only ^ 1) << 9) |
  3600. (contents << 10) |
  3601. ((seg_not_present ^ 1) << 15) |
  3602. (seg_32bit << 22) |
  3603. (limit_in_pages << 23) |
  3604. (lm << 21) |
  3605. 0x7000;
  3606. if (!oldmode)
  3607. entry_2 |= (useable << 20);
  3608. /* Install the new entry ... */
  3609. install:
  3610. lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
  3611. lp[0] = tswap32(entry_1);
  3612. lp[1] = tswap32(entry_2);
  3613. return 0;
  3614. }
  3615. /* specific and weird i386 syscalls */
  3616. static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
  3617. unsigned long bytecount)
  3618. {
  3619. abi_long ret;
  3620. switch (func) {
  3621. case 0:
  3622. ret = read_ldt(ptr, bytecount);
  3623. break;
  3624. case 1:
  3625. ret = write_ldt(env, ptr, bytecount, 1);
  3626. break;
  3627. case 0x11:
  3628. ret = write_ldt(env, ptr, bytecount, 0);
  3629. break;
  3630. default:
  3631. ret = -TARGET_ENOSYS;
  3632. break;
  3633. }
  3634. return ret;
  3635. }
  3636. #if defined(TARGET_I386) && defined(TARGET_ABI32)
  3637. static abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
  3638. {
  3639. uint64_t *gdt_table = g2h(env->gdt.base);
  3640. struct target_modify_ldt_ldt_s ldt_info;
  3641. struct target_modify_ldt_ldt_s *target_ldt_info;
  3642. int seg_32bit, contents, read_exec_only, limit_in_pages;
  3643. int seg_not_present, useable, lm;
  3644. uint32_t *lp, entry_1, entry_2;
  3645. int i;
  3646. lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
  3647. if (!target_ldt_info)
  3648. return -TARGET_EFAULT;
  3649. ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
  3650. ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
  3651. ldt_info.limit = tswap32(target_ldt_info->limit);
  3652. ldt_info.flags = tswap32(target_ldt_info->flags);
  3653. if (ldt_info.entry_number == -1) {
  3654. for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
  3655. if (gdt_table[i] == 0) {
  3656. ldt_info.entry_number = i;
  3657. target_ldt_info->entry_number = tswap32(i);
  3658. break;
  3659. }
  3660. }
  3661. }
  3662. unlock_user_struct(target_ldt_info, ptr, 1);
  3663. if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
  3664. ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
  3665. return -TARGET_EINVAL;
  3666. seg_32bit = ldt_info.flags & 1;
  3667. contents = (ldt_info.flags >> 1) & 3;
  3668. read_exec_only = (ldt_info.flags >> 3) & 1;
  3669. limit_in_pages = (ldt_info.flags >> 4) & 1;
  3670. seg_not_present = (ldt_info.flags >> 5) & 1;
  3671. useable = (ldt_info.flags >> 6) & 1;
  3672. #ifdef TARGET_ABI32
  3673. lm = 0;
  3674. #else
  3675. lm = (ldt_info.flags >> 7) & 1;
  3676. #endif
  3677. if (contents == 3) {
  3678. if (seg_not_present == 0)
  3679. return -TARGET_EINVAL;
  3680. }
  3681. /* NOTE: same code as Linux kernel */
  3682. /* Allow LDTs to be cleared by the user. */
  3683. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  3684. if ((contents == 0 &&
  3685. read_exec_only == 1 &&
  3686. seg_32bit == 0 &&
  3687. limit_in_pages == 0 &&
  3688. seg_not_present == 1 &&
  3689. useable == 0 )) {
  3690. entry_1 = 0;
  3691. entry_2 = 0;
  3692. goto install;
  3693. }
  3694. }
  3695. entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
  3696. (ldt_info.limit & 0x0ffff);
  3697. entry_2 = (ldt_info.base_addr & 0xff000000) |
  3698. ((ldt_info.base_addr & 0x00ff0000) >> 16) |
  3699. (ldt_info.limit & 0xf0000) |
  3700. ((read_exec_only ^ 1) << 9) |
  3701. (contents << 10) |
  3702. ((seg_not_present ^ 1) << 15) |
  3703. (seg_32bit << 22) |
  3704. (limit_in_pages << 23) |
  3705. (useable << 20) |
  3706. (lm << 21) |
  3707. 0x7000;
  3708. /* Install the new entry ... */
  3709. install:
  3710. lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
  3711. lp[0] = tswap32(entry_1);
  3712. lp[1] = tswap32(entry_2);
  3713. return 0;
  3714. }
  3715. static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
  3716. {
  3717. struct target_modify_ldt_ldt_s *target_ldt_info;
  3718. uint64_t *gdt_table = g2h(env->gdt.base);
  3719. uint32_t base_addr, limit, flags;
  3720. int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
  3721. int seg_not_present, useable, lm;
  3722. uint32_t *lp, entry_1, entry_2;
  3723. lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
  3724. if (!target_ldt_info)
  3725. return -TARGET_EFAULT;
  3726. idx = tswap32(target_ldt_info->entry_number);
  3727. if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
  3728. idx > TARGET_GDT_ENTRY_TLS_MAX) {
  3729. unlock_user_struct(target_ldt_info, ptr, 1);
  3730. return -TARGET_EINVAL;
  3731. }
  3732. lp = (uint32_t *)(gdt_table + idx);
  3733. entry_1 = tswap32(lp[0]);
  3734. entry_2 = tswap32(lp[1]);
  3735. read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
  3736. contents = (entry_2 >> 10) & 3;
  3737. seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
  3738. seg_32bit = (entry_2 >> 22) & 1;
  3739. limit_in_pages = (entry_2 >> 23) & 1;
  3740. useable = (entry_2 >> 20) & 1;
  3741. #ifdef TARGET_ABI32
  3742. lm = 0;
  3743. #else
  3744. lm = (entry_2 >> 21) & 1;
  3745. #endif
  3746. flags = (seg_32bit << 0) | (contents << 1) |
  3747. (read_exec_only << 3) | (limit_in_pages << 4) |
  3748. (seg_not_present << 5) | (useable << 6) | (lm << 7);
  3749. limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
  3750. base_addr = (entry_1 >> 16) |
  3751. (entry_2 & 0xff000000) |
  3752. ((entry_2 & 0xff) << 16);
  3753. target_ldt_info->base_addr = tswapal(base_addr);
  3754. target_ldt_info->limit = tswap32(limit);
  3755. target_ldt_info->flags = tswap32(flags);
  3756. unlock_user_struct(target_ldt_info, ptr, 1);
  3757. return 0;
  3758. }
  3759. #endif /* TARGET_I386 && TARGET_ABI32 */
  3760. #ifndef TARGET_ABI32
  3761. static abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
  3762. {
  3763. abi_long ret = 0;
  3764. abi_ulong val;
  3765. int idx;
  3766. switch(code) {
  3767. case TARGET_ARCH_SET_GS:
  3768. case TARGET_ARCH_SET_FS:
  3769. if (code == TARGET_ARCH_SET_GS)
  3770. idx = R_GS;
  3771. else
  3772. idx = R_FS;
  3773. cpu_x86_load_seg(env, idx, 0);
  3774. env->segs[idx].base = addr;
  3775. break;
  3776. case TARGET_ARCH_GET_GS:
  3777. case TARGET_ARCH_GET_FS:
  3778. if (code == TARGET_ARCH_GET_GS)
  3779. idx = R_GS;
  3780. else
  3781. idx = R_FS;
  3782. val = env->segs[idx].base;
  3783. if (put_user(val, addr, abi_ulong))
  3784. ret = -TARGET_EFAULT;
  3785. break;
  3786. default:
  3787. ret = -TARGET_EINVAL;
  3788. break;
  3789. }
  3790. return ret;
  3791. }
  3792. #endif
  3793. #endif /* defined(TARGET_I386) */
  3794. #define NEW_STACK_SIZE 0x40000
  3795. #if defined(CONFIG_USE_NPTL)
  3796. static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
  3797. typedef struct {
  3798. CPUArchState *env;
  3799. pthread_mutex_t mutex;
  3800. pthread_cond_t cond;
  3801. pthread_t thread;
  3802. uint32_t tid;
  3803. abi_ulong child_tidptr;
  3804. abi_ulong parent_tidptr;
  3805. sigset_t sigmask;
  3806. } new_thread_info;
  3807. static void *clone_func(void *arg)
  3808. {
  3809. new_thread_info *info = arg;
  3810. CPUArchState *env;
  3811. TaskState *ts;
  3812. env = info->env;
  3813. thread_env = env;
  3814. ts = (TaskState *)thread_env->opaque;
  3815. info->tid = gettid();
  3816. env->host_tid = info->tid;
  3817. task_settid(ts);
  3818. if (info->child_tidptr)
  3819. put_user_u32(info->tid, info->child_tidptr);
  3820. if (info->parent_tidptr)
  3821. put_user_u32(info->tid, info->parent_tidptr);
  3822. /* Enable signals. */
  3823. sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
  3824. /* Signal to the parent that we're ready. */
  3825. pthread_mutex_lock(&info->mutex);
  3826. pthread_cond_broadcast(&info->cond);
  3827. pthread_mutex_unlock(&info->mutex);
  3828. /* Wait until the parent has finshed initializing the tls state. */
  3829. pthread_mutex_lock(&clone_lock);
  3830. pthread_mutex_unlock(&clone_lock);
  3831. cpu_loop(env);
  3832. /* never exits */
  3833. return NULL;
  3834. }
  3835. #else
  3836. static int clone_func(void *arg)
  3837. {
  3838. CPUArchState *env = arg;
  3839. cpu_loop(env);
  3840. /* never exits */
  3841. return 0;
  3842. }
  3843. #endif
  3844. /* do_fork() Must return host values and target errnos (unlike most
  3845. do_*() functions). */
  3846. static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
  3847. abi_ulong parent_tidptr, target_ulong newtls,
  3848. abi_ulong child_tidptr)
  3849. {
  3850. int ret;
  3851. TaskState *ts;
  3852. CPUArchState *new_env;
  3853. #if defined(CONFIG_USE_NPTL)
  3854. unsigned int nptl_flags;
  3855. sigset_t sigmask;
  3856. #else
  3857. uint8_t *new_stack;
  3858. #endif
  3859. /* Emulate vfork() with fork() */
  3860. if (flags & CLONE_VFORK)
  3861. flags &= ~(CLONE_VFORK | CLONE_VM);
  3862. if (flags & CLONE_VM) {
  3863. TaskState *parent_ts = (TaskState *)env->opaque;
  3864. #if defined(CONFIG_USE_NPTL)
  3865. new_thread_info info;
  3866. pthread_attr_t attr;
  3867. #endif
  3868. ts = g_malloc0(sizeof(TaskState));
  3869. init_task_state(ts);
  3870. /* we create a new CPU instance. */
  3871. new_env = cpu_copy(env);
  3872. #if defined(TARGET_I386) || defined(TARGET_SPARC) || defined(TARGET_PPC)
  3873. cpu_reset(ENV_GET_CPU(new_env));
  3874. #endif
  3875. /* Init regs that differ from the parent. */
  3876. cpu_clone_regs(new_env, newsp);
  3877. new_env->opaque = ts;
  3878. ts->bprm = parent_ts->bprm;
  3879. ts->info = parent_ts->info;
  3880. #if defined(CONFIG_USE_NPTL)
  3881. nptl_flags = flags;
  3882. flags &= ~CLONE_NPTL_FLAGS2;
  3883. if (nptl_flags & CLONE_CHILD_CLEARTID) {
  3884. ts->child_tidptr = child_tidptr;
  3885. }
  3886. if (nptl_flags & CLONE_SETTLS)
  3887. cpu_set_tls (new_env, newtls);
  3888. /* Grab a mutex so that thread setup appears atomic. */
  3889. pthread_mutex_lock(&clone_lock);
  3890. memset(&info, 0, sizeof(info));
  3891. pthread_mutex_init(&info.mutex, NULL);
  3892. pthread_mutex_lock(&info.mutex);
  3893. pthread_cond_init(&info.cond, NULL);
  3894. info.env = new_env;
  3895. if (nptl_flags & CLONE_CHILD_SETTID)
  3896. info.child_tidptr = child_tidptr;
  3897. if (nptl_flags & CLONE_PARENT_SETTID)
  3898. info.parent_tidptr = parent_tidptr;
  3899. ret = pthread_attr_init(&attr);
  3900. ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
  3901. ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  3902. /* It is not safe to deliver signals until the child has finished
  3903. initializing, so temporarily block all signals. */
  3904. sigfillset(&sigmask);
  3905. sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
  3906. ret = pthread_create(&info.thread, &attr, clone_func, &info);
  3907. /* TODO: Free new CPU state if thread creation failed. */
  3908. sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
  3909. pthread_attr_destroy(&attr);
  3910. if (ret == 0) {
  3911. /* Wait for the child to initialize. */
  3912. pthread_cond_wait(&info.cond, &info.mutex);
  3913. ret = info.tid;
  3914. if (flags & CLONE_PARENT_SETTID)
  3915. put_user_u32(ret, parent_tidptr);
  3916. } else {
  3917. ret = -1;
  3918. }
  3919. pthread_mutex_unlock(&info.mutex);
  3920. pthread_cond_destroy(&info.cond);
  3921. pthread_mutex_destroy(&info.mutex);
  3922. pthread_mutex_unlock(&clone_lock);
  3923. #else
  3924. if (flags & CLONE_NPTL_FLAGS2)
  3925. return -EINVAL;
  3926. /* This is probably going to die very quickly, but do it anyway. */
  3927. new_stack = g_malloc0 (NEW_STACK_SIZE);
  3928. #ifdef __ia64__
  3929. ret = __clone2(clone_func, new_stack, NEW_STACK_SIZE, flags, new_env);
  3930. #else
  3931. ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
  3932. #endif
  3933. #endif
  3934. } else {
  3935. /* if no CLONE_VM, we consider it is a fork */
  3936. if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0)
  3937. return -EINVAL;
  3938. fork_start();
  3939. ret = fork();
  3940. if (ret == 0) {
  3941. /* Child Process. */
  3942. cpu_clone_regs(env, newsp);
  3943. fork_end(1);
  3944. #if defined(CONFIG_USE_NPTL)
  3945. /* There is a race condition here. The parent process could
  3946. theoretically read the TID in the child process before the child
  3947. tid is set. This would require using either ptrace
  3948. (not implemented) or having *_tidptr to point at a shared memory
  3949. mapping. We can't repeat the spinlock hack used above because
  3950. the child process gets its own copy of the lock. */
  3951. if (flags & CLONE_CHILD_SETTID)
  3952. put_user_u32(gettid(), child_tidptr);
  3953. if (flags & CLONE_PARENT_SETTID)
  3954. put_user_u32(gettid(), parent_tidptr);
  3955. ts = (TaskState *)env->opaque;
  3956. if (flags & CLONE_SETTLS)
  3957. cpu_set_tls (env, newtls);
  3958. if (flags & CLONE_CHILD_CLEARTID)
  3959. ts->child_tidptr = child_tidptr;
  3960. #endif
  3961. } else {
  3962. fork_end(0);
  3963. }
  3964. }
  3965. return ret;
  3966. }
  3967. /* warning : doesn't handle linux specific flags... */
  3968. static int target_to_host_fcntl_cmd(int cmd)
  3969. {
  3970. switch(cmd) {
  3971. case TARGET_F_DUPFD:
  3972. case TARGET_F_GETFD:
  3973. case TARGET_F_SETFD:
  3974. case TARGET_F_GETFL:
  3975. case TARGET_F_SETFL:
  3976. return cmd;
  3977. case TARGET_F_GETLK:
  3978. return F_GETLK;
  3979. case TARGET_F_SETLK:
  3980. return F_SETLK;
  3981. case TARGET_F_SETLKW:
  3982. return F_SETLKW;
  3983. case TARGET_F_GETOWN:
  3984. return F_GETOWN;
  3985. case TARGET_F_SETOWN:
  3986. return F_SETOWN;
  3987. case TARGET_F_GETSIG:
  3988. return F_GETSIG;
  3989. case TARGET_F_SETSIG:
  3990. return F_SETSIG;
  3991. #if TARGET_ABI_BITS == 32
  3992. case TARGET_F_GETLK64:
  3993. return F_GETLK64;
  3994. case TARGET_F_SETLK64:
  3995. return F_SETLK64;
  3996. case TARGET_F_SETLKW64:
  3997. return F_SETLKW64;
  3998. #endif
  3999. case TARGET_F_SETLEASE:
  4000. return F_SETLEASE;
  4001. case TARGET_F_GETLEASE:
  4002. return F_GETLEASE;
  4003. #ifdef F_DUPFD_CLOEXEC
  4004. case TARGET_F_DUPFD_CLOEXEC:
  4005. return F_DUPFD_CLOEXEC;
  4006. #endif
  4007. case TARGET_F_NOTIFY:
  4008. return F_NOTIFY;
  4009. default:
  4010. return -TARGET_EINVAL;
  4011. }
  4012. return -TARGET_EINVAL;
  4013. }
  4014. static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
  4015. {
  4016. struct flock fl;
  4017. struct target_flock *target_fl;
  4018. struct flock64 fl64;
  4019. struct target_flock64 *target_fl64;
  4020. abi_long ret;
  4021. int host_cmd = target_to_host_fcntl_cmd(cmd);
  4022. if (host_cmd == -TARGET_EINVAL)
  4023. return host_cmd;
  4024. switch(cmd) {
  4025. case TARGET_F_GETLK:
  4026. if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
  4027. return -TARGET_EFAULT;
  4028. fl.l_type = tswap16(target_fl->l_type);
  4029. fl.l_whence = tswap16(target_fl->l_whence);
  4030. fl.l_start = tswapal(target_fl->l_start);
  4031. fl.l_len = tswapal(target_fl->l_len);
  4032. fl.l_pid = tswap32(target_fl->l_pid);
  4033. unlock_user_struct(target_fl, arg, 0);
  4034. ret = get_errno(fcntl(fd, host_cmd, &fl));
  4035. if (ret == 0) {
  4036. if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
  4037. return -TARGET_EFAULT;
  4038. target_fl->l_type = tswap16(fl.l_type);
  4039. target_fl->l_whence = tswap16(fl.l_whence);
  4040. target_fl->l_start = tswapal(fl.l_start);
  4041. target_fl->l_len = tswapal(fl.l_len);
  4042. target_fl->l_pid = tswap32(fl.l_pid);
  4043. unlock_user_struct(target_fl, arg, 1);
  4044. }
  4045. break;
  4046. case TARGET_F_SETLK:
  4047. case TARGET_F_SETLKW:
  4048. if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
  4049. return -TARGET_EFAULT;
  4050. fl.l_type = tswap16(target_fl->l_type);
  4051. fl.l_whence = tswap16(target_fl->l_whence);
  4052. fl.l_start = tswapal(target_fl->l_start);
  4053. fl.l_len = tswapal(target_fl->l_len);
  4054. fl.l_pid = tswap32(target_fl->l_pid);
  4055. unlock_user_struct(target_fl, arg, 0);
  4056. ret = get_errno(fcntl(fd, host_cmd, &fl));
  4057. break;
  4058. case TARGET_F_GETLK64:
  4059. if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
  4060. return -TARGET_EFAULT;
  4061. fl64.l_type = tswap16(target_fl64->l_type) >> 1;
  4062. fl64.l_whence = tswap16(target_fl64->l_whence);
  4063. fl64.l_start = tswap64(target_fl64->l_start);
  4064. fl64.l_len = tswap64(target_fl64->l_len);
  4065. fl64.l_pid = tswap32(target_fl64->l_pid);
  4066. unlock_user_struct(target_fl64, arg, 0);
  4067. ret = get_errno(fcntl(fd, host_cmd, &fl64));
  4068. if (ret == 0) {
  4069. if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
  4070. return -TARGET_EFAULT;
  4071. target_fl64->l_type = tswap16(fl64.l_type) >> 1;
  4072. target_fl64->l_whence = tswap16(fl64.l_whence);
  4073. target_fl64->l_start = tswap64(fl64.l_start);
  4074. target_fl64->l_len = tswap64(fl64.l_len);
  4075. target_fl64->l_pid = tswap32(fl64.l_pid);
  4076. unlock_user_struct(target_fl64, arg, 1);
  4077. }
  4078. break;
  4079. case TARGET_F_SETLK64:
  4080. case TARGET_F_SETLKW64:
  4081. if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
  4082. return -TARGET_EFAULT;
  4083. fl64.l_type = tswap16(target_fl64->l_type) >> 1;
  4084. fl64.l_whence = tswap16(target_fl64->l_whence);
  4085. fl64.l_start = tswap64(target_fl64->l_start);
  4086. fl64.l_len = tswap64(target_fl64->l_len);
  4087. fl64.l_pid = tswap32(target_fl64->l_pid);
  4088. unlock_user_struct(target_fl64, arg, 0);
  4089. ret = get_errno(fcntl(fd, host_cmd, &fl64));
  4090. break;
  4091. case TARGET_F_GETFL:
  4092. ret = get_errno(fcntl(fd, host_cmd, arg));
  4093. if (ret >= 0) {
  4094. ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
  4095. }
  4096. break;
  4097. case TARGET_F_SETFL:
  4098. ret = get_errno(fcntl(fd, host_cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
  4099. break;
  4100. case TARGET_F_SETOWN:
  4101. case TARGET_F_GETOWN:
  4102. case TARGET_F_SETSIG:
  4103. case TARGET_F_GETSIG:
  4104. case TARGET_F_SETLEASE:
  4105. case TARGET_F_GETLEASE:
  4106. ret = get_errno(fcntl(fd, host_cmd, arg));
  4107. break;
  4108. default:
  4109. ret = get_errno(fcntl(fd, cmd, arg));
  4110. break;
  4111. }
  4112. return ret;
  4113. }
  4114. #ifdef USE_UID16
  4115. static inline int high2lowuid(int uid)
  4116. {
  4117. if (uid > 65535)
  4118. return 65534;
  4119. else
  4120. return uid;
  4121. }
  4122. static inline int high2lowgid(int gid)
  4123. {
  4124. if (gid > 65535)
  4125. return 65534;
  4126. else
  4127. return gid;
  4128. }
  4129. static inline int low2highuid(int uid)
  4130. {
  4131. if ((int16_t)uid == -1)
  4132. return -1;
  4133. else
  4134. return uid;
  4135. }
  4136. static inline int low2highgid(int gid)
  4137. {
  4138. if ((int16_t)gid == -1)
  4139. return -1;
  4140. else
  4141. return gid;
  4142. }
  4143. static inline int tswapid(int id)
  4144. {
  4145. return tswap16(id);
  4146. }
  4147. #else /* !USE_UID16 */
  4148. static inline int high2lowuid(int uid)
  4149. {
  4150. return uid;
  4151. }
  4152. static inline int high2lowgid(int gid)
  4153. {
  4154. return gid;
  4155. }
  4156. static inline int low2highuid(int uid)
  4157. {
  4158. return uid;
  4159. }
  4160. static inline int low2highgid(int gid)
  4161. {
  4162. return gid;
  4163. }
  4164. static inline int tswapid(int id)
  4165. {
  4166. return tswap32(id);
  4167. }
  4168. #endif /* USE_UID16 */
  4169. void syscall_init(void)
  4170. {
  4171. IOCTLEntry *ie;
  4172. const argtype *arg_type;
  4173. int size;
  4174. int i;
  4175. #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
  4176. #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
  4177. #include "syscall_types.h"
  4178. #undef STRUCT
  4179. #undef STRUCT_SPECIAL
  4180. /* Build target_to_host_errno_table[] table from
  4181. * host_to_target_errno_table[]. */
  4182. for (i = 0; i < ERRNO_TABLE_SIZE; i++) {
  4183. target_to_host_errno_table[host_to_target_errno_table[i]] = i;
  4184. }
  4185. /* we patch the ioctl size if necessary. We rely on the fact that
  4186. no ioctl has all the bits at '1' in the size field */
  4187. ie = ioctl_entries;
  4188. while (ie->target_cmd != 0) {
  4189. if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
  4190. TARGET_IOC_SIZEMASK) {
  4191. arg_type = ie->arg_type;
  4192. if (arg_type[0] != TYPE_PTR) {
  4193. fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
  4194. ie->target_cmd);
  4195. exit(1);
  4196. }
  4197. arg_type++;
  4198. size = thunk_type_size(arg_type, 0);
  4199. ie->target_cmd = (ie->target_cmd &
  4200. ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
  4201. (size << TARGET_IOC_SIZESHIFT);
  4202. }
  4203. /* automatic consistency check if same arch */
  4204. #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
  4205. (defined(__x86_64__) && defined(TARGET_X86_64))
  4206. if (unlikely(ie->target_cmd != ie->host_cmd)) {
  4207. fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
  4208. ie->name, ie->target_cmd, ie->host_cmd);
  4209. }
  4210. #endif
  4211. ie++;
  4212. }
  4213. }
  4214. #if TARGET_ABI_BITS == 32
  4215. static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
  4216. {
  4217. #ifdef TARGET_WORDS_BIGENDIAN
  4218. return ((uint64_t)word0 << 32) | word1;
  4219. #else
  4220. return ((uint64_t)word1 << 32) | word0;
  4221. #endif
  4222. }
  4223. #else /* TARGET_ABI_BITS == 32 */
  4224. static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
  4225. {
  4226. return word0;
  4227. }
  4228. #endif /* TARGET_ABI_BITS != 32 */
  4229. #ifdef TARGET_NR_truncate64
  4230. static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
  4231. abi_long arg2,
  4232. abi_long arg3,
  4233. abi_long arg4)
  4234. {
  4235. if (regpairs_aligned(cpu_env)) {
  4236. arg2 = arg3;
  4237. arg3 = arg4;
  4238. }
  4239. return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
  4240. }
  4241. #endif
  4242. #ifdef TARGET_NR_ftruncate64
  4243. static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
  4244. abi_long arg2,
  4245. abi_long arg3,
  4246. abi_long arg4)
  4247. {
  4248. if (regpairs_aligned(cpu_env)) {
  4249. arg2 = arg3;
  4250. arg3 = arg4;
  4251. }
  4252. return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
  4253. }
  4254. #endif
  4255. static inline abi_long target_to_host_timespec(struct timespec *host_ts,
  4256. abi_ulong target_addr)
  4257. {
  4258. struct target_timespec *target_ts;
  4259. if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
  4260. return -TARGET_EFAULT;
  4261. host_ts->tv_sec = tswapal(target_ts->tv_sec);
  4262. host_ts->tv_nsec = tswapal(target_ts->tv_nsec);
  4263. unlock_user_struct(target_ts, target_addr, 0);
  4264. return 0;
  4265. }
  4266. static inline abi_long host_to_target_timespec(abi_ulong target_addr,
  4267. struct timespec *host_ts)
  4268. {
  4269. struct target_timespec *target_ts;
  4270. if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
  4271. return -TARGET_EFAULT;
  4272. target_ts->tv_sec = tswapal(host_ts->tv_sec);
  4273. target_ts->tv_nsec = tswapal(host_ts->tv_nsec);
  4274. unlock_user_struct(target_ts, target_addr, 1);
  4275. return 0;
  4276. }
  4277. #if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
  4278. static inline abi_long host_to_target_stat64(void *cpu_env,
  4279. abi_ulong target_addr,
  4280. struct stat *host_st)
  4281. {
  4282. #ifdef TARGET_ARM
  4283. if (((CPUARMState *)cpu_env)->eabi) {
  4284. struct target_eabi_stat64 *target_st;
  4285. if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
  4286. return -TARGET_EFAULT;
  4287. memset(target_st, 0, sizeof(struct target_eabi_stat64));
  4288. __put_user(host_st->st_dev, &target_st->st_dev);
  4289. __put_user(host_st->st_ino, &target_st->st_ino);
  4290. #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
  4291. __put_user(host_st->st_ino, &target_st->__st_ino);
  4292. #endif
  4293. __put_user(host_st->st_mode, &target_st->st_mode);
  4294. __put_user(host_st->st_nlink, &target_st->st_nlink);
  4295. __put_user(host_st->st_uid, &target_st->st_uid);
  4296. __put_user(host_st->st_gid, &target_st->st_gid);
  4297. __put_user(host_st->st_rdev, &target_st->st_rdev);
  4298. __put_user(host_st->st_size, &target_st->st_size);
  4299. __put_user(host_st->st_blksize, &target_st->st_blksize);
  4300. __put_user(host_st->st_blocks, &target_st->st_blocks);
  4301. __put_user(host_st->st_atime, &target_st->target_st_atime);
  4302. __put_user(host_st->st_mtime, &target_st->target_st_mtime);
  4303. __put_user(host_st->st_ctime, &target_st->target_st_ctime);
  4304. unlock_user_struct(target_st, target_addr, 1);
  4305. } else
  4306. #endif
  4307. {
  4308. #if TARGET_ABI_BITS == 64 && !defined(TARGET_ALPHA)
  4309. struct target_stat *target_st;
  4310. #else
  4311. struct target_stat64 *target_st;
  4312. #endif
  4313. if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
  4314. return -TARGET_EFAULT;
  4315. memset(target_st, 0, sizeof(*target_st));
  4316. __put_user(host_st->st_dev, &target_st->st_dev);
  4317. __put_user(host_st->st_ino, &target_st->st_ino);
  4318. #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
  4319. __put_user(host_st->st_ino, &target_st->__st_ino);
  4320. #endif
  4321. __put_user(host_st->st_mode, &target_st->st_mode);
  4322. __put_user(host_st->st_nlink, &target_st->st_nlink);
  4323. __put_user(host_st->st_uid, &target_st->st_uid);
  4324. __put_user(host_st->st_gid, &target_st->st_gid);
  4325. __put_user(host_st->st_rdev, &target_st->st_rdev);
  4326. /* XXX: better use of kernel struct */
  4327. __put_user(host_st->st_size, &target_st->st_size);
  4328. __put_user(host_st->st_blksize, &target_st->st_blksize);
  4329. __put_user(host_st->st_blocks, &target_st->st_blocks);
  4330. __put_user(host_st->st_atime, &target_st->target_st_atime);
  4331. __put_user(host_st->st_mtime, &target_st->target_st_mtime);
  4332. __put_user(host_st->st_ctime, &target_st->target_st_ctime);
  4333. unlock_user_struct(target_st, target_addr, 1);
  4334. }
  4335. return 0;
  4336. }
  4337. #endif
  4338. #if defined(CONFIG_USE_NPTL)
  4339. /* ??? Using host futex calls even when target atomic operations
  4340. are not really atomic probably breaks things. However implementing
  4341. futexes locally would make futexes shared between multiple processes
  4342. tricky. However they're probably useless because guest atomic
  4343. operations won't work either. */
  4344. static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
  4345. target_ulong uaddr2, int val3)
  4346. {
  4347. struct timespec ts, *pts;
  4348. int base_op;
  4349. /* ??? We assume FUTEX_* constants are the same on both host
  4350. and target. */
  4351. #ifdef FUTEX_CMD_MASK
  4352. base_op = op & FUTEX_CMD_MASK;
  4353. #else
  4354. base_op = op;
  4355. #endif
  4356. switch (base_op) {
  4357. case FUTEX_WAIT:
  4358. if (timeout) {
  4359. pts = &ts;
  4360. target_to_host_timespec(pts, timeout);
  4361. } else {
  4362. pts = NULL;
  4363. }
  4364. return get_errno(sys_futex(g2h(uaddr), op, tswap32(val),
  4365. pts, NULL, 0));
  4366. case FUTEX_WAKE:
  4367. return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
  4368. case FUTEX_FD:
  4369. return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
  4370. case FUTEX_REQUEUE:
  4371. case FUTEX_CMP_REQUEUE:
  4372. case FUTEX_WAKE_OP:
  4373. /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
  4374. TIMEOUT parameter is interpreted as a uint32_t by the kernel.
  4375. But the prototype takes a `struct timespec *'; insert casts
  4376. to satisfy the compiler. We do not need to tswap TIMEOUT
  4377. since it's not compared to guest memory. */
  4378. pts = (struct timespec *)(uintptr_t) timeout;
  4379. return get_errno(sys_futex(g2h(uaddr), op, val, pts,
  4380. g2h(uaddr2),
  4381. (base_op == FUTEX_CMP_REQUEUE
  4382. ? tswap32(val3)
  4383. : val3)));
  4384. default:
  4385. return -TARGET_ENOSYS;
  4386. }
  4387. }
  4388. #endif
  4389. /* Map host to target signal numbers for the wait family of syscalls.
  4390. Assume all other status bits are the same. */
  4391. static int host_to_target_waitstatus(int status)
  4392. {
  4393. if (WIFSIGNALED(status)) {
  4394. return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
  4395. }
  4396. if (WIFSTOPPED(status)) {
  4397. return (host_to_target_signal(WSTOPSIG(status)) << 8)
  4398. | (status & 0xff);
  4399. }
  4400. return status;
  4401. }
  4402. int get_osversion(void)
  4403. {
  4404. static int osversion;
  4405. struct new_utsname buf;
  4406. const char *s;
  4407. int i, n, tmp;
  4408. if (osversion)
  4409. return osversion;
  4410. if (qemu_uname_release && *qemu_uname_release) {
  4411. s = qemu_uname_release;
  4412. } else {
  4413. if (sys_uname(&buf))
  4414. return 0;
  4415. s = buf.release;
  4416. }
  4417. tmp = 0;
  4418. for (i = 0; i < 3; i++) {
  4419. n = 0;
  4420. while (*s >= '0' && *s <= '9') {
  4421. n *= 10;
  4422. n += *s - '0';
  4423. s++;
  4424. }
  4425. tmp = (tmp << 8) + n;
  4426. if (*s == '.')
  4427. s++;
  4428. }
  4429. osversion = tmp;
  4430. return osversion;
  4431. }
  4432. static int open_self_maps(void *cpu_env, int fd)
  4433. {
  4434. #if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)
  4435. TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
  4436. #endif
  4437. FILE *fp;
  4438. char *line = NULL;
  4439. size_t len = 0;
  4440. ssize_t read;
  4441. fp = fopen("/proc/self/maps", "r");
  4442. if (fp == NULL) {
  4443. return -EACCES;
  4444. }
  4445. while ((read = getline(&line, &len, fp)) != -1) {
  4446. int fields, dev_maj, dev_min, inode;
  4447. uint64_t min, max, offset;
  4448. char flag_r, flag_w, flag_x, flag_p;
  4449. char path[512] = "";
  4450. fields = sscanf(line, "%"PRIx64"-%"PRIx64" %c%c%c%c %"PRIx64" %x:%x %d"
  4451. " %512s", &min, &max, &flag_r, &flag_w, &flag_x,
  4452. &flag_p, &offset, &dev_maj, &dev_min, &inode, path);
  4453. if ((fields < 10) || (fields > 11)) {
  4454. continue;
  4455. }
  4456. if (!strncmp(path, "[stack]", 7)) {
  4457. continue;
  4458. }
  4459. if (h2g_valid(min) && h2g_valid(max)) {
  4460. dprintf(fd, TARGET_ABI_FMT_lx "-" TARGET_ABI_FMT_lx
  4461. " %c%c%c%c %08" PRIx64 " %02x:%02x %d%s%s\n",
  4462. h2g(min), h2g(max), flag_r, flag_w,
  4463. flag_x, flag_p, offset, dev_maj, dev_min, inode,
  4464. path[0] ? " " : "", path);
  4465. }
  4466. }
  4467. free(line);
  4468. fclose(fp);
  4469. #if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)
  4470. dprintf(fd, "%08llx-%08llx rw-p %08llx 00:00 0 [stack]\n",
  4471. (unsigned long long)ts->info->stack_limit,
  4472. (unsigned long long)(ts->stack_base + (TARGET_PAGE_SIZE - 1))
  4473. & TARGET_PAGE_MASK,
  4474. (unsigned long long)0);
  4475. #endif
  4476. return 0;
  4477. }
  4478. static int open_self_stat(void *cpu_env, int fd)
  4479. {
  4480. TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
  4481. abi_ulong start_stack = ts->info->start_stack;
  4482. int i;
  4483. for (i = 0; i < 44; i++) {
  4484. char buf[128];
  4485. int len;
  4486. uint64_t val = 0;
  4487. if (i == 0) {
  4488. /* pid */
  4489. val = getpid();
  4490. snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
  4491. } else if (i == 1) {
  4492. /* app name */
  4493. snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
  4494. } else if (i == 27) {
  4495. /* stack bottom */
  4496. val = start_stack;
  4497. snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
  4498. } else {
  4499. /* for the rest, there is MasterCard */
  4500. snprintf(buf, sizeof(buf), "0%c", i == 43 ? '\n' : ' ');
  4501. }
  4502. len = strlen(buf);
  4503. if (write(fd, buf, len) != len) {
  4504. return -1;
  4505. }
  4506. }
  4507. return 0;
  4508. }
  4509. static int open_self_auxv(void *cpu_env, int fd)
  4510. {
  4511. TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
  4512. abi_ulong auxv = ts->info->saved_auxv;
  4513. abi_ulong len = ts->info->auxv_len;
  4514. char *ptr;
  4515. /*
  4516. * Auxiliary vector is stored in target process stack.
  4517. * read in whole auxv vector and copy it to file
  4518. */
  4519. ptr = lock_user(VERIFY_READ, auxv, len, 0);
  4520. if (ptr != NULL) {
  4521. while (len > 0) {
  4522. ssize_t r;
  4523. r = write(fd, ptr, len);
  4524. if (r <= 0) {
  4525. break;
  4526. }
  4527. len -= r;
  4528. ptr += r;
  4529. }
  4530. lseek(fd, 0, SEEK_SET);
  4531. unlock_user(ptr, auxv, len);
  4532. }
  4533. return 0;
  4534. }
  4535. static int do_open(void *cpu_env, const char *pathname, int flags, mode_t mode)
  4536. {
  4537. struct fake_open {
  4538. const char *filename;
  4539. int (*fill)(void *cpu_env, int fd);
  4540. };
  4541. const struct fake_open *fake_open;
  4542. static const struct fake_open fakes[] = {
  4543. { "/proc/self/maps", open_self_maps },
  4544. { "/proc/self/stat", open_self_stat },
  4545. { "/proc/self/auxv", open_self_auxv },
  4546. { NULL, NULL }
  4547. };
  4548. for (fake_open = fakes; fake_open->filename; fake_open++) {
  4549. if (!strncmp(pathname, fake_open->filename,
  4550. strlen(fake_open->filename))) {
  4551. break;
  4552. }
  4553. }
  4554. if (fake_open->filename) {
  4555. const char *tmpdir;
  4556. char filename[PATH_MAX];
  4557. int fd, r;
  4558. /* create temporary file to map stat to */
  4559. tmpdir = getenv("TMPDIR");
  4560. if (!tmpdir)
  4561. tmpdir = "/tmp";
  4562. snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
  4563. fd = mkstemp(filename);
  4564. if (fd < 0) {
  4565. return fd;
  4566. }
  4567. unlink(filename);
  4568. if ((r = fake_open->fill(cpu_env, fd))) {
  4569. close(fd);
  4570. return r;
  4571. }
  4572. lseek(fd, 0, SEEK_SET);
  4573. return fd;
  4574. }
  4575. return get_errno(open(path(pathname), flags, mode));
  4576. }
  4577. /* do_syscall() should always have a single exit point at the end so
  4578. that actions, such as logging of syscall results, can be performed.
  4579. All errnos that do_syscall() returns must be -TARGET_<errcode>. */
  4580. abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
  4581. abi_long arg2, abi_long arg3, abi_long arg4,
  4582. abi_long arg5, abi_long arg6, abi_long arg7,
  4583. abi_long arg8)
  4584. {
  4585. abi_long ret;
  4586. struct stat st;
  4587. struct statfs stfs;
  4588. void *p;
  4589. #ifdef DEBUG
  4590. gemu_log("syscall %d", num);
  4591. #endif
  4592. if(do_strace)
  4593. print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
  4594. switch(num) {
  4595. case TARGET_NR_exit:
  4596. #ifdef CONFIG_USE_NPTL
  4597. /* In old applications this may be used to implement _exit(2).
  4598. However in threaded applictions it is used for thread termination,
  4599. and _exit_group is used for application termination.
  4600. Do thread termination if we have more then one thread. */
  4601. /* FIXME: This probably breaks if a signal arrives. We should probably
  4602. be disabling signals. */
  4603. if (first_cpu->next_cpu) {
  4604. TaskState *ts;
  4605. CPUArchState **lastp;
  4606. CPUArchState *p;
  4607. cpu_list_lock();
  4608. lastp = &first_cpu;
  4609. p = first_cpu;
  4610. while (p && p != (CPUArchState *)cpu_env) {
  4611. lastp = &p->next_cpu;
  4612. p = p->next_cpu;
  4613. }
  4614. /* If we didn't find the CPU for this thread then something is
  4615. horribly wrong. */
  4616. if (!p)
  4617. abort();
  4618. /* Remove the CPU from the list. */
  4619. *lastp = p->next_cpu;
  4620. cpu_list_unlock();
  4621. ts = ((CPUArchState *)cpu_env)->opaque;
  4622. if (ts->child_tidptr) {
  4623. put_user_u32(0, ts->child_tidptr);
  4624. sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
  4625. NULL, NULL, 0);
  4626. }
  4627. thread_env = NULL;
  4628. object_delete(OBJECT(ENV_GET_CPU(cpu_env)));
  4629. g_free(ts);
  4630. pthread_exit(NULL);
  4631. }
  4632. #endif
  4633. #ifdef TARGET_GPROF
  4634. _mcleanup();
  4635. #endif
  4636. gdb_exit(cpu_env, arg1);
  4637. _exit(arg1);
  4638. ret = 0; /* avoid warning */
  4639. break;
  4640. case TARGET_NR_read:
  4641. if (arg3 == 0)
  4642. ret = 0;
  4643. else {
  4644. if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
  4645. goto efault;
  4646. ret = get_errno(read(arg1, p, arg3));
  4647. unlock_user(p, arg2, ret);
  4648. }
  4649. break;
  4650. case TARGET_NR_write:
  4651. if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
  4652. goto efault;
  4653. ret = get_errno(write(arg1, p, arg3));
  4654. unlock_user(p, arg2, 0);
  4655. break;
  4656. case TARGET_NR_open:
  4657. if (!(p = lock_user_string(arg1)))
  4658. goto efault;
  4659. ret = get_errno(do_open(cpu_env, p,
  4660. target_to_host_bitmask(arg2, fcntl_flags_tbl),
  4661. arg3));
  4662. unlock_user(p, arg1, 0);
  4663. break;
  4664. #if defined(TARGET_NR_openat) && defined(__NR_openat)
  4665. case TARGET_NR_openat:
  4666. if (!(p = lock_user_string(arg2)))
  4667. goto efault;
  4668. ret = get_errno(sys_openat(arg1,
  4669. path(p),
  4670. target_to_host_bitmask(arg3, fcntl_flags_tbl),
  4671. arg4));
  4672. unlock_user(p, arg2, 0);
  4673. break;
  4674. #endif
  4675. case TARGET_NR_close:
  4676. ret = get_errno(close(arg1));
  4677. break;
  4678. case TARGET_NR_brk:
  4679. ret = do_brk(arg1);
  4680. break;
  4681. case TARGET_NR_fork:
  4682. ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
  4683. break;
  4684. #ifdef TARGET_NR_waitpid
  4685. case TARGET_NR_waitpid:
  4686. {
  4687. int status;
  4688. ret = get_errno(waitpid(arg1, &status, arg3));
  4689. if (!is_error(ret) && arg2 && ret
  4690. && put_user_s32(host_to_target_waitstatus(status), arg2))
  4691. goto efault;
  4692. }
  4693. break;
  4694. #endif
  4695. #ifdef TARGET_NR_waitid
  4696. case TARGET_NR_waitid:
  4697. {
  4698. siginfo_t info;
  4699. info.si_pid = 0;
  4700. ret = get_errno(waitid(arg1, arg2, &info, arg4));
  4701. if (!is_error(ret) && arg3 && info.si_pid != 0) {
  4702. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
  4703. goto efault;
  4704. host_to_target_siginfo(p, &info);
  4705. unlock_user(p, arg3, sizeof(target_siginfo_t));
  4706. }
  4707. }
  4708. break;
  4709. #endif
  4710. #ifdef TARGET_NR_creat /* not on alpha */
  4711. case TARGET_NR_creat:
  4712. if (!(p = lock_user_string(arg1)))
  4713. goto efault;
  4714. ret = get_errno(creat(p, arg2));
  4715. unlock_user(p, arg1, 0);
  4716. break;
  4717. #endif
  4718. case TARGET_NR_link:
  4719. {
  4720. void * p2;
  4721. p = lock_user_string(arg1);
  4722. p2 = lock_user_string(arg2);
  4723. if (!p || !p2)
  4724. ret = -TARGET_EFAULT;
  4725. else
  4726. ret = get_errno(link(p, p2));
  4727. unlock_user(p2, arg2, 0);
  4728. unlock_user(p, arg1, 0);
  4729. }
  4730. break;
  4731. #if defined(TARGET_NR_linkat) && defined(__NR_linkat)
  4732. case TARGET_NR_linkat:
  4733. {
  4734. void * p2 = NULL;
  4735. if (!arg2 || !arg4)
  4736. goto efault;
  4737. p = lock_user_string(arg2);
  4738. p2 = lock_user_string(arg4);
  4739. if (!p || !p2)
  4740. ret = -TARGET_EFAULT;
  4741. else
  4742. ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
  4743. unlock_user(p, arg2, 0);
  4744. unlock_user(p2, arg4, 0);
  4745. }
  4746. break;
  4747. #endif
  4748. case TARGET_NR_unlink:
  4749. if (!(p = lock_user_string(arg1)))
  4750. goto efault;
  4751. ret = get_errno(unlink(p));
  4752. unlock_user(p, arg1, 0);
  4753. break;
  4754. #if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
  4755. case TARGET_NR_unlinkat:
  4756. if (!(p = lock_user_string(arg2)))
  4757. goto efault;
  4758. ret = get_errno(sys_unlinkat(arg1, p, arg3));
  4759. unlock_user(p, arg2, 0);
  4760. break;
  4761. #endif
  4762. case TARGET_NR_execve:
  4763. {
  4764. char **argp, **envp;
  4765. int argc, envc;
  4766. abi_ulong gp;
  4767. abi_ulong guest_argp;
  4768. abi_ulong guest_envp;
  4769. abi_ulong addr;
  4770. char **q;
  4771. int total_size = 0;
  4772. argc = 0;
  4773. guest_argp = arg2;
  4774. for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
  4775. if (get_user_ual(addr, gp))
  4776. goto efault;
  4777. if (!addr)
  4778. break;
  4779. argc++;
  4780. }
  4781. envc = 0;
  4782. guest_envp = arg3;
  4783. for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
  4784. if (get_user_ual(addr, gp))
  4785. goto efault;
  4786. if (!addr)
  4787. break;
  4788. envc++;
  4789. }
  4790. argp = alloca((argc + 1) * sizeof(void *));
  4791. envp = alloca((envc + 1) * sizeof(void *));
  4792. for (gp = guest_argp, q = argp; gp;
  4793. gp += sizeof(abi_ulong), q++) {
  4794. if (get_user_ual(addr, gp))
  4795. goto execve_efault;
  4796. if (!addr)
  4797. break;
  4798. if (!(*q = lock_user_string(addr)))
  4799. goto execve_efault;
  4800. total_size += strlen(*q) + 1;
  4801. }
  4802. *q = NULL;
  4803. for (gp = guest_envp, q = envp; gp;
  4804. gp += sizeof(abi_ulong), q++) {
  4805. if (get_user_ual(addr, gp))
  4806. goto execve_efault;
  4807. if (!addr)
  4808. break;
  4809. if (!(*q = lock_user_string(addr)))
  4810. goto execve_efault;
  4811. total_size += strlen(*q) + 1;
  4812. }
  4813. *q = NULL;
  4814. /* This case will not be caught by the host's execve() if its
  4815. page size is bigger than the target's. */
  4816. if (total_size > MAX_ARG_PAGES * TARGET_PAGE_SIZE) {
  4817. ret = -TARGET_E2BIG;
  4818. goto execve_end;
  4819. }
  4820. if (!(p = lock_user_string(arg1)))
  4821. goto execve_efault;
  4822. ret = get_errno(execve(p, argp, envp));
  4823. unlock_user(p, arg1, 0);
  4824. goto execve_end;
  4825. execve_efault:
  4826. ret = -TARGET_EFAULT;
  4827. execve_end:
  4828. for (gp = guest_argp, q = argp; *q;
  4829. gp += sizeof(abi_ulong), q++) {
  4830. if (get_user_ual(addr, gp)
  4831. || !addr)
  4832. break;
  4833. unlock_user(*q, addr, 0);
  4834. }
  4835. for (gp = guest_envp, q = envp; *q;
  4836. gp += sizeof(abi_ulong), q++) {
  4837. if (get_user_ual(addr, gp)
  4838. || !addr)
  4839. break;
  4840. unlock_user(*q, addr, 0);
  4841. }
  4842. }
  4843. break;
  4844. case TARGET_NR_chdir:
  4845. if (!(p = lock_user_string(arg1)))
  4846. goto efault;
  4847. ret = get_errno(chdir(p));
  4848. unlock_user(p, arg1, 0);
  4849. break;
  4850. #ifdef TARGET_NR_time
  4851. case TARGET_NR_time:
  4852. {
  4853. time_t host_time;
  4854. ret = get_errno(time(&host_time));
  4855. if (!is_error(ret)
  4856. && arg1
  4857. && put_user_sal(host_time, arg1))
  4858. goto efault;
  4859. }
  4860. break;
  4861. #endif
  4862. case TARGET_NR_mknod:
  4863. if (!(p = lock_user_string(arg1)))
  4864. goto efault;
  4865. ret = get_errno(mknod(p, arg2, arg3));
  4866. unlock_user(p, arg1, 0);
  4867. break;
  4868. #if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
  4869. case TARGET_NR_mknodat:
  4870. if (!(p = lock_user_string(arg2)))
  4871. goto efault;
  4872. ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
  4873. unlock_user(p, arg2, 0);
  4874. break;
  4875. #endif
  4876. case TARGET_NR_chmod:
  4877. if (!(p = lock_user_string(arg1)))
  4878. goto efault;
  4879. ret = get_errno(chmod(p, arg2));
  4880. unlock_user(p, arg1, 0);
  4881. break;
  4882. #ifdef TARGET_NR_break
  4883. case TARGET_NR_break:
  4884. goto unimplemented;
  4885. #endif
  4886. #ifdef TARGET_NR_oldstat
  4887. case TARGET_NR_oldstat:
  4888. goto unimplemented;
  4889. #endif
  4890. case TARGET_NR_lseek:
  4891. ret = get_errno(lseek(arg1, arg2, arg3));
  4892. break;
  4893. #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
  4894. /* Alpha specific */
  4895. case TARGET_NR_getxpid:
  4896. ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
  4897. ret = get_errno(getpid());
  4898. break;
  4899. #endif
  4900. #ifdef TARGET_NR_getpid
  4901. case TARGET_NR_getpid:
  4902. ret = get_errno(getpid());
  4903. break;
  4904. #endif
  4905. case TARGET_NR_mount:
  4906. {
  4907. /* need to look at the data field */
  4908. void *p2, *p3;
  4909. p = lock_user_string(arg1);
  4910. p2 = lock_user_string(arg2);
  4911. p3 = lock_user_string(arg3);
  4912. if (!p || !p2 || !p3)
  4913. ret = -TARGET_EFAULT;
  4914. else {
  4915. /* FIXME - arg5 should be locked, but it isn't clear how to
  4916. * do that since it's not guaranteed to be a NULL-terminated
  4917. * string.
  4918. */
  4919. if ( ! arg5 )
  4920. ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, NULL));
  4921. else
  4922. ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
  4923. }
  4924. unlock_user(p, arg1, 0);
  4925. unlock_user(p2, arg2, 0);
  4926. unlock_user(p3, arg3, 0);
  4927. break;
  4928. }
  4929. #ifdef TARGET_NR_umount
  4930. case TARGET_NR_umount:
  4931. if (!(p = lock_user_string(arg1)))
  4932. goto efault;
  4933. ret = get_errno(umount(p));
  4934. unlock_user(p, arg1, 0);
  4935. break;
  4936. #endif
  4937. #ifdef TARGET_NR_stime /* not on alpha */
  4938. case TARGET_NR_stime:
  4939. {
  4940. time_t host_time;
  4941. if (get_user_sal(host_time, arg1))
  4942. goto efault;
  4943. ret = get_errno(stime(&host_time));
  4944. }
  4945. break;
  4946. #endif
  4947. case TARGET_NR_ptrace:
  4948. goto unimplemented;
  4949. #ifdef TARGET_NR_alarm /* not on alpha */
  4950. case TARGET_NR_alarm:
  4951. ret = alarm(arg1);
  4952. break;
  4953. #endif
  4954. #ifdef TARGET_NR_oldfstat
  4955. case TARGET_NR_oldfstat:
  4956. goto unimplemented;
  4957. #endif
  4958. #ifdef TARGET_NR_pause /* not on alpha */
  4959. case TARGET_NR_pause:
  4960. ret = get_errno(pause());
  4961. break;
  4962. #endif
  4963. #ifdef TARGET_NR_utime
  4964. case TARGET_NR_utime:
  4965. {
  4966. struct utimbuf tbuf, *host_tbuf;
  4967. struct target_utimbuf *target_tbuf;
  4968. if (arg2) {
  4969. if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
  4970. goto efault;
  4971. tbuf.actime = tswapal(target_tbuf->actime);
  4972. tbuf.modtime = tswapal(target_tbuf->modtime);
  4973. unlock_user_struct(target_tbuf, arg2, 0);
  4974. host_tbuf = &tbuf;
  4975. } else {
  4976. host_tbuf = NULL;
  4977. }
  4978. if (!(p = lock_user_string(arg1)))
  4979. goto efault;
  4980. ret = get_errno(utime(p, host_tbuf));
  4981. unlock_user(p, arg1, 0);
  4982. }
  4983. break;
  4984. #endif
  4985. case TARGET_NR_utimes:
  4986. {
  4987. struct timeval *tvp, tv[2];
  4988. if (arg2) {
  4989. if (copy_from_user_timeval(&tv[0], arg2)
  4990. || copy_from_user_timeval(&tv[1],
  4991. arg2 + sizeof(struct target_timeval)))
  4992. goto efault;
  4993. tvp = tv;
  4994. } else {
  4995. tvp = NULL;
  4996. }
  4997. if (!(p = lock_user_string(arg1)))
  4998. goto efault;
  4999. ret = get_errno(utimes(p, tvp));
  5000. unlock_user(p, arg1, 0);
  5001. }
  5002. break;
  5003. #if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
  5004. case TARGET_NR_futimesat:
  5005. {
  5006. struct timeval *tvp, tv[2];
  5007. if (arg3) {
  5008. if (copy_from_user_timeval(&tv[0], arg3)
  5009. || copy_from_user_timeval(&tv[1],
  5010. arg3 + sizeof(struct target_timeval)))
  5011. goto efault;
  5012. tvp = tv;
  5013. } else {
  5014. tvp = NULL;
  5015. }
  5016. if (!(p = lock_user_string(arg2)))
  5017. goto efault;
  5018. ret = get_errno(sys_futimesat(arg1, path(p), tvp));
  5019. unlock_user(p, arg2, 0);
  5020. }
  5021. break;
  5022. #endif
  5023. #ifdef TARGET_NR_stty
  5024. case TARGET_NR_stty:
  5025. goto unimplemented;
  5026. #endif
  5027. #ifdef TARGET_NR_gtty
  5028. case TARGET_NR_gtty:
  5029. goto unimplemented;
  5030. #endif
  5031. case TARGET_NR_access:
  5032. if (!(p = lock_user_string(arg1)))
  5033. goto efault;
  5034. ret = get_errno(access(path(p), arg2));
  5035. unlock_user(p, arg1, 0);
  5036. break;
  5037. #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
  5038. case TARGET_NR_faccessat:
  5039. if (!(p = lock_user_string(arg2)))
  5040. goto efault;
  5041. ret = get_errno(sys_faccessat(arg1, p, arg3));
  5042. unlock_user(p, arg2, 0);
  5043. break;
  5044. #endif
  5045. #ifdef TARGET_NR_nice /* not on alpha */
  5046. case TARGET_NR_nice:
  5047. ret = get_errno(nice(arg1));
  5048. break;
  5049. #endif
  5050. #ifdef TARGET_NR_ftime
  5051. case TARGET_NR_ftime:
  5052. goto unimplemented;
  5053. #endif
  5054. case TARGET_NR_sync:
  5055. sync();
  5056. ret = 0;
  5057. break;
  5058. case TARGET_NR_kill:
  5059. ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
  5060. break;
  5061. case TARGET_NR_rename:
  5062. {
  5063. void *p2;
  5064. p = lock_user_string(arg1);
  5065. p2 = lock_user_string(arg2);
  5066. if (!p || !p2)
  5067. ret = -TARGET_EFAULT;
  5068. else
  5069. ret = get_errno(rename(p, p2));
  5070. unlock_user(p2, arg2, 0);
  5071. unlock_user(p, arg1, 0);
  5072. }
  5073. break;
  5074. #if defined(TARGET_NR_renameat) && defined(__NR_renameat)
  5075. case TARGET_NR_renameat:
  5076. {
  5077. void *p2;
  5078. p = lock_user_string(arg2);
  5079. p2 = lock_user_string(arg4);
  5080. if (!p || !p2)
  5081. ret = -TARGET_EFAULT;
  5082. else
  5083. ret = get_errno(sys_renameat(arg1, p, arg3, p2));
  5084. unlock_user(p2, arg4, 0);
  5085. unlock_user(p, arg2, 0);
  5086. }
  5087. break;
  5088. #endif
  5089. case TARGET_NR_mkdir:
  5090. if (!(p = lock_user_string(arg1)))
  5091. goto efault;
  5092. ret = get_errno(mkdir(p, arg2));
  5093. unlock_user(p, arg1, 0);
  5094. break;
  5095. #if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
  5096. case TARGET_NR_mkdirat:
  5097. if (!(p = lock_user_string(arg2)))
  5098. goto efault;
  5099. ret = get_errno(sys_mkdirat(arg1, p, arg3));
  5100. unlock_user(p, arg2, 0);
  5101. break;
  5102. #endif
  5103. case TARGET_NR_rmdir:
  5104. if (!(p = lock_user_string(arg1)))
  5105. goto efault;
  5106. ret = get_errno(rmdir(p));
  5107. unlock_user(p, arg1, 0);
  5108. break;
  5109. case TARGET_NR_dup:
  5110. ret = get_errno(dup(arg1));
  5111. break;
  5112. case TARGET_NR_pipe:
  5113. ret = do_pipe(cpu_env, arg1, 0, 0);
  5114. break;
  5115. #ifdef TARGET_NR_pipe2
  5116. case TARGET_NR_pipe2:
  5117. ret = do_pipe(cpu_env, arg1,
  5118. target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
  5119. break;
  5120. #endif
  5121. case TARGET_NR_times:
  5122. {
  5123. struct target_tms *tmsp;
  5124. struct tms tms;
  5125. ret = get_errno(times(&tms));
  5126. if (arg1) {
  5127. tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
  5128. if (!tmsp)
  5129. goto efault;
  5130. tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
  5131. tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
  5132. tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
  5133. tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
  5134. }
  5135. if (!is_error(ret))
  5136. ret = host_to_target_clock_t(ret);
  5137. }
  5138. break;
  5139. #ifdef TARGET_NR_prof
  5140. case TARGET_NR_prof:
  5141. goto unimplemented;
  5142. #endif
  5143. #ifdef TARGET_NR_signal
  5144. case TARGET_NR_signal:
  5145. goto unimplemented;
  5146. #endif
  5147. case TARGET_NR_acct:
  5148. if (arg1 == 0) {
  5149. ret = get_errno(acct(NULL));
  5150. } else {
  5151. if (!(p = lock_user_string(arg1)))
  5152. goto efault;
  5153. ret = get_errno(acct(path(p)));
  5154. unlock_user(p, arg1, 0);
  5155. }
  5156. break;
  5157. #ifdef TARGET_NR_umount2 /* not on alpha */
  5158. case TARGET_NR_umount2:
  5159. if (!(p = lock_user_string(arg1)))
  5160. goto efault;
  5161. ret = get_errno(umount2(p, arg2));
  5162. unlock_user(p, arg1, 0);
  5163. break;
  5164. #endif
  5165. #ifdef TARGET_NR_lock
  5166. case TARGET_NR_lock:
  5167. goto unimplemented;
  5168. #endif
  5169. case TARGET_NR_ioctl:
  5170. ret = do_ioctl(arg1, arg2, arg3);
  5171. break;
  5172. case TARGET_NR_fcntl:
  5173. ret = do_fcntl(arg1, arg2, arg3);
  5174. break;
  5175. #ifdef TARGET_NR_mpx
  5176. case TARGET_NR_mpx:
  5177. goto unimplemented;
  5178. #endif
  5179. case TARGET_NR_setpgid:
  5180. ret = get_errno(setpgid(arg1, arg2));
  5181. break;
  5182. #ifdef TARGET_NR_ulimit
  5183. case TARGET_NR_ulimit:
  5184. goto unimplemented;
  5185. #endif
  5186. #ifdef TARGET_NR_oldolduname
  5187. case TARGET_NR_oldolduname:
  5188. goto unimplemented;
  5189. #endif
  5190. case TARGET_NR_umask:
  5191. ret = get_errno(umask(arg1));
  5192. break;
  5193. case TARGET_NR_chroot:
  5194. if (!(p = lock_user_string(arg1)))
  5195. goto efault;
  5196. ret = get_errno(chroot(p));
  5197. unlock_user(p, arg1, 0);
  5198. break;
  5199. case TARGET_NR_ustat:
  5200. goto unimplemented;
  5201. case TARGET_NR_dup2:
  5202. ret = get_errno(dup2(arg1, arg2));
  5203. break;
  5204. #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
  5205. case TARGET_NR_dup3:
  5206. ret = get_errno(dup3(arg1, arg2, arg3));
  5207. break;
  5208. #endif
  5209. #ifdef TARGET_NR_getppid /* not on alpha */
  5210. case TARGET_NR_getppid:
  5211. ret = get_errno(getppid());
  5212. break;
  5213. #endif
  5214. case TARGET_NR_getpgrp:
  5215. ret = get_errno(getpgrp());
  5216. break;
  5217. case TARGET_NR_setsid:
  5218. ret = get_errno(setsid());
  5219. break;
  5220. #ifdef TARGET_NR_sigaction
  5221. case TARGET_NR_sigaction:
  5222. {
  5223. #if defined(TARGET_ALPHA)
  5224. struct target_sigaction act, oact, *pact = 0;
  5225. struct target_old_sigaction *old_act;
  5226. if (arg2) {
  5227. if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
  5228. goto efault;
  5229. act._sa_handler = old_act->_sa_handler;
  5230. target_siginitset(&act.sa_mask, old_act->sa_mask);
  5231. act.sa_flags = old_act->sa_flags;
  5232. act.sa_restorer = 0;
  5233. unlock_user_struct(old_act, arg2, 0);
  5234. pact = &act;
  5235. }
  5236. ret = get_errno(do_sigaction(arg1, pact, &oact));
  5237. if (!is_error(ret) && arg3) {
  5238. if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
  5239. goto efault;
  5240. old_act->_sa_handler = oact._sa_handler;
  5241. old_act->sa_mask = oact.sa_mask.sig[0];
  5242. old_act->sa_flags = oact.sa_flags;
  5243. unlock_user_struct(old_act, arg3, 1);
  5244. }
  5245. #elif defined(TARGET_MIPS)
  5246. struct target_sigaction act, oact, *pact, *old_act;
  5247. if (arg2) {
  5248. if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
  5249. goto efault;
  5250. act._sa_handler = old_act->_sa_handler;
  5251. target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
  5252. act.sa_flags = old_act->sa_flags;
  5253. unlock_user_struct(old_act, arg2, 0);
  5254. pact = &act;
  5255. } else {
  5256. pact = NULL;
  5257. }
  5258. ret = get_errno(do_sigaction(arg1, pact, &oact));
  5259. if (!is_error(ret) && arg3) {
  5260. if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
  5261. goto efault;
  5262. old_act->_sa_handler = oact._sa_handler;
  5263. old_act->sa_flags = oact.sa_flags;
  5264. old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
  5265. old_act->sa_mask.sig[1] = 0;
  5266. old_act->sa_mask.sig[2] = 0;
  5267. old_act->sa_mask.sig[3] = 0;
  5268. unlock_user_struct(old_act, arg3, 1);
  5269. }
  5270. #else
  5271. struct target_old_sigaction *old_act;
  5272. struct target_sigaction act, oact, *pact;
  5273. if (arg2) {
  5274. if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
  5275. goto efault;
  5276. act._sa_handler = old_act->_sa_handler;
  5277. target_siginitset(&act.sa_mask, old_act->sa_mask);
  5278. act.sa_flags = old_act->sa_flags;
  5279. act.sa_restorer = old_act->sa_restorer;
  5280. unlock_user_struct(old_act, arg2, 0);
  5281. pact = &act;
  5282. } else {
  5283. pact = NULL;
  5284. }
  5285. ret = get_errno(do_sigaction(arg1, pact, &oact));
  5286. if (!is_error(ret) && arg3) {
  5287. if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
  5288. goto efault;
  5289. old_act->_sa_handler = oact._sa_handler;
  5290. old_act->sa_mask = oact.sa_mask.sig[0];
  5291. old_act->sa_flags = oact.sa_flags;
  5292. old_act->sa_restorer = oact.sa_restorer;
  5293. unlock_user_struct(old_act, arg3, 1);
  5294. }
  5295. #endif
  5296. }
  5297. break;
  5298. #endif
  5299. case TARGET_NR_rt_sigaction:
  5300. {
  5301. #if defined(TARGET_ALPHA)
  5302. struct target_sigaction act, oact, *pact = 0;
  5303. struct target_rt_sigaction *rt_act;
  5304. /* ??? arg4 == sizeof(sigset_t). */
  5305. if (arg2) {
  5306. if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
  5307. goto efault;
  5308. act._sa_handler = rt_act->_sa_handler;
  5309. act.sa_mask = rt_act->sa_mask;
  5310. act.sa_flags = rt_act->sa_flags;
  5311. act.sa_restorer = arg5;
  5312. unlock_user_struct(rt_act, arg2, 0);
  5313. pact = &act;
  5314. }
  5315. ret = get_errno(do_sigaction(arg1, pact, &oact));
  5316. if (!is_error(ret) && arg3) {
  5317. if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
  5318. goto efault;
  5319. rt_act->_sa_handler = oact._sa_handler;
  5320. rt_act->sa_mask = oact.sa_mask;
  5321. rt_act->sa_flags = oact.sa_flags;
  5322. unlock_user_struct(rt_act, arg3, 1);
  5323. }
  5324. #else
  5325. struct target_sigaction *act;
  5326. struct target_sigaction *oact;
  5327. if (arg2) {
  5328. if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
  5329. goto efault;
  5330. } else
  5331. act = NULL;
  5332. if (arg3) {
  5333. if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
  5334. ret = -TARGET_EFAULT;
  5335. goto rt_sigaction_fail;
  5336. }
  5337. } else
  5338. oact = NULL;
  5339. ret = get_errno(do_sigaction(arg1, act, oact));
  5340. rt_sigaction_fail:
  5341. if (act)
  5342. unlock_user_struct(act, arg2, 0);
  5343. if (oact)
  5344. unlock_user_struct(oact, arg3, 1);
  5345. #endif
  5346. }
  5347. break;
  5348. #ifdef TARGET_NR_sgetmask /* not on alpha */
  5349. case TARGET_NR_sgetmask:
  5350. {
  5351. sigset_t cur_set;
  5352. abi_ulong target_set;
  5353. sigprocmask(0, NULL, &cur_set);
  5354. host_to_target_old_sigset(&target_set, &cur_set);
  5355. ret = target_set;
  5356. }
  5357. break;
  5358. #endif
  5359. #ifdef TARGET_NR_ssetmask /* not on alpha */
  5360. case TARGET_NR_ssetmask:
  5361. {
  5362. sigset_t set, oset, cur_set;
  5363. abi_ulong target_set = arg1;
  5364. sigprocmask(0, NULL, &cur_set);
  5365. target_to_host_old_sigset(&set, &target_set);
  5366. sigorset(&set, &set, &cur_set);
  5367. sigprocmask(SIG_SETMASK, &set, &oset);
  5368. host_to_target_old_sigset(&target_set, &oset);
  5369. ret = target_set;
  5370. }
  5371. break;
  5372. #endif
  5373. #ifdef TARGET_NR_sigprocmask
  5374. case TARGET_NR_sigprocmask:
  5375. {
  5376. #if defined(TARGET_ALPHA)
  5377. sigset_t set, oldset;
  5378. abi_ulong mask;
  5379. int how;
  5380. switch (arg1) {
  5381. case TARGET_SIG_BLOCK:
  5382. how = SIG_BLOCK;
  5383. break;
  5384. case TARGET_SIG_UNBLOCK:
  5385. how = SIG_UNBLOCK;
  5386. break;
  5387. case TARGET_SIG_SETMASK:
  5388. how = SIG_SETMASK;
  5389. break;
  5390. default:
  5391. ret = -TARGET_EINVAL;
  5392. goto fail;
  5393. }
  5394. mask = arg2;
  5395. target_to_host_old_sigset(&set, &mask);
  5396. ret = get_errno(sigprocmask(how, &set, &oldset));
  5397. if (!is_error(ret)) {
  5398. host_to_target_old_sigset(&mask, &oldset);
  5399. ret = mask;
  5400. ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0; /* force no error */
  5401. }
  5402. #else
  5403. sigset_t set, oldset, *set_ptr;
  5404. int how;
  5405. if (arg2) {
  5406. switch (arg1) {
  5407. case TARGET_SIG_BLOCK:
  5408. how = SIG_BLOCK;
  5409. break;
  5410. case TARGET_SIG_UNBLOCK:
  5411. how = SIG_UNBLOCK;
  5412. break;
  5413. case TARGET_SIG_SETMASK:
  5414. how = SIG_SETMASK;
  5415. break;
  5416. default:
  5417. ret = -TARGET_EINVAL;
  5418. goto fail;
  5419. }
  5420. if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
  5421. goto efault;
  5422. target_to_host_old_sigset(&set, p);
  5423. unlock_user(p, arg2, 0);
  5424. set_ptr = &set;
  5425. } else {
  5426. how = 0;
  5427. set_ptr = NULL;
  5428. }
  5429. ret = get_errno(sigprocmask(how, set_ptr, &oldset));
  5430. if (!is_error(ret) && arg3) {
  5431. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
  5432. goto efault;
  5433. host_to_target_old_sigset(p, &oldset);
  5434. unlock_user(p, arg3, sizeof(target_sigset_t));
  5435. }
  5436. #endif
  5437. }
  5438. break;
  5439. #endif
  5440. case TARGET_NR_rt_sigprocmask:
  5441. {
  5442. int how = arg1;
  5443. sigset_t set, oldset, *set_ptr;
  5444. if (arg2) {
  5445. switch(how) {
  5446. case TARGET_SIG_BLOCK:
  5447. how = SIG_BLOCK;
  5448. break;
  5449. case TARGET_SIG_UNBLOCK:
  5450. how = SIG_UNBLOCK;
  5451. break;
  5452. case TARGET_SIG_SETMASK:
  5453. how = SIG_SETMASK;
  5454. break;
  5455. default:
  5456. ret = -TARGET_EINVAL;
  5457. goto fail;
  5458. }
  5459. if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
  5460. goto efault;
  5461. target_to_host_sigset(&set, p);
  5462. unlock_user(p, arg2, 0);
  5463. set_ptr = &set;
  5464. } else {
  5465. how = 0;
  5466. set_ptr = NULL;
  5467. }
  5468. ret = get_errno(sigprocmask(how, set_ptr, &oldset));
  5469. if (!is_error(ret) && arg3) {
  5470. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
  5471. goto efault;
  5472. host_to_target_sigset(p, &oldset);
  5473. unlock_user(p, arg3, sizeof(target_sigset_t));
  5474. }
  5475. }
  5476. break;
  5477. #ifdef TARGET_NR_sigpending
  5478. case TARGET_NR_sigpending:
  5479. {
  5480. sigset_t set;
  5481. ret = get_errno(sigpending(&set));
  5482. if (!is_error(ret)) {
  5483. if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
  5484. goto efault;
  5485. host_to_target_old_sigset(p, &set);
  5486. unlock_user(p, arg1, sizeof(target_sigset_t));
  5487. }
  5488. }
  5489. break;
  5490. #endif
  5491. case TARGET_NR_rt_sigpending:
  5492. {
  5493. sigset_t set;
  5494. ret = get_errno(sigpending(&set));
  5495. if (!is_error(ret)) {
  5496. if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
  5497. goto efault;
  5498. host_to_target_sigset(p, &set);
  5499. unlock_user(p, arg1, sizeof(target_sigset_t));
  5500. }
  5501. }
  5502. break;
  5503. #ifdef TARGET_NR_sigsuspend
  5504. case TARGET_NR_sigsuspend:
  5505. {
  5506. sigset_t set;
  5507. #if defined(TARGET_ALPHA)
  5508. abi_ulong mask = arg1;
  5509. target_to_host_old_sigset(&set, &mask);
  5510. #else
  5511. if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
  5512. goto efault;
  5513. target_to_host_old_sigset(&set, p);
  5514. unlock_user(p, arg1, 0);
  5515. #endif
  5516. ret = get_errno(sigsuspend(&set));
  5517. }
  5518. break;
  5519. #endif
  5520. case TARGET_NR_rt_sigsuspend:
  5521. {
  5522. sigset_t set;
  5523. if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
  5524. goto efault;
  5525. target_to_host_sigset(&set, p);
  5526. unlock_user(p, arg1, 0);
  5527. ret = get_errno(sigsuspend(&set));
  5528. }
  5529. break;
  5530. case TARGET_NR_rt_sigtimedwait:
  5531. {
  5532. sigset_t set;
  5533. struct timespec uts, *puts;
  5534. siginfo_t uinfo;
  5535. if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
  5536. goto efault;
  5537. target_to_host_sigset(&set, p);
  5538. unlock_user(p, arg1, 0);
  5539. if (arg3) {
  5540. puts = &uts;
  5541. target_to_host_timespec(puts, arg3);
  5542. } else {
  5543. puts = NULL;
  5544. }
  5545. ret = get_errno(sigtimedwait(&set, &uinfo, puts));
  5546. if (!is_error(ret) && arg2) {
  5547. if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), 0)))
  5548. goto efault;
  5549. host_to_target_siginfo(p, &uinfo);
  5550. unlock_user(p, arg2, sizeof(target_siginfo_t));
  5551. }
  5552. }
  5553. break;
  5554. case TARGET_NR_rt_sigqueueinfo:
  5555. {
  5556. siginfo_t uinfo;
  5557. if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
  5558. goto efault;
  5559. target_to_host_siginfo(&uinfo, p);
  5560. unlock_user(p, arg1, 0);
  5561. ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
  5562. }
  5563. break;
  5564. #ifdef TARGET_NR_sigreturn
  5565. case TARGET_NR_sigreturn:
  5566. /* NOTE: ret is eax, so not transcoding must be done */
  5567. ret = do_sigreturn(cpu_env);
  5568. break;
  5569. #endif
  5570. case TARGET_NR_rt_sigreturn:
  5571. /* NOTE: ret is eax, so not transcoding must be done */
  5572. ret = do_rt_sigreturn(cpu_env);
  5573. break;
  5574. case TARGET_NR_sethostname:
  5575. if (!(p = lock_user_string(arg1)))
  5576. goto efault;
  5577. ret = get_errno(sethostname(p, arg2));
  5578. unlock_user(p, arg1, 0);
  5579. break;
  5580. case TARGET_NR_setrlimit:
  5581. {
  5582. int resource = target_to_host_resource(arg1);
  5583. struct target_rlimit *target_rlim;
  5584. struct rlimit rlim;
  5585. if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
  5586. goto efault;
  5587. rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
  5588. rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
  5589. unlock_user_struct(target_rlim, arg2, 0);
  5590. ret = get_errno(setrlimit(resource, &rlim));
  5591. }
  5592. break;
  5593. case TARGET_NR_getrlimit:
  5594. {
  5595. int resource = target_to_host_resource(arg1);
  5596. struct target_rlimit *target_rlim;
  5597. struct rlimit rlim;
  5598. ret = get_errno(getrlimit(resource, &rlim));
  5599. if (!is_error(ret)) {
  5600. if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
  5601. goto efault;
  5602. target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
  5603. target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
  5604. unlock_user_struct(target_rlim, arg2, 1);
  5605. }
  5606. }
  5607. break;
  5608. case TARGET_NR_getrusage:
  5609. {
  5610. struct rusage rusage;
  5611. ret = get_errno(getrusage(arg1, &rusage));
  5612. if (!is_error(ret)) {
  5613. host_to_target_rusage(arg2, &rusage);
  5614. }
  5615. }
  5616. break;
  5617. case TARGET_NR_gettimeofday:
  5618. {
  5619. struct timeval tv;
  5620. ret = get_errno(gettimeofday(&tv, NULL));
  5621. if (!is_error(ret)) {
  5622. if (copy_to_user_timeval(arg1, &tv))
  5623. goto efault;
  5624. }
  5625. }
  5626. break;
  5627. case TARGET_NR_settimeofday:
  5628. {
  5629. struct timeval tv;
  5630. if (copy_from_user_timeval(&tv, arg1))
  5631. goto efault;
  5632. ret = get_errno(settimeofday(&tv, NULL));
  5633. }
  5634. break;
  5635. #if defined(TARGET_NR_select) && !defined(TARGET_S390X) && !defined(TARGET_S390)
  5636. case TARGET_NR_select:
  5637. {
  5638. struct target_sel_arg_struct *sel;
  5639. abi_ulong inp, outp, exp, tvp;
  5640. long nsel;
  5641. if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
  5642. goto efault;
  5643. nsel = tswapal(sel->n);
  5644. inp = tswapal(sel->inp);
  5645. outp = tswapal(sel->outp);
  5646. exp = tswapal(sel->exp);
  5647. tvp = tswapal(sel->tvp);
  5648. unlock_user_struct(sel, arg1, 0);
  5649. ret = do_select(nsel, inp, outp, exp, tvp);
  5650. }
  5651. break;
  5652. #endif
  5653. #ifdef TARGET_NR_pselect6
  5654. case TARGET_NR_pselect6:
  5655. {
  5656. abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
  5657. fd_set rfds, wfds, efds;
  5658. fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
  5659. struct timespec ts, *ts_ptr;
  5660. /*
  5661. * The 6th arg is actually two args smashed together,
  5662. * so we cannot use the C library.
  5663. */
  5664. sigset_t set;
  5665. struct {
  5666. sigset_t *set;
  5667. size_t size;
  5668. } sig, *sig_ptr;
  5669. abi_ulong arg_sigset, arg_sigsize, *arg7;
  5670. target_sigset_t *target_sigset;
  5671. n = arg1;
  5672. rfd_addr = arg2;
  5673. wfd_addr = arg3;
  5674. efd_addr = arg4;
  5675. ts_addr = arg5;
  5676. ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
  5677. if (ret) {
  5678. goto fail;
  5679. }
  5680. ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
  5681. if (ret) {
  5682. goto fail;
  5683. }
  5684. ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
  5685. if (ret) {
  5686. goto fail;
  5687. }
  5688. /*
  5689. * This takes a timespec, and not a timeval, so we cannot
  5690. * use the do_select() helper ...
  5691. */
  5692. if (ts_addr) {
  5693. if (target_to_host_timespec(&ts, ts_addr)) {
  5694. goto efault;
  5695. }
  5696. ts_ptr = &ts;
  5697. } else {
  5698. ts_ptr = NULL;
  5699. }
  5700. /* Extract the two packed args for the sigset */
  5701. if (arg6) {
  5702. sig_ptr = &sig;
  5703. sig.size = _NSIG / 8;
  5704. arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
  5705. if (!arg7) {
  5706. goto efault;
  5707. }
  5708. arg_sigset = tswapal(arg7[0]);
  5709. arg_sigsize = tswapal(arg7[1]);
  5710. unlock_user(arg7, arg6, 0);
  5711. if (arg_sigset) {
  5712. sig.set = &set;
  5713. if (arg_sigsize != sizeof(*target_sigset)) {
  5714. /* Like the kernel, we enforce correct size sigsets */
  5715. ret = -TARGET_EINVAL;
  5716. goto fail;
  5717. }
  5718. target_sigset = lock_user(VERIFY_READ, arg_sigset,
  5719. sizeof(*target_sigset), 1);
  5720. if (!target_sigset) {
  5721. goto efault;
  5722. }
  5723. target_to_host_sigset(&set, target_sigset);
  5724. unlock_user(target_sigset, arg_sigset, 0);
  5725. } else {
  5726. sig.set = NULL;
  5727. }
  5728. } else {
  5729. sig_ptr = NULL;
  5730. }
  5731. ret = get_errno(sys_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
  5732. ts_ptr, sig_ptr));
  5733. if (!is_error(ret)) {
  5734. if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
  5735. goto efault;
  5736. if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
  5737. goto efault;
  5738. if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
  5739. goto efault;
  5740. if (ts_addr && host_to_target_timespec(ts_addr, &ts))
  5741. goto efault;
  5742. }
  5743. }
  5744. break;
  5745. #endif
  5746. case TARGET_NR_symlink:
  5747. {
  5748. void *p2;
  5749. p = lock_user_string(arg1);
  5750. p2 = lock_user_string(arg2);
  5751. if (!p || !p2)
  5752. ret = -TARGET_EFAULT;
  5753. else
  5754. ret = get_errno(symlink(p, p2));
  5755. unlock_user(p2, arg2, 0);
  5756. unlock_user(p, arg1, 0);
  5757. }
  5758. break;
  5759. #if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
  5760. case TARGET_NR_symlinkat:
  5761. {
  5762. void *p2;
  5763. p = lock_user_string(arg1);
  5764. p2 = lock_user_string(arg3);
  5765. if (!p || !p2)
  5766. ret = -TARGET_EFAULT;
  5767. else
  5768. ret = get_errno(sys_symlinkat(p, arg2, p2));
  5769. unlock_user(p2, arg3, 0);
  5770. unlock_user(p, arg1, 0);
  5771. }
  5772. break;
  5773. #endif
  5774. #ifdef TARGET_NR_oldlstat
  5775. case TARGET_NR_oldlstat:
  5776. goto unimplemented;
  5777. #endif
  5778. case TARGET_NR_readlink:
  5779. {
  5780. void *p2, *temp;
  5781. p = lock_user_string(arg1);
  5782. p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  5783. if (!p || !p2)
  5784. ret = -TARGET_EFAULT;
  5785. else {
  5786. if (strncmp((const char *)p, "/proc/self/exe", 14) == 0) {
  5787. char real[PATH_MAX];
  5788. temp = realpath(exec_path,real);
  5789. ret = (temp==NULL) ? get_errno(-1) : strlen(real) ;
  5790. snprintf((char *)p2, arg3, "%s", real);
  5791. }
  5792. else
  5793. ret = get_errno(readlink(path(p), p2, arg3));
  5794. }
  5795. unlock_user(p2, arg2, ret);
  5796. unlock_user(p, arg1, 0);
  5797. }
  5798. break;
  5799. #if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
  5800. case TARGET_NR_readlinkat:
  5801. {
  5802. void *p2;
  5803. p = lock_user_string(arg2);
  5804. p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  5805. if (!p || !p2)
  5806. ret = -TARGET_EFAULT;
  5807. else
  5808. ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
  5809. unlock_user(p2, arg3, ret);
  5810. unlock_user(p, arg2, 0);
  5811. }
  5812. break;
  5813. #endif
  5814. #ifdef TARGET_NR_uselib
  5815. case TARGET_NR_uselib:
  5816. goto unimplemented;
  5817. #endif
  5818. #ifdef TARGET_NR_swapon
  5819. case TARGET_NR_swapon:
  5820. if (!(p = lock_user_string(arg1)))
  5821. goto efault;
  5822. ret = get_errno(swapon(p, arg2));
  5823. unlock_user(p, arg1, 0);
  5824. break;
  5825. #endif
  5826. case TARGET_NR_reboot:
  5827. if (!(p = lock_user_string(arg4)))
  5828. goto efault;
  5829. ret = reboot(arg1, arg2, arg3, p);
  5830. unlock_user(p, arg4, 0);
  5831. break;
  5832. #ifdef TARGET_NR_readdir
  5833. case TARGET_NR_readdir:
  5834. goto unimplemented;
  5835. #endif
  5836. #ifdef TARGET_NR_mmap
  5837. case TARGET_NR_mmap:
  5838. #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || defined(TARGET_ARM) || \
  5839. defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
  5840. || defined(TARGET_S390X)
  5841. {
  5842. abi_ulong *v;
  5843. abi_ulong v1, v2, v3, v4, v5, v6;
  5844. if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
  5845. goto efault;
  5846. v1 = tswapal(v[0]);
  5847. v2 = tswapal(v[1]);
  5848. v3 = tswapal(v[2]);
  5849. v4 = tswapal(v[3]);
  5850. v5 = tswapal(v[4]);
  5851. v6 = tswapal(v[5]);
  5852. unlock_user(v, arg1, 0);
  5853. ret = get_errno(target_mmap(v1, v2, v3,
  5854. target_to_host_bitmask(v4, mmap_flags_tbl),
  5855. v5, v6));
  5856. }
  5857. #else
  5858. ret = get_errno(target_mmap(arg1, arg2, arg3,
  5859. target_to_host_bitmask(arg4, mmap_flags_tbl),
  5860. arg5,
  5861. arg6));
  5862. #endif
  5863. break;
  5864. #endif
  5865. #ifdef TARGET_NR_mmap2
  5866. case TARGET_NR_mmap2:
  5867. #ifndef MMAP_SHIFT
  5868. #define MMAP_SHIFT 12
  5869. #endif
  5870. ret = get_errno(target_mmap(arg1, arg2, arg3,
  5871. target_to_host_bitmask(arg4, mmap_flags_tbl),
  5872. arg5,
  5873. arg6 << MMAP_SHIFT));
  5874. break;
  5875. #endif
  5876. case TARGET_NR_munmap:
  5877. ret = get_errno(target_munmap(arg1, arg2));
  5878. break;
  5879. case TARGET_NR_mprotect:
  5880. {
  5881. TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
  5882. /* Special hack to detect libc making the stack executable. */
  5883. if ((arg3 & PROT_GROWSDOWN)
  5884. && arg1 >= ts->info->stack_limit
  5885. && arg1 <= ts->info->start_stack) {
  5886. arg3 &= ~PROT_GROWSDOWN;
  5887. arg2 = arg2 + arg1 - ts->info->stack_limit;
  5888. arg1 = ts->info->stack_limit;
  5889. }
  5890. }
  5891. ret = get_errno(target_mprotect(arg1, arg2, arg3));
  5892. break;
  5893. #ifdef TARGET_NR_mremap
  5894. case TARGET_NR_mremap:
  5895. ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
  5896. break;
  5897. #endif
  5898. /* ??? msync/mlock/munlock are broken for softmmu. */
  5899. #ifdef TARGET_NR_msync
  5900. case TARGET_NR_msync:
  5901. ret = get_errno(msync(g2h(arg1), arg2, arg3));
  5902. break;
  5903. #endif
  5904. #ifdef TARGET_NR_mlock
  5905. case TARGET_NR_mlock:
  5906. ret = get_errno(mlock(g2h(arg1), arg2));
  5907. break;
  5908. #endif
  5909. #ifdef TARGET_NR_munlock
  5910. case TARGET_NR_munlock:
  5911. ret = get_errno(munlock(g2h(arg1), arg2));
  5912. break;
  5913. #endif
  5914. #ifdef TARGET_NR_mlockall
  5915. case TARGET_NR_mlockall:
  5916. ret = get_errno(mlockall(arg1));
  5917. break;
  5918. #endif
  5919. #ifdef TARGET_NR_munlockall
  5920. case TARGET_NR_munlockall:
  5921. ret = get_errno(munlockall());
  5922. break;
  5923. #endif
  5924. case TARGET_NR_truncate:
  5925. if (!(p = lock_user_string(arg1)))
  5926. goto efault;
  5927. ret = get_errno(truncate(p, arg2));
  5928. unlock_user(p, arg1, 0);
  5929. break;
  5930. case TARGET_NR_ftruncate:
  5931. ret = get_errno(ftruncate(arg1, arg2));
  5932. break;
  5933. case TARGET_NR_fchmod:
  5934. ret = get_errno(fchmod(arg1, arg2));
  5935. break;
  5936. #if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
  5937. case TARGET_NR_fchmodat:
  5938. if (!(p = lock_user_string(arg2)))
  5939. goto efault;
  5940. ret = get_errno(sys_fchmodat(arg1, p, arg3));
  5941. unlock_user(p, arg2, 0);
  5942. break;
  5943. #endif
  5944. case TARGET_NR_getpriority:
  5945. /* Note that negative values are valid for getpriority, so we must
  5946. differentiate based on errno settings. */
  5947. errno = 0;
  5948. ret = getpriority(arg1, arg2);
  5949. if (ret == -1 && errno != 0) {
  5950. ret = -host_to_target_errno(errno);
  5951. break;
  5952. }
  5953. #ifdef TARGET_ALPHA
  5954. /* Return value is the unbiased priority. Signal no error. */
  5955. ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0;
  5956. #else
  5957. /* Return value is a biased priority to avoid negative numbers. */
  5958. ret = 20 - ret;
  5959. #endif
  5960. break;
  5961. case TARGET_NR_setpriority:
  5962. ret = get_errno(setpriority(arg1, arg2, arg3));
  5963. break;
  5964. #ifdef TARGET_NR_profil
  5965. case TARGET_NR_profil:
  5966. goto unimplemented;
  5967. #endif
  5968. case TARGET_NR_statfs:
  5969. if (!(p = lock_user_string(arg1)))
  5970. goto efault;
  5971. ret = get_errno(statfs(path(p), &stfs));
  5972. unlock_user(p, arg1, 0);
  5973. convert_statfs:
  5974. if (!is_error(ret)) {
  5975. struct target_statfs *target_stfs;
  5976. if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
  5977. goto efault;
  5978. __put_user(stfs.f_type, &target_stfs->f_type);
  5979. __put_user(stfs.f_bsize, &target_stfs->f_bsize);
  5980. __put_user(stfs.f_blocks, &target_stfs->f_blocks);
  5981. __put_user(stfs.f_bfree, &target_stfs->f_bfree);
  5982. __put_user(stfs.f_bavail, &target_stfs->f_bavail);
  5983. __put_user(stfs.f_files, &target_stfs->f_files);
  5984. __put_user(stfs.f_ffree, &target_stfs->f_ffree);
  5985. __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
  5986. __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
  5987. __put_user(stfs.f_namelen, &target_stfs->f_namelen);
  5988. unlock_user_struct(target_stfs, arg2, 1);
  5989. }
  5990. break;
  5991. case TARGET_NR_fstatfs:
  5992. ret = get_errno(fstatfs(arg1, &stfs));
  5993. goto convert_statfs;
  5994. #ifdef TARGET_NR_statfs64
  5995. case TARGET_NR_statfs64:
  5996. if (!(p = lock_user_string(arg1)))
  5997. goto efault;
  5998. ret = get_errno(statfs(path(p), &stfs));
  5999. unlock_user(p, arg1, 0);
  6000. convert_statfs64:
  6001. if (!is_error(ret)) {
  6002. struct target_statfs64 *target_stfs;
  6003. if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
  6004. goto efault;
  6005. __put_user(stfs.f_type, &target_stfs->f_type);
  6006. __put_user(stfs.f_bsize, &target_stfs->f_bsize);
  6007. __put_user(stfs.f_blocks, &target_stfs->f_blocks);
  6008. __put_user(stfs.f_bfree, &target_stfs->f_bfree);
  6009. __put_user(stfs.f_bavail, &target_stfs->f_bavail);
  6010. __put_user(stfs.f_files, &target_stfs->f_files);
  6011. __put_user(stfs.f_ffree, &target_stfs->f_ffree);
  6012. __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
  6013. __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
  6014. __put_user(stfs.f_namelen, &target_stfs->f_namelen);
  6015. unlock_user_struct(target_stfs, arg3, 1);
  6016. }
  6017. break;
  6018. case TARGET_NR_fstatfs64:
  6019. ret = get_errno(fstatfs(arg1, &stfs));
  6020. goto convert_statfs64;
  6021. #endif
  6022. #ifdef TARGET_NR_ioperm
  6023. case TARGET_NR_ioperm:
  6024. goto unimplemented;
  6025. #endif
  6026. #ifdef TARGET_NR_socketcall
  6027. case TARGET_NR_socketcall:
  6028. ret = do_socketcall(arg1, arg2);
  6029. break;
  6030. #endif
  6031. #ifdef TARGET_NR_accept
  6032. case TARGET_NR_accept:
  6033. ret = do_accept(arg1, arg2, arg3);
  6034. break;
  6035. #endif
  6036. #ifdef TARGET_NR_bind
  6037. case TARGET_NR_bind:
  6038. ret = do_bind(arg1, arg2, arg3);
  6039. break;
  6040. #endif
  6041. #ifdef TARGET_NR_connect
  6042. case TARGET_NR_connect:
  6043. ret = do_connect(arg1, arg2, arg3);
  6044. break;
  6045. #endif
  6046. #ifdef TARGET_NR_getpeername
  6047. case TARGET_NR_getpeername:
  6048. ret = do_getpeername(arg1, arg2, arg3);
  6049. break;
  6050. #endif
  6051. #ifdef TARGET_NR_getsockname
  6052. case TARGET_NR_getsockname:
  6053. ret = do_getsockname(arg1, arg2, arg3);
  6054. break;
  6055. #endif
  6056. #ifdef TARGET_NR_getsockopt
  6057. case TARGET_NR_getsockopt:
  6058. ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
  6059. break;
  6060. #endif
  6061. #ifdef TARGET_NR_listen
  6062. case TARGET_NR_listen:
  6063. ret = get_errno(listen(arg1, arg2));
  6064. break;
  6065. #endif
  6066. #ifdef TARGET_NR_recv
  6067. case TARGET_NR_recv:
  6068. ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
  6069. break;
  6070. #endif
  6071. #ifdef TARGET_NR_recvfrom
  6072. case TARGET_NR_recvfrom:
  6073. ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
  6074. break;
  6075. #endif
  6076. #ifdef TARGET_NR_recvmsg
  6077. case TARGET_NR_recvmsg:
  6078. ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
  6079. break;
  6080. #endif
  6081. #ifdef TARGET_NR_send
  6082. case TARGET_NR_send:
  6083. ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
  6084. break;
  6085. #endif
  6086. #ifdef TARGET_NR_sendmsg
  6087. case TARGET_NR_sendmsg:
  6088. ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
  6089. break;
  6090. #endif
  6091. #ifdef TARGET_NR_sendto
  6092. case TARGET_NR_sendto:
  6093. ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
  6094. break;
  6095. #endif
  6096. #ifdef TARGET_NR_shutdown
  6097. case TARGET_NR_shutdown:
  6098. ret = get_errno(shutdown(arg1, arg2));
  6099. break;
  6100. #endif
  6101. #ifdef TARGET_NR_socket
  6102. case TARGET_NR_socket:
  6103. ret = do_socket(arg1, arg2, arg3);
  6104. break;
  6105. #endif
  6106. #ifdef TARGET_NR_socketpair
  6107. case TARGET_NR_socketpair:
  6108. ret = do_socketpair(arg1, arg2, arg3, arg4);
  6109. break;
  6110. #endif
  6111. #ifdef TARGET_NR_setsockopt
  6112. case TARGET_NR_setsockopt:
  6113. ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
  6114. break;
  6115. #endif
  6116. case TARGET_NR_syslog:
  6117. if (!(p = lock_user_string(arg2)))
  6118. goto efault;
  6119. ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
  6120. unlock_user(p, arg2, 0);
  6121. break;
  6122. case TARGET_NR_setitimer:
  6123. {
  6124. struct itimerval value, ovalue, *pvalue;
  6125. if (arg2) {
  6126. pvalue = &value;
  6127. if (copy_from_user_timeval(&pvalue->it_interval, arg2)
  6128. || copy_from_user_timeval(&pvalue->it_value,
  6129. arg2 + sizeof(struct target_timeval)))
  6130. goto efault;
  6131. } else {
  6132. pvalue = NULL;
  6133. }
  6134. ret = get_errno(setitimer(arg1, pvalue, &ovalue));
  6135. if (!is_error(ret) && arg3) {
  6136. if (copy_to_user_timeval(arg3,
  6137. &ovalue.it_interval)
  6138. || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
  6139. &ovalue.it_value))
  6140. goto efault;
  6141. }
  6142. }
  6143. break;
  6144. case TARGET_NR_getitimer:
  6145. {
  6146. struct itimerval value;
  6147. ret = get_errno(getitimer(arg1, &value));
  6148. if (!is_error(ret) && arg2) {
  6149. if (copy_to_user_timeval(arg2,
  6150. &value.it_interval)
  6151. || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
  6152. &value.it_value))
  6153. goto efault;
  6154. }
  6155. }
  6156. break;
  6157. case TARGET_NR_stat:
  6158. if (!(p = lock_user_string(arg1)))
  6159. goto efault;
  6160. ret = get_errno(stat(path(p), &st));
  6161. unlock_user(p, arg1, 0);
  6162. goto do_stat;
  6163. case TARGET_NR_lstat:
  6164. if (!(p = lock_user_string(arg1)))
  6165. goto efault;
  6166. ret = get_errno(lstat(path(p), &st));
  6167. unlock_user(p, arg1, 0);
  6168. goto do_stat;
  6169. case TARGET_NR_fstat:
  6170. {
  6171. ret = get_errno(fstat(arg1, &st));
  6172. do_stat:
  6173. if (!is_error(ret)) {
  6174. struct target_stat *target_st;
  6175. if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
  6176. goto efault;
  6177. memset(target_st, 0, sizeof(*target_st));
  6178. __put_user(st.st_dev, &target_st->st_dev);
  6179. __put_user(st.st_ino, &target_st->st_ino);
  6180. __put_user(st.st_mode, &target_st->st_mode);
  6181. __put_user(st.st_uid, &target_st->st_uid);
  6182. __put_user(st.st_gid, &target_st->st_gid);
  6183. __put_user(st.st_nlink, &target_st->st_nlink);
  6184. __put_user(st.st_rdev, &target_st->st_rdev);
  6185. __put_user(st.st_size, &target_st->st_size);
  6186. __put_user(st.st_blksize, &target_st->st_blksize);
  6187. __put_user(st.st_blocks, &target_st->st_blocks);
  6188. __put_user(st.st_atime, &target_st->target_st_atime);
  6189. __put_user(st.st_mtime, &target_st->target_st_mtime);
  6190. __put_user(st.st_ctime, &target_st->target_st_ctime);
  6191. unlock_user_struct(target_st, arg2, 1);
  6192. }
  6193. }
  6194. break;
  6195. #ifdef TARGET_NR_olduname
  6196. case TARGET_NR_olduname:
  6197. goto unimplemented;
  6198. #endif
  6199. #ifdef TARGET_NR_iopl
  6200. case TARGET_NR_iopl:
  6201. goto unimplemented;
  6202. #endif
  6203. case TARGET_NR_vhangup:
  6204. ret = get_errno(vhangup());
  6205. break;
  6206. #ifdef TARGET_NR_idle
  6207. case TARGET_NR_idle:
  6208. goto unimplemented;
  6209. #endif
  6210. #ifdef TARGET_NR_syscall
  6211. case TARGET_NR_syscall:
  6212. ret = do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
  6213. arg6, arg7, arg8, 0);
  6214. break;
  6215. #endif
  6216. case TARGET_NR_wait4:
  6217. {
  6218. int status;
  6219. abi_long status_ptr = arg2;
  6220. struct rusage rusage, *rusage_ptr;
  6221. abi_ulong target_rusage = arg4;
  6222. if (target_rusage)
  6223. rusage_ptr = &rusage;
  6224. else
  6225. rusage_ptr = NULL;
  6226. ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
  6227. if (!is_error(ret)) {
  6228. if (status_ptr && ret) {
  6229. status = host_to_target_waitstatus(status);
  6230. if (put_user_s32(status, status_ptr))
  6231. goto efault;
  6232. }
  6233. if (target_rusage)
  6234. host_to_target_rusage(target_rusage, &rusage);
  6235. }
  6236. }
  6237. break;
  6238. #ifdef TARGET_NR_swapoff
  6239. case TARGET_NR_swapoff:
  6240. if (!(p = lock_user_string(arg1)))
  6241. goto efault;
  6242. ret = get_errno(swapoff(p));
  6243. unlock_user(p, arg1, 0);
  6244. break;
  6245. #endif
  6246. case TARGET_NR_sysinfo:
  6247. {
  6248. struct target_sysinfo *target_value;
  6249. struct sysinfo value;
  6250. ret = get_errno(sysinfo(&value));
  6251. if (!is_error(ret) && arg1)
  6252. {
  6253. if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
  6254. goto efault;
  6255. __put_user(value.uptime, &target_value->uptime);
  6256. __put_user(value.loads[0], &target_value->loads[0]);
  6257. __put_user(value.loads[1], &target_value->loads[1]);
  6258. __put_user(value.loads[2], &target_value->loads[2]);
  6259. __put_user(value.totalram, &target_value->totalram);
  6260. __put_user(value.freeram, &target_value->freeram);
  6261. __put_user(value.sharedram, &target_value->sharedram);
  6262. __put_user(value.bufferram, &target_value->bufferram);
  6263. __put_user(value.totalswap, &target_value->totalswap);
  6264. __put_user(value.freeswap, &target_value->freeswap);
  6265. __put_user(value.procs, &target_value->procs);
  6266. __put_user(value.totalhigh, &target_value->totalhigh);
  6267. __put_user(value.freehigh, &target_value->freehigh);
  6268. __put_user(value.mem_unit, &target_value->mem_unit);
  6269. unlock_user_struct(target_value, arg1, 1);
  6270. }
  6271. }
  6272. break;
  6273. #ifdef TARGET_NR_ipc
  6274. case TARGET_NR_ipc:
  6275. ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
  6276. break;
  6277. #endif
  6278. #ifdef TARGET_NR_semget
  6279. case TARGET_NR_semget:
  6280. ret = get_errno(semget(arg1, arg2, arg3));
  6281. break;
  6282. #endif
  6283. #ifdef TARGET_NR_semop
  6284. case TARGET_NR_semop:
  6285. ret = get_errno(do_semop(arg1, arg2, arg3));
  6286. break;
  6287. #endif
  6288. #ifdef TARGET_NR_semctl
  6289. case TARGET_NR_semctl:
  6290. ret = do_semctl(arg1, arg2, arg3, (union target_semun)(abi_ulong)arg4);
  6291. break;
  6292. #endif
  6293. #ifdef TARGET_NR_msgctl
  6294. case TARGET_NR_msgctl:
  6295. ret = do_msgctl(arg1, arg2, arg3);
  6296. break;
  6297. #endif
  6298. #ifdef TARGET_NR_msgget
  6299. case TARGET_NR_msgget:
  6300. ret = get_errno(msgget(arg1, arg2));
  6301. break;
  6302. #endif
  6303. #ifdef TARGET_NR_msgrcv
  6304. case TARGET_NR_msgrcv:
  6305. ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
  6306. break;
  6307. #endif
  6308. #ifdef TARGET_NR_msgsnd
  6309. case TARGET_NR_msgsnd:
  6310. ret = do_msgsnd(arg1, arg2, arg3, arg4);
  6311. break;
  6312. #endif
  6313. #ifdef TARGET_NR_shmget
  6314. case TARGET_NR_shmget:
  6315. ret = get_errno(shmget(arg1, arg2, arg3));
  6316. break;
  6317. #endif
  6318. #ifdef TARGET_NR_shmctl
  6319. case TARGET_NR_shmctl:
  6320. ret = do_shmctl(arg1, arg2, arg3);
  6321. break;
  6322. #endif
  6323. #ifdef TARGET_NR_shmat
  6324. case TARGET_NR_shmat:
  6325. ret = do_shmat(arg1, arg2, arg3);
  6326. break;
  6327. #endif
  6328. #ifdef TARGET_NR_shmdt
  6329. case TARGET_NR_shmdt:
  6330. ret = do_shmdt(arg1);
  6331. break;
  6332. #endif
  6333. case TARGET_NR_fsync:
  6334. ret = get_errno(fsync(arg1));
  6335. break;
  6336. case TARGET_NR_clone:
  6337. #if defined(TARGET_SH4) || defined(TARGET_ALPHA)
  6338. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
  6339. #elif defined(TARGET_CRIS)
  6340. ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg4, arg5));
  6341. #elif defined(TARGET_S390X)
  6342. ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
  6343. #else
  6344. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
  6345. #endif
  6346. break;
  6347. #ifdef __NR_exit_group
  6348. /* new thread calls */
  6349. case TARGET_NR_exit_group:
  6350. #ifdef TARGET_GPROF
  6351. _mcleanup();
  6352. #endif
  6353. gdb_exit(cpu_env, arg1);
  6354. ret = get_errno(exit_group(arg1));
  6355. break;
  6356. #endif
  6357. case TARGET_NR_setdomainname:
  6358. if (!(p = lock_user_string(arg1)))
  6359. goto efault;
  6360. ret = get_errno(setdomainname(p, arg2));
  6361. unlock_user(p, arg1, 0);
  6362. break;
  6363. case TARGET_NR_uname:
  6364. /* no need to transcode because we use the linux syscall */
  6365. {
  6366. struct new_utsname * buf;
  6367. if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
  6368. goto efault;
  6369. ret = get_errno(sys_uname(buf));
  6370. if (!is_error(ret)) {
  6371. /* Overrite the native machine name with whatever is being
  6372. emulated. */
  6373. strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
  6374. /* Allow the user to override the reported release. */
  6375. if (qemu_uname_release && *qemu_uname_release)
  6376. strcpy (buf->release, qemu_uname_release);
  6377. }
  6378. unlock_user_struct(buf, arg1, 1);
  6379. }
  6380. break;
  6381. #ifdef TARGET_I386
  6382. case TARGET_NR_modify_ldt:
  6383. ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
  6384. break;
  6385. #if !defined(TARGET_X86_64)
  6386. case TARGET_NR_vm86old:
  6387. goto unimplemented;
  6388. case TARGET_NR_vm86:
  6389. ret = do_vm86(cpu_env, arg1, arg2);
  6390. break;
  6391. #endif
  6392. #endif
  6393. case TARGET_NR_adjtimex:
  6394. goto unimplemented;
  6395. #ifdef TARGET_NR_create_module
  6396. case TARGET_NR_create_module:
  6397. #endif
  6398. case TARGET_NR_init_module:
  6399. case TARGET_NR_delete_module:
  6400. #ifdef TARGET_NR_get_kernel_syms
  6401. case TARGET_NR_get_kernel_syms:
  6402. #endif
  6403. goto unimplemented;
  6404. case TARGET_NR_quotactl:
  6405. goto unimplemented;
  6406. case TARGET_NR_getpgid:
  6407. ret = get_errno(getpgid(arg1));
  6408. break;
  6409. case TARGET_NR_fchdir:
  6410. ret = get_errno(fchdir(arg1));
  6411. break;
  6412. #ifdef TARGET_NR_bdflush /* not on x86_64 */
  6413. case TARGET_NR_bdflush:
  6414. goto unimplemented;
  6415. #endif
  6416. #ifdef TARGET_NR_sysfs
  6417. case TARGET_NR_sysfs:
  6418. goto unimplemented;
  6419. #endif
  6420. case TARGET_NR_personality:
  6421. ret = get_errno(personality(arg1));
  6422. break;
  6423. #ifdef TARGET_NR_afs_syscall
  6424. case TARGET_NR_afs_syscall:
  6425. goto unimplemented;
  6426. #endif
  6427. #ifdef TARGET_NR__llseek /* Not on alpha */
  6428. case TARGET_NR__llseek:
  6429. {
  6430. int64_t res;
  6431. #if !defined(__NR_llseek)
  6432. res = lseek(arg1, ((uint64_t)arg2 << 32) | arg3, arg5);
  6433. if (res == -1) {
  6434. ret = get_errno(res);
  6435. } else {
  6436. ret = 0;
  6437. }
  6438. #else
  6439. ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
  6440. #endif
  6441. if ((ret == 0) && put_user_s64(res, arg4)) {
  6442. goto efault;
  6443. }
  6444. }
  6445. break;
  6446. #endif
  6447. case TARGET_NR_getdents:
  6448. #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
  6449. {
  6450. struct target_dirent *target_dirp;
  6451. struct linux_dirent *dirp;
  6452. abi_long count = arg3;
  6453. dirp = malloc(count);
  6454. if (!dirp) {
  6455. ret = -TARGET_ENOMEM;
  6456. goto fail;
  6457. }
  6458. ret = get_errno(sys_getdents(arg1, dirp, count));
  6459. if (!is_error(ret)) {
  6460. struct linux_dirent *de;
  6461. struct target_dirent *tde;
  6462. int len = ret;
  6463. int reclen, treclen;
  6464. int count1, tnamelen;
  6465. count1 = 0;
  6466. de = dirp;
  6467. if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
  6468. goto efault;
  6469. tde = target_dirp;
  6470. while (len > 0) {
  6471. reclen = de->d_reclen;
  6472. tnamelen = reclen - offsetof(struct linux_dirent, d_name);
  6473. assert(tnamelen >= 0);
  6474. treclen = tnamelen + offsetof(struct target_dirent, d_name);
  6475. assert(count1 + treclen <= count);
  6476. tde->d_reclen = tswap16(treclen);
  6477. tde->d_ino = tswapal(de->d_ino);
  6478. tde->d_off = tswapal(de->d_off);
  6479. memcpy(tde->d_name, de->d_name, tnamelen);
  6480. de = (struct linux_dirent *)((char *)de + reclen);
  6481. len -= reclen;
  6482. tde = (struct target_dirent *)((char *)tde + treclen);
  6483. count1 += treclen;
  6484. }
  6485. ret = count1;
  6486. unlock_user(target_dirp, arg2, ret);
  6487. }
  6488. free(dirp);
  6489. }
  6490. #else
  6491. {
  6492. struct linux_dirent *dirp;
  6493. abi_long count = arg3;
  6494. if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
  6495. goto efault;
  6496. ret = get_errno(sys_getdents(arg1, dirp, count));
  6497. if (!is_error(ret)) {
  6498. struct linux_dirent *de;
  6499. int len = ret;
  6500. int reclen;
  6501. de = dirp;
  6502. while (len > 0) {
  6503. reclen = de->d_reclen;
  6504. if (reclen > len)
  6505. break;
  6506. de->d_reclen = tswap16(reclen);
  6507. tswapls(&de->d_ino);
  6508. tswapls(&de->d_off);
  6509. de = (struct linux_dirent *)((char *)de + reclen);
  6510. len -= reclen;
  6511. }
  6512. }
  6513. unlock_user(dirp, arg2, ret);
  6514. }
  6515. #endif
  6516. break;
  6517. #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
  6518. case TARGET_NR_getdents64:
  6519. {
  6520. struct linux_dirent64 *dirp;
  6521. abi_long count = arg3;
  6522. if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
  6523. goto efault;
  6524. ret = get_errno(sys_getdents64(arg1, dirp, count));
  6525. if (!is_error(ret)) {
  6526. struct linux_dirent64 *de;
  6527. int len = ret;
  6528. int reclen;
  6529. de = dirp;
  6530. while (len > 0) {
  6531. reclen = de->d_reclen;
  6532. if (reclen > len)
  6533. break;
  6534. de->d_reclen = tswap16(reclen);
  6535. tswap64s((uint64_t *)&de->d_ino);
  6536. tswap64s((uint64_t *)&de->d_off);
  6537. de = (struct linux_dirent64 *)((char *)de + reclen);
  6538. len -= reclen;
  6539. }
  6540. }
  6541. unlock_user(dirp, arg2, ret);
  6542. }
  6543. break;
  6544. #endif /* TARGET_NR_getdents64 */
  6545. #if defined(TARGET_NR__newselect) || defined(TARGET_S390X)
  6546. #ifdef TARGET_S390X
  6547. case TARGET_NR_select:
  6548. #else
  6549. case TARGET_NR__newselect:
  6550. #endif
  6551. ret = do_select(arg1, arg2, arg3, arg4, arg5);
  6552. break;
  6553. #endif
  6554. #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
  6555. # ifdef TARGET_NR_poll
  6556. case TARGET_NR_poll:
  6557. # endif
  6558. # ifdef TARGET_NR_ppoll
  6559. case TARGET_NR_ppoll:
  6560. # endif
  6561. {
  6562. struct target_pollfd *target_pfd;
  6563. unsigned int nfds = arg2;
  6564. int timeout = arg3;
  6565. struct pollfd *pfd;
  6566. unsigned int i;
  6567. target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
  6568. if (!target_pfd)
  6569. goto efault;
  6570. pfd = alloca(sizeof(struct pollfd) * nfds);
  6571. for(i = 0; i < nfds; i++) {
  6572. pfd[i].fd = tswap32(target_pfd[i].fd);
  6573. pfd[i].events = tswap16(target_pfd[i].events);
  6574. }
  6575. # ifdef TARGET_NR_ppoll
  6576. if (num == TARGET_NR_ppoll) {
  6577. struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
  6578. target_sigset_t *target_set;
  6579. sigset_t _set, *set = &_set;
  6580. if (arg3) {
  6581. if (target_to_host_timespec(timeout_ts, arg3)) {
  6582. unlock_user(target_pfd, arg1, 0);
  6583. goto efault;
  6584. }
  6585. } else {
  6586. timeout_ts = NULL;
  6587. }
  6588. if (arg4) {
  6589. target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1);
  6590. if (!target_set) {
  6591. unlock_user(target_pfd, arg1, 0);
  6592. goto efault;
  6593. }
  6594. target_to_host_sigset(set, target_set);
  6595. } else {
  6596. set = NULL;
  6597. }
  6598. ret = get_errno(sys_ppoll(pfd, nfds, timeout_ts, set, _NSIG/8));
  6599. if (!is_error(ret) && arg3) {
  6600. host_to_target_timespec(arg3, timeout_ts);
  6601. }
  6602. if (arg4) {
  6603. unlock_user(target_set, arg4, 0);
  6604. }
  6605. } else
  6606. # endif
  6607. ret = get_errno(poll(pfd, nfds, timeout));
  6608. if (!is_error(ret)) {
  6609. for(i = 0; i < nfds; i++) {
  6610. target_pfd[i].revents = tswap16(pfd[i].revents);
  6611. }
  6612. }
  6613. unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
  6614. }
  6615. break;
  6616. #endif
  6617. case TARGET_NR_flock:
  6618. /* NOTE: the flock constant seems to be the same for every
  6619. Linux platform */
  6620. ret = get_errno(flock(arg1, arg2));
  6621. break;
  6622. case TARGET_NR_readv:
  6623. {
  6624. int count = arg3;
  6625. struct iovec *vec;
  6626. vec = alloca(count * sizeof(struct iovec));
  6627. if (lock_iovec(VERIFY_WRITE, vec, arg2, count, 0) < 0)
  6628. goto efault;
  6629. ret = get_errno(readv(arg1, vec, count));
  6630. unlock_iovec(vec, arg2, count, 1);
  6631. }
  6632. break;
  6633. case TARGET_NR_writev:
  6634. {
  6635. int count = arg3;
  6636. struct iovec *vec;
  6637. vec = alloca(count * sizeof(struct iovec));
  6638. if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
  6639. goto efault;
  6640. ret = get_errno(writev(arg1, vec, count));
  6641. unlock_iovec(vec, arg2, count, 0);
  6642. }
  6643. break;
  6644. case TARGET_NR_getsid:
  6645. ret = get_errno(getsid(arg1));
  6646. break;
  6647. #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
  6648. case TARGET_NR_fdatasync:
  6649. ret = get_errno(fdatasync(arg1));
  6650. break;
  6651. #endif
  6652. case TARGET_NR__sysctl:
  6653. /* We don't implement this, but ENOTDIR is always a safe
  6654. return value. */
  6655. ret = -TARGET_ENOTDIR;
  6656. break;
  6657. case TARGET_NR_sched_getaffinity:
  6658. {
  6659. unsigned int mask_size;
  6660. unsigned long *mask;
  6661. /*
  6662. * sched_getaffinity needs multiples of ulong, so need to take
  6663. * care of mismatches between target ulong and host ulong sizes.
  6664. */
  6665. if (arg2 & (sizeof(abi_ulong) - 1)) {
  6666. ret = -TARGET_EINVAL;
  6667. break;
  6668. }
  6669. mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
  6670. mask = alloca(mask_size);
  6671. ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
  6672. if (!is_error(ret)) {
  6673. if (copy_to_user(arg3, mask, ret)) {
  6674. goto efault;
  6675. }
  6676. }
  6677. }
  6678. break;
  6679. case TARGET_NR_sched_setaffinity:
  6680. {
  6681. unsigned int mask_size;
  6682. unsigned long *mask;
  6683. /*
  6684. * sched_setaffinity needs multiples of ulong, so need to take
  6685. * care of mismatches between target ulong and host ulong sizes.
  6686. */
  6687. if (arg2 & (sizeof(abi_ulong) - 1)) {
  6688. ret = -TARGET_EINVAL;
  6689. break;
  6690. }
  6691. mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
  6692. mask = alloca(mask_size);
  6693. if (!lock_user_struct(VERIFY_READ, p, arg3, 1)) {
  6694. goto efault;
  6695. }
  6696. memcpy(mask, p, arg2);
  6697. unlock_user_struct(p, arg2, 0);
  6698. ret = get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
  6699. }
  6700. break;
  6701. case TARGET_NR_sched_setparam:
  6702. {
  6703. struct sched_param *target_schp;
  6704. struct sched_param schp;
  6705. if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
  6706. goto efault;
  6707. schp.sched_priority = tswap32(target_schp->sched_priority);
  6708. unlock_user_struct(target_schp, arg2, 0);
  6709. ret = get_errno(sched_setparam(arg1, &schp));
  6710. }
  6711. break;
  6712. case TARGET_NR_sched_getparam:
  6713. {
  6714. struct sched_param *target_schp;
  6715. struct sched_param schp;
  6716. ret = get_errno(sched_getparam(arg1, &schp));
  6717. if (!is_error(ret)) {
  6718. if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
  6719. goto efault;
  6720. target_schp->sched_priority = tswap32(schp.sched_priority);
  6721. unlock_user_struct(target_schp, arg2, 1);
  6722. }
  6723. }
  6724. break;
  6725. case TARGET_NR_sched_setscheduler:
  6726. {
  6727. struct sched_param *target_schp;
  6728. struct sched_param schp;
  6729. if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
  6730. goto efault;
  6731. schp.sched_priority = tswap32(target_schp->sched_priority);
  6732. unlock_user_struct(target_schp, arg3, 0);
  6733. ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
  6734. }
  6735. break;
  6736. case TARGET_NR_sched_getscheduler:
  6737. ret = get_errno(sched_getscheduler(arg1));
  6738. break;
  6739. case TARGET_NR_sched_yield:
  6740. ret = get_errno(sched_yield());
  6741. break;
  6742. case TARGET_NR_sched_get_priority_max:
  6743. ret = get_errno(sched_get_priority_max(arg1));
  6744. break;
  6745. case TARGET_NR_sched_get_priority_min:
  6746. ret = get_errno(sched_get_priority_min(arg1));
  6747. break;
  6748. case TARGET_NR_sched_rr_get_interval:
  6749. {
  6750. struct timespec ts;
  6751. ret = get_errno(sched_rr_get_interval(arg1, &ts));
  6752. if (!is_error(ret)) {
  6753. host_to_target_timespec(arg2, &ts);
  6754. }
  6755. }
  6756. break;
  6757. case TARGET_NR_nanosleep:
  6758. {
  6759. struct timespec req, rem;
  6760. target_to_host_timespec(&req, arg1);
  6761. ret = get_errno(nanosleep(&req, &rem));
  6762. if (is_error(ret) && arg2) {
  6763. host_to_target_timespec(arg2, &rem);
  6764. }
  6765. }
  6766. break;
  6767. #ifdef TARGET_NR_query_module
  6768. case TARGET_NR_query_module:
  6769. goto unimplemented;
  6770. #endif
  6771. #ifdef TARGET_NR_nfsservctl
  6772. case TARGET_NR_nfsservctl:
  6773. goto unimplemented;
  6774. #endif
  6775. case TARGET_NR_prctl:
  6776. switch (arg1) {
  6777. case PR_GET_PDEATHSIG:
  6778. {
  6779. int deathsig;
  6780. ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
  6781. if (!is_error(ret) && arg2
  6782. && put_user_ual(deathsig, arg2)) {
  6783. goto efault;
  6784. }
  6785. break;
  6786. }
  6787. #ifdef PR_GET_NAME
  6788. case PR_GET_NAME:
  6789. {
  6790. void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
  6791. if (!name) {
  6792. goto efault;
  6793. }
  6794. ret = get_errno(prctl(arg1, (unsigned long)name,
  6795. arg3, arg4, arg5));
  6796. unlock_user(name, arg2, 16);
  6797. break;
  6798. }
  6799. case PR_SET_NAME:
  6800. {
  6801. void *name = lock_user(VERIFY_READ, arg2, 16, 1);
  6802. if (!name) {
  6803. goto efault;
  6804. }
  6805. ret = get_errno(prctl(arg1, (unsigned long)name,
  6806. arg3, arg4, arg5));
  6807. unlock_user(name, arg2, 0);
  6808. break;
  6809. }
  6810. #endif
  6811. default:
  6812. /* Most prctl options have no pointer arguments */
  6813. ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
  6814. break;
  6815. }
  6816. break;
  6817. #ifdef TARGET_NR_arch_prctl
  6818. case TARGET_NR_arch_prctl:
  6819. #if defined(TARGET_I386) && !defined(TARGET_ABI32)
  6820. ret = do_arch_prctl(cpu_env, arg1, arg2);
  6821. break;
  6822. #else
  6823. goto unimplemented;
  6824. #endif
  6825. #endif
  6826. #ifdef TARGET_NR_pread
  6827. case TARGET_NR_pread:
  6828. if (regpairs_aligned(cpu_env))
  6829. arg4 = arg5;
  6830. if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
  6831. goto efault;
  6832. ret = get_errno(pread(arg1, p, arg3, arg4));
  6833. unlock_user(p, arg2, ret);
  6834. break;
  6835. case TARGET_NR_pwrite:
  6836. if (regpairs_aligned(cpu_env))
  6837. arg4 = arg5;
  6838. if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
  6839. goto efault;
  6840. ret = get_errno(pwrite(arg1, p, arg3, arg4));
  6841. unlock_user(p, arg2, 0);
  6842. break;
  6843. #endif
  6844. #ifdef TARGET_NR_pread64
  6845. case TARGET_NR_pread64:
  6846. if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
  6847. goto efault;
  6848. ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
  6849. unlock_user(p, arg2, ret);
  6850. break;
  6851. case TARGET_NR_pwrite64:
  6852. if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
  6853. goto efault;
  6854. ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
  6855. unlock_user(p, arg2, 0);
  6856. break;
  6857. #endif
  6858. case TARGET_NR_getcwd:
  6859. if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
  6860. goto efault;
  6861. ret = get_errno(sys_getcwd1(p, arg2));
  6862. unlock_user(p, arg1, ret);
  6863. break;
  6864. case TARGET_NR_capget:
  6865. goto unimplemented;
  6866. case TARGET_NR_capset:
  6867. goto unimplemented;
  6868. case TARGET_NR_sigaltstack:
  6869. #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
  6870. defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
  6871. defined(TARGET_M68K) || defined(TARGET_S390X) || defined(TARGET_OPENRISC)
  6872. ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUArchState *)cpu_env));
  6873. break;
  6874. #else
  6875. goto unimplemented;
  6876. #endif
  6877. case TARGET_NR_sendfile:
  6878. goto unimplemented;
  6879. #ifdef TARGET_NR_getpmsg
  6880. case TARGET_NR_getpmsg:
  6881. goto unimplemented;
  6882. #endif
  6883. #ifdef TARGET_NR_putpmsg
  6884. case TARGET_NR_putpmsg:
  6885. goto unimplemented;
  6886. #endif
  6887. #ifdef TARGET_NR_vfork
  6888. case TARGET_NR_vfork:
  6889. ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
  6890. 0, 0, 0, 0));
  6891. break;
  6892. #endif
  6893. #ifdef TARGET_NR_ugetrlimit
  6894. case TARGET_NR_ugetrlimit:
  6895. {
  6896. struct rlimit rlim;
  6897. int resource = target_to_host_resource(arg1);
  6898. ret = get_errno(getrlimit(resource, &rlim));
  6899. if (!is_error(ret)) {
  6900. struct target_rlimit *target_rlim;
  6901. if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
  6902. goto efault;
  6903. target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
  6904. target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
  6905. unlock_user_struct(target_rlim, arg2, 1);
  6906. }
  6907. break;
  6908. }
  6909. #endif
  6910. #ifdef TARGET_NR_truncate64
  6911. case TARGET_NR_truncate64:
  6912. if (!(p = lock_user_string(arg1)))
  6913. goto efault;
  6914. ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
  6915. unlock_user(p, arg1, 0);
  6916. break;
  6917. #endif
  6918. #ifdef TARGET_NR_ftruncate64
  6919. case TARGET_NR_ftruncate64:
  6920. ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
  6921. break;
  6922. #endif
  6923. #ifdef TARGET_NR_stat64
  6924. case TARGET_NR_stat64:
  6925. if (!(p = lock_user_string(arg1)))
  6926. goto efault;
  6927. ret = get_errno(stat(path(p), &st));
  6928. unlock_user(p, arg1, 0);
  6929. if (!is_error(ret))
  6930. ret = host_to_target_stat64(cpu_env, arg2, &st);
  6931. break;
  6932. #endif
  6933. #ifdef TARGET_NR_lstat64
  6934. case TARGET_NR_lstat64:
  6935. if (!(p = lock_user_string(arg1)))
  6936. goto efault;
  6937. ret = get_errno(lstat(path(p), &st));
  6938. unlock_user(p, arg1, 0);
  6939. if (!is_error(ret))
  6940. ret = host_to_target_stat64(cpu_env, arg2, &st);
  6941. break;
  6942. #endif
  6943. #ifdef TARGET_NR_fstat64
  6944. case TARGET_NR_fstat64:
  6945. ret = get_errno(fstat(arg1, &st));
  6946. if (!is_error(ret))
  6947. ret = host_to_target_stat64(cpu_env, arg2, &st);
  6948. break;
  6949. #endif
  6950. #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
  6951. (defined(__NR_fstatat64) || defined(__NR_newfstatat))
  6952. #ifdef TARGET_NR_fstatat64
  6953. case TARGET_NR_fstatat64:
  6954. #endif
  6955. #ifdef TARGET_NR_newfstatat
  6956. case TARGET_NR_newfstatat:
  6957. #endif
  6958. if (!(p = lock_user_string(arg2)))
  6959. goto efault;
  6960. #ifdef __NR_fstatat64
  6961. ret = get_errno(sys_fstatat64(arg1, path(p), &st, arg4));
  6962. #else
  6963. ret = get_errno(sys_newfstatat(arg1, path(p), &st, arg4));
  6964. #endif
  6965. if (!is_error(ret))
  6966. ret = host_to_target_stat64(cpu_env, arg3, &st);
  6967. break;
  6968. #endif
  6969. case TARGET_NR_lchown:
  6970. if (!(p = lock_user_string(arg1)))
  6971. goto efault;
  6972. ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
  6973. unlock_user(p, arg1, 0);
  6974. break;
  6975. #ifdef TARGET_NR_getuid
  6976. case TARGET_NR_getuid:
  6977. ret = get_errno(high2lowuid(getuid()));
  6978. break;
  6979. #endif
  6980. #ifdef TARGET_NR_getgid
  6981. case TARGET_NR_getgid:
  6982. ret = get_errno(high2lowgid(getgid()));
  6983. break;
  6984. #endif
  6985. #ifdef TARGET_NR_geteuid
  6986. case TARGET_NR_geteuid:
  6987. ret = get_errno(high2lowuid(geteuid()));
  6988. break;
  6989. #endif
  6990. #ifdef TARGET_NR_getegid
  6991. case TARGET_NR_getegid:
  6992. ret = get_errno(high2lowgid(getegid()));
  6993. break;
  6994. #endif
  6995. case TARGET_NR_setreuid:
  6996. ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
  6997. break;
  6998. case TARGET_NR_setregid:
  6999. ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
  7000. break;
  7001. case TARGET_NR_getgroups:
  7002. {
  7003. int gidsetsize = arg1;
  7004. target_id *target_grouplist;
  7005. gid_t *grouplist;
  7006. int i;
  7007. grouplist = alloca(gidsetsize * sizeof(gid_t));
  7008. ret = get_errno(getgroups(gidsetsize, grouplist));
  7009. if (gidsetsize == 0)
  7010. break;
  7011. if (!is_error(ret)) {
  7012. target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
  7013. if (!target_grouplist)
  7014. goto efault;
  7015. for(i = 0;i < ret; i++)
  7016. target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
  7017. unlock_user(target_grouplist, arg2, gidsetsize * 2);
  7018. }
  7019. }
  7020. break;
  7021. case TARGET_NR_setgroups:
  7022. {
  7023. int gidsetsize = arg1;
  7024. target_id *target_grouplist;
  7025. gid_t *grouplist;
  7026. int i;
  7027. grouplist = alloca(gidsetsize * sizeof(gid_t));
  7028. target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
  7029. if (!target_grouplist) {
  7030. ret = -TARGET_EFAULT;
  7031. goto fail;
  7032. }
  7033. for(i = 0;i < gidsetsize; i++)
  7034. grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
  7035. unlock_user(target_grouplist, arg2, 0);
  7036. ret = get_errno(setgroups(gidsetsize, grouplist));
  7037. }
  7038. break;
  7039. case TARGET_NR_fchown:
  7040. ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
  7041. break;
  7042. #if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
  7043. case TARGET_NR_fchownat:
  7044. if (!(p = lock_user_string(arg2)))
  7045. goto efault;
  7046. ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
  7047. unlock_user(p, arg2, 0);
  7048. break;
  7049. #endif
  7050. #ifdef TARGET_NR_setresuid
  7051. case TARGET_NR_setresuid:
  7052. ret = get_errno(setresuid(low2highuid(arg1),
  7053. low2highuid(arg2),
  7054. low2highuid(arg3)));
  7055. break;
  7056. #endif
  7057. #ifdef TARGET_NR_getresuid
  7058. case TARGET_NR_getresuid:
  7059. {
  7060. uid_t ruid, euid, suid;
  7061. ret = get_errno(getresuid(&ruid, &euid, &suid));
  7062. if (!is_error(ret)) {
  7063. if (put_user_u16(high2lowuid(ruid), arg1)
  7064. || put_user_u16(high2lowuid(euid), arg2)
  7065. || put_user_u16(high2lowuid(suid), arg3))
  7066. goto efault;
  7067. }
  7068. }
  7069. break;
  7070. #endif
  7071. #ifdef TARGET_NR_getresgid
  7072. case TARGET_NR_setresgid:
  7073. ret = get_errno(setresgid(low2highgid(arg1),
  7074. low2highgid(arg2),
  7075. low2highgid(arg3)));
  7076. break;
  7077. #endif
  7078. #ifdef TARGET_NR_getresgid
  7079. case TARGET_NR_getresgid:
  7080. {
  7081. gid_t rgid, egid, sgid;
  7082. ret = get_errno(getresgid(&rgid, &egid, &sgid));
  7083. if (!is_error(ret)) {
  7084. if (put_user_u16(high2lowgid(rgid), arg1)
  7085. || put_user_u16(high2lowgid(egid), arg2)
  7086. || put_user_u16(high2lowgid(sgid), arg3))
  7087. goto efault;
  7088. }
  7089. }
  7090. break;
  7091. #endif
  7092. case TARGET_NR_chown:
  7093. if (!(p = lock_user_string(arg1)))
  7094. goto efault;
  7095. ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
  7096. unlock_user(p, arg1, 0);
  7097. break;
  7098. case TARGET_NR_setuid:
  7099. ret = get_errno(setuid(low2highuid(arg1)));
  7100. break;
  7101. case TARGET_NR_setgid:
  7102. ret = get_errno(setgid(low2highgid(arg1)));
  7103. break;
  7104. case TARGET_NR_setfsuid:
  7105. ret = get_errno(setfsuid(arg1));
  7106. break;
  7107. case TARGET_NR_setfsgid:
  7108. ret = get_errno(setfsgid(arg1));
  7109. break;
  7110. #ifdef TARGET_NR_lchown32
  7111. case TARGET_NR_lchown32:
  7112. if (!(p = lock_user_string(arg1)))
  7113. goto efault;
  7114. ret = get_errno(lchown(p, arg2, arg3));
  7115. unlock_user(p, arg1, 0);
  7116. break;
  7117. #endif
  7118. #ifdef TARGET_NR_getuid32
  7119. case TARGET_NR_getuid32:
  7120. ret = get_errno(getuid());
  7121. break;
  7122. #endif
  7123. #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
  7124. /* Alpha specific */
  7125. case TARGET_NR_getxuid:
  7126. {
  7127. uid_t euid;
  7128. euid=geteuid();
  7129. ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
  7130. }
  7131. ret = get_errno(getuid());
  7132. break;
  7133. #endif
  7134. #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
  7135. /* Alpha specific */
  7136. case TARGET_NR_getxgid:
  7137. {
  7138. uid_t egid;
  7139. egid=getegid();
  7140. ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
  7141. }
  7142. ret = get_errno(getgid());
  7143. break;
  7144. #endif
  7145. #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
  7146. /* Alpha specific */
  7147. case TARGET_NR_osf_getsysinfo:
  7148. ret = -TARGET_EOPNOTSUPP;
  7149. switch (arg1) {
  7150. case TARGET_GSI_IEEE_FP_CONTROL:
  7151. {
  7152. uint64_t swcr, fpcr = cpu_alpha_load_fpcr (cpu_env);
  7153. /* Copied from linux ieee_fpcr_to_swcr. */
  7154. swcr = (fpcr >> 35) & SWCR_STATUS_MASK;
  7155. swcr |= (fpcr >> 36) & SWCR_MAP_DMZ;
  7156. swcr |= (~fpcr >> 48) & (SWCR_TRAP_ENABLE_INV
  7157. | SWCR_TRAP_ENABLE_DZE
  7158. | SWCR_TRAP_ENABLE_OVF);
  7159. swcr |= (~fpcr >> 57) & (SWCR_TRAP_ENABLE_UNF
  7160. | SWCR_TRAP_ENABLE_INE);
  7161. swcr |= (fpcr >> 47) & SWCR_MAP_UMZ;
  7162. swcr |= (~fpcr >> 41) & SWCR_TRAP_ENABLE_DNO;
  7163. if (put_user_u64 (swcr, arg2))
  7164. goto efault;
  7165. ret = 0;
  7166. }
  7167. break;
  7168. /* case GSI_IEEE_STATE_AT_SIGNAL:
  7169. -- Not implemented in linux kernel.
  7170. case GSI_UACPROC:
  7171. -- Retrieves current unaligned access state; not much used.
  7172. case GSI_PROC_TYPE:
  7173. -- Retrieves implver information; surely not used.
  7174. case GSI_GET_HWRPB:
  7175. -- Grabs a copy of the HWRPB; surely not used.
  7176. */
  7177. }
  7178. break;
  7179. #endif
  7180. #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
  7181. /* Alpha specific */
  7182. case TARGET_NR_osf_setsysinfo:
  7183. ret = -TARGET_EOPNOTSUPP;
  7184. switch (arg1) {
  7185. case TARGET_SSI_IEEE_FP_CONTROL:
  7186. {
  7187. uint64_t swcr, fpcr, orig_fpcr;
  7188. if (get_user_u64 (swcr, arg2)) {
  7189. goto efault;
  7190. }
  7191. orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
  7192. fpcr = orig_fpcr & FPCR_DYN_MASK;
  7193. /* Copied from linux ieee_swcr_to_fpcr. */
  7194. fpcr |= (swcr & SWCR_STATUS_MASK) << 35;
  7195. fpcr |= (swcr & SWCR_MAP_DMZ) << 36;
  7196. fpcr |= (~swcr & (SWCR_TRAP_ENABLE_INV
  7197. | SWCR_TRAP_ENABLE_DZE
  7198. | SWCR_TRAP_ENABLE_OVF)) << 48;
  7199. fpcr |= (~swcr & (SWCR_TRAP_ENABLE_UNF
  7200. | SWCR_TRAP_ENABLE_INE)) << 57;
  7201. fpcr |= (swcr & SWCR_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0);
  7202. fpcr |= (~swcr & SWCR_TRAP_ENABLE_DNO) << 41;
  7203. cpu_alpha_store_fpcr(cpu_env, fpcr);
  7204. ret = 0;
  7205. }
  7206. break;
  7207. case TARGET_SSI_IEEE_RAISE_EXCEPTION:
  7208. {
  7209. uint64_t exc, fpcr, orig_fpcr;
  7210. int si_code;
  7211. if (get_user_u64(exc, arg2)) {
  7212. goto efault;
  7213. }
  7214. orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
  7215. /* We only add to the exception status here. */
  7216. fpcr = orig_fpcr | ((exc & SWCR_STATUS_MASK) << 35);
  7217. cpu_alpha_store_fpcr(cpu_env, fpcr);
  7218. ret = 0;
  7219. /* Old exceptions are not signaled. */
  7220. fpcr &= ~(orig_fpcr & FPCR_STATUS_MASK);
  7221. /* If any exceptions set by this call,
  7222. and are unmasked, send a signal. */
  7223. si_code = 0;
  7224. if ((fpcr & (FPCR_INE | FPCR_INED)) == FPCR_INE) {
  7225. si_code = TARGET_FPE_FLTRES;
  7226. }
  7227. if ((fpcr & (FPCR_UNF | FPCR_UNFD)) == FPCR_UNF) {
  7228. si_code = TARGET_FPE_FLTUND;
  7229. }
  7230. if ((fpcr & (FPCR_OVF | FPCR_OVFD)) == FPCR_OVF) {
  7231. si_code = TARGET_FPE_FLTOVF;
  7232. }
  7233. if ((fpcr & (FPCR_DZE | FPCR_DZED)) == FPCR_DZE) {
  7234. si_code = TARGET_FPE_FLTDIV;
  7235. }
  7236. if ((fpcr & (FPCR_INV | FPCR_INVD)) == FPCR_INV) {
  7237. si_code = TARGET_FPE_FLTINV;
  7238. }
  7239. if (si_code != 0) {
  7240. target_siginfo_t info;
  7241. info.si_signo = SIGFPE;
  7242. info.si_errno = 0;
  7243. info.si_code = si_code;
  7244. info._sifields._sigfault._addr
  7245. = ((CPUArchState *)cpu_env)->pc;
  7246. queue_signal((CPUArchState *)cpu_env, info.si_signo, &info);
  7247. }
  7248. }
  7249. break;
  7250. /* case SSI_NVPAIRS:
  7251. -- Used with SSIN_UACPROC to enable unaligned accesses.
  7252. case SSI_IEEE_STATE_AT_SIGNAL:
  7253. case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
  7254. -- Not implemented in linux kernel
  7255. */
  7256. }
  7257. break;
  7258. #endif
  7259. #ifdef TARGET_NR_osf_sigprocmask
  7260. /* Alpha specific. */
  7261. case TARGET_NR_osf_sigprocmask:
  7262. {
  7263. abi_ulong mask;
  7264. int how;
  7265. sigset_t set, oldset;
  7266. switch(arg1) {
  7267. case TARGET_SIG_BLOCK:
  7268. how = SIG_BLOCK;
  7269. break;
  7270. case TARGET_SIG_UNBLOCK:
  7271. how = SIG_UNBLOCK;
  7272. break;
  7273. case TARGET_SIG_SETMASK:
  7274. how = SIG_SETMASK;
  7275. break;
  7276. default:
  7277. ret = -TARGET_EINVAL;
  7278. goto fail;
  7279. }
  7280. mask = arg2;
  7281. target_to_host_old_sigset(&set, &mask);
  7282. sigprocmask(how, &set, &oldset);
  7283. host_to_target_old_sigset(&mask, &oldset);
  7284. ret = mask;
  7285. }
  7286. break;
  7287. #endif
  7288. #ifdef TARGET_NR_getgid32
  7289. case TARGET_NR_getgid32:
  7290. ret = get_errno(getgid());
  7291. break;
  7292. #endif
  7293. #ifdef TARGET_NR_geteuid32
  7294. case TARGET_NR_geteuid32:
  7295. ret = get_errno(geteuid());
  7296. break;
  7297. #endif
  7298. #ifdef TARGET_NR_getegid32
  7299. case TARGET_NR_getegid32:
  7300. ret = get_errno(getegid());
  7301. break;
  7302. #endif
  7303. #ifdef TARGET_NR_setreuid32
  7304. case TARGET_NR_setreuid32:
  7305. ret = get_errno(setreuid(arg1, arg2));
  7306. break;
  7307. #endif
  7308. #ifdef TARGET_NR_setregid32
  7309. case TARGET_NR_setregid32:
  7310. ret = get_errno(setregid(arg1, arg2));
  7311. break;
  7312. #endif
  7313. #ifdef TARGET_NR_getgroups32
  7314. case TARGET_NR_getgroups32:
  7315. {
  7316. int gidsetsize = arg1;
  7317. uint32_t *target_grouplist;
  7318. gid_t *grouplist;
  7319. int i;
  7320. grouplist = alloca(gidsetsize * sizeof(gid_t));
  7321. ret = get_errno(getgroups(gidsetsize, grouplist));
  7322. if (gidsetsize == 0)
  7323. break;
  7324. if (!is_error(ret)) {
  7325. target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
  7326. if (!target_grouplist) {
  7327. ret = -TARGET_EFAULT;
  7328. goto fail;
  7329. }
  7330. for(i = 0;i < ret; i++)
  7331. target_grouplist[i] = tswap32(grouplist[i]);
  7332. unlock_user(target_grouplist, arg2, gidsetsize * 4);
  7333. }
  7334. }
  7335. break;
  7336. #endif
  7337. #ifdef TARGET_NR_setgroups32
  7338. case TARGET_NR_setgroups32:
  7339. {
  7340. int gidsetsize = arg1;
  7341. uint32_t *target_grouplist;
  7342. gid_t *grouplist;
  7343. int i;
  7344. grouplist = alloca(gidsetsize * sizeof(gid_t));
  7345. target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
  7346. if (!target_grouplist) {
  7347. ret = -TARGET_EFAULT;
  7348. goto fail;
  7349. }
  7350. for(i = 0;i < gidsetsize; i++)
  7351. grouplist[i] = tswap32(target_grouplist[i]);
  7352. unlock_user(target_grouplist, arg2, 0);
  7353. ret = get_errno(setgroups(gidsetsize, grouplist));
  7354. }
  7355. break;
  7356. #endif
  7357. #ifdef TARGET_NR_fchown32
  7358. case TARGET_NR_fchown32:
  7359. ret = get_errno(fchown(arg1, arg2, arg3));
  7360. break;
  7361. #endif
  7362. #ifdef TARGET_NR_setresuid32
  7363. case TARGET_NR_setresuid32:
  7364. ret = get_errno(setresuid(arg1, arg2, arg3));
  7365. break;
  7366. #endif
  7367. #ifdef TARGET_NR_getresuid32
  7368. case TARGET_NR_getresuid32:
  7369. {
  7370. uid_t ruid, euid, suid;
  7371. ret = get_errno(getresuid(&ruid, &euid, &suid));
  7372. if (!is_error(ret)) {
  7373. if (put_user_u32(ruid, arg1)
  7374. || put_user_u32(euid, arg2)
  7375. || put_user_u32(suid, arg3))
  7376. goto efault;
  7377. }
  7378. }
  7379. break;
  7380. #endif
  7381. #ifdef TARGET_NR_setresgid32
  7382. case TARGET_NR_setresgid32:
  7383. ret = get_errno(setresgid(arg1, arg2, arg3));
  7384. break;
  7385. #endif
  7386. #ifdef TARGET_NR_getresgid32
  7387. case TARGET_NR_getresgid32:
  7388. {
  7389. gid_t rgid, egid, sgid;
  7390. ret = get_errno(getresgid(&rgid, &egid, &sgid));
  7391. if (!is_error(ret)) {
  7392. if (put_user_u32(rgid, arg1)
  7393. || put_user_u32(egid, arg2)
  7394. || put_user_u32(sgid, arg3))
  7395. goto efault;
  7396. }
  7397. }
  7398. break;
  7399. #endif
  7400. #ifdef TARGET_NR_chown32
  7401. case TARGET_NR_chown32:
  7402. if (!(p = lock_user_string(arg1)))
  7403. goto efault;
  7404. ret = get_errno(chown(p, arg2, arg3));
  7405. unlock_user(p, arg1, 0);
  7406. break;
  7407. #endif
  7408. #ifdef TARGET_NR_setuid32
  7409. case TARGET_NR_setuid32:
  7410. ret = get_errno(setuid(arg1));
  7411. break;
  7412. #endif
  7413. #ifdef TARGET_NR_setgid32
  7414. case TARGET_NR_setgid32:
  7415. ret = get_errno(setgid(arg1));
  7416. break;
  7417. #endif
  7418. #ifdef TARGET_NR_setfsuid32
  7419. case TARGET_NR_setfsuid32:
  7420. ret = get_errno(setfsuid(arg1));
  7421. break;
  7422. #endif
  7423. #ifdef TARGET_NR_setfsgid32
  7424. case TARGET_NR_setfsgid32:
  7425. ret = get_errno(setfsgid(arg1));
  7426. break;
  7427. #endif
  7428. case TARGET_NR_pivot_root:
  7429. goto unimplemented;
  7430. #ifdef TARGET_NR_mincore
  7431. case TARGET_NR_mincore:
  7432. {
  7433. void *a;
  7434. ret = -TARGET_EFAULT;
  7435. if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
  7436. goto efault;
  7437. if (!(p = lock_user_string(arg3)))
  7438. goto mincore_fail;
  7439. ret = get_errno(mincore(a, arg2, p));
  7440. unlock_user(p, arg3, ret);
  7441. mincore_fail:
  7442. unlock_user(a, arg1, 0);
  7443. }
  7444. break;
  7445. #endif
  7446. #ifdef TARGET_NR_arm_fadvise64_64
  7447. case TARGET_NR_arm_fadvise64_64:
  7448. {
  7449. /*
  7450. * arm_fadvise64_64 looks like fadvise64_64 but
  7451. * with different argument order
  7452. */
  7453. abi_long temp;
  7454. temp = arg3;
  7455. arg3 = arg4;
  7456. arg4 = temp;
  7457. }
  7458. #endif
  7459. #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
  7460. #ifdef TARGET_NR_fadvise64_64
  7461. case TARGET_NR_fadvise64_64:
  7462. #endif
  7463. #ifdef TARGET_NR_fadvise64
  7464. case TARGET_NR_fadvise64:
  7465. #endif
  7466. #ifdef TARGET_S390X
  7467. switch (arg4) {
  7468. case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
  7469. case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
  7470. case 6: arg4 = POSIX_FADV_DONTNEED; break;
  7471. case 7: arg4 = POSIX_FADV_NOREUSE; break;
  7472. default: break;
  7473. }
  7474. #endif
  7475. ret = -posix_fadvise(arg1, arg2, arg3, arg4);
  7476. break;
  7477. #endif
  7478. #ifdef TARGET_NR_madvise
  7479. case TARGET_NR_madvise:
  7480. /* A straight passthrough may not be safe because qemu sometimes
  7481. turns private flie-backed mappings into anonymous mappings.
  7482. This will break MADV_DONTNEED.
  7483. This is a hint, so ignoring and returning success is ok. */
  7484. ret = get_errno(0);
  7485. break;
  7486. #endif
  7487. #if TARGET_ABI_BITS == 32
  7488. case TARGET_NR_fcntl64:
  7489. {
  7490. int cmd;
  7491. struct flock64 fl;
  7492. struct target_flock64 *target_fl;
  7493. #ifdef TARGET_ARM
  7494. struct target_eabi_flock64 *target_efl;
  7495. #endif
  7496. cmd = target_to_host_fcntl_cmd(arg2);
  7497. if (cmd == -TARGET_EINVAL) {
  7498. ret = cmd;
  7499. break;
  7500. }
  7501. switch(arg2) {
  7502. case TARGET_F_GETLK64:
  7503. #ifdef TARGET_ARM
  7504. if (((CPUARMState *)cpu_env)->eabi) {
  7505. if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
  7506. goto efault;
  7507. fl.l_type = tswap16(target_efl->l_type);
  7508. fl.l_whence = tswap16(target_efl->l_whence);
  7509. fl.l_start = tswap64(target_efl->l_start);
  7510. fl.l_len = tswap64(target_efl->l_len);
  7511. fl.l_pid = tswap32(target_efl->l_pid);
  7512. unlock_user_struct(target_efl, arg3, 0);
  7513. } else
  7514. #endif
  7515. {
  7516. if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
  7517. goto efault;
  7518. fl.l_type = tswap16(target_fl->l_type);
  7519. fl.l_whence = tswap16(target_fl->l_whence);
  7520. fl.l_start = tswap64(target_fl->l_start);
  7521. fl.l_len = tswap64(target_fl->l_len);
  7522. fl.l_pid = tswap32(target_fl->l_pid);
  7523. unlock_user_struct(target_fl, arg3, 0);
  7524. }
  7525. ret = get_errno(fcntl(arg1, cmd, &fl));
  7526. if (ret == 0) {
  7527. #ifdef TARGET_ARM
  7528. if (((CPUARMState *)cpu_env)->eabi) {
  7529. if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
  7530. goto efault;
  7531. target_efl->l_type = tswap16(fl.l_type);
  7532. target_efl->l_whence = tswap16(fl.l_whence);
  7533. target_efl->l_start = tswap64(fl.l_start);
  7534. target_efl->l_len = tswap64(fl.l_len);
  7535. target_efl->l_pid = tswap32(fl.l_pid);
  7536. unlock_user_struct(target_efl, arg3, 1);
  7537. } else
  7538. #endif
  7539. {
  7540. if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
  7541. goto efault;
  7542. target_fl->l_type = tswap16(fl.l_type);
  7543. target_fl->l_whence = tswap16(fl.l_whence);
  7544. target_fl->l_start = tswap64(fl.l_start);
  7545. target_fl->l_len = tswap64(fl.l_len);
  7546. target_fl->l_pid = tswap32(fl.l_pid);
  7547. unlock_user_struct(target_fl, arg3, 1);
  7548. }
  7549. }
  7550. break;
  7551. case TARGET_F_SETLK64:
  7552. case TARGET_F_SETLKW64:
  7553. #ifdef TARGET_ARM
  7554. if (((CPUARMState *)cpu_env)->eabi) {
  7555. if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
  7556. goto efault;
  7557. fl.l_type = tswap16(target_efl->l_type);
  7558. fl.l_whence = tswap16(target_efl->l_whence);
  7559. fl.l_start = tswap64(target_efl->l_start);
  7560. fl.l_len = tswap64(target_efl->l_len);
  7561. fl.l_pid = tswap32(target_efl->l_pid);
  7562. unlock_user_struct(target_efl, arg3, 0);
  7563. } else
  7564. #endif
  7565. {
  7566. if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
  7567. goto efault;
  7568. fl.l_type = tswap16(target_fl->l_type);
  7569. fl.l_whence = tswap16(target_fl->l_whence);
  7570. fl.l_start = tswap64(target_fl->l_start);
  7571. fl.l_len = tswap64(target_fl->l_len);
  7572. fl.l_pid = tswap32(target_fl->l_pid);
  7573. unlock_user_struct(target_fl, arg3, 0);
  7574. }
  7575. ret = get_errno(fcntl(arg1, cmd, &fl));
  7576. break;
  7577. default:
  7578. ret = do_fcntl(arg1, arg2, arg3);
  7579. break;
  7580. }
  7581. break;
  7582. }
  7583. #endif
  7584. #ifdef TARGET_NR_cacheflush
  7585. case TARGET_NR_cacheflush:
  7586. /* self-modifying code is handled automatically, so nothing needed */
  7587. ret = 0;
  7588. break;
  7589. #endif
  7590. #ifdef TARGET_NR_security
  7591. case TARGET_NR_security:
  7592. goto unimplemented;
  7593. #endif
  7594. #ifdef TARGET_NR_getpagesize
  7595. case TARGET_NR_getpagesize:
  7596. ret = TARGET_PAGE_SIZE;
  7597. break;
  7598. #endif
  7599. case TARGET_NR_gettid:
  7600. ret = get_errno(gettid());
  7601. break;
  7602. #ifdef TARGET_NR_readahead
  7603. case TARGET_NR_readahead:
  7604. #if TARGET_ABI_BITS == 32
  7605. if (regpairs_aligned(cpu_env)) {
  7606. arg2 = arg3;
  7607. arg3 = arg4;
  7608. arg4 = arg5;
  7609. }
  7610. ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
  7611. #else
  7612. ret = get_errno(readahead(arg1, arg2, arg3));
  7613. #endif
  7614. break;
  7615. #endif
  7616. #ifdef CONFIG_ATTR
  7617. #ifdef TARGET_NR_setxattr
  7618. case TARGET_NR_listxattr:
  7619. case TARGET_NR_llistxattr:
  7620. {
  7621. void *p, *b = 0;
  7622. if (arg2) {
  7623. b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  7624. if (!b) {
  7625. ret = -TARGET_EFAULT;
  7626. break;
  7627. }
  7628. }
  7629. p = lock_user_string(arg1);
  7630. if (p) {
  7631. if (num == TARGET_NR_listxattr) {
  7632. ret = get_errno(listxattr(p, b, arg3));
  7633. } else {
  7634. ret = get_errno(llistxattr(p, b, arg3));
  7635. }
  7636. } else {
  7637. ret = -TARGET_EFAULT;
  7638. }
  7639. unlock_user(p, arg1, 0);
  7640. unlock_user(b, arg2, arg3);
  7641. break;
  7642. }
  7643. case TARGET_NR_flistxattr:
  7644. {
  7645. void *b = 0;
  7646. if (arg2) {
  7647. b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  7648. if (!b) {
  7649. ret = -TARGET_EFAULT;
  7650. break;
  7651. }
  7652. }
  7653. ret = get_errno(flistxattr(arg1, b, arg3));
  7654. unlock_user(b, arg2, arg3);
  7655. break;
  7656. }
  7657. case TARGET_NR_setxattr:
  7658. case TARGET_NR_lsetxattr:
  7659. {
  7660. void *p, *n, *v = 0;
  7661. if (arg3) {
  7662. v = lock_user(VERIFY_READ, arg3, arg4, 1);
  7663. if (!v) {
  7664. ret = -TARGET_EFAULT;
  7665. break;
  7666. }
  7667. }
  7668. p = lock_user_string(arg1);
  7669. n = lock_user_string(arg2);
  7670. if (p && n) {
  7671. if (num == TARGET_NR_setxattr) {
  7672. ret = get_errno(setxattr(p, n, v, arg4, arg5));
  7673. } else {
  7674. ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
  7675. }
  7676. } else {
  7677. ret = -TARGET_EFAULT;
  7678. }
  7679. unlock_user(p, arg1, 0);
  7680. unlock_user(n, arg2, 0);
  7681. unlock_user(v, arg3, 0);
  7682. }
  7683. break;
  7684. case TARGET_NR_fsetxattr:
  7685. {
  7686. void *n, *v = 0;
  7687. if (arg3) {
  7688. v = lock_user(VERIFY_READ, arg3, arg4, 1);
  7689. if (!v) {
  7690. ret = -TARGET_EFAULT;
  7691. break;
  7692. }
  7693. }
  7694. n = lock_user_string(arg2);
  7695. if (n) {
  7696. ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
  7697. } else {
  7698. ret = -TARGET_EFAULT;
  7699. }
  7700. unlock_user(n, arg2, 0);
  7701. unlock_user(v, arg3, 0);
  7702. }
  7703. break;
  7704. case TARGET_NR_getxattr:
  7705. case TARGET_NR_lgetxattr:
  7706. {
  7707. void *p, *n, *v = 0;
  7708. if (arg3) {
  7709. v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  7710. if (!v) {
  7711. ret = -TARGET_EFAULT;
  7712. break;
  7713. }
  7714. }
  7715. p = lock_user_string(arg1);
  7716. n = lock_user_string(arg2);
  7717. if (p && n) {
  7718. if (num == TARGET_NR_getxattr) {
  7719. ret = get_errno(getxattr(p, n, v, arg4));
  7720. } else {
  7721. ret = get_errno(lgetxattr(p, n, v, arg4));
  7722. }
  7723. } else {
  7724. ret = -TARGET_EFAULT;
  7725. }
  7726. unlock_user(p, arg1, 0);
  7727. unlock_user(n, arg2, 0);
  7728. unlock_user(v, arg3, arg4);
  7729. }
  7730. break;
  7731. case TARGET_NR_fgetxattr:
  7732. {
  7733. void *n, *v = 0;
  7734. if (arg3) {
  7735. v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  7736. if (!v) {
  7737. ret = -TARGET_EFAULT;
  7738. break;
  7739. }
  7740. }
  7741. n = lock_user_string(arg2);
  7742. if (n) {
  7743. ret = get_errno(fgetxattr(arg1, n, v, arg4));
  7744. } else {
  7745. ret = -TARGET_EFAULT;
  7746. }
  7747. unlock_user(n, arg2, 0);
  7748. unlock_user(v, arg3, arg4);
  7749. }
  7750. break;
  7751. case TARGET_NR_removexattr:
  7752. case TARGET_NR_lremovexattr:
  7753. {
  7754. void *p, *n;
  7755. p = lock_user_string(arg1);
  7756. n = lock_user_string(arg2);
  7757. if (p && n) {
  7758. if (num == TARGET_NR_removexattr) {
  7759. ret = get_errno(removexattr(p, n));
  7760. } else {
  7761. ret = get_errno(lremovexattr(p, n));
  7762. }
  7763. } else {
  7764. ret = -TARGET_EFAULT;
  7765. }
  7766. unlock_user(p, arg1, 0);
  7767. unlock_user(n, arg2, 0);
  7768. }
  7769. break;
  7770. case TARGET_NR_fremovexattr:
  7771. {
  7772. void *n;
  7773. n = lock_user_string(arg2);
  7774. if (n) {
  7775. ret = get_errno(fremovexattr(arg1, n));
  7776. } else {
  7777. ret = -TARGET_EFAULT;
  7778. }
  7779. unlock_user(n, arg2, 0);
  7780. }
  7781. break;
  7782. #endif
  7783. #endif /* CONFIG_ATTR */
  7784. #ifdef TARGET_NR_set_thread_area
  7785. case TARGET_NR_set_thread_area:
  7786. #if defined(TARGET_MIPS)
  7787. ((CPUMIPSState *) cpu_env)->tls_value = arg1;
  7788. ret = 0;
  7789. break;
  7790. #elif defined(TARGET_CRIS)
  7791. if (arg1 & 0xff)
  7792. ret = -TARGET_EINVAL;
  7793. else {
  7794. ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
  7795. ret = 0;
  7796. }
  7797. break;
  7798. #elif defined(TARGET_I386) && defined(TARGET_ABI32)
  7799. ret = do_set_thread_area(cpu_env, arg1);
  7800. break;
  7801. #else
  7802. goto unimplemented_nowarn;
  7803. #endif
  7804. #endif
  7805. #ifdef TARGET_NR_get_thread_area
  7806. case TARGET_NR_get_thread_area:
  7807. #if defined(TARGET_I386) && defined(TARGET_ABI32)
  7808. ret = do_get_thread_area(cpu_env, arg1);
  7809. #else
  7810. goto unimplemented_nowarn;
  7811. #endif
  7812. #endif
  7813. #ifdef TARGET_NR_getdomainname
  7814. case TARGET_NR_getdomainname:
  7815. goto unimplemented_nowarn;
  7816. #endif
  7817. #ifdef TARGET_NR_clock_gettime
  7818. case TARGET_NR_clock_gettime:
  7819. {
  7820. struct timespec ts;
  7821. ret = get_errno(clock_gettime(arg1, &ts));
  7822. if (!is_error(ret)) {
  7823. host_to_target_timespec(arg2, &ts);
  7824. }
  7825. break;
  7826. }
  7827. #endif
  7828. #ifdef TARGET_NR_clock_getres
  7829. case TARGET_NR_clock_getres:
  7830. {
  7831. struct timespec ts;
  7832. ret = get_errno(clock_getres(arg1, &ts));
  7833. if (!is_error(ret)) {
  7834. host_to_target_timespec(arg2, &ts);
  7835. }
  7836. break;
  7837. }
  7838. #endif
  7839. #ifdef TARGET_NR_clock_nanosleep
  7840. case TARGET_NR_clock_nanosleep:
  7841. {
  7842. struct timespec ts;
  7843. target_to_host_timespec(&ts, arg3);
  7844. ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
  7845. if (arg4)
  7846. host_to_target_timespec(arg4, &ts);
  7847. break;
  7848. }
  7849. #endif
  7850. #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
  7851. case TARGET_NR_set_tid_address:
  7852. ret = get_errno(set_tid_address((int *)g2h(arg1)));
  7853. break;
  7854. #endif
  7855. #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
  7856. case TARGET_NR_tkill:
  7857. ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
  7858. break;
  7859. #endif
  7860. #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
  7861. case TARGET_NR_tgkill:
  7862. ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
  7863. target_to_host_signal(arg3)));
  7864. break;
  7865. #endif
  7866. #ifdef TARGET_NR_set_robust_list
  7867. case TARGET_NR_set_robust_list:
  7868. goto unimplemented_nowarn;
  7869. #endif
  7870. #if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
  7871. case TARGET_NR_utimensat:
  7872. {
  7873. struct timespec *tsp, ts[2];
  7874. if (!arg3) {
  7875. tsp = NULL;
  7876. } else {
  7877. target_to_host_timespec(ts, arg3);
  7878. target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
  7879. tsp = ts;
  7880. }
  7881. if (!arg2)
  7882. ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
  7883. else {
  7884. if (!(p = lock_user_string(arg2))) {
  7885. ret = -TARGET_EFAULT;
  7886. goto fail;
  7887. }
  7888. ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
  7889. unlock_user(p, arg2, 0);
  7890. }
  7891. }
  7892. break;
  7893. #endif
  7894. #if defined(CONFIG_USE_NPTL)
  7895. case TARGET_NR_futex:
  7896. ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
  7897. break;
  7898. #endif
  7899. #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
  7900. case TARGET_NR_inotify_init:
  7901. ret = get_errno(sys_inotify_init());
  7902. break;
  7903. #endif
  7904. #ifdef CONFIG_INOTIFY1
  7905. #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
  7906. case TARGET_NR_inotify_init1:
  7907. ret = get_errno(sys_inotify_init1(arg1));
  7908. break;
  7909. #endif
  7910. #endif
  7911. #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
  7912. case TARGET_NR_inotify_add_watch:
  7913. p = lock_user_string(arg2);
  7914. ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
  7915. unlock_user(p, arg2, 0);
  7916. break;
  7917. #endif
  7918. #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
  7919. case TARGET_NR_inotify_rm_watch:
  7920. ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
  7921. break;
  7922. #endif
  7923. #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
  7924. case TARGET_NR_mq_open:
  7925. {
  7926. struct mq_attr posix_mq_attr;
  7927. p = lock_user_string(arg1 - 1);
  7928. if (arg4 != 0)
  7929. copy_from_user_mq_attr (&posix_mq_attr, arg4);
  7930. ret = get_errno(mq_open(p, arg2, arg3, &posix_mq_attr));
  7931. unlock_user (p, arg1, 0);
  7932. }
  7933. break;
  7934. case TARGET_NR_mq_unlink:
  7935. p = lock_user_string(arg1 - 1);
  7936. ret = get_errno(mq_unlink(p));
  7937. unlock_user (p, arg1, 0);
  7938. break;
  7939. case TARGET_NR_mq_timedsend:
  7940. {
  7941. struct timespec ts;
  7942. p = lock_user (VERIFY_READ, arg2, arg3, 1);
  7943. if (arg5 != 0) {
  7944. target_to_host_timespec(&ts, arg5);
  7945. ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
  7946. host_to_target_timespec(arg5, &ts);
  7947. }
  7948. else
  7949. ret = get_errno(mq_send(arg1, p, arg3, arg4));
  7950. unlock_user (p, arg2, arg3);
  7951. }
  7952. break;
  7953. case TARGET_NR_mq_timedreceive:
  7954. {
  7955. struct timespec ts;
  7956. unsigned int prio;
  7957. p = lock_user (VERIFY_READ, arg2, arg3, 1);
  7958. if (arg5 != 0) {
  7959. target_to_host_timespec(&ts, arg5);
  7960. ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
  7961. host_to_target_timespec(arg5, &ts);
  7962. }
  7963. else
  7964. ret = get_errno(mq_receive(arg1, p, arg3, &prio));
  7965. unlock_user (p, arg2, arg3);
  7966. if (arg4 != 0)
  7967. put_user_u32(prio, arg4);
  7968. }
  7969. break;
  7970. /* Not implemented for now... */
  7971. /* case TARGET_NR_mq_notify: */
  7972. /* break; */
  7973. case TARGET_NR_mq_getsetattr:
  7974. {
  7975. struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
  7976. ret = 0;
  7977. if (arg3 != 0) {
  7978. ret = mq_getattr(arg1, &posix_mq_attr_out);
  7979. copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
  7980. }
  7981. if (arg2 != 0) {
  7982. copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
  7983. ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
  7984. }
  7985. }
  7986. break;
  7987. #endif
  7988. #ifdef CONFIG_SPLICE
  7989. #ifdef TARGET_NR_tee
  7990. case TARGET_NR_tee:
  7991. {
  7992. ret = get_errno(tee(arg1,arg2,arg3,arg4));
  7993. }
  7994. break;
  7995. #endif
  7996. #ifdef TARGET_NR_splice
  7997. case TARGET_NR_splice:
  7998. {
  7999. loff_t loff_in, loff_out;
  8000. loff_t *ploff_in = NULL, *ploff_out = NULL;
  8001. if(arg2) {
  8002. get_user_u64(loff_in, arg2);
  8003. ploff_in = &loff_in;
  8004. }
  8005. if(arg4) {
  8006. get_user_u64(loff_out, arg2);
  8007. ploff_out = &loff_out;
  8008. }
  8009. ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
  8010. }
  8011. break;
  8012. #endif
  8013. #ifdef TARGET_NR_vmsplice
  8014. case TARGET_NR_vmsplice:
  8015. {
  8016. int count = arg3;
  8017. struct iovec *vec;
  8018. vec = alloca(count * sizeof(struct iovec));
  8019. if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
  8020. goto efault;
  8021. ret = get_errno(vmsplice(arg1, vec, count, arg4));
  8022. unlock_iovec(vec, arg2, count, 0);
  8023. }
  8024. break;
  8025. #endif
  8026. #endif /* CONFIG_SPLICE */
  8027. #ifdef CONFIG_EVENTFD
  8028. #if defined(TARGET_NR_eventfd)
  8029. case TARGET_NR_eventfd:
  8030. ret = get_errno(eventfd(arg1, 0));
  8031. break;
  8032. #endif
  8033. #if defined(TARGET_NR_eventfd2)
  8034. case TARGET_NR_eventfd2:
  8035. ret = get_errno(eventfd(arg1, arg2));
  8036. break;
  8037. #endif
  8038. #endif /* CONFIG_EVENTFD */
  8039. #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
  8040. case TARGET_NR_fallocate:
  8041. #if TARGET_ABI_BITS == 32
  8042. ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
  8043. target_offset64(arg5, arg6)));
  8044. #else
  8045. ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
  8046. #endif
  8047. break;
  8048. #endif
  8049. #if defined(CONFIG_SYNC_FILE_RANGE)
  8050. #if defined(TARGET_NR_sync_file_range)
  8051. case TARGET_NR_sync_file_range:
  8052. #if TARGET_ABI_BITS == 32
  8053. #if defined(TARGET_MIPS)
  8054. ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
  8055. target_offset64(arg5, arg6), arg7));
  8056. #else
  8057. ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
  8058. target_offset64(arg4, arg5), arg6));
  8059. #endif /* !TARGET_MIPS */
  8060. #else
  8061. ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
  8062. #endif
  8063. break;
  8064. #endif
  8065. #if defined(TARGET_NR_sync_file_range2)
  8066. case TARGET_NR_sync_file_range2:
  8067. /* This is like sync_file_range but the arguments are reordered */
  8068. #if TARGET_ABI_BITS == 32
  8069. ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
  8070. target_offset64(arg5, arg6), arg2));
  8071. #else
  8072. ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
  8073. #endif
  8074. break;
  8075. #endif
  8076. #endif
  8077. #if defined(CONFIG_EPOLL)
  8078. #if defined(TARGET_NR_epoll_create)
  8079. case TARGET_NR_epoll_create:
  8080. ret = get_errno(epoll_create(arg1));
  8081. break;
  8082. #endif
  8083. #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
  8084. case TARGET_NR_epoll_create1:
  8085. ret = get_errno(epoll_create1(arg1));
  8086. break;
  8087. #endif
  8088. #if defined(TARGET_NR_epoll_ctl)
  8089. case TARGET_NR_epoll_ctl:
  8090. {
  8091. struct epoll_event ep;
  8092. struct epoll_event *epp = 0;
  8093. if (arg4) {
  8094. struct target_epoll_event *target_ep;
  8095. if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
  8096. goto efault;
  8097. }
  8098. ep.events = tswap32(target_ep->events);
  8099. /* The epoll_data_t union is just opaque data to the kernel,
  8100. * so we transfer all 64 bits across and need not worry what
  8101. * actual data type it is.
  8102. */
  8103. ep.data.u64 = tswap64(target_ep->data.u64);
  8104. unlock_user_struct(target_ep, arg4, 0);
  8105. epp = &ep;
  8106. }
  8107. ret = get_errno(epoll_ctl(arg1, arg2, arg3, epp));
  8108. break;
  8109. }
  8110. #endif
  8111. #if defined(TARGET_NR_epoll_pwait) && defined(CONFIG_EPOLL_PWAIT)
  8112. #define IMPLEMENT_EPOLL_PWAIT
  8113. #endif
  8114. #if defined(TARGET_NR_epoll_wait) || defined(IMPLEMENT_EPOLL_PWAIT)
  8115. #if defined(TARGET_NR_epoll_wait)
  8116. case TARGET_NR_epoll_wait:
  8117. #endif
  8118. #if defined(IMPLEMENT_EPOLL_PWAIT)
  8119. case TARGET_NR_epoll_pwait:
  8120. #endif
  8121. {
  8122. struct target_epoll_event *target_ep;
  8123. struct epoll_event *ep;
  8124. int epfd = arg1;
  8125. int maxevents = arg3;
  8126. int timeout = arg4;
  8127. target_ep = lock_user(VERIFY_WRITE, arg2,
  8128. maxevents * sizeof(struct target_epoll_event), 1);
  8129. if (!target_ep) {
  8130. goto efault;
  8131. }
  8132. ep = alloca(maxevents * sizeof(struct epoll_event));
  8133. switch (num) {
  8134. #if defined(IMPLEMENT_EPOLL_PWAIT)
  8135. case TARGET_NR_epoll_pwait:
  8136. {
  8137. target_sigset_t *target_set;
  8138. sigset_t _set, *set = &_set;
  8139. if (arg5) {
  8140. target_set = lock_user(VERIFY_READ, arg5,
  8141. sizeof(target_sigset_t), 1);
  8142. if (!target_set) {
  8143. unlock_user(target_ep, arg2, 0);
  8144. goto efault;
  8145. }
  8146. target_to_host_sigset(set, target_set);
  8147. unlock_user(target_set, arg5, 0);
  8148. } else {
  8149. set = NULL;
  8150. }
  8151. ret = get_errno(epoll_pwait(epfd, ep, maxevents, timeout, set));
  8152. break;
  8153. }
  8154. #endif
  8155. #if defined(TARGET_NR_epoll_wait)
  8156. case TARGET_NR_epoll_wait:
  8157. ret = get_errno(epoll_wait(epfd, ep, maxevents, timeout));
  8158. break;
  8159. #endif
  8160. default:
  8161. ret = -TARGET_ENOSYS;
  8162. }
  8163. if (!is_error(ret)) {
  8164. int i;
  8165. for (i = 0; i < ret; i++) {
  8166. target_ep[i].events = tswap32(ep[i].events);
  8167. target_ep[i].data.u64 = tswap64(ep[i].data.u64);
  8168. }
  8169. }
  8170. unlock_user(target_ep, arg2, ret * sizeof(struct target_epoll_event));
  8171. break;
  8172. }
  8173. #endif
  8174. #endif
  8175. #ifdef TARGET_NR_prlimit64
  8176. case TARGET_NR_prlimit64:
  8177. {
  8178. /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
  8179. struct target_rlimit64 *target_rnew, *target_rold;
  8180. struct host_rlimit64 rnew, rold, *rnewp = 0;
  8181. if (arg3) {
  8182. if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
  8183. goto efault;
  8184. }
  8185. rnew.rlim_cur = tswap64(target_rnew->rlim_cur);
  8186. rnew.rlim_max = tswap64(target_rnew->rlim_max);
  8187. unlock_user_struct(target_rnew, arg3, 0);
  8188. rnewp = &rnew;
  8189. }
  8190. ret = get_errno(sys_prlimit64(arg1, arg2, rnewp, arg4 ? &rold : 0));
  8191. if (!is_error(ret) && arg4) {
  8192. if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
  8193. goto efault;
  8194. }
  8195. target_rold->rlim_cur = tswap64(rold.rlim_cur);
  8196. target_rold->rlim_max = tswap64(rold.rlim_max);
  8197. unlock_user_struct(target_rold, arg4, 1);
  8198. }
  8199. break;
  8200. }
  8201. #endif
  8202. default:
  8203. unimplemented:
  8204. gemu_log("qemu: Unsupported syscall: %d\n", num);
  8205. #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
  8206. unimplemented_nowarn:
  8207. #endif
  8208. ret = -TARGET_ENOSYS;
  8209. break;
  8210. }
  8211. fail:
  8212. #ifdef DEBUG
  8213. gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
  8214. #endif
  8215. if(do_strace)
  8216. print_syscall_ret(num, ret);
  8217. return ret;
  8218. efault:
  8219. ret = -TARGET_EFAULT;
  8220. goto fail;
  8221. }