syscall.c 296 KB

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