2
0

syscall.c 422 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339
  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/in.h>
  55. #include <netinet/ip.h>
  56. #include <netinet/tcp.h>
  57. #include <netinet/udp.h>
  58. #include <linux/wireless.h>
  59. #include <linux/icmp.h>
  60. #include <linux/icmpv6.h>
  61. #include <linux/if_tun.h>
  62. #include <linux/in6.h>
  63. #include <linux/errqueue.h>
  64. #include <linux/random.h>
  65. #ifdef CONFIG_TIMERFD
  66. #include <sys/timerfd.h>
  67. #endif
  68. #ifdef CONFIG_EVENTFD
  69. #include <sys/eventfd.h>
  70. #endif
  71. #ifdef CONFIG_EPOLL
  72. #include <sys/epoll.h>
  73. #endif
  74. #ifdef CONFIG_ATTR
  75. #include "qemu/xattr.h"
  76. #endif
  77. #ifdef CONFIG_SENDFILE
  78. #include <sys/sendfile.h>
  79. #endif
  80. #ifdef HAVE_SYS_KCOV_H
  81. #include <sys/kcov.h>
  82. #endif
  83. #define termios host_termios
  84. #define winsize host_winsize
  85. #define termio host_termio
  86. #define sgttyb host_sgttyb /* same as target */
  87. #define tchars host_tchars /* same as target */
  88. #define ltchars host_ltchars /* same as target */
  89. #include <linux/termios.h>
  90. #include <linux/unistd.h>
  91. #include <linux/cdrom.h>
  92. #include <linux/hdreg.h>
  93. #include <linux/soundcard.h>
  94. #include <linux/kd.h>
  95. #include <linux/mtio.h>
  96. #ifdef HAVE_SYS_MOUNT_FSCONFIG
  97. /*
  98. * glibc >= 2.36 linux/mount.h conflicts with sys/mount.h,
  99. * which in turn prevents use of linux/fs.h. So we have to
  100. * define the constants ourselves for now.
  101. */
  102. #define FS_IOC_GETFLAGS _IOR('f', 1, long)
  103. #define FS_IOC_SETFLAGS _IOW('f', 2, long)
  104. #define FS_IOC_GETVERSION _IOR('v', 1, long)
  105. #define FS_IOC_SETVERSION _IOW('v', 2, long)
  106. #define FS_IOC_FIEMAP _IOWR('f', 11, struct fiemap)
  107. #define FS_IOC32_GETFLAGS _IOR('f', 1, int)
  108. #define FS_IOC32_SETFLAGS _IOW('f', 2, int)
  109. #define FS_IOC32_GETVERSION _IOR('v', 1, int)
  110. #define FS_IOC32_SETVERSION _IOW('v', 2, int)
  111. #define BLKGETSIZE64 _IOR(0x12,114,size_t)
  112. #define BLKDISCARD _IO(0x12,119)
  113. #define BLKIOMIN _IO(0x12,120)
  114. #define BLKIOOPT _IO(0x12,121)
  115. #define BLKALIGNOFF _IO(0x12,122)
  116. #define BLKPBSZGET _IO(0x12,123)
  117. #define BLKDISCARDZEROES _IO(0x12,124)
  118. #define BLKSECDISCARD _IO(0x12,125)
  119. #define BLKROTATIONAL _IO(0x12,126)
  120. #define BLKZEROOUT _IO(0x12,127)
  121. #define FIBMAP _IO(0x00,1)
  122. #define FIGETBSZ _IO(0x00,2)
  123. struct file_clone_range {
  124. __s64 src_fd;
  125. __u64 src_offset;
  126. __u64 src_length;
  127. __u64 dest_offset;
  128. };
  129. #define FICLONE _IOW(0x94, 9, int)
  130. #define FICLONERANGE _IOW(0x94, 13, struct file_clone_range)
  131. #else
  132. #include <linux/fs.h>
  133. #endif
  134. #include <linux/fd.h>
  135. #if defined(CONFIG_FIEMAP)
  136. #include <linux/fiemap.h>
  137. #endif
  138. #include <linux/fb.h>
  139. #if defined(CONFIG_USBFS)
  140. #include <linux/usbdevice_fs.h>
  141. #include <linux/usb/ch9.h>
  142. #endif
  143. #include <linux/vt.h>
  144. #include <linux/dm-ioctl.h>
  145. #include <linux/reboot.h>
  146. #include <linux/route.h>
  147. #include <linux/filter.h>
  148. #include <linux/blkpg.h>
  149. #include <netpacket/packet.h>
  150. #include <linux/netlink.h>
  151. #include <linux/if_alg.h>
  152. #include <linux/rtc.h>
  153. #include <sound/asound.h>
  154. #ifdef HAVE_BTRFS_H
  155. #include <linux/btrfs.h>
  156. #endif
  157. #ifdef HAVE_DRM_H
  158. #include <libdrm/drm.h>
  159. #include <libdrm/i915_drm.h>
  160. #endif
  161. #include "linux_loop.h"
  162. #include "uname.h"
  163. #include "qemu.h"
  164. #include "user-internals.h"
  165. #include "strace.h"
  166. #include "signal-common.h"
  167. #include "loader.h"
  168. #include "user-mmap.h"
  169. #include "user/safe-syscall.h"
  170. #include "qemu/guest-random.h"
  171. #include "qemu/selfmap.h"
  172. #include "user/syscall-trace.h"
  173. #include "special-errno.h"
  174. #include "qapi/error.h"
  175. #include "fd-trans.h"
  176. #include "tcg/tcg.h"
  177. #include "cpu_loop-common.h"
  178. #ifndef CLONE_IO
  179. #define CLONE_IO 0x80000000 /* Clone io context */
  180. #endif
  181. /* We can't directly call the host clone syscall, because this will
  182. * badly confuse libc (breaking mutexes, for example). So we must
  183. * divide clone flags into:
  184. * * flag combinations that look like pthread_create()
  185. * * flag combinations that look like fork()
  186. * * flags we can implement within QEMU itself
  187. * * flags we can't support and will return an error for
  188. */
  189. /* For thread creation, all these flags must be present; for
  190. * fork, none must be present.
  191. */
  192. #define CLONE_THREAD_FLAGS \
  193. (CLONE_VM | CLONE_FS | CLONE_FILES | \
  194. CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM)
  195. /* These flags are ignored:
  196. * CLONE_DETACHED is now ignored by the kernel;
  197. * CLONE_IO is just an optimisation hint to the I/O scheduler
  198. */
  199. #define CLONE_IGNORED_FLAGS \
  200. (CLONE_DETACHED | CLONE_IO)
  201. /* Flags for fork which we can implement within QEMU itself */
  202. #define CLONE_OPTIONAL_FORK_FLAGS \
  203. (CLONE_SETTLS | CLONE_PARENT_SETTID | \
  204. CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID)
  205. /* Flags for thread creation which we can implement within QEMU itself */
  206. #define CLONE_OPTIONAL_THREAD_FLAGS \
  207. (CLONE_SETTLS | CLONE_PARENT_SETTID | \
  208. CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | CLONE_PARENT)
  209. #define CLONE_INVALID_FORK_FLAGS \
  210. (~(CSIGNAL | CLONE_OPTIONAL_FORK_FLAGS | CLONE_IGNORED_FLAGS))
  211. #define CLONE_INVALID_THREAD_FLAGS \
  212. (~(CSIGNAL | CLONE_THREAD_FLAGS | CLONE_OPTIONAL_THREAD_FLAGS | \
  213. CLONE_IGNORED_FLAGS))
  214. /* CLONE_VFORK is special cased early in do_fork(). The other flag bits
  215. * have almost all been allocated. We cannot support any of
  216. * CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC,
  217. * CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, CLONE_PTRACE, CLONE_UNTRACED.
  218. * The checks against the invalid thread masks above will catch these.
  219. * (The one remaining unallocated bit is 0x1000 which used to be CLONE_PID.)
  220. */
  221. /* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
  222. * once. This exercises the codepaths for restart.
  223. */
  224. //#define DEBUG_ERESTARTSYS
  225. //#include <linux/msdos_fs.h>
  226. #define VFAT_IOCTL_READDIR_BOTH \
  227. _IOC(_IOC_READ, 'r', 1, (sizeof(struct linux_dirent) + 256) * 2)
  228. #define VFAT_IOCTL_READDIR_SHORT \
  229. _IOC(_IOC_READ, 'r', 2, (sizeof(struct linux_dirent) + 256) * 2)
  230. #undef _syscall0
  231. #undef _syscall1
  232. #undef _syscall2
  233. #undef _syscall3
  234. #undef _syscall4
  235. #undef _syscall5
  236. #undef _syscall6
  237. #define _syscall0(type,name) \
  238. static type name (void) \
  239. { \
  240. return syscall(__NR_##name); \
  241. }
  242. #define _syscall1(type,name,type1,arg1) \
  243. static type name (type1 arg1) \
  244. { \
  245. return syscall(__NR_##name, arg1); \
  246. }
  247. #define _syscall2(type,name,type1,arg1,type2,arg2) \
  248. static type name (type1 arg1,type2 arg2) \
  249. { \
  250. return syscall(__NR_##name, arg1, arg2); \
  251. }
  252. #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
  253. static type name (type1 arg1,type2 arg2,type3 arg3) \
  254. { \
  255. return syscall(__NR_##name, arg1, arg2, arg3); \
  256. }
  257. #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
  258. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
  259. { \
  260. return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
  261. }
  262. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  263. type5,arg5) \
  264. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
  265. { \
  266. return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
  267. }
  268. #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  269. type5,arg5,type6,arg6) \
  270. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
  271. type6 arg6) \
  272. { \
  273. return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
  274. }
  275. #define __NR_sys_uname __NR_uname
  276. #define __NR_sys_getcwd1 __NR_getcwd
  277. #define __NR_sys_getdents __NR_getdents
  278. #define __NR_sys_getdents64 __NR_getdents64
  279. #define __NR_sys_getpriority __NR_getpriority
  280. #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
  281. #define __NR_sys_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo
  282. #define __NR_sys_syslog __NR_syslog
  283. #if defined(__NR_futex)
  284. # define __NR_sys_futex __NR_futex
  285. #endif
  286. #if defined(__NR_futex_time64)
  287. # define __NR_sys_futex_time64 __NR_futex_time64
  288. #endif
  289. #define __NR_sys_statx __NR_statx
  290. #if defined(__alpha__) || defined(__x86_64__) || defined(__s390x__)
  291. #define __NR__llseek __NR_lseek
  292. #endif
  293. /* Newer kernel ports have llseek() instead of _llseek() */
  294. #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
  295. #define TARGET_NR__llseek TARGET_NR_llseek
  296. #endif
  297. /* some platforms need to mask more bits than just TARGET_O_NONBLOCK */
  298. #ifndef TARGET_O_NONBLOCK_MASK
  299. #define TARGET_O_NONBLOCK_MASK TARGET_O_NONBLOCK
  300. #endif
  301. #define __NR_sys_gettid __NR_gettid
  302. _syscall0(int, sys_gettid)
  303. /* For the 64-bit guest on 32-bit host case we must emulate
  304. * getdents using getdents64, because otherwise the host
  305. * might hand us back more dirent records than we can fit
  306. * into the guest buffer after structure format conversion.
  307. * Otherwise we emulate getdents with getdents if the host has it.
  308. */
  309. #if defined(__NR_getdents) && HOST_LONG_BITS >= TARGET_ABI_BITS
  310. #define EMULATE_GETDENTS_WITH_GETDENTS
  311. #endif
  312. #if defined(TARGET_NR_getdents) && defined(EMULATE_GETDENTS_WITH_GETDENTS)
  313. _syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
  314. #endif
  315. #if (defined(TARGET_NR_getdents) && \
  316. !defined(EMULATE_GETDENTS_WITH_GETDENTS)) || \
  317. (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
  318. _syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
  319. #endif
  320. #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
  321. _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
  322. loff_t *, res, uint, wh);
  323. #endif
  324. _syscall3(int, sys_rt_sigqueueinfo, pid_t, pid, int, sig, siginfo_t *, uinfo)
  325. _syscall4(int, sys_rt_tgsigqueueinfo, pid_t, pid, pid_t, tid, int, sig,
  326. siginfo_t *, uinfo)
  327. _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
  328. #ifdef __NR_exit_group
  329. _syscall1(int,exit_group,int,error_code)
  330. #endif
  331. #if defined(__NR_close_range) && defined(TARGET_NR_close_range)
  332. #define __NR_sys_close_range __NR_close_range
  333. _syscall3(int,sys_close_range,int,first,int,last,int,flags)
  334. #ifndef CLOSE_RANGE_CLOEXEC
  335. #define CLOSE_RANGE_CLOEXEC (1U << 2)
  336. #endif
  337. #endif
  338. #if defined(__NR_futex)
  339. _syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
  340. const struct timespec *,timeout,int *,uaddr2,int,val3)
  341. #endif
  342. #if defined(__NR_futex_time64)
  343. _syscall6(int,sys_futex_time64,int *,uaddr,int,op,int,val,
  344. const struct timespec *,timeout,int *,uaddr2,int,val3)
  345. #endif
  346. #if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
  347. _syscall2(int, pidfd_open, pid_t, pid, unsigned int, flags);
  348. #endif
  349. #if defined(__NR_pidfd_send_signal) && defined(TARGET_NR_pidfd_send_signal)
  350. _syscall4(int, pidfd_send_signal, int, pidfd, int, sig, siginfo_t *, info,
  351. unsigned int, flags);
  352. #endif
  353. #if defined(__NR_pidfd_getfd) && defined(TARGET_NR_pidfd_getfd)
  354. _syscall3(int, pidfd_getfd, int, pidfd, int, targetfd, unsigned int, flags);
  355. #endif
  356. #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
  357. _syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
  358. unsigned long *, user_mask_ptr);
  359. #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
  360. _syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
  361. unsigned long *, user_mask_ptr);
  362. /* sched_attr is not defined in glibc */
  363. struct sched_attr {
  364. uint32_t size;
  365. uint32_t sched_policy;
  366. uint64_t sched_flags;
  367. int32_t sched_nice;
  368. uint32_t sched_priority;
  369. uint64_t sched_runtime;
  370. uint64_t sched_deadline;
  371. uint64_t sched_period;
  372. uint32_t sched_util_min;
  373. uint32_t sched_util_max;
  374. };
  375. #define __NR_sys_sched_getattr __NR_sched_getattr
  376. _syscall4(int, sys_sched_getattr, pid_t, pid, struct sched_attr *, attr,
  377. unsigned int, size, unsigned int, flags);
  378. #define __NR_sys_sched_setattr __NR_sched_setattr
  379. _syscall3(int, sys_sched_setattr, pid_t, pid, struct sched_attr *, attr,
  380. unsigned int, flags);
  381. #define __NR_sys_sched_getscheduler __NR_sched_getscheduler
  382. _syscall1(int, sys_sched_getscheduler, pid_t, pid);
  383. #define __NR_sys_sched_setscheduler __NR_sched_setscheduler
  384. _syscall3(int, sys_sched_setscheduler, pid_t, pid, int, policy,
  385. const struct sched_param *, param);
  386. #define __NR_sys_sched_getparam __NR_sched_getparam
  387. _syscall2(int, sys_sched_getparam, pid_t, pid,
  388. struct sched_param *, param);
  389. #define __NR_sys_sched_setparam __NR_sched_setparam
  390. _syscall2(int, sys_sched_setparam, pid_t, pid,
  391. const struct sched_param *, param);
  392. #define __NR_sys_getcpu __NR_getcpu
  393. _syscall3(int, sys_getcpu, unsigned *, cpu, unsigned *, node, void *, tcache);
  394. _syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
  395. void *, arg);
  396. _syscall2(int, capget, struct __user_cap_header_struct *, header,
  397. struct __user_cap_data_struct *, data);
  398. _syscall2(int, capset, struct __user_cap_header_struct *, header,
  399. struct __user_cap_data_struct *, data);
  400. #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
  401. _syscall2(int, ioprio_get, int, which, int, who)
  402. #endif
  403. #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
  404. _syscall3(int, ioprio_set, int, which, int, who, int, ioprio)
  405. #endif
  406. #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
  407. _syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags)
  408. #endif
  409. #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
  410. _syscall5(int, kcmp, pid_t, pid1, pid_t, pid2, int, type,
  411. unsigned long, idx1, unsigned long, idx2)
  412. #endif
  413. /*
  414. * It is assumed that struct statx is architecture independent.
  415. */
  416. #if defined(TARGET_NR_statx) && defined(__NR_statx)
  417. _syscall5(int, sys_statx, int, dirfd, const char *, pathname, int, flags,
  418. unsigned int, mask, struct target_statx *, statxbuf)
  419. #endif
  420. #if defined(TARGET_NR_membarrier) && defined(__NR_membarrier)
  421. _syscall2(int, membarrier, int, cmd, int, flags)
  422. #endif
  423. static const bitmask_transtbl fcntl_flags_tbl[] = {
  424. { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
  425. { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
  426. { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
  427. { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
  428. { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
  429. { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
  430. { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
  431. { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
  432. { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
  433. { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
  434. { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
  435. { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
  436. { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
  437. #if defined(O_DIRECT)
  438. { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
  439. #endif
  440. #if defined(O_NOATIME)
  441. { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
  442. #endif
  443. #if defined(O_CLOEXEC)
  444. { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
  445. #endif
  446. #if defined(O_PATH)
  447. { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
  448. #endif
  449. #if defined(O_TMPFILE)
  450. { TARGET_O_TMPFILE, TARGET_O_TMPFILE, O_TMPFILE, O_TMPFILE },
  451. #endif
  452. /* Don't terminate the list prematurely on 64-bit host+guest. */
  453. #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
  454. { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
  455. #endif
  456. { 0, 0, 0, 0 }
  457. };
  458. _syscall2(int, sys_getcwd1, char *, buf, size_t, size)
  459. #if defined(TARGET_NR_utimensat) || defined(TARGET_NR_utimensat_time64)
  460. #if defined(__NR_utimensat)
  461. #define __NR_sys_utimensat __NR_utimensat
  462. _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
  463. const struct timespec *,tsp,int,flags)
  464. #else
  465. static int sys_utimensat(int dirfd, const char *pathname,
  466. const struct timespec times[2], int flags)
  467. {
  468. errno = ENOSYS;
  469. return -1;
  470. }
  471. #endif
  472. #endif /* TARGET_NR_utimensat */
  473. #ifdef TARGET_NR_renameat2
  474. #if defined(__NR_renameat2)
  475. #define __NR_sys_renameat2 __NR_renameat2
  476. _syscall5(int, sys_renameat2, int, oldfd, const char *, old, int, newfd,
  477. const char *, new, unsigned int, flags)
  478. #else
  479. static int sys_renameat2(int oldfd, const char *old,
  480. int newfd, const char *new, int flags)
  481. {
  482. if (flags == 0) {
  483. return renameat(oldfd, old, newfd, new);
  484. }
  485. errno = ENOSYS;
  486. return -1;
  487. }
  488. #endif
  489. #endif /* TARGET_NR_renameat2 */
  490. #ifdef CONFIG_INOTIFY
  491. #include <sys/inotify.h>
  492. #else
  493. /* Userspace can usually survive runtime without inotify */
  494. #undef TARGET_NR_inotify_init
  495. #undef TARGET_NR_inotify_init1
  496. #undef TARGET_NR_inotify_add_watch
  497. #undef TARGET_NR_inotify_rm_watch
  498. #endif /* CONFIG_INOTIFY */
  499. #if defined(TARGET_NR_prlimit64)
  500. #ifndef __NR_prlimit64
  501. # define __NR_prlimit64 -1
  502. #endif
  503. #define __NR_sys_prlimit64 __NR_prlimit64
  504. /* The glibc rlimit structure may not be that used by the underlying syscall */
  505. struct host_rlimit64 {
  506. uint64_t rlim_cur;
  507. uint64_t rlim_max;
  508. };
  509. _syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
  510. const struct host_rlimit64 *, new_limit,
  511. struct host_rlimit64 *, old_limit)
  512. #endif
  513. #if defined(TARGET_NR_timer_create)
  514. /* Maximum of 32 active POSIX timers allowed at any one time. */
  515. #define GUEST_TIMER_MAX 32
  516. static timer_t g_posix_timers[GUEST_TIMER_MAX];
  517. static int g_posix_timer_allocated[GUEST_TIMER_MAX];
  518. static inline int next_free_host_timer(void)
  519. {
  520. int k;
  521. for (k = 0; k < ARRAY_SIZE(g_posix_timer_allocated); k++) {
  522. if (qatomic_xchg(g_posix_timer_allocated + k, 1) == 0) {
  523. return k;
  524. }
  525. }
  526. return -1;
  527. }
  528. static inline void free_host_timer_slot(int id)
  529. {
  530. qatomic_store_release(g_posix_timer_allocated + id, 0);
  531. }
  532. #endif
  533. static inline int host_to_target_errno(int host_errno)
  534. {
  535. switch (host_errno) {
  536. #define E(X) case X: return TARGET_##X;
  537. #include "errnos.c.inc"
  538. #undef E
  539. default:
  540. return host_errno;
  541. }
  542. }
  543. static inline int target_to_host_errno(int target_errno)
  544. {
  545. switch (target_errno) {
  546. #define E(X) case TARGET_##X: return X;
  547. #include "errnos.c.inc"
  548. #undef E
  549. default:
  550. return target_errno;
  551. }
  552. }
  553. abi_long get_errno(abi_long ret)
  554. {
  555. if (ret == -1)
  556. return -host_to_target_errno(errno);
  557. else
  558. return ret;
  559. }
  560. const char *target_strerror(int err)
  561. {
  562. if (err == QEMU_ERESTARTSYS) {
  563. return "To be restarted";
  564. }
  565. if (err == QEMU_ESIGRETURN) {
  566. return "Successful exit from sigreturn";
  567. }
  568. return strerror(target_to_host_errno(err));
  569. }
  570. static int check_zeroed_user(abi_long addr, size_t ksize, size_t usize)
  571. {
  572. int i;
  573. uint8_t b;
  574. if (usize <= ksize) {
  575. return 1;
  576. }
  577. for (i = ksize; i < usize; i++) {
  578. if (get_user_u8(b, addr + i)) {
  579. return -TARGET_EFAULT;
  580. }
  581. if (b != 0) {
  582. return 0;
  583. }
  584. }
  585. return 1;
  586. }
  587. #define safe_syscall0(type, name) \
  588. static type safe_##name(void) \
  589. { \
  590. return safe_syscall(__NR_##name); \
  591. }
  592. #define safe_syscall1(type, name, type1, arg1) \
  593. static type safe_##name(type1 arg1) \
  594. { \
  595. return safe_syscall(__NR_##name, arg1); \
  596. }
  597. #define safe_syscall2(type, name, type1, arg1, type2, arg2) \
  598. static type safe_##name(type1 arg1, type2 arg2) \
  599. { \
  600. return safe_syscall(__NR_##name, arg1, arg2); \
  601. }
  602. #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
  603. static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
  604. { \
  605. return safe_syscall(__NR_##name, arg1, arg2, arg3); \
  606. }
  607. #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
  608. type4, arg4) \
  609. static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
  610. { \
  611. return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
  612. }
  613. #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
  614. type4, arg4, type5, arg5) \
  615. static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  616. type5 arg5) \
  617. { \
  618. return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
  619. }
  620. #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
  621. type4, arg4, type5, arg5, type6, arg6) \
  622. static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  623. type5 arg5, type6 arg6) \
  624. { \
  625. return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
  626. }
  627. safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count)
  628. safe_syscall3(ssize_t, write, int, fd, const void *, buff, size_t, count)
  629. safe_syscall4(int, openat, int, dirfd, const char *, pathname, \
  630. int, flags, mode_t, mode)
  631. #if defined(TARGET_NR_wait4) || defined(TARGET_NR_waitpid)
  632. safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \
  633. struct rusage *, rusage)
  634. #endif
  635. safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \
  636. int, options, struct rusage *, rusage)
  637. safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp)
  638. #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
  639. defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
  640. safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \
  641. fd_set *, exceptfds, struct timespec *, timeout, void *, sig)
  642. #endif
  643. #if defined(TARGET_NR_ppoll) || defined(TARGET_NR_ppoll_time64)
  644. safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds,
  645. struct timespec *, tsp, const sigset_t *, sigmask,
  646. size_t, sigsetsize)
  647. #endif
  648. safe_syscall6(int, epoll_pwait, int, epfd, struct epoll_event *, events,
  649. int, maxevents, int, timeout, const sigset_t *, sigmask,
  650. size_t, sigsetsize)
  651. #if defined(__NR_futex)
  652. safe_syscall6(int,futex,int *,uaddr,int,op,int,val, \
  653. const struct timespec *,timeout,int *,uaddr2,int,val3)
  654. #endif
  655. #if defined(__NR_futex_time64)
  656. safe_syscall6(int,futex_time64,int *,uaddr,int,op,int,val, \
  657. const struct timespec *,timeout,int *,uaddr2,int,val3)
  658. #endif
  659. safe_syscall2(int, rt_sigsuspend, sigset_t *, newset, size_t, sigsetsize)
  660. safe_syscall2(int, kill, pid_t, pid, int, sig)
  661. safe_syscall2(int, tkill, int, tid, int, sig)
  662. safe_syscall3(int, tgkill, int, tgid, int, pid, int, sig)
  663. safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt)
  664. safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt)
  665. safe_syscall5(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt,
  666. unsigned long, pos_l, unsigned long, pos_h)
  667. safe_syscall5(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, iovcnt,
  668. unsigned long, pos_l, unsigned long, pos_h)
  669. safe_syscall3(int, connect, int, fd, const struct sockaddr *, addr,
  670. socklen_t, addrlen)
  671. safe_syscall6(ssize_t, sendto, int, fd, const void *, buf, size_t, len,
  672. int, flags, const struct sockaddr *, addr, socklen_t, addrlen)
  673. safe_syscall6(ssize_t, recvfrom, int, fd, void *, buf, size_t, len,
  674. int, flags, struct sockaddr *, addr, socklen_t *, addrlen)
  675. safe_syscall3(ssize_t, sendmsg, int, fd, const struct msghdr *, msg, int, flags)
  676. safe_syscall3(ssize_t, recvmsg, int, fd, struct msghdr *, msg, int, flags)
  677. safe_syscall2(int, flock, int, fd, int, operation)
  678. #if defined(TARGET_NR_rt_sigtimedwait) || defined(TARGET_NR_rt_sigtimedwait_time64)
  679. safe_syscall4(int, rt_sigtimedwait, const sigset_t *, these, siginfo_t *, uinfo,
  680. const struct timespec *, uts, size_t, sigsetsize)
  681. #endif
  682. safe_syscall4(int, accept4, int, fd, struct sockaddr *, addr, socklen_t *, len,
  683. int, flags)
  684. #if defined(TARGET_NR_nanosleep)
  685. safe_syscall2(int, nanosleep, const struct timespec *, req,
  686. struct timespec *, rem)
  687. #endif
  688. #if defined(TARGET_NR_clock_nanosleep) || \
  689. defined(TARGET_NR_clock_nanosleep_time64)
  690. safe_syscall4(int, clock_nanosleep, const clockid_t, clock, int, flags,
  691. const struct timespec *, req, struct timespec *, rem)
  692. #endif
  693. #ifdef __NR_ipc
  694. #ifdef __s390x__
  695. safe_syscall5(int, ipc, int, call, long, first, long, second, long, third,
  696. void *, ptr)
  697. #else
  698. safe_syscall6(int, ipc, int, call, long, first, long, second, long, third,
  699. void *, ptr, long, fifth)
  700. #endif
  701. #endif
  702. #ifdef __NR_msgsnd
  703. safe_syscall4(int, msgsnd, int, msgid, const void *, msgp, size_t, sz,
  704. int, flags)
  705. #endif
  706. #ifdef __NR_msgrcv
  707. safe_syscall5(int, msgrcv, int, msgid, void *, msgp, size_t, sz,
  708. long, msgtype, int, flags)
  709. #endif
  710. #ifdef __NR_semtimedop
  711. safe_syscall4(int, semtimedop, int, semid, struct sembuf *, tsops,
  712. unsigned, nsops, const struct timespec *, timeout)
  713. #endif
  714. #if defined(TARGET_NR_mq_timedsend) || \
  715. defined(TARGET_NR_mq_timedsend_time64)
  716. safe_syscall5(int, mq_timedsend, int, mqdes, const char *, msg_ptr,
  717. size_t, len, unsigned, prio, const struct timespec *, timeout)
  718. #endif
  719. #if defined(TARGET_NR_mq_timedreceive) || \
  720. defined(TARGET_NR_mq_timedreceive_time64)
  721. safe_syscall5(int, mq_timedreceive, int, mqdes, char *, msg_ptr,
  722. size_t, len, unsigned *, prio, const struct timespec *, timeout)
  723. #endif
  724. #if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range)
  725. safe_syscall6(ssize_t, copy_file_range, int, infd, loff_t *, pinoff,
  726. int, outfd, loff_t *, poutoff, size_t, length,
  727. unsigned int, flags)
  728. #endif
  729. /* We do ioctl like this rather than via safe_syscall3 to preserve the
  730. * "third argument might be integer or pointer or not present" behaviour of
  731. * the libc function.
  732. */
  733. #define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
  734. /* Similarly for fcntl. Note that callers must always:
  735. * pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK
  736. * use the flock64 struct rather than unsuffixed flock
  737. * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts.
  738. */
  739. #ifdef __NR_fcntl64
  740. #define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
  741. #else
  742. #define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
  743. #endif
  744. static inline int host_to_target_sock_type(int host_type)
  745. {
  746. int target_type;
  747. switch (host_type & 0xf /* SOCK_TYPE_MASK */) {
  748. case SOCK_DGRAM:
  749. target_type = TARGET_SOCK_DGRAM;
  750. break;
  751. case SOCK_STREAM:
  752. target_type = TARGET_SOCK_STREAM;
  753. break;
  754. default:
  755. target_type = host_type & 0xf /* SOCK_TYPE_MASK */;
  756. break;
  757. }
  758. #if defined(SOCK_CLOEXEC)
  759. if (host_type & SOCK_CLOEXEC) {
  760. target_type |= TARGET_SOCK_CLOEXEC;
  761. }
  762. #endif
  763. #if defined(SOCK_NONBLOCK)
  764. if (host_type & SOCK_NONBLOCK) {
  765. target_type |= TARGET_SOCK_NONBLOCK;
  766. }
  767. #endif
  768. return target_type;
  769. }
  770. static abi_ulong target_brk;
  771. static abi_ulong target_original_brk;
  772. static abi_ulong brk_page;
  773. void target_set_brk(abi_ulong new_brk)
  774. {
  775. target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
  776. brk_page = HOST_PAGE_ALIGN(target_brk);
  777. }
  778. //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
  779. #define DEBUGF_BRK(message, args...)
  780. /* do_brk() must return target values and target errnos. */
  781. abi_long do_brk(abi_ulong new_brk)
  782. {
  783. abi_long mapped_addr;
  784. abi_ulong new_alloc_size;
  785. /* brk pointers are always untagged */
  786. DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
  787. if (!new_brk) {
  788. DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
  789. return target_brk;
  790. }
  791. if (new_brk < target_original_brk) {
  792. DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
  793. target_brk);
  794. return target_brk;
  795. }
  796. /* If the new brk is less than the highest page reserved to the
  797. * target heap allocation, set it and we're almost done... */
  798. if (new_brk <= brk_page) {
  799. /* Heap contents are initialized to zero, as for anonymous
  800. * mapped pages. */
  801. if (new_brk > target_brk) {
  802. memset(g2h_untagged(target_brk), 0, new_brk - target_brk);
  803. }
  804. target_brk = new_brk;
  805. DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
  806. return target_brk;
  807. }
  808. /* We need to allocate more memory after the brk... Note that
  809. * we don't use MAP_FIXED because that will map over the top of
  810. * any existing mapping (like the one with the host libc or qemu
  811. * itself); instead we treat "mapped but at wrong address" as
  812. * a failure and unmap again.
  813. */
  814. new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
  815. mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
  816. PROT_READ|PROT_WRITE,
  817. MAP_ANON|MAP_PRIVATE, 0, 0));
  818. if (mapped_addr == brk_page) {
  819. /* Heap contents are initialized to zero, as for anonymous
  820. * mapped pages. Technically the new pages are already
  821. * initialized to zero since they *are* anonymous mapped
  822. * pages, however we have to take care with the contents that
  823. * come from the remaining part of the previous page: it may
  824. * contains garbage data due to a previous heap usage (grown
  825. * then shrunken). */
  826. memset(g2h_untagged(target_brk), 0, brk_page - target_brk);
  827. target_brk = new_brk;
  828. brk_page = HOST_PAGE_ALIGN(target_brk);
  829. DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
  830. target_brk);
  831. return target_brk;
  832. } else if (mapped_addr != -1) {
  833. /* Mapped but at wrong address, meaning there wasn't actually
  834. * enough space for this brk.
  835. */
  836. target_munmap(mapped_addr, new_alloc_size);
  837. mapped_addr = -1;
  838. DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
  839. }
  840. else {
  841. DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
  842. }
  843. #if defined(TARGET_ALPHA)
  844. /* We (partially) emulate OSF/1 on Alpha, which requires we
  845. return a proper errno, not an unchanged brk value. */
  846. return -TARGET_ENOMEM;
  847. #endif
  848. /* For everything else, return the previous break. */
  849. return target_brk;
  850. }
  851. #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
  852. defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
  853. static inline abi_long copy_from_user_fdset(fd_set *fds,
  854. abi_ulong target_fds_addr,
  855. int n)
  856. {
  857. int i, nw, j, k;
  858. abi_ulong b, *target_fds;
  859. nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
  860. if (!(target_fds = lock_user(VERIFY_READ,
  861. target_fds_addr,
  862. sizeof(abi_ulong) * nw,
  863. 1)))
  864. return -TARGET_EFAULT;
  865. FD_ZERO(fds);
  866. k = 0;
  867. for (i = 0; i < nw; i++) {
  868. /* grab the abi_ulong */
  869. __get_user(b, &target_fds[i]);
  870. for (j = 0; j < TARGET_ABI_BITS; j++) {
  871. /* check the bit inside the abi_ulong */
  872. if ((b >> j) & 1)
  873. FD_SET(k, fds);
  874. k++;
  875. }
  876. }
  877. unlock_user(target_fds, target_fds_addr, 0);
  878. return 0;
  879. }
  880. static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
  881. abi_ulong target_fds_addr,
  882. int n)
  883. {
  884. if (target_fds_addr) {
  885. if (copy_from_user_fdset(fds, target_fds_addr, n))
  886. return -TARGET_EFAULT;
  887. *fds_ptr = fds;
  888. } else {
  889. *fds_ptr = NULL;
  890. }
  891. return 0;
  892. }
  893. static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
  894. const fd_set *fds,
  895. int n)
  896. {
  897. int i, nw, j, k;
  898. abi_long v;
  899. abi_ulong *target_fds;
  900. nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
  901. if (!(target_fds = lock_user(VERIFY_WRITE,
  902. target_fds_addr,
  903. sizeof(abi_ulong) * nw,
  904. 0)))
  905. return -TARGET_EFAULT;
  906. k = 0;
  907. for (i = 0; i < nw; i++) {
  908. v = 0;
  909. for (j = 0; j < TARGET_ABI_BITS; j++) {
  910. v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
  911. k++;
  912. }
  913. __put_user(v, &target_fds[i]);
  914. }
  915. unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
  916. return 0;
  917. }
  918. #endif
  919. #if defined(__alpha__)
  920. #define HOST_HZ 1024
  921. #else
  922. #define HOST_HZ 100
  923. #endif
  924. static inline abi_long host_to_target_clock_t(long ticks)
  925. {
  926. #if HOST_HZ == TARGET_HZ
  927. return ticks;
  928. #else
  929. return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
  930. #endif
  931. }
  932. static inline abi_long host_to_target_rusage(abi_ulong target_addr,
  933. const struct rusage *rusage)
  934. {
  935. struct target_rusage *target_rusage;
  936. if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
  937. return -TARGET_EFAULT;
  938. target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
  939. target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
  940. target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
  941. target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
  942. target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
  943. target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
  944. target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
  945. target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
  946. target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
  947. target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
  948. target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
  949. target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
  950. target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
  951. target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
  952. target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
  953. target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
  954. target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
  955. target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
  956. unlock_user_struct(target_rusage, target_addr, 1);
  957. return 0;
  958. }
  959. #ifdef TARGET_NR_setrlimit
  960. static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
  961. {
  962. abi_ulong target_rlim_swap;
  963. rlim_t result;
  964. target_rlim_swap = tswapal(target_rlim);
  965. if (target_rlim_swap == TARGET_RLIM_INFINITY)
  966. return RLIM_INFINITY;
  967. result = target_rlim_swap;
  968. if (target_rlim_swap != (rlim_t)result)
  969. return RLIM_INFINITY;
  970. return result;
  971. }
  972. #endif
  973. #if defined(TARGET_NR_getrlimit) || defined(TARGET_NR_ugetrlimit)
  974. static inline abi_ulong host_to_target_rlim(rlim_t rlim)
  975. {
  976. abi_ulong target_rlim_swap;
  977. abi_ulong result;
  978. if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
  979. target_rlim_swap = TARGET_RLIM_INFINITY;
  980. else
  981. target_rlim_swap = rlim;
  982. result = tswapal(target_rlim_swap);
  983. return result;
  984. }
  985. #endif
  986. static inline int target_to_host_resource(int code)
  987. {
  988. switch (code) {
  989. case TARGET_RLIMIT_AS:
  990. return RLIMIT_AS;
  991. case TARGET_RLIMIT_CORE:
  992. return RLIMIT_CORE;
  993. case TARGET_RLIMIT_CPU:
  994. return RLIMIT_CPU;
  995. case TARGET_RLIMIT_DATA:
  996. return RLIMIT_DATA;
  997. case TARGET_RLIMIT_FSIZE:
  998. return RLIMIT_FSIZE;
  999. case TARGET_RLIMIT_LOCKS:
  1000. return RLIMIT_LOCKS;
  1001. case TARGET_RLIMIT_MEMLOCK:
  1002. return RLIMIT_MEMLOCK;
  1003. case TARGET_RLIMIT_MSGQUEUE:
  1004. return RLIMIT_MSGQUEUE;
  1005. case TARGET_RLIMIT_NICE:
  1006. return RLIMIT_NICE;
  1007. case TARGET_RLIMIT_NOFILE:
  1008. return RLIMIT_NOFILE;
  1009. case TARGET_RLIMIT_NPROC:
  1010. return RLIMIT_NPROC;
  1011. case TARGET_RLIMIT_RSS:
  1012. return RLIMIT_RSS;
  1013. case TARGET_RLIMIT_RTPRIO:
  1014. return RLIMIT_RTPRIO;
  1015. #ifdef RLIMIT_RTTIME
  1016. case TARGET_RLIMIT_RTTIME:
  1017. return RLIMIT_RTTIME;
  1018. #endif
  1019. case TARGET_RLIMIT_SIGPENDING:
  1020. return RLIMIT_SIGPENDING;
  1021. case TARGET_RLIMIT_STACK:
  1022. return RLIMIT_STACK;
  1023. default:
  1024. return code;
  1025. }
  1026. }
  1027. static inline abi_long copy_from_user_timeval(struct timeval *tv,
  1028. abi_ulong target_tv_addr)
  1029. {
  1030. struct target_timeval *target_tv;
  1031. if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) {
  1032. return -TARGET_EFAULT;
  1033. }
  1034. __get_user(tv->tv_sec, &target_tv->tv_sec);
  1035. __get_user(tv->tv_usec, &target_tv->tv_usec);
  1036. unlock_user_struct(target_tv, target_tv_addr, 0);
  1037. return 0;
  1038. }
  1039. static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
  1040. const struct timeval *tv)
  1041. {
  1042. struct target_timeval *target_tv;
  1043. if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
  1044. return -TARGET_EFAULT;
  1045. }
  1046. __put_user(tv->tv_sec, &target_tv->tv_sec);
  1047. __put_user(tv->tv_usec, &target_tv->tv_usec);
  1048. unlock_user_struct(target_tv, target_tv_addr, 1);
  1049. return 0;
  1050. }
  1051. #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
  1052. static inline abi_long copy_from_user_timeval64(struct timeval *tv,
  1053. abi_ulong target_tv_addr)
  1054. {
  1055. struct target__kernel_sock_timeval *target_tv;
  1056. if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) {
  1057. return -TARGET_EFAULT;
  1058. }
  1059. __get_user(tv->tv_sec, &target_tv->tv_sec);
  1060. __get_user(tv->tv_usec, &target_tv->tv_usec);
  1061. unlock_user_struct(target_tv, target_tv_addr, 0);
  1062. return 0;
  1063. }
  1064. #endif
  1065. static inline abi_long copy_to_user_timeval64(abi_ulong target_tv_addr,
  1066. const struct timeval *tv)
  1067. {
  1068. struct target__kernel_sock_timeval *target_tv;
  1069. if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
  1070. return -TARGET_EFAULT;
  1071. }
  1072. __put_user(tv->tv_sec, &target_tv->tv_sec);
  1073. __put_user(tv->tv_usec, &target_tv->tv_usec);
  1074. unlock_user_struct(target_tv, target_tv_addr, 1);
  1075. return 0;
  1076. }
  1077. #if defined(TARGET_NR_futex) || \
  1078. defined(TARGET_NR_rt_sigtimedwait) || \
  1079. defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6) || \
  1080. defined(TARGET_NR_nanosleep) || defined(TARGET_NR_clock_settime) || \
  1081. defined(TARGET_NR_utimensat) || defined(TARGET_NR_mq_timedsend) || \
  1082. defined(TARGET_NR_mq_timedreceive) || defined(TARGET_NR_ipc) || \
  1083. defined(TARGET_NR_semop) || defined(TARGET_NR_semtimedop) || \
  1084. defined(TARGET_NR_timer_settime) || \
  1085. (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
  1086. static inline abi_long target_to_host_timespec(struct timespec *host_ts,
  1087. abi_ulong target_addr)
  1088. {
  1089. struct target_timespec *target_ts;
  1090. if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
  1091. return -TARGET_EFAULT;
  1092. }
  1093. __get_user(host_ts->tv_sec, &target_ts->tv_sec);
  1094. __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1095. unlock_user_struct(target_ts, target_addr, 0);
  1096. return 0;
  1097. }
  1098. #endif
  1099. #if defined(TARGET_NR_clock_settime64) || defined(TARGET_NR_futex_time64) || \
  1100. defined(TARGET_NR_timer_settime64) || \
  1101. defined(TARGET_NR_mq_timedsend_time64) || \
  1102. defined(TARGET_NR_mq_timedreceive_time64) || \
  1103. (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)) || \
  1104. defined(TARGET_NR_clock_nanosleep_time64) || \
  1105. defined(TARGET_NR_rt_sigtimedwait_time64) || \
  1106. defined(TARGET_NR_utimensat) || \
  1107. defined(TARGET_NR_utimensat_time64) || \
  1108. defined(TARGET_NR_semtimedop_time64) || \
  1109. defined(TARGET_NR_pselect6_time64) || defined(TARGET_NR_ppoll_time64)
  1110. static inline abi_long target_to_host_timespec64(struct timespec *host_ts,
  1111. abi_ulong target_addr)
  1112. {
  1113. struct target__kernel_timespec *target_ts;
  1114. if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
  1115. return -TARGET_EFAULT;
  1116. }
  1117. __get_user(host_ts->tv_sec, &target_ts->tv_sec);
  1118. __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1119. /* in 32bit mode, this drops the padding */
  1120. host_ts->tv_nsec = (long)(abi_long)host_ts->tv_nsec;
  1121. unlock_user_struct(target_ts, target_addr, 0);
  1122. return 0;
  1123. }
  1124. #endif
  1125. static inline abi_long host_to_target_timespec(abi_ulong target_addr,
  1126. struct timespec *host_ts)
  1127. {
  1128. struct target_timespec *target_ts;
  1129. if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
  1130. return -TARGET_EFAULT;
  1131. }
  1132. __put_user(host_ts->tv_sec, &target_ts->tv_sec);
  1133. __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1134. unlock_user_struct(target_ts, target_addr, 1);
  1135. return 0;
  1136. }
  1137. static inline abi_long host_to_target_timespec64(abi_ulong target_addr,
  1138. struct timespec *host_ts)
  1139. {
  1140. struct target__kernel_timespec *target_ts;
  1141. if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
  1142. return -TARGET_EFAULT;
  1143. }
  1144. __put_user(host_ts->tv_sec, &target_ts->tv_sec);
  1145. __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1146. unlock_user_struct(target_ts, target_addr, 1);
  1147. return 0;
  1148. }
  1149. #if defined(TARGET_NR_gettimeofday)
  1150. static inline abi_long copy_to_user_timezone(abi_ulong target_tz_addr,
  1151. struct timezone *tz)
  1152. {
  1153. struct target_timezone *target_tz;
  1154. if (!lock_user_struct(VERIFY_WRITE, target_tz, target_tz_addr, 1)) {
  1155. return -TARGET_EFAULT;
  1156. }
  1157. __put_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
  1158. __put_user(tz->tz_dsttime, &target_tz->tz_dsttime);
  1159. unlock_user_struct(target_tz, target_tz_addr, 1);
  1160. return 0;
  1161. }
  1162. #endif
  1163. #if defined(TARGET_NR_settimeofday)
  1164. static inline abi_long copy_from_user_timezone(struct timezone *tz,
  1165. abi_ulong target_tz_addr)
  1166. {
  1167. struct target_timezone *target_tz;
  1168. if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) {
  1169. return -TARGET_EFAULT;
  1170. }
  1171. __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
  1172. __get_user(tz->tz_dsttime, &target_tz->tz_dsttime);
  1173. unlock_user_struct(target_tz, target_tz_addr, 0);
  1174. return 0;
  1175. }
  1176. #endif
  1177. #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
  1178. #include <mqueue.h>
  1179. static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
  1180. abi_ulong target_mq_attr_addr)
  1181. {
  1182. struct target_mq_attr *target_mq_attr;
  1183. if (!lock_user_struct(VERIFY_READ, target_mq_attr,
  1184. target_mq_attr_addr, 1))
  1185. return -TARGET_EFAULT;
  1186. __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
  1187. __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
  1188. __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
  1189. __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
  1190. unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
  1191. return 0;
  1192. }
  1193. static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
  1194. const struct mq_attr *attr)
  1195. {
  1196. struct target_mq_attr *target_mq_attr;
  1197. if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
  1198. target_mq_attr_addr, 0))
  1199. return -TARGET_EFAULT;
  1200. __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
  1201. __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
  1202. __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
  1203. __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
  1204. unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
  1205. return 0;
  1206. }
  1207. #endif
  1208. #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
  1209. /* do_select() must return target values and target errnos. */
  1210. static abi_long do_select(int n,
  1211. abi_ulong rfd_addr, abi_ulong wfd_addr,
  1212. abi_ulong efd_addr, abi_ulong target_tv_addr)
  1213. {
  1214. fd_set rfds, wfds, efds;
  1215. fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
  1216. struct timeval tv;
  1217. struct timespec ts, *ts_ptr;
  1218. abi_long ret;
  1219. ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
  1220. if (ret) {
  1221. return ret;
  1222. }
  1223. ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
  1224. if (ret) {
  1225. return ret;
  1226. }
  1227. ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
  1228. if (ret) {
  1229. return ret;
  1230. }
  1231. if (target_tv_addr) {
  1232. if (copy_from_user_timeval(&tv, target_tv_addr))
  1233. return -TARGET_EFAULT;
  1234. ts.tv_sec = tv.tv_sec;
  1235. ts.tv_nsec = tv.tv_usec * 1000;
  1236. ts_ptr = &ts;
  1237. } else {
  1238. ts_ptr = NULL;
  1239. }
  1240. ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
  1241. ts_ptr, NULL));
  1242. if (!is_error(ret)) {
  1243. if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
  1244. return -TARGET_EFAULT;
  1245. if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
  1246. return -TARGET_EFAULT;
  1247. if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
  1248. return -TARGET_EFAULT;
  1249. if (target_tv_addr) {
  1250. tv.tv_sec = ts.tv_sec;
  1251. tv.tv_usec = ts.tv_nsec / 1000;
  1252. if (copy_to_user_timeval(target_tv_addr, &tv)) {
  1253. return -TARGET_EFAULT;
  1254. }
  1255. }
  1256. }
  1257. return ret;
  1258. }
  1259. #if defined(TARGET_WANT_OLD_SYS_SELECT)
  1260. static abi_long do_old_select(abi_ulong arg1)
  1261. {
  1262. struct target_sel_arg_struct *sel;
  1263. abi_ulong inp, outp, exp, tvp;
  1264. long nsel;
  1265. if (!lock_user_struct(VERIFY_READ, sel, arg1, 1)) {
  1266. return -TARGET_EFAULT;
  1267. }
  1268. nsel = tswapal(sel->n);
  1269. inp = tswapal(sel->inp);
  1270. outp = tswapal(sel->outp);
  1271. exp = tswapal(sel->exp);
  1272. tvp = tswapal(sel->tvp);
  1273. unlock_user_struct(sel, arg1, 0);
  1274. return do_select(nsel, inp, outp, exp, tvp);
  1275. }
  1276. #endif
  1277. #endif
  1278. #if defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
  1279. static abi_long do_pselect6(abi_long arg1, abi_long arg2, abi_long arg3,
  1280. abi_long arg4, abi_long arg5, abi_long arg6,
  1281. bool time64)
  1282. {
  1283. abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
  1284. fd_set rfds, wfds, efds;
  1285. fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
  1286. struct timespec ts, *ts_ptr;
  1287. abi_long ret;
  1288. /*
  1289. * The 6th arg is actually two args smashed together,
  1290. * so we cannot use the C library.
  1291. */
  1292. struct {
  1293. sigset_t *set;
  1294. size_t size;
  1295. } sig, *sig_ptr;
  1296. abi_ulong arg_sigset, arg_sigsize, *arg7;
  1297. n = arg1;
  1298. rfd_addr = arg2;
  1299. wfd_addr = arg3;
  1300. efd_addr = arg4;
  1301. ts_addr = arg5;
  1302. ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
  1303. if (ret) {
  1304. return ret;
  1305. }
  1306. ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
  1307. if (ret) {
  1308. return ret;
  1309. }
  1310. ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
  1311. if (ret) {
  1312. return ret;
  1313. }
  1314. /*
  1315. * This takes a timespec, and not a timeval, so we cannot
  1316. * use the do_select() helper ...
  1317. */
  1318. if (ts_addr) {
  1319. if (time64) {
  1320. if (target_to_host_timespec64(&ts, ts_addr)) {
  1321. return -TARGET_EFAULT;
  1322. }
  1323. } else {
  1324. if (target_to_host_timespec(&ts, ts_addr)) {
  1325. return -TARGET_EFAULT;
  1326. }
  1327. }
  1328. ts_ptr = &ts;
  1329. } else {
  1330. ts_ptr = NULL;
  1331. }
  1332. /* Extract the two packed args for the sigset */
  1333. sig_ptr = NULL;
  1334. if (arg6) {
  1335. arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
  1336. if (!arg7) {
  1337. return -TARGET_EFAULT;
  1338. }
  1339. arg_sigset = tswapal(arg7[0]);
  1340. arg_sigsize = tswapal(arg7[1]);
  1341. unlock_user(arg7, arg6, 0);
  1342. if (arg_sigset) {
  1343. ret = process_sigsuspend_mask(&sig.set, arg_sigset, arg_sigsize);
  1344. if (ret != 0) {
  1345. return ret;
  1346. }
  1347. sig_ptr = &sig;
  1348. sig.size = SIGSET_T_SIZE;
  1349. }
  1350. }
  1351. ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
  1352. ts_ptr, sig_ptr));
  1353. if (sig_ptr) {
  1354. finish_sigsuspend_mask(ret);
  1355. }
  1356. if (!is_error(ret)) {
  1357. if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) {
  1358. return -TARGET_EFAULT;
  1359. }
  1360. if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) {
  1361. return -TARGET_EFAULT;
  1362. }
  1363. if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) {
  1364. return -TARGET_EFAULT;
  1365. }
  1366. if (time64) {
  1367. if (ts_addr && host_to_target_timespec64(ts_addr, &ts)) {
  1368. return -TARGET_EFAULT;
  1369. }
  1370. } else {
  1371. if (ts_addr && host_to_target_timespec(ts_addr, &ts)) {
  1372. return -TARGET_EFAULT;
  1373. }
  1374. }
  1375. }
  1376. return ret;
  1377. }
  1378. #endif
  1379. #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) || \
  1380. defined(TARGET_NR_ppoll_time64)
  1381. static abi_long do_ppoll(abi_long arg1, abi_long arg2, abi_long arg3,
  1382. abi_long arg4, abi_long arg5, bool ppoll, bool time64)
  1383. {
  1384. struct target_pollfd *target_pfd;
  1385. unsigned int nfds = arg2;
  1386. struct pollfd *pfd;
  1387. unsigned int i;
  1388. abi_long ret;
  1389. pfd = NULL;
  1390. target_pfd = NULL;
  1391. if (nfds) {
  1392. if (nfds > (INT_MAX / sizeof(struct target_pollfd))) {
  1393. return -TARGET_EINVAL;
  1394. }
  1395. target_pfd = lock_user(VERIFY_WRITE, arg1,
  1396. sizeof(struct target_pollfd) * nfds, 1);
  1397. if (!target_pfd) {
  1398. return -TARGET_EFAULT;
  1399. }
  1400. pfd = alloca(sizeof(struct pollfd) * nfds);
  1401. for (i = 0; i < nfds; i++) {
  1402. pfd[i].fd = tswap32(target_pfd[i].fd);
  1403. pfd[i].events = tswap16(target_pfd[i].events);
  1404. }
  1405. }
  1406. if (ppoll) {
  1407. struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
  1408. sigset_t *set = NULL;
  1409. if (arg3) {
  1410. if (time64) {
  1411. if (target_to_host_timespec64(timeout_ts, arg3)) {
  1412. unlock_user(target_pfd, arg1, 0);
  1413. return -TARGET_EFAULT;
  1414. }
  1415. } else {
  1416. if (target_to_host_timespec(timeout_ts, arg3)) {
  1417. unlock_user(target_pfd, arg1, 0);
  1418. return -TARGET_EFAULT;
  1419. }
  1420. }
  1421. } else {
  1422. timeout_ts = NULL;
  1423. }
  1424. if (arg4) {
  1425. ret = process_sigsuspend_mask(&set, arg4, arg5);
  1426. if (ret != 0) {
  1427. unlock_user(target_pfd, arg1, 0);
  1428. return ret;
  1429. }
  1430. }
  1431. ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts,
  1432. set, SIGSET_T_SIZE));
  1433. if (set) {
  1434. finish_sigsuspend_mask(ret);
  1435. }
  1436. if (!is_error(ret) && arg3) {
  1437. if (time64) {
  1438. if (host_to_target_timespec64(arg3, timeout_ts)) {
  1439. return -TARGET_EFAULT;
  1440. }
  1441. } else {
  1442. if (host_to_target_timespec(arg3, timeout_ts)) {
  1443. return -TARGET_EFAULT;
  1444. }
  1445. }
  1446. }
  1447. } else {
  1448. struct timespec ts, *pts;
  1449. if (arg3 >= 0) {
  1450. /* Convert ms to secs, ns */
  1451. ts.tv_sec = arg3 / 1000;
  1452. ts.tv_nsec = (arg3 % 1000) * 1000000LL;
  1453. pts = &ts;
  1454. } else {
  1455. /* -ve poll() timeout means "infinite" */
  1456. pts = NULL;
  1457. }
  1458. ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0));
  1459. }
  1460. if (!is_error(ret)) {
  1461. for (i = 0; i < nfds; i++) {
  1462. target_pfd[i].revents = tswap16(pfd[i].revents);
  1463. }
  1464. }
  1465. unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
  1466. return ret;
  1467. }
  1468. #endif
  1469. static abi_long do_pipe(CPUArchState *cpu_env, abi_ulong pipedes,
  1470. int flags, int is_pipe2)
  1471. {
  1472. int host_pipe[2];
  1473. abi_long ret;
  1474. ret = pipe2(host_pipe, flags);
  1475. if (is_error(ret))
  1476. return get_errno(ret);
  1477. /* Several targets have special calling conventions for the original
  1478. pipe syscall, but didn't replicate this into the pipe2 syscall. */
  1479. if (!is_pipe2) {
  1480. #if defined(TARGET_ALPHA)
  1481. cpu_env->ir[IR_A4] = host_pipe[1];
  1482. return host_pipe[0];
  1483. #elif defined(TARGET_MIPS)
  1484. cpu_env->active_tc.gpr[3] = host_pipe[1];
  1485. return host_pipe[0];
  1486. #elif defined(TARGET_SH4)
  1487. cpu_env->gregs[1] = host_pipe[1];
  1488. return host_pipe[0];
  1489. #elif defined(TARGET_SPARC)
  1490. cpu_env->regwptr[1] = host_pipe[1];
  1491. return host_pipe[0];
  1492. #endif
  1493. }
  1494. if (put_user_s32(host_pipe[0], pipedes)
  1495. || put_user_s32(host_pipe[1], pipedes + sizeof(abi_int)))
  1496. return -TARGET_EFAULT;
  1497. return get_errno(ret);
  1498. }
  1499. static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
  1500. abi_ulong target_addr,
  1501. socklen_t len)
  1502. {
  1503. struct target_ip_mreqn *target_smreqn;
  1504. target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
  1505. if (!target_smreqn)
  1506. return -TARGET_EFAULT;
  1507. mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
  1508. mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
  1509. if (len == sizeof(struct target_ip_mreqn))
  1510. mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
  1511. unlock_user(target_smreqn, target_addr, 0);
  1512. return 0;
  1513. }
  1514. static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr,
  1515. abi_ulong target_addr,
  1516. socklen_t len)
  1517. {
  1518. const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
  1519. sa_family_t sa_family;
  1520. struct target_sockaddr *target_saddr;
  1521. if (fd_trans_target_to_host_addr(fd)) {
  1522. return fd_trans_target_to_host_addr(fd)(addr, target_addr, len);
  1523. }
  1524. target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
  1525. if (!target_saddr)
  1526. return -TARGET_EFAULT;
  1527. sa_family = tswap16(target_saddr->sa_family);
  1528. /* Oops. The caller might send a incomplete sun_path; sun_path
  1529. * must be terminated by \0 (see the manual page), but
  1530. * unfortunately it is quite common to specify sockaddr_un
  1531. * length as "strlen(x->sun_path)" while it should be
  1532. * "strlen(...) + 1". We'll fix that here if needed.
  1533. * Linux kernel has a similar feature.
  1534. */
  1535. if (sa_family == AF_UNIX) {
  1536. if (len < unix_maxlen && len > 0) {
  1537. char *cp = (char*)target_saddr;
  1538. if ( cp[len-1] && !cp[len] )
  1539. len++;
  1540. }
  1541. if (len > unix_maxlen)
  1542. len = unix_maxlen;
  1543. }
  1544. memcpy(addr, target_saddr, len);
  1545. addr->sa_family = sa_family;
  1546. if (sa_family == AF_NETLINK) {
  1547. struct sockaddr_nl *nladdr;
  1548. nladdr = (struct sockaddr_nl *)addr;
  1549. nladdr->nl_pid = tswap32(nladdr->nl_pid);
  1550. nladdr->nl_groups = tswap32(nladdr->nl_groups);
  1551. } else if (sa_family == AF_PACKET) {
  1552. struct target_sockaddr_ll *lladdr;
  1553. lladdr = (struct target_sockaddr_ll *)addr;
  1554. lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex);
  1555. lladdr->sll_hatype = tswap16(lladdr->sll_hatype);
  1556. }
  1557. unlock_user(target_saddr, target_addr, 0);
  1558. return 0;
  1559. }
  1560. static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
  1561. struct sockaddr *addr,
  1562. socklen_t len)
  1563. {
  1564. struct target_sockaddr *target_saddr;
  1565. if (len == 0) {
  1566. return 0;
  1567. }
  1568. assert(addr);
  1569. target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
  1570. if (!target_saddr)
  1571. return -TARGET_EFAULT;
  1572. memcpy(target_saddr, addr, len);
  1573. if (len >= offsetof(struct target_sockaddr, sa_family) +
  1574. sizeof(target_saddr->sa_family)) {
  1575. target_saddr->sa_family = tswap16(addr->sa_family);
  1576. }
  1577. if (addr->sa_family == AF_NETLINK &&
  1578. len >= sizeof(struct target_sockaddr_nl)) {
  1579. struct target_sockaddr_nl *target_nl =
  1580. (struct target_sockaddr_nl *)target_saddr;
  1581. target_nl->nl_pid = tswap32(target_nl->nl_pid);
  1582. target_nl->nl_groups = tswap32(target_nl->nl_groups);
  1583. } else if (addr->sa_family == AF_PACKET) {
  1584. struct sockaddr_ll *target_ll = (struct sockaddr_ll *)target_saddr;
  1585. target_ll->sll_ifindex = tswap32(target_ll->sll_ifindex);
  1586. target_ll->sll_hatype = tswap16(target_ll->sll_hatype);
  1587. } else if (addr->sa_family == AF_INET6 &&
  1588. len >= sizeof(struct target_sockaddr_in6)) {
  1589. struct target_sockaddr_in6 *target_in6 =
  1590. (struct target_sockaddr_in6 *)target_saddr;
  1591. target_in6->sin6_scope_id = tswap16(target_in6->sin6_scope_id);
  1592. }
  1593. unlock_user(target_saddr, target_addr, len);
  1594. return 0;
  1595. }
  1596. static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
  1597. struct target_msghdr *target_msgh)
  1598. {
  1599. struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
  1600. abi_long msg_controllen;
  1601. abi_ulong target_cmsg_addr;
  1602. struct target_cmsghdr *target_cmsg, *target_cmsg_start;
  1603. socklen_t space = 0;
  1604. msg_controllen = tswapal(target_msgh->msg_controllen);
  1605. if (msg_controllen < sizeof (struct target_cmsghdr))
  1606. goto the_end;
  1607. target_cmsg_addr = tswapal(target_msgh->msg_control);
  1608. target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
  1609. target_cmsg_start = target_cmsg;
  1610. if (!target_cmsg)
  1611. return -TARGET_EFAULT;
  1612. while (cmsg && target_cmsg) {
  1613. void *data = CMSG_DATA(cmsg);
  1614. void *target_data = TARGET_CMSG_DATA(target_cmsg);
  1615. int len = tswapal(target_cmsg->cmsg_len)
  1616. - sizeof(struct target_cmsghdr);
  1617. space += CMSG_SPACE(len);
  1618. if (space > msgh->msg_controllen) {
  1619. space -= CMSG_SPACE(len);
  1620. /* This is a QEMU bug, since we allocated the payload
  1621. * area ourselves (unlike overflow in host-to-target
  1622. * conversion, which is just the guest giving us a buffer
  1623. * that's too small). It can't happen for the payload types
  1624. * we currently support; if it becomes an issue in future
  1625. * we would need to improve our allocation strategy to
  1626. * something more intelligent than "twice the size of the
  1627. * target buffer we're reading from".
  1628. */
  1629. qemu_log_mask(LOG_UNIMP,
  1630. ("Unsupported ancillary data %d/%d: "
  1631. "unhandled msg size\n"),
  1632. tswap32(target_cmsg->cmsg_level),
  1633. tswap32(target_cmsg->cmsg_type));
  1634. break;
  1635. }
  1636. if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
  1637. cmsg->cmsg_level = SOL_SOCKET;
  1638. } else {
  1639. cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
  1640. }
  1641. cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
  1642. cmsg->cmsg_len = CMSG_LEN(len);
  1643. if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
  1644. int *fd = (int *)data;
  1645. int *target_fd = (int *)target_data;
  1646. int i, numfds = len / sizeof(int);
  1647. for (i = 0; i < numfds; i++) {
  1648. __get_user(fd[i], target_fd + i);
  1649. }
  1650. } else if (cmsg->cmsg_level == SOL_SOCKET
  1651. && cmsg->cmsg_type == SCM_CREDENTIALS) {
  1652. struct ucred *cred = (struct ucred *)data;
  1653. struct target_ucred *target_cred =
  1654. (struct target_ucred *)target_data;
  1655. __get_user(cred->pid, &target_cred->pid);
  1656. __get_user(cred->uid, &target_cred->uid);
  1657. __get_user(cred->gid, &target_cred->gid);
  1658. } else {
  1659. qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n",
  1660. cmsg->cmsg_level, cmsg->cmsg_type);
  1661. memcpy(data, target_data, len);
  1662. }
  1663. cmsg = CMSG_NXTHDR(msgh, cmsg);
  1664. target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
  1665. target_cmsg_start);
  1666. }
  1667. unlock_user(target_cmsg, target_cmsg_addr, 0);
  1668. the_end:
  1669. msgh->msg_controllen = space;
  1670. return 0;
  1671. }
  1672. static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
  1673. struct msghdr *msgh)
  1674. {
  1675. struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
  1676. abi_long msg_controllen;
  1677. abi_ulong target_cmsg_addr;
  1678. struct target_cmsghdr *target_cmsg, *target_cmsg_start;
  1679. socklen_t space = 0;
  1680. msg_controllen = tswapal(target_msgh->msg_controllen);
  1681. if (msg_controllen < sizeof (struct target_cmsghdr))
  1682. goto the_end;
  1683. target_cmsg_addr = tswapal(target_msgh->msg_control);
  1684. target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
  1685. target_cmsg_start = target_cmsg;
  1686. if (!target_cmsg)
  1687. return -TARGET_EFAULT;
  1688. while (cmsg && target_cmsg) {
  1689. void *data = CMSG_DATA(cmsg);
  1690. void *target_data = TARGET_CMSG_DATA(target_cmsg);
  1691. int len = cmsg->cmsg_len - sizeof(struct cmsghdr);
  1692. int tgt_len, tgt_space;
  1693. /* We never copy a half-header but may copy half-data;
  1694. * this is Linux's behaviour in put_cmsg(). Note that
  1695. * truncation here is a guest problem (which we report
  1696. * to the guest via the CTRUNC bit), unlike truncation
  1697. * in target_to_host_cmsg, which is a QEMU bug.
  1698. */
  1699. if (msg_controllen < sizeof(struct target_cmsghdr)) {
  1700. target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
  1701. break;
  1702. }
  1703. if (cmsg->cmsg_level == SOL_SOCKET) {
  1704. target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
  1705. } else {
  1706. target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
  1707. }
  1708. target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
  1709. /* Payload types which need a different size of payload on
  1710. * the target must adjust tgt_len here.
  1711. */
  1712. tgt_len = len;
  1713. switch (cmsg->cmsg_level) {
  1714. case SOL_SOCKET:
  1715. switch (cmsg->cmsg_type) {
  1716. case SO_TIMESTAMP:
  1717. tgt_len = sizeof(struct target_timeval);
  1718. break;
  1719. default:
  1720. break;
  1721. }
  1722. break;
  1723. default:
  1724. break;
  1725. }
  1726. if (msg_controllen < TARGET_CMSG_LEN(tgt_len)) {
  1727. target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
  1728. tgt_len = msg_controllen - sizeof(struct target_cmsghdr);
  1729. }
  1730. /* We must now copy-and-convert len bytes of payload
  1731. * into tgt_len bytes of destination space. Bear in mind
  1732. * that in both source and destination we may be dealing
  1733. * with a truncated value!
  1734. */
  1735. switch (cmsg->cmsg_level) {
  1736. case SOL_SOCKET:
  1737. switch (cmsg->cmsg_type) {
  1738. case SCM_RIGHTS:
  1739. {
  1740. int *fd = (int *)data;
  1741. int *target_fd = (int *)target_data;
  1742. int i, numfds = tgt_len / sizeof(int);
  1743. for (i = 0; i < numfds; i++) {
  1744. __put_user(fd[i], target_fd + i);
  1745. }
  1746. break;
  1747. }
  1748. case SO_TIMESTAMP:
  1749. {
  1750. struct timeval *tv = (struct timeval *)data;
  1751. struct target_timeval *target_tv =
  1752. (struct target_timeval *)target_data;
  1753. if (len != sizeof(struct timeval) ||
  1754. tgt_len != sizeof(struct target_timeval)) {
  1755. goto unimplemented;
  1756. }
  1757. /* copy struct timeval to target */
  1758. __put_user(tv->tv_sec, &target_tv->tv_sec);
  1759. __put_user(tv->tv_usec, &target_tv->tv_usec);
  1760. break;
  1761. }
  1762. case SCM_CREDENTIALS:
  1763. {
  1764. struct ucred *cred = (struct ucred *)data;
  1765. struct target_ucred *target_cred =
  1766. (struct target_ucred *)target_data;
  1767. __put_user(cred->pid, &target_cred->pid);
  1768. __put_user(cred->uid, &target_cred->uid);
  1769. __put_user(cred->gid, &target_cred->gid);
  1770. break;
  1771. }
  1772. default:
  1773. goto unimplemented;
  1774. }
  1775. break;
  1776. case SOL_IP:
  1777. switch (cmsg->cmsg_type) {
  1778. case IP_TTL:
  1779. {
  1780. uint32_t *v = (uint32_t *)data;
  1781. uint32_t *t_int = (uint32_t *)target_data;
  1782. if (len != sizeof(uint32_t) ||
  1783. tgt_len != sizeof(uint32_t)) {
  1784. goto unimplemented;
  1785. }
  1786. __put_user(*v, t_int);
  1787. break;
  1788. }
  1789. case IP_RECVERR:
  1790. {
  1791. struct errhdr_t {
  1792. struct sock_extended_err ee;
  1793. struct sockaddr_in offender;
  1794. };
  1795. struct errhdr_t *errh = (struct errhdr_t *)data;
  1796. struct errhdr_t *target_errh =
  1797. (struct errhdr_t *)target_data;
  1798. if (len != sizeof(struct errhdr_t) ||
  1799. tgt_len != sizeof(struct errhdr_t)) {
  1800. goto unimplemented;
  1801. }
  1802. __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
  1803. __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
  1804. __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
  1805. __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
  1806. __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
  1807. __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
  1808. __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
  1809. host_to_target_sockaddr((unsigned long) &target_errh->offender,
  1810. (void *) &errh->offender, sizeof(errh->offender));
  1811. break;
  1812. }
  1813. default:
  1814. goto unimplemented;
  1815. }
  1816. break;
  1817. case SOL_IPV6:
  1818. switch (cmsg->cmsg_type) {
  1819. case IPV6_HOPLIMIT:
  1820. {
  1821. uint32_t *v = (uint32_t *)data;
  1822. uint32_t *t_int = (uint32_t *)target_data;
  1823. if (len != sizeof(uint32_t) ||
  1824. tgt_len != sizeof(uint32_t)) {
  1825. goto unimplemented;
  1826. }
  1827. __put_user(*v, t_int);
  1828. break;
  1829. }
  1830. case IPV6_RECVERR:
  1831. {
  1832. struct errhdr6_t {
  1833. struct sock_extended_err ee;
  1834. struct sockaddr_in6 offender;
  1835. };
  1836. struct errhdr6_t *errh = (struct errhdr6_t *)data;
  1837. struct errhdr6_t *target_errh =
  1838. (struct errhdr6_t *)target_data;
  1839. if (len != sizeof(struct errhdr6_t) ||
  1840. tgt_len != sizeof(struct errhdr6_t)) {
  1841. goto unimplemented;
  1842. }
  1843. __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
  1844. __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
  1845. __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
  1846. __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
  1847. __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
  1848. __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
  1849. __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
  1850. host_to_target_sockaddr((unsigned long) &target_errh->offender,
  1851. (void *) &errh->offender, sizeof(errh->offender));
  1852. break;
  1853. }
  1854. default:
  1855. goto unimplemented;
  1856. }
  1857. break;
  1858. default:
  1859. unimplemented:
  1860. qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n",
  1861. cmsg->cmsg_level, cmsg->cmsg_type);
  1862. memcpy(target_data, data, MIN(len, tgt_len));
  1863. if (tgt_len > len) {
  1864. memset(target_data + len, 0, tgt_len - len);
  1865. }
  1866. }
  1867. target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(tgt_len));
  1868. tgt_space = TARGET_CMSG_SPACE(tgt_len);
  1869. if (msg_controllen < tgt_space) {
  1870. tgt_space = msg_controllen;
  1871. }
  1872. msg_controllen -= tgt_space;
  1873. space += tgt_space;
  1874. cmsg = CMSG_NXTHDR(msgh, cmsg);
  1875. target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
  1876. target_cmsg_start);
  1877. }
  1878. unlock_user(target_cmsg, target_cmsg_addr, space);
  1879. the_end:
  1880. target_msgh->msg_controllen = tswapal(space);
  1881. return 0;
  1882. }
  1883. /* do_setsockopt() Must return target values and target errnos. */
  1884. static abi_long do_setsockopt(int sockfd, int level, int optname,
  1885. abi_ulong optval_addr, socklen_t optlen)
  1886. {
  1887. abi_long ret;
  1888. int val;
  1889. struct ip_mreqn *ip_mreq;
  1890. struct ip_mreq_source *ip_mreq_source;
  1891. switch(level) {
  1892. case SOL_TCP:
  1893. case SOL_UDP:
  1894. /* TCP and UDP options all take an 'int' value. */
  1895. if (optlen < sizeof(uint32_t))
  1896. return -TARGET_EINVAL;
  1897. if (get_user_u32(val, optval_addr))
  1898. return -TARGET_EFAULT;
  1899. ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
  1900. break;
  1901. case SOL_IP:
  1902. switch(optname) {
  1903. case IP_TOS:
  1904. case IP_TTL:
  1905. case IP_HDRINCL:
  1906. case IP_ROUTER_ALERT:
  1907. case IP_RECVOPTS:
  1908. case IP_RETOPTS:
  1909. case IP_PKTINFO:
  1910. case IP_MTU_DISCOVER:
  1911. case IP_RECVERR:
  1912. case IP_RECVTTL:
  1913. case IP_RECVTOS:
  1914. #ifdef IP_FREEBIND
  1915. case IP_FREEBIND:
  1916. #endif
  1917. case IP_MULTICAST_TTL:
  1918. case IP_MULTICAST_LOOP:
  1919. val = 0;
  1920. if (optlen >= sizeof(uint32_t)) {
  1921. if (get_user_u32(val, optval_addr))
  1922. return -TARGET_EFAULT;
  1923. } else if (optlen >= 1) {
  1924. if (get_user_u8(val, optval_addr))
  1925. return -TARGET_EFAULT;
  1926. }
  1927. ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
  1928. break;
  1929. case IP_ADD_MEMBERSHIP:
  1930. case IP_DROP_MEMBERSHIP:
  1931. if (optlen < sizeof (struct target_ip_mreq) ||
  1932. optlen > sizeof (struct target_ip_mreqn))
  1933. return -TARGET_EINVAL;
  1934. ip_mreq = (struct ip_mreqn *) alloca(optlen);
  1935. target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
  1936. ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
  1937. break;
  1938. case IP_BLOCK_SOURCE:
  1939. case IP_UNBLOCK_SOURCE:
  1940. case IP_ADD_SOURCE_MEMBERSHIP:
  1941. case IP_DROP_SOURCE_MEMBERSHIP:
  1942. if (optlen != sizeof (struct target_ip_mreq_source))
  1943. return -TARGET_EINVAL;
  1944. ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
  1945. if (!ip_mreq_source) {
  1946. return -TARGET_EFAULT;
  1947. }
  1948. ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
  1949. unlock_user (ip_mreq_source, optval_addr, 0);
  1950. break;
  1951. default:
  1952. goto unimplemented;
  1953. }
  1954. break;
  1955. case SOL_IPV6:
  1956. switch (optname) {
  1957. case IPV6_MTU_DISCOVER:
  1958. case IPV6_MTU:
  1959. case IPV6_V6ONLY:
  1960. case IPV6_RECVPKTINFO:
  1961. case IPV6_UNICAST_HOPS:
  1962. case IPV6_MULTICAST_HOPS:
  1963. case IPV6_MULTICAST_LOOP:
  1964. case IPV6_RECVERR:
  1965. case IPV6_RECVHOPLIMIT:
  1966. case IPV6_2292HOPLIMIT:
  1967. case IPV6_CHECKSUM:
  1968. case IPV6_ADDRFORM:
  1969. case IPV6_2292PKTINFO:
  1970. case IPV6_RECVTCLASS:
  1971. case IPV6_RECVRTHDR:
  1972. case IPV6_2292RTHDR:
  1973. case IPV6_RECVHOPOPTS:
  1974. case IPV6_2292HOPOPTS:
  1975. case IPV6_RECVDSTOPTS:
  1976. case IPV6_2292DSTOPTS:
  1977. case IPV6_TCLASS:
  1978. case IPV6_ADDR_PREFERENCES:
  1979. #ifdef IPV6_RECVPATHMTU
  1980. case IPV6_RECVPATHMTU:
  1981. #endif
  1982. #ifdef IPV6_TRANSPARENT
  1983. case IPV6_TRANSPARENT:
  1984. #endif
  1985. #ifdef IPV6_FREEBIND
  1986. case IPV6_FREEBIND:
  1987. #endif
  1988. #ifdef IPV6_RECVORIGDSTADDR
  1989. case IPV6_RECVORIGDSTADDR:
  1990. #endif
  1991. val = 0;
  1992. if (optlen < sizeof(uint32_t)) {
  1993. return -TARGET_EINVAL;
  1994. }
  1995. if (get_user_u32(val, optval_addr)) {
  1996. return -TARGET_EFAULT;
  1997. }
  1998. ret = get_errno(setsockopt(sockfd, level, optname,
  1999. &val, sizeof(val)));
  2000. break;
  2001. case IPV6_PKTINFO:
  2002. {
  2003. struct in6_pktinfo pki;
  2004. if (optlen < sizeof(pki)) {
  2005. return -TARGET_EINVAL;
  2006. }
  2007. if (copy_from_user(&pki, optval_addr, sizeof(pki))) {
  2008. return -TARGET_EFAULT;
  2009. }
  2010. pki.ipi6_ifindex = tswap32(pki.ipi6_ifindex);
  2011. ret = get_errno(setsockopt(sockfd, level, optname,
  2012. &pki, sizeof(pki)));
  2013. break;
  2014. }
  2015. case IPV6_ADD_MEMBERSHIP:
  2016. case IPV6_DROP_MEMBERSHIP:
  2017. {
  2018. struct ipv6_mreq ipv6mreq;
  2019. if (optlen < sizeof(ipv6mreq)) {
  2020. return -TARGET_EINVAL;
  2021. }
  2022. if (copy_from_user(&ipv6mreq, optval_addr, sizeof(ipv6mreq))) {
  2023. return -TARGET_EFAULT;
  2024. }
  2025. ipv6mreq.ipv6mr_interface = tswap32(ipv6mreq.ipv6mr_interface);
  2026. ret = get_errno(setsockopt(sockfd, level, optname,
  2027. &ipv6mreq, sizeof(ipv6mreq)));
  2028. break;
  2029. }
  2030. default:
  2031. goto unimplemented;
  2032. }
  2033. break;
  2034. case SOL_ICMPV6:
  2035. switch (optname) {
  2036. case ICMPV6_FILTER:
  2037. {
  2038. struct icmp6_filter icmp6f;
  2039. if (optlen > sizeof(icmp6f)) {
  2040. optlen = sizeof(icmp6f);
  2041. }
  2042. if (copy_from_user(&icmp6f, optval_addr, optlen)) {
  2043. return -TARGET_EFAULT;
  2044. }
  2045. for (val = 0; val < 8; val++) {
  2046. icmp6f.data[val] = tswap32(icmp6f.data[val]);
  2047. }
  2048. ret = get_errno(setsockopt(sockfd, level, optname,
  2049. &icmp6f, optlen));
  2050. break;
  2051. }
  2052. default:
  2053. goto unimplemented;
  2054. }
  2055. break;
  2056. case SOL_RAW:
  2057. switch (optname) {
  2058. case ICMP_FILTER:
  2059. case IPV6_CHECKSUM:
  2060. /* those take an u32 value */
  2061. if (optlen < sizeof(uint32_t)) {
  2062. return -TARGET_EINVAL;
  2063. }
  2064. if (get_user_u32(val, optval_addr)) {
  2065. return -TARGET_EFAULT;
  2066. }
  2067. ret = get_errno(setsockopt(sockfd, level, optname,
  2068. &val, sizeof(val)));
  2069. break;
  2070. default:
  2071. goto unimplemented;
  2072. }
  2073. break;
  2074. #if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE)
  2075. case SOL_ALG:
  2076. switch (optname) {
  2077. case ALG_SET_KEY:
  2078. {
  2079. char *alg_key = g_malloc(optlen);
  2080. if (!alg_key) {
  2081. return -TARGET_ENOMEM;
  2082. }
  2083. if (copy_from_user(alg_key, optval_addr, optlen)) {
  2084. g_free(alg_key);
  2085. return -TARGET_EFAULT;
  2086. }
  2087. ret = get_errno(setsockopt(sockfd, level, optname,
  2088. alg_key, optlen));
  2089. g_free(alg_key);
  2090. break;
  2091. }
  2092. case ALG_SET_AEAD_AUTHSIZE:
  2093. {
  2094. ret = get_errno(setsockopt(sockfd, level, optname,
  2095. NULL, optlen));
  2096. break;
  2097. }
  2098. default:
  2099. goto unimplemented;
  2100. }
  2101. break;
  2102. #endif
  2103. case TARGET_SOL_SOCKET:
  2104. switch (optname) {
  2105. case TARGET_SO_RCVTIMEO:
  2106. {
  2107. struct timeval tv;
  2108. optname = SO_RCVTIMEO;
  2109. set_timeout:
  2110. if (optlen != sizeof(struct target_timeval)) {
  2111. return -TARGET_EINVAL;
  2112. }
  2113. if (copy_from_user_timeval(&tv, optval_addr)) {
  2114. return -TARGET_EFAULT;
  2115. }
  2116. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
  2117. &tv, sizeof(tv)));
  2118. return ret;
  2119. }
  2120. case TARGET_SO_SNDTIMEO:
  2121. optname = SO_SNDTIMEO;
  2122. goto set_timeout;
  2123. case TARGET_SO_ATTACH_FILTER:
  2124. {
  2125. struct target_sock_fprog *tfprog;
  2126. struct target_sock_filter *tfilter;
  2127. struct sock_fprog fprog;
  2128. struct sock_filter *filter;
  2129. int i;
  2130. if (optlen != sizeof(*tfprog)) {
  2131. return -TARGET_EINVAL;
  2132. }
  2133. if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
  2134. return -TARGET_EFAULT;
  2135. }
  2136. if (!lock_user_struct(VERIFY_READ, tfilter,
  2137. tswapal(tfprog->filter), 0)) {
  2138. unlock_user_struct(tfprog, optval_addr, 1);
  2139. return -TARGET_EFAULT;
  2140. }
  2141. fprog.len = tswap16(tfprog->len);
  2142. filter = g_try_new(struct sock_filter, fprog.len);
  2143. if (filter == NULL) {
  2144. unlock_user_struct(tfilter, tfprog->filter, 1);
  2145. unlock_user_struct(tfprog, optval_addr, 1);
  2146. return -TARGET_ENOMEM;
  2147. }
  2148. for (i = 0; i < fprog.len; i++) {
  2149. filter[i].code = tswap16(tfilter[i].code);
  2150. filter[i].jt = tfilter[i].jt;
  2151. filter[i].jf = tfilter[i].jf;
  2152. filter[i].k = tswap32(tfilter[i].k);
  2153. }
  2154. fprog.filter = filter;
  2155. ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
  2156. SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
  2157. g_free(filter);
  2158. unlock_user_struct(tfilter, tfprog->filter, 1);
  2159. unlock_user_struct(tfprog, optval_addr, 1);
  2160. return ret;
  2161. }
  2162. case TARGET_SO_BINDTODEVICE:
  2163. {
  2164. char *dev_ifname, *addr_ifname;
  2165. if (optlen > IFNAMSIZ - 1) {
  2166. optlen = IFNAMSIZ - 1;
  2167. }
  2168. dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1);
  2169. if (!dev_ifname) {
  2170. return -TARGET_EFAULT;
  2171. }
  2172. optname = SO_BINDTODEVICE;
  2173. addr_ifname = alloca(IFNAMSIZ);
  2174. memcpy(addr_ifname, dev_ifname, optlen);
  2175. addr_ifname[optlen] = 0;
  2176. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
  2177. addr_ifname, optlen));
  2178. unlock_user (dev_ifname, optval_addr, 0);
  2179. return ret;
  2180. }
  2181. case TARGET_SO_LINGER:
  2182. {
  2183. struct linger lg;
  2184. struct target_linger *tlg;
  2185. if (optlen != sizeof(struct target_linger)) {
  2186. return -TARGET_EINVAL;
  2187. }
  2188. if (!lock_user_struct(VERIFY_READ, tlg, optval_addr, 1)) {
  2189. return -TARGET_EFAULT;
  2190. }
  2191. __get_user(lg.l_onoff, &tlg->l_onoff);
  2192. __get_user(lg.l_linger, &tlg->l_linger);
  2193. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, SO_LINGER,
  2194. &lg, sizeof(lg)));
  2195. unlock_user_struct(tlg, optval_addr, 0);
  2196. return ret;
  2197. }
  2198. /* Options with 'int' argument. */
  2199. case TARGET_SO_DEBUG:
  2200. optname = SO_DEBUG;
  2201. break;
  2202. case TARGET_SO_REUSEADDR:
  2203. optname = SO_REUSEADDR;
  2204. break;
  2205. #ifdef SO_REUSEPORT
  2206. case TARGET_SO_REUSEPORT:
  2207. optname = SO_REUSEPORT;
  2208. break;
  2209. #endif
  2210. case TARGET_SO_TYPE:
  2211. optname = SO_TYPE;
  2212. break;
  2213. case TARGET_SO_ERROR:
  2214. optname = SO_ERROR;
  2215. break;
  2216. case TARGET_SO_DONTROUTE:
  2217. optname = SO_DONTROUTE;
  2218. break;
  2219. case TARGET_SO_BROADCAST:
  2220. optname = SO_BROADCAST;
  2221. break;
  2222. case TARGET_SO_SNDBUF:
  2223. optname = SO_SNDBUF;
  2224. break;
  2225. case TARGET_SO_SNDBUFFORCE:
  2226. optname = SO_SNDBUFFORCE;
  2227. break;
  2228. case TARGET_SO_RCVBUF:
  2229. optname = SO_RCVBUF;
  2230. break;
  2231. case TARGET_SO_RCVBUFFORCE:
  2232. optname = SO_RCVBUFFORCE;
  2233. break;
  2234. case TARGET_SO_KEEPALIVE:
  2235. optname = SO_KEEPALIVE;
  2236. break;
  2237. case TARGET_SO_OOBINLINE:
  2238. optname = SO_OOBINLINE;
  2239. break;
  2240. case TARGET_SO_NO_CHECK:
  2241. optname = SO_NO_CHECK;
  2242. break;
  2243. case TARGET_SO_PRIORITY:
  2244. optname = SO_PRIORITY;
  2245. break;
  2246. #ifdef SO_BSDCOMPAT
  2247. case TARGET_SO_BSDCOMPAT:
  2248. optname = SO_BSDCOMPAT;
  2249. break;
  2250. #endif
  2251. case TARGET_SO_PASSCRED:
  2252. optname = SO_PASSCRED;
  2253. break;
  2254. case TARGET_SO_PASSSEC:
  2255. optname = SO_PASSSEC;
  2256. break;
  2257. case TARGET_SO_TIMESTAMP:
  2258. optname = SO_TIMESTAMP;
  2259. break;
  2260. case TARGET_SO_RCVLOWAT:
  2261. optname = SO_RCVLOWAT;
  2262. break;
  2263. default:
  2264. goto unimplemented;
  2265. }
  2266. if (optlen < sizeof(uint32_t))
  2267. return -TARGET_EINVAL;
  2268. if (get_user_u32(val, optval_addr))
  2269. return -TARGET_EFAULT;
  2270. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
  2271. break;
  2272. #ifdef SOL_NETLINK
  2273. case SOL_NETLINK:
  2274. switch (optname) {
  2275. case NETLINK_PKTINFO:
  2276. case NETLINK_ADD_MEMBERSHIP:
  2277. case NETLINK_DROP_MEMBERSHIP:
  2278. case NETLINK_BROADCAST_ERROR:
  2279. case NETLINK_NO_ENOBUFS:
  2280. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
  2281. case NETLINK_LISTEN_ALL_NSID:
  2282. case NETLINK_CAP_ACK:
  2283. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
  2284. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
  2285. case NETLINK_EXT_ACK:
  2286. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2287. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
  2288. case NETLINK_GET_STRICT_CHK:
  2289. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2290. break;
  2291. default:
  2292. goto unimplemented;
  2293. }
  2294. val = 0;
  2295. if (optlen < sizeof(uint32_t)) {
  2296. return -TARGET_EINVAL;
  2297. }
  2298. if (get_user_u32(val, optval_addr)) {
  2299. return -TARGET_EFAULT;
  2300. }
  2301. ret = get_errno(setsockopt(sockfd, SOL_NETLINK, optname, &val,
  2302. sizeof(val)));
  2303. break;
  2304. #endif /* SOL_NETLINK */
  2305. default:
  2306. unimplemented:
  2307. qemu_log_mask(LOG_UNIMP, "Unsupported setsockopt level=%d optname=%d\n",
  2308. level, optname);
  2309. ret = -TARGET_ENOPROTOOPT;
  2310. }
  2311. return ret;
  2312. }
  2313. /* do_getsockopt() Must return target values and target errnos. */
  2314. static abi_long do_getsockopt(int sockfd, int level, int optname,
  2315. abi_ulong optval_addr, abi_ulong optlen)
  2316. {
  2317. abi_long ret;
  2318. int len, val;
  2319. socklen_t lv;
  2320. switch(level) {
  2321. case TARGET_SOL_SOCKET:
  2322. level = SOL_SOCKET;
  2323. switch (optname) {
  2324. /* These don't just return a single integer */
  2325. case TARGET_SO_PEERNAME:
  2326. goto unimplemented;
  2327. case TARGET_SO_RCVTIMEO: {
  2328. struct timeval tv;
  2329. socklen_t tvlen;
  2330. optname = SO_RCVTIMEO;
  2331. get_timeout:
  2332. if (get_user_u32(len, optlen)) {
  2333. return -TARGET_EFAULT;
  2334. }
  2335. if (len < 0) {
  2336. return -TARGET_EINVAL;
  2337. }
  2338. tvlen = sizeof(tv);
  2339. ret = get_errno(getsockopt(sockfd, level, optname,
  2340. &tv, &tvlen));
  2341. if (ret < 0) {
  2342. return ret;
  2343. }
  2344. if (len > sizeof(struct target_timeval)) {
  2345. len = sizeof(struct target_timeval);
  2346. }
  2347. if (copy_to_user_timeval(optval_addr, &tv)) {
  2348. return -TARGET_EFAULT;
  2349. }
  2350. if (put_user_u32(len, optlen)) {
  2351. return -TARGET_EFAULT;
  2352. }
  2353. break;
  2354. }
  2355. case TARGET_SO_SNDTIMEO:
  2356. optname = SO_SNDTIMEO;
  2357. goto get_timeout;
  2358. case TARGET_SO_PEERCRED: {
  2359. struct ucred cr;
  2360. socklen_t crlen;
  2361. struct target_ucred *tcr;
  2362. if (get_user_u32(len, optlen)) {
  2363. return -TARGET_EFAULT;
  2364. }
  2365. if (len < 0) {
  2366. return -TARGET_EINVAL;
  2367. }
  2368. crlen = sizeof(cr);
  2369. ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
  2370. &cr, &crlen));
  2371. if (ret < 0) {
  2372. return ret;
  2373. }
  2374. if (len > crlen) {
  2375. len = crlen;
  2376. }
  2377. if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
  2378. return -TARGET_EFAULT;
  2379. }
  2380. __put_user(cr.pid, &tcr->pid);
  2381. __put_user(cr.uid, &tcr->uid);
  2382. __put_user(cr.gid, &tcr->gid);
  2383. unlock_user_struct(tcr, optval_addr, 1);
  2384. if (put_user_u32(len, optlen)) {
  2385. return -TARGET_EFAULT;
  2386. }
  2387. break;
  2388. }
  2389. case TARGET_SO_PEERSEC: {
  2390. char *name;
  2391. if (get_user_u32(len, optlen)) {
  2392. return -TARGET_EFAULT;
  2393. }
  2394. if (len < 0) {
  2395. return -TARGET_EINVAL;
  2396. }
  2397. name = lock_user(VERIFY_WRITE, optval_addr, len, 0);
  2398. if (!name) {
  2399. return -TARGET_EFAULT;
  2400. }
  2401. lv = len;
  2402. ret = get_errno(getsockopt(sockfd, level, SO_PEERSEC,
  2403. name, &lv));
  2404. if (put_user_u32(lv, optlen)) {
  2405. ret = -TARGET_EFAULT;
  2406. }
  2407. unlock_user(name, optval_addr, lv);
  2408. break;
  2409. }
  2410. case TARGET_SO_LINGER:
  2411. {
  2412. struct linger lg;
  2413. socklen_t lglen;
  2414. struct target_linger *tlg;
  2415. if (get_user_u32(len, optlen)) {
  2416. return -TARGET_EFAULT;
  2417. }
  2418. if (len < 0) {
  2419. return -TARGET_EINVAL;
  2420. }
  2421. lglen = sizeof(lg);
  2422. ret = get_errno(getsockopt(sockfd, level, SO_LINGER,
  2423. &lg, &lglen));
  2424. if (ret < 0) {
  2425. return ret;
  2426. }
  2427. if (len > lglen) {
  2428. len = lglen;
  2429. }
  2430. if (!lock_user_struct(VERIFY_WRITE, tlg, optval_addr, 0)) {
  2431. return -TARGET_EFAULT;
  2432. }
  2433. __put_user(lg.l_onoff, &tlg->l_onoff);
  2434. __put_user(lg.l_linger, &tlg->l_linger);
  2435. unlock_user_struct(tlg, optval_addr, 1);
  2436. if (put_user_u32(len, optlen)) {
  2437. return -TARGET_EFAULT;
  2438. }
  2439. break;
  2440. }
  2441. /* Options with 'int' argument. */
  2442. case TARGET_SO_DEBUG:
  2443. optname = SO_DEBUG;
  2444. goto int_case;
  2445. case TARGET_SO_REUSEADDR:
  2446. optname = SO_REUSEADDR;
  2447. goto int_case;
  2448. #ifdef SO_REUSEPORT
  2449. case TARGET_SO_REUSEPORT:
  2450. optname = SO_REUSEPORT;
  2451. goto int_case;
  2452. #endif
  2453. case TARGET_SO_TYPE:
  2454. optname = SO_TYPE;
  2455. goto int_case;
  2456. case TARGET_SO_ERROR:
  2457. optname = SO_ERROR;
  2458. goto int_case;
  2459. case TARGET_SO_DONTROUTE:
  2460. optname = SO_DONTROUTE;
  2461. goto int_case;
  2462. case TARGET_SO_BROADCAST:
  2463. optname = SO_BROADCAST;
  2464. goto int_case;
  2465. case TARGET_SO_SNDBUF:
  2466. optname = SO_SNDBUF;
  2467. goto int_case;
  2468. case TARGET_SO_RCVBUF:
  2469. optname = SO_RCVBUF;
  2470. goto int_case;
  2471. case TARGET_SO_KEEPALIVE:
  2472. optname = SO_KEEPALIVE;
  2473. goto int_case;
  2474. case TARGET_SO_OOBINLINE:
  2475. optname = SO_OOBINLINE;
  2476. goto int_case;
  2477. case TARGET_SO_NO_CHECK:
  2478. optname = SO_NO_CHECK;
  2479. goto int_case;
  2480. case TARGET_SO_PRIORITY:
  2481. optname = SO_PRIORITY;
  2482. goto int_case;
  2483. #ifdef SO_BSDCOMPAT
  2484. case TARGET_SO_BSDCOMPAT:
  2485. optname = SO_BSDCOMPAT;
  2486. goto int_case;
  2487. #endif
  2488. case TARGET_SO_PASSCRED:
  2489. optname = SO_PASSCRED;
  2490. goto int_case;
  2491. case TARGET_SO_TIMESTAMP:
  2492. optname = SO_TIMESTAMP;
  2493. goto int_case;
  2494. case TARGET_SO_RCVLOWAT:
  2495. optname = SO_RCVLOWAT;
  2496. goto int_case;
  2497. case TARGET_SO_ACCEPTCONN:
  2498. optname = SO_ACCEPTCONN;
  2499. goto int_case;
  2500. case TARGET_SO_PROTOCOL:
  2501. optname = SO_PROTOCOL;
  2502. goto int_case;
  2503. case TARGET_SO_DOMAIN:
  2504. optname = SO_DOMAIN;
  2505. goto int_case;
  2506. default:
  2507. goto int_case;
  2508. }
  2509. break;
  2510. case SOL_TCP:
  2511. case SOL_UDP:
  2512. /* TCP and UDP options all take an 'int' value. */
  2513. int_case:
  2514. if (get_user_u32(len, optlen))
  2515. return -TARGET_EFAULT;
  2516. if (len < 0)
  2517. return -TARGET_EINVAL;
  2518. lv = sizeof(lv);
  2519. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2520. if (ret < 0)
  2521. return ret;
  2522. if (optname == SO_TYPE) {
  2523. val = host_to_target_sock_type(val);
  2524. }
  2525. if (len > lv)
  2526. len = lv;
  2527. if (len == 4) {
  2528. if (put_user_u32(val, optval_addr))
  2529. return -TARGET_EFAULT;
  2530. } else {
  2531. if (put_user_u8(val, optval_addr))
  2532. return -TARGET_EFAULT;
  2533. }
  2534. if (put_user_u32(len, optlen))
  2535. return -TARGET_EFAULT;
  2536. break;
  2537. case SOL_IP:
  2538. switch(optname) {
  2539. case IP_TOS:
  2540. case IP_TTL:
  2541. case IP_HDRINCL:
  2542. case IP_ROUTER_ALERT:
  2543. case IP_RECVOPTS:
  2544. case IP_RETOPTS:
  2545. case IP_PKTINFO:
  2546. case IP_MTU_DISCOVER:
  2547. case IP_RECVERR:
  2548. case IP_RECVTOS:
  2549. #ifdef IP_FREEBIND
  2550. case IP_FREEBIND:
  2551. #endif
  2552. case IP_MULTICAST_TTL:
  2553. case IP_MULTICAST_LOOP:
  2554. if (get_user_u32(len, optlen))
  2555. return -TARGET_EFAULT;
  2556. if (len < 0)
  2557. return -TARGET_EINVAL;
  2558. lv = sizeof(lv);
  2559. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2560. if (ret < 0)
  2561. return ret;
  2562. if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
  2563. len = 1;
  2564. if (put_user_u32(len, optlen)
  2565. || put_user_u8(val, optval_addr))
  2566. return -TARGET_EFAULT;
  2567. } else {
  2568. if (len > sizeof(int))
  2569. len = sizeof(int);
  2570. if (put_user_u32(len, optlen)
  2571. || put_user_u32(val, optval_addr))
  2572. return -TARGET_EFAULT;
  2573. }
  2574. break;
  2575. default:
  2576. ret = -TARGET_ENOPROTOOPT;
  2577. break;
  2578. }
  2579. break;
  2580. case SOL_IPV6:
  2581. switch (optname) {
  2582. case IPV6_MTU_DISCOVER:
  2583. case IPV6_MTU:
  2584. case IPV6_V6ONLY:
  2585. case IPV6_RECVPKTINFO:
  2586. case IPV6_UNICAST_HOPS:
  2587. case IPV6_MULTICAST_HOPS:
  2588. case IPV6_MULTICAST_LOOP:
  2589. case IPV6_RECVERR:
  2590. case IPV6_RECVHOPLIMIT:
  2591. case IPV6_2292HOPLIMIT:
  2592. case IPV6_CHECKSUM:
  2593. case IPV6_ADDRFORM:
  2594. case IPV6_2292PKTINFO:
  2595. case IPV6_RECVTCLASS:
  2596. case IPV6_RECVRTHDR:
  2597. case IPV6_2292RTHDR:
  2598. case IPV6_RECVHOPOPTS:
  2599. case IPV6_2292HOPOPTS:
  2600. case IPV6_RECVDSTOPTS:
  2601. case IPV6_2292DSTOPTS:
  2602. case IPV6_TCLASS:
  2603. case IPV6_ADDR_PREFERENCES:
  2604. #ifdef IPV6_RECVPATHMTU
  2605. case IPV6_RECVPATHMTU:
  2606. #endif
  2607. #ifdef IPV6_TRANSPARENT
  2608. case IPV6_TRANSPARENT:
  2609. #endif
  2610. #ifdef IPV6_FREEBIND
  2611. case IPV6_FREEBIND:
  2612. #endif
  2613. #ifdef IPV6_RECVORIGDSTADDR
  2614. case IPV6_RECVORIGDSTADDR:
  2615. #endif
  2616. if (get_user_u32(len, optlen))
  2617. return -TARGET_EFAULT;
  2618. if (len < 0)
  2619. return -TARGET_EINVAL;
  2620. lv = sizeof(lv);
  2621. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2622. if (ret < 0)
  2623. return ret;
  2624. if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
  2625. len = 1;
  2626. if (put_user_u32(len, optlen)
  2627. || put_user_u8(val, optval_addr))
  2628. return -TARGET_EFAULT;
  2629. } else {
  2630. if (len > sizeof(int))
  2631. len = sizeof(int);
  2632. if (put_user_u32(len, optlen)
  2633. || put_user_u32(val, optval_addr))
  2634. return -TARGET_EFAULT;
  2635. }
  2636. break;
  2637. default:
  2638. ret = -TARGET_ENOPROTOOPT;
  2639. break;
  2640. }
  2641. break;
  2642. #ifdef SOL_NETLINK
  2643. case SOL_NETLINK:
  2644. switch (optname) {
  2645. case NETLINK_PKTINFO:
  2646. case NETLINK_BROADCAST_ERROR:
  2647. case NETLINK_NO_ENOBUFS:
  2648. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
  2649. case NETLINK_LISTEN_ALL_NSID:
  2650. case NETLINK_CAP_ACK:
  2651. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
  2652. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
  2653. case NETLINK_EXT_ACK:
  2654. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2655. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
  2656. case NETLINK_GET_STRICT_CHK:
  2657. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2658. if (get_user_u32(len, optlen)) {
  2659. return -TARGET_EFAULT;
  2660. }
  2661. if (len != sizeof(val)) {
  2662. return -TARGET_EINVAL;
  2663. }
  2664. lv = len;
  2665. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2666. if (ret < 0) {
  2667. return ret;
  2668. }
  2669. if (put_user_u32(lv, optlen)
  2670. || put_user_u32(val, optval_addr)) {
  2671. return -TARGET_EFAULT;
  2672. }
  2673. break;
  2674. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
  2675. case NETLINK_LIST_MEMBERSHIPS:
  2676. {
  2677. uint32_t *results;
  2678. int i;
  2679. if (get_user_u32(len, optlen)) {
  2680. return -TARGET_EFAULT;
  2681. }
  2682. if (len < 0) {
  2683. return -TARGET_EINVAL;
  2684. }
  2685. results = lock_user(VERIFY_WRITE, optval_addr, len, 1);
  2686. if (!results && len > 0) {
  2687. return -TARGET_EFAULT;
  2688. }
  2689. lv = len;
  2690. ret = get_errno(getsockopt(sockfd, level, optname, results, &lv));
  2691. if (ret < 0) {
  2692. unlock_user(results, optval_addr, 0);
  2693. return ret;
  2694. }
  2695. /* swap host endianess to target endianess. */
  2696. for (i = 0; i < (len / sizeof(uint32_t)); i++) {
  2697. results[i] = tswap32(results[i]);
  2698. }
  2699. if (put_user_u32(lv, optlen)) {
  2700. return -TARGET_EFAULT;
  2701. }
  2702. unlock_user(results, optval_addr, 0);
  2703. break;
  2704. }
  2705. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
  2706. default:
  2707. goto unimplemented;
  2708. }
  2709. break;
  2710. #endif /* SOL_NETLINK */
  2711. default:
  2712. unimplemented:
  2713. qemu_log_mask(LOG_UNIMP,
  2714. "getsockopt level=%d optname=%d not yet supported\n",
  2715. level, optname);
  2716. ret = -TARGET_EOPNOTSUPP;
  2717. break;
  2718. }
  2719. return ret;
  2720. }
  2721. /* Convert target low/high pair representing file offset into the host
  2722. * low/high pair. This function doesn't handle offsets bigger than 64 bits
  2723. * as the kernel doesn't handle them either.
  2724. */
  2725. static void target_to_host_low_high(abi_ulong tlow,
  2726. abi_ulong thigh,
  2727. unsigned long *hlow,
  2728. unsigned long *hhigh)
  2729. {
  2730. uint64_t off = tlow |
  2731. ((unsigned long long)thigh << TARGET_LONG_BITS / 2) <<
  2732. TARGET_LONG_BITS / 2;
  2733. *hlow = off;
  2734. *hhigh = (off >> HOST_LONG_BITS / 2) >> HOST_LONG_BITS / 2;
  2735. }
  2736. static struct iovec *lock_iovec(int type, abi_ulong target_addr,
  2737. abi_ulong count, int copy)
  2738. {
  2739. struct target_iovec *target_vec;
  2740. struct iovec *vec;
  2741. abi_ulong total_len, max_len;
  2742. int i;
  2743. int err = 0;
  2744. bool bad_address = false;
  2745. if (count == 0) {
  2746. errno = 0;
  2747. return NULL;
  2748. }
  2749. if (count > IOV_MAX) {
  2750. errno = EINVAL;
  2751. return NULL;
  2752. }
  2753. vec = g_try_new0(struct iovec, count);
  2754. if (vec == NULL) {
  2755. errno = ENOMEM;
  2756. return NULL;
  2757. }
  2758. target_vec = lock_user(VERIFY_READ, target_addr,
  2759. count * sizeof(struct target_iovec), 1);
  2760. if (target_vec == NULL) {
  2761. err = EFAULT;
  2762. goto fail2;
  2763. }
  2764. /* ??? If host page size > target page size, this will result in a
  2765. value larger than what we can actually support. */
  2766. max_len = 0x7fffffff & TARGET_PAGE_MASK;
  2767. total_len = 0;
  2768. for (i = 0; i < count; i++) {
  2769. abi_ulong base = tswapal(target_vec[i].iov_base);
  2770. abi_long len = tswapal(target_vec[i].iov_len);
  2771. if (len < 0) {
  2772. err = EINVAL;
  2773. goto fail;
  2774. } else if (len == 0) {
  2775. /* Zero length pointer is ignored. */
  2776. vec[i].iov_base = 0;
  2777. } else {
  2778. vec[i].iov_base = lock_user(type, base, len, copy);
  2779. /* If the first buffer pointer is bad, this is a fault. But
  2780. * subsequent bad buffers will result in a partial write; this
  2781. * is realized by filling the vector with null pointers and
  2782. * zero lengths. */
  2783. if (!vec[i].iov_base) {
  2784. if (i == 0) {
  2785. err = EFAULT;
  2786. goto fail;
  2787. } else {
  2788. bad_address = true;
  2789. }
  2790. }
  2791. if (bad_address) {
  2792. len = 0;
  2793. }
  2794. if (len > max_len - total_len) {
  2795. len = max_len - total_len;
  2796. }
  2797. }
  2798. vec[i].iov_len = len;
  2799. total_len += len;
  2800. }
  2801. unlock_user(target_vec, target_addr, 0);
  2802. return vec;
  2803. fail:
  2804. while (--i >= 0) {
  2805. if (tswapal(target_vec[i].iov_len) > 0) {
  2806. unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0);
  2807. }
  2808. }
  2809. unlock_user(target_vec, target_addr, 0);
  2810. fail2:
  2811. g_free(vec);
  2812. errno = err;
  2813. return NULL;
  2814. }
  2815. static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
  2816. abi_ulong count, int copy)
  2817. {
  2818. struct target_iovec *target_vec;
  2819. int i;
  2820. target_vec = lock_user(VERIFY_READ, target_addr,
  2821. count * sizeof(struct target_iovec), 1);
  2822. if (target_vec) {
  2823. for (i = 0; i < count; i++) {
  2824. abi_ulong base = tswapal(target_vec[i].iov_base);
  2825. abi_long len = tswapal(target_vec[i].iov_len);
  2826. if (len < 0) {
  2827. break;
  2828. }
  2829. unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
  2830. }
  2831. unlock_user(target_vec, target_addr, 0);
  2832. }
  2833. g_free(vec);
  2834. }
  2835. static inline int target_to_host_sock_type(int *type)
  2836. {
  2837. int host_type = 0;
  2838. int target_type = *type;
  2839. switch (target_type & TARGET_SOCK_TYPE_MASK) {
  2840. case TARGET_SOCK_DGRAM:
  2841. host_type = SOCK_DGRAM;
  2842. break;
  2843. case TARGET_SOCK_STREAM:
  2844. host_type = SOCK_STREAM;
  2845. break;
  2846. default:
  2847. host_type = target_type & TARGET_SOCK_TYPE_MASK;
  2848. break;
  2849. }
  2850. if (target_type & TARGET_SOCK_CLOEXEC) {
  2851. #if defined(SOCK_CLOEXEC)
  2852. host_type |= SOCK_CLOEXEC;
  2853. #else
  2854. return -TARGET_EINVAL;
  2855. #endif
  2856. }
  2857. if (target_type & TARGET_SOCK_NONBLOCK) {
  2858. #if defined(SOCK_NONBLOCK)
  2859. host_type |= SOCK_NONBLOCK;
  2860. #elif !defined(O_NONBLOCK)
  2861. return -TARGET_EINVAL;
  2862. #endif
  2863. }
  2864. *type = host_type;
  2865. return 0;
  2866. }
  2867. /* Try to emulate socket type flags after socket creation. */
  2868. static int sock_flags_fixup(int fd, int target_type)
  2869. {
  2870. #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
  2871. if (target_type & TARGET_SOCK_NONBLOCK) {
  2872. int flags = fcntl(fd, F_GETFL);
  2873. if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) {
  2874. close(fd);
  2875. return -TARGET_EINVAL;
  2876. }
  2877. }
  2878. #endif
  2879. return fd;
  2880. }
  2881. /* do_socket() Must return target values and target errnos. */
  2882. static abi_long do_socket(int domain, int type, int protocol)
  2883. {
  2884. int target_type = type;
  2885. int ret;
  2886. ret = target_to_host_sock_type(&type);
  2887. if (ret) {
  2888. return ret;
  2889. }
  2890. if (domain == PF_NETLINK && !(
  2891. #ifdef CONFIG_RTNETLINK
  2892. protocol == NETLINK_ROUTE ||
  2893. #endif
  2894. protocol == NETLINK_KOBJECT_UEVENT ||
  2895. protocol == NETLINK_AUDIT)) {
  2896. return -TARGET_EPROTONOSUPPORT;
  2897. }
  2898. if (domain == AF_PACKET ||
  2899. (domain == AF_INET && type == SOCK_PACKET)) {
  2900. protocol = tswap16(protocol);
  2901. }
  2902. ret = get_errno(socket(domain, type, protocol));
  2903. if (ret >= 0) {
  2904. ret = sock_flags_fixup(ret, target_type);
  2905. if (type == SOCK_PACKET) {
  2906. /* Manage an obsolete case :
  2907. * if socket type is SOCK_PACKET, bind by name
  2908. */
  2909. fd_trans_register(ret, &target_packet_trans);
  2910. } else if (domain == PF_NETLINK) {
  2911. switch (protocol) {
  2912. #ifdef CONFIG_RTNETLINK
  2913. case NETLINK_ROUTE:
  2914. fd_trans_register(ret, &target_netlink_route_trans);
  2915. break;
  2916. #endif
  2917. case NETLINK_KOBJECT_UEVENT:
  2918. /* nothing to do: messages are strings */
  2919. break;
  2920. case NETLINK_AUDIT:
  2921. fd_trans_register(ret, &target_netlink_audit_trans);
  2922. break;
  2923. default:
  2924. g_assert_not_reached();
  2925. }
  2926. }
  2927. }
  2928. return ret;
  2929. }
  2930. /* do_bind() Must return target values and target errnos. */
  2931. static abi_long do_bind(int sockfd, abi_ulong target_addr,
  2932. socklen_t addrlen)
  2933. {
  2934. void *addr;
  2935. abi_long ret;
  2936. if ((int)addrlen < 0) {
  2937. return -TARGET_EINVAL;
  2938. }
  2939. addr = alloca(addrlen+1);
  2940. ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
  2941. if (ret)
  2942. return ret;
  2943. return get_errno(bind(sockfd, addr, addrlen));
  2944. }
  2945. /* do_connect() Must return target values and target errnos. */
  2946. static abi_long do_connect(int sockfd, abi_ulong target_addr,
  2947. socklen_t addrlen)
  2948. {
  2949. void *addr;
  2950. abi_long ret;
  2951. if ((int)addrlen < 0) {
  2952. return -TARGET_EINVAL;
  2953. }
  2954. addr = alloca(addrlen+1);
  2955. ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
  2956. if (ret)
  2957. return ret;
  2958. return get_errno(safe_connect(sockfd, addr, addrlen));
  2959. }
  2960. /* do_sendrecvmsg_locked() Must return target values and target errnos. */
  2961. static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
  2962. int flags, int send)
  2963. {
  2964. abi_long ret, len;
  2965. struct msghdr msg;
  2966. abi_ulong count;
  2967. struct iovec *vec;
  2968. abi_ulong target_vec;
  2969. if (msgp->msg_name) {
  2970. msg.msg_namelen = tswap32(msgp->msg_namelen);
  2971. msg.msg_name = alloca(msg.msg_namelen+1);
  2972. ret = target_to_host_sockaddr(fd, msg.msg_name,
  2973. tswapal(msgp->msg_name),
  2974. msg.msg_namelen);
  2975. if (ret == -TARGET_EFAULT) {
  2976. /* For connected sockets msg_name and msg_namelen must
  2977. * be ignored, so returning EFAULT immediately is wrong.
  2978. * Instead, pass a bad msg_name to the host kernel, and
  2979. * let it decide whether to return EFAULT or not.
  2980. */
  2981. msg.msg_name = (void *)-1;
  2982. } else if (ret) {
  2983. goto out2;
  2984. }
  2985. } else {
  2986. msg.msg_name = NULL;
  2987. msg.msg_namelen = 0;
  2988. }
  2989. msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
  2990. msg.msg_control = alloca(msg.msg_controllen);
  2991. memset(msg.msg_control, 0, msg.msg_controllen);
  2992. msg.msg_flags = tswap32(msgp->msg_flags);
  2993. count = tswapal(msgp->msg_iovlen);
  2994. target_vec = tswapal(msgp->msg_iov);
  2995. if (count > IOV_MAX) {
  2996. /* sendrcvmsg returns a different errno for this condition than
  2997. * readv/writev, so we must catch it here before lock_iovec() does.
  2998. */
  2999. ret = -TARGET_EMSGSIZE;
  3000. goto out2;
  3001. }
  3002. vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
  3003. target_vec, count, send);
  3004. if (vec == NULL) {
  3005. ret = -host_to_target_errno(errno);
  3006. goto out2;
  3007. }
  3008. msg.msg_iovlen = count;
  3009. msg.msg_iov = vec;
  3010. if (send) {
  3011. if (fd_trans_target_to_host_data(fd)) {
  3012. void *host_msg;
  3013. host_msg = g_malloc(msg.msg_iov->iov_len);
  3014. memcpy(host_msg, msg.msg_iov->iov_base, msg.msg_iov->iov_len);
  3015. ret = fd_trans_target_to_host_data(fd)(host_msg,
  3016. msg.msg_iov->iov_len);
  3017. if (ret >= 0) {
  3018. msg.msg_iov->iov_base = host_msg;
  3019. ret = get_errno(safe_sendmsg(fd, &msg, flags));
  3020. }
  3021. g_free(host_msg);
  3022. } else {
  3023. ret = target_to_host_cmsg(&msg, msgp);
  3024. if (ret == 0) {
  3025. ret = get_errno(safe_sendmsg(fd, &msg, flags));
  3026. }
  3027. }
  3028. } else {
  3029. ret = get_errno(safe_recvmsg(fd, &msg, flags));
  3030. if (!is_error(ret)) {
  3031. len = ret;
  3032. if (fd_trans_host_to_target_data(fd)) {
  3033. ret = fd_trans_host_to_target_data(fd)(msg.msg_iov->iov_base,
  3034. MIN(msg.msg_iov->iov_len, len));
  3035. }
  3036. if (!is_error(ret)) {
  3037. ret = host_to_target_cmsg(msgp, &msg);
  3038. }
  3039. if (!is_error(ret)) {
  3040. msgp->msg_namelen = tswap32(msg.msg_namelen);
  3041. msgp->msg_flags = tswap32(msg.msg_flags);
  3042. if (msg.msg_name != NULL && msg.msg_name != (void *)-1) {
  3043. ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
  3044. msg.msg_name, msg.msg_namelen);
  3045. if (ret) {
  3046. goto out;
  3047. }
  3048. }
  3049. ret = len;
  3050. }
  3051. }
  3052. }
  3053. out:
  3054. unlock_iovec(vec, target_vec, count, !send);
  3055. out2:
  3056. return ret;
  3057. }
  3058. static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
  3059. int flags, int send)
  3060. {
  3061. abi_long ret;
  3062. struct target_msghdr *msgp;
  3063. if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
  3064. msgp,
  3065. target_msg,
  3066. send ? 1 : 0)) {
  3067. return -TARGET_EFAULT;
  3068. }
  3069. ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
  3070. unlock_user_struct(msgp, target_msg, send ? 0 : 1);
  3071. return ret;
  3072. }
  3073. /* We don't rely on the C library to have sendmmsg/recvmmsg support,
  3074. * so it might not have this *mmsg-specific flag either.
  3075. */
  3076. #ifndef MSG_WAITFORONE
  3077. #define MSG_WAITFORONE 0x10000
  3078. #endif
  3079. static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec,
  3080. unsigned int vlen, unsigned int flags,
  3081. int send)
  3082. {
  3083. struct target_mmsghdr *mmsgp;
  3084. abi_long ret = 0;
  3085. int i;
  3086. if (vlen > UIO_MAXIOV) {
  3087. vlen = UIO_MAXIOV;
  3088. }
  3089. mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1);
  3090. if (!mmsgp) {
  3091. return -TARGET_EFAULT;
  3092. }
  3093. for (i = 0; i < vlen; i++) {
  3094. ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send);
  3095. if (is_error(ret)) {
  3096. break;
  3097. }
  3098. mmsgp[i].msg_len = tswap32(ret);
  3099. /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
  3100. if (flags & MSG_WAITFORONE) {
  3101. flags |= MSG_DONTWAIT;
  3102. }
  3103. }
  3104. unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i);
  3105. /* Return number of datagrams sent if we sent any at all;
  3106. * otherwise return the error.
  3107. */
  3108. if (i) {
  3109. return i;
  3110. }
  3111. return ret;
  3112. }
  3113. /* do_accept4() Must return target values and target errnos. */
  3114. static abi_long do_accept4(int fd, abi_ulong target_addr,
  3115. abi_ulong target_addrlen_addr, int flags)
  3116. {
  3117. socklen_t addrlen, ret_addrlen;
  3118. void *addr;
  3119. abi_long ret;
  3120. int host_flags;
  3121. host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
  3122. if (target_addr == 0) {
  3123. return get_errno(safe_accept4(fd, NULL, NULL, host_flags));
  3124. }
  3125. /* linux returns EFAULT if addrlen pointer is invalid */
  3126. if (get_user_u32(addrlen, target_addrlen_addr))
  3127. return -TARGET_EFAULT;
  3128. if ((int)addrlen < 0) {
  3129. return -TARGET_EINVAL;
  3130. }
  3131. if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
  3132. return -TARGET_EFAULT;
  3133. }
  3134. addr = alloca(addrlen);
  3135. ret_addrlen = addrlen;
  3136. ret = get_errno(safe_accept4(fd, addr, &ret_addrlen, host_flags));
  3137. if (!is_error(ret)) {
  3138. host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
  3139. if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
  3140. ret = -TARGET_EFAULT;
  3141. }
  3142. }
  3143. return ret;
  3144. }
  3145. /* do_getpeername() Must return target values and target errnos. */
  3146. static abi_long do_getpeername(int fd, abi_ulong target_addr,
  3147. abi_ulong target_addrlen_addr)
  3148. {
  3149. socklen_t addrlen, ret_addrlen;
  3150. void *addr;
  3151. abi_long ret;
  3152. if (get_user_u32(addrlen, target_addrlen_addr))
  3153. return -TARGET_EFAULT;
  3154. if ((int)addrlen < 0) {
  3155. return -TARGET_EINVAL;
  3156. }
  3157. if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
  3158. return -TARGET_EFAULT;
  3159. }
  3160. addr = alloca(addrlen);
  3161. ret_addrlen = addrlen;
  3162. ret = get_errno(getpeername(fd, addr, &ret_addrlen));
  3163. if (!is_error(ret)) {
  3164. host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
  3165. if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
  3166. ret = -TARGET_EFAULT;
  3167. }
  3168. }
  3169. return ret;
  3170. }
  3171. /* do_getsockname() Must return target values and target errnos. */
  3172. static abi_long do_getsockname(int fd, abi_ulong target_addr,
  3173. abi_ulong target_addrlen_addr)
  3174. {
  3175. socklen_t addrlen, ret_addrlen;
  3176. void *addr;
  3177. abi_long ret;
  3178. if (get_user_u32(addrlen, target_addrlen_addr))
  3179. return -TARGET_EFAULT;
  3180. if ((int)addrlen < 0) {
  3181. return -TARGET_EINVAL;
  3182. }
  3183. if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
  3184. return -TARGET_EFAULT;
  3185. }
  3186. addr = alloca(addrlen);
  3187. ret_addrlen = addrlen;
  3188. ret = get_errno(getsockname(fd, addr, &ret_addrlen));
  3189. if (!is_error(ret)) {
  3190. host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
  3191. if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
  3192. ret = -TARGET_EFAULT;
  3193. }
  3194. }
  3195. return ret;
  3196. }
  3197. /* do_socketpair() Must return target values and target errnos. */
  3198. static abi_long do_socketpair(int domain, int type, int protocol,
  3199. abi_ulong target_tab_addr)
  3200. {
  3201. int tab[2];
  3202. abi_long ret;
  3203. target_to_host_sock_type(&type);
  3204. ret = get_errno(socketpair(domain, type, protocol, tab));
  3205. if (!is_error(ret)) {
  3206. if (put_user_s32(tab[0], target_tab_addr)
  3207. || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
  3208. ret = -TARGET_EFAULT;
  3209. }
  3210. return ret;
  3211. }
  3212. /* do_sendto() Must return target values and target errnos. */
  3213. static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
  3214. abi_ulong target_addr, socklen_t addrlen)
  3215. {
  3216. void *addr;
  3217. void *host_msg;
  3218. void *copy_msg = NULL;
  3219. abi_long ret;
  3220. if ((int)addrlen < 0) {
  3221. return -TARGET_EINVAL;
  3222. }
  3223. host_msg = lock_user(VERIFY_READ, msg, len, 1);
  3224. if (!host_msg)
  3225. return -TARGET_EFAULT;
  3226. if (fd_trans_target_to_host_data(fd)) {
  3227. copy_msg = host_msg;
  3228. host_msg = g_malloc(len);
  3229. memcpy(host_msg, copy_msg, len);
  3230. ret = fd_trans_target_to_host_data(fd)(host_msg, len);
  3231. if (ret < 0) {
  3232. goto fail;
  3233. }
  3234. }
  3235. if (target_addr) {
  3236. addr = alloca(addrlen+1);
  3237. ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen);
  3238. if (ret) {
  3239. goto fail;
  3240. }
  3241. ret = get_errno(safe_sendto(fd, host_msg, len, flags, addr, addrlen));
  3242. } else {
  3243. ret = get_errno(safe_sendto(fd, host_msg, len, flags, NULL, 0));
  3244. }
  3245. fail:
  3246. if (copy_msg) {
  3247. g_free(host_msg);
  3248. host_msg = copy_msg;
  3249. }
  3250. unlock_user(host_msg, msg, 0);
  3251. return ret;
  3252. }
  3253. /* do_recvfrom() Must return target values and target errnos. */
  3254. static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
  3255. abi_ulong target_addr,
  3256. abi_ulong target_addrlen)
  3257. {
  3258. socklen_t addrlen, ret_addrlen;
  3259. void *addr;
  3260. void *host_msg;
  3261. abi_long ret;
  3262. if (!msg) {
  3263. host_msg = NULL;
  3264. } else {
  3265. host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
  3266. if (!host_msg) {
  3267. return -TARGET_EFAULT;
  3268. }
  3269. }
  3270. if (target_addr) {
  3271. if (get_user_u32(addrlen, target_addrlen)) {
  3272. ret = -TARGET_EFAULT;
  3273. goto fail;
  3274. }
  3275. if ((int)addrlen < 0) {
  3276. ret = -TARGET_EINVAL;
  3277. goto fail;
  3278. }
  3279. addr = alloca(addrlen);
  3280. ret_addrlen = addrlen;
  3281. ret = get_errno(safe_recvfrom(fd, host_msg, len, flags,
  3282. addr, &ret_addrlen));
  3283. } else {
  3284. addr = NULL; /* To keep compiler quiet. */
  3285. addrlen = 0; /* To keep compiler quiet. */
  3286. ret = get_errno(safe_recvfrom(fd, host_msg, len, flags, NULL, 0));
  3287. }
  3288. if (!is_error(ret)) {
  3289. if (fd_trans_host_to_target_data(fd)) {
  3290. abi_long trans;
  3291. trans = fd_trans_host_to_target_data(fd)(host_msg, MIN(ret, len));
  3292. if (is_error(trans)) {
  3293. ret = trans;
  3294. goto fail;
  3295. }
  3296. }
  3297. if (target_addr) {
  3298. host_to_target_sockaddr(target_addr, addr,
  3299. MIN(addrlen, ret_addrlen));
  3300. if (put_user_u32(ret_addrlen, target_addrlen)) {
  3301. ret = -TARGET_EFAULT;
  3302. goto fail;
  3303. }
  3304. }
  3305. unlock_user(host_msg, msg, len);
  3306. } else {
  3307. fail:
  3308. unlock_user(host_msg, msg, 0);
  3309. }
  3310. return ret;
  3311. }
  3312. #ifdef TARGET_NR_socketcall
  3313. /* do_socketcall() must return target values and target errnos. */
  3314. static abi_long do_socketcall(int num, abi_ulong vptr)
  3315. {
  3316. static const unsigned nargs[] = { /* number of arguments per operation */
  3317. [TARGET_SYS_SOCKET] = 3, /* domain, type, protocol */
  3318. [TARGET_SYS_BIND] = 3, /* fd, addr, addrlen */
  3319. [TARGET_SYS_CONNECT] = 3, /* fd, addr, addrlen */
  3320. [TARGET_SYS_LISTEN] = 2, /* fd, backlog */
  3321. [TARGET_SYS_ACCEPT] = 3, /* fd, addr, addrlen */
  3322. [TARGET_SYS_GETSOCKNAME] = 3, /* fd, addr, addrlen */
  3323. [TARGET_SYS_GETPEERNAME] = 3, /* fd, addr, addrlen */
  3324. [TARGET_SYS_SOCKETPAIR] = 4, /* domain, type, protocol, tab */
  3325. [TARGET_SYS_SEND] = 4, /* fd, msg, len, flags */
  3326. [TARGET_SYS_RECV] = 4, /* fd, msg, len, flags */
  3327. [TARGET_SYS_SENDTO] = 6, /* fd, msg, len, flags, addr, addrlen */
  3328. [TARGET_SYS_RECVFROM] = 6, /* fd, msg, len, flags, addr, addrlen */
  3329. [TARGET_SYS_SHUTDOWN] = 2, /* fd, how */
  3330. [TARGET_SYS_SETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
  3331. [TARGET_SYS_GETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
  3332. [TARGET_SYS_SENDMSG] = 3, /* fd, msg, flags */
  3333. [TARGET_SYS_RECVMSG] = 3, /* fd, msg, flags */
  3334. [TARGET_SYS_ACCEPT4] = 4, /* fd, addr, addrlen, flags */
  3335. [TARGET_SYS_RECVMMSG] = 4, /* fd, msgvec, vlen, flags */
  3336. [TARGET_SYS_SENDMMSG] = 4, /* fd, msgvec, vlen, flags */
  3337. };
  3338. abi_long a[6]; /* max 6 args */
  3339. unsigned i;
  3340. /* check the range of the first argument num */
  3341. /* (TARGET_SYS_SENDMMSG is the highest among TARGET_SYS_xxx) */
  3342. if (num < 1 || num > TARGET_SYS_SENDMMSG) {
  3343. return -TARGET_EINVAL;
  3344. }
  3345. /* ensure we have space for args */
  3346. if (nargs[num] > ARRAY_SIZE(a)) {
  3347. return -TARGET_EINVAL;
  3348. }
  3349. /* collect the arguments in a[] according to nargs[] */
  3350. for (i = 0; i < nargs[num]; ++i) {
  3351. if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) {
  3352. return -TARGET_EFAULT;
  3353. }
  3354. }
  3355. /* now when we have the args, invoke the appropriate underlying function */
  3356. switch (num) {
  3357. case TARGET_SYS_SOCKET: /* domain, type, protocol */
  3358. return do_socket(a[0], a[1], a[2]);
  3359. case TARGET_SYS_BIND: /* sockfd, addr, addrlen */
  3360. return do_bind(a[0], a[1], a[2]);
  3361. case TARGET_SYS_CONNECT: /* sockfd, addr, addrlen */
  3362. return do_connect(a[0], a[1], a[2]);
  3363. case TARGET_SYS_LISTEN: /* sockfd, backlog */
  3364. return get_errno(listen(a[0], a[1]));
  3365. case TARGET_SYS_ACCEPT: /* sockfd, addr, addrlen */
  3366. return do_accept4(a[0], a[1], a[2], 0);
  3367. case TARGET_SYS_GETSOCKNAME: /* sockfd, addr, addrlen */
  3368. return do_getsockname(a[0], a[1], a[2]);
  3369. case TARGET_SYS_GETPEERNAME: /* sockfd, addr, addrlen */
  3370. return do_getpeername(a[0], a[1], a[2]);
  3371. case TARGET_SYS_SOCKETPAIR: /* domain, type, protocol, tab */
  3372. return do_socketpair(a[0], a[1], a[2], a[3]);
  3373. case TARGET_SYS_SEND: /* sockfd, msg, len, flags */
  3374. return do_sendto(a[0], a[1], a[2], a[3], 0, 0);
  3375. case TARGET_SYS_RECV: /* sockfd, msg, len, flags */
  3376. return do_recvfrom(a[0], a[1], a[2], a[3], 0, 0);
  3377. case TARGET_SYS_SENDTO: /* sockfd, msg, len, flags, addr, addrlen */
  3378. return do_sendto(a[0], a[1], a[2], a[3], a[4], a[5]);
  3379. case TARGET_SYS_RECVFROM: /* sockfd, msg, len, flags, addr, addrlen */
  3380. return do_recvfrom(a[0], a[1], a[2], a[3], a[4], a[5]);
  3381. case TARGET_SYS_SHUTDOWN: /* sockfd, how */
  3382. return get_errno(shutdown(a[0], a[1]));
  3383. case TARGET_SYS_SETSOCKOPT: /* sockfd, level, optname, optval, optlen */
  3384. return do_setsockopt(a[0], a[1], a[2], a[3], a[4]);
  3385. case TARGET_SYS_GETSOCKOPT: /* sockfd, level, optname, optval, optlen */
  3386. return do_getsockopt(a[0], a[1], a[2], a[3], a[4]);
  3387. case TARGET_SYS_SENDMSG: /* sockfd, msg, flags */
  3388. return do_sendrecvmsg(a[0], a[1], a[2], 1);
  3389. case TARGET_SYS_RECVMSG: /* sockfd, msg, flags */
  3390. return do_sendrecvmsg(a[0], a[1], a[2], 0);
  3391. case TARGET_SYS_ACCEPT4: /* sockfd, addr, addrlen, flags */
  3392. return do_accept4(a[0], a[1], a[2], a[3]);
  3393. case TARGET_SYS_RECVMMSG: /* sockfd, msgvec, vlen, flags */
  3394. return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 0);
  3395. case TARGET_SYS_SENDMMSG: /* sockfd, msgvec, vlen, flags */
  3396. return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 1);
  3397. default:
  3398. qemu_log_mask(LOG_UNIMP, "Unsupported socketcall: %d\n", num);
  3399. return -TARGET_EINVAL;
  3400. }
  3401. }
  3402. #endif
  3403. #define N_SHM_REGIONS 32
  3404. static struct shm_region {
  3405. abi_ulong start;
  3406. abi_ulong size;
  3407. bool in_use;
  3408. } shm_regions[N_SHM_REGIONS];
  3409. #ifndef TARGET_SEMID64_DS
  3410. /* asm-generic version of this struct */
  3411. struct target_semid64_ds
  3412. {
  3413. struct target_ipc_perm sem_perm;
  3414. abi_ulong sem_otime;
  3415. #if TARGET_ABI_BITS == 32
  3416. abi_ulong __unused1;
  3417. #endif
  3418. abi_ulong sem_ctime;
  3419. #if TARGET_ABI_BITS == 32
  3420. abi_ulong __unused2;
  3421. #endif
  3422. abi_ulong sem_nsems;
  3423. abi_ulong __unused3;
  3424. abi_ulong __unused4;
  3425. };
  3426. #endif
  3427. static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
  3428. abi_ulong target_addr)
  3429. {
  3430. struct target_ipc_perm *target_ip;
  3431. struct target_semid64_ds *target_sd;
  3432. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  3433. return -TARGET_EFAULT;
  3434. target_ip = &(target_sd->sem_perm);
  3435. host_ip->__key = tswap32(target_ip->__key);
  3436. host_ip->uid = tswap32(target_ip->uid);
  3437. host_ip->gid = tswap32(target_ip->gid);
  3438. host_ip->cuid = tswap32(target_ip->cuid);
  3439. host_ip->cgid = tswap32(target_ip->cgid);
  3440. #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
  3441. host_ip->mode = tswap32(target_ip->mode);
  3442. #else
  3443. host_ip->mode = tswap16(target_ip->mode);
  3444. #endif
  3445. #if defined(TARGET_PPC)
  3446. host_ip->__seq = tswap32(target_ip->__seq);
  3447. #else
  3448. host_ip->__seq = tswap16(target_ip->__seq);
  3449. #endif
  3450. unlock_user_struct(target_sd, target_addr, 0);
  3451. return 0;
  3452. }
  3453. static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
  3454. struct ipc_perm *host_ip)
  3455. {
  3456. struct target_ipc_perm *target_ip;
  3457. struct target_semid64_ds *target_sd;
  3458. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  3459. return -TARGET_EFAULT;
  3460. target_ip = &(target_sd->sem_perm);
  3461. target_ip->__key = tswap32(host_ip->__key);
  3462. target_ip->uid = tswap32(host_ip->uid);
  3463. target_ip->gid = tswap32(host_ip->gid);
  3464. target_ip->cuid = tswap32(host_ip->cuid);
  3465. target_ip->cgid = tswap32(host_ip->cgid);
  3466. #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
  3467. target_ip->mode = tswap32(host_ip->mode);
  3468. #else
  3469. target_ip->mode = tswap16(host_ip->mode);
  3470. #endif
  3471. #if defined(TARGET_PPC)
  3472. target_ip->__seq = tswap32(host_ip->__seq);
  3473. #else
  3474. target_ip->__seq = tswap16(host_ip->__seq);
  3475. #endif
  3476. unlock_user_struct(target_sd, target_addr, 1);
  3477. return 0;
  3478. }
  3479. static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
  3480. abi_ulong target_addr)
  3481. {
  3482. struct target_semid64_ds *target_sd;
  3483. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  3484. return -TARGET_EFAULT;
  3485. if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
  3486. return -TARGET_EFAULT;
  3487. host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
  3488. host_sd->sem_otime = tswapal(target_sd->sem_otime);
  3489. host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
  3490. unlock_user_struct(target_sd, target_addr, 0);
  3491. return 0;
  3492. }
  3493. static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
  3494. struct semid_ds *host_sd)
  3495. {
  3496. struct target_semid64_ds *target_sd;
  3497. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  3498. return -TARGET_EFAULT;
  3499. if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
  3500. return -TARGET_EFAULT;
  3501. target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
  3502. target_sd->sem_otime = tswapal(host_sd->sem_otime);
  3503. target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
  3504. unlock_user_struct(target_sd, target_addr, 1);
  3505. return 0;
  3506. }
  3507. struct target_seminfo {
  3508. int semmap;
  3509. int semmni;
  3510. int semmns;
  3511. int semmnu;
  3512. int semmsl;
  3513. int semopm;
  3514. int semume;
  3515. int semusz;
  3516. int semvmx;
  3517. int semaem;
  3518. };
  3519. static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
  3520. struct seminfo *host_seminfo)
  3521. {
  3522. struct target_seminfo *target_seminfo;
  3523. if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
  3524. return -TARGET_EFAULT;
  3525. __put_user(host_seminfo->semmap, &target_seminfo->semmap);
  3526. __put_user(host_seminfo->semmni, &target_seminfo->semmni);
  3527. __put_user(host_seminfo->semmns, &target_seminfo->semmns);
  3528. __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
  3529. __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
  3530. __put_user(host_seminfo->semopm, &target_seminfo->semopm);
  3531. __put_user(host_seminfo->semume, &target_seminfo->semume);
  3532. __put_user(host_seminfo->semusz, &target_seminfo->semusz);
  3533. __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
  3534. __put_user(host_seminfo->semaem, &target_seminfo->semaem);
  3535. unlock_user_struct(target_seminfo, target_addr, 1);
  3536. return 0;
  3537. }
  3538. union semun {
  3539. int val;
  3540. struct semid_ds *buf;
  3541. unsigned short *array;
  3542. struct seminfo *__buf;
  3543. };
  3544. union target_semun {
  3545. int val;
  3546. abi_ulong buf;
  3547. abi_ulong array;
  3548. abi_ulong __buf;
  3549. };
  3550. static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
  3551. abi_ulong target_addr)
  3552. {
  3553. int nsems;
  3554. unsigned short *array;
  3555. union semun semun;
  3556. struct semid_ds semid_ds;
  3557. int i, ret;
  3558. semun.buf = &semid_ds;
  3559. ret = semctl(semid, 0, IPC_STAT, semun);
  3560. if (ret == -1)
  3561. return get_errno(ret);
  3562. nsems = semid_ds.sem_nsems;
  3563. *host_array = g_try_new(unsigned short, nsems);
  3564. if (!*host_array) {
  3565. return -TARGET_ENOMEM;
  3566. }
  3567. array = lock_user(VERIFY_READ, target_addr,
  3568. nsems*sizeof(unsigned short), 1);
  3569. if (!array) {
  3570. g_free(*host_array);
  3571. return -TARGET_EFAULT;
  3572. }
  3573. for(i=0; i<nsems; i++) {
  3574. __get_user((*host_array)[i], &array[i]);
  3575. }
  3576. unlock_user(array, target_addr, 0);
  3577. return 0;
  3578. }
  3579. static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
  3580. unsigned short **host_array)
  3581. {
  3582. int nsems;
  3583. unsigned short *array;
  3584. union semun semun;
  3585. struct semid_ds semid_ds;
  3586. int i, ret;
  3587. semun.buf = &semid_ds;
  3588. ret = semctl(semid, 0, IPC_STAT, semun);
  3589. if (ret == -1)
  3590. return get_errno(ret);
  3591. nsems = semid_ds.sem_nsems;
  3592. array = lock_user(VERIFY_WRITE, target_addr,
  3593. nsems*sizeof(unsigned short), 0);
  3594. if (!array)
  3595. return -TARGET_EFAULT;
  3596. for(i=0; i<nsems; i++) {
  3597. __put_user((*host_array)[i], &array[i]);
  3598. }
  3599. g_free(*host_array);
  3600. unlock_user(array, target_addr, 1);
  3601. return 0;
  3602. }
  3603. static inline abi_long do_semctl(int semid, int semnum, int cmd,
  3604. abi_ulong target_arg)
  3605. {
  3606. union target_semun target_su = { .buf = target_arg };
  3607. union semun arg;
  3608. struct semid_ds dsarg;
  3609. unsigned short *array = NULL;
  3610. struct seminfo seminfo;
  3611. abi_long ret = -TARGET_EINVAL;
  3612. abi_long err;
  3613. cmd &= 0xff;
  3614. switch( cmd ) {
  3615. case GETVAL:
  3616. case SETVAL:
  3617. /* In 64 bit cross-endian situations, we will erroneously pick up
  3618. * the wrong half of the union for the "val" element. To rectify
  3619. * this, the entire 8-byte structure is byteswapped, followed by
  3620. * a swap of the 4 byte val field. In other cases, the data is
  3621. * already in proper host byte order. */
  3622. if (sizeof(target_su.val) != (sizeof(target_su.buf))) {
  3623. target_su.buf = tswapal(target_su.buf);
  3624. arg.val = tswap32(target_su.val);
  3625. } else {
  3626. arg.val = target_su.val;
  3627. }
  3628. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3629. break;
  3630. case GETALL:
  3631. case SETALL:
  3632. err = target_to_host_semarray(semid, &array, target_su.array);
  3633. if (err)
  3634. return err;
  3635. arg.array = array;
  3636. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3637. err = host_to_target_semarray(semid, target_su.array, &array);
  3638. if (err)
  3639. return err;
  3640. break;
  3641. case IPC_STAT:
  3642. case IPC_SET:
  3643. case SEM_STAT:
  3644. err = target_to_host_semid_ds(&dsarg, target_su.buf);
  3645. if (err)
  3646. return err;
  3647. arg.buf = &dsarg;
  3648. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3649. err = host_to_target_semid_ds(target_su.buf, &dsarg);
  3650. if (err)
  3651. return err;
  3652. break;
  3653. case IPC_INFO:
  3654. case SEM_INFO:
  3655. arg.__buf = &seminfo;
  3656. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3657. err = host_to_target_seminfo(target_su.__buf, &seminfo);
  3658. if (err)
  3659. return err;
  3660. break;
  3661. case IPC_RMID:
  3662. case GETPID:
  3663. case GETNCNT:
  3664. case GETZCNT:
  3665. ret = get_errno(semctl(semid, semnum, cmd, NULL));
  3666. break;
  3667. }
  3668. return ret;
  3669. }
  3670. struct target_sembuf {
  3671. unsigned short sem_num;
  3672. short sem_op;
  3673. short sem_flg;
  3674. };
  3675. static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
  3676. abi_ulong target_addr,
  3677. unsigned nsops)
  3678. {
  3679. struct target_sembuf *target_sembuf;
  3680. int i;
  3681. target_sembuf = lock_user(VERIFY_READ, target_addr,
  3682. nsops*sizeof(struct target_sembuf), 1);
  3683. if (!target_sembuf)
  3684. return -TARGET_EFAULT;
  3685. for(i=0; i<nsops; i++) {
  3686. __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
  3687. __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
  3688. __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
  3689. }
  3690. unlock_user(target_sembuf, target_addr, 0);
  3691. return 0;
  3692. }
  3693. #if defined(TARGET_NR_ipc) || defined(TARGET_NR_semop) || \
  3694. defined(TARGET_NR_semtimedop) || defined(TARGET_NR_semtimedop_time64)
  3695. /*
  3696. * This macro is required to handle the s390 variants, which passes the
  3697. * arguments in a different order than default.
  3698. */
  3699. #ifdef __s390x__
  3700. #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
  3701. (__nsops), (__timeout), (__sops)
  3702. #else
  3703. #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
  3704. (__nsops), 0, (__sops), (__timeout)
  3705. #endif
  3706. static inline abi_long do_semtimedop(int semid,
  3707. abi_long ptr,
  3708. unsigned nsops,
  3709. abi_long timeout, bool time64)
  3710. {
  3711. struct sembuf *sops;
  3712. struct timespec ts, *pts = NULL;
  3713. abi_long ret;
  3714. if (timeout) {
  3715. pts = &ts;
  3716. if (time64) {
  3717. if (target_to_host_timespec64(pts, timeout)) {
  3718. return -TARGET_EFAULT;
  3719. }
  3720. } else {
  3721. if (target_to_host_timespec(pts, timeout)) {
  3722. return -TARGET_EFAULT;
  3723. }
  3724. }
  3725. }
  3726. if (nsops > TARGET_SEMOPM) {
  3727. return -TARGET_E2BIG;
  3728. }
  3729. sops = g_new(struct sembuf, nsops);
  3730. if (target_to_host_sembuf(sops, ptr, nsops)) {
  3731. g_free(sops);
  3732. return -TARGET_EFAULT;
  3733. }
  3734. ret = -TARGET_ENOSYS;
  3735. #ifdef __NR_semtimedop
  3736. ret = get_errno(safe_semtimedop(semid, sops, nsops, pts));
  3737. #endif
  3738. #ifdef __NR_ipc
  3739. if (ret == -TARGET_ENOSYS) {
  3740. ret = get_errno(safe_ipc(IPCOP_semtimedop, semid,
  3741. SEMTIMEDOP_IPC_ARGS(nsops, sops, (long)pts)));
  3742. }
  3743. #endif
  3744. g_free(sops);
  3745. return ret;
  3746. }
  3747. #endif
  3748. struct target_msqid_ds
  3749. {
  3750. struct target_ipc_perm msg_perm;
  3751. abi_ulong msg_stime;
  3752. #if TARGET_ABI_BITS == 32
  3753. abi_ulong __unused1;
  3754. #endif
  3755. abi_ulong msg_rtime;
  3756. #if TARGET_ABI_BITS == 32
  3757. abi_ulong __unused2;
  3758. #endif
  3759. abi_ulong msg_ctime;
  3760. #if TARGET_ABI_BITS == 32
  3761. abi_ulong __unused3;
  3762. #endif
  3763. abi_ulong __msg_cbytes;
  3764. abi_ulong msg_qnum;
  3765. abi_ulong msg_qbytes;
  3766. abi_ulong msg_lspid;
  3767. abi_ulong msg_lrpid;
  3768. abi_ulong __unused4;
  3769. abi_ulong __unused5;
  3770. };
  3771. static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
  3772. abi_ulong target_addr)
  3773. {
  3774. struct target_msqid_ds *target_md;
  3775. if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
  3776. return -TARGET_EFAULT;
  3777. if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
  3778. return -TARGET_EFAULT;
  3779. host_md->msg_stime = tswapal(target_md->msg_stime);
  3780. host_md->msg_rtime = tswapal(target_md->msg_rtime);
  3781. host_md->msg_ctime = tswapal(target_md->msg_ctime);
  3782. host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
  3783. host_md->msg_qnum = tswapal(target_md->msg_qnum);
  3784. host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
  3785. host_md->msg_lspid = tswapal(target_md->msg_lspid);
  3786. host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
  3787. unlock_user_struct(target_md, target_addr, 0);
  3788. return 0;
  3789. }
  3790. static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
  3791. struct msqid_ds *host_md)
  3792. {
  3793. struct target_msqid_ds *target_md;
  3794. if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
  3795. return -TARGET_EFAULT;
  3796. if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
  3797. return -TARGET_EFAULT;
  3798. target_md->msg_stime = tswapal(host_md->msg_stime);
  3799. target_md->msg_rtime = tswapal(host_md->msg_rtime);
  3800. target_md->msg_ctime = tswapal(host_md->msg_ctime);
  3801. target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
  3802. target_md->msg_qnum = tswapal(host_md->msg_qnum);
  3803. target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
  3804. target_md->msg_lspid = tswapal(host_md->msg_lspid);
  3805. target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
  3806. unlock_user_struct(target_md, target_addr, 1);
  3807. return 0;
  3808. }
  3809. struct target_msginfo {
  3810. int msgpool;
  3811. int msgmap;
  3812. int msgmax;
  3813. int msgmnb;
  3814. int msgmni;
  3815. int msgssz;
  3816. int msgtql;
  3817. unsigned short int msgseg;
  3818. };
  3819. static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
  3820. struct msginfo *host_msginfo)
  3821. {
  3822. struct target_msginfo *target_msginfo;
  3823. if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
  3824. return -TARGET_EFAULT;
  3825. __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
  3826. __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
  3827. __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
  3828. __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
  3829. __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
  3830. __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
  3831. __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
  3832. __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
  3833. unlock_user_struct(target_msginfo, target_addr, 1);
  3834. return 0;
  3835. }
  3836. static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
  3837. {
  3838. struct msqid_ds dsarg;
  3839. struct msginfo msginfo;
  3840. abi_long ret = -TARGET_EINVAL;
  3841. cmd &= 0xff;
  3842. switch (cmd) {
  3843. case IPC_STAT:
  3844. case IPC_SET:
  3845. case MSG_STAT:
  3846. if (target_to_host_msqid_ds(&dsarg,ptr))
  3847. return -TARGET_EFAULT;
  3848. ret = get_errno(msgctl(msgid, cmd, &dsarg));
  3849. if (host_to_target_msqid_ds(ptr,&dsarg))
  3850. return -TARGET_EFAULT;
  3851. break;
  3852. case IPC_RMID:
  3853. ret = get_errno(msgctl(msgid, cmd, NULL));
  3854. break;
  3855. case IPC_INFO:
  3856. case MSG_INFO:
  3857. ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
  3858. if (host_to_target_msginfo(ptr, &msginfo))
  3859. return -TARGET_EFAULT;
  3860. break;
  3861. }
  3862. return ret;
  3863. }
  3864. struct target_msgbuf {
  3865. abi_long mtype;
  3866. char mtext[1];
  3867. };
  3868. static inline abi_long do_msgsnd(int msqid, abi_long msgp,
  3869. ssize_t msgsz, int msgflg)
  3870. {
  3871. struct target_msgbuf *target_mb;
  3872. struct msgbuf *host_mb;
  3873. abi_long ret = 0;
  3874. if (msgsz < 0) {
  3875. return -TARGET_EINVAL;
  3876. }
  3877. if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
  3878. return -TARGET_EFAULT;
  3879. host_mb = g_try_malloc(msgsz + sizeof(long));
  3880. if (!host_mb) {
  3881. unlock_user_struct(target_mb, msgp, 0);
  3882. return -TARGET_ENOMEM;
  3883. }
  3884. host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
  3885. memcpy(host_mb->mtext, target_mb->mtext, msgsz);
  3886. ret = -TARGET_ENOSYS;
  3887. #ifdef __NR_msgsnd
  3888. ret = get_errno(safe_msgsnd(msqid, host_mb, msgsz, msgflg));
  3889. #endif
  3890. #ifdef __NR_ipc
  3891. if (ret == -TARGET_ENOSYS) {
  3892. #ifdef __s390x__
  3893. ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
  3894. host_mb));
  3895. #else
  3896. ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
  3897. host_mb, 0));
  3898. #endif
  3899. }
  3900. #endif
  3901. g_free(host_mb);
  3902. unlock_user_struct(target_mb, msgp, 0);
  3903. return ret;
  3904. }
  3905. #ifdef __NR_ipc
  3906. #if defined(__sparc__)
  3907. /* SPARC for msgrcv it does not use the kludge on final 2 arguments. */
  3908. #define MSGRCV_ARGS(__msgp, __msgtyp) __msgp, __msgtyp
  3909. #elif defined(__s390x__)
  3910. /* The s390 sys_ipc variant has only five parameters. */
  3911. #define MSGRCV_ARGS(__msgp, __msgtyp) \
  3912. ((long int[]){(long int)__msgp, __msgtyp})
  3913. #else
  3914. #define MSGRCV_ARGS(__msgp, __msgtyp) \
  3915. ((long int[]){(long int)__msgp, __msgtyp}), 0
  3916. #endif
  3917. #endif
  3918. static inline abi_long do_msgrcv(int msqid, abi_long msgp,
  3919. ssize_t msgsz, abi_long msgtyp,
  3920. int msgflg)
  3921. {
  3922. struct target_msgbuf *target_mb;
  3923. char *target_mtext;
  3924. struct msgbuf *host_mb;
  3925. abi_long ret = 0;
  3926. if (msgsz < 0) {
  3927. return -TARGET_EINVAL;
  3928. }
  3929. if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
  3930. return -TARGET_EFAULT;
  3931. host_mb = g_try_malloc(msgsz + sizeof(long));
  3932. if (!host_mb) {
  3933. ret = -TARGET_ENOMEM;
  3934. goto end;
  3935. }
  3936. ret = -TARGET_ENOSYS;
  3937. #ifdef __NR_msgrcv
  3938. ret = get_errno(safe_msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
  3939. #endif
  3940. #ifdef __NR_ipc
  3941. if (ret == -TARGET_ENOSYS) {
  3942. ret = get_errno(safe_ipc(IPCOP_CALL(1, IPCOP_msgrcv), msqid, msgsz,
  3943. msgflg, MSGRCV_ARGS(host_mb, msgtyp)));
  3944. }
  3945. #endif
  3946. if (ret > 0) {
  3947. abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
  3948. target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
  3949. if (!target_mtext) {
  3950. ret = -TARGET_EFAULT;
  3951. goto end;
  3952. }
  3953. memcpy(target_mb->mtext, host_mb->mtext, ret);
  3954. unlock_user(target_mtext, target_mtext_addr, ret);
  3955. }
  3956. target_mb->mtype = tswapal(host_mb->mtype);
  3957. end:
  3958. if (target_mb)
  3959. unlock_user_struct(target_mb, msgp, 1);
  3960. g_free(host_mb);
  3961. return ret;
  3962. }
  3963. static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
  3964. abi_ulong target_addr)
  3965. {
  3966. struct target_shmid_ds *target_sd;
  3967. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  3968. return -TARGET_EFAULT;
  3969. if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
  3970. return -TARGET_EFAULT;
  3971. __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
  3972. __get_user(host_sd->shm_atime, &target_sd->shm_atime);
  3973. __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
  3974. __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
  3975. __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
  3976. __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
  3977. __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
  3978. unlock_user_struct(target_sd, target_addr, 0);
  3979. return 0;
  3980. }
  3981. static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
  3982. struct shmid_ds *host_sd)
  3983. {
  3984. struct target_shmid_ds *target_sd;
  3985. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  3986. return -TARGET_EFAULT;
  3987. if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
  3988. return -TARGET_EFAULT;
  3989. __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
  3990. __put_user(host_sd->shm_atime, &target_sd->shm_atime);
  3991. __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
  3992. __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
  3993. __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
  3994. __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
  3995. __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
  3996. unlock_user_struct(target_sd, target_addr, 1);
  3997. return 0;
  3998. }
  3999. struct target_shminfo {
  4000. abi_ulong shmmax;
  4001. abi_ulong shmmin;
  4002. abi_ulong shmmni;
  4003. abi_ulong shmseg;
  4004. abi_ulong shmall;
  4005. };
  4006. static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
  4007. struct shminfo *host_shminfo)
  4008. {
  4009. struct target_shminfo *target_shminfo;
  4010. if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
  4011. return -TARGET_EFAULT;
  4012. __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
  4013. __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
  4014. __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
  4015. __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
  4016. __put_user(host_shminfo->shmall, &target_shminfo->shmall);
  4017. unlock_user_struct(target_shminfo, target_addr, 1);
  4018. return 0;
  4019. }
  4020. struct target_shm_info {
  4021. int used_ids;
  4022. abi_ulong shm_tot;
  4023. abi_ulong shm_rss;
  4024. abi_ulong shm_swp;
  4025. abi_ulong swap_attempts;
  4026. abi_ulong swap_successes;
  4027. };
  4028. static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
  4029. struct shm_info *host_shm_info)
  4030. {
  4031. struct target_shm_info *target_shm_info;
  4032. if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
  4033. return -TARGET_EFAULT;
  4034. __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
  4035. __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
  4036. __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
  4037. __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
  4038. __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
  4039. __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
  4040. unlock_user_struct(target_shm_info, target_addr, 1);
  4041. return 0;
  4042. }
  4043. static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
  4044. {
  4045. struct shmid_ds dsarg;
  4046. struct shminfo shminfo;
  4047. struct shm_info shm_info;
  4048. abi_long ret = -TARGET_EINVAL;
  4049. cmd &= 0xff;
  4050. switch(cmd) {
  4051. case IPC_STAT:
  4052. case IPC_SET:
  4053. case SHM_STAT:
  4054. if (target_to_host_shmid_ds(&dsarg, buf))
  4055. return -TARGET_EFAULT;
  4056. ret = get_errno(shmctl(shmid, cmd, &dsarg));
  4057. if (host_to_target_shmid_ds(buf, &dsarg))
  4058. return -TARGET_EFAULT;
  4059. break;
  4060. case IPC_INFO:
  4061. ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
  4062. if (host_to_target_shminfo(buf, &shminfo))
  4063. return -TARGET_EFAULT;
  4064. break;
  4065. case SHM_INFO:
  4066. ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
  4067. if (host_to_target_shm_info(buf, &shm_info))
  4068. return -TARGET_EFAULT;
  4069. break;
  4070. case IPC_RMID:
  4071. case SHM_LOCK:
  4072. case SHM_UNLOCK:
  4073. ret = get_errno(shmctl(shmid, cmd, NULL));
  4074. break;
  4075. }
  4076. return ret;
  4077. }
  4078. #ifndef TARGET_FORCE_SHMLBA
  4079. /* For most architectures, SHMLBA is the same as the page size;
  4080. * some architectures have larger values, in which case they should
  4081. * define TARGET_FORCE_SHMLBA and provide a target_shmlba() function.
  4082. * This corresponds to the kernel arch code defining __ARCH_FORCE_SHMLBA
  4083. * and defining its own value for SHMLBA.
  4084. *
  4085. * The kernel also permits SHMLBA to be set by the architecture to a
  4086. * value larger than the page size without setting __ARCH_FORCE_SHMLBA;
  4087. * this means that addresses are rounded to the large size if
  4088. * SHM_RND is set but addresses not aligned to that size are not rejected
  4089. * as long as they are at least page-aligned. Since the only architecture
  4090. * which uses this is ia64 this code doesn't provide for that oddity.
  4091. */
  4092. static inline abi_ulong target_shmlba(CPUArchState *cpu_env)
  4093. {
  4094. return TARGET_PAGE_SIZE;
  4095. }
  4096. #endif
  4097. static inline abi_ulong do_shmat(CPUArchState *cpu_env,
  4098. int shmid, abi_ulong shmaddr, int shmflg)
  4099. {
  4100. CPUState *cpu = env_cpu(cpu_env);
  4101. abi_long raddr;
  4102. void *host_raddr;
  4103. struct shmid_ds shm_info;
  4104. int i,ret;
  4105. abi_ulong shmlba;
  4106. /* shmat pointers are always untagged */
  4107. /* find out the length of the shared memory segment */
  4108. ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
  4109. if (is_error(ret)) {
  4110. /* can't get length, bail out */
  4111. return ret;
  4112. }
  4113. shmlba = target_shmlba(cpu_env);
  4114. if (shmaddr & (shmlba - 1)) {
  4115. if (shmflg & SHM_RND) {
  4116. shmaddr &= ~(shmlba - 1);
  4117. } else {
  4118. return -TARGET_EINVAL;
  4119. }
  4120. }
  4121. if (!guest_range_valid_untagged(shmaddr, shm_info.shm_segsz)) {
  4122. return -TARGET_EINVAL;
  4123. }
  4124. mmap_lock();
  4125. /*
  4126. * We're mapping shared memory, so ensure we generate code for parallel
  4127. * execution and flush old translations. This will work up to the level
  4128. * supported by the host -- anything that requires EXCP_ATOMIC will not
  4129. * be atomic with respect to an external process.
  4130. */
  4131. if (!(cpu->tcg_cflags & CF_PARALLEL)) {
  4132. cpu->tcg_cflags |= CF_PARALLEL;
  4133. tb_flush(cpu);
  4134. }
  4135. if (shmaddr)
  4136. host_raddr = shmat(shmid, (void *)g2h_untagged(shmaddr), shmflg);
  4137. else {
  4138. abi_ulong mmap_start;
  4139. /* In order to use the host shmat, we need to honor host SHMLBA. */
  4140. mmap_start = mmap_find_vma(0, shm_info.shm_segsz, MAX(SHMLBA, shmlba));
  4141. if (mmap_start == -1) {
  4142. errno = ENOMEM;
  4143. host_raddr = (void *)-1;
  4144. } else
  4145. host_raddr = shmat(shmid, g2h_untagged(mmap_start),
  4146. shmflg | SHM_REMAP);
  4147. }
  4148. if (host_raddr == (void *)-1) {
  4149. mmap_unlock();
  4150. return get_errno((long)host_raddr);
  4151. }
  4152. raddr=h2g((unsigned long)host_raddr);
  4153. page_set_flags(raddr, raddr + shm_info.shm_segsz,
  4154. PAGE_VALID | PAGE_RESET | PAGE_READ |
  4155. (shmflg & SHM_RDONLY ? 0 : PAGE_WRITE));
  4156. for (i = 0; i < N_SHM_REGIONS; i++) {
  4157. if (!shm_regions[i].in_use) {
  4158. shm_regions[i].in_use = true;
  4159. shm_regions[i].start = raddr;
  4160. shm_regions[i].size = shm_info.shm_segsz;
  4161. break;
  4162. }
  4163. }
  4164. mmap_unlock();
  4165. return raddr;
  4166. }
  4167. static inline abi_long do_shmdt(abi_ulong shmaddr)
  4168. {
  4169. int i;
  4170. abi_long rv;
  4171. /* shmdt pointers are always untagged */
  4172. mmap_lock();
  4173. for (i = 0; i < N_SHM_REGIONS; ++i) {
  4174. if (shm_regions[i].in_use && shm_regions[i].start == shmaddr) {
  4175. shm_regions[i].in_use = false;
  4176. page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
  4177. break;
  4178. }
  4179. }
  4180. rv = get_errno(shmdt(g2h_untagged(shmaddr)));
  4181. mmap_unlock();
  4182. return rv;
  4183. }
  4184. #ifdef TARGET_NR_ipc
  4185. /* ??? This only works with linear mappings. */
  4186. /* do_ipc() must return target values and target errnos. */
  4187. static abi_long do_ipc(CPUArchState *cpu_env,
  4188. unsigned int call, abi_long first,
  4189. abi_long second, abi_long third,
  4190. abi_long ptr, abi_long fifth)
  4191. {
  4192. int version;
  4193. abi_long ret = 0;
  4194. version = call >> 16;
  4195. call &= 0xffff;
  4196. switch (call) {
  4197. case IPCOP_semop:
  4198. ret = do_semtimedop(first, ptr, second, 0, false);
  4199. break;
  4200. case IPCOP_semtimedop:
  4201. /*
  4202. * The s390 sys_ipc variant has only five parameters instead of six
  4203. * (as for default variant) and the only difference is the handling of
  4204. * SEMTIMEDOP where on s390 the third parameter is used as a pointer
  4205. * to a struct timespec where the generic variant uses fifth parameter.
  4206. */
  4207. #if defined(TARGET_S390X)
  4208. ret = do_semtimedop(first, ptr, second, third, TARGET_ABI_BITS == 64);
  4209. #else
  4210. ret = do_semtimedop(first, ptr, second, fifth, TARGET_ABI_BITS == 64);
  4211. #endif
  4212. break;
  4213. case IPCOP_semget:
  4214. ret = get_errno(semget(first, second, third));
  4215. break;
  4216. case IPCOP_semctl: {
  4217. /* The semun argument to semctl is passed by value, so dereference the
  4218. * ptr argument. */
  4219. abi_ulong atptr;
  4220. get_user_ual(atptr, ptr);
  4221. ret = do_semctl(first, second, third, atptr);
  4222. break;
  4223. }
  4224. case IPCOP_msgget:
  4225. ret = get_errno(msgget(first, second));
  4226. break;
  4227. case IPCOP_msgsnd:
  4228. ret = do_msgsnd(first, ptr, second, third);
  4229. break;
  4230. case IPCOP_msgctl:
  4231. ret = do_msgctl(first, second, ptr);
  4232. break;
  4233. case IPCOP_msgrcv:
  4234. switch (version) {
  4235. case 0:
  4236. {
  4237. struct target_ipc_kludge {
  4238. abi_long msgp;
  4239. abi_long msgtyp;
  4240. } *tmp;
  4241. if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
  4242. ret = -TARGET_EFAULT;
  4243. break;
  4244. }
  4245. ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
  4246. unlock_user_struct(tmp, ptr, 0);
  4247. break;
  4248. }
  4249. default:
  4250. ret = do_msgrcv(first, ptr, second, fifth, third);
  4251. }
  4252. break;
  4253. case IPCOP_shmat:
  4254. switch (version) {
  4255. default:
  4256. {
  4257. abi_ulong raddr;
  4258. raddr = do_shmat(cpu_env, first, ptr, second);
  4259. if (is_error(raddr))
  4260. return get_errno(raddr);
  4261. if (put_user_ual(raddr, third))
  4262. return -TARGET_EFAULT;
  4263. break;
  4264. }
  4265. case 1:
  4266. ret = -TARGET_EINVAL;
  4267. break;
  4268. }
  4269. break;
  4270. case IPCOP_shmdt:
  4271. ret = do_shmdt(ptr);
  4272. break;
  4273. case IPCOP_shmget:
  4274. /* IPC_* flag values are the same on all linux platforms */
  4275. ret = get_errno(shmget(first, second, third));
  4276. break;
  4277. /* IPC_* and SHM_* command values are the same on all linux platforms */
  4278. case IPCOP_shmctl:
  4279. ret = do_shmctl(first, second, ptr);
  4280. break;
  4281. default:
  4282. qemu_log_mask(LOG_UNIMP, "Unsupported ipc call: %d (version %d)\n",
  4283. call, version);
  4284. ret = -TARGET_ENOSYS;
  4285. break;
  4286. }
  4287. return ret;
  4288. }
  4289. #endif
  4290. /* kernel structure types definitions */
  4291. #define STRUCT(name, ...) STRUCT_ ## name,
  4292. #define STRUCT_SPECIAL(name) STRUCT_ ## name,
  4293. enum {
  4294. #include "syscall_types.h"
  4295. STRUCT_MAX
  4296. };
  4297. #undef STRUCT
  4298. #undef STRUCT_SPECIAL
  4299. #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
  4300. #define STRUCT_SPECIAL(name)
  4301. #include "syscall_types.h"
  4302. #undef STRUCT
  4303. #undef STRUCT_SPECIAL
  4304. #define MAX_STRUCT_SIZE 4096
  4305. #ifdef CONFIG_FIEMAP
  4306. /* So fiemap access checks don't overflow on 32 bit systems.
  4307. * This is very slightly smaller than the limit imposed by
  4308. * the underlying kernel.
  4309. */
  4310. #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
  4311. / sizeof(struct fiemap_extent))
  4312. static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
  4313. int fd, int cmd, abi_long arg)
  4314. {
  4315. /* The parameter for this ioctl is a struct fiemap followed
  4316. * by an array of struct fiemap_extent whose size is set
  4317. * in fiemap->fm_extent_count. The array is filled in by the
  4318. * ioctl.
  4319. */
  4320. int target_size_in, target_size_out;
  4321. struct fiemap *fm;
  4322. const argtype *arg_type = ie->arg_type;
  4323. const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
  4324. void *argptr, *p;
  4325. abi_long ret;
  4326. int i, extent_size = thunk_type_size(extent_arg_type, 0);
  4327. uint32_t outbufsz;
  4328. int free_fm = 0;
  4329. assert(arg_type[0] == TYPE_PTR);
  4330. assert(ie->access == IOC_RW);
  4331. arg_type++;
  4332. target_size_in = thunk_type_size(arg_type, 0);
  4333. argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
  4334. if (!argptr) {
  4335. return -TARGET_EFAULT;
  4336. }
  4337. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4338. unlock_user(argptr, arg, 0);
  4339. fm = (struct fiemap *)buf_temp;
  4340. if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
  4341. return -TARGET_EINVAL;
  4342. }
  4343. outbufsz = sizeof (*fm) +
  4344. (sizeof(struct fiemap_extent) * fm->fm_extent_count);
  4345. if (outbufsz > MAX_STRUCT_SIZE) {
  4346. /* We can't fit all the extents into the fixed size buffer.
  4347. * Allocate one that is large enough and use it instead.
  4348. */
  4349. fm = g_try_malloc(outbufsz);
  4350. if (!fm) {
  4351. return -TARGET_ENOMEM;
  4352. }
  4353. memcpy(fm, buf_temp, sizeof(struct fiemap));
  4354. free_fm = 1;
  4355. }
  4356. ret = get_errno(safe_ioctl(fd, ie->host_cmd, fm));
  4357. if (!is_error(ret)) {
  4358. target_size_out = target_size_in;
  4359. /* An extent_count of 0 means we were only counting the extents
  4360. * so there are no structs to copy
  4361. */
  4362. if (fm->fm_extent_count != 0) {
  4363. target_size_out += fm->fm_mapped_extents * extent_size;
  4364. }
  4365. argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
  4366. if (!argptr) {
  4367. ret = -TARGET_EFAULT;
  4368. } else {
  4369. /* Convert the struct fiemap */
  4370. thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
  4371. if (fm->fm_extent_count != 0) {
  4372. p = argptr + target_size_in;
  4373. /* ...and then all the struct fiemap_extents */
  4374. for (i = 0; i < fm->fm_mapped_extents; i++) {
  4375. thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
  4376. THUNK_TARGET);
  4377. p += extent_size;
  4378. }
  4379. }
  4380. unlock_user(argptr, arg, target_size_out);
  4381. }
  4382. }
  4383. if (free_fm) {
  4384. g_free(fm);
  4385. }
  4386. return ret;
  4387. }
  4388. #endif
  4389. static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
  4390. int fd, int cmd, abi_long arg)
  4391. {
  4392. const argtype *arg_type = ie->arg_type;
  4393. int target_size;
  4394. void *argptr;
  4395. int ret;
  4396. struct ifconf *host_ifconf;
  4397. uint32_t outbufsz;
  4398. const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
  4399. const argtype ifreq_max_type[] = { MK_STRUCT(STRUCT_ifmap_ifreq) };
  4400. int target_ifreq_size;
  4401. int nb_ifreq;
  4402. int free_buf = 0;
  4403. int i;
  4404. int target_ifc_len;
  4405. abi_long target_ifc_buf;
  4406. int host_ifc_len;
  4407. char *host_ifc_buf;
  4408. assert(arg_type[0] == TYPE_PTR);
  4409. assert(ie->access == IOC_RW);
  4410. arg_type++;
  4411. target_size = thunk_type_size(arg_type, 0);
  4412. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4413. if (!argptr)
  4414. return -TARGET_EFAULT;
  4415. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4416. unlock_user(argptr, arg, 0);
  4417. host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
  4418. target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
  4419. target_ifreq_size = thunk_type_size(ifreq_max_type, 0);
  4420. if (target_ifc_buf != 0) {
  4421. target_ifc_len = host_ifconf->ifc_len;
  4422. nb_ifreq = target_ifc_len / target_ifreq_size;
  4423. host_ifc_len = nb_ifreq * sizeof(struct ifreq);
  4424. outbufsz = sizeof(*host_ifconf) + host_ifc_len;
  4425. if (outbufsz > MAX_STRUCT_SIZE) {
  4426. /*
  4427. * We can't fit all the extents into the fixed size buffer.
  4428. * Allocate one that is large enough and use it instead.
  4429. */
  4430. host_ifconf = g_try_malloc(outbufsz);
  4431. if (!host_ifconf) {
  4432. return -TARGET_ENOMEM;
  4433. }
  4434. memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
  4435. free_buf = 1;
  4436. }
  4437. host_ifc_buf = (char *)host_ifconf + sizeof(*host_ifconf);
  4438. host_ifconf->ifc_len = host_ifc_len;
  4439. } else {
  4440. host_ifc_buf = NULL;
  4441. }
  4442. host_ifconf->ifc_buf = host_ifc_buf;
  4443. ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_ifconf));
  4444. if (!is_error(ret)) {
  4445. /* convert host ifc_len to target ifc_len */
  4446. nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
  4447. target_ifc_len = nb_ifreq * target_ifreq_size;
  4448. host_ifconf->ifc_len = target_ifc_len;
  4449. /* restore target ifc_buf */
  4450. host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
  4451. /* copy struct ifconf to target user */
  4452. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4453. if (!argptr)
  4454. return -TARGET_EFAULT;
  4455. thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
  4456. unlock_user(argptr, arg, target_size);
  4457. if (target_ifc_buf != 0) {
  4458. /* copy ifreq[] to target user */
  4459. argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
  4460. for (i = 0; i < nb_ifreq ; i++) {
  4461. thunk_convert(argptr + i * target_ifreq_size,
  4462. host_ifc_buf + i * sizeof(struct ifreq),
  4463. ifreq_arg_type, THUNK_TARGET);
  4464. }
  4465. unlock_user(argptr, target_ifc_buf, target_ifc_len);
  4466. }
  4467. }
  4468. if (free_buf) {
  4469. g_free(host_ifconf);
  4470. }
  4471. return ret;
  4472. }
  4473. #if defined(CONFIG_USBFS)
  4474. #if HOST_LONG_BITS > 64
  4475. #error USBDEVFS thunks do not support >64 bit hosts yet.
  4476. #endif
  4477. struct live_urb {
  4478. uint64_t target_urb_adr;
  4479. uint64_t target_buf_adr;
  4480. char *target_buf_ptr;
  4481. struct usbdevfs_urb host_urb;
  4482. };
  4483. static GHashTable *usbdevfs_urb_hashtable(void)
  4484. {
  4485. static GHashTable *urb_hashtable;
  4486. if (!urb_hashtable) {
  4487. urb_hashtable = g_hash_table_new(g_int64_hash, g_int64_equal);
  4488. }
  4489. return urb_hashtable;
  4490. }
  4491. static void urb_hashtable_insert(struct live_urb *urb)
  4492. {
  4493. GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
  4494. g_hash_table_insert(urb_hashtable, urb, urb);
  4495. }
  4496. static struct live_urb *urb_hashtable_lookup(uint64_t target_urb_adr)
  4497. {
  4498. GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
  4499. return g_hash_table_lookup(urb_hashtable, &target_urb_adr);
  4500. }
  4501. static void urb_hashtable_remove(struct live_urb *urb)
  4502. {
  4503. GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
  4504. g_hash_table_remove(urb_hashtable, urb);
  4505. }
  4506. static abi_long
  4507. do_ioctl_usbdevfs_reapurb(const IOCTLEntry *ie, uint8_t *buf_temp,
  4508. int fd, int cmd, abi_long arg)
  4509. {
  4510. const argtype usbfsurb_arg_type[] = { MK_STRUCT(STRUCT_usbdevfs_urb) };
  4511. const argtype ptrvoid_arg_type[] = { TYPE_PTRVOID, 0, 0 };
  4512. struct live_urb *lurb;
  4513. void *argptr;
  4514. uint64_t hurb;
  4515. int target_size;
  4516. uintptr_t target_urb_adr;
  4517. abi_long ret;
  4518. target_size = thunk_type_size(usbfsurb_arg_type, THUNK_TARGET);
  4519. memset(buf_temp, 0, sizeof(uint64_t));
  4520. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4521. if (is_error(ret)) {
  4522. return ret;
  4523. }
  4524. memcpy(&hurb, buf_temp, sizeof(uint64_t));
  4525. lurb = (void *)((uintptr_t)hurb - offsetof(struct live_urb, host_urb));
  4526. if (!lurb->target_urb_adr) {
  4527. return -TARGET_EFAULT;
  4528. }
  4529. urb_hashtable_remove(lurb);
  4530. unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr,
  4531. lurb->host_urb.buffer_length);
  4532. lurb->target_buf_ptr = NULL;
  4533. /* restore the guest buffer pointer */
  4534. lurb->host_urb.buffer = (void *)(uintptr_t)lurb->target_buf_adr;
  4535. /* update the guest urb struct */
  4536. argptr = lock_user(VERIFY_WRITE, lurb->target_urb_adr, target_size, 0);
  4537. if (!argptr) {
  4538. g_free(lurb);
  4539. return -TARGET_EFAULT;
  4540. }
  4541. thunk_convert(argptr, &lurb->host_urb, usbfsurb_arg_type, THUNK_TARGET);
  4542. unlock_user(argptr, lurb->target_urb_adr, target_size);
  4543. target_size = thunk_type_size(ptrvoid_arg_type, THUNK_TARGET);
  4544. /* write back the urb handle */
  4545. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4546. if (!argptr) {
  4547. g_free(lurb);
  4548. return -TARGET_EFAULT;
  4549. }
  4550. /* GHashTable uses 64-bit keys but thunk_convert expects uintptr_t */
  4551. target_urb_adr = lurb->target_urb_adr;
  4552. thunk_convert(argptr, &target_urb_adr, ptrvoid_arg_type, THUNK_TARGET);
  4553. unlock_user(argptr, arg, target_size);
  4554. g_free(lurb);
  4555. return ret;
  4556. }
  4557. static abi_long
  4558. do_ioctl_usbdevfs_discardurb(const IOCTLEntry *ie,
  4559. uint8_t *buf_temp __attribute__((unused)),
  4560. int fd, int cmd, abi_long arg)
  4561. {
  4562. struct live_urb *lurb;
  4563. /* map target address back to host URB with metadata. */
  4564. lurb = urb_hashtable_lookup(arg);
  4565. if (!lurb) {
  4566. return -TARGET_EFAULT;
  4567. }
  4568. return get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb));
  4569. }
  4570. static abi_long
  4571. do_ioctl_usbdevfs_submiturb(const IOCTLEntry *ie, uint8_t *buf_temp,
  4572. int fd, int cmd, abi_long arg)
  4573. {
  4574. const argtype *arg_type = ie->arg_type;
  4575. int target_size;
  4576. abi_long ret;
  4577. void *argptr;
  4578. int rw_dir;
  4579. struct live_urb *lurb;
  4580. /*
  4581. * each submitted URB needs to map to a unique ID for the
  4582. * kernel, and that unique ID needs to be a pointer to
  4583. * host memory. hence, we need to malloc for each URB.
  4584. * isochronous transfers have a variable length struct.
  4585. */
  4586. arg_type++;
  4587. target_size = thunk_type_size(arg_type, THUNK_TARGET);
  4588. /* construct host copy of urb and metadata */
  4589. lurb = g_try_new0(struct live_urb, 1);
  4590. if (!lurb) {
  4591. return -TARGET_ENOMEM;
  4592. }
  4593. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4594. if (!argptr) {
  4595. g_free(lurb);
  4596. return -TARGET_EFAULT;
  4597. }
  4598. thunk_convert(&lurb->host_urb, argptr, arg_type, THUNK_HOST);
  4599. unlock_user(argptr, arg, 0);
  4600. lurb->target_urb_adr = arg;
  4601. lurb->target_buf_adr = (uintptr_t)lurb->host_urb.buffer;
  4602. /* buffer space used depends on endpoint type so lock the entire buffer */
  4603. /* control type urbs should check the buffer contents for true direction */
  4604. rw_dir = lurb->host_urb.endpoint & USB_DIR_IN ? VERIFY_WRITE : VERIFY_READ;
  4605. lurb->target_buf_ptr = lock_user(rw_dir, lurb->target_buf_adr,
  4606. lurb->host_urb.buffer_length, 1);
  4607. if (lurb->target_buf_ptr == NULL) {
  4608. g_free(lurb);
  4609. return -TARGET_EFAULT;
  4610. }
  4611. /* update buffer pointer in host copy */
  4612. lurb->host_urb.buffer = lurb->target_buf_ptr;
  4613. ret = get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb));
  4614. if (is_error(ret)) {
  4615. unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr, 0);
  4616. g_free(lurb);
  4617. } else {
  4618. urb_hashtable_insert(lurb);
  4619. }
  4620. return ret;
  4621. }
  4622. #endif /* CONFIG_USBFS */
  4623. static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
  4624. int cmd, abi_long arg)
  4625. {
  4626. void *argptr;
  4627. struct dm_ioctl *host_dm;
  4628. abi_long guest_data;
  4629. uint32_t guest_data_size;
  4630. int target_size;
  4631. const argtype *arg_type = ie->arg_type;
  4632. abi_long ret;
  4633. void *big_buf = NULL;
  4634. char *host_data;
  4635. arg_type++;
  4636. target_size = thunk_type_size(arg_type, 0);
  4637. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4638. if (!argptr) {
  4639. ret = -TARGET_EFAULT;
  4640. goto out;
  4641. }
  4642. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4643. unlock_user(argptr, arg, 0);
  4644. /* buf_temp is too small, so fetch things into a bigger buffer */
  4645. big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
  4646. memcpy(big_buf, buf_temp, target_size);
  4647. buf_temp = big_buf;
  4648. host_dm = big_buf;
  4649. guest_data = arg + host_dm->data_start;
  4650. if ((guest_data - arg) < 0) {
  4651. ret = -TARGET_EINVAL;
  4652. goto out;
  4653. }
  4654. guest_data_size = host_dm->data_size - host_dm->data_start;
  4655. host_data = (char*)host_dm + host_dm->data_start;
  4656. argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
  4657. if (!argptr) {
  4658. ret = -TARGET_EFAULT;
  4659. goto out;
  4660. }
  4661. switch (ie->host_cmd) {
  4662. case DM_REMOVE_ALL:
  4663. case DM_LIST_DEVICES:
  4664. case DM_DEV_CREATE:
  4665. case DM_DEV_REMOVE:
  4666. case DM_DEV_SUSPEND:
  4667. case DM_DEV_STATUS:
  4668. case DM_DEV_WAIT:
  4669. case DM_TABLE_STATUS:
  4670. case DM_TABLE_CLEAR:
  4671. case DM_TABLE_DEPS:
  4672. case DM_LIST_VERSIONS:
  4673. /* no input data */
  4674. break;
  4675. case DM_DEV_RENAME:
  4676. case DM_DEV_SET_GEOMETRY:
  4677. /* data contains only strings */
  4678. memcpy(host_data, argptr, guest_data_size);
  4679. break;
  4680. case DM_TARGET_MSG:
  4681. memcpy(host_data, argptr, guest_data_size);
  4682. *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
  4683. break;
  4684. case DM_TABLE_LOAD:
  4685. {
  4686. void *gspec = argptr;
  4687. void *cur_data = host_data;
  4688. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
  4689. int spec_size = thunk_type_size(arg_type, 0);
  4690. int i;
  4691. for (i = 0; i < host_dm->target_count; i++) {
  4692. struct dm_target_spec *spec = cur_data;
  4693. uint32_t next;
  4694. int slen;
  4695. thunk_convert(spec, gspec, arg_type, THUNK_HOST);
  4696. slen = strlen((char*)gspec + spec_size) + 1;
  4697. next = spec->next;
  4698. spec->next = sizeof(*spec) + slen;
  4699. strcpy((char*)&spec[1], gspec + spec_size);
  4700. gspec += next;
  4701. cur_data += spec->next;
  4702. }
  4703. break;
  4704. }
  4705. default:
  4706. ret = -TARGET_EINVAL;
  4707. unlock_user(argptr, guest_data, 0);
  4708. goto out;
  4709. }
  4710. unlock_user(argptr, guest_data, 0);
  4711. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4712. if (!is_error(ret)) {
  4713. guest_data = arg + host_dm->data_start;
  4714. guest_data_size = host_dm->data_size - host_dm->data_start;
  4715. argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
  4716. switch (ie->host_cmd) {
  4717. case DM_REMOVE_ALL:
  4718. case DM_DEV_CREATE:
  4719. case DM_DEV_REMOVE:
  4720. case DM_DEV_RENAME:
  4721. case DM_DEV_SUSPEND:
  4722. case DM_DEV_STATUS:
  4723. case DM_TABLE_LOAD:
  4724. case DM_TABLE_CLEAR:
  4725. case DM_TARGET_MSG:
  4726. case DM_DEV_SET_GEOMETRY:
  4727. /* no return data */
  4728. break;
  4729. case DM_LIST_DEVICES:
  4730. {
  4731. struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
  4732. uint32_t remaining_data = guest_data_size;
  4733. void *cur_data = argptr;
  4734. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
  4735. int nl_size = 12; /* can't use thunk_size due to alignment */
  4736. while (1) {
  4737. uint32_t next = nl->next;
  4738. if (next) {
  4739. nl->next = nl_size + (strlen(nl->name) + 1);
  4740. }
  4741. if (remaining_data < nl->next) {
  4742. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  4743. break;
  4744. }
  4745. thunk_convert(cur_data, nl, arg_type, THUNK_TARGET);
  4746. strcpy(cur_data + nl_size, nl->name);
  4747. cur_data += nl->next;
  4748. remaining_data -= nl->next;
  4749. if (!next) {
  4750. break;
  4751. }
  4752. nl = (void*)nl + next;
  4753. }
  4754. break;
  4755. }
  4756. case DM_DEV_WAIT:
  4757. case DM_TABLE_STATUS:
  4758. {
  4759. struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
  4760. void *cur_data = argptr;
  4761. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
  4762. int spec_size = thunk_type_size(arg_type, 0);
  4763. int i;
  4764. for (i = 0; i < host_dm->target_count; i++) {
  4765. uint32_t next = spec->next;
  4766. int slen = strlen((char*)&spec[1]) + 1;
  4767. spec->next = (cur_data - argptr) + spec_size + slen;
  4768. if (guest_data_size < spec->next) {
  4769. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  4770. break;
  4771. }
  4772. thunk_convert(cur_data, spec, arg_type, THUNK_TARGET);
  4773. strcpy(cur_data + spec_size, (char*)&spec[1]);
  4774. cur_data = argptr + spec->next;
  4775. spec = (void*)host_dm + host_dm->data_start + next;
  4776. }
  4777. break;
  4778. }
  4779. case DM_TABLE_DEPS:
  4780. {
  4781. void *hdata = (void*)host_dm + host_dm->data_start;
  4782. int count = *(uint32_t*)hdata;
  4783. uint64_t *hdev = hdata + 8;
  4784. uint64_t *gdev = argptr + 8;
  4785. int i;
  4786. *(uint32_t*)argptr = tswap32(count);
  4787. for (i = 0; i < count; i++) {
  4788. *gdev = tswap64(*hdev);
  4789. gdev++;
  4790. hdev++;
  4791. }
  4792. break;
  4793. }
  4794. case DM_LIST_VERSIONS:
  4795. {
  4796. struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
  4797. uint32_t remaining_data = guest_data_size;
  4798. void *cur_data = argptr;
  4799. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
  4800. int vers_size = thunk_type_size(arg_type, 0);
  4801. while (1) {
  4802. uint32_t next = vers->next;
  4803. if (next) {
  4804. vers->next = vers_size + (strlen(vers->name) + 1);
  4805. }
  4806. if (remaining_data < vers->next) {
  4807. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  4808. break;
  4809. }
  4810. thunk_convert(cur_data, vers, arg_type, THUNK_TARGET);
  4811. strcpy(cur_data + vers_size, vers->name);
  4812. cur_data += vers->next;
  4813. remaining_data -= vers->next;
  4814. if (!next) {
  4815. break;
  4816. }
  4817. vers = (void*)vers + next;
  4818. }
  4819. break;
  4820. }
  4821. default:
  4822. unlock_user(argptr, guest_data, 0);
  4823. ret = -TARGET_EINVAL;
  4824. goto out;
  4825. }
  4826. unlock_user(argptr, guest_data, guest_data_size);
  4827. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4828. if (!argptr) {
  4829. ret = -TARGET_EFAULT;
  4830. goto out;
  4831. }
  4832. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  4833. unlock_user(argptr, arg, target_size);
  4834. }
  4835. out:
  4836. g_free(big_buf);
  4837. return ret;
  4838. }
  4839. static abi_long do_ioctl_blkpg(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
  4840. int cmd, abi_long arg)
  4841. {
  4842. void *argptr;
  4843. int target_size;
  4844. const argtype *arg_type = ie->arg_type;
  4845. const argtype part_arg_type[] = { MK_STRUCT(STRUCT_blkpg_partition) };
  4846. abi_long ret;
  4847. struct blkpg_ioctl_arg *host_blkpg = (void*)buf_temp;
  4848. struct blkpg_partition host_part;
  4849. /* Read and convert blkpg */
  4850. arg_type++;
  4851. target_size = thunk_type_size(arg_type, 0);
  4852. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4853. if (!argptr) {
  4854. ret = -TARGET_EFAULT;
  4855. goto out;
  4856. }
  4857. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4858. unlock_user(argptr, arg, 0);
  4859. switch (host_blkpg->op) {
  4860. case BLKPG_ADD_PARTITION:
  4861. case BLKPG_DEL_PARTITION:
  4862. /* payload is struct blkpg_partition */
  4863. break;
  4864. default:
  4865. /* Unknown opcode */
  4866. ret = -TARGET_EINVAL;
  4867. goto out;
  4868. }
  4869. /* Read and convert blkpg->data */
  4870. arg = (abi_long)(uintptr_t)host_blkpg->data;
  4871. target_size = thunk_type_size(part_arg_type, 0);
  4872. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4873. if (!argptr) {
  4874. ret = -TARGET_EFAULT;
  4875. goto out;
  4876. }
  4877. thunk_convert(&host_part, argptr, part_arg_type, THUNK_HOST);
  4878. unlock_user(argptr, arg, 0);
  4879. /* Swizzle the data pointer to our local copy and call! */
  4880. host_blkpg->data = &host_part;
  4881. ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_blkpg));
  4882. out:
  4883. return ret;
  4884. }
  4885. static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
  4886. int fd, int cmd, abi_long arg)
  4887. {
  4888. const argtype *arg_type = ie->arg_type;
  4889. const StructEntry *se;
  4890. const argtype *field_types;
  4891. const int *dst_offsets, *src_offsets;
  4892. int target_size;
  4893. void *argptr;
  4894. abi_ulong *target_rt_dev_ptr = NULL;
  4895. unsigned long *host_rt_dev_ptr = NULL;
  4896. abi_long ret;
  4897. int i;
  4898. assert(ie->access == IOC_W);
  4899. assert(*arg_type == TYPE_PTR);
  4900. arg_type++;
  4901. assert(*arg_type == TYPE_STRUCT);
  4902. target_size = thunk_type_size(arg_type, 0);
  4903. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4904. if (!argptr) {
  4905. return -TARGET_EFAULT;
  4906. }
  4907. arg_type++;
  4908. assert(*arg_type == (int)STRUCT_rtentry);
  4909. se = struct_entries + *arg_type++;
  4910. assert(se->convert[0] == NULL);
  4911. /* convert struct here to be able to catch rt_dev string */
  4912. field_types = se->field_types;
  4913. dst_offsets = se->field_offsets[THUNK_HOST];
  4914. src_offsets = se->field_offsets[THUNK_TARGET];
  4915. for (i = 0; i < se->nb_fields; i++) {
  4916. if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) {
  4917. assert(*field_types == TYPE_PTRVOID);
  4918. target_rt_dev_ptr = (abi_ulong *)(argptr + src_offsets[i]);
  4919. host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]);
  4920. if (*target_rt_dev_ptr != 0) {
  4921. *host_rt_dev_ptr = (unsigned long)lock_user_string(
  4922. tswapal(*target_rt_dev_ptr));
  4923. if (!*host_rt_dev_ptr) {
  4924. unlock_user(argptr, arg, 0);
  4925. return -TARGET_EFAULT;
  4926. }
  4927. } else {
  4928. *host_rt_dev_ptr = 0;
  4929. }
  4930. field_types++;
  4931. continue;
  4932. }
  4933. field_types = thunk_convert(buf_temp + dst_offsets[i],
  4934. argptr + src_offsets[i],
  4935. field_types, THUNK_HOST);
  4936. }
  4937. unlock_user(argptr, arg, 0);
  4938. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4939. assert(host_rt_dev_ptr != NULL);
  4940. assert(target_rt_dev_ptr != NULL);
  4941. if (*host_rt_dev_ptr != 0) {
  4942. unlock_user((void *)*host_rt_dev_ptr,
  4943. *target_rt_dev_ptr, 0);
  4944. }
  4945. return ret;
  4946. }
  4947. static abi_long do_ioctl_kdsigaccept(const IOCTLEntry *ie, uint8_t *buf_temp,
  4948. int fd, int cmd, abi_long arg)
  4949. {
  4950. int sig = target_to_host_signal(arg);
  4951. return get_errno(safe_ioctl(fd, ie->host_cmd, sig));
  4952. }
  4953. static abi_long do_ioctl_SIOCGSTAMP(const IOCTLEntry *ie, uint8_t *buf_temp,
  4954. int fd, int cmd, abi_long arg)
  4955. {
  4956. struct timeval tv;
  4957. abi_long ret;
  4958. ret = get_errno(safe_ioctl(fd, SIOCGSTAMP, &tv));
  4959. if (is_error(ret)) {
  4960. return ret;
  4961. }
  4962. if (cmd == (int)TARGET_SIOCGSTAMP_OLD) {
  4963. if (copy_to_user_timeval(arg, &tv)) {
  4964. return -TARGET_EFAULT;
  4965. }
  4966. } else {
  4967. if (copy_to_user_timeval64(arg, &tv)) {
  4968. return -TARGET_EFAULT;
  4969. }
  4970. }
  4971. return ret;
  4972. }
  4973. static abi_long do_ioctl_SIOCGSTAMPNS(const IOCTLEntry *ie, uint8_t *buf_temp,
  4974. int fd, int cmd, abi_long arg)
  4975. {
  4976. struct timespec ts;
  4977. abi_long ret;
  4978. ret = get_errno(safe_ioctl(fd, SIOCGSTAMPNS, &ts));
  4979. if (is_error(ret)) {
  4980. return ret;
  4981. }
  4982. if (cmd == (int)TARGET_SIOCGSTAMPNS_OLD) {
  4983. if (host_to_target_timespec(arg, &ts)) {
  4984. return -TARGET_EFAULT;
  4985. }
  4986. } else{
  4987. if (host_to_target_timespec64(arg, &ts)) {
  4988. return -TARGET_EFAULT;
  4989. }
  4990. }
  4991. return ret;
  4992. }
  4993. #ifdef TIOCGPTPEER
  4994. static abi_long do_ioctl_tiocgptpeer(const IOCTLEntry *ie, uint8_t *buf_temp,
  4995. int fd, int cmd, abi_long arg)
  4996. {
  4997. int flags = target_to_host_bitmask(arg, fcntl_flags_tbl);
  4998. return get_errno(safe_ioctl(fd, ie->host_cmd, flags));
  4999. }
  5000. #endif
  5001. #ifdef HAVE_DRM_H
  5002. static void unlock_drm_version(struct drm_version *host_ver,
  5003. struct target_drm_version *target_ver,
  5004. bool copy)
  5005. {
  5006. unlock_user(host_ver->name, target_ver->name,
  5007. copy ? host_ver->name_len : 0);
  5008. unlock_user(host_ver->date, target_ver->date,
  5009. copy ? host_ver->date_len : 0);
  5010. unlock_user(host_ver->desc, target_ver->desc,
  5011. copy ? host_ver->desc_len : 0);
  5012. }
  5013. static inline abi_long target_to_host_drmversion(struct drm_version *host_ver,
  5014. struct target_drm_version *target_ver)
  5015. {
  5016. memset(host_ver, 0, sizeof(*host_ver));
  5017. __get_user(host_ver->name_len, &target_ver->name_len);
  5018. if (host_ver->name_len) {
  5019. host_ver->name = lock_user(VERIFY_WRITE, target_ver->name,
  5020. target_ver->name_len, 0);
  5021. if (!host_ver->name) {
  5022. return -EFAULT;
  5023. }
  5024. }
  5025. __get_user(host_ver->date_len, &target_ver->date_len);
  5026. if (host_ver->date_len) {
  5027. host_ver->date = lock_user(VERIFY_WRITE, target_ver->date,
  5028. target_ver->date_len, 0);
  5029. if (!host_ver->date) {
  5030. goto err;
  5031. }
  5032. }
  5033. __get_user(host_ver->desc_len, &target_ver->desc_len);
  5034. if (host_ver->desc_len) {
  5035. host_ver->desc = lock_user(VERIFY_WRITE, target_ver->desc,
  5036. target_ver->desc_len, 0);
  5037. if (!host_ver->desc) {
  5038. goto err;
  5039. }
  5040. }
  5041. return 0;
  5042. err:
  5043. unlock_drm_version(host_ver, target_ver, false);
  5044. return -EFAULT;
  5045. }
  5046. static inline void host_to_target_drmversion(
  5047. struct target_drm_version *target_ver,
  5048. struct drm_version *host_ver)
  5049. {
  5050. __put_user(host_ver->version_major, &target_ver->version_major);
  5051. __put_user(host_ver->version_minor, &target_ver->version_minor);
  5052. __put_user(host_ver->version_patchlevel, &target_ver->version_patchlevel);
  5053. __put_user(host_ver->name_len, &target_ver->name_len);
  5054. __put_user(host_ver->date_len, &target_ver->date_len);
  5055. __put_user(host_ver->desc_len, &target_ver->desc_len);
  5056. unlock_drm_version(host_ver, target_ver, true);
  5057. }
  5058. static abi_long do_ioctl_drm(const IOCTLEntry *ie, uint8_t *buf_temp,
  5059. int fd, int cmd, abi_long arg)
  5060. {
  5061. struct drm_version *ver;
  5062. struct target_drm_version *target_ver;
  5063. abi_long ret;
  5064. switch (ie->host_cmd) {
  5065. case DRM_IOCTL_VERSION:
  5066. if (!lock_user_struct(VERIFY_WRITE, target_ver, arg, 0)) {
  5067. return -TARGET_EFAULT;
  5068. }
  5069. ver = (struct drm_version *)buf_temp;
  5070. ret = target_to_host_drmversion(ver, target_ver);
  5071. if (!is_error(ret)) {
  5072. ret = get_errno(safe_ioctl(fd, ie->host_cmd, ver));
  5073. if (is_error(ret)) {
  5074. unlock_drm_version(ver, target_ver, false);
  5075. } else {
  5076. host_to_target_drmversion(target_ver, ver);
  5077. }
  5078. }
  5079. unlock_user_struct(target_ver, arg, 0);
  5080. return ret;
  5081. }
  5082. return -TARGET_ENOSYS;
  5083. }
  5084. static abi_long do_ioctl_drm_i915_getparam(const IOCTLEntry *ie,
  5085. struct drm_i915_getparam *gparam,
  5086. int fd, abi_long arg)
  5087. {
  5088. abi_long ret;
  5089. int value;
  5090. struct target_drm_i915_getparam *target_gparam;
  5091. if (!lock_user_struct(VERIFY_READ, target_gparam, arg, 0)) {
  5092. return -TARGET_EFAULT;
  5093. }
  5094. __get_user(gparam->param, &target_gparam->param);
  5095. gparam->value = &value;
  5096. ret = get_errno(safe_ioctl(fd, ie->host_cmd, gparam));
  5097. put_user_s32(value, target_gparam->value);
  5098. unlock_user_struct(target_gparam, arg, 0);
  5099. return ret;
  5100. }
  5101. static abi_long do_ioctl_drm_i915(const IOCTLEntry *ie, uint8_t *buf_temp,
  5102. int fd, int cmd, abi_long arg)
  5103. {
  5104. switch (ie->host_cmd) {
  5105. case DRM_IOCTL_I915_GETPARAM:
  5106. return do_ioctl_drm_i915_getparam(ie,
  5107. (struct drm_i915_getparam *)buf_temp,
  5108. fd, arg);
  5109. default:
  5110. return -TARGET_ENOSYS;
  5111. }
  5112. }
  5113. #endif
  5114. static abi_long do_ioctl_TUNSETTXFILTER(const IOCTLEntry *ie, uint8_t *buf_temp,
  5115. int fd, int cmd, abi_long arg)
  5116. {
  5117. struct tun_filter *filter = (struct tun_filter *)buf_temp;
  5118. struct tun_filter *target_filter;
  5119. char *target_addr;
  5120. assert(ie->access == IOC_W);
  5121. target_filter = lock_user(VERIFY_READ, arg, sizeof(*target_filter), 1);
  5122. if (!target_filter) {
  5123. return -TARGET_EFAULT;
  5124. }
  5125. filter->flags = tswap16(target_filter->flags);
  5126. filter->count = tswap16(target_filter->count);
  5127. unlock_user(target_filter, arg, 0);
  5128. if (filter->count) {
  5129. if (offsetof(struct tun_filter, addr) + filter->count * ETH_ALEN >
  5130. MAX_STRUCT_SIZE) {
  5131. return -TARGET_EFAULT;
  5132. }
  5133. target_addr = lock_user(VERIFY_READ,
  5134. arg + offsetof(struct tun_filter, addr),
  5135. filter->count * ETH_ALEN, 1);
  5136. if (!target_addr) {
  5137. return -TARGET_EFAULT;
  5138. }
  5139. memcpy(filter->addr, target_addr, filter->count * ETH_ALEN);
  5140. unlock_user(target_addr, arg + offsetof(struct tun_filter, addr), 0);
  5141. }
  5142. return get_errno(safe_ioctl(fd, ie->host_cmd, filter));
  5143. }
  5144. IOCTLEntry ioctl_entries[] = {
  5145. #define IOCTL(cmd, access, ...) \
  5146. { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
  5147. #define IOCTL_SPECIAL(cmd, access, dofn, ...) \
  5148. { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
  5149. #define IOCTL_IGNORE(cmd) \
  5150. { TARGET_ ## cmd, 0, #cmd },
  5151. #include "ioctls.h"
  5152. { 0, 0, },
  5153. };
  5154. /* ??? Implement proper locking for ioctls. */
  5155. /* do_ioctl() Must return target values and target errnos. */
  5156. static abi_long do_ioctl(int fd, int cmd, abi_long arg)
  5157. {
  5158. const IOCTLEntry *ie;
  5159. const argtype *arg_type;
  5160. abi_long ret;
  5161. uint8_t buf_temp[MAX_STRUCT_SIZE];
  5162. int target_size;
  5163. void *argptr;
  5164. ie = ioctl_entries;
  5165. for(;;) {
  5166. if (ie->target_cmd == 0) {
  5167. qemu_log_mask(
  5168. LOG_UNIMP, "Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
  5169. return -TARGET_ENOSYS;
  5170. }
  5171. if (ie->target_cmd == cmd)
  5172. break;
  5173. ie++;
  5174. }
  5175. arg_type = ie->arg_type;
  5176. if (ie->do_ioctl) {
  5177. return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
  5178. } else if (!ie->host_cmd) {
  5179. /* Some architectures define BSD ioctls in their headers
  5180. that are not implemented in Linux. */
  5181. return -TARGET_ENOSYS;
  5182. }
  5183. switch(arg_type[0]) {
  5184. case TYPE_NULL:
  5185. /* no argument */
  5186. ret = get_errno(safe_ioctl(fd, ie->host_cmd));
  5187. break;
  5188. case TYPE_PTRVOID:
  5189. case TYPE_INT:
  5190. case TYPE_LONG:
  5191. case TYPE_ULONG:
  5192. ret = get_errno(safe_ioctl(fd, ie->host_cmd, arg));
  5193. break;
  5194. case TYPE_PTR:
  5195. arg_type++;
  5196. target_size = thunk_type_size(arg_type, 0);
  5197. switch(ie->access) {
  5198. case IOC_R:
  5199. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  5200. if (!is_error(ret)) {
  5201. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  5202. if (!argptr)
  5203. return -TARGET_EFAULT;
  5204. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  5205. unlock_user(argptr, arg, target_size);
  5206. }
  5207. break;
  5208. case IOC_W:
  5209. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  5210. if (!argptr)
  5211. return -TARGET_EFAULT;
  5212. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  5213. unlock_user(argptr, arg, 0);
  5214. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  5215. break;
  5216. default:
  5217. case IOC_RW:
  5218. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  5219. if (!argptr)
  5220. return -TARGET_EFAULT;
  5221. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  5222. unlock_user(argptr, arg, 0);
  5223. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  5224. if (!is_error(ret)) {
  5225. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  5226. if (!argptr)
  5227. return -TARGET_EFAULT;
  5228. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  5229. unlock_user(argptr, arg, target_size);
  5230. }
  5231. break;
  5232. }
  5233. break;
  5234. default:
  5235. qemu_log_mask(LOG_UNIMP,
  5236. "Unsupported ioctl type: cmd=0x%04lx type=%d\n",
  5237. (long)cmd, arg_type[0]);
  5238. ret = -TARGET_ENOSYS;
  5239. break;
  5240. }
  5241. return ret;
  5242. }
  5243. static const bitmask_transtbl iflag_tbl[] = {
  5244. { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
  5245. { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
  5246. { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
  5247. { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
  5248. { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
  5249. { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
  5250. { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
  5251. { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
  5252. { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
  5253. { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
  5254. { TARGET_IXON, TARGET_IXON, IXON, IXON },
  5255. { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
  5256. { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
  5257. { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
  5258. { TARGET_IUTF8, TARGET_IUTF8, IUTF8, IUTF8},
  5259. { 0, 0, 0, 0 }
  5260. };
  5261. static const bitmask_transtbl oflag_tbl[] = {
  5262. { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
  5263. { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
  5264. { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
  5265. { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
  5266. { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
  5267. { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
  5268. { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
  5269. { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
  5270. { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
  5271. { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
  5272. { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
  5273. { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
  5274. { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
  5275. { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
  5276. { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
  5277. { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
  5278. { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
  5279. { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
  5280. { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
  5281. { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
  5282. { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
  5283. { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
  5284. { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
  5285. { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
  5286. { 0, 0, 0, 0 }
  5287. };
  5288. static const bitmask_transtbl cflag_tbl[] = {
  5289. { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
  5290. { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
  5291. { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
  5292. { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
  5293. { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
  5294. { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
  5295. { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
  5296. { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
  5297. { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
  5298. { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
  5299. { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
  5300. { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
  5301. { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
  5302. { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
  5303. { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
  5304. { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
  5305. { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
  5306. { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
  5307. { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
  5308. { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
  5309. { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
  5310. { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
  5311. { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
  5312. { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
  5313. { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
  5314. { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
  5315. { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
  5316. { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
  5317. { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
  5318. { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
  5319. { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
  5320. { 0, 0, 0, 0 }
  5321. };
  5322. static const bitmask_transtbl lflag_tbl[] = {
  5323. { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
  5324. { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
  5325. { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
  5326. { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
  5327. { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
  5328. { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
  5329. { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
  5330. { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
  5331. { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
  5332. { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
  5333. { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
  5334. { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
  5335. { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
  5336. { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
  5337. { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
  5338. { TARGET_EXTPROC, TARGET_EXTPROC, EXTPROC, EXTPROC},
  5339. { 0, 0, 0, 0 }
  5340. };
  5341. static void target_to_host_termios (void *dst, const void *src)
  5342. {
  5343. struct host_termios *host = dst;
  5344. const struct target_termios *target = src;
  5345. host->c_iflag =
  5346. target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
  5347. host->c_oflag =
  5348. target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
  5349. host->c_cflag =
  5350. target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
  5351. host->c_lflag =
  5352. target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
  5353. host->c_line = target->c_line;
  5354. memset(host->c_cc, 0, sizeof(host->c_cc));
  5355. host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
  5356. host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
  5357. host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
  5358. host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
  5359. host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
  5360. host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
  5361. host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
  5362. host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
  5363. host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
  5364. host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
  5365. host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
  5366. host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
  5367. host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
  5368. host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
  5369. host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
  5370. host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
  5371. host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
  5372. }
  5373. static void host_to_target_termios (void *dst, const void *src)
  5374. {
  5375. struct target_termios *target = dst;
  5376. const struct host_termios *host = src;
  5377. target->c_iflag =
  5378. tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
  5379. target->c_oflag =
  5380. tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
  5381. target->c_cflag =
  5382. tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
  5383. target->c_lflag =
  5384. tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
  5385. target->c_line = host->c_line;
  5386. memset(target->c_cc, 0, sizeof(target->c_cc));
  5387. target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
  5388. target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
  5389. target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
  5390. target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
  5391. target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
  5392. target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
  5393. target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
  5394. target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
  5395. target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
  5396. target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
  5397. target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
  5398. target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
  5399. target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
  5400. target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
  5401. target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
  5402. target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
  5403. target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
  5404. }
  5405. static const StructEntry struct_termios_def = {
  5406. .convert = { host_to_target_termios, target_to_host_termios },
  5407. .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
  5408. .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
  5409. .print = print_termios,
  5410. };
  5411. static const bitmask_transtbl mmap_flags_tbl[] = {
  5412. { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
  5413. { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
  5414. { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
  5415. { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS,
  5416. MAP_ANONYMOUS, MAP_ANONYMOUS },
  5417. { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN,
  5418. MAP_GROWSDOWN, MAP_GROWSDOWN },
  5419. { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE,
  5420. MAP_DENYWRITE, MAP_DENYWRITE },
  5421. { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE,
  5422. MAP_EXECUTABLE, MAP_EXECUTABLE },
  5423. { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
  5424. { TARGET_MAP_NORESERVE, TARGET_MAP_NORESERVE,
  5425. MAP_NORESERVE, MAP_NORESERVE },
  5426. { TARGET_MAP_HUGETLB, TARGET_MAP_HUGETLB, MAP_HUGETLB, MAP_HUGETLB },
  5427. /* MAP_STACK had been ignored by the kernel for quite some time.
  5428. Recognize it for the target insofar as we do not want to pass
  5429. it through to the host. */
  5430. { TARGET_MAP_STACK, TARGET_MAP_STACK, 0, 0 },
  5431. { 0, 0, 0, 0 }
  5432. };
  5433. /*
  5434. * NOTE: TARGET_ABI32 is defined for TARGET_I386 (but not for TARGET_X86_64)
  5435. * TARGET_I386 is defined if TARGET_X86_64 is defined
  5436. */
  5437. #if defined(TARGET_I386)
  5438. /* NOTE: there is really one LDT for all the threads */
  5439. static uint8_t *ldt_table;
  5440. static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
  5441. {
  5442. int size;
  5443. void *p;
  5444. if (!ldt_table)
  5445. return 0;
  5446. size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
  5447. if (size > bytecount)
  5448. size = bytecount;
  5449. p = lock_user(VERIFY_WRITE, ptr, size, 0);
  5450. if (!p)
  5451. return -TARGET_EFAULT;
  5452. /* ??? Should this by byteswapped? */
  5453. memcpy(p, ldt_table, size);
  5454. unlock_user(p, ptr, size);
  5455. return size;
  5456. }
  5457. /* XXX: add locking support */
  5458. static abi_long write_ldt(CPUX86State *env,
  5459. abi_ulong ptr, unsigned long bytecount, int oldmode)
  5460. {
  5461. struct target_modify_ldt_ldt_s ldt_info;
  5462. struct target_modify_ldt_ldt_s *target_ldt_info;
  5463. int seg_32bit, contents, read_exec_only, limit_in_pages;
  5464. int seg_not_present, useable, lm;
  5465. uint32_t *lp, entry_1, entry_2;
  5466. if (bytecount != sizeof(ldt_info))
  5467. return -TARGET_EINVAL;
  5468. if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
  5469. return -TARGET_EFAULT;
  5470. ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
  5471. ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
  5472. ldt_info.limit = tswap32(target_ldt_info->limit);
  5473. ldt_info.flags = tswap32(target_ldt_info->flags);
  5474. unlock_user_struct(target_ldt_info, ptr, 0);
  5475. if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
  5476. return -TARGET_EINVAL;
  5477. seg_32bit = ldt_info.flags & 1;
  5478. contents = (ldt_info.flags >> 1) & 3;
  5479. read_exec_only = (ldt_info.flags >> 3) & 1;
  5480. limit_in_pages = (ldt_info.flags >> 4) & 1;
  5481. seg_not_present = (ldt_info.flags >> 5) & 1;
  5482. useable = (ldt_info.flags >> 6) & 1;
  5483. #ifdef TARGET_ABI32
  5484. lm = 0;
  5485. #else
  5486. lm = (ldt_info.flags >> 7) & 1;
  5487. #endif
  5488. if (contents == 3) {
  5489. if (oldmode)
  5490. return -TARGET_EINVAL;
  5491. if (seg_not_present == 0)
  5492. return -TARGET_EINVAL;
  5493. }
  5494. /* allocate the LDT */
  5495. if (!ldt_table) {
  5496. env->ldt.base = target_mmap(0,
  5497. TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
  5498. PROT_READ|PROT_WRITE,
  5499. MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
  5500. if (env->ldt.base == -1)
  5501. return -TARGET_ENOMEM;
  5502. memset(g2h_untagged(env->ldt.base), 0,
  5503. TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
  5504. env->ldt.limit = 0xffff;
  5505. ldt_table = g2h_untagged(env->ldt.base);
  5506. }
  5507. /* NOTE: same code as Linux kernel */
  5508. /* Allow LDTs to be cleared by the user. */
  5509. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  5510. if (oldmode ||
  5511. (contents == 0 &&
  5512. read_exec_only == 1 &&
  5513. seg_32bit == 0 &&
  5514. limit_in_pages == 0 &&
  5515. seg_not_present == 1 &&
  5516. useable == 0 )) {
  5517. entry_1 = 0;
  5518. entry_2 = 0;
  5519. goto install;
  5520. }
  5521. }
  5522. entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
  5523. (ldt_info.limit & 0x0ffff);
  5524. entry_2 = (ldt_info.base_addr & 0xff000000) |
  5525. ((ldt_info.base_addr & 0x00ff0000) >> 16) |
  5526. (ldt_info.limit & 0xf0000) |
  5527. ((read_exec_only ^ 1) << 9) |
  5528. (contents << 10) |
  5529. ((seg_not_present ^ 1) << 15) |
  5530. (seg_32bit << 22) |
  5531. (limit_in_pages << 23) |
  5532. (lm << 21) |
  5533. 0x7000;
  5534. if (!oldmode)
  5535. entry_2 |= (useable << 20);
  5536. /* Install the new entry ... */
  5537. install:
  5538. lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
  5539. lp[0] = tswap32(entry_1);
  5540. lp[1] = tswap32(entry_2);
  5541. return 0;
  5542. }
  5543. /* specific and weird i386 syscalls */
  5544. static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
  5545. unsigned long bytecount)
  5546. {
  5547. abi_long ret;
  5548. switch (func) {
  5549. case 0:
  5550. ret = read_ldt(ptr, bytecount);
  5551. break;
  5552. case 1:
  5553. ret = write_ldt(env, ptr, bytecount, 1);
  5554. break;
  5555. case 0x11:
  5556. ret = write_ldt(env, ptr, bytecount, 0);
  5557. break;
  5558. default:
  5559. ret = -TARGET_ENOSYS;
  5560. break;
  5561. }
  5562. return ret;
  5563. }
  5564. #if defined(TARGET_ABI32)
  5565. abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
  5566. {
  5567. uint64_t *gdt_table = g2h_untagged(env->gdt.base);
  5568. struct target_modify_ldt_ldt_s ldt_info;
  5569. struct target_modify_ldt_ldt_s *target_ldt_info;
  5570. int seg_32bit, contents, read_exec_only, limit_in_pages;
  5571. int seg_not_present, useable, lm;
  5572. uint32_t *lp, entry_1, entry_2;
  5573. int i;
  5574. lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
  5575. if (!target_ldt_info)
  5576. return -TARGET_EFAULT;
  5577. ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
  5578. ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
  5579. ldt_info.limit = tswap32(target_ldt_info->limit);
  5580. ldt_info.flags = tswap32(target_ldt_info->flags);
  5581. if (ldt_info.entry_number == -1) {
  5582. for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
  5583. if (gdt_table[i] == 0) {
  5584. ldt_info.entry_number = i;
  5585. target_ldt_info->entry_number = tswap32(i);
  5586. break;
  5587. }
  5588. }
  5589. }
  5590. unlock_user_struct(target_ldt_info, ptr, 1);
  5591. if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
  5592. ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
  5593. return -TARGET_EINVAL;
  5594. seg_32bit = ldt_info.flags & 1;
  5595. contents = (ldt_info.flags >> 1) & 3;
  5596. read_exec_only = (ldt_info.flags >> 3) & 1;
  5597. limit_in_pages = (ldt_info.flags >> 4) & 1;
  5598. seg_not_present = (ldt_info.flags >> 5) & 1;
  5599. useable = (ldt_info.flags >> 6) & 1;
  5600. #ifdef TARGET_ABI32
  5601. lm = 0;
  5602. #else
  5603. lm = (ldt_info.flags >> 7) & 1;
  5604. #endif
  5605. if (contents == 3) {
  5606. if (seg_not_present == 0)
  5607. return -TARGET_EINVAL;
  5608. }
  5609. /* NOTE: same code as Linux kernel */
  5610. /* Allow LDTs to be cleared by the user. */
  5611. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  5612. if ((contents == 0 &&
  5613. read_exec_only == 1 &&
  5614. seg_32bit == 0 &&
  5615. limit_in_pages == 0 &&
  5616. seg_not_present == 1 &&
  5617. useable == 0 )) {
  5618. entry_1 = 0;
  5619. entry_2 = 0;
  5620. goto install;
  5621. }
  5622. }
  5623. entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
  5624. (ldt_info.limit & 0x0ffff);
  5625. entry_2 = (ldt_info.base_addr & 0xff000000) |
  5626. ((ldt_info.base_addr & 0x00ff0000) >> 16) |
  5627. (ldt_info.limit & 0xf0000) |
  5628. ((read_exec_only ^ 1) << 9) |
  5629. (contents << 10) |
  5630. ((seg_not_present ^ 1) << 15) |
  5631. (seg_32bit << 22) |
  5632. (limit_in_pages << 23) |
  5633. (useable << 20) |
  5634. (lm << 21) |
  5635. 0x7000;
  5636. /* Install the new entry ... */
  5637. install:
  5638. lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
  5639. lp[0] = tswap32(entry_1);
  5640. lp[1] = tswap32(entry_2);
  5641. return 0;
  5642. }
  5643. static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
  5644. {
  5645. struct target_modify_ldt_ldt_s *target_ldt_info;
  5646. uint64_t *gdt_table = g2h_untagged(env->gdt.base);
  5647. uint32_t base_addr, limit, flags;
  5648. int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
  5649. int seg_not_present, useable, lm;
  5650. uint32_t *lp, entry_1, entry_2;
  5651. lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
  5652. if (!target_ldt_info)
  5653. return -TARGET_EFAULT;
  5654. idx = tswap32(target_ldt_info->entry_number);
  5655. if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
  5656. idx > TARGET_GDT_ENTRY_TLS_MAX) {
  5657. unlock_user_struct(target_ldt_info, ptr, 1);
  5658. return -TARGET_EINVAL;
  5659. }
  5660. lp = (uint32_t *)(gdt_table + idx);
  5661. entry_1 = tswap32(lp[0]);
  5662. entry_2 = tswap32(lp[1]);
  5663. read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
  5664. contents = (entry_2 >> 10) & 3;
  5665. seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
  5666. seg_32bit = (entry_2 >> 22) & 1;
  5667. limit_in_pages = (entry_2 >> 23) & 1;
  5668. useable = (entry_2 >> 20) & 1;
  5669. #ifdef TARGET_ABI32
  5670. lm = 0;
  5671. #else
  5672. lm = (entry_2 >> 21) & 1;
  5673. #endif
  5674. flags = (seg_32bit << 0) | (contents << 1) |
  5675. (read_exec_only << 3) | (limit_in_pages << 4) |
  5676. (seg_not_present << 5) | (useable << 6) | (lm << 7);
  5677. limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
  5678. base_addr = (entry_1 >> 16) |
  5679. (entry_2 & 0xff000000) |
  5680. ((entry_2 & 0xff) << 16);
  5681. target_ldt_info->base_addr = tswapal(base_addr);
  5682. target_ldt_info->limit = tswap32(limit);
  5683. target_ldt_info->flags = tswap32(flags);
  5684. unlock_user_struct(target_ldt_info, ptr, 1);
  5685. return 0;
  5686. }
  5687. abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
  5688. {
  5689. return -TARGET_ENOSYS;
  5690. }
  5691. #else
  5692. abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
  5693. {
  5694. abi_long ret = 0;
  5695. abi_ulong val;
  5696. int idx;
  5697. switch(code) {
  5698. case TARGET_ARCH_SET_GS:
  5699. case TARGET_ARCH_SET_FS:
  5700. if (code == TARGET_ARCH_SET_GS)
  5701. idx = R_GS;
  5702. else
  5703. idx = R_FS;
  5704. cpu_x86_load_seg(env, idx, 0);
  5705. env->segs[idx].base = addr;
  5706. break;
  5707. case TARGET_ARCH_GET_GS:
  5708. case TARGET_ARCH_GET_FS:
  5709. if (code == TARGET_ARCH_GET_GS)
  5710. idx = R_GS;
  5711. else
  5712. idx = R_FS;
  5713. val = env->segs[idx].base;
  5714. if (put_user(val, addr, abi_ulong))
  5715. ret = -TARGET_EFAULT;
  5716. break;
  5717. default:
  5718. ret = -TARGET_EINVAL;
  5719. break;
  5720. }
  5721. return ret;
  5722. }
  5723. #endif /* defined(TARGET_ABI32 */
  5724. #endif /* defined(TARGET_I386) */
  5725. /*
  5726. * These constants are generic. Supply any that are missing from the host.
  5727. */
  5728. #ifndef PR_SET_NAME
  5729. # define PR_SET_NAME 15
  5730. # define PR_GET_NAME 16
  5731. #endif
  5732. #ifndef PR_SET_FP_MODE
  5733. # define PR_SET_FP_MODE 45
  5734. # define PR_GET_FP_MODE 46
  5735. # define PR_FP_MODE_FR (1 << 0)
  5736. # define PR_FP_MODE_FRE (1 << 1)
  5737. #endif
  5738. #ifndef PR_SVE_SET_VL
  5739. # define PR_SVE_SET_VL 50
  5740. # define PR_SVE_GET_VL 51
  5741. # define PR_SVE_VL_LEN_MASK 0xffff
  5742. # define PR_SVE_VL_INHERIT (1 << 17)
  5743. #endif
  5744. #ifndef PR_PAC_RESET_KEYS
  5745. # define PR_PAC_RESET_KEYS 54
  5746. # define PR_PAC_APIAKEY (1 << 0)
  5747. # define PR_PAC_APIBKEY (1 << 1)
  5748. # define PR_PAC_APDAKEY (1 << 2)
  5749. # define PR_PAC_APDBKEY (1 << 3)
  5750. # define PR_PAC_APGAKEY (1 << 4)
  5751. #endif
  5752. #ifndef PR_SET_TAGGED_ADDR_CTRL
  5753. # define PR_SET_TAGGED_ADDR_CTRL 55
  5754. # define PR_GET_TAGGED_ADDR_CTRL 56
  5755. # define PR_TAGGED_ADDR_ENABLE (1UL << 0)
  5756. #endif
  5757. #ifndef PR_MTE_TCF_SHIFT
  5758. # define PR_MTE_TCF_SHIFT 1
  5759. # define PR_MTE_TCF_NONE (0UL << PR_MTE_TCF_SHIFT)
  5760. # define PR_MTE_TCF_SYNC (1UL << PR_MTE_TCF_SHIFT)
  5761. # define PR_MTE_TCF_ASYNC (2UL << PR_MTE_TCF_SHIFT)
  5762. # define PR_MTE_TCF_MASK (3UL << PR_MTE_TCF_SHIFT)
  5763. # define PR_MTE_TAG_SHIFT 3
  5764. # define PR_MTE_TAG_MASK (0xffffUL << PR_MTE_TAG_SHIFT)
  5765. #endif
  5766. #ifndef PR_SET_IO_FLUSHER
  5767. # define PR_SET_IO_FLUSHER 57
  5768. # define PR_GET_IO_FLUSHER 58
  5769. #endif
  5770. #ifndef PR_SET_SYSCALL_USER_DISPATCH
  5771. # define PR_SET_SYSCALL_USER_DISPATCH 59
  5772. #endif
  5773. #ifndef PR_SME_SET_VL
  5774. # define PR_SME_SET_VL 63
  5775. # define PR_SME_GET_VL 64
  5776. # define PR_SME_VL_LEN_MASK 0xffff
  5777. # define PR_SME_VL_INHERIT (1 << 17)
  5778. #endif
  5779. #include "target_prctl.h"
  5780. static abi_long do_prctl_inval0(CPUArchState *env)
  5781. {
  5782. return -TARGET_EINVAL;
  5783. }
  5784. static abi_long do_prctl_inval1(CPUArchState *env, abi_long arg2)
  5785. {
  5786. return -TARGET_EINVAL;
  5787. }
  5788. #ifndef do_prctl_get_fp_mode
  5789. #define do_prctl_get_fp_mode do_prctl_inval0
  5790. #endif
  5791. #ifndef do_prctl_set_fp_mode
  5792. #define do_prctl_set_fp_mode do_prctl_inval1
  5793. #endif
  5794. #ifndef do_prctl_sve_get_vl
  5795. #define do_prctl_sve_get_vl do_prctl_inval0
  5796. #endif
  5797. #ifndef do_prctl_sve_set_vl
  5798. #define do_prctl_sve_set_vl do_prctl_inval1
  5799. #endif
  5800. #ifndef do_prctl_reset_keys
  5801. #define do_prctl_reset_keys do_prctl_inval1
  5802. #endif
  5803. #ifndef do_prctl_set_tagged_addr_ctrl
  5804. #define do_prctl_set_tagged_addr_ctrl do_prctl_inval1
  5805. #endif
  5806. #ifndef do_prctl_get_tagged_addr_ctrl
  5807. #define do_prctl_get_tagged_addr_ctrl do_prctl_inval0
  5808. #endif
  5809. #ifndef do_prctl_get_unalign
  5810. #define do_prctl_get_unalign do_prctl_inval1
  5811. #endif
  5812. #ifndef do_prctl_set_unalign
  5813. #define do_prctl_set_unalign do_prctl_inval1
  5814. #endif
  5815. #ifndef do_prctl_sme_get_vl
  5816. #define do_prctl_sme_get_vl do_prctl_inval0
  5817. #endif
  5818. #ifndef do_prctl_sme_set_vl
  5819. #define do_prctl_sme_set_vl do_prctl_inval1
  5820. #endif
  5821. static abi_long do_prctl(CPUArchState *env, abi_long option, abi_long arg2,
  5822. abi_long arg3, abi_long arg4, abi_long arg5)
  5823. {
  5824. abi_long ret;
  5825. switch (option) {
  5826. case PR_GET_PDEATHSIG:
  5827. {
  5828. int deathsig;
  5829. ret = get_errno(prctl(PR_GET_PDEATHSIG, &deathsig,
  5830. arg3, arg4, arg5));
  5831. if (!is_error(ret) &&
  5832. put_user_s32(host_to_target_signal(deathsig), arg2)) {
  5833. return -TARGET_EFAULT;
  5834. }
  5835. return ret;
  5836. }
  5837. case PR_SET_PDEATHSIG:
  5838. return get_errno(prctl(PR_SET_PDEATHSIG, target_to_host_signal(arg2),
  5839. arg3, arg4, arg5));
  5840. case PR_GET_NAME:
  5841. {
  5842. void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
  5843. if (!name) {
  5844. return -TARGET_EFAULT;
  5845. }
  5846. ret = get_errno(prctl(PR_GET_NAME, (uintptr_t)name,
  5847. arg3, arg4, arg5));
  5848. unlock_user(name, arg2, 16);
  5849. return ret;
  5850. }
  5851. case PR_SET_NAME:
  5852. {
  5853. void *name = lock_user(VERIFY_READ, arg2, 16, 1);
  5854. if (!name) {
  5855. return -TARGET_EFAULT;
  5856. }
  5857. ret = get_errno(prctl(PR_SET_NAME, (uintptr_t)name,
  5858. arg3, arg4, arg5));
  5859. unlock_user(name, arg2, 0);
  5860. return ret;
  5861. }
  5862. case PR_GET_FP_MODE:
  5863. return do_prctl_get_fp_mode(env);
  5864. case PR_SET_FP_MODE:
  5865. return do_prctl_set_fp_mode(env, arg2);
  5866. case PR_SVE_GET_VL:
  5867. return do_prctl_sve_get_vl(env);
  5868. case PR_SVE_SET_VL:
  5869. return do_prctl_sve_set_vl(env, arg2);
  5870. case PR_SME_GET_VL:
  5871. return do_prctl_sme_get_vl(env);
  5872. case PR_SME_SET_VL:
  5873. return do_prctl_sme_set_vl(env, arg2);
  5874. case PR_PAC_RESET_KEYS:
  5875. if (arg3 || arg4 || arg5) {
  5876. return -TARGET_EINVAL;
  5877. }
  5878. return do_prctl_reset_keys(env, arg2);
  5879. case PR_SET_TAGGED_ADDR_CTRL:
  5880. if (arg3 || arg4 || arg5) {
  5881. return -TARGET_EINVAL;
  5882. }
  5883. return do_prctl_set_tagged_addr_ctrl(env, arg2);
  5884. case PR_GET_TAGGED_ADDR_CTRL:
  5885. if (arg2 || arg3 || arg4 || arg5) {
  5886. return -TARGET_EINVAL;
  5887. }
  5888. return do_prctl_get_tagged_addr_ctrl(env);
  5889. case PR_GET_UNALIGN:
  5890. return do_prctl_get_unalign(env, arg2);
  5891. case PR_SET_UNALIGN:
  5892. return do_prctl_set_unalign(env, arg2);
  5893. case PR_CAP_AMBIENT:
  5894. case PR_CAPBSET_READ:
  5895. case PR_CAPBSET_DROP:
  5896. case PR_GET_DUMPABLE:
  5897. case PR_SET_DUMPABLE:
  5898. case PR_GET_KEEPCAPS:
  5899. case PR_SET_KEEPCAPS:
  5900. case PR_GET_SECUREBITS:
  5901. case PR_SET_SECUREBITS:
  5902. case PR_GET_TIMING:
  5903. case PR_SET_TIMING:
  5904. case PR_GET_TIMERSLACK:
  5905. case PR_SET_TIMERSLACK:
  5906. case PR_MCE_KILL:
  5907. case PR_MCE_KILL_GET:
  5908. case PR_GET_NO_NEW_PRIVS:
  5909. case PR_SET_NO_NEW_PRIVS:
  5910. case PR_GET_IO_FLUSHER:
  5911. case PR_SET_IO_FLUSHER:
  5912. /* Some prctl options have no pointer arguments and we can pass on. */
  5913. return get_errno(prctl(option, arg2, arg3, arg4, arg5));
  5914. case PR_GET_CHILD_SUBREAPER:
  5915. case PR_SET_CHILD_SUBREAPER:
  5916. case PR_GET_SPECULATION_CTRL:
  5917. case PR_SET_SPECULATION_CTRL:
  5918. case PR_GET_TID_ADDRESS:
  5919. /* TODO */
  5920. return -TARGET_EINVAL;
  5921. case PR_GET_FPEXC:
  5922. case PR_SET_FPEXC:
  5923. /* Was used for SPE on PowerPC. */
  5924. return -TARGET_EINVAL;
  5925. case PR_GET_ENDIAN:
  5926. case PR_SET_ENDIAN:
  5927. case PR_GET_FPEMU:
  5928. case PR_SET_FPEMU:
  5929. case PR_SET_MM:
  5930. case PR_GET_SECCOMP:
  5931. case PR_SET_SECCOMP:
  5932. case PR_SET_SYSCALL_USER_DISPATCH:
  5933. case PR_GET_THP_DISABLE:
  5934. case PR_SET_THP_DISABLE:
  5935. case PR_GET_TSC:
  5936. case PR_SET_TSC:
  5937. /* Disable to prevent the target disabling stuff we need. */
  5938. return -TARGET_EINVAL;
  5939. default:
  5940. qemu_log_mask(LOG_UNIMP, "Unsupported prctl: " TARGET_ABI_FMT_ld "\n",
  5941. option);
  5942. return -TARGET_EINVAL;
  5943. }
  5944. }
  5945. #define NEW_STACK_SIZE 0x40000
  5946. static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
  5947. typedef struct {
  5948. CPUArchState *env;
  5949. pthread_mutex_t mutex;
  5950. pthread_cond_t cond;
  5951. pthread_t thread;
  5952. uint32_t tid;
  5953. abi_ulong child_tidptr;
  5954. abi_ulong parent_tidptr;
  5955. sigset_t sigmask;
  5956. } new_thread_info;
  5957. static void *clone_func(void *arg)
  5958. {
  5959. new_thread_info *info = arg;
  5960. CPUArchState *env;
  5961. CPUState *cpu;
  5962. TaskState *ts;
  5963. rcu_register_thread();
  5964. tcg_register_thread();
  5965. env = info->env;
  5966. cpu = env_cpu(env);
  5967. thread_cpu = cpu;
  5968. ts = (TaskState *)cpu->opaque;
  5969. info->tid = sys_gettid();
  5970. task_settid(ts);
  5971. if (info->child_tidptr)
  5972. put_user_u32(info->tid, info->child_tidptr);
  5973. if (info->parent_tidptr)
  5974. put_user_u32(info->tid, info->parent_tidptr);
  5975. qemu_guest_random_seed_thread_part2(cpu->random_seed);
  5976. /* Enable signals. */
  5977. sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
  5978. /* Signal to the parent that we're ready. */
  5979. pthread_mutex_lock(&info->mutex);
  5980. pthread_cond_broadcast(&info->cond);
  5981. pthread_mutex_unlock(&info->mutex);
  5982. /* Wait until the parent has finished initializing the tls state. */
  5983. pthread_mutex_lock(&clone_lock);
  5984. pthread_mutex_unlock(&clone_lock);
  5985. cpu_loop(env);
  5986. /* never exits */
  5987. return NULL;
  5988. }
  5989. /* do_fork() Must return host values and target errnos (unlike most
  5990. do_*() functions). */
  5991. static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
  5992. abi_ulong parent_tidptr, target_ulong newtls,
  5993. abi_ulong child_tidptr)
  5994. {
  5995. CPUState *cpu = env_cpu(env);
  5996. int ret;
  5997. TaskState *ts;
  5998. CPUState *new_cpu;
  5999. CPUArchState *new_env;
  6000. sigset_t sigmask;
  6001. flags &= ~CLONE_IGNORED_FLAGS;
  6002. /* Emulate vfork() with fork() */
  6003. if (flags & CLONE_VFORK)
  6004. flags &= ~(CLONE_VFORK | CLONE_VM);
  6005. if (flags & CLONE_VM) {
  6006. TaskState *parent_ts = (TaskState *)cpu->opaque;
  6007. new_thread_info info;
  6008. pthread_attr_t attr;
  6009. if (((flags & CLONE_THREAD_FLAGS) != CLONE_THREAD_FLAGS) ||
  6010. (flags & CLONE_INVALID_THREAD_FLAGS)) {
  6011. return -TARGET_EINVAL;
  6012. }
  6013. ts = g_new0(TaskState, 1);
  6014. init_task_state(ts);
  6015. /* Grab a mutex so that thread setup appears atomic. */
  6016. pthread_mutex_lock(&clone_lock);
  6017. /*
  6018. * If this is our first additional thread, we need to ensure we
  6019. * generate code for parallel execution and flush old translations.
  6020. * Do this now so that the copy gets CF_PARALLEL too.
  6021. */
  6022. if (!(cpu->tcg_cflags & CF_PARALLEL)) {
  6023. cpu->tcg_cflags |= CF_PARALLEL;
  6024. tb_flush(cpu);
  6025. }
  6026. /* we create a new CPU instance. */
  6027. new_env = cpu_copy(env);
  6028. /* Init regs that differ from the parent. */
  6029. cpu_clone_regs_child(new_env, newsp, flags);
  6030. cpu_clone_regs_parent(env, flags);
  6031. new_cpu = env_cpu(new_env);
  6032. new_cpu->opaque = ts;
  6033. ts->bprm = parent_ts->bprm;
  6034. ts->info = parent_ts->info;
  6035. ts->signal_mask = parent_ts->signal_mask;
  6036. if (flags & CLONE_CHILD_CLEARTID) {
  6037. ts->child_tidptr = child_tidptr;
  6038. }
  6039. if (flags & CLONE_SETTLS) {
  6040. cpu_set_tls (new_env, newtls);
  6041. }
  6042. memset(&info, 0, sizeof(info));
  6043. pthread_mutex_init(&info.mutex, NULL);
  6044. pthread_mutex_lock(&info.mutex);
  6045. pthread_cond_init(&info.cond, NULL);
  6046. info.env = new_env;
  6047. if (flags & CLONE_CHILD_SETTID) {
  6048. info.child_tidptr = child_tidptr;
  6049. }
  6050. if (flags & CLONE_PARENT_SETTID) {
  6051. info.parent_tidptr = parent_tidptr;
  6052. }
  6053. ret = pthread_attr_init(&attr);
  6054. ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
  6055. ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  6056. /* It is not safe to deliver signals until the child has finished
  6057. initializing, so temporarily block all signals. */
  6058. sigfillset(&sigmask);
  6059. sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
  6060. cpu->random_seed = qemu_guest_random_seed_thread_part1();
  6061. ret = pthread_create(&info.thread, &attr, clone_func, &info);
  6062. /* TODO: Free new CPU state if thread creation failed. */
  6063. sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
  6064. pthread_attr_destroy(&attr);
  6065. if (ret == 0) {
  6066. /* Wait for the child to initialize. */
  6067. pthread_cond_wait(&info.cond, &info.mutex);
  6068. ret = info.tid;
  6069. } else {
  6070. ret = -1;
  6071. }
  6072. pthread_mutex_unlock(&info.mutex);
  6073. pthread_cond_destroy(&info.cond);
  6074. pthread_mutex_destroy(&info.mutex);
  6075. pthread_mutex_unlock(&clone_lock);
  6076. } else {
  6077. /* if no CLONE_VM, we consider it is a fork */
  6078. if (flags & CLONE_INVALID_FORK_FLAGS) {
  6079. return -TARGET_EINVAL;
  6080. }
  6081. /* We can't support custom termination signals */
  6082. if ((flags & CSIGNAL) != TARGET_SIGCHLD) {
  6083. return -TARGET_EINVAL;
  6084. }
  6085. if (block_signals()) {
  6086. return -QEMU_ERESTARTSYS;
  6087. }
  6088. fork_start();
  6089. ret = fork();
  6090. if (ret == 0) {
  6091. /* Child Process. */
  6092. cpu_clone_regs_child(env, newsp, flags);
  6093. fork_end(1);
  6094. /* There is a race condition here. The parent process could
  6095. theoretically read the TID in the child process before the child
  6096. tid is set. This would require using either ptrace
  6097. (not implemented) or having *_tidptr to point at a shared memory
  6098. mapping. We can't repeat the spinlock hack used above because
  6099. the child process gets its own copy of the lock. */
  6100. if (flags & CLONE_CHILD_SETTID)
  6101. put_user_u32(sys_gettid(), child_tidptr);
  6102. if (flags & CLONE_PARENT_SETTID)
  6103. put_user_u32(sys_gettid(), parent_tidptr);
  6104. ts = (TaskState *)cpu->opaque;
  6105. if (flags & CLONE_SETTLS)
  6106. cpu_set_tls (env, newtls);
  6107. if (flags & CLONE_CHILD_CLEARTID)
  6108. ts->child_tidptr = child_tidptr;
  6109. } else {
  6110. cpu_clone_regs_parent(env, flags);
  6111. fork_end(0);
  6112. }
  6113. }
  6114. return ret;
  6115. }
  6116. /* warning : doesn't handle linux specific flags... */
  6117. static int target_to_host_fcntl_cmd(int cmd)
  6118. {
  6119. int ret;
  6120. switch(cmd) {
  6121. case TARGET_F_DUPFD:
  6122. case TARGET_F_GETFD:
  6123. case TARGET_F_SETFD:
  6124. case TARGET_F_GETFL:
  6125. case TARGET_F_SETFL:
  6126. case TARGET_F_OFD_GETLK:
  6127. case TARGET_F_OFD_SETLK:
  6128. case TARGET_F_OFD_SETLKW:
  6129. ret = cmd;
  6130. break;
  6131. case TARGET_F_GETLK:
  6132. ret = F_GETLK64;
  6133. break;
  6134. case TARGET_F_SETLK:
  6135. ret = F_SETLK64;
  6136. break;
  6137. case TARGET_F_SETLKW:
  6138. ret = F_SETLKW64;
  6139. break;
  6140. case TARGET_F_GETOWN:
  6141. ret = F_GETOWN;
  6142. break;
  6143. case TARGET_F_SETOWN:
  6144. ret = F_SETOWN;
  6145. break;
  6146. case TARGET_F_GETSIG:
  6147. ret = F_GETSIG;
  6148. break;
  6149. case TARGET_F_SETSIG:
  6150. ret = F_SETSIG;
  6151. break;
  6152. #if TARGET_ABI_BITS == 32
  6153. case TARGET_F_GETLK64:
  6154. ret = F_GETLK64;
  6155. break;
  6156. case TARGET_F_SETLK64:
  6157. ret = F_SETLK64;
  6158. break;
  6159. case TARGET_F_SETLKW64:
  6160. ret = F_SETLKW64;
  6161. break;
  6162. #endif
  6163. case TARGET_F_SETLEASE:
  6164. ret = F_SETLEASE;
  6165. break;
  6166. case TARGET_F_GETLEASE:
  6167. ret = F_GETLEASE;
  6168. break;
  6169. #ifdef F_DUPFD_CLOEXEC
  6170. case TARGET_F_DUPFD_CLOEXEC:
  6171. ret = F_DUPFD_CLOEXEC;
  6172. break;
  6173. #endif
  6174. case TARGET_F_NOTIFY:
  6175. ret = F_NOTIFY;
  6176. break;
  6177. #ifdef F_GETOWN_EX
  6178. case TARGET_F_GETOWN_EX:
  6179. ret = F_GETOWN_EX;
  6180. break;
  6181. #endif
  6182. #ifdef F_SETOWN_EX
  6183. case TARGET_F_SETOWN_EX:
  6184. ret = F_SETOWN_EX;
  6185. break;
  6186. #endif
  6187. #ifdef F_SETPIPE_SZ
  6188. case TARGET_F_SETPIPE_SZ:
  6189. ret = F_SETPIPE_SZ;
  6190. break;
  6191. case TARGET_F_GETPIPE_SZ:
  6192. ret = F_GETPIPE_SZ;
  6193. break;
  6194. #endif
  6195. #ifdef F_ADD_SEALS
  6196. case TARGET_F_ADD_SEALS:
  6197. ret = F_ADD_SEALS;
  6198. break;
  6199. case TARGET_F_GET_SEALS:
  6200. ret = F_GET_SEALS;
  6201. break;
  6202. #endif
  6203. default:
  6204. ret = -TARGET_EINVAL;
  6205. break;
  6206. }
  6207. #if defined(__powerpc64__)
  6208. /* On PPC64, glibc headers has the F_*LK* defined to 12, 13 and 14 and
  6209. * is not supported by kernel. The glibc fcntl call actually adjusts
  6210. * them to 5, 6 and 7 before making the syscall(). Since we make the
  6211. * syscall directly, adjust to what is supported by the kernel.
  6212. */
  6213. if (ret >= F_GETLK64 && ret <= F_SETLKW64) {
  6214. ret -= F_GETLK64 - 5;
  6215. }
  6216. #endif
  6217. return ret;
  6218. }
  6219. #define FLOCK_TRANSTBL \
  6220. switch (type) { \
  6221. TRANSTBL_CONVERT(F_RDLCK); \
  6222. TRANSTBL_CONVERT(F_WRLCK); \
  6223. TRANSTBL_CONVERT(F_UNLCK); \
  6224. }
  6225. static int target_to_host_flock(int type)
  6226. {
  6227. #define TRANSTBL_CONVERT(a) case TARGET_##a: return a
  6228. FLOCK_TRANSTBL
  6229. #undef TRANSTBL_CONVERT
  6230. return -TARGET_EINVAL;
  6231. }
  6232. static int host_to_target_flock(int type)
  6233. {
  6234. #define TRANSTBL_CONVERT(a) case a: return TARGET_##a
  6235. FLOCK_TRANSTBL
  6236. #undef TRANSTBL_CONVERT
  6237. /* if we don't know how to convert the value coming
  6238. * from the host we copy to the target field as-is
  6239. */
  6240. return type;
  6241. }
  6242. static inline abi_long copy_from_user_flock(struct flock64 *fl,
  6243. abi_ulong target_flock_addr)
  6244. {
  6245. struct target_flock *target_fl;
  6246. int l_type;
  6247. if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
  6248. return -TARGET_EFAULT;
  6249. }
  6250. __get_user(l_type, &target_fl->l_type);
  6251. l_type = target_to_host_flock(l_type);
  6252. if (l_type < 0) {
  6253. return l_type;
  6254. }
  6255. fl->l_type = l_type;
  6256. __get_user(fl->l_whence, &target_fl->l_whence);
  6257. __get_user(fl->l_start, &target_fl->l_start);
  6258. __get_user(fl->l_len, &target_fl->l_len);
  6259. __get_user(fl->l_pid, &target_fl->l_pid);
  6260. unlock_user_struct(target_fl, target_flock_addr, 0);
  6261. return 0;
  6262. }
  6263. static inline abi_long copy_to_user_flock(abi_ulong target_flock_addr,
  6264. const struct flock64 *fl)
  6265. {
  6266. struct target_flock *target_fl;
  6267. short l_type;
  6268. if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
  6269. return -TARGET_EFAULT;
  6270. }
  6271. l_type = host_to_target_flock(fl->l_type);
  6272. __put_user(l_type, &target_fl->l_type);
  6273. __put_user(fl->l_whence, &target_fl->l_whence);
  6274. __put_user(fl->l_start, &target_fl->l_start);
  6275. __put_user(fl->l_len, &target_fl->l_len);
  6276. __put_user(fl->l_pid, &target_fl->l_pid);
  6277. unlock_user_struct(target_fl, target_flock_addr, 1);
  6278. return 0;
  6279. }
  6280. typedef abi_long from_flock64_fn(struct flock64 *fl, abi_ulong target_addr);
  6281. typedef abi_long to_flock64_fn(abi_ulong target_addr, const struct flock64 *fl);
  6282. #if defined(TARGET_ARM) && TARGET_ABI_BITS == 32
  6283. struct target_oabi_flock64 {
  6284. abi_short l_type;
  6285. abi_short l_whence;
  6286. abi_llong l_start;
  6287. abi_llong l_len;
  6288. abi_int l_pid;
  6289. } QEMU_PACKED;
  6290. static inline abi_long copy_from_user_oabi_flock64(struct flock64 *fl,
  6291. abi_ulong target_flock_addr)
  6292. {
  6293. struct target_oabi_flock64 *target_fl;
  6294. int l_type;
  6295. if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
  6296. return -TARGET_EFAULT;
  6297. }
  6298. __get_user(l_type, &target_fl->l_type);
  6299. l_type = target_to_host_flock(l_type);
  6300. if (l_type < 0) {
  6301. return l_type;
  6302. }
  6303. fl->l_type = l_type;
  6304. __get_user(fl->l_whence, &target_fl->l_whence);
  6305. __get_user(fl->l_start, &target_fl->l_start);
  6306. __get_user(fl->l_len, &target_fl->l_len);
  6307. __get_user(fl->l_pid, &target_fl->l_pid);
  6308. unlock_user_struct(target_fl, target_flock_addr, 0);
  6309. return 0;
  6310. }
  6311. static inline abi_long copy_to_user_oabi_flock64(abi_ulong target_flock_addr,
  6312. const struct flock64 *fl)
  6313. {
  6314. struct target_oabi_flock64 *target_fl;
  6315. short l_type;
  6316. if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
  6317. return -TARGET_EFAULT;
  6318. }
  6319. l_type = host_to_target_flock(fl->l_type);
  6320. __put_user(l_type, &target_fl->l_type);
  6321. __put_user(fl->l_whence, &target_fl->l_whence);
  6322. __put_user(fl->l_start, &target_fl->l_start);
  6323. __put_user(fl->l_len, &target_fl->l_len);
  6324. __put_user(fl->l_pid, &target_fl->l_pid);
  6325. unlock_user_struct(target_fl, target_flock_addr, 1);
  6326. return 0;
  6327. }
  6328. #endif
  6329. static inline abi_long copy_from_user_flock64(struct flock64 *fl,
  6330. abi_ulong target_flock_addr)
  6331. {
  6332. struct target_flock64 *target_fl;
  6333. int l_type;
  6334. if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
  6335. return -TARGET_EFAULT;
  6336. }
  6337. __get_user(l_type, &target_fl->l_type);
  6338. l_type = target_to_host_flock(l_type);
  6339. if (l_type < 0) {
  6340. return l_type;
  6341. }
  6342. fl->l_type = l_type;
  6343. __get_user(fl->l_whence, &target_fl->l_whence);
  6344. __get_user(fl->l_start, &target_fl->l_start);
  6345. __get_user(fl->l_len, &target_fl->l_len);
  6346. __get_user(fl->l_pid, &target_fl->l_pid);
  6347. unlock_user_struct(target_fl, target_flock_addr, 0);
  6348. return 0;
  6349. }
  6350. static inline abi_long copy_to_user_flock64(abi_ulong target_flock_addr,
  6351. const struct flock64 *fl)
  6352. {
  6353. struct target_flock64 *target_fl;
  6354. short l_type;
  6355. if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
  6356. return -TARGET_EFAULT;
  6357. }
  6358. l_type = host_to_target_flock(fl->l_type);
  6359. __put_user(l_type, &target_fl->l_type);
  6360. __put_user(fl->l_whence, &target_fl->l_whence);
  6361. __put_user(fl->l_start, &target_fl->l_start);
  6362. __put_user(fl->l_len, &target_fl->l_len);
  6363. __put_user(fl->l_pid, &target_fl->l_pid);
  6364. unlock_user_struct(target_fl, target_flock_addr, 1);
  6365. return 0;
  6366. }
  6367. static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
  6368. {
  6369. struct flock64 fl64;
  6370. #ifdef F_GETOWN_EX
  6371. struct f_owner_ex fox;
  6372. struct target_f_owner_ex *target_fox;
  6373. #endif
  6374. abi_long ret;
  6375. int host_cmd = target_to_host_fcntl_cmd(cmd);
  6376. if (host_cmd == -TARGET_EINVAL)
  6377. return host_cmd;
  6378. switch(cmd) {
  6379. case TARGET_F_GETLK:
  6380. ret = copy_from_user_flock(&fl64, arg);
  6381. if (ret) {
  6382. return ret;
  6383. }
  6384. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  6385. if (ret == 0) {
  6386. ret = copy_to_user_flock(arg, &fl64);
  6387. }
  6388. break;
  6389. case TARGET_F_SETLK:
  6390. case TARGET_F_SETLKW:
  6391. ret = copy_from_user_flock(&fl64, arg);
  6392. if (ret) {
  6393. return ret;
  6394. }
  6395. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  6396. break;
  6397. case TARGET_F_GETLK64:
  6398. case TARGET_F_OFD_GETLK:
  6399. ret = copy_from_user_flock64(&fl64, arg);
  6400. if (ret) {
  6401. return ret;
  6402. }
  6403. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  6404. if (ret == 0) {
  6405. ret = copy_to_user_flock64(arg, &fl64);
  6406. }
  6407. break;
  6408. case TARGET_F_SETLK64:
  6409. case TARGET_F_SETLKW64:
  6410. case TARGET_F_OFD_SETLK:
  6411. case TARGET_F_OFD_SETLKW:
  6412. ret = copy_from_user_flock64(&fl64, arg);
  6413. if (ret) {
  6414. return ret;
  6415. }
  6416. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  6417. break;
  6418. case TARGET_F_GETFL:
  6419. ret = get_errno(safe_fcntl(fd, host_cmd, arg));
  6420. if (ret >= 0) {
  6421. ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
  6422. }
  6423. break;
  6424. case TARGET_F_SETFL:
  6425. ret = get_errno(safe_fcntl(fd, host_cmd,
  6426. target_to_host_bitmask(arg,
  6427. fcntl_flags_tbl)));
  6428. break;
  6429. #ifdef F_GETOWN_EX
  6430. case TARGET_F_GETOWN_EX:
  6431. ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
  6432. if (ret >= 0) {
  6433. if (!lock_user_struct(VERIFY_WRITE, target_fox, arg, 0))
  6434. return -TARGET_EFAULT;
  6435. target_fox->type = tswap32(fox.type);
  6436. target_fox->pid = tswap32(fox.pid);
  6437. unlock_user_struct(target_fox, arg, 1);
  6438. }
  6439. break;
  6440. #endif
  6441. #ifdef F_SETOWN_EX
  6442. case TARGET_F_SETOWN_EX:
  6443. if (!lock_user_struct(VERIFY_READ, target_fox, arg, 1))
  6444. return -TARGET_EFAULT;
  6445. fox.type = tswap32(target_fox->type);
  6446. fox.pid = tswap32(target_fox->pid);
  6447. unlock_user_struct(target_fox, arg, 0);
  6448. ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
  6449. break;
  6450. #endif
  6451. case TARGET_F_SETSIG:
  6452. ret = get_errno(safe_fcntl(fd, host_cmd, target_to_host_signal(arg)));
  6453. break;
  6454. case TARGET_F_GETSIG:
  6455. ret = host_to_target_signal(get_errno(safe_fcntl(fd, host_cmd, arg)));
  6456. break;
  6457. case TARGET_F_SETOWN:
  6458. case TARGET_F_GETOWN:
  6459. case TARGET_F_SETLEASE:
  6460. case TARGET_F_GETLEASE:
  6461. case TARGET_F_SETPIPE_SZ:
  6462. case TARGET_F_GETPIPE_SZ:
  6463. case TARGET_F_ADD_SEALS:
  6464. case TARGET_F_GET_SEALS:
  6465. ret = get_errno(safe_fcntl(fd, host_cmd, arg));
  6466. break;
  6467. default:
  6468. ret = get_errno(safe_fcntl(fd, cmd, arg));
  6469. break;
  6470. }
  6471. return ret;
  6472. }
  6473. #ifdef USE_UID16
  6474. static inline int high2lowuid(int uid)
  6475. {
  6476. if (uid > 65535)
  6477. return 65534;
  6478. else
  6479. return uid;
  6480. }
  6481. static inline int high2lowgid(int gid)
  6482. {
  6483. if (gid > 65535)
  6484. return 65534;
  6485. else
  6486. return gid;
  6487. }
  6488. static inline int low2highuid(int uid)
  6489. {
  6490. if ((int16_t)uid == -1)
  6491. return -1;
  6492. else
  6493. return uid;
  6494. }
  6495. static inline int low2highgid(int gid)
  6496. {
  6497. if ((int16_t)gid == -1)
  6498. return -1;
  6499. else
  6500. return gid;
  6501. }
  6502. static inline int tswapid(int id)
  6503. {
  6504. return tswap16(id);
  6505. }
  6506. #define put_user_id(x, gaddr) put_user_u16(x, gaddr)
  6507. #else /* !USE_UID16 */
  6508. static inline int high2lowuid(int uid)
  6509. {
  6510. return uid;
  6511. }
  6512. static inline int high2lowgid(int gid)
  6513. {
  6514. return gid;
  6515. }
  6516. static inline int low2highuid(int uid)
  6517. {
  6518. return uid;
  6519. }
  6520. static inline int low2highgid(int gid)
  6521. {
  6522. return gid;
  6523. }
  6524. static inline int tswapid(int id)
  6525. {
  6526. return tswap32(id);
  6527. }
  6528. #define put_user_id(x, gaddr) put_user_u32(x, gaddr)
  6529. #endif /* USE_UID16 */
  6530. /* We must do direct syscalls for setting UID/GID, because we want to
  6531. * implement the Linux system call semantics of "change only for this thread",
  6532. * not the libc/POSIX semantics of "change for all threads in process".
  6533. * (See http://ewontfix.com/17/ for more details.)
  6534. * We use the 32-bit version of the syscalls if present; if it is not
  6535. * then either the host architecture supports 32-bit UIDs natively with
  6536. * the standard syscall, or the 16-bit UID is the best we can do.
  6537. */
  6538. #ifdef __NR_setuid32
  6539. #define __NR_sys_setuid __NR_setuid32
  6540. #else
  6541. #define __NR_sys_setuid __NR_setuid
  6542. #endif
  6543. #ifdef __NR_setgid32
  6544. #define __NR_sys_setgid __NR_setgid32
  6545. #else
  6546. #define __NR_sys_setgid __NR_setgid
  6547. #endif
  6548. #ifdef __NR_setresuid32
  6549. #define __NR_sys_setresuid __NR_setresuid32
  6550. #else
  6551. #define __NR_sys_setresuid __NR_setresuid
  6552. #endif
  6553. #ifdef __NR_setresgid32
  6554. #define __NR_sys_setresgid __NR_setresgid32
  6555. #else
  6556. #define __NR_sys_setresgid __NR_setresgid
  6557. #endif
  6558. _syscall1(int, sys_setuid, uid_t, uid)
  6559. _syscall1(int, sys_setgid, gid_t, gid)
  6560. _syscall3(int, sys_setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
  6561. _syscall3(int, sys_setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
  6562. void syscall_init(void)
  6563. {
  6564. IOCTLEntry *ie;
  6565. const argtype *arg_type;
  6566. int size;
  6567. thunk_init(STRUCT_MAX);
  6568. #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
  6569. #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
  6570. #include "syscall_types.h"
  6571. #undef STRUCT
  6572. #undef STRUCT_SPECIAL
  6573. /* we patch the ioctl size if necessary. We rely on the fact that
  6574. no ioctl has all the bits at '1' in the size field */
  6575. ie = ioctl_entries;
  6576. while (ie->target_cmd != 0) {
  6577. if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
  6578. TARGET_IOC_SIZEMASK) {
  6579. arg_type = ie->arg_type;
  6580. if (arg_type[0] != TYPE_PTR) {
  6581. fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
  6582. ie->target_cmd);
  6583. exit(1);
  6584. }
  6585. arg_type++;
  6586. size = thunk_type_size(arg_type, 0);
  6587. ie->target_cmd = (ie->target_cmd &
  6588. ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
  6589. (size << TARGET_IOC_SIZESHIFT);
  6590. }
  6591. /* automatic consistency check if same arch */
  6592. #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
  6593. (defined(__x86_64__) && defined(TARGET_X86_64))
  6594. if (unlikely(ie->target_cmd != ie->host_cmd)) {
  6595. fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
  6596. ie->name, ie->target_cmd, ie->host_cmd);
  6597. }
  6598. #endif
  6599. ie++;
  6600. }
  6601. }
  6602. #ifdef TARGET_NR_truncate64
  6603. static inline abi_long target_truncate64(CPUArchState *cpu_env, const char *arg1,
  6604. abi_long arg2,
  6605. abi_long arg3,
  6606. abi_long arg4)
  6607. {
  6608. if (regpairs_aligned(cpu_env, TARGET_NR_truncate64)) {
  6609. arg2 = arg3;
  6610. arg3 = arg4;
  6611. }
  6612. return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
  6613. }
  6614. #endif
  6615. #ifdef TARGET_NR_ftruncate64
  6616. static inline abi_long target_ftruncate64(CPUArchState *cpu_env, abi_long arg1,
  6617. abi_long arg2,
  6618. abi_long arg3,
  6619. abi_long arg4)
  6620. {
  6621. if (regpairs_aligned(cpu_env, TARGET_NR_ftruncate64)) {
  6622. arg2 = arg3;
  6623. arg3 = arg4;
  6624. }
  6625. return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
  6626. }
  6627. #endif
  6628. #if defined(TARGET_NR_timer_settime) || \
  6629. (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
  6630. static inline abi_long target_to_host_itimerspec(struct itimerspec *host_its,
  6631. abi_ulong target_addr)
  6632. {
  6633. if (target_to_host_timespec(&host_its->it_interval, target_addr +
  6634. offsetof(struct target_itimerspec,
  6635. it_interval)) ||
  6636. target_to_host_timespec(&host_its->it_value, target_addr +
  6637. offsetof(struct target_itimerspec,
  6638. it_value))) {
  6639. return -TARGET_EFAULT;
  6640. }
  6641. return 0;
  6642. }
  6643. #endif
  6644. #if defined(TARGET_NR_timer_settime64) || \
  6645. (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD))
  6646. static inline abi_long target_to_host_itimerspec64(struct itimerspec *host_its,
  6647. abi_ulong target_addr)
  6648. {
  6649. if (target_to_host_timespec64(&host_its->it_interval, target_addr +
  6650. offsetof(struct target__kernel_itimerspec,
  6651. it_interval)) ||
  6652. target_to_host_timespec64(&host_its->it_value, target_addr +
  6653. offsetof(struct target__kernel_itimerspec,
  6654. it_value))) {
  6655. return -TARGET_EFAULT;
  6656. }
  6657. return 0;
  6658. }
  6659. #endif
  6660. #if ((defined(TARGET_NR_timerfd_gettime) || \
  6661. defined(TARGET_NR_timerfd_settime)) && defined(CONFIG_TIMERFD)) || \
  6662. defined(TARGET_NR_timer_gettime) || defined(TARGET_NR_timer_settime)
  6663. static inline abi_long host_to_target_itimerspec(abi_ulong target_addr,
  6664. struct itimerspec *host_its)
  6665. {
  6666. if (host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
  6667. it_interval),
  6668. &host_its->it_interval) ||
  6669. host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
  6670. it_value),
  6671. &host_its->it_value)) {
  6672. return -TARGET_EFAULT;
  6673. }
  6674. return 0;
  6675. }
  6676. #endif
  6677. #if ((defined(TARGET_NR_timerfd_gettime64) || \
  6678. defined(TARGET_NR_timerfd_settime64)) && defined(CONFIG_TIMERFD)) || \
  6679. defined(TARGET_NR_timer_gettime64) || defined(TARGET_NR_timer_settime64)
  6680. static inline abi_long host_to_target_itimerspec64(abi_ulong target_addr,
  6681. struct itimerspec *host_its)
  6682. {
  6683. if (host_to_target_timespec64(target_addr +
  6684. offsetof(struct target__kernel_itimerspec,
  6685. it_interval),
  6686. &host_its->it_interval) ||
  6687. host_to_target_timespec64(target_addr +
  6688. offsetof(struct target__kernel_itimerspec,
  6689. it_value),
  6690. &host_its->it_value)) {
  6691. return -TARGET_EFAULT;
  6692. }
  6693. return 0;
  6694. }
  6695. #endif
  6696. #if defined(TARGET_NR_adjtimex) || \
  6697. (defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME))
  6698. static inline abi_long target_to_host_timex(struct timex *host_tx,
  6699. abi_long target_addr)
  6700. {
  6701. struct target_timex *target_tx;
  6702. if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) {
  6703. return -TARGET_EFAULT;
  6704. }
  6705. __get_user(host_tx->modes, &target_tx->modes);
  6706. __get_user(host_tx->offset, &target_tx->offset);
  6707. __get_user(host_tx->freq, &target_tx->freq);
  6708. __get_user(host_tx->maxerror, &target_tx->maxerror);
  6709. __get_user(host_tx->esterror, &target_tx->esterror);
  6710. __get_user(host_tx->status, &target_tx->status);
  6711. __get_user(host_tx->constant, &target_tx->constant);
  6712. __get_user(host_tx->precision, &target_tx->precision);
  6713. __get_user(host_tx->tolerance, &target_tx->tolerance);
  6714. __get_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
  6715. __get_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
  6716. __get_user(host_tx->tick, &target_tx->tick);
  6717. __get_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  6718. __get_user(host_tx->jitter, &target_tx->jitter);
  6719. __get_user(host_tx->shift, &target_tx->shift);
  6720. __get_user(host_tx->stabil, &target_tx->stabil);
  6721. __get_user(host_tx->jitcnt, &target_tx->jitcnt);
  6722. __get_user(host_tx->calcnt, &target_tx->calcnt);
  6723. __get_user(host_tx->errcnt, &target_tx->errcnt);
  6724. __get_user(host_tx->stbcnt, &target_tx->stbcnt);
  6725. __get_user(host_tx->tai, &target_tx->tai);
  6726. unlock_user_struct(target_tx, target_addr, 0);
  6727. return 0;
  6728. }
  6729. static inline abi_long host_to_target_timex(abi_long target_addr,
  6730. struct timex *host_tx)
  6731. {
  6732. struct target_timex *target_tx;
  6733. if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) {
  6734. return -TARGET_EFAULT;
  6735. }
  6736. __put_user(host_tx->modes, &target_tx->modes);
  6737. __put_user(host_tx->offset, &target_tx->offset);
  6738. __put_user(host_tx->freq, &target_tx->freq);
  6739. __put_user(host_tx->maxerror, &target_tx->maxerror);
  6740. __put_user(host_tx->esterror, &target_tx->esterror);
  6741. __put_user(host_tx->status, &target_tx->status);
  6742. __put_user(host_tx->constant, &target_tx->constant);
  6743. __put_user(host_tx->precision, &target_tx->precision);
  6744. __put_user(host_tx->tolerance, &target_tx->tolerance);
  6745. __put_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
  6746. __put_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
  6747. __put_user(host_tx->tick, &target_tx->tick);
  6748. __put_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  6749. __put_user(host_tx->jitter, &target_tx->jitter);
  6750. __put_user(host_tx->shift, &target_tx->shift);
  6751. __put_user(host_tx->stabil, &target_tx->stabil);
  6752. __put_user(host_tx->jitcnt, &target_tx->jitcnt);
  6753. __put_user(host_tx->calcnt, &target_tx->calcnt);
  6754. __put_user(host_tx->errcnt, &target_tx->errcnt);
  6755. __put_user(host_tx->stbcnt, &target_tx->stbcnt);
  6756. __put_user(host_tx->tai, &target_tx->tai);
  6757. unlock_user_struct(target_tx, target_addr, 1);
  6758. return 0;
  6759. }
  6760. #endif
  6761. #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
  6762. static inline abi_long target_to_host_timex64(struct timex *host_tx,
  6763. abi_long target_addr)
  6764. {
  6765. struct target__kernel_timex *target_tx;
  6766. if (copy_from_user_timeval64(&host_tx->time, target_addr +
  6767. offsetof(struct target__kernel_timex,
  6768. time))) {
  6769. return -TARGET_EFAULT;
  6770. }
  6771. if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) {
  6772. return -TARGET_EFAULT;
  6773. }
  6774. __get_user(host_tx->modes, &target_tx->modes);
  6775. __get_user(host_tx->offset, &target_tx->offset);
  6776. __get_user(host_tx->freq, &target_tx->freq);
  6777. __get_user(host_tx->maxerror, &target_tx->maxerror);
  6778. __get_user(host_tx->esterror, &target_tx->esterror);
  6779. __get_user(host_tx->status, &target_tx->status);
  6780. __get_user(host_tx->constant, &target_tx->constant);
  6781. __get_user(host_tx->precision, &target_tx->precision);
  6782. __get_user(host_tx->tolerance, &target_tx->tolerance);
  6783. __get_user(host_tx->tick, &target_tx->tick);
  6784. __get_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  6785. __get_user(host_tx->jitter, &target_tx->jitter);
  6786. __get_user(host_tx->shift, &target_tx->shift);
  6787. __get_user(host_tx->stabil, &target_tx->stabil);
  6788. __get_user(host_tx->jitcnt, &target_tx->jitcnt);
  6789. __get_user(host_tx->calcnt, &target_tx->calcnt);
  6790. __get_user(host_tx->errcnt, &target_tx->errcnt);
  6791. __get_user(host_tx->stbcnt, &target_tx->stbcnt);
  6792. __get_user(host_tx->tai, &target_tx->tai);
  6793. unlock_user_struct(target_tx, target_addr, 0);
  6794. return 0;
  6795. }
  6796. static inline abi_long host_to_target_timex64(abi_long target_addr,
  6797. struct timex *host_tx)
  6798. {
  6799. struct target__kernel_timex *target_tx;
  6800. if (copy_to_user_timeval64(target_addr +
  6801. offsetof(struct target__kernel_timex, time),
  6802. &host_tx->time)) {
  6803. return -TARGET_EFAULT;
  6804. }
  6805. if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) {
  6806. return -TARGET_EFAULT;
  6807. }
  6808. __put_user(host_tx->modes, &target_tx->modes);
  6809. __put_user(host_tx->offset, &target_tx->offset);
  6810. __put_user(host_tx->freq, &target_tx->freq);
  6811. __put_user(host_tx->maxerror, &target_tx->maxerror);
  6812. __put_user(host_tx->esterror, &target_tx->esterror);
  6813. __put_user(host_tx->status, &target_tx->status);
  6814. __put_user(host_tx->constant, &target_tx->constant);
  6815. __put_user(host_tx->precision, &target_tx->precision);
  6816. __put_user(host_tx->tolerance, &target_tx->tolerance);
  6817. __put_user(host_tx->tick, &target_tx->tick);
  6818. __put_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  6819. __put_user(host_tx->jitter, &target_tx->jitter);
  6820. __put_user(host_tx->shift, &target_tx->shift);
  6821. __put_user(host_tx->stabil, &target_tx->stabil);
  6822. __put_user(host_tx->jitcnt, &target_tx->jitcnt);
  6823. __put_user(host_tx->calcnt, &target_tx->calcnt);
  6824. __put_user(host_tx->errcnt, &target_tx->errcnt);
  6825. __put_user(host_tx->stbcnt, &target_tx->stbcnt);
  6826. __put_user(host_tx->tai, &target_tx->tai);
  6827. unlock_user_struct(target_tx, target_addr, 1);
  6828. return 0;
  6829. }
  6830. #endif
  6831. #ifndef HAVE_SIGEV_NOTIFY_THREAD_ID
  6832. #define sigev_notify_thread_id _sigev_un._tid
  6833. #endif
  6834. static inline abi_long target_to_host_sigevent(struct sigevent *host_sevp,
  6835. abi_ulong target_addr)
  6836. {
  6837. struct target_sigevent *target_sevp;
  6838. if (!lock_user_struct(VERIFY_READ, target_sevp, target_addr, 1)) {
  6839. return -TARGET_EFAULT;
  6840. }
  6841. /* This union is awkward on 64 bit systems because it has a 32 bit
  6842. * integer and a pointer in it; we follow the conversion approach
  6843. * used for handling sigval types in signal.c so the guest should get
  6844. * the correct value back even if we did a 64 bit byteswap and it's
  6845. * using the 32 bit integer.
  6846. */
  6847. host_sevp->sigev_value.sival_ptr =
  6848. (void *)(uintptr_t)tswapal(target_sevp->sigev_value.sival_ptr);
  6849. host_sevp->sigev_signo =
  6850. target_to_host_signal(tswap32(target_sevp->sigev_signo));
  6851. host_sevp->sigev_notify = tswap32(target_sevp->sigev_notify);
  6852. host_sevp->sigev_notify_thread_id = tswap32(target_sevp->_sigev_un._tid);
  6853. unlock_user_struct(target_sevp, target_addr, 1);
  6854. return 0;
  6855. }
  6856. #if defined(TARGET_NR_mlockall)
  6857. static inline int target_to_host_mlockall_arg(int arg)
  6858. {
  6859. int result = 0;
  6860. if (arg & TARGET_MCL_CURRENT) {
  6861. result |= MCL_CURRENT;
  6862. }
  6863. if (arg & TARGET_MCL_FUTURE) {
  6864. result |= MCL_FUTURE;
  6865. }
  6866. #ifdef MCL_ONFAULT
  6867. if (arg & TARGET_MCL_ONFAULT) {
  6868. result |= MCL_ONFAULT;
  6869. }
  6870. #endif
  6871. return result;
  6872. }
  6873. #endif
  6874. #if (defined(TARGET_NR_stat64) || defined(TARGET_NR_lstat64) || \
  6875. defined(TARGET_NR_fstat64) || defined(TARGET_NR_fstatat64) || \
  6876. defined(TARGET_NR_newfstatat))
  6877. static inline abi_long host_to_target_stat64(CPUArchState *cpu_env,
  6878. abi_ulong target_addr,
  6879. struct stat *host_st)
  6880. {
  6881. #if defined(TARGET_ARM) && defined(TARGET_ABI32)
  6882. if (cpu_env->eabi) {
  6883. struct target_eabi_stat64 *target_st;
  6884. if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
  6885. return -TARGET_EFAULT;
  6886. memset(target_st, 0, sizeof(struct target_eabi_stat64));
  6887. __put_user(host_st->st_dev, &target_st->st_dev);
  6888. __put_user(host_st->st_ino, &target_st->st_ino);
  6889. #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
  6890. __put_user(host_st->st_ino, &target_st->__st_ino);
  6891. #endif
  6892. __put_user(host_st->st_mode, &target_st->st_mode);
  6893. __put_user(host_st->st_nlink, &target_st->st_nlink);
  6894. __put_user(host_st->st_uid, &target_st->st_uid);
  6895. __put_user(host_st->st_gid, &target_st->st_gid);
  6896. __put_user(host_st->st_rdev, &target_st->st_rdev);
  6897. __put_user(host_st->st_size, &target_st->st_size);
  6898. __put_user(host_st->st_blksize, &target_st->st_blksize);
  6899. __put_user(host_st->st_blocks, &target_st->st_blocks);
  6900. __put_user(host_st->st_atime, &target_st->target_st_atime);
  6901. __put_user(host_st->st_mtime, &target_st->target_st_mtime);
  6902. __put_user(host_st->st_ctime, &target_st->target_st_ctime);
  6903. #ifdef HAVE_STRUCT_STAT_ST_ATIM
  6904. __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
  6905. __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
  6906. __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
  6907. #endif
  6908. unlock_user_struct(target_st, target_addr, 1);
  6909. } else
  6910. #endif
  6911. {
  6912. #if defined(TARGET_HAS_STRUCT_STAT64)
  6913. struct target_stat64 *target_st;
  6914. #else
  6915. struct target_stat *target_st;
  6916. #endif
  6917. if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
  6918. return -TARGET_EFAULT;
  6919. memset(target_st, 0, sizeof(*target_st));
  6920. __put_user(host_st->st_dev, &target_st->st_dev);
  6921. __put_user(host_st->st_ino, &target_st->st_ino);
  6922. #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
  6923. __put_user(host_st->st_ino, &target_st->__st_ino);
  6924. #endif
  6925. __put_user(host_st->st_mode, &target_st->st_mode);
  6926. __put_user(host_st->st_nlink, &target_st->st_nlink);
  6927. __put_user(host_st->st_uid, &target_st->st_uid);
  6928. __put_user(host_st->st_gid, &target_st->st_gid);
  6929. __put_user(host_st->st_rdev, &target_st->st_rdev);
  6930. /* XXX: better use of kernel struct */
  6931. __put_user(host_st->st_size, &target_st->st_size);
  6932. __put_user(host_st->st_blksize, &target_st->st_blksize);
  6933. __put_user(host_st->st_blocks, &target_st->st_blocks);
  6934. __put_user(host_st->st_atime, &target_st->target_st_atime);
  6935. __put_user(host_st->st_mtime, &target_st->target_st_mtime);
  6936. __put_user(host_st->st_ctime, &target_st->target_st_ctime);
  6937. #ifdef HAVE_STRUCT_STAT_ST_ATIM
  6938. __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
  6939. __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
  6940. __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
  6941. #endif
  6942. unlock_user_struct(target_st, target_addr, 1);
  6943. }
  6944. return 0;
  6945. }
  6946. #endif
  6947. #if defined(TARGET_NR_statx) && defined(__NR_statx)
  6948. static inline abi_long host_to_target_statx(struct target_statx *host_stx,
  6949. abi_ulong target_addr)
  6950. {
  6951. struct target_statx *target_stx;
  6952. if (!lock_user_struct(VERIFY_WRITE, target_stx, target_addr, 0)) {
  6953. return -TARGET_EFAULT;
  6954. }
  6955. memset(target_stx, 0, sizeof(*target_stx));
  6956. __put_user(host_stx->stx_mask, &target_stx->stx_mask);
  6957. __put_user(host_stx->stx_blksize, &target_stx->stx_blksize);
  6958. __put_user(host_stx->stx_attributes, &target_stx->stx_attributes);
  6959. __put_user(host_stx->stx_nlink, &target_stx->stx_nlink);
  6960. __put_user(host_stx->stx_uid, &target_stx->stx_uid);
  6961. __put_user(host_stx->stx_gid, &target_stx->stx_gid);
  6962. __put_user(host_stx->stx_mode, &target_stx->stx_mode);
  6963. __put_user(host_stx->stx_ino, &target_stx->stx_ino);
  6964. __put_user(host_stx->stx_size, &target_stx->stx_size);
  6965. __put_user(host_stx->stx_blocks, &target_stx->stx_blocks);
  6966. __put_user(host_stx->stx_attributes_mask, &target_stx->stx_attributes_mask);
  6967. __put_user(host_stx->stx_atime.tv_sec, &target_stx->stx_atime.tv_sec);
  6968. __put_user(host_stx->stx_atime.tv_nsec, &target_stx->stx_atime.tv_nsec);
  6969. __put_user(host_stx->stx_btime.tv_sec, &target_stx->stx_btime.tv_sec);
  6970. __put_user(host_stx->stx_btime.tv_nsec, &target_stx->stx_btime.tv_nsec);
  6971. __put_user(host_stx->stx_ctime.tv_sec, &target_stx->stx_ctime.tv_sec);
  6972. __put_user(host_stx->stx_ctime.tv_nsec, &target_stx->stx_ctime.tv_nsec);
  6973. __put_user(host_stx->stx_mtime.tv_sec, &target_stx->stx_mtime.tv_sec);
  6974. __put_user(host_stx->stx_mtime.tv_nsec, &target_stx->stx_mtime.tv_nsec);
  6975. __put_user(host_stx->stx_rdev_major, &target_stx->stx_rdev_major);
  6976. __put_user(host_stx->stx_rdev_minor, &target_stx->stx_rdev_minor);
  6977. __put_user(host_stx->stx_dev_major, &target_stx->stx_dev_major);
  6978. __put_user(host_stx->stx_dev_minor, &target_stx->stx_dev_minor);
  6979. unlock_user_struct(target_stx, target_addr, 1);
  6980. return 0;
  6981. }
  6982. #endif
  6983. static int do_sys_futex(int *uaddr, int op, int val,
  6984. const struct timespec *timeout, int *uaddr2,
  6985. int val3)
  6986. {
  6987. #if HOST_LONG_BITS == 64
  6988. #if defined(__NR_futex)
  6989. /* always a 64-bit time_t, it doesn't define _time64 version */
  6990. return sys_futex(uaddr, op, val, timeout, uaddr2, val3);
  6991. #endif
  6992. #else /* HOST_LONG_BITS == 64 */
  6993. #if defined(__NR_futex_time64)
  6994. if (sizeof(timeout->tv_sec) == 8) {
  6995. /* _time64 function on 32bit arch */
  6996. return sys_futex_time64(uaddr, op, val, timeout, uaddr2, val3);
  6997. }
  6998. #endif
  6999. #if defined(__NR_futex)
  7000. /* old function on 32bit arch */
  7001. return sys_futex(uaddr, op, val, timeout, uaddr2, val3);
  7002. #endif
  7003. #endif /* HOST_LONG_BITS == 64 */
  7004. g_assert_not_reached();
  7005. }
  7006. static int do_safe_futex(int *uaddr, int op, int val,
  7007. const struct timespec *timeout, int *uaddr2,
  7008. int val3)
  7009. {
  7010. #if HOST_LONG_BITS == 64
  7011. #if defined(__NR_futex)
  7012. /* always a 64-bit time_t, it doesn't define _time64 version */
  7013. return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3));
  7014. #endif
  7015. #else /* HOST_LONG_BITS == 64 */
  7016. #if defined(__NR_futex_time64)
  7017. if (sizeof(timeout->tv_sec) == 8) {
  7018. /* _time64 function on 32bit arch */
  7019. return get_errno(safe_futex_time64(uaddr, op, val, timeout, uaddr2,
  7020. val3));
  7021. }
  7022. #endif
  7023. #if defined(__NR_futex)
  7024. /* old function on 32bit arch */
  7025. return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3));
  7026. #endif
  7027. #endif /* HOST_LONG_BITS == 64 */
  7028. return -TARGET_ENOSYS;
  7029. }
  7030. /* ??? Using host futex calls even when target atomic operations
  7031. are not really atomic probably breaks things. However implementing
  7032. futexes locally would make futexes shared between multiple processes
  7033. tricky. However they're probably useless because guest atomic
  7034. operations won't work either. */
  7035. #if defined(TARGET_NR_futex) || defined(TARGET_NR_futex_time64)
  7036. static int do_futex(CPUState *cpu, bool time64, target_ulong uaddr,
  7037. int op, int val, target_ulong timeout,
  7038. target_ulong uaddr2, int val3)
  7039. {
  7040. struct timespec ts, *pts = NULL;
  7041. void *haddr2 = NULL;
  7042. int base_op;
  7043. /* We assume FUTEX_* constants are the same on both host and target. */
  7044. #ifdef FUTEX_CMD_MASK
  7045. base_op = op & FUTEX_CMD_MASK;
  7046. #else
  7047. base_op = op;
  7048. #endif
  7049. switch (base_op) {
  7050. case FUTEX_WAIT:
  7051. case FUTEX_WAIT_BITSET:
  7052. val = tswap32(val);
  7053. break;
  7054. case FUTEX_WAIT_REQUEUE_PI:
  7055. val = tswap32(val);
  7056. haddr2 = g2h(cpu, uaddr2);
  7057. break;
  7058. case FUTEX_LOCK_PI:
  7059. case FUTEX_LOCK_PI2:
  7060. break;
  7061. case FUTEX_WAKE:
  7062. case FUTEX_WAKE_BITSET:
  7063. case FUTEX_TRYLOCK_PI:
  7064. case FUTEX_UNLOCK_PI:
  7065. timeout = 0;
  7066. break;
  7067. case FUTEX_FD:
  7068. val = target_to_host_signal(val);
  7069. timeout = 0;
  7070. break;
  7071. case FUTEX_CMP_REQUEUE:
  7072. case FUTEX_CMP_REQUEUE_PI:
  7073. val3 = tswap32(val3);
  7074. /* fall through */
  7075. case FUTEX_REQUEUE:
  7076. case FUTEX_WAKE_OP:
  7077. /*
  7078. * For these, the 4th argument is not TIMEOUT, but VAL2.
  7079. * But the prototype of do_safe_futex takes a pointer, so
  7080. * insert casts to satisfy the compiler. We do not need
  7081. * to tswap VAL2 since it's not compared to guest memory.
  7082. */
  7083. pts = (struct timespec *)(uintptr_t)timeout;
  7084. timeout = 0;
  7085. haddr2 = g2h(cpu, uaddr2);
  7086. break;
  7087. default:
  7088. return -TARGET_ENOSYS;
  7089. }
  7090. if (timeout) {
  7091. pts = &ts;
  7092. if (time64
  7093. ? target_to_host_timespec64(pts, timeout)
  7094. : target_to_host_timespec(pts, timeout)) {
  7095. return -TARGET_EFAULT;
  7096. }
  7097. }
  7098. return do_safe_futex(g2h(cpu, uaddr), op, val, pts, haddr2, val3);
  7099. }
  7100. #endif
  7101. #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  7102. static abi_long do_name_to_handle_at(abi_long dirfd, abi_long pathname,
  7103. abi_long handle, abi_long mount_id,
  7104. abi_long flags)
  7105. {
  7106. struct file_handle *target_fh;
  7107. struct file_handle *fh;
  7108. int mid = 0;
  7109. abi_long ret;
  7110. char *name;
  7111. unsigned int size, total_size;
  7112. if (get_user_s32(size, handle)) {
  7113. return -TARGET_EFAULT;
  7114. }
  7115. name = lock_user_string(pathname);
  7116. if (!name) {
  7117. return -TARGET_EFAULT;
  7118. }
  7119. total_size = sizeof(struct file_handle) + size;
  7120. target_fh = lock_user(VERIFY_WRITE, handle, total_size, 0);
  7121. if (!target_fh) {
  7122. unlock_user(name, pathname, 0);
  7123. return -TARGET_EFAULT;
  7124. }
  7125. fh = g_malloc0(total_size);
  7126. fh->handle_bytes = size;
  7127. ret = get_errno(name_to_handle_at(dirfd, path(name), fh, &mid, flags));
  7128. unlock_user(name, pathname, 0);
  7129. /* man name_to_handle_at(2):
  7130. * Other than the use of the handle_bytes field, the caller should treat
  7131. * the file_handle structure as an opaque data type
  7132. */
  7133. memcpy(target_fh, fh, total_size);
  7134. target_fh->handle_bytes = tswap32(fh->handle_bytes);
  7135. target_fh->handle_type = tswap32(fh->handle_type);
  7136. g_free(fh);
  7137. unlock_user(target_fh, handle, total_size);
  7138. if (put_user_s32(mid, mount_id)) {
  7139. return -TARGET_EFAULT;
  7140. }
  7141. return ret;
  7142. }
  7143. #endif
  7144. #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  7145. static abi_long do_open_by_handle_at(abi_long mount_fd, abi_long handle,
  7146. abi_long flags)
  7147. {
  7148. struct file_handle *target_fh;
  7149. struct file_handle *fh;
  7150. unsigned int size, total_size;
  7151. abi_long ret;
  7152. if (get_user_s32(size, handle)) {
  7153. return -TARGET_EFAULT;
  7154. }
  7155. total_size = sizeof(struct file_handle) + size;
  7156. target_fh = lock_user(VERIFY_READ, handle, total_size, 1);
  7157. if (!target_fh) {
  7158. return -TARGET_EFAULT;
  7159. }
  7160. fh = g_memdup(target_fh, total_size);
  7161. fh->handle_bytes = size;
  7162. fh->handle_type = tswap32(target_fh->handle_type);
  7163. ret = get_errno(open_by_handle_at(mount_fd, fh,
  7164. target_to_host_bitmask(flags, fcntl_flags_tbl)));
  7165. g_free(fh);
  7166. unlock_user(target_fh, handle, total_size);
  7167. return ret;
  7168. }
  7169. #endif
  7170. #if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4)
  7171. static abi_long do_signalfd4(int fd, abi_long mask, int flags)
  7172. {
  7173. int host_flags;
  7174. target_sigset_t *target_mask;
  7175. sigset_t host_mask;
  7176. abi_long ret;
  7177. if (flags & ~(TARGET_O_NONBLOCK_MASK | TARGET_O_CLOEXEC)) {
  7178. return -TARGET_EINVAL;
  7179. }
  7180. if (!lock_user_struct(VERIFY_READ, target_mask, mask, 1)) {
  7181. return -TARGET_EFAULT;
  7182. }
  7183. target_to_host_sigset(&host_mask, target_mask);
  7184. host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
  7185. ret = get_errno(signalfd(fd, &host_mask, host_flags));
  7186. if (ret >= 0) {
  7187. fd_trans_register(ret, &target_signalfd_trans);
  7188. }
  7189. unlock_user_struct(target_mask, mask, 0);
  7190. return ret;
  7191. }
  7192. #endif
  7193. /* Map host to target signal numbers for the wait family of syscalls.
  7194. Assume all other status bits are the same. */
  7195. int host_to_target_waitstatus(int status)
  7196. {
  7197. if (WIFSIGNALED(status)) {
  7198. return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
  7199. }
  7200. if (WIFSTOPPED(status)) {
  7201. return (host_to_target_signal(WSTOPSIG(status)) << 8)
  7202. | (status & 0xff);
  7203. }
  7204. return status;
  7205. }
  7206. static int open_self_cmdline(CPUArchState *cpu_env, int fd)
  7207. {
  7208. CPUState *cpu = env_cpu(cpu_env);
  7209. struct linux_binprm *bprm = ((TaskState *)cpu->opaque)->bprm;
  7210. int i;
  7211. for (i = 0; i < bprm->argc; i++) {
  7212. size_t len = strlen(bprm->argv[i]) + 1;
  7213. if (write(fd, bprm->argv[i], len) != len) {
  7214. return -1;
  7215. }
  7216. }
  7217. return 0;
  7218. }
  7219. static int open_self_maps(CPUArchState *cpu_env, int fd)
  7220. {
  7221. CPUState *cpu = env_cpu(cpu_env);
  7222. TaskState *ts = cpu->opaque;
  7223. GSList *map_info = read_self_maps();
  7224. GSList *s;
  7225. int count;
  7226. for (s = map_info; s; s = g_slist_next(s)) {
  7227. MapInfo *e = (MapInfo *) s->data;
  7228. if (h2g_valid(e->start)) {
  7229. unsigned long min = e->start;
  7230. unsigned long max = e->end;
  7231. int flags = page_get_flags(h2g(min));
  7232. const char *path;
  7233. max = h2g_valid(max - 1) ?
  7234. max : (uintptr_t) g2h_untagged(GUEST_ADDR_MAX) + 1;
  7235. if (page_check_range(h2g(min), max - min, flags) == -1) {
  7236. continue;
  7237. }
  7238. #ifdef TARGET_HPPA
  7239. if (h2g(max) == ts->info->stack_limit) {
  7240. #else
  7241. if (h2g(min) == ts->info->stack_limit) {
  7242. #endif
  7243. path = "[stack]";
  7244. } else {
  7245. path = e->path;
  7246. }
  7247. count = dprintf(fd, TARGET_ABI_FMT_ptr "-" TARGET_ABI_FMT_ptr
  7248. " %c%c%c%c %08" PRIx64 " %s %"PRId64,
  7249. h2g(min), h2g(max - 1) + 1,
  7250. (flags & PAGE_READ) ? 'r' : '-',
  7251. (flags & PAGE_WRITE_ORG) ? 'w' : '-',
  7252. (flags & PAGE_EXEC) ? 'x' : '-',
  7253. e->is_priv ? 'p' : 's',
  7254. (uint64_t) e->offset, e->dev, e->inode);
  7255. if (path) {
  7256. dprintf(fd, "%*s%s\n", 73 - count, "", path);
  7257. } else {
  7258. dprintf(fd, "\n");
  7259. }
  7260. }
  7261. }
  7262. free_self_maps(map_info);
  7263. #ifdef TARGET_VSYSCALL_PAGE
  7264. /*
  7265. * We only support execution from the vsyscall page.
  7266. * This is as if CONFIG_LEGACY_VSYSCALL_XONLY=y from v5.3.
  7267. */
  7268. count = dprintf(fd, TARGET_FMT_lx "-" TARGET_FMT_lx
  7269. " --xp 00000000 00:00 0",
  7270. TARGET_VSYSCALL_PAGE, TARGET_VSYSCALL_PAGE + TARGET_PAGE_SIZE);
  7271. dprintf(fd, "%*s%s\n", 73 - count, "", "[vsyscall]");
  7272. #endif
  7273. return 0;
  7274. }
  7275. static int open_self_stat(CPUArchState *cpu_env, int fd)
  7276. {
  7277. CPUState *cpu = env_cpu(cpu_env);
  7278. TaskState *ts = cpu->opaque;
  7279. g_autoptr(GString) buf = g_string_new(NULL);
  7280. int i;
  7281. for (i = 0; i < 44; i++) {
  7282. if (i == 0) {
  7283. /* pid */
  7284. g_string_printf(buf, FMT_pid " ", getpid());
  7285. } else if (i == 1) {
  7286. /* app name */
  7287. gchar *bin = g_strrstr(ts->bprm->argv[0], "/");
  7288. bin = bin ? bin + 1 : ts->bprm->argv[0];
  7289. g_string_printf(buf, "(%.15s) ", bin);
  7290. } else if (i == 3) {
  7291. /* ppid */
  7292. g_string_printf(buf, FMT_pid " ", getppid());
  7293. } else if (i == 21) {
  7294. /* starttime */
  7295. g_string_printf(buf, "%" PRIu64 " ", ts->start_boottime);
  7296. } else if (i == 27) {
  7297. /* stack bottom */
  7298. g_string_printf(buf, TARGET_ABI_FMT_ld " ", ts->info->start_stack);
  7299. } else {
  7300. /* for the rest, there is MasterCard */
  7301. g_string_printf(buf, "0%c", i == 43 ? '\n' : ' ');
  7302. }
  7303. if (write(fd, buf->str, buf->len) != buf->len) {
  7304. return -1;
  7305. }
  7306. }
  7307. return 0;
  7308. }
  7309. static int open_self_auxv(CPUArchState *cpu_env, int fd)
  7310. {
  7311. CPUState *cpu = env_cpu(cpu_env);
  7312. TaskState *ts = cpu->opaque;
  7313. abi_ulong auxv = ts->info->saved_auxv;
  7314. abi_ulong len = ts->info->auxv_len;
  7315. char *ptr;
  7316. /*
  7317. * Auxiliary vector is stored in target process stack.
  7318. * read in whole auxv vector and copy it to file
  7319. */
  7320. ptr = lock_user(VERIFY_READ, auxv, len, 0);
  7321. if (ptr != NULL) {
  7322. while (len > 0) {
  7323. ssize_t r;
  7324. r = write(fd, ptr, len);
  7325. if (r <= 0) {
  7326. break;
  7327. }
  7328. len -= r;
  7329. ptr += r;
  7330. }
  7331. lseek(fd, 0, SEEK_SET);
  7332. unlock_user(ptr, auxv, len);
  7333. }
  7334. return 0;
  7335. }
  7336. static int is_proc_myself(const char *filename, const char *entry)
  7337. {
  7338. if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
  7339. filename += strlen("/proc/");
  7340. if (!strncmp(filename, "self/", strlen("self/"))) {
  7341. filename += strlen("self/");
  7342. } else if (*filename >= '1' && *filename <= '9') {
  7343. char myself[80];
  7344. snprintf(myself, sizeof(myself), "%d/", getpid());
  7345. if (!strncmp(filename, myself, strlen(myself))) {
  7346. filename += strlen(myself);
  7347. } else {
  7348. return 0;
  7349. }
  7350. } else {
  7351. return 0;
  7352. }
  7353. if (!strcmp(filename, entry)) {
  7354. return 1;
  7355. }
  7356. }
  7357. return 0;
  7358. }
  7359. static void excp_dump_file(FILE *logfile, CPUArchState *env,
  7360. const char *fmt, int code)
  7361. {
  7362. if (logfile) {
  7363. CPUState *cs = env_cpu(env);
  7364. fprintf(logfile, fmt, code);
  7365. fprintf(logfile, "Failing executable: %s\n", exec_path);
  7366. cpu_dump_state(cs, logfile, 0);
  7367. open_self_maps(env, fileno(logfile));
  7368. }
  7369. }
  7370. void target_exception_dump(CPUArchState *env, const char *fmt, int code)
  7371. {
  7372. /* dump to console */
  7373. excp_dump_file(stderr, env, fmt, code);
  7374. /* dump to log file */
  7375. if (qemu_log_separate()) {
  7376. FILE *logfile = qemu_log_trylock();
  7377. excp_dump_file(logfile, env, fmt, code);
  7378. qemu_log_unlock(logfile);
  7379. }
  7380. }
  7381. #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN || \
  7382. defined(TARGET_SPARC) || defined(TARGET_M68K) || defined(TARGET_HPPA)
  7383. static int is_proc(const char *filename, const char *entry)
  7384. {
  7385. return strcmp(filename, entry) == 0;
  7386. }
  7387. #endif
  7388. #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
  7389. static int open_net_route(CPUArchState *cpu_env, int fd)
  7390. {
  7391. FILE *fp;
  7392. char *line = NULL;
  7393. size_t len = 0;
  7394. ssize_t read;
  7395. fp = fopen("/proc/net/route", "r");
  7396. if (fp == NULL) {
  7397. return -1;
  7398. }
  7399. /* read header */
  7400. read = getline(&line, &len, fp);
  7401. dprintf(fd, "%s", line);
  7402. /* read routes */
  7403. while ((read = getline(&line, &len, fp)) != -1) {
  7404. char iface[16];
  7405. uint32_t dest, gw, mask;
  7406. unsigned int flags, refcnt, use, metric, mtu, window, irtt;
  7407. int fields;
  7408. fields = sscanf(line,
  7409. "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
  7410. iface, &dest, &gw, &flags, &refcnt, &use, &metric,
  7411. &mask, &mtu, &window, &irtt);
  7412. if (fields != 11) {
  7413. continue;
  7414. }
  7415. dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
  7416. iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
  7417. metric, tswap32(mask), mtu, window, irtt);
  7418. }
  7419. free(line);
  7420. fclose(fp);
  7421. return 0;
  7422. }
  7423. #endif
  7424. #if defined(TARGET_SPARC)
  7425. static int open_cpuinfo(CPUArchState *cpu_env, int fd)
  7426. {
  7427. dprintf(fd, "type\t\t: sun4u\n");
  7428. return 0;
  7429. }
  7430. #endif
  7431. #if defined(TARGET_HPPA)
  7432. static int open_cpuinfo(CPUArchState *cpu_env, int fd)
  7433. {
  7434. dprintf(fd, "cpu family\t: PA-RISC 1.1e\n");
  7435. dprintf(fd, "cpu\t\t: PA7300LC (PCX-L2)\n");
  7436. dprintf(fd, "capabilities\t: os32\n");
  7437. dprintf(fd, "model\t\t: 9000/778/B160L\n");
  7438. dprintf(fd, "model name\t: Merlin L2 160 QEMU (9000/778/B160L)\n");
  7439. return 0;
  7440. }
  7441. #endif
  7442. #if defined(TARGET_M68K)
  7443. static int open_hardware(CPUArchState *cpu_env, int fd)
  7444. {
  7445. dprintf(fd, "Model:\t\tqemu-m68k\n");
  7446. return 0;
  7447. }
  7448. #endif
  7449. static int do_openat(CPUArchState *cpu_env, int dirfd, const char *pathname, int flags, mode_t mode)
  7450. {
  7451. struct fake_open {
  7452. const char *filename;
  7453. int (*fill)(CPUArchState *cpu_env, int fd);
  7454. int (*cmp)(const char *s1, const char *s2);
  7455. };
  7456. const struct fake_open *fake_open;
  7457. static const struct fake_open fakes[] = {
  7458. { "maps", open_self_maps, is_proc_myself },
  7459. { "stat", open_self_stat, is_proc_myself },
  7460. { "auxv", open_self_auxv, is_proc_myself },
  7461. { "cmdline", open_self_cmdline, is_proc_myself },
  7462. #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
  7463. { "/proc/net/route", open_net_route, is_proc },
  7464. #endif
  7465. #if defined(TARGET_SPARC) || defined(TARGET_HPPA)
  7466. { "/proc/cpuinfo", open_cpuinfo, is_proc },
  7467. #endif
  7468. #if defined(TARGET_M68K)
  7469. { "/proc/hardware", open_hardware, is_proc },
  7470. #endif
  7471. { NULL, NULL, NULL }
  7472. };
  7473. if (is_proc_myself(pathname, "exe")) {
  7474. return safe_openat(dirfd, exec_path, flags, mode);
  7475. }
  7476. for (fake_open = fakes; fake_open->filename; fake_open++) {
  7477. if (fake_open->cmp(pathname, fake_open->filename)) {
  7478. break;
  7479. }
  7480. }
  7481. if (fake_open->filename) {
  7482. const char *tmpdir;
  7483. char filename[PATH_MAX];
  7484. int fd, r;
  7485. fd = memfd_create("qemu-open", 0);
  7486. if (fd < 0) {
  7487. if (errno != ENOSYS) {
  7488. return fd;
  7489. }
  7490. /* create temporary file to map stat to */
  7491. tmpdir = getenv("TMPDIR");
  7492. if (!tmpdir)
  7493. tmpdir = "/tmp";
  7494. snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
  7495. fd = mkstemp(filename);
  7496. if (fd < 0) {
  7497. return fd;
  7498. }
  7499. unlink(filename);
  7500. }
  7501. if ((r = fake_open->fill(cpu_env, fd))) {
  7502. int e = errno;
  7503. close(fd);
  7504. errno = e;
  7505. return r;
  7506. }
  7507. lseek(fd, 0, SEEK_SET);
  7508. return fd;
  7509. }
  7510. return safe_openat(dirfd, path(pathname), flags, mode);
  7511. }
  7512. #define TIMER_MAGIC 0x0caf0000
  7513. #define TIMER_MAGIC_MASK 0xffff0000
  7514. /* Convert QEMU provided timer ID back to internal 16bit index format */
  7515. static target_timer_t get_timer_id(abi_long arg)
  7516. {
  7517. target_timer_t timerid = arg;
  7518. if ((timerid & TIMER_MAGIC_MASK) != TIMER_MAGIC) {
  7519. return -TARGET_EINVAL;
  7520. }
  7521. timerid &= 0xffff;
  7522. if (timerid >= ARRAY_SIZE(g_posix_timers)) {
  7523. return -TARGET_EINVAL;
  7524. }
  7525. return timerid;
  7526. }
  7527. static int target_to_host_cpu_mask(unsigned long *host_mask,
  7528. size_t host_size,
  7529. abi_ulong target_addr,
  7530. size_t target_size)
  7531. {
  7532. unsigned target_bits = sizeof(abi_ulong) * 8;
  7533. unsigned host_bits = sizeof(*host_mask) * 8;
  7534. abi_ulong *target_mask;
  7535. unsigned i, j;
  7536. assert(host_size >= target_size);
  7537. target_mask = lock_user(VERIFY_READ, target_addr, target_size, 1);
  7538. if (!target_mask) {
  7539. return -TARGET_EFAULT;
  7540. }
  7541. memset(host_mask, 0, host_size);
  7542. for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
  7543. unsigned bit = i * target_bits;
  7544. abi_ulong val;
  7545. __get_user(val, &target_mask[i]);
  7546. for (j = 0; j < target_bits; j++, bit++) {
  7547. if (val & (1UL << j)) {
  7548. host_mask[bit / host_bits] |= 1UL << (bit % host_bits);
  7549. }
  7550. }
  7551. }
  7552. unlock_user(target_mask, target_addr, 0);
  7553. return 0;
  7554. }
  7555. static int host_to_target_cpu_mask(const unsigned long *host_mask,
  7556. size_t host_size,
  7557. abi_ulong target_addr,
  7558. size_t target_size)
  7559. {
  7560. unsigned target_bits = sizeof(abi_ulong) * 8;
  7561. unsigned host_bits = sizeof(*host_mask) * 8;
  7562. abi_ulong *target_mask;
  7563. unsigned i, j;
  7564. assert(host_size >= target_size);
  7565. target_mask = lock_user(VERIFY_WRITE, target_addr, target_size, 0);
  7566. if (!target_mask) {
  7567. return -TARGET_EFAULT;
  7568. }
  7569. for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
  7570. unsigned bit = i * target_bits;
  7571. abi_ulong val = 0;
  7572. for (j = 0; j < target_bits; j++, bit++) {
  7573. if (host_mask[bit / host_bits] & (1UL << (bit % host_bits))) {
  7574. val |= 1UL << j;
  7575. }
  7576. }
  7577. __put_user(val, &target_mask[i]);
  7578. }
  7579. unlock_user(target_mask, target_addr, target_size);
  7580. return 0;
  7581. }
  7582. #ifdef TARGET_NR_getdents
  7583. static int do_getdents(abi_long dirfd, abi_long arg2, abi_long count)
  7584. {
  7585. g_autofree void *hdirp = NULL;
  7586. void *tdirp;
  7587. int hlen, hoff, toff;
  7588. int hreclen, treclen;
  7589. off64_t prev_diroff = 0;
  7590. hdirp = g_try_malloc(count);
  7591. if (!hdirp) {
  7592. return -TARGET_ENOMEM;
  7593. }
  7594. #ifdef EMULATE_GETDENTS_WITH_GETDENTS
  7595. hlen = sys_getdents(dirfd, hdirp, count);
  7596. #else
  7597. hlen = sys_getdents64(dirfd, hdirp, count);
  7598. #endif
  7599. hlen = get_errno(hlen);
  7600. if (is_error(hlen)) {
  7601. return hlen;
  7602. }
  7603. tdirp = lock_user(VERIFY_WRITE, arg2, count, 0);
  7604. if (!tdirp) {
  7605. return -TARGET_EFAULT;
  7606. }
  7607. for (hoff = toff = 0; hoff < hlen; hoff += hreclen, toff += treclen) {
  7608. #ifdef EMULATE_GETDENTS_WITH_GETDENTS
  7609. struct linux_dirent *hde = hdirp + hoff;
  7610. #else
  7611. struct linux_dirent64 *hde = hdirp + hoff;
  7612. #endif
  7613. struct target_dirent *tde = tdirp + toff;
  7614. int namelen;
  7615. uint8_t type;
  7616. namelen = strlen(hde->d_name);
  7617. hreclen = hde->d_reclen;
  7618. treclen = offsetof(struct target_dirent, d_name) + namelen + 2;
  7619. treclen = QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent));
  7620. if (toff + treclen > count) {
  7621. /*
  7622. * If the host struct is smaller than the target struct, or
  7623. * requires less alignment and thus packs into less space,
  7624. * then the host can return more entries than we can pass
  7625. * on to the guest.
  7626. */
  7627. if (toff == 0) {
  7628. toff = -TARGET_EINVAL; /* result buffer is too small */
  7629. break;
  7630. }
  7631. /*
  7632. * Return what we have, resetting the file pointer to the
  7633. * location of the first record not returned.
  7634. */
  7635. lseek64(dirfd, prev_diroff, SEEK_SET);
  7636. break;
  7637. }
  7638. prev_diroff = hde->d_off;
  7639. tde->d_ino = tswapal(hde->d_ino);
  7640. tde->d_off = tswapal(hde->d_off);
  7641. tde->d_reclen = tswap16(treclen);
  7642. memcpy(tde->d_name, hde->d_name, namelen + 1);
  7643. /*
  7644. * The getdents type is in what was formerly a padding byte at the
  7645. * end of the structure.
  7646. */
  7647. #ifdef EMULATE_GETDENTS_WITH_GETDENTS
  7648. type = *((uint8_t *)hde + hreclen - 1);
  7649. #else
  7650. type = hde->d_type;
  7651. #endif
  7652. *((uint8_t *)tde + treclen - 1) = type;
  7653. }
  7654. unlock_user(tdirp, arg2, toff);
  7655. return toff;
  7656. }
  7657. #endif /* TARGET_NR_getdents */
  7658. #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
  7659. static int do_getdents64(abi_long dirfd, abi_long arg2, abi_long count)
  7660. {
  7661. g_autofree void *hdirp = NULL;
  7662. void *tdirp;
  7663. int hlen, hoff, toff;
  7664. int hreclen, treclen;
  7665. off64_t prev_diroff = 0;
  7666. hdirp = g_try_malloc(count);
  7667. if (!hdirp) {
  7668. return -TARGET_ENOMEM;
  7669. }
  7670. hlen = get_errno(sys_getdents64(dirfd, hdirp, count));
  7671. if (is_error(hlen)) {
  7672. return hlen;
  7673. }
  7674. tdirp = lock_user(VERIFY_WRITE, arg2, count, 0);
  7675. if (!tdirp) {
  7676. return -TARGET_EFAULT;
  7677. }
  7678. for (hoff = toff = 0; hoff < hlen; hoff += hreclen, toff += treclen) {
  7679. struct linux_dirent64 *hde = hdirp + hoff;
  7680. struct target_dirent64 *tde = tdirp + toff;
  7681. int namelen;
  7682. namelen = strlen(hde->d_name) + 1;
  7683. hreclen = hde->d_reclen;
  7684. treclen = offsetof(struct target_dirent64, d_name) + namelen;
  7685. treclen = QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent64));
  7686. if (toff + treclen > count) {
  7687. /*
  7688. * If the host struct is smaller than the target struct, or
  7689. * requires less alignment and thus packs into less space,
  7690. * then the host can return more entries than we can pass
  7691. * on to the guest.
  7692. */
  7693. if (toff == 0) {
  7694. toff = -TARGET_EINVAL; /* result buffer is too small */
  7695. break;
  7696. }
  7697. /*
  7698. * Return what we have, resetting the file pointer to the
  7699. * location of the first record not returned.
  7700. */
  7701. lseek64(dirfd, prev_diroff, SEEK_SET);
  7702. break;
  7703. }
  7704. prev_diroff = hde->d_off;
  7705. tde->d_ino = tswap64(hde->d_ino);
  7706. tde->d_off = tswap64(hde->d_off);
  7707. tde->d_reclen = tswap16(treclen);
  7708. tde->d_type = hde->d_type;
  7709. memcpy(tde->d_name, hde->d_name, namelen);
  7710. }
  7711. unlock_user(tdirp, arg2, toff);
  7712. return toff;
  7713. }
  7714. #endif /* TARGET_NR_getdents64 */
  7715. #if defined(TARGET_NR_pivot_root) && defined(__NR_pivot_root)
  7716. _syscall2(int, pivot_root, const char *, new_root, const char *, put_old)
  7717. #endif
  7718. /* This is an internal helper for do_syscall so that it is easier
  7719. * to have a single return point, so that actions, such as logging
  7720. * of syscall results, can be performed.
  7721. * All errnos that do_syscall() returns must be -TARGET_<errcode>.
  7722. */
  7723. static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
  7724. abi_long arg2, abi_long arg3, abi_long arg4,
  7725. abi_long arg5, abi_long arg6, abi_long arg7,
  7726. abi_long arg8)
  7727. {
  7728. CPUState *cpu = env_cpu(cpu_env);
  7729. abi_long ret;
  7730. #if defined(TARGET_NR_stat) || defined(TARGET_NR_stat64) \
  7731. || defined(TARGET_NR_lstat) || defined(TARGET_NR_lstat64) \
  7732. || defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64) \
  7733. || defined(TARGET_NR_statx)
  7734. struct stat st;
  7735. #endif
  7736. #if defined(TARGET_NR_statfs) || defined(TARGET_NR_statfs64) \
  7737. || defined(TARGET_NR_fstatfs)
  7738. struct statfs stfs;
  7739. #endif
  7740. void *p;
  7741. switch(num) {
  7742. case TARGET_NR_exit:
  7743. /* In old applications this may be used to implement _exit(2).
  7744. However in threaded applications it is used for thread termination,
  7745. and _exit_group is used for application termination.
  7746. Do thread termination if we have more then one thread. */
  7747. if (block_signals()) {
  7748. return -QEMU_ERESTARTSYS;
  7749. }
  7750. pthread_mutex_lock(&clone_lock);
  7751. if (CPU_NEXT(first_cpu)) {
  7752. TaskState *ts = cpu->opaque;
  7753. object_property_set_bool(OBJECT(cpu), "realized", false, NULL);
  7754. object_unref(OBJECT(cpu));
  7755. /*
  7756. * At this point the CPU should be unrealized and removed
  7757. * from cpu lists. We can clean-up the rest of the thread
  7758. * data without the lock held.
  7759. */
  7760. pthread_mutex_unlock(&clone_lock);
  7761. if (ts->child_tidptr) {
  7762. put_user_u32(0, ts->child_tidptr);
  7763. do_sys_futex(g2h(cpu, ts->child_tidptr),
  7764. FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
  7765. }
  7766. thread_cpu = NULL;
  7767. g_free(ts);
  7768. rcu_unregister_thread();
  7769. pthread_exit(NULL);
  7770. }
  7771. pthread_mutex_unlock(&clone_lock);
  7772. preexit_cleanup(cpu_env, arg1);
  7773. _exit(arg1);
  7774. return 0; /* avoid warning */
  7775. case TARGET_NR_read:
  7776. if (arg2 == 0 && arg3 == 0) {
  7777. return get_errno(safe_read(arg1, 0, 0));
  7778. } else {
  7779. if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
  7780. return -TARGET_EFAULT;
  7781. ret = get_errno(safe_read(arg1, p, arg3));
  7782. if (ret >= 0 &&
  7783. fd_trans_host_to_target_data(arg1)) {
  7784. ret = fd_trans_host_to_target_data(arg1)(p, ret);
  7785. }
  7786. unlock_user(p, arg2, ret);
  7787. }
  7788. return ret;
  7789. case TARGET_NR_write:
  7790. if (arg2 == 0 && arg3 == 0) {
  7791. return get_errno(safe_write(arg1, 0, 0));
  7792. }
  7793. if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
  7794. return -TARGET_EFAULT;
  7795. if (fd_trans_target_to_host_data(arg1)) {
  7796. void *copy = g_malloc(arg3);
  7797. memcpy(copy, p, arg3);
  7798. ret = fd_trans_target_to_host_data(arg1)(copy, arg3);
  7799. if (ret >= 0) {
  7800. ret = get_errno(safe_write(arg1, copy, ret));
  7801. }
  7802. g_free(copy);
  7803. } else {
  7804. ret = get_errno(safe_write(arg1, p, arg3));
  7805. }
  7806. unlock_user(p, arg2, 0);
  7807. return ret;
  7808. #ifdef TARGET_NR_open
  7809. case TARGET_NR_open:
  7810. if (!(p = lock_user_string(arg1)))
  7811. return -TARGET_EFAULT;
  7812. ret = get_errno(do_openat(cpu_env, AT_FDCWD, p,
  7813. target_to_host_bitmask(arg2, fcntl_flags_tbl),
  7814. arg3));
  7815. fd_trans_unregister(ret);
  7816. unlock_user(p, arg1, 0);
  7817. return ret;
  7818. #endif
  7819. case TARGET_NR_openat:
  7820. if (!(p = lock_user_string(arg2)))
  7821. return -TARGET_EFAULT;
  7822. ret = get_errno(do_openat(cpu_env, arg1, p,
  7823. target_to_host_bitmask(arg3, fcntl_flags_tbl),
  7824. arg4));
  7825. fd_trans_unregister(ret);
  7826. unlock_user(p, arg2, 0);
  7827. return ret;
  7828. #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  7829. case TARGET_NR_name_to_handle_at:
  7830. ret = do_name_to_handle_at(arg1, arg2, arg3, arg4, arg5);
  7831. return ret;
  7832. #endif
  7833. #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  7834. case TARGET_NR_open_by_handle_at:
  7835. ret = do_open_by_handle_at(arg1, arg2, arg3);
  7836. fd_trans_unregister(ret);
  7837. return ret;
  7838. #endif
  7839. #if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
  7840. case TARGET_NR_pidfd_open:
  7841. return get_errno(pidfd_open(arg1, arg2));
  7842. #endif
  7843. #if defined(__NR_pidfd_send_signal) && defined(TARGET_NR_pidfd_send_signal)
  7844. case TARGET_NR_pidfd_send_signal:
  7845. {
  7846. siginfo_t uinfo, *puinfo;
  7847. if (arg3) {
  7848. p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1);
  7849. if (!p) {
  7850. return -TARGET_EFAULT;
  7851. }
  7852. target_to_host_siginfo(&uinfo, p);
  7853. unlock_user(p, arg3, 0);
  7854. puinfo = &uinfo;
  7855. } else {
  7856. puinfo = NULL;
  7857. }
  7858. ret = get_errno(pidfd_send_signal(arg1, target_to_host_signal(arg2),
  7859. puinfo, arg4));
  7860. }
  7861. return ret;
  7862. #endif
  7863. #if defined(__NR_pidfd_getfd) && defined(TARGET_NR_pidfd_getfd)
  7864. case TARGET_NR_pidfd_getfd:
  7865. return get_errno(pidfd_getfd(arg1, arg2, arg3));
  7866. #endif
  7867. case TARGET_NR_close:
  7868. fd_trans_unregister(arg1);
  7869. return get_errno(close(arg1));
  7870. #if defined(__NR_close_range) && defined(TARGET_NR_close_range)
  7871. case TARGET_NR_close_range:
  7872. ret = get_errno(sys_close_range(arg1, arg2, arg3));
  7873. if (ret == 0 && !(arg3 & CLOSE_RANGE_CLOEXEC)) {
  7874. abi_long fd, maxfd;
  7875. maxfd = MIN(arg2, target_fd_max);
  7876. for (fd = arg1; fd < maxfd; fd++) {
  7877. fd_trans_unregister(fd);
  7878. }
  7879. }
  7880. return ret;
  7881. #endif
  7882. case TARGET_NR_brk:
  7883. return do_brk(arg1);
  7884. #ifdef TARGET_NR_fork
  7885. case TARGET_NR_fork:
  7886. return get_errno(do_fork(cpu_env, TARGET_SIGCHLD, 0, 0, 0, 0));
  7887. #endif
  7888. #ifdef TARGET_NR_waitpid
  7889. case TARGET_NR_waitpid:
  7890. {
  7891. int status;
  7892. ret = get_errno(safe_wait4(arg1, &status, arg3, 0));
  7893. if (!is_error(ret) && arg2 && ret
  7894. && put_user_s32(host_to_target_waitstatus(status), arg2))
  7895. return -TARGET_EFAULT;
  7896. }
  7897. return ret;
  7898. #endif
  7899. #ifdef TARGET_NR_waitid
  7900. case TARGET_NR_waitid:
  7901. {
  7902. siginfo_t info;
  7903. info.si_pid = 0;
  7904. ret = get_errno(safe_waitid(arg1, arg2, &info, arg4, NULL));
  7905. if (!is_error(ret) && arg3 && info.si_pid != 0) {
  7906. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
  7907. return -TARGET_EFAULT;
  7908. host_to_target_siginfo(p, &info);
  7909. unlock_user(p, arg3, sizeof(target_siginfo_t));
  7910. }
  7911. }
  7912. return ret;
  7913. #endif
  7914. #ifdef TARGET_NR_creat /* not on alpha */
  7915. case TARGET_NR_creat:
  7916. if (!(p = lock_user_string(arg1)))
  7917. return -TARGET_EFAULT;
  7918. ret = get_errno(creat(p, arg2));
  7919. fd_trans_unregister(ret);
  7920. unlock_user(p, arg1, 0);
  7921. return ret;
  7922. #endif
  7923. #ifdef TARGET_NR_link
  7924. case TARGET_NR_link:
  7925. {
  7926. void * p2;
  7927. p = lock_user_string(arg1);
  7928. p2 = lock_user_string(arg2);
  7929. if (!p || !p2)
  7930. ret = -TARGET_EFAULT;
  7931. else
  7932. ret = get_errno(link(p, p2));
  7933. unlock_user(p2, arg2, 0);
  7934. unlock_user(p, arg1, 0);
  7935. }
  7936. return ret;
  7937. #endif
  7938. #if defined(TARGET_NR_linkat)
  7939. case TARGET_NR_linkat:
  7940. {
  7941. void * p2 = NULL;
  7942. if (!arg2 || !arg4)
  7943. return -TARGET_EFAULT;
  7944. p = lock_user_string(arg2);
  7945. p2 = lock_user_string(arg4);
  7946. if (!p || !p2)
  7947. ret = -TARGET_EFAULT;
  7948. else
  7949. ret = get_errno(linkat(arg1, p, arg3, p2, arg5));
  7950. unlock_user(p, arg2, 0);
  7951. unlock_user(p2, arg4, 0);
  7952. }
  7953. return ret;
  7954. #endif
  7955. #ifdef TARGET_NR_unlink
  7956. case TARGET_NR_unlink:
  7957. if (!(p = lock_user_string(arg1)))
  7958. return -TARGET_EFAULT;
  7959. ret = get_errno(unlink(p));
  7960. unlock_user(p, arg1, 0);
  7961. return ret;
  7962. #endif
  7963. #if defined(TARGET_NR_unlinkat)
  7964. case TARGET_NR_unlinkat:
  7965. if (!(p = lock_user_string(arg2)))
  7966. return -TARGET_EFAULT;
  7967. ret = get_errno(unlinkat(arg1, p, arg3));
  7968. unlock_user(p, arg2, 0);
  7969. return ret;
  7970. #endif
  7971. case TARGET_NR_execve:
  7972. {
  7973. char **argp, **envp;
  7974. int argc, envc;
  7975. abi_ulong gp;
  7976. abi_ulong guest_argp;
  7977. abi_ulong guest_envp;
  7978. abi_ulong addr;
  7979. char **q;
  7980. argc = 0;
  7981. guest_argp = arg2;
  7982. for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
  7983. if (get_user_ual(addr, gp))
  7984. return -TARGET_EFAULT;
  7985. if (!addr)
  7986. break;
  7987. argc++;
  7988. }
  7989. envc = 0;
  7990. guest_envp = arg3;
  7991. for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
  7992. if (get_user_ual(addr, gp))
  7993. return -TARGET_EFAULT;
  7994. if (!addr)
  7995. break;
  7996. envc++;
  7997. }
  7998. argp = g_new0(char *, argc + 1);
  7999. envp = g_new0(char *, envc + 1);
  8000. for (gp = guest_argp, q = argp; gp;
  8001. gp += sizeof(abi_ulong), q++) {
  8002. if (get_user_ual(addr, gp))
  8003. goto execve_efault;
  8004. if (!addr)
  8005. break;
  8006. if (!(*q = lock_user_string(addr)))
  8007. goto execve_efault;
  8008. }
  8009. *q = NULL;
  8010. for (gp = guest_envp, q = envp; gp;
  8011. gp += sizeof(abi_ulong), q++) {
  8012. if (get_user_ual(addr, gp))
  8013. goto execve_efault;
  8014. if (!addr)
  8015. break;
  8016. if (!(*q = lock_user_string(addr)))
  8017. goto execve_efault;
  8018. }
  8019. *q = NULL;
  8020. if (!(p = lock_user_string(arg1)))
  8021. goto execve_efault;
  8022. /* Although execve() is not an interruptible syscall it is
  8023. * a special case where we must use the safe_syscall wrapper:
  8024. * if we allow a signal to happen before we make the host
  8025. * syscall then we will 'lose' it, because at the point of
  8026. * execve the process leaves QEMU's control. So we use the
  8027. * safe syscall wrapper to ensure that we either take the
  8028. * signal as a guest signal, or else it does not happen
  8029. * before the execve completes and makes it the other
  8030. * program's problem.
  8031. */
  8032. if (is_proc_myself(p, "exe")) {
  8033. ret = get_errno(safe_execve(exec_path, argp, envp));
  8034. } else {
  8035. ret = get_errno(safe_execve(p, argp, envp));
  8036. }
  8037. unlock_user(p, arg1, 0);
  8038. goto execve_end;
  8039. execve_efault:
  8040. ret = -TARGET_EFAULT;
  8041. execve_end:
  8042. for (gp = guest_argp, q = argp; *q;
  8043. gp += sizeof(abi_ulong), q++) {
  8044. if (get_user_ual(addr, gp)
  8045. || !addr)
  8046. break;
  8047. unlock_user(*q, addr, 0);
  8048. }
  8049. for (gp = guest_envp, q = envp; *q;
  8050. gp += sizeof(abi_ulong), q++) {
  8051. if (get_user_ual(addr, gp)
  8052. || !addr)
  8053. break;
  8054. unlock_user(*q, addr, 0);
  8055. }
  8056. g_free(argp);
  8057. g_free(envp);
  8058. }
  8059. return ret;
  8060. case TARGET_NR_chdir:
  8061. if (!(p = lock_user_string(arg1)))
  8062. return -TARGET_EFAULT;
  8063. ret = get_errno(chdir(p));
  8064. unlock_user(p, arg1, 0);
  8065. return ret;
  8066. #ifdef TARGET_NR_time
  8067. case TARGET_NR_time:
  8068. {
  8069. time_t host_time;
  8070. ret = get_errno(time(&host_time));
  8071. if (!is_error(ret)
  8072. && arg1
  8073. && put_user_sal(host_time, arg1))
  8074. return -TARGET_EFAULT;
  8075. }
  8076. return ret;
  8077. #endif
  8078. #ifdef TARGET_NR_mknod
  8079. case TARGET_NR_mknod:
  8080. if (!(p = lock_user_string(arg1)))
  8081. return -TARGET_EFAULT;
  8082. ret = get_errno(mknod(p, arg2, arg3));
  8083. unlock_user(p, arg1, 0);
  8084. return ret;
  8085. #endif
  8086. #if defined(TARGET_NR_mknodat)
  8087. case TARGET_NR_mknodat:
  8088. if (!(p = lock_user_string(arg2)))
  8089. return -TARGET_EFAULT;
  8090. ret = get_errno(mknodat(arg1, p, arg3, arg4));
  8091. unlock_user(p, arg2, 0);
  8092. return ret;
  8093. #endif
  8094. #ifdef TARGET_NR_chmod
  8095. case TARGET_NR_chmod:
  8096. if (!(p = lock_user_string(arg1)))
  8097. return -TARGET_EFAULT;
  8098. ret = get_errno(chmod(p, arg2));
  8099. unlock_user(p, arg1, 0);
  8100. return ret;
  8101. #endif
  8102. #ifdef TARGET_NR_lseek
  8103. case TARGET_NR_lseek:
  8104. return get_errno(lseek(arg1, arg2, arg3));
  8105. #endif
  8106. #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
  8107. /* Alpha specific */
  8108. case TARGET_NR_getxpid:
  8109. cpu_env->ir[IR_A4] = getppid();
  8110. return get_errno(getpid());
  8111. #endif
  8112. #ifdef TARGET_NR_getpid
  8113. case TARGET_NR_getpid:
  8114. return get_errno(getpid());
  8115. #endif
  8116. case TARGET_NR_mount:
  8117. {
  8118. /* need to look at the data field */
  8119. void *p2, *p3;
  8120. if (arg1) {
  8121. p = lock_user_string(arg1);
  8122. if (!p) {
  8123. return -TARGET_EFAULT;
  8124. }
  8125. } else {
  8126. p = NULL;
  8127. }
  8128. p2 = lock_user_string(arg2);
  8129. if (!p2) {
  8130. if (arg1) {
  8131. unlock_user(p, arg1, 0);
  8132. }
  8133. return -TARGET_EFAULT;
  8134. }
  8135. if (arg3) {
  8136. p3 = lock_user_string(arg3);
  8137. if (!p3) {
  8138. if (arg1) {
  8139. unlock_user(p, arg1, 0);
  8140. }
  8141. unlock_user(p2, arg2, 0);
  8142. return -TARGET_EFAULT;
  8143. }
  8144. } else {
  8145. p3 = NULL;
  8146. }
  8147. /* FIXME - arg5 should be locked, but it isn't clear how to
  8148. * do that since it's not guaranteed to be a NULL-terminated
  8149. * string.
  8150. */
  8151. if (!arg5) {
  8152. ret = mount(p, p2, p3, (unsigned long)arg4, NULL);
  8153. } else {
  8154. ret = mount(p, p2, p3, (unsigned long)arg4, g2h(cpu, arg5));
  8155. }
  8156. ret = get_errno(ret);
  8157. if (arg1) {
  8158. unlock_user(p, arg1, 0);
  8159. }
  8160. unlock_user(p2, arg2, 0);
  8161. if (arg3) {
  8162. unlock_user(p3, arg3, 0);
  8163. }
  8164. }
  8165. return ret;
  8166. #if defined(TARGET_NR_umount) || defined(TARGET_NR_oldumount)
  8167. #if defined(TARGET_NR_umount)
  8168. case TARGET_NR_umount:
  8169. #endif
  8170. #if defined(TARGET_NR_oldumount)
  8171. case TARGET_NR_oldumount:
  8172. #endif
  8173. if (!(p = lock_user_string(arg1)))
  8174. return -TARGET_EFAULT;
  8175. ret = get_errno(umount(p));
  8176. unlock_user(p, arg1, 0);
  8177. return ret;
  8178. #endif
  8179. #ifdef TARGET_NR_stime /* not on alpha */
  8180. case TARGET_NR_stime:
  8181. {
  8182. struct timespec ts;
  8183. ts.tv_nsec = 0;
  8184. if (get_user_sal(ts.tv_sec, arg1)) {
  8185. return -TARGET_EFAULT;
  8186. }
  8187. return get_errno(clock_settime(CLOCK_REALTIME, &ts));
  8188. }
  8189. #endif
  8190. #ifdef TARGET_NR_alarm /* not on alpha */
  8191. case TARGET_NR_alarm:
  8192. return alarm(arg1);
  8193. #endif
  8194. #ifdef TARGET_NR_pause /* not on alpha */
  8195. case TARGET_NR_pause:
  8196. if (!block_signals()) {
  8197. sigsuspend(&((TaskState *)cpu->opaque)->signal_mask);
  8198. }
  8199. return -TARGET_EINTR;
  8200. #endif
  8201. #ifdef TARGET_NR_utime
  8202. case TARGET_NR_utime:
  8203. {
  8204. struct utimbuf tbuf, *host_tbuf;
  8205. struct target_utimbuf *target_tbuf;
  8206. if (arg2) {
  8207. if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
  8208. return -TARGET_EFAULT;
  8209. tbuf.actime = tswapal(target_tbuf->actime);
  8210. tbuf.modtime = tswapal(target_tbuf->modtime);
  8211. unlock_user_struct(target_tbuf, arg2, 0);
  8212. host_tbuf = &tbuf;
  8213. } else {
  8214. host_tbuf = NULL;
  8215. }
  8216. if (!(p = lock_user_string(arg1)))
  8217. return -TARGET_EFAULT;
  8218. ret = get_errno(utime(p, host_tbuf));
  8219. unlock_user(p, arg1, 0);
  8220. }
  8221. return ret;
  8222. #endif
  8223. #ifdef TARGET_NR_utimes
  8224. case TARGET_NR_utimes:
  8225. {
  8226. struct timeval *tvp, tv[2];
  8227. if (arg2) {
  8228. if (copy_from_user_timeval(&tv[0], arg2)
  8229. || copy_from_user_timeval(&tv[1],
  8230. arg2 + sizeof(struct target_timeval)))
  8231. return -TARGET_EFAULT;
  8232. tvp = tv;
  8233. } else {
  8234. tvp = NULL;
  8235. }
  8236. if (!(p = lock_user_string(arg1)))
  8237. return -TARGET_EFAULT;
  8238. ret = get_errno(utimes(p, tvp));
  8239. unlock_user(p, arg1, 0);
  8240. }
  8241. return ret;
  8242. #endif
  8243. #if defined(TARGET_NR_futimesat)
  8244. case TARGET_NR_futimesat:
  8245. {
  8246. struct timeval *tvp, tv[2];
  8247. if (arg3) {
  8248. if (copy_from_user_timeval(&tv[0], arg3)
  8249. || copy_from_user_timeval(&tv[1],
  8250. arg3 + sizeof(struct target_timeval)))
  8251. return -TARGET_EFAULT;
  8252. tvp = tv;
  8253. } else {
  8254. tvp = NULL;
  8255. }
  8256. if (!(p = lock_user_string(arg2))) {
  8257. return -TARGET_EFAULT;
  8258. }
  8259. ret = get_errno(futimesat(arg1, path(p), tvp));
  8260. unlock_user(p, arg2, 0);
  8261. }
  8262. return ret;
  8263. #endif
  8264. #ifdef TARGET_NR_access
  8265. case TARGET_NR_access:
  8266. if (!(p = lock_user_string(arg1))) {
  8267. return -TARGET_EFAULT;
  8268. }
  8269. ret = get_errno(access(path(p), arg2));
  8270. unlock_user(p, arg1, 0);
  8271. return ret;
  8272. #endif
  8273. #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
  8274. case TARGET_NR_faccessat:
  8275. if (!(p = lock_user_string(arg2))) {
  8276. return -TARGET_EFAULT;
  8277. }
  8278. ret = get_errno(faccessat(arg1, p, arg3, 0));
  8279. unlock_user(p, arg2, 0);
  8280. return ret;
  8281. #endif
  8282. #if defined(TARGET_NR_faccessat2)
  8283. case TARGET_NR_faccessat2:
  8284. if (!(p = lock_user_string(arg2))) {
  8285. return -TARGET_EFAULT;
  8286. }
  8287. ret = get_errno(faccessat(arg1, p, arg3, arg4));
  8288. unlock_user(p, arg2, 0);
  8289. return ret;
  8290. #endif
  8291. #ifdef TARGET_NR_nice /* not on alpha */
  8292. case TARGET_NR_nice:
  8293. return get_errno(nice(arg1));
  8294. #endif
  8295. case TARGET_NR_sync:
  8296. sync();
  8297. return 0;
  8298. #if defined(TARGET_NR_syncfs) && defined(CONFIG_SYNCFS)
  8299. case TARGET_NR_syncfs:
  8300. return get_errno(syncfs(arg1));
  8301. #endif
  8302. case TARGET_NR_kill:
  8303. return get_errno(safe_kill(arg1, target_to_host_signal(arg2)));
  8304. #ifdef TARGET_NR_rename
  8305. case TARGET_NR_rename:
  8306. {
  8307. void *p2;
  8308. p = lock_user_string(arg1);
  8309. p2 = lock_user_string(arg2);
  8310. if (!p || !p2)
  8311. ret = -TARGET_EFAULT;
  8312. else
  8313. ret = get_errno(rename(p, p2));
  8314. unlock_user(p2, arg2, 0);
  8315. unlock_user(p, arg1, 0);
  8316. }
  8317. return ret;
  8318. #endif
  8319. #if defined(TARGET_NR_renameat)
  8320. case TARGET_NR_renameat:
  8321. {
  8322. void *p2;
  8323. p = lock_user_string(arg2);
  8324. p2 = lock_user_string(arg4);
  8325. if (!p || !p2)
  8326. ret = -TARGET_EFAULT;
  8327. else
  8328. ret = get_errno(renameat(arg1, p, arg3, p2));
  8329. unlock_user(p2, arg4, 0);
  8330. unlock_user(p, arg2, 0);
  8331. }
  8332. return ret;
  8333. #endif
  8334. #if defined(TARGET_NR_renameat2)
  8335. case TARGET_NR_renameat2:
  8336. {
  8337. void *p2;
  8338. p = lock_user_string(arg2);
  8339. p2 = lock_user_string(arg4);
  8340. if (!p || !p2) {
  8341. ret = -TARGET_EFAULT;
  8342. } else {
  8343. ret = get_errno(sys_renameat2(arg1, p, arg3, p2, arg5));
  8344. }
  8345. unlock_user(p2, arg4, 0);
  8346. unlock_user(p, arg2, 0);
  8347. }
  8348. return ret;
  8349. #endif
  8350. #ifdef TARGET_NR_mkdir
  8351. case TARGET_NR_mkdir:
  8352. if (!(p = lock_user_string(arg1)))
  8353. return -TARGET_EFAULT;
  8354. ret = get_errno(mkdir(p, arg2));
  8355. unlock_user(p, arg1, 0);
  8356. return ret;
  8357. #endif
  8358. #if defined(TARGET_NR_mkdirat)
  8359. case TARGET_NR_mkdirat:
  8360. if (!(p = lock_user_string(arg2)))
  8361. return -TARGET_EFAULT;
  8362. ret = get_errno(mkdirat(arg1, p, arg3));
  8363. unlock_user(p, arg2, 0);
  8364. return ret;
  8365. #endif
  8366. #ifdef TARGET_NR_rmdir
  8367. case TARGET_NR_rmdir:
  8368. if (!(p = lock_user_string(arg1)))
  8369. return -TARGET_EFAULT;
  8370. ret = get_errno(rmdir(p));
  8371. unlock_user(p, arg1, 0);
  8372. return ret;
  8373. #endif
  8374. case TARGET_NR_dup:
  8375. ret = get_errno(dup(arg1));
  8376. if (ret >= 0) {
  8377. fd_trans_dup(arg1, ret);
  8378. }
  8379. return ret;
  8380. #ifdef TARGET_NR_pipe
  8381. case TARGET_NR_pipe:
  8382. return do_pipe(cpu_env, arg1, 0, 0);
  8383. #endif
  8384. #ifdef TARGET_NR_pipe2
  8385. case TARGET_NR_pipe2:
  8386. return do_pipe(cpu_env, arg1,
  8387. target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
  8388. #endif
  8389. case TARGET_NR_times:
  8390. {
  8391. struct target_tms *tmsp;
  8392. struct tms tms;
  8393. ret = get_errno(times(&tms));
  8394. if (arg1) {
  8395. tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
  8396. if (!tmsp)
  8397. return -TARGET_EFAULT;
  8398. tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
  8399. tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
  8400. tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
  8401. tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
  8402. }
  8403. if (!is_error(ret))
  8404. ret = host_to_target_clock_t(ret);
  8405. }
  8406. return ret;
  8407. case TARGET_NR_acct:
  8408. if (arg1 == 0) {
  8409. ret = get_errno(acct(NULL));
  8410. } else {
  8411. if (!(p = lock_user_string(arg1))) {
  8412. return -TARGET_EFAULT;
  8413. }
  8414. ret = get_errno(acct(path(p)));
  8415. unlock_user(p, arg1, 0);
  8416. }
  8417. return ret;
  8418. #ifdef TARGET_NR_umount2
  8419. case TARGET_NR_umount2:
  8420. if (!(p = lock_user_string(arg1)))
  8421. return -TARGET_EFAULT;
  8422. ret = get_errno(umount2(p, arg2));
  8423. unlock_user(p, arg1, 0);
  8424. return ret;
  8425. #endif
  8426. case TARGET_NR_ioctl:
  8427. return do_ioctl(arg1, arg2, arg3);
  8428. #ifdef TARGET_NR_fcntl
  8429. case TARGET_NR_fcntl:
  8430. return do_fcntl(arg1, arg2, arg3);
  8431. #endif
  8432. case TARGET_NR_setpgid:
  8433. return get_errno(setpgid(arg1, arg2));
  8434. case TARGET_NR_umask:
  8435. return get_errno(umask(arg1));
  8436. case TARGET_NR_chroot:
  8437. if (!(p = lock_user_string(arg1)))
  8438. return -TARGET_EFAULT;
  8439. ret = get_errno(chroot(p));
  8440. unlock_user(p, arg1, 0);
  8441. return ret;
  8442. #ifdef TARGET_NR_dup2
  8443. case TARGET_NR_dup2:
  8444. ret = get_errno(dup2(arg1, arg2));
  8445. if (ret >= 0) {
  8446. fd_trans_dup(arg1, arg2);
  8447. }
  8448. return ret;
  8449. #endif
  8450. #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
  8451. case TARGET_NR_dup3:
  8452. {
  8453. int host_flags;
  8454. if ((arg3 & ~TARGET_O_CLOEXEC) != 0) {
  8455. return -EINVAL;
  8456. }
  8457. host_flags = target_to_host_bitmask(arg3, fcntl_flags_tbl);
  8458. ret = get_errno(dup3(arg1, arg2, host_flags));
  8459. if (ret >= 0) {
  8460. fd_trans_dup(arg1, arg2);
  8461. }
  8462. return ret;
  8463. }
  8464. #endif
  8465. #ifdef TARGET_NR_getppid /* not on alpha */
  8466. case TARGET_NR_getppid:
  8467. return get_errno(getppid());
  8468. #endif
  8469. #ifdef TARGET_NR_getpgrp
  8470. case TARGET_NR_getpgrp:
  8471. return get_errno(getpgrp());
  8472. #endif
  8473. case TARGET_NR_setsid:
  8474. return get_errno(setsid());
  8475. #ifdef TARGET_NR_sigaction
  8476. case TARGET_NR_sigaction:
  8477. {
  8478. #if defined(TARGET_MIPS)
  8479. struct target_sigaction act, oact, *pact, *old_act;
  8480. if (arg2) {
  8481. if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
  8482. return -TARGET_EFAULT;
  8483. act._sa_handler = old_act->_sa_handler;
  8484. target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
  8485. act.sa_flags = old_act->sa_flags;
  8486. unlock_user_struct(old_act, arg2, 0);
  8487. pact = &act;
  8488. } else {
  8489. pact = NULL;
  8490. }
  8491. ret = get_errno(do_sigaction(arg1, pact, &oact, 0));
  8492. if (!is_error(ret) && arg3) {
  8493. if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
  8494. return -TARGET_EFAULT;
  8495. old_act->_sa_handler = oact._sa_handler;
  8496. old_act->sa_flags = oact.sa_flags;
  8497. old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
  8498. old_act->sa_mask.sig[1] = 0;
  8499. old_act->sa_mask.sig[2] = 0;
  8500. old_act->sa_mask.sig[3] = 0;
  8501. unlock_user_struct(old_act, arg3, 1);
  8502. }
  8503. #else
  8504. struct target_old_sigaction *old_act;
  8505. struct target_sigaction act, oact, *pact;
  8506. if (arg2) {
  8507. if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
  8508. return -TARGET_EFAULT;
  8509. act._sa_handler = old_act->_sa_handler;
  8510. target_siginitset(&act.sa_mask, old_act->sa_mask);
  8511. act.sa_flags = old_act->sa_flags;
  8512. #ifdef TARGET_ARCH_HAS_SA_RESTORER
  8513. act.sa_restorer = old_act->sa_restorer;
  8514. #endif
  8515. unlock_user_struct(old_act, arg2, 0);
  8516. pact = &act;
  8517. } else {
  8518. pact = NULL;
  8519. }
  8520. ret = get_errno(do_sigaction(arg1, pact, &oact, 0));
  8521. if (!is_error(ret) && arg3) {
  8522. if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
  8523. return -TARGET_EFAULT;
  8524. old_act->_sa_handler = oact._sa_handler;
  8525. old_act->sa_mask = oact.sa_mask.sig[0];
  8526. old_act->sa_flags = oact.sa_flags;
  8527. #ifdef TARGET_ARCH_HAS_SA_RESTORER
  8528. old_act->sa_restorer = oact.sa_restorer;
  8529. #endif
  8530. unlock_user_struct(old_act, arg3, 1);
  8531. }
  8532. #endif
  8533. }
  8534. return ret;
  8535. #endif
  8536. case TARGET_NR_rt_sigaction:
  8537. {
  8538. /*
  8539. * For Alpha and SPARC this is a 5 argument syscall, with
  8540. * a 'restorer' parameter which must be copied into the
  8541. * sa_restorer field of the sigaction struct.
  8542. * For Alpha that 'restorer' is arg5; for SPARC it is arg4,
  8543. * and arg5 is the sigsetsize.
  8544. */
  8545. #if defined(TARGET_ALPHA)
  8546. target_ulong sigsetsize = arg4;
  8547. target_ulong restorer = arg5;
  8548. #elif defined(TARGET_SPARC)
  8549. target_ulong restorer = arg4;
  8550. target_ulong sigsetsize = arg5;
  8551. #else
  8552. target_ulong sigsetsize = arg4;
  8553. target_ulong restorer = 0;
  8554. #endif
  8555. struct target_sigaction *act = NULL;
  8556. struct target_sigaction *oact = NULL;
  8557. if (sigsetsize != sizeof(target_sigset_t)) {
  8558. return -TARGET_EINVAL;
  8559. }
  8560. if (arg2 && !lock_user_struct(VERIFY_READ, act, arg2, 1)) {
  8561. return -TARGET_EFAULT;
  8562. }
  8563. if (arg3 && !lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
  8564. ret = -TARGET_EFAULT;
  8565. } else {
  8566. ret = get_errno(do_sigaction(arg1, act, oact, restorer));
  8567. if (oact) {
  8568. unlock_user_struct(oact, arg3, 1);
  8569. }
  8570. }
  8571. if (act) {
  8572. unlock_user_struct(act, arg2, 0);
  8573. }
  8574. }
  8575. return ret;
  8576. #ifdef TARGET_NR_sgetmask /* not on alpha */
  8577. case TARGET_NR_sgetmask:
  8578. {
  8579. sigset_t cur_set;
  8580. abi_ulong target_set;
  8581. ret = do_sigprocmask(0, NULL, &cur_set);
  8582. if (!ret) {
  8583. host_to_target_old_sigset(&target_set, &cur_set);
  8584. ret = target_set;
  8585. }
  8586. }
  8587. return ret;
  8588. #endif
  8589. #ifdef TARGET_NR_ssetmask /* not on alpha */
  8590. case TARGET_NR_ssetmask:
  8591. {
  8592. sigset_t set, oset;
  8593. abi_ulong target_set = arg1;
  8594. target_to_host_old_sigset(&set, &target_set);
  8595. ret = do_sigprocmask(SIG_SETMASK, &set, &oset);
  8596. if (!ret) {
  8597. host_to_target_old_sigset(&target_set, &oset);
  8598. ret = target_set;
  8599. }
  8600. }
  8601. return ret;
  8602. #endif
  8603. #ifdef TARGET_NR_sigprocmask
  8604. case TARGET_NR_sigprocmask:
  8605. {
  8606. #if defined(TARGET_ALPHA)
  8607. sigset_t set, oldset;
  8608. abi_ulong mask;
  8609. int how;
  8610. switch (arg1) {
  8611. case TARGET_SIG_BLOCK:
  8612. how = SIG_BLOCK;
  8613. break;
  8614. case TARGET_SIG_UNBLOCK:
  8615. how = SIG_UNBLOCK;
  8616. break;
  8617. case TARGET_SIG_SETMASK:
  8618. how = SIG_SETMASK;
  8619. break;
  8620. default:
  8621. return -TARGET_EINVAL;
  8622. }
  8623. mask = arg2;
  8624. target_to_host_old_sigset(&set, &mask);
  8625. ret = do_sigprocmask(how, &set, &oldset);
  8626. if (!is_error(ret)) {
  8627. host_to_target_old_sigset(&mask, &oldset);
  8628. ret = mask;
  8629. cpu_env->ir[IR_V0] = 0; /* force no error */
  8630. }
  8631. #else
  8632. sigset_t set, oldset, *set_ptr;
  8633. int how;
  8634. if (arg2) {
  8635. p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1);
  8636. if (!p) {
  8637. return -TARGET_EFAULT;
  8638. }
  8639. target_to_host_old_sigset(&set, p);
  8640. unlock_user(p, arg2, 0);
  8641. set_ptr = &set;
  8642. switch (arg1) {
  8643. case TARGET_SIG_BLOCK:
  8644. how = SIG_BLOCK;
  8645. break;
  8646. case TARGET_SIG_UNBLOCK:
  8647. how = SIG_UNBLOCK;
  8648. break;
  8649. case TARGET_SIG_SETMASK:
  8650. how = SIG_SETMASK;
  8651. break;
  8652. default:
  8653. return -TARGET_EINVAL;
  8654. }
  8655. } else {
  8656. how = 0;
  8657. set_ptr = NULL;
  8658. }
  8659. ret = do_sigprocmask(how, set_ptr, &oldset);
  8660. if (!is_error(ret) && arg3) {
  8661. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
  8662. return -TARGET_EFAULT;
  8663. host_to_target_old_sigset(p, &oldset);
  8664. unlock_user(p, arg3, sizeof(target_sigset_t));
  8665. }
  8666. #endif
  8667. }
  8668. return ret;
  8669. #endif
  8670. case TARGET_NR_rt_sigprocmask:
  8671. {
  8672. int how = arg1;
  8673. sigset_t set, oldset, *set_ptr;
  8674. if (arg4 != sizeof(target_sigset_t)) {
  8675. return -TARGET_EINVAL;
  8676. }
  8677. if (arg2) {
  8678. p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1);
  8679. if (!p) {
  8680. return -TARGET_EFAULT;
  8681. }
  8682. target_to_host_sigset(&set, p);
  8683. unlock_user(p, arg2, 0);
  8684. set_ptr = &set;
  8685. switch(how) {
  8686. case TARGET_SIG_BLOCK:
  8687. how = SIG_BLOCK;
  8688. break;
  8689. case TARGET_SIG_UNBLOCK:
  8690. how = SIG_UNBLOCK;
  8691. break;
  8692. case TARGET_SIG_SETMASK:
  8693. how = SIG_SETMASK;
  8694. break;
  8695. default:
  8696. return -TARGET_EINVAL;
  8697. }
  8698. } else {
  8699. how = 0;
  8700. set_ptr = NULL;
  8701. }
  8702. ret = do_sigprocmask(how, set_ptr, &oldset);
  8703. if (!is_error(ret) && arg3) {
  8704. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
  8705. return -TARGET_EFAULT;
  8706. host_to_target_sigset(p, &oldset);
  8707. unlock_user(p, arg3, sizeof(target_sigset_t));
  8708. }
  8709. }
  8710. return ret;
  8711. #ifdef TARGET_NR_sigpending
  8712. case TARGET_NR_sigpending:
  8713. {
  8714. sigset_t set;
  8715. ret = get_errno(sigpending(&set));
  8716. if (!is_error(ret)) {
  8717. if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
  8718. return -TARGET_EFAULT;
  8719. host_to_target_old_sigset(p, &set);
  8720. unlock_user(p, arg1, sizeof(target_sigset_t));
  8721. }
  8722. }
  8723. return ret;
  8724. #endif
  8725. case TARGET_NR_rt_sigpending:
  8726. {
  8727. sigset_t set;
  8728. /* Yes, this check is >, not != like most. We follow the kernel's
  8729. * logic and it does it like this because it implements
  8730. * NR_sigpending through the same code path, and in that case
  8731. * the old_sigset_t is smaller in size.
  8732. */
  8733. if (arg2 > sizeof(target_sigset_t)) {
  8734. return -TARGET_EINVAL;
  8735. }
  8736. ret = get_errno(sigpending(&set));
  8737. if (!is_error(ret)) {
  8738. if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
  8739. return -TARGET_EFAULT;
  8740. host_to_target_sigset(p, &set);
  8741. unlock_user(p, arg1, sizeof(target_sigset_t));
  8742. }
  8743. }
  8744. return ret;
  8745. #ifdef TARGET_NR_sigsuspend
  8746. case TARGET_NR_sigsuspend:
  8747. {
  8748. sigset_t *set;
  8749. #if defined(TARGET_ALPHA)
  8750. TaskState *ts = cpu->opaque;
  8751. /* target_to_host_old_sigset will bswap back */
  8752. abi_ulong mask = tswapal(arg1);
  8753. set = &ts->sigsuspend_mask;
  8754. target_to_host_old_sigset(set, &mask);
  8755. #else
  8756. ret = process_sigsuspend_mask(&set, arg1, sizeof(target_sigset_t));
  8757. if (ret != 0) {
  8758. return ret;
  8759. }
  8760. #endif
  8761. ret = get_errno(safe_rt_sigsuspend(set, SIGSET_T_SIZE));
  8762. finish_sigsuspend_mask(ret);
  8763. }
  8764. return ret;
  8765. #endif
  8766. case TARGET_NR_rt_sigsuspend:
  8767. {
  8768. sigset_t *set;
  8769. ret = process_sigsuspend_mask(&set, arg1, arg2);
  8770. if (ret != 0) {
  8771. return ret;
  8772. }
  8773. ret = get_errno(safe_rt_sigsuspend(set, SIGSET_T_SIZE));
  8774. finish_sigsuspend_mask(ret);
  8775. }
  8776. return ret;
  8777. #ifdef TARGET_NR_rt_sigtimedwait
  8778. case TARGET_NR_rt_sigtimedwait:
  8779. {
  8780. sigset_t set;
  8781. struct timespec uts, *puts;
  8782. siginfo_t uinfo;
  8783. if (arg4 != sizeof(target_sigset_t)) {
  8784. return -TARGET_EINVAL;
  8785. }
  8786. if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
  8787. return -TARGET_EFAULT;
  8788. target_to_host_sigset(&set, p);
  8789. unlock_user(p, arg1, 0);
  8790. if (arg3) {
  8791. puts = &uts;
  8792. if (target_to_host_timespec(puts, arg3)) {
  8793. return -TARGET_EFAULT;
  8794. }
  8795. } else {
  8796. puts = NULL;
  8797. }
  8798. ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts,
  8799. SIGSET_T_SIZE));
  8800. if (!is_error(ret)) {
  8801. if (arg2) {
  8802. p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t),
  8803. 0);
  8804. if (!p) {
  8805. return -TARGET_EFAULT;
  8806. }
  8807. host_to_target_siginfo(p, &uinfo);
  8808. unlock_user(p, arg2, sizeof(target_siginfo_t));
  8809. }
  8810. ret = host_to_target_signal(ret);
  8811. }
  8812. }
  8813. return ret;
  8814. #endif
  8815. #ifdef TARGET_NR_rt_sigtimedwait_time64
  8816. case TARGET_NR_rt_sigtimedwait_time64:
  8817. {
  8818. sigset_t set;
  8819. struct timespec uts, *puts;
  8820. siginfo_t uinfo;
  8821. if (arg4 != sizeof(target_sigset_t)) {
  8822. return -TARGET_EINVAL;
  8823. }
  8824. p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1);
  8825. if (!p) {
  8826. return -TARGET_EFAULT;
  8827. }
  8828. target_to_host_sigset(&set, p);
  8829. unlock_user(p, arg1, 0);
  8830. if (arg3) {
  8831. puts = &uts;
  8832. if (target_to_host_timespec64(puts, arg3)) {
  8833. return -TARGET_EFAULT;
  8834. }
  8835. } else {
  8836. puts = NULL;
  8837. }
  8838. ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts,
  8839. SIGSET_T_SIZE));
  8840. if (!is_error(ret)) {
  8841. if (arg2) {
  8842. p = lock_user(VERIFY_WRITE, arg2,
  8843. sizeof(target_siginfo_t), 0);
  8844. if (!p) {
  8845. return -TARGET_EFAULT;
  8846. }
  8847. host_to_target_siginfo(p, &uinfo);
  8848. unlock_user(p, arg2, sizeof(target_siginfo_t));
  8849. }
  8850. ret = host_to_target_signal(ret);
  8851. }
  8852. }
  8853. return ret;
  8854. #endif
  8855. case TARGET_NR_rt_sigqueueinfo:
  8856. {
  8857. siginfo_t uinfo;
  8858. p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1);
  8859. if (!p) {
  8860. return -TARGET_EFAULT;
  8861. }
  8862. target_to_host_siginfo(&uinfo, p);
  8863. unlock_user(p, arg3, 0);
  8864. ret = get_errno(sys_rt_sigqueueinfo(arg1, target_to_host_signal(arg2), &uinfo));
  8865. }
  8866. return ret;
  8867. case TARGET_NR_rt_tgsigqueueinfo:
  8868. {
  8869. siginfo_t uinfo;
  8870. p = lock_user(VERIFY_READ, arg4, sizeof(target_siginfo_t), 1);
  8871. if (!p) {
  8872. return -TARGET_EFAULT;
  8873. }
  8874. target_to_host_siginfo(&uinfo, p);
  8875. unlock_user(p, arg4, 0);
  8876. ret = get_errno(sys_rt_tgsigqueueinfo(arg1, arg2, target_to_host_signal(arg3), &uinfo));
  8877. }
  8878. return ret;
  8879. #ifdef TARGET_NR_sigreturn
  8880. case TARGET_NR_sigreturn:
  8881. if (block_signals()) {
  8882. return -QEMU_ERESTARTSYS;
  8883. }
  8884. return do_sigreturn(cpu_env);
  8885. #endif
  8886. case TARGET_NR_rt_sigreturn:
  8887. if (block_signals()) {
  8888. return -QEMU_ERESTARTSYS;
  8889. }
  8890. return do_rt_sigreturn(cpu_env);
  8891. case TARGET_NR_sethostname:
  8892. if (!(p = lock_user_string(arg1)))
  8893. return -TARGET_EFAULT;
  8894. ret = get_errno(sethostname(p, arg2));
  8895. unlock_user(p, arg1, 0);
  8896. return ret;
  8897. #ifdef TARGET_NR_setrlimit
  8898. case TARGET_NR_setrlimit:
  8899. {
  8900. int resource = target_to_host_resource(arg1);
  8901. struct target_rlimit *target_rlim;
  8902. struct rlimit rlim;
  8903. if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
  8904. return -TARGET_EFAULT;
  8905. rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
  8906. rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
  8907. unlock_user_struct(target_rlim, arg2, 0);
  8908. /*
  8909. * If we just passed through resource limit settings for memory then
  8910. * they would also apply to QEMU's own allocations, and QEMU will
  8911. * crash or hang or die if its allocations fail. Ideally we would
  8912. * track the guest allocations in QEMU and apply the limits ourselves.
  8913. * For now, just tell the guest the call succeeded but don't actually
  8914. * limit anything.
  8915. */
  8916. if (resource != RLIMIT_AS &&
  8917. resource != RLIMIT_DATA &&
  8918. resource != RLIMIT_STACK) {
  8919. return get_errno(setrlimit(resource, &rlim));
  8920. } else {
  8921. return 0;
  8922. }
  8923. }
  8924. #endif
  8925. #ifdef TARGET_NR_getrlimit
  8926. case TARGET_NR_getrlimit:
  8927. {
  8928. int resource = target_to_host_resource(arg1);
  8929. struct target_rlimit *target_rlim;
  8930. struct rlimit rlim;
  8931. ret = get_errno(getrlimit(resource, &rlim));
  8932. if (!is_error(ret)) {
  8933. if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
  8934. return -TARGET_EFAULT;
  8935. target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
  8936. target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
  8937. unlock_user_struct(target_rlim, arg2, 1);
  8938. }
  8939. }
  8940. return ret;
  8941. #endif
  8942. case TARGET_NR_getrusage:
  8943. {
  8944. struct rusage rusage;
  8945. ret = get_errno(getrusage(arg1, &rusage));
  8946. if (!is_error(ret)) {
  8947. ret = host_to_target_rusage(arg2, &rusage);
  8948. }
  8949. }
  8950. return ret;
  8951. #if defined(TARGET_NR_gettimeofday)
  8952. case TARGET_NR_gettimeofday:
  8953. {
  8954. struct timeval tv;
  8955. struct timezone tz;
  8956. ret = get_errno(gettimeofday(&tv, &tz));
  8957. if (!is_error(ret)) {
  8958. if (arg1 && copy_to_user_timeval(arg1, &tv)) {
  8959. return -TARGET_EFAULT;
  8960. }
  8961. if (arg2 && copy_to_user_timezone(arg2, &tz)) {
  8962. return -TARGET_EFAULT;
  8963. }
  8964. }
  8965. }
  8966. return ret;
  8967. #endif
  8968. #if defined(TARGET_NR_settimeofday)
  8969. case TARGET_NR_settimeofday:
  8970. {
  8971. struct timeval tv, *ptv = NULL;
  8972. struct timezone tz, *ptz = NULL;
  8973. if (arg1) {
  8974. if (copy_from_user_timeval(&tv, arg1)) {
  8975. return -TARGET_EFAULT;
  8976. }
  8977. ptv = &tv;
  8978. }
  8979. if (arg2) {
  8980. if (copy_from_user_timezone(&tz, arg2)) {
  8981. return -TARGET_EFAULT;
  8982. }
  8983. ptz = &tz;
  8984. }
  8985. return get_errno(settimeofday(ptv, ptz));
  8986. }
  8987. #endif
  8988. #if defined(TARGET_NR_select)
  8989. case TARGET_NR_select:
  8990. #if defined(TARGET_WANT_NI_OLD_SELECT)
  8991. /* some architectures used to have old_select here
  8992. * but now ENOSYS it.
  8993. */
  8994. ret = -TARGET_ENOSYS;
  8995. #elif defined(TARGET_WANT_OLD_SYS_SELECT)
  8996. ret = do_old_select(arg1);
  8997. #else
  8998. ret = do_select(arg1, arg2, arg3, arg4, arg5);
  8999. #endif
  9000. return ret;
  9001. #endif
  9002. #ifdef TARGET_NR_pselect6
  9003. case TARGET_NR_pselect6:
  9004. return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, false);
  9005. #endif
  9006. #ifdef TARGET_NR_pselect6_time64
  9007. case TARGET_NR_pselect6_time64:
  9008. return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, true);
  9009. #endif
  9010. #ifdef TARGET_NR_symlink
  9011. case TARGET_NR_symlink:
  9012. {
  9013. void *p2;
  9014. p = lock_user_string(arg1);
  9015. p2 = lock_user_string(arg2);
  9016. if (!p || !p2)
  9017. ret = -TARGET_EFAULT;
  9018. else
  9019. ret = get_errno(symlink(p, p2));
  9020. unlock_user(p2, arg2, 0);
  9021. unlock_user(p, arg1, 0);
  9022. }
  9023. return ret;
  9024. #endif
  9025. #if defined(TARGET_NR_symlinkat)
  9026. case TARGET_NR_symlinkat:
  9027. {
  9028. void *p2;
  9029. p = lock_user_string(arg1);
  9030. p2 = lock_user_string(arg3);
  9031. if (!p || !p2)
  9032. ret = -TARGET_EFAULT;
  9033. else
  9034. ret = get_errno(symlinkat(p, arg2, p2));
  9035. unlock_user(p2, arg3, 0);
  9036. unlock_user(p, arg1, 0);
  9037. }
  9038. return ret;
  9039. #endif
  9040. #ifdef TARGET_NR_readlink
  9041. case TARGET_NR_readlink:
  9042. {
  9043. void *p2;
  9044. p = lock_user_string(arg1);
  9045. p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  9046. if (!p || !p2) {
  9047. ret = -TARGET_EFAULT;
  9048. } else if (!arg3) {
  9049. /* Short circuit this for the magic exe check. */
  9050. ret = -TARGET_EINVAL;
  9051. } else if (is_proc_myself((const char *)p, "exe")) {
  9052. char real[PATH_MAX], *temp;
  9053. temp = realpath(exec_path, real);
  9054. /* Return value is # of bytes that we wrote to the buffer. */
  9055. if (temp == NULL) {
  9056. ret = get_errno(-1);
  9057. } else {
  9058. /* Don't worry about sign mismatch as earlier mapping
  9059. * logic would have thrown a bad address error. */
  9060. ret = MIN(strlen(real), arg3);
  9061. /* We cannot NUL terminate the string. */
  9062. memcpy(p2, real, ret);
  9063. }
  9064. } else {
  9065. ret = get_errno(readlink(path(p), p2, arg3));
  9066. }
  9067. unlock_user(p2, arg2, ret);
  9068. unlock_user(p, arg1, 0);
  9069. }
  9070. return ret;
  9071. #endif
  9072. #if defined(TARGET_NR_readlinkat)
  9073. case TARGET_NR_readlinkat:
  9074. {
  9075. void *p2;
  9076. p = lock_user_string(arg2);
  9077. p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  9078. if (!p || !p2) {
  9079. ret = -TARGET_EFAULT;
  9080. } else if (!arg4) {
  9081. /* Short circuit this for the magic exe check. */
  9082. ret = -TARGET_EINVAL;
  9083. } else if (is_proc_myself((const char *)p, "exe")) {
  9084. char real[PATH_MAX], *temp;
  9085. temp = realpath(exec_path, real);
  9086. /* Return value is # of bytes that we wrote to the buffer. */
  9087. if (temp == NULL) {
  9088. ret = get_errno(-1);
  9089. } else {
  9090. /* Don't worry about sign mismatch as earlier mapping
  9091. * logic would have thrown a bad address error. */
  9092. ret = MIN(strlen(real), arg4);
  9093. /* We cannot NUL terminate the string. */
  9094. memcpy(p2, real, ret);
  9095. }
  9096. } else {
  9097. ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
  9098. }
  9099. unlock_user(p2, arg3, ret);
  9100. unlock_user(p, arg2, 0);
  9101. }
  9102. return ret;
  9103. #endif
  9104. #ifdef TARGET_NR_swapon
  9105. case TARGET_NR_swapon:
  9106. if (!(p = lock_user_string(arg1)))
  9107. return -TARGET_EFAULT;
  9108. ret = get_errno(swapon(p, arg2));
  9109. unlock_user(p, arg1, 0);
  9110. return ret;
  9111. #endif
  9112. case TARGET_NR_reboot:
  9113. if (arg3 == LINUX_REBOOT_CMD_RESTART2) {
  9114. /* arg4 must be ignored in all other cases */
  9115. p = lock_user_string(arg4);
  9116. if (!p) {
  9117. return -TARGET_EFAULT;
  9118. }
  9119. ret = get_errno(reboot(arg1, arg2, arg3, p));
  9120. unlock_user(p, arg4, 0);
  9121. } else {
  9122. ret = get_errno(reboot(arg1, arg2, arg3, NULL));
  9123. }
  9124. return ret;
  9125. #ifdef TARGET_NR_mmap
  9126. case TARGET_NR_mmap:
  9127. #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
  9128. (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
  9129. defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
  9130. || defined(TARGET_S390X)
  9131. {
  9132. abi_ulong *v;
  9133. abi_ulong v1, v2, v3, v4, v5, v6;
  9134. if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
  9135. return -TARGET_EFAULT;
  9136. v1 = tswapal(v[0]);
  9137. v2 = tswapal(v[1]);
  9138. v3 = tswapal(v[2]);
  9139. v4 = tswapal(v[3]);
  9140. v5 = tswapal(v[4]);
  9141. v6 = tswapal(v[5]);
  9142. unlock_user(v, arg1, 0);
  9143. ret = get_errno(target_mmap(v1, v2, v3,
  9144. target_to_host_bitmask(v4, mmap_flags_tbl),
  9145. v5, v6));
  9146. }
  9147. #else
  9148. /* mmap pointers are always untagged */
  9149. ret = get_errno(target_mmap(arg1, arg2, arg3,
  9150. target_to_host_bitmask(arg4, mmap_flags_tbl),
  9151. arg5,
  9152. arg6));
  9153. #endif
  9154. return ret;
  9155. #endif
  9156. #ifdef TARGET_NR_mmap2
  9157. case TARGET_NR_mmap2:
  9158. #ifndef MMAP_SHIFT
  9159. #define MMAP_SHIFT 12
  9160. #endif
  9161. ret = target_mmap(arg1, arg2, arg3,
  9162. target_to_host_bitmask(arg4, mmap_flags_tbl),
  9163. arg5, arg6 << MMAP_SHIFT);
  9164. return get_errno(ret);
  9165. #endif
  9166. case TARGET_NR_munmap:
  9167. arg1 = cpu_untagged_addr(cpu, arg1);
  9168. return get_errno(target_munmap(arg1, arg2));
  9169. case TARGET_NR_mprotect:
  9170. arg1 = cpu_untagged_addr(cpu, arg1);
  9171. {
  9172. TaskState *ts = cpu->opaque;
  9173. /* Special hack to detect libc making the stack executable. */
  9174. if ((arg3 & PROT_GROWSDOWN)
  9175. && arg1 >= ts->info->stack_limit
  9176. && arg1 <= ts->info->start_stack) {
  9177. arg3 &= ~PROT_GROWSDOWN;
  9178. arg2 = arg2 + arg1 - ts->info->stack_limit;
  9179. arg1 = ts->info->stack_limit;
  9180. }
  9181. }
  9182. return get_errno(target_mprotect(arg1, arg2, arg3));
  9183. #ifdef TARGET_NR_mremap
  9184. case TARGET_NR_mremap:
  9185. arg1 = cpu_untagged_addr(cpu, arg1);
  9186. /* mremap new_addr (arg5) is always untagged */
  9187. return get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
  9188. #endif
  9189. /* ??? msync/mlock/munlock are broken for softmmu. */
  9190. #ifdef TARGET_NR_msync
  9191. case TARGET_NR_msync:
  9192. return get_errno(msync(g2h(cpu, arg1), arg2, arg3));
  9193. #endif
  9194. #ifdef TARGET_NR_mlock
  9195. case TARGET_NR_mlock:
  9196. return get_errno(mlock(g2h(cpu, arg1), arg2));
  9197. #endif
  9198. #ifdef TARGET_NR_munlock
  9199. case TARGET_NR_munlock:
  9200. return get_errno(munlock(g2h(cpu, arg1), arg2));
  9201. #endif
  9202. #ifdef TARGET_NR_mlockall
  9203. case TARGET_NR_mlockall:
  9204. return get_errno(mlockall(target_to_host_mlockall_arg(arg1)));
  9205. #endif
  9206. #ifdef TARGET_NR_munlockall
  9207. case TARGET_NR_munlockall:
  9208. return get_errno(munlockall());
  9209. #endif
  9210. #ifdef TARGET_NR_truncate
  9211. case TARGET_NR_truncate:
  9212. if (!(p = lock_user_string(arg1)))
  9213. return -TARGET_EFAULT;
  9214. ret = get_errno(truncate(p, arg2));
  9215. unlock_user(p, arg1, 0);
  9216. return ret;
  9217. #endif
  9218. #ifdef TARGET_NR_ftruncate
  9219. case TARGET_NR_ftruncate:
  9220. return get_errno(ftruncate(arg1, arg2));
  9221. #endif
  9222. case TARGET_NR_fchmod:
  9223. return get_errno(fchmod(arg1, arg2));
  9224. #if defined(TARGET_NR_fchmodat)
  9225. case TARGET_NR_fchmodat:
  9226. if (!(p = lock_user_string(arg2)))
  9227. return -TARGET_EFAULT;
  9228. ret = get_errno(fchmodat(arg1, p, arg3, 0));
  9229. unlock_user(p, arg2, 0);
  9230. return ret;
  9231. #endif
  9232. case TARGET_NR_getpriority:
  9233. /* Note that negative values are valid for getpriority, so we must
  9234. differentiate based on errno settings. */
  9235. errno = 0;
  9236. ret = getpriority(arg1, arg2);
  9237. if (ret == -1 && errno != 0) {
  9238. return -host_to_target_errno(errno);
  9239. }
  9240. #ifdef TARGET_ALPHA
  9241. /* Return value is the unbiased priority. Signal no error. */
  9242. cpu_env->ir[IR_V0] = 0;
  9243. #else
  9244. /* Return value is a biased priority to avoid negative numbers. */
  9245. ret = 20 - ret;
  9246. #endif
  9247. return ret;
  9248. case TARGET_NR_setpriority:
  9249. return get_errno(setpriority(arg1, arg2, arg3));
  9250. #ifdef TARGET_NR_statfs
  9251. case TARGET_NR_statfs:
  9252. if (!(p = lock_user_string(arg1))) {
  9253. return -TARGET_EFAULT;
  9254. }
  9255. ret = get_errno(statfs(path(p), &stfs));
  9256. unlock_user(p, arg1, 0);
  9257. convert_statfs:
  9258. if (!is_error(ret)) {
  9259. struct target_statfs *target_stfs;
  9260. if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
  9261. return -TARGET_EFAULT;
  9262. __put_user(stfs.f_type, &target_stfs->f_type);
  9263. __put_user(stfs.f_bsize, &target_stfs->f_bsize);
  9264. __put_user(stfs.f_blocks, &target_stfs->f_blocks);
  9265. __put_user(stfs.f_bfree, &target_stfs->f_bfree);
  9266. __put_user(stfs.f_bavail, &target_stfs->f_bavail);
  9267. __put_user(stfs.f_files, &target_stfs->f_files);
  9268. __put_user(stfs.f_ffree, &target_stfs->f_ffree);
  9269. __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
  9270. __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
  9271. __put_user(stfs.f_namelen, &target_stfs->f_namelen);
  9272. __put_user(stfs.f_frsize, &target_stfs->f_frsize);
  9273. #ifdef _STATFS_F_FLAGS
  9274. __put_user(stfs.f_flags, &target_stfs->f_flags);
  9275. #else
  9276. __put_user(0, &target_stfs->f_flags);
  9277. #endif
  9278. memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
  9279. unlock_user_struct(target_stfs, arg2, 1);
  9280. }
  9281. return ret;
  9282. #endif
  9283. #ifdef TARGET_NR_fstatfs
  9284. case TARGET_NR_fstatfs:
  9285. ret = get_errno(fstatfs(arg1, &stfs));
  9286. goto convert_statfs;
  9287. #endif
  9288. #ifdef TARGET_NR_statfs64
  9289. case TARGET_NR_statfs64:
  9290. if (!(p = lock_user_string(arg1))) {
  9291. return -TARGET_EFAULT;
  9292. }
  9293. ret = get_errno(statfs(path(p), &stfs));
  9294. unlock_user(p, arg1, 0);
  9295. convert_statfs64:
  9296. if (!is_error(ret)) {
  9297. struct target_statfs64 *target_stfs;
  9298. if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
  9299. return -TARGET_EFAULT;
  9300. __put_user(stfs.f_type, &target_stfs->f_type);
  9301. __put_user(stfs.f_bsize, &target_stfs->f_bsize);
  9302. __put_user(stfs.f_blocks, &target_stfs->f_blocks);
  9303. __put_user(stfs.f_bfree, &target_stfs->f_bfree);
  9304. __put_user(stfs.f_bavail, &target_stfs->f_bavail);
  9305. __put_user(stfs.f_files, &target_stfs->f_files);
  9306. __put_user(stfs.f_ffree, &target_stfs->f_ffree);
  9307. __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
  9308. __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
  9309. __put_user(stfs.f_namelen, &target_stfs->f_namelen);
  9310. __put_user(stfs.f_frsize, &target_stfs->f_frsize);
  9311. #ifdef _STATFS_F_FLAGS
  9312. __put_user(stfs.f_flags, &target_stfs->f_flags);
  9313. #else
  9314. __put_user(0, &target_stfs->f_flags);
  9315. #endif
  9316. memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
  9317. unlock_user_struct(target_stfs, arg3, 1);
  9318. }
  9319. return ret;
  9320. case TARGET_NR_fstatfs64:
  9321. ret = get_errno(fstatfs(arg1, &stfs));
  9322. goto convert_statfs64;
  9323. #endif
  9324. #ifdef TARGET_NR_socketcall
  9325. case TARGET_NR_socketcall:
  9326. return do_socketcall(arg1, arg2);
  9327. #endif
  9328. #ifdef TARGET_NR_accept
  9329. case TARGET_NR_accept:
  9330. return do_accept4(arg1, arg2, arg3, 0);
  9331. #endif
  9332. #ifdef TARGET_NR_accept4
  9333. case TARGET_NR_accept4:
  9334. return do_accept4(arg1, arg2, arg3, arg4);
  9335. #endif
  9336. #ifdef TARGET_NR_bind
  9337. case TARGET_NR_bind:
  9338. return do_bind(arg1, arg2, arg3);
  9339. #endif
  9340. #ifdef TARGET_NR_connect
  9341. case TARGET_NR_connect:
  9342. return do_connect(arg1, arg2, arg3);
  9343. #endif
  9344. #ifdef TARGET_NR_getpeername
  9345. case TARGET_NR_getpeername:
  9346. return do_getpeername(arg1, arg2, arg3);
  9347. #endif
  9348. #ifdef TARGET_NR_getsockname
  9349. case TARGET_NR_getsockname:
  9350. return do_getsockname(arg1, arg2, arg3);
  9351. #endif
  9352. #ifdef TARGET_NR_getsockopt
  9353. case TARGET_NR_getsockopt:
  9354. return do_getsockopt(arg1, arg2, arg3, arg4, arg5);
  9355. #endif
  9356. #ifdef TARGET_NR_listen
  9357. case TARGET_NR_listen:
  9358. return get_errno(listen(arg1, arg2));
  9359. #endif
  9360. #ifdef TARGET_NR_recv
  9361. case TARGET_NR_recv:
  9362. return do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
  9363. #endif
  9364. #ifdef TARGET_NR_recvfrom
  9365. case TARGET_NR_recvfrom:
  9366. return do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
  9367. #endif
  9368. #ifdef TARGET_NR_recvmsg
  9369. case TARGET_NR_recvmsg:
  9370. return do_sendrecvmsg(arg1, arg2, arg3, 0);
  9371. #endif
  9372. #ifdef TARGET_NR_send
  9373. case TARGET_NR_send:
  9374. return do_sendto(arg1, arg2, arg3, arg4, 0, 0);
  9375. #endif
  9376. #ifdef TARGET_NR_sendmsg
  9377. case TARGET_NR_sendmsg:
  9378. return do_sendrecvmsg(arg1, arg2, arg3, 1);
  9379. #endif
  9380. #ifdef TARGET_NR_sendmmsg
  9381. case TARGET_NR_sendmmsg:
  9382. return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 1);
  9383. #endif
  9384. #ifdef TARGET_NR_recvmmsg
  9385. case TARGET_NR_recvmmsg:
  9386. return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 0);
  9387. #endif
  9388. #ifdef TARGET_NR_sendto
  9389. case TARGET_NR_sendto:
  9390. return do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
  9391. #endif
  9392. #ifdef TARGET_NR_shutdown
  9393. case TARGET_NR_shutdown:
  9394. return get_errno(shutdown(arg1, arg2));
  9395. #endif
  9396. #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
  9397. case TARGET_NR_getrandom:
  9398. p = lock_user(VERIFY_WRITE, arg1, arg2, 0);
  9399. if (!p) {
  9400. return -TARGET_EFAULT;
  9401. }
  9402. ret = get_errno(getrandom(p, arg2, arg3));
  9403. unlock_user(p, arg1, ret);
  9404. return ret;
  9405. #endif
  9406. #ifdef TARGET_NR_socket
  9407. case TARGET_NR_socket:
  9408. return do_socket(arg1, arg2, arg3);
  9409. #endif
  9410. #ifdef TARGET_NR_socketpair
  9411. case TARGET_NR_socketpair:
  9412. return do_socketpair(arg1, arg2, arg3, arg4);
  9413. #endif
  9414. #ifdef TARGET_NR_setsockopt
  9415. case TARGET_NR_setsockopt:
  9416. return do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
  9417. #endif
  9418. #if defined(TARGET_NR_syslog)
  9419. case TARGET_NR_syslog:
  9420. {
  9421. int len = arg2;
  9422. switch (arg1) {
  9423. case TARGET_SYSLOG_ACTION_CLOSE: /* Close log */
  9424. case TARGET_SYSLOG_ACTION_OPEN: /* Open log */
  9425. case TARGET_SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
  9426. case TARGET_SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging */
  9427. case TARGET_SYSLOG_ACTION_CONSOLE_ON: /* Enable logging */
  9428. case TARGET_SYSLOG_ACTION_CONSOLE_LEVEL: /* Set messages level */
  9429. case TARGET_SYSLOG_ACTION_SIZE_UNREAD: /* Number of chars */
  9430. case TARGET_SYSLOG_ACTION_SIZE_BUFFER: /* Size of the buffer */
  9431. return get_errno(sys_syslog((int)arg1, NULL, (int)arg3));
  9432. case TARGET_SYSLOG_ACTION_READ: /* Read from log */
  9433. case TARGET_SYSLOG_ACTION_READ_CLEAR: /* Read/clear msgs */
  9434. case TARGET_SYSLOG_ACTION_READ_ALL: /* Read last messages */
  9435. {
  9436. if (len < 0) {
  9437. return -TARGET_EINVAL;
  9438. }
  9439. if (len == 0) {
  9440. return 0;
  9441. }
  9442. p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  9443. if (!p) {
  9444. return -TARGET_EFAULT;
  9445. }
  9446. ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
  9447. unlock_user(p, arg2, arg3);
  9448. }
  9449. return ret;
  9450. default:
  9451. return -TARGET_EINVAL;
  9452. }
  9453. }
  9454. break;
  9455. #endif
  9456. case TARGET_NR_setitimer:
  9457. {
  9458. struct itimerval value, ovalue, *pvalue;
  9459. if (arg2) {
  9460. pvalue = &value;
  9461. if (copy_from_user_timeval(&pvalue->it_interval, arg2)
  9462. || copy_from_user_timeval(&pvalue->it_value,
  9463. arg2 + sizeof(struct target_timeval)))
  9464. return -TARGET_EFAULT;
  9465. } else {
  9466. pvalue = NULL;
  9467. }
  9468. ret = get_errno(setitimer(arg1, pvalue, &ovalue));
  9469. if (!is_error(ret) && arg3) {
  9470. if (copy_to_user_timeval(arg3,
  9471. &ovalue.it_interval)
  9472. || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
  9473. &ovalue.it_value))
  9474. return -TARGET_EFAULT;
  9475. }
  9476. }
  9477. return ret;
  9478. case TARGET_NR_getitimer:
  9479. {
  9480. struct itimerval value;
  9481. ret = get_errno(getitimer(arg1, &value));
  9482. if (!is_error(ret) && arg2) {
  9483. if (copy_to_user_timeval(arg2,
  9484. &value.it_interval)
  9485. || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
  9486. &value.it_value))
  9487. return -TARGET_EFAULT;
  9488. }
  9489. }
  9490. return ret;
  9491. #ifdef TARGET_NR_stat
  9492. case TARGET_NR_stat:
  9493. if (!(p = lock_user_string(arg1))) {
  9494. return -TARGET_EFAULT;
  9495. }
  9496. ret = get_errno(stat(path(p), &st));
  9497. unlock_user(p, arg1, 0);
  9498. goto do_stat;
  9499. #endif
  9500. #ifdef TARGET_NR_lstat
  9501. case TARGET_NR_lstat:
  9502. if (!(p = lock_user_string(arg1))) {
  9503. return -TARGET_EFAULT;
  9504. }
  9505. ret = get_errno(lstat(path(p), &st));
  9506. unlock_user(p, arg1, 0);
  9507. goto do_stat;
  9508. #endif
  9509. #ifdef TARGET_NR_fstat
  9510. case TARGET_NR_fstat:
  9511. {
  9512. ret = get_errno(fstat(arg1, &st));
  9513. #if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat)
  9514. do_stat:
  9515. #endif
  9516. if (!is_error(ret)) {
  9517. struct target_stat *target_st;
  9518. if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
  9519. return -TARGET_EFAULT;
  9520. memset(target_st, 0, sizeof(*target_st));
  9521. __put_user(st.st_dev, &target_st->st_dev);
  9522. __put_user(st.st_ino, &target_st->st_ino);
  9523. __put_user(st.st_mode, &target_st->st_mode);
  9524. __put_user(st.st_uid, &target_st->st_uid);
  9525. __put_user(st.st_gid, &target_st->st_gid);
  9526. __put_user(st.st_nlink, &target_st->st_nlink);
  9527. __put_user(st.st_rdev, &target_st->st_rdev);
  9528. __put_user(st.st_size, &target_st->st_size);
  9529. __put_user(st.st_blksize, &target_st->st_blksize);
  9530. __put_user(st.st_blocks, &target_st->st_blocks);
  9531. __put_user(st.st_atime, &target_st->target_st_atime);
  9532. __put_user(st.st_mtime, &target_st->target_st_mtime);
  9533. __put_user(st.st_ctime, &target_st->target_st_ctime);
  9534. #if defined(HAVE_STRUCT_STAT_ST_ATIM) && defined(TARGET_STAT_HAVE_NSEC)
  9535. __put_user(st.st_atim.tv_nsec,
  9536. &target_st->target_st_atime_nsec);
  9537. __put_user(st.st_mtim.tv_nsec,
  9538. &target_st->target_st_mtime_nsec);
  9539. __put_user(st.st_ctim.tv_nsec,
  9540. &target_st->target_st_ctime_nsec);
  9541. #endif
  9542. unlock_user_struct(target_st, arg2, 1);
  9543. }
  9544. }
  9545. return ret;
  9546. #endif
  9547. case TARGET_NR_vhangup:
  9548. return get_errno(vhangup());
  9549. #ifdef TARGET_NR_syscall
  9550. case TARGET_NR_syscall:
  9551. return do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
  9552. arg6, arg7, arg8, 0);
  9553. #endif
  9554. #if defined(TARGET_NR_wait4)
  9555. case TARGET_NR_wait4:
  9556. {
  9557. int status;
  9558. abi_long status_ptr = arg2;
  9559. struct rusage rusage, *rusage_ptr;
  9560. abi_ulong target_rusage = arg4;
  9561. abi_long rusage_err;
  9562. if (target_rusage)
  9563. rusage_ptr = &rusage;
  9564. else
  9565. rusage_ptr = NULL;
  9566. ret = get_errno(safe_wait4(arg1, &status, arg3, rusage_ptr));
  9567. if (!is_error(ret)) {
  9568. if (status_ptr && ret) {
  9569. status = host_to_target_waitstatus(status);
  9570. if (put_user_s32(status, status_ptr))
  9571. return -TARGET_EFAULT;
  9572. }
  9573. if (target_rusage) {
  9574. rusage_err = host_to_target_rusage(target_rusage, &rusage);
  9575. if (rusage_err) {
  9576. ret = rusage_err;
  9577. }
  9578. }
  9579. }
  9580. }
  9581. return ret;
  9582. #endif
  9583. #ifdef TARGET_NR_swapoff
  9584. case TARGET_NR_swapoff:
  9585. if (!(p = lock_user_string(arg1)))
  9586. return -TARGET_EFAULT;
  9587. ret = get_errno(swapoff(p));
  9588. unlock_user(p, arg1, 0);
  9589. return ret;
  9590. #endif
  9591. case TARGET_NR_sysinfo:
  9592. {
  9593. struct target_sysinfo *target_value;
  9594. struct sysinfo value;
  9595. ret = get_errno(sysinfo(&value));
  9596. if (!is_error(ret) && arg1)
  9597. {
  9598. if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
  9599. return -TARGET_EFAULT;
  9600. __put_user(value.uptime, &target_value->uptime);
  9601. __put_user(value.loads[0], &target_value->loads[0]);
  9602. __put_user(value.loads[1], &target_value->loads[1]);
  9603. __put_user(value.loads[2], &target_value->loads[2]);
  9604. __put_user(value.totalram, &target_value->totalram);
  9605. __put_user(value.freeram, &target_value->freeram);
  9606. __put_user(value.sharedram, &target_value->sharedram);
  9607. __put_user(value.bufferram, &target_value->bufferram);
  9608. __put_user(value.totalswap, &target_value->totalswap);
  9609. __put_user(value.freeswap, &target_value->freeswap);
  9610. __put_user(value.procs, &target_value->procs);
  9611. __put_user(value.totalhigh, &target_value->totalhigh);
  9612. __put_user(value.freehigh, &target_value->freehigh);
  9613. __put_user(value.mem_unit, &target_value->mem_unit);
  9614. unlock_user_struct(target_value, arg1, 1);
  9615. }
  9616. }
  9617. return ret;
  9618. #ifdef TARGET_NR_ipc
  9619. case TARGET_NR_ipc:
  9620. return do_ipc(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6);
  9621. #endif
  9622. #ifdef TARGET_NR_semget
  9623. case TARGET_NR_semget:
  9624. return get_errno(semget(arg1, arg2, arg3));
  9625. #endif
  9626. #ifdef TARGET_NR_semop
  9627. case TARGET_NR_semop:
  9628. return do_semtimedop(arg1, arg2, arg3, 0, false);
  9629. #endif
  9630. #ifdef TARGET_NR_semtimedop
  9631. case TARGET_NR_semtimedop:
  9632. return do_semtimedop(arg1, arg2, arg3, arg4, false);
  9633. #endif
  9634. #ifdef TARGET_NR_semtimedop_time64
  9635. case TARGET_NR_semtimedop_time64:
  9636. return do_semtimedop(arg1, arg2, arg3, arg4, true);
  9637. #endif
  9638. #ifdef TARGET_NR_semctl
  9639. case TARGET_NR_semctl:
  9640. return do_semctl(arg1, arg2, arg3, arg4);
  9641. #endif
  9642. #ifdef TARGET_NR_msgctl
  9643. case TARGET_NR_msgctl:
  9644. return do_msgctl(arg1, arg2, arg3);
  9645. #endif
  9646. #ifdef TARGET_NR_msgget
  9647. case TARGET_NR_msgget:
  9648. return get_errno(msgget(arg1, arg2));
  9649. #endif
  9650. #ifdef TARGET_NR_msgrcv
  9651. case TARGET_NR_msgrcv:
  9652. return do_msgrcv(arg1, arg2, arg3, arg4, arg5);
  9653. #endif
  9654. #ifdef TARGET_NR_msgsnd
  9655. case TARGET_NR_msgsnd:
  9656. return do_msgsnd(arg1, arg2, arg3, arg4);
  9657. #endif
  9658. #ifdef TARGET_NR_shmget
  9659. case TARGET_NR_shmget:
  9660. return get_errno(shmget(arg1, arg2, arg3));
  9661. #endif
  9662. #ifdef TARGET_NR_shmctl
  9663. case TARGET_NR_shmctl:
  9664. return do_shmctl(arg1, arg2, arg3);
  9665. #endif
  9666. #ifdef TARGET_NR_shmat
  9667. case TARGET_NR_shmat:
  9668. return do_shmat(cpu_env, arg1, arg2, arg3);
  9669. #endif
  9670. #ifdef TARGET_NR_shmdt
  9671. case TARGET_NR_shmdt:
  9672. return do_shmdt(arg1);
  9673. #endif
  9674. case TARGET_NR_fsync:
  9675. return get_errno(fsync(arg1));
  9676. case TARGET_NR_clone:
  9677. /* Linux manages to have three different orderings for its
  9678. * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
  9679. * match the kernel's CONFIG_CLONE_* settings.
  9680. * Microblaze is further special in that it uses a sixth
  9681. * implicit argument to clone for the TLS pointer.
  9682. */
  9683. #if defined(TARGET_MICROBLAZE)
  9684. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5));
  9685. #elif defined(TARGET_CLONE_BACKWARDS)
  9686. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
  9687. #elif defined(TARGET_CLONE_BACKWARDS2)
  9688. ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
  9689. #else
  9690. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
  9691. #endif
  9692. return ret;
  9693. #ifdef __NR_exit_group
  9694. /* new thread calls */
  9695. case TARGET_NR_exit_group:
  9696. preexit_cleanup(cpu_env, arg1);
  9697. return get_errno(exit_group(arg1));
  9698. #endif
  9699. case TARGET_NR_setdomainname:
  9700. if (!(p = lock_user_string(arg1)))
  9701. return -TARGET_EFAULT;
  9702. ret = get_errno(setdomainname(p, arg2));
  9703. unlock_user(p, arg1, 0);
  9704. return ret;
  9705. case TARGET_NR_uname:
  9706. /* no need to transcode because we use the linux syscall */
  9707. {
  9708. struct new_utsname * buf;
  9709. if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
  9710. return -TARGET_EFAULT;
  9711. ret = get_errno(sys_uname(buf));
  9712. if (!is_error(ret)) {
  9713. /* Overwrite the native machine name with whatever is being
  9714. emulated. */
  9715. g_strlcpy(buf->machine, cpu_to_uname_machine(cpu_env),
  9716. sizeof(buf->machine));
  9717. /* Allow the user to override the reported release. */
  9718. if (qemu_uname_release && *qemu_uname_release) {
  9719. g_strlcpy(buf->release, qemu_uname_release,
  9720. sizeof(buf->release));
  9721. }
  9722. }
  9723. unlock_user_struct(buf, arg1, 1);
  9724. }
  9725. return ret;
  9726. #ifdef TARGET_I386
  9727. case TARGET_NR_modify_ldt:
  9728. return do_modify_ldt(cpu_env, arg1, arg2, arg3);
  9729. #if !defined(TARGET_X86_64)
  9730. case TARGET_NR_vm86:
  9731. return do_vm86(cpu_env, arg1, arg2);
  9732. #endif
  9733. #endif
  9734. #if defined(TARGET_NR_adjtimex)
  9735. case TARGET_NR_adjtimex:
  9736. {
  9737. struct timex host_buf;
  9738. if (target_to_host_timex(&host_buf, arg1) != 0) {
  9739. return -TARGET_EFAULT;
  9740. }
  9741. ret = get_errno(adjtimex(&host_buf));
  9742. if (!is_error(ret)) {
  9743. if (host_to_target_timex(arg1, &host_buf) != 0) {
  9744. return -TARGET_EFAULT;
  9745. }
  9746. }
  9747. }
  9748. return ret;
  9749. #endif
  9750. #if defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME)
  9751. case TARGET_NR_clock_adjtime:
  9752. {
  9753. struct timex htx, *phtx = &htx;
  9754. if (target_to_host_timex(phtx, arg2) != 0) {
  9755. return -TARGET_EFAULT;
  9756. }
  9757. ret = get_errno(clock_adjtime(arg1, phtx));
  9758. if (!is_error(ret) && phtx) {
  9759. if (host_to_target_timex(arg2, phtx) != 0) {
  9760. return -TARGET_EFAULT;
  9761. }
  9762. }
  9763. }
  9764. return ret;
  9765. #endif
  9766. #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
  9767. case TARGET_NR_clock_adjtime64:
  9768. {
  9769. struct timex htx;
  9770. if (target_to_host_timex64(&htx, arg2) != 0) {
  9771. return -TARGET_EFAULT;
  9772. }
  9773. ret = get_errno(clock_adjtime(arg1, &htx));
  9774. if (!is_error(ret) && host_to_target_timex64(arg2, &htx)) {
  9775. return -TARGET_EFAULT;
  9776. }
  9777. }
  9778. return ret;
  9779. #endif
  9780. case TARGET_NR_getpgid:
  9781. return get_errno(getpgid(arg1));
  9782. case TARGET_NR_fchdir:
  9783. return get_errno(fchdir(arg1));
  9784. case TARGET_NR_personality:
  9785. return get_errno(personality(arg1));
  9786. #ifdef TARGET_NR__llseek /* Not on alpha */
  9787. case TARGET_NR__llseek:
  9788. {
  9789. int64_t res;
  9790. #if !defined(__NR_llseek)
  9791. res = lseek(arg1, ((uint64_t)arg2 << 32) | (abi_ulong)arg3, arg5);
  9792. if (res == -1) {
  9793. ret = get_errno(res);
  9794. } else {
  9795. ret = 0;
  9796. }
  9797. #else
  9798. ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
  9799. #endif
  9800. if ((ret == 0) && put_user_s64(res, arg4)) {
  9801. return -TARGET_EFAULT;
  9802. }
  9803. }
  9804. return ret;
  9805. #endif
  9806. #ifdef TARGET_NR_getdents
  9807. case TARGET_NR_getdents:
  9808. return do_getdents(arg1, arg2, arg3);
  9809. #endif /* TARGET_NR_getdents */
  9810. #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
  9811. case TARGET_NR_getdents64:
  9812. return do_getdents64(arg1, arg2, arg3);
  9813. #endif /* TARGET_NR_getdents64 */
  9814. #if defined(TARGET_NR__newselect)
  9815. case TARGET_NR__newselect:
  9816. return do_select(arg1, arg2, arg3, arg4, arg5);
  9817. #endif
  9818. #ifdef TARGET_NR_poll
  9819. case TARGET_NR_poll:
  9820. return do_ppoll(arg1, arg2, arg3, arg4, arg5, false, false);
  9821. #endif
  9822. #ifdef TARGET_NR_ppoll
  9823. case TARGET_NR_ppoll:
  9824. return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, false);
  9825. #endif
  9826. #ifdef TARGET_NR_ppoll_time64
  9827. case TARGET_NR_ppoll_time64:
  9828. return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, true);
  9829. #endif
  9830. case TARGET_NR_flock:
  9831. /* NOTE: the flock constant seems to be the same for every
  9832. Linux platform */
  9833. return get_errno(safe_flock(arg1, arg2));
  9834. case TARGET_NR_readv:
  9835. {
  9836. struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
  9837. if (vec != NULL) {
  9838. ret = get_errno(safe_readv(arg1, vec, arg3));
  9839. unlock_iovec(vec, arg2, arg3, 1);
  9840. } else {
  9841. ret = -host_to_target_errno(errno);
  9842. }
  9843. }
  9844. return ret;
  9845. case TARGET_NR_writev:
  9846. {
  9847. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  9848. if (vec != NULL) {
  9849. ret = get_errno(safe_writev(arg1, vec, arg3));
  9850. unlock_iovec(vec, arg2, arg3, 0);
  9851. } else {
  9852. ret = -host_to_target_errno(errno);
  9853. }
  9854. }
  9855. return ret;
  9856. #if defined(TARGET_NR_preadv)
  9857. case TARGET_NR_preadv:
  9858. {
  9859. struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
  9860. if (vec != NULL) {
  9861. unsigned long low, high;
  9862. target_to_host_low_high(arg4, arg5, &low, &high);
  9863. ret = get_errno(safe_preadv(arg1, vec, arg3, low, high));
  9864. unlock_iovec(vec, arg2, arg3, 1);
  9865. } else {
  9866. ret = -host_to_target_errno(errno);
  9867. }
  9868. }
  9869. return ret;
  9870. #endif
  9871. #if defined(TARGET_NR_pwritev)
  9872. case TARGET_NR_pwritev:
  9873. {
  9874. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  9875. if (vec != NULL) {
  9876. unsigned long low, high;
  9877. target_to_host_low_high(arg4, arg5, &low, &high);
  9878. ret = get_errno(safe_pwritev(arg1, vec, arg3, low, high));
  9879. unlock_iovec(vec, arg2, arg3, 0);
  9880. } else {
  9881. ret = -host_to_target_errno(errno);
  9882. }
  9883. }
  9884. return ret;
  9885. #endif
  9886. case TARGET_NR_getsid:
  9887. return get_errno(getsid(arg1));
  9888. #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
  9889. case TARGET_NR_fdatasync:
  9890. return get_errno(fdatasync(arg1));
  9891. #endif
  9892. case TARGET_NR_sched_getaffinity:
  9893. {
  9894. unsigned int mask_size;
  9895. unsigned long *mask;
  9896. /*
  9897. * sched_getaffinity needs multiples of ulong, so need to take
  9898. * care of mismatches between target ulong and host ulong sizes.
  9899. */
  9900. if (arg2 & (sizeof(abi_ulong) - 1)) {
  9901. return -TARGET_EINVAL;
  9902. }
  9903. mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
  9904. mask = alloca(mask_size);
  9905. memset(mask, 0, mask_size);
  9906. ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
  9907. if (!is_error(ret)) {
  9908. if (ret > arg2) {
  9909. /* More data returned than the caller's buffer will fit.
  9910. * This only happens if sizeof(abi_long) < sizeof(long)
  9911. * and the caller passed us a buffer holding an odd number
  9912. * of abi_longs. If the host kernel is actually using the
  9913. * extra 4 bytes then fail EINVAL; otherwise we can just
  9914. * ignore them and only copy the interesting part.
  9915. */
  9916. int numcpus = sysconf(_SC_NPROCESSORS_CONF);
  9917. if (numcpus > arg2 * 8) {
  9918. return -TARGET_EINVAL;
  9919. }
  9920. ret = arg2;
  9921. }
  9922. if (host_to_target_cpu_mask(mask, mask_size, arg3, ret)) {
  9923. return -TARGET_EFAULT;
  9924. }
  9925. }
  9926. }
  9927. return ret;
  9928. case TARGET_NR_sched_setaffinity:
  9929. {
  9930. unsigned int mask_size;
  9931. unsigned long *mask;
  9932. /*
  9933. * sched_setaffinity needs multiples of ulong, so need to take
  9934. * care of mismatches between target ulong and host ulong sizes.
  9935. */
  9936. if (arg2 & (sizeof(abi_ulong) - 1)) {
  9937. return -TARGET_EINVAL;
  9938. }
  9939. mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
  9940. mask = alloca(mask_size);
  9941. ret = target_to_host_cpu_mask(mask, mask_size, arg3, arg2);
  9942. if (ret) {
  9943. return ret;
  9944. }
  9945. return get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
  9946. }
  9947. case TARGET_NR_getcpu:
  9948. {
  9949. unsigned cpu, node;
  9950. ret = get_errno(sys_getcpu(arg1 ? &cpu : NULL,
  9951. arg2 ? &node : NULL,
  9952. NULL));
  9953. if (is_error(ret)) {
  9954. return ret;
  9955. }
  9956. if (arg1 && put_user_u32(cpu, arg1)) {
  9957. return -TARGET_EFAULT;
  9958. }
  9959. if (arg2 && put_user_u32(node, arg2)) {
  9960. return -TARGET_EFAULT;
  9961. }
  9962. }
  9963. return ret;
  9964. case TARGET_NR_sched_setparam:
  9965. {
  9966. struct target_sched_param *target_schp;
  9967. struct sched_param schp;
  9968. if (arg2 == 0) {
  9969. return -TARGET_EINVAL;
  9970. }
  9971. if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1)) {
  9972. return -TARGET_EFAULT;
  9973. }
  9974. schp.sched_priority = tswap32(target_schp->sched_priority);
  9975. unlock_user_struct(target_schp, arg2, 0);
  9976. return get_errno(sys_sched_setparam(arg1, &schp));
  9977. }
  9978. case TARGET_NR_sched_getparam:
  9979. {
  9980. struct target_sched_param *target_schp;
  9981. struct sched_param schp;
  9982. if (arg2 == 0) {
  9983. return -TARGET_EINVAL;
  9984. }
  9985. ret = get_errno(sys_sched_getparam(arg1, &schp));
  9986. if (!is_error(ret)) {
  9987. if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0)) {
  9988. return -TARGET_EFAULT;
  9989. }
  9990. target_schp->sched_priority = tswap32(schp.sched_priority);
  9991. unlock_user_struct(target_schp, arg2, 1);
  9992. }
  9993. }
  9994. return ret;
  9995. case TARGET_NR_sched_setscheduler:
  9996. {
  9997. struct target_sched_param *target_schp;
  9998. struct sched_param schp;
  9999. if (arg3 == 0) {
  10000. return -TARGET_EINVAL;
  10001. }
  10002. if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1)) {
  10003. return -TARGET_EFAULT;
  10004. }
  10005. schp.sched_priority = tswap32(target_schp->sched_priority);
  10006. unlock_user_struct(target_schp, arg3, 0);
  10007. return get_errno(sys_sched_setscheduler(arg1, arg2, &schp));
  10008. }
  10009. case TARGET_NR_sched_getscheduler:
  10010. return get_errno(sys_sched_getscheduler(arg1));
  10011. case TARGET_NR_sched_getattr:
  10012. {
  10013. struct target_sched_attr *target_scha;
  10014. struct sched_attr scha;
  10015. if (arg2 == 0) {
  10016. return -TARGET_EINVAL;
  10017. }
  10018. if (arg3 > sizeof(scha)) {
  10019. arg3 = sizeof(scha);
  10020. }
  10021. ret = get_errno(sys_sched_getattr(arg1, &scha, arg3, arg4));
  10022. if (!is_error(ret)) {
  10023. target_scha = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  10024. if (!target_scha) {
  10025. return -TARGET_EFAULT;
  10026. }
  10027. target_scha->size = tswap32(scha.size);
  10028. target_scha->sched_policy = tswap32(scha.sched_policy);
  10029. target_scha->sched_flags = tswap64(scha.sched_flags);
  10030. target_scha->sched_nice = tswap32(scha.sched_nice);
  10031. target_scha->sched_priority = tswap32(scha.sched_priority);
  10032. target_scha->sched_runtime = tswap64(scha.sched_runtime);
  10033. target_scha->sched_deadline = tswap64(scha.sched_deadline);
  10034. target_scha->sched_period = tswap64(scha.sched_period);
  10035. if (scha.size > offsetof(struct sched_attr, sched_util_min)) {
  10036. target_scha->sched_util_min = tswap32(scha.sched_util_min);
  10037. target_scha->sched_util_max = tswap32(scha.sched_util_max);
  10038. }
  10039. unlock_user(target_scha, arg2, arg3);
  10040. }
  10041. return ret;
  10042. }
  10043. case TARGET_NR_sched_setattr:
  10044. {
  10045. struct target_sched_attr *target_scha;
  10046. struct sched_attr scha;
  10047. uint32_t size;
  10048. int zeroed;
  10049. if (arg2 == 0) {
  10050. return -TARGET_EINVAL;
  10051. }
  10052. if (get_user_u32(size, arg2)) {
  10053. return -TARGET_EFAULT;
  10054. }
  10055. if (!size) {
  10056. size = offsetof(struct target_sched_attr, sched_util_min);
  10057. }
  10058. if (size < offsetof(struct target_sched_attr, sched_util_min)) {
  10059. if (put_user_u32(sizeof(struct target_sched_attr), arg2)) {
  10060. return -TARGET_EFAULT;
  10061. }
  10062. return -TARGET_E2BIG;
  10063. }
  10064. zeroed = check_zeroed_user(arg2, sizeof(struct target_sched_attr), size);
  10065. if (zeroed < 0) {
  10066. return zeroed;
  10067. } else if (zeroed == 0) {
  10068. if (put_user_u32(sizeof(struct target_sched_attr), arg2)) {
  10069. return -TARGET_EFAULT;
  10070. }
  10071. return -TARGET_E2BIG;
  10072. }
  10073. if (size > sizeof(struct target_sched_attr)) {
  10074. size = sizeof(struct target_sched_attr);
  10075. }
  10076. target_scha = lock_user(VERIFY_READ, arg2, size, 1);
  10077. if (!target_scha) {
  10078. return -TARGET_EFAULT;
  10079. }
  10080. scha.size = size;
  10081. scha.sched_policy = tswap32(target_scha->sched_policy);
  10082. scha.sched_flags = tswap64(target_scha->sched_flags);
  10083. scha.sched_nice = tswap32(target_scha->sched_nice);
  10084. scha.sched_priority = tswap32(target_scha->sched_priority);
  10085. scha.sched_runtime = tswap64(target_scha->sched_runtime);
  10086. scha.sched_deadline = tswap64(target_scha->sched_deadline);
  10087. scha.sched_period = tswap64(target_scha->sched_period);
  10088. if (size > offsetof(struct target_sched_attr, sched_util_min)) {
  10089. scha.sched_util_min = tswap32(target_scha->sched_util_min);
  10090. scha.sched_util_max = tswap32(target_scha->sched_util_max);
  10091. }
  10092. unlock_user(target_scha, arg2, 0);
  10093. return get_errno(sys_sched_setattr(arg1, &scha, arg3));
  10094. }
  10095. case TARGET_NR_sched_yield:
  10096. return get_errno(sched_yield());
  10097. case TARGET_NR_sched_get_priority_max:
  10098. return get_errno(sched_get_priority_max(arg1));
  10099. case TARGET_NR_sched_get_priority_min:
  10100. return get_errno(sched_get_priority_min(arg1));
  10101. #ifdef TARGET_NR_sched_rr_get_interval
  10102. case TARGET_NR_sched_rr_get_interval:
  10103. {
  10104. struct timespec ts;
  10105. ret = get_errno(sched_rr_get_interval(arg1, &ts));
  10106. if (!is_error(ret)) {
  10107. ret = host_to_target_timespec(arg2, &ts);
  10108. }
  10109. }
  10110. return ret;
  10111. #endif
  10112. #ifdef TARGET_NR_sched_rr_get_interval_time64
  10113. case TARGET_NR_sched_rr_get_interval_time64:
  10114. {
  10115. struct timespec ts;
  10116. ret = get_errno(sched_rr_get_interval(arg1, &ts));
  10117. if (!is_error(ret)) {
  10118. ret = host_to_target_timespec64(arg2, &ts);
  10119. }
  10120. }
  10121. return ret;
  10122. #endif
  10123. #if defined(TARGET_NR_nanosleep)
  10124. case TARGET_NR_nanosleep:
  10125. {
  10126. struct timespec req, rem;
  10127. target_to_host_timespec(&req, arg1);
  10128. ret = get_errno(safe_nanosleep(&req, &rem));
  10129. if (is_error(ret) && arg2) {
  10130. host_to_target_timespec(arg2, &rem);
  10131. }
  10132. }
  10133. return ret;
  10134. #endif
  10135. case TARGET_NR_prctl:
  10136. return do_prctl(cpu_env, arg1, arg2, arg3, arg4, arg5);
  10137. break;
  10138. #ifdef TARGET_NR_arch_prctl
  10139. case TARGET_NR_arch_prctl:
  10140. return do_arch_prctl(cpu_env, arg1, arg2);
  10141. #endif
  10142. #ifdef TARGET_NR_pread64
  10143. case TARGET_NR_pread64:
  10144. if (regpairs_aligned(cpu_env, num)) {
  10145. arg4 = arg5;
  10146. arg5 = arg6;
  10147. }
  10148. if (arg2 == 0 && arg3 == 0) {
  10149. /* Special-case NULL buffer and zero length, which should succeed */
  10150. p = 0;
  10151. } else {
  10152. p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  10153. if (!p) {
  10154. return -TARGET_EFAULT;
  10155. }
  10156. }
  10157. ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
  10158. unlock_user(p, arg2, ret);
  10159. return ret;
  10160. case TARGET_NR_pwrite64:
  10161. if (regpairs_aligned(cpu_env, num)) {
  10162. arg4 = arg5;
  10163. arg5 = arg6;
  10164. }
  10165. if (arg2 == 0 && arg3 == 0) {
  10166. /* Special-case NULL buffer and zero length, which should succeed */
  10167. p = 0;
  10168. } else {
  10169. p = lock_user(VERIFY_READ, arg2, arg3, 1);
  10170. if (!p) {
  10171. return -TARGET_EFAULT;
  10172. }
  10173. }
  10174. ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
  10175. unlock_user(p, arg2, 0);
  10176. return ret;
  10177. #endif
  10178. case TARGET_NR_getcwd:
  10179. if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
  10180. return -TARGET_EFAULT;
  10181. ret = get_errno(sys_getcwd1(p, arg2));
  10182. unlock_user(p, arg1, ret);
  10183. return ret;
  10184. case TARGET_NR_capget:
  10185. case TARGET_NR_capset:
  10186. {
  10187. struct target_user_cap_header *target_header;
  10188. struct target_user_cap_data *target_data = NULL;
  10189. struct __user_cap_header_struct header;
  10190. struct __user_cap_data_struct data[2];
  10191. struct __user_cap_data_struct *dataptr = NULL;
  10192. int i, target_datalen;
  10193. int data_items = 1;
  10194. if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) {
  10195. return -TARGET_EFAULT;
  10196. }
  10197. header.version = tswap32(target_header->version);
  10198. header.pid = tswap32(target_header->pid);
  10199. if (header.version != _LINUX_CAPABILITY_VERSION) {
  10200. /* Version 2 and up takes pointer to two user_data structs */
  10201. data_items = 2;
  10202. }
  10203. target_datalen = sizeof(*target_data) * data_items;
  10204. if (arg2) {
  10205. if (num == TARGET_NR_capget) {
  10206. target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0);
  10207. } else {
  10208. target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1);
  10209. }
  10210. if (!target_data) {
  10211. unlock_user_struct(target_header, arg1, 0);
  10212. return -TARGET_EFAULT;
  10213. }
  10214. if (num == TARGET_NR_capset) {
  10215. for (i = 0; i < data_items; i++) {
  10216. data[i].effective = tswap32(target_data[i].effective);
  10217. data[i].permitted = tswap32(target_data[i].permitted);
  10218. data[i].inheritable = tswap32(target_data[i].inheritable);
  10219. }
  10220. }
  10221. dataptr = data;
  10222. }
  10223. if (num == TARGET_NR_capget) {
  10224. ret = get_errno(capget(&header, dataptr));
  10225. } else {
  10226. ret = get_errno(capset(&header, dataptr));
  10227. }
  10228. /* The kernel always updates version for both capget and capset */
  10229. target_header->version = tswap32(header.version);
  10230. unlock_user_struct(target_header, arg1, 1);
  10231. if (arg2) {
  10232. if (num == TARGET_NR_capget) {
  10233. for (i = 0; i < data_items; i++) {
  10234. target_data[i].effective = tswap32(data[i].effective);
  10235. target_data[i].permitted = tswap32(data[i].permitted);
  10236. target_data[i].inheritable = tswap32(data[i].inheritable);
  10237. }
  10238. unlock_user(target_data, arg2, target_datalen);
  10239. } else {
  10240. unlock_user(target_data, arg2, 0);
  10241. }
  10242. }
  10243. return ret;
  10244. }
  10245. case TARGET_NR_sigaltstack:
  10246. return do_sigaltstack(arg1, arg2, cpu_env);
  10247. #ifdef CONFIG_SENDFILE
  10248. #ifdef TARGET_NR_sendfile
  10249. case TARGET_NR_sendfile:
  10250. {
  10251. off_t *offp = NULL;
  10252. off_t off;
  10253. if (arg3) {
  10254. ret = get_user_sal(off, arg3);
  10255. if (is_error(ret)) {
  10256. return ret;
  10257. }
  10258. offp = &off;
  10259. }
  10260. ret = get_errno(sendfile(arg1, arg2, offp, arg4));
  10261. if (!is_error(ret) && arg3) {
  10262. abi_long ret2 = put_user_sal(off, arg3);
  10263. if (is_error(ret2)) {
  10264. ret = ret2;
  10265. }
  10266. }
  10267. return ret;
  10268. }
  10269. #endif
  10270. #ifdef TARGET_NR_sendfile64
  10271. case TARGET_NR_sendfile64:
  10272. {
  10273. off_t *offp = NULL;
  10274. off_t off;
  10275. if (arg3) {
  10276. ret = get_user_s64(off, arg3);
  10277. if (is_error(ret)) {
  10278. return ret;
  10279. }
  10280. offp = &off;
  10281. }
  10282. ret = get_errno(sendfile(arg1, arg2, offp, arg4));
  10283. if (!is_error(ret) && arg3) {
  10284. abi_long ret2 = put_user_s64(off, arg3);
  10285. if (is_error(ret2)) {
  10286. ret = ret2;
  10287. }
  10288. }
  10289. return ret;
  10290. }
  10291. #endif
  10292. #endif
  10293. #ifdef TARGET_NR_vfork
  10294. case TARGET_NR_vfork:
  10295. return get_errno(do_fork(cpu_env,
  10296. CLONE_VFORK | CLONE_VM | TARGET_SIGCHLD,
  10297. 0, 0, 0, 0));
  10298. #endif
  10299. #ifdef TARGET_NR_ugetrlimit
  10300. case TARGET_NR_ugetrlimit:
  10301. {
  10302. struct rlimit rlim;
  10303. int resource = target_to_host_resource(arg1);
  10304. ret = get_errno(getrlimit(resource, &rlim));
  10305. if (!is_error(ret)) {
  10306. struct target_rlimit *target_rlim;
  10307. if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
  10308. return -TARGET_EFAULT;
  10309. target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
  10310. target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
  10311. unlock_user_struct(target_rlim, arg2, 1);
  10312. }
  10313. return ret;
  10314. }
  10315. #endif
  10316. #ifdef TARGET_NR_truncate64
  10317. case TARGET_NR_truncate64:
  10318. if (!(p = lock_user_string(arg1)))
  10319. return -TARGET_EFAULT;
  10320. ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
  10321. unlock_user(p, arg1, 0);
  10322. return ret;
  10323. #endif
  10324. #ifdef TARGET_NR_ftruncate64
  10325. case TARGET_NR_ftruncate64:
  10326. return target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
  10327. #endif
  10328. #ifdef TARGET_NR_stat64
  10329. case TARGET_NR_stat64:
  10330. if (!(p = lock_user_string(arg1))) {
  10331. return -TARGET_EFAULT;
  10332. }
  10333. ret = get_errno(stat(path(p), &st));
  10334. unlock_user(p, arg1, 0);
  10335. if (!is_error(ret))
  10336. ret = host_to_target_stat64(cpu_env, arg2, &st);
  10337. return ret;
  10338. #endif
  10339. #ifdef TARGET_NR_lstat64
  10340. case TARGET_NR_lstat64:
  10341. if (!(p = lock_user_string(arg1))) {
  10342. return -TARGET_EFAULT;
  10343. }
  10344. ret = get_errno(lstat(path(p), &st));
  10345. unlock_user(p, arg1, 0);
  10346. if (!is_error(ret))
  10347. ret = host_to_target_stat64(cpu_env, arg2, &st);
  10348. return ret;
  10349. #endif
  10350. #ifdef TARGET_NR_fstat64
  10351. case TARGET_NR_fstat64:
  10352. ret = get_errno(fstat(arg1, &st));
  10353. if (!is_error(ret))
  10354. ret = host_to_target_stat64(cpu_env, arg2, &st);
  10355. return ret;
  10356. #endif
  10357. #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
  10358. #ifdef TARGET_NR_fstatat64
  10359. case TARGET_NR_fstatat64:
  10360. #endif
  10361. #ifdef TARGET_NR_newfstatat
  10362. case TARGET_NR_newfstatat:
  10363. #endif
  10364. if (!(p = lock_user_string(arg2))) {
  10365. return -TARGET_EFAULT;
  10366. }
  10367. ret = get_errno(fstatat(arg1, path(p), &st, arg4));
  10368. unlock_user(p, arg2, 0);
  10369. if (!is_error(ret))
  10370. ret = host_to_target_stat64(cpu_env, arg3, &st);
  10371. return ret;
  10372. #endif
  10373. #if defined(TARGET_NR_statx)
  10374. case TARGET_NR_statx:
  10375. {
  10376. struct target_statx *target_stx;
  10377. int dirfd = arg1;
  10378. int flags = arg3;
  10379. p = lock_user_string(arg2);
  10380. if (p == NULL) {
  10381. return -TARGET_EFAULT;
  10382. }
  10383. #if defined(__NR_statx)
  10384. {
  10385. /*
  10386. * It is assumed that struct statx is architecture independent.
  10387. */
  10388. struct target_statx host_stx;
  10389. int mask = arg4;
  10390. ret = get_errno(sys_statx(dirfd, p, flags, mask, &host_stx));
  10391. if (!is_error(ret)) {
  10392. if (host_to_target_statx(&host_stx, arg5) != 0) {
  10393. unlock_user(p, arg2, 0);
  10394. return -TARGET_EFAULT;
  10395. }
  10396. }
  10397. if (ret != -TARGET_ENOSYS) {
  10398. unlock_user(p, arg2, 0);
  10399. return ret;
  10400. }
  10401. }
  10402. #endif
  10403. ret = get_errno(fstatat(dirfd, path(p), &st, flags));
  10404. unlock_user(p, arg2, 0);
  10405. if (!is_error(ret)) {
  10406. if (!lock_user_struct(VERIFY_WRITE, target_stx, arg5, 0)) {
  10407. return -TARGET_EFAULT;
  10408. }
  10409. memset(target_stx, 0, sizeof(*target_stx));
  10410. __put_user(major(st.st_dev), &target_stx->stx_dev_major);
  10411. __put_user(minor(st.st_dev), &target_stx->stx_dev_minor);
  10412. __put_user(st.st_ino, &target_stx->stx_ino);
  10413. __put_user(st.st_mode, &target_stx->stx_mode);
  10414. __put_user(st.st_uid, &target_stx->stx_uid);
  10415. __put_user(st.st_gid, &target_stx->stx_gid);
  10416. __put_user(st.st_nlink, &target_stx->stx_nlink);
  10417. __put_user(major(st.st_rdev), &target_stx->stx_rdev_major);
  10418. __put_user(minor(st.st_rdev), &target_stx->stx_rdev_minor);
  10419. __put_user(st.st_size, &target_stx->stx_size);
  10420. __put_user(st.st_blksize, &target_stx->stx_blksize);
  10421. __put_user(st.st_blocks, &target_stx->stx_blocks);
  10422. __put_user(st.st_atime, &target_stx->stx_atime.tv_sec);
  10423. __put_user(st.st_mtime, &target_stx->stx_mtime.tv_sec);
  10424. __put_user(st.st_ctime, &target_stx->stx_ctime.tv_sec);
  10425. unlock_user_struct(target_stx, arg5, 1);
  10426. }
  10427. }
  10428. return ret;
  10429. #endif
  10430. #ifdef TARGET_NR_lchown
  10431. case TARGET_NR_lchown:
  10432. if (!(p = lock_user_string(arg1)))
  10433. return -TARGET_EFAULT;
  10434. ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
  10435. unlock_user(p, arg1, 0);
  10436. return ret;
  10437. #endif
  10438. #ifdef TARGET_NR_getuid
  10439. case TARGET_NR_getuid:
  10440. return get_errno(high2lowuid(getuid()));
  10441. #endif
  10442. #ifdef TARGET_NR_getgid
  10443. case TARGET_NR_getgid:
  10444. return get_errno(high2lowgid(getgid()));
  10445. #endif
  10446. #ifdef TARGET_NR_geteuid
  10447. case TARGET_NR_geteuid:
  10448. return get_errno(high2lowuid(geteuid()));
  10449. #endif
  10450. #ifdef TARGET_NR_getegid
  10451. case TARGET_NR_getegid:
  10452. return get_errno(high2lowgid(getegid()));
  10453. #endif
  10454. case TARGET_NR_setreuid:
  10455. return get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
  10456. case TARGET_NR_setregid:
  10457. return get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
  10458. case TARGET_NR_getgroups:
  10459. {
  10460. int gidsetsize = arg1;
  10461. target_id *target_grouplist;
  10462. gid_t *grouplist;
  10463. int i;
  10464. grouplist = alloca(gidsetsize * sizeof(gid_t));
  10465. ret = get_errno(getgroups(gidsetsize, grouplist));
  10466. if (gidsetsize == 0)
  10467. return ret;
  10468. if (!is_error(ret)) {
  10469. target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * sizeof(target_id), 0);
  10470. if (!target_grouplist)
  10471. return -TARGET_EFAULT;
  10472. for(i = 0;i < ret; i++)
  10473. target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
  10474. unlock_user(target_grouplist, arg2, gidsetsize * sizeof(target_id));
  10475. }
  10476. }
  10477. return ret;
  10478. case TARGET_NR_setgroups:
  10479. {
  10480. int gidsetsize = arg1;
  10481. target_id *target_grouplist;
  10482. gid_t *grouplist = NULL;
  10483. int i;
  10484. if (gidsetsize) {
  10485. grouplist = alloca(gidsetsize * sizeof(gid_t));
  10486. target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * sizeof(target_id), 1);
  10487. if (!target_grouplist) {
  10488. return -TARGET_EFAULT;
  10489. }
  10490. for (i = 0; i < gidsetsize; i++) {
  10491. grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
  10492. }
  10493. unlock_user(target_grouplist, arg2, 0);
  10494. }
  10495. return get_errno(setgroups(gidsetsize, grouplist));
  10496. }
  10497. case TARGET_NR_fchown:
  10498. return get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
  10499. #if defined(TARGET_NR_fchownat)
  10500. case TARGET_NR_fchownat:
  10501. if (!(p = lock_user_string(arg2)))
  10502. return -TARGET_EFAULT;
  10503. ret = get_errno(fchownat(arg1, p, low2highuid(arg3),
  10504. low2highgid(arg4), arg5));
  10505. unlock_user(p, arg2, 0);
  10506. return ret;
  10507. #endif
  10508. #ifdef TARGET_NR_setresuid
  10509. case TARGET_NR_setresuid:
  10510. return get_errno(sys_setresuid(low2highuid(arg1),
  10511. low2highuid(arg2),
  10512. low2highuid(arg3)));
  10513. #endif
  10514. #ifdef TARGET_NR_getresuid
  10515. case TARGET_NR_getresuid:
  10516. {
  10517. uid_t ruid, euid, suid;
  10518. ret = get_errno(getresuid(&ruid, &euid, &suid));
  10519. if (!is_error(ret)) {
  10520. if (put_user_id(high2lowuid(ruid), arg1)
  10521. || put_user_id(high2lowuid(euid), arg2)
  10522. || put_user_id(high2lowuid(suid), arg3))
  10523. return -TARGET_EFAULT;
  10524. }
  10525. }
  10526. return ret;
  10527. #endif
  10528. #ifdef TARGET_NR_getresgid
  10529. case TARGET_NR_setresgid:
  10530. return get_errno(sys_setresgid(low2highgid(arg1),
  10531. low2highgid(arg2),
  10532. low2highgid(arg3)));
  10533. #endif
  10534. #ifdef TARGET_NR_getresgid
  10535. case TARGET_NR_getresgid:
  10536. {
  10537. gid_t rgid, egid, sgid;
  10538. ret = get_errno(getresgid(&rgid, &egid, &sgid));
  10539. if (!is_error(ret)) {
  10540. if (put_user_id(high2lowgid(rgid), arg1)
  10541. || put_user_id(high2lowgid(egid), arg2)
  10542. || put_user_id(high2lowgid(sgid), arg3))
  10543. return -TARGET_EFAULT;
  10544. }
  10545. }
  10546. return ret;
  10547. #endif
  10548. #ifdef TARGET_NR_chown
  10549. case TARGET_NR_chown:
  10550. if (!(p = lock_user_string(arg1)))
  10551. return -TARGET_EFAULT;
  10552. ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
  10553. unlock_user(p, arg1, 0);
  10554. return ret;
  10555. #endif
  10556. case TARGET_NR_setuid:
  10557. return get_errno(sys_setuid(low2highuid(arg1)));
  10558. case TARGET_NR_setgid:
  10559. return get_errno(sys_setgid(low2highgid(arg1)));
  10560. case TARGET_NR_setfsuid:
  10561. return get_errno(setfsuid(arg1));
  10562. case TARGET_NR_setfsgid:
  10563. return get_errno(setfsgid(arg1));
  10564. #ifdef TARGET_NR_lchown32
  10565. case TARGET_NR_lchown32:
  10566. if (!(p = lock_user_string(arg1)))
  10567. return -TARGET_EFAULT;
  10568. ret = get_errno(lchown(p, arg2, arg3));
  10569. unlock_user(p, arg1, 0);
  10570. return ret;
  10571. #endif
  10572. #ifdef TARGET_NR_getuid32
  10573. case TARGET_NR_getuid32:
  10574. return get_errno(getuid());
  10575. #endif
  10576. #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
  10577. /* Alpha specific */
  10578. case TARGET_NR_getxuid:
  10579. {
  10580. uid_t euid;
  10581. euid=geteuid();
  10582. cpu_env->ir[IR_A4]=euid;
  10583. }
  10584. return get_errno(getuid());
  10585. #endif
  10586. #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
  10587. /* Alpha specific */
  10588. case TARGET_NR_getxgid:
  10589. {
  10590. uid_t egid;
  10591. egid=getegid();
  10592. cpu_env->ir[IR_A4]=egid;
  10593. }
  10594. return get_errno(getgid());
  10595. #endif
  10596. #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
  10597. /* Alpha specific */
  10598. case TARGET_NR_osf_getsysinfo:
  10599. ret = -TARGET_EOPNOTSUPP;
  10600. switch (arg1) {
  10601. case TARGET_GSI_IEEE_FP_CONTROL:
  10602. {
  10603. uint64_t fpcr = cpu_alpha_load_fpcr(cpu_env);
  10604. uint64_t swcr = cpu_env->swcr;
  10605. swcr &= ~SWCR_STATUS_MASK;
  10606. swcr |= (fpcr >> 35) & SWCR_STATUS_MASK;
  10607. if (put_user_u64 (swcr, arg2))
  10608. return -TARGET_EFAULT;
  10609. ret = 0;
  10610. }
  10611. break;
  10612. /* case GSI_IEEE_STATE_AT_SIGNAL:
  10613. -- Not implemented in linux kernel.
  10614. case GSI_UACPROC:
  10615. -- Retrieves current unaligned access state; not much used.
  10616. case GSI_PROC_TYPE:
  10617. -- Retrieves implver information; surely not used.
  10618. case GSI_GET_HWRPB:
  10619. -- Grabs a copy of the HWRPB; surely not used.
  10620. */
  10621. }
  10622. return ret;
  10623. #endif
  10624. #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
  10625. /* Alpha specific */
  10626. case TARGET_NR_osf_setsysinfo:
  10627. ret = -TARGET_EOPNOTSUPP;
  10628. switch (arg1) {
  10629. case TARGET_SSI_IEEE_FP_CONTROL:
  10630. {
  10631. uint64_t swcr, fpcr;
  10632. if (get_user_u64 (swcr, arg2)) {
  10633. return -TARGET_EFAULT;
  10634. }
  10635. /*
  10636. * The kernel calls swcr_update_status to update the
  10637. * status bits from the fpcr at every point that it
  10638. * could be queried. Therefore, we store the status
  10639. * bits only in FPCR.
  10640. */
  10641. cpu_env->swcr = swcr & (SWCR_TRAP_ENABLE_MASK | SWCR_MAP_MASK);
  10642. fpcr = cpu_alpha_load_fpcr(cpu_env);
  10643. fpcr &= ((uint64_t)FPCR_DYN_MASK << 32);
  10644. fpcr |= alpha_ieee_swcr_to_fpcr(swcr);
  10645. cpu_alpha_store_fpcr(cpu_env, fpcr);
  10646. ret = 0;
  10647. }
  10648. break;
  10649. case TARGET_SSI_IEEE_RAISE_EXCEPTION:
  10650. {
  10651. uint64_t exc, fpcr, fex;
  10652. if (get_user_u64(exc, arg2)) {
  10653. return -TARGET_EFAULT;
  10654. }
  10655. exc &= SWCR_STATUS_MASK;
  10656. fpcr = cpu_alpha_load_fpcr(cpu_env);
  10657. /* Old exceptions are not signaled. */
  10658. fex = alpha_ieee_fpcr_to_swcr(fpcr);
  10659. fex = exc & ~fex;
  10660. fex >>= SWCR_STATUS_TO_EXCSUM_SHIFT;
  10661. fex &= (cpu_env)->swcr;
  10662. /* Update the hardware fpcr. */
  10663. fpcr |= alpha_ieee_swcr_to_fpcr(exc);
  10664. cpu_alpha_store_fpcr(cpu_env, fpcr);
  10665. if (fex) {
  10666. int si_code = TARGET_FPE_FLTUNK;
  10667. target_siginfo_t info;
  10668. if (fex & SWCR_TRAP_ENABLE_DNO) {
  10669. si_code = TARGET_FPE_FLTUND;
  10670. }
  10671. if (fex & SWCR_TRAP_ENABLE_INE) {
  10672. si_code = TARGET_FPE_FLTRES;
  10673. }
  10674. if (fex & SWCR_TRAP_ENABLE_UNF) {
  10675. si_code = TARGET_FPE_FLTUND;
  10676. }
  10677. if (fex & SWCR_TRAP_ENABLE_OVF) {
  10678. si_code = TARGET_FPE_FLTOVF;
  10679. }
  10680. if (fex & SWCR_TRAP_ENABLE_DZE) {
  10681. si_code = TARGET_FPE_FLTDIV;
  10682. }
  10683. if (fex & SWCR_TRAP_ENABLE_INV) {
  10684. si_code = TARGET_FPE_FLTINV;
  10685. }
  10686. info.si_signo = SIGFPE;
  10687. info.si_errno = 0;
  10688. info.si_code = si_code;
  10689. info._sifields._sigfault._addr = (cpu_env)->pc;
  10690. queue_signal(cpu_env, info.si_signo,
  10691. QEMU_SI_FAULT, &info);
  10692. }
  10693. ret = 0;
  10694. }
  10695. break;
  10696. /* case SSI_NVPAIRS:
  10697. -- Used with SSIN_UACPROC to enable unaligned accesses.
  10698. case SSI_IEEE_STATE_AT_SIGNAL:
  10699. case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
  10700. -- Not implemented in linux kernel
  10701. */
  10702. }
  10703. return ret;
  10704. #endif
  10705. #ifdef TARGET_NR_osf_sigprocmask
  10706. /* Alpha specific. */
  10707. case TARGET_NR_osf_sigprocmask:
  10708. {
  10709. abi_ulong mask;
  10710. int how;
  10711. sigset_t set, oldset;
  10712. switch(arg1) {
  10713. case TARGET_SIG_BLOCK:
  10714. how = SIG_BLOCK;
  10715. break;
  10716. case TARGET_SIG_UNBLOCK:
  10717. how = SIG_UNBLOCK;
  10718. break;
  10719. case TARGET_SIG_SETMASK:
  10720. how = SIG_SETMASK;
  10721. break;
  10722. default:
  10723. return -TARGET_EINVAL;
  10724. }
  10725. mask = arg2;
  10726. target_to_host_old_sigset(&set, &mask);
  10727. ret = do_sigprocmask(how, &set, &oldset);
  10728. if (!ret) {
  10729. host_to_target_old_sigset(&mask, &oldset);
  10730. ret = mask;
  10731. }
  10732. }
  10733. return ret;
  10734. #endif
  10735. #ifdef TARGET_NR_getgid32
  10736. case TARGET_NR_getgid32:
  10737. return get_errno(getgid());
  10738. #endif
  10739. #ifdef TARGET_NR_geteuid32
  10740. case TARGET_NR_geteuid32:
  10741. return get_errno(geteuid());
  10742. #endif
  10743. #ifdef TARGET_NR_getegid32
  10744. case TARGET_NR_getegid32:
  10745. return get_errno(getegid());
  10746. #endif
  10747. #ifdef TARGET_NR_setreuid32
  10748. case TARGET_NR_setreuid32:
  10749. return get_errno(setreuid(arg1, arg2));
  10750. #endif
  10751. #ifdef TARGET_NR_setregid32
  10752. case TARGET_NR_setregid32:
  10753. return get_errno(setregid(arg1, arg2));
  10754. #endif
  10755. #ifdef TARGET_NR_getgroups32
  10756. case TARGET_NR_getgroups32:
  10757. {
  10758. int gidsetsize = arg1;
  10759. uint32_t *target_grouplist;
  10760. gid_t *grouplist;
  10761. int i;
  10762. grouplist = alloca(gidsetsize * sizeof(gid_t));
  10763. ret = get_errno(getgroups(gidsetsize, grouplist));
  10764. if (gidsetsize == 0)
  10765. return ret;
  10766. if (!is_error(ret)) {
  10767. target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
  10768. if (!target_grouplist) {
  10769. return -TARGET_EFAULT;
  10770. }
  10771. for(i = 0;i < ret; i++)
  10772. target_grouplist[i] = tswap32(grouplist[i]);
  10773. unlock_user(target_grouplist, arg2, gidsetsize * 4);
  10774. }
  10775. }
  10776. return ret;
  10777. #endif
  10778. #ifdef TARGET_NR_setgroups32
  10779. case TARGET_NR_setgroups32:
  10780. {
  10781. int gidsetsize = arg1;
  10782. uint32_t *target_grouplist;
  10783. gid_t *grouplist;
  10784. int i;
  10785. grouplist = alloca(gidsetsize * sizeof(gid_t));
  10786. target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
  10787. if (!target_grouplist) {
  10788. return -TARGET_EFAULT;
  10789. }
  10790. for(i = 0;i < gidsetsize; i++)
  10791. grouplist[i] = tswap32(target_grouplist[i]);
  10792. unlock_user(target_grouplist, arg2, 0);
  10793. return get_errno(setgroups(gidsetsize, grouplist));
  10794. }
  10795. #endif
  10796. #ifdef TARGET_NR_fchown32
  10797. case TARGET_NR_fchown32:
  10798. return get_errno(fchown(arg1, arg2, arg3));
  10799. #endif
  10800. #ifdef TARGET_NR_setresuid32
  10801. case TARGET_NR_setresuid32:
  10802. return get_errno(sys_setresuid(arg1, arg2, arg3));
  10803. #endif
  10804. #ifdef TARGET_NR_getresuid32
  10805. case TARGET_NR_getresuid32:
  10806. {
  10807. uid_t ruid, euid, suid;
  10808. ret = get_errno(getresuid(&ruid, &euid, &suid));
  10809. if (!is_error(ret)) {
  10810. if (put_user_u32(ruid, arg1)
  10811. || put_user_u32(euid, arg2)
  10812. || put_user_u32(suid, arg3))
  10813. return -TARGET_EFAULT;
  10814. }
  10815. }
  10816. return ret;
  10817. #endif
  10818. #ifdef TARGET_NR_setresgid32
  10819. case TARGET_NR_setresgid32:
  10820. return get_errno(sys_setresgid(arg1, arg2, arg3));
  10821. #endif
  10822. #ifdef TARGET_NR_getresgid32
  10823. case TARGET_NR_getresgid32:
  10824. {
  10825. gid_t rgid, egid, sgid;
  10826. ret = get_errno(getresgid(&rgid, &egid, &sgid));
  10827. if (!is_error(ret)) {
  10828. if (put_user_u32(rgid, arg1)
  10829. || put_user_u32(egid, arg2)
  10830. || put_user_u32(sgid, arg3))
  10831. return -TARGET_EFAULT;
  10832. }
  10833. }
  10834. return ret;
  10835. #endif
  10836. #ifdef TARGET_NR_chown32
  10837. case TARGET_NR_chown32:
  10838. if (!(p = lock_user_string(arg1)))
  10839. return -TARGET_EFAULT;
  10840. ret = get_errno(chown(p, arg2, arg3));
  10841. unlock_user(p, arg1, 0);
  10842. return ret;
  10843. #endif
  10844. #ifdef TARGET_NR_setuid32
  10845. case TARGET_NR_setuid32:
  10846. return get_errno(sys_setuid(arg1));
  10847. #endif
  10848. #ifdef TARGET_NR_setgid32
  10849. case TARGET_NR_setgid32:
  10850. return get_errno(sys_setgid(arg1));
  10851. #endif
  10852. #ifdef TARGET_NR_setfsuid32
  10853. case TARGET_NR_setfsuid32:
  10854. return get_errno(setfsuid(arg1));
  10855. #endif
  10856. #ifdef TARGET_NR_setfsgid32
  10857. case TARGET_NR_setfsgid32:
  10858. return get_errno(setfsgid(arg1));
  10859. #endif
  10860. #ifdef TARGET_NR_mincore
  10861. case TARGET_NR_mincore:
  10862. {
  10863. void *a = lock_user(VERIFY_READ, arg1, arg2, 0);
  10864. if (!a) {
  10865. return -TARGET_ENOMEM;
  10866. }
  10867. p = lock_user_string(arg3);
  10868. if (!p) {
  10869. ret = -TARGET_EFAULT;
  10870. } else {
  10871. ret = get_errno(mincore(a, arg2, p));
  10872. unlock_user(p, arg3, ret);
  10873. }
  10874. unlock_user(a, arg1, 0);
  10875. }
  10876. return ret;
  10877. #endif
  10878. #ifdef TARGET_NR_arm_fadvise64_64
  10879. case TARGET_NR_arm_fadvise64_64:
  10880. /* arm_fadvise64_64 looks like fadvise64_64 but
  10881. * with different argument order: fd, advice, offset, len
  10882. * rather than the usual fd, offset, len, advice.
  10883. * Note that offset and len are both 64-bit so appear as
  10884. * pairs of 32-bit registers.
  10885. */
  10886. ret = posix_fadvise(arg1, target_offset64(arg3, arg4),
  10887. target_offset64(arg5, arg6), arg2);
  10888. return -host_to_target_errno(ret);
  10889. #endif
  10890. #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
  10891. #ifdef TARGET_NR_fadvise64_64
  10892. case TARGET_NR_fadvise64_64:
  10893. #if defined(TARGET_PPC) || defined(TARGET_XTENSA)
  10894. /* 6 args: fd, advice, offset (high, low), len (high, low) */
  10895. ret = arg2;
  10896. arg2 = arg3;
  10897. arg3 = arg4;
  10898. arg4 = arg5;
  10899. arg5 = arg6;
  10900. arg6 = ret;
  10901. #else
  10902. /* 6 args: fd, offset (high, low), len (high, low), advice */
  10903. if (regpairs_aligned(cpu_env, num)) {
  10904. /* offset is in (3,4), len in (5,6) and advice in 7 */
  10905. arg2 = arg3;
  10906. arg3 = arg4;
  10907. arg4 = arg5;
  10908. arg5 = arg6;
  10909. arg6 = arg7;
  10910. }
  10911. #endif
  10912. ret = posix_fadvise(arg1, target_offset64(arg2, arg3),
  10913. target_offset64(arg4, arg5), arg6);
  10914. return -host_to_target_errno(ret);
  10915. #endif
  10916. #ifdef TARGET_NR_fadvise64
  10917. case TARGET_NR_fadvise64:
  10918. /* 5 args: fd, offset (high, low), len, advice */
  10919. if (regpairs_aligned(cpu_env, num)) {
  10920. /* offset is in (3,4), len in 5 and advice in 6 */
  10921. arg2 = arg3;
  10922. arg3 = arg4;
  10923. arg4 = arg5;
  10924. arg5 = arg6;
  10925. }
  10926. ret = posix_fadvise(arg1, target_offset64(arg2, arg3), arg4, arg5);
  10927. return -host_to_target_errno(ret);
  10928. #endif
  10929. #else /* not a 32-bit ABI */
  10930. #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_fadvise64)
  10931. #ifdef TARGET_NR_fadvise64_64
  10932. case TARGET_NR_fadvise64_64:
  10933. #endif
  10934. #ifdef TARGET_NR_fadvise64
  10935. case TARGET_NR_fadvise64:
  10936. #endif
  10937. #ifdef TARGET_S390X
  10938. switch (arg4) {
  10939. case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
  10940. case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
  10941. case 6: arg4 = POSIX_FADV_DONTNEED; break;
  10942. case 7: arg4 = POSIX_FADV_NOREUSE; break;
  10943. default: break;
  10944. }
  10945. #endif
  10946. return -host_to_target_errno(posix_fadvise(arg1, arg2, arg3, arg4));
  10947. #endif
  10948. #endif /* end of 64-bit ABI fadvise handling */
  10949. #ifdef TARGET_NR_madvise
  10950. case TARGET_NR_madvise:
  10951. return target_madvise(arg1, arg2, arg3);
  10952. #endif
  10953. #ifdef TARGET_NR_fcntl64
  10954. case TARGET_NR_fcntl64:
  10955. {
  10956. int cmd;
  10957. struct flock64 fl;
  10958. from_flock64_fn *copyfrom = copy_from_user_flock64;
  10959. to_flock64_fn *copyto = copy_to_user_flock64;
  10960. #ifdef TARGET_ARM
  10961. if (!cpu_env->eabi) {
  10962. copyfrom = copy_from_user_oabi_flock64;
  10963. copyto = copy_to_user_oabi_flock64;
  10964. }
  10965. #endif
  10966. cmd = target_to_host_fcntl_cmd(arg2);
  10967. if (cmd == -TARGET_EINVAL) {
  10968. return cmd;
  10969. }
  10970. switch(arg2) {
  10971. case TARGET_F_GETLK64:
  10972. ret = copyfrom(&fl, arg3);
  10973. if (ret) {
  10974. break;
  10975. }
  10976. ret = get_errno(safe_fcntl(arg1, cmd, &fl));
  10977. if (ret == 0) {
  10978. ret = copyto(arg3, &fl);
  10979. }
  10980. break;
  10981. case TARGET_F_SETLK64:
  10982. case TARGET_F_SETLKW64:
  10983. ret = copyfrom(&fl, arg3);
  10984. if (ret) {
  10985. break;
  10986. }
  10987. ret = get_errno(safe_fcntl(arg1, cmd, &fl));
  10988. break;
  10989. default:
  10990. ret = do_fcntl(arg1, arg2, arg3);
  10991. break;
  10992. }
  10993. return ret;
  10994. }
  10995. #endif
  10996. #ifdef TARGET_NR_cacheflush
  10997. case TARGET_NR_cacheflush:
  10998. /* self-modifying code is handled automatically, so nothing needed */
  10999. return 0;
  11000. #endif
  11001. #ifdef TARGET_NR_getpagesize
  11002. case TARGET_NR_getpagesize:
  11003. return TARGET_PAGE_SIZE;
  11004. #endif
  11005. case TARGET_NR_gettid:
  11006. return get_errno(sys_gettid());
  11007. #ifdef TARGET_NR_readahead
  11008. case TARGET_NR_readahead:
  11009. #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
  11010. if (regpairs_aligned(cpu_env, num)) {
  11011. arg2 = arg3;
  11012. arg3 = arg4;
  11013. arg4 = arg5;
  11014. }
  11015. ret = get_errno(readahead(arg1, target_offset64(arg2, arg3) , arg4));
  11016. #else
  11017. ret = get_errno(readahead(arg1, arg2, arg3));
  11018. #endif
  11019. return ret;
  11020. #endif
  11021. #ifdef CONFIG_ATTR
  11022. #ifdef TARGET_NR_setxattr
  11023. case TARGET_NR_listxattr:
  11024. case TARGET_NR_llistxattr:
  11025. {
  11026. void *p, *b = 0;
  11027. if (arg2) {
  11028. b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  11029. if (!b) {
  11030. return -TARGET_EFAULT;
  11031. }
  11032. }
  11033. p = lock_user_string(arg1);
  11034. if (p) {
  11035. if (num == TARGET_NR_listxattr) {
  11036. ret = get_errno(listxattr(p, b, arg3));
  11037. } else {
  11038. ret = get_errno(llistxattr(p, b, arg3));
  11039. }
  11040. } else {
  11041. ret = -TARGET_EFAULT;
  11042. }
  11043. unlock_user(p, arg1, 0);
  11044. unlock_user(b, arg2, arg3);
  11045. return ret;
  11046. }
  11047. case TARGET_NR_flistxattr:
  11048. {
  11049. void *b = 0;
  11050. if (arg2) {
  11051. b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  11052. if (!b) {
  11053. return -TARGET_EFAULT;
  11054. }
  11055. }
  11056. ret = get_errno(flistxattr(arg1, b, arg3));
  11057. unlock_user(b, arg2, arg3);
  11058. return ret;
  11059. }
  11060. case TARGET_NR_setxattr:
  11061. case TARGET_NR_lsetxattr:
  11062. {
  11063. void *p, *n, *v = 0;
  11064. if (arg3) {
  11065. v = lock_user(VERIFY_READ, arg3, arg4, 1);
  11066. if (!v) {
  11067. return -TARGET_EFAULT;
  11068. }
  11069. }
  11070. p = lock_user_string(arg1);
  11071. n = lock_user_string(arg2);
  11072. if (p && n) {
  11073. if (num == TARGET_NR_setxattr) {
  11074. ret = get_errno(setxattr(p, n, v, arg4, arg5));
  11075. } else {
  11076. ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
  11077. }
  11078. } else {
  11079. ret = -TARGET_EFAULT;
  11080. }
  11081. unlock_user(p, arg1, 0);
  11082. unlock_user(n, arg2, 0);
  11083. unlock_user(v, arg3, 0);
  11084. }
  11085. return ret;
  11086. case TARGET_NR_fsetxattr:
  11087. {
  11088. void *n, *v = 0;
  11089. if (arg3) {
  11090. v = lock_user(VERIFY_READ, arg3, arg4, 1);
  11091. if (!v) {
  11092. return -TARGET_EFAULT;
  11093. }
  11094. }
  11095. n = lock_user_string(arg2);
  11096. if (n) {
  11097. ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
  11098. } else {
  11099. ret = -TARGET_EFAULT;
  11100. }
  11101. unlock_user(n, arg2, 0);
  11102. unlock_user(v, arg3, 0);
  11103. }
  11104. return ret;
  11105. case TARGET_NR_getxattr:
  11106. case TARGET_NR_lgetxattr:
  11107. {
  11108. void *p, *n, *v = 0;
  11109. if (arg3) {
  11110. v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  11111. if (!v) {
  11112. return -TARGET_EFAULT;
  11113. }
  11114. }
  11115. p = lock_user_string(arg1);
  11116. n = lock_user_string(arg2);
  11117. if (p && n) {
  11118. if (num == TARGET_NR_getxattr) {
  11119. ret = get_errno(getxattr(p, n, v, arg4));
  11120. } else {
  11121. ret = get_errno(lgetxattr(p, n, v, arg4));
  11122. }
  11123. } else {
  11124. ret = -TARGET_EFAULT;
  11125. }
  11126. unlock_user(p, arg1, 0);
  11127. unlock_user(n, arg2, 0);
  11128. unlock_user(v, arg3, arg4);
  11129. }
  11130. return ret;
  11131. case TARGET_NR_fgetxattr:
  11132. {
  11133. void *n, *v = 0;
  11134. if (arg3) {
  11135. v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  11136. if (!v) {
  11137. return -TARGET_EFAULT;
  11138. }
  11139. }
  11140. n = lock_user_string(arg2);
  11141. if (n) {
  11142. ret = get_errno(fgetxattr(arg1, n, v, arg4));
  11143. } else {
  11144. ret = -TARGET_EFAULT;
  11145. }
  11146. unlock_user(n, arg2, 0);
  11147. unlock_user(v, arg3, arg4);
  11148. }
  11149. return ret;
  11150. case TARGET_NR_removexattr:
  11151. case TARGET_NR_lremovexattr:
  11152. {
  11153. void *p, *n;
  11154. p = lock_user_string(arg1);
  11155. n = lock_user_string(arg2);
  11156. if (p && n) {
  11157. if (num == TARGET_NR_removexattr) {
  11158. ret = get_errno(removexattr(p, n));
  11159. } else {
  11160. ret = get_errno(lremovexattr(p, n));
  11161. }
  11162. } else {
  11163. ret = -TARGET_EFAULT;
  11164. }
  11165. unlock_user(p, arg1, 0);
  11166. unlock_user(n, arg2, 0);
  11167. }
  11168. return ret;
  11169. case TARGET_NR_fremovexattr:
  11170. {
  11171. void *n;
  11172. n = lock_user_string(arg2);
  11173. if (n) {
  11174. ret = get_errno(fremovexattr(arg1, n));
  11175. } else {
  11176. ret = -TARGET_EFAULT;
  11177. }
  11178. unlock_user(n, arg2, 0);
  11179. }
  11180. return ret;
  11181. #endif
  11182. #endif /* CONFIG_ATTR */
  11183. #ifdef TARGET_NR_set_thread_area
  11184. case TARGET_NR_set_thread_area:
  11185. #if defined(TARGET_MIPS)
  11186. cpu_env->active_tc.CP0_UserLocal = arg1;
  11187. return 0;
  11188. #elif defined(TARGET_CRIS)
  11189. if (arg1 & 0xff)
  11190. ret = -TARGET_EINVAL;
  11191. else {
  11192. cpu_env->pregs[PR_PID] = arg1;
  11193. ret = 0;
  11194. }
  11195. return ret;
  11196. #elif defined(TARGET_I386) && defined(TARGET_ABI32)
  11197. return do_set_thread_area(cpu_env, arg1);
  11198. #elif defined(TARGET_M68K)
  11199. {
  11200. TaskState *ts = cpu->opaque;
  11201. ts->tp_value = arg1;
  11202. return 0;
  11203. }
  11204. #else
  11205. return -TARGET_ENOSYS;
  11206. #endif
  11207. #endif
  11208. #ifdef TARGET_NR_get_thread_area
  11209. case TARGET_NR_get_thread_area:
  11210. #if defined(TARGET_I386) && defined(TARGET_ABI32)
  11211. return do_get_thread_area(cpu_env, arg1);
  11212. #elif defined(TARGET_M68K)
  11213. {
  11214. TaskState *ts = cpu->opaque;
  11215. return ts->tp_value;
  11216. }
  11217. #else
  11218. return -TARGET_ENOSYS;
  11219. #endif
  11220. #endif
  11221. #ifdef TARGET_NR_getdomainname
  11222. case TARGET_NR_getdomainname:
  11223. return -TARGET_ENOSYS;
  11224. #endif
  11225. #ifdef TARGET_NR_clock_settime
  11226. case TARGET_NR_clock_settime:
  11227. {
  11228. struct timespec ts;
  11229. ret = target_to_host_timespec(&ts, arg2);
  11230. if (!is_error(ret)) {
  11231. ret = get_errno(clock_settime(arg1, &ts));
  11232. }
  11233. return ret;
  11234. }
  11235. #endif
  11236. #ifdef TARGET_NR_clock_settime64
  11237. case TARGET_NR_clock_settime64:
  11238. {
  11239. struct timespec ts;
  11240. ret = target_to_host_timespec64(&ts, arg2);
  11241. if (!is_error(ret)) {
  11242. ret = get_errno(clock_settime(arg1, &ts));
  11243. }
  11244. return ret;
  11245. }
  11246. #endif
  11247. #ifdef TARGET_NR_clock_gettime
  11248. case TARGET_NR_clock_gettime:
  11249. {
  11250. struct timespec ts;
  11251. ret = get_errno(clock_gettime(arg1, &ts));
  11252. if (!is_error(ret)) {
  11253. ret = host_to_target_timespec(arg2, &ts);
  11254. }
  11255. return ret;
  11256. }
  11257. #endif
  11258. #ifdef TARGET_NR_clock_gettime64
  11259. case TARGET_NR_clock_gettime64:
  11260. {
  11261. struct timespec ts;
  11262. ret = get_errno(clock_gettime(arg1, &ts));
  11263. if (!is_error(ret)) {
  11264. ret = host_to_target_timespec64(arg2, &ts);
  11265. }
  11266. return ret;
  11267. }
  11268. #endif
  11269. #ifdef TARGET_NR_clock_getres
  11270. case TARGET_NR_clock_getres:
  11271. {
  11272. struct timespec ts;
  11273. ret = get_errno(clock_getres(arg1, &ts));
  11274. if (!is_error(ret)) {
  11275. host_to_target_timespec(arg2, &ts);
  11276. }
  11277. return ret;
  11278. }
  11279. #endif
  11280. #ifdef TARGET_NR_clock_getres_time64
  11281. case TARGET_NR_clock_getres_time64:
  11282. {
  11283. struct timespec ts;
  11284. ret = get_errno(clock_getres(arg1, &ts));
  11285. if (!is_error(ret)) {
  11286. host_to_target_timespec64(arg2, &ts);
  11287. }
  11288. return ret;
  11289. }
  11290. #endif
  11291. #ifdef TARGET_NR_clock_nanosleep
  11292. case TARGET_NR_clock_nanosleep:
  11293. {
  11294. struct timespec ts;
  11295. if (target_to_host_timespec(&ts, arg3)) {
  11296. return -TARGET_EFAULT;
  11297. }
  11298. ret = get_errno(safe_clock_nanosleep(arg1, arg2,
  11299. &ts, arg4 ? &ts : NULL));
  11300. /*
  11301. * if the call is interrupted by a signal handler, it fails
  11302. * with error -TARGET_EINTR and if arg4 is not NULL and arg2 is not
  11303. * TIMER_ABSTIME, it returns the remaining unslept time in arg4.
  11304. */
  11305. if (ret == -TARGET_EINTR && arg4 && arg2 != TIMER_ABSTIME &&
  11306. host_to_target_timespec(arg4, &ts)) {
  11307. return -TARGET_EFAULT;
  11308. }
  11309. return ret;
  11310. }
  11311. #endif
  11312. #ifdef TARGET_NR_clock_nanosleep_time64
  11313. case TARGET_NR_clock_nanosleep_time64:
  11314. {
  11315. struct timespec ts;
  11316. if (target_to_host_timespec64(&ts, arg3)) {
  11317. return -TARGET_EFAULT;
  11318. }
  11319. ret = get_errno(safe_clock_nanosleep(arg1, arg2,
  11320. &ts, arg4 ? &ts : NULL));
  11321. if (ret == -TARGET_EINTR && arg4 && arg2 != TIMER_ABSTIME &&
  11322. host_to_target_timespec64(arg4, &ts)) {
  11323. return -TARGET_EFAULT;
  11324. }
  11325. return ret;
  11326. }
  11327. #endif
  11328. #if defined(TARGET_NR_set_tid_address)
  11329. case TARGET_NR_set_tid_address:
  11330. {
  11331. TaskState *ts = cpu->opaque;
  11332. ts->child_tidptr = arg1;
  11333. /* do not call host set_tid_address() syscall, instead return tid() */
  11334. return get_errno(sys_gettid());
  11335. }
  11336. #endif
  11337. case TARGET_NR_tkill:
  11338. return get_errno(safe_tkill((int)arg1, target_to_host_signal(arg2)));
  11339. case TARGET_NR_tgkill:
  11340. return get_errno(safe_tgkill((int)arg1, (int)arg2,
  11341. target_to_host_signal(arg3)));
  11342. #ifdef TARGET_NR_set_robust_list
  11343. case TARGET_NR_set_robust_list:
  11344. case TARGET_NR_get_robust_list:
  11345. /* The ABI for supporting robust futexes has userspace pass
  11346. * the kernel a pointer to a linked list which is updated by
  11347. * userspace after the syscall; the list is walked by the kernel
  11348. * when the thread exits. Since the linked list in QEMU guest
  11349. * memory isn't a valid linked list for the host and we have
  11350. * no way to reliably intercept the thread-death event, we can't
  11351. * support these. Silently return ENOSYS so that guest userspace
  11352. * falls back to a non-robust futex implementation (which should
  11353. * be OK except in the corner case of the guest crashing while
  11354. * holding a mutex that is shared with another process via
  11355. * shared memory).
  11356. */
  11357. return -TARGET_ENOSYS;
  11358. #endif
  11359. #if defined(TARGET_NR_utimensat)
  11360. case TARGET_NR_utimensat:
  11361. {
  11362. struct timespec *tsp, ts[2];
  11363. if (!arg3) {
  11364. tsp = NULL;
  11365. } else {
  11366. if (target_to_host_timespec(ts, arg3)) {
  11367. return -TARGET_EFAULT;
  11368. }
  11369. if (target_to_host_timespec(ts + 1, arg3 +
  11370. sizeof(struct target_timespec))) {
  11371. return -TARGET_EFAULT;
  11372. }
  11373. tsp = ts;
  11374. }
  11375. if (!arg2)
  11376. ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
  11377. else {
  11378. if (!(p = lock_user_string(arg2))) {
  11379. return -TARGET_EFAULT;
  11380. }
  11381. ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
  11382. unlock_user(p, arg2, 0);
  11383. }
  11384. }
  11385. return ret;
  11386. #endif
  11387. #ifdef TARGET_NR_utimensat_time64
  11388. case TARGET_NR_utimensat_time64:
  11389. {
  11390. struct timespec *tsp, ts[2];
  11391. if (!arg3) {
  11392. tsp = NULL;
  11393. } else {
  11394. if (target_to_host_timespec64(ts, arg3)) {
  11395. return -TARGET_EFAULT;
  11396. }
  11397. if (target_to_host_timespec64(ts + 1, arg3 +
  11398. sizeof(struct target__kernel_timespec))) {
  11399. return -TARGET_EFAULT;
  11400. }
  11401. tsp = ts;
  11402. }
  11403. if (!arg2)
  11404. ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
  11405. else {
  11406. p = lock_user_string(arg2);
  11407. if (!p) {
  11408. return -TARGET_EFAULT;
  11409. }
  11410. ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
  11411. unlock_user(p, arg2, 0);
  11412. }
  11413. }
  11414. return ret;
  11415. #endif
  11416. #ifdef TARGET_NR_futex
  11417. case TARGET_NR_futex:
  11418. return do_futex(cpu, false, arg1, arg2, arg3, arg4, arg5, arg6);
  11419. #endif
  11420. #ifdef TARGET_NR_futex_time64
  11421. case TARGET_NR_futex_time64:
  11422. return do_futex(cpu, true, arg1, arg2, arg3, arg4, arg5, arg6);
  11423. #endif
  11424. #ifdef CONFIG_INOTIFY
  11425. #if defined(TARGET_NR_inotify_init)
  11426. case TARGET_NR_inotify_init:
  11427. ret = get_errno(inotify_init());
  11428. if (ret >= 0) {
  11429. fd_trans_register(ret, &target_inotify_trans);
  11430. }
  11431. return ret;
  11432. #endif
  11433. #if defined(TARGET_NR_inotify_init1) && defined(CONFIG_INOTIFY1)
  11434. case TARGET_NR_inotify_init1:
  11435. ret = get_errno(inotify_init1(target_to_host_bitmask(arg1,
  11436. fcntl_flags_tbl)));
  11437. if (ret >= 0) {
  11438. fd_trans_register(ret, &target_inotify_trans);
  11439. }
  11440. return ret;
  11441. #endif
  11442. #if defined(TARGET_NR_inotify_add_watch)
  11443. case TARGET_NR_inotify_add_watch:
  11444. p = lock_user_string(arg2);
  11445. ret = get_errno(inotify_add_watch(arg1, path(p), arg3));
  11446. unlock_user(p, arg2, 0);
  11447. return ret;
  11448. #endif
  11449. #if defined(TARGET_NR_inotify_rm_watch)
  11450. case TARGET_NR_inotify_rm_watch:
  11451. return get_errno(inotify_rm_watch(arg1, arg2));
  11452. #endif
  11453. #endif
  11454. #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
  11455. case TARGET_NR_mq_open:
  11456. {
  11457. struct mq_attr posix_mq_attr;
  11458. struct mq_attr *pposix_mq_attr;
  11459. int host_flags;
  11460. host_flags = target_to_host_bitmask(arg2, fcntl_flags_tbl);
  11461. pposix_mq_attr = NULL;
  11462. if (arg4) {
  11463. if (copy_from_user_mq_attr(&posix_mq_attr, arg4) != 0) {
  11464. return -TARGET_EFAULT;
  11465. }
  11466. pposix_mq_attr = &posix_mq_attr;
  11467. }
  11468. p = lock_user_string(arg1 - 1);
  11469. if (!p) {
  11470. return -TARGET_EFAULT;
  11471. }
  11472. ret = get_errno(mq_open(p, host_flags, arg3, pposix_mq_attr));
  11473. unlock_user (p, arg1, 0);
  11474. }
  11475. return ret;
  11476. case TARGET_NR_mq_unlink:
  11477. p = lock_user_string(arg1 - 1);
  11478. if (!p) {
  11479. return -TARGET_EFAULT;
  11480. }
  11481. ret = get_errno(mq_unlink(p));
  11482. unlock_user (p, arg1, 0);
  11483. return ret;
  11484. #ifdef TARGET_NR_mq_timedsend
  11485. case TARGET_NR_mq_timedsend:
  11486. {
  11487. struct timespec ts;
  11488. p = lock_user (VERIFY_READ, arg2, arg3, 1);
  11489. if (arg5 != 0) {
  11490. if (target_to_host_timespec(&ts, arg5)) {
  11491. return -TARGET_EFAULT;
  11492. }
  11493. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts));
  11494. if (!is_error(ret) && host_to_target_timespec(arg5, &ts)) {
  11495. return -TARGET_EFAULT;
  11496. }
  11497. } else {
  11498. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL));
  11499. }
  11500. unlock_user (p, arg2, arg3);
  11501. }
  11502. return ret;
  11503. #endif
  11504. #ifdef TARGET_NR_mq_timedsend_time64
  11505. case TARGET_NR_mq_timedsend_time64:
  11506. {
  11507. struct timespec ts;
  11508. p = lock_user(VERIFY_READ, arg2, arg3, 1);
  11509. if (arg5 != 0) {
  11510. if (target_to_host_timespec64(&ts, arg5)) {
  11511. return -TARGET_EFAULT;
  11512. }
  11513. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts));
  11514. if (!is_error(ret) && host_to_target_timespec64(arg5, &ts)) {
  11515. return -TARGET_EFAULT;
  11516. }
  11517. } else {
  11518. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL));
  11519. }
  11520. unlock_user(p, arg2, arg3);
  11521. }
  11522. return ret;
  11523. #endif
  11524. #ifdef TARGET_NR_mq_timedreceive
  11525. case TARGET_NR_mq_timedreceive:
  11526. {
  11527. struct timespec ts;
  11528. unsigned int prio;
  11529. p = lock_user (VERIFY_READ, arg2, arg3, 1);
  11530. if (arg5 != 0) {
  11531. if (target_to_host_timespec(&ts, arg5)) {
  11532. return -TARGET_EFAULT;
  11533. }
  11534. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  11535. &prio, &ts));
  11536. if (!is_error(ret) && host_to_target_timespec(arg5, &ts)) {
  11537. return -TARGET_EFAULT;
  11538. }
  11539. } else {
  11540. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  11541. &prio, NULL));
  11542. }
  11543. unlock_user (p, arg2, arg3);
  11544. if (arg4 != 0)
  11545. put_user_u32(prio, arg4);
  11546. }
  11547. return ret;
  11548. #endif
  11549. #ifdef TARGET_NR_mq_timedreceive_time64
  11550. case TARGET_NR_mq_timedreceive_time64:
  11551. {
  11552. struct timespec ts;
  11553. unsigned int prio;
  11554. p = lock_user(VERIFY_READ, arg2, arg3, 1);
  11555. if (arg5 != 0) {
  11556. if (target_to_host_timespec64(&ts, arg5)) {
  11557. return -TARGET_EFAULT;
  11558. }
  11559. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  11560. &prio, &ts));
  11561. if (!is_error(ret) && host_to_target_timespec64(arg5, &ts)) {
  11562. return -TARGET_EFAULT;
  11563. }
  11564. } else {
  11565. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  11566. &prio, NULL));
  11567. }
  11568. unlock_user(p, arg2, arg3);
  11569. if (arg4 != 0) {
  11570. put_user_u32(prio, arg4);
  11571. }
  11572. }
  11573. return ret;
  11574. #endif
  11575. /* Not implemented for now... */
  11576. /* case TARGET_NR_mq_notify: */
  11577. /* break; */
  11578. case TARGET_NR_mq_getsetattr:
  11579. {
  11580. struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
  11581. ret = 0;
  11582. if (arg2 != 0) {
  11583. copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
  11584. ret = get_errno(mq_setattr(arg1, &posix_mq_attr_in,
  11585. &posix_mq_attr_out));
  11586. } else if (arg3 != 0) {
  11587. ret = get_errno(mq_getattr(arg1, &posix_mq_attr_out));
  11588. }
  11589. if (ret == 0 && arg3 != 0) {
  11590. copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
  11591. }
  11592. }
  11593. return ret;
  11594. #endif
  11595. #ifdef CONFIG_SPLICE
  11596. #ifdef TARGET_NR_tee
  11597. case TARGET_NR_tee:
  11598. {
  11599. ret = get_errno(tee(arg1,arg2,arg3,arg4));
  11600. }
  11601. return ret;
  11602. #endif
  11603. #ifdef TARGET_NR_splice
  11604. case TARGET_NR_splice:
  11605. {
  11606. loff_t loff_in, loff_out;
  11607. loff_t *ploff_in = NULL, *ploff_out = NULL;
  11608. if (arg2) {
  11609. if (get_user_u64(loff_in, arg2)) {
  11610. return -TARGET_EFAULT;
  11611. }
  11612. ploff_in = &loff_in;
  11613. }
  11614. if (arg4) {
  11615. if (get_user_u64(loff_out, arg4)) {
  11616. return -TARGET_EFAULT;
  11617. }
  11618. ploff_out = &loff_out;
  11619. }
  11620. ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
  11621. if (arg2) {
  11622. if (put_user_u64(loff_in, arg2)) {
  11623. return -TARGET_EFAULT;
  11624. }
  11625. }
  11626. if (arg4) {
  11627. if (put_user_u64(loff_out, arg4)) {
  11628. return -TARGET_EFAULT;
  11629. }
  11630. }
  11631. }
  11632. return ret;
  11633. #endif
  11634. #ifdef TARGET_NR_vmsplice
  11635. case TARGET_NR_vmsplice:
  11636. {
  11637. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  11638. if (vec != NULL) {
  11639. ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
  11640. unlock_iovec(vec, arg2, arg3, 0);
  11641. } else {
  11642. ret = -host_to_target_errno(errno);
  11643. }
  11644. }
  11645. return ret;
  11646. #endif
  11647. #endif /* CONFIG_SPLICE */
  11648. #ifdef CONFIG_EVENTFD
  11649. #if defined(TARGET_NR_eventfd)
  11650. case TARGET_NR_eventfd:
  11651. ret = get_errno(eventfd(arg1, 0));
  11652. if (ret >= 0) {
  11653. fd_trans_register(ret, &target_eventfd_trans);
  11654. }
  11655. return ret;
  11656. #endif
  11657. #if defined(TARGET_NR_eventfd2)
  11658. case TARGET_NR_eventfd2:
  11659. {
  11660. int host_flags = arg2 & (~(TARGET_O_NONBLOCK_MASK | TARGET_O_CLOEXEC));
  11661. if (arg2 & TARGET_O_NONBLOCK) {
  11662. host_flags |= O_NONBLOCK;
  11663. }
  11664. if (arg2 & TARGET_O_CLOEXEC) {
  11665. host_flags |= O_CLOEXEC;
  11666. }
  11667. ret = get_errno(eventfd(arg1, host_flags));
  11668. if (ret >= 0) {
  11669. fd_trans_register(ret, &target_eventfd_trans);
  11670. }
  11671. return ret;
  11672. }
  11673. #endif
  11674. #endif /* CONFIG_EVENTFD */
  11675. #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
  11676. case TARGET_NR_fallocate:
  11677. #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
  11678. ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
  11679. target_offset64(arg5, arg6)));
  11680. #else
  11681. ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
  11682. #endif
  11683. return ret;
  11684. #endif
  11685. #if defined(CONFIG_SYNC_FILE_RANGE)
  11686. #if defined(TARGET_NR_sync_file_range)
  11687. case TARGET_NR_sync_file_range:
  11688. #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
  11689. #if defined(TARGET_MIPS)
  11690. ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
  11691. target_offset64(arg5, arg6), arg7));
  11692. #else
  11693. ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
  11694. target_offset64(arg4, arg5), arg6));
  11695. #endif /* !TARGET_MIPS */
  11696. #else
  11697. ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
  11698. #endif
  11699. return ret;
  11700. #endif
  11701. #if defined(TARGET_NR_sync_file_range2) || \
  11702. defined(TARGET_NR_arm_sync_file_range)
  11703. #if defined(TARGET_NR_sync_file_range2)
  11704. case TARGET_NR_sync_file_range2:
  11705. #endif
  11706. #if defined(TARGET_NR_arm_sync_file_range)
  11707. case TARGET_NR_arm_sync_file_range:
  11708. #endif
  11709. /* This is like sync_file_range but the arguments are reordered */
  11710. #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
  11711. ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
  11712. target_offset64(arg5, arg6), arg2));
  11713. #else
  11714. ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
  11715. #endif
  11716. return ret;
  11717. #endif
  11718. #endif
  11719. #if defined(TARGET_NR_signalfd4)
  11720. case TARGET_NR_signalfd4:
  11721. return do_signalfd4(arg1, arg2, arg4);
  11722. #endif
  11723. #if defined(TARGET_NR_signalfd)
  11724. case TARGET_NR_signalfd:
  11725. return do_signalfd4(arg1, arg2, 0);
  11726. #endif
  11727. #if defined(CONFIG_EPOLL)
  11728. #if defined(TARGET_NR_epoll_create)
  11729. case TARGET_NR_epoll_create:
  11730. return get_errno(epoll_create(arg1));
  11731. #endif
  11732. #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
  11733. case TARGET_NR_epoll_create1:
  11734. return get_errno(epoll_create1(target_to_host_bitmask(arg1, fcntl_flags_tbl)));
  11735. #endif
  11736. #if defined(TARGET_NR_epoll_ctl)
  11737. case TARGET_NR_epoll_ctl:
  11738. {
  11739. struct epoll_event ep;
  11740. struct epoll_event *epp = 0;
  11741. if (arg4) {
  11742. if (arg2 != EPOLL_CTL_DEL) {
  11743. struct target_epoll_event *target_ep;
  11744. if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
  11745. return -TARGET_EFAULT;
  11746. }
  11747. ep.events = tswap32(target_ep->events);
  11748. /*
  11749. * The epoll_data_t union is just opaque data to the kernel,
  11750. * so we transfer all 64 bits across and need not worry what
  11751. * actual data type it is.
  11752. */
  11753. ep.data.u64 = tswap64(target_ep->data.u64);
  11754. unlock_user_struct(target_ep, arg4, 0);
  11755. }
  11756. /*
  11757. * before kernel 2.6.9, EPOLL_CTL_DEL operation required a
  11758. * non-null pointer, even though this argument is ignored.
  11759. *
  11760. */
  11761. epp = &ep;
  11762. }
  11763. return get_errno(epoll_ctl(arg1, arg2, arg3, epp));
  11764. }
  11765. #endif
  11766. #if defined(TARGET_NR_epoll_wait) || defined(TARGET_NR_epoll_pwait)
  11767. #if defined(TARGET_NR_epoll_wait)
  11768. case TARGET_NR_epoll_wait:
  11769. #endif
  11770. #if defined(TARGET_NR_epoll_pwait)
  11771. case TARGET_NR_epoll_pwait:
  11772. #endif
  11773. {
  11774. struct target_epoll_event *target_ep;
  11775. struct epoll_event *ep;
  11776. int epfd = arg1;
  11777. int maxevents = arg3;
  11778. int timeout = arg4;
  11779. if (maxevents <= 0 || maxevents > TARGET_EP_MAX_EVENTS) {
  11780. return -TARGET_EINVAL;
  11781. }
  11782. target_ep = lock_user(VERIFY_WRITE, arg2,
  11783. maxevents * sizeof(struct target_epoll_event), 1);
  11784. if (!target_ep) {
  11785. return -TARGET_EFAULT;
  11786. }
  11787. ep = g_try_new(struct epoll_event, maxevents);
  11788. if (!ep) {
  11789. unlock_user(target_ep, arg2, 0);
  11790. return -TARGET_ENOMEM;
  11791. }
  11792. switch (num) {
  11793. #if defined(TARGET_NR_epoll_pwait)
  11794. case TARGET_NR_epoll_pwait:
  11795. {
  11796. sigset_t *set = NULL;
  11797. if (arg5) {
  11798. ret = process_sigsuspend_mask(&set, arg5, arg6);
  11799. if (ret != 0) {
  11800. break;
  11801. }
  11802. }
  11803. ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
  11804. set, SIGSET_T_SIZE));
  11805. if (set) {
  11806. finish_sigsuspend_mask(ret);
  11807. }
  11808. break;
  11809. }
  11810. #endif
  11811. #if defined(TARGET_NR_epoll_wait)
  11812. case TARGET_NR_epoll_wait:
  11813. ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
  11814. NULL, 0));
  11815. break;
  11816. #endif
  11817. default:
  11818. ret = -TARGET_ENOSYS;
  11819. }
  11820. if (!is_error(ret)) {
  11821. int i;
  11822. for (i = 0; i < ret; i++) {
  11823. target_ep[i].events = tswap32(ep[i].events);
  11824. target_ep[i].data.u64 = tswap64(ep[i].data.u64);
  11825. }
  11826. unlock_user(target_ep, arg2,
  11827. ret * sizeof(struct target_epoll_event));
  11828. } else {
  11829. unlock_user(target_ep, arg2, 0);
  11830. }
  11831. g_free(ep);
  11832. return ret;
  11833. }
  11834. #endif
  11835. #endif
  11836. #ifdef TARGET_NR_prlimit64
  11837. case TARGET_NR_prlimit64:
  11838. {
  11839. /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
  11840. struct target_rlimit64 *target_rnew, *target_rold;
  11841. struct host_rlimit64 rnew, rold, *rnewp = 0;
  11842. int resource = target_to_host_resource(arg2);
  11843. if (arg3 && (resource != RLIMIT_AS &&
  11844. resource != RLIMIT_DATA &&
  11845. resource != RLIMIT_STACK)) {
  11846. if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
  11847. return -TARGET_EFAULT;
  11848. }
  11849. rnew.rlim_cur = tswap64(target_rnew->rlim_cur);
  11850. rnew.rlim_max = tswap64(target_rnew->rlim_max);
  11851. unlock_user_struct(target_rnew, arg3, 0);
  11852. rnewp = &rnew;
  11853. }
  11854. ret = get_errno(sys_prlimit64(arg1, resource, rnewp, arg4 ? &rold : 0));
  11855. if (!is_error(ret) && arg4) {
  11856. if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
  11857. return -TARGET_EFAULT;
  11858. }
  11859. target_rold->rlim_cur = tswap64(rold.rlim_cur);
  11860. target_rold->rlim_max = tswap64(rold.rlim_max);
  11861. unlock_user_struct(target_rold, arg4, 1);
  11862. }
  11863. return ret;
  11864. }
  11865. #endif
  11866. #ifdef TARGET_NR_gethostname
  11867. case TARGET_NR_gethostname:
  11868. {
  11869. char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
  11870. if (name) {
  11871. ret = get_errno(gethostname(name, arg2));
  11872. unlock_user(name, arg1, arg2);
  11873. } else {
  11874. ret = -TARGET_EFAULT;
  11875. }
  11876. return ret;
  11877. }
  11878. #endif
  11879. #ifdef TARGET_NR_atomic_cmpxchg_32
  11880. case TARGET_NR_atomic_cmpxchg_32:
  11881. {
  11882. /* should use start_exclusive from main.c */
  11883. abi_ulong mem_value;
  11884. if (get_user_u32(mem_value, arg6)) {
  11885. target_siginfo_t info;
  11886. info.si_signo = SIGSEGV;
  11887. info.si_errno = 0;
  11888. info.si_code = TARGET_SEGV_MAPERR;
  11889. info._sifields._sigfault._addr = arg6;
  11890. queue_signal(cpu_env, info.si_signo, QEMU_SI_FAULT, &info);
  11891. ret = 0xdeadbeef;
  11892. }
  11893. if (mem_value == arg2)
  11894. put_user_u32(arg1, arg6);
  11895. return mem_value;
  11896. }
  11897. #endif
  11898. #ifdef TARGET_NR_atomic_barrier
  11899. case TARGET_NR_atomic_barrier:
  11900. /* Like the kernel implementation and the
  11901. qemu arm barrier, no-op this? */
  11902. return 0;
  11903. #endif
  11904. #ifdef TARGET_NR_timer_create
  11905. case TARGET_NR_timer_create:
  11906. {
  11907. /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
  11908. struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL;
  11909. int clkid = arg1;
  11910. int timer_index = next_free_host_timer();
  11911. if (timer_index < 0) {
  11912. ret = -TARGET_EAGAIN;
  11913. } else {
  11914. timer_t *phtimer = g_posix_timers + timer_index;
  11915. if (arg2) {
  11916. phost_sevp = &host_sevp;
  11917. ret = target_to_host_sigevent(phost_sevp, arg2);
  11918. if (ret != 0) {
  11919. free_host_timer_slot(timer_index);
  11920. return ret;
  11921. }
  11922. }
  11923. ret = get_errno(timer_create(clkid, phost_sevp, phtimer));
  11924. if (ret) {
  11925. free_host_timer_slot(timer_index);
  11926. } else {
  11927. if (put_user(TIMER_MAGIC | timer_index, arg3, target_timer_t)) {
  11928. timer_delete(*phtimer);
  11929. free_host_timer_slot(timer_index);
  11930. return -TARGET_EFAULT;
  11931. }
  11932. }
  11933. }
  11934. return ret;
  11935. }
  11936. #endif
  11937. #ifdef TARGET_NR_timer_settime
  11938. case TARGET_NR_timer_settime:
  11939. {
  11940. /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
  11941. * struct itimerspec * old_value */
  11942. target_timer_t timerid = get_timer_id(arg1);
  11943. if (timerid < 0) {
  11944. ret = timerid;
  11945. } else if (arg3 == 0) {
  11946. ret = -TARGET_EINVAL;
  11947. } else {
  11948. timer_t htimer = g_posix_timers[timerid];
  11949. struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
  11950. if (target_to_host_itimerspec(&hspec_new, arg3)) {
  11951. return -TARGET_EFAULT;
  11952. }
  11953. ret = get_errno(
  11954. timer_settime(htimer, arg2, &hspec_new, &hspec_old));
  11955. if (arg4 && host_to_target_itimerspec(arg4, &hspec_old)) {
  11956. return -TARGET_EFAULT;
  11957. }
  11958. }
  11959. return ret;
  11960. }
  11961. #endif
  11962. #ifdef TARGET_NR_timer_settime64
  11963. case TARGET_NR_timer_settime64:
  11964. {
  11965. target_timer_t timerid = get_timer_id(arg1);
  11966. if (timerid < 0) {
  11967. ret = timerid;
  11968. } else if (arg3 == 0) {
  11969. ret = -TARGET_EINVAL;
  11970. } else {
  11971. timer_t htimer = g_posix_timers[timerid];
  11972. struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
  11973. if (target_to_host_itimerspec64(&hspec_new, arg3)) {
  11974. return -TARGET_EFAULT;
  11975. }
  11976. ret = get_errno(
  11977. timer_settime(htimer, arg2, &hspec_new, &hspec_old));
  11978. if (arg4 && host_to_target_itimerspec64(arg4, &hspec_old)) {
  11979. return -TARGET_EFAULT;
  11980. }
  11981. }
  11982. return ret;
  11983. }
  11984. #endif
  11985. #ifdef TARGET_NR_timer_gettime
  11986. case TARGET_NR_timer_gettime:
  11987. {
  11988. /* args: timer_t timerid, struct itimerspec *curr_value */
  11989. target_timer_t timerid = get_timer_id(arg1);
  11990. if (timerid < 0) {
  11991. ret = timerid;
  11992. } else if (!arg2) {
  11993. ret = -TARGET_EFAULT;
  11994. } else {
  11995. timer_t htimer = g_posix_timers[timerid];
  11996. struct itimerspec hspec;
  11997. ret = get_errno(timer_gettime(htimer, &hspec));
  11998. if (host_to_target_itimerspec(arg2, &hspec)) {
  11999. ret = -TARGET_EFAULT;
  12000. }
  12001. }
  12002. return ret;
  12003. }
  12004. #endif
  12005. #ifdef TARGET_NR_timer_gettime64
  12006. case TARGET_NR_timer_gettime64:
  12007. {
  12008. /* args: timer_t timerid, struct itimerspec64 *curr_value */
  12009. target_timer_t timerid = get_timer_id(arg1);
  12010. if (timerid < 0) {
  12011. ret = timerid;
  12012. } else if (!arg2) {
  12013. ret = -TARGET_EFAULT;
  12014. } else {
  12015. timer_t htimer = g_posix_timers[timerid];
  12016. struct itimerspec hspec;
  12017. ret = get_errno(timer_gettime(htimer, &hspec));
  12018. if (host_to_target_itimerspec64(arg2, &hspec)) {
  12019. ret = -TARGET_EFAULT;
  12020. }
  12021. }
  12022. return ret;
  12023. }
  12024. #endif
  12025. #ifdef TARGET_NR_timer_getoverrun
  12026. case TARGET_NR_timer_getoverrun:
  12027. {
  12028. /* args: timer_t timerid */
  12029. target_timer_t timerid = get_timer_id(arg1);
  12030. if (timerid < 0) {
  12031. ret = timerid;
  12032. } else {
  12033. timer_t htimer = g_posix_timers[timerid];
  12034. ret = get_errno(timer_getoverrun(htimer));
  12035. }
  12036. return ret;
  12037. }
  12038. #endif
  12039. #ifdef TARGET_NR_timer_delete
  12040. case TARGET_NR_timer_delete:
  12041. {
  12042. /* args: timer_t timerid */
  12043. target_timer_t timerid = get_timer_id(arg1);
  12044. if (timerid < 0) {
  12045. ret = timerid;
  12046. } else {
  12047. timer_t htimer = g_posix_timers[timerid];
  12048. ret = get_errno(timer_delete(htimer));
  12049. free_host_timer_slot(timerid);
  12050. }
  12051. return ret;
  12052. }
  12053. #endif
  12054. #if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
  12055. case TARGET_NR_timerfd_create:
  12056. return get_errno(timerfd_create(arg1,
  12057. target_to_host_bitmask(arg2, fcntl_flags_tbl)));
  12058. #endif
  12059. #if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
  12060. case TARGET_NR_timerfd_gettime:
  12061. {
  12062. struct itimerspec its_curr;
  12063. ret = get_errno(timerfd_gettime(arg1, &its_curr));
  12064. if (arg2 && host_to_target_itimerspec(arg2, &its_curr)) {
  12065. return -TARGET_EFAULT;
  12066. }
  12067. }
  12068. return ret;
  12069. #endif
  12070. #if defined(TARGET_NR_timerfd_gettime64) && defined(CONFIG_TIMERFD)
  12071. case TARGET_NR_timerfd_gettime64:
  12072. {
  12073. struct itimerspec its_curr;
  12074. ret = get_errno(timerfd_gettime(arg1, &its_curr));
  12075. if (arg2 && host_to_target_itimerspec64(arg2, &its_curr)) {
  12076. return -TARGET_EFAULT;
  12077. }
  12078. }
  12079. return ret;
  12080. #endif
  12081. #if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
  12082. case TARGET_NR_timerfd_settime:
  12083. {
  12084. struct itimerspec its_new, its_old, *p_new;
  12085. if (arg3) {
  12086. if (target_to_host_itimerspec(&its_new, arg3)) {
  12087. return -TARGET_EFAULT;
  12088. }
  12089. p_new = &its_new;
  12090. } else {
  12091. p_new = NULL;
  12092. }
  12093. ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
  12094. if (arg4 && host_to_target_itimerspec(arg4, &its_old)) {
  12095. return -TARGET_EFAULT;
  12096. }
  12097. }
  12098. return ret;
  12099. #endif
  12100. #if defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)
  12101. case TARGET_NR_timerfd_settime64:
  12102. {
  12103. struct itimerspec its_new, its_old, *p_new;
  12104. if (arg3) {
  12105. if (target_to_host_itimerspec64(&its_new, arg3)) {
  12106. return -TARGET_EFAULT;
  12107. }
  12108. p_new = &its_new;
  12109. } else {
  12110. p_new = NULL;
  12111. }
  12112. ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
  12113. if (arg4 && host_to_target_itimerspec64(arg4, &its_old)) {
  12114. return -TARGET_EFAULT;
  12115. }
  12116. }
  12117. return ret;
  12118. #endif
  12119. #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
  12120. case TARGET_NR_ioprio_get:
  12121. return get_errno(ioprio_get(arg1, arg2));
  12122. #endif
  12123. #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
  12124. case TARGET_NR_ioprio_set:
  12125. return get_errno(ioprio_set(arg1, arg2, arg3));
  12126. #endif
  12127. #if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
  12128. case TARGET_NR_setns:
  12129. return get_errno(setns(arg1, arg2));
  12130. #endif
  12131. #if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
  12132. case TARGET_NR_unshare:
  12133. return get_errno(unshare(arg1));
  12134. #endif
  12135. #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
  12136. case TARGET_NR_kcmp:
  12137. return get_errno(kcmp(arg1, arg2, arg3, arg4, arg5));
  12138. #endif
  12139. #ifdef TARGET_NR_swapcontext
  12140. case TARGET_NR_swapcontext:
  12141. /* PowerPC specific. */
  12142. return do_swapcontext(cpu_env, arg1, arg2, arg3);
  12143. #endif
  12144. #ifdef TARGET_NR_memfd_create
  12145. case TARGET_NR_memfd_create:
  12146. p = lock_user_string(arg1);
  12147. if (!p) {
  12148. return -TARGET_EFAULT;
  12149. }
  12150. ret = get_errno(memfd_create(p, arg2));
  12151. fd_trans_unregister(ret);
  12152. unlock_user(p, arg1, 0);
  12153. return ret;
  12154. #endif
  12155. #if defined TARGET_NR_membarrier && defined __NR_membarrier
  12156. case TARGET_NR_membarrier:
  12157. return get_errno(membarrier(arg1, arg2));
  12158. #endif
  12159. #if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range)
  12160. case TARGET_NR_copy_file_range:
  12161. {
  12162. loff_t inoff, outoff;
  12163. loff_t *pinoff = NULL, *poutoff = NULL;
  12164. if (arg2) {
  12165. if (get_user_u64(inoff, arg2)) {
  12166. return -TARGET_EFAULT;
  12167. }
  12168. pinoff = &inoff;
  12169. }
  12170. if (arg4) {
  12171. if (get_user_u64(outoff, arg4)) {
  12172. return -TARGET_EFAULT;
  12173. }
  12174. poutoff = &outoff;
  12175. }
  12176. /* Do not sign-extend the count parameter. */
  12177. ret = get_errno(safe_copy_file_range(arg1, pinoff, arg3, poutoff,
  12178. (abi_ulong)arg5, arg6));
  12179. if (!is_error(ret) && ret > 0) {
  12180. if (arg2) {
  12181. if (put_user_u64(inoff, arg2)) {
  12182. return -TARGET_EFAULT;
  12183. }
  12184. }
  12185. if (arg4) {
  12186. if (put_user_u64(outoff, arg4)) {
  12187. return -TARGET_EFAULT;
  12188. }
  12189. }
  12190. }
  12191. }
  12192. return ret;
  12193. #endif
  12194. #if defined(TARGET_NR_pivot_root)
  12195. case TARGET_NR_pivot_root:
  12196. {
  12197. void *p2;
  12198. p = lock_user_string(arg1); /* new_root */
  12199. p2 = lock_user_string(arg2); /* put_old */
  12200. if (!p || !p2) {
  12201. ret = -TARGET_EFAULT;
  12202. } else {
  12203. ret = get_errno(pivot_root(p, p2));
  12204. }
  12205. unlock_user(p2, arg2, 0);
  12206. unlock_user(p, arg1, 0);
  12207. }
  12208. return ret;
  12209. #endif
  12210. default:
  12211. qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num);
  12212. return -TARGET_ENOSYS;
  12213. }
  12214. return ret;
  12215. }
  12216. abi_long do_syscall(CPUArchState *cpu_env, int num, abi_long arg1,
  12217. abi_long arg2, abi_long arg3, abi_long arg4,
  12218. abi_long arg5, abi_long arg6, abi_long arg7,
  12219. abi_long arg8)
  12220. {
  12221. CPUState *cpu = env_cpu(cpu_env);
  12222. abi_long ret;
  12223. #ifdef DEBUG_ERESTARTSYS
  12224. /* Debug-only code for exercising the syscall-restart code paths
  12225. * in the per-architecture cpu main loops: restart every syscall
  12226. * the guest makes once before letting it through.
  12227. */
  12228. {
  12229. static bool flag;
  12230. flag = !flag;
  12231. if (flag) {
  12232. return -QEMU_ERESTARTSYS;
  12233. }
  12234. }
  12235. #endif
  12236. record_syscall_start(cpu, num, arg1,
  12237. arg2, arg3, arg4, arg5, arg6, arg7, arg8);
  12238. if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
  12239. print_syscall(cpu_env, num, arg1, arg2, arg3, arg4, arg5, arg6);
  12240. }
  12241. ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4,
  12242. arg5, arg6, arg7, arg8);
  12243. if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
  12244. print_syscall_ret(cpu_env, num, ret, arg1, arg2,
  12245. arg3, arg4, arg5, arg6);
  12246. }
  12247. record_syscall_return(cpu, num, ret);
  12248. return ret;
  12249. }