syscall.c 386 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109
  1. /*
  2. * Linux syscalls
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #define _ATFILE_SOURCE
  20. #include "qemu/osdep.h"
  21. #include "qemu/cutils.h"
  22. #include "qemu/path.h"
  23. #include "qemu/memfd.h"
  24. #include "qemu/queue.h"
  25. #include <elf.h>
  26. #include <endian.h>
  27. #include <grp.h>
  28. #include <sys/ipc.h>
  29. #include <sys/msg.h>
  30. #include <sys/wait.h>
  31. #include <sys/mount.h>
  32. #include <sys/file.h>
  33. #include <sys/fsuid.h>
  34. #include <sys/personality.h>
  35. #include <sys/prctl.h>
  36. #include <sys/resource.h>
  37. #include <sys/swap.h>
  38. #include <linux/capability.h>
  39. #include <sched.h>
  40. #include <sys/timex.h>
  41. #include <sys/socket.h>
  42. #include <linux/sockios.h>
  43. #include <sys/un.h>
  44. #include <sys/uio.h>
  45. #include <poll.h>
  46. #include <sys/times.h>
  47. #include <sys/shm.h>
  48. #include <sys/sem.h>
  49. #include <sys/statfs.h>
  50. #include <utime.h>
  51. #include <sys/sysinfo.h>
  52. #include <sys/signalfd.h>
  53. //#include <sys/user.h>
  54. #include <netinet/ip.h>
  55. #include <netinet/tcp.h>
  56. #include <linux/wireless.h>
  57. #include <linux/icmp.h>
  58. #include <linux/icmpv6.h>
  59. #include <linux/errqueue.h>
  60. #include <linux/random.h>
  61. #ifdef CONFIG_TIMERFD
  62. #include <sys/timerfd.h>
  63. #endif
  64. #ifdef CONFIG_EVENTFD
  65. #include <sys/eventfd.h>
  66. #endif
  67. #ifdef CONFIG_EPOLL
  68. #include <sys/epoll.h>
  69. #endif
  70. #ifdef CONFIG_ATTR
  71. #include "qemu/xattr.h"
  72. #endif
  73. #ifdef CONFIG_SENDFILE
  74. #include <sys/sendfile.h>
  75. #endif
  76. #define termios host_termios
  77. #define winsize host_winsize
  78. #define termio host_termio
  79. #define sgttyb host_sgttyb /* same as target */
  80. #define tchars host_tchars /* same as target */
  81. #define ltchars host_ltchars /* same as target */
  82. #include <linux/termios.h>
  83. #include <linux/unistd.h>
  84. #include <linux/cdrom.h>
  85. #include <linux/hdreg.h>
  86. #include <linux/soundcard.h>
  87. #include <linux/kd.h>
  88. #include <linux/mtio.h>
  89. #include <linux/fs.h>
  90. #include <linux/fd.h>
  91. #if defined(CONFIG_FIEMAP)
  92. #include <linux/fiemap.h>
  93. #endif
  94. #include <linux/fb.h>
  95. #if defined(CONFIG_USBFS)
  96. #include <linux/usbdevice_fs.h>
  97. #include <linux/usb/ch9.h>
  98. #endif
  99. #include <linux/vt.h>
  100. #include <linux/dm-ioctl.h>
  101. #include <linux/reboot.h>
  102. #include <linux/route.h>
  103. #include <linux/filter.h>
  104. #include <linux/blkpg.h>
  105. #include <netpacket/packet.h>
  106. #include <linux/netlink.h>
  107. #include <linux/if_alg.h>
  108. #include "linux_loop.h"
  109. #include "uname.h"
  110. #include "qemu.h"
  111. #include "qemu/guest-random.h"
  112. #include "user/syscall-trace.h"
  113. #include "qapi/error.h"
  114. #include "fd-trans.h"
  115. #ifndef CLONE_IO
  116. #define CLONE_IO 0x80000000 /* Clone io context */
  117. #endif
  118. /* We can't directly call the host clone syscall, because this will
  119. * badly confuse libc (breaking mutexes, for example). So we must
  120. * divide clone flags into:
  121. * * flag combinations that look like pthread_create()
  122. * * flag combinations that look like fork()
  123. * * flags we can implement within QEMU itself
  124. * * flags we can't support and will return an error for
  125. */
  126. /* For thread creation, all these flags must be present; for
  127. * fork, none must be present.
  128. */
  129. #define CLONE_THREAD_FLAGS \
  130. (CLONE_VM | CLONE_FS | CLONE_FILES | \
  131. CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM)
  132. /* These flags are ignored:
  133. * CLONE_DETACHED is now ignored by the kernel;
  134. * CLONE_IO is just an optimisation hint to the I/O scheduler
  135. */
  136. #define CLONE_IGNORED_FLAGS \
  137. (CLONE_DETACHED | CLONE_IO)
  138. /* Flags for fork which we can implement within QEMU itself */
  139. #define CLONE_OPTIONAL_FORK_FLAGS \
  140. (CLONE_SETTLS | CLONE_PARENT_SETTID | \
  141. CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID)
  142. /* Flags for thread creation which we can implement within QEMU itself */
  143. #define CLONE_OPTIONAL_THREAD_FLAGS \
  144. (CLONE_SETTLS | CLONE_PARENT_SETTID | \
  145. CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | CLONE_PARENT)
  146. #define CLONE_INVALID_FORK_FLAGS \
  147. (~(CSIGNAL | CLONE_OPTIONAL_FORK_FLAGS | CLONE_IGNORED_FLAGS))
  148. #define CLONE_INVALID_THREAD_FLAGS \
  149. (~(CSIGNAL | CLONE_THREAD_FLAGS | CLONE_OPTIONAL_THREAD_FLAGS | \
  150. CLONE_IGNORED_FLAGS))
  151. /* CLONE_VFORK is special cased early in do_fork(). The other flag bits
  152. * have almost all been allocated. We cannot support any of
  153. * CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC,
  154. * CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, CLONE_PTRACE, CLONE_UNTRACED.
  155. * The checks against the invalid thread masks above will catch these.
  156. * (The one remaining unallocated bit is 0x1000 which used to be CLONE_PID.)
  157. */
  158. /* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
  159. * once. This exercises the codepaths for restart.
  160. */
  161. //#define DEBUG_ERESTARTSYS
  162. //#include <linux/msdos_fs.h>
  163. #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
  164. #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
  165. #undef _syscall0
  166. #undef _syscall1
  167. #undef _syscall2
  168. #undef _syscall3
  169. #undef _syscall4
  170. #undef _syscall5
  171. #undef _syscall6
  172. #define _syscall0(type,name) \
  173. static type name (void) \
  174. { \
  175. return syscall(__NR_##name); \
  176. }
  177. #define _syscall1(type,name,type1,arg1) \
  178. static type name (type1 arg1) \
  179. { \
  180. return syscall(__NR_##name, arg1); \
  181. }
  182. #define _syscall2(type,name,type1,arg1,type2,arg2) \
  183. static type name (type1 arg1,type2 arg2) \
  184. { \
  185. return syscall(__NR_##name, arg1, arg2); \
  186. }
  187. #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
  188. static type name (type1 arg1,type2 arg2,type3 arg3) \
  189. { \
  190. return syscall(__NR_##name, arg1, arg2, arg3); \
  191. }
  192. #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
  193. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
  194. { \
  195. return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
  196. }
  197. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  198. type5,arg5) \
  199. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
  200. { \
  201. return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
  202. }
  203. #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  204. type5,arg5,type6,arg6) \
  205. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
  206. type6 arg6) \
  207. { \
  208. return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
  209. }
  210. #define __NR_sys_uname __NR_uname
  211. #define __NR_sys_getcwd1 __NR_getcwd
  212. #define __NR_sys_getdents __NR_getdents
  213. #define __NR_sys_getdents64 __NR_getdents64
  214. #define __NR_sys_getpriority __NR_getpriority
  215. #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
  216. #define __NR_sys_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo
  217. #define __NR_sys_syslog __NR_syslog
  218. #define __NR_sys_futex __NR_futex
  219. #define __NR_sys_inotify_init __NR_inotify_init
  220. #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
  221. #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
  222. #define __NR_sys_statx __NR_statx
  223. #if defined(__alpha__) || defined(__x86_64__) || defined(__s390x__)
  224. #define __NR__llseek __NR_lseek
  225. #endif
  226. /* Newer kernel ports have llseek() instead of _llseek() */
  227. #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
  228. #define TARGET_NR__llseek TARGET_NR_llseek
  229. #endif
  230. #define __NR_sys_gettid __NR_gettid
  231. _syscall0(int, sys_gettid)
  232. /* For the 64-bit guest on 32-bit host case we must emulate
  233. * getdents using getdents64, because otherwise the host
  234. * might hand us back more dirent records than we can fit
  235. * into the guest buffer after structure format conversion.
  236. * Otherwise we emulate getdents with getdents if the host has it.
  237. */
  238. #if defined(__NR_getdents) && HOST_LONG_BITS >= TARGET_ABI_BITS
  239. #define EMULATE_GETDENTS_WITH_GETDENTS
  240. #endif
  241. #if defined(TARGET_NR_getdents) && defined(EMULATE_GETDENTS_WITH_GETDENTS)
  242. _syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
  243. #endif
  244. #if (defined(TARGET_NR_getdents) && \
  245. !defined(EMULATE_GETDENTS_WITH_GETDENTS)) || \
  246. (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
  247. _syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
  248. #endif
  249. #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
  250. _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
  251. loff_t *, res, uint, wh);
  252. #endif
  253. _syscall3(int, sys_rt_sigqueueinfo, pid_t, pid, int, sig, siginfo_t *, uinfo)
  254. _syscall4(int, sys_rt_tgsigqueueinfo, pid_t, pid, pid_t, tid, int, sig,
  255. siginfo_t *, uinfo)
  256. _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
  257. #ifdef __NR_exit_group
  258. _syscall1(int,exit_group,int,error_code)
  259. #endif
  260. #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
  261. _syscall1(int,set_tid_address,int *,tidptr)
  262. #endif
  263. #if defined(TARGET_NR_futex) && defined(__NR_futex)
  264. _syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
  265. const struct timespec *,timeout,int *,uaddr2,int,val3)
  266. #endif
  267. #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
  268. _syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
  269. unsigned long *, user_mask_ptr);
  270. #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
  271. _syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
  272. unsigned long *, user_mask_ptr);
  273. #define __NR_sys_getcpu __NR_getcpu
  274. _syscall3(int, sys_getcpu, unsigned *, cpu, unsigned *, node, void *, tcache);
  275. _syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
  276. void *, arg);
  277. _syscall2(int, capget, struct __user_cap_header_struct *, header,
  278. struct __user_cap_data_struct *, data);
  279. _syscall2(int, capset, struct __user_cap_header_struct *, header,
  280. struct __user_cap_data_struct *, data);
  281. #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
  282. _syscall2(int, ioprio_get, int, which, int, who)
  283. #endif
  284. #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
  285. _syscall3(int, ioprio_set, int, which, int, who, int, ioprio)
  286. #endif
  287. #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
  288. _syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags)
  289. #endif
  290. #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
  291. _syscall5(int, kcmp, pid_t, pid1, pid_t, pid2, int, type,
  292. unsigned long, idx1, unsigned long, idx2)
  293. #endif
  294. /*
  295. * It is assumed that struct statx is architecture independent.
  296. */
  297. #if defined(TARGET_NR_statx) && defined(__NR_statx)
  298. _syscall5(int, sys_statx, int, dirfd, const char *, pathname, int, flags,
  299. unsigned int, mask, struct target_statx *, statxbuf)
  300. #endif
  301. static bitmask_transtbl fcntl_flags_tbl[] = {
  302. { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
  303. { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
  304. { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
  305. { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
  306. { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
  307. { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
  308. { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
  309. { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
  310. { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
  311. { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
  312. { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
  313. { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
  314. { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
  315. #if defined(O_DIRECT)
  316. { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
  317. #endif
  318. #if defined(O_NOATIME)
  319. { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
  320. #endif
  321. #if defined(O_CLOEXEC)
  322. { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
  323. #endif
  324. #if defined(O_PATH)
  325. { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
  326. #endif
  327. #if defined(O_TMPFILE)
  328. { TARGET_O_TMPFILE, TARGET_O_TMPFILE, O_TMPFILE, O_TMPFILE },
  329. #endif
  330. /* Don't terminate the list prematurely on 64-bit host+guest. */
  331. #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
  332. { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
  333. #endif
  334. { 0, 0, 0, 0 }
  335. };
  336. static int sys_getcwd1(char *buf, size_t size)
  337. {
  338. if (getcwd(buf, size) == NULL) {
  339. /* getcwd() sets errno */
  340. return (-1);
  341. }
  342. return strlen(buf)+1;
  343. }
  344. #ifdef TARGET_NR_utimensat
  345. #if defined(__NR_utimensat)
  346. #define __NR_sys_utimensat __NR_utimensat
  347. _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
  348. const struct timespec *,tsp,int,flags)
  349. #else
  350. static int sys_utimensat(int dirfd, const char *pathname,
  351. const struct timespec times[2], int flags)
  352. {
  353. errno = ENOSYS;
  354. return -1;
  355. }
  356. #endif
  357. #endif /* TARGET_NR_utimensat */
  358. #ifdef TARGET_NR_renameat2
  359. #if defined(__NR_renameat2)
  360. #define __NR_sys_renameat2 __NR_renameat2
  361. _syscall5(int, sys_renameat2, int, oldfd, const char *, old, int, newfd,
  362. const char *, new, unsigned int, flags)
  363. #else
  364. static int sys_renameat2(int oldfd, const char *old,
  365. int newfd, const char *new, int flags)
  366. {
  367. if (flags == 0) {
  368. return renameat(oldfd, old, newfd, new);
  369. }
  370. errno = ENOSYS;
  371. return -1;
  372. }
  373. #endif
  374. #endif /* TARGET_NR_renameat2 */
  375. #ifdef CONFIG_INOTIFY
  376. #include <sys/inotify.h>
  377. #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
  378. static int sys_inotify_init(void)
  379. {
  380. return (inotify_init());
  381. }
  382. #endif
  383. #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
  384. static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
  385. {
  386. return (inotify_add_watch(fd, pathname, mask));
  387. }
  388. #endif
  389. #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
  390. static int sys_inotify_rm_watch(int fd, int32_t wd)
  391. {
  392. return (inotify_rm_watch(fd, wd));
  393. }
  394. #endif
  395. #ifdef CONFIG_INOTIFY1
  396. #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
  397. static int sys_inotify_init1(int flags)
  398. {
  399. return (inotify_init1(flags));
  400. }
  401. #endif
  402. #endif
  403. #else
  404. /* Userspace can usually survive runtime without inotify */
  405. #undef TARGET_NR_inotify_init
  406. #undef TARGET_NR_inotify_init1
  407. #undef TARGET_NR_inotify_add_watch
  408. #undef TARGET_NR_inotify_rm_watch
  409. #endif /* CONFIG_INOTIFY */
  410. #if defined(TARGET_NR_prlimit64)
  411. #ifndef __NR_prlimit64
  412. # define __NR_prlimit64 -1
  413. #endif
  414. #define __NR_sys_prlimit64 __NR_prlimit64
  415. /* The glibc rlimit structure may not be that used by the underlying syscall */
  416. struct host_rlimit64 {
  417. uint64_t rlim_cur;
  418. uint64_t rlim_max;
  419. };
  420. _syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
  421. const struct host_rlimit64 *, new_limit,
  422. struct host_rlimit64 *, old_limit)
  423. #endif
  424. #if defined(TARGET_NR_timer_create)
  425. /* Maxiumum of 32 active POSIX timers allowed at any one time. */
  426. static timer_t g_posix_timers[32] = { 0, } ;
  427. static inline int next_free_host_timer(void)
  428. {
  429. int k ;
  430. /* FIXME: Does finding the next free slot require a lock? */
  431. for (k = 0; k < ARRAY_SIZE(g_posix_timers); k++) {
  432. if (g_posix_timers[k] == 0) {
  433. g_posix_timers[k] = (timer_t) 1;
  434. return k;
  435. }
  436. }
  437. return -1;
  438. }
  439. #endif
  440. /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
  441. #ifdef TARGET_ARM
  442. static inline int regpairs_aligned(void *cpu_env, int num)
  443. {
  444. return ((((CPUARMState *)cpu_env)->eabi) == 1) ;
  445. }
  446. #elif defined(TARGET_MIPS) && (TARGET_ABI_BITS == 32)
  447. static inline int regpairs_aligned(void *cpu_env, int num) { return 1; }
  448. #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
  449. /* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
  450. * of registers which translates to the same as ARM/MIPS, because we start with
  451. * r3 as arg1 */
  452. static inline int regpairs_aligned(void *cpu_env, int num) { return 1; }
  453. #elif defined(TARGET_SH4)
  454. /* SH4 doesn't align register pairs, except for p{read,write}64 */
  455. static inline int regpairs_aligned(void *cpu_env, int num)
  456. {
  457. switch (num) {
  458. case TARGET_NR_pread64:
  459. case TARGET_NR_pwrite64:
  460. return 1;
  461. default:
  462. return 0;
  463. }
  464. }
  465. #elif defined(TARGET_XTENSA)
  466. static inline int regpairs_aligned(void *cpu_env, int num) { return 1; }
  467. #else
  468. static inline int regpairs_aligned(void *cpu_env, int num) { return 0; }
  469. #endif
  470. #define ERRNO_TABLE_SIZE 1200
  471. /* target_to_host_errno_table[] is initialized from
  472. * host_to_target_errno_table[] in syscall_init(). */
  473. static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
  474. };
  475. /*
  476. * This list is the union of errno values overridden in asm-<arch>/errno.h
  477. * minus the errnos that are not actually generic to all archs.
  478. */
  479. static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
  480. [EAGAIN] = TARGET_EAGAIN,
  481. [EIDRM] = TARGET_EIDRM,
  482. [ECHRNG] = TARGET_ECHRNG,
  483. [EL2NSYNC] = TARGET_EL2NSYNC,
  484. [EL3HLT] = TARGET_EL3HLT,
  485. [EL3RST] = TARGET_EL3RST,
  486. [ELNRNG] = TARGET_ELNRNG,
  487. [EUNATCH] = TARGET_EUNATCH,
  488. [ENOCSI] = TARGET_ENOCSI,
  489. [EL2HLT] = TARGET_EL2HLT,
  490. [EDEADLK] = TARGET_EDEADLK,
  491. [ENOLCK] = TARGET_ENOLCK,
  492. [EBADE] = TARGET_EBADE,
  493. [EBADR] = TARGET_EBADR,
  494. [EXFULL] = TARGET_EXFULL,
  495. [ENOANO] = TARGET_ENOANO,
  496. [EBADRQC] = TARGET_EBADRQC,
  497. [EBADSLT] = TARGET_EBADSLT,
  498. [EBFONT] = TARGET_EBFONT,
  499. [ENOSTR] = TARGET_ENOSTR,
  500. [ENODATA] = TARGET_ENODATA,
  501. [ETIME] = TARGET_ETIME,
  502. [ENOSR] = TARGET_ENOSR,
  503. [ENONET] = TARGET_ENONET,
  504. [ENOPKG] = TARGET_ENOPKG,
  505. [EREMOTE] = TARGET_EREMOTE,
  506. [ENOLINK] = TARGET_ENOLINK,
  507. [EADV] = TARGET_EADV,
  508. [ESRMNT] = TARGET_ESRMNT,
  509. [ECOMM] = TARGET_ECOMM,
  510. [EPROTO] = TARGET_EPROTO,
  511. [EDOTDOT] = TARGET_EDOTDOT,
  512. [EMULTIHOP] = TARGET_EMULTIHOP,
  513. [EBADMSG] = TARGET_EBADMSG,
  514. [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
  515. [EOVERFLOW] = TARGET_EOVERFLOW,
  516. [ENOTUNIQ] = TARGET_ENOTUNIQ,
  517. [EBADFD] = TARGET_EBADFD,
  518. [EREMCHG] = TARGET_EREMCHG,
  519. [ELIBACC] = TARGET_ELIBACC,
  520. [ELIBBAD] = TARGET_ELIBBAD,
  521. [ELIBSCN] = TARGET_ELIBSCN,
  522. [ELIBMAX] = TARGET_ELIBMAX,
  523. [ELIBEXEC] = TARGET_ELIBEXEC,
  524. [EILSEQ] = TARGET_EILSEQ,
  525. [ENOSYS] = TARGET_ENOSYS,
  526. [ELOOP] = TARGET_ELOOP,
  527. [ERESTART] = TARGET_ERESTART,
  528. [ESTRPIPE] = TARGET_ESTRPIPE,
  529. [ENOTEMPTY] = TARGET_ENOTEMPTY,
  530. [EUSERS] = TARGET_EUSERS,
  531. [ENOTSOCK] = TARGET_ENOTSOCK,
  532. [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
  533. [EMSGSIZE] = TARGET_EMSGSIZE,
  534. [EPROTOTYPE] = TARGET_EPROTOTYPE,
  535. [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
  536. [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
  537. [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
  538. [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
  539. [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
  540. [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
  541. [EADDRINUSE] = TARGET_EADDRINUSE,
  542. [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
  543. [ENETDOWN] = TARGET_ENETDOWN,
  544. [ENETUNREACH] = TARGET_ENETUNREACH,
  545. [ENETRESET] = TARGET_ENETRESET,
  546. [ECONNABORTED] = TARGET_ECONNABORTED,
  547. [ECONNRESET] = TARGET_ECONNRESET,
  548. [ENOBUFS] = TARGET_ENOBUFS,
  549. [EISCONN] = TARGET_EISCONN,
  550. [ENOTCONN] = TARGET_ENOTCONN,
  551. [EUCLEAN] = TARGET_EUCLEAN,
  552. [ENOTNAM] = TARGET_ENOTNAM,
  553. [ENAVAIL] = TARGET_ENAVAIL,
  554. [EISNAM] = TARGET_EISNAM,
  555. [EREMOTEIO] = TARGET_EREMOTEIO,
  556. [EDQUOT] = TARGET_EDQUOT,
  557. [ESHUTDOWN] = TARGET_ESHUTDOWN,
  558. [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
  559. [ETIMEDOUT] = TARGET_ETIMEDOUT,
  560. [ECONNREFUSED] = TARGET_ECONNREFUSED,
  561. [EHOSTDOWN] = TARGET_EHOSTDOWN,
  562. [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
  563. [EALREADY] = TARGET_EALREADY,
  564. [EINPROGRESS] = TARGET_EINPROGRESS,
  565. [ESTALE] = TARGET_ESTALE,
  566. [ECANCELED] = TARGET_ECANCELED,
  567. [ENOMEDIUM] = TARGET_ENOMEDIUM,
  568. [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
  569. #ifdef ENOKEY
  570. [ENOKEY] = TARGET_ENOKEY,
  571. #endif
  572. #ifdef EKEYEXPIRED
  573. [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
  574. #endif
  575. #ifdef EKEYREVOKED
  576. [EKEYREVOKED] = TARGET_EKEYREVOKED,
  577. #endif
  578. #ifdef EKEYREJECTED
  579. [EKEYREJECTED] = TARGET_EKEYREJECTED,
  580. #endif
  581. #ifdef EOWNERDEAD
  582. [EOWNERDEAD] = TARGET_EOWNERDEAD,
  583. #endif
  584. #ifdef ENOTRECOVERABLE
  585. [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
  586. #endif
  587. #ifdef ENOMSG
  588. [ENOMSG] = TARGET_ENOMSG,
  589. #endif
  590. #ifdef ERKFILL
  591. [ERFKILL] = TARGET_ERFKILL,
  592. #endif
  593. #ifdef EHWPOISON
  594. [EHWPOISON] = TARGET_EHWPOISON,
  595. #endif
  596. };
  597. static inline int host_to_target_errno(int err)
  598. {
  599. if (err >= 0 && err < ERRNO_TABLE_SIZE &&
  600. host_to_target_errno_table[err]) {
  601. return host_to_target_errno_table[err];
  602. }
  603. return err;
  604. }
  605. static inline int target_to_host_errno(int err)
  606. {
  607. if (err >= 0 && err < ERRNO_TABLE_SIZE &&
  608. target_to_host_errno_table[err]) {
  609. return target_to_host_errno_table[err];
  610. }
  611. return err;
  612. }
  613. static inline abi_long get_errno(abi_long ret)
  614. {
  615. if (ret == -1)
  616. return -host_to_target_errno(errno);
  617. else
  618. return ret;
  619. }
  620. const char *target_strerror(int err)
  621. {
  622. if (err == TARGET_ERESTARTSYS) {
  623. return "To be restarted";
  624. }
  625. if (err == TARGET_QEMU_ESIGRETURN) {
  626. return "Successful exit from sigreturn";
  627. }
  628. if ((err >= ERRNO_TABLE_SIZE) || (err < 0)) {
  629. return NULL;
  630. }
  631. return strerror(target_to_host_errno(err));
  632. }
  633. #define safe_syscall0(type, name) \
  634. static type safe_##name(void) \
  635. { \
  636. return safe_syscall(__NR_##name); \
  637. }
  638. #define safe_syscall1(type, name, type1, arg1) \
  639. static type safe_##name(type1 arg1) \
  640. { \
  641. return safe_syscall(__NR_##name, arg1); \
  642. }
  643. #define safe_syscall2(type, name, type1, arg1, type2, arg2) \
  644. static type safe_##name(type1 arg1, type2 arg2) \
  645. { \
  646. return safe_syscall(__NR_##name, arg1, arg2); \
  647. }
  648. #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
  649. static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
  650. { \
  651. return safe_syscall(__NR_##name, arg1, arg2, arg3); \
  652. }
  653. #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
  654. type4, arg4) \
  655. static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
  656. { \
  657. return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
  658. }
  659. #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
  660. type4, arg4, type5, arg5) \
  661. static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  662. type5 arg5) \
  663. { \
  664. return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
  665. }
  666. #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
  667. type4, arg4, type5, arg5, type6, arg6) \
  668. static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  669. type5 arg5, type6 arg6) \
  670. { \
  671. return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
  672. }
  673. safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count)
  674. safe_syscall3(ssize_t, write, int, fd, const void *, buff, size_t, count)
  675. safe_syscall4(int, openat, int, dirfd, const char *, pathname, \
  676. int, flags, mode_t, mode)
  677. safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \
  678. struct rusage *, rusage)
  679. safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \
  680. int, options, struct rusage *, rusage)
  681. safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp)
  682. safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \
  683. fd_set *, exceptfds, struct timespec *, timeout, void *, sig)
  684. safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds,
  685. struct timespec *, tsp, const sigset_t *, sigmask,
  686. size_t, sigsetsize)
  687. safe_syscall6(int, epoll_pwait, int, epfd, struct epoll_event *, events,
  688. int, maxevents, int, timeout, const sigset_t *, sigmask,
  689. size_t, sigsetsize)
  690. safe_syscall6(int,futex,int *,uaddr,int,op,int,val, \
  691. const struct timespec *,timeout,int *,uaddr2,int,val3)
  692. safe_syscall2(int, rt_sigsuspend, sigset_t *, newset, size_t, sigsetsize)
  693. safe_syscall2(int, kill, pid_t, pid, int, sig)
  694. safe_syscall2(int, tkill, int, tid, int, sig)
  695. safe_syscall3(int, tgkill, int, tgid, int, pid, int, sig)
  696. safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt)
  697. safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt)
  698. safe_syscall5(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt,
  699. unsigned long, pos_l, unsigned long, pos_h)
  700. safe_syscall5(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, iovcnt,
  701. unsigned long, pos_l, unsigned long, pos_h)
  702. safe_syscall3(int, connect, int, fd, const struct sockaddr *, addr,
  703. socklen_t, addrlen)
  704. safe_syscall6(ssize_t, sendto, int, fd, const void *, buf, size_t, len,
  705. int, flags, const struct sockaddr *, addr, socklen_t, addrlen)
  706. safe_syscall6(ssize_t, recvfrom, int, fd, void *, buf, size_t, len,
  707. int, flags, struct sockaddr *, addr, socklen_t *, addrlen)
  708. safe_syscall3(ssize_t, sendmsg, int, fd, const struct msghdr *, msg, int, flags)
  709. safe_syscall3(ssize_t, recvmsg, int, fd, struct msghdr *, msg, int, flags)
  710. safe_syscall2(int, flock, int, fd, int, operation)
  711. safe_syscall4(int, rt_sigtimedwait, const sigset_t *, these, siginfo_t *, uinfo,
  712. const struct timespec *, uts, size_t, sigsetsize)
  713. safe_syscall4(int, accept4, int, fd, struct sockaddr *, addr, socklen_t *, len,
  714. int, flags)
  715. safe_syscall2(int, nanosleep, const struct timespec *, req,
  716. struct timespec *, rem)
  717. #ifdef TARGET_NR_clock_nanosleep
  718. safe_syscall4(int, clock_nanosleep, const clockid_t, clock, int, flags,
  719. const struct timespec *, req, struct timespec *, rem)
  720. #endif
  721. #ifdef __NR_ipc
  722. safe_syscall6(int, ipc, int, call, long, first, long, second, long, third,
  723. void *, ptr, long, fifth)
  724. #endif
  725. #ifdef __NR_msgsnd
  726. safe_syscall4(int, msgsnd, int, msgid, const void *, msgp, size_t, sz,
  727. int, flags)
  728. #endif
  729. #ifdef __NR_msgrcv
  730. safe_syscall5(int, msgrcv, int, msgid, void *, msgp, size_t, sz,
  731. long, msgtype, int, flags)
  732. #endif
  733. #ifdef __NR_semtimedop
  734. safe_syscall4(int, semtimedop, int, semid, struct sembuf *, tsops,
  735. unsigned, nsops, const struct timespec *, timeout)
  736. #endif
  737. #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
  738. safe_syscall5(int, mq_timedsend, int, mqdes, const char *, msg_ptr,
  739. size_t, len, unsigned, prio, const struct timespec *, timeout)
  740. safe_syscall5(int, mq_timedreceive, int, mqdes, char *, msg_ptr,
  741. size_t, len, unsigned *, prio, const struct timespec *, timeout)
  742. #endif
  743. /* We do ioctl like this rather than via safe_syscall3 to preserve the
  744. * "third argument might be integer or pointer or not present" behaviour of
  745. * the libc function.
  746. */
  747. #define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
  748. /* Similarly for fcntl. Note that callers must always:
  749. * pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK
  750. * use the flock64 struct rather than unsuffixed flock
  751. * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts.
  752. */
  753. #ifdef __NR_fcntl64
  754. #define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
  755. #else
  756. #define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
  757. #endif
  758. static inline int host_to_target_sock_type(int host_type)
  759. {
  760. int target_type;
  761. switch (host_type & 0xf /* SOCK_TYPE_MASK */) {
  762. case SOCK_DGRAM:
  763. target_type = TARGET_SOCK_DGRAM;
  764. break;
  765. case SOCK_STREAM:
  766. target_type = TARGET_SOCK_STREAM;
  767. break;
  768. default:
  769. target_type = host_type & 0xf /* SOCK_TYPE_MASK */;
  770. break;
  771. }
  772. #if defined(SOCK_CLOEXEC)
  773. if (host_type & SOCK_CLOEXEC) {
  774. target_type |= TARGET_SOCK_CLOEXEC;
  775. }
  776. #endif
  777. #if defined(SOCK_NONBLOCK)
  778. if (host_type & SOCK_NONBLOCK) {
  779. target_type |= TARGET_SOCK_NONBLOCK;
  780. }
  781. #endif
  782. return target_type;
  783. }
  784. static abi_ulong target_brk;
  785. static abi_ulong target_original_brk;
  786. static abi_ulong brk_page;
  787. void target_set_brk(abi_ulong new_brk)
  788. {
  789. target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
  790. brk_page = HOST_PAGE_ALIGN(target_brk);
  791. }
  792. //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
  793. #define DEBUGF_BRK(message, args...)
  794. /* do_brk() must return target values and target errnos. */
  795. abi_long do_brk(abi_ulong new_brk)
  796. {
  797. abi_long mapped_addr;
  798. abi_ulong new_alloc_size;
  799. DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
  800. if (!new_brk) {
  801. DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
  802. return target_brk;
  803. }
  804. if (new_brk < target_original_brk) {
  805. DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
  806. target_brk);
  807. return target_brk;
  808. }
  809. /* If the new brk is less than the highest page reserved to the
  810. * target heap allocation, set it and we're almost done... */
  811. if (new_brk <= brk_page) {
  812. /* Heap contents are initialized to zero, as for anonymous
  813. * mapped pages. */
  814. if (new_brk > target_brk) {
  815. memset(g2h(target_brk), 0, new_brk - target_brk);
  816. }
  817. target_brk = new_brk;
  818. DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
  819. return target_brk;
  820. }
  821. /* We need to allocate more memory after the brk... Note that
  822. * we don't use MAP_FIXED because that will map over the top of
  823. * any existing mapping (like the one with the host libc or qemu
  824. * itself); instead we treat "mapped but at wrong address" as
  825. * a failure and unmap again.
  826. */
  827. new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
  828. mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
  829. PROT_READ|PROT_WRITE,
  830. MAP_ANON|MAP_PRIVATE, 0, 0));
  831. if (mapped_addr == brk_page) {
  832. /* Heap contents are initialized to zero, as for anonymous
  833. * mapped pages. Technically the new pages are already
  834. * initialized to zero since they *are* anonymous mapped
  835. * pages, however we have to take care with the contents that
  836. * come from the remaining part of the previous page: it may
  837. * contains garbage data due to a previous heap usage (grown
  838. * then shrunken). */
  839. memset(g2h(target_brk), 0, brk_page - target_brk);
  840. target_brk = new_brk;
  841. brk_page = HOST_PAGE_ALIGN(target_brk);
  842. DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
  843. target_brk);
  844. return target_brk;
  845. } else if (mapped_addr != -1) {
  846. /* Mapped but at wrong address, meaning there wasn't actually
  847. * enough space for this brk.
  848. */
  849. target_munmap(mapped_addr, new_alloc_size);
  850. mapped_addr = -1;
  851. DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
  852. }
  853. else {
  854. DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
  855. }
  856. #if defined(TARGET_ALPHA)
  857. /* We (partially) emulate OSF/1 on Alpha, which requires we
  858. return a proper errno, not an unchanged brk value. */
  859. return -TARGET_ENOMEM;
  860. #endif
  861. /* For everything else, return the previous break. */
  862. return target_brk;
  863. }
  864. static inline abi_long copy_from_user_fdset(fd_set *fds,
  865. abi_ulong target_fds_addr,
  866. int n)
  867. {
  868. int i, nw, j, k;
  869. abi_ulong b, *target_fds;
  870. nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
  871. if (!(target_fds = lock_user(VERIFY_READ,
  872. target_fds_addr,
  873. sizeof(abi_ulong) * nw,
  874. 1)))
  875. return -TARGET_EFAULT;
  876. FD_ZERO(fds);
  877. k = 0;
  878. for (i = 0; i < nw; i++) {
  879. /* grab the abi_ulong */
  880. __get_user(b, &target_fds[i]);
  881. for (j = 0; j < TARGET_ABI_BITS; j++) {
  882. /* check the bit inside the abi_ulong */
  883. if ((b >> j) & 1)
  884. FD_SET(k, fds);
  885. k++;
  886. }
  887. }
  888. unlock_user(target_fds, target_fds_addr, 0);
  889. return 0;
  890. }
  891. static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
  892. abi_ulong target_fds_addr,
  893. int n)
  894. {
  895. if (target_fds_addr) {
  896. if (copy_from_user_fdset(fds, target_fds_addr, n))
  897. return -TARGET_EFAULT;
  898. *fds_ptr = fds;
  899. } else {
  900. *fds_ptr = NULL;
  901. }
  902. return 0;
  903. }
  904. static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
  905. const fd_set *fds,
  906. int n)
  907. {
  908. int i, nw, j, k;
  909. abi_long v;
  910. abi_ulong *target_fds;
  911. nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
  912. if (!(target_fds = lock_user(VERIFY_WRITE,
  913. target_fds_addr,
  914. sizeof(abi_ulong) * nw,
  915. 0)))
  916. return -TARGET_EFAULT;
  917. k = 0;
  918. for (i = 0; i < nw; i++) {
  919. v = 0;
  920. for (j = 0; j < TARGET_ABI_BITS; j++) {
  921. v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
  922. k++;
  923. }
  924. __put_user(v, &target_fds[i]);
  925. }
  926. unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
  927. return 0;
  928. }
  929. #if defined(__alpha__)
  930. #define HOST_HZ 1024
  931. #else
  932. #define HOST_HZ 100
  933. #endif
  934. static inline abi_long host_to_target_clock_t(long ticks)
  935. {
  936. #if HOST_HZ == TARGET_HZ
  937. return ticks;
  938. #else
  939. return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
  940. #endif
  941. }
  942. static inline abi_long host_to_target_rusage(abi_ulong target_addr,
  943. const struct rusage *rusage)
  944. {
  945. struct target_rusage *target_rusage;
  946. if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
  947. return -TARGET_EFAULT;
  948. target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
  949. target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
  950. target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
  951. target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
  952. target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
  953. target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
  954. target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
  955. target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
  956. target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
  957. target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
  958. target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
  959. target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
  960. target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
  961. target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
  962. target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
  963. target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
  964. target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
  965. target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
  966. unlock_user_struct(target_rusage, target_addr, 1);
  967. return 0;
  968. }
  969. static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
  970. {
  971. abi_ulong target_rlim_swap;
  972. rlim_t result;
  973. target_rlim_swap = tswapal(target_rlim);
  974. if (target_rlim_swap == TARGET_RLIM_INFINITY)
  975. return RLIM_INFINITY;
  976. result = target_rlim_swap;
  977. if (target_rlim_swap != (rlim_t)result)
  978. return RLIM_INFINITY;
  979. return result;
  980. }
  981. static inline abi_ulong host_to_target_rlim(rlim_t rlim)
  982. {
  983. abi_ulong target_rlim_swap;
  984. abi_ulong result;
  985. if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
  986. target_rlim_swap = TARGET_RLIM_INFINITY;
  987. else
  988. target_rlim_swap = rlim;
  989. result = tswapal(target_rlim_swap);
  990. return result;
  991. }
  992. static inline int target_to_host_resource(int code)
  993. {
  994. switch (code) {
  995. case TARGET_RLIMIT_AS:
  996. return RLIMIT_AS;
  997. case TARGET_RLIMIT_CORE:
  998. return RLIMIT_CORE;
  999. case TARGET_RLIMIT_CPU:
  1000. return RLIMIT_CPU;
  1001. case TARGET_RLIMIT_DATA:
  1002. return RLIMIT_DATA;
  1003. case TARGET_RLIMIT_FSIZE:
  1004. return RLIMIT_FSIZE;
  1005. case TARGET_RLIMIT_LOCKS:
  1006. return RLIMIT_LOCKS;
  1007. case TARGET_RLIMIT_MEMLOCK:
  1008. return RLIMIT_MEMLOCK;
  1009. case TARGET_RLIMIT_MSGQUEUE:
  1010. return RLIMIT_MSGQUEUE;
  1011. case TARGET_RLIMIT_NICE:
  1012. return RLIMIT_NICE;
  1013. case TARGET_RLIMIT_NOFILE:
  1014. return RLIMIT_NOFILE;
  1015. case TARGET_RLIMIT_NPROC:
  1016. return RLIMIT_NPROC;
  1017. case TARGET_RLIMIT_RSS:
  1018. return RLIMIT_RSS;
  1019. case TARGET_RLIMIT_RTPRIO:
  1020. return RLIMIT_RTPRIO;
  1021. case TARGET_RLIMIT_SIGPENDING:
  1022. return RLIMIT_SIGPENDING;
  1023. case TARGET_RLIMIT_STACK:
  1024. return RLIMIT_STACK;
  1025. default:
  1026. return code;
  1027. }
  1028. }
  1029. static inline abi_long copy_from_user_timeval(struct timeval *tv,
  1030. abi_ulong target_tv_addr)
  1031. {
  1032. struct target_timeval *target_tv;
  1033. if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) {
  1034. return -TARGET_EFAULT;
  1035. }
  1036. __get_user(tv->tv_sec, &target_tv->tv_sec);
  1037. __get_user(tv->tv_usec, &target_tv->tv_usec);
  1038. unlock_user_struct(target_tv, target_tv_addr, 0);
  1039. return 0;
  1040. }
  1041. static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
  1042. const struct timeval *tv)
  1043. {
  1044. struct target_timeval *target_tv;
  1045. if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
  1046. return -TARGET_EFAULT;
  1047. }
  1048. __put_user(tv->tv_sec, &target_tv->tv_sec);
  1049. __put_user(tv->tv_usec, &target_tv->tv_usec);
  1050. unlock_user_struct(target_tv, target_tv_addr, 1);
  1051. return 0;
  1052. }
  1053. static inline abi_long copy_to_user_timeval64(abi_ulong target_tv_addr,
  1054. const struct timeval *tv)
  1055. {
  1056. struct target__kernel_sock_timeval *target_tv;
  1057. if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
  1058. return -TARGET_EFAULT;
  1059. }
  1060. __put_user(tv->tv_sec, &target_tv->tv_sec);
  1061. __put_user(tv->tv_usec, &target_tv->tv_usec);
  1062. unlock_user_struct(target_tv, target_tv_addr, 1);
  1063. return 0;
  1064. }
  1065. static inline abi_long target_to_host_timespec(struct timespec *host_ts,
  1066. abi_ulong target_addr)
  1067. {
  1068. struct target_timespec *target_ts;
  1069. if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
  1070. return -TARGET_EFAULT;
  1071. }
  1072. __get_user(host_ts->tv_sec, &target_ts->tv_sec);
  1073. __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1074. unlock_user_struct(target_ts, target_addr, 0);
  1075. return 0;
  1076. }
  1077. static inline abi_long host_to_target_timespec(abi_ulong target_addr,
  1078. struct timespec *host_ts)
  1079. {
  1080. struct target_timespec *target_ts;
  1081. if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
  1082. return -TARGET_EFAULT;
  1083. }
  1084. __put_user(host_ts->tv_sec, &target_ts->tv_sec);
  1085. __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1086. unlock_user_struct(target_ts, target_addr, 1);
  1087. return 0;
  1088. }
  1089. static inline abi_long host_to_target_timespec64(abi_ulong target_addr,
  1090. struct timespec *host_ts)
  1091. {
  1092. struct target__kernel_timespec *target_ts;
  1093. if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
  1094. return -TARGET_EFAULT;
  1095. }
  1096. __put_user(host_ts->tv_sec, &target_ts->tv_sec);
  1097. __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1098. unlock_user_struct(target_ts, target_addr, 1);
  1099. return 0;
  1100. }
  1101. static inline abi_long copy_from_user_timezone(struct timezone *tz,
  1102. abi_ulong target_tz_addr)
  1103. {
  1104. struct target_timezone *target_tz;
  1105. if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) {
  1106. return -TARGET_EFAULT;
  1107. }
  1108. __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
  1109. __get_user(tz->tz_dsttime, &target_tz->tz_dsttime);
  1110. unlock_user_struct(target_tz, target_tz_addr, 0);
  1111. return 0;
  1112. }
  1113. #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
  1114. #include <mqueue.h>
  1115. static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
  1116. abi_ulong target_mq_attr_addr)
  1117. {
  1118. struct target_mq_attr *target_mq_attr;
  1119. if (!lock_user_struct(VERIFY_READ, target_mq_attr,
  1120. target_mq_attr_addr, 1))
  1121. return -TARGET_EFAULT;
  1122. __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
  1123. __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
  1124. __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
  1125. __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
  1126. unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
  1127. return 0;
  1128. }
  1129. static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
  1130. const struct mq_attr *attr)
  1131. {
  1132. struct target_mq_attr *target_mq_attr;
  1133. if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
  1134. target_mq_attr_addr, 0))
  1135. return -TARGET_EFAULT;
  1136. __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
  1137. __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
  1138. __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
  1139. __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
  1140. unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
  1141. return 0;
  1142. }
  1143. #endif
  1144. #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
  1145. /* do_select() must return target values and target errnos. */
  1146. static abi_long do_select(int n,
  1147. abi_ulong rfd_addr, abi_ulong wfd_addr,
  1148. abi_ulong efd_addr, abi_ulong target_tv_addr)
  1149. {
  1150. fd_set rfds, wfds, efds;
  1151. fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
  1152. struct timeval tv;
  1153. struct timespec ts, *ts_ptr;
  1154. abi_long ret;
  1155. ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
  1156. if (ret) {
  1157. return ret;
  1158. }
  1159. ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
  1160. if (ret) {
  1161. return ret;
  1162. }
  1163. ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
  1164. if (ret) {
  1165. return ret;
  1166. }
  1167. if (target_tv_addr) {
  1168. if (copy_from_user_timeval(&tv, target_tv_addr))
  1169. return -TARGET_EFAULT;
  1170. ts.tv_sec = tv.tv_sec;
  1171. ts.tv_nsec = tv.tv_usec * 1000;
  1172. ts_ptr = &ts;
  1173. } else {
  1174. ts_ptr = NULL;
  1175. }
  1176. ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
  1177. ts_ptr, NULL));
  1178. if (!is_error(ret)) {
  1179. if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
  1180. return -TARGET_EFAULT;
  1181. if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
  1182. return -TARGET_EFAULT;
  1183. if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
  1184. return -TARGET_EFAULT;
  1185. if (target_tv_addr) {
  1186. tv.tv_sec = ts.tv_sec;
  1187. tv.tv_usec = ts.tv_nsec / 1000;
  1188. if (copy_to_user_timeval(target_tv_addr, &tv)) {
  1189. return -TARGET_EFAULT;
  1190. }
  1191. }
  1192. }
  1193. return ret;
  1194. }
  1195. #if defined(TARGET_WANT_OLD_SYS_SELECT)
  1196. static abi_long do_old_select(abi_ulong arg1)
  1197. {
  1198. struct target_sel_arg_struct *sel;
  1199. abi_ulong inp, outp, exp, tvp;
  1200. long nsel;
  1201. if (!lock_user_struct(VERIFY_READ, sel, arg1, 1)) {
  1202. return -TARGET_EFAULT;
  1203. }
  1204. nsel = tswapal(sel->n);
  1205. inp = tswapal(sel->inp);
  1206. outp = tswapal(sel->outp);
  1207. exp = tswapal(sel->exp);
  1208. tvp = tswapal(sel->tvp);
  1209. unlock_user_struct(sel, arg1, 0);
  1210. return do_select(nsel, inp, outp, exp, tvp);
  1211. }
  1212. #endif
  1213. #endif
  1214. static abi_long do_pipe2(int host_pipe[], int flags)
  1215. {
  1216. #ifdef CONFIG_PIPE2
  1217. return pipe2(host_pipe, flags);
  1218. #else
  1219. return -ENOSYS;
  1220. #endif
  1221. }
  1222. static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
  1223. int flags, int is_pipe2)
  1224. {
  1225. int host_pipe[2];
  1226. abi_long ret;
  1227. ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
  1228. if (is_error(ret))
  1229. return get_errno(ret);
  1230. /* Several targets have special calling conventions for the original
  1231. pipe syscall, but didn't replicate this into the pipe2 syscall. */
  1232. if (!is_pipe2) {
  1233. #if defined(TARGET_ALPHA)
  1234. ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
  1235. return host_pipe[0];
  1236. #elif defined(TARGET_MIPS)
  1237. ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
  1238. return host_pipe[0];
  1239. #elif defined(TARGET_SH4)
  1240. ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
  1241. return host_pipe[0];
  1242. #elif defined(TARGET_SPARC)
  1243. ((CPUSPARCState*)cpu_env)->regwptr[1] = host_pipe[1];
  1244. return host_pipe[0];
  1245. #endif
  1246. }
  1247. if (put_user_s32(host_pipe[0], pipedes)
  1248. || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
  1249. return -TARGET_EFAULT;
  1250. return get_errno(ret);
  1251. }
  1252. static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
  1253. abi_ulong target_addr,
  1254. socklen_t len)
  1255. {
  1256. struct target_ip_mreqn *target_smreqn;
  1257. target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
  1258. if (!target_smreqn)
  1259. return -TARGET_EFAULT;
  1260. mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
  1261. mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
  1262. if (len == sizeof(struct target_ip_mreqn))
  1263. mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
  1264. unlock_user(target_smreqn, target_addr, 0);
  1265. return 0;
  1266. }
  1267. static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr,
  1268. abi_ulong target_addr,
  1269. socklen_t len)
  1270. {
  1271. const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
  1272. sa_family_t sa_family;
  1273. struct target_sockaddr *target_saddr;
  1274. if (fd_trans_target_to_host_addr(fd)) {
  1275. return fd_trans_target_to_host_addr(fd)(addr, target_addr, len);
  1276. }
  1277. target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
  1278. if (!target_saddr)
  1279. return -TARGET_EFAULT;
  1280. sa_family = tswap16(target_saddr->sa_family);
  1281. /* Oops. The caller might send a incomplete sun_path; sun_path
  1282. * must be terminated by \0 (see the manual page), but
  1283. * unfortunately it is quite common to specify sockaddr_un
  1284. * length as "strlen(x->sun_path)" while it should be
  1285. * "strlen(...) + 1". We'll fix that here if needed.
  1286. * Linux kernel has a similar feature.
  1287. */
  1288. if (sa_family == AF_UNIX) {
  1289. if (len < unix_maxlen && len > 0) {
  1290. char *cp = (char*)target_saddr;
  1291. if ( cp[len-1] && !cp[len] )
  1292. len++;
  1293. }
  1294. if (len > unix_maxlen)
  1295. len = unix_maxlen;
  1296. }
  1297. memcpy(addr, target_saddr, len);
  1298. addr->sa_family = sa_family;
  1299. if (sa_family == AF_NETLINK) {
  1300. struct sockaddr_nl *nladdr;
  1301. nladdr = (struct sockaddr_nl *)addr;
  1302. nladdr->nl_pid = tswap32(nladdr->nl_pid);
  1303. nladdr->nl_groups = tswap32(nladdr->nl_groups);
  1304. } else if (sa_family == AF_PACKET) {
  1305. struct target_sockaddr_ll *lladdr;
  1306. lladdr = (struct target_sockaddr_ll *)addr;
  1307. lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex);
  1308. lladdr->sll_hatype = tswap16(lladdr->sll_hatype);
  1309. }
  1310. unlock_user(target_saddr, target_addr, 0);
  1311. return 0;
  1312. }
  1313. static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
  1314. struct sockaddr *addr,
  1315. socklen_t len)
  1316. {
  1317. struct target_sockaddr *target_saddr;
  1318. if (len == 0) {
  1319. return 0;
  1320. }
  1321. assert(addr);
  1322. target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
  1323. if (!target_saddr)
  1324. return -TARGET_EFAULT;
  1325. memcpy(target_saddr, addr, len);
  1326. if (len >= offsetof(struct target_sockaddr, sa_family) +
  1327. sizeof(target_saddr->sa_family)) {
  1328. target_saddr->sa_family = tswap16(addr->sa_family);
  1329. }
  1330. if (addr->sa_family == AF_NETLINK &&
  1331. len >= sizeof(struct target_sockaddr_nl)) {
  1332. struct target_sockaddr_nl *target_nl =
  1333. (struct target_sockaddr_nl *)target_saddr;
  1334. target_nl->nl_pid = tswap32(target_nl->nl_pid);
  1335. target_nl->nl_groups = tswap32(target_nl->nl_groups);
  1336. } else if (addr->sa_family == AF_PACKET) {
  1337. struct sockaddr_ll *target_ll = (struct sockaddr_ll *)target_saddr;
  1338. target_ll->sll_ifindex = tswap32(target_ll->sll_ifindex);
  1339. target_ll->sll_hatype = tswap16(target_ll->sll_hatype);
  1340. } else if (addr->sa_family == AF_INET6 &&
  1341. len >= sizeof(struct target_sockaddr_in6)) {
  1342. struct target_sockaddr_in6 *target_in6 =
  1343. (struct target_sockaddr_in6 *)target_saddr;
  1344. target_in6->sin6_scope_id = tswap16(target_in6->sin6_scope_id);
  1345. }
  1346. unlock_user(target_saddr, target_addr, len);
  1347. return 0;
  1348. }
  1349. static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
  1350. struct target_msghdr *target_msgh)
  1351. {
  1352. struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
  1353. abi_long msg_controllen;
  1354. abi_ulong target_cmsg_addr;
  1355. struct target_cmsghdr *target_cmsg, *target_cmsg_start;
  1356. socklen_t space = 0;
  1357. msg_controllen = tswapal(target_msgh->msg_controllen);
  1358. if (msg_controllen < sizeof (struct target_cmsghdr))
  1359. goto the_end;
  1360. target_cmsg_addr = tswapal(target_msgh->msg_control);
  1361. target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
  1362. target_cmsg_start = target_cmsg;
  1363. if (!target_cmsg)
  1364. return -TARGET_EFAULT;
  1365. while (cmsg && target_cmsg) {
  1366. void *data = CMSG_DATA(cmsg);
  1367. void *target_data = TARGET_CMSG_DATA(target_cmsg);
  1368. int len = tswapal(target_cmsg->cmsg_len)
  1369. - sizeof(struct target_cmsghdr);
  1370. space += CMSG_SPACE(len);
  1371. if (space > msgh->msg_controllen) {
  1372. space -= CMSG_SPACE(len);
  1373. /* This is a QEMU bug, since we allocated the payload
  1374. * area ourselves (unlike overflow in host-to-target
  1375. * conversion, which is just the guest giving us a buffer
  1376. * that's too small). It can't happen for the payload types
  1377. * we currently support; if it becomes an issue in future
  1378. * we would need to improve our allocation strategy to
  1379. * something more intelligent than "twice the size of the
  1380. * target buffer we're reading from".
  1381. */
  1382. gemu_log("Host cmsg overflow\n");
  1383. break;
  1384. }
  1385. if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
  1386. cmsg->cmsg_level = SOL_SOCKET;
  1387. } else {
  1388. cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
  1389. }
  1390. cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
  1391. cmsg->cmsg_len = CMSG_LEN(len);
  1392. if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
  1393. int *fd = (int *)data;
  1394. int *target_fd = (int *)target_data;
  1395. int i, numfds = len / sizeof(int);
  1396. for (i = 0; i < numfds; i++) {
  1397. __get_user(fd[i], target_fd + i);
  1398. }
  1399. } else if (cmsg->cmsg_level == SOL_SOCKET
  1400. && cmsg->cmsg_type == SCM_CREDENTIALS) {
  1401. struct ucred *cred = (struct ucred *)data;
  1402. struct target_ucred *target_cred =
  1403. (struct target_ucred *)target_data;
  1404. __get_user(cred->pid, &target_cred->pid);
  1405. __get_user(cred->uid, &target_cred->uid);
  1406. __get_user(cred->gid, &target_cred->gid);
  1407. } else {
  1408. gemu_log("Unsupported ancillary data: %d/%d\n",
  1409. cmsg->cmsg_level, cmsg->cmsg_type);
  1410. memcpy(data, target_data, len);
  1411. }
  1412. cmsg = CMSG_NXTHDR(msgh, cmsg);
  1413. target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
  1414. target_cmsg_start);
  1415. }
  1416. unlock_user(target_cmsg, target_cmsg_addr, 0);
  1417. the_end:
  1418. msgh->msg_controllen = space;
  1419. return 0;
  1420. }
  1421. static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
  1422. struct msghdr *msgh)
  1423. {
  1424. struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
  1425. abi_long msg_controllen;
  1426. abi_ulong target_cmsg_addr;
  1427. struct target_cmsghdr *target_cmsg, *target_cmsg_start;
  1428. socklen_t space = 0;
  1429. msg_controllen = tswapal(target_msgh->msg_controllen);
  1430. if (msg_controllen < sizeof (struct target_cmsghdr))
  1431. goto the_end;
  1432. target_cmsg_addr = tswapal(target_msgh->msg_control);
  1433. target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
  1434. target_cmsg_start = target_cmsg;
  1435. if (!target_cmsg)
  1436. return -TARGET_EFAULT;
  1437. while (cmsg && target_cmsg) {
  1438. void *data = CMSG_DATA(cmsg);
  1439. void *target_data = TARGET_CMSG_DATA(target_cmsg);
  1440. int len = cmsg->cmsg_len - sizeof(struct cmsghdr);
  1441. int tgt_len, tgt_space;
  1442. /* We never copy a half-header but may copy half-data;
  1443. * this is Linux's behaviour in put_cmsg(). Note that
  1444. * truncation here is a guest problem (which we report
  1445. * to the guest via the CTRUNC bit), unlike truncation
  1446. * in target_to_host_cmsg, which is a QEMU bug.
  1447. */
  1448. if (msg_controllen < sizeof(struct target_cmsghdr)) {
  1449. target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
  1450. break;
  1451. }
  1452. if (cmsg->cmsg_level == SOL_SOCKET) {
  1453. target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
  1454. } else {
  1455. target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
  1456. }
  1457. target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
  1458. /* Payload types which need a different size of payload on
  1459. * the target must adjust tgt_len here.
  1460. */
  1461. tgt_len = len;
  1462. switch (cmsg->cmsg_level) {
  1463. case SOL_SOCKET:
  1464. switch (cmsg->cmsg_type) {
  1465. case SO_TIMESTAMP:
  1466. tgt_len = sizeof(struct target_timeval);
  1467. break;
  1468. default:
  1469. break;
  1470. }
  1471. break;
  1472. default:
  1473. break;
  1474. }
  1475. if (msg_controllen < TARGET_CMSG_LEN(tgt_len)) {
  1476. target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
  1477. tgt_len = msg_controllen - sizeof(struct target_cmsghdr);
  1478. }
  1479. /* We must now copy-and-convert len bytes of payload
  1480. * into tgt_len bytes of destination space. Bear in mind
  1481. * that in both source and destination we may be dealing
  1482. * with a truncated value!
  1483. */
  1484. switch (cmsg->cmsg_level) {
  1485. case SOL_SOCKET:
  1486. switch (cmsg->cmsg_type) {
  1487. case SCM_RIGHTS:
  1488. {
  1489. int *fd = (int *)data;
  1490. int *target_fd = (int *)target_data;
  1491. int i, numfds = tgt_len / sizeof(int);
  1492. for (i = 0; i < numfds; i++) {
  1493. __put_user(fd[i], target_fd + i);
  1494. }
  1495. break;
  1496. }
  1497. case SO_TIMESTAMP:
  1498. {
  1499. struct timeval *tv = (struct timeval *)data;
  1500. struct target_timeval *target_tv =
  1501. (struct target_timeval *)target_data;
  1502. if (len != sizeof(struct timeval) ||
  1503. tgt_len != sizeof(struct target_timeval)) {
  1504. goto unimplemented;
  1505. }
  1506. /* copy struct timeval to target */
  1507. __put_user(tv->tv_sec, &target_tv->tv_sec);
  1508. __put_user(tv->tv_usec, &target_tv->tv_usec);
  1509. break;
  1510. }
  1511. case SCM_CREDENTIALS:
  1512. {
  1513. struct ucred *cred = (struct ucred *)data;
  1514. struct target_ucred *target_cred =
  1515. (struct target_ucred *)target_data;
  1516. __put_user(cred->pid, &target_cred->pid);
  1517. __put_user(cred->uid, &target_cred->uid);
  1518. __put_user(cred->gid, &target_cred->gid);
  1519. break;
  1520. }
  1521. default:
  1522. goto unimplemented;
  1523. }
  1524. break;
  1525. case SOL_IP:
  1526. switch (cmsg->cmsg_type) {
  1527. case IP_TTL:
  1528. {
  1529. uint32_t *v = (uint32_t *)data;
  1530. uint32_t *t_int = (uint32_t *)target_data;
  1531. if (len != sizeof(uint32_t) ||
  1532. tgt_len != sizeof(uint32_t)) {
  1533. goto unimplemented;
  1534. }
  1535. __put_user(*v, t_int);
  1536. break;
  1537. }
  1538. case IP_RECVERR:
  1539. {
  1540. struct errhdr_t {
  1541. struct sock_extended_err ee;
  1542. struct sockaddr_in offender;
  1543. };
  1544. struct errhdr_t *errh = (struct errhdr_t *)data;
  1545. struct errhdr_t *target_errh =
  1546. (struct errhdr_t *)target_data;
  1547. if (len != sizeof(struct errhdr_t) ||
  1548. tgt_len != sizeof(struct errhdr_t)) {
  1549. goto unimplemented;
  1550. }
  1551. __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
  1552. __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
  1553. __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
  1554. __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
  1555. __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
  1556. __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
  1557. __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
  1558. host_to_target_sockaddr((unsigned long) &target_errh->offender,
  1559. (void *) &errh->offender, sizeof(errh->offender));
  1560. break;
  1561. }
  1562. default:
  1563. goto unimplemented;
  1564. }
  1565. break;
  1566. case SOL_IPV6:
  1567. switch (cmsg->cmsg_type) {
  1568. case IPV6_HOPLIMIT:
  1569. {
  1570. uint32_t *v = (uint32_t *)data;
  1571. uint32_t *t_int = (uint32_t *)target_data;
  1572. if (len != sizeof(uint32_t) ||
  1573. tgt_len != sizeof(uint32_t)) {
  1574. goto unimplemented;
  1575. }
  1576. __put_user(*v, t_int);
  1577. break;
  1578. }
  1579. case IPV6_RECVERR:
  1580. {
  1581. struct errhdr6_t {
  1582. struct sock_extended_err ee;
  1583. struct sockaddr_in6 offender;
  1584. };
  1585. struct errhdr6_t *errh = (struct errhdr6_t *)data;
  1586. struct errhdr6_t *target_errh =
  1587. (struct errhdr6_t *)target_data;
  1588. if (len != sizeof(struct errhdr6_t) ||
  1589. tgt_len != sizeof(struct errhdr6_t)) {
  1590. goto unimplemented;
  1591. }
  1592. __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
  1593. __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
  1594. __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
  1595. __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
  1596. __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
  1597. __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
  1598. __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
  1599. host_to_target_sockaddr((unsigned long) &target_errh->offender,
  1600. (void *) &errh->offender, sizeof(errh->offender));
  1601. break;
  1602. }
  1603. default:
  1604. goto unimplemented;
  1605. }
  1606. break;
  1607. default:
  1608. unimplemented:
  1609. gemu_log("Unsupported ancillary data: %d/%d\n",
  1610. cmsg->cmsg_level, cmsg->cmsg_type);
  1611. memcpy(target_data, data, MIN(len, tgt_len));
  1612. if (tgt_len > len) {
  1613. memset(target_data + len, 0, tgt_len - len);
  1614. }
  1615. }
  1616. target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(tgt_len));
  1617. tgt_space = TARGET_CMSG_SPACE(tgt_len);
  1618. if (msg_controllen < tgt_space) {
  1619. tgt_space = msg_controllen;
  1620. }
  1621. msg_controllen -= tgt_space;
  1622. space += tgt_space;
  1623. cmsg = CMSG_NXTHDR(msgh, cmsg);
  1624. target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
  1625. target_cmsg_start);
  1626. }
  1627. unlock_user(target_cmsg, target_cmsg_addr, space);
  1628. the_end:
  1629. target_msgh->msg_controllen = tswapal(space);
  1630. return 0;
  1631. }
  1632. /* do_setsockopt() Must return target values and target errnos. */
  1633. static abi_long do_setsockopt(int sockfd, int level, int optname,
  1634. abi_ulong optval_addr, socklen_t optlen)
  1635. {
  1636. abi_long ret;
  1637. int val;
  1638. struct ip_mreqn *ip_mreq;
  1639. struct ip_mreq_source *ip_mreq_source;
  1640. switch(level) {
  1641. case SOL_TCP:
  1642. /* TCP options all take an 'int' value. */
  1643. if (optlen < sizeof(uint32_t))
  1644. return -TARGET_EINVAL;
  1645. if (get_user_u32(val, optval_addr))
  1646. return -TARGET_EFAULT;
  1647. ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
  1648. break;
  1649. case SOL_IP:
  1650. switch(optname) {
  1651. case IP_TOS:
  1652. case IP_TTL:
  1653. case IP_HDRINCL:
  1654. case IP_ROUTER_ALERT:
  1655. case IP_RECVOPTS:
  1656. case IP_RETOPTS:
  1657. case IP_PKTINFO:
  1658. case IP_MTU_DISCOVER:
  1659. case IP_RECVERR:
  1660. case IP_RECVTTL:
  1661. case IP_RECVTOS:
  1662. #ifdef IP_FREEBIND
  1663. case IP_FREEBIND:
  1664. #endif
  1665. case IP_MULTICAST_TTL:
  1666. case IP_MULTICAST_LOOP:
  1667. val = 0;
  1668. if (optlen >= sizeof(uint32_t)) {
  1669. if (get_user_u32(val, optval_addr))
  1670. return -TARGET_EFAULT;
  1671. } else if (optlen >= 1) {
  1672. if (get_user_u8(val, optval_addr))
  1673. return -TARGET_EFAULT;
  1674. }
  1675. ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
  1676. break;
  1677. case IP_ADD_MEMBERSHIP:
  1678. case IP_DROP_MEMBERSHIP:
  1679. if (optlen < sizeof (struct target_ip_mreq) ||
  1680. optlen > sizeof (struct target_ip_mreqn))
  1681. return -TARGET_EINVAL;
  1682. ip_mreq = (struct ip_mreqn *) alloca(optlen);
  1683. target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
  1684. ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
  1685. break;
  1686. case IP_BLOCK_SOURCE:
  1687. case IP_UNBLOCK_SOURCE:
  1688. case IP_ADD_SOURCE_MEMBERSHIP:
  1689. case IP_DROP_SOURCE_MEMBERSHIP:
  1690. if (optlen != sizeof (struct target_ip_mreq_source))
  1691. return -TARGET_EINVAL;
  1692. ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
  1693. ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
  1694. unlock_user (ip_mreq_source, optval_addr, 0);
  1695. break;
  1696. default:
  1697. goto unimplemented;
  1698. }
  1699. break;
  1700. case SOL_IPV6:
  1701. switch (optname) {
  1702. case IPV6_MTU_DISCOVER:
  1703. case IPV6_MTU:
  1704. case IPV6_V6ONLY:
  1705. case IPV6_RECVPKTINFO:
  1706. case IPV6_UNICAST_HOPS:
  1707. case IPV6_MULTICAST_HOPS:
  1708. case IPV6_MULTICAST_LOOP:
  1709. case IPV6_RECVERR:
  1710. case IPV6_RECVHOPLIMIT:
  1711. case IPV6_2292HOPLIMIT:
  1712. case IPV6_CHECKSUM:
  1713. case IPV6_ADDRFORM:
  1714. case IPV6_2292PKTINFO:
  1715. case IPV6_RECVTCLASS:
  1716. case IPV6_RECVRTHDR:
  1717. case IPV6_2292RTHDR:
  1718. case IPV6_RECVHOPOPTS:
  1719. case IPV6_2292HOPOPTS:
  1720. case IPV6_RECVDSTOPTS:
  1721. case IPV6_2292DSTOPTS:
  1722. case IPV6_TCLASS:
  1723. #ifdef IPV6_RECVPATHMTU
  1724. case IPV6_RECVPATHMTU:
  1725. #endif
  1726. #ifdef IPV6_TRANSPARENT
  1727. case IPV6_TRANSPARENT:
  1728. #endif
  1729. #ifdef IPV6_FREEBIND
  1730. case IPV6_FREEBIND:
  1731. #endif
  1732. #ifdef IPV6_RECVORIGDSTADDR
  1733. case IPV6_RECVORIGDSTADDR:
  1734. #endif
  1735. val = 0;
  1736. if (optlen < sizeof(uint32_t)) {
  1737. return -TARGET_EINVAL;
  1738. }
  1739. if (get_user_u32(val, optval_addr)) {
  1740. return -TARGET_EFAULT;
  1741. }
  1742. ret = get_errno(setsockopt(sockfd, level, optname,
  1743. &val, sizeof(val)));
  1744. break;
  1745. case IPV6_PKTINFO:
  1746. {
  1747. struct in6_pktinfo pki;
  1748. if (optlen < sizeof(pki)) {
  1749. return -TARGET_EINVAL;
  1750. }
  1751. if (copy_from_user(&pki, optval_addr, sizeof(pki))) {
  1752. return -TARGET_EFAULT;
  1753. }
  1754. pki.ipi6_ifindex = tswap32(pki.ipi6_ifindex);
  1755. ret = get_errno(setsockopt(sockfd, level, optname,
  1756. &pki, sizeof(pki)));
  1757. break;
  1758. }
  1759. case IPV6_ADD_MEMBERSHIP:
  1760. case IPV6_DROP_MEMBERSHIP:
  1761. {
  1762. struct ipv6_mreq ipv6mreq;
  1763. if (optlen < sizeof(ipv6mreq)) {
  1764. return -TARGET_EINVAL;
  1765. }
  1766. if (copy_from_user(&ipv6mreq, optval_addr, sizeof(ipv6mreq))) {
  1767. return -TARGET_EFAULT;
  1768. }
  1769. ipv6mreq.ipv6mr_interface = tswap32(ipv6mreq.ipv6mr_interface);
  1770. ret = get_errno(setsockopt(sockfd, level, optname,
  1771. &ipv6mreq, sizeof(ipv6mreq)));
  1772. break;
  1773. }
  1774. default:
  1775. goto unimplemented;
  1776. }
  1777. break;
  1778. case SOL_ICMPV6:
  1779. switch (optname) {
  1780. case ICMPV6_FILTER:
  1781. {
  1782. struct icmp6_filter icmp6f;
  1783. if (optlen > sizeof(icmp6f)) {
  1784. optlen = sizeof(icmp6f);
  1785. }
  1786. if (copy_from_user(&icmp6f, optval_addr, optlen)) {
  1787. return -TARGET_EFAULT;
  1788. }
  1789. for (val = 0; val < 8; val++) {
  1790. icmp6f.data[val] = tswap32(icmp6f.data[val]);
  1791. }
  1792. ret = get_errno(setsockopt(sockfd, level, optname,
  1793. &icmp6f, optlen));
  1794. break;
  1795. }
  1796. default:
  1797. goto unimplemented;
  1798. }
  1799. break;
  1800. case SOL_RAW:
  1801. switch (optname) {
  1802. case ICMP_FILTER:
  1803. case IPV6_CHECKSUM:
  1804. /* those take an u32 value */
  1805. if (optlen < sizeof(uint32_t)) {
  1806. return -TARGET_EINVAL;
  1807. }
  1808. if (get_user_u32(val, optval_addr)) {
  1809. return -TARGET_EFAULT;
  1810. }
  1811. ret = get_errno(setsockopt(sockfd, level, optname,
  1812. &val, sizeof(val)));
  1813. break;
  1814. default:
  1815. goto unimplemented;
  1816. }
  1817. break;
  1818. #if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE)
  1819. case SOL_ALG:
  1820. switch (optname) {
  1821. case ALG_SET_KEY:
  1822. {
  1823. char *alg_key = g_malloc(optlen);
  1824. if (!alg_key) {
  1825. return -TARGET_ENOMEM;
  1826. }
  1827. if (copy_from_user(alg_key, optval_addr, optlen)) {
  1828. g_free(alg_key);
  1829. return -TARGET_EFAULT;
  1830. }
  1831. ret = get_errno(setsockopt(sockfd, level, optname,
  1832. alg_key, optlen));
  1833. g_free(alg_key);
  1834. break;
  1835. }
  1836. case ALG_SET_AEAD_AUTHSIZE:
  1837. {
  1838. ret = get_errno(setsockopt(sockfd, level, optname,
  1839. NULL, optlen));
  1840. break;
  1841. }
  1842. default:
  1843. goto unimplemented;
  1844. }
  1845. break;
  1846. #endif
  1847. case TARGET_SOL_SOCKET:
  1848. switch (optname) {
  1849. case TARGET_SO_RCVTIMEO:
  1850. {
  1851. struct timeval tv;
  1852. optname = SO_RCVTIMEO;
  1853. set_timeout:
  1854. if (optlen != sizeof(struct target_timeval)) {
  1855. return -TARGET_EINVAL;
  1856. }
  1857. if (copy_from_user_timeval(&tv, optval_addr)) {
  1858. return -TARGET_EFAULT;
  1859. }
  1860. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
  1861. &tv, sizeof(tv)));
  1862. return ret;
  1863. }
  1864. case TARGET_SO_SNDTIMEO:
  1865. optname = SO_SNDTIMEO;
  1866. goto set_timeout;
  1867. case TARGET_SO_ATTACH_FILTER:
  1868. {
  1869. struct target_sock_fprog *tfprog;
  1870. struct target_sock_filter *tfilter;
  1871. struct sock_fprog fprog;
  1872. struct sock_filter *filter;
  1873. int i;
  1874. if (optlen != sizeof(*tfprog)) {
  1875. return -TARGET_EINVAL;
  1876. }
  1877. if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
  1878. return -TARGET_EFAULT;
  1879. }
  1880. if (!lock_user_struct(VERIFY_READ, tfilter,
  1881. tswapal(tfprog->filter), 0)) {
  1882. unlock_user_struct(tfprog, optval_addr, 1);
  1883. return -TARGET_EFAULT;
  1884. }
  1885. fprog.len = tswap16(tfprog->len);
  1886. filter = g_try_new(struct sock_filter, fprog.len);
  1887. if (filter == NULL) {
  1888. unlock_user_struct(tfilter, tfprog->filter, 1);
  1889. unlock_user_struct(tfprog, optval_addr, 1);
  1890. return -TARGET_ENOMEM;
  1891. }
  1892. for (i = 0; i < fprog.len; i++) {
  1893. filter[i].code = tswap16(tfilter[i].code);
  1894. filter[i].jt = tfilter[i].jt;
  1895. filter[i].jf = tfilter[i].jf;
  1896. filter[i].k = tswap32(tfilter[i].k);
  1897. }
  1898. fprog.filter = filter;
  1899. ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
  1900. SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
  1901. g_free(filter);
  1902. unlock_user_struct(tfilter, tfprog->filter, 1);
  1903. unlock_user_struct(tfprog, optval_addr, 1);
  1904. return ret;
  1905. }
  1906. case TARGET_SO_BINDTODEVICE:
  1907. {
  1908. char *dev_ifname, *addr_ifname;
  1909. if (optlen > IFNAMSIZ - 1) {
  1910. optlen = IFNAMSIZ - 1;
  1911. }
  1912. dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1);
  1913. if (!dev_ifname) {
  1914. return -TARGET_EFAULT;
  1915. }
  1916. optname = SO_BINDTODEVICE;
  1917. addr_ifname = alloca(IFNAMSIZ);
  1918. memcpy(addr_ifname, dev_ifname, optlen);
  1919. addr_ifname[optlen] = 0;
  1920. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
  1921. addr_ifname, optlen));
  1922. unlock_user (dev_ifname, optval_addr, 0);
  1923. return ret;
  1924. }
  1925. case TARGET_SO_LINGER:
  1926. {
  1927. struct linger lg;
  1928. struct target_linger *tlg;
  1929. if (optlen != sizeof(struct target_linger)) {
  1930. return -TARGET_EINVAL;
  1931. }
  1932. if (!lock_user_struct(VERIFY_READ, tlg, optval_addr, 1)) {
  1933. return -TARGET_EFAULT;
  1934. }
  1935. __get_user(lg.l_onoff, &tlg->l_onoff);
  1936. __get_user(lg.l_linger, &tlg->l_linger);
  1937. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, SO_LINGER,
  1938. &lg, sizeof(lg)));
  1939. unlock_user_struct(tlg, optval_addr, 0);
  1940. return ret;
  1941. }
  1942. /* Options with 'int' argument. */
  1943. case TARGET_SO_DEBUG:
  1944. optname = SO_DEBUG;
  1945. break;
  1946. case TARGET_SO_REUSEADDR:
  1947. optname = SO_REUSEADDR;
  1948. break;
  1949. #ifdef SO_REUSEPORT
  1950. case TARGET_SO_REUSEPORT:
  1951. optname = SO_REUSEPORT;
  1952. break;
  1953. #endif
  1954. case TARGET_SO_TYPE:
  1955. optname = SO_TYPE;
  1956. break;
  1957. case TARGET_SO_ERROR:
  1958. optname = SO_ERROR;
  1959. break;
  1960. case TARGET_SO_DONTROUTE:
  1961. optname = SO_DONTROUTE;
  1962. break;
  1963. case TARGET_SO_BROADCAST:
  1964. optname = SO_BROADCAST;
  1965. break;
  1966. case TARGET_SO_SNDBUF:
  1967. optname = SO_SNDBUF;
  1968. break;
  1969. case TARGET_SO_SNDBUFFORCE:
  1970. optname = SO_SNDBUFFORCE;
  1971. break;
  1972. case TARGET_SO_RCVBUF:
  1973. optname = SO_RCVBUF;
  1974. break;
  1975. case TARGET_SO_RCVBUFFORCE:
  1976. optname = SO_RCVBUFFORCE;
  1977. break;
  1978. case TARGET_SO_KEEPALIVE:
  1979. optname = SO_KEEPALIVE;
  1980. break;
  1981. case TARGET_SO_OOBINLINE:
  1982. optname = SO_OOBINLINE;
  1983. break;
  1984. case TARGET_SO_NO_CHECK:
  1985. optname = SO_NO_CHECK;
  1986. break;
  1987. case TARGET_SO_PRIORITY:
  1988. optname = SO_PRIORITY;
  1989. break;
  1990. #ifdef SO_BSDCOMPAT
  1991. case TARGET_SO_BSDCOMPAT:
  1992. optname = SO_BSDCOMPAT;
  1993. break;
  1994. #endif
  1995. case TARGET_SO_PASSCRED:
  1996. optname = SO_PASSCRED;
  1997. break;
  1998. case TARGET_SO_PASSSEC:
  1999. optname = SO_PASSSEC;
  2000. break;
  2001. case TARGET_SO_TIMESTAMP:
  2002. optname = SO_TIMESTAMP;
  2003. break;
  2004. case TARGET_SO_RCVLOWAT:
  2005. optname = SO_RCVLOWAT;
  2006. break;
  2007. default:
  2008. goto unimplemented;
  2009. }
  2010. if (optlen < sizeof(uint32_t))
  2011. return -TARGET_EINVAL;
  2012. if (get_user_u32(val, optval_addr))
  2013. return -TARGET_EFAULT;
  2014. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
  2015. break;
  2016. #ifdef SOL_NETLINK
  2017. case SOL_NETLINK:
  2018. switch (optname) {
  2019. case NETLINK_PKTINFO:
  2020. case NETLINK_ADD_MEMBERSHIP:
  2021. case NETLINK_DROP_MEMBERSHIP:
  2022. case NETLINK_BROADCAST_ERROR:
  2023. case NETLINK_NO_ENOBUFS:
  2024. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
  2025. case NETLINK_LISTEN_ALL_NSID:
  2026. case NETLINK_CAP_ACK:
  2027. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
  2028. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
  2029. case NETLINK_EXT_ACK:
  2030. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2031. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
  2032. case NETLINK_GET_STRICT_CHK:
  2033. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2034. break;
  2035. default:
  2036. goto unimplemented;
  2037. }
  2038. val = 0;
  2039. if (optlen < sizeof(uint32_t)) {
  2040. return -TARGET_EINVAL;
  2041. }
  2042. if (get_user_u32(val, optval_addr)) {
  2043. return -TARGET_EFAULT;
  2044. }
  2045. ret = get_errno(setsockopt(sockfd, SOL_NETLINK, optname, &val,
  2046. sizeof(val)));
  2047. break;
  2048. #endif /* SOL_NETLINK */
  2049. default:
  2050. unimplemented:
  2051. gemu_log("Unsupported setsockopt level=%d optname=%d\n", level, optname);
  2052. ret = -TARGET_ENOPROTOOPT;
  2053. }
  2054. return ret;
  2055. }
  2056. /* do_getsockopt() Must return target values and target errnos. */
  2057. static abi_long do_getsockopt(int sockfd, int level, int optname,
  2058. abi_ulong optval_addr, abi_ulong optlen)
  2059. {
  2060. abi_long ret;
  2061. int len, val;
  2062. socklen_t lv;
  2063. switch(level) {
  2064. case TARGET_SOL_SOCKET:
  2065. level = SOL_SOCKET;
  2066. switch (optname) {
  2067. /* These don't just return a single integer */
  2068. case TARGET_SO_RCVTIMEO:
  2069. case TARGET_SO_SNDTIMEO:
  2070. case TARGET_SO_PEERNAME:
  2071. goto unimplemented;
  2072. case TARGET_SO_PEERCRED: {
  2073. struct ucred cr;
  2074. socklen_t crlen;
  2075. struct target_ucred *tcr;
  2076. if (get_user_u32(len, optlen)) {
  2077. return -TARGET_EFAULT;
  2078. }
  2079. if (len < 0) {
  2080. return -TARGET_EINVAL;
  2081. }
  2082. crlen = sizeof(cr);
  2083. ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
  2084. &cr, &crlen));
  2085. if (ret < 0) {
  2086. return ret;
  2087. }
  2088. if (len > crlen) {
  2089. len = crlen;
  2090. }
  2091. if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
  2092. return -TARGET_EFAULT;
  2093. }
  2094. __put_user(cr.pid, &tcr->pid);
  2095. __put_user(cr.uid, &tcr->uid);
  2096. __put_user(cr.gid, &tcr->gid);
  2097. unlock_user_struct(tcr, optval_addr, 1);
  2098. if (put_user_u32(len, optlen)) {
  2099. return -TARGET_EFAULT;
  2100. }
  2101. break;
  2102. }
  2103. case TARGET_SO_LINGER:
  2104. {
  2105. struct linger lg;
  2106. socklen_t lglen;
  2107. struct target_linger *tlg;
  2108. if (get_user_u32(len, optlen)) {
  2109. return -TARGET_EFAULT;
  2110. }
  2111. if (len < 0) {
  2112. return -TARGET_EINVAL;
  2113. }
  2114. lglen = sizeof(lg);
  2115. ret = get_errno(getsockopt(sockfd, level, SO_LINGER,
  2116. &lg, &lglen));
  2117. if (ret < 0) {
  2118. return ret;
  2119. }
  2120. if (len > lglen) {
  2121. len = lglen;
  2122. }
  2123. if (!lock_user_struct(VERIFY_WRITE, tlg, optval_addr, 0)) {
  2124. return -TARGET_EFAULT;
  2125. }
  2126. __put_user(lg.l_onoff, &tlg->l_onoff);
  2127. __put_user(lg.l_linger, &tlg->l_linger);
  2128. unlock_user_struct(tlg, optval_addr, 1);
  2129. if (put_user_u32(len, optlen)) {
  2130. return -TARGET_EFAULT;
  2131. }
  2132. break;
  2133. }
  2134. /* Options with 'int' argument. */
  2135. case TARGET_SO_DEBUG:
  2136. optname = SO_DEBUG;
  2137. goto int_case;
  2138. case TARGET_SO_REUSEADDR:
  2139. optname = SO_REUSEADDR;
  2140. goto int_case;
  2141. #ifdef SO_REUSEPORT
  2142. case TARGET_SO_REUSEPORT:
  2143. optname = SO_REUSEPORT;
  2144. goto int_case;
  2145. #endif
  2146. case TARGET_SO_TYPE:
  2147. optname = SO_TYPE;
  2148. goto int_case;
  2149. case TARGET_SO_ERROR:
  2150. optname = SO_ERROR;
  2151. goto int_case;
  2152. case TARGET_SO_DONTROUTE:
  2153. optname = SO_DONTROUTE;
  2154. goto int_case;
  2155. case TARGET_SO_BROADCAST:
  2156. optname = SO_BROADCAST;
  2157. goto int_case;
  2158. case TARGET_SO_SNDBUF:
  2159. optname = SO_SNDBUF;
  2160. goto int_case;
  2161. case TARGET_SO_RCVBUF:
  2162. optname = SO_RCVBUF;
  2163. goto int_case;
  2164. case TARGET_SO_KEEPALIVE:
  2165. optname = SO_KEEPALIVE;
  2166. goto int_case;
  2167. case TARGET_SO_OOBINLINE:
  2168. optname = SO_OOBINLINE;
  2169. goto int_case;
  2170. case TARGET_SO_NO_CHECK:
  2171. optname = SO_NO_CHECK;
  2172. goto int_case;
  2173. case TARGET_SO_PRIORITY:
  2174. optname = SO_PRIORITY;
  2175. goto int_case;
  2176. #ifdef SO_BSDCOMPAT
  2177. case TARGET_SO_BSDCOMPAT:
  2178. optname = SO_BSDCOMPAT;
  2179. goto int_case;
  2180. #endif
  2181. case TARGET_SO_PASSCRED:
  2182. optname = SO_PASSCRED;
  2183. goto int_case;
  2184. case TARGET_SO_TIMESTAMP:
  2185. optname = SO_TIMESTAMP;
  2186. goto int_case;
  2187. case TARGET_SO_RCVLOWAT:
  2188. optname = SO_RCVLOWAT;
  2189. goto int_case;
  2190. case TARGET_SO_ACCEPTCONN:
  2191. optname = SO_ACCEPTCONN;
  2192. goto int_case;
  2193. default:
  2194. goto int_case;
  2195. }
  2196. break;
  2197. case SOL_TCP:
  2198. /* TCP options all take an 'int' value. */
  2199. int_case:
  2200. if (get_user_u32(len, optlen))
  2201. return -TARGET_EFAULT;
  2202. if (len < 0)
  2203. return -TARGET_EINVAL;
  2204. lv = sizeof(lv);
  2205. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2206. if (ret < 0)
  2207. return ret;
  2208. if (optname == SO_TYPE) {
  2209. val = host_to_target_sock_type(val);
  2210. }
  2211. if (len > lv)
  2212. len = lv;
  2213. if (len == 4) {
  2214. if (put_user_u32(val, optval_addr))
  2215. return -TARGET_EFAULT;
  2216. } else {
  2217. if (put_user_u8(val, optval_addr))
  2218. return -TARGET_EFAULT;
  2219. }
  2220. if (put_user_u32(len, optlen))
  2221. return -TARGET_EFAULT;
  2222. break;
  2223. case SOL_IP:
  2224. switch(optname) {
  2225. case IP_TOS:
  2226. case IP_TTL:
  2227. case IP_HDRINCL:
  2228. case IP_ROUTER_ALERT:
  2229. case IP_RECVOPTS:
  2230. case IP_RETOPTS:
  2231. case IP_PKTINFO:
  2232. case IP_MTU_DISCOVER:
  2233. case IP_RECVERR:
  2234. case IP_RECVTOS:
  2235. #ifdef IP_FREEBIND
  2236. case IP_FREEBIND:
  2237. #endif
  2238. case IP_MULTICAST_TTL:
  2239. case IP_MULTICAST_LOOP:
  2240. if (get_user_u32(len, optlen))
  2241. return -TARGET_EFAULT;
  2242. if (len < 0)
  2243. return -TARGET_EINVAL;
  2244. lv = sizeof(lv);
  2245. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2246. if (ret < 0)
  2247. return ret;
  2248. if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
  2249. len = 1;
  2250. if (put_user_u32(len, optlen)
  2251. || put_user_u8(val, optval_addr))
  2252. return -TARGET_EFAULT;
  2253. } else {
  2254. if (len > sizeof(int))
  2255. len = sizeof(int);
  2256. if (put_user_u32(len, optlen)
  2257. || put_user_u32(val, optval_addr))
  2258. return -TARGET_EFAULT;
  2259. }
  2260. break;
  2261. default:
  2262. ret = -TARGET_ENOPROTOOPT;
  2263. break;
  2264. }
  2265. break;
  2266. case SOL_IPV6:
  2267. switch (optname) {
  2268. case IPV6_MTU_DISCOVER:
  2269. case IPV6_MTU:
  2270. case IPV6_V6ONLY:
  2271. case IPV6_RECVPKTINFO:
  2272. case IPV6_UNICAST_HOPS:
  2273. case IPV6_MULTICAST_HOPS:
  2274. case IPV6_MULTICAST_LOOP:
  2275. case IPV6_RECVERR:
  2276. case IPV6_RECVHOPLIMIT:
  2277. case IPV6_2292HOPLIMIT:
  2278. case IPV6_CHECKSUM:
  2279. case IPV6_ADDRFORM:
  2280. case IPV6_2292PKTINFO:
  2281. case IPV6_RECVTCLASS:
  2282. case IPV6_RECVRTHDR:
  2283. case IPV6_2292RTHDR:
  2284. case IPV6_RECVHOPOPTS:
  2285. case IPV6_2292HOPOPTS:
  2286. case IPV6_RECVDSTOPTS:
  2287. case IPV6_2292DSTOPTS:
  2288. case IPV6_TCLASS:
  2289. #ifdef IPV6_RECVPATHMTU
  2290. case IPV6_RECVPATHMTU:
  2291. #endif
  2292. #ifdef IPV6_TRANSPARENT
  2293. case IPV6_TRANSPARENT:
  2294. #endif
  2295. #ifdef IPV6_FREEBIND
  2296. case IPV6_FREEBIND:
  2297. #endif
  2298. #ifdef IPV6_RECVORIGDSTADDR
  2299. case IPV6_RECVORIGDSTADDR:
  2300. #endif
  2301. if (get_user_u32(len, optlen))
  2302. return -TARGET_EFAULT;
  2303. if (len < 0)
  2304. return -TARGET_EINVAL;
  2305. lv = sizeof(lv);
  2306. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2307. if (ret < 0)
  2308. return ret;
  2309. if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
  2310. len = 1;
  2311. if (put_user_u32(len, optlen)
  2312. || put_user_u8(val, optval_addr))
  2313. return -TARGET_EFAULT;
  2314. } else {
  2315. if (len > sizeof(int))
  2316. len = sizeof(int);
  2317. if (put_user_u32(len, optlen)
  2318. || put_user_u32(val, optval_addr))
  2319. return -TARGET_EFAULT;
  2320. }
  2321. break;
  2322. default:
  2323. ret = -TARGET_ENOPROTOOPT;
  2324. break;
  2325. }
  2326. break;
  2327. #ifdef SOL_NETLINK
  2328. case SOL_NETLINK:
  2329. switch (optname) {
  2330. case NETLINK_PKTINFO:
  2331. case NETLINK_BROADCAST_ERROR:
  2332. case NETLINK_NO_ENOBUFS:
  2333. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
  2334. case NETLINK_LISTEN_ALL_NSID:
  2335. case NETLINK_CAP_ACK:
  2336. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
  2337. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
  2338. case NETLINK_EXT_ACK:
  2339. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2340. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
  2341. case NETLINK_GET_STRICT_CHK:
  2342. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2343. if (get_user_u32(len, optlen)) {
  2344. return -TARGET_EFAULT;
  2345. }
  2346. if (len != sizeof(val)) {
  2347. return -TARGET_EINVAL;
  2348. }
  2349. lv = len;
  2350. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2351. if (ret < 0) {
  2352. return ret;
  2353. }
  2354. if (put_user_u32(lv, optlen)
  2355. || put_user_u32(val, optval_addr)) {
  2356. return -TARGET_EFAULT;
  2357. }
  2358. break;
  2359. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
  2360. case NETLINK_LIST_MEMBERSHIPS:
  2361. {
  2362. uint32_t *results;
  2363. int i;
  2364. if (get_user_u32(len, optlen)) {
  2365. return -TARGET_EFAULT;
  2366. }
  2367. if (len < 0) {
  2368. return -TARGET_EINVAL;
  2369. }
  2370. results = lock_user(VERIFY_WRITE, optval_addr, len, 1);
  2371. if (!results) {
  2372. return -TARGET_EFAULT;
  2373. }
  2374. lv = len;
  2375. ret = get_errno(getsockopt(sockfd, level, optname, results, &lv));
  2376. if (ret < 0) {
  2377. unlock_user(results, optval_addr, 0);
  2378. return ret;
  2379. }
  2380. /* swap host endianess to target endianess. */
  2381. for (i = 0; i < (len / sizeof(uint32_t)); i++) {
  2382. results[i] = tswap32(results[i]);
  2383. }
  2384. if (put_user_u32(lv, optlen)) {
  2385. return -TARGET_EFAULT;
  2386. }
  2387. unlock_user(results, optval_addr, 0);
  2388. break;
  2389. }
  2390. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
  2391. default:
  2392. goto unimplemented;
  2393. }
  2394. break;
  2395. #endif /* SOL_NETLINK */
  2396. default:
  2397. unimplemented:
  2398. gemu_log("getsockopt level=%d optname=%d not yet supported\n",
  2399. level, optname);
  2400. ret = -TARGET_EOPNOTSUPP;
  2401. break;
  2402. }
  2403. return ret;
  2404. }
  2405. /* Convert target low/high pair representing file offset into the host
  2406. * low/high pair. This function doesn't handle offsets bigger than 64 bits
  2407. * as the kernel doesn't handle them either.
  2408. */
  2409. static void target_to_host_low_high(abi_ulong tlow,
  2410. abi_ulong thigh,
  2411. unsigned long *hlow,
  2412. unsigned long *hhigh)
  2413. {
  2414. uint64_t off = tlow |
  2415. ((unsigned long long)thigh << TARGET_LONG_BITS / 2) <<
  2416. TARGET_LONG_BITS / 2;
  2417. *hlow = off;
  2418. *hhigh = (off >> HOST_LONG_BITS / 2) >> HOST_LONG_BITS / 2;
  2419. }
  2420. static struct iovec *lock_iovec(int type, abi_ulong target_addr,
  2421. abi_ulong count, int copy)
  2422. {
  2423. struct target_iovec *target_vec;
  2424. struct iovec *vec;
  2425. abi_ulong total_len, max_len;
  2426. int i;
  2427. int err = 0;
  2428. bool bad_address = false;
  2429. if (count == 0) {
  2430. errno = 0;
  2431. return NULL;
  2432. }
  2433. if (count > IOV_MAX) {
  2434. errno = EINVAL;
  2435. return NULL;
  2436. }
  2437. vec = g_try_new0(struct iovec, count);
  2438. if (vec == NULL) {
  2439. errno = ENOMEM;
  2440. return NULL;
  2441. }
  2442. target_vec = lock_user(VERIFY_READ, target_addr,
  2443. count * sizeof(struct target_iovec), 1);
  2444. if (target_vec == NULL) {
  2445. err = EFAULT;
  2446. goto fail2;
  2447. }
  2448. /* ??? If host page size > target page size, this will result in a
  2449. value larger than what we can actually support. */
  2450. max_len = 0x7fffffff & TARGET_PAGE_MASK;
  2451. total_len = 0;
  2452. for (i = 0; i < count; i++) {
  2453. abi_ulong base = tswapal(target_vec[i].iov_base);
  2454. abi_long len = tswapal(target_vec[i].iov_len);
  2455. if (len < 0) {
  2456. err = EINVAL;
  2457. goto fail;
  2458. } else if (len == 0) {
  2459. /* Zero length pointer is ignored. */
  2460. vec[i].iov_base = 0;
  2461. } else {
  2462. vec[i].iov_base = lock_user(type, base, len, copy);
  2463. /* If the first buffer pointer is bad, this is a fault. But
  2464. * subsequent bad buffers will result in a partial write; this
  2465. * is realized by filling the vector with null pointers and
  2466. * zero lengths. */
  2467. if (!vec[i].iov_base) {
  2468. if (i == 0) {
  2469. err = EFAULT;
  2470. goto fail;
  2471. } else {
  2472. bad_address = true;
  2473. }
  2474. }
  2475. if (bad_address) {
  2476. len = 0;
  2477. }
  2478. if (len > max_len - total_len) {
  2479. len = max_len - total_len;
  2480. }
  2481. }
  2482. vec[i].iov_len = len;
  2483. total_len += len;
  2484. }
  2485. unlock_user(target_vec, target_addr, 0);
  2486. return vec;
  2487. fail:
  2488. while (--i >= 0) {
  2489. if (tswapal(target_vec[i].iov_len) > 0) {
  2490. unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0);
  2491. }
  2492. }
  2493. unlock_user(target_vec, target_addr, 0);
  2494. fail2:
  2495. g_free(vec);
  2496. errno = err;
  2497. return NULL;
  2498. }
  2499. static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
  2500. abi_ulong count, int copy)
  2501. {
  2502. struct target_iovec *target_vec;
  2503. int i;
  2504. target_vec = lock_user(VERIFY_READ, target_addr,
  2505. count * sizeof(struct target_iovec), 1);
  2506. if (target_vec) {
  2507. for (i = 0; i < count; i++) {
  2508. abi_ulong base = tswapal(target_vec[i].iov_base);
  2509. abi_long len = tswapal(target_vec[i].iov_len);
  2510. if (len < 0) {
  2511. break;
  2512. }
  2513. unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
  2514. }
  2515. unlock_user(target_vec, target_addr, 0);
  2516. }
  2517. g_free(vec);
  2518. }
  2519. static inline int target_to_host_sock_type(int *type)
  2520. {
  2521. int host_type = 0;
  2522. int target_type = *type;
  2523. switch (target_type & TARGET_SOCK_TYPE_MASK) {
  2524. case TARGET_SOCK_DGRAM:
  2525. host_type = SOCK_DGRAM;
  2526. break;
  2527. case TARGET_SOCK_STREAM:
  2528. host_type = SOCK_STREAM;
  2529. break;
  2530. default:
  2531. host_type = target_type & TARGET_SOCK_TYPE_MASK;
  2532. break;
  2533. }
  2534. if (target_type & TARGET_SOCK_CLOEXEC) {
  2535. #if defined(SOCK_CLOEXEC)
  2536. host_type |= SOCK_CLOEXEC;
  2537. #else
  2538. return -TARGET_EINVAL;
  2539. #endif
  2540. }
  2541. if (target_type & TARGET_SOCK_NONBLOCK) {
  2542. #if defined(SOCK_NONBLOCK)
  2543. host_type |= SOCK_NONBLOCK;
  2544. #elif !defined(O_NONBLOCK)
  2545. return -TARGET_EINVAL;
  2546. #endif
  2547. }
  2548. *type = host_type;
  2549. return 0;
  2550. }
  2551. /* Try to emulate socket type flags after socket creation. */
  2552. static int sock_flags_fixup(int fd, int target_type)
  2553. {
  2554. #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
  2555. if (target_type & TARGET_SOCK_NONBLOCK) {
  2556. int flags = fcntl(fd, F_GETFL);
  2557. if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) {
  2558. close(fd);
  2559. return -TARGET_EINVAL;
  2560. }
  2561. }
  2562. #endif
  2563. return fd;
  2564. }
  2565. /* do_socket() Must return target values and target errnos. */
  2566. static abi_long do_socket(int domain, int type, int protocol)
  2567. {
  2568. int target_type = type;
  2569. int ret;
  2570. ret = target_to_host_sock_type(&type);
  2571. if (ret) {
  2572. return ret;
  2573. }
  2574. if (domain == PF_NETLINK && !(
  2575. #ifdef CONFIG_RTNETLINK
  2576. protocol == NETLINK_ROUTE ||
  2577. #endif
  2578. protocol == NETLINK_KOBJECT_UEVENT ||
  2579. protocol == NETLINK_AUDIT)) {
  2580. return -EPFNOSUPPORT;
  2581. }
  2582. if (domain == AF_PACKET ||
  2583. (domain == AF_INET && type == SOCK_PACKET)) {
  2584. protocol = tswap16(protocol);
  2585. }
  2586. ret = get_errno(socket(domain, type, protocol));
  2587. if (ret >= 0) {
  2588. ret = sock_flags_fixup(ret, target_type);
  2589. if (type == SOCK_PACKET) {
  2590. /* Manage an obsolete case :
  2591. * if socket type is SOCK_PACKET, bind by name
  2592. */
  2593. fd_trans_register(ret, &target_packet_trans);
  2594. } else if (domain == PF_NETLINK) {
  2595. switch (protocol) {
  2596. #ifdef CONFIG_RTNETLINK
  2597. case NETLINK_ROUTE:
  2598. fd_trans_register(ret, &target_netlink_route_trans);
  2599. break;
  2600. #endif
  2601. case NETLINK_KOBJECT_UEVENT:
  2602. /* nothing to do: messages are strings */
  2603. break;
  2604. case NETLINK_AUDIT:
  2605. fd_trans_register(ret, &target_netlink_audit_trans);
  2606. break;
  2607. default:
  2608. g_assert_not_reached();
  2609. }
  2610. }
  2611. }
  2612. return ret;
  2613. }
  2614. /* do_bind() Must return target values and target errnos. */
  2615. static abi_long do_bind(int sockfd, abi_ulong target_addr,
  2616. socklen_t addrlen)
  2617. {
  2618. void *addr;
  2619. abi_long ret;
  2620. if ((int)addrlen < 0) {
  2621. return -TARGET_EINVAL;
  2622. }
  2623. addr = alloca(addrlen+1);
  2624. ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
  2625. if (ret)
  2626. return ret;
  2627. return get_errno(bind(sockfd, addr, addrlen));
  2628. }
  2629. /* do_connect() Must return target values and target errnos. */
  2630. static abi_long do_connect(int sockfd, abi_ulong target_addr,
  2631. socklen_t addrlen)
  2632. {
  2633. void *addr;
  2634. abi_long ret;
  2635. if ((int)addrlen < 0) {
  2636. return -TARGET_EINVAL;
  2637. }
  2638. addr = alloca(addrlen+1);
  2639. ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
  2640. if (ret)
  2641. return ret;
  2642. return get_errno(safe_connect(sockfd, addr, addrlen));
  2643. }
  2644. /* do_sendrecvmsg_locked() Must return target values and target errnos. */
  2645. static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
  2646. int flags, int send)
  2647. {
  2648. abi_long ret, len;
  2649. struct msghdr msg;
  2650. abi_ulong count;
  2651. struct iovec *vec;
  2652. abi_ulong target_vec;
  2653. if (msgp->msg_name) {
  2654. msg.msg_namelen = tswap32(msgp->msg_namelen);
  2655. msg.msg_name = alloca(msg.msg_namelen+1);
  2656. ret = target_to_host_sockaddr(fd, msg.msg_name,
  2657. tswapal(msgp->msg_name),
  2658. msg.msg_namelen);
  2659. if (ret == -TARGET_EFAULT) {
  2660. /* For connected sockets msg_name and msg_namelen must
  2661. * be ignored, so returning EFAULT immediately is wrong.
  2662. * Instead, pass a bad msg_name to the host kernel, and
  2663. * let it decide whether to return EFAULT or not.
  2664. */
  2665. msg.msg_name = (void *)-1;
  2666. } else if (ret) {
  2667. goto out2;
  2668. }
  2669. } else {
  2670. msg.msg_name = NULL;
  2671. msg.msg_namelen = 0;
  2672. }
  2673. msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
  2674. msg.msg_control = alloca(msg.msg_controllen);
  2675. memset(msg.msg_control, 0, msg.msg_controllen);
  2676. msg.msg_flags = tswap32(msgp->msg_flags);
  2677. count = tswapal(msgp->msg_iovlen);
  2678. target_vec = tswapal(msgp->msg_iov);
  2679. if (count > IOV_MAX) {
  2680. /* sendrcvmsg returns a different errno for this condition than
  2681. * readv/writev, so we must catch it here before lock_iovec() does.
  2682. */
  2683. ret = -TARGET_EMSGSIZE;
  2684. goto out2;
  2685. }
  2686. vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
  2687. target_vec, count, send);
  2688. if (vec == NULL) {
  2689. ret = -host_to_target_errno(errno);
  2690. goto out2;
  2691. }
  2692. msg.msg_iovlen = count;
  2693. msg.msg_iov = vec;
  2694. if (send) {
  2695. if (fd_trans_target_to_host_data(fd)) {
  2696. void *host_msg;
  2697. host_msg = g_malloc(msg.msg_iov->iov_len);
  2698. memcpy(host_msg, msg.msg_iov->iov_base, msg.msg_iov->iov_len);
  2699. ret = fd_trans_target_to_host_data(fd)(host_msg,
  2700. msg.msg_iov->iov_len);
  2701. if (ret >= 0) {
  2702. msg.msg_iov->iov_base = host_msg;
  2703. ret = get_errno(safe_sendmsg(fd, &msg, flags));
  2704. }
  2705. g_free(host_msg);
  2706. } else {
  2707. ret = target_to_host_cmsg(&msg, msgp);
  2708. if (ret == 0) {
  2709. ret = get_errno(safe_sendmsg(fd, &msg, flags));
  2710. }
  2711. }
  2712. } else {
  2713. ret = get_errno(safe_recvmsg(fd, &msg, flags));
  2714. if (!is_error(ret)) {
  2715. len = ret;
  2716. if (fd_trans_host_to_target_data(fd)) {
  2717. ret = fd_trans_host_to_target_data(fd)(msg.msg_iov->iov_base,
  2718. MIN(msg.msg_iov->iov_len, len));
  2719. } else {
  2720. ret = host_to_target_cmsg(msgp, &msg);
  2721. }
  2722. if (!is_error(ret)) {
  2723. msgp->msg_namelen = tswap32(msg.msg_namelen);
  2724. msgp->msg_flags = tswap32(msg.msg_flags);
  2725. if (msg.msg_name != NULL && msg.msg_name != (void *)-1) {
  2726. ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
  2727. msg.msg_name, msg.msg_namelen);
  2728. if (ret) {
  2729. goto out;
  2730. }
  2731. }
  2732. ret = len;
  2733. }
  2734. }
  2735. }
  2736. out:
  2737. unlock_iovec(vec, target_vec, count, !send);
  2738. out2:
  2739. return ret;
  2740. }
  2741. static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
  2742. int flags, int send)
  2743. {
  2744. abi_long ret;
  2745. struct target_msghdr *msgp;
  2746. if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
  2747. msgp,
  2748. target_msg,
  2749. send ? 1 : 0)) {
  2750. return -TARGET_EFAULT;
  2751. }
  2752. ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
  2753. unlock_user_struct(msgp, target_msg, send ? 0 : 1);
  2754. return ret;
  2755. }
  2756. /* We don't rely on the C library to have sendmmsg/recvmmsg support,
  2757. * so it might not have this *mmsg-specific flag either.
  2758. */
  2759. #ifndef MSG_WAITFORONE
  2760. #define MSG_WAITFORONE 0x10000
  2761. #endif
  2762. static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec,
  2763. unsigned int vlen, unsigned int flags,
  2764. int send)
  2765. {
  2766. struct target_mmsghdr *mmsgp;
  2767. abi_long ret = 0;
  2768. int i;
  2769. if (vlen > UIO_MAXIOV) {
  2770. vlen = UIO_MAXIOV;
  2771. }
  2772. mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1);
  2773. if (!mmsgp) {
  2774. return -TARGET_EFAULT;
  2775. }
  2776. for (i = 0; i < vlen; i++) {
  2777. ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send);
  2778. if (is_error(ret)) {
  2779. break;
  2780. }
  2781. mmsgp[i].msg_len = tswap32(ret);
  2782. /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
  2783. if (flags & MSG_WAITFORONE) {
  2784. flags |= MSG_DONTWAIT;
  2785. }
  2786. }
  2787. unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i);
  2788. /* Return number of datagrams sent if we sent any at all;
  2789. * otherwise return the error.
  2790. */
  2791. if (i) {
  2792. return i;
  2793. }
  2794. return ret;
  2795. }
  2796. /* do_accept4() Must return target values and target errnos. */
  2797. static abi_long do_accept4(int fd, abi_ulong target_addr,
  2798. abi_ulong target_addrlen_addr, int flags)
  2799. {
  2800. socklen_t addrlen, ret_addrlen;
  2801. void *addr;
  2802. abi_long ret;
  2803. int host_flags;
  2804. host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
  2805. if (target_addr == 0) {
  2806. return get_errno(safe_accept4(fd, NULL, NULL, host_flags));
  2807. }
  2808. /* linux returns EINVAL if addrlen pointer is invalid */
  2809. if (get_user_u32(addrlen, target_addrlen_addr))
  2810. return -TARGET_EINVAL;
  2811. if ((int)addrlen < 0) {
  2812. return -TARGET_EINVAL;
  2813. }
  2814. if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
  2815. return -TARGET_EINVAL;
  2816. addr = alloca(addrlen);
  2817. ret_addrlen = addrlen;
  2818. ret = get_errno(safe_accept4(fd, addr, &ret_addrlen, host_flags));
  2819. if (!is_error(ret)) {
  2820. host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
  2821. if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
  2822. ret = -TARGET_EFAULT;
  2823. }
  2824. }
  2825. return ret;
  2826. }
  2827. /* do_getpeername() Must return target values and target errnos. */
  2828. static abi_long do_getpeername(int fd, abi_ulong target_addr,
  2829. abi_ulong target_addrlen_addr)
  2830. {
  2831. socklen_t addrlen, ret_addrlen;
  2832. void *addr;
  2833. abi_long ret;
  2834. if (get_user_u32(addrlen, target_addrlen_addr))
  2835. return -TARGET_EFAULT;
  2836. if ((int)addrlen < 0) {
  2837. return -TARGET_EINVAL;
  2838. }
  2839. if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
  2840. return -TARGET_EFAULT;
  2841. addr = alloca(addrlen);
  2842. ret_addrlen = addrlen;
  2843. ret = get_errno(getpeername(fd, addr, &ret_addrlen));
  2844. if (!is_error(ret)) {
  2845. host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
  2846. if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
  2847. ret = -TARGET_EFAULT;
  2848. }
  2849. }
  2850. return ret;
  2851. }
  2852. /* do_getsockname() Must return target values and target errnos. */
  2853. static abi_long do_getsockname(int fd, abi_ulong target_addr,
  2854. abi_ulong target_addrlen_addr)
  2855. {
  2856. socklen_t addrlen, ret_addrlen;
  2857. void *addr;
  2858. abi_long ret;
  2859. if (get_user_u32(addrlen, target_addrlen_addr))
  2860. return -TARGET_EFAULT;
  2861. if ((int)addrlen < 0) {
  2862. return -TARGET_EINVAL;
  2863. }
  2864. if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
  2865. return -TARGET_EFAULT;
  2866. addr = alloca(addrlen);
  2867. ret_addrlen = addrlen;
  2868. ret = get_errno(getsockname(fd, addr, &ret_addrlen));
  2869. if (!is_error(ret)) {
  2870. host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
  2871. if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
  2872. ret = -TARGET_EFAULT;
  2873. }
  2874. }
  2875. return ret;
  2876. }
  2877. /* do_socketpair() Must return target values and target errnos. */
  2878. static abi_long do_socketpair(int domain, int type, int protocol,
  2879. abi_ulong target_tab_addr)
  2880. {
  2881. int tab[2];
  2882. abi_long ret;
  2883. target_to_host_sock_type(&type);
  2884. ret = get_errno(socketpair(domain, type, protocol, tab));
  2885. if (!is_error(ret)) {
  2886. if (put_user_s32(tab[0], target_tab_addr)
  2887. || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
  2888. ret = -TARGET_EFAULT;
  2889. }
  2890. return ret;
  2891. }
  2892. /* do_sendto() Must return target values and target errnos. */
  2893. static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
  2894. abi_ulong target_addr, socklen_t addrlen)
  2895. {
  2896. void *addr;
  2897. void *host_msg;
  2898. void *copy_msg = NULL;
  2899. abi_long ret;
  2900. if ((int)addrlen < 0) {
  2901. return -TARGET_EINVAL;
  2902. }
  2903. host_msg = lock_user(VERIFY_READ, msg, len, 1);
  2904. if (!host_msg)
  2905. return -TARGET_EFAULT;
  2906. if (fd_trans_target_to_host_data(fd)) {
  2907. copy_msg = host_msg;
  2908. host_msg = g_malloc(len);
  2909. memcpy(host_msg, copy_msg, len);
  2910. ret = fd_trans_target_to_host_data(fd)(host_msg, len);
  2911. if (ret < 0) {
  2912. goto fail;
  2913. }
  2914. }
  2915. if (target_addr) {
  2916. addr = alloca(addrlen+1);
  2917. ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen);
  2918. if (ret) {
  2919. goto fail;
  2920. }
  2921. ret = get_errno(safe_sendto(fd, host_msg, len, flags, addr, addrlen));
  2922. } else {
  2923. ret = get_errno(safe_sendto(fd, host_msg, len, flags, NULL, 0));
  2924. }
  2925. fail:
  2926. if (copy_msg) {
  2927. g_free(host_msg);
  2928. host_msg = copy_msg;
  2929. }
  2930. unlock_user(host_msg, msg, 0);
  2931. return ret;
  2932. }
  2933. /* do_recvfrom() Must return target values and target errnos. */
  2934. static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
  2935. abi_ulong target_addr,
  2936. abi_ulong target_addrlen)
  2937. {
  2938. socklen_t addrlen, ret_addrlen;
  2939. void *addr;
  2940. void *host_msg;
  2941. abi_long ret;
  2942. host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
  2943. if (!host_msg)
  2944. return -TARGET_EFAULT;
  2945. if (target_addr) {
  2946. if (get_user_u32(addrlen, target_addrlen)) {
  2947. ret = -TARGET_EFAULT;
  2948. goto fail;
  2949. }
  2950. if ((int)addrlen < 0) {
  2951. ret = -TARGET_EINVAL;
  2952. goto fail;
  2953. }
  2954. addr = alloca(addrlen);
  2955. ret_addrlen = addrlen;
  2956. ret = get_errno(safe_recvfrom(fd, host_msg, len, flags,
  2957. addr, &ret_addrlen));
  2958. } else {
  2959. addr = NULL; /* To keep compiler quiet. */
  2960. addrlen = 0; /* To keep compiler quiet. */
  2961. ret = get_errno(safe_recvfrom(fd, host_msg, len, flags, NULL, 0));
  2962. }
  2963. if (!is_error(ret)) {
  2964. if (fd_trans_host_to_target_data(fd)) {
  2965. abi_long trans;
  2966. trans = fd_trans_host_to_target_data(fd)(host_msg, MIN(ret, len));
  2967. if (is_error(trans)) {
  2968. ret = trans;
  2969. goto fail;
  2970. }
  2971. }
  2972. if (target_addr) {
  2973. host_to_target_sockaddr(target_addr, addr,
  2974. MIN(addrlen, ret_addrlen));
  2975. if (put_user_u32(ret_addrlen, target_addrlen)) {
  2976. ret = -TARGET_EFAULT;
  2977. goto fail;
  2978. }
  2979. }
  2980. unlock_user(host_msg, msg, len);
  2981. } else {
  2982. fail:
  2983. unlock_user(host_msg, msg, 0);
  2984. }
  2985. return ret;
  2986. }
  2987. #ifdef TARGET_NR_socketcall
  2988. /* do_socketcall() must return target values and target errnos. */
  2989. static abi_long do_socketcall(int num, abi_ulong vptr)
  2990. {
  2991. static const unsigned nargs[] = { /* number of arguments per operation */
  2992. [TARGET_SYS_SOCKET] = 3, /* domain, type, protocol */
  2993. [TARGET_SYS_BIND] = 3, /* fd, addr, addrlen */
  2994. [TARGET_SYS_CONNECT] = 3, /* fd, addr, addrlen */
  2995. [TARGET_SYS_LISTEN] = 2, /* fd, backlog */
  2996. [TARGET_SYS_ACCEPT] = 3, /* fd, addr, addrlen */
  2997. [TARGET_SYS_GETSOCKNAME] = 3, /* fd, addr, addrlen */
  2998. [TARGET_SYS_GETPEERNAME] = 3, /* fd, addr, addrlen */
  2999. [TARGET_SYS_SOCKETPAIR] = 4, /* domain, type, protocol, tab */
  3000. [TARGET_SYS_SEND] = 4, /* fd, msg, len, flags */
  3001. [TARGET_SYS_RECV] = 4, /* fd, msg, len, flags */
  3002. [TARGET_SYS_SENDTO] = 6, /* fd, msg, len, flags, addr, addrlen */
  3003. [TARGET_SYS_RECVFROM] = 6, /* fd, msg, len, flags, addr, addrlen */
  3004. [TARGET_SYS_SHUTDOWN] = 2, /* fd, how */
  3005. [TARGET_SYS_SETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
  3006. [TARGET_SYS_GETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
  3007. [TARGET_SYS_SENDMSG] = 3, /* fd, msg, flags */
  3008. [TARGET_SYS_RECVMSG] = 3, /* fd, msg, flags */
  3009. [TARGET_SYS_ACCEPT4] = 4, /* fd, addr, addrlen, flags */
  3010. [TARGET_SYS_RECVMMSG] = 4, /* fd, msgvec, vlen, flags */
  3011. [TARGET_SYS_SENDMMSG] = 4, /* fd, msgvec, vlen, flags */
  3012. };
  3013. abi_long a[6]; /* max 6 args */
  3014. unsigned i;
  3015. /* check the range of the first argument num */
  3016. /* (TARGET_SYS_SENDMMSG is the highest among TARGET_SYS_xxx) */
  3017. if (num < 1 || num > TARGET_SYS_SENDMMSG) {
  3018. return -TARGET_EINVAL;
  3019. }
  3020. /* ensure we have space for args */
  3021. if (nargs[num] > ARRAY_SIZE(a)) {
  3022. return -TARGET_EINVAL;
  3023. }
  3024. /* collect the arguments in a[] according to nargs[] */
  3025. for (i = 0; i < nargs[num]; ++i) {
  3026. if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) {
  3027. return -TARGET_EFAULT;
  3028. }
  3029. }
  3030. /* now when we have the args, invoke the appropriate underlying function */
  3031. switch (num) {
  3032. case TARGET_SYS_SOCKET: /* domain, type, protocol */
  3033. return do_socket(a[0], a[1], a[2]);
  3034. case TARGET_SYS_BIND: /* sockfd, addr, addrlen */
  3035. return do_bind(a[0], a[1], a[2]);
  3036. case TARGET_SYS_CONNECT: /* sockfd, addr, addrlen */
  3037. return do_connect(a[0], a[1], a[2]);
  3038. case TARGET_SYS_LISTEN: /* sockfd, backlog */
  3039. return get_errno(listen(a[0], a[1]));
  3040. case TARGET_SYS_ACCEPT: /* sockfd, addr, addrlen */
  3041. return do_accept4(a[0], a[1], a[2], 0);
  3042. case TARGET_SYS_GETSOCKNAME: /* sockfd, addr, addrlen */
  3043. return do_getsockname(a[0], a[1], a[2]);
  3044. case TARGET_SYS_GETPEERNAME: /* sockfd, addr, addrlen */
  3045. return do_getpeername(a[0], a[1], a[2]);
  3046. case TARGET_SYS_SOCKETPAIR: /* domain, type, protocol, tab */
  3047. return do_socketpair(a[0], a[1], a[2], a[3]);
  3048. case TARGET_SYS_SEND: /* sockfd, msg, len, flags */
  3049. return do_sendto(a[0], a[1], a[2], a[3], 0, 0);
  3050. case TARGET_SYS_RECV: /* sockfd, msg, len, flags */
  3051. return do_recvfrom(a[0], a[1], a[2], a[3], 0, 0);
  3052. case TARGET_SYS_SENDTO: /* sockfd, msg, len, flags, addr, addrlen */
  3053. return do_sendto(a[0], a[1], a[2], a[3], a[4], a[5]);
  3054. case TARGET_SYS_RECVFROM: /* sockfd, msg, len, flags, addr, addrlen */
  3055. return do_recvfrom(a[0], a[1], a[2], a[3], a[4], a[5]);
  3056. case TARGET_SYS_SHUTDOWN: /* sockfd, how */
  3057. return get_errno(shutdown(a[0], a[1]));
  3058. case TARGET_SYS_SETSOCKOPT: /* sockfd, level, optname, optval, optlen */
  3059. return do_setsockopt(a[0], a[1], a[2], a[3], a[4]);
  3060. case TARGET_SYS_GETSOCKOPT: /* sockfd, level, optname, optval, optlen */
  3061. return do_getsockopt(a[0], a[1], a[2], a[3], a[4]);
  3062. case TARGET_SYS_SENDMSG: /* sockfd, msg, flags */
  3063. return do_sendrecvmsg(a[0], a[1], a[2], 1);
  3064. case TARGET_SYS_RECVMSG: /* sockfd, msg, flags */
  3065. return do_sendrecvmsg(a[0], a[1], a[2], 0);
  3066. case TARGET_SYS_ACCEPT4: /* sockfd, addr, addrlen, flags */
  3067. return do_accept4(a[0], a[1], a[2], a[3]);
  3068. case TARGET_SYS_RECVMMSG: /* sockfd, msgvec, vlen, flags */
  3069. return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 0);
  3070. case TARGET_SYS_SENDMMSG: /* sockfd, msgvec, vlen, flags */
  3071. return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 1);
  3072. default:
  3073. gemu_log("Unsupported socketcall: %d\n", num);
  3074. return -TARGET_EINVAL;
  3075. }
  3076. }
  3077. #endif
  3078. #define N_SHM_REGIONS 32
  3079. static struct shm_region {
  3080. abi_ulong start;
  3081. abi_ulong size;
  3082. bool in_use;
  3083. } shm_regions[N_SHM_REGIONS];
  3084. #ifndef TARGET_SEMID64_DS
  3085. /* asm-generic version of this struct */
  3086. struct target_semid64_ds
  3087. {
  3088. struct target_ipc_perm sem_perm;
  3089. abi_ulong sem_otime;
  3090. #if TARGET_ABI_BITS == 32
  3091. abi_ulong __unused1;
  3092. #endif
  3093. abi_ulong sem_ctime;
  3094. #if TARGET_ABI_BITS == 32
  3095. abi_ulong __unused2;
  3096. #endif
  3097. abi_ulong sem_nsems;
  3098. abi_ulong __unused3;
  3099. abi_ulong __unused4;
  3100. };
  3101. #endif
  3102. static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
  3103. abi_ulong target_addr)
  3104. {
  3105. struct target_ipc_perm *target_ip;
  3106. struct target_semid64_ds *target_sd;
  3107. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  3108. return -TARGET_EFAULT;
  3109. target_ip = &(target_sd->sem_perm);
  3110. host_ip->__key = tswap32(target_ip->__key);
  3111. host_ip->uid = tswap32(target_ip->uid);
  3112. host_ip->gid = tswap32(target_ip->gid);
  3113. host_ip->cuid = tswap32(target_ip->cuid);
  3114. host_ip->cgid = tswap32(target_ip->cgid);
  3115. #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
  3116. host_ip->mode = tswap32(target_ip->mode);
  3117. #else
  3118. host_ip->mode = tswap16(target_ip->mode);
  3119. #endif
  3120. #if defined(TARGET_PPC)
  3121. host_ip->__seq = tswap32(target_ip->__seq);
  3122. #else
  3123. host_ip->__seq = tswap16(target_ip->__seq);
  3124. #endif
  3125. unlock_user_struct(target_sd, target_addr, 0);
  3126. return 0;
  3127. }
  3128. static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
  3129. struct ipc_perm *host_ip)
  3130. {
  3131. struct target_ipc_perm *target_ip;
  3132. struct target_semid64_ds *target_sd;
  3133. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  3134. return -TARGET_EFAULT;
  3135. target_ip = &(target_sd->sem_perm);
  3136. target_ip->__key = tswap32(host_ip->__key);
  3137. target_ip->uid = tswap32(host_ip->uid);
  3138. target_ip->gid = tswap32(host_ip->gid);
  3139. target_ip->cuid = tswap32(host_ip->cuid);
  3140. target_ip->cgid = tswap32(host_ip->cgid);
  3141. #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
  3142. target_ip->mode = tswap32(host_ip->mode);
  3143. #else
  3144. target_ip->mode = tswap16(host_ip->mode);
  3145. #endif
  3146. #if defined(TARGET_PPC)
  3147. target_ip->__seq = tswap32(host_ip->__seq);
  3148. #else
  3149. target_ip->__seq = tswap16(host_ip->__seq);
  3150. #endif
  3151. unlock_user_struct(target_sd, target_addr, 1);
  3152. return 0;
  3153. }
  3154. static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
  3155. abi_ulong target_addr)
  3156. {
  3157. struct target_semid64_ds *target_sd;
  3158. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  3159. return -TARGET_EFAULT;
  3160. if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
  3161. return -TARGET_EFAULT;
  3162. host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
  3163. host_sd->sem_otime = tswapal(target_sd->sem_otime);
  3164. host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
  3165. unlock_user_struct(target_sd, target_addr, 0);
  3166. return 0;
  3167. }
  3168. static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
  3169. struct semid_ds *host_sd)
  3170. {
  3171. struct target_semid64_ds *target_sd;
  3172. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  3173. return -TARGET_EFAULT;
  3174. if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
  3175. return -TARGET_EFAULT;
  3176. target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
  3177. target_sd->sem_otime = tswapal(host_sd->sem_otime);
  3178. target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
  3179. unlock_user_struct(target_sd, target_addr, 1);
  3180. return 0;
  3181. }
  3182. struct target_seminfo {
  3183. int semmap;
  3184. int semmni;
  3185. int semmns;
  3186. int semmnu;
  3187. int semmsl;
  3188. int semopm;
  3189. int semume;
  3190. int semusz;
  3191. int semvmx;
  3192. int semaem;
  3193. };
  3194. static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
  3195. struct seminfo *host_seminfo)
  3196. {
  3197. struct target_seminfo *target_seminfo;
  3198. if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
  3199. return -TARGET_EFAULT;
  3200. __put_user(host_seminfo->semmap, &target_seminfo->semmap);
  3201. __put_user(host_seminfo->semmni, &target_seminfo->semmni);
  3202. __put_user(host_seminfo->semmns, &target_seminfo->semmns);
  3203. __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
  3204. __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
  3205. __put_user(host_seminfo->semopm, &target_seminfo->semopm);
  3206. __put_user(host_seminfo->semume, &target_seminfo->semume);
  3207. __put_user(host_seminfo->semusz, &target_seminfo->semusz);
  3208. __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
  3209. __put_user(host_seminfo->semaem, &target_seminfo->semaem);
  3210. unlock_user_struct(target_seminfo, target_addr, 1);
  3211. return 0;
  3212. }
  3213. union semun {
  3214. int val;
  3215. struct semid_ds *buf;
  3216. unsigned short *array;
  3217. struct seminfo *__buf;
  3218. };
  3219. union target_semun {
  3220. int val;
  3221. abi_ulong buf;
  3222. abi_ulong array;
  3223. abi_ulong __buf;
  3224. };
  3225. static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
  3226. abi_ulong target_addr)
  3227. {
  3228. int nsems;
  3229. unsigned short *array;
  3230. union semun semun;
  3231. struct semid_ds semid_ds;
  3232. int i, ret;
  3233. semun.buf = &semid_ds;
  3234. ret = semctl(semid, 0, IPC_STAT, semun);
  3235. if (ret == -1)
  3236. return get_errno(ret);
  3237. nsems = semid_ds.sem_nsems;
  3238. *host_array = g_try_new(unsigned short, nsems);
  3239. if (!*host_array) {
  3240. return -TARGET_ENOMEM;
  3241. }
  3242. array = lock_user(VERIFY_READ, target_addr,
  3243. nsems*sizeof(unsigned short), 1);
  3244. if (!array) {
  3245. g_free(*host_array);
  3246. return -TARGET_EFAULT;
  3247. }
  3248. for(i=0; i<nsems; i++) {
  3249. __get_user((*host_array)[i], &array[i]);
  3250. }
  3251. unlock_user(array, target_addr, 0);
  3252. return 0;
  3253. }
  3254. static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
  3255. unsigned short **host_array)
  3256. {
  3257. int nsems;
  3258. unsigned short *array;
  3259. union semun semun;
  3260. struct semid_ds semid_ds;
  3261. int i, ret;
  3262. semun.buf = &semid_ds;
  3263. ret = semctl(semid, 0, IPC_STAT, semun);
  3264. if (ret == -1)
  3265. return get_errno(ret);
  3266. nsems = semid_ds.sem_nsems;
  3267. array = lock_user(VERIFY_WRITE, target_addr,
  3268. nsems*sizeof(unsigned short), 0);
  3269. if (!array)
  3270. return -TARGET_EFAULT;
  3271. for(i=0; i<nsems; i++) {
  3272. __put_user((*host_array)[i], &array[i]);
  3273. }
  3274. g_free(*host_array);
  3275. unlock_user(array, target_addr, 1);
  3276. return 0;
  3277. }
  3278. static inline abi_long do_semctl(int semid, int semnum, int cmd,
  3279. abi_ulong target_arg)
  3280. {
  3281. union target_semun target_su = { .buf = target_arg };
  3282. union semun arg;
  3283. struct semid_ds dsarg;
  3284. unsigned short *array = NULL;
  3285. struct seminfo seminfo;
  3286. abi_long ret = -TARGET_EINVAL;
  3287. abi_long err;
  3288. cmd &= 0xff;
  3289. switch( cmd ) {
  3290. case GETVAL:
  3291. case SETVAL:
  3292. /* In 64 bit cross-endian situations, we will erroneously pick up
  3293. * the wrong half of the union for the "val" element. To rectify
  3294. * this, the entire 8-byte structure is byteswapped, followed by
  3295. * a swap of the 4 byte val field. In other cases, the data is
  3296. * already in proper host byte order. */
  3297. if (sizeof(target_su.val) != (sizeof(target_su.buf))) {
  3298. target_su.buf = tswapal(target_su.buf);
  3299. arg.val = tswap32(target_su.val);
  3300. } else {
  3301. arg.val = target_su.val;
  3302. }
  3303. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3304. break;
  3305. case GETALL:
  3306. case SETALL:
  3307. err = target_to_host_semarray(semid, &array, target_su.array);
  3308. if (err)
  3309. return err;
  3310. arg.array = array;
  3311. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3312. err = host_to_target_semarray(semid, target_su.array, &array);
  3313. if (err)
  3314. return err;
  3315. break;
  3316. case IPC_STAT:
  3317. case IPC_SET:
  3318. case SEM_STAT:
  3319. err = target_to_host_semid_ds(&dsarg, target_su.buf);
  3320. if (err)
  3321. return err;
  3322. arg.buf = &dsarg;
  3323. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3324. err = host_to_target_semid_ds(target_su.buf, &dsarg);
  3325. if (err)
  3326. return err;
  3327. break;
  3328. case IPC_INFO:
  3329. case SEM_INFO:
  3330. arg.__buf = &seminfo;
  3331. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3332. err = host_to_target_seminfo(target_su.__buf, &seminfo);
  3333. if (err)
  3334. return err;
  3335. break;
  3336. case IPC_RMID:
  3337. case GETPID:
  3338. case GETNCNT:
  3339. case GETZCNT:
  3340. ret = get_errno(semctl(semid, semnum, cmd, NULL));
  3341. break;
  3342. }
  3343. return ret;
  3344. }
  3345. struct target_sembuf {
  3346. unsigned short sem_num;
  3347. short sem_op;
  3348. short sem_flg;
  3349. };
  3350. static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
  3351. abi_ulong target_addr,
  3352. unsigned nsops)
  3353. {
  3354. struct target_sembuf *target_sembuf;
  3355. int i;
  3356. target_sembuf = lock_user(VERIFY_READ, target_addr,
  3357. nsops*sizeof(struct target_sembuf), 1);
  3358. if (!target_sembuf)
  3359. return -TARGET_EFAULT;
  3360. for(i=0; i<nsops; i++) {
  3361. __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
  3362. __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
  3363. __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
  3364. }
  3365. unlock_user(target_sembuf, target_addr, 0);
  3366. return 0;
  3367. }
  3368. static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
  3369. {
  3370. struct sembuf sops[nsops];
  3371. abi_long ret;
  3372. if (target_to_host_sembuf(sops, ptr, nsops))
  3373. return -TARGET_EFAULT;
  3374. ret = -TARGET_ENOSYS;
  3375. #ifdef __NR_semtimedop
  3376. ret = get_errno(safe_semtimedop(semid, sops, nsops, NULL));
  3377. #endif
  3378. #ifdef __NR_ipc
  3379. if (ret == -TARGET_ENOSYS) {
  3380. ret = get_errno(safe_ipc(IPCOP_semtimedop, semid, nsops, 0, sops, 0));
  3381. }
  3382. #endif
  3383. return ret;
  3384. }
  3385. struct target_msqid_ds
  3386. {
  3387. struct target_ipc_perm msg_perm;
  3388. abi_ulong msg_stime;
  3389. #if TARGET_ABI_BITS == 32
  3390. abi_ulong __unused1;
  3391. #endif
  3392. abi_ulong msg_rtime;
  3393. #if TARGET_ABI_BITS == 32
  3394. abi_ulong __unused2;
  3395. #endif
  3396. abi_ulong msg_ctime;
  3397. #if TARGET_ABI_BITS == 32
  3398. abi_ulong __unused3;
  3399. #endif
  3400. abi_ulong __msg_cbytes;
  3401. abi_ulong msg_qnum;
  3402. abi_ulong msg_qbytes;
  3403. abi_ulong msg_lspid;
  3404. abi_ulong msg_lrpid;
  3405. abi_ulong __unused4;
  3406. abi_ulong __unused5;
  3407. };
  3408. static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
  3409. abi_ulong target_addr)
  3410. {
  3411. struct target_msqid_ds *target_md;
  3412. if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
  3413. return -TARGET_EFAULT;
  3414. if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
  3415. return -TARGET_EFAULT;
  3416. host_md->msg_stime = tswapal(target_md->msg_stime);
  3417. host_md->msg_rtime = tswapal(target_md->msg_rtime);
  3418. host_md->msg_ctime = tswapal(target_md->msg_ctime);
  3419. host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
  3420. host_md->msg_qnum = tswapal(target_md->msg_qnum);
  3421. host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
  3422. host_md->msg_lspid = tswapal(target_md->msg_lspid);
  3423. host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
  3424. unlock_user_struct(target_md, target_addr, 0);
  3425. return 0;
  3426. }
  3427. static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
  3428. struct msqid_ds *host_md)
  3429. {
  3430. struct target_msqid_ds *target_md;
  3431. if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
  3432. return -TARGET_EFAULT;
  3433. if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
  3434. return -TARGET_EFAULT;
  3435. target_md->msg_stime = tswapal(host_md->msg_stime);
  3436. target_md->msg_rtime = tswapal(host_md->msg_rtime);
  3437. target_md->msg_ctime = tswapal(host_md->msg_ctime);
  3438. target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
  3439. target_md->msg_qnum = tswapal(host_md->msg_qnum);
  3440. target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
  3441. target_md->msg_lspid = tswapal(host_md->msg_lspid);
  3442. target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
  3443. unlock_user_struct(target_md, target_addr, 1);
  3444. return 0;
  3445. }
  3446. struct target_msginfo {
  3447. int msgpool;
  3448. int msgmap;
  3449. int msgmax;
  3450. int msgmnb;
  3451. int msgmni;
  3452. int msgssz;
  3453. int msgtql;
  3454. unsigned short int msgseg;
  3455. };
  3456. static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
  3457. struct msginfo *host_msginfo)
  3458. {
  3459. struct target_msginfo *target_msginfo;
  3460. if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
  3461. return -TARGET_EFAULT;
  3462. __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
  3463. __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
  3464. __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
  3465. __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
  3466. __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
  3467. __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
  3468. __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
  3469. __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
  3470. unlock_user_struct(target_msginfo, target_addr, 1);
  3471. return 0;
  3472. }
  3473. static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
  3474. {
  3475. struct msqid_ds dsarg;
  3476. struct msginfo msginfo;
  3477. abi_long ret = -TARGET_EINVAL;
  3478. cmd &= 0xff;
  3479. switch (cmd) {
  3480. case IPC_STAT:
  3481. case IPC_SET:
  3482. case MSG_STAT:
  3483. if (target_to_host_msqid_ds(&dsarg,ptr))
  3484. return -TARGET_EFAULT;
  3485. ret = get_errno(msgctl(msgid, cmd, &dsarg));
  3486. if (host_to_target_msqid_ds(ptr,&dsarg))
  3487. return -TARGET_EFAULT;
  3488. break;
  3489. case IPC_RMID:
  3490. ret = get_errno(msgctl(msgid, cmd, NULL));
  3491. break;
  3492. case IPC_INFO:
  3493. case MSG_INFO:
  3494. ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
  3495. if (host_to_target_msginfo(ptr, &msginfo))
  3496. return -TARGET_EFAULT;
  3497. break;
  3498. }
  3499. return ret;
  3500. }
  3501. struct target_msgbuf {
  3502. abi_long mtype;
  3503. char mtext[1];
  3504. };
  3505. static inline abi_long do_msgsnd(int msqid, abi_long msgp,
  3506. ssize_t msgsz, int msgflg)
  3507. {
  3508. struct target_msgbuf *target_mb;
  3509. struct msgbuf *host_mb;
  3510. abi_long ret = 0;
  3511. if (msgsz < 0) {
  3512. return -TARGET_EINVAL;
  3513. }
  3514. if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
  3515. return -TARGET_EFAULT;
  3516. host_mb = g_try_malloc(msgsz + sizeof(long));
  3517. if (!host_mb) {
  3518. unlock_user_struct(target_mb, msgp, 0);
  3519. return -TARGET_ENOMEM;
  3520. }
  3521. host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
  3522. memcpy(host_mb->mtext, target_mb->mtext, msgsz);
  3523. ret = -TARGET_ENOSYS;
  3524. #ifdef __NR_msgsnd
  3525. ret = get_errno(safe_msgsnd(msqid, host_mb, msgsz, msgflg));
  3526. #endif
  3527. #ifdef __NR_ipc
  3528. if (ret == -TARGET_ENOSYS) {
  3529. ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
  3530. host_mb, 0));
  3531. }
  3532. #endif
  3533. g_free(host_mb);
  3534. unlock_user_struct(target_mb, msgp, 0);
  3535. return ret;
  3536. }
  3537. static inline abi_long do_msgrcv(int msqid, abi_long msgp,
  3538. ssize_t msgsz, abi_long msgtyp,
  3539. int msgflg)
  3540. {
  3541. struct target_msgbuf *target_mb;
  3542. char *target_mtext;
  3543. struct msgbuf *host_mb;
  3544. abi_long ret = 0;
  3545. if (msgsz < 0) {
  3546. return -TARGET_EINVAL;
  3547. }
  3548. if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
  3549. return -TARGET_EFAULT;
  3550. host_mb = g_try_malloc(msgsz + sizeof(long));
  3551. if (!host_mb) {
  3552. ret = -TARGET_ENOMEM;
  3553. goto end;
  3554. }
  3555. ret = -TARGET_ENOSYS;
  3556. #ifdef __NR_msgrcv
  3557. ret = get_errno(safe_msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
  3558. #endif
  3559. #ifdef __NR_ipc
  3560. if (ret == -TARGET_ENOSYS) {
  3561. ret = get_errno(safe_ipc(IPCOP_CALL(1, IPCOP_msgrcv), msqid, msgsz,
  3562. msgflg, host_mb, msgtyp));
  3563. }
  3564. #endif
  3565. if (ret > 0) {
  3566. abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
  3567. target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
  3568. if (!target_mtext) {
  3569. ret = -TARGET_EFAULT;
  3570. goto end;
  3571. }
  3572. memcpy(target_mb->mtext, host_mb->mtext, ret);
  3573. unlock_user(target_mtext, target_mtext_addr, ret);
  3574. }
  3575. target_mb->mtype = tswapal(host_mb->mtype);
  3576. end:
  3577. if (target_mb)
  3578. unlock_user_struct(target_mb, msgp, 1);
  3579. g_free(host_mb);
  3580. return ret;
  3581. }
  3582. static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
  3583. abi_ulong target_addr)
  3584. {
  3585. struct target_shmid_ds *target_sd;
  3586. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  3587. return -TARGET_EFAULT;
  3588. if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
  3589. return -TARGET_EFAULT;
  3590. __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
  3591. __get_user(host_sd->shm_atime, &target_sd->shm_atime);
  3592. __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
  3593. __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
  3594. __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
  3595. __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
  3596. __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
  3597. unlock_user_struct(target_sd, target_addr, 0);
  3598. return 0;
  3599. }
  3600. static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
  3601. struct shmid_ds *host_sd)
  3602. {
  3603. struct target_shmid_ds *target_sd;
  3604. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  3605. return -TARGET_EFAULT;
  3606. if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
  3607. return -TARGET_EFAULT;
  3608. __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
  3609. __put_user(host_sd->shm_atime, &target_sd->shm_atime);
  3610. __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
  3611. __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
  3612. __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
  3613. __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
  3614. __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
  3615. unlock_user_struct(target_sd, target_addr, 1);
  3616. return 0;
  3617. }
  3618. struct target_shminfo {
  3619. abi_ulong shmmax;
  3620. abi_ulong shmmin;
  3621. abi_ulong shmmni;
  3622. abi_ulong shmseg;
  3623. abi_ulong shmall;
  3624. };
  3625. static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
  3626. struct shminfo *host_shminfo)
  3627. {
  3628. struct target_shminfo *target_shminfo;
  3629. if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
  3630. return -TARGET_EFAULT;
  3631. __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
  3632. __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
  3633. __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
  3634. __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
  3635. __put_user(host_shminfo->shmall, &target_shminfo->shmall);
  3636. unlock_user_struct(target_shminfo, target_addr, 1);
  3637. return 0;
  3638. }
  3639. struct target_shm_info {
  3640. int used_ids;
  3641. abi_ulong shm_tot;
  3642. abi_ulong shm_rss;
  3643. abi_ulong shm_swp;
  3644. abi_ulong swap_attempts;
  3645. abi_ulong swap_successes;
  3646. };
  3647. static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
  3648. struct shm_info *host_shm_info)
  3649. {
  3650. struct target_shm_info *target_shm_info;
  3651. if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
  3652. return -TARGET_EFAULT;
  3653. __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
  3654. __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
  3655. __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
  3656. __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
  3657. __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
  3658. __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
  3659. unlock_user_struct(target_shm_info, target_addr, 1);
  3660. return 0;
  3661. }
  3662. static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
  3663. {
  3664. struct shmid_ds dsarg;
  3665. struct shminfo shminfo;
  3666. struct shm_info shm_info;
  3667. abi_long ret = -TARGET_EINVAL;
  3668. cmd &= 0xff;
  3669. switch(cmd) {
  3670. case IPC_STAT:
  3671. case IPC_SET:
  3672. case SHM_STAT:
  3673. if (target_to_host_shmid_ds(&dsarg, buf))
  3674. return -TARGET_EFAULT;
  3675. ret = get_errno(shmctl(shmid, cmd, &dsarg));
  3676. if (host_to_target_shmid_ds(buf, &dsarg))
  3677. return -TARGET_EFAULT;
  3678. break;
  3679. case IPC_INFO:
  3680. ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
  3681. if (host_to_target_shminfo(buf, &shminfo))
  3682. return -TARGET_EFAULT;
  3683. break;
  3684. case SHM_INFO:
  3685. ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
  3686. if (host_to_target_shm_info(buf, &shm_info))
  3687. return -TARGET_EFAULT;
  3688. break;
  3689. case IPC_RMID:
  3690. case SHM_LOCK:
  3691. case SHM_UNLOCK:
  3692. ret = get_errno(shmctl(shmid, cmd, NULL));
  3693. break;
  3694. }
  3695. return ret;
  3696. }
  3697. #ifndef TARGET_FORCE_SHMLBA
  3698. /* For most architectures, SHMLBA is the same as the page size;
  3699. * some architectures have larger values, in which case they should
  3700. * define TARGET_FORCE_SHMLBA and provide a target_shmlba() function.
  3701. * This corresponds to the kernel arch code defining __ARCH_FORCE_SHMLBA
  3702. * and defining its own value for SHMLBA.
  3703. *
  3704. * The kernel also permits SHMLBA to be set by the architecture to a
  3705. * value larger than the page size without setting __ARCH_FORCE_SHMLBA;
  3706. * this means that addresses are rounded to the large size if
  3707. * SHM_RND is set but addresses not aligned to that size are not rejected
  3708. * as long as they are at least page-aligned. Since the only architecture
  3709. * which uses this is ia64 this code doesn't provide for that oddity.
  3710. */
  3711. static inline abi_ulong target_shmlba(CPUArchState *cpu_env)
  3712. {
  3713. return TARGET_PAGE_SIZE;
  3714. }
  3715. #endif
  3716. static inline abi_ulong do_shmat(CPUArchState *cpu_env,
  3717. int shmid, abi_ulong shmaddr, int shmflg)
  3718. {
  3719. abi_long raddr;
  3720. void *host_raddr;
  3721. struct shmid_ds shm_info;
  3722. int i,ret;
  3723. abi_ulong shmlba;
  3724. /* find out the length of the shared memory segment */
  3725. ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
  3726. if (is_error(ret)) {
  3727. /* can't get length, bail out */
  3728. return ret;
  3729. }
  3730. shmlba = target_shmlba(cpu_env);
  3731. if (shmaddr & (shmlba - 1)) {
  3732. if (shmflg & SHM_RND) {
  3733. shmaddr &= ~(shmlba - 1);
  3734. } else {
  3735. return -TARGET_EINVAL;
  3736. }
  3737. }
  3738. if (!guest_range_valid(shmaddr, shm_info.shm_segsz)) {
  3739. return -TARGET_EINVAL;
  3740. }
  3741. mmap_lock();
  3742. if (shmaddr)
  3743. host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
  3744. else {
  3745. abi_ulong mmap_start;
  3746. /* In order to use the host shmat, we need to honor host SHMLBA. */
  3747. mmap_start = mmap_find_vma(0, shm_info.shm_segsz, MAX(SHMLBA, shmlba));
  3748. if (mmap_start == -1) {
  3749. errno = ENOMEM;
  3750. host_raddr = (void *)-1;
  3751. } else
  3752. host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
  3753. }
  3754. if (host_raddr == (void *)-1) {
  3755. mmap_unlock();
  3756. return get_errno((long)host_raddr);
  3757. }
  3758. raddr=h2g((unsigned long)host_raddr);
  3759. page_set_flags(raddr, raddr + shm_info.shm_segsz,
  3760. PAGE_VALID | PAGE_READ |
  3761. ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
  3762. for (i = 0; i < N_SHM_REGIONS; i++) {
  3763. if (!shm_regions[i].in_use) {
  3764. shm_regions[i].in_use = true;
  3765. shm_regions[i].start = raddr;
  3766. shm_regions[i].size = shm_info.shm_segsz;
  3767. break;
  3768. }
  3769. }
  3770. mmap_unlock();
  3771. return raddr;
  3772. }
  3773. static inline abi_long do_shmdt(abi_ulong shmaddr)
  3774. {
  3775. int i;
  3776. abi_long rv;
  3777. mmap_lock();
  3778. for (i = 0; i < N_SHM_REGIONS; ++i) {
  3779. if (shm_regions[i].in_use && shm_regions[i].start == shmaddr) {
  3780. shm_regions[i].in_use = false;
  3781. page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
  3782. break;
  3783. }
  3784. }
  3785. rv = get_errno(shmdt(g2h(shmaddr)));
  3786. mmap_unlock();
  3787. return rv;
  3788. }
  3789. #ifdef TARGET_NR_ipc
  3790. /* ??? This only works with linear mappings. */
  3791. /* do_ipc() must return target values and target errnos. */
  3792. static abi_long do_ipc(CPUArchState *cpu_env,
  3793. unsigned int call, abi_long first,
  3794. abi_long second, abi_long third,
  3795. abi_long ptr, abi_long fifth)
  3796. {
  3797. int version;
  3798. abi_long ret = 0;
  3799. version = call >> 16;
  3800. call &= 0xffff;
  3801. switch (call) {
  3802. case IPCOP_semop:
  3803. ret = do_semop(first, ptr, second);
  3804. break;
  3805. case IPCOP_semget:
  3806. ret = get_errno(semget(first, second, third));
  3807. break;
  3808. case IPCOP_semctl: {
  3809. /* The semun argument to semctl is passed by value, so dereference the
  3810. * ptr argument. */
  3811. abi_ulong atptr;
  3812. get_user_ual(atptr, ptr);
  3813. ret = do_semctl(first, second, third, atptr);
  3814. break;
  3815. }
  3816. case IPCOP_msgget:
  3817. ret = get_errno(msgget(first, second));
  3818. break;
  3819. case IPCOP_msgsnd:
  3820. ret = do_msgsnd(first, ptr, second, third);
  3821. break;
  3822. case IPCOP_msgctl:
  3823. ret = do_msgctl(first, second, ptr);
  3824. break;
  3825. case IPCOP_msgrcv:
  3826. switch (version) {
  3827. case 0:
  3828. {
  3829. struct target_ipc_kludge {
  3830. abi_long msgp;
  3831. abi_long msgtyp;
  3832. } *tmp;
  3833. if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
  3834. ret = -TARGET_EFAULT;
  3835. break;
  3836. }
  3837. ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
  3838. unlock_user_struct(tmp, ptr, 0);
  3839. break;
  3840. }
  3841. default:
  3842. ret = do_msgrcv(first, ptr, second, fifth, third);
  3843. }
  3844. break;
  3845. case IPCOP_shmat:
  3846. switch (version) {
  3847. default:
  3848. {
  3849. abi_ulong raddr;
  3850. raddr = do_shmat(cpu_env, first, ptr, second);
  3851. if (is_error(raddr))
  3852. return get_errno(raddr);
  3853. if (put_user_ual(raddr, third))
  3854. return -TARGET_EFAULT;
  3855. break;
  3856. }
  3857. case 1:
  3858. ret = -TARGET_EINVAL;
  3859. break;
  3860. }
  3861. break;
  3862. case IPCOP_shmdt:
  3863. ret = do_shmdt(ptr);
  3864. break;
  3865. case IPCOP_shmget:
  3866. /* IPC_* flag values are the same on all linux platforms */
  3867. ret = get_errno(shmget(first, second, third));
  3868. break;
  3869. /* IPC_* and SHM_* command values are the same on all linux platforms */
  3870. case IPCOP_shmctl:
  3871. ret = do_shmctl(first, second, ptr);
  3872. break;
  3873. default:
  3874. gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
  3875. ret = -TARGET_ENOSYS;
  3876. break;
  3877. }
  3878. return ret;
  3879. }
  3880. #endif
  3881. /* kernel structure types definitions */
  3882. #define STRUCT(name, ...) STRUCT_ ## name,
  3883. #define STRUCT_SPECIAL(name) STRUCT_ ## name,
  3884. enum {
  3885. #include "syscall_types.h"
  3886. STRUCT_MAX
  3887. };
  3888. #undef STRUCT
  3889. #undef STRUCT_SPECIAL
  3890. #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
  3891. #define STRUCT_SPECIAL(name)
  3892. #include "syscall_types.h"
  3893. #undef STRUCT
  3894. #undef STRUCT_SPECIAL
  3895. typedef struct IOCTLEntry IOCTLEntry;
  3896. typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
  3897. int fd, int cmd, abi_long arg);
  3898. struct IOCTLEntry {
  3899. int target_cmd;
  3900. unsigned int host_cmd;
  3901. const char *name;
  3902. int access;
  3903. do_ioctl_fn *do_ioctl;
  3904. const argtype arg_type[5];
  3905. };
  3906. #define IOC_R 0x0001
  3907. #define IOC_W 0x0002
  3908. #define IOC_RW (IOC_R | IOC_W)
  3909. #define MAX_STRUCT_SIZE 4096
  3910. #ifdef CONFIG_FIEMAP
  3911. /* So fiemap access checks don't overflow on 32 bit systems.
  3912. * This is very slightly smaller than the limit imposed by
  3913. * the underlying kernel.
  3914. */
  3915. #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
  3916. / sizeof(struct fiemap_extent))
  3917. static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
  3918. int fd, int cmd, abi_long arg)
  3919. {
  3920. /* The parameter for this ioctl is a struct fiemap followed
  3921. * by an array of struct fiemap_extent whose size is set
  3922. * in fiemap->fm_extent_count. The array is filled in by the
  3923. * ioctl.
  3924. */
  3925. int target_size_in, target_size_out;
  3926. struct fiemap *fm;
  3927. const argtype *arg_type = ie->arg_type;
  3928. const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
  3929. void *argptr, *p;
  3930. abi_long ret;
  3931. int i, extent_size = thunk_type_size(extent_arg_type, 0);
  3932. uint32_t outbufsz;
  3933. int free_fm = 0;
  3934. assert(arg_type[0] == TYPE_PTR);
  3935. assert(ie->access == IOC_RW);
  3936. arg_type++;
  3937. target_size_in = thunk_type_size(arg_type, 0);
  3938. argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
  3939. if (!argptr) {
  3940. return -TARGET_EFAULT;
  3941. }
  3942. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  3943. unlock_user(argptr, arg, 0);
  3944. fm = (struct fiemap *)buf_temp;
  3945. if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
  3946. return -TARGET_EINVAL;
  3947. }
  3948. outbufsz = sizeof (*fm) +
  3949. (sizeof(struct fiemap_extent) * fm->fm_extent_count);
  3950. if (outbufsz > MAX_STRUCT_SIZE) {
  3951. /* We can't fit all the extents into the fixed size buffer.
  3952. * Allocate one that is large enough and use it instead.
  3953. */
  3954. fm = g_try_malloc(outbufsz);
  3955. if (!fm) {
  3956. return -TARGET_ENOMEM;
  3957. }
  3958. memcpy(fm, buf_temp, sizeof(struct fiemap));
  3959. free_fm = 1;
  3960. }
  3961. ret = get_errno(safe_ioctl(fd, ie->host_cmd, fm));
  3962. if (!is_error(ret)) {
  3963. target_size_out = target_size_in;
  3964. /* An extent_count of 0 means we were only counting the extents
  3965. * so there are no structs to copy
  3966. */
  3967. if (fm->fm_extent_count != 0) {
  3968. target_size_out += fm->fm_mapped_extents * extent_size;
  3969. }
  3970. argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
  3971. if (!argptr) {
  3972. ret = -TARGET_EFAULT;
  3973. } else {
  3974. /* Convert the struct fiemap */
  3975. thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
  3976. if (fm->fm_extent_count != 0) {
  3977. p = argptr + target_size_in;
  3978. /* ...and then all the struct fiemap_extents */
  3979. for (i = 0; i < fm->fm_mapped_extents; i++) {
  3980. thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
  3981. THUNK_TARGET);
  3982. p += extent_size;
  3983. }
  3984. }
  3985. unlock_user(argptr, arg, target_size_out);
  3986. }
  3987. }
  3988. if (free_fm) {
  3989. g_free(fm);
  3990. }
  3991. return ret;
  3992. }
  3993. #endif
  3994. static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
  3995. int fd, int cmd, abi_long arg)
  3996. {
  3997. const argtype *arg_type = ie->arg_type;
  3998. int target_size;
  3999. void *argptr;
  4000. int ret;
  4001. struct ifconf *host_ifconf;
  4002. uint32_t outbufsz;
  4003. const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
  4004. int target_ifreq_size;
  4005. int nb_ifreq;
  4006. int free_buf = 0;
  4007. int i;
  4008. int target_ifc_len;
  4009. abi_long target_ifc_buf;
  4010. int host_ifc_len;
  4011. char *host_ifc_buf;
  4012. assert(arg_type[0] == TYPE_PTR);
  4013. assert(ie->access == IOC_RW);
  4014. arg_type++;
  4015. target_size = thunk_type_size(arg_type, 0);
  4016. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4017. if (!argptr)
  4018. return -TARGET_EFAULT;
  4019. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4020. unlock_user(argptr, arg, 0);
  4021. host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
  4022. target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
  4023. target_ifreq_size = thunk_type_size(ifreq_arg_type, 0);
  4024. if (target_ifc_buf != 0) {
  4025. target_ifc_len = host_ifconf->ifc_len;
  4026. nb_ifreq = target_ifc_len / target_ifreq_size;
  4027. host_ifc_len = nb_ifreq * sizeof(struct ifreq);
  4028. outbufsz = sizeof(*host_ifconf) + host_ifc_len;
  4029. if (outbufsz > MAX_STRUCT_SIZE) {
  4030. /*
  4031. * We can't fit all the extents into the fixed size buffer.
  4032. * Allocate one that is large enough and use it instead.
  4033. */
  4034. host_ifconf = malloc(outbufsz);
  4035. if (!host_ifconf) {
  4036. return -TARGET_ENOMEM;
  4037. }
  4038. memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
  4039. free_buf = 1;
  4040. }
  4041. host_ifc_buf = (char *)host_ifconf + sizeof(*host_ifconf);
  4042. host_ifconf->ifc_len = host_ifc_len;
  4043. } else {
  4044. host_ifc_buf = NULL;
  4045. }
  4046. host_ifconf->ifc_buf = host_ifc_buf;
  4047. ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_ifconf));
  4048. if (!is_error(ret)) {
  4049. /* convert host ifc_len to target ifc_len */
  4050. nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
  4051. target_ifc_len = nb_ifreq * target_ifreq_size;
  4052. host_ifconf->ifc_len = target_ifc_len;
  4053. /* restore target ifc_buf */
  4054. host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
  4055. /* copy struct ifconf to target user */
  4056. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4057. if (!argptr)
  4058. return -TARGET_EFAULT;
  4059. thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
  4060. unlock_user(argptr, arg, target_size);
  4061. if (target_ifc_buf != 0) {
  4062. /* copy ifreq[] to target user */
  4063. argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
  4064. for (i = 0; i < nb_ifreq ; i++) {
  4065. thunk_convert(argptr + i * target_ifreq_size,
  4066. host_ifc_buf + i * sizeof(struct ifreq),
  4067. ifreq_arg_type, THUNK_TARGET);
  4068. }
  4069. unlock_user(argptr, target_ifc_buf, target_ifc_len);
  4070. }
  4071. }
  4072. if (free_buf) {
  4073. free(host_ifconf);
  4074. }
  4075. return ret;
  4076. }
  4077. #if defined(CONFIG_USBFS)
  4078. #if HOST_LONG_BITS > 64
  4079. #error USBDEVFS thunks do not support >64 bit hosts yet.
  4080. #endif
  4081. struct live_urb {
  4082. uint64_t target_urb_adr;
  4083. uint64_t target_buf_adr;
  4084. char *target_buf_ptr;
  4085. struct usbdevfs_urb host_urb;
  4086. };
  4087. static GHashTable *usbdevfs_urb_hashtable(void)
  4088. {
  4089. static GHashTable *urb_hashtable;
  4090. if (!urb_hashtable) {
  4091. urb_hashtable = g_hash_table_new(g_int64_hash, g_int64_equal);
  4092. }
  4093. return urb_hashtable;
  4094. }
  4095. static void urb_hashtable_insert(struct live_urb *urb)
  4096. {
  4097. GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
  4098. g_hash_table_insert(urb_hashtable, urb, urb);
  4099. }
  4100. static struct live_urb *urb_hashtable_lookup(uint64_t target_urb_adr)
  4101. {
  4102. GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
  4103. return g_hash_table_lookup(urb_hashtable, &target_urb_adr);
  4104. }
  4105. static void urb_hashtable_remove(struct live_urb *urb)
  4106. {
  4107. GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
  4108. g_hash_table_remove(urb_hashtable, urb);
  4109. }
  4110. static abi_long
  4111. do_ioctl_usbdevfs_reapurb(const IOCTLEntry *ie, uint8_t *buf_temp,
  4112. int fd, int cmd, abi_long arg)
  4113. {
  4114. const argtype usbfsurb_arg_type[] = { MK_STRUCT(STRUCT_usbdevfs_urb) };
  4115. const argtype ptrvoid_arg_type[] = { TYPE_PTRVOID, 0, 0 };
  4116. struct live_urb *lurb;
  4117. void *argptr;
  4118. uint64_t hurb;
  4119. int target_size;
  4120. uintptr_t target_urb_adr;
  4121. abi_long ret;
  4122. target_size = thunk_type_size(usbfsurb_arg_type, THUNK_TARGET);
  4123. memset(buf_temp, 0, sizeof(uint64_t));
  4124. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4125. if (is_error(ret)) {
  4126. return ret;
  4127. }
  4128. memcpy(&hurb, buf_temp, sizeof(uint64_t));
  4129. lurb = (void *)((uintptr_t)hurb - offsetof(struct live_urb, host_urb));
  4130. if (!lurb->target_urb_adr) {
  4131. return -TARGET_EFAULT;
  4132. }
  4133. urb_hashtable_remove(lurb);
  4134. unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr,
  4135. lurb->host_urb.buffer_length);
  4136. lurb->target_buf_ptr = NULL;
  4137. /* restore the guest buffer pointer */
  4138. lurb->host_urb.buffer = (void *)(uintptr_t)lurb->target_buf_adr;
  4139. /* update the guest urb struct */
  4140. argptr = lock_user(VERIFY_WRITE, lurb->target_urb_adr, target_size, 0);
  4141. if (!argptr) {
  4142. g_free(lurb);
  4143. return -TARGET_EFAULT;
  4144. }
  4145. thunk_convert(argptr, &lurb->host_urb, usbfsurb_arg_type, THUNK_TARGET);
  4146. unlock_user(argptr, lurb->target_urb_adr, target_size);
  4147. target_size = thunk_type_size(ptrvoid_arg_type, THUNK_TARGET);
  4148. /* write back the urb handle */
  4149. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4150. if (!argptr) {
  4151. g_free(lurb);
  4152. return -TARGET_EFAULT;
  4153. }
  4154. /* GHashTable uses 64-bit keys but thunk_convert expects uintptr_t */
  4155. target_urb_adr = lurb->target_urb_adr;
  4156. thunk_convert(argptr, &target_urb_adr, ptrvoid_arg_type, THUNK_TARGET);
  4157. unlock_user(argptr, arg, target_size);
  4158. g_free(lurb);
  4159. return ret;
  4160. }
  4161. static abi_long
  4162. do_ioctl_usbdevfs_discardurb(const IOCTLEntry *ie,
  4163. uint8_t *buf_temp __attribute__((unused)),
  4164. int fd, int cmd, abi_long arg)
  4165. {
  4166. struct live_urb *lurb;
  4167. /* map target address back to host URB with metadata. */
  4168. lurb = urb_hashtable_lookup(arg);
  4169. if (!lurb) {
  4170. return -TARGET_EFAULT;
  4171. }
  4172. return get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb));
  4173. }
  4174. static abi_long
  4175. do_ioctl_usbdevfs_submiturb(const IOCTLEntry *ie, uint8_t *buf_temp,
  4176. int fd, int cmd, abi_long arg)
  4177. {
  4178. const argtype *arg_type = ie->arg_type;
  4179. int target_size;
  4180. abi_long ret;
  4181. void *argptr;
  4182. int rw_dir;
  4183. struct live_urb *lurb;
  4184. /*
  4185. * each submitted URB needs to map to a unique ID for the
  4186. * kernel, and that unique ID needs to be a pointer to
  4187. * host memory. hence, we need to malloc for each URB.
  4188. * isochronous transfers have a variable length struct.
  4189. */
  4190. arg_type++;
  4191. target_size = thunk_type_size(arg_type, THUNK_TARGET);
  4192. /* construct host copy of urb and metadata */
  4193. lurb = g_try_malloc0(sizeof(struct live_urb));
  4194. if (!lurb) {
  4195. return -TARGET_ENOMEM;
  4196. }
  4197. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4198. if (!argptr) {
  4199. g_free(lurb);
  4200. return -TARGET_EFAULT;
  4201. }
  4202. thunk_convert(&lurb->host_urb, argptr, arg_type, THUNK_HOST);
  4203. unlock_user(argptr, arg, 0);
  4204. lurb->target_urb_adr = arg;
  4205. lurb->target_buf_adr = (uintptr_t)lurb->host_urb.buffer;
  4206. /* buffer space used depends on endpoint type so lock the entire buffer */
  4207. /* control type urbs should check the buffer contents for true direction */
  4208. rw_dir = lurb->host_urb.endpoint & USB_DIR_IN ? VERIFY_WRITE : VERIFY_READ;
  4209. lurb->target_buf_ptr = lock_user(rw_dir, lurb->target_buf_adr,
  4210. lurb->host_urb.buffer_length, 1);
  4211. if (lurb->target_buf_ptr == NULL) {
  4212. g_free(lurb);
  4213. return -TARGET_EFAULT;
  4214. }
  4215. /* update buffer pointer in host copy */
  4216. lurb->host_urb.buffer = lurb->target_buf_ptr;
  4217. ret = get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb));
  4218. if (is_error(ret)) {
  4219. unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr, 0);
  4220. g_free(lurb);
  4221. } else {
  4222. urb_hashtable_insert(lurb);
  4223. }
  4224. return ret;
  4225. }
  4226. #endif /* CONFIG_USBFS */
  4227. static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
  4228. int cmd, abi_long arg)
  4229. {
  4230. void *argptr;
  4231. struct dm_ioctl *host_dm;
  4232. abi_long guest_data;
  4233. uint32_t guest_data_size;
  4234. int target_size;
  4235. const argtype *arg_type = ie->arg_type;
  4236. abi_long ret;
  4237. void *big_buf = NULL;
  4238. char *host_data;
  4239. arg_type++;
  4240. target_size = thunk_type_size(arg_type, 0);
  4241. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4242. if (!argptr) {
  4243. ret = -TARGET_EFAULT;
  4244. goto out;
  4245. }
  4246. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4247. unlock_user(argptr, arg, 0);
  4248. /* buf_temp is too small, so fetch things into a bigger buffer */
  4249. big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
  4250. memcpy(big_buf, buf_temp, target_size);
  4251. buf_temp = big_buf;
  4252. host_dm = big_buf;
  4253. guest_data = arg + host_dm->data_start;
  4254. if ((guest_data - arg) < 0) {
  4255. ret = -TARGET_EINVAL;
  4256. goto out;
  4257. }
  4258. guest_data_size = host_dm->data_size - host_dm->data_start;
  4259. host_data = (char*)host_dm + host_dm->data_start;
  4260. argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
  4261. if (!argptr) {
  4262. ret = -TARGET_EFAULT;
  4263. goto out;
  4264. }
  4265. switch (ie->host_cmd) {
  4266. case DM_REMOVE_ALL:
  4267. case DM_LIST_DEVICES:
  4268. case DM_DEV_CREATE:
  4269. case DM_DEV_REMOVE:
  4270. case DM_DEV_SUSPEND:
  4271. case DM_DEV_STATUS:
  4272. case DM_DEV_WAIT:
  4273. case DM_TABLE_STATUS:
  4274. case DM_TABLE_CLEAR:
  4275. case DM_TABLE_DEPS:
  4276. case DM_LIST_VERSIONS:
  4277. /* no input data */
  4278. break;
  4279. case DM_DEV_RENAME:
  4280. case DM_DEV_SET_GEOMETRY:
  4281. /* data contains only strings */
  4282. memcpy(host_data, argptr, guest_data_size);
  4283. break;
  4284. case DM_TARGET_MSG:
  4285. memcpy(host_data, argptr, guest_data_size);
  4286. *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
  4287. break;
  4288. case DM_TABLE_LOAD:
  4289. {
  4290. void *gspec = argptr;
  4291. void *cur_data = host_data;
  4292. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
  4293. int spec_size = thunk_type_size(arg_type, 0);
  4294. int i;
  4295. for (i = 0; i < host_dm->target_count; i++) {
  4296. struct dm_target_spec *spec = cur_data;
  4297. uint32_t next;
  4298. int slen;
  4299. thunk_convert(spec, gspec, arg_type, THUNK_HOST);
  4300. slen = strlen((char*)gspec + spec_size) + 1;
  4301. next = spec->next;
  4302. spec->next = sizeof(*spec) + slen;
  4303. strcpy((char*)&spec[1], gspec + spec_size);
  4304. gspec += next;
  4305. cur_data += spec->next;
  4306. }
  4307. break;
  4308. }
  4309. default:
  4310. ret = -TARGET_EINVAL;
  4311. unlock_user(argptr, guest_data, 0);
  4312. goto out;
  4313. }
  4314. unlock_user(argptr, guest_data, 0);
  4315. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4316. if (!is_error(ret)) {
  4317. guest_data = arg + host_dm->data_start;
  4318. guest_data_size = host_dm->data_size - host_dm->data_start;
  4319. argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
  4320. switch (ie->host_cmd) {
  4321. case DM_REMOVE_ALL:
  4322. case DM_DEV_CREATE:
  4323. case DM_DEV_REMOVE:
  4324. case DM_DEV_RENAME:
  4325. case DM_DEV_SUSPEND:
  4326. case DM_DEV_STATUS:
  4327. case DM_TABLE_LOAD:
  4328. case DM_TABLE_CLEAR:
  4329. case DM_TARGET_MSG:
  4330. case DM_DEV_SET_GEOMETRY:
  4331. /* no return data */
  4332. break;
  4333. case DM_LIST_DEVICES:
  4334. {
  4335. struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
  4336. uint32_t remaining_data = guest_data_size;
  4337. void *cur_data = argptr;
  4338. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
  4339. int nl_size = 12; /* can't use thunk_size due to alignment */
  4340. while (1) {
  4341. uint32_t next = nl->next;
  4342. if (next) {
  4343. nl->next = nl_size + (strlen(nl->name) + 1);
  4344. }
  4345. if (remaining_data < nl->next) {
  4346. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  4347. break;
  4348. }
  4349. thunk_convert(cur_data, nl, arg_type, THUNK_TARGET);
  4350. strcpy(cur_data + nl_size, nl->name);
  4351. cur_data += nl->next;
  4352. remaining_data -= nl->next;
  4353. if (!next) {
  4354. break;
  4355. }
  4356. nl = (void*)nl + next;
  4357. }
  4358. break;
  4359. }
  4360. case DM_DEV_WAIT:
  4361. case DM_TABLE_STATUS:
  4362. {
  4363. struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
  4364. void *cur_data = argptr;
  4365. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
  4366. int spec_size = thunk_type_size(arg_type, 0);
  4367. int i;
  4368. for (i = 0; i < host_dm->target_count; i++) {
  4369. uint32_t next = spec->next;
  4370. int slen = strlen((char*)&spec[1]) + 1;
  4371. spec->next = (cur_data - argptr) + spec_size + slen;
  4372. if (guest_data_size < spec->next) {
  4373. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  4374. break;
  4375. }
  4376. thunk_convert(cur_data, spec, arg_type, THUNK_TARGET);
  4377. strcpy(cur_data + spec_size, (char*)&spec[1]);
  4378. cur_data = argptr + spec->next;
  4379. spec = (void*)host_dm + host_dm->data_start + next;
  4380. }
  4381. break;
  4382. }
  4383. case DM_TABLE_DEPS:
  4384. {
  4385. void *hdata = (void*)host_dm + host_dm->data_start;
  4386. int count = *(uint32_t*)hdata;
  4387. uint64_t *hdev = hdata + 8;
  4388. uint64_t *gdev = argptr + 8;
  4389. int i;
  4390. *(uint32_t*)argptr = tswap32(count);
  4391. for (i = 0; i < count; i++) {
  4392. *gdev = tswap64(*hdev);
  4393. gdev++;
  4394. hdev++;
  4395. }
  4396. break;
  4397. }
  4398. case DM_LIST_VERSIONS:
  4399. {
  4400. struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
  4401. uint32_t remaining_data = guest_data_size;
  4402. void *cur_data = argptr;
  4403. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
  4404. int vers_size = thunk_type_size(arg_type, 0);
  4405. while (1) {
  4406. uint32_t next = vers->next;
  4407. if (next) {
  4408. vers->next = vers_size + (strlen(vers->name) + 1);
  4409. }
  4410. if (remaining_data < vers->next) {
  4411. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  4412. break;
  4413. }
  4414. thunk_convert(cur_data, vers, arg_type, THUNK_TARGET);
  4415. strcpy(cur_data + vers_size, vers->name);
  4416. cur_data += vers->next;
  4417. remaining_data -= vers->next;
  4418. if (!next) {
  4419. break;
  4420. }
  4421. vers = (void*)vers + next;
  4422. }
  4423. break;
  4424. }
  4425. default:
  4426. unlock_user(argptr, guest_data, 0);
  4427. ret = -TARGET_EINVAL;
  4428. goto out;
  4429. }
  4430. unlock_user(argptr, guest_data, guest_data_size);
  4431. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4432. if (!argptr) {
  4433. ret = -TARGET_EFAULT;
  4434. goto out;
  4435. }
  4436. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  4437. unlock_user(argptr, arg, target_size);
  4438. }
  4439. out:
  4440. g_free(big_buf);
  4441. return ret;
  4442. }
  4443. static abi_long do_ioctl_blkpg(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
  4444. int cmd, abi_long arg)
  4445. {
  4446. void *argptr;
  4447. int target_size;
  4448. const argtype *arg_type = ie->arg_type;
  4449. const argtype part_arg_type[] = { MK_STRUCT(STRUCT_blkpg_partition) };
  4450. abi_long ret;
  4451. struct blkpg_ioctl_arg *host_blkpg = (void*)buf_temp;
  4452. struct blkpg_partition host_part;
  4453. /* Read and convert blkpg */
  4454. arg_type++;
  4455. target_size = thunk_type_size(arg_type, 0);
  4456. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4457. if (!argptr) {
  4458. ret = -TARGET_EFAULT;
  4459. goto out;
  4460. }
  4461. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4462. unlock_user(argptr, arg, 0);
  4463. switch (host_blkpg->op) {
  4464. case BLKPG_ADD_PARTITION:
  4465. case BLKPG_DEL_PARTITION:
  4466. /* payload is struct blkpg_partition */
  4467. break;
  4468. default:
  4469. /* Unknown opcode */
  4470. ret = -TARGET_EINVAL;
  4471. goto out;
  4472. }
  4473. /* Read and convert blkpg->data */
  4474. arg = (abi_long)(uintptr_t)host_blkpg->data;
  4475. target_size = thunk_type_size(part_arg_type, 0);
  4476. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4477. if (!argptr) {
  4478. ret = -TARGET_EFAULT;
  4479. goto out;
  4480. }
  4481. thunk_convert(&host_part, argptr, part_arg_type, THUNK_HOST);
  4482. unlock_user(argptr, arg, 0);
  4483. /* Swizzle the data pointer to our local copy and call! */
  4484. host_blkpg->data = &host_part;
  4485. ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_blkpg));
  4486. out:
  4487. return ret;
  4488. }
  4489. static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
  4490. int fd, int cmd, abi_long arg)
  4491. {
  4492. const argtype *arg_type = ie->arg_type;
  4493. const StructEntry *se;
  4494. const argtype *field_types;
  4495. const int *dst_offsets, *src_offsets;
  4496. int target_size;
  4497. void *argptr;
  4498. abi_ulong *target_rt_dev_ptr = NULL;
  4499. unsigned long *host_rt_dev_ptr = NULL;
  4500. abi_long ret;
  4501. int i;
  4502. assert(ie->access == IOC_W);
  4503. assert(*arg_type == TYPE_PTR);
  4504. arg_type++;
  4505. assert(*arg_type == TYPE_STRUCT);
  4506. target_size = thunk_type_size(arg_type, 0);
  4507. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4508. if (!argptr) {
  4509. return -TARGET_EFAULT;
  4510. }
  4511. arg_type++;
  4512. assert(*arg_type == (int)STRUCT_rtentry);
  4513. se = struct_entries + *arg_type++;
  4514. assert(se->convert[0] == NULL);
  4515. /* convert struct here to be able to catch rt_dev string */
  4516. field_types = se->field_types;
  4517. dst_offsets = se->field_offsets[THUNK_HOST];
  4518. src_offsets = se->field_offsets[THUNK_TARGET];
  4519. for (i = 0; i < se->nb_fields; i++) {
  4520. if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) {
  4521. assert(*field_types == TYPE_PTRVOID);
  4522. target_rt_dev_ptr = (abi_ulong *)(argptr + src_offsets[i]);
  4523. host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]);
  4524. if (*target_rt_dev_ptr != 0) {
  4525. *host_rt_dev_ptr = (unsigned long)lock_user_string(
  4526. tswapal(*target_rt_dev_ptr));
  4527. if (!*host_rt_dev_ptr) {
  4528. unlock_user(argptr, arg, 0);
  4529. return -TARGET_EFAULT;
  4530. }
  4531. } else {
  4532. *host_rt_dev_ptr = 0;
  4533. }
  4534. field_types++;
  4535. continue;
  4536. }
  4537. field_types = thunk_convert(buf_temp + dst_offsets[i],
  4538. argptr + src_offsets[i],
  4539. field_types, THUNK_HOST);
  4540. }
  4541. unlock_user(argptr, arg, 0);
  4542. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4543. assert(host_rt_dev_ptr != NULL);
  4544. assert(target_rt_dev_ptr != NULL);
  4545. if (*host_rt_dev_ptr != 0) {
  4546. unlock_user((void *)*host_rt_dev_ptr,
  4547. *target_rt_dev_ptr, 0);
  4548. }
  4549. return ret;
  4550. }
  4551. static abi_long do_ioctl_kdsigaccept(const IOCTLEntry *ie, uint8_t *buf_temp,
  4552. int fd, int cmd, abi_long arg)
  4553. {
  4554. int sig = target_to_host_signal(arg);
  4555. return get_errno(safe_ioctl(fd, ie->host_cmd, sig));
  4556. }
  4557. static abi_long do_ioctl_SIOCGSTAMP(const IOCTLEntry *ie, uint8_t *buf_temp,
  4558. int fd, int cmd, abi_long arg)
  4559. {
  4560. struct timeval tv;
  4561. abi_long ret;
  4562. ret = get_errno(safe_ioctl(fd, SIOCGSTAMP, &tv));
  4563. if (is_error(ret)) {
  4564. return ret;
  4565. }
  4566. if (cmd == (int)TARGET_SIOCGSTAMP_OLD) {
  4567. if (copy_to_user_timeval(arg, &tv)) {
  4568. return -TARGET_EFAULT;
  4569. }
  4570. } else {
  4571. if (copy_to_user_timeval64(arg, &tv)) {
  4572. return -TARGET_EFAULT;
  4573. }
  4574. }
  4575. return ret;
  4576. }
  4577. static abi_long do_ioctl_SIOCGSTAMPNS(const IOCTLEntry *ie, uint8_t *buf_temp,
  4578. int fd, int cmd, abi_long arg)
  4579. {
  4580. struct timespec ts;
  4581. abi_long ret;
  4582. ret = get_errno(safe_ioctl(fd, SIOCGSTAMPNS, &ts));
  4583. if (is_error(ret)) {
  4584. return ret;
  4585. }
  4586. if (cmd == (int)TARGET_SIOCGSTAMPNS_OLD) {
  4587. if (host_to_target_timespec(arg, &ts)) {
  4588. return -TARGET_EFAULT;
  4589. }
  4590. } else{
  4591. if (host_to_target_timespec64(arg, &ts)) {
  4592. return -TARGET_EFAULT;
  4593. }
  4594. }
  4595. return ret;
  4596. }
  4597. #ifdef TIOCGPTPEER
  4598. static abi_long do_ioctl_tiocgptpeer(const IOCTLEntry *ie, uint8_t *buf_temp,
  4599. int fd, int cmd, abi_long arg)
  4600. {
  4601. int flags = target_to_host_bitmask(arg, fcntl_flags_tbl);
  4602. return get_errno(safe_ioctl(fd, ie->host_cmd, flags));
  4603. }
  4604. #endif
  4605. static IOCTLEntry ioctl_entries[] = {
  4606. #define IOCTL(cmd, access, ...) \
  4607. { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
  4608. #define IOCTL_SPECIAL(cmd, access, dofn, ...) \
  4609. { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
  4610. #define IOCTL_IGNORE(cmd) \
  4611. { TARGET_ ## cmd, 0, #cmd },
  4612. #include "ioctls.h"
  4613. { 0, 0, },
  4614. };
  4615. /* ??? Implement proper locking for ioctls. */
  4616. /* do_ioctl() Must return target values and target errnos. */
  4617. static abi_long do_ioctl(int fd, int cmd, abi_long arg)
  4618. {
  4619. const IOCTLEntry *ie;
  4620. const argtype *arg_type;
  4621. abi_long ret;
  4622. uint8_t buf_temp[MAX_STRUCT_SIZE];
  4623. int target_size;
  4624. void *argptr;
  4625. ie = ioctl_entries;
  4626. for(;;) {
  4627. if (ie->target_cmd == 0) {
  4628. gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
  4629. return -TARGET_ENOSYS;
  4630. }
  4631. if (ie->target_cmd == cmd)
  4632. break;
  4633. ie++;
  4634. }
  4635. arg_type = ie->arg_type;
  4636. if (ie->do_ioctl) {
  4637. return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
  4638. } else if (!ie->host_cmd) {
  4639. /* Some architectures define BSD ioctls in their headers
  4640. that are not implemented in Linux. */
  4641. return -TARGET_ENOSYS;
  4642. }
  4643. switch(arg_type[0]) {
  4644. case TYPE_NULL:
  4645. /* no argument */
  4646. ret = get_errno(safe_ioctl(fd, ie->host_cmd));
  4647. break;
  4648. case TYPE_PTRVOID:
  4649. case TYPE_INT:
  4650. ret = get_errno(safe_ioctl(fd, ie->host_cmd, arg));
  4651. break;
  4652. case TYPE_PTR:
  4653. arg_type++;
  4654. target_size = thunk_type_size(arg_type, 0);
  4655. switch(ie->access) {
  4656. case IOC_R:
  4657. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4658. if (!is_error(ret)) {
  4659. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4660. if (!argptr)
  4661. return -TARGET_EFAULT;
  4662. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  4663. unlock_user(argptr, arg, target_size);
  4664. }
  4665. break;
  4666. case IOC_W:
  4667. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4668. if (!argptr)
  4669. return -TARGET_EFAULT;
  4670. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4671. unlock_user(argptr, arg, 0);
  4672. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4673. break;
  4674. default:
  4675. case IOC_RW:
  4676. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4677. if (!argptr)
  4678. return -TARGET_EFAULT;
  4679. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4680. unlock_user(argptr, arg, 0);
  4681. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4682. if (!is_error(ret)) {
  4683. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4684. if (!argptr)
  4685. return -TARGET_EFAULT;
  4686. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  4687. unlock_user(argptr, arg, target_size);
  4688. }
  4689. break;
  4690. }
  4691. break;
  4692. default:
  4693. gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
  4694. (long)cmd, arg_type[0]);
  4695. ret = -TARGET_ENOSYS;
  4696. break;
  4697. }
  4698. return ret;
  4699. }
  4700. static const bitmask_transtbl iflag_tbl[] = {
  4701. { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
  4702. { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
  4703. { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
  4704. { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
  4705. { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
  4706. { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
  4707. { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
  4708. { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
  4709. { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
  4710. { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
  4711. { TARGET_IXON, TARGET_IXON, IXON, IXON },
  4712. { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
  4713. { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
  4714. { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
  4715. { 0, 0, 0, 0 }
  4716. };
  4717. static const bitmask_transtbl oflag_tbl[] = {
  4718. { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
  4719. { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
  4720. { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
  4721. { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
  4722. { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
  4723. { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
  4724. { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
  4725. { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
  4726. { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
  4727. { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
  4728. { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
  4729. { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
  4730. { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
  4731. { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
  4732. { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
  4733. { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
  4734. { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
  4735. { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
  4736. { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
  4737. { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
  4738. { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
  4739. { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
  4740. { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
  4741. { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
  4742. { 0, 0, 0, 0 }
  4743. };
  4744. static const bitmask_transtbl cflag_tbl[] = {
  4745. { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
  4746. { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
  4747. { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
  4748. { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
  4749. { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
  4750. { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
  4751. { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
  4752. { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
  4753. { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
  4754. { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
  4755. { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
  4756. { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
  4757. { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
  4758. { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
  4759. { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
  4760. { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
  4761. { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
  4762. { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
  4763. { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
  4764. { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
  4765. { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
  4766. { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
  4767. { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
  4768. { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
  4769. { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
  4770. { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
  4771. { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
  4772. { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
  4773. { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
  4774. { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
  4775. { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
  4776. { 0, 0, 0, 0 }
  4777. };
  4778. static const bitmask_transtbl lflag_tbl[] = {
  4779. { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
  4780. { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
  4781. { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
  4782. { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
  4783. { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
  4784. { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
  4785. { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
  4786. { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
  4787. { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
  4788. { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
  4789. { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
  4790. { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
  4791. { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
  4792. { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
  4793. { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
  4794. { 0, 0, 0, 0 }
  4795. };
  4796. static void target_to_host_termios (void *dst, const void *src)
  4797. {
  4798. struct host_termios *host = dst;
  4799. const struct target_termios *target = src;
  4800. host->c_iflag =
  4801. target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
  4802. host->c_oflag =
  4803. target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
  4804. host->c_cflag =
  4805. target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
  4806. host->c_lflag =
  4807. target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
  4808. host->c_line = target->c_line;
  4809. memset(host->c_cc, 0, sizeof(host->c_cc));
  4810. host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
  4811. host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
  4812. host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
  4813. host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
  4814. host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
  4815. host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
  4816. host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
  4817. host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
  4818. host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
  4819. host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
  4820. host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
  4821. host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
  4822. host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
  4823. host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
  4824. host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
  4825. host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
  4826. host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
  4827. }
  4828. static void host_to_target_termios (void *dst, const void *src)
  4829. {
  4830. struct target_termios *target = dst;
  4831. const struct host_termios *host = src;
  4832. target->c_iflag =
  4833. tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
  4834. target->c_oflag =
  4835. tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
  4836. target->c_cflag =
  4837. tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
  4838. target->c_lflag =
  4839. tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
  4840. target->c_line = host->c_line;
  4841. memset(target->c_cc, 0, sizeof(target->c_cc));
  4842. target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
  4843. target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
  4844. target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
  4845. target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
  4846. target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
  4847. target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
  4848. target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
  4849. target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
  4850. target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
  4851. target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
  4852. target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
  4853. target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
  4854. target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
  4855. target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
  4856. target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
  4857. target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
  4858. target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
  4859. }
  4860. static const StructEntry struct_termios_def = {
  4861. .convert = { host_to_target_termios, target_to_host_termios },
  4862. .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
  4863. .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
  4864. };
  4865. static bitmask_transtbl mmap_flags_tbl[] = {
  4866. { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
  4867. { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
  4868. { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
  4869. { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS,
  4870. MAP_ANONYMOUS, MAP_ANONYMOUS },
  4871. { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN,
  4872. MAP_GROWSDOWN, MAP_GROWSDOWN },
  4873. { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE,
  4874. MAP_DENYWRITE, MAP_DENYWRITE },
  4875. { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE,
  4876. MAP_EXECUTABLE, MAP_EXECUTABLE },
  4877. { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
  4878. { TARGET_MAP_NORESERVE, TARGET_MAP_NORESERVE,
  4879. MAP_NORESERVE, MAP_NORESERVE },
  4880. { TARGET_MAP_HUGETLB, TARGET_MAP_HUGETLB, MAP_HUGETLB, MAP_HUGETLB },
  4881. /* MAP_STACK had been ignored by the kernel for quite some time.
  4882. Recognize it for the target insofar as we do not want to pass
  4883. it through to the host. */
  4884. { TARGET_MAP_STACK, TARGET_MAP_STACK, 0, 0 },
  4885. { 0, 0, 0, 0 }
  4886. };
  4887. #if defined(TARGET_I386)
  4888. /* NOTE: there is really one LDT for all the threads */
  4889. static uint8_t *ldt_table;
  4890. static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
  4891. {
  4892. int size;
  4893. void *p;
  4894. if (!ldt_table)
  4895. return 0;
  4896. size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
  4897. if (size > bytecount)
  4898. size = bytecount;
  4899. p = lock_user(VERIFY_WRITE, ptr, size, 0);
  4900. if (!p)
  4901. return -TARGET_EFAULT;
  4902. /* ??? Should this by byteswapped? */
  4903. memcpy(p, ldt_table, size);
  4904. unlock_user(p, ptr, size);
  4905. return size;
  4906. }
  4907. /* XXX: add locking support */
  4908. static abi_long write_ldt(CPUX86State *env,
  4909. abi_ulong ptr, unsigned long bytecount, int oldmode)
  4910. {
  4911. struct target_modify_ldt_ldt_s ldt_info;
  4912. struct target_modify_ldt_ldt_s *target_ldt_info;
  4913. int seg_32bit, contents, read_exec_only, limit_in_pages;
  4914. int seg_not_present, useable, lm;
  4915. uint32_t *lp, entry_1, entry_2;
  4916. if (bytecount != sizeof(ldt_info))
  4917. return -TARGET_EINVAL;
  4918. if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
  4919. return -TARGET_EFAULT;
  4920. ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
  4921. ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
  4922. ldt_info.limit = tswap32(target_ldt_info->limit);
  4923. ldt_info.flags = tswap32(target_ldt_info->flags);
  4924. unlock_user_struct(target_ldt_info, ptr, 0);
  4925. if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
  4926. return -TARGET_EINVAL;
  4927. seg_32bit = ldt_info.flags & 1;
  4928. contents = (ldt_info.flags >> 1) & 3;
  4929. read_exec_only = (ldt_info.flags >> 3) & 1;
  4930. limit_in_pages = (ldt_info.flags >> 4) & 1;
  4931. seg_not_present = (ldt_info.flags >> 5) & 1;
  4932. useable = (ldt_info.flags >> 6) & 1;
  4933. #ifdef TARGET_ABI32
  4934. lm = 0;
  4935. #else
  4936. lm = (ldt_info.flags >> 7) & 1;
  4937. #endif
  4938. if (contents == 3) {
  4939. if (oldmode)
  4940. return -TARGET_EINVAL;
  4941. if (seg_not_present == 0)
  4942. return -TARGET_EINVAL;
  4943. }
  4944. /* allocate the LDT */
  4945. if (!ldt_table) {
  4946. env->ldt.base = target_mmap(0,
  4947. TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
  4948. PROT_READ|PROT_WRITE,
  4949. MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
  4950. if (env->ldt.base == -1)
  4951. return -TARGET_ENOMEM;
  4952. memset(g2h(env->ldt.base), 0,
  4953. TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
  4954. env->ldt.limit = 0xffff;
  4955. ldt_table = g2h(env->ldt.base);
  4956. }
  4957. /* NOTE: same code as Linux kernel */
  4958. /* Allow LDTs to be cleared by the user. */
  4959. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  4960. if (oldmode ||
  4961. (contents == 0 &&
  4962. read_exec_only == 1 &&
  4963. seg_32bit == 0 &&
  4964. limit_in_pages == 0 &&
  4965. seg_not_present == 1 &&
  4966. useable == 0 )) {
  4967. entry_1 = 0;
  4968. entry_2 = 0;
  4969. goto install;
  4970. }
  4971. }
  4972. entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
  4973. (ldt_info.limit & 0x0ffff);
  4974. entry_2 = (ldt_info.base_addr & 0xff000000) |
  4975. ((ldt_info.base_addr & 0x00ff0000) >> 16) |
  4976. (ldt_info.limit & 0xf0000) |
  4977. ((read_exec_only ^ 1) << 9) |
  4978. (contents << 10) |
  4979. ((seg_not_present ^ 1) << 15) |
  4980. (seg_32bit << 22) |
  4981. (limit_in_pages << 23) |
  4982. (lm << 21) |
  4983. 0x7000;
  4984. if (!oldmode)
  4985. entry_2 |= (useable << 20);
  4986. /* Install the new entry ... */
  4987. install:
  4988. lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
  4989. lp[0] = tswap32(entry_1);
  4990. lp[1] = tswap32(entry_2);
  4991. return 0;
  4992. }
  4993. /* specific and weird i386 syscalls */
  4994. static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
  4995. unsigned long bytecount)
  4996. {
  4997. abi_long ret;
  4998. switch (func) {
  4999. case 0:
  5000. ret = read_ldt(ptr, bytecount);
  5001. break;
  5002. case 1:
  5003. ret = write_ldt(env, ptr, bytecount, 1);
  5004. break;
  5005. case 0x11:
  5006. ret = write_ldt(env, ptr, bytecount, 0);
  5007. break;
  5008. default:
  5009. ret = -TARGET_ENOSYS;
  5010. break;
  5011. }
  5012. return ret;
  5013. }
  5014. #if defined(TARGET_I386) && defined(TARGET_ABI32)
  5015. abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
  5016. {
  5017. uint64_t *gdt_table = g2h(env->gdt.base);
  5018. struct target_modify_ldt_ldt_s ldt_info;
  5019. struct target_modify_ldt_ldt_s *target_ldt_info;
  5020. int seg_32bit, contents, read_exec_only, limit_in_pages;
  5021. int seg_not_present, useable, lm;
  5022. uint32_t *lp, entry_1, entry_2;
  5023. int i;
  5024. lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
  5025. if (!target_ldt_info)
  5026. return -TARGET_EFAULT;
  5027. ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
  5028. ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
  5029. ldt_info.limit = tswap32(target_ldt_info->limit);
  5030. ldt_info.flags = tswap32(target_ldt_info->flags);
  5031. if (ldt_info.entry_number == -1) {
  5032. for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
  5033. if (gdt_table[i] == 0) {
  5034. ldt_info.entry_number = i;
  5035. target_ldt_info->entry_number = tswap32(i);
  5036. break;
  5037. }
  5038. }
  5039. }
  5040. unlock_user_struct(target_ldt_info, ptr, 1);
  5041. if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
  5042. ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
  5043. return -TARGET_EINVAL;
  5044. seg_32bit = ldt_info.flags & 1;
  5045. contents = (ldt_info.flags >> 1) & 3;
  5046. read_exec_only = (ldt_info.flags >> 3) & 1;
  5047. limit_in_pages = (ldt_info.flags >> 4) & 1;
  5048. seg_not_present = (ldt_info.flags >> 5) & 1;
  5049. useable = (ldt_info.flags >> 6) & 1;
  5050. #ifdef TARGET_ABI32
  5051. lm = 0;
  5052. #else
  5053. lm = (ldt_info.flags >> 7) & 1;
  5054. #endif
  5055. if (contents == 3) {
  5056. if (seg_not_present == 0)
  5057. return -TARGET_EINVAL;
  5058. }
  5059. /* NOTE: same code as Linux kernel */
  5060. /* Allow LDTs to be cleared by the user. */
  5061. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  5062. if ((contents == 0 &&
  5063. read_exec_only == 1 &&
  5064. seg_32bit == 0 &&
  5065. limit_in_pages == 0 &&
  5066. seg_not_present == 1 &&
  5067. useable == 0 )) {
  5068. entry_1 = 0;
  5069. entry_2 = 0;
  5070. goto install;
  5071. }
  5072. }
  5073. entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
  5074. (ldt_info.limit & 0x0ffff);
  5075. entry_2 = (ldt_info.base_addr & 0xff000000) |
  5076. ((ldt_info.base_addr & 0x00ff0000) >> 16) |
  5077. (ldt_info.limit & 0xf0000) |
  5078. ((read_exec_only ^ 1) << 9) |
  5079. (contents << 10) |
  5080. ((seg_not_present ^ 1) << 15) |
  5081. (seg_32bit << 22) |
  5082. (limit_in_pages << 23) |
  5083. (useable << 20) |
  5084. (lm << 21) |
  5085. 0x7000;
  5086. /* Install the new entry ... */
  5087. install:
  5088. lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
  5089. lp[0] = tswap32(entry_1);
  5090. lp[1] = tswap32(entry_2);
  5091. return 0;
  5092. }
  5093. static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
  5094. {
  5095. struct target_modify_ldt_ldt_s *target_ldt_info;
  5096. uint64_t *gdt_table = g2h(env->gdt.base);
  5097. uint32_t base_addr, limit, flags;
  5098. int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
  5099. int seg_not_present, useable, lm;
  5100. uint32_t *lp, entry_1, entry_2;
  5101. lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
  5102. if (!target_ldt_info)
  5103. return -TARGET_EFAULT;
  5104. idx = tswap32(target_ldt_info->entry_number);
  5105. if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
  5106. idx > TARGET_GDT_ENTRY_TLS_MAX) {
  5107. unlock_user_struct(target_ldt_info, ptr, 1);
  5108. return -TARGET_EINVAL;
  5109. }
  5110. lp = (uint32_t *)(gdt_table + idx);
  5111. entry_1 = tswap32(lp[0]);
  5112. entry_2 = tswap32(lp[1]);
  5113. read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
  5114. contents = (entry_2 >> 10) & 3;
  5115. seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
  5116. seg_32bit = (entry_2 >> 22) & 1;
  5117. limit_in_pages = (entry_2 >> 23) & 1;
  5118. useable = (entry_2 >> 20) & 1;
  5119. #ifdef TARGET_ABI32
  5120. lm = 0;
  5121. #else
  5122. lm = (entry_2 >> 21) & 1;
  5123. #endif
  5124. flags = (seg_32bit << 0) | (contents << 1) |
  5125. (read_exec_only << 3) | (limit_in_pages << 4) |
  5126. (seg_not_present << 5) | (useable << 6) | (lm << 7);
  5127. limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
  5128. base_addr = (entry_1 >> 16) |
  5129. (entry_2 & 0xff000000) |
  5130. ((entry_2 & 0xff) << 16);
  5131. target_ldt_info->base_addr = tswapal(base_addr);
  5132. target_ldt_info->limit = tswap32(limit);
  5133. target_ldt_info->flags = tswap32(flags);
  5134. unlock_user_struct(target_ldt_info, ptr, 1);
  5135. return 0;
  5136. }
  5137. #endif /* TARGET_I386 && TARGET_ABI32 */
  5138. #ifndef TARGET_ABI32
  5139. abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
  5140. {
  5141. abi_long ret = 0;
  5142. abi_ulong val;
  5143. int idx;
  5144. switch(code) {
  5145. case TARGET_ARCH_SET_GS:
  5146. case TARGET_ARCH_SET_FS:
  5147. if (code == TARGET_ARCH_SET_GS)
  5148. idx = R_GS;
  5149. else
  5150. idx = R_FS;
  5151. cpu_x86_load_seg(env, idx, 0);
  5152. env->segs[idx].base = addr;
  5153. break;
  5154. case TARGET_ARCH_GET_GS:
  5155. case TARGET_ARCH_GET_FS:
  5156. if (code == TARGET_ARCH_GET_GS)
  5157. idx = R_GS;
  5158. else
  5159. idx = R_FS;
  5160. val = env->segs[idx].base;
  5161. if (put_user(val, addr, abi_ulong))
  5162. ret = -TARGET_EFAULT;
  5163. break;
  5164. default:
  5165. ret = -TARGET_EINVAL;
  5166. break;
  5167. }
  5168. return ret;
  5169. }
  5170. #endif
  5171. #endif /* defined(TARGET_I386) */
  5172. #define NEW_STACK_SIZE 0x40000
  5173. static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
  5174. typedef struct {
  5175. CPUArchState *env;
  5176. pthread_mutex_t mutex;
  5177. pthread_cond_t cond;
  5178. pthread_t thread;
  5179. uint32_t tid;
  5180. abi_ulong child_tidptr;
  5181. abi_ulong parent_tidptr;
  5182. sigset_t sigmask;
  5183. } new_thread_info;
  5184. static void *clone_func(void *arg)
  5185. {
  5186. new_thread_info *info = arg;
  5187. CPUArchState *env;
  5188. CPUState *cpu;
  5189. TaskState *ts;
  5190. rcu_register_thread();
  5191. tcg_register_thread();
  5192. env = info->env;
  5193. cpu = env_cpu(env);
  5194. thread_cpu = cpu;
  5195. ts = (TaskState *)cpu->opaque;
  5196. info->tid = sys_gettid();
  5197. task_settid(ts);
  5198. if (info->child_tidptr)
  5199. put_user_u32(info->tid, info->child_tidptr);
  5200. if (info->parent_tidptr)
  5201. put_user_u32(info->tid, info->parent_tidptr);
  5202. qemu_guest_random_seed_thread_part2(cpu->random_seed);
  5203. /* Enable signals. */
  5204. sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
  5205. /* Signal to the parent that we're ready. */
  5206. pthread_mutex_lock(&info->mutex);
  5207. pthread_cond_broadcast(&info->cond);
  5208. pthread_mutex_unlock(&info->mutex);
  5209. /* Wait until the parent has finished initializing the tls state. */
  5210. pthread_mutex_lock(&clone_lock);
  5211. pthread_mutex_unlock(&clone_lock);
  5212. cpu_loop(env);
  5213. /* never exits */
  5214. return NULL;
  5215. }
  5216. /* do_fork() Must return host values and target errnos (unlike most
  5217. do_*() functions). */
  5218. static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
  5219. abi_ulong parent_tidptr, target_ulong newtls,
  5220. abi_ulong child_tidptr)
  5221. {
  5222. CPUState *cpu = env_cpu(env);
  5223. int ret;
  5224. TaskState *ts;
  5225. CPUState *new_cpu;
  5226. CPUArchState *new_env;
  5227. sigset_t sigmask;
  5228. flags &= ~CLONE_IGNORED_FLAGS;
  5229. /* Emulate vfork() with fork() */
  5230. if (flags & CLONE_VFORK)
  5231. flags &= ~(CLONE_VFORK | CLONE_VM);
  5232. if (flags & CLONE_VM) {
  5233. TaskState *parent_ts = (TaskState *)cpu->opaque;
  5234. new_thread_info info;
  5235. pthread_attr_t attr;
  5236. if (((flags & CLONE_THREAD_FLAGS) != CLONE_THREAD_FLAGS) ||
  5237. (flags & CLONE_INVALID_THREAD_FLAGS)) {
  5238. return -TARGET_EINVAL;
  5239. }
  5240. ts = g_new0(TaskState, 1);
  5241. init_task_state(ts);
  5242. /* Grab a mutex so that thread setup appears atomic. */
  5243. pthread_mutex_lock(&clone_lock);
  5244. /* we create a new CPU instance. */
  5245. new_env = cpu_copy(env);
  5246. /* Init regs that differ from the parent. */
  5247. cpu_clone_regs_child(new_env, newsp, flags);
  5248. cpu_clone_regs_parent(env, flags);
  5249. new_cpu = env_cpu(new_env);
  5250. new_cpu->opaque = ts;
  5251. ts->bprm = parent_ts->bprm;
  5252. ts->info = parent_ts->info;
  5253. ts->signal_mask = parent_ts->signal_mask;
  5254. if (flags & CLONE_CHILD_CLEARTID) {
  5255. ts->child_tidptr = child_tidptr;
  5256. }
  5257. if (flags & CLONE_SETTLS) {
  5258. cpu_set_tls (new_env, newtls);
  5259. }
  5260. memset(&info, 0, sizeof(info));
  5261. pthread_mutex_init(&info.mutex, NULL);
  5262. pthread_mutex_lock(&info.mutex);
  5263. pthread_cond_init(&info.cond, NULL);
  5264. info.env = new_env;
  5265. if (flags & CLONE_CHILD_SETTID) {
  5266. info.child_tidptr = child_tidptr;
  5267. }
  5268. if (flags & CLONE_PARENT_SETTID) {
  5269. info.parent_tidptr = parent_tidptr;
  5270. }
  5271. ret = pthread_attr_init(&attr);
  5272. ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
  5273. ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  5274. /* It is not safe to deliver signals until the child has finished
  5275. initializing, so temporarily block all signals. */
  5276. sigfillset(&sigmask);
  5277. sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
  5278. cpu->random_seed = qemu_guest_random_seed_thread_part1();
  5279. /* If this is our first additional thread, we need to ensure we
  5280. * generate code for parallel execution and flush old translations.
  5281. */
  5282. if (!parallel_cpus) {
  5283. parallel_cpus = true;
  5284. tb_flush(cpu);
  5285. }
  5286. ret = pthread_create(&info.thread, &attr, clone_func, &info);
  5287. /* TODO: Free new CPU state if thread creation failed. */
  5288. sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
  5289. pthread_attr_destroy(&attr);
  5290. if (ret == 0) {
  5291. /* Wait for the child to initialize. */
  5292. pthread_cond_wait(&info.cond, &info.mutex);
  5293. ret = info.tid;
  5294. } else {
  5295. ret = -1;
  5296. }
  5297. pthread_mutex_unlock(&info.mutex);
  5298. pthread_cond_destroy(&info.cond);
  5299. pthread_mutex_destroy(&info.mutex);
  5300. pthread_mutex_unlock(&clone_lock);
  5301. } else {
  5302. /* if no CLONE_VM, we consider it is a fork */
  5303. if (flags & CLONE_INVALID_FORK_FLAGS) {
  5304. return -TARGET_EINVAL;
  5305. }
  5306. /* We can't support custom termination signals */
  5307. if ((flags & CSIGNAL) != TARGET_SIGCHLD) {
  5308. return -TARGET_EINVAL;
  5309. }
  5310. if (block_signals()) {
  5311. return -TARGET_ERESTARTSYS;
  5312. }
  5313. fork_start();
  5314. ret = fork();
  5315. if (ret == 0) {
  5316. /* Child Process. */
  5317. cpu_clone_regs_child(env, newsp, flags);
  5318. fork_end(1);
  5319. /* There is a race condition here. The parent process could
  5320. theoretically read the TID in the child process before the child
  5321. tid is set. This would require using either ptrace
  5322. (not implemented) or having *_tidptr to point at a shared memory
  5323. mapping. We can't repeat the spinlock hack used above because
  5324. the child process gets its own copy of the lock. */
  5325. if (flags & CLONE_CHILD_SETTID)
  5326. put_user_u32(sys_gettid(), child_tidptr);
  5327. if (flags & CLONE_PARENT_SETTID)
  5328. put_user_u32(sys_gettid(), parent_tidptr);
  5329. ts = (TaskState *)cpu->opaque;
  5330. if (flags & CLONE_SETTLS)
  5331. cpu_set_tls (env, newtls);
  5332. if (flags & CLONE_CHILD_CLEARTID)
  5333. ts->child_tidptr = child_tidptr;
  5334. } else {
  5335. cpu_clone_regs_parent(env, flags);
  5336. fork_end(0);
  5337. }
  5338. }
  5339. return ret;
  5340. }
  5341. /* warning : doesn't handle linux specific flags... */
  5342. static int target_to_host_fcntl_cmd(int cmd)
  5343. {
  5344. int ret;
  5345. switch(cmd) {
  5346. case TARGET_F_DUPFD:
  5347. case TARGET_F_GETFD:
  5348. case TARGET_F_SETFD:
  5349. case TARGET_F_GETFL:
  5350. case TARGET_F_SETFL:
  5351. ret = cmd;
  5352. break;
  5353. case TARGET_F_GETLK:
  5354. ret = F_GETLK64;
  5355. break;
  5356. case TARGET_F_SETLK:
  5357. ret = F_SETLK64;
  5358. break;
  5359. case TARGET_F_SETLKW:
  5360. ret = F_SETLKW64;
  5361. break;
  5362. case TARGET_F_GETOWN:
  5363. ret = F_GETOWN;
  5364. break;
  5365. case TARGET_F_SETOWN:
  5366. ret = F_SETOWN;
  5367. break;
  5368. case TARGET_F_GETSIG:
  5369. ret = F_GETSIG;
  5370. break;
  5371. case TARGET_F_SETSIG:
  5372. ret = F_SETSIG;
  5373. break;
  5374. #if TARGET_ABI_BITS == 32
  5375. case TARGET_F_GETLK64:
  5376. ret = F_GETLK64;
  5377. break;
  5378. case TARGET_F_SETLK64:
  5379. ret = F_SETLK64;
  5380. break;
  5381. case TARGET_F_SETLKW64:
  5382. ret = F_SETLKW64;
  5383. break;
  5384. #endif
  5385. case TARGET_F_SETLEASE:
  5386. ret = F_SETLEASE;
  5387. break;
  5388. case TARGET_F_GETLEASE:
  5389. ret = F_GETLEASE;
  5390. break;
  5391. #ifdef F_DUPFD_CLOEXEC
  5392. case TARGET_F_DUPFD_CLOEXEC:
  5393. ret = F_DUPFD_CLOEXEC;
  5394. break;
  5395. #endif
  5396. case TARGET_F_NOTIFY:
  5397. ret = F_NOTIFY;
  5398. break;
  5399. #ifdef F_GETOWN_EX
  5400. case TARGET_F_GETOWN_EX:
  5401. ret = F_GETOWN_EX;
  5402. break;
  5403. #endif
  5404. #ifdef F_SETOWN_EX
  5405. case TARGET_F_SETOWN_EX:
  5406. ret = F_SETOWN_EX;
  5407. break;
  5408. #endif
  5409. #ifdef F_SETPIPE_SZ
  5410. case TARGET_F_SETPIPE_SZ:
  5411. ret = F_SETPIPE_SZ;
  5412. break;
  5413. case TARGET_F_GETPIPE_SZ:
  5414. ret = F_GETPIPE_SZ;
  5415. break;
  5416. #endif
  5417. default:
  5418. ret = -TARGET_EINVAL;
  5419. break;
  5420. }
  5421. #if defined(__powerpc64__)
  5422. /* On PPC64, glibc headers has the F_*LK* defined to 12, 13 and 14 and
  5423. * is not supported by kernel. The glibc fcntl call actually adjusts
  5424. * them to 5, 6 and 7 before making the syscall(). Since we make the
  5425. * syscall directly, adjust to what is supported by the kernel.
  5426. */
  5427. if (ret >= F_GETLK64 && ret <= F_SETLKW64) {
  5428. ret -= F_GETLK64 - 5;
  5429. }
  5430. #endif
  5431. return ret;
  5432. }
  5433. #define FLOCK_TRANSTBL \
  5434. switch (type) { \
  5435. TRANSTBL_CONVERT(F_RDLCK); \
  5436. TRANSTBL_CONVERT(F_WRLCK); \
  5437. TRANSTBL_CONVERT(F_UNLCK); \
  5438. TRANSTBL_CONVERT(F_EXLCK); \
  5439. TRANSTBL_CONVERT(F_SHLCK); \
  5440. }
  5441. static int target_to_host_flock(int type)
  5442. {
  5443. #define TRANSTBL_CONVERT(a) case TARGET_##a: return a
  5444. FLOCK_TRANSTBL
  5445. #undef TRANSTBL_CONVERT
  5446. return -TARGET_EINVAL;
  5447. }
  5448. static int host_to_target_flock(int type)
  5449. {
  5450. #define TRANSTBL_CONVERT(a) case a: return TARGET_##a
  5451. FLOCK_TRANSTBL
  5452. #undef TRANSTBL_CONVERT
  5453. /* if we don't know how to convert the value coming
  5454. * from the host we copy to the target field as-is
  5455. */
  5456. return type;
  5457. }
  5458. static inline abi_long copy_from_user_flock(struct flock64 *fl,
  5459. abi_ulong target_flock_addr)
  5460. {
  5461. struct target_flock *target_fl;
  5462. int l_type;
  5463. if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
  5464. return -TARGET_EFAULT;
  5465. }
  5466. __get_user(l_type, &target_fl->l_type);
  5467. l_type = target_to_host_flock(l_type);
  5468. if (l_type < 0) {
  5469. return l_type;
  5470. }
  5471. fl->l_type = l_type;
  5472. __get_user(fl->l_whence, &target_fl->l_whence);
  5473. __get_user(fl->l_start, &target_fl->l_start);
  5474. __get_user(fl->l_len, &target_fl->l_len);
  5475. __get_user(fl->l_pid, &target_fl->l_pid);
  5476. unlock_user_struct(target_fl, target_flock_addr, 0);
  5477. return 0;
  5478. }
  5479. static inline abi_long copy_to_user_flock(abi_ulong target_flock_addr,
  5480. const struct flock64 *fl)
  5481. {
  5482. struct target_flock *target_fl;
  5483. short l_type;
  5484. if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
  5485. return -TARGET_EFAULT;
  5486. }
  5487. l_type = host_to_target_flock(fl->l_type);
  5488. __put_user(l_type, &target_fl->l_type);
  5489. __put_user(fl->l_whence, &target_fl->l_whence);
  5490. __put_user(fl->l_start, &target_fl->l_start);
  5491. __put_user(fl->l_len, &target_fl->l_len);
  5492. __put_user(fl->l_pid, &target_fl->l_pid);
  5493. unlock_user_struct(target_fl, target_flock_addr, 1);
  5494. return 0;
  5495. }
  5496. typedef abi_long from_flock64_fn(struct flock64 *fl, abi_ulong target_addr);
  5497. typedef abi_long to_flock64_fn(abi_ulong target_addr, const struct flock64 *fl);
  5498. #if defined(TARGET_ARM) && TARGET_ABI_BITS == 32
  5499. static inline abi_long copy_from_user_oabi_flock64(struct flock64 *fl,
  5500. abi_ulong target_flock_addr)
  5501. {
  5502. struct target_oabi_flock64 *target_fl;
  5503. int l_type;
  5504. if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
  5505. return -TARGET_EFAULT;
  5506. }
  5507. __get_user(l_type, &target_fl->l_type);
  5508. l_type = target_to_host_flock(l_type);
  5509. if (l_type < 0) {
  5510. return l_type;
  5511. }
  5512. fl->l_type = l_type;
  5513. __get_user(fl->l_whence, &target_fl->l_whence);
  5514. __get_user(fl->l_start, &target_fl->l_start);
  5515. __get_user(fl->l_len, &target_fl->l_len);
  5516. __get_user(fl->l_pid, &target_fl->l_pid);
  5517. unlock_user_struct(target_fl, target_flock_addr, 0);
  5518. return 0;
  5519. }
  5520. static inline abi_long copy_to_user_oabi_flock64(abi_ulong target_flock_addr,
  5521. const struct flock64 *fl)
  5522. {
  5523. struct target_oabi_flock64 *target_fl;
  5524. short l_type;
  5525. if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
  5526. return -TARGET_EFAULT;
  5527. }
  5528. l_type = host_to_target_flock(fl->l_type);
  5529. __put_user(l_type, &target_fl->l_type);
  5530. __put_user(fl->l_whence, &target_fl->l_whence);
  5531. __put_user(fl->l_start, &target_fl->l_start);
  5532. __put_user(fl->l_len, &target_fl->l_len);
  5533. __put_user(fl->l_pid, &target_fl->l_pid);
  5534. unlock_user_struct(target_fl, target_flock_addr, 1);
  5535. return 0;
  5536. }
  5537. #endif
  5538. static inline abi_long copy_from_user_flock64(struct flock64 *fl,
  5539. abi_ulong target_flock_addr)
  5540. {
  5541. struct target_flock64 *target_fl;
  5542. int l_type;
  5543. if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
  5544. return -TARGET_EFAULT;
  5545. }
  5546. __get_user(l_type, &target_fl->l_type);
  5547. l_type = target_to_host_flock(l_type);
  5548. if (l_type < 0) {
  5549. return l_type;
  5550. }
  5551. fl->l_type = l_type;
  5552. __get_user(fl->l_whence, &target_fl->l_whence);
  5553. __get_user(fl->l_start, &target_fl->l_start);
  5554. __get_user(fl->l_len, &target_fl->l_len);
  5555. __get_user(fl->l_pid, &target_fl->l_pid);
  5556. unlock_user_struct(target_fl, target_flock_addr, 0);
  5557. return 0;
  5558. }
  5559. static inline abi_long copy_to_user_flock64(abi_ulong target_flock_addr,
  5560. const struct flock64 *fl)
  5561. {
  5562. struct target_flock64 *target_fl;
  5563. short l_type;
  5564. if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
  5565. return -TARGET_EFAULT;
  5566. }
  5567. l_type = host_to_target_flock(fl->l_type);
  5568. __put_user(l_type, &target_fl->l_type);
  5569. __put_user(fl->l_whence, &target_fl->l_whence);
  5570. __put_user(fl->l_start, &target_fl->l_start);
  5571. __put_user(fl->l_len, &target_fl->l_len);
  5572. __put_user(fl->l_pid, &target_fl->l_pid);
  5573. unlock_user_struct(target_fl, target_flock_addr, 1);
  5574. return 0;
  5575. }
  5576. static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
  5577. {
  5578. struct flock64 fl64;
  5579. #ifdef F_GETOWN_EX
  5580. struct f_owner_ex fox;
  5581. struct target_f_owner_ex *target_fox;
  5582. #endif
  5583. abi_long ret;
  5584. int host_cmd = target_to_host_fcntl_cmd(cmd);
  5585. if (host_cmd == -TARGET_EINVAL)
  5586. return host_cmd;
  5587. switch(cmd) {
  5588. case TARGET_F_GETLK:
  5589. ret = copy_from_user_flock(&fl64, arg);
  5590. if (ret) {
  5591. return ret;
  5592. }
  5593. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  5594. if (ret == 0) {
  5595. ret = copy_to_user_flock(arg, &fl64);
  5596. }
  5597. break;
  5598. case TARGET_F_SETLK:
  5599. case TARGET_F_SETLKW:
  5600. ret = copy_from_user_flock(&fl64, arg);
  5601. if (ret) {
  5602. return ret;
  5603. }
  5604. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  5605. break;
  5606. case TARGET_F_GETLK64:
  5607. ret = copy_from_user_flock64(&fl64, arg);
  5608. if (ret) {
  5609. return ret;
  5610. }
  5611. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  5612. if (ret == 0) {
  5613. ret = copy_to_user_flock64(arg, &fl64);
  5614. }
  5615. break;
  5616. case TARGET_F_SETLK64:
  5617. case TARGET_F_SETLKW64:
  5618. ret = copy_from_user_flock64(&fl64, arg);
  5619. if (ret) {
  5620. return ret;
  5621. }
  5622. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  5623. break;
  5624. case TARGET_F_GETFL:
  5625. ret = get_errno(safe_fcntl(fd, host_cmd, arg));
  5626. if (ret >= 0) {
  5627. ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
  5628. }
  5629. break;
  5630. case TARGET_F_SETFL:
  5631. ret = get_errno(safe_fcntl(fd, host_cmd,
  5632. target_to_host_bitmask(arg,
  5633. fcntl_flags_tbl)));
  5634. break;
  5635. #ifdef F_GETOWN_EX
  5636. case TARGET_F_GETOWN_EX:
  5637. ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
  5638. if (ret >= 0) {
  5639. if (!lock_user_struct(VERIFY_WRITE, target_fox, arg, 0))
  5640. return -TARGET_EFAULT;
  5641. target_fox->type = tswap32(fox.type);
  5642. target_fox->pid = tswap32(fox.pid);
  5643. unlock_user_struct(target_fox, arg, 1);
  5644. }
  5645. break;
  5646. #endif
  5647. #ifdef F_SETOWN_EX
  5648. case TARGET_F_SETOWN_EX:
  5649. if (!lock_user_struct(VERIFY_READ, target_fox, arg, 1))
  5650. return -TARGET_EFAULT;
  5651. fox.type = tswap32(target_fox->type);
  5652. fox.pid = tswap32(target_fox->pid);
  5653. unlock_user_struct(target_fox, arg, 0);
  5654. ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
  5655. break;
  5656. #endif
  5657. case TARGET_F_SETOWN:
  5658. case TARGET_F_GETOWN:
  5659. case TARGET_F_SETSIG:
  5660. case TARGET_F_GETSIG:
  5661. case TARGET_F_SETLEASE:
  5662. case TARGET_F_GETLEASE:
  5663. case TARGET_F_SETPIPE_SZ:
  5664. case TARGET_F_GETPIPE_SZ:
  5665. ret = get_errno(safe_fcntl(fd, host_cmd, arg));
  5666. break;
  5667. default:
  5668. ret = get_errno(safe_fcntl(fd, cmd, arg));
  5669. break;
  5670. }
  5671. return ret;
  5672. }
  5673. #ifdef USE_UID16
  5674. static inline int high2lowuid(int uid)
  5675. {
  5676. if (uid > 65535)
  5677. return 65534;
  5678. else
  5679. return uid;
  5680. }
  5681. static inline int high2lowgid(int gid)
  5682. {
  5683. if (gid > 65535)
  5684. return 65534;
  5685. else
  5686. return gid;
  5687. }
  5688. static inline int low2highuid(int uid)
  5689. {
  5690. if ((int16_t)uid == -1)
  5691. return -1;
  5692. else
  5693. return uid;
  5694. }
  5695. static inline int low2highgid(int gid)
  5696. {
  5697. if ((int16_t)gid == -1)
  5698. return -1;
  5699. else
  5700. return gid;
  5701. }
  5702. static inline int tswapid(int id)
  5703. {
  5704. return tswap16(id);
  5705. }
  5706. #define put_user_id(x, gaddr) put_user_u16(x, gaddr)
  5707. #else /* !USE_UID16 */
  5708. static inline int high2lowuid(int uid)
  5709. {
  5710. return uid;
  5711. }
  5712. static inline int high2lowgid(int gid)
  5713. {
  5714. return gid;
  5715. }
  5716. static inline int low2highuid(int uid)
  5717. {
  5718. return uid;
  5719. }
  5720. static inline int low2highgid(int gid)
  5721. {
  5722. return gid;
  5723. }
  5724. static inline int tswapid(int id)
  5725. {
  5726. return tswap32(id);
  5727. }
  5728. #define put_user_id(x, gaddr) put_user_u32(x, gaddr)
  5729. #endif /* USE_UID16 */
  5730. /* We must do direct syscalls for setting UID/GID, because we want to
  5731. * implement the Linux system call semantics of "change only for this thread",
  5732. * not the libc/POSIX semantics of "change for all threads in process".
  5733. * (See http://ewontfix.com/17/ for more details.)
  5734. * We use the 32-bit version of the syscalls if present; if it is not
  5735. * then either the host architecture supports 32-bit UIDs natively with
  5736. * the standard syscall, or the 16-bit UID is the best we can do.
  5737. */
  5738. #ifdef __NR_setuid32
  5739. #define __NR_sys_setuid __NR_setuid32
  5740. #else
  5741. #define __NR_sys_setuid __NR_setuid
  5742. #endif
  5743. #ifdef __NR_setgid32
  5744. #define __NR_sys_setgid __NR_setgid32
  5745. #else
  5746. #define __NR_sys_setgid __NR_setgid
  5747. #endif
  5748. #ifdef __NR_setresuid32
  5749. #define __NR_sys_setresuid __NR_setresuid32
  5750. #else
  5751. #define __NR_sys_setresuid __NR_setresuid
  5752. #endif
  5753. #ifdef __NR_setresgid32
  5754. #define __NR_sys_setresgid __NR_setresgid32
  5755. #else
  5756. #define __NR_sys_setresgid __NR_setresgid
  5757. #endif
  5758. _syscall1(int, sys_setuid, uid_t, uid)
  5759. _syscall1(int, sys_setgid, gid_t, gid)
  5760. _syscall3(int, sys_setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
  5761. _syscall3(int, sys_setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
  5762. void syscall_init(void)
  5763. {
  5764. IOCTLEntry *ie;
  5765. const argtype *arg_type;
  5766. int size;
  5767. int i;
  5768. thunk_init(STRUCT_MAX);
  5769. #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
  5770. #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
  5771. #include "syscall_types.h"
  5772. #undef STRUCT
  5773. #undef STRUCT_SPECIAL
  5774. /* Build target_to_host_errno_table[] table from
  5775. * host_to_target_errno_table[]. */
  5776. for (i = 0; i < ERRNO_TABLE_SIZE; i++) {
  5777. target_to_host_errno_table[host_to_target_errno_table[i]] = i;
  5778. }
  5779. /* we patch the ioctl size if necessary. We rely on the fact that
  5780. no ioctl has all the bits at '1' in the size field */
  5781. ie = ioctl_entries;
  5782. while (ie->target_cmd != 0) {
  5783. if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
  5784. TARGET_IOC_SIZEMASK) {
  5785. arg_type = ie->arg_type;
  5786. if (arg_type[0] != TYPE_PTR) {
  5787. fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
  5788. ie->target_cmd);
  5789. exit(1);
  5790. }
  5791. arg_type++;
  5792. size = thunk_type_size(arg_type, 0);
  5793. ie->target_cmd = (ie->target_cmd &
  5794. ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
  5795. (size << TARGET_IOC_SIZESHIFT);
  5796. }
  5797. /* automatic consistency check if same arch */
  5798. #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
  5799. (defined(__x86_64__) && defined(TARGET_X86_64))
  5800. if (unlikely(ie->target_cmd != ie->host_cmd)) {
  5801. fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
  5802. ie->name, ie->target_cmd, ie->host_cmd);
  5803. }
  5804. #endif
  5805. ie++;
  5806. }
  5807. }
  5808. #if TARGET_ABI_BITS == 32
  5809. static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
  5810. {
  5811. #ifdef TARGET_WORDS_BIGENDIAN
  5812. return ((uint64_t)word0 << 32) | word1;
  5813. #else
  5814. return ((uint64_t)word1 << 32) | word0;
  5815. #endif
  5816. }
  5817. #else /* TARGET_ABI_BITS == 32 */
  5818. static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
  5819. {
  5820. return word0;
  5821. }
  5822. #endif /* TARGET_ABI_BITS != 32 */
  5823. #ifdef TARGET_NR_truncate64
  5824. static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
  5825. abi_long arg2,
  5826. abi_long arg3,
  5827. abi_long arg4)
  5828. {
  5829. if (regpairs_aligned(cpu_env, TARGET_NR_truncate64)) {
  5830. arg2 = arg3;
  5831. arg3 = arg4;
  5832. }
  5833. return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
  5834. }
  5835. #endif
  5836. #ifdef TARGET_NR_ftruncate64
  5837. static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
  5838. abi_long arg2,
  5839. abi_long arg3,
  5840. abi_long arg4)
  5841. {
  5842. if (regpairs_aligned(cpu_env, TARGET_NR_ftruncate64)) {
  5843. arg2 = arg3;
  5844. arg3 = arg4;
  5845. }
  5846. return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
  5847. }
  5848. #endif
  5849. static inline abi_long target_to_host_itimerspec(struct itimerspec *host_itspec,
  5850. abi_ulong target_addr)
  5851. {
  5852. struct target_itimerspec *target_itspec;
  5853. if (!lock_user_struct(VERIFY_READ, target_itspec, target_addr, 1)) {
  5854. return -TARGET_EFAULT;
  5855. }
  5856. host_itspec->it_interval.tv_sec =
  5857. tswapal(target_itspec->it_interval.tv_sec);
  5858. host_itspec->it_interval.tv_nsec =
  5859. tswapal(target_itspec->it_interval.tv_nsec);
  5860. host_itspec->it_value.tv_sec = tswapal(target_itspec->it_value.tv_sec);
  5861. host_itspec->it_value.tv_nsec = tswapal(target_itspec->it_value.tv_nsec);
  5862. unlock_user_struct(target_itspec, target_addr, 1);
  5863. return 0;
  5864. }
  5865. static inline abi_long host_to_target_itimerspec(abi_ulong target_addr,
  5866. struct itimerspec *host_its)
  5867. {
  5868. struct target_itimerspec *target_itspec;
  5869. if (!lock_user_struct(VERIFY_WRITE, target_itspec, target_addr, 0)) {
  5870. return -TARGET_EFAULT;
  5871. }
  5872. target_itspec->it_interval.tv_sec = tswapal(host_its->it_interval.tv_sec);
  5873. target_itspec->it_interval.tv_nsec = tswapal(host_its->it_interval.tv_nsec);
  5874. target_itspec->it_value.tv_sec = tswapal(host_its->it_value.tv_sec);
  5875. target_itspec->it_value.tv_nsec = tswapal(host_its->it_value.tv_nsec);
  5876. unlock_user_struct(target_itspec, target_addr, 0);
  5877. return 0;
  5878. }
  5879. static inline abi_long target_to_host_timex(struct timex *host_tx,
  5880. abi_long target_addr)
  5881. {
  5882. struct target_timex *target_tx;
  5883. if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) {
  5884. return -TARGET_EFAULT;
  5885. }
  5886. __get_user(host_tx->modes, &target_tx->modes);
  5887. __get_user(host_tx->offset, &target_tx->offset);
  5888. __get_user(host_tx->freq, &target_tx->freq);
  5889. __get_user(host_tx->maxerror, &target_tx->maxerror);
  5890. __get_user(host_tx->esterror, &target_tx->esterror);
  5891. __get_user(host_tx->status, &target_tx->status);
  5892. __get_user(host_tx->constant, &target_tx->constant);
  5893. __get_user(host_tx->precision, &target_tx->precision);
  5894. __get_user(host_tx->tolerance, &target_tx->tolerance);
  5895. __get_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
  5896. __get_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
  5897. __get_user(host_tx->tick, &target_tx->tick);
  5898. __get_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  5899. __get_user(host_tx->jitter, &target_tx->jitter);
  5900. __get_user(host_tx->shift, &target_tx->shift);
  5901. __get_user(host_tx->stabil, &target_tx->stabil);
  5902. __get_user(host_tx->jitcnt, &target_tx->jitcnt);
  5903. __get_user(host_tx->calcnt, &target_tx->calcnt);
  5904. __get_user(host_tx->errcnt, &target_tx->errcnt);
  5905. __get_user(host_tx->stbcnt, &target_tx->stbcnt);
  5906. __get_user(host_tx->tai, &target_tx->tai);
  5907. unlock_user_struct(target_tx, target_addr, 0);
  5908. return 0;
  5909. }
  5910. static inline abi_long host_to_target_timex(abi_long target_addr,
  5911. struct timex *host_tx)
  5912. {
  5913. struct target_timex *target_tx;
  5914. if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) {
  5915. return -TARGET_EFAULT;
  5916. }
  5917. __put_user(host_tx->modes, &target_tx->modes);
  5918. __put_user(host_tx->offset, &target_tx->offset);
  5919. __put_user(host_tx->freq, &target_tx->freq);
  5920. __put_user(host_tx->maxerror, &target_tx->maxerror);
  5921. __put_user(host_tx->esterror, &target_tx->esterror);
  5922. __put_user(host_tx->status, &target_tx->status);
  5923. __put_user(host_tx->constant, &target_tx->constant);
  5924. __put_user(host_tx->precision, &target_tx->precision);
  5925. __put_user(host_tx->tolerance, &target_tx->tolerance);
  5926. __put_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
  5927. __put_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
  5928. __put_user(host_tx->tick, &target_tx->tick);
  5929. __put_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  5930. __put_user(host_tx->jitter, &target_tx->jitter);
  5931. __put_user(host_tx->shift, &target_tx->shift);
  5932. __put_user(host_tx->stabil, &target_tx->stabil);
  5933. __put_user(host_tx->jitcnt, &target_tx->jitcnt);
  5934. __put_user(host_tx->calcnt, &target_tx->calcnt);
  5935. __put_user(host_tx->errcnt, &target_tx->errcnt);
  5936. __put_user(host_tx->stbcnt, &target_tx->stbcnt);
  5937. __put_user(host_tx->tai, &target_tx->tai);
  5938. unlock_user_struct(target_tx, target_addr, 1);
  5939. return 0;
  5940. }
  5941. static inline abi_long target_to_host_sigevent(struct sigevent *host_sevp,
  5942. abi_ulong target_addr)
  5943. {
  5944. struct target_sigevent *target_sevp;
  5945. if (!lock_user_struct(VERIFY_READ, target_sevp, target_addr, 1)) {
  5946. return -TARGET_EFAULT;
  5947. }
  5948. /* This union is awkward on 64 bit systems because it has a 32 bit
  5949. * integer and a pointer in it; we follow the conversion approach
  5950. * used for handling sigval types in signal.c so the guest should get
  5951. * the correct value back even if we did a 64 bit byteswap and it's
  5952. * using the 32 bit integer.
  5953. */
  5954. host_sevp->sigev_value.sival_ptr =
  5955. (void *)(uintptr_t)tswapal(target_sevp->sigev_value.sival_ptr);
  5956. host_sevp->sigev_signo =
  5957. target_to_host_signal(tswap32(target_sevp->sigev_signo));
  5958. host_sevp->sigev_notify = tswap32(target_sevp->sigev_notify);
  5959. host_sevp->_sigev_un._tid = tswap32(target_sevp->_sigev_un._tid);
  5960. unlock_user_struct(target_sevp, target_addr, 1);
  5961. return 0;
  5962. }
  5963. #if defined(TARGET_NR_mlockall)
  5964. static inline int target_to_host_mlockall_arg(int arg)
  5965. {
  5966. int result = 0;
  5967. if (arg & TARGET_MLOCKALL_MCL_CURRENT) {
  5968. result |= MCL_CURRENT;
  5969. }
  5970. if (arg & TARGET_MLOCKALL_MCL_FUTURE) {
  5971. result |= MCL_FUTURE;
  5972. }
  5973. return result;
  5974. }
  5975. #endif
  5976. #if (defined(TARGET_NR_stat64) || defined(TARGET_NR_lstat64) || \
  5977. defined(TARGET_NR_fstat64) || defined(TARGET_NR_fstatat64) || \
  5978. defined(TARGET_NR_newfstatat))
  5979. static inline abi_long host_to_target_stat64(void *cpu_env,
  5980. abi_ulong target_addr,
  5981. struct stat *host_st)
  5982. {
  5983. #if defined(TARGET_ARM) && defined(TARGET_ABI32)
  5984. if (((CPUARMState *)cpu_env)->eabi) {
  5985. struct target_eabi_stat64 *target_st;
  5986. if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
  5987. return -TARGET_EFAULT;
  5988. memset(target_st, 0, sizeof(struct target_eabi_stat64));
  5989. __put_user(host_st->st_dev, &target_st->st_dev);
  5990. __put_user(host_st->st_ino, &target_st->st_ino);
  5991. #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
  5992. __put_user(host_st->st_ino, &target_st->__st_ino);
  5993. #endif
  5994. __put_user(host_st->st_mode, &target_st->st_mode);
  5995. __put_user(host_st->st_nlink, &target_st->st_nlink);
  5996. __put_user(host_st->st_uid, &target_st->st_uid);
  5997. __put_user(host_st->st_gid, &target_st->st_gid);
  5998. __put_user(host_st->st_rdev, &target_st->st_rdev);
  5999. __put_user(host_st->st_size, &target_st->st_size);
  6000. __put_user(host_st->st_blksize, &target_st->st_blksize);
  6001. __put_user(host_st->st_blocks, &target_st->st_blocks);
  6002. __put_user(host_st->st_atime, &target_st->target_st_atime);
  6003. __put_user(host_st->st_mtime, &target_st->target_st_mtime);
  6004. __put_user(host_st->st_ctime, &target_st->target_st_ctime);
  6005. #if _POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700
  6006. __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
  6007. __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
  6008. __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
  6009. #endif
  6010. unlock_user_struct(target_st, target_addr, 1);
  6011. } else
  6012. #endif
  6013. {
  6014. #if defined(TARGET_HAS_STRUCT_STAT64)
  6015. struct target_stat64 *target_st;
  6016. #else
  6017. struct target_stat *target_st;
  6018. #endif
  6019. if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
  6020. return -TARGET_EFAULT;
  6021. memset(target_st, 0, sizeof(*target_st));
  6022. __put_user(host_st->st_dev, &target_st->st_dev);
  6023. __put_user(host_st->st_ino, &target_st->st_ino);
  6024. #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
  6025. __put_user(host_st->st_ino, &target_st->__st_ino);
  6026. #endif
  6027. __put_user(host_st->st_mode, &target_st->st_mode);
  6028. __put_user(host_st->st_nlink, &target_st->st_nlink);
  6029. __put_user(host_st->st_uid, &target_st->st_uid);
  6030. __put_user(host_st->st_gid, &target_st->st_gid);
  6031. __put_user(host_st->st_rdev, &target_st->st_rdev);
  6032. /* XXX: better use of kernel struct */
  6033. __put_user(host_st->st_size, &target_st->st_size);
  6034. __put_user(host_st->st_blksize, &target_st->st_blksize);
  6035. __put_user(host_st->st_blocks, &target_st->st_blocks);
  6036. __put_user(host_st->st_atime, &target_st->target_st_atime);
  6037. __put_user(host_st->st_mtime, &target_st->target_st_mtime);
  6038. __put_user(host_st->st_ctime, &target_st->target_st_ctime);
  6039. #if _POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700
  6040. __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
  6041. __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
  6042. __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
  6043. #endif
  6044. unlock_user_struct(target_st, target_addr, 1);
  6045. }
  6046. return 0;
  6047. }
  6048. #endif
  6049. #if defined(TARGET_NR_statx) && defined(__NR_statx)
  6050. static inline abi_long host_to_target_statx(struct target_statx *host_stx,
  6051. abi_ulong target_addr)
  6052. {
  6053. struct target_statx *target_stx;
  6054. if (!lock_user_struct(VERIFY_WRITE, target_stx, target_addr, 0)) {
  6055. return -TARGET_EFAULT;
  6056. }
  6057. memset(target_stx, 0, sizeof(*target_stx));
  6058. __put_user(host_stx->stx_mask, &target_stx->stx_mask);
  6059. __put_user(host_stx->stx_blksize, &target_stx->stx_blksize);
  6060. __put_user(host_stx->stx_attributes, &target_stx->stx_attributes);
  6061. __put_user(host_stx->stx_nlink, &target_stx->stx_nlink);
  6062. __put_user(host_stx->stx_uid, &target_stx->stx_uid);
  6063. __put_user(host_stx->stx_gid, &target_stx->stx_gid);
  6064. __put_user(host_stx->stx_mode, &target_stx->stx_mode);
  6065. __put_user(host_stx->stx_ino, &target_stx->stx_ino);
  6066. __put_user(host_stx->stx_size, &target_stx->stx_size);
  6067. __put_user(host_stx->stx_blocks, &target_stx->stx_blocks);
  6068. __put_user(host_stx->stx_attributes_mask, &target_stx->stx_attributes_mask);
  6069. __put_user(host_stx->stx_atime.tv_sec, &target_stx->stx_atime.tv_sec);
  6070. __put_user(host_stx->stx_atime.tv_nsec, &target_stx->stx_atime.tv_nsec);
  6071. __put_user(host_stx->stx_btime.tv_sec, &target_stx->stx_btime.tv_sec);
  6072. __put_user(host_stx->stx_btime.tv_nsec, &target_stx->stx_btime.tv_nsec);
  6073. __put_user(host_stx->stx_ctime.tv_sec, &target_stx->stx_ctime.tv_sec);
  6074. __put_user(host_stx->stx_ctime.tv_nsec, &target_stx->stx_ctime.tv_nsec);
  6075. __put_user(host_stx->stx_mtime.tv_sec, &target_stx->stx_mtime.tv_sec);
  6076. __put_user(host_stx->stx_mtime.tv_nsec, &target_stx->stx_mtime.tv_nsec);
  6077. __put_user(host_stx->stx_rdev_major, &target_stx->stx_rdev_major);
  6078. __put_user(host_stx->stx_rdev_minor, &target_stx->stx_rdev_minor);
  6079. __put_user(host_stx->stx_dev_major, &target_stx->stx_dev_major);
  6080. __put_user(host_stx->stx_dev_minor, &target_stx->stx_dev_minor);
  6081. unlock_user_struct(target_stx, target_addr, 1);
  6082. return 0;
  6083. }
  6084. #endif
  6085. /* ??? Using host futex calls even when target atomic operations
  6086. are not really atomic probably breaks things. However implementing
  6087. futexes locally would make futexes shared between multiple processes
  6088. tricky. However they're probably useless because guest atomic
  6089. operations won't work either. */
  6090. static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
  6091. target_ulong uaddr2, int val3)
  6092. {
  6093. struct timespec ts, *pts;
  6094. int base_op;
  6095. /* ??? We assume FUTEX_* constants are the same on both host
  6096. and target. */
  6097. #ifdef FUTEX_CMD_MASK
  6098. base_op = op & FUTEX_CMD_MASK;
  6099. #else
  6100. base_op = op;
  6101. #endif
  6102. switch (base_op) {
  6103. case FUTEX_WAIT:
  6104. case FUTEX_WAIT_BITSET:
  6105. if (timeout) {
  6106. pts = &ts;
  6107. target_to_host_timespec(pts, timeout);
  6108. } else {
  6109. pts = NULL;
  6110. }
  6111. return get_errno(safe_futex(g2h(uaddr), op, tswap32(val),
  6112. pts, NULL, val3));
  6113. case FUTEX_WAKE:
  6114. return get_errno(safe_futex(g2h(uaddr), op, val, NULL, NULL, 0));
  6115. case FUTEX_FD:
  6116. return get_errno(safe_futex(g2h(uaddr), op, val, NULL, NULL, 0));
  6117. case FUTEX_REQUEUE:
  6118. case FUTEX_CMP_REQUEUE:
  6119. case FUTEX_WAKE_OP:
  6120. /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
  6121. TIMEOUT parameter is interpreted as a uint32_t by the kernel.
  6122. But the prototype takes a `struct timespec *'; insert casts
  6123. to satisfy the compiler. We do not need to tswap TIMEOUT
  6124. since it's not compared to guest memory. */
  6125. pts = (struct timespec *)(uintptr_t) timeout;
  6126. return get_errno(safe_futex(g2h(uaddr), op, val, pts,
  6127. g2h(uaddr2),
  6128. (base_op == FUTEX_CMP_REQUEUE
  6129. ? tswap32(val3)
  6130. : val3)));
  6131. default:
  6132. return -TARGET_ENOSYS;
  6133. }
  6134. }
  6135. #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  6136. static abi_long do_name_to_handle_at(abi_long dirfd, abi_long pathname,
  6137. abi_long handle, abi_long mount_id,
  6138. abi_long flags)
  6139. {
  6140. struct file_handle *target_fh;
  6141. struct file_handle *fh;
  6142. int mid = 0;
  6143. abi_long ret;
  6144. char *name;
  6145. unsigned int size, total_size;
  6146. if (get_user_s32(size, handle)) {
  6147. return -TARGET_EFAULT;
  6148. }
  6149. name = lock_user_string(pathname);
  6150. if (!name) {
  6151. return -TARGET_EFAULT;
  6152. }
  6153. total_size = sizeof(struct file_handle) + size;
  6154. target_fh = lock_user(VERIFY_WRITE, handle, total_size, 0);
  6155. if (!target_fh) {
  6156. unlock_user(name, pathname, 0);
  6157. return -TARGET_EFAULT;
  6158. }
  6159. fh = g_malloc0(total_size);
  6160. fh->handle_bytes = size;
  6161. ret = get_errno(name_to_handle_at(dirfd, path(name), fh, &mid, flags));
  6162. unlock_user(name, pathname, 0);
  6163. /* man name_to_handle_at(2):
  6164. * Other than the use of the handle_bytes field, the caller should treat
  6165. * the file_handle structure as an opaque data type
  6166. */
  6167. memcpy(target_fh, fh, total_size);
  6168. target_fh->handle_bytes = tswap32(fh->handle_bytes);
  6169. target_fh->handle_type = tswap32(fh->handle_type);
  6170. g_free(fh);
  6171. unlock_user(target_fh, handle, total_size);
  6172. if (put_user_s32(mid, mount_id)) {
  6173. return -TARGET_EFAULT;
  6174. }
  6175. return ret;
  6176. }
  6177. #endif
  6178. #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  6179. static abi_long do_open_by_handle_at(abi_long mount_fd, abi_long handle,
  6180. abi_long flags)
  6181. {
  6182. struct file_handle *target_fh;
  6183. struct file_handle *fh;
  6184. unsigned int size, total_size;
  6185. abi_long ret;
  6186. if (get_user_s32(size, handle)) {
  6187. return -TARGET_EFAULT;
  6188. }
  6189. total_size = sizeof(struct file_handle) + size;
  6190. target_fh = lock_user(VERIFY_READ, handle, total_size, 1);
  6191. if (!target_fh) {
  6192. return -TARGET_EFAULT;
  6193. }
  6194. fh = g_memdup(target_fh, total_size);
  6195. fh->handle_bytes = size;
  6196. fh->handle_type = tswap32(target_fh->handle_type);
  6197. ret = get_errno(open_by_handle_at(mount_fd, fh,
  6198. target_to_host_bitmask(flags, fcntl_flags_tbl)));
  6199. g_free(fh);
  6200. unlock_user(target_fh, handle, total_size);
  6201. return ret;
  6202. }
  6203. #endif
  6204. #if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4)
  6205. static abi_long do_signalfd4(int fd, abi_long mask, int flags)
  6206. {
  6207. int host_flags;
  6208. target_sigset_t *target_mask;
  6209. sigset_t host_mask;
  6210. abi_long ret;
  6211. if (flags & ~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC)) {
  6212. return -TARGET_EINVAL;
  6213. }
  6214. if (!lock_user_struct(VERIFY_READ, target_mask, mask, 1)) {
  6215. return -TARGET_EFAULT;
  6216. }
  6217. target_to_host_sigset(&host_mask, target_mask);
  6218. host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
  6219. ret = get_errno(signalfd(fd, &host_mask, host_flags));
  6220. if (ret >= 0) {
  6221. fd_trans_register(ret, &target_signalfd_trans);
  6222. }
  6223. unlock_user_struct(target_mask, mask, 0);
  6224. return ret;
  6225. }
  6226. #endif
  6227. /* Map host to target signal numbers for the wait family of syscalls.
  6228. Assume all other status bits are the same. */
  6229. int host_to_target_waitstatus(int status)
  6230. {
  6231. if (WIFSIGNALED(status)) {
  6232. return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
  6233. }
  6234. if (WIFSTOPPED(status)) {
  6235. return (host_to_target_signal(WSTOPSIG(status)) << 8)
  6236. | (status & 0xff);
  6237. }
  6238. return status;
  6239. }
  6240. static int open_self_cmdline(void *cpu_env, int fd)
  6241. {
  6242. CPUState *cpu = env_cpu((CPUArchState *)cpu_env);
  6243. struct linux_binprm *bprm = ((TaskState *)cpu->opaque)->bprm;
  6244. int i;
  6245. for (i = 0; i < bprm->argc; i++) {
  6246. size_t len = strlen(bprm->argv[i]) + 1;
  6247. if (write(fd, bprm->argv[i], len) != len) {
  6248. return -1;
  6249. }
  6250. }
  6251. return 0;
  6252. }
  6253. static int open_self_maps(void *cpu_env, int fd)
  6254. {
  6255. CPUState *cpu = env_cpu((CPUArchState *)cpu_env);
  6256. TaskState *ts = cpu->opaque;
  6257. FILE *fp;
  6258. char *line = NULL;
  6259. size_t len = 0;
  6260. ssize_t read;
  6261. fp = fopen("/proc/self/maps", "r");
  6262. if (fp == NULL) {
  6263. return -1;
  6264. }
  6265. while ((read = getline(&line, &len, fp)) != -1) {
  6266. int fields, dev_maj, dev_min, inode;
  6267. uint64_t min, max, offset;
  6268. char flag_r, flag_w, flag_x, flag_p;
  6269. char path[512] = "";
  6270. fields = sscanf(line, "%"PRIx64"-%"PRIx64" %c%c%c%c %"PRIx64" %x:%x %d"
  6271. " %512s", &min, &max, &flag_r, &flag_w, &flag_x,
  6272. &flag_p, &offset, &dev_maj, &dev_min, &inode, path);
  6273. if ((fields < 10) || (fields > 11)) {
  6274. continue;
  6275. }
  6276. if (h2g_valid(min)) {
  6277. int flags = page_get_flags(h2g(min));
  6278. max = h2g_valid(max - 1) ? max : (uintptr_t)g2h(GUEST_ADDR_MAX) + 1;
  6279. if (page_check_range(h2g(min), max - min, flags) == -1) {
  6280. continue;
  6281. }
  6282. if (h2g(min) == ts->info->stack_limit) {
  6283. pstrcpy(path, sizeof(path), " [stack]");
  6284. }
  6285. dprintf(fd, TARGET_ABI_FMT_ptr "-" TARGET_ABI_FMT_ptr
  6286. " %c%c%c%c %08" PRIx64 " %02x:%02x %d %s%s\n",
  6287. h2g(min), h2g(max - 1) + 1, flag_r, flag_w,
  6288. flag_x, flag_p, offset, dev_maj, dev_min, inode,
  6289. path[0] ? " " : "", path);
  6290. }
  6291. }
  6292. free(line);
  6293. fclose(fp);
  6294. return 0;
  6295. }
  6296. static int open_self_stat(void *cpu_env, int fd)
  6297. {
  6298. CPUState *cpu = env_cpu((CPUArchState *)cpu_env);
  6299. TaskState *ts = cpu->opaque;
  6300. abi_ulong start_stack = ts->info->start_stack;
  6301. int i;
  6302. for (i = 0; i < 44; i++) {
  6303. char buf[128];
  6304. int len;
  6305. uint64_t val = 0;
  6306. if (i == 0) {
  6307. /* pid */
  6308. val = getpid();
  6309. snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
  6310. } else if (i == 1) {
  6311. /* app name */
  6312. snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
  6313. } else if (i == 27) {
  6314. /* stack bottom */
  6315. val = start_stack;
  6316. snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
  6317. } else {
  6318. /* for the rest, there is MasterCard */
  6319. snprintf(buf, sizeof(buf), "0%c", i == 43 ? '\n' : ' ');
  6320. }
  6321. len = strlen(buf);
  6322. if (write(fd, buf, len) != len) {
  6323. return -1;
  6324. }
  6325. }
  6326. return 0;
  6327. }
  6328. static int open_self_auxv(void *cpu_env, int fd)
  6329. {
  6330. CPUState *cpu = env_cpu((CPUArchState *)cpu_env);
  6331. TaskState *ts = cpu->opaque;
  6332. abi_ulong auxv = ts->info->saved_auxv;
  6333. abi_ulong len = ts->info->auxv_len;
  6334. char *ptr;
  6335. /*
  6336. * Auxiliary vector is stored in target process stack.
  6337. * read in whole auxv vector and copy it to file
  6338. */
  6339. ptr = lock_user(VERIFY_READ, auxv, len, 0);
  6340. if (ptr != NULL) {
  6341. while (len > 0) {
  6342. ssize_t r;
  6343. r = write(fd, ptr, len);
  6344. if (r <= 0) {
  6345. break;
  6346. }
  6347. len -= r;
  6348. ptr += r;
  6349. }
  6350. lseek(fd, 0, SEEK_SET);
  6351. unlock_user(ptr, auxv, len);
  6352. }
  6353. return 0;
  6354. }
  6355. static int is_proc_myself(const char *filename, const char *entry)
  6356. {
  6357. if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
  6358. filename += strlen("/proc/");
  6359. if (!strncmp(filename, "self/", strlen("self/"))) {
  6360. filename += strlen("self/");
  6361. } else if (*filename >= '1' && *filename <= '9') {
  6362. char myself[80];
  6363. snprintf(myself, sizeof(myself), "%d/", getpid());
  6364. if (!strncmp(filename, myself, strlen(myself))) {
  6365. filename += strlen(myself);
  6366. } else {
  6367. return 0;
  6368. }
  6369. } else {
  6370. return 0;
  6371. }
  6372. if (!strcmp(filename, entry)) {
  6373. return 1;
  6374. }
  6375. }
  6376. return 0;
  6377. }
  6378. #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN) || \
  6379. defined(TARGET_SPARC) || defined(TARGET_M68K)
  6380. static int is_proc(const char *filename, const char *entry)
  6381. {
  6382. return strcmp(filename, entry) == 0;
  6383. }
  6384. #endif
  6385. #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
  6386. static int open_net_route(void *cpu_env, int fd)
  6387. {
  6388. FILE *fp;
  6389. char *line = NULL;
  6390. size_t len = 0;
  6391. ssize_t read;
  6392. fp = fopen("/proc/net/route", "r");
  6393. if (fp == NULL) {
  6394. return -1;
  6395. }
  6396. /* read header */
  6397. read = getline(&line, &len, fp);
  6398. dprintf(fd, "%s", line);
  6399. /* read routes */
  6400. while ((read = getline(&line, &len, fp)) != -1) {
  6401. char iface[16];
  6402. uint32_t dest, gw, mask;
  6403. unsigned int flags, refcnt, use, metric, mtu, window, irtt;
  6404. int fields;
  6405. fields = sscanf(line,
  6406. "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
  6407. iface, &dest, &gw, &flags, &refcnt, &use, &metric,
  6408. &mask, &mtu, &window, &irtt);
  6409. if (fields != 11) {
  6410. continue;
  6411. }
  6412. dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
  6413. iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
  6414. metric, tswap32(mask), mtu, window, irtt);
  6415. }
  6416. free(line);
  6417. fclose(fp);
  6418. return 0;
  6419. }
  6420. #endif
  6421. #if defined(TARGET_SPARC)
  6422. static int open_cpuinfo(void *cpu_env, int fd)
  6423. {
  6424. dprintf(fd, "type\t\t: sun4u\n");
  6425. return 0;
  6426. }
  6427. #endif
  6428. #if defined(TARGET_M68K)
  6429. static int open_hardware(void *cpu_env, int fd)
  6430. {
  6431. dprintf(fd, "Model:\t\tqemu-m68k\n");
  6432. return 0;
  6433. }
  6434. #endif
  6435. static int do_openat(void *cpu_env, int dirfd, const char *pathname, int flags, mode_t mode)
  6436. {
  6437. struct fake_open {
  6438. const char *filename;
  6439. int (*fill)(void *cpu_env, int fd);
  6440. int (*cmp)(const char *s1, const char *s2);
  6441. };
  6442. const struct fake_open *fake_open;
  6443. static const struct fake_open fakes[] = {
  6444. { "maps", open_self_maps, is_proc_myself },
  6445. { "stat", open_self_stat, is_proc_myself },
  6446. { "auxv", open_self_auxv, is_proc_myself },
  6447. { "cmdline", open_self_cmdline, is_proc_myself },
  6448. #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
  6449. { "/proc/net/route", open_net_route, is_proc },
  6450. #endif
  6451. #if defined(TARGET_SPARC)
  6452. { "/proc/cpuinfo", open_cpuinfo, is_proc },
  6453. #endif
  6454. #if defined(TARGET_M68K)
  6455. { "/proc/hardware", open_hardware, is_proc },
  6456. #endif
  6457. { NULL, NULL, NULL }
  6458. };
  6459. if (is_proc_myself(pathname, "exe")) {
  6460. int execfd = qemu_getauxval(AT_EXECFD);
  6461. return execfd ? execfd : safe_openat(dirfd, exec_path, flags, mode);
  6462. }
  6463. for (fake_open = fakes; fake_open->filename; fake_open++) {
  6464. if (fake_open->cmp(pathname, fake_open->filename)) {
  6465. break;
  6466. }
  6467. }
  6468. if (fake_open->filename) {
  6469. const char *tmpdir;
  6470. char filename[PATH_MAX];
  6471. int fd, r;
  6472. /* create temporary file to map stat to */
  6473. tmpdir = getenv("TMPDIR");
  6474. if (!tmpdir)
  6475. tmpdir = "/tmp";
  6476. snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
  6477. fd = mkstemp(filename);
  6478. if (fd < 0) {
  6479. return fd;
  6480. }
  6481. unlink(filename);
  6482. if ((r = fake_open->fill(cpu_env, fd))) {
  6483. int e = errno;
  6484. close(fd);
  6485. errno = e;
  6486. return r;
  6487. }
  6488. lseek(fd, 0, SEEK_SET);
  6489. return fd;
  6490. }
  6491. return safe_openat(dirfd, path(pathname), flags, mode);
  6492. }
  6493. #define TIMER_MAGIC 0x0caf0000
  6494. #define TIMER_MAGIC_MASK 0xffff0000
  6495. /* Convert QEMU provided timer ID back to internal 16bit index format */
  6496. static target_timer_t get_timer_id(abi_long arg)
  6497. {
  6498. target_timer_t timerid = arg;
  6499. if ((timerid & TIMER_MAGIC_MASK) != TIMER_MAGIC) {
  6500. return -TARGET_EINVAL;
  6501. }
  6502. timerid &= 0xffff;
  6503. if (timerid >= ARRAY_SIZE(g_posix_timers)) {
  6504. return -TARGET_EINVAL;
  6505. }
  6506. return timerid;
  6507. }
  6508. static int target_to_host_cpu_mask(unsigned long *host_mask,
  6509. size_t host_size,
  6510. abi_ulong target_addr,
  6511. size_t target_size)
  6512. {
  6513. unsigned target_bits = sizeof(abi_ulong) * 8;
  6514. unsigned host_bits = sizeof(*host_mask) * 8;
  6515. abi_ulong *target_mask;
  6516. unsigned i, j;
  6517. assert(host_size >= target_size);
  6518. target_mask = lock_user(VERIFY_READ, target_addr, target_size, 1);
  6519. if (!target_mask) {
  6520. return -TARGET_EFAULT;
  6521. }
  6522. memset(host_mask, 0, host_size);
  6523. for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
  6524. unsigned bit = i * target_bits;
  6525. abi_ulong val;
  6526. __get_user(val, &target_mask[i]);
  6527. for (j = 0; j < target_bits; j++, bit++) {
  6528. if (val & (1UL << j)) {
  6529. host_mask[bit / host_bits] |= 1UL << (bit % host_bits);
  6530. }
  6531. }
  6532. }
  6533. unlock_user(target_mask, target_addr, 0);
  6534. return 0;
  6535. }
  6536. static int host_to_target_cpu_mask(const unsigned long *host_mask,
  6537. size_t host_size,
  6538. abi_ulong target_addr,
  6539. size_t target_size)
  6540. {
  6541. unsigned target_bits = sizeof(abi_ulong) * 8;
  6542. unsigned host_bits = sizeof(*host_mask) * 8;
  6543. abi_ulong *target_mask;
  6544. unsigned i, j;
  6545. assert(host_size >= target_size);
  6546. target_mask = lock_user(VERIFY_WRITE, target_addr, target_size, 0);
  6547. if (!target_mask) {
  6548. return -TARGET_EFAULT;
  6549. }
  6550. for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
  6551. unsigned bit = i * target_bits;
  6552. abi_ulong val = 0;
  6553. for (j = 0; j < target_bits; j++, bit++) {
  6554. if (host_mask[bit / host_bits] & (1UL << (bit % host_bits))) {
  6555. val |= 1UL << j;
  6556. }
  6557. }
  6558. __put_user(val, &target_mask[i]);
  6559. }
  6560. unlock_user(target_mask, target_addr, target_size);
  6561. return 0;
  6562. }
  6563. /* This is an internal helper for do_syscall so that it is easier
  6564. * to have a single return point, so that actions, such as logging
  6565. * of syscall results, can be performed.
  6566. * All errnos that do_syscall() returns must be -TARGET_<errcode>.
  6567. */
  6568. static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
  6569. abi_long arg2, abi_long arg3, abi_long arg4,
  6570. abi_long arg5, abi_long arg6, abi_long arg7,
  6571. abi_long arg8)
  6572. {
  6573. CPUState *cpu = env_cpu(cpu_env);
  6574. abi_long ret;
  6575. #if defined(TARGET_NR_stat) || defined(TARGET_NR_stat64) \
  6576. || defined(TARGET_NR_lstat) || defined(TARGET_NR_lstat64) \
  6577. || defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64) \
  6578. || defined(TARGET_NR_statx)
  6579. struct stat st;
  6580. #endif
  6581. #if defined(TARGET_NR_statfs) || defined(TARGET_NR_statfs64) \
  6582. || defined(TARGET_NR_fstatfs)
  6583. struct statfs stfs;
  6584. #endif
  6585. void *p;
  6586. switch(num) {
  6587. case TARGET_NR_exit:
  6588. /* In old applications this may be used to implement _exit(2).
  6589. However in threaded applictions it is used for thread termination,
  6590. and _exit_group is used for application termination.
  6591. Do thread termination if we have more then one thread. */
  6592. if (block_signals()) {
  6593. return -TARGET_ERESTARTSYS;
  6594. }
  6595. cpu_list_lock();
  6596. if (CPU_NEXT(first_cpu)) {
  6597. TaskState *ts;
  6598. /* Remove the CPU from the list. */
  6599. QTAILQ_REMOVE_RCU(&cpus, cpu, node);
  6600. cpu_list_unlock();
  6601. ts = cpu->opaque;
  6602. if (ts->child_tidptr) {
  6603. put_user_u32(0, ts->child_tidptr);
  6604. sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
  6605. NULL, NULL, 0);
  6606. }
  6607. thread_cpu = NULL;
  6608. object_unref(OBJECT(cpu));
  6609. g_free(ts);
  6610. rcu_unregister_thread();
  6611. pthread_exit(NULL);
  6612. }
  6613. cpu_list_unlock();
  6614. preexit_cleanup(cpu_env, arg1);
  6615. _exit(arg1);
  6616. return 0; /* avoid warning */
  6617. case TARGET_NR_read:
  6618. if (arg2 == 0 && arg3 == 0) {
  6619. return get_errno(safe_read(arg1, 0, 0));
  6620. } else {
  6621. if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
  6622. return -TARGET_EFAULT;
  6623. ret = get_errno(safe_read(arg1, p, arg3));
  6624. if (ret >= 0 &&
  6625. fd_trans_host_to_target_data(arg1)) {
  6626. ret = fd_trans_host_to_target_data(arg1)(p, ret);
  6627. }
  6628. unlock_user(p, arg2, ret);
  6629. }
  6630. return ret;
  6631. case TARGET_NR_write:
  6632. if (arg2 == 0 && arg3 == 0) {
  6633. return get_errno(safe_write(arg1, 0, 0));
  6634. }
  6635. if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
  6636. return -TARGET_EFAULT;
  6637. if (fd_trans_target_to_host_data(arg1)) {
  6638. void *copy = g_malloc(arg3);
  6639. memcpy(copy, p, arg3);
  6640. ret = fd_trans_target_to_host_data(arg1)(copy, arg3);
  6641. if (ret >= 0) {
  6642. ret = get_errno(safe_write(arg1, copy, ret));
  6643. }
  6644. g_free(copy);
  6645. } else {
  6646. ret = get_errno(safe_write(arg1, p, arg3));
  6647. }
  6648. unlock_user(p, arg2, 0);
  6649. return ret;
  6650. #ifdef TARGET_NR_open
  6651. case TARGET_NR_open:
  6652. if (!(p = lock_user_string(arg1)))
  6653. return -TARGET_EFAULT;
  6654. ret = get_errno(do_openat(cpu_env, AT_FDCWD, p,
  6655. target_to_host_bitmask(arg2, fcntl_flags_tbl),
  6656. arg3));
  6657. fd_trans_unregister(ret);
  6658. unlock_user(p, arg1, 0);
  6659. return ret;
  6660. #endif
  6661. case TARGET_NR_openat:
  6662. if (!(p = lock_user_string(arg2)))
  6663. return -TARGET_EFAULT;
  6664. ret = get_errno(do_openat(cpu_env, arg1, p,
  6665. target_to_host_bitmask(arg3, fcntl_flags_tbl),
  6666. arg4));
  6667. fd_trans_unregister(ret);
  6668. unlock_user(p, arg2, 0);
  6669. return ret;
  6670. #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  6671. case TARGET_NR_name_to_handle_at:
  6672. ret = do_name_to_handle_at(arg1, arg2, arg3, arg4, arg5);
  6673. return ret;
  6674. #endif
  6675. #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  6676. case TARGET_NR_open_by_handle_at:
  6677. ret = do_open_by_handle_at(arg1, arg2, arg3);
  6678. fd_trans_unregister(ret);
  6679. return ret;
  6680. #endif
  6681. case TARGET_NR_close:
  6682. fd_trans_unregister(arg1);
  6683. return get_errno(close(arg1));
  6684. case TARGET_NR_brk:
  6685. return do_brk(arg1);
  6686. #ifdef TARGET_NR_fork
  6687. case TARGET_NR_fork:
  6688. return get_errno(do_fork(cpu_env, TARGET_SIGCHLD, 0, 0, 0, 0));
  6689. #endif
  6690. #ifdef TARGET_NR_waitpid
  6691. case TARGET_NR_waitpid:
  6692. {
  6693. int status;
  6694. ret = get_errno(safe_wait4(arg1, &status, arg3, 0));
  6695. if (!is_error(ret) && arg2 && ret
  6696. && put_user_s32(host_to_target_waitstatus(status), arg2))
  6697. return -TARGET_EFAULT;
  6698. }
  6699. return ret;
  6700. #endif
  6701. #ifdef TARGET_NR_waitid
  6702. case TARGET_NR_waitid:
  6703. {
  6704. siginfo_t info;
  6705. info.si_pid = 0;
  6706. ret = get_errno(safe_waitid(arg1, arg2, &info, arg4, NULL));
  6707. if (!is_error(ret) && arg3 && info.si_pid != 0) {
  6708. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
  6709. return -TARGET_EFAULT;
  6710. host_to_target_siginfo(p, &info);
  6711. unlock_user(p, arg3, sizeof(target_siginfo_t));
  6712. }
  6713. }
  6714. return ret;
  6715. #endif
  6716. #ifdef TARGET_NR_creat /* not on alpha */
  6717. case TARGET_NR_creat:
  6718. if (!(p = lock_user_string(arg1)))
  6719. return -TARGET_EFAULT;
  6720. ret = get_errno(creat(p, arg2));
  6721. fd_trans_unregister(ret);
  6722. unlock_user(p, arg1, 0);
  6723. return ret;
  6724. #endif
  6725. #ifdef TARGET_NR_link
  6726. case TARGET_NR_link:
  6727. {
  6728. void * p2;
  6729. p = lock_user_string(arg1);
  6730. p2 = lock_user_string(arg2);
  6731. if (!p || !p2)
  6732. ret = -TARGET_EFAULT;
  6733. else
  6734. ret = get_errno(link(p, p2));
  6735. unlock_user(p2, arg2, 0);
  6736. unlock_user(p, arg1, 0);
  6737. }
  6738. return ret;
  6739. #endif
  6740. #if defined(TARGET_NR_linkat)
  6741. case TARGET_NR_linkat:
  6742. {
  6743. void * p2 = NULL;
  6744. if (!arg2 || !arg4)
  6745. return -TARGET_EFAULT;
  6746. p = lock_user_string(arg2);
  6747. p2 = lock_user_string(arg4);
  6748. if (!p || !p2)
  6749. ret = -TARGET_EFAULT;
  6750. else
  6751. ret = get_errno(linkat(arg1, p, arg3, p2, arg5));
  6752. unlock_user(p, arg2, 0);
  6753. unlock_user(p2, arg4, 0);
  6754. }
  6755. return ret;
  6756. #endif
  6757. #ifdef TARGET_NR_unlink
  6758. case TARGET_NR_unlink:
  6759. if (!(p = lock_user_string(arg1)))
  6760. return -TARGET_EFAULT;
  6761. ret = get_errno(unlink(p));
  6762. unlock_user(p, arg1, 0);
  6763. return ret;
  6764. #endif
  6765. #if defined(TARGET_NR_unlinkat)
  6766. case TARGET_NR_unlinkat:
  6767. if (!(p = lock_user_string(arg2)))
  6768. return -TARGET_EFAULT;
  6769. ret = get_errno(unlinkat(arg1, p, arg3));
  6770. unlock_user(p, arg2, 0);
  6771. return ret;
  6772. #endif
  6773. case TARGET_NR_execve:
  6774. {
  6775. char **argp, **envp;
  6776. int argc, envc;
  6777. abi_ulong gp;
  6778. abi_ulong guest_argp;
  6779. abi_ulong guest_envp;
  6780. abi_ulong addr;
  6781. char **q;
  6782. int total_size = 0;
  6783. argc = 0;
  6784. guest_argp = arg2;
  6785. for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
  6786. if (get_user_ual(addr, gp))
  6787. return -TARGET_EFAULT;
  6788. if (!addr)
  6789. break;
  6790. argc++;
  6791. }
  6792. envc = 0;
  6793. guest_envp = arg3;
  6794. for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
  6795. if (get_user_ual(addr, gp))
  6796. return -TARGET_EFAULT;
  6797. if (!addr)
  6798. break;
  6799. envc++;
  6800. }
  6801. argp = g_new0(char *, argc + 1);
  6802. envp = g_new0(char *, envc + 1);
  6803. for (gp = guest_argp, q = argp; gp;
  6804. gp += sizeof(abi_ulong), q++) {
  6805. if (get_user_ual(addr, gp))
  6806. goto execve_efault;
  6807. if (!addr)
  6808. break;
  6809. if (!(*q = lock_user_string(addr)))
  6810. goto execve_efault;
  6811. total_size += strlen(*q) + 1;
  6812. }
  6813. *q = NULL;
  6814. for (gp = guest_envp, q = envp; gp;
  6815. gp += sizeof(abi_ulong), q++) {
  6816. if (get_user_ual(addr, gp))
  6817. goto execve_efault;
  6818. if (!addr)
  6819. break;
  6820. if (!(*q = lock_user_string(addr)))
  6821. goto execve_efault;
  6822. total_size += strlen(*q) + 1;
  6823. }
  6824. *q = NULL;
  6825. if (!(p = lock_user_string(arg1)))
  6826. goto execve_efault;
  6827. /* Although execve() is not an interruptible syscall it is
  6828. * a special case where we must use the safe_syscall wrapper:
  6829. * if we allow a signal to happen before we make the host
  6830. * syscall then we will 'lose' it, because at the point of
  6831. * execve the process leaves QEMU's control. So we use the
  6832. * safe syscall wrapper to ensure that we either take the
  6833. * signal as a guest signal, or else it does not happen
  6834. * before the execve completes and makes it the other
  6835. * program's problem.
  6836. */
  6837. ret = get_errno(safe_execve(p, argp, envp));
  6838. unlock_user(p, arg1, 0);
  6839. goto execve_end;
  6840. execve_efault:
  6841. ret = -TARGET_EFAULT;
  6842. execve_end:
  6843. for (gp = guest_argp, q = argp; *q;
  6844. gp += sizeof(abi_ulong), q++) {
  6845. if (get_user_ual(addr, gp)
  6846. || !addr)
  6847. break;
  6848. unlock_user(*q, addr, 0);
  6849. }
  6850. for (gp = guest_envp, q = envp; *q;
  6851. gp += sizeof(abi_ulong), q++) {
  6852. if (get_user_ual(addr, gp)
  6853. || !addr)
  6854. break;
  6855. unlock_user(*q, addr, 0);
  6856. }
  6857. g_free(argp);
  6858. g_free(envp);
  6859. }
  6860. return ret;
  6861. case TARGET_NR_chdir:
  6862. if (!(p = lock_user_string(arg1)))
  6863. return -TARGET_EFAULT;
  6864. ret = get_errno(chdir(p));
  6865. unlock_user(p, arg1, 0);
  6866. return ret;
  6867. #ifdef TARGET_NR_time
  6868. case TARGET_NR_time:
  6869. {
  6870. time_t host_time;
  6871. ret = get_errno(time(&host_time));
  6872. if (!is_error(ret)
  6873. && arg1
  6874. && put_user_sal(host_time, arg1))
  6875. return -TARGET_EFAULT;
  6876. }
  6877. return ret;
  6878. #endif
  6879. #ifdef TARGET_NR_mknod
  6880. case TARGET_NR_mknod:
  6881. if (!(p = lock_user_string(arg1)))
  6882. return -TARGET_EFAULT;
  6883. ret = get_errno(mknod(p, arg2, arg3));
  6884. unlock_user(p, arg1, 0);
  6885. return ret;
  6886. #endif
  6887. #if defined(TARGET_NR_mknodat)
  6888. case TARGET_NR_mknodat:
  6889. if (!(p = lock_user_string(arg2)))
  6890. return -TARGET_EFAULT;
  6891. ret = get_errno(mknodat(arg1, p, arg3, arg4));
  6892. unlock_user(p, arg2, 0);
  6893. return ret;
  6894. #endif
  6895. #ifdef TARGET_NR_chmod
  6896. case TARGET_NR_chmod:
  6897. if (!(p = lock_user_string(arg1)))
  6898. return -TARGET_EFAULT;
  6899. ret = get_errno(chmod(p, arg2));
  6900. unlock_user(p, arg1, 0);
  6901. return ret;
  6902. #endif
  6903. #ifdef TARGET_NR_lseek
  6904. case TARGET_NR_lseek:
  6905. return get_errno(lseek(arg1, arg2, arg3));
  6906. #endif
  6907. #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
  6908. /* Alpha specific */
  6909. case TARGET_NR_getxpid:
  6910. ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
  6911. return get_errno(getpid());
  6912. #endif
  6913. #ifdef TARGET_NR_getpid
  6914. case TARGET_NR_getpid:
  6915. return get_errno(getpid());
  6916. #endif
  6917. case TARGET_NR_mount:
  6918. {
  6919. /* need to look at the data field */
  6920. void *p2, *p3;
  6921. if (arg1) {
  6922. p = lock_user_string(arg1);
  6923. if (!p) {
  6924. return -TARGET_EFAULT;
  6925. }
  6926. } else {
  6927. p = NULL;
  6928. }
  6929. p2 = lock_user_string(arg2);
  6930. if (!p2) {
  6931. if (arg1) {
  6932. unlock_user(p, arg1, 0);
  6933. }
  6934. return -TARGET_EFAULT;
  6935. }
  6936. if (arg3) {
  6937. p3 = lock_user_string(arg3);
  6938. if (!p3) {
  6939. if (arg1) {
  6940. unlock_user(p, arg1, 0);
  6941. }
  6942. unlock_user(p2, arg2, 0);
  6943. return -TARGET_EFAULT;
  6944. }
  6945. } else {
  6946. p3 = NULL;
  6947. }
  6948. /* FIXME - arg5 should be locked, but it isn't clear how to
  6949. * do that since it's not guaranteed to be a NULL-terminated
  6950. * string.
  6951. */
  6952. if (!arg5) {
  6953. ret = mount(p, p2, p3, (unsigned long)arg4, NULL);
  6954. } else {
  6955. ret = mount(p, p2, p3, (unsigned long)arg4, g2h(arg5));
  6956. }
  6957. ret = get_errno(ret);
  6958. if (arg1) {
  6959. unlock_user(p, arg1, 0);
  6960. }
  6961. unlock_user(p2, arg2, 0);
  6962. if (arg3) {
  6963. unlock_user(p3, arg3, 0);
  6964. }
  6965. }
  6966. return ret;
  6967. #ifdef TARGET_NR_umount
  6968. case TARGET_NR_umount:
  6969. if (!(p = lock_user_string(arg1)))
  6970. return -TARGET_EFAULT;
  6971. ret = get_errno(umount(p));
  6972. unlock_user(p, arg1, 0);
  6973. return ret;
  6974. #endif
  6975. #ifdef TARGET_NR_stime /* not on alpha */
  6976. case TARGET_NR_stime:
  6977. {
  6978. struct timespec ts;
  6979. ts.tv_nsec = 0;
  6980. if (get_user_sal(ts.tv_sec, arg1)) {
  6981. return -TARGET_EFAULT;
  6982. }
  6983. return get_errno(clock_settime(CLOCK_REALTIME, &ts));
  6984. }
  6985. #endif
  6986. #ifdef TARGET_NR_alarm /* not on alpha */
  6987. case TARGET_NR_alarm:
  6988. return alarm(arg1);
  6989. #endif
  6990. #ifdef TARGET_NR_pause /* not on alpha */
  6991. case TARGET_NR_pause:
  6992. if (!block_signals()) {
  6993. sigsuspend(&((TaskState *)cpu->opaque)->signal_mask);
  6994. }
  6995. return -TARGET_EINTR;
  6996. #endif
  6997. #ifdef TARGET_NR_utime
  6998. case TARGET_NR_utime:
  6999. {
  7000. struct utimbuf tbuf, *host_tbuf;
  7001. struct target_utimbuf *target_tbuf;
  7002. if (arg2) {
  7003. if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
  7004. return -TARGET_EFAULT;
  7005. tbuf.actime = tswapal(target_tbuf->actime);
  7006. tbuf.modtime = tswapal(target_tbuf->modtime);
  7007. unlock_user_struct(target_tbuf, arg2, 0);
  7008. host_tbuf = &tbuf;
  7009. } else {
  7010. host_tbuf = NULL;
  7011. }
  7012. if (!(p = lock_user_string(arg1)))
  7013. return -TARGET_EFAULT;
  7014. ret = get_errno(utime(p, host_tbuf));
  7015. unlock_user(p, arg1, 0);
  7016. }
  7017. return ret;
  7018. #endif
  7019. #ifdef TARGET_NR_utimes
  7020. case TARGET_NR_utimes:
  7021. {
  7022. struct timeval *tvp, tv[2];
  7023. if (arg2) {
  7024. if (copy_from_user_timeval(&tv[0], arg2)
  7025. || copy_from_user_timeval(&tv[1],
  7026. arg2 + sizeof(struct target_timeval)))
  7027. return -TARGET_EFAULT;
  7028. tvp = tv;
  7029. } else {
  7030. tvp = NULL;
  7031. }
  7032. if (!(p = lock_user_string(arg1)))
  7033. return -TARGET_EFAULT;
  7034. ret = get_errno(utimes(p, tvp));
  7035. unlock_user(p, arg1, 0);
  7036. }
  7037. return ret;
  7038. #endif
  7039. #if defined(TARGET_NR_futimesat)
  7040. case TARGET_NR_futimesat:
  7041. {
  7042. struct timeval *tvp, tv[2];
  7043. if (arg3) {
  7044. if (copy_from_user_timeval(&tv[0], arg3)
  7045. || copy_from_user_timeval(&tv[1],
  7046. arg3 + sizeof(struct target_timeval)))
  7047. return -TARGET_EFAULT;
  7048. tvp = tv;
  7049. } else {
  7050. tvp = NULL;
  7051. }
  7052. if (!(p = lock_user_string(arg2))) {
  7053. return -TARGET_EFAULT;
  7054. }
  7055. ret = get_errno(futimesat(arg1, path(p), tvp));
  7056. unlock_user(p, arg2, 0);
  7057. }
  7058. return ret;
  7059. #endif
  7060. #ifdef TARGET_NR_access
  7061. case TARGET_NR_access:
  7062. if (!(p = lock_user_string(arg1))) {
  7063. return -TARGET_EFAULT;
  7064. }
  7065. ret = get_errno(access(path(p), arg2));
  7066. unlock_user(p, arg1, 0);
  7067. return ret;
  7068. #endif
  7069. #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
  7070. case TARGET_NR_faccessat:
  7071. if (!(p = lock_user_string(arg2))) {
  7072. return -TARGET_EFAULT;
  7073. }
  7074. ret = get_errno(faccessat(arg1, p, arg3, 0));
  7075. unlock_user(p, arg2, 0);
  7076. return ret;
  7077. #endif
  7078. #ifdef TARGET_NR_nice /* not on alpha */
  7079. case TARGET_NR_nice:
  7080. return get_errno(nice(arg1));
  7081. #endif
  7082. case TARGET_NR_sync:
  7083. sync();
  7084. return 0;
  7085. #if defined(TARGET_NR_syncfs) && defined(CONFIG_SYNCFS)
  7086. case TARGET_NR_syncfs:
  7087. return get_errno(syncfs(arg1));
  7088. #endif
  7089. case TARGET_NR_kill:
  7090. return get_errno(safe_kill(arg1, target_to_host_signal(arg2)));
  7091. #ifdef TARGET_NR_rename
  7092. case TARGET_NR_rename:
  7093. {
  7094. void *p2;
  7095. p = lock_user_string(arg1);
  7096. p2 = lock_user_string(arg2);
  7097. if (!p || !p2)
  7098. ret = -TARGET_EFAULT;
  7099. else
  7100. ret = get_errno(rename(p, p2));
  7101. unlock_user(p2, arg2, 0);
  7102. unlock_user(p, arg1, 0);
  7103. }
  7104. return ret;
  7105. #endif
  7106. #if defined(TARGET_NR_renameat)
  7107. case TARGET_NR_renameat:
  7108. {
  7109. void *p2;
  7110. p = lock_user_string(arg2);
  7111. p2 = lock_user_string(arg4);
  7112. if (!p || !p2)
  7113. ret = -TARGET_EFAULT;
  7114. else
  7115. ret = get_errno(renameat(arg1, p, arg3, p2));
  7116. unlock_user(p2, arg4, 0);
  7117. unlock_user(p, arg2, 0);
  7118. }
  7119. return ret;
  7120. #endif
  7121. #if defined(TARGET_NR_renameat2)
  7122. case TARGET_NR_renameat2:
  7123. {
  7124. void *p2;
  7125. p = lock_user_string(arg2);
  7126. p2 = lock_user_string(arg4);
  7127. if (!p || !p2) {
  7128. ret = -TARGET_EFAULT;
  7129. } else {
  7130. ret = get_errno(sys_renameat2(arg1, p, arg3, p2, arg5));
  7131. }
  7132. unlock_user(p2, arg4, 0);
  7133. unlock_user(p, arg2, 0);
  7134. }
  7135. return ret;
  7136. #endif
  7137. #ifdef TARGET_NR_mkdir
  7138. case TARGET_NR_mkdir:
  7139. if (!(p = lock_user_string(arg1)))
  7140. return -TARGET_EFAULT;
  7141. ret = get_errno(mkdir(p, arg2));
  7142. unlock_user(p, arg1, 0);
  7143. return ret;
  7144. #endif
  7145. #if defined(TARGET_NR_mkdirat)
  7146. case TARGET_NR_mkdirat:
  7147. if (!(p = lock_user_string(arg2)))
  7148. return -TARGET_EFAULT;
  7149. ret = get_errno(mkdirat(arg1, p, arg3));
  7150. unlock_user(p, arg2, 0);
  7151. return ret;
  7152. #endif
  7153. #ifdef TARGET_NR_rmdir
  7154. case TARGET_NR_rmdir:
  7155. if (!(p = lock_user_string(arg1)))
  7156. return -TARGET_EFAULT;
  7157. ret = get_errno(rmdir(p));
  7158. unlock_user(p, arg1, 0);
  7159. return ret;
  7160. #endif
  7161. case TARGET_NR_dup:
  7162. ret = get_errno(dup(arg1));
  7163. if (ret >= 0) {
  7164. fd_trans_dup(arg1, ret);
  7165. }
  7166. return ret;
  7167. #ifdef TARGET_NR_pipe
  7168. case TARGET_NR_pipe:
  7169. return do_pipe(cpu_env, arg1, 0, 0);
  7170. #endif
  7171. #ifdef TARGET_NR_pipe2
  7172. case TARGET_NR_pipe2:
  7173. return do_pipe(cpu_env, arg1,
  7174. target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
  7175. #endif
  7176. case TARGET_NR_times:
  7177. {
  7178. struct target_tms *tmsp;
  7179. struct tms tms;
  7180. ret = get_errno(times(&tms));
  7181. if (arg1) {
  7182. tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
  7183. if (!tmsp)
  7184. return -TARGET_EFAULT;
  7185. tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
  7186. tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
  7187. tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
  7188. tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
  7189. }
  7190. if (!is_error(ret))
  7191. ret = host_to_target_clock_t(ret);
  7192. }
  7193. return ret;
  7194. case TARGET_NR_acct:
  7195. if (arg1 == 0) {
  7196. ret = get_errno(acct(NULL));
  7197. } else {
  7198. if (!(p = lock_user_string(arg1))) {
  7199. return -TARGET_EFAULT;
  7200. }
  7201. ret = get_errno(acct(path(p)));
  7202. unlock_user(p, arg1, 0);
  7203. }
  7204. return ret;
  7205. #ifdef TARGET_NR_umount2
  7206. case TARGET_NR_umount2:
  7207. if (!(p = lock_user_string(arg1)))
  7208. return -TARGET_EFAULT;
  7209. ret = get_errno(umount2(p, arg2));
  7210. unlock_user(p, arg1, 0);
  7211. return ret;
  7212. #endif
  7213. case TARGET_NR_ioctl:
  7214. return do_ioctl(arg1, arg2, arg3);
  7215. #ifdef TARGET_NR_fcntl
  7216. case TARGET_NR_fcntl:
  7217. return do_fcntl(arg1, arg2, arg3);
  7218. #endif
  7219. case TARGET_NR_setpgid:
  7220. return get_errno(setpgid(arg1, arg2));
  7221. case TARGET_NR_umask:
  7222. return get_errno(umask(arg1));
  7223. case TARGET_NR_chroot:
  7224. if (!(p = lock_user_string(arg1)))
  7225. return -TARGET_EFAULT;
  7226. ret = get_errno(chroot(p));
  7227. unlock_user(p, arg1, 0);
  7228. return ret;
  7229. #ifdef TARGET_NR_dup2
  7230. case TARGET_NR_dup2:
  7231. ret = get_errno(dup2(arg1, arg2));
  7232. if (ret >= 0) {
  7233. fd_trans_dup(arg1, arg2);
  7234. }
  7235. return ret;
  7236. #endif
  7237. #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
  7238. case TARGET_NR_dup3:
  7239. {
  7240. int host_flags;
  7241. if ((arg3 & ~TARGET_O_CLOEXEC) != 0) {
  7242. return -EINVAL;
  7243. }
  7244. host_flags = target_to_host_bitmask(arg3, fcntl_flags_tbl);
  7245. ret = get_errno(dup3(arg1, arg2, host_flags));
  7246. if (ret >= 0) {
  7247. fd_trans_dup(arg1, arg2);
  7248. }
  7249. return ret;
  7250. }
  7251. #endif
  7252. #ifdef TARGET_NR_getppid /* not on alpha */
  7253. case TARGET_NR_getppid:
  7254. return get_errno(getppid());
  7255. #endif
  7256. #ifdef TARGET_NR_getpgrp
  7257. case TARGET_NR_getpgrp:
  7258. return get_errno(getpgrp());
  7259. #endif
  7260. case TARGET_NR_setsid:
  7261. return get_errno(setsid());
  7262. #ifdef TARGET_NR_sigaction
  7263. case TARGET_NR_sigaction:
  7264. {
  7265. #if defined(TARGET_ALPHA)
  7266. struct target_sigaction act, oact, *pact = 0;
  7267. struct target_old_sigaction *old_act;
  7268. if (arg2) {
  7269. if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
  7270. return -TARGET_EFAULT;
  7271. act._sa_handler = old_act->_sa_handler;
  7272. target_siginitset(&act.sa_mask, old_act->sa_mask);
  7273. act.sa_flags = old_act->sa_flags;
  7274. act.sa_restorer = 0;
  7275. unlock_user_struct(old_act, arg2, 0);
  7276. pact = &act;
  7277. }
  7278. ret = get_errno(do_sigaction(arg1, pact, &oact));
  7279. if (!is_error(ret) && arg3) {
  7280. if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
  7281. return -TARGET_EFAULT;
  7282. old_act->_sa_handler = oact._sa_handler;
  7283. old_act->sa_mask = oact.sa_mask.sig[0];
  7284. old_act->sa_flags = oact.sa_flags;
  7285. unlock_user_struct(old_act, arg3, 1);
  7286. }
  7287. #elif defined(TARGET_MIPS)
  7288. struct target_sigaction act, oact, *pact, *old_act;
  7289. if (arg2) {
  7290. if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
  7291. return -TARGET_EFAULT;
  7292. act._sa_handler = old_act->_sa_handler;
  7293. target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
  7294. act.sa_flags = old_act->sa_flags;
  7295. unlock_user_struct(old_act, arg2, 0);
  7296. pact = &act;
  7297. } else {
  7298. pact = NULL;
  7299. }
  7300. ret = get_errno(do_sigaction(arg1, pact, &oact));
  7301. if (!is_error(ret) && arg3) {
  7302. if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
  7303. return -TARGET_EFAULT;
  7304. old_act->_sa_handler = oact._sa_handler;
  7305. old_act->sa_flags = oact.sa_flags;
  7306. old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
  7307. old_act->sa_mask.sig[1] = 0;
  7308. old_act->sa_mask.sig[2] = 0;
  7309. old_act->sa_mask.sig[3] = 0;
  7310. unlock_user_struct(old_act, arg3, 1);
  7311. }
  7312. #else
  7313. struct target_old_sigaction *old_act;
  7314. struct target_sigaction act, oact, *pact;
  7315. if (arg2) {
  7316. if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
  7317. return -TARGET_EFAULT;
  7318. act._sa_handler = old_act->_sa_handler;
  7319. target_siginitset(&act.sa_mask, old_act->sa_mask);
  7320. act.sa_flags = old_act->sa_flags;
  7321. act.sa_restorer = old_act->sa_restorer;
  7322. #ifdef TARGET_ARCH_HAS_KA_RESTORER
  7323. act.ka_restorer = 0;
  7324. #endif
  7325. unlock_user_struct(old_act, arg2, 0);
  7326. pact = &act;
  7327. } else {
  7328. pact = NULL;
  7329. }
  7330. ret = get_errno(do_sigaction(arg1, pact, &oact));
  7331. if (!is_error(ret) && arg3) {
  7332. if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
  7333. return -TARGET_EFAULT;
  7334. old_act->_sa_handler = oact._sa_handler;
  7335. old_act->sa_mask = oact.sa_mask.sig[0];
  7336. old_act->sa_flags = oact.sa_flags;
  7337. old_act->sa_restorer = oact.sa_restorer;
  7338. unlock_user_struct(old_act, arg3, 1);
  7339. }
  7340. #endif
  7341. }
  7342. return ret;
  7343. #endif
  7344. case TARGET_NR_rt_sigaction:
  7345. {
  7346. #if defined(TARGET_ALPHA)
  7347. /* For Alpha and SPARC this is a 5 argument syscall, with
  7348. * a 'restorer' parameter which must be copied into the
  7349. * sa_restorer field of the sigaction struct.
  7350. * For Alpha that 'restorer' is arg5; for SPARC it is arg4,
  7351. * and arg5 is the sigsetsize.
  7352. * Alpha also has a separate rt_sigaction struct that it uses
  7353. * here; SPARC uses the usual sigaction struct.
  7354. */
  7355. struct target_rt_sigaction *rt_act;
  7356. struct target_sigaction act, oact, *pact = 0;
  7357. if (arg4 != sizeof(target_sigset_t)) {
  7358. return -TARGET_EINVAL;
  7359. }
  7360. if (arg2) {
  7361. if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
  7362. return -TARGET_EFAULT;
  7363. act._sa_handler = rt_act->_sa_handler;
  7364. act.sa_mask = rt_act->sa_mask;
  7365. act.sa_flags = rt_act->sa_flags;
  7366. act.sa_restorer = arg5;
  7367. unlock_user_struct(rt_act, arg2, 0);
  7368. pact = &act;
  7369. }
  7370. ret = get_errno(do_sigaction(arg1, pact, &oact));
  7371. if (!is_error(ret) && arg3) {
  7372. if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
  7373. return -TARGET_EFAULT;
  7374. rt_act->_sa_handler = oact._sa_handler;
  7375. rt_act->sa_mask = oact.sa_mask;
  7376. rt_act->sa_flags = oact.sa_flags;
  7377. unlock_user_struct(rt_act, arg3, 1);
  7378. }
  7379. #else
  7380. #ifdef TARGET_SPARC
  7381. target_ulong restorer = arg4;
  7382. target_ulong sigsetsize = arg5;
  7383. #else
  7384. target_ulong sigsetsize = arg4;
  7385. #endif
  7386. struct target_sigaction *act;
  7387. struct target_sigaction *oact;
  7388. if (sigsetsize != sizeof(target_sigset_t)) {
  7389. return -TARGET_EINVAL;
  7390. }
  7391. if (arg2) {
  7392. if (!lock_user_struct(VERIFY_READ, act, arg2, 1)) {
  7393. return -TARGET_EFAULT;
  7394. }
  7395. #ifdef TARGET_ARCH_HAS_KA_RESTORER
  7396. act->ka_restorer = restorer;
  7397. #endif
  7398. } else {
  7399. act = NULL;
  7400. }
  7401. if (arg3) {
  7402. if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
  7403. ret = -TARGET_EFAULT;
  7404. goto rt_sigaction_fail;
  7405. }
  7406. } else
  7407. oact = NULL;
  7408. ret = get_errno(do_sigaction(arg1, act, oact));
  7409. rt_sigaction_fail:
  7410. if (act)
  7411. unlock_user_struct(act, arg2, 0);
  7412. if (oact)
  7413. unlock_user_struct(oact, arg3, 1);
  7414. #endif
  7415. }
  7416. return ret;
  7417. #ifdef TARGET_NR_sgetmask /* not on alpha */
  7418. case TARGET_NR_sgetmask:
  7419. {
  7420. sigset_t cur_set;
  7421. abi_ulong target_set;
  7422. ret = do_sigprocmask(0, NULL, &cur_set);
  7423. if (!ret) {
  7424. host_to_target_old_sigset(&target_set, &cur_set);
  7425. ret = target_set;
  7426. }
  7427. }
  7428. return ret;
  7429. #endif
  7430. #ifdef TARGET_NR_ssetmask /* not on alpha */
  7431. case TARGET_NR_ssetmask:
  7432. {
  7433. sigset_t set, oset;
  7434. abi_ulong target_set = arg1;
  7435. target_to_host_old_sigset(&set, &target_set);
  7436. ret = do_sigprocmask(SIG_SETMASK, &set, &oset);
  7437. if (!ret) {
  7438. host_to_target_old_sigset(&target_set, &oset);
  7439. ret = target_set;
  7440. }
  7441. }
  7442. return ret;
  7443. #endif
  7444. #ifdef TARGET_NR_sigprocmask
  7445. case TARGET_NR_sigprocmask:
  7446. {
  7447. #if defined(TARGET_ALPHA)
  7448. sigset_t set, oldset;
  7449. abi_ulong mask;
  7450. int how;
  7451. switch (arg1) {
  7452. case TARGET_SIG_BLOCK:
  7453. how = SIG_BLOCK;
  7454. break;
  7455. case TARGET_SIG_UNBLOCK:
  7456. how = SIG_UNBLOCK;
  7457. break;
  7458. case TARGET_SIG_SETMASK:
  7459. how = SIG_SETMASK;
  7460. break;
  7461. default:
  7462. return -TARGET_EINVAL;
  7463. }
  7464. mask = arg2;
  7465. target_to_host_old_sigset(&set, &mask);
  7466. ret = do_sigprocmask(how, &set, &oldset);
  7467. if (!is_error(ret)) {
  7468. host_to_target_old_sigset(&mask, &oldset);
  7469. ret = mask;
  7470. ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0; /* force no error */
  7471. }
  7472. #else
  7473. sigset_t set, oldset, *set_ptr;
  7474. int how;
  7475. if (arg2) {
  7476. switch (arg1) {
  7477. case TARGET_SIG_BLOCK:
  7478. how = SIG_BLOCK;
  7479. break;
  7480. case TARGET_SIG_UNBLOCK:
  7481. how = SIG_UNBLOCK;
  7482. break;
  7483. case TARGET_SIG_SETMASK:
  7484. how = SIG_SETMASK;
  7485. break;
  7486. default:
  7487. return -TARGET_EINVAL;
  7488. }
  7489. if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
  7490. return -TARGET_EFAULT;
  7491. target_to_host_old_sigset(&set, p);
  7492. unlock_user(p, arg2, 0);
  7493. set_ptr = &set;
  7494. } else {
  7495. how = 0;
  7496. set_ptr = NULL;
  7497. }
  7498. ret = do_sigprocmask(how, set_ptr, &oldset);
  7499. if (!is_error(ret) && arg3) {
  7500. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
  7501. return -TARGET_EFAULT;
  7502. host_to_target_old_sigset(p, &oldset);
  7503. unlock_user(p, arg3, sizeof(target_sigset_t));
  7504. }
  7505. #endif
  7506. }
  7507. return ret;
  7508. #endif
  7509. case TARGET_NR_rt_sigprocmask:
  7510. {
  7511. int how = arg1;
  7512. sigset_t set, oldset, *set_ptr;
  7513. if (arg4 != sizeof(target_sigset_t)) {
  7514. return -TARGET_EINVAL;
  7515. }
  7516. if (arg2) {
  7517. switch(how) {
  7518. case TARGET_SIG_BLOCK:
  7519. how = SIG_BLOCK;
  7520. break;
  7521. case TARGET_SIG_UNBLOCK:
  7522. how = SIG_UNBLOCK;
  7523. break;
  7524. case TARGET_SIG_SETMASK:
  7525. how = SIG_SETMASK;
  7526. break;
  7527. default:
  7528. return -TARGET_EINVAL;
  7529. }
  7530. if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
  7531. return -TARGET_EFAULT;
  7532. target_to_host_sigset(&set, p);
  7533. unlock_user(p, arg2, 0);
  7534. set_ptr = &set;
  7535. } else {
  7536. how = 0;
  7537. set_ptr = NULL;
  7538. }
  7539. ret = do_sigprocmask(how, set_ptr, &oldset);
  7540. if (!is_error(ret) && arg3) {
  7541. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
  7542. return -TARGET_EFAULT;
  7543. host_to_target_sigset(p, &oldset);
  7544. unlock_user(p, arg3, sizeof(target_sigset_t));
  7545. }
  7546. }
  7547. return ret;
  7548. #ifdef TARGET_NR_sigpending
  7549. case TARGET_NR_sigpending:
  7550. {
  7551. sigset_t set;
  7552. ret = get_errno(sigpending(&set));
  7553. if (!is_error(ret)) {
  7554. if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
  7555. return -TARGET_EFAULT;
  7556. host_to_target_old_sigset(p, &set);
  7557. unlock_user(p, arg1, sizeof(target_sigset_t));
  7558. }
  7559. }
  7560. return ret;
  7561. #endif
  7562. case TARGET_NR_rt_sigpending:
  7563. {
  7564. sigset_t set;
  7565. /* Yes, this check is >, not != like most. We follow the kernel's
  7566. * logic and it does it like this because it implements
  7567. * NR_sigpending through the same code path, and in that case
  7568. * the old_sigset_t is smaller in size.
  7569. */
  7570. if (arg2 > sizeof(target_sigset_t)) {
  7571. return -TARGET_EINVAL;
  7572. }
  7573. ret = get_errno(sigpending(&set));
  7574. if (!is_error(ret)) {
  7575. if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
  7576. return -TARGET_EFAULT;
  7577. host_to_target_sigset(p, &set);
  7578. unlock_user(p, arg1, sizeof(target_sigset_t));
  7579. }
  7580. }
  7581. return ret;
  7582. #ifdef TARGET_NR_sigsuspend
  7583. case TARGET_NR_sigsuspend:
  7584. {
  7585. TaskState *ts = cpu->opaque;
  7586. #if defined(TARGET_ALPHA)
  7587. abi_ulong mask = arg1;
  7588. target_to_host_old_sigset(&ts->sigsuspend_mask, &mask);
  7589. #else
  7590. if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
  7591. return -TARGET_EFAULT;
  7592. target_to_host_old_sigset(&ts->sigsuspend_mask, p);
  7593. unlock_user(p, arg1, 0);
  7594. #endif
  7595. ret = get_errno(safe_rt_sigsuspend(&ts->sigsuspend_mask,
  7596. SIGSET_T_SIZE));
  7597. if (ret != -TARGET_ERESTARTSYS) {
  7598. ts->in_sigsuspend = 1;
  7599. }
  7600. }
  7601. return ret;
  7602. #endif
  7603. case TARGET_NR_rt_sigsuspend:
  7604. {
  7605. TaskState *ts = cpu->opaque;
  7606. if (arg2 != sizeof(target_sigset_t)) {
  7607. return -TARGET_EINVAL;
  7608. }
  7609. if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
  7610. return -TARGET_EFAULT;
  7611. target_to_host_sigset(&ts->sigsuspend_mask, p);
  7612. unlock_user(p, arg1, 0);
  7613. ret = get_errno(safe_rt_sigsuspend(&ts->sigsuspend_mask,
  7614. SIGSET_T_SIZE));
  7615. if (ret != -TARGET_ERESTARTSYS) {
  7616. ts->in_sigsuspend = 1;
  7617. }
  7618. }
  7619. return ret;
  7620. case TARGET_NR_rt_sigtimedwait:
  7621. {
  7622. sigset_t set;
  7623. struct timespec uts, *puts;
  7624. siginfo_t uinfo;
  7625. if (arg4 != sizeof(target_sigset_t)) {
  7626. return -TARGET_EINVAL;
  7627. }
  7628. if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
  7629. return -TARGET_EFAULT;
  7630. target_to_host_sigset(&set, p);
  7631. unlock_user(p, arg1, 0);
  7632. if (arg3) {
  7633. puts = &uts;
  7634. target_to_host_timespec(puts, arg3);
  7635. } else {
  7636. puts = NULL;
  7637. }
  7638. ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts,
  7639. SIGSET_T_SIZE));
  7640. if (!is_error(ret)) {
  7641. if (arg2) {
  7642. p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t),
  7643. 0);
  7644. if (!p) {
  7645. return -TARGET_EFAULT;
  7646. }
  7647. host_to_target_siginfo(p, &uinfo);
  7648. unlock_user(p, arg2, sizeof(target_siginfo_t));
  7649. }
  7650. ret = host_to_target_signal(ret);
  7651. }
  7652. }
  7653. return ret;
  7654. case TARGET_NR_rt_sigqueueinfo:
  7655. {
  7656. siginfo_t uinfo;
  7657. p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1);
  7658. if (!p) {
  7659. return -TARGET_EFAULT;
  7660. }
  7661. target_to_host_siginfo(&uinfo, p);
  7662. unlock_user(p, arg3, 0);
  7663. ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
  7664. }
  7665. return ret;
  7666. case TARGET_NR_rt_tgsigqueueinfo:
  7667. {
  7668. siginfo_t uinfo;
  7669. p = lock_user(VERIFY_READ, arg4, sizeof(target_siginfo_t), 1);
  7670. if (!p) {
  7671. return -TARGET_EFAULT;
  7672. }
  7673. target_to_host_siginfo(&uinfo, p);
  7674. unlock_user(p, arg4, 0);
  7675. ret = get_errno(sys_rt_tgsigqueueinfo(arg1, arg2, arg3, &uinfo));
  7676. }
  7677. return ret;
  7678. #ifdef TARGET_NR_sigreturn
  7679. case TARGET_NR_sigreturn:
  7680. if (block_signals()) {
  7681. return -TARGET_ERESTARTSYS;
  7682. }
  7683. return do_sigreturn(cpu_env);
  7684. #endif
  7685. case TARGET_NR_rt_sigreturn:
  7686. if (block_signals()) {
  7687. return -TARGET_ERESTARTSYS;
  7688. }
  7689. return do_rt_sigreturn(cpu_env);
  7690. case TARGET_NR_sethostname:
  7691. if (!(p = lock_user_string(arg1)))
  7692. return -TARGET_EFAULT;
  7693. ret = get_errno(sethostname(p, arg2));
  7694. unlock_user(p, arg1, 0);
  7695. return ret;
  7696. #ifdef TARGET_NR_setrlimit
  7697. case TARGET_NR_setrlimit:
  7698. {
  7699. int resource = target_to_host_resource(arg1);
  7700. struct target_rlimit *target_rlim;
  7701. struct rlimit rlim;
  7702. if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
  7703. return -TARGET_EFAULT;
  7704. rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
  7705. rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
  7706. unlock_user_struct(target_rlim, arg2, 0);
  7707. /*
  7708. * If we just passed through resource limit settings for memory then
  7709. * they would also apply to QEMU's own allocations, and QEMU will
  7710. * crash or hang or die if its allocations fail. Ideally we would
  7711. * track the guest allocations in QEMU and apply the limits ourselves.
  7712. * For now, just tell the guest the call succeeded but don't actually
  7713. * limit anything.
  7714. */
  7715. if (resource != RLIMIT_AS &&
  7716. resource != RLIMIT_DATA &&
  7717. resource != RLIMIT_STACK) {
  7718. return get_errno(setrlimit(resource, &rlim));
  7719. } else {
  7720. return 0;
  7721. }
  7722. }
  7723. #endif
  7724. #ifdef TARGET_NR_getrlimit
  7725. case TARGET_NR_getrlimit:
  7726. {
  7727. int resource = target_to_host_resource(arg1);
  7728. struct target_rlimit *target_rlim;
  7729. struct rlimit rlim;
  7730. ret = get_errno(getrlimit(resource, &rlim));
  7731. if (!is_error(ret)) {
  7732. if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
  7733. return -TARGET_EFAULT;
  7734. target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
  7735. target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
  7736. unlock_user_struct(target_rlim, arg2, 1);
  7737. }
  7738. }
  7739. return ret;
  7740. #endif
  7741. case TARGET_NR_getrusage:
  7742. {
  7743. struct rusage rusage;
  7744. ret = get_errno(getrusage(arg1, &rusage));
  7745. if (!is_error(ret)) {
  7746. ret = host_to_target_rusage(arg2, &rusage);
  7747. }
  7748. }
  7749. return ret;
  7750. case TARGET_NR_gettimeofday:
  7751. {
  7752. struct timeval tv;
  7753. ret = get_errno(gettimeofday(&tv, NULL));
  7754. if (!is_error(ret)) {
  7755. if (copy_to_user_timeval(arg1, &tv))
  7756. return -TARGET_EFAULT;
  7757. }
  7758. }
  7759. return ret;
  7760. case TARGET_NR_settimeofday:
  7761. {
  7762. struct timeval tv, *ptv = NULL;
  7763. struct timezone tz, *ptz = NULL;
  7764. if (arg1) {
  7765. if (copy_from_user_timeval(&tv, arg1)) {
  7766. return -TARGET_EFAULT;
  7767. }
  7768. ptv = &tv;
  7769. }
  7770. if (arg2) {
  7771. if (copy_from_user_timezone(&tz, arg2)) {
  7772. return -TARGET_EFAULT;
  7773. }
  7774. ptz = &tz;
  7775. }
  7776. return get_errno(settimeofday(ptv, ptz));
  7777. }
  7778. #if defined(TARGET_NR_select)
  7779. case TARGET_NR_select:
  7780. #if defined(TARGET_WANT_NI_OLD_SELECT)
  7781. /* some architectures used to have old_select here
  7782. * but now ENOSYS it.
  7783. */
  7784. ret = -TARGET_ENOSYS;
  7785. #elif defined(TARGET_WANT_OLD_SYS_SELECT)
  7786. ret = do_old_select(arg1);
  7787. #else
  7788. ret = do_select(arg1, arg2, arg3, arg4, arg5);
  7789. #endif
  7790. return ret;
  7791. #endif
  7792. #ifdef TARGET_NR_pselect6
  7793. case TARGET_NR_pselect6:
  7794. {
  7795. abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
  7796. fd_set rfds, wfds, efds;
  7797. fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
  7798. struct timespec ts, *ts_ptr;
  7799. /*
  7800. * The 6th arg is actually two args smashed together,
  7801. * so we cannot use the C library.
  7802. */
  7803. sigset_t set;
  7804. struct {
  7805. sigset_t *set;
  7806. size_t size;
  7807. } sig, *sig_ptr;
  7808. abi_ulong arg_sigset, arg_sigsize, *arg7;
  7809. target_sigset_t *target_sigset;
  7810. n = arg1;
  7811. rfd_addr = arg2;
  7812. wfd_addr = arg3;
  7813. efd_addr = arg4;
  7814. ts_addr = arg5;
  7815. ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
  7816. if (ret) {
  7817. return ret;
  7818. }
  7819. ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
  7820. if (ret) {
  7821. return ret;
  7822. }
  7823. ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
  7824. if (ret) {
  7825. return ret;
  7826. }
  7827. /*
  7828. * This takes a timespec, and not a timeval, so we cannot
  7829. * use the do_select() helper ...
  7830. */
  7831. if (ts_addr) {
  7832. if (target_to_host_timespec(&ts, ts_addr)) {
  7833. return -TARGET_EFAULT;
  7834. }
  7835. ts_ptr = &ts;
  7836. } else {
  7837. ts_ptr = NULL;
  7838. }
  7839. /* Extract the two packed args for the sigset */
  7840. if (arg6) {
  7841. sig_ptr = &sig;
  7842. sig.size = SIGSET_T_SIZE;
  7843. arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
  7844. if (!arg7) {
  7845. return -TARGET_EFAULT;
  7846. }
  7847. arg_sigset = tswapal(arg7[0]);
  7848. arg_sigsize = tswapal(arg7[1]);
  7849. unlock_user(arg7, arg6, 0);
  7850. if (arg_sigset) {
  7851. sig.set = &set;
  7852. if (arg_sigsize != sizeof(*target_sigset)) {
  7853. /* Like the kernel, we enforce correct size sigsets */
  7854. return -TARGET_EINVAL;
  7855. }
  7856. target_sigset = lock_user(VERIFY_READ, arg_sigset,
  7857. sizeof(*target_sigset), 1);
  7858. if (!target_sigset) {
  7859. return -TARGET_EFAULT;
  7860. }
  7861. target_to_host_sigset(&set, target_sigset);
  7862. unlock_user(target_sigset, arg_sigset, 0);
  7863. } else {
  7864. sig.set = NULL;
  7865. }
  7866. } else {
  7867. sig_ptr = NULL;
  7868. }
  7869. ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
  7870. ts_ptr, sig_ptr));
  7871. if (!is_error(ret)) {
  7872. if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
  7873. return -TARGET_EFAULT;
  7874. if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
  7875. return -TARGET_EFAULT;
  7876. if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
  7877. return -TARGET_EFAULT;
  7878. if (ts_addr && host_to_target_timespec(ts_addr, &ts))
  7879. return -TARGET_EFAULT;
  7880. }
  7881. }
  7882. return ret;
  7883. #endif
  7884. #ifdef TARGET_NR_symlink
  7885. case TARGET_NR_symlink:
  7886. {
  7887. void *p2;
  7888. p = lock_user_string(arg1);
  7889. p2 = lock_user_string(arg2);
  7890. if (!p || !p2)
  7891. ret = -TARGET_EFAULT;
  7892. else
  7893. ret = get_errno(symlink(p, p2));
  7894. unlock_user(p2, arg2, 0);
  7895. unlock_user(p, arg1, 0);
  7896. }
  7897. return ret;
  7898. #endif
  7899. #if defined(TARGET_NR_symlinkat)
  7900. case TARGET_NR_symlinkat:
  7901. {
  7902. void *p2;
  7903. p = lock_user_string(arg1);
  7904. p2 = lock_user_string(arg3);
  7905. if (!p || !p2)
  7906. ret = -TARGET_EFAULT;
  7907. else
  7908. ret = get_errno(symlinkat(p, arg2, p2));
  7909. unlock_user(p2, arg3, 0);
  7910. unlock_user(p, arg1, 0);
  7911. }
  7912. return ret;
  7913. #endif
  7914. #ifdef TARGET_NR_readlink
  7915. case TARGET_NR_readlink:
  7916. {
  7917. void *p2;
  7918. p = lock_user_string(arg1);
  7919. p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  7920. if (!p || !p2) {
  7921. ret = -TARGET_EFAULT;
  7922. } else if (!arg3) {
  7923. /* Short circuit this for the magic exe check. */
  7924. ret = -TARGET_EINVAL;
  7925. } else if (is_proc_myself((const char *)p, "exe")) {
  7926. char real[PATH_MAX], *temp;
  7927. temp = realpath(exec_path, real);
  7928. /* Return value is # of bytes that we wrote to the buffer. */
  7929. if (temp == NULL) {
  7930. ret = get_errno(-1);
  7931. } else {
  7932. /* Don't worry about sign mismatch as earlier mapping
  7933. * logic would have thrown a bad address error. */
  7934. ret = MIN(strlen(real), arg3);
  7935. /* We cannot NUL terminate the string. */
  7936. memcpy(p2, real, ret);
  7937. }
  7938. } else {
  7939. ret = get_errno(readlink(path(p), p2, arg3));
  7940. }
  7941. unlock_user(p2, arg2, ret);
  7942. unlock_user(p, arg1, 0);
  7943. }
  7944. return ret;
  7945. #endif
  7946. #if defined(TARGET_NR_readlinkat)
  7947. case TARGET_NR_readlinkat:
  7948. {
  7949. void *p2;
  7950. p = lock_user_string(arg2);
  7951. p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  7952. if (!p || !p2) {
  7953. ret = -TARGET_EFAULT;
  7954. } else if (is_proc_myself((const char *)p, "exe")) {
  7955. char real[PATH_MAX], *temp;
  7956. temp = realpath(exec_path, real);
  7957. ret = temp == NULL ? get_errno(-1) : strlen(real) ;
  7958. snprintf((char *)p2, arg4, "%s", real);
  7959. } else {
  7960. ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
  7961. }
  7962. unlock_user(p2, arg3, ret);
  7963. unlock_user(p, arg2, 0);
  7964. }
  7965. return ret;
  7966. #endif
  7967. #ifdef TARGET_NR_swapon
  7968. case TARGET_NR_swapon:
  7969. if (!(p = lock_user_string(arg1)))
  7970. return -TARGET_EFAULT;
  7971. ret = get_errno(swapon(p, arg2));
  7972. unlock_user(p, arg1, 0);
  7973. return ret;
  7974. #endif
  7975. case TARGET_NR_reboot:
  7976. if (arg3 == LINUX_REBOOT_CMD_RESTART2) {
  7977. /* arg4 must be ignored in all other cases */
  7978. p = lock_user_string(arg4);
  7979. if (!p) {
  7980. return -TARGET_EFAULT;
  7981. }
  7982. ret = get_errno(reboot(arg1, arg2, arg3, p));
  7983. unlock_user(p, arg4, 0);
  7984. } else {
  7985. ret = get_errno(reboot(arg1, arg2, arg3, NULL));
  7986. }
  7987. return ret;
  7988. #ifdef TARGET_NR_mmap
  7989. case TARGET_NR_mmap:
  7990. #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
  7991. (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
  7992. defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
  7993. || defined(TARGET_S390X)
  7994. {
  7995. abi_ulong *v;
  7996. abi_ulong v1, v2, v3, v4, v5, v6;
  7997. if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
  7998. return -TARGET_EFAULT;
  7999. v1 = tswapal(v[0]);
  8000. v2 = tswapal(v[1]);
  8001. v3 = tswapal(v[2]);
  8002. v4 = tswapal(v[3]);
  8003. v5 = tswapal(v[4]);
  8004. v6 = tswapal(v[5]);
  8005. unlock_user(v, arg1, 0);
  8006. ret = get_errno(target_mmap(v1, v2, v3,
  8007. target_to_host_bitmask(v4, mmap_flags_tbl),
  8008. v5, v6));
  8009. }
  8010. #else
  8011. ret = get_errno(target_mmap(arg1, arg2, arg3,
  8012. target_to_host_bitmask(arg4, mmap_flags_tbl),
  8013. arg5,
  8014. arg6));
  8015. #endif
  8016. return ret;
  8017. #endif
  8018. #ifdef TARGET_NR_mmap2
  8019. case TARGET_NR_mmap2:
  8020. #ifndef MMAP_SHIFT
  8021. #define MMAP_SHIFT 12
  8022. #endif
  8023. ret = target_mmap(arg1, arg2, arg3,
  8024. target_to_host_bitmask(arg4, mmap_flags_tbl),
  8025. arg5, arg6 << MMAP_SHIFT);
  8026. return get_errno(ret);
  8027. #endif
  8028. case TARGET_NR_munmap:
  8029. return get_errno(target_munmap(arg1, arg2));
  8030. case TARGET_NR_mprotect:
  8031. {
  8032. TaskState *ts = cpu->opaque;
  8033. /* Special hack to detect libc making the stack executable. */
  8034. if ((arg3 & PROT_GROWSDOWN)
  8035. && arg1 >= ts->info->stack_limit
  8036. && arg1 <= ts->info->start_stack) {
  8037. arg3 &= ~PROT_GROWSDOWN;
  8038. arg2 = arg2 + arg1 - ts->info->stack_limit;
  8039. arg1 = ts->info->stack_limit;
  8040. }
  8041. }
  8042. return get_errno(target_mprotect(arg1, arg2, arg3));
  8043. #ifdef TARGET_NR_mremap
  8044. case TARGET_NR_mremap:
  8045. return get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
  8046. #endif
  8047. /* ??? msync/mlock/munlock are broken for softmmu. */
  8048. #ifdef TARGET_NR_msync
  8049. case TARGET_NR_msync:
  8050. return get_errno(msync(g2h(arg1), arg2, arg3));
  8051. #endif
  8052. #ifdef TARGET_NR_mlock
  8053. case TARGET_NR_mlock:
  8054. return get_errno(mlock(g2h(arg1), arg2));
  8055. #endif
  8056. #ifdef TARGET_NR_munlock
  8057. case TARGET_NR_munlock:
  8058. return get_errno(munlock(g2h(arg1), arg2));
  8059. #endif
  8060. #ifdef TARGET_NR_mlockall
  8061. case TARGET_NR_mlockall:
  8062. return get_errno(mlockall(target_to_host_mlockall_arg(arg1)));
  8063. #endif
  8064. #ifdef TARGET_NR_munlockall
  8065. case TARGET_NR_munlockall:
  8066. return get_errno(munlockall());
  8067. #endif
  8068. #ifdef TARGET_NR_truncate
  8069. case TARGET_NR_truncate:
  8070. if (!(p = lock_user_string(arg1)))
  8071. return -TARGET_EFAULT;
  8072. ret = get_errno(truncate(p, arg2));
  8073. unlock_user(p, arg1, 0);
  8074. return ret;
  8075. #endif
  8076. #ifdef TARGET_NR_ftruncate
  8077. case TARGET_NR_ftruncate:
  8078. return get_errno(ftruncate(arg1, arg2));
  8079. #endif
  8080. case TARGET_NR_fchmod:
  8081. return get_errno(fchmod(arg1, arg2));
  8082. #if defined(TARGET_NR_fchmodat)
  8083. case TARGET_NR_fchmodat:
  8084. if (!(p = lock_user_string(arg2)))
  8085. return -TARGET_EFAULT;
  8086. ret = get_errno(fchmodat(arg1, p, arg3, 0));
  8087. unlock_user(p, arg2, 0);
  8088. return ret;
  8089. #endif
  8090. case TARGET_NR_getpriority:
  8091. /* Note that negative values are valid for getpriority, so we must
  8092. differentiate based on errno settings. */
  8093. errno = 0;
  8094. ret = getpriority(arg1, arg2);
  8095. if (ret == -1 && errno != 0) {
  8096. return -host_to_target_errno(errno);
  8097. }
  8098. #ifdef TARGET_ALPHA
  8099. /* Return value is the unbiased priority. Signal no error. */
  8100. ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0;
  8101. #else
  8102. /* Return value is a biased priority to avoid negative numbers. */
  8103. ret = 20 - ret;
  8104. #endif
  8105. return ret;
  8106. case TARGET_NR_setpriority:
  8107. return get_errno(setpriority(arg1, arg2, arg3));
  8108. #ifdef TARGET_NR_statfs
  8109. case TARGET_NR_statfs:
  8110. if (!(p = lock_user_string(arg1))) {
  8111. return -TARGET_EFAULT;
  8112. }
  8113. ret = get_errno(statfs(path(p), &stfs));
  8114. unlock_user(p, arg1, 0);
  8115. convert_statfs:
  8116. if (!is_error(ret)) {
  8117. struct target_statfs *target_stfs;
  8118. if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
  8119. return -TARGET_EFAULT;
  8120. __put_user(stfs.f_type, &target_stfs->f_type);
  8121. __put_user(stfs.f_bsize, &target_stfs->f_bsize);
  8122. __put_user(stfs.f_blocks, &target_stfs->f_blocks);
  8123. __put_user(stfs.f_bfree, &target_stfs->f_bfree);
  8124. __put_user(stfs.f_bavail, &target_stfs->f_bavail);
  8125. __put_user(stfs.f_files, &target_stfs->f_files);
  8126. __put_user(stfs.f_ffree, &target_stfs->f_ffree);
  8127. __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
  8128. __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
  8129. __put_user(stfs.f_namelen, &target_stfs->f_namelen);
  8130. __put_user(stfs.f_frsize, &target_stfs->f_frsize);
  8131. #ifdef _STATFS_F_FLAGS
  8132. __put_user(stfs.f_flags, &target_stfs->f_flags);
  8133. #else
  8134. __put_user(0, &target_stfs->f_flags);
  8135. #endif
  8136. memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
  8137. unlock_user_struct(target_stfs, arg2, 1);
  8138. }
  8139. return ret;
  8140. #endif
  8141. #ifdef TARGET_NR_fstatfs
  8142. case TARGET_NR_fstatfs:
  8143. ret = get_errno(fstatfs(arg1, &stfs));
  8144. goto convert_statfs;
  8145. #endif
  8146. #ifdef TARGET_NR_statfs64
  8147. case TARGET_NR_statfs64:
  8148. if (!(p = lock_user_string(arg1))) {
  8149. return -TARGET_EFAULT;
  8150. }
  8151. ret = get_errno(statfs(path(p), &stfs));
  8152. unlock_user(p, arg1, 0);
  8153. convert_statfs64:
  8154. if (!is_error(ret)) {
  8155. struct target_statfs64 *target_stfs;
  8156. if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
  8157. return -TARGET_EFAULT;
  8158. __put_user(stfs.f_type, &target_stfs->f_type);
  8159. __put_user(stfs.f_bsize, &target_stfs->f_bsize);
  8160. __put_user(stfs.f_blocks, &target_stfs->f_blocks);
  8161. __put_user(stfs.f_bfree, &target_stfs->f_bfree);
  8162. __put_user(stfs.f_bavail, &target_stfs->f_bavail);
  8163. __put_user(stfs.f_files, &target_stfs->f_files);
  8164. __put_user(stfs.f_ffree, &target_stfs->f_ffree);
  8165. __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
  8166. __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
  8167. __put_user(stfs.f_namelen, &target_stfs->f_namelen);
  8168. __put_user(stfs.f_frsize, &target_stfs->f_frsize);
  8169. memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
  8170. unlock_user_struct(target_stfs, arg3, 1);
  8171. }
  8172. return ret;
  8173. case TARGET_NR_fstatfs64:
  8174. ret = get_errno(fstatfs(arg1, &stfs));
  8175. goto convert_statfs64;
  8176. #endif
  8177. #ifdef TARGET_NR_socketcall
  8178. case TARGET_NR_socketcall:
  8179. return do_socketcall(arg1, arg2);
  8180. #endif
  8181. #ifdef TARGET_NR_accept
  8182. case TARGET_NR_accept:
  8183. return do_accept4(arg1, arg2, arg3, 0);
  8184. #endif
  8185. #ifdef TARGET_NR_accept4
  8186. case TARGET_NR_accept4:
  8187. return do_accept4(arg1, arg2, arg3, arg4);
  8188. #endif
  8189. #ifdef TARGET_NR_bind
  8190. case TARGET_NR_bind:
  8191. return do_bind(arg1, arg2, arg3);
  8192. #endif
  8193. #ifdef TARGET_NR_connect
  8194. case TARGET_NR_connect:
  8195. return do_connect(arg1, arg2, arg3);
  8196. #endif
  8197. #ifdef TARGET_NR_getpeername
  8198. case TARGET_NR_getpeername:
  8199. return do_getpeername(arg1, arg2, arg3);
  8200. #endif
  8201. #ifdef TARGET_NR_getsockname
  8202. case TARGET_NR_getsockname:
  8203. return do_getsockname(arg1, arg2, arg3);
  8204. #endif
  8205. #ifdef TARGET_NR_getsockopt
  8206. case TARGET_NR_getsockopt:
  8207. return do_getsockopt(arg1, arg2, arg3, arg4, arg5);
  8208. #endif
  8209. #ifdef TARGET_NR_listen
  8210. case TARGET_NR_listen:
  8211. return get_errno(listen(arg1, arg2));
  8212. #endif
  8213. #ifdef TARGET_NR_recv
  8214. case TARGET_NR_recv:
  8215. return do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
  8216. #endif
  8217. #ifdef TARGET_NR_recvfrom
  8218. case TARGET_NR_recvfrom:
  8219. return do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
  8220. #endif
  8221. #ifdef TARGET_NR_recvmsg
  8222. case TARGET_NR_recvmsg:
  8223. return do_sendrecvmsg(arg1, arg2, arg3, 0);
  8224. #endif
  8225. #ifdef TARGET_NR_send
  8226. case TARGET_NR_send:
  8227. return do_sendto(arg1, arg2, arg3, arg4, 0, 0);
  8228. #endif
  8229. #ifdef TARGET_NR_sendmsg
  8230. case TARGET_NR_sendmsg:
  8231. return do_sendrecvmsg(arg1, arg2, arg3, 1);
  8232. #endif
  8233. #ifdef TARGET_NR_sendmmsg
  8234. case TARGET_NR_sendmmsg:
  8235. return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 1);
  8236. case TARGET_NR_recvmmsg:
  8237. return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 0);
  8238. #endif
  8239. #ifdef TARGET_NR_sendto
  8240. case TARGET_NR_sendto:
  8241. return do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
  8242. #endif
  8243. #ifdef TARGET_NR_shutdown
  8244. case TARGET_NR_shutdown:
  8245. return get_errno(shutdown(arg1, arg2));
  8246. #endif
  8247. #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
  8248. case TARGET_NR_getrandom:
  8249. p = lock_user(VERIFY_WRITE, arg1, arg2, 0);
  8250. if (!p) {
  8251. return -TARGET_EFAULT;
  8252. }
  8253. ret = get_errno(getrandom(p, arg2, arg3));
  8254. unlock_user(p, arg1, ret);
  8255. return ret;
  8256. #endif
  8257. #ifdef TARGET_NR_socket
  8258. case TARGET_NR_socket:
  8259. return do_socket(arg1, arg2, arg3);
  8260. #endif
  8261. #ifdef TARGET_NR_socketpair
  8262. case TARGET_NR_socketpair:
  8263. return do_socketpair(arg1, arg2, arg3, arg4);
  8264. #endif
  8265. #ifdef TARGET_NR_setsockopt
  8266. case TARGET_NR_setsockopt:
  8267. return do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
  8268. #endif
  8269. #if defined(TARGET_NR_syslog)
  8270. case TARGET_NR_syslog:
  8271. {
  8272. int len = arg2;
  8273. switch (arg1) {
  8274. case TARGET_SYSLOG_ACTION_CLOSE: /* Close log */
  8275. case TARGET_SYSLOG_ACTION_OPEN: /* Open log */
  8276. case TARGET_SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
  8277. case TARGET_SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging */
  8278. case TARGET_SYSLOG_ACTION_CONSOLE_ON: /* Enable logging */
  8279. case TARGET_SYSLOG_ACTION_CONSOLE_LEVEL: /* Set messages level */
  8280. case TARGET_SYSLOG_ACTION_SIZE_UNREAD: /* Number of chars */
  8281. case TARGET_SYSLOG_ACTION_SIZE_BUFFER: /* Size of the buffer */
  8282. return get_errno(sys_syslog((int)arg1, NULL, (int)arg3));
  8283. case TARGET_SYSLOG_ACTION_READ: /* Read from log */
  8284. case TARGET_SYSLOG_ACTION_READ_CLEAR: /* Read/clear msgs */
  8285. case TARGET_SYSLOG_ACTION_READ_ALL: /* Read last messages */
  8286. {
  8287. if (len < 0) {
  8288. return -TARGET_EINVAL;
  8289. }
  8290. if (len == 0) {
  8291. return 0;
  8292. }
  8293. p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  8294. if (!p) {
  8295. return -TARGET_EFAULT;
  8296. }
  8297. ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
  8298. unlock_user(p, arg2, arg3);
  8299. }
  8300. return ret;
  8301. default:
  8302. return -TARGET_EINVAL;
  8303. }
  8304. }
  8305. break;
  8306. #endif
  8307. case TARGET_NR_setitimer:
  8308. {
  8309. struct itimerval value, ovalue, *pvalue;
  8310. if (arg2) {
  8311. pvalue = &value;
  8312. if (copy_from_user_timeval(&pvalue->it_interval, arg2)
  8313. || copy_from_user_timeval(&pvalue->it_value,
  8314. arg2 + sizeof(struct target_timeval)))
  8315. return -TARGET_EFAULT;
  8316. } else {
  8317. pvalue = NULL;
  8318. }
  8319. ret = get_errno(setitimer(arg1, pvalue, &ovalue));
  8320. if (!is_error(ret) && arg3) {
  8321. if (copy_to_user_timeval(arg3,
  8322. &ovalue.it_interval)
  8323. || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
  8324. &ovalue.it_value))
  8325. return -TARGET_EFAULT;
  8326. }
  8327. }
  8328. return ret;
  8329. case TARGET_NR_getitimer:
  8330. {
  8331. struct itimerval value;
  8332. ret = get_errno(getitimer(arg1, &value));
  8333. if (!is_error(ret) && arg2) {
  8334. if (copy_to_user_timeval(arg2,
  8335. &value.it_interval)
  8336. || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
  8337. &value.it_value))
  8338. return -TARGET_EFAULT;
  8339. }
  8340. }
  8341. return ret;
  8342. #ifdef TARGET_NR_stat
  8343. case TARGET_NR_stat:
  8344. if (!(p = lock_user_string(arg1))) {
  8345. return -TARGET_EFAULT;
  8346. }
  8347. ret = get_errno(stat(path(p), &st));
  8348. unlock_user(p, arg1, 0);
  8349. goto do_stat;
  8350. #endif
  8351. #ifdef TARGET_NR_lstat
  8352. case TARGET_NR_lstat:
  8353. if (!(p = lock_user_string(arg1))) {
  8354. return -TARGET_EFAULT;
  8355. }
  8356. ret = get_errno(lstat(path(p), &st));
  8357. unlock_user(p, arg1, 0);
  8358. goto do_stat;
  8359. #endif
  8360. #ifdef TARGET_NR_fstat
  8361. case TARGET_NR_fstat:
  8362. {
  8363. ret = get_errno(fstat(arg1, &st));
  8364. #if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat)
  8365. do_stat:
  8366. #endif
  8367. if (!is_error(ret)) {
  8368. struct target_stat *target_st;
  8369. if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
  8370. return -TARGET_EFAULT;
  8371. memset(target_st, 0, sizeof(*target_st));
  8372. __put_user(st.st_dev, &target_st->st_dev);
  8373. __put_user(st.st_ino, &target_st->st_ino);
  8374. __put_user(st.st_mode, &target_st->st_mode);
  8375. __put_user(st.st_uid, &target_st->st_uid);
  8376. __put_user(st.st_gid, &target_st->st_gid);
  8377. __put_user(st.st_nlink, &target_st->st_nlink);
  8378. __put_user(st.st_rdev, &target_st->st_rdev);
  8379. __put_user(st.st_size, &target_st->st_size);
  8380. __put_user(st.st_blksize, &target_st->st_blksize);
  8381. __put_user(st.st_blocks, &target_st->st_blocks);
  8382. __put_user(st.st_atime, &target_st->target_st_atime);
  8383. __put_user(st.st_mtime, &target_st->target_st_mtime);
  8384. __put_user(st.st_ctime, &target_st->target_st_ctime);
  8385. #if (_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700) && \
  8386. defined(TARGET_STAT_HAVE_NSEC)
  8387. __put_user(st.st_atim.tv_nsec,
  8388. &target_st->target_st_atime_nsec);
  8389. __put_user(st.st_mtim.tv_nsec,
  8390. &target_st->target_st_mtime_nsec);
  8391. __put_user(st.st_ctim.tv_nsec,
  8392. &target_st->target_st_ctime_nsec);
  8393. #endif
  8394. unlock_user_struct(target_st, arg2, 1);
  8395. }
  8396. }
  8397. return ret;
  8398. #endif
  8399. case TARGET_NR_vhangup:
  8400. return get_errno(vhangup());
  8401. #ifdef TARGET_NR_syscall
  8402. case TARGET_NR_syscall:
  8403. return do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
  8404. arg6, arg7, arg8, 0);
  8405. #endif
  8406. case TARGET_NR_wait4:
  8407. {
  8408. int status;
  8409. abi_long status_ptr = arg2;
  8410. struct rusage rusage, *rusage_ptr;
  8411. abi_ulong target_rusage = arg4;
  8412. abi_long rusage_err;
  8413. if (target_rusage)
  8414. rusage_ptr = &rusage;
  8415. else
  8416. rusage_ptr = NULL;
  8417. ret = get_errno(safe_wait4(arg1, &status, arg3, rusage_ptr));
  8418. if (!is_error(ret)) {
  8419. if (status_ptr && ret) {
  8420. status = host_to_target_waitstatus(status);
  8421. if (put_user_s32(status, status_ptr))
  8422. return -TARGET_EFAULT;
  8423. }
  8424. if (target_rusage) {
  8425. rusage_err = host_to_target_rusage(target_rusage, &rusage);
  8426. if (rusage_err) {
  8427. ret = rusage_err;
  8428. }
  8429. }
  8430. }
  8431. }
  8432. return ret;
  8433. #ifdef TARGET_NR_swapoff
  8434. case TARGET_NR_swapoff:
  8435. if (!(p = lock_user_string(arg1)))
  8436. return -TARGET_EFAULT;
  8437. ret = get_errno(swapoff(p));
  8438. unlock_user(p, arg1, 0);
  8439. return ret;
  8440. #endif
  8441. case TARGET_NR_sysinfo:
  8442. {
  8443. struct target_sysinfo *target_value;
  8444. struct sysinfo value;
  8445. ret = get_errno(sysinfo(&value));
  8446. if (!is_error(ret) && arg1)
  8447. {
  8448. if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
  8449. return -TARGET_EFAULT;
  8450. __put_user(value.uptime, &target_value->uptime);
  8451. __put_user(value.loads[0], &target_value->loads[0]);
  8452. __put_user(value.loads[1], &target_value->loads[1]);
  8453. __put_user(value.loads[2], &target_value->loads[2]);
  8454. __put_user(value.totalram, &target_value->totalram);
  8455. __put_user(value.freeram, &target_value->freeram);
  8456. __put_user(value.sharedram, &target_value->sharedram);
  8457. __put_user(value.bufferram, &target_value->bufferram);
  8458. __put_user(value.totalswap, &target_value->totalswap);
  8459. __put_user(value.freeswap, &target_value->freeswap);
  8460. __put_user(value.procs, &target_value->procs);
  8461. __put_user(value.totalhigh, &target_value->totalhigh);
  8462. __put_user(value.freehigh, &target_value->freehigh);
  8463. __put_user(value.mem_unit, &target_value->mem_unit);
  8464. unlock_user_struct(target_value, arg1, 1);
  8465. }
  8466. }
  8467. return ret;
  8468. #ifdef TARGET_NR_ipc
  8469. case TARGET_NR_ipc:
  8470. return do_ipc(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6);
  8471. #endif
  8472. #ifdef TARGET_NR_semget
  8473. case TARGET_NR_semget:
  8474. return get_errno(semget(arg1, arg2, arg3));
  8475. #endif
  8476. #ifdef TARGET_NR_semop
  8477. case TARGET_NR_semop:
  8478. return do_semop(arg1, arg2, arg3);
  8479. #endif
  8480. #ifdef TARGET_NR_semctl
  8481. case TARGET_NR_semctl:
  8482. return do_semctl(arg1, arg2, arg3, arg4);
  8483. #endif
  8484. #ifdef TARGET_NR_msgctl
  8485. case TARGET_NR_msgctl:
  8486. return do_msgctl(arg1, arg2, arg3);
  8487. #endif
  8488. #ifdef TARGET_NR_msgget
  8489. case TARGET_NR_msgget:
  8490. return get_errno(msgget(arg1, arg2));
  8491. #endif
  8492. #ifdef TARGET_NR_msgrcv
  8493. case TARGET_NR_msgrcv:
  8494. return do_msgrcv(arg1, arg2, arg3, arg4, arg5);
  8495. #endif
  8496. #ifdef TARGET_NR_msgsnd
  8497. case TARGET_NR_msgsnd:
  8498. return do_msgsnd(arg1, arg2, arg3, arg4);
  8499. #endif
  8500. #ifdef TARGET_NR_shmget
  8501. case TARGET_NR_shmget:
  8502. return get_errno(shmget(arg1, arg2, arg3));
  8503. #endif
  8504. #ifdef TARGET_NR_shmctl
  8505. case TARGET_NR_shmctl:
  8506. return do_shmctl(arg1, arg2, arg3);
  8507. #endif
  8508. #ifdef TARGET_NR_shmat
  8509. case TARGET_NR_shmat:
  8510. return do_shmat(cpu_env, arg1, arg2, arg3);
  8511. #endif
  8512. #ifdef TARGET_NR_shmdt
  8513. case TARGET_NR_shmdt:
  8514. return do_shmdt(arg1);
  8515. #endif
  8516. case TARGET_NR_fsync:
  8517. return get_errno(fsync(arg1));
  8518. case TARGET_NR_clone:
  8519. /* Linux manages to have three different orderings for its
  8520. * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
  8521. * match the kernel's CONFIG_CLONE_* settings.
  8522. * Microblaze is further special in that it uses a sixth
  8523. * implicit argument to clone for the TLS pointer.
  8524. */
  8525. #if defined(TARGET_MICROBLAZE)
  8526. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5));
  8527. #elif defined(TARGET_CLONE_BACKWARDS)
  8528. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
  8529. #elif defined(TARGET_CLONE_BACKWARDS2)
  8530. ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
  8531. #else
  8532. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
  8533. #endif
  8534. return ret;
  8535. #ifdef __NR_exit_group
  8536. /* new thread calls */
  8537. case TARGET_NR_exit_group:
  8538. preexit_cleanup(cpu_env, arg1);
  8539. return get_errno(exit_group(arg1));
  8540. #endif
  8541. case TARGET_NR_setdomainname:
  8542. if (!(p = lock_user_string(arg1)))
  8543. return -TARGET_EFAULT;
  8544. ret = get_errno(setdomainname(p, arg2));
  8545. unlock_user(p, arg1, 0);
  8546. return ret;
  8547. case TARGET_NR_uname:
  8548. /* no need to transcode because we use the linux syscall */
  8549. {
  8550. struct new_utsname * buf;
  8551. if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
  8552. return -TARGET_EFAULT;
  8553. ret = get_errno(sys_uname(buf));
  8554. if (!is_error(ret)) {
  8555. /* Overwrite the native machine name with whatever is being
  8556. emulated. */
  8557. g_strlcpy(buf->machine, cpu_to_uname_machine(cpu_env),
  8558. sizeof(buf->machine));
  8559. /* Allow the user to override the reported release. */
  8560. if (qemu_uname_release && *qemu_uname_release) {
  8561. g_strlcpy(buf->release, qemu_uname_release,
  8562. sizeof(buf->release));
  8563. }
  8564. }
  8565. unlock_user_struct(buf, arg1, 1);
  8566. }
  8567. return ret;
  8568. #ifdef TARGET_I386
  8569. case TARGET_NR_modify_ldt:
  8570. return do_modify_ldt(cpu_env, arg1, arg2, arg3);
  8571. #if !defined(TARGET_X86_64)
  8572. case TARGET_NR_vm86:
  8573. return do_vm86(cpu_env, arg1, arg2);
  8574. #endif
  8575. #endif
  8576. case TARGET_NR_adjtimex:
  8577. {
  8578. struct timex host_buf;
  8579. if (target_to_host_timex(&host_buf, arg1) != 0) {
  8580. return -TARGET_EFAULT;
  8581. }
  8582. ret = get_errno(adjtimex(&host_buf));
  8583. if (!is_error(ret)) {
  8584. if (host_to_target_timex(arg1, &host_buf) != 0) {
  8585. return -TARGET_EFAULT;
  8586. }
  8587. }
  8588. }
  8589. return ret;
  8590. #if defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME)
  8591. case TARGET_NR_clock_adjtime:
  8592. {
  8593. struct timex htx, *phtx = &htx;
  8594. if (target_to_host_timex(phtx, arg2) != 0) {
  8595. return -TARGET_EFAULT;
  8596. }
  8597. ret = get_errno(clock_adjtime(arg1, phtx));
  8598. if (!is_error(ret) && phtx) {
  8599. if (host_to_target_timex(arg2, phtx) != 0) {
  8600. return -TARGET_EFAULT;
  8601. }
  8602. }
  8603. }
  8604. return ret;
  8605. #endif
  8606. case TARGET_NR_getpgid:
  8607. return get_errno(getpgid(arg1));
  8608. case TARGET_NR_fchdir:
  8609. return get_errno(fchdir(arg1));
  8610. case TARGET_NR_personality:
  8611. return get_errno(personality(arg1));
  8612. #ifdef TARGET_NR__llseek /* Not on alpha */
  8613. case TARGET_NR__llseek:
  8614. {
  8615. int64_t res;
  8616. #if !defined(__NR_llseek)
  8617. res = lseek(arg1, ((uint64_t)arg2 << 32) | (abi_ulong)arg3, arg5);
  8618. if (res == -1) {
  8619. ret = get_errno(res);
  8620. } else {
  8621. ret = 0;
  8622. }
  8623. #else
  8624. ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
  8625. #endif
  8626. if ((ret == 0) && put_user_s64(res, arg4)) {
  8627. return -TARGET_EFAULT;
  8628. }
  8629. }
  8630. return ret;
  8631. #endif
  8632. #ifdef TARGET_NR_getdents
  8633. case TARGET_NR_getdents:
  8634. #ifdef EMULATE_GETDENTS_WITH_GETDENTS
  8635. #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
  8636. {
  8637. struct target_dirent *target_dirp;
  8638. struct linux_dirent *dirp;
  8639. abi_long count = arg3;
  8640. dirp = g_try_malloc(count);
  8641. if (!dirp) {
  8642. return -TARGET_ENOMEM;
  8643. }
  8644. ret = get_errno(sys_getdents(arg1, dirp, count));
  8645. if (!is_error(ret)) {
  8646. struct linux_dirent *de;
  8647. struct target_dirent *tde;
  8648. int len = ret;
  8649. int reclen, treclen;
  8650. int count1, tnamelen;
  8651. count1 = 0;
  8652. de = dirp;
  8653. if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
  8654. return -TARGET_EFAULT;
  8655. tde = target_dirp;
  8656. while (len > 0) {
  8657. reclen = de->d_reclen;
  8658. tnamelen = reclen - offsetof(struct linux_dirent, d_name);
  8659. assert(tnamelen >= 0);
  8660. treclen = tnamelen + offsetof(struct target_dirent, d_name);
  8661. assert(count1 + treclen <= count);
  8662. tde->d_reclen = tswap16(treclen);
  8663. tde->d_ino = tswapal(de->d_ino);
  8664. tde->d_off = tswapal(de->d_off);
  8665. memcpy(tde->d_name, de->d_name, tnamelen);
  8666. de = (struct linux_dirent *)((char *)de + reclen);
  8667. len -= reclen;
  8668. tde = (struct target_dirent *)((char *)tde + treclen);
  8669. count1 += treclen;
  8670. }
  8671. ret = count1;
  8672. unlock_user(target_dirp, arg2, ret);
  8673. }
  8674. g_free(dirp);
  8675. }
  8676. #else
  8677. {
  8678. struct linux_dirent *dirp;
  8679. abi_long count = arg3;
  8680. if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
  8681. return -TARGET_EFAULT;
  8682. ret = get_errno(sys_getdents(arg1, dirp, count));
  8683. if (!is_error(ret)) {
  8684. struct linux_dirent *de;
  8685. int len = ret;
  8686. int reclen;
  8687. de = dirp;
  8688. while (len > 0) {
  8689. reclen = de->d_reclen;
  8690. if (reclen > len)
  8691. break;
  8692. de->d_reclen = tswap16(reclen);
  8693. tswapls(&de->d_ino);
  8694. tswapls(&de->d_off);
  8695. de = (struct linux_dirent *)((char *)de + reclen);
  8696. len -= reclen;
  8697. }
  8698. }
  8699. unlock_user(dirp, arg2, ret);
  8700. }
  8701. #endif
  8702. #else
  8703. /* Implement getdents in terms of getdents64 */
  8704. {
  8705. struct linux_dirent64 *dirp;
  8706. abi_long count = arg3;
  8707. dirp = lock_user(VERIFY_WRITE, arg2, count, 0);
  8708. if (!dirp) {
  8709. return -TARGET_EFAULT;
  8710. }
  8711. ret = get_errno(sys_getdents64(arg1, dirp, count));
  8712. if (!is_error(ret)) {
  8713. /* Convert the dirent64 structs to target dirent. We do this
  8714. * in-place, since we can guarantee that a target_dirent is no
  8715. * larger than a dirent64; however this means we have to be
  8716. * careful to read everything before writing in the new format.
  8717. */
  8718. struct linux_dirent64 *de;
  8719. struct target_dirent *tde;
  8720. int len = ret;
  8721. int tlen = 0;
  8722. de = dirp;
  8723. tde = (struct target_dirent *)dirp;
  8724. while (len > 0) {
  8725. int namelen, treclen;
  8726. int reclen = de->d_reclen;
  8727. uint64_t ino = de->d_ino;
  8728. int64_t off = de->d_off;
  8729. uint8_t type = de->d_type;
  8730. namelen = strlen(de->d_name);
  8731. treclen = offsetof(struct target_dirent, d_name)
  8732. + namelen + 2;
  8733. treclen = QEMU_ALIGN_UP(treclen, sizeof(abi_long));
  8734. memmove(tde->d_name, de->d_name, namelen + 1);
  8735. tde->d_ino = tswapal(ino);
  8736. tde->d_off = tswapal(off);
  8737. tde->d_reclen = tswap16(treclen);
  8738. /* The target_dirent type is in what was formerly a padding
  8739. * byte at the end of the structure:
  8740. */
  8741. *(((char *)tde) + treclen - 1) = type;
  8742. de = (struct linux_dirent64 *)((char *)de + reclen);
  8743. tde = (struct target_dirent *)((char *)tde + treclen);
  8744. len -= reclen;
  8745. tlen += treclen;
  8746. }
  8747. ret = tlen;
  8748. }
  8749. unlock_user(dirp, arg2, ret);
  8750. }
  8751. #endif
  8752. return ret;
  8753. #endif /* TARGET_NR_getdents */
  8754. #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
  8755. case TARGET_NR_getdents64:
  8756. {
  8757. struct linux_dirent64 *dirp;
  8758. abi_long count = arg3;
  8759. if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
  8760. return -TARGET_EFAULT;
  8761. ret = get_errno(sys_getdents64(arg1, dirp, count));
  8762. if (!is_error(ret)) {
  8763. struct linux_dirent64 *de;
  8764. int len = ret;
  8765. int reclen;
  8766. de = dirp;
  8767. while (len > 0) {
  8768. reclen = de->d_reclen;
  8769. if (reclen > len)
  8770. break;
  8771. de->d_reclen = tswap16(reclen);
  8772. tswap64s((uint64_t *)&de->d_ino);
  8773. tswap64s((uint64_t *)&de->d_off);
  8774. de = (struct linux_dirent64 *)((char *)de + reclen);
  8775. len -= reclen;
  8776. }
  8777. }
  8778. unlock_user(dirp, arg2, ret);
  8779. }
  8780. return ret;
  8781. #endif /* TARGET_NR_getdents64 */
  8782. #if defined(TARGET_NR__newselect)
  8783. case TARGET_NR__newselect:
  8784. return do_select(arg1, arg2, arg3, arg4, arg5);
  8785. #endif
  8786. #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
  8787. # ifdef TARGET_NR_poll
  8788. case TARGET_NR_poll:
  8789. # endif
  8790. # ifdef TARGET_NR_ppoll
  8791. case TARGET_NR_ppoll:
  8792. # endif
  8793. {
  8794. struct target_pollfd *target_pfd;
  8795. unsigned int nfds = arg2;
  8796. struct pollfd *pfd;
  8797. unsigned int i;
  8798. pfd = NULL;
  8799. target_pfd = NULL;
  8800. if (nfds) {
  8801. if (nfds > (INT_MAX / sizeof(struct target_pollfd))) {
  8802. return -TARGET_EINVAL;
  8803. }
  8804. target_pfd = lock_user(VERIFY_WRITE, arg1,
  8805. sizeof(struct target_pollfd) * nfds, 1);
  8806. if (!target_pfd) {
  8807. return -TARGET_EFAULT;
  8808. }
  8809. pfd = alloca(sizeof(struct pollfd) * nfds);
  8810. for (i = 0; i < nfds; i++) {
  8811. pfd[i].fd = tswap32(target_pfd[i].fd);
  8812. pfd[i].events = tswap16(target_pfd[i].events);
  8813. }
  8814. }
  8815. switch (num) {
  8816. # ifdef TARGET_NR_ppoll
  8817. case TARGET_NR_ppoll:
  8818. {
  8819. struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
  8820. target_sigset_t *target_set;
  8821. sigset_t _set, *set = &_set;
  8822. if (arg3) {
  8823. if (target_to_host_timespec(timeout_ts, arg3)) {
  8824. unlock_user(target_pfd, arg1, 0);
  8825. return -TARGET_EFAULT;
  8826. }
  8827. } else {
  8828. timeout_ts = NULL;
  8829. }
  8830. if (arg4) {
  8831. if (arg5 != sizeof(target_sigset_t)) {
  8832. unlock_user(target_pfd, arg1, 0);
  8833. return -TARGET_EINVAL;
  8834. }
  8835. target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1);
  8836. if (!target_set) {
  8837. unlock_user(target_pfd, arg1, 0);
  8838. return -TARGET_EFAULT;
  8839. }
  8840. target_to_host_sigset(set, target_set);
  8841. } else {
  8842. set = NULL;
  8843. }
  8844. ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts,
  8845. set, SIGSET_T_SIZE));
  8846. if (!is_error(ret) && arg3) {
  8847. host_to_target_timespec(arg3, timeout_ts);
  8848. }
  8849. if (arg4) {
  8850. unlock_user(target_set, arg4, 0);
  8851. }
  8852. break;
  8853. }
  8854. # endif
  8855. # ifdef TARGET_NR_poll
  8856. case TARGET_NR_poll:
  8857. {
  8858. struct timespec ts, *pts;
  8859. if (arg3 >= 0) {
  8860. /* Convert ms to secs, ns */
  8861. ts.tv_sec = arg3 / 1000;
  8862. ts.tv_nsec = (arg3 % 1000) * 1000000LL;
  8863. pts = &ts;
  8864. } else {
  8865. /* -ve poll() timeout means "infinite" */
  8866. pts = NULL;
  8867. }
  8868. ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0));
  8869. break;
  8870. }
  8871. # endif
  8872. default:
  8873. g_assert_not_reached();
  8874. }
  8875. if (!is_error(ret)) {
  8876. for(i = 0; i < nfds; i++) {
  8877. target_pfd[i].revents = tswap16(pfd[i].revents);
  8878. }
  8879. }
  8880. unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
  8881. }
  8882. return ret;
  8883. #endif
  8884. case TARGET_NR_flock:
  8885. /* NOTE: the flock constant seems to be the same for every
  8886. Linux platform */
  8887. return get_errno(safe_flock(arg1, arg2));
  8888. case TARGET_NR_readv:
  8889. {
  8890. struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
  8891. if (vec != NULL) {
  8892. ret = get_errno(safe_readv(arg1, vec, arg3));
  8893. unlock_iovec(vec, arg2, arg3, 1);
  8894. } else {
  8895. ret = -host_to_target_errno(errno);
  8896. }
  8897. }
  8898. return ret;
  8899. case TARGET_NR_writev:
  8900. {
  8901. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  8902. if (vec != NULL) {
  8903. ret = get_errno(safe_writev(arg1, vec, arg3));
  8904. unlock_iovec(vec, arg2, arg3, 0);
  8905. } else {
  8906. ret = -host_to_target_errno(errno);
  8907. }
  8908. }
  8909. return ret;
  8910. #if defined(TARGET_NR_preadv)
  8911. case TARGET_NR_preadv:
  8912. {
  8913. struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
  8914. if (vec != NULL) {
  8915. unsigned long low, high;
  8916. target_to_host_low_high(arg4, arg5, &low, &high);
  8917. ret = get_errno(safe_preadv(arg1, vec, arg3, low, high));
  8918. unlock_iovec(vec, arg2, arg3, 1);
  8919. } else {
  8920. ret = -host_to_target_errno(errno);
  8921. }
  8922. }
  8923. return ret;
  8924. #endif
  8925. #if defined(TARGET_NR_pwritev)
  8926. case TARGET_NR_pwritev:
  8927. {
  8928. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  8929. if (vec != NULL) {
  8930. unsigned long low, high;
  8931. target_to_host_low_high(arg4, arg5, &low, &high);
  8932. ret = get_errno(safe_pwritev(arg1, vec, arg3, low, high));
  8933. unlock_iovec(vec, arg2, arg3, 0);
  8934. } else {
  8935. ret = -host_to_target_errno(errno);
  8936. }
  8937. }
  8938. return ret;
  8939. #endif
  8940. case TARGET_NR_getsid:
  8941. return get_errno(getsid(arg1));
  8942. #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
  8943. case TARGET_NR_fdatasync:
  8944. return get_errno(fdatasync(arg1));
  8945. #endif
  8946. #ifdef TARGET_NR__sysctl
  8947. case TARGET_NR__sysctl:
  8948. /* We don't implement this, but ENOTDIR is always a safe
  8949. return value. */
  8950. return -TARGET_ENOTDIR;
  8951. #endif
  8952. case TARGET_NR_sched_getaffinity:
  8953. {
  8954. unsigned int mask_size;
  8955. unsigned long *mask;
  8956. /*
  8957. * sched_getaffinity needs multiples of ulong, so need to take
  8958. * care of mismatches between target ulong and host ulong sizes.
  8959. */
  8960. if (arg2 & (sizeof(abi_ulong) - 1)) {
  8961. return -TARGET_EINVAL;
  8962. }
  8963. mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
  8964. mask = alloca(mask_size);
  8965. memset(mask, 0, mask_size);
  8966. ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
  8967. if (!is_error(ret)) {
  8968. if (ret > arg2) {
  8969. /* More data returned than the caller's buffer will fit.
  8970. * This only happens if sizeof(abi_long) < sizeof(long)
  8971. * and the caller passed us a buffer holding an odd number
  8972. * of abi_longs. If the host kernel is actually using the
  8973. * extra 4 bytes then fail EINVAL; otherwise we can just
  8974. * ignore them and only copy the interesting part.
  8975. */
  8976. int numcpus = sysconf(_SC_NPROCESSORS_CONF);
  8977. if (numcpus > arg2 * 8) {
  8978. return -TARGET_EINVAL;
  8979. }
  8980. ret = arg2;
  8981. }
  8982. if (host_to_target_cpu_mask(mask, mask_size, arg3, ret)) {
  8983. return -TARGET_EFAULT;
  8984. }
  8985. }
  8986. }
  8987. return ret;
  8988. case TARGET_NR_sched_setaffinity:
  8989. {
  8990. unsigned int mask_size;
  8991. unsigned long *mask;
  8992. /*
  8993. * sched_setaffinity needs multiples of ulong, so need to take
  8994. * care of mismatches between target ulong and host ulong sizes.
  8995. */
  8996. if (arg2 & (sizeof(abi_ulong) - 1)) {
  8997. return -TARGET_EINVAL;
  8998. }
  8999. mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
  9000. mask = alloca(mask_size);
  9001. ret = target_to_host_cpu_mask(mask, mask_size, arg3, arg2);
  9002. if (ret) {
  9003. return ret;
  9004. }
  9005. return get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
  9006. }
  9007. case TARGET_NR_getcpu:
  9008. {
  9009. unsigned cpu, node;
  9010. ret = get_errno(sys_getcpu(arg1 ? &cpu : NULL,
  9011. arg2 ? &node : NULL,
  9012. NULL));
  9013. if (is_error(ret)) {
  9014. return ret;
  9015. }
  9016. if (arg1 && put_user_u32(cpu, arg1)) {
  9017. return -TARGET_EFAULT;
  9018. }
  9019. if (arg2 && put_user_u32(node, arg2)) {
  9020. return -TARGET_EFAULT;
  9021. }
  9022. }
  9023. return ret;
  9024. case TARGET_NR_sched_setparam:
  9025. {
  9026. struct sched_param *target_schp;
  9027. struct sched_param schp;
  9028. if (arg2 == 0) {
  9029. return -TARGET_EINVAL;
  9030. }
  9031. if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
  9032. return -TARGET_EFAULT;
  9033. schp.sched_priority = tswap32(target_schp->sched_priority);
  9034. unlock_user_struct(target_schp, arg2, 0);
  9035. return get_errno(sched_setparam(arg1, &schp));
  9036. }
  9037. case TARGET_NR_sched_getparam:
  9038. {
  9039. struct sched_param *target_schp;
  9040. struct sched_param schp;
  9041. if (arg2 == 0) {
  9042. return -TARGET_EINVAL;
  9043. }
  9044. ret = get_errno(sched_getparam(arg1, &schp));
  9045. if (!is_error(ret)) {
  9046. if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
  9047. return -TARGET_EFAULT;
  9048. target_schp->sched_priority = tswap32(schp.sched_priority);
  9049. unlock_user_struct(target_schp, arg2, 1);
  9050. }
  9051. }
  9052. return ret;
  9053. case TARGET_NR_sched_setscheduler:
  9054. {
  9055. struct sched_param *target_schp;
  9056. struct sched_param schp;
  9057. if (arg3 == 0) {
  9058. return -TARGET_EINVAL;
  9059. }
  9060. if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
  9061. return -TARGET_EFAULT;
  9062. schp.sched_priority = tswap32(target_schp->sched_priority);
  9063. unlock_user_struct(target_schp, arg3, 0);
  9064. return get_errno(sched_setscheduler(arg1, arg2, &schp));
  9065. }
  9066. case TARGET_NR_sched_getscheduler:
  9067. return get_errno(sched_getscheduler(arg1));
  9068. case TARGET_NR_sched_yield:
  9069. return get_errno(sched_yield());
  9070. case TARGET_NR_sched_get_priority_max:
  9071. return get_errno(sched_get_priority_max(arg1));
  9072. case TARGET_NR_sched_get_priority_min:
  9073. return get_errno(sched_get_priority_min(arg1));
  9074. case TARGET_NR_sched_rr_get_interval:
  9075. {
  9076. struct timespec ts;
  9077. ret = get_errno(sched_rr_get_interval(arg1, &ts));
  9078. if (!is_error(ret)) {
  9079. ret = host_to_target_timespec(arg2, &ts);
  9080. }
  9081. }
  9082. return ret;
  9083. case TARGET_NR_nanosleep:
  9084. {
  9085. struct timespec req, rem;
  9086. target_to_host_timespec(&req, arg1);
  9087. ret = get_errno(safe_nanosleep(&req, &rem));
  9088. if (is_error(ret) && arg2) {
  9089. host_to_target_timespec(arg2, &rem);
  9090. }
  9091. }
  9092. return ret;
  9093. case TARGET_NR_prctl:
  9094. switch (arg1) {
  9095. case PR_GET_PDEATHSIG:
  9096. {
  9097. int deathsig;
  9098. ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
  9099. if (!is_error(ret) && arg2
  9100. && put_user_ual(deathsig, arg2)) {
  9101. return -TARGET_EFAULT;
  9102. }
  9103. return ret;
  9104. }
  9105. #ifdef PR_GET_NAME
  9106. case PR_GET_NAME:
  9107. {
  9108. void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
  9109. if (!name) {
  9110. return -TARGET_EFAULT;
  9111. }
  9112. ret = get_errno(prctl(arg1, (unsigned long)name,
  9113. arg3, arg4, arg5));
  9114. unlock_user(name, arg2, 16);
  9115. return ret;
  9116. }
  9117. case PR_SET_NAME:
  9118. {
  9119. void *name = lock_user(VERIFY_READ, arg2, 16, 1);
  9120. if (!name) {
  9121. return -TARGET_EFAULT;
  9122. }
  9123. ret = get_errno(prctl(arg1, (unsigned long)name,
  9124. arg3, arg4, arg5));
  9125. unlock_user(name, arg2, 0);
  9126. return ret;
  9127. }
  9128. #endif
  9129. #ifdef TARGET_MIPS
  9130. case TARGET_PR_GET_FP_MODE:
  9131. {
  9132. CPUMIPSState *env = ((CPUMIPSState *)cpu_env);
  9133. ret = 0;
  9134. if (env->CP0_Status & (1 << CP0St_FR)) {
  9135. ret |= TARGET_PR_FP_MODE_FR;
  9136. }
  9137. if (env->CP0_Config5 & (1 << CP0C5_FRE)) {
  9138. ret |= TARGET_PR_FP_MODE_FRE;
  9139. }
  9140. return ret;
  9141. }
  9142. case TARGET_PR_SET_FP_MODE:
  9143. {
  9144. CPUMIPSState *env = ((CPUMIPSState *)cpu_env);
  9145. bool old_fr = env->CP0_Status & (1 << CP0St_FR);
  9146. bool old_fre = env->CP0_Config5 & (1 << CP0C5_FRE);
  9147. bool new_fr = arg2 & TARGET_PR_FP_MODE_FR;
  9148. bool new_fre = arg2 & TARGET_PR_FP_MODE_FRE;
  9149. const unsigned int known_bits = TARGET_PR_FP_MODE_FR |
  9150. TARGET_PR_FP_MODE_FRE;
  9151. /* If nothing to change, return right away, successfully. */
  9152. if (old_fr == new_fr && old_fre == new_fre) {
  9153. return 0;
  9154. }
  9155. /* Check the value is valid */
  9156. if (arg2 & ~known_bits) {
  9157. return -TARGET_EOPNOTSUPP;
  9158. }
  9159. /* Setting FRE without FR is not supported. */
  9160. if (new_fre && !new_fr) {
  9161. return -TARGET_EOPNOTSUPP;
  9162. }
  9163. if (new_fr && !(env->active_fpu.fcr0 & (1 << FCR0_F64))) {
  9164. /* FR1 is not supported */
  9165. return -TARGET_EOPNOTSUPP;
  9166. }
  9167. if (!new_fr && (env->active_fpu.fcr0 & (1 << FCR0_F64))
  9168. && !(env->CP0_Status_rw_bitmask & (1 << CP0St_FR))) {
  9169. /* cannot set FR=0 */
  9170. return -TARGET_EOPNOTSUPP;
  9171. }
  9172. if (new_fre && !(env->active_fpu.fcr0 & (1 << FCR0_FREP))) {
  9173. /* Cannot set FRE=1 */
  9174. return -TARGET_EOPNOTSUPP;
  9175. }
  9176. int i;
  9177. fpr_t *fpr = env->active_fpu.fpr;
  9178. for (i = 0; i < 32 ; i += 2) {
  9179. if (!old_fr && new_fr) {
  9180. fpr[i].w[!FP_ENDIAN_IDX] = fpr[i + 1].w[FP_ENDIAN_IDX];
  9181. } else if (old_fr && !new_fr) {
  9182. fpr[i + 1].w[FP_ENDIAN_IDX] = fpr[i].w[!FP_ENDIAN_IDX];
  9183. }
  9184. }
  9185. if (new_fr) {
  9186. env->CP0_Status |= (1 << CP0St_FR);
  9187. env->hflags |= MIPS_HFLAG_F64;
  9188. } else {
  9189. env->CP0_Status &= ~(1 << CP0St_FR);
  9190. env->hflags &= ~MIPS_HFLAG_F64;
  9191. }
  9192. if (new_fre) {
  9193. env->CP0_Config5 |= (1 << CP0C5_FRE);
  9194. if (env->active_fpu.fcr0 & (1 << FCR0_FREP)) {
  9195. env->hflags |= MIPS_HFLAG_FRE;
  9196. }
  9197. } else {
  9198. env->CP0_Config5 &= ~(1 << CP0C5_FRE);
  9199. env->hflags &= ~MIPS_HFLAG_FRE;
  9200. }
  9201. return 0;
  9202. }
  9203. #endif /* MIPS */
  9204. #ifdef TARGET_AARCH64
  9205. case TARGET_PR_SVE_SET_VL:
  9206. /*
  9207. * We cannot support either PR_SVE_SET_VL_ONEXEC or
  9208. * PR_SVE_VL_INHERIT. Note the kernel definition
  9209. * of sve_vl_valid allows for VQ=512, i.e. VL=8192,
  9210. * even though the current architectural maximum is VQ=16.
  9211. */
  9212. ret = -TARGET_EINVAL;
  9213. if (cpu_isar_feature(aa64_sve, env_archcpu(cpu_env))
  9214. && arg2 >= 0 && arg2 <= 512 * 16 && !(arg2 & 15)) {
  9215. CPUARMState *env = cpu_env;
  9216. ARMCPU *cpu = env_archcpu(env);
  9217. uint32_t vq, old_vq;
  9218. old_vq = (env->vfp.zcr_el[1] & 0xf) + 1;
  9219. vq = MAX(arg2 / 16, 1);
  9220. vq = MIN(vq, cpu->sve_max_vq);
  9221. if (vq < old_vq) {
  9222. aarch64_sve_narrow_vq(env, vq);
  9223. }
  9224. env->vfp.zcr_el[1] = vq - 1;
  9225. arm_rebuild_hflags(env);
  9226. ret = vq * 16;
  9227. }
  9228. return ret;
  9229. case TARGET_PR_SVE_GET_VL:
  9230. ret = -TARGET_EINVAL;
  9231. {
  9232. ARMCPU *cpu = env_archcpu(cpu_env);
  9233. if (cpu_isar_feature(aa64_sve, cpu)) {
  9234. ret = ((cpu->env.vfp.zcr_el[1] & 0xf) + 1) * 16;
  9235. }
  9236. }
  9237. return ret;
  9238. case TARGET_PR_PAC_RESET_KEYS:
  9239. {
  9240. CPUARMState *env = cpu_env;
  9241. ARMCPU *cpu = env_archcpu(env);
  9242. if (arg3 || arg4 || arg5) {
  9243. return -TARGET_EINVAL;
  9244. }
  9245. if (cpu_isar_feature(aa64_pauth, cpu)) {
  9246. int all = (TARGET_PR_PAC_APIAKEY | TARGET_PR_PAC_APIBKEY |
  9247. TARGET_PR_PAC_APDAKEY | TARGET_PR_PAC_APDBKEY |
  9248. TARGET_PR_PAC_APGAKEY);
  9249. int ret = 0;
  9250. Error *err = NULL;
  9251. if (arg2 == 0) {
  9252. arg2 = all;
  9253. } else if (arg2 & ~all) {
  9254. return -TARGET_EINVAL;
  9255. }
  9256. if (arg2 & TARGET_PR_PAC_APIAKEY) {
  9257. ret |= qemu_guest_getrandom(&env->keys.apia,
  9258. sizeof(ARMPACKey), &err);
  9259. }
  9260. if (arg2 & TARGET_PR_PAC_APIBKEY) {
  9261. ret |= qemu_guest_getrandom(&env->keys.apib,
  9262. sizeof(ARMPACKey), &err);
  9263. }
  9264. if (arg2 & TARGET_PR_PAC_APDAKEY) {
  9265. ret |= qemu_guest_getrandom(&env->keys.apda,
  9266. sizeof(ARMPACKey), &err);
  9267. }
  9268. if (arg2 & TARGET_PR_PAC_APDBKEY) {
  9269. ret |= qemu_guest_getrandom(&env->keys.apdb,
  9270. sizeof(ARMPACKey), &err);
  9271. }
  9272. if (arg2 & TARGET_PR_PAC_APGAKEY) {
  9273. ret |= qemu_guest_getrandom(&env->keys.apga,
  9274. sizeof(ARMPACKey), &err);
  9275. }
  9276. if (ret != 0) {
  9277. /*
  9278. * Some unknown failure in the crypto. The best
  9279. * we can do is log it and fail the syscall.
  9280. * The real syscall cannot fail this way.
  9281. */
  9282. qemu_log_mask(LOG_UNIMP,
  9283. "PR_PAC_RESET_KEYS: Crypto failure: %s",
  9284. error_get_pretty(err));
  9285. error_free(err);
  9286. return -TARGET_EIO;
  9287. }
  9288. return 0;
  9289. }
  9290. }
  9291. return -TARGET_EINVAL;
  9292. #endif /* AARCH64 */
  9293. case PR_GET_SECCOMP:
  9294. case PR_SET_SECCOMP:
  9295. /* Disable seccomp to prevent the target disabling syscalls we
  9296. * need. */
  9297. return -TARGET_EINVAL;
  9298. default:
  9299. /* Most prctl options have no pointer arguments */
  9300. return get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
  9301. }
  9302. break;
  9303. #ifdef TARGET_NR_arch_prctl
  9304. case TARGET_NR_arch_prctl:
  9305. #if defined(TARGET_I386) && !defined(TARGET_ABI32)
  9306. return do_arch_prctl(cpu_env, arg1, arg2);
  9307. #else
  9308. #error unreachable
  9309. #endif
  9310. #endif
  9311. #ifdef TARGET_NR_pread64
  9312. case TARGET_NR_pread64:
  9313. if (regpairs_aligned(cpu_env, num)) {
  9314. arg4 = arg5;
  9315. arg5 = arg6;
  9316. }
  9317. if (arg2 == 0 && arg3 == 0) {
  9318. /* Special-case NULL buffer and zero length, which should succeed */
  9319. p = 0;
  9320. } else {
  9321. p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  9322. if (!p) {
  9323. return -TARGET_EFAULT;
  9324. }
  9325. }
  9326. ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
  9327. unlock_user(p, arg2, ret);
  9328. return ret;
  9329. case TARGET_NR_pwrite64:
  9330. if (regpairs_aligned(cpu_env, num)) {
  9331. arg4 = arg5;
  9332. arg5 = arg6;
  9333. }
  9334. if (arg2 == 0 && arg3 == 0) {
  9335. /* Special-case NULL buffer and zero length, which should succeed */
  9336. p = 0;
  9337. } else {
  9338. p = lock_user(VERIFY_READ, arg2, arg3, 1);
  9339. if (!p) {
  9340. return -TARGET_EFAULT;
  9341. }
  9342. }
  9343. ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
  9344. unlock_user(p, arg2, 0);
  9345. return ret;
  9346. #endif
  9347. case TARGET_NR_getcwd:
  9348. if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
  9349. return -TARGET_EFAULT;
  9350. ret = get_errno(sys_getcwd1(p, arg2));
  9351. unlock_user(p, arg1, ret);
  9352. return ret;
  9353. case TARGET_NR_capget:
  9354. case TARGET_NR_capset:
  9355. {
  9356. struct target_user_cap_header *target_header;
  9357. struct target_user_cap_data *target_data = NULL;
  9358. struct __user_cap_header_struct header;
  9359. struct __user_cap_data_struct data[2];
  9360. struct __user_cap_data_struct *dataptr = NULL;
  9361. int i, target_datalen;
  9362. int data_items = 1;
  9363. if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) {
  9364. return -TARGET_EFAULT;
  9365. }
  9366. header.version = tswap32(target_header->version);
  9367. header.pid = tswap32(target_header->pid);
  9368. if (header.version != _LINUX_CAPABILITY_VERSION) {
  9369. /* Version 2 and up takes pointer to two user_data structs */
  9370. data_items = 2;
  9371. }
  9372. target_datalen = sizeof(*target_data) * data_items;
  9373. if (arg2) {
  9374. if (num == TARGET_NR_capget) {
  9375. target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0);
  9376. } else {
  9377. target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1);
  9378. }
  9379. if (!target_data) {
  9380. unlock_user_struct(target_header, arg1, 0);
  9381. return -TARGET_EFAULT;
  9382. }
  9383. if (num == TARGET_NR_capset) {
  9384. for (i = 0; i < data_items; i++) {
  9385. data[i].effective = tswap32(target_data[i].effective);
  9386. data[i].permitted = tswap32(target_data[i].permitted);
  9387. data[i].inheritable = tswap32(target_data[i].inheritable);
  9388. }
  9389. }
  9390. dataptr = data;
  9391. }
  9392. if (num == TARGET_NR_capget) {
  9393. ret = get_errno(capget(&header, dataptr));
  9394. } else {
  9395. ret = get_errno(capset(&header, dataptr));
  9396. }
  9397. /* The kernel always updates version for both capget and capset */
  9398. target_header->version = tswap32(header.version);
  9399. unlock_user_struct(target_header, arg1, 1);
  9400. if (arg2) {
  9401. if (num == TARGET_NR_capget) {
  9402. for (i = 0; i < data_items; i++) {
  9403. target_data[i].effective = tswap32(data[i].effective);
  9404. target_data[i].permitted = tswap32(data[i].permitted);
  9405. target_data[i].inheritable = tswap32(data[i].inheritable);
  9406. }
  9407. unlock_user(target_data, arg2, target_datalen);
  9408. } else {
  9409. unlock_user(target_data, arg2, 0);
  9410. }
  9411. }
  9412. return ret;
  9413. }
  9414. case TARGET_NR_sigaltstack:
  9415. return do_sigaltstack(arg1, arg2,
  9416. get_sp_from_cpustate((CPUArchState *)cpu_env));
  9417. #ifdef CONFIG_SENDFILE
  9418. #ifdef TARGET_NR_sendfile
  9419. case TARGET_NR_sendfile:
  9420. {
  9421. off_t *offp = NULL;
  9422. off_t off;
  9423. if (arg3) {
  9424. ret = get_user_sal(off, arg3);
  9425. if (is_error(ret)) {
  9426. return ret;
  9427. }
  9428. offp = &off;
  9429. }
  9430. ret = get_errno(sendfile(arg1, arg2, offp, arg4));
  9431. if (!is_error(ret) && arg3) {
  9432. abi_long ret2 = put_user_sal(off, arg3);
  9433. if (is_error(ret2)) {
  9434. ret = ret2;
  9435. }
  9436. }
  9437. return ret;
  9438. }
  9439. #endif
  9440. #ifdef TARGET_NR_sendfile64
  9441. case TARGET_NR_sendfile64:
  9442. {
  9443. off_t *offp = NULL;
  9444. off_t off;
  9445. if (arg3) {
  9446. ret = get_user_s64(off, arg3);
  9447. if (is_error(ret)) {
  9448. return ret;
  9449. }
  9450. offp = &off;
  9451. }
  9452. ret = get_errno(sendfile(arg1, arg2, offp, arg4));
  9453. if (!is_error(ret) && arg3) {
  9454. abi_long ret2 = put_user_s64(off, arg3);
  9455. if (is_error(ret2)) {
  9456. ret = ret2;
  9457. }
  9458. }
  9459. return ret;
  9460. }
  9461. #endif
  9462. #endif
  9463. #ifdef TARGET_NR_vfork
  9464. case TARGET_NR_vfork:
  9465. return get_errno(do_fork(cpu_env,
  9466. CLONE_VFORK | CLONE_VM | TARGET_SIGCHLD,
  9467. 0, 0, 0, 0));
  9468. #endif
  9469. #ifdef TARGET_NR_ugetrlimit
  9470. case TARGET_NR_ugetrlimit:
  9471. {
  9472. struct rlimit rlim;
  9473. int resource = target_to_host_resource(arg1);
  9474. ret = get_errno(getrlimit(resource, &rlim));
  9475. if (!is_error(ret)) {
  9476. struct target_rlimit *target_rlim;
  9477. if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
  9478. return -TARGET_EFAULT;
  9479. target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
  9480. target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
  9481. unlock_user_struct(target_rlim, arg2, 1);
  9482. }
  9483. return ret;
  9484. }
  9485. #endif
  9486. #ifdef TARGET_NR_truncate64
  9487. case TARGET_NR_truncate64:
  9488. if (!(p = lock_user_string(arg1)))
  9489. return -TARGET_EFAULT;
  9490. ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
  9491. unlock_user(p, arg1, 0);
  9492. return ret;
  9493. #endif
  9494. #ifdef TARGET_NR_ftruncate64
  9495. case TARGET_NR_ftruncate64:
  9496. return target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
  9497. #endif
  9498. #ifdef TARGET_NR_stat64
  9499. case TARGET_NR_stat64:
  9500. if (!(p = lock_user_string(arg1))) {
  9501. return -TARGET_EFAULT;
  9502. }
  9503. ret = get_errno(stat(path(p), &st));
  9504. unlock_user(p, arg1, 0);
  9505. if (!is_error(ret))
  9506. ret = host_to_target_stat64(cpu_env, arg2, &st);
  9507. return ret;
  9508. #endif
  9509. #ifdef TARGET_NR_lstat64
  9510. case TARGET_NR_lstat64:
  9511. if (!(p = lock_user_string(arg1))) {
  9512. return -TARGET_EFAULT;
  9513. }
  9514. ret = get_errno(lstat(path(p), &st));
  9515. unlock_user(p, arg1, 0);
  9516. if (!is_error(ret))
  9517. ret = host_to_target_stat64(cpu_env, arg2, &st);
  9518. return ret;
  9519. #endif
  9520. #ifdef TARGET_NR_fstat64
  9521. case TARGET_NR_fstat64:
  9522. ret = get_errno(fstat(arg1, &st));
  9523. if (!is_error(ret))
  9524. ret = host_to_target_stat64(cpu_env, arg2, &st);
  9525. return ret;
  9526. #endif
  9527. #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
  9528. #ifdef TARGET_NR_fstatat64
  9529. case TARGET_NR_fstatat64:
  9530. #endif
  9531. #ifdef TARGET_NR_newfstatat
  9532. case TARGET_NR_newfstatat:
  9533. #endif
  9534. if (!(p = lock_user_string(arg2))) {
  9535. return -TARGET_EFAULT;
  9536. }
  9537. ret = get_errno(fstatat(arg1, path(p), &st, arg4));
  9538. unlock_user(p, arg2, 0);
  9539. if (!is_error(ret))
  9540. ret = host_to_target_stat64(cpu_env, arg3, &st);
  9541. return ret;
  9542. #endif
  9543. #if defined(TARGET_NR_statx)
  9544. case TARGET_NR_statx:
  9545. {
  9546. struct target_statx *target_stx;
  9547. int dirfd = arg1;
  9548. int flags = arg3;
  9549. p = lock_user_string(arg2);
  9550. if (p == NULL) {
  9551. return -TARGET_EFAULT;
  9552. }
  9553. #if defined(__NR_statx)
  9554. {
  9555. /*
  9556. * It is assumed that struct statx is architecture independent.
  9557. */
  9558. struct target_statx host_stx;
  9559. int mask = arg4;
  9560. ret = get_errno(sys_statx(dirfd, p, flags, mask, &host_stx));
  9561. if (!is_error(ret)) {
  9562. if (host_to_target_statx(&host_stx, arg5) != 0) {
  9563. unlock_user(p, arg2, 0);
  9564. return -TARGET_EFAULT;
  9565. }
  9566. }
  9567. if (ret != -TARGET_ENOSYS) {
  9568. unlock_user(p, arg2, 0);
  9569. return ret;
  9570. }
  9571. }
  9572. #endif
  9573. ret = get_errno(fstatat(dirfd, path(p), &st, flags));
  9574. unlock_user(p, arg2, 0);
  9575. if (!is_error(ret)) {
  9576. if (!lock_user_struct(VERIFY_WRITE, target_stx, arg5, 0)) {
  9577. return -TARGET_EFAULT;
  9578. }
  9579. memset(target_stx, 0, sizeof(*target_stx));
  9580. __put_user(major(st.st_dev), &target_stx->stx_dev_major);
  9581. __put_user(minor(st.st_dev), &target_stx->stx_dev_minor);
  9582. __put_user(st.st_ino, &target_stx->stx_ino);
  9583. __put_user(st.st_mode, &target_stx->stx_mode);
  9584. __put_user(st.st_uid, &target_stx->stx_uid);
  9585. __put_user(st.st_gid, &target_stx->stx_gid);
  9586. __put_user(st.st_nlink, &target_stx->stx_nlink);
  9587. __put_user(major(st.st_rdev), &target_stx->stx_rdev_major);
  9588. __put_user(minor(st.st_rdev), &target_stx->stx_rdev_minor);
  9589. __put_user(st.st_size, &target_stx->stx_size);
  9590. __put_user(st.st_blksize, &target_stx->stx_blksize);
  9591. __put_user(st.st_blocks, &target_stx->stx_blocks);
  9592. __put_user(st.st_atime, &target_stx->stx_atime.tv_sec);
  9593. __put_user(st.st_mtime, &target_stx->stx_mtime.tv_sec);
  9594. __put_user(st.st_ctime, &target_stx->stx_ctime.tv_sec);
  9595. unlock_user_struct(target_stx, arg5, 1);
  9596. }
  9597. }
  9598. return ret;
  9599. #endif
  9600. #ifdef TARGET_NR_lchown
  9601. case TARGET_NR_lchown:
  9602. if (!(p = lock_user_string(arg1)))
  9603. return -TARGET_EFAULT;
  9604. ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
  9605. unlock_user(p, arg1, 0);
  9606. return ret;
  9607. #endif
  9608. #ifdef TARGET_NR_getuid
  9609. case TARGET_NR_getuid:
  9610. return get_errno(high2lowuid(getuid()));
  9611. #endif
  9612. #ifdef TARGET_NR_getgid
  9613. case TARGET_NR_getgid:
  9614. return get_errno(high2lowgid(getgid()));
  9615. #endif
  9616. #ifdef TARGET_NR_geteuid
  9617. case TARGET_NR_geteuid:
  9618. return get_errno(high2lowuid(geteuid()));
  9619. #endif
  9620. #ifdef TARGET_NR_getegid
  9621. case TARGET_NR_getegid:
  9622. return get_errno(high2lowgid(getegid()));
  9623. #endif
  9624. case TARGET_NR_setreuid:
  9625. return get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
  9626. case TARGET_NR_setregid:
  9627. return get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
  9628. case TARGET_NR_getgroups:
  9629. {
  9630. int gidsetsize = arg1;
  9631. target_id *target_grouplist;
  9632. gid_t *grouplist;
  9633. int i;
  9634. grouplist = alloca(gidsetsize * sizeof(gid_t));
  9635. ret = get_errno(getgroups(gidsetsize, grouplist));
  9636. if (gidsetsize == 0)
  9637. return ret;
  9638. if (!is_error(ret)) {
  9639. target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * sizeof(target_id), 0);
  9640. if (!target_grouplist)
  9641. return -TARGET_EFAULT;
  9642. for(i = 0;i < ret; i++)
  9643. target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
  9644. unlock_user(target_grouplist, arg2, gidsetsize * sizeof(target_id));
  9645. }
  9646. }
  9647. return ret;
  9648. case TARGET_NR_setgroups:
  9649. {
  9650. int gidsetsize = arg1;
  9651. target_id *target_grouplist;
  9652. gid_t *grouplist = NULL;
  9653. int i;
  9654. if (gidsetsize) {
  9655. grouplist = alloca(gidsetsize * sizeof(gid_t));
  9656. target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * sizeof(target_id), 1);
  9657. if (!target_grouplist) {
  9658. return -TARGET_EFAULT;
  9659. }
  9660. for (i = 0; i < gidsetsize; i++) {
  9661. grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
  9662. }
  9663. unlock_user(target_grouplist, arg2, 0);
  9664. }
  9665. return get_errno(setgroups(gidsetsize, grouplist));
  9666. }
  9667. case TARGET_NR_fchown:
  9668. return get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
  9669. #if defined(TARGET_NR_fchownat)
  9670. case TARGET_NR_fchownat:
  9671. if (!(p = lock_user_string(arg2)))
  9672. return -TARGET_EFAULT;
  9673. ret = get_errno(fchownat(arg1, p, low2highuid(arg3),
  9674. low2highgid(arg4), arg5));
  9675. unlock_user(p, arg2, 0);
  9676. return ret;
  9677. #endif
  9678. #ifdef TARGET_NR_setresuid
  9679. case TARGET_NR_setresuid:
  9680. return get_errno(sys_setresuid(low2highuid(arg1),
  9681. low2highuid(arg2),
  9682. low2highuid(arg3)));
  9683. #endif
  9684. #ifdef TARGET_NR_getresuid
  9685. case TARGET_NR_getresuid:
  9686. {
  9687. uid_t ruid, euid, suid;
  9688. ret = get_errno(getresuid(&ruid, &euid, &suid));
  9689. if (!is_error(ret)) {
  9690. if (put_user_id(high2lowuid(ruid), arg1)
  9691. || put_user_id(high2lowuid(euid), arg2)
  9692. || put_user_id(high2lowuid(suid), arg3))
  9693. return -TARGET_EFAULT;
  9694. }
  9695. }
  9696. return ret;
  9697. #endif
  9698. #ifdef TARGET_NR_getresgid
  9699. case TARGET_NR_setresgid:
  9700. return get_errno(sys_setresgid(low2highgid(arg1),
  9701. low2highgid(arg2),
  9702. low2highgid(arg3)));
  9703. #endif
  9704. #ifdef TARGET_NR_getresgid
  9705. case TARGET_NR_getresgid:
  9706. {
  9707. gid_t rgid, egid, sgid;
  9708. ret = get_errno(getresgid(&rgid, &egid, &sgid));
  9709. if (!is_error(ret)) {
  9710. if (put_user_id(high2lowgid(rgid), arg1)
  9711. || put_user_id(high2lowgid(egid), arg2)
  9712. || put_user_id(high2lowgid(sgid), arg3))
  9713. return -TARGET_EFAULT;
  9714. }
  9715. }
  9716. return ret;
  9717. #endif
  9718. #ifdef TARGET_NR_chown
  9719. case TARGET_NR_chown:
  9720. if (!(p = lock_user_string(arg1)))
  9721. return -TARGET_EFAULT;
  9722. ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
  9723. unlock_user(p, arg1, 0);
  9724. return ret;
  9725. #endif
  9726. case TARGET_NR_setuid:
  9727. return get_errno(sys_setuid(low2highuid(arg1)));
  9728. case TARGET_NR_setgid:
  9729. return get_errno(sys_setgid(low2highgid(arg1)));
  9730. case TARGET_NR_setfsuid:
  9731. return get_errno(setfsuid(arg1));
  9732. case TARGET_NR_setfsgid:
  9733. return get_errno(setfsgid(arg1));
  9734. #ifdef TARGET_NR_lchown32
  9735. case TARGET_NR_lchown32:
  9736. if (!(p = lock_user_string(arg1)))
  9737. return -TARGET_EFAULT;
  9738. ret = get_errno(lchown(p, arg2, arg3));
  9739. unlock_user(p, arg1, 0);
  9740. return ret;
  9741. #endif
  9742. #ifdef TARGET_NR_getuid32
  9743. case TARGET_NR_getuid32:
  9744. return get_errno(getuid());
  9745. #endif
  9746. #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
  9747. /* Alpha specific */
  9748. case TARGET_NR_getxuid:
  9749. {
  9750. uid_t euid;
  9751. euid=geteuid();
  9752. ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
  9753. }
  9754. return get_errno(getuid());
  9755. #endif
  9756. #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
  9757. /* Alpha specific */
  9758. case TARGET_NR_getxgid:
  9759. {
  9760. uid_t egid;
  9761. egid=getegid();
  9762. ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
  9763. }
  9764. return get_errno(getgid());
  9765. #endif
  9766. #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
  9767. /* Alpha specific */
  9768. case TARGET_NR_osf_getsysinfo:
  9769. ret = -TARGET_EOPNOTSUPP;
  9770. switch (arg1) {
  9771. case TARGET_GSI_IEEE_FP_CONTROL:
  9772. {
  9773. uint64_t fpcr = cpu_alpha_load_fpcr(cpu_env);
  9774. uint64_t swcr = ((CPUAlphaState *)cpu_env)->swcr;
  9775. swcr &= ~SWCR_STATUS_MASK;
  9776. swcr |= (fpcr >> 35) & SWCR_STATUS_MASK;
  9777. if (put_user_u64 (swcr, arg2))
  9778. return -TARGET_EFAULT;
  9779. ret = 0;
  9780. }
  9781. break;
  9782. /* case GSI_IEEE_STATE_AT_SIGNAL:
  9783. -- Not implemented in linux kernel.
  9784. case GSI_UACPROC:
  9785. -- Retrieves current unaligned access state; not much used.
  9786. case GSI_PROC_TYPE:
  9787. -- Retrieves implver information; surely not used.
  9788. case GSI_GET_HWRPB:
  9789. -- Grabs a copy of the HWRPB; surely not used.
  9790. */
  9791. }
  9792. return ret;
  9793. #endif
  9794. #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
  9795. /* Alpha specific */
  9796. case TARGET_NR_osf_setsysinfo:
  9797. ret = -TARGET_EOPNOTSUPP;
  9798. switch (arg1) {
  9799. case TARGET_SSI_IEEE_FP_CONTROL:
  9800. {
  9801. uint64_t swcr, fpcr;
  9802. if (get_user_u64 (swcr, arg2)) {
  9803. return -TARGET_EFAULT;
  9804. }
  9805. /*
  9806. * The kernel calls swcr_update_status to update the
  9807. * status bits from the fpcr at every point that it
  9808. * could be queried. Therefore, we store the status
  9809. * bits only in FPCR.
  9810. */
  9811. ((CPUAlphaState *)cpu_env)->swcr
  9812. = swcr & (SWCR_TRAP_ENABLE_MASK | SWCR_MAP_MASK);
  9813. fpcr = cpu_alpha_load_fpcr(cpu_env);
  9814. fpcr &= ((uint64_t)FPCR_DYN_MASK << 32);
  9815. fpcr |= alpha_ieee_swcr_to_fpcr(swcr);
  9816. cpu_alpha_store_fpcr(cpu_env, fpcr);
  9817. ret = 0;
  9818. }
  9819. break;
  9820. case TARGET_SSI_IEEE_RAISE_EXCEPTION:
  9821. {
  9822. uint64_t exc, fpcr, fex;
  9823. if (get_user_u64(exc, arg2)) {
  9824. return -TARGET_EFAULT;
  9825. }
  9826. exc &= SWCR_STATUS_MASK;
  9827. fpcr = cpu_alpha_load_fpcr(cpu_env);
  9828. /* Old exceptions are not signaled. */
  9829. fex = alpha_ieee_fpcr_to_swcr(fpcr);
  9830. fex = exc & ~fex;
  9831. fex >>= SWCR_STATUS_TO_EXCSUM_SHIFT;
  9832. fex &= ((CPUArchState *)cpu_env)->swcr;
  9833. /* Update the hardware fpcr. */
  9834. fpcr |= alpha_ieee_swcr_to_fpcr(exc);
  9835. cpu_alpha_store_fpcr(cpu_env, fpcr);
  9836. if (fex) {
  9837. int si_code = TARGET_FPE_FLTUNK;
  9838. target_siginfo_t info;
  9839. if (fex & SWCR_TRAP_ENABLE_DNO) {
  9840. si_code = TARGET_FPE_FLTUND;
  9841. }
  9842. if (fex & SWCR_TRAP_ENABLE_INE) {
  9843. si_code = TARGET_FPE_FLTRES;
  9844. }
  9845. if (fex & SWCR_TRAP_ENABLE_UNF) {
  9846. si_code = TARGET_FPE_FLTUND;
  9847. }
  9848. if (fex & SWCR_TRAP_ENABLE_OVF) {
  9849. si_code = TARGET_FPE_FLTOVF;
  9850. }
  9851. if (fex & SWCR_TRAP_ENABLE_DZE) {
  9852. si_code = TARGET_FPE_FLTDIV;
  9853. }
  9854. if (fex & SWCR_TRAP_ENABLE_INV) {
  9855. si_code = TARGET_FPE_FLTINV;
  9856. }
  9857. info.si_signo = SIGFPE;
  9858. info.si_errno = 0;
  9859. info.si_code = si_code;
  9860. info._sifields._sigfault._addr
  9861. = ((CPUArchState *)cpu_env)->pc;
  9862. queue_signal((CPUArchState *)cpu_env, info.si_signo,
  9863. QEMU_SI_FAULT, &info);
  9864. }
  9865. ret = 0;
  9866. }
  9867. break;
  9868. /* case SSI_NVPAIRS:
  9869. -- Used with SSIN_UACPROC to enable unaligned accesses.
  9870. case SSI_IEEE_STATE_AT_SIGNAL:
  9871. case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
  9872. -- Not implemented in linux kernel
  9873. */
  9874. }
  9875. return ret;
  9876. #endif
  9877. #ifdef TARGET_NR_osf_sigprocmask
  9878. /* Alpha specific. */
  9879. case TARGET_NR_osf_sigprocmask:
  9880. {
  9881. abi_ulong mask;
  9882. int how;
  9883. sigset_t set, oldset;
  9884. switch(arg1) {
  9885. case TARGET_SIG_BLOCK:
  9886. how = SIG_BLOCK;
  9887. break;
  9888. case TARGET_SIG_UNBLOCK:
  9889. how = SIG_UNBLOCK;
  9890. break;
  9891. case TARGET_SIG_SETMASK:
  9892. how = SIG_SETMASK;
  9893. break;
  9894. default:
  9895. return -TARGET_EINVAL;
  9896. }
  9897. mask = arg2;
  9898. target_to_host_old_sigset(&set, &mask);
  9899. ret = do_sigprocmask(how, &set, &oldset);
  9900. if (!ret) {
  9901. host_to_target_old_sigset(&mask, &oldset);
  9902. ret = mask;
  9903. }
  9904. }
  9905. return ret;
  9906. #endif
  9907. #ifdef TARGET_NR_getgid32
  9908. case TARGET_NR_getgid32:
  9909. return get_errno(getgid());
  9910. #endif
  9911. #ifdef TARGET_NR_geteuid32
  9912. case TARGET_NR_geteuid32:
  9913. return get_errno(geteuid());
  9914. #endif
  9915. #ifdef TARGET_NR_getegid32
  9916. case TARGET_NR_getegid32:
  9917. return get_errno(getegid());
  9918. #endif
  9919. #ifdef TARGET_NR_setreuid32
  9920. case TARGET_NR_setreuid32:
  9921. return get_errno(setreuid(arg1, arg2));
  9922. #endif
  9923. #ifdef TARGET_NR_setregid32
  9924. case TARGET_NR_setregid32:
  9925. return get_errno(setregid(arg1, arg2));
  9926. #endif
  9927. #ifdef TARGET_NR_getgroups32
  9928. case TARGET_NR_getgroups32:
  9929. {
  9930. int gidsetsize = arg1;
  9931. uint32_t *target_grouplist;
  9932. gid_t *grouplist;
  9933. int i;
  9934. grouplist = alloca(gidsetsize * sizeof(gid_t));
  9935. ret = get_errno(getgroups(gidsetsize, grouplist));
  9936. if (gidsetsize == 0)
  9937. return ret;
  9938. if (!is_error(ret)) {
  9939. target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
  9940. if (!target_grouplist) {
  9941. return -TARGET_EFAULT;
  9942. }
  9943. for(i = 0;i < ret; i++)
  9944. target_grouplist[i] = tswap32(grouplist[i]);
  9945. unlock_user(target_grouplist, arg2, gidsetsize * 4);
  9946. }
  9947. }
  9948. return ret;
  9949. #endif
  9950. #ifdef TARGET_NR_setgroups32
  9951. case TARGET_NR_setgroups32:
  9952. {
  9953. int gidsetsize = arg1;
  9954. uint32_t *target_grouplist;
  9955. gid_t *grouplist;
  9956. int i;
  9957. grouplist = alloca(gidsetsize * sizeof(gid_t));
  9958. target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
  9959. if (!target_grouplist) {
  9960. return -TARGET_EFAULT;
  9961. }
  9962. for(i = 0;i < gidsetsize; i++)
  9963. grouplist[i] = tswap32(target_grouplist[i]);
  9964. unlock_user(target_grouplist, arg2, 0);
  9965. return get_errno(setgroups(gidsetsize, grouplist));
  9966. }
  9967. #endif
  9968. #ifdef TARGET_NR_fchown32
  9969. case TARGET_NR_fchown32:
  9970. return get_errno(fchown(arg1, arg2, arg3));
  9971. #endif
  9972. #ifdef TARGET_NR_setresuid32
  9973. case TARGET_NR_setresuid32:
  9974. return get_errno(sys_setresuid(arg1, arg2, arg3));
  9975. #endif
  9976. #ifdef TARGET_NR_getresuid32
  9977. case TARGET_NR_getresuid32:
  9978. {
  9979. uid_t ruid, euid, suid;
  9980. ret = get_errno(getresuid(&ruid, &euid, &suid));
  9981. if (!is_error(ret)) {
  9982. if (put_user_u32(ruid, arg1)
  9983. || put_user_u32(euid, arg2)
  9984. || put_user_u32(suid, arg3))
  9985. return -TARGET_EFAULT;
  9986. }
  9987. }
  9988. return ret;
  9989. #endif
  9990. #ifdef TARGET_NR_setresgid32
  9991. case TARGET_NR_setresgid32:
  9992. return get_errno(sys_setresgid(arg1, arg2, arg3));
  9993. #endif
  9994. #ifdef TARGET_NR_getresgid32
  9995. case TARGET_NR_getresgid32:
  9996. {
  9997. gid_t rgid, egid, sgid;
  9998. ret = get_errno(getresgid(&rgid, &egid, &sgid));
  9999. if (!is_error(ret)) {
  10000. if (put_user_u32(rgid, arg1)
  10001. || put_user_u32(egid, arg2)
  10002. || put_user_u32(sgid, arg3))
  10003. return -TARGET_EFAULT;
  10004. }
  10005. }
  10006. return ret;
  10007. #endif
  10008. #ifdef TARGET_NR_chown32
  10009. case TARGET_NR_chown32:
  10010. if (!(p = lock_user_string(arg1)))
  10011. return -TARGET_EFAULT;
  10012. ret = get_errno(chown(p, arg2, arg3));
  10013. unlock_user(p, arg1, 0);
  10014. return ret;
  10015. #endif
  10016. #ifdef TARGET_NR_setuid32
  10017. case TARGET_NR_setuid32:
  10018. return get_errno(sys_setuid(arg1));
  10019. #endif
  10020. #ifdef TARGET_NR_setgid32
  10021. case TARGET_NR_setgid32:
  10022. return get_errno(sys_setgid(arg1));
  10023. #endif
  10024. #ifdef TARGET_NR_setfsuid32
  10025. case TARGET_NR_setfsuid32:
  10026. return get_errno(setfsuid(arg1));
  10027. #endif
  10028. #ifdef TARGET_NR_setfsgid32
  10029. case TARGET_NR_setfsgid32:
  10030. return get_errno(setfsgid(arg1));
  10031. #endif
  10032. #ifdef TARGET_NR_mincore
  10033. case TARGET_NR_mincore:
  10034. {
  10035. void *a = lock_user(VERIFY_READ, arg1, arg2, 0);
  10036. if (!a) {
  10037. return -TARGET_ENOMEM;
  10038. }
  10039. p = lock_user_string(arg3);
  10040. if (!p) {
  10041. ret = -TARGET_EFAULT;
  10042. } else {
  10043. ret = get_errno(mincore(a, arg2, p));
  10044. unlock_user(p, arg3, ret);
  10045. }
  10046. unlock_user(a, arg1, 0);
  10047. }
  10048. return ret;
  10049. #endif
  10050. #ifdef TARGET_NR_arm_fadvise64_64
  10051. case TARGET_NR_arm_fadvise64_64:
  10052. /* arm_fadvise64_64 looks like fadvise64_64 but
  10053. * with different argument order: fd, advice, offset, len
  10054. * rather than the usual fd, offset, len, advice.
  10055. * Note that offset and len are both 64-bit so appear as
  10056. * pairs of 32-bit registers.
  10057. */
  10058. ret = posix_fadvise(arg1, target_offset64(arg3, arg4),
  10059. target_offset64(arg5, arg6), arg2);
  10060. return -host_to_target_errno(ret);
  10061. #endif
  10062. #if TARGET_ABI_BITS == 32
  10063. #ifdef TARGET_NR_fadvise64_64
  10064. case TARGET_NR_fadvise64_64:
  10065. #if defined(TARGET_PPC) || defined(TARGET_XTENSA)
  10066. /* 6 args: fd, advice, offset (high, low), len (high, low) */
  10067. ret = arg2;
  10068. arg2 = arg3;
  10069. arg3 = arg4;
  10070. arg4 = arg5;
  10071. arg5 = arg6;
  10072. arg6 = ret;
  10073. #else
  10074. /* 6 args: fd, offset (high, low), len (high, low), advice */
  10075. if (regpairs_aligned(cpu_env, num)) {
  10076. /* offset is in (3,4), len in (5,6) and advice in 7 */
  10077. arg2 = arg3;
  10078. arg3 = arg4;
  10079. arg4 = arg5;
  10080. arg5 = arg6;
  10081. arg6 = arg7;
  10082. }
  10083. #endif
  10084. ret = posix_fadvise(arg1, target_offset64(arg2, arg3),
  10085. target_offset64(arg4, arg5), arg6);
  10086. return -host_to_target_errno(ret);
  10087. #endif
  10088. #ifdef TARGET_NR_fadvise64
  10089. case TARGET_NR_fadvise64:
  10090. /* 5 args: fd, offset (high, low), len, advice */
  10091. if (regpairs_aligned(cpu_env, num)) {
  10092. /* offset is in (3,4), len in 5 and advice in 6 */
  10093. arg2 = arg3;
  10094. arg3 = arg4;
  10095. arg4 = arg5;
  10096. arg5 = arg6;
  10097. }
  10098. ret = posix_fadvise(arg1, target_offset64(arg2, arg3), arg4, arg5);
  10099. return -host_to_target_errno(ret);
  10100. #endif
  10101. #else /* not a 32-bit ABI */
  10102. #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_fadvise64)
  10103. #ifdef TARGET_NR_fadvise64_64
  10104. case TARGET_NR_fadvise64_64:
  10105. #endif
  10106. #ifdef TARGET_NR_fadvise64
  10107. case TARGET_NR_fadvise64:
  10108. #endif
  10109. #ifdef TARGET_S390X
  10110. switch (arg4) {
  10111. case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
  10112. case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
  10113. case 6: arg4 = POSIX_FADV_DONTNEED; break;
  10114. case 7: arg4 = POSIX_FADV_NOREUSE; break;
  10115. default: break;
  10116. }
  10117. #endif
  10118. return -host_to_target_errno(posix_fadvise(arg1, arg2, arg3, arg4));
  10119. #endif
  10120. #endif /* end of 64-bit ABI fadvise handling */
  10121. #ifdef TARGET_NR_madvise
  10122. case TARGET_NR_madvise:
  10123. /* A straight passthrough may not be safe because qemu sometimes
  10124. turns private file-backed mappings into anonymous mappings.
  10125. This will break MADV_DONTNEED.
  10126. This is a hint, so ignoring and returning success is ok. */
  10127. return 0;
  10128. #endif
  10129. #if TARGET_ABI_BITS == 32
  10130. case TARGET_NR_fcntl64:
  10131. {
  10132. int cmd;
  10133. struct flock64 fl;
  10134. from_flock64_fn *copyfrom = copy_from_user_flock64;
  10135. to_flock64_fn *copyto = copy_to_user_flock64;
  10136. #ifdef TARGET_ARM
  10137. if (!((CPUARMState *)cpu_env)->eabi) {
  10138. copyfrom = copy_from_user_oabi_flock64;
  10139. copyto = copy_to_user_oabi_flock64;
  10140. }
  10141. #endif
  10142. cmd = target_to_host_fcntl_cmd(arg2);
  10143. if (cmd == -TARGET_EINVAL) {
  10144. return cmd;
  10145. }
  10146. switch(arg2) {
  10147. case TARGET_F_GETLK64:
  10148. ret = copyfrom(&fl, arg3);
  10149. if (ret) {
  10150. break;
  10151. }
  10152. ret = get_errno(safe_fcntl(arg1, cmd, &fl));
  10153. if (ret == 0) {
  10154. ret = copyto(arg3, &fl);
  10155. }
  10156. break;
  10157. case TARGET_F_SETLK64:
  10158. case TARGET_F_SETLKW64:
  10159. ret = copyfrom(&fl, arg3);
  10160. if (ret) {
  10161. break;
  10162. }
  10163. ret = get_errno(safe_fcntl(arg1, cmd, &fl));
  10164. break;
  10165. default:
  10166. ret = do_fcntl(arg1, arg2, arg3);
  10167. break;
  10168. }
  10169. return ret;
  10170. }
  10171. #endif
  10172. #ifdef TARGET_NR_cacheflush
  10173. case TARGET_NR_cacheflush:
  10174. /* self-modifying code is handled automatically, so nothing needed */
  10175. return 0;
  10176. #endif
  10177. #ifdef TARGET_NR_getpagesize
  10178. case TARGET_NR_getpagesize:
  10179. return TARGET_PAGE_SIZE;
  10180. #endif
  10181. case TARGET_NR_gettid:
  10182. return get_errno(sys_gettid());
  10183. #ifdef TARGET_NR_readahead
  10184. case TARGET_NR_readahead:
  10185. #if TARGET_ABI_BITS == 32
  10186. if (regpairs_aligned(cpu_env, num)) {
  10187. arg2 = arg3;
  10188. arg3 = arg4;
  10189. arg4 = arg5;
  10190. }
  10191. ret = get_errno(readahead(arg1, target_offset64(arg2, arg3) , arg4));
  10192. #else
  10193. ret = get_errno(readahead(arg1, arg2, arg3));
  10194. #endif
  10195. return ret;
  10196. #endif
  10197. #ifdef CONFIG_ATTR
  10198. #ifdef TARGET_NR_setxattr
  10199. case TARGET_NR_listxattr:
  10200. case TARGET_NR_llistxattr:
  10201. {
  10202. void *p, *b = 0;
  10203. if (arg2) {
  10204. b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  10205. if (!b) {
  10206. return -TARGET_EFAULT;
  10207. }
  10208. }
  10209. p = lock_user_string(arg1);
  10210. if (p) {
  10211. if (num == TARGET_NR_listxattr) {
  10212. ret = get_errno(listxattr(p, b, arg3));
  10213. } else {
  10214. ret = get_errno(llistxattr(p, b, arg3));
  10215. }
  10216. } else {
  10217. ret = -TARGET_EFAULT;
  10218. }
  10219. unlock_user(p, arg1, 0);
  10220. unlock_user(b, arg2, arg3);
  10221. return ret;
  10222. }
  10223. case TARGET_NR_flistxattr:
  10224. {
  10225. void *b = 0;
  10226. if (arg2) {
  10227. b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  10228. if (!b) {
  10229. return -TARGET_EFAULT;
  10230. }
  10231. }
  10232. ret = get_errno(flistxattr(arg1, b, arg3));
  10233. unlock_user(b, arg2, arg3);
  10234. return ret;
  10235. }
  10236. case TARGET_NR_setxattr:
  10237. case TARGET_NR_lsetxattr:
  10238. {
  10239. void *p, *n, *v = 0;
  10240. if (arg3) {
  10241. v = lock_user(VERIFY_READ, arg3, arg4, 1);
  10242. if (!v) {
  10243. return -TARGET_EFAULT;
  10244. }
  10245. }
  10246. p = lock_user_string(arg1);
  10247. n = lock_user_string(arg2);
  10248. if (p && n) {
  10249. if (num == TARGET_NR_setxattr) {
  10250. ret = get_errno(setxattr(p, n, v, arg4, arg5));
  10251. } else {
  10252. ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
  10253. }
  10254. } else {
  10255. ret = -TARGET_EFAULT;
  10256. }
  10257. unlock_user(p, arg1, 0);
  10258. unlock_user(n, arg2, 0);
  10259. unlock_user(v, arg3, 0);
  10260. }
  10261. return ret;
  10262. case TARGET_NR_fsetxattr:
  10263. {
  10264. void *n, *v = 0;
  10265. if (arg3) {
  10266. v = lock_user(VERIFY_READ, arg3, arg4, 1);
  10267. if (!v) {
  10268. return -TARGET_EFAULT;
  10269. }
  10270. }
  10271. n = lock_user_string(arg2);
  10272. if (n) {
  10273. ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
  10274. } else {
  10275. ret = -TARGET_EFAULT;
  10276. }
  10277. unlock_user(n, arg2, 0);
  10278. unlock_user(v, arg3, 0);
  10279. }
  10280. return ret;
  10281. case TARGET_NR_getxattr:
  10282. case TARGET_NR_lgetxattr:
  10283. {
  10284. void *p, *n, *v = 0;
  10285. if (arg3) {
  10286. v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  10287. if (!v) {
  10288. return -TARGET_EFAULT;
  10289. }
  10290. }
  10291. p = lock_user_string(arg1);
  10292. n = lock_user_string(arg2);
  10293. if (p && n) {
  10294. if (num == TARGET_NR_getxattr) {
  10295. ret = get_errno(getxattr(p, n, v, arg4));
  10296. } else {
  10297. ret = get_errno(lgetxattr(p, n, v, arg4));
  10298. }
  10299. } else {
  10300. ret = -TARGET_EFAULT;
  10301. }
  10302. unlock_user(p, arg1, 0);
  10303. unlock_user(n, arg2, 0);
  10304. unlock_user(v, arg3, arg4);
  10305. }
  10306. return ret;
  10307. case TARGET_NR_fgetxattr:
  10308. {
  10309. void *n, *v = 0;
  10310. if (arg3) {
  10311. v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  10312. if (!v) {
  10313. return -TARGET_EFAULT;
  10314. }
  10315. }
  10316. n = lock_user_string(arg2);
  10317. if (n) {
  10318. ret = get_errno(fgetxattr(arg1, n, v, arg4));
  10319. } else {
  10320. ret = -TARGET_EFAULT;
  10321. }
  10322. unlock_user(n, arg2, 0);
  10323. unlock_user(v, arg3, arg4);
  10324. }
  10325. return ret;
  10326. case TARGET_NR_removexattr:
  10327. case TARGET_NR_lremovexattr:
  10328. {
  10329. void *p, *n;
  10330. p = lock_user_string(arg1);
  10331. n = lock_user_string(arg2);
  10332. if (p && n) {
  10333. if (num == TARGET_NR_removexattr) {
  10334. ret = get_errno(removexattr(p, n));
  10335. } else {
  10336. ret = get_errno(lremovexattr(p, n));
  10337. }
  10338. } else {
  10339. ret = -TARGET_EFAULT;
  10340. }
  10341. unlock_user(p, arg1, 0);
  10342. unlock_user(n, arg2, 0);
  10343. }
  10344. return ret;
  10345. case TARGET_NR_fremovexattr:
  10346. {
  10347. void *n;
  10348. n = lock_user_string(arg2);
  10349. if (n) {
  10350. ret = get_errno(fremovexattr(arg1, n));
  10351. } else {
  10352. ret = -TARGET_EFAULT;
  10353. }
  10354. unlock_user(n, arg2, 0);
  10355. }
  10356. return ret;
  10357. #endif
  10358. #endif /* CONFIG_ATTR */
  10359. #ifdef TARGET_NR_set_thread_area
  10360. case TARGET_NR_set_thread_area:
  10361. #if defined(TARGET_MIPS)
  10362. ((CPUMIPSState *) cpu_env)->active_tc.CP0_UserLocal = arg1;
  10363. return 0;
  10364. #elif defined(TARGET_CRIS)
  10365. if (arg1 & 0xff)
  10366. ret = -TARGET_EINVAL;
  10367. else {
  10368. ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
  10369. ret = 0;
  10370. }
  10371. return ret;
  10372. #elif defined(TARGET_I386) && defined(TARGET_ABI32)
  10373. return do_set_thread_area(cpu_env, arg1);
  10374. #elif defined(TARGET_M68K)
  10375. {
  10376. TaskState *ts = cpu->opaque;
  10377. ts->tp_value = arg1;
  10378. return 0;
  10379. }
  10380. #else
  10381. return -TARGET_ENOSYS;
  10382. #endif
  10383. #endif
  10384. #ifdef TARGET_NR_get_thread_area
  10385. case TARGET_NR_get_thread_area:
  10386. #if defined(TARGET_I386) && defined(TARGET_ABI32)
  10387. return do_get_thread_area(cpu_env, arg1);
  10388. #elif defined(TARGET_M68K)
  10389. {
  10390. TaskState *ts = cpu->opaque;
  10391. return ts->tp_value;
  10392. }
  10393. #else
  10394. return -TARGET_ENOSYS;
  10395. #endif
  10396. #endif
  10397. #ifdef TARGET_NR_getdomainname
  10398. case TARGET_NR_getdomainname:
  10399. return -TARGET_ENOSYS;
  10400. #endif
  10401. #ifdef TARGET_NR_clock_settime
  10402. case TARGET_NR_clock_settime:
  10403. {
  10404. struct timespec ts;
  10405. ret = target_to_host_timespec(&ts, arg2);
  10406. if (!is_error(ret)) {
  10407. ret = get_errno(clock_settime(arg1, &ts));
  10408. }
  10409. return ret;
  10410. }
  10411. #endif
  10412. #ifdef TARGET_NR_clock_gettime
  10413. case TARGET_NR_clock_gettime:
  10414. {
  10415. struct timespec ts;
  10416. ret = get_errno(clock_gettime(arg1, &ts));
  10417. if (!is_error(ret)) {
  10418. ret = host_to_target_timespec(arg2, &ts);
  10419. }
  10420. return ret;
  10421. }
  10422. #endif
  10423. #ifdef TARGET_NR_clock_getres
  10424. case TARGET_NR_clock_getres:
  10425. {
  10426. struct timespec ts;
  10427. ret = get_errno(clock_getres(arg1, &ts));
  10428. if (!is_error(ret)) {
  10429. host_to_target_timespec(arg2, &ts);
  10430. }
  10431. return ret;
  10432. }
  10433. #endif
  10434. #ifdef TARGET_NR_clock_nanosleep
  10435. case TARGET_NR_clock_nanosleep:
  10436. {
  10437. struct timespec ts;
  10438. target_to_host_timespec(&ts, arg3);
  10439. ret = get_errno(safe_clock_nanosleep(arg1, arg2,
  10440. &ts, arg4 ? &ts : NULL));
  10441. if (arg4)
  10442. host_to_target_timespec(arg4, &ts);
  10443. #if defined(TARGET_PPC)
  10444. /* clock_nanosleep is odd in that it returns positive errno values.
  10445. * On PPC, CR0 bit 3 should be set in such a situation. */
  10446. if (ret && ret != -TARGET_ERESTARTSYS) {
  10447. ((CPUPPCState *)cpu_env)->crf[0] |= 1;
  10448. }
  10449. #endif
  10450. return ret;
  10451. }
  10452. #endif
  10453. #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
  10454. case TARGET_NR_set_tid_address:
  10455. return get_errno(set_tid_address((int *)g2h(arg1)));
  10456. #endif
  10457. case TARGET_NR_tkill:
  10458. return get_errno(safe_tkill((int)arg1, target_to_host_signal(arg2)));
  10459. case TARGET_NR_tgkill:
  10460. return get_errno(safe_tgkill((int)arg1, (int)arg2,
  10461. target_to_host_signal(arg3)));
  10462. #ifdef TARGET_NR_set_robust_list
  10463. case TARGET_NR_set_robust_list:
  10464. case TARGET_NR_get_robust_list:
  10465. /* The ABI for supporting robust futexes has userspace pass
  10466. * the kernel a pointer to a linked list which is updated by
  10467. * userspace after the syscall; the list is walked by the kernel
  10468. * when the thread exits. Since the linked list in QEMU guest
  10469. * memory isn't a valid linked list for the host and we have
  10470. * no way to reliably intercept the thread-death event, we can't
  10471. * support these. Silently return ENOSYS so that guest userspace
  10472. * falls back to a non-robust futex implementation (which should
  10473. * be OK except in the corner case of the guest crashing while
  10474. * holding a mutex that is shared with another process via
  10475. * shared memory).
  10476. */
  10477. return -TARGET_ENOSYS;
  10478. #endif
  10479. #if defined(TARGET_NR_utimensat)
  10480. case TARGET_NR_utimensat:
  10481. {
  10482. struct timespec *tsp, ts[2];
  10483. if (!arg3) {
  10484. tsp = NULL;
  10485. } else {
  10486. target_to_host_timespec(ts, arg3);
  10487. target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
  10488. tsp = ts;
  10489. }
  10490. if (!arg2)
  10491. ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
  10492. else {
  10493. if (!(p = lock_user_string(arg2))) {
  10494. return -TARGET_EFAULT;
  10495. }
  10496. ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
  10497. unlock_user(p, arg2, 0);
  10498. }
  10499. }
  10500. return ret;
  10501. #endif
  10502. case TARGET_NR_futex:
  10503. return do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
  10504. #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
  10505. case TARGET_NR_inotify_init:
  10506. ret = get_errno(sys_inotify_init());
  10507. if (ret >= 0) {
  10508. fd_trans_register(ret, &target_inotify_trans);
  10509. }
  10510. return ret;
  10511. #endif
  10512. #ifdef CONFIG_INOTIFY1
  10513. #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
  10514. case TARGET_NR_inotify_init1:
  10515. ret = get_errno(sys_inotify_init1(target_to_host_bitmask(arg1,
  10516. fcntl_flags_tbl)));
  10517. if (ret >= 0) {
  10518. fd_trans_register(ret, &target_inotify_trans);
  10519. }
  10520. return ret;
  10521. #endif
  10522. #endif
  10523. #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
  10524. case TARGET_NR_inotify_add_watch:
  10525. p = lock_user_string(arg2);
  10526. ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
  10527. unlock_user(p, arg2, 0);
  10528. return ret;
  10529. #endif
  10530. #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
  10531. case TARGET_NR_inotify_rm_watch:
  10532. return get_errno(sys_inotify_rm_watch(arg1, arg2));
  10533. #endif
  10534. #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
  10535. case TARGET_NR_mq_open:
  10536. {
  10537. struct mq_attr posix_mq_attr;
  10538. struct mq_attr *pposix_mq_attr;
  10539. int host_flags;
  10540. host_flags = target_to_host_bitmask(arg2, fcntl_flags_tbl);
  10541. pposix_mq_attr = NULL;
  10542. if (arg4) {
  10543. if (copy_from_user_mq_attr(&posix_mq_attr, arg4) != 0) {
  10544. return -TARGET_EFAULT;
  10545. }
  10546. pposix_mq_attr = &posix_mq_attr;
  10547. }
  10548. p = lock_user_string(arg1 - 1);
  10549. if (!p) {
  10550. return -TARGET_EFAULT;
  10551. }
  10552. ret = get_errno(mq_open(p, host_flags, arg3, pposix_mq_attr));
  10553. unlock_user (p, arg1, 0);
  10554. }
  10555. return ret;
  10556. case TARGET_NR_mq_unlink:
  10557. p = lock_user_string(arg1 - 1);
  10558. if (!p) {
  10559. return -TARGET_EFAULT;
  10560. }
  10561. ret = get_errno(mq_unlink(p));
  10562. unlock_user (p, arg1, 0);
  10563. return ret;
  10564. case TARGET_NR_mq_timedsend:
  10565. {
  10566. struct timespec ts;
  10567. p = lock_user (VERIFY_READ, arg2, arg3, 1);
  10568. if (arg5 != 0) {
  10569. target_to_host_timespec(&ts, arg5);
  10570. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts));
  10571. host_to_target_timespec(arg5, &ts);
  10572. } else {
  10573. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL));
  10574. }
  10575. unlock_user (p, arg2, arg3);
  10576. }
  10577. return ret;
  10578. case TARGET_NR_mq_timedreceive:
  10579. {
  10580. struct timespec ts;
  10581. unsigned int prio;
  10582. p = lock_user (VERIFY_READ, arg2, arg3, 1);
  10583. if (arg5 != 0) {
  10584. target_to_host_timespec(&ts, arg5);
  10585. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  10586. &prio, &ts));
  10587. host_to_target_timespec(arg5, &ts);
  10588. } else {
  10589. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  10590. &prio, NULL));
  10591. }
  10592. unlock_user (p, arg2, arg3);
  10593. if (arg4 != 0)
  10594. put_user_u32(prio, arg4);
  10595. }
  10596. return ret;
  10597. /* Not implemented for now... */
  10598. /* case TARGET_NR_mq_notify: */
  10599. /* break; */
  10600. case TARGET_NR_mq_getsetattr:
  10601. {
  10602. struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
  10603. ret = 0;
  10604. if (arg2 != 0) {
  10605. copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
  10606. ret = get_errno(mq_setattr(arg1, &posix_mq_attr_in,
  10607. &posix_mq_attr_out));
  10608. } else if (arg3 != 0) {
  10609. ret = get_errno(mq_getattr(arg1, &posix_mq_attr_out));
  10610. }
  10611. if (ret == 0 && arg3 != 0) {
  10612. copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
  10613. }
  10614. }
  10615. return ret;
  10616. #endif
  10617. #ifdef CONFIG_SPLICE
  10618. #ifdef TARGET_NR_tee
  10619. case TARGET_NR_tee:
  10620. {
  10621. ret = get_errno(tee(arg1,arg2,arg3,arg4));
  10622. }
  10623. return ret;
  10624. #endif
  10625. #ifdef TARGET_NR_splice
  10626. case TARGET_NR_splice:
  10627. {
  10628. loff_t loff_in, loff_out;
  10629. loff_t *ploff_in = NULL, *ploff_out = NULL;
  10630. if (arg2) {
  10631. if (get_user_u64(loff_in, arg2)) {
  10632. return -TARGET_EFAULT;
  10633. }
  10634. ploff_in = &loff_in;
  10635. }
  10636. if (arg4) {
  10637. if (get_user_u64(loff_out, arg4)) {
  10638. return -TARGET_EFAULT;
  10639. }
  10640. ploff_out = &loff_out;
  10641. }
  10642. ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
  10643. if (arg2) {
  10644. if (put_user_u64(loff_in, arg2)) {
  10645. return -TARGET_EFAULT;
  10646. }
  10647. }
  10648. if (arg4) {
  10649. if (put_user_u64(loff_out, arg4)) {
  10650. return -TARGET_EFAULT;
  10651. }
  10652. }
  10653. }
  10654. return ret;
  10655. #endif
  10656. #ifdef TARGET_NR_vmsplice
  10657. case TARGET_NR_vmsplice:
  10658. {
  10659. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  10660. if (vec != NULL) {
  10661. ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
  10662. unlock_iovec(vec, arg2, arg3, 0);
  10663. } else {
  10664. ret = -host_to_target_errno(errno);
  10665. }
  10666. }
  10667. return ret;
  10668. #endif
  10669. #endif /* CONFIG_SPLICE */
  10670. #ifdef CONFIG_EVENTFD
  10671. #if defined(TARGET_NR_eventfd)
  10672. case TARGET_NR_eventfd:
  10673. ret = get_errno(eventfd(arg1, 0));
  10674. if (ret >= 0) {
  10675. fd_trans_register(ret, &target_eventfd_trans);
  10676. }
  10677. return ret;
  10678. #endif
  10679. #if defined(TARGET_NR_eventfd2)
  10680. case TARGET_NR_eventfd2:
  10681. {
  10682. int host_flags = arg2 & (~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC));
  10683. if (arg2 & TARGET_O_NONBLOCK) {
  10684. host_flags |= O_NONBLOCK;
  10685. }
  10686. if (arg2 & TARGET_O_CLOEXEC) {
  10687. host_flags |= O_CLOEXEC;
  10688. }
  10689. ret = get_errno(eventfd(arg1, host_flags));
  10690. if (ret >= 0) {
  10691. fd_trans_register(ret, &target_eventfd_trans);
  10692. }
  10693. return ret;
  10694. }
  10695. #endif
  10696. #endif /* CONFIG_EVENTFD */
  10697. #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
  10698. case TARGET_NR_fallocate:
  10699. #if TARGET_ABI_BITS == 32
  10700. ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
  10701. target_offset64(arg5, arg6)));
  10702. #else
  10703. ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
  10704. #endif
  10705. return ret;
  10706. #endif
  10707. #if defined(CONFIG_SYNC_FILE_RANGE)
  10708. #if defined(TARGET_NR_sync_file_range)
  10709. case TARGET_NR_sync_file_range:
  10710. #if TARGET_ABI_BITS == 32
  10711. #if defined(TARGET_MIPS)
  10712. ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
  10713. target_offset64(arg5, arg6), arg7));
  10714. #else
  10715. ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
  10716. target_offset64(arg4, arg5), arg6));
  10717. #endif /* !TARGET_MIPS */
  10718. #else
  10719. ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
  10720. #endif
  10721. return ret;
  10722. #endif
  10723. #if defined(TARGET_NR_sync_file_range2)
  10724. case TARGET_NR_sync_file_range2:
  10725. /* This is like sync_file_range but the arguments are reordered */
  10726. #if TARGET_ABI_BITS == 32
  10727. ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
  10728. target_offset64(arg5, arg6), arg2));
  10729. #else
  10730. ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
  10731. #endif
  10732. return ret;
  10733. #endif
  10734. #endif
  10735. #if defined(TARGET_NR_signalfd4)
  10736. case TARGET_NR_signalfd4:
  10737. return do_signalfd4(arg1, arg2, arg4);
  10738. #endif
  10739. #if defined(TARGET_NR_signalfd)
  10740. case TARGET_NR_signalfd:
  10741. return do_signalfd4(arg1, arg2, 0);
  10742. #endif
  10743. #if defined(CONFIG_EPOLL)
  10744. #if defined(TARGET_NR_epoll_create)
  10745. case TARGET_NR_epoll_create:
  10746. return get_errno(epoll_create(arg1));
  10747. #endif
  10748. #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
  10749. case TARGET_NR_epoll_create1:
  10750. return get_errno(epoll_create1(arg1));
  10751. #endif
  10752. #if defined(TARGET_NR_epoll_ctl)
  10753. case TARGET_NR_epoll_ctl:
  10754. {
  10755. struct epoll_event ep;
  10756. struct epoll_event *epp = 0;
  10757. if (arg4) {
  10758. struct target_epoll_event *target_ep;
  10759. if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
  10760. return -TARGET_EFAULT;
  10761. }
  10762. ep.events = tswap32(target_ep->events);
  10763. /* The epoll_data_t union is just opaque data to the kernel,
  10764. * so we transfer all 64 bits across and need not worry what
  10765. * actual data type it is.
  10766. */
  10767. ep.data.u64 = tswap64(target_ep->data.u64);
  10768. unlock_user_struct(target_ep, arg4, 0);
  10769. epp = &ep;
  10770. }
  10771. return get_errno(epoll_ctl(arg1, arg2, arg3, epp));
  10772. }
  10773. #endif
  10774. #if defined(TARGET_NR_epoll_wait) || defined(TARGET_NR_epoll_pwait)
  10775. #if defined(TARGET_NR_epoll_wait)
  10776. case TARGET_NR_epoll_wait:
  10777. #endif
  10778. #if defined(TARGET_NR_epoll_pwait)
  10779. case TARGET_NR_epoll_pwait:
  10780. #endif
  10781. {
  10782. struct target_epoll_event *target_ep;
  10783. struct epoll_event *ep;
  10784. int epfd = arg1;
  10785. int maxevents = arg3;
  10786. int timeout = arg4;
  10787. if (maxevents <= 0 || maxevents > TARGET_EP_MAX_EVENTS) {
  10788. return -TARGET_EINVAL;
  10789. }
  10790. target_ep = lock_user(VERIFY_WRITE, arg2,
  10791. maxevents * sizeof(struct target_epoll_event), 1);
  10792. if (!target_ep) {
  10793. return -TARGET_EFAULT;
  10794. }
  10795. ep = g_try_new(struct epoll_event, maxevents);
  10796. if (!ep) {
  10797. unlock_user(target_ep, arg2, 0);
  10798. return -TARGET_ENOMEM;
  10799. }
  10800. switch (num) {
  10801. #if defined(TARGET_NR_epoll_pwait)
  10802. case TARGET_NR_epoll_pwait:
  10803. {
  10804. target_sigset_t *target_set;
  10805. sigset_t _set, *set = &_set;
  10806. if (arg5) {
  10807. if (arg6 != sizeof(target_sigset_t)) {
  10808. ret = -TARGET_EINVAL;
  10809. break;
  10810. }
  10811. target_set = lock_user(VERIFY_READ, arg5,
  10812. sizeof(target_sigset_t), 1);
  10813. if (!target_set) {
  10814. ret = -TARGET_EFAULT;
  10815. break;
  10816. }
  10817. target_to_host_sigset(set, target_set);
  10818. unlock_user(target_set, arg5, 0);
  10819. } else {
  10820. set = NULL;
  10821. }
  10822. ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
  10823. set, SIGSET_T_SIZE));
  10824. break;
  10825. }
  10826. #endif
  10827. #if defined(TARGET_NR_epoll_wait)
  10828. case TARGET_NR_epoll_wait:
  10829. ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
  10830. NULL, 0));
  10831. break;
  10832. #endif
  10833. default:
  10834. ret = -TARGET_ENOSYS;
  10835. }
  10836. if (!is_error(ret)) {
  10837. int i;
  10838. for (i = 0; i < ret; i++) {
  10839. target_ep[i].events = tswap32(ep[i].events);
  10840. target_ep[i].data.u64 = tswap64(ep[i].data.u64);
  10841. }
  10842. unlock_user(target_ep, arg2,
  10843. ret * sizeof(struct target_epoll_event));
  10844. } else {
  10845. unlock_user(target_ep, arg2, 0);
  10846. }
  10847. g_free(ep);
  10848. return ret;
  10849. }
  10850. #endif
  10851. #endif
  10852. #ifdef TARGET_NR_prlimit64
  10853. case TARGET_NR_prlimit64:
  10854. {
  10855. /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
  10856. struct target_rlimit64 *target_rnew, *target_rold;
  10857. struct host_rlimit64 rnew, rold, *rnewp = 0;
  10858. int resource = target_to_host_resource(arg2);
  10859. if (arg3) {
  10860. if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
  10861. return -TARGET_EFAULT;
  10862. }
  10863. rnew.rlim_cur = tswap64(target_rnew->rlim_cur);
  10864. rnew.rlim_max = tswap64(target_rnew->rlim_max);
  10865. unlock_user_struct(target_rnew, arg3, 0);
  10866. rnewp = &rnew;
  10867. }
  10868. ret = get_errno(sys_prlimit64(arg1, resource, rnewp, arg4 ? &rold : 0));
  10869. if (!is_error(ret) && arg4) {
  10870. if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
  10871. return -TARGET_EFAULT;
  10872. }
  10873. target_rold->rlim_cur = tswap64(rold.rlim_cur);
  10874. target_rold->rlim_max = tswap64(rold.rlim_max);
  10875. unlock_user_struct(target_rold, arg4, 1);
  10876. }
  10877. return ret;
  10878. }
  10879. #endif
  10880. #ifdef TARGET_NR_gethostname
  10881. case TARGET_NR_gethostname:
  10882. {
  10883. char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
  10884. if (name) {
  10885. ret = get_errno(gethostname(name, arg2));
  10886. unlock_user(name, arg1, arg2);
  10887. } else {
  10888. ret = -TARGET_EFAULT;
  10889. }
  10890. return ret;
  10891. }
  10892. #endif
  10893. #ifdef TARGET_NR_atomic_cmpxchg_32
  10894. case TARGET_NR_atomic_cmpxchg_32:
  10895. {
  10896. /* should use start_exclusive from main.c */
  10897. abi_ulong mem_value;
  10898. if (get_user_u32(mem_value, arg6)) {
  10899. target_siginfo_t info;
  10900. info.si_signo = SIGSEGV;
  10901. info.si_errno = 0;
  10902. info.si_code = TARGET_SEGV_MAPERR;
  10903. info._sifields._sigfault._addr = arg6;
  10904. queue_signal((CPUArchState *)cpu_env, info.si_signo,
  10905. QEMU_SI_FAULT, &info);
  10906. ret = 0xdeadbeef;
  10907. }
  10908. if (mem_value == arg2)
  10909. put_user_u32(arg1, arg6);
  10910. return mem_value;
  10911. }
  10912. #endif
  10913. #ifdef TARGET_NR_atomic_barrier
  10914. case TARGET_NR_atomic_barrier:
  10915. /* Like the kernel implementation and the
  10916. qemu arm barrier, no-op this? */
  10917. return 0;
  10918. #endif
  10919. #ifdef TARGET_NR_timer_create
  10920. case TARGET_NR_timer_create:
  10921. {
  10922. /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
  10923. struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL;
  10924. int clkid = arg1;
  10925. int timer_index = next_free_host_timer();
  10926. if (timer_index < 0) {
  10927. ret = -TARGET_EAGAIN;
  10928. } else {
  10929. timer_t *phtimer = g_posix_timers + timer_index;
  10930. if (arg2) {
  10931. phost_sevp = &host_sevp;
  10932. ret = target_to_host_sigevent(phost_sevp, arg2);
  10933. if (ret != 0) {
  10934. return ret;
  10935. }
  10936. }
  10937. ret = get_errno(timer_create(clkid, phost_sevp, phtimer));
  10938. if (ret) {
  10939. phtimer = NULL;
  10940. } else {
  10941. if (put_user(TIMER_MAGIC | timer_index, arg3, target_timer_t)) {
  10942. return -TARGET_EFAULT;
  10943. }
  10944. }
  10945. }
  10946. return ret;
  10947. }
  10948. #endif
  10949. #ifdef TARGET_NR_timer_settime
  10950. case TARGET_NR_timer_settime:
  10951. {
  10952. /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
  10953. * struct itimerspec * old_value */
  10954. target_timer_t timerid = get_timer_id(arg1);
  10955. if (timerid < 0) {
  10956. ret = timerid;
  10957. } else if (arg3 == 0) {
  10958. ret = -TARGET_EINVAL;
  10959. } else {
  10960. timer_t htimer = g_posix_timers[timerid];
  10961. struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
  10962. if (target_to_host_itimerspec(&hspec_new, arg3)) {
  10963. return -TARGET_EFAULT;
  10964. }
  10965. ret = get_errno(
  10966. timer_settime(htimer, arg2, &hspec_new, &hspec_old));
  10967. if (arg4 && host_to_target_itimerspec(arg4, &hspec_old)) {
  10968. return -TARGET_EFAULT;
  10969. }
  10970. }
  10971. return ret;
  10972. }
  10973. #endif
  10974. #ifdef TARGET_NR_timer_gettime
  10975. case TARGET_NR_timer_gettime:
  10976. {
  10977. /* args: timer_t timerid, struct itimerspec *curr_value */
  10978. target_timer_t timerid = get_timer_id(arg1);
  10979. if (timerid < 0) {
  10980. ret = timerid;
  10981. } else if (!arg2) {
  10982. ret = -TARGET_EFAULT;
  10983. } else {
  10984. timer_t htimer = g_posix_timers[timerid];
  10985. struct itimerspec hspec;
  10986. ret = get_errno(timer_gettime(htimer, &hspec));
  10987. if (host_to_target_itimerspec(arg2, &hspec)) {
  10988. ret = -TARGET_EFAULT;
  10989. }
  10990. }
  10991. return ret;
  10992. }
  10993. #endif
  10994. #ifdef TARGET_NR_timer_getoverrun
  10995. case TARGET_NR_timer_getoverrun:
  10996. {
  10997. /* args: timer_t timerid */
  10998. target_timer_t timerid = get_timer_id(arg1);
  10999. if (timerid < 0) {
  11000. ret = timerid;
  11001. } else {
  11002. timer_t htimer = g_posix_timers[timerid];
  11003. ret = get_errno(timer_getoverrun(htimer));
  11004. }
  11005. return ret;
  11006. }
  11007. #endif
  11008. #ifdef TARGET_NR_timer_delete
  11009. case TARGET_NR_timer_delete:
  11010. {
  11011. /* args: timer_t timerid */
  11012. target_timer_t timerid = get_timer_id(arg1);
  11013. if (timerid < 0) {
  11014. ret = timerid;
  11015. } else {
  11016. timer_t htimer = g_posix_timers[timerid];
  11017. ret = get_errno(timer_delete(htimer));
  11018. g_posix_timers[timerid] = 0;
  11019. }
  11020. return ret;
  11021. }
  11022. #endif
  11023. #if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
  11024. case TARGET_NR_timerfd_create:
  11025. return get_errno(timerfd_create(arg1,
  11026. target_to_host_bitmask(arg2, fcntl_flags_tbl)));
  11027. #endif
  11028. #if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
  11029. case TARGET_NR_timerfd_gettime:
  11030. {
  11031. struct itimerspec its_curr;
  11032. ret = get_errno(timerfd_gettime(arg1, &its_curr));
  11033. if (arg2 && host_to_target_itimerspec(arg2, &its_curr)) {
  11034. return -TARGET_EFAULT;
  11035. }
  11036. }
  11037. return ret;
  11038. #endif
  11039. #if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
  11040. case TARGET_NR_timerfd_settime:
  11041. {
  11042. struct itimerspec its_new, its_old, *p_new;
  11043. if (arg3) {
  11044. if (target_to_host_itimerspec(&its_new, arg3)) {
  11045. return -TARGET_EFAULT;
  11046. }
  11047. p_new = &its_new;
  11048. } else {
  11049. p_new = NULL;
  11050. }
  11051. ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
  11052. if (arg4 && host_to_target_itimerspec(arg4, &its_old)) {
  11053. return -TARGET_EFAULT;
  11054. }
  11055. }
  11056. return ret;
  11057. #endif
  11058. #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
  11059. case TARGET_NR_ioprio_get:
  11060. return get_errno(ioprio_get(arg1, arg2));
  11061. #endif
  11062. #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
  11063. case TARGET_NR_ioprio_set:
  11064. return get_errno(ioprio_set(arg1, arg2, arg3));
  11065. #endif
  11066. #if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
  11067. case TARGET_NR_setns:
  11068. return get_errno(setns(arg1, arg2));
  11069. #endif
  11070. #if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
  11071. case TARGET_NR_unshare:
  11072. return get_errno(unshare(arg1));
  11073. #endif
  11074. #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
  11075. case TARGET_NR_kcmp:
  11076. return get_errno(kcmp(arg1, arg2, arg3, arg4, arg5));
  11077. #endif
  11078. #ifdef TARGET_NR_swapcontext
  11079. case TARGET_NR_swapcontext:
  11080. /* PowerPC specific. */
  11081. return do_swapcontext(cpu_env, arg1, arg2, arg3);
  11082. #endif
  11083. #ifdef TARGET_NR_memfd_create
  11084. case TARGET_NR_memfd_create:
  11085. p = lock_user_string(arg1);
  11086. if (!p) {
  11087. return -TARGET_EFAULT;
  11088. }
  11089. ret = get_errno(memfd_create(p, arg2));
  11090. fd_trans_unregister(ret);
  11091. unlock_user(p, arg1, 0);
  11092. return ret;
  11093. #endif
  11094. default:
  11095. qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num);
  11096. return -TARGET_ENOSYS;
  11097. }
  11098. return ret;
  11099. }
  11100. abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
  11101. abi_long arg2, abi_long arg3, abi_long arg4,
  11102. abi_long arg5, abi_long arg6, abi_long arg7,
  11103. abi_long arg8)
  11104. {
  11105. CPUState *cpu = env_cpu(cpu_env);
  11106. abi_long ret;
  11107. #ifdef DEBUG_ERESTARTSYS
  11108. /* Debug-only code for exercising the syscall-restart code paths
  11109. * in the per-architecture cpu main loops: restart every syscall
  11110. * the guest makes once before letting it through.
  11111. */
  11112. {
  11113. static bool flag;
  11114. flag = !flag;
  11115. if (flag) {
  11116. return -TARGET_ERESTARTSYS;
  11117. }
  11118. }
  11119. #endif
  11120. record_syscall_start(cpu, num, arg1,
  11121. arg2, arg3, arg4, arg5, arg6, arg7, arg8);
  11122. if (unlikely(do_strace)) {
  11123. print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
  11124. ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4,
  11125. arg5, arg6, arg7, arg8);
  11126. print_syscall_ret(num, ret);
  11127. } else {
  11128. ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4,
  11129. arg5, arg6, arg7, arg8);
  11130. }
  11131. record_syscall_return(cpu, num, ret);
  11132. return ret;
  11133. }