syscall.c 439 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912
  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 "qemu/plugin.h"
  26. #include "tcg/startup.h"
  27. #include "target_mman.h"
  28. #include "exec/page-protection.h"
  29. #include "exec/translation-block.h"
  30. #include <elf.h>
  31. #include <endian.h>
  32. #include <grp.h>
  33. #include <sys/ipc.h>
  34. #include <sys/msg.h>
  35. #include <sys/wait.h>
  36. #include <sys/mount.h>
  37. #include <sys/file.h>
  38. #include <sys/fsuid.h>
  39. #include <sys/personality.h>
  40. #include <sys/prctl.h>
  41. #include <sys/resource.h>
  42. #include <sys/swap.h>
  43. #include <linux/capability.h>
  44. #include <sched.h>
  45. #include <sys/timex.h>
  46. #include <sys/socket.h>
  47. #include <linux/sockios.h>
  48. #include <sys/un.h>
  49. #include <sys/uio.h>
  50. #include <poll.h>
  51. #include <sys/times.h>
  52. #include <sys/shm.h>
  53. #include <sys/sem.h>
  54. #include <sys/statfs.h>
  55. #include <utime.h>
  56. #include <sys/sysinfo.h>
  57. #include <sys/signalfd.h>
  58. #include <netinet/in.h>
  59. #include <netinet/ip.h>
  60. #include <netinet/tcp.h>
  61. #include <netinet/udp.h>
  62. #include <linux/wireless.h>
  63. #include <linux/icmp.h>
  64. #include <linux/icmpv6.h>
  65. #include <linux/if_tun.h>
  66. #include <linux/in6.h>
  67. #include <linux/errqueue.h>
  68. #include <linux/random.h>
  69. #ifdef CONFIG_TIMERFD
  70. #include <sys/timerfd.h>
  71. #endif
  72. #ifdef CONFIG_EVENTFD
  73. #include <sys/eventfd.h>
  74. #endif
  75. #ifdef CONFIG_EPOLL
  76. #include <sys/epoll.h>
  77. #endif
  78. #ifdef CONFIG_ATTR
  79. #include "qemu/xattr.h"
  80. #endif
  81. #ifdef CONFIG_SENDFILE
  82. #include <sys/sendfile.h>
  83. #endif
  84. #ifdef HAVE_SYS_KCOV_H
  85. #include <sys/kcov.h>
  86. #endif
  87. #define termios host_termios
  88. #define winsize host_winsize
  89. #define termio host_termio
  90. #define sgttyb host_sgttyb /* same as target */
  91. #define tchars host_tchars /* same as target */
  92. #define ltchars host_ltchars /* same as target */
  93. #include <linux/termios.h>
  94. #include <linux/unistd.h>
  95. #include <linux/cdrom.h>
  96. #include <linux/hdreg.h>
  97. #include <linux/soundcard.h>
  98. #include <linux/kd.h>
  99. #include <linux/mtio.h>
  100. #include <linux/fs.h>
  101. #include <linux/fd.h>
  102. #if defined(CONFIG_FIEMAP)
  103. #include <linux/fiemap.h>
  104. #endif
  105. #include <linux/fb.h>
  106. #if defined(CONFIG_USBFS)
  107. #include <linux/usbdevice_fs.h>
  108. #include <linux/usb/ch9.h>
  109. #endif
  110. #include <linux/vt.h>
  111. #include <linux/dm-ioctl.h>
  112. #include <linux/reboot.h>
  113. #include <linux/route.h>
  114. #include <linux/filter.h>
  115. #include <linux/blkpg.h>
  116. #include <netpacket/packet.h>
  117. #include <linux/netlink.h>
  118. #include <linux/if_alg.h>
  119. #include <linux/rtc.h>
  120. #include <sound/asound.h>
  121. #ifdef HAVE_BTRFS_H
  122. #include <linux/btrfs.h>
  123. #endif
  124. #ifdef HAVE_DRM_H
  125. #include <libdrm/drm.h>
  126. #include <libdrm/i915_drm.h>
  127. #endif
  128. #include "linux_loop.h"
  129. #include "uname.h"
  130. #include "qemu.h"
  131. #include "user-internals.h"
  132. #include "strace.h"
  133. #include "signal-common.h"
  134. #include "loader.h"
  135. #include "user-mmap.h"
  136. #include "user/page-protection.h"
  137. #include "user/safe-syscall.h"
  138. #include "qemu/guest-random.h"
  139. #include "qemu/selfmap.h"
  140. #include "user/syscall-trace.h"
  141. #include "special-errno.h"
  142. #include "qapi/error.h"
  143. #include "fd-trans.h"
  144. #include "user/cpu_loop.h"
  145. #ifndef CLONE_IO
  146. #define CLONE_IO 0x80000000 /* Clone io context */
  147. #endif
  148. /* We can't directly call the host clone syscall, because this will
  149. * badly confuse libc (breaking mutexes, for example). So we must
  150. * divide clone flags into:
  151. * * flag combinations that look like pthread_create()
  152. * * flag combinations that look like fork()
  153. * * flags we can implement within QEMU itself
  154. * * flags we can't support and will return an error for
  155. */
  156. /* For thread creation, all these flags must be present; for
  157. * fork, none must be present.
  158. */
  159. #define CLONE_THREAD_FLAGS \
  160. (CLONE_VM | CLONE_FS | CLONE_FILES | \
  161. CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM)
  162. /* These flags are ignored:
  163. * CLONE_DETACHED is now ignored by the kernel;
  164. * CLONE_IO is just an optimisation hint to the I/O scheduler
  165. */
  166. #define CLONE_IGNORED_FLAGS \
  167. (CLONE_DETACHED | CLONE_IO)
  168. #ifndef CLONE_PIDFD
  169. # define CLONE_PIDFD 0x00001000
  170. #endif
  171. /* Flags for fork which we can implement within QEMU itself */
  172. #define CLONE_OPTIONAL_FORK_FLAGS \
  173. (CLONE_SETTLS | CLONE_PARENT_SETTID | CLONE_PIDFD | \
  174. CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID)
  175. /* Flags for thread creation which we can implement within QEMU itself */
  176. #define CLONE_OPTIONAL_THREAD_FLAGS \
  177. (CLONE_SETTLS | CLONE_PARENT_SETTID | \
  178. CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | CLONE_PARENT)
  179. #define CLONE_INVALID_FORK_FLAGS \
  180. (~(CSIGNAL | CLONE_OPTIONAL_FORK_FLAGS | CLONE_IGNORED_FLAGS))
  181. #define CLONE_INVALID_THREAD_FLAGS \
  182. (~(CSIGNAL | CLONE_THREAD_FLAGS | CLONE_OPTIONAL_THREAD_FLAGS | \
  183. CLONE_IGNORED_FLAGS))
  184. /* CLONE_VFORK is special cased early in do_fork(). The other flag bits
  185. * have almost all been allocated. We cannot support any of
  186. * CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC,
  187. * CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, CLONE_PTRACE, CLONE_UNTRACED.
  188. * The checks against the invalid thread masks above will catch these.
  189. * (The one remaining unallocated bit is 0x1000 which used to be CLONE_PID.)
  190. */
  191. /* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
  192. * once. This exercises the codepaths for restart.
  193. */
  194. //#define DEBUG_ERESTARTSYS
  195. //#include <linux/msdos_fs.h>
  196. #define VFAT_IOCTL_READDIR_BOTH \
  197. _IOC(_IOC_READ, 'r', 1, (sizeof(struct linux_dirent) + 256) * 2)
  198. #define VFAT_IOCTL_READDIR_SHORT \
  199. _IOC(_IOC_READ, 'r', 2, (sizeof(struct linux_dirent) + 256) * 2)
  200. #undef _syscall0
  201. #undef _syscall1
  202. #undef _syscall2
  203. #undef _syscall3
  204. #undef _syscall4
  205. #undef _syscall5
  206. #undef _syscall6
  207. #define _syscall0(type,name) \
  208. static type name (void) \
  209. { \
  210. return syscall(__NR_##name); \
  211. }
  212. #define _syscall1(type,name,type1,arg1) \
  213. static type name (type1 arg1) \
  214. { \
  215. return syscall(__NR_##name, arg1); \
  216. }
  217. #define _syscall2(type,name,type1,arg1,type2,arg2) \
  218. static type name (type1 arg1,type2 arg2) \
  219. { \
  220. return syscall(__NR_##name, arg1, arg2); \
  221. }
  222. #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
  223. static type name (type1 arg1,type2 arg2,type3 arg3) \
  224. { \
  225. return syscall(__NR_##name, arg1, arg2, arg3); \
  226. }
  227. #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
  228. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
  229. { \
  230. return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
  231. }
  232. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  233. type5,arg5) \
  234. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
  235. { \
  236. return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
  237. }
  238. #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  239. type5,arg5,type6,arg6) \
  240. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
  241. type6 arg6) \
  242. { \
  243. return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
  244. }
  245. #define __NR_sys_uname __NR_uname
  246. #define __NR_sys_getcwd1 __NR_getcwd
  247. #define __NR_sys_getdents __NR_getdents
  248. #define __NR_sys_getdents64 __NR_getdents64
  249. #define __NR_sys_getpriority __NR_getpriority
  250. #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
  251. #define __NR_sys_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo
  252. #define __NR_sys_syslog __NR_syslog
  253. #if defined(__NR_futex)
  254. # define __NR_sys_futex __NR_futex
  255. #endif
  256. #if defined(__NR_futex_time64)
  257. # define __NR_sys_futex_time64 __NR_futex_time64
  258. #endif
  259. #define __NR_sys_statx __NR_statx
  260. #if defined(__alpha__) || defined(__x86_64__) || defined(__s390x__)
  261. #define __NR__llseek __NR_lseek
  262. #endif
  263. /* Newer kernel ports have llseek() instead of _llseek() */
  264. #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
  265. #define TARGET_NR__llseek TARGET_NR_llseek
  266. #endif
  267. /* some platforms need to mask more bits than just TARGET_O_NONBLOCK */
  268. #ifndef TARGET_O_NONBLOCK_MASK
  269. #define TARGET_O_NONBLOCK_MASK TARGET_O_NONBLOCK
  270. #endif
  271. #define __NR_sys_gettid __NR_gettid
  272. _syscall0(int, sys_gettid)
  273. /* For the 64-bit guest on 32-bit host case we must emulate
  274. * getdents using getdents64, because otherwise the host
  275. * might hand us back more dirent records than we can fit
  276. * into the guest buffer after structure format conversion.
  277. * Otherwise we emulate getdents with getdents if the host has it.
  278. */
  279. #if defined(__NR_getdents) && HOST_LONG_BITS >= TARGET_ABI_BITS
  280. #define EMULATE_GETDENTS_WITH_GETDENTS
  281. #endif
  282. #if defined(TARGET_NR_getdents) && defined(EMULATE_GETDENTS_WITH_GETDENTS)
  283. _syscall3(int, sys_getdents, unsigned int, fd, struct linux_dirent *, dirp, unsigned int, count);
  284. #endif
  285. #if (defined(TARGET_NR_getdents) && \
  286. !defined(EMULATE_GETDENTS_WITH_GETDENTS)) || \
  287. (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
  288. _syscall3(int, sys_getdents64, unsigned int, fd, struct linux_dirent64 *, dirp, unsigned int, count);
  289. #endif
  290. #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
  291. _syscall5(int, _llseek, unsigned int, fd, unsigned long, hi, unsigned long, lo,
  292. loff_t *, res, unsigned int, wh);
  293. #endif
  294. _syscall3(int, sys_rt_sigqueueinfo, pid_t, pid, int, sig, siginfo_t *, uinfo)
  295. _syscall4(int, sys_rt_tgsigqueueinfo, pid_t, pid, pid_t, tid, int, sig,
  296. siginfo_t *, uinfo)
  297. _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
  298. #ifdef __NR_exit_group
  299. _syscall1(int,exit_group,int,error_code)
  300. #endif
  301. #if defined(__NR_close_range) && defined(TARGET_NR_close_range)
  302. #define __NR_sys_close_range __NR_close_range
  303. _syscall3(int,sys_close_range,int,first,int,last,int,flags)
  304. #ifndef CLOSE_RANGE_CLOEXEC
  305. #define CLOSE_RANGE_CLOEXEC (1U << 2)
  306. #endif
  307. #endif
  308. #if defined(__NR_futex)
  309. _syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
  310. const struct timespec *,timeout,int *,uaddr2,int,val3)
  311. #endif
  312. #if defined(__NR_futex_time64)
  313. _syscall6(int,sys_futex_time64,int *,uaddr,int,op,int,val,
  314. const struct timespec *,timeout,int *,uaddr2,int,val3)
  315. #endif
  316. #if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
  317. _syscall2(int, pidfd_open, pid_t, pid, unsigned int, flags);
  318. #endif
  319. #if defined(__NR_pidfd_send_signal) && defined(TARGET_NR_pidfd_send_signal)
  320. _syscall4(int, pidfd_send_signal, int, pidfd, int, sig, siginfo_t *, info,
  321. unsigned int, flags);
  322. #endif
  323. #if defined(__NR_pidfd_getfd) && defined(TARGET_NR_pidfd_getfd)
  324. _syscall3(int, pidfd_getfd, int, pidfd, int, targetfd, unsigned int, flags);
  325. #endif
  326. #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
  327. _syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
  328. unsigned long *, user_mask_ptr);
  329. #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
  330. _syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
  331. unsigned long *, user_mask_ptr);
  332. /* sched_attr is not defined in glibc */
  333. struct sched_attr {
  334. uint32_t size;
  335. uint32_t sched_policy;
  336. uint64_t sched_flags;
  337. int32_t sched_nice;
  338. uint32_t sched_priority;
  339. uint64_t sched_runtime;
  340. uint64_t sched_deadline;
  341. uint64_t sched_period;
  342. uint32_t sched_util_min;
  343. uint32_t sched_util_max;
  344. };
  345. #define __NR_sys_sched_getattr __NR_sched_getattr
  346. _syscall4(int, sys_sched_getattr, pid_t, pid, struct sched_attr *, attr,
  347. unsigned int, size, unsigned int, flags);
  348. #define __NR_sys_sched_setattr __NR_sched_setattr
  349. _syscall3(int, sys_sched_setattr, pid_t, pid, struct sched_attr *, attr,
  350. unsigned int, flags);
  351. #define __NR_sys_sched_getscheduler __NR_sched_getscheduler
  352. _syscall1(int, sys_sched_getscheduler, pid_t, pid);
  353. #define __NR_sys_sched_setscheduler __NR_sched_setscheduler
  354. _syscall3(int, sys_sched_setscheduler, pid_t, pid, int, policy,
  355. const struct sched_param *, param);
  356. #define __NR_sys_sched_getparam __NR_sched_getparam
  357. _syscall2(int, sys_sched_getparam, pid_t, pid,
  358. struct sched_param *, param);
  359. #define __NR_sys_sched_setparam __NR_sched_setparam
  360. _syscall2(int, sys_sched_setparam, pid_t, pid,
  361. const struct sched_param *, param);
  362. #define __NR_sys_getcpu __NR_getcpu
  363. _syscall3(int, sys_getcpu, unsigned *, cpu, unsigned *, node, void *, tcache);
  364. _syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
  365. void *, arg);
  366. _syscall2(int, capget, struct __user_cap_header_struct *, header,
  367. struct __user_cap_data_struct *, data);
  368. _syscall2(int, capset, struct __user_cap_header_struct *, header,
  369. struct __user_cap_data_struct *, data);
  370. #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
  371. _syscall2(int, ioprio_get, int, which, int, who)
  372. #endif
  373. #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
  374. _syscall3(int, ioprio_set, int, which, int, who, int, ioprio)
  375. #endif
  376. #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
  377. _syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags)
  378. #endif
  379. #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
  380. _syscall5(int, kcmp, pid_t, pid1, pid_t, pid2, int, type,
  381. unsigned long, idx1, unsigned long, idx2)
  382. #endif
  383. /*
  384. * It is assumed that struct statx is architecture independent.
  385. */
  386. #if defined(TARGET_NR_statx) && defined(__NR_statx)
  387. _syscall5(int, sys_statx, int, dirfd, const char *, pathname, int, flags,
  388. unsigned int, mask, struct target_statx *, statxbuf)
  389. #endif
  390. #if defined(TARGET_NR_membarrier) && defined(__NR_membarrier)
  391. _syscall2(int, membarrier, int, cmd, int, flags)
  392. #endif
  393. static const bitmask_transtbl fcntl_flags_tbl[] = {
  394. { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
  395. { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
  396. { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
  397. { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
  398. { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
  399. { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
  400. { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
  401. { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
  402. { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
  403. { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
  404. { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
  405. { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
  406. { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
  407. #if defined(O_DIRECT)
  408. { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
  409. #endif
  410. #if defined(O_NOATIME)
  411. { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
  412. #endif
  413. #if defined(O_CLOEXEC)
  414. { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
  415. #endif
  416. #if defined(O_PATH)
  417. { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
  418. #endif
  419. #if defined(O_TMPFILE)
  420. { TARGET_O_TMPFILE, TARGET_O_TMPFILE, O_TMPFILE, O_TMPFILE },
  421. #endif
  422. /* Don't terminate the list prematurely on 64-bit host+guest. */
  423. #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
  424. { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
  425. #endif
  426. };
  427. _syscall2(int, sys_getcwd1, char *, buf, size_t, size)
  428. #if defined(TARGET_NR_utimensat) || defined(TARGET_NR_utimensat_time64)
  429. #if defined(__NR_utimensat)
  430. #define __NR_sys_utimensat __NR_utimensat
  431. _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
  432. const struct timespec *,tsp,int,flags)
  433. #else
  434. static int sys_utimensat(int dirfd, const char *pathname,
  435. const struct timespec times[2], int flags)
  436. {
  437. errno = ENOSYS;
  438. return -1;
  439. }
  440. #endif
  441. #endif /* TARGET_NR_utimensat */
  442. #ifdef TARGET_NR_renameat2
  443. #if defined(__NR_renameat2)
  444. #define __NR_sys_renameat2 __NR_renameat2
  445. _syscall5(int, sys_renameat2, int, oldfd, const char *, old, int, newfd,
  446. const char *, new, unsigned int, flags)
  447. #else
  448. static int sys_renameat2(int oldfd, const char *old,
  449. int newfd, const char *new, int flags)
  450. {
  451. if (flags == 0) {
  452. return renameat(oldfd, old, newfd, new);
  453. }
  454. errno = ENOSYS;
  455. return -1;
  456. }
  457. #endif
  458. #endif /* TARGET_NR_renameat2 */
  459. #ifdef CONFIG_INOTIFY
  460. #include <sys/inotify.h>
  461. #else
  462. /* Userspace can usually survive runtime without inotify */
  463. #undef TARGET_NR_inotify_init
  464. #undef TARGET_NR_inotify_init1
  465. #undef TARGET_NR_inotify_add_watch
  466. #undef TARGET_NR_inotify_rm_watch
  467. #endif /* CONFIG_INOTIFY */
  468. #if defined(TARGET_NR_prlimit64)
  469. #ifndef __NR_prlimit64
  470. # define __NR_prlimit64 -1
  471. #endif
  472. #define __NR_sys_prlimit64 __NR_prlimit64
  473. /* The glibc rlimit structure may not be that used by the underlying syscall */
  474. struct host_rlimit64 {
  475. uint64_t rlim_cur;
  476. uint64_t rlim_max;
  477. };
  478. _syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
  479. const struct host_rlimit64 *, new_limit,
  480. struct host_rlimit64 *, old_limit)
  481. #endif
  482. #if defined(TARGET_NR_timer_create)
  483. /* Maximum of 32 active POSIX timers allowed at any one time. */
  484. #define GUEST_TIMER_MAX 32
  485. static timer_t g_posix_timers[GUEST_TIMER_MAX];
  486. static int g_posix_timer_allocated[GUEST_TIMER_MAX];
  487. static inline int next_free_host_timer(void)
  488. {
  489. int k;
  490. for (k = 0; k < ARRAY_SIZE(g_posix_timer_allocated); k++) {
  491. if (qatomic_xchg(g_posix_timer_allocated + k, 1) == 0) {
  492. return k;
  493. }
  494. }
  495. return -1;
  496. }
  497. static inline void free_host_timer_slot(int id)
  498. {
  499. qatomic_store_release(g_posix_timer_allocated + id, 0);
  500. }
  501. #endif
  502. static inline int host_to_target_errno(int host_errno)
  503. {
  504. switch (host_errno) {
  505. #define E(X) case X: return TARGET_##X;
  506. #include "errnos.c.inc"
  507. #undef E
  508. default:
  509. return host_errno;
  510. }
  511. }
  512. static inline int target_to_host_errno(int target_errno)
  513. {
  514. switch (target_errno) {
  515. #define E(X) case TARGET_##X: return X;
  516. #include "errnos.c.inc"
  517. #undef E
  518. default:
  519. return target_errno;
  520. }
  521. }
  522. abi_long get_errno(abi_long ret)
  523. {
  524. if (ret == -1)
  525. return -host_to_target_errno(errno);
  526. else
  527. return ret;
  528. }
  529. const char *target_strerror(int err)
  530. {
  531. if (err == QEMU_ERESTARTSYS) {
  532. return "To be restarted";
  533. }
  534. if (err == QEMU_ESIGRETURN) {
  535. return "Successful exit from sigreturn";
  536. }
  537. return strerror(target_to_host_errno(err));
  538. }
  539. static int check_zeroed_user(abi_long addr, size_t ksize, size_t usize)
  540. {
  541. int i;
  542. uint8_t b;
  543. if (usize <= ksize) {
  544. return 1;
  545. }
  546. for (i = ksize; i < usize; i++) {
  547. if (get_user_u8(b, addr + i)) {
  548. return -TARGET_EFAULT;
  549. }
  550. if (b != 0) {
  551. return 0;
  552. }
  553. }
  554. return 1;
  555. }
  556. /*
  557. * Copies a target struct to a host struct, in a way that guarantees
  558. * backwards-compatibility for struct syscall arguments.
  559. *
  560. * Similar to kernels uaccess.h:copy_struct_from_user()
  561. */
  562. int copy_struct_from_user(void *dst, size_t ksize, abi_ptr src, size_t usize)
  563. {
  564. size_t size = MIN(ksize, usize);
  565. size_t rest = MAX(ksize, usize) - size;
  566. /* Deal with trailing bytes. */
  567. if (usize < ksize) {
  568. memset(dst + size, 0, rest);
  569. } else if (usize > ksize) {
  570. int ret = check_zeroed_user(src, ksize, usize);
  571. if (ret <= 0) {
  572. return ret ?: -TARGET_E2BIG;
  573. }
  574. }
  575. /* Copy the interoperable parts of the struct. */
  576. if (copy_from_user(dst, src, size)) {
  577. return -TARGET_EFAULT;
  578. }
  579. return 0;
  580. }
  581. #define safe_syscall0(type, name) \
  582. static type safe_##name(void) \
  583. { \
  584. return safe_syscall(__NR_##name); \
  585. }
  586. #define safe_syscall1(type, name, type1, arg1) \
  587. static type safe_##name(type1 arg1) \
  588. { \
  589. return safe_syscall(__NR_##name, arg1); \
  590. }
  591. #define safe_syscall2(type, name, type1, arg1, type2, arg2) \
  592. static type safe_##name(type1 arg1, type2 arg2) \
  593. { \
  594. return safe_syscall(__NR_##name, arg1, arg2); \
  595. }
  596. #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
  597. static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
  598. { \
  599. return safe_syscall(__NR_##name, arg1, arg2, arg3); \
  600. }
  601. #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
  602. type4, arg4) \
  603. static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
  604. { \
  605. return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
  606. }
  607. #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
  608. type4, arg4, type5, arg5) \
  609. static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  610. type5 arg5) \
  611. { \
  612. return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
  613. }
  614. #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
  615. type4, arg4, type5, arg5, type6, arg6) \
  616. static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  617. type5 arg5, type6 arg6) \
  618. { \
  619. return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
  620. }
  621. safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count)
  622. safe_syscall3(ssize_t, write, int, fd, const void *, buff, size_t, count)
  623. safe_syscall4(int, openat, int, dirfd, const char *, pathname, \
  624. int, flags, mode_t, mode)
  625. safe_syscall4(int, openat2, int, dirfd, const char *, pathname, \
  626. const struct open_how_ver0 *, how, size_t, size)
  627. #if defined(TARGET_NR_wait4) || defined(TARGET_NR_waitpid)
  628. safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \
  629. struct rusage *, rusage)
  630. #endif
  631. safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \
  632. int, options, struct rusage *, rusage)
  633. safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp)
  634. safe_syscall5(int, execveat, int, dirfd, const char *, filename,
  635. char **, argv, char **, envp, int, flags)
  636. #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
  637. defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
  638. safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \
  639. fd_set *, exceptfds, struct timespec *, timeout, void *, sig)
  640. #endif
  641. #if defined(TARGET_NR_ppoll) || defined(TARGET_NR_ppoll_time64)
  642. safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds,
  643. struct timespec *, tsp, const sigset_t *, sigmask,
  644. size_t, sigsetsize)
  645. #endif
  646. safe_syscall6(int, epoll_pwait, int, epfd, struct epoll_event *, events,
  647. int, maxevents, int, timeout, const sigset_t *, sigmask,
  648. size_t, sigsetsize)
  649. #if defined(__NR_futex)
  650. safe_syscall6(int,futex,int *,uaddr,int,op,int,val, \
  651. const struct timespec *,timeout,int *,uaddr2,int,val3)
  652. #endif
  653. #if defined(__NR_futex_time64)
  654. safe_syscall6(int,futex_time64,int *,uaddr,int,op,int,val, \
  655. const struct timespec *,timeout,int *,uaddr2,int,val3)
  656. #endif
  657. safe_syscall2(int, rt_sigsuspend, sigset_t *, newset, size_t, sigsetsize)
  658. safe_syscall2(int, kill, pid_t, pid, int, sig)
  659. safe_syscall2(int, tkill, int, tid, int, sig)
  660. safe_syscall3(int, tgkill, int, tgid, int, pid, int, sig)
  661. safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt)
  662. safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt)
  663. safe_syscall5(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt,
  664. unsigned long, pos_l, unsigned long, pos_h)
  665. safe_syscall5(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, iovcnt,
  666. unsigned long, pos_l, unsigned long, pos_h)
  667. safe_syscall3(int, connect, int, fd, const struct sockaddr *, addr,
  668. socklen_t, addrlen)
  669. safe_syscall6(ssize_t, sendto, int, fd, const void *, buf, size_t, len,
  670. int, flags, const struct sockaddr *, addr, socklen_t, addrlen)
  671. safe_syscall6(ssize_t, recvfrom, int, fd, void *, buf, size_t, len,
  672. int, flags, struct sockaddr *, addr, socklen_t *, addrlen)
  673. safe_syscall3(ssize_t, sendmsg, int, fd, const struct msghdr *, msg, int, flags)
  674. safe_syscall3(ssize_t, recvmsg, int, fd, struct msghdr *, msg, int, flags)
  675. safe_syscall2(int, flock, int, fd, int, operation)
  676. #if defined(TARGET_NR_rt_sigtimedwait) || defined(TARGET_NR_rt_sigtimedwait_time64)
  677. safe_syscall4(int, rt_sigtimedwait, const sigset_t *, these, siginfo_t *, uinfo,
  678. const struct timespec *, uts, size_t, sigsetsize)
  679. #endif
  680. safe_syscall4(int, accept4, int, fd, struct sockaddr *, addr, socklen_t *, len,
  681. int, flags)
  682. #if defined(TARGET_NR_nanosleep)
  683. safe_syscall2(int, nanosleep, const struct timespec *, req,
  684. struct timespec *, rem)
  685. #endif
  686. #if defined(TARGET_NR_clock_nanosleep) || \
  687. defined(TARGET_NR_clock_nanosleep_time64)
  688. safe_syscall4(int, clock_nanosleep, const clockid_t, clock, int, flags,
  689. const struct timespec *, req, struct timespec *, rem)
  690. #endif
  691. #ifdef __NR_ipc
  692. #ifdef __s390x__
  693. safe_syscall5(int, ipc, int, call, long, first, long, second, long, third,
  694. void *, ptr)
  695. #else
  696. safe_syscall6(int, ipc, int, call, long, first, long, second, long, third,
  697. void *, ptr, long, fifth)
  698. #endif
  699. #endif
  700. #ifdef __NR_msgsnd
  701. safe_syscall4(int, msgsnd, int, msgid, const void *, msgp, size_t, sz,
  702. int, flags)
  703. #endif
  704. #ifdef __NR_msgrcv
  705. safe_syscall5(int, msgrcv, int, msgid, void *, msgp, size_t, sz,
  706. long, msgtype, int, flags)
  707. #endif
  708. #ifdef __NR_semtimedop
  709. safe_syscall4(int, semtimedop, int, semid, struct sembuf *, tsops,
  710. unsigned, nsops, const struct timespec *, timeout)
  711. #endif
  712. #if defined(TARGET_NR_mq_timedsend) || \
  713. defined(TARGET_NR_mq_timedsend_time64)
  714. safe_syscall5(int, mq_timedsend, int, mqdes, const char *, msg_ptr,
  715. size_t, len, unsigned, prio, const struct timespec *, timeout)
  716. #endif
  717. #if defined(TARGET_NR_mq_timedreceive) || \
  718. defined(TARGET_NR_mq_timedreceive_time64)
  719. safe_syscall5(int, mq_timedreceive, int, mqdes, char *, msg_ptr,
  720. size_t, len, unsigned *, prio, const struct timespec *, timeout)
  721. #endif
  722. #if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range)
  723. safe_syscall6(ssize_t, copy_file_range, int, infd, loff_t *, pinoff,
  724. int, outfd, loff_t *, poutoff, size_t, length,
  725. unsigned int, flags)
  726. #endif
  727. /* We do ioctl like this rather than via safe_syscall3 to preserve the
  728. * "third argument might be integer or pointer or not present" behaviour of
  729. * the libc function.
  730. */
  731. #define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
  732. /* Similarly for fcntl. Since we always build with LFS enabled,
  733. * we should be using the 64-bit structures automatically.
  734. */
  735. #ifdef __NR_fcntl64
  736. #define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
  737. #else
  738. #define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
  739. #endif
  740. static inline int host_to_target_sock_type(int host_type)
  741. {
  742. int target_type;
  743. switch (host_type & 0xf /* SOCK_TYPE_MASK */) {
  744. case SOCK_DGRAM:
  745. target_type = TARGET_SOCK_DGRAM;
  746. break;
  747. case SOCK_STREAM:
  748. target_type = TARGET_SOCK_STREAM;
  749. break;
  750. default:
  751. target_type = host_type & 0xf /* SOCK_TYPE_MASK */;
  752. break;
  753. }
  754. #if defined(SOCK_CLOEXEC)
  755. if (host_type & SOCK_CLOEXEC) {
  756. target_type |= TARGET_SOCK_CLOEXEC;
  757. }
  758. #endif
  759. #if defined(SOCK_NONBLOCK)
  760. if (host_type & SOCK_NONBLOCK) {
  761. target_type |= TARGET_SOCK_NONBLOCK;
  762. }
  763. #endif
  764. return target_type;
  765. }
  766. static abi_ulong target_brk, initial_target_brk;
  767. void target_set_brk(abi_ulong new_brk)
  768. {
  769. target_brk = TARGET_PAGE_ALIGN(new_brk);
  770. initial_target_brk = target_brk;
  771. }
  772. /* do_brk() must return target values and target errnos. */
  773. abi_long do_brk(abi_ulong brk_val)
  774. {
  775. abi_long mapped_addr;
  776. abi_ulong new_brk;
  777. abi_ulong old_brk;
  778. /* brk pointers are always untagged */
  779. /* do not allow to shrink below initial brk value */
  780. if (brk_val < initial_target_brk) {
  781. return target_brk;
  782. }
  783. new_brk = TARGET_PAGE_ALIGN(brk_val);
  784. old_brk = TARGET_PAGE_ALIGN(target_brk);
  785. /* new and old target_brk might be on the same page */
  786. if (new_brk == old_brk) {
  787. target_brk = brk_val;
  788. return target_brk;
  789. }
  790. /* Release heap if necessary */
  791. if (new_brk < old_brk) {
  792. target_munmap(new_brk, old_brk - new_brk);
  793. target_brk = brk_val;
  794. return target_brk;
  795. }
  796. mapped_addr = target_mmap(old_brk, new_brk - old_brk,
  797. PROT_READ | PROT_WRITE,
  798. MAP_FIXED_NOREPLACE | MAP_ANON | MAP_PRIVATE,
  799. -1, 0);
  800. if (mapped_addr == old_brk) {
  801. target_brk = brk_val;
  802. return target_brk;
  803. }
  804. #if defined(TARGET_ALPHA)
  805. /* We (partially) emulate OSF/1 on Alpha, which requires we
  806. return a proper errno, not an unchanged brk value. */
  807. return -TARGET_ENOMEM;
  808. #endif
  809. /* For everything else, return the previous break. */
  810. return target_brk;
  811. }
  812. #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
  813. defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
  814. static inline abi_long copy_from_user_fdset(fd_set *fds,
  815. abi_ulong target_fds_addr,
  816. int n)
  817. {
  818. int i, nw, j, k;
  819. abi_ulong b, *target_fds;
  820. nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
  821. if (!(target_fds = lock_user(VERIFY_READ,
  822. target_fds_addr,
  823. sizeof(abi_ulong) * nw,
  824. 1)))
  825. return -TARGET_EFAULT;
  826. FD_ZERO(fds);
  827. k = 0;
  828. for (i = 0; i < nw; i++) {
  829. /* grab the abi_ulong */
  830. __get_user(b, &target_fds[i]);
  831. for (j = 0; j < TARGET_ABI_BITS; j++) {
  832. /* check the bit inside the abi_ulong */
  833. if ((b >> j) & 1)
  834. FD_SET(k, fds);
  835. k++;
  836. }
  837. }
  838. unlock_user(target_fds, target_fds_addr, 0);
  839. return 0;
  840. }
  841. static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
  842. abi_ulong target_fds_addr,
  843. int n)
  844. {
  845. if (target_fds_addr) {
  846. if (copy_from_user_fdset(fds, target_fds_addr, n))
  847. return -TARGET_EFAULT;
  848. *fds_ptr = fds;
  849. } else {
  850. *fds_ptr = NULL;
  851. }
  852. return 0;
  853. }
  854. static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
  855. const fd_set *fds,
  856. int n)
  857. {
  858. int i, nw, j, k;
  859. abi_long v;
  860. abi_ulong *target_fds;
  861. nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
  862. if (!(target_fds = lock_user(VERIFY_WRITE,
  863. target_fds_addr,
  864. sizeof(abi_ulong) * nw,
  865. 0)))
  866. return -TARGET_EFAULT;
  867. k = 0;
  868. for (i = 0; i < nw; i++) {
  869. v = 0;
  870. for (j = 0; j < TARGET_ABI_BITS; j++) {
  871. v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
  872. k++;
  873. }
  874. __put_user(v, &target_fds[i]);
  875. }
  876. unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
  877. return 0;
  878. }
  879. #endif
  880. #if defined(__alpha__)
  881. #define HOST_HZ 1024
  882. #else
  883. #define HOST_HZ 100
  884. #endif
  885. static inline abi_long host_to_target_clock_t(long ticks)
  886. {
  887. #if HOST_HZ == TARGET_HZ
  888. return ticks;
  889. #else
  890. return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
  891. #endif
  892. }
  893. static inline abi_long host_to_target_rusage(abi_ulong target_addr,
  894. const struct rusage *rusage)
  895. {
  896. struct target_rusage *target_rusage;
  897. if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
  898. return -TARGET_EFAULT;
  899. target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
  900. target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
  901. target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
  902. target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
  903. target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
  904. target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
  905. target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
  906. target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
  907. target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
  908. target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
  909. target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
  910. target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
  911. target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
  912. target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
  913. target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
  914. target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
  915. target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
  916. target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
  917. unlock_user_struct(target_rusage, target_addr, 1);
  918. return 0;
  919. }
  920. #ifdef TARGET_NR_setrlimit
  921. static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
  922. {
  923. abi_ulong target_rlim_swap;
  924. rlim_t result;
  925. target_rlim_swap = tswapal(target_rlim);
  926. if (target_rlim_swap == TARGET_RLIM_INFINITY)
  927. return RLIM_INFINITY;
  928. result = target_rlim_swap;
  929. if (target_rlim_swap != (rlim_t)result)
  930. return RLIM_INFINITY;
  931. return result;
  932. }
  933. #endif
  934. #if defined(TARGET_NR_getrlimit) || defined(TARGET_NR_ugetrlimit)
  935. static inline abi_ulong host_to_target_rlim(rlim_t rlim)
  936. {
  937. abi_ulong target_rlim_swap;
  938. abi_ulong result;
  939. if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
  940. target_rlim_swap = TARGET_RLIM_INFINITY;
  941. else
  942. target_rlim_swap = rlim;
  943. result = tswapal(target_rlim_swap);
  944. return result;
  945. }
  946. #endif
  947. static inline int target_to_host_resource(int code)
  948. {
  949. switch (code) {
  950. case TARGET_RLIMIT_AS:
  951. return RLIMIT_AS;
  952. case TARGET_RLIMIT_CORE:
  953. return RLIMIT_CORE;
  954. case TARGET_RLIMIT_CPU:
  955. return RLIMIT_CPU;
  956. case TARGET_RLIMIT_DATA:
  957. return RLIMIT_DATA;
  958. case TARGET_RLIMIT_FSIZE:
  959. return RLIMIT_FSIZE;
  960. case TARGET_RLIMIT_LOCKS:
  961. return RLIMIT_LOCKS;
  962. case TARGET_RLIMIT_MEMLOCK:
  963. return RLIMIT_MEMLOCK;
  964. case TARGET_RLIMIT_MSGQUEUE:
  965. return RLIMIT_MSGQUEUE;
  966. case TARGET_RLIMIT_NICE:
  967. return RLIMIT_NICE;
  968. case TARGET_RLIMIT_NOFILE:
  969. return RLIMIT_NOFILE;
  970. case TARGET_RLIMIT_NPROC:
  971. return RLIMIT_NPROC;
  972. case TARGET_RLIMIT_RSS:
  973. return RLIMIT_RSS;
  974. case TARGET_RLIMIT_RTPRIO:
  975. return RLIMIT_RTPRIO;
  976. #ifdef RLIMIT_RTTIME
  977. case TARGET_RLIMIT_RTTIME:
  978. return RLIMIT_RTTIME;
  979. #endif
  980. case TARGET_RLIMIT_SIGPENDING:
  981. return RLIMIT_SIGPENDING;
  982. case TARGET_RLIMIT_STACK:
  983. return RLIMIT_STACK;
  984. default:
  985. return code;
  986. }
  987. }
  988. static inline abi_long copy_from_user_timeval(struct timeval *tv,
  989. abi_ulong target_tv_addr)
  990. {
  991. struct target_timeval *target_tv;
  992. if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) {
  993. return -TARGET_EFAULT;
  994. }
  995. __get_user(tv->tv_sec, &target_tv->tv_sec);
  996. __get_user(tv->tv_usec, &target_tv->tv_usec);
  997. unlock_user_struct(target_tv, target_tv_addr, 0);
  998. return 0;
  999. }
  1000. static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
  1001. const struct timeval *tv)
  1002. {
  1003. struct target_timeval *target_tv;
  1004. if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
  1005. return -TARGET_EFAULT;
  1006. }
  1007. __put_user(tv->tv_sec, &target_tv->tv_sec);
  1008. __put_user(tv->tv_usec, &target_tv->tv_usec);
  1009. unlock_user_struct(target_tv, target_tv_addr, 1);
  1010. return 0;
  1011. }
  1012. #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
  1013. static inline abi_long copy_from_user_timeval64(struct timeval *tv,
  1014. abi_ulong target_tv_addr)
  1015. {
  1016. struct target__kernel_sock_timeval *target_tv;
  1017. if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) {
  1018. return -TARGET_EFAULT;
  1019. }
  1020. __get_user(tv->tv_sec, &target_tv->tv_sec);
  1021. __get_user(tv->tv_usec, &target_tv->tv_usec);
  1022. unlock_user_struct(target_tv, target_tv_addr, 0);
  1023. return 0;
  1024. }
  1025. #endif
  1026. static inline abi_long copy_to_user_timeval64(abi_ulong target_tv_addr,
  1027. const struct timeval *tv)
  1028. {
  1029. struct target__kernel_sock_timeval *target_tv;
  1030. if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
  1031. return -TARGET_EFAULT;
  1032. }
  1033. __put_user(tv->tv_sec, &target_tv->tv_sec);
  1034. __put_user(tv->tv_usec, &target_tv->tv_usec);
  1035. unlock_user_struct(target_tv, target_tv_addr, 1);
  1036. return 0;
  1037. }
  1038. #if defined(TARGET_NR_futex) || \
  1039. defined(TARGET_NR_rt_sigtimedwait) || \
  1040. defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6) || \
  1041. defined(TARGET_NR_nanosleep) || defined(TARGET_NR_clock_settime) || \
  1042. defined(TARGET_NR_utimensat) || defined(TARGET_NR_mq_timedsend) || \
  1043. defined(TARGET_NR_mq_timedreceive) || defined(TARGET_NR_ipc) || \
  1044. defined(TARGET_NR_semop) || defined(TARGET_NR_semtimedop) || \
  1045. defined(TARGET_NR_timer_settime) || \
  1046. (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
  1047. static inline abi_long target_to_host_timespec(struct timespec *host_ts,
  1048. abi_ulong target_addr)
  1049. {
  1050. struct target_timespec *target_ts;
  1051. if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
  1052. return -TARGET_EFAULT;
  1053. }
  1054. __get_user(host_ts->tv_sec, &target_ts->tv_sec);
  1055. __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1056. unlock_user_struct(target_ts, target_addr, 0);
  1057. return 0;
  1058. }
  1059. #endif
  1060. #if defined(TARGET_NR_clock_settime64) || defined(TARGET_NR_futex_time64) || \
  1061. defined(TARGET_NR_timer_settime64) || \
  1062. defined(TARGET_NR_mq_timedsend_time64) || \
  1063. defined(TARGET_NR_mq_timedreceive_time64) || \
  1064. (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)) || \
  1065. defined(TARGET_NR_clock_nanosleep_time64) || \
  1066. defined(TARGET_NR_rt_sigtimedwait_time64) || \
  1067. defined(TARGET_NR_utimensat) || \
  1068. defined(TARGET_NR_utimensat_time64) || \
  1069. defined(TARGET_NR_semtimedop_time64) || \
  1070. defined(TARGET_NR_pselect6_time64) || defined(TARGET_NR_ppoll_time64)
  1071. static inline abi_long target_to_host_timespec64(struct timespec *host_ts,
  1072. abi_ulong target_addr)
  1073. {
  1074. struct target__kernel_timespec *target_ts;
  1075. if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
  1076. return -TARGET_EFAULT;
  1077. }
  1078. __get_user(host_ts->tv_sec, &target_ts->tv_sec);
  1079. __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1080. /* in 32bit mode, this drops the padding */
  1081. host_ts->tv_nsec = (long)(abi_long)host_ts->tv_nsec;
  1082. unlock_user_struct(target_ts, target_addr, 0);
  1083. return 0;
  1084. }
  1085. #endif
  1086. static inline abi_long host_to_target_timespec(abi_ulong target_addr,
  1087. struct timespec *host_ts)
  1088. {
  1089. struct target_timespec *target_ts;
  1090. if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
  1091. return -TARGET_EFAULT;
  1092. }
  1093. __put_user(host_ts->tv_sec, &target_ts->tv_sec);
  1094. __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1095. unlock_user_struct(target_ts, target_addr, 1);
  1096. return 0;
  1097. }
  1098. static inline abi_long host_to_target_timespec64(abi_ulong target_addr,
  1099. struct timespec *host_ts)
  1100. {
  1101. struct target__kernel_timespec *target_ts;
  1102. if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
  1103. return -TARGET_EFAULT;
  1104. }
  1105. __put_user(host_ts->tv_sec, &target_ts->tv_sec);
  1106. __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1107. unlock_user_struct(target_ts, target_addr, 1);
  1108. return 0;
  1109. }
  1110. #if defined(TARGET_NR_gettimeofday)
  1111. static inline abi_long copy_to_user_timezone(abi_ulong target_tz_addr,
  1112. struct timezone *tz)
  1113. {
  1114. struct target_timezone *target_tz;
  1115. if (!lock_user_struct(VERIFY_WRITE, target_tz, target_tz_addr, 1)) {
  1116. return -TARGET_EFAULT;
  1117. }
  1118. __put_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
  1119. __put_user(tz->tz_dsttime, &target_tz->tz_dsttime);
  1120. unlock_user_struct(target_tz, target_tz_addr, 1);
  1121. return 0;
  1122. }
  1123. #endif
  1124. #if defined(TARGET_NR_settimeofday)
  1125. static inline abi_long copy_from_user_timezone(struct timezone *tz,
  1126. abi_ulong target_tz_addr)
  1127. {
  1128. struct target_timezone *target_tz;
  1129. if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) {
  1130. return -TARGET_EFAULT;
  1131. }
  1132. __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
  1133. __get_user(tz->tz_dsttime, &target_tz->tz_dsttime);
  1134. unlock_user_struct(target_tz, target_tz_addr, 0);
  1135. return 0;
  1136. }
  1137. #endif
  1138. #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
  1139. #include <mqueue.h>
  1140. static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
  1141. abi_ulong target_mq_attr_addr)
  1142. {
  1143. struct target_mq_attr *target_mq_attr;
  1144. if (!lock_user_struct(VERIFY_READ, target_mq_attr,
  1145. target_mq_attr_addr, 1))
  1146. return -TARGET_EFAULT;
  1147. __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
  1148. __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
  1149. __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
  1150. __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
  1151. unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
  1152. return 0;
  1153. }
  1154. static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
  1155. const struct mq_attr *attr)
  1156. {
  1157. struct target_mq_attr *target_mq_attr;
  1158. if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
  1159. target_mq_attr_addr, 0))
  1160. return -TARGET_EFAULT;
  1161. __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
  1162. __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
  1163. __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
  1164. __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
  1165. unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
  1166. return 0;
  1167. }
  1168. #endif
  1169. #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
  1170. /* do_select() must return target values and target errnos. */
  1171. static abi_long do_select(int n,
  1172. abi_ulong rfd_addr, abi_ulong wfd_addr,
  1173. abi_ulong efd_addr, abi_ulong target_tv_addr)
  1174. {
  1175. fd_set rfds, wfds, efds;
  1176. fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
  1177. struct timeval tv;
  1178. struct timespec ts, *ts_ptr;
  1179. abi_long ret;
  1180. ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
  1181. if (ret) {
  1182. return ret;
  1183. }
  1184. ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
  1185. if (ret) {
  1186. return ret;
  1187. }
  1188. ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
  1189. if (ret) {
  1190. return ret;
  1191. }
  1192. if (target_tv_addr) {
  1193. if (copy_from_user_timeval(&tv, target_tv_addr))
  1194. return -TARGET_EFAULT;
  1195. ts.tv_sec = tv.tv_sec;
  1196. ts.tv_nsec = tv.tv_usec * 1000;
  1197. ts_ptr = &ts;
  1198. } else {
  1199. ts_ptr = NULL;
  1200. }
  1201. ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
  1202. ts_ptr, NULL));
  1203. if (!is_error(ret)) {
  1204. if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
  1205. return -TARGET_EFAULT;
  1206. if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
  1207. return -TARGET_EFAULT;
  1208. if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
  1209. return -TARGET_EFAULT;
  1210. if (target_tv_addr) {
  1211. tv.tv_sec = ts.tv_sec;
  1212. tv.tv_usec = ts.tv_nsec / 1000;
  1213. if (copy_to_user_timeval(target_tv_addr, &tv)) {
  1214. return -TARGET_EFAULT;
  1215. }
  1216. }
  1217. }
  1218. return ret;
  1219. }
  1220. #if defined(TARGET_WANT_OLD_SYS_SELECT)
  1221. static abi_long do_old_select(abi_ulong arg1)
  1222. {
  1223. struct target_sel_arg_struct *sel;
  1224. abi_ulong inp, outp, exp, tvp;
  1225. long nsel;
  1226. if (!lock_user_struct(VERIFY_READ, sel, arg1, 1)) {
  1227. return -TARGET_EFAULT;
  1228. }
  1229. nsel = tswapal(sel->n);
  1230. inp = tswapal(sel->inp);
  1231. outp = tswapal(sel->outp);
  1232. exp = tswapal(sel->exp);
  1233. tvp = tswapal(sel->tvp);
  1234. unlock_user_struct(sel, arg1, 0);
  1235. return do_select(nsel, inp, outp, exp, tvp);
  1236. }
  1237. #endif
  1238. #endif
  1239. #if defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
  1240. static abi_long do_pselect6(abi_long arg1, abi_long arg2, abi_long arg3,
  1241. abi_long arg4, abi_long arg5, abi_long arg6,
  1242. bool time64)
  1243. {
  1244. abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
  1245. fd_set rfds, wfds, efds;
  1246. fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
  1247. struct timespec ts, *ts_ptr;
  1248. abi_long ret;
  1249. /*
  1250. * The 6th arg is actually two args smashed together,
  1251. * so we cannot use the C library.
  1252. */
  1253. struct {
  1254. sigset_t *set;
  1255. size_t size;
  1256. } sig, *sig_ptr;
  1257. abi_ulong arg_sigset, arg_sigsize, *arg7;
  1258. n = arg1;
  1259. rfd_addr = arg2;
  1260. wfd_addr = arg3;
  1261. efd_addr = arg4;
  1262. ts_addr = arg5;
  1263. ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
  1264. if (ret) {
  1265. return ret;
  1266. }
  1267. ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
  1268. if (ret) {
  1269. return ret;
  1270. }
  1271. ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
  1272. if (ret) {
  1273. return ret;
  1274. }
  1275. /*
  1276. * This takes a timespec, and not a timeval, so we cannot
  1277. * use the do_select() helper ...
  1278. */
  1279. if (ts_addr) {
  1280. if (time64) {
  1281. if (target_to_host_timespec64(&ts, ts_addr)) {
  1282. return -TARGET_EFAULT;
  1283. }
  1284. } else {
  1285. if (target_to_host_timespec(&ts, ts_addr)) {
  1286. return -TARGET_EFAULT;
  1287. }
  1288. }
  1289. ts_ptr = &ts;
  1290. } else {
  1291. ts_ptr = NULL;
  1292. }
  1293. /* Extract the two packed args for the sigset */
  1294. sig_ptr = NULL;
  1295. if (arg6) {
  1296. arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
  1297. if (!arg7) {
  1298. return -TARGET_EFAULT;
  1299. }
  1300. arg_sigset = tswapal(arg7[0]);
  1301. arg_sigsize = tswapal(arg7[1]);
  1302. unlock_user(arg7, arg6, 0);
  1303. if (arg_sigset) {
  1304. ret = process_sigsuspend_mask(&sig.set, arg_sigset, arg_sigsize);
  1305. if (ret != 0) {
  1306. return ret;
  1307. }
  1308. sig_ptr = &sig;
  1309. sig.size = SIGSET_T_SIZE;
  1310. }
  1311. }
  1312. ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
  1313. ts_ptr, sig_ptr));
  1314. if (sig_ptr) {
  1315. finish_sigsuspend_mask(ret);
  1316. }
  1317. if (!is_error(ret)) {
  1318. if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) {
  1319. return -TARGET_EFAULT;
  1320. }
  1321. if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) {
  1322. return -TARGET_EFAULT;
  1323. }
  1324. if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) {
  1325. return -TARGET_EFAULT;
  1326. }
  1327. if (time64) {
  1328. if (ts_addr && host_to_target_timespec64(ts_addr, &ts)) {
  1329. return -TARGET_EFAULT;
  1330. }
  1331. } else {
  1332. if (ts_addr && host_to_target_timespec(ts_addr, &ts)) {
  1333. return -TARGET_EFAULT;
  1334. }
  1335. }
  1336. }
  1337. return ret;
  1338. }
  1339. #endif
  1340. #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) || \
  1341. defined(TARGET_NR_ppoll_time64)
  1342. static abi_long do_ppoll(abi_long arg1, abi_long arg2, abi_long arg3,
  1343. abi_long arg4, abi_long arg5, bool ppoll, bool time64)
  1344. {
  1345. struct target_pollfd *target_pfd;
  1346. unsigned int nfds = arg2;
  1347. struct pollfd *pfd;
  1348. unsigned int i;
  1349. abi_long ret;
  1350. pfd = NULL;
  1351. target_pfd = NULL;
  1352. if (nfds) {
  1353. if (nfds > (INT_MAX / sizeof(struct target_pollfd))) {
  1354. return -TARGET_EINVAL;
  1355. }
  1356. target_pfd = lock_user(VERIFY_WRITE, arg1,
  1357. sizeof(struct target_pollfd) * nfds, 1);
  1358. if (!target_pfd) {
  1359. return -TARGET_EFAULT;
  1360. }
  1361. pfd = alloca(sizeof(struct pollfd) * nfds);
  1362. for (i = 0; i < nfds; i++) {
  1363. pfd[i].fd = tswap32(target_pfd[i].fd);
  1364. pfd[i].events = tswap16(target_pfd[i].events);
  1365. }
  1366. }
  1367. if (ppoll) {
  1368. struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
  1369. sigset_t *set = NULL;
  1370. if (arg3) {
  1371. if (time64) {
  1372. if (target_to_host_timespec64(timeout_ts, arg3)) {
  1373. unlock_user(target_pfd, arg1, 0);
  1374. return -TARGET_EFAULT;
  1375. }
  1376. } else {
  1377. if (target_to_host_timespec(timeout_ts, arg3)) {
  1378. unlock_user(target_pfd, arg1, 0);
  1379. return -TARGET_EFAULT;
  1380. }
  1381. }
  1382. } else {
  1383. timeout_ts = NULL;
  1384. }
  1385. if (arg4) {
  1386. ret = process_sigsuspend_mask(&set, arg4, arg5);
  1387. if (ret != 0) {
  1388. unlock_user(target_pfd, arg1, 0);
  1389. return ret;
  1390. }
  1391. }
  1392. ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts,
  1393. set, SIGSET_T_SIZE));
  1394. if (set) {
  1395. finish_sigsuspend_mask(ret);
  1396. }
  1397. if (!is_error(ret) && arg3) {
  1398. if (time64) {
  1399. if (host_to_target_timespec64(arg3, timeout_ts)) {
  1400. return -TARGET_EFAULT;
  1401. }
  1402. } else {
  1403. if (host_to_target_timespec(arg3, timeout_ts)) {
  1404. return -TARGET_EFAULT;
  1405. }
  1406. }
  1407. }
  1408. } else {
  1409. struct timespec ts, *pts;
  1410. if (arg3 >= 0) {
  1411. /* Convert ms to secs, ns */
  1412. ts.tv_sec = arg3 / 1000;
  1413. ts.tv_nsec = (arg3 % 1000) * 1000000LL;
  1414. pts = &ts;
  1415. } else {
  1416. /* -ve poll() timeout means "infinite" */
  1417. pts = NULL;
  1418. }
  1419. ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0));
  1420. }
  1421. if (!is_error(ret)) {
  1422. for (i = 0; i < nfds; i++) {
  1423. target_pfd[i].revents = tswap16(pfd[i].revents);
  1424. }
  1425. }
  1426. unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
  1427. return ret;
  1428. }
  1429. #endif
  1430. static abi_long do_pipe(CPUArchState *cpu_env, abi_ulong pipedes,
  1431. int flags, int is_pipe2)
  1432. {
  1433. int host_pipe[2];
  1434. abi_long ret;
  1435. ret = pipe2(host_pipe, flags);
  1436. if (is_error(ret))
  1437. return get_errno(ret);
  1438. /* Several targets have special calling conventions for the original
  1439. pipe syscall, but didn't replicate this into the pipe2 syscall. */
  1440. if (!is_pipe2) {
  1441. #if defined(TARGET_ALPHA)
  1442. cpu_env->ir[IR_A4] = host_pipe[1];
  1443. return host_pipe[0];
  1444. #elif defined(TARGET_MIPS)
  1445. cpu_env->active_tc.gpr[3] = host_pipe[1];
  1446. return host_pipe[0];
  1447. #elif defined(TARGET_SH4)
  1448. cpu_env->gregs[1] = host_pipe[1];
  1449. return host_pipe[0];
  1450. #elif defined(TARGET_SPARC)
  1451. cpu_env->regwptr[1] = host_pipe[1];
  1452. return host_pipe[0];
  1453. #endif
  1454. }
  1455. if (put_user_s32(host_pipe[0], pipedes)
  1456. || put_user_s32(host_pipe[1], pipedes + sizeof(abi_int)))
  1457. return -TARGET_EFAULT;
  1458. return get_errno(ret);
  1459. }
  1460. static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr,
  1461. abi_ulong target_addr,
  1462. socklen_t len)
  1463. {
  1464. const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
  1465. sa_family_t sa_family;
  1466. struct target_sockaddr *target_saddr;
  1467. if (fd_trans_target_to_host_addr(fd)) {
  1468. return fd_trans_target_to_host_addr(fd)(addr, target_addr, len);
  1469. }
  1470. target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
  1471. if (!target_saddr)
  1472. return -TARGET_EFAULT;
  1473. sa_family = tswap16(target_saddr->sa_family);
  1474. /* Oops. The caller might send a incomplete sun_path; sun_path
  1475. * must be terminated by \0 (see the manual page), but
  1476. * unfortunately it is quite common to specify sockaddr_un
  1477. * length as "strlen(x->sun_path)" while it should be
  1478. * "strlen(...) + 1". We'll fix that here if needed.
  1479. * Linux kernel has a similar feature.
  1480. */
  1481. if (sa_family == AF_UNIX) {
  1482. if (len < unix_maxlen && len > 0) {
  1483. char *cp = (char*)target_saddr;
  1484. if ( cp[len-1] && !cp[len] )
  1485. len++;
  1486. }
  1487. if (len > unix_maxlen)
  1488. len = unix_maxlen;
  1489. }
  1490. memcpy(addr, target_saddr, len);
  1491. addr->sa_family = sa_family;
  1492. if (sa_family == AF_NETLINK) {
  1493. struct sockaddr_nl *nladdr;
  1494. nladdr = (struct sockaddr_nl *)addr;
  1495. nladdr->nl_pid = tswap32(nladdr->nl_pid);
  1496. nladdr->nl_groups = tswap32(nladdr->nl_groups);
  1497. } else if (sa_family == AF_PACKET) {
  1498. struct target_sockaddr_ll *lladdr;
  1499. lladdr = (struct target_sockaddr_ll *)addr;
  1500. lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex);
  1501. lladdr->sll_hatype = tswap16(lladdr->sll_hatype);
  1502. } else if (sa_family == AF_INET6) {
  1503. struct sockaddr_in6 *in6addr;
  1504. in6addr = (struct sockaddr_in6 *)addr;
  1505. in6addr->sin6_scope_id = tswap32(in6addr->sin6_scope_id);
  1506. }
  1507. unlock_user(target_saddr, target_addr, 0);
  1508. return 0;
  1509. }
  1510. static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
  1511. struct sockaddr *addr,
  1512. socklen_t len)
  1513. {
  1514. struct target_sockaddr *target_saddr;
  1515. if (len == 0) {
  1516. return 0;
  1517. }
  1518. assert(addr);
  1519. target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
  1520. if (!target_saddr)
  1521. return -TARGET_EFAULT;
  1522. memcpy(target_saddr, addr, len);
  1523. if (len >= offsetof(struct target_sockaddr, sa_family) +
  1524. sizeof(target_saddr->sa_family)) {
  1525. target_saddr->sa_family = tswap16(addr->sa_family);
  1526. }
  1527. if (addr->sa_family == AF_NETLINK &&
  1528. len >= sizeof(struct target_sockaddr_nl)) {
  1529. struct target_sockaddr_nl *target_nl =
  1530. (struct target_sockaddr_nl *)target_saddr;
  1531. target_nl->nl_pid = tswap32(target_nl->nl_pid);
  1532. target_nl->nl_groups = tswap32(target_nl->nl_groups);
  1533. } else if (addr->sa_family == AF_PACKET) {
  1534. struct sockaddr_ll *target_ll = (struct sockaddr_ll *)target_saddr;
  1535. target_ll->sll_ifindex = tswap32(target_ll->sll_ifindex);
  1536. target_ll->sll_hatype = tswap16(target_ll->sll_hatype);
  1537. } else if (addr->sa_family == AF_INET6 &&
  1538. len >= sizeof(struct target_sockaddr_in6)) {
  1539. struct target_sockaddr_in6 *target_in6 =
  1540. (struct target_sockaddr_in6 *)target_saddr;
  1541. target_in6->sin6_scope_id = tswap16(target_in6->sin6_scope_id);
  1542. }
  1543. unlock_user(target_saddr, target_addr, len);
  1544. return 0;
  1545. }
  1546. static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
  1547. struct target_msghdr *target_msgh)
  1548. {
  1549. struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
  1550. abi_long msg_controllen;
  1551. abi_ulong target_cmsg_addr;
  1552. struct target_cmsghdr *target_cmsg, *target_cmsg_start;
  1553. socklen_t space = 0;
  1554. msg_controllen = tswapal(target_msgh->msg_controllen);
  1555. if (msg_controllen < sizeof (struct target_cmsghdr))
  1556. goto the_end;
  1557. target_cmsg_addr = tswapal(target_msgh->msg_control);
  1558. target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
  1559. target_cmsg_start = target_cmsg;
  1560. if (!target_cmsg)
  1561. return -TARGET_EFAULT;
  1562. while (cmsg && target_cmsg) {
  1563. void *data = CMSG_DATA(cmsg);
  1564. void *target_data = TARGET_CMSG_DATA(target_cmsg);
  1565. int len = tswapal(target_cmsg->cmsg_len)
  1566. - sizeof(struct target_cmsghdr);
  1567. space += CMSG_SPACE(len);
  1568. if (space > msgh->msg_controllen) {
  1569. space -= CMSG_SPACE(len);
  1570. /* This is a QEMU bug, since we allocated the payload
  1571. * area ourselves (unlike overflow in host-to-target
  1572. * conversion, which is just the guest giving us a buffer
  1573. * that's too small). It can't happen for the payload types
  1574. * we currently support; if it becomes an issue in future
  1575. * we would need to improve our allocation strategy to
  1576. * something more intelligent than "twice the size of the
  1577. * target buffer we're reading from".
  1578. */
  1579. qemu_log_mask(LOG_UNIMP,
  1580. ("Unsupported ancillary data %d/%d: "
  1581. "unhandled msg size\n"),
  1582. tswap32(target_cmsg->cmsg_level),
  1583. tswap32(target_cmsg->cmsg_type));
  1584. break;
  1585. }
  1586. if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
  1587. cmsg->cmsg_level = SOL_SOCKET;
  1588. } else {
  1589. cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
  1590. }
  1591. cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
  1592. cmsg->cmsg_len = CMSG_LEN(len);
  1593. if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
  1594. int *fd = (int *)data;
  1595. int *target_fd = (int *)target_data;
  1596. int i, numfds = len / sizeof(int);
  1597. for (i = 0; i < numfds; i++) {
  1598. __get_user(fd[i], target_fd + i);
  1599. }
  1600. } else if (cmsg->cmsg_level == SOL_SOCKET
  1601. && cmsg->cmsg_type == SCM_CREDENTIALS) {
  1602. struct ucred *cred = (struct ucred *)data;
  1603. struct target_ucred *target_cred =
  1604. (struct target_ucred *)target_data;
  1605. __get_user(cred->pid, &target_cred->pid);
  1606. __get_user(cred->uid, &target_cred->uid);
  1607. __get_user(cred->gid, &target_cred->gid);
  1608. } else if (cmsg->cmsg_level == SOL_ALG) {
  1609. uint32_t *dst = (uint32_t *)data;
  1610. memcpy(dst, target_data, len);
  1611. /* fix endianness of first 32-bit word */
  1612. if (len >= sizeof(uint32_t)) {
  1613. *dst = tswap32(*dst);
  1614. }
  1615. } else {
  1616. qemu_log_mask(LOG_UNIMP, "Unsupported target ancillary data: %d/%d\n",
  1617. cmsg->cmsg_level, cmsg->cmsg_type);
  1618. memcpy(data, target_data, len);
  1619. }
  1620. cmsg = CMSG_NXTHDR(msgh, cmsg);
  1621. target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
  1622. target_cmsg_start);
  1623. }
  1624. unlock_user(target_cmsg, target_cmsg_addr, 0);
  1625. the_end:
  1626. msgh->msg_controllen = space;
  1627. return 0;
  1628. }
  1629. static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
  1630. struct msghdr *msgh)
  1631. {
  1632. struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
  1633. abi_long msg_controllen;
  1634. abi_ulong target_cmsg_addr;
  1635. struct target_cmsghdr *target_cmsg, *target_cmsg_start;
  1636. socklen_t space = 0;
  1637. msg_controllen = tswapal(target_msgh->msg_controllen);
  1638. if (msg_controllen < sizeof (struct target_cmsghdr))
  1639. goto the_end;
  1640. target_cmsg_addr = tswapal(target_msgh->msg_control);
  1641. target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
  1642. target_cmsg_start = target_cmsg;
  1643. if (!target_cmsg)
  1644. return -TARGET_EFAULT;
  1645. while (cmsg && target_cmsg) {
  1646. void *data = CMSG_DATA(cmsg);
  1647. void *target_data = TARGET_CMSG_DATA(target_cmsg);
  1648. int len = cmsg->cmsg_len - sizeof(struct cmsghdr);
  1649. int tgt_len, tgt_space;
  1650. /* We never copy a half-header but may copy half-data;
  1651. * this is Linux's behaviour in put_cmsg(). Note that
  1652. * truncation here is a guest problem (which we report
  1653. * to the guest via the CTRUNC bit), unlike truncation
  1654. * in target_to_host_cmsg, which is a QEMU bug.
  1655. */
  1656. if (msg_controllen < sizeof(struct target_cmsghdr)) {
  1657. target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
  1658. break;
  1659. }
  1660. if (cmsg->cmsg_level == SOL_SOCKET) {
  1661. target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
  1662. } else {
  1663. target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
  1664. }
  1665. target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
  1666. /* Payload types which need a different size of payload on
  1667. * the target must adjust tgt_len here.
  1668. */
  1669. tgt_len = len;
  1670. switch (cmsg->cmsg_level) {
  1671. case SOL_SOCKET:
  1672. switch (cmsg->cmsg_type) {
  1673. case SO_TIMESTAMP:
  1674. tgt_len = sizeof(struct target_timeval);
  1675. break;
  1676. default:
  1677. break;
  1678. }
  1679. break;
  1680. default:
  1681. break;
  1682. }
  1683. if (msg_controllen < TARGET_CMSG_LEN(tgt_len)) {
  1684. target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
  1685. tgt_len = msg_controllen - sizeof(struct target_cmsghdr);
  1686. }
  1687. /* We must now copy-and-convert len bytes of payload
  1688. * into tgt_len bytes of destination space. Bear in mind
  1689. * that in both source and destination we may be dealing
  1690. * with a truncated value!
  1691. */
  1692. switch (cmsg->cmsg_level) {
  1693. case SOL_SOCKET:
  1694. switch (cmsg->cmsg_type) {
  1695. case SCM_RIGHTS:
  1696. {
  1697. int *fd = (int *)data;
  1698. int *target_fd = (int *)target_data;
  1699. int i, numfds = tgt_len / sizeof(int);
  1700. for (i = 0; i < numfds; i++) {
  1701. __put_user(fd[i], target_fd + i);
  1702. }
  1703. break;
  1704. }
  1705. case SO_TIMESTAMP:
  1706. {
  1707. struct timeval *tv = (struct timeval *)data;
  1708. struct target_timeval *target_tv =
  1709. (struct target_timeval *)target_data;
  1710. if (len != sizeof(struct timeval) ||
  1711. tgt_len != sizeof(struct target_timeval)) {
  1712. goto unimplemented;
  1713. }
  1714. /* copy struct timeval to target */
  1715. __put_user(tv->tv_sec, &target_tv->tv_sec);
  1716. __put_user(tv->tv_usec, &target_tv->tv_usec);
  1717. break;
  1718. }
  1719. case SCM_CREDENTIALS:
  1720. {
  1721. struct ucred *cred = (struct ucred *)data;
  1722. struct target_ucred *target_cred =
  1723. (struct target_ucred *)target_data;
  1724. __put_user(cred->pid, &target_cred->pid);
  1725. __put_user(cred->uid, &target_cred->uid);
  1726. __put_user(cred->gid, &target_cred->gid);
  1727. break;
  1728. }
  1729. default:
  1730. goto unimplemented;
  1731. }
  1732. break;
  1733. case SOL_IP:
  1734. switch (cmsg->cmsg_type) {
  1735. case IP_TTL:
  1736. {
  1737. uint32_t *v = (uint32_t *)data;
  1738. uint32_t *t_int = (uint32_t *)target_data;
  1739. if (len != sizeof(uint32_t) ||
  1740. tgt_len != sizeof(uint32_t)) {
  1741. goto unimplemented;
  1742. }
  1743. __put_user(*v, t_int);
  1744. break;
  1745. }
  1746. case IP_RECVERR:
  1747. {
  1748. struct errhdr_t {
  1749. struct sock_extended_err ee;
  1750. struct sockaddr_in offender;
  1751. };
  1752. struct errhdr_t *errh = (struct errhdr_t *)data;
  1753. struct errhdr_t *target_errh =
  1754. (struct errhdr_t *)target_data;
  1755. if (len != sizeof(struct errhdr_t) ||
  1756. tgt_len != sizeof(struct errhdr_t)) {
  1757. goto unimplemented;
  1758. }
  1759. __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
  1760. __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
  1761. __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
  1762. __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
  1763. __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
  1764. __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
  1765. __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
  1766. host_to_target_sockaddr((unsigned long) &target_errh->offender,
  1767. (void *) &errh->offender, sizeof(errh->offender));
  1768. break;
  1769. }
  1770. case IP_PKTINFO:
  1771. {
  1772. struct in_pktinfo *pkti = data;
  1773. struct target_in_pktinfo *target_pi = target_data;
  1774. __put_user(pkti->ipi_ifindex, &target_pi->ipi_ifindex);
  1775. target_pi->ipi_spec_dst.s_addr = pkti->ipi_spec_dst.s_addr;
  1776. target_pi->ipi_addr.s_addr = pkti->ipi_addr.s_addr;
  1777. break;
  1778. }
  1779. default:
  1780. goto unimplemented;
  1781. }
  1782. break;
  1783. case SOL_IPV6:
  1784. switch (cmsg->cmsg_type) {
  1785. case IPV6_HOPLIMIT:
  1786. {
  1787. uint32_t *v = (uint32_t *)data;
  1788. uint32_t *t_int = (uint32_t *)target_data;
  1789. if (len != sizeof(uint32_t) ||
  1790. tgt_len != sizeof(uint32_t)) {
  1791. goto unimplemented;
  1792. }
  1793. __put_user(*v, t_int);
  1794. break;
  1795. }
  1796. case IPV6_RECVERR:
  1797. {
  1798. struct errhdr6_t {
  1799. struct sock_extended_err ee;
  1800. struct sockaddr_in6 offender;
  1801. };
  1802. struct errhdr6_t *errh = (struct errhdr6_t *)data;
  1803. struct errhdr6_t *target_errh =
  1804. (struct errhdr6_t *)target_data;
  1805. if (len != sizeof(struct errhdr6_t) ||
  1806. tgt_len != sizeof(struct errhdr6_t)) {
  1807. goto unimplemented;
  1808. }
  1809. __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
  1810. __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
  1811. __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
  1812. __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
  1813. __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
  1814. __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
  1815. __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
  1816. host_to_target_sockaddr((unsigned long) &target_errh->offender,
  1817. (void *) &errh->offender, sizeof(errh->offender));
  1818. break;
  1819. }
  1820. default:
  1821. goto unimplemented;
  1822. }
  1823. break;
  1824. default:
  1825. unimplemented:
  1826. qemu_log_mask(LOG_UNIMP, "Unsupported host ancillary data: %d/%d\n",
  1827. cmsg->cmsg_level, cmsg->cmsg_type);
  1828. memcpy(target_data, data, MIN(len, tgt_len));
  1829. if (tgt_len > len) {
  1830. memset(target_data + len, 0, tgt_len - len);
  1831. }
  1832. }
  1833. target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(tgt_len));
  1834. tgt_space = TARGET_CMSG_SPACE(tgt_len);
  1835. if (msg_controllen < tgt_space) {
  1836. tgt_space = msg_controllen;
  1837. }
  1838. msg_controllen -= tgt_space;
  1839. space += tgt_space;
  1840. cmsg = CMSG_NXTHDR(msgh, cmsg);
  1841. target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
  1842. target_cmsg_start);
  1843. }
  1844. unlock_user(target_cmsg, target_cmsg_addr, space);
  1845. the_end:
  1846. target_msgh->msg_controllen = tswapal(space);
  1847. return 0;
  1848. }
  1849. /* do_setsockopt() Must return target values and target errnos. */
  1850. static abi_long do_setsockopt(int sockfd, int level, int optname,
  1851. abi_ulong optval_addr, socklen_t optlen)
  1852. {
  1853. abi_long ret;
  1854. int val;
  1855. switch(level) {
  1856. case SOL_TCP:
  1857. case SOL_UDP:
  1858. /* TCP and UDP options all take an 'int' value. */
  1859. if (optlen < sizeof(uint32_t))
  1860. return -TARGET_EINVAL;
  1861. if (get_user_u32(val, optval_addr))
  1862. return -TARGET_EFAULT;
  1863. ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
  1864. break;
  1865. case SOL_IP:
  1866. switch(optname) {
  1867. case IP_TOS:
  1868. case IP_TTL:
  1869. case IP_HDRINCL:
  1870. case IP_ROUTER_ALERT:
  1871. case IP_RECVOPTS:
  1872. case IP_RETOPTS:
  1873. case IP_PKTINFO:
  1874. case IP_MTU_DISCOVER:
  1875. case IP_RECVERR:
  1876. case IP_RECVTTL:
  1877. case IP_RECVTOS:
  1878. #ifdef IP_FREEBIND
  1879. case IP_FREEBIND:
  1880. #endif
  1881. case IP_MULTICAST_TTL:
  1882. case IP_MULTICAST_LOOP:
  1883. val = 0;
  1884. if (optlen >= sizeof(uint32_t)) {
  1885. if (get_user_u32(val, optval_addr))
  1886. return -TARGET_EFAULT;
  1887. } else if (optlen >= 1) {
  1888. if (get_user_u8(val, optval_addr))
  1889. return -TARGET_EFAULT;
  1890. }
  1891. ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
  1892. break;
  1893. case IP_MULTICAST_IF:
  1894. case IP_ADD_MEMBERSHIP:
  1895. case IP_DROP_MEMBERSHIP:
  1896. {
  1897. struct ip_mreqn ip_mreq;
  1898. struct target_ip_mreqn *target_smreqn;
  1899. int min_size;
  1900. QEMU_BUILD_BUG_ON(sizeof(struct ip_mreq) !=
  1901. sizeof(struct target_ip_mreq));
  1902. if (optname == IP_MULTICAST_IF) {
  1903. min_size = sizeof(struct in_addr);
  1904. } else {
  1905. min_size = sizeof(struct target_ip_mreq);
  1906. }
  1907. if (optlen < min_size ||
  1908. optlen > sizeof (struct target_ip_mreqn)) {
  1909. return -TARGET_EINVAL;
  1910. }
  1911. target_smreqn = lock_user(VERIFY_READ, optval_addr, optlen, 1);
  1912. if (!target_smreqn) {
  1913. return -TARGET_EFAULT;
  1914. }
  1915. ip_mreq.imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
  1916. if (optlen >= sizeof(struct target_ip_mreq)) {
  1917. ip_mreq.imr_address.s_addr = target_smreqn->imr_address.s_addr;
  1918. if (optlen >= sizeof(struct target_ip_mreqn)) {
  1919. __put_user(target_smreqn->imr_ifindex, &ip_mreq.imr_ifindex);
  1920. optlen = sizeof(struct ip_mreqn);
  1921. }
  1922. }
  1923. unlock_user(target_smreqn, optval_addr, 0);
  1924. ret = get_errno(setsockopt(sockfd, level, optname, &ip_mreq, optlen));
  1925. break;
  1926. }
  1927. case IP_BLOCK_SOURCE:
  1928. case IP_UNBLOCK_SOURCE:
  1929. case IP_ADD_SOURCE_MEMBERSHIP:
  1930. case IP_DROP_SOURCE_MEMBERSHIP:
  1931. {
  1932. struct ip_mreq_source *ip_mreq_source;
  1933. if (optlen != sizeof (struct target_ip_mreq_source))
  1934. return -TARGET_EINVAL;
  1935. ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
  1936. if (!ip_mreq_source) {
  1937. return -TARGET_EFAULT;
  1938. }
  1939. ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
  1940. unlock_user (ip_mreq_source, optval_addr, 0);
  1941. break;
  1942. }
  1943. default:
  1944. goto unimplemented;
  1945. }
  1946. break;
  1947. case SOL_IPV6:
  1948. switch (optname) {
  1949. case IPV6_MTU_DISCOVER:
  1950. case IPV6_MTU:
  1951. case IPV6_V6ONLY:
  1952. case IPV6_RECVPKTINFO:
  1953. case IPV6_UNICAST_HOPS:
  1954. case IPV6_MULTICAST_HOPS:
  1955. case IPV6_MULTICAST_LOOP:
  1956. case IPV6_RECVERR:
  1957. case IPV6_RECVHOPLIMIT:
  1958. case IPV6_2292HOPLIMIT:
  1959. case IPV6_CHECKSUM:
  1960. case IPV6_ADDRFORM:
  1961. case IPV6_2292PKTINFO:
  1962. case IPV6_RECVTCLASS:
  1963. case IPV6_RECVRTHDR:
  1964. case IPV6_2292RTHDR:
  1965. case IPV6_RECVHOPOPTS:
  1966. case IPV6_2292HOPOPTS:
  1967. case IPV6_RECVDSTOPTS:
  1968. case IPV6_2292DSTOPTS:
  1969. case IPV6_TCLASS:
  1970. case IPV6_ADDR_PREFERENCES:
  1971. #ifdef IPV6_RECVPATHMTU
  1972. case IPV6_RECVPATHMTU:
  1973. #endif
  1974. #ifdef IPV6_TRANSPARENT
  1975. case IPV6_TRANSPARENT:
  1976. #endif
  1977. #ifdef IPV6_FREEBIND
  1978. case IPV6_FREEBIND:
  1979. #endif
  1980. #ifdef IPV6_RECVORIGDSTADDR
  1981. case IPV6_RECVORIGDSTADDR:
  1982. #endif
  1983. val = 0;
  1984. if (optlen < sizeof(uint32_t)) {
  1985. return -TARGET_EINVAL;
  1986. }
  1987. if (get_user_u32(val, optval_addr)) {
  1988. return -TARGET_EFAULT;
  1989. }
  1990. ret = get_errno(setsockopt(sockfd, level, optname,
  1991. &val, sizeof(val)));
  1992. break;
  1993. case IPV6_PKTINFO:
  1994. {
  1995. struct in6_pktinfo pki;
  1996. if (optlen < sizeof(pki)) {
  1997. return -TARGET_EINVAL;
  1998. }
  1999. if (copy_from_user(&pki, optval_addr, sizeof(pki))) {
  2000. return -TARGET_EFAULT;
  2001. }
  2002. pki.ipi6_ifindex = tswap32(pki.ipi6_ifindex);
  2003. ret = get_errno(setsockopt(sockfd, level, optname,
  2004. &pki, sizeof(pki)));
  2005. break;
  2006. }
  2007. case IPV6_ADD_MEMBERSHIP:
  2008. case IPV6_DROP_MEMBERSHIP:
  2009. {
  2010. struct ipv6_mreq ipv6mreq;
  2011. if (optlen < sizeof(ipv6mreq)) {
  2012. return -TARGET_EINVAL;
  2013. }
  2014. if (copy_from_user(&ipv6mreq, optval_addr, sizeof(ipv6mreq))) {
  2015. return -TARGET_EFAULT;
  2016. }
  2017. ipv6mreq.ipv6mr_interface = tswap32(ipv6mreq.ipv6mr_interface);
  2018. ret = get_errno(setsockopt(sockfd, level, optname,
  2019. &ipv6mreq, sizeof(ipv6mreq)));
  2020. break;
  2021. }
  2022. default:
  2023. goto unimplemented;
  2024. }
  2025. break;
  2026. case SOL_ICMPV6:
  2027. switch (optname) {
  2028. case ICMPV6_FILTER:
  2029. {
  2030. struct icmp6_filter icmp6f;
  2031. if (optlen > sizeof(icmp6f)) {
  2032. optlen = sizeof(icmp6f);
  2033. }
  2034. if (copy_from_user(&icmp6f, optval_addr, optlen)) {
  2035. return -TARGET_EFAULT;
  2036. }
  2037. for (val = 0; val < 8; val++) {
  2038. icmp6f.data[val] = tswap32(icmp6f.data[val]);
  2039. }
  2040. ret = get_errno(setsockopt(sockfd, level, optname,
  2041. &icmp6f, optlen));
  2042. break;
  2043. }
  2044. default:
  2045. goto unimplemented;
  2046. }
  2047. break;
  2048. case SOL_RAW:
  2049. switch (optname) {
  2050. case ICMP_FILTER:
  2051. case IPV6_CHECKSUM:
  2052. /* those take an u32 value */
  2053. if (optlen < sizeof(uint32_t)) {
  2054. return -TARGET_EINVAL;
  2055. }
  2056. if (get_user_u32(val, optval_addr)) {
  2057. return -TARGET_EFAULT;
  2058. }
  2059. ret = get_errno(setsockopt(sockfd, level, optname,
  2060. &val, sizeof(val)));
  2061. break;
  2062. default:
  2063. goto unimplemented;
  2064. }
  2065. break;
  2066. #if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE)
  2067. case SOL_ALG:
  2068. switch (optname) {
  2069. case ALG_SET_KEY:
  2070. {
  2071. char *alg_key = lock_user(VERIFY_READ, optval_addr, optlen, 1);
  2072. if (!alg_key) {
  2073. return -TARGET_EFAULT;
  2074. }
  2075. ret = get_errno(setsockopt(sockfd, level, optname,
  2076. alg_key, optlen));
  2077. unlock_user(alg_key, optval_addr, optlen);
  2078. break;
  2079. }
  2080. case ALG_SET_AEAD_AUTHSIZE:
  2081. {
  2082. ret = get_errno(setsockopt(sockfd, level, optname,
  2083. NULL, optlen));
  2084. break;
  2085. }
  2086. default:
  2087. goto unimplemented;
  2088. }
  2089. break;
  2090. #endif
  2091. case TARGET_SOL_SOCKET:
  2092. switch (optname) {
  2093. case TARGET_SO_RCVTIMEO:
  2094. case TARGET_SO_SNDTIMEO:
  2095. {
  2096. struct timeval tv;
  2097. if (optlen != sizeof(struct target_timeval)) {
  2098. return -TARGET_EINVAL;
  2099. }
  2100. if (copy_from_user_timeval(&tv, optval_addr)) {
  2101. return -TARGET_EFAULT;
  2102. }
  2103. ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
  2104. optname == TARGET_SO_RCVTIMEO ?
  2105. SO_RCVTIMEO : SO_SNDTIMEO,
  2106. &tv, sizeof(tv)));
  2107. return ret;
  2108. }
  2109. case TARGET_SO_ATTACH_FILTER:
  2110. {
  2111. struct target_sock_fprog *tfprog;
  2112. struct target_sock_filter *tfilter;
  2113. struct sock_fprog fprog;
  2114. struct sock_filter *filter;
  2115. int i;
  2116. if (optlen != sizeof(*tfprog)) {
  2117. return -TARGET_EINVAL;
  2118. }
  2119. if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
  2120. return -TARGET_EFAULT;
  2121. }
  2122. if (!lock_user_struct(VERIFY_READ, tfilter,
  2123. tswapal(tfprog->filter), 0)) {
  2124. unlock_user_struct(tfprog, optval_addr, 1);
  2125. return -TARGET_EFAULT;
  2126. }
  2127. fprog.len = tswap16(tfprog->len);
  2128. filter = g_try_new(struct sock_filter, fprog.len);
  2129. if (filter == NULL) {
  2130. unlock_user_struct(tfilter, tfprog->filter, 1);
  2131. unlock_user_struct(tfprog, optval_addr, 1);
  2132. return -TARGET_ENOMEM;
  2133. }
  2134. for (i = 0; i < fprog.len; i++) {
  2135. filter[i].code = tswap16(tfilter[i].code);
  2136. filter[i].jt = tfilter[i].jt;
  2137. filter[i].jf = tfilter[i].jf;
  2138. filter[i].k = tswap32(tfilter[i].k);
  2139. }
  2140. fprog.filter = filter;
  2141. ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
  2142. SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
  2143. g_free(filter);
  2144. unlock_user_struct(tfilter, tfprog->filter, 1);
  2145. unlock_user_struct(tfprog, optval_addr, 1);
  2146. return ret;
  2147. }
  2148. case TARGET_SO_BINDTODEVICE:
  2149. {
  2150. char *dev_ifname, *addr_ifname;
  2151. if (optlen > IFNAMSIZ - 1) {
  2152. optlen = IFNAMSIZ - 1;
  2153. }
  2154. dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1);
  2155. if (!dev_ifname) {
  2156. return -TARGET_EFAULT;
  2157. }
  2158. optname = SO_BINDTODEVICE;
  2159. addr_ifname = alloca(IFNAMSIZ);
  2160. memcpy(addr_ifname, dev_ifname, optlen);
  2161. addr_ifname[optlen] = 0;
  2162. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
  2163. addr_ifname, optlen));
  2164. unlock_user (dev_ifname, optval_addr, 0);
  2165. return ret;
  2166. }
  2167. case TARGET_SO_LINGER:
  2168. {
  2169. struct linger lg;
  2170. struct target_linger *tlg;
  2171. if (optlen != sizeof(struct target_linger)) {
  2172. return -TARGET_EINVAL;
  2173. }
  2174. if (!lock_user_struct(VERIFY_READ, tlg, optval_addr, 1)) {
  2175. return -TARGET_EFAULT;
  2176. }
  2177. __get_user(lg.l_onoff, &tlg->l_onoff);
  2178. __get_user(lg.l_linger, &tlg->l_linger);
  2179. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, SO_LINGER,
  2180. &lg, sizeof(lg)));
  2181. unlock_user_struct(tlg, optval_addr, 0);
  2182. return ret;
  2183. }
  2184. /* Options with 'int' argument. */
  2185. case TARGET_SO_DEBUG:
  2186. optname = SO_DEBUG;
  2187. break;
  2188. case TARGET_SO_REUSEADDR:
  2189. optname = SO_REUSEADDR;
  2190. break;
  2191. #ifdef SO_REUSEPORT
  2192. case TARGET_SO_REUSEPORT:
  2193. optname = SO_REUSEPORT;
  2194. break;
  2195. #endif
  2196. case TARGET_SO_TYPE:
  2197. optname = SO_TYPE;
  2198. break;
  2199. case TARGET_SO_ERROR:
  2200. optname = SO_ERROR;
  2201. break;
  2202. case TARGET_SO_DONTROUTE:
  2203. optname = SO_DONTROUTE;
  2204. break;
  2205. case TARGET_SO_BROADCAST:
  2206. optname = SO_BROADCAST;
  2207. break;
  2208. case TARGET_SO_SNDBUF:
  2209. optname = SO_SNDBUF;
  2210. break;
  2211. case TARGET_SO_SNDBUFFORCE:
  2212. optname = SO_SNDBUFFORCE;
  2213. break;
  2214. case TARGET_SO_RCVBUF:
  2215. optname = SO_RCVBUF;
  2216. break;
  2217. case TARGET_SO_RCVBUFFORCE:
  2218. optname = SO_RCVBUFFORCE;
  2219. break;
  2220. case TARGET_SO_KEEPALIVE:
  2221. optname = SO_KEEPALIVE;
  2222. break;
  2223. case TARGET_SO_OOBINLINE:
  2224. optname = SO_OOBINLINE;
  2225. break;
  2226. case TARGET_SO_NO_CHECK:
  2227. optname = SO_NO_CHECK;
  2228. break;
  2229. case TARGET_SO_PRIORITY:
  2230. optname = SO_PRIORITY;
  2231. break;
  2232. #ifdef SO_BSDCOMPAT
  2233. case TARGET_SO_BSDCOMPAT:
  2234. optname = SO_BSDCOMPAT;
  2235. break;
  2236. #endif
  2237. case TARGET_SO_PASSCRED:
  2238. optname = SO_PASSCRED;
  2239. break;
  2240. case TARGET_SO_PASSSEC:
  2241. optname = SO_PASSSEC;
  2242. break;
  2243. case TARGET_SO_TIMESTAMP:
  2244. optname = SO_TIMESTAMP;
  2245. break;
  2246. case TARGET_SO_RCVLOWAT:
  2247. optname = SO_RCVLOWAT;
  2248. break;
  2249. default:
  2250. goto unimplemented;
  2251. }
  2252. if (optlen < sizeof(uint32_t))
  2253. return -TARGET_EINVAL;
  2254. if (get_user_u32(val, optval_addr))
  2255. return -TARGET_EFAULT;
  2256. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
  2257. break;
  2258. #ifdef SOL_NETLINK
  2259. case SOL_NETLINK:
  2260. switch (optname) {
  2261. case NETLINK_PKTINFO:
  2262. case NETLINK_ADD_MEMBERSHIP:
  2263. case NETLINK_DROP_MEMBERSHIP:
  2264. case NETLINK_BROADCAST_ERROR:
  2265. case NETLINK_NO_ENOBUFS:
  2266. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
  2267. case NETLINK_LISTEN_ALL_NSID:
  2268. case NETLINK_CAP_ACK:
  2269. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
  2270. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
  2271. case NETLINK_EXT_ACK:
  2272. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2273. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
  2274. case NETLINK_GET_STRICT_CHK:
  2275. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2276. break;
  2277. default:
  2278. goto unimplemented;
  2279. }
  2280. val = 0;
  2281. if (optlen < sizeof(uint32_t)) {
  2282. return -TARGET_EINVAL;
  2283. }
  2284. if (get_user_u32(val, optval_addr)) {
  2285. return -TARGET_EFAULT;
  2286. }
  2287. ret = get_errno(setsockopt(sockfd, SOL_NETLINK, optname, &val,
  2288. sizeof(val)));
  2289. break;
  2290. #endif /* SOL_NETLINK */
  2291. default:
  2292. unimplemented:
  2293. qemu_log_mask(LOG_UNIMP, "Unsupported setsockopt level=%d optname=%d\n",
  2294. level, optname);
  2295. ret = -TARGET_ENOPROTOOPT;
  2296. }
  2297. return ret;
  2298. }
  2299. /* do_getsockopt() Must return target values and target errnos. */
  2300. static abi_long do_getsockopt(int sockfd, int level, int optname,
  2301. abi_ulong optval_addr, abi_ulong optlen)
  2302. {
  2303. abi_long ret;
  2304. int len, val;
  2305. socklen_t lv;
  2306. switch(level) {
  2307. case TARGET_SOL_SOCKET:
  2308. level = SOL_SOCKET;
  2309. switch (optname) {
  2310. /* These don't just return a single integer */
  2311. case TARGET_SO_PEERNAME:
  2312. goto unimplemented;
  2313. case TARGET_SO_RCVTIMEO: {
  2314. struct timeval tv;
  2315. socklen_t tvlen;
  2316. optname = SO_RCVTIMEO;
  2317. get_timeout:
  2318. if (get_user_u32(len, optlen)) {
  2319. return -TARGET_EFAULT;
  2320. }
  2321. if (len < 0) {
  2322. return -TARGET_EINVAL;
  2323. }
  2324. tvlen = sizeof(tv);
  2325. ret = get_errno(getsockopt(sockfd, level, optname,
  2326. &tv, &tvlen));
  2327. if (ret < 0) {
  2328. return ret;
  2329. }
  2330. if (len > sizeof(struct target_timeval)) {
  2331. len = sizeof(struct target_timeval);
  2332. }
  2333. if (copy_to_user_timeval(optval_addr, &tv)) {
  2334. return -TARGET_EFAULT;
  2335. }
  2336. if (put_user_u32(len, optlen)) {
  2337. return -TARGET_EFAULT;
  2338. }
  2339. break;
  2340. }
  2341. case TARGET_SO_SNDTIMEO:
  2342. optname = SO_SNDTIMEO;
  2343. goto get_timeout;
  2344. case TARGET_SO_PEERCRED: {
  2345. struct ucred cr;
  2346. socklen_t crlen;
  2347. struct target_ucred *tcr;
  2348. if (get_user_u32(len, optlen)) {
  2349. return -TARGET_EFAULT;
  2350. }
  2351. if (len < 0) {
  2352. return -TARGET_EINVAL;
  2353. }
  2354. crlen = sizeof(cr);
  2355. ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
  2356. &cr, &crlen));
  2357. if (ret < 0) {
  2358. return ret;
  2359. }
  2360. if (len > crlen) {
  2361. len = crlen;
  2362. }
  2363. if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
  2364. return -TARGET_EFAULT;
  2365. }
  2366. __put_user(cr.pid, &tcr->pid);
  2367. __put_user(cr.uid, &tcr->uid);
  2368. __put_user(cr.gid, &tcr->gid);
  2369. unlock_user_struct(tcr, optval_addr, 1);
  2370. if (put_user_u32(len, optlen)) {
  2371. return -TARGET_EFAULT;
  2372. }
  2373. break;
  2374. }
  2375. case TARGET_SO_PEERSEC: {
  2376. char *name;
  2377. if (get_user_u32(len, optlen)) {
  2378. return -TARGET_EFAULT;
  2379. }
  2380. if (len < 0) {
  2381. return -TARGET_EINVAL;
  2382. }
  2383. name = lock_user(VERIFY_WRITE, optval_addr, len, 0);
  2384. if (!name) {
  2385. return -TARGET_EFAULT;
  2386. }
  2387. lv = len;
  2388. ret = get_errno(getsockopt(sockfd, level, SO_PEERSEC,
  2389. name, &lv));
  2390. if (put_user_u32(lv, optlen)) {
  2391. ret = -TARGET_EFAULT;
  2392. }
  2393. unlock_user(name, optval_addr, lv);
  2394. break;
  2395. }
  2396. case TARGET_SO_LINGER:
  2397. {
  2398. struct linger lg;
  2399. socklen_t lglen;
  2400. struct target_linger *tlg;
  2401. if (get_user_u32(len, optlen)) {
  2402. return -TARGET_EFAULT;
  2403. }
  2404. if (len < 0) {
  2405. return -TARGET_EINVAL;
  2406. }
  2407. lglen = sizeof(lg);
  2408. ret = get_errno(getsockopt(sockfd, level, SO_LINGER,
  2409. &lg, &lglen));
  2410. if (ret < 0) {
  2411. return ret;
  2412. }
  2413. if (len > lglen) {
  2414. len = lglen;
  2415. }
  2416. if (!lock_user_struct(VERIFY_WRITE, tlg, optval_addr, 0)) {
  2417. return -TARGET_EFAULT;
  2418. }
  2419. __put_user(lg.l_onoff, &tlg->l_onoff);
  2420. __put_user(lg.l_linger, &tlg->l_linger);
  2421. unlock_user_struct(tlg, optval_addr, 1);
  2422. if (put_user_u32(len, optlen)) {
  2423. return -TARGET_EFAULT;
  2424. }
  2425. break;
  2426. }
  2427. /* Options with 'int' argument. */
  2428. case TARGET_SO_DEBUG:
  2429. optname = SO_DEBUG;
  2430. goto int_case;
  2431. case TARGET_SO_REUSEADDR:
  2432. optname = SO_REUSEADDR;
  2433. goto int_case;
  2434. #ifdef SO_REUSEPORT
  2435. case TARGET_SO_REUSEPORT:
  2436. optname = SO_REUSEPORT;
  2437. goto int_case;
  2438. #endif
  2439. case TARGET_SO_TYPE:
  2440. optname = SO_TYPE;
  2441. goto int_case;
  2442. case TARGET_SO_ERROR:
  2443. optname = SO_ERROR;
  2444. goto int_case;
  2445. case TARGET_SO_DONTROUTE:
  2446. optname = SO_DONTROUTE;
  2447. goto int_case;
  2448. case TARGET_SO_BROADCAST:
  2449. optname = SO_BROADCAST;
  2450. goto int_case;
  2451. case TARGET_SO_SNDBUF:
  2452. optname = SO_SNDBUF;
  2453. goto int_case;
  2454. case TARGET_SO_RCVBUF:
  2455. optname = SO_RCVBUF;
  2456. goto int_case;
  2457. case TARGET_SO_KEEPALIVE:
  2458. optname = SO_KEEPALIVE;
  2459. goto int_case;
  2460. case TARGET_SO_OOBINLINE:
  2461. optname = SO_OOBINLINE;
  2462. goto int_case;
  2463. case TARGET_SO_NO_CHECK:
  2464. optname = SO_NO_CHECK;
  2465. goto int_case;
  2466. case TARGET_SO_PRIORITY:
  2467. optname = SO_PRIORITY;
  2468. goto int_case;
  2469. #ifdef SO_BSDCOMPAT
  2470. case TARGET_SO_BSDCOMPAT:
  2471. optname = SO_BSDCOMPAT;
  2472. goto int_case;
  2473. #endif
  2474. case TARGET_SO_PASSCRED:
  2475. optname = SO_PASSCRED;
  2476. goto int_case;
  2477. case TARGET_SO_TIMESTAMP:
  2478. optname = SO_TIMESTAMP;
  2479. goto int_case;
  2480. case TARGET_SO_RCVLOWAT:
  2481. optname = SO_RCVLOWAT;
  2482. goto int_case;
  2483. case TARGET_SO_ACCEPTCONN:
  2484. optname = SO_ACCEPTCONN;
  2485. goto int_case;
  2486. case TARGET_SO_PROTOCOL:
  2487. optname = SO_PROTOCOL;
  2488. goto int_case;
  2489. case TARGET_SO_DOMAIN:
  2490. optname = SO_DOMAIN;
  2491. goto int_case;
  2492. default:
  2493. goto int_case;
  2494. }
  2495. break;
  2496. case SOL_TCP:
  2497. case SOL_UDP:
  2498. /* TCP and UDP options all take an 'int' value. */
  2499. int_case:
  2500. if (get_user_u32(len, optlen))
  2501. return -TARGET_EFAULT;
  2502. if (len < 0)
  2503. return -TARGET_EINVAL;
  2504. lv = sizeof(lv);
  2505. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2506. if (ret < 0)
  2507. return ret;
  2508. switch (optname) {
  2509. case SO_TYPE:
  2510. val = host_to_target_sock_type(val);
  2511. break;
  2512. case SO_ERROR:
  2513. val = host_to_target_errno(val);
  2514. break;
  2515. }
  2516. if (len > lv)
  2517. len = lv;
  2518. if (len == 4) {
  2519. if (put_user_u32(val, optval_addr))
  2520. return -TARGET_EFAULT;
  2521. } else {
  2522. if (put_user_u8(val, optval_addr))
  2523. return -TARGET_EFAULT;
  2524. }
  2525. if (put_user_u32(len, optlen))
  2526. return -TARGET_EFAULT;
  2527. break;
  2528. case SOL_IP:
  2529. switch(optname) {
  2530. case IP_TOS:
  2531. case IP_TTL:
  2532. case IP_HDRINCL:
  2533. case IP_ROUTER_ALERT:
  2534. case IP_RECVOPTS:
  2535. case IP_RETOPTS:
  2536. case IP_PKTINFO:
  2537. case IP_MTU_DISCOVER:
  2538. case IP_RECVERR:
  2539. case IP_RECVTOS:
  2540. #ifdef IP_FREEBIND
  2541. case IP_FREEBIND:
  2542. #endif
  2543. case IP_MULTICAST_TTL:
  2544. case IP_MULTICAST_LOOP:
  2545. if (get_user_u32(len, optlen))
  2546. return -TARGET_EFAULT;
  2547. if (len < 0)
  2548. return -TARGET_EINVAL;
  2549. lv = sizeof(lv);
  2550. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2551. if (ret < 0)
  2552. return ret;
  2553. if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
  2554. len = 1;
  2555. if (put_user_u32(len, optlen)
  2556. || put_user_u8(val, optval_addr))
  2557. return -TARGET_EFAULT;
  2558. } else {
  2559. if (len > sizeof(int))
  2560. len = sizeof(int);
  2561. if (put_user_u32(len, optlen)
  2562. || put_user_u32(val, optval_addr))
  2563. return -TARGET_EFAULT;
  2564. }
  2565. break;
  2566. default:
  2567. ret = -TARGET_ENOPROTOOPT;
  2568. break;
  2569. }
  2570. break;
  2571. case SOL_IPV6:
  2572. switch (optname) {
  2573. case IPV6_MTU_DISCOVER:
  2574. case IPV6_MTU:
  2575. case IPV6_V6ONLY:
  2576. case IPV6_RECVPKTINFO:
  2577. case IPV6_UNICAST_HOPS:
  2578. case IPV6_MULTICAST_HOPS:
  2579. case IPV6_MULTICAST_LOOP:
  2580. case IPV6_RECVERR:
  2581. case IPV6_RECVHOPLIMIT:
  2582. case IPV6_2292HOPLIMIT:
  2583. case IPV6_CHECKSUM:
  2584. case IPV6_ADDRFORM:
  2585. case IPV6_2292PKTINFO:
  2586. case IPV6_RECVTCLASS:
  2587. case IPV6_RECVRTHDR:
  2588. case IPV6_2292RTHDR:
  2589. case IPV6_RECVHOPOPTS:
  2590. case IPV6_2292HOPOPTS:
  2591. case IPV6_RECVDSTOPTS:
  2592. case IPV6_2292DSTOPTS:
  2593. case IPV6_TCLASS:
  2594. case IPV6_ADDR_PREFERENCES:
  2595. #ifdef IPV6_RECVPATHMTU
  2596. case IPV6_RECVPATHMTU:
  2597. #endif
  2598. #ifdef IPV6_TRANSPARENT
  2599. case IPV6_TRANSPARENT:
  2600. #endif
  2601. #ifdef IPV6_FREEBIND
  2602. case IPV6_FREEBIND:
  2603. #endif
  2604. #ifdef IPV6_RECVORIGDSTADDR
  2605. case IPV6_RECVORIGDSTADDR:
  2606. #endif
  2607. if (get_user_u32(len, optlen))
  2608. return -TARGET_EFAULT;
  2609. if (len < 0)
  2610. return -TARGET_EINVAL;
  2611. lv = sizeof(lv);
  2612. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2613. if (ret < 0)
  2614. return ret;
  2615. if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
  2616. len = 1;
  2617. if (put_user_u32(len, optlen)
  2618. || put_user_u8(val, optval_addr))
  2619. return -TARGET_EFAULT;
  2620. } else {
  2621. if (len > sizeof(int))
  2622. len = sizeof(int);
  2623. if (put_user_u32(len, optlen)
  2624. || put_user_u32(val, optval_addr))
  2625. return -TARGET_EFAULT;
  2626. }
  2627. break;
  2628. default:
  2629. ret = -TARGET_ENOPROTOOPT;
  2630. break;
  2631. }
  2632. break;
  2633. #ifdef SOL_NETLINK
  2634. case SOL_NETLINK:
  2635. switch (optname) {
  2636. case NETLINK_PKTINFO:
  2637. case NETLINK_BROADCAST_ERROR:
  2638. case NETLINK_NO_ENOBUFS:
  2639. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
  2640. case NETLINK_LISTEN_ALL_NSID:
  2641. case NETLINK_CAP_ACK:
  2642. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
  2643. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
  2644. case NETLINK_EXT_ACK:
  2645. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2646. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
  2647. case NETLINK_GET_STRICT_CHK:
  2648. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2649. if (get_user_u32(len, optlen)) {
  2650. return -TARGET_EFAULT;
  2651. }
  2652. if (len != sizeof(val)) {
  2653. return -TARGET_EINVAL;
  2654. }
  2655. lv = len;
  2656. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2657. if (ret < 0) {
  2658. return ret;
  2659. }
  2660. if (put_user_u32(lv, optlen)
  2661. || put_user_u32(val, optval_addr)) {
  2662. return -TARGET_EFAULT;
  2663. }
  2664. break;
  2665. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
  2666. case NETLINK_LIST_MEMBERSHIPS:
  2667. {
  2668. uint32_t *results;
  2669. int i;
  2670. if (get_user_u32(len, optlen)) {
  2671. return -TARGET_EFAULT;
  2672. }
  2673. if (len < 0) {
  2674. return -TARGET_EINVAL;
  2675. }
  2676. results = lock_user(VERIFY_WRITE, optval_addr, len, 1);
  2677. if (!results && len > 0) {
  2678. return -TARGET_EFAULT;
  2679. }
  2680. lv = len;
  2681. ret = get_errno(getsockopt(sockfd, level, optname, results, &lv));
  2682. if (ret < 0) {
  2683. unlock_user(results, optval_addr, 0);
  2684. return ret;
  2685. }
  2686. /* swap host endianness to target endianness. */
  2687. for (i = 0; i < (len / sizeof(uint32_t)); i++) {
  2688. results[i] = tswap32(results[i]);
  2689. }
  2690. if (put_user_u32(lv, optlen)) {
  2691. return -TARGET_EFAULT;
  2692. }
  2693. unlock_user(results, optval_addr, 0);
  2694. break;
  2695. }
  2696. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
  2697. default:
  2698. goto unimplemented;
  2699. }
  2700. break;
  2701. #endif /* SOL_NETLINK */
  2702. default:
  2703. unimplemented:
  2704. qemu_log_mask(LOG_UNIMP,
  2705. "getsockopt level=%d optname=%d not yet supported\n",
  2706. level, optname);
  2707. ret = -TARGET_EOPNOTSUPP;
  2708. break;
  2709. }
  2710. return ret;
  2711. }
  2712. /* Convert target low/high pair representing file offset into the host
  2713. * low/high pair. This function doesn't handle offsets bigger than 64 bits
  2714. * as the kernel doesn't handle them either.
  2715. */
  2716. static void target_to_host_low_high(abi_ulong tlow,
  2717. abi_ulong thigh,
  2718. unsigned long *hlow,
  2719. unsigned long *hhigh)
  2720. {
  2721. uint64_t off = tlow |
  2722. ((unsigned long long)thigh << TARGET_LONG_BITS / 2) <<
  2723. TARGET_LONG_BITS / 2;
  2724. *hlow = off;
  2725. *hhigh = (off >> HOST_LONG_BITS / 2) >> HOST_LONG_BITS / 2;
  2726. }
  2727. static struct iovec *lock_iovec(int type, abi_ulong target_addr,
  2728. abi_ulong count, int copy)
  2729. {
  2730. struct target_iovec *target_vec;
  2731. struct iovec *vec;
  2732. abi_ulong total_len, max_len;
  2733. int i;
  2734. int err = 0;
  2735. bool bad_address = false;
  2736. if (count == 0) {
  2737. errno = 0;
  2738. return NULL;
  2739. }
  2740. if (count > IOV_MAX) {
  2741. errno = EINVAL;
  2742. return NULL;
  2743. }
  2744. vec = g_try_new0(struct iovec, count);
  2745. if (vec == NULL) {
  2746. errno = ENOMEM;
  2747. return NULL;
  2748. }
  2749. target_vec = lock_user(VERIFY_READ, target_addr,
  2750. count * sizeof(struct target_iovec), 1);
  2751. if (target_vec == NULL) {
  2752. err = EFAULT;
  2753. goto fail2;
  2754. }
  2755. /* ??? If host page size > target page size, this will result in a
  2756. value larger than what we can actually support. */
  2757. max_len = 0x7fffffff & TARGET_PAGE_MASK;
  2758. total_len = 0;
  2759. for (i = 0; i < count; i++) {
  2760. abi_ulong base = tswapal(target_vec[i].iov_base);
  2761. abi_long len = tswapal(target_vec[i].iov_len);
  2762. if (len < 0) {
  2763. err = EINVAL;
  2764. goto fail;
  2765. } else if (len == 0) {
  2766. /* Zero length pointer is ignored. */
  2767. vec[i].iov_base = 0;
  2768. } else {
  2769. vec[i].iov_base = lock_user(type, base, len, copy);
  2770. /* If the first buffer pointer is bad, this is a fault. But
  2771. * subsequent bad buffers will result in a partial write; this
  2772. * is realized by filling the vector with null pointers and
  2773. * zero lengths. */
  2774. if (!vec[i].iov_base) {
  2775. if (i == 0) {
  2776. err = EFAULT;
  2777. goto fail;
  2778. } else {
  2779. bad_address = true;
  2780. }
  2781. }
  2782. if (bad_address) {
  2783. len = 0;
  2784. }
  2785. if (len > max_len - total_len) {
  2786. len = max_len - total_len;
  2787. }
  2788. }
  2789. vec[i].iov_len = len;
  2790. total_len += len;
  2791. }
  2792. unlock_user(target_vec, target_addr, 0);
  2793. return vec;
  2794. fail:
  2795. while (--i >= 0) {
  2796. if (tswapal(target_vec[i].iov_len) > 0) {
  2797. unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0);
  2798. }
  2799. }
  2800. unlock_user(target_vec, target_addr, 0);
  2801. fail2:
  2802. g_free(vec);
  2803. errno = err;
  2804. return NULL;
  2805. }
  2806. static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
  2807. abi_ulong count, int copy)
  2808. {
  2809. struct target_iovec *target_vec;
  2810. int i;
  2811. target_vec = lock_user(VERIFY_READ, target_addr,
  2812. count * sizeof(struct target_iovec), 1);
  2813. if (target_vec) {
  2814. for (i = 0; i < count; i++) {
  2815. abi_ulong base = tswapal(target_vec[i].iov_base);
  2816. abi_long len = tswapal(target_vec[i].iov_len);
  2817. if (len < 0) {
  2818. break;
  2819. }
  2820. unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
  2821. }
  2822. unlock_user(target_vec, target_addr, 0);
  2823. }
  2824. g_free(vec);
  2825. }
  2826. static inline int target_to_host_sock_type(int *type)
  2827. {
  2828. int host_type = 0;
  2829. int target_type = *type;
  2830. switch (target_type & TARGET_SOCK_TYPE_MASK) {
  2831. case TARGET_SOCK_DGRAM:
  2832. host_type = SOCK_DGRAM;
  2833. break;
  2834. case TARGET_SOCK_STREAM:
  2835. host_type = SOCK_STREAM;
  2836. break;
  2837. default:
  2838. host_type = target_type & TARGET_SOCK_TYPE_MASK;
  2839. break;
  2840. }
  2841. if (target_type & TARGET_SOCK_CLOEXEC) {
  2842. #if defined(SOCK_CLOEXEC)
  2843. host_type |= SOCK_CLOEXEC;
  2844. #else
  2845. return -TARGET_EINVAL;
  2846. #endif
  2847. }
  2848. if (target_type & TARGET_SOCK_NONBLOCK) {
  2849. #if defined(SOCK_NONBLOCK)
  2850. host_type |= SOCK_NONBLOCK;
  2851. #elif !defined(O_NONBLOCK)
  2852. return -TARGET_EINVAL;
  2853. #endif
  2854. }
  2855. *type = host_type;
  2856. return 0;
  2857. }
  2858. /* Try to emulate socket type flags after socket creation. */
  2859. static int sock_flags_fixup(int fd, int target_type)
  2860. {
  2861. #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
  2862. if (target_type & TARGET_SOCK_NONBLOCK) {
  2863. int flags = fcntl(fd, F_GETFL);
  2864. if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) {
  2865. close(fd);
  2866. return -TARGET_EINVAL;
  2867. }
  2868. }
  2869. #endif
  2870. return fd;
  2871. }
  2872. /* do_socket() Must return target values and target errnos. */
  2873. static abi_long do_socket(int domain, int type, int protocol)
  2874. {
  2875. int target_type = type;
  2876. int ret;
  2877. ret = target_to_host_sock_type(&type);
  2878. if (ret) {
  2879. return ret;
  2880. }
  2881. if (domain == PF_NETLINK && !(
  2882. #ifdef CONFIG_RTNETLINK
  2883. protocol == NETLINK_ROUTE ||
  2884. #endif
  2885. protocol == NETLINK_KOBJECT_UEVENT ||
  2886. protocol == NETLINK_AUDIT)) {
  2887. return -TARGET_EPROTONOSUPPORT;
  2888. }
  2889. if (domain == AF_PACKET ||
  2890. (domain == AF_INET && type == SOCK_PACKET)) {
  2891. protocol = tswap16(protocol);
  2892. }
  2893. ret = get_errno(socket(domain, type, protocol));
  2894. if (ret >= 0) {
  2895. ret = sock_flags_fixup(ret, target_type);
  2896. if (type == SOCK_PACKET) {
  2897. /* Manage an obsolete case :
  2898. * if socket type is SOCK_PACKET, bind by name
  2899. */
  2900. fd_trans_register(ret, &target_packet_trans);
  2901. } else if (domain == PF_NETLINK) {
  2902. switch (protocol) {
  2903. #ifdef CONFIG_RTNETLINK
  2904. case NETLINK_ROUTE:
  2905. fd_trans_register(ret, &target_netlink_route_trans);
  2906. break;
  2907. #endif
  2908. case NETLINK_KOBJECT_UEVENT:
  2909. /* nothing to do: messages are strings */
  2910. break;
  2911. case NETLINK_AUDIT:
  2912. fd_trans_register(ret, &target_netlink_audit_trans);
  2913. break;
  2914. default:
  2915. g_assert_not_reached();
  2916. }
  2917. }
  2918. }
  2919. return ret;
  2920. }
  2921. /* do_bind() Must return target values and target errnos. */
  2922. static abi_long do_bind(int sockfd, abi_ulong target_addr,
  2923. socklen_t addrlen)
  2924. {
  2925. void *addr;
  2926. abi_long ret;
  2927. if ((int)addrlen < 0) {
  2928. return -TARGET_EINVAL;
  2929. }
  2930. addr = alloca(addrlen+1);
  2931. ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
  2932. if (ret)
  2933. return ret;
  2934. return get_errno(bind(sockfd, addr, addrlen));
  2935. }
  2936. /* do_connect() Must return target values and target errnos. */
  2937. static abi_long do_connect(int sockfd, abi_ulong target_addr,
  2938. socklen_t addrlen)
  2939. {
  2940. void *addr;
  2941. abi_long ret;
  2942. if ((int)addrlen < 0) {
  2943. return -TARGET_EINVAL;
  2944. }
  2945. addr = alloca(addrlen+1);
  2946. ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
  2947. if (ret)
  2948. return ret;
  2949. return get_errno(safe_connect(sockfd, addr, addrlen));
  2950. }
  2951. /* do_sendrecvmsg_locked() Must return target values and target errnos. */
  2952. static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
  2953. int flags, int send)
  2954. {
  2955. abi_long ret, len;
  2956. struct msghdr msg;
  2957. abi_ulong count;
  2958. struct iovec *vec;
  2959. abi_ulong target_vec;
  2960. if (msgp->msg_name) {
  2961. msg.msg_namelen = tswap32(msgp->msg_namelen);
  2962. msg.msg_name = alloca(msg.msg_namelen+1);
  2963. ret = target_to_host_sockaddr(fd, msg.msg_name,
  2964. tswapal(msgp->msg_name),
  2965. msg.msg_namelen);
  2966. if (ret == -TARGET_EFAULT) {
  2967. /* For connected sockets msg_name and msg_namelen must
  2968. * be ignored, so returning EFAULT immediately is wrong.
  2969. * Instead, pass a bad msg_name to the host kernel, and
  2970. * let it decide whether to return EFAULT or not.
  2971. */
  2972. msg.msg_name = (void *)-1;
  2973. } else if (ret) {
  2974. goto out2;
  2975. }
  2976. } else {
  2977. msg.msg_name = NULL;
  2978. msg.msg_namelen = 0;
  2979. }
  2980. msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
  2981. msg.msg_control = alloca(msg.msg_controllen);
  2982. memset(msg.msg_control, 0, msg.msg_controllen);
  2983. msg.msg_flags = tswap32(msgp->msg_flags);
  2984. count = tswapal(msgp->msg_iovlen);
  2985. target_vec = tswapal(msgp->msg_iov);
  2986. if (count > IOV_MAX) {
  2987. /* sendrcvmsg returns a different errno for this condition than
  2988. * readv/writev, so we must catch it here before lock_iovec() does.
  2989. */
  2990. ret = -TARGET_EMSGSIZE;
  2991. goto out2;
  2992. }
  2993. vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
  2994. target_vec, count, send);
  2995. if (vec == NULL) {
  2996. ret = -host_to_target_errno(errno);
  2997. /* allow sending packet without any iov, e.g. with MSG_MORE flag */
  2998. if (!send || ret) {
  2999. goto out2;
  3000. }
  3001. }
  3002. msg.msg_iovlen = count;
  3003. msg.msg_iov = vec;
  3004. if (send) {
  3005. if (fd_trans_target_to_host_data(fd)) {
  3006. void *host_msg;
  3007. host_msg = g_malloc(msg.msg_iov->iov_len);
  3008. memcpy(host_msg, msg.msg_iov->iov_base, msg.msg_iov->iov_len);
  3009. ret = fd_trans_target_to_host_data(fd)(host_msg,
  3010. msg.msg_iov->iov_len);
  3011. if (ret >= 0) {
  3012. msg.msg_iov->iov_base = host_msg;
  3013. ret = get_errno(safe_sendmsg(fd, &msg, flags));
  3014. }
  3015. g_free(host_msg);
  3016. } else {
  3017. ret = target_to_host_cmsg(&msg, msgp);
  3018. if (ret == 0) {
  3019. ret = get_errno(safe_sendmsg(fd, &msg, flags));
  3020. }
  3021. }
  3022. } else {
  3023. ret = get_errno(safe_recvmsg(fd, &msg, flags));
  3024. if (!is_error(ret)) {
  3025. len = ret;
  3026. if (fd_trans_host_to_target_data(fd)) {
  3027. ret = fd_trans_host_to_target_data(fd)(msg.msg_iov->iov_base,
  3028. MIN(msg.msg_iov->iov_len, len));
  3029. }
  3030. if (!is_error(ret)) {
  3031. ret = host_to_target_cmsg(msgp, &msg);
  3032. }
  3033. if (!is_error(ret)) {
  3034. msgp->msg_namelen = tswap32(msg.msg_namelen);
  3035. msgp->msg_flags = tswap32(msg.msg_flags);
  3036. if (msg.msg_name != NULL && msg.msg_name != (void *)-1) {
  3037. ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
  3038. msg.msg_name, msg.msg_namelen);
  3039. if (ret) {
  3040. goto out;
  3041. }
  3042. }
  3043. ret = len;
  3044. }
  3045. }
  3046. }
  3047. out:
  3048. if (vec) {
  3049. unlock_iovec(vec, target_vec, count, !send);
  3050. }
  3051. out2:
  3052. return ret;
  3053. }
  3054. static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
  3055. int flags, int send)
  3056. {
  3057. abi_long ret;
  3058. struct target_msghdr *msgp;
  3059. if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
  3060. msgp,
  3061. target_msg,
  3062. send ? 1 : 0)) {
  3063. return -TARGET_EFAULT;
  3064. }
  3065. ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
  3066. unlock_user_struct(msgp, target_msg, send ? 0 : 1);
  3067. return ret;
  3068. }
  3069. /* We don't rely on the C library to have sendmmsg/recvmmsg support,
  3070. * so it might not have this *mmsg-specific flag either.
  3071. */
  3072. #ifndef MSG_WAITFORONE
  3073. #define MSG_WAITFORONE 0x10000
  3074. #endif
  3075. static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec,
  3076. unsigned int vlen, unsigned int flags,
  3077. int send)
  3078. {
  3079. struct target_mmsghdr *mmsgp;
  3080. abi_long ret = 0;
  3081. int i;
  3082. if (vlen > UIO_MAXIOV) {
  3083. vlen = UIO_MAXIOV;
  3084. }
  3085. mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1);
  3086. if (!mmsgp) {
  3087. return -TARGET_EFAULT;
  3088. }
  3089. for (i = 0; i < vlen; i++) {
  3090. ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send);
  3091. if (is_error(ret)) {
  3092. break;
  3093. }
  3094. mmsgp[i].msg_len = tswap32(ret);
  3095. /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
  3096. if (flags & MSG_WAITFORONE) {
  3097. flags |= MSG_DONTWAIT;
  3098. }
  3099. }
  3100. unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i);
  3101. /* Return number of datagrams sent if we sent any at all;
  3102. * otherwise return the error.
  3103. */
  3104. if (i) {
  3105. return i;
  3106. }
  3107. return ret;
  3108. }
  3109. /* do_accept4() Must return target values and target errnos. */
  3110. static abi_long do_accept4(int fd, abi_ulong target_addr,
  3111. abi_ulong target_addrlen_addr, int flags)
  3112. {
  3113. socklen_t addrlen, ret_addrlen;
  3114. void *addr;
  3115. abi_long ret;
  3116. int host_flags;
  3117. if (flags & ~(TARGET_SOCK_CLOEXEC | TARGET_SOCK_NONBLOCK)) {
  3118. return -TARGET_EINVAL;
  3119. }
  3120. host_flags = 0;
  3121. if (flags & TARGET_SOCK_NONBLOCK) {
  3122. host_flags |= SOCK_NONBLOCK;
  3123. }
  3124. if (flags & TARGET_SOCK_CLOEXEC) {
  3125. host_flags |= SOCK_CLOEXEC;
  3126. }
  3127. if (target_addr == 0) {
  3128. return get_errno(safe_accept4(fd, NULL, NULL, host_flags));
  3129. }
  3130. /* linux returns EFAULT if addrlen pointer is invalid */
  3131. if (get_user_u32(addrlen, target_addrlen_addr))
  3132. return -TARGET_EFAULT;
  3133. if ((int)addrlen < 0) {
  3134. return -TARGET_EINVAL;
  3135. }
  3136. if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
  3137. return -TARGET_EFAULT;
  3138. }
  3139. addr = alloca(addrlen);
  3140. ret_addrlen = addrlen;
  3141. ret = get_errno(safe_accept4(fd, addr, &ret_addrlen, host_flags));
  3142. if (!is_error(ret)) {
  3143. host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
  3144. if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
  3145. ret = -TARGET_EFAULT;
  3146. }
  3147. }
  3148. return ret;
  3149. }
  3150. /* do_getpeername() Must return target values and target errnos. */
  3151. static abi_long do_getpeername(int fd, abi_ulong target_addr,
  3152. abi_ulong target_addrlen_addr)
  3153. {
  3154. socklen_t addrlen, ret_addrlen;
  3155. void *addr;
  3156. abi_long ret;
  3157. if (get_user_u32(addrlen, target_addrlen_addr))
  3158. return -TARGET_EFAULT;
  3159. if ((int)addrlen < 0) {
  3160. return -TARGET_EINVAL;
  3161. }
  3162. if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
  3163. return -TARGET_EFAULT;
  3164. }
  3165. addr = alloca(addrlen);
  3166. ret_addrlen = addrlen;
  3167. ret = get_errno(getpeername(fd, addr, &ret_addrlen));
  3168. if (!is_error(ret)) {
  3169. host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
  3170. if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
  3171. ret = -TARGET_EFAULT;
  3172. }
  3173. }
  3174. return ret;
  3175. }
  3176. /* do_getsockname() Must return target values and target errnos. */
  3177. static abi_long do_getsockname(int fd, abi_ulong target_addr,
  3178. abi_ulong target_addrlen_addr)
  3179. {
  3180. socklen_t addrlen, ret_addrlen;
  3181. void *addr;
  3182. abi_long ret;
  3183. if (get_user_u32(addrlen, target_addrlen_addr))
  3184. return -TARGET_EFAULT;
  3185. if ((int)addrlen < 0) {
  3186. return -TARGET_EINVAL;
  3187. }
  3188. if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
  3189. return -TARGET_EFAULT;
  3190. }
  3191. addr = alloca(addrlen);
  3192. ret_addrlen = addrlen;
  3193. ret = get_errno(getsockname(fd, addr, &ret_addrlen));
  3194. if (!is_error(ret)) {
  3195. host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
  3196. if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
  3197. ret = -TARGET_EFAULT;
  3198. }
  3199. }
  3200. return ret;
  3201. }
  3202. /* do_socketpair() Must return target values and target errnos. */
  3203. static abi_long do_socketpair(int domain, int type, int protocol,
  3204. abi_ulong target_tab_addr)
  3205. {
  3206. int tab[2];
  3207. abi_long ret;
  3208. target_to_host_sock_type(&type);
  3209. ret = get_errno(socketpair(domain, type, protocol, tab));
  3210. if (!is_error(ret)) {
  3211. if (put_user_s32(tab[0], target_tab_addr)
  3212. || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
  3213. ret = -TARGET_EFAULT;
  3214. }
  3215. return ret;
  3216. }
  3217. /* do_sendto() Must return target values and target errnos. */
  3218. static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
  3219. abi_ulong target_addr, socklen_t addrlen)
  3220. {
  3221. void *addr;
  3222. void *host_msg;
  3223. void *copy_msg = NULL;
  3224. abi_long ret;
  3225. if ((int)addrlen < 0) {
  3226. return -TARGET_EINVAL;
  3227. }
  3228. host_msg = lock_user(VERIFY_READ, msg, len, 1);
  3229. if (!host_msg)
  3230. return -TARGET_EFAULT;
  3231. if (fd_trans_target_to_host_data(fd)) {
  3232. copy_msg = host_msg;
  3233. host_msg = g_malloc(len);
  3234. memcpy(host_msg, copy_msg, len);
  3235. ret = fd_trans_target_to_host_data(fd)(host_msg, len);
  3236. if (ret < 0) {
  3237. goto fail;
  3238. }
  3239. }
  3240. if (target_addr) {
  3241. addr = alloca(addrlen+1);
  3242. ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen);
  3243. if (ret) {
  3244. goto fail;
  3245. }
  3246. ret = get_errno(safe_sendto(fd, host_msg, len, flags, addr, addrlen));
  3247. } else {
  3248. ret = get_errno(safe_sendto(fd, host_msg, len, flags, NULL, 0));
  3249. }
  3250. fail:
  3251. if (copy_msg) {
  3252. g_free(host_msg);
  3253. host_msg = copy_msg;
  3254. }
  3255. unlock_user(host_msg, msg, 0);
  3256. return ret;
  3257. }
  3258. /* do_recvfrom() Must return target values and target errnos. */
  3259. static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
  3260. abi_ulong target_addr,
  3261. abi_ulong target_addrlen)
  3262. {
  3263. socklen_t addrlen, ret_addrlen;
  3264. void *addr;
  3265. void *host_msg;
  3266. abi_long ret;
  3267. if (!msg) {
  3268. host_msg = NULL;
  3269. } else {
  3270. host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
  3271. if (!host_msg) {
  3272. return -TARGET_EFAULT;
  3273. }
  3274. }
  3275. if (target_addr) {
  3276. if (get_user_u32(addrlen, target_addrlen)) {
  3277. ret = -TARGET_EFAULT;
  3278. goto fail;
  3279. }
  3280. if ((int)addrlen < 0) {
  3281. ret = -TARGET_EINVAL;
  3282. goto fail;
  3283. }
  3284. addr = alloca(addrlen);
  3285. ret_addrlen = addrlen;
  3286. ret = get_errno(safe_recvfrom(fd, host_msg, len, flags,
  3287. addr, &ret_addrlen));
  3288. } else {
  3289. addr = NULL; /* To keep compiler quiet. */
  3290. addrlen = 0; /* To keep compiler quiet. */
  3291. ret = get_errno(safe_recvfrom(fd, host_msg, len, flags, NULL, 0));
  3292. }
  3293. if (!is_error(ret)) {
  3294. if (fd_trans_host_to_target_data(fd)) {
  3295. abi_long trans;
  3296. trans = fd_trans_host_to_target_data(fd)(host_msg, MIN(ret, len));
  3297. if (is_error(trans)) {
  3298. ret = trans;
  3299. goto fail;
  3300. }
  3301. }
  3302. if (target_addr) {
  3303. host_to_target_sockaddr(target_addr, addr,
  3304. MIN(addrlen, ret_addrlen));
  3305. if (put_user_u32(ret_addrlen, target_addrlen)) {
  3306. ret = -TARGET_EFAULT;
  3307. goto fail;
  3308. }
  3309. }
  3310. unlock_user(host_msg, msg, len);
  3311. } else {
  3312. fail:
  3313. unlock_user(host_msg, msg, 0);
  3314. }
  3315. return ret;
  3316. }
  3317. #ifdef TARGET_NR_socketcall
  3318. /* do_socketcall() must return target values and target errnos. */
  3319. static abi_long do_socketcall(int num, abi_ulong vptr)
  3320. {
  3321. static const unsigned nargs[] = { /* number of arguments per operation */
  3322. [TARGET_SYS_SOCKET] = 3, /* domain, type, protocol */
  3323. [TARGET_SYS_BIND] = 3, /* fd, addr, addrlen */
  3324. [TARGET_SYS_CONNECT] = 3, /* fd, addr, addrlen */
  3325. [TARGET_SYS_LISTEN] = 2, /* fd, backlog */
  3326. [TARGET_SYS_ACCEPT] = 3, /* fd, addr, addrlen */
  3327. [TARGET_SYS_GETSOCKNAME] = 3, /* fd, addr, addrlen */
  3328. [TARGET_SYS_GETPEERNAME] = 3, /* fd, addr, addrlen */
  3329. [TARGET_SYS_SOCKETPAIR] = 4, /* domain, type, protocol, tab */
  3330. [TARGET_SYS_SEND] = 4, /* fd, msg, len, flags */
  3331. [TARGET_SYS_RECV] = 4, /* fd, msg, len, flags */
  3332. [TARGET_SYS_SENDTO] = 6, /* fd, msg, len, flags, addr, addrlen */
  3333. [TARGET_SYS_RECVFROM] = 6, /* fd, msg, len, flags, addr, addrlen */
  3334. [TARGET_SYS_SHUTDOWN] = 2, /* fd, how */
  3335. [TARGET_SYS_SETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
  3336. [TARGET_SYS_GETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
  3337. [TARGET_SYS_SENDMSG] = 3, /* fd, msg, flags */
  3338. [TARGET_SYS_RECVMSG] = 3, /* fd, msg, flags */
  3339. [TARGET_SYS_ACCEPT4] = 4, /* fd, addr, addrlen, flags */
  3340. [TARGET_SYS_RECVMMSG] = 4, /* fd, msgvec, vlen, flags */
  3341. [TARGET_SYS_SENDMMSG] = 4, /* fd, msgvec, vlen, flags */
  3342. };
  3343. abi_long a[6]; /* max 6 args */
  3344. unsigned i;
  3345. /* check the range of the first argument num */
  3346. /* (TARGET_SYS_SENDMMSG is the highest among TARGET_SYS_xxx) */
  3347. if (num < 1 || num > TARGET_SYS_SENDMMSG) {
  3348. return -TARGET_EINVAL;
  3349. }
  3350. /* ensure we have space for args */
  3351. if (nargs[num] > ARRAY_SIZE(a)) {
  3352. return -TARGET_EINVAL;
  3353. }
  3354. /* collect the arguments in a[] according to nargs[] */
  3355. for (i = 0; i < nargs[num]; ++i) {
  3356. if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) {
  3357. return -TARGET_EFAULT;
  3358. }
  3359. }
  3360. /* now when we have the args, invoke the appropriate underlying function */
  3361. switch (num) {
  3362. case TARGET_SYS_SOCKET: /* domain, type, protocol */
  3363. return do_socket(a[0], a[1], a[2]);
  3364. case TARGET_SYS_BIND: /* sockfd, addr, addrlen */
  3365. return do_bind(a[0], a[1], a[2]);
  3366. case TARGET_SYS_CONNECT: /* sockfd, addr, addrlen */
  3367. return do_connect(a[0], a[1], a[2]);
  3368. case TARGET_SYS_LISTEN: /* sockfd, backlog */
  3369. return get_errno(listen(a[0], a[1]));
  3370. case TARGET_SYS_ACCEPT: /* sockfd, addr, addrlen */
  3371. return do_accept4(a[0], a[1], a[2], 0);
  3372. case TARGET_SYS_GETSOCKNAME: /* sockfd, addr, addrlen */
  3373. return do_getsockname(a[0], a[1], a[2]);
  3374. case TARGET_SYS_GETPEERNAME: /* sockfd, addr, addrlen */
  3375. return do_getpeername(a[0], a[1], a[2]);
  3376. case TARGET_SYS_SOCKETPAIR: /* domain, type, protocol, tab */
  3377. return do_socketpair(a[0], a[1], a[2], a[3]);
  3378. case TARGET_SYS_SEND: /* sockfd, msg, len, flags */
  3379. return do_sendto(a[0], a[1], a[2], a[3], 0, 0);
  3380. case TARGET_SYS_RECV: /* sockfd, msg, len, flags */
  3381. return do_recvfrom(a[0], a[1], a[2], a[3], 0, 0);
  3382. case TARGET_SYS_SENDTO: /* sockfd, msg, len, flags, addr, addrlen */
  3383. return do_sendto(a[0], a[1], a[2], a[3], a[4], a[5]);
  3384. case TARGET_SYS_RECVFROM: /* sockfd, msg, len, flags, addr, addrlen */
  3385. return do_recvfrom(a[0], a[1], a[2], a[3], a[4], a[5]);
  3386. case TARGET_SYS_SHUTDOWN: /* sockfd, how */
  3387. return get_errno(shutdown(a[0], a[1]));
  3388. case TARGET_SYS_SETSOCKOPT: /* sockfd, level, optname, optval, optlen */
  3389. return do_setsockopt(a[0], a[1], a[2], a[3], a[4]);
  3390. case TARGET_SYS_GETSOCKOPT: /* sockfd, level, optname, optval, optlen */
  3391. return do_getsockopt(a[0], a[1], a[2], a[3], a[4]);
  3392. case TARGET_SYS_SENDMSG: /* sockfd, msg, flags */
  3393. return do_sendrecvmsg(a[0], a[1], a[2], 1);
  3394. case TARGET_SYS_RECVMSG: /* sockfd, msg, flags */
  3395. return do_sendrecvmsg(a[0], a[1], a[2], 0);
  3396. case TARGET_SYS_ACCEPT4: /* sockfd, addr, addrlen, flags */
  3397. return do_accept4(a[0], a[1], a[2], a[3]);
  3398. case TARGET_SYS_RECVMMSG: /* sockfd, msgvec, vlen, flags */
  3399. return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 0);
  3400. case TARGET_SYS_SENDMMSG: /* sockfd, msgvec, vlen, flags */
  3401. return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 1);
  3402. default:
  3403. qemu_log_mask(LOG_UNIMP, "Unsupported socketcall: %d\n", num);
  3404. return -TARGET_EINVAL;
  3405. }
  3406. }
  3407. #endif
  3408. #ifndef TARGET_SEMID64_DS
  3409. /* asm-generic version of this struct */
  3410. struct target_semid64_ds
  3411. {
  3412. struct target_ipc_perm sem_perm;
  3413. abi_ulong sem_otime;
  3414. #if TARGET_ABI_BITS == 32
  3415. abi_ulong __unused1;
  3416. #endif
  3417. abi_ulong sem_ctime;
  3418. #if TARGET_ABI_BITS == 32
  3419. abi_ulong __unused2;
  3420. #endif
  3421. abi_ulong sem_nsems;
  3422. abi_ulong __unused3;
  3423. abi_ulong __unused4;
  3424. };
  3425. #endif
  3426. static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
  3427. abi_ulong target_addr)
  3428. {
  3429. struct target_ipc_perm *target_ip;
  3430. struct target_semid64_ds *target_sd;
  3431. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  3432. return -TARGET_EFAULT;
  3433. target_ip = &(target_sd->sem_perm);
  3434. host_ip->__key = tswap32(target_ip->__key);
  3435. host_ip->uid = tswap32(target_ip->uid);
  3436. host_ip->gid = tswap32(target_ip->gid);
  3437. host_ip->cuid = tswap32(target_ip->cuid);
  3438. host_ip->cgid = tswap32(target_ip->cgid);
  3439. #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
  3440. host_ip->mode = tswap32(target_ip->mode);
  3441. #else
  3442. host_ip->mode = tswap16(target_ip->mode);
  3443. #endif
  3444. #if defined(TARGET_PPC)
  3445. host_ip->__seq = tswap32(target_ip->__seq);
  3446. #else
  3447. host_ip->__seq = tswap16(target_ip->__seq);
  3448. #endif
  3449. unlock_user_struct(target_sd, target_addr, 0);
  3450. return 0;
  3451. }
  3452. static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
  3453. struct ipc_perm *host_ip)
  3454. {
  3455. struct target_ipc_perm *target_ip;
  3456. struct target_semid64_ds *target_sd;
  3457. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  3458. return -TARGET_EFAULT;
  3459. target_ip = &(target_sd->sem_perm);
  3460. target_ip->__key = tswap32(host_ip->__key);
  3461. target_ip->uid = tswap32(host_ip->uid);
  3462. target_ip->gid = tswap32(host_ip->gid);
  3463. target_ip->cuid = tswap32(host_ip->cuid);
  3464. target_ip->cgid = tswap32(host_ip->cgid);
  3465. #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
  3466. target_ip->mode = tswap32(host_ip->mode);
  3467. #else
  3468. target_ip->mode = tswap16(host_ip->mode);
  3469. #endif
  3470. #if defined(TARGET_PPC)
  3471. target_ip->__seq = tswap32(host_ip->__seq);
  3472. #else
  3473. target_ip->__seq = tswap16(host_ip->__seq);
  3474. #endif
  3475. unlock_user_struct(target_sd, target_addr, 1);
  3476. return 0;
  3477. }
  3478. static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
  3479. abi_ulong target_addr)
  3480. {
  3481. struct target_semid64_ds *target_sd;
  3482. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  3483. return -TARGET_EFAULT;
  3484. if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
  3485. return -TARGET_EFAULT;
  3486. host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
  3487. host_sd->sem_otime = tswapal(target_sd->sem_otime);
  3488. host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
  3489. unlock_user_struct(target_sd, target_addr, 0);
  3490. return 0;
  3491. }
  3492. static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
  3493. struct semid_ds *host_sd)
  3494. {
  3495. struct target_semid64_ds *target_sd;
  3496. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  3497. return -TARGET_EFAULT;
  3498. if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
  3499. return -TARGET_EFAULT;
  3500. target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
  3501. target_sd->sem_otime = tswapal(host_sd->sem_otime);
  3502. target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
  3503. unlock_user_struct(target_sd, target_addr, 1);
  3504. return 0;
  3505. }
  3506. struct target_seminfo {
  3507. int semmap;
  3508. int semmni;
  3509. int semmns;
  3510. int semmnu;
  3511. int semmsl;
  3512. int semopm;
  3513. int semume;
  3514. int semusz;
  3515. int semvmx;
  3516. int semaem;
  3517. };
  3518. static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
  3519. struct seminfo *host_seminfo)
  3520. {
  3521. struct target_seminfo *target_seminfo;
  3522. if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
  3523. return -TARGET_EFAULT;
  3524. __put_user(host_seminfo->semmap, &target_seminfo->semmap);
  3525. __put_user(host_seminfo->semmni, &target_seminfo->semmni);
  3526. __put_user(host_seminfo->semmns, &target_seminfo->semmns);
  3527. __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
  3528. __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
  3529. __put_user(host_seminfo->semopm, &target_seminfo->semopm);
  3530. __put_user(host_seminfo->semume, &target_seminfo->semume);
  3531. __put_user(host_seminfo->semusz, &target_seminfo->semusz);
  3532. __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
  3533. __put_user(host_seminfo->semaem, &target_seminfo->semaem);
  3534. unlock_user_struct(target_seminfo, target_addr, 1);
  3535. return 0;
  3536. }
  3537. union semun {
  3538. int val;
  3539. struct semid_ds *buf;
  3540. unsigned short *array;
  3541. struct seminfo *__buf;
  3542. };
  3543. union target_semun {
  3544. int val;
  3545. abi_ulong buf;
  3546. abi_ulong array;
  3547. abi_ulong __buf;
  3548. };
  3549. static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
  3550. abi_ulong target_addr)
  3551. {
  3552. int nsems;
  3553. unsigned short *array;
  3554. union semun semun;
  3555. struct semid_ds semid_ds;
  3556. int i, ret;
  3557. semun.buf = &semid_ds;
  3558. ret = semctl(semid, 0, IPC_STAT, semun);
  3559. if (ret == -1)
  3560. return get_errno(ret);
  3561. nsems = semid_ds.sem_nsems;
  3562. *host_array = g_try_new(unsigned short, nsems);
  3563. if (!*host_array) {
  3564. return -TARGET_ENOMEM;
  3565. }
  3566. array = lock_user(VERIFY_READ, target_addr,
  3567. nsems*sizeof(unsigned short), 1);
  3568. if (!array) {
  3569. g_free(*host_array);
  3570. return -TARGET_EFAULT;
  3571. }
  3572. for(i=0; i<nsems; i++) {
  3573. __get_user((*host_array)[i], &array[i]);
  3574. }
  3575. unlock_user(array, target_addr, 0);
  3576. return 0;
  3577. }
  3578. static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
  3579. unsigned short **host_array)
  3580. {
  3581. int nsems;
  3582. unsigned short *array;
  3583. union semun semun;
  3584. struct semid_ds semid_ds;
  3585. int i, ret;
  3586. semun.buf = &semid_ds;
  3587. ret = semctl(semid, 0, IPC_STAT, semun);
  3588. if (ret == -1)
  3589. return get_errno(ret);
  3590. nsems = semid_ds.sem_nsems;
  3591. array = lock_user(VERIFY_WRITE, target_addr,
  3592. nsems*sizeof(unsigned short), 0);
  3593. if (!array)
  3594. return -TARGET_EFAULT;
  3595. for(i=0; i<nsems; i++) {
  3596. __put_user((*host_array)[i], &array[i]);
  3597. }
  3598. g_free(*host_array);
  3599. unlock_user(array, target_addr, 1);
  3600. return 0;
  3601. }
  3602. static inline abi_long do_semctl(int semid, int semnum, int cmd,
  3603. abi_ulong target_arg)
  3604. {
  3605. union target_semun target_su = { .buf = target_arg };
  3606. union semun arg;
  3607. struct semid_ds dsarg;
  3608. unsigned short *array = NULL;
  3609. struct seminfo seminfo;
  3610. abi_long ret = -TARGET_EINVAL;
  3611. abi_long err;
  3612. cmd &= 0xff;
  3613. switch( cmd ) {
  3614. case GETVAL:
  3615. case SETVAL:
  3616. /* In 64 bit cross-endian situations, we will erroneously pick up
  3617. * the wrong half of the union for the "val" element. To rectify
  3618. * this, the entire 8-byte structure is byteswapped, followed by
  3619. * a swap of the 4 byte val field. In other cases, the data is
  3620. * already in proper host byte order. */
  3621. if (sizeof(target_su.val) != (sizeof(target_su.buf))) {
  3622. target_su.buf = tswapal(target_su.buf);
  3623. arg.val = tswap32(target_su.val);
  3624. } else {
  3625. arg.val = target_su.val;
  3626. }
  3627. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3628. break;
  3629. case GETALL:
  3630. case SETALL:
  3631. err = target_to_host_semarray(semid, &array, target_su.array);
  3632. if (err)
  3633. return err;
  3634. arg.array = array;
  3635. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3636. err = host_to_target_semarray(semid, target_su.array, &array);
  3637. if (err)
  3638. return err;
  3639. break;
  3640. case IPC_STAT:
  3641. case IPC_SET:
  3642. case SEM_STAT:
  3643. err = target_to_host_semid_ds(&dsarg, target_su.buf);
  3644. if (err)
  3645. return err;
  3646. arg.buf = &dsarg;
  3647. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3648. err = host_to_target_semid_ds(target_su.buf, &dsarg);
  3649. if (err)
  3650. return err;
  3651. break;
  3652. case IPC_INFO:
  3653. case SEM_INFO:
  3654. arg.__buf = &seminfo;
  3655. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3656. err = host_to_target_seminfo(target_su.__buf, &seminfo);
  3657. if (err)
  3658. return err;
  3659. break;
  3660. case IPC_RMID:
  3661. case GETPID:
  3662. case GETNCNT:
  3663. case GETZCNT:
  3664. ret = get_errno(semctl(semid, semnum, cmd, NULL));
  3665. break;
  3666. }
  3667. return ret;
  3668. }
  3669. struct target_sembuf {
  3670. unsigned short sem_num;
  3671. short sem_op;
  3672. short sem_flg;
  3673. };
  3674. static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
  3675. abi_ulong target_addr,
  3676. unsigned nsops)
  3677. {
  3678. struct target_sembuf *target_sembuf;
  3679. int i;
  3680. target_sembuf = lock_user(VERIFY_READ, target_addr,
  3681. nsops*sizeof(struct target_sembuf), 1);
  3682. if (!target_sembuf)
  3683. return -TARGET_EFAULT;
  3684. for(i=0; i<nsops; i++) {
  3685. __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
  3686. __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
  3687. __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
  3688. }
  3689. unlock_user(target_sembuf, target_addr, 0);
  3690. return 0;
  3691. }
  3692. #if defined(TARGET_NR_ipc) || defined(TARGET_NR_semop) || \
  3693. defined(TARGET_NR_semtimedop) || defined(TARGET_NR_semtimedop_time64)
  3694. /*
  3695. * This macro is required to handle the s390 variants, which passes the
  3696. * arguments in a different order than default.
  3697. */
  3698. #ifdef __s390x__
  3699. #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
  3700. (__nsops), (__timeout), (__sops)
  3701. #else
  3702. #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
  3703. (__nsops), 0, (__sops), (__timeout)
  3704. #endif
  3705. static inline abi_long do_semtimedop(int semid,
  3706. abi_long ptr,
  3707. unsigned nsops,
  3708. abi_long timeout, bool time64)
  3709. {
  3710. struct sembuf *sops;
  3711. struct timespec ts, *pts = NULL;
  3712. abi_long ret;
  3713. if (timeout) {
  3714. pts = &ts;
  3715. if (time64) {
  3716. if (target_to_host_timespec64(pts, timeout)) {
  3717. return -TARGET_EFAULT;
  3718. }
  3719. } else {
  3720. if (target_to_host_timespec(pts, timeout)) {
  3721. return -TARGET_EFAULT;
  3722. }
  3723. }
  3724. }
  3725. if (nsops > TARGET_SEMOPM) {
  3726. return -TARGET_E2BIG;
  3727. }
  3728. sops = g_new(struct sembuf, nsops);
  3729. if (target_to_host_sembuf(sops, ptr, nsops)) {
  3730. g_free(sops);
  3731. return -TARGET_EFAULT;
  3732. }
  3733. ret = -TARGET_ENOSYS;
  3734. #ifdef __NR_semtimedop
  3735. ret = get_errno(safe_semtimedop(semid, sops, nsops, pts));
  3736. #endif
  3737. #ifdef __NR_ipc
  3738. if (ret == -TARGET_ENOSYS) {
  3739. ret = get_errno(safe_ipc(IPCOP_semtimedop, semid,
  3740. SEMTIMEDOP_IPC_ARGS(nsops, sops, (long)pts)));
  3741. }
  3742. #endif
  3743. g_free(sops);
  3744. return ret;
  3745. }
  3746. #endif
  3747. struct target_msqid_ds
  3748. {
  3749. struct target_ipc_perm msg_perm;
  3750. abi_ulong msg_stime;
  3751. #if TARGET_ABI_BITS == 32
  3752. abi_ulong __unused1;
  3753. #endif
  3754. abi_ulong msg_rtime;
  3755. #if TARGET_ABI_BITS == 32
  3756. abi_ulong __unused2;
  3757. #endif
  3758. abi_ulong msg_ctime;
  3759. #if TARGET_ABI_BITS == 32
  3760. abi_ulong __unused3;
  3761. #endif
  3762. abi_ulong __msg_cbytes;
  3763. abi_ulong msg_qnum;
  3764. abi_ulong msg_qbytes;
  3765. abi_ulong msg_lspid;
  3766. abi_ulong msg_lrpid;
  3767. abi_ulong __unused4;
  3768. abi_ulong __unused5;
  3769. };
  3770. static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
  3771. abi_ulong target_addr)
  3772. {
  3773. struct target_msqid_ds *target_md;
  3774. if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
  3775. return -TARGET_EFAULT;
  3776. if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
  3777. return -TARGET_EFAULT;
  3778. host_md->msg_stime = tswapal(target_md->msg_stime);
  3779. host_md->msg_rtime = tswapal(target_md->msg_rtime);
  3780. host_md->msg_ctime = tswapal(target_md->msg_ctime);
  3781. host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
  3782. host_md->msg_qnum = tswapal(target_md->msg_qnum);
  3783. host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
  3784. host_md->msg_lspid = tswapal(target_md->msg_lspid);
  3785. host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
  3786. unlock_user_struct(target_md, target_addr, 0);
  3787. return 0;
  3788. }
  3789. static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
  3790. struct msqid_ds *host_md)
  3791. {
  3792. struct target_msqid_ds *target_md;
  3793. if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
  3794. return -TARGET_EFAULT;
  3795. if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
  3796. return -TARGET_EFAULT;
  3797. target_md->msg_stime = tswapal(host_md->msg_stime);
  3798. target_md->msg_rtime = tswapal(host_md->msg_rtime);
  3799. target_md->msg_ctime = tswapal(host_md->msg_ctime);
  3800. target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
  3801. target_md->msg_qnum = tswapal(host_md->msg_qnum);
  3802. target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
  3803. target_md->msg_lspid = tswapal(host_md->msg_lspid);
  3804. target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
  3805. unlock_user_struct(target_md, target_addr, 1);
  3806. return 0;
  3807. }
  3808. struct target_msginfo {
  3809. int msgpool;
  3810. int msgmap;
  3811. int msgmax;
  3812. int msgmnb;
  3813. int msgmni;
  3814. int msgssz;
  3815. int msgtql;
  3816. unsigned short int msgseg;
  3817. };
  3818. static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
  3819. struct msginfo *host_msginfo)
  3820. {
  3821. struct target_msginfo *target_msginfo;
  3822. if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
  3823. return -TARGET_EFAULT;
  3824. __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
  3825. __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
  3826. __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
  3827. __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
  3828. __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
  3829. __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
  3830. __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
  3831. __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
  3832. unlock_user_struct(target_msginfo, target_addr, 1);
  3833. return 0;
  3834. }
  3835. static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
  3836. {
  3837. struct msqid_ds dsarg;
  3838. struct msginfo msginfo;
  3839. abi_long ret = -TARGET_EINVAL;
  3840. cmd &= 0xff;
  3841. switch (cmd) {
  3842. case IPC_STAT:
  3843. case IPC_SET:
  3844. case MSG_STAT:
  3845. if (target_to_host_msqid_ds(&dsarg,ptr))
  3846. return -TARGET_EFAULT;
  3847. ret = get_errno(msgctl(msgid, cmd, &dsarg));
  3848. if (host_to_target_msqid_ds(ptr,&dsarg))
  3849. return -TARGET_EFAULT;
  3850. break;
  3851. case IPC_RMID:
  3852. ret = get_errno(msgctl(msgid, cmd, NULL));
  3853. break;
  3854. case IPC_INFO:
  3855. case MSG_INFO:
  3856. ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
  3857. if (host_to_target_msginfo(ptr, &msginfo))
  3858. return -TARGET_EFAULT;
  3859. break;
  3860. }
  3861. return ret;
  3862. }
  3863. struct target_msgbuf {
  3864. abi_long mtype;
  3865. char mtext[1];
  3866. };
  3867. static inline abi_long do_msgsnd(int msqid, abi_long msgp,
  3868. ssize_t msgsz, int msgflg)
  3869. {
  3870. struct target_msgbuf *target_mb;
  3871. struct msgbuf *host_mb;
  3872. abi_long ret = 0;
  3873. if (msgsz < 0) {
  3874. return -TARGET_EINVAL;
  3875. }
  3876. if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
  3877. return -TARGET_EFAULT;
  3878. host_mb = g_try_malloc(msgsz + sizeof(long));
  3879. if (!host_mb) {
  3880. unlock_user_struct(target_mb, msgp, 0);
  3881. return -TARGET_ENOMEM;
  3882. }
  3883. host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
  3884. memcpy(host_mb->mtext, target_mb->mtext, msgsz);
  3885. ret = -TARGET_ENOSYS;
  3886. #ifdef __NR_msgsnd
  3887. ret = get_errno(safe_msgsnd(msqid, host_mb, msgsz, msgflg));
  3888. #endif
  3889. #ifdef __NR_ipc
  3890. if (ret == -TARGET_ENOSYS) {
  3891. #ifdef __s390x__
  3892. ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
  3893. host_mb));
  3894. #else
  3895. ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
  3896. host_mb, 0));
  3897. #endif
  3898. }
  3899. #endif
  3900. g_free(host_mb);
  3901. unlock_user_struct(target_mb, msgp, 0);
  3902. return ret;
  3903. }
  3904. #ifdef __NR_ipc
  3905. #if defined(__sparc__)
  3906. /* SPARC for msgrcv it does not use the kludge on final 2 arguments. */
  3907. #define MSGRCV_ARGS(__msgp, __msgtyp) __msgp, __msgtyp
  3908. #elif defined(__s390x__)
  3909. /* The s390 sys_ipc variant has only five parameters. */
  3910. #define MSGRCV_ARGS(__msgp, __msgtyp) \
  3911. ((long int[]){(long int)__msgp, __msgtyp})
  3912. #else
  3913. #define MSGRCV_ARGS(__msgp, __msgtyp) \
  3914. ((long int[]){(long int)__msgp, __msgtyp}), 0
  3915. #endif
  3916. #endif
  3917. static inline abi_long do_msgrcv(int msqid, abi_long msgp,
  3918. ssize_t msgsz, abi_long msgtyp,
  3919. int msgflg)
  3920. {
  3921. struct target_msgbuf *target_mb;
  3922. char *target_mtext;
  3923. struct msgbuf *host_mb;
  3924. abi_long ret = 0;
  3925. if (msgsz < 0) {
  3926. return -TARGET_EINVAL;
  3927. }
  3928. if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
  3929. return -TARGET_EFAULT;
  3930. host_mb = g_try_malloc(msgsz + sizeof(long));
  3931. if (!host_mb) {
  3932. ret = -TARGET_ENOMEM;
  3933. goto end;
  3934. }
  3935. ret = -TARGET_ENOSYS;
  3936. #ifdef __NR_msgrcv
  3937. ret = get_errno(safe_msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
  3938. #endif
  3939. #ifdef __NR_ipc
  3940. if (ret == -TARGET_ENOSYS) {
  3941. ret = get_errno(safe_ipc(IPCOP_CALL(1, IPCOP_msgrcv), msqid, msgsz,
  3942. msgflg, MSGRCV_ARGS(host_mb, msgtyp)));
  3943. }
  3944. #endif
  3945. if (ret > 0) {
  3946. abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
  3947. target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
  3948. if (!target_mtext) {
  3949. ret = -TARGET_EFAULT;
  3950. goto end;
  3951. }
  3952. memcpy(target_mb->mtext, host_mb->mtext, ret);
  3953. unlock_user(target_mtext, target_mtext_addr, ret);
  3954. }
  3955. target_mb->mtype = tswapal(host_mb->mtype);
  3956. end:
  3957. if (target_mb)
  3958. unlock_user_struct(target_mb, msgp, 1);
  3959. g_free(host_mb);
  3960. return ret;
  3961. }
  3962. static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
  3963. abi_ulong target_addr)
  3964. {
  3965. struct target_shmid_ds *target_sd;
  3966. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  3967. return -TARGET_EFAULT;
  3968. if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
  3969. return -TARGET_EFAULT;
  3970. __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
  3971. __get_user(host_sd->shm_atime, &target_sd->shm_atime);
  3972. __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
  3973. __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
  3974. __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
  3975. __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
  3976. __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
  3977. unlock_user_struct(target_sd, target_addr, 0);
  3978. return 0;
  3979. }
  3980. static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
  3981. struct shmid_ds *host_sd)
  3982. {
  3983. struct target_shmid_ds *target_sd;
  3984. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  3985. return -TARGET_EFAULT;
  3986. if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
  3987. return -TARGET_EFAULT;
  3988. __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
  3989. __put_user(host_sd->shm_atime, &target_sd->shm_atime);
  3990. __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
  3991. __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
  3992. __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
  3993. __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
  3994. __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
  3995. unlock_user_struct(target_sd, target_addr, 1);
  3996. return 0;
  3997. }
  3998. struct target_shminfo {
  3999. abi_ulong shmmax;
  4000. abi_ulong shmmin;
  4001. abi_ulong shmmni;
  4002. abi_ulong shmseg;
  4003. abi_ulong shmall;
  4004. };
  4005. static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
  4006. struct shminfo *host_shminfo)
  4007. {
  4008. struct target_shminfo *target_shminfo;
  4009. if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
  4010. return -TARGET_EFAULT;
  4011. __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
  4012. __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
  4013. __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
  4014. __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
  4015. __put_user(host_shminfo->shmall, &target_shminfo->shmall);
  4016. unlock_user_struct(target_shminfo, target_addr, 1);
  4017. return 0;
  4018. }
  4019. struct target_shm_info {
  4020. int used_ids;
  4021. abi_ulong shm_tot;
  4022. abi_ulong shm_rss;
  4023. abi_ulong shm_swp;
  4024. abi_ulong swap_attempts;
  4025. abi_ulong swap_successes;
  4026. };
  4027. static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
  4028. struct shm_info *host_shm_info)
  4029. {
  4030. struct target_shm_info *target_shm_info;
  4031. if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
  4032. return -TARGET_EFAULT;
  4033. __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
  4034. __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
  4035. __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
  4036. __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
  4037. __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
  4038. __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
  4039. unlock_user_struct(target_shm_info, target_addr, 1);
  4040. return 0;
  4041. }
  4042. static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
  4043. {
  4044. struct shmid_ds dsarg;
  4045. struct shminfo shminfo;
  4046. struct shm_info shm_info;
  4047. abi_long ret = -TARGET_EINVAL;
  4048. cmd &= 0xff;
  4049. switch(cmd) {
  4050. case IPC_STAT:
  4051. case IPC_SET:
  4052. case SHM_STAT:
  4053. if (target_to_host_shmid_ds(&dsarg, buf))
  4054. return -TARGET_EFAULT;
  4055. ret = get_errno(shmctl(shmid, cmd, &dsarg));
  4056. if (host_to_target_shmid_ds(buf, &dsarg))
  4057. return -TARGET_EFAULT;
  4058. break;
  4059. case IPC_INFO:
  4060. ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
  4061. if (host_to_target_shminfo(buf, &shminfo))
  4062. return -TARGET_EFAULT;
  4063. break;
  4064. case SHM_INFO:
  4065. ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
  4066. if (host_to_target_shm_info(buf, &shm_info))
  4067. return -TARGET_EFAULT;
  4068. break;
  4069. case IPC_RMID:
  4070. case SHM_LOCK:
  4071. case SHM_UNLOCK:
  4072. ret = get_errno(shmctl(shmid, cmd, NULL));
  4073. break;
  4074. }
  4075. return ret;
  4076. }
  4077. #ifdef TARGET_NR_ipc
  4078. /* ??? This only works with linear mappings. */
  4079. /* do_ipc() must return target values and target errnos. */
  4080. static abi_long do_ipc(CPUArchState *cpu_env,
  4081. unsigned int call, abi_long first,
  4082. abi_long second, abi_long third,
  4083. abi_long ptr, abi_long fifth)
  4084. {
  4085. int version;
  4086. abi_long ret = 0;
  4087. version = call >> 16;
  4088. call &= 0xffff;
  4089. switch (call) {
  4090. case IPCOP_semop:
  4091. ret = do_semtimedop(first, ptr, second, 0, false);
  4092. break;
  4093. case IPCOP_semtimedop:
  4094. /*
  4095. * The s390 sys_ipc variant has only five parameters instead of six
  4096. * (as for default variant) and the only difference is the handling of
  4097. * SEMTIMEDOP where on s390 the third parameter is used as a pointer
  4098. * to a struct timespec where the generic variant uses fifth parameter.
  4099. */
  4100. #if defined(TARGET_S390X)
  4101. ret = do_semtimedop(first, ptr, second, third, TARGET_ABI_BITS == 64);
  4102. #else
  4103. ret = do_semtimedop(first, ptr, second, fifth, TARGET_ABI_BITS == 64);
  4104. #endif
  4105. break;
  4106. case IPCOP_semget:
  4107. ret = get_errno(semget(first, second, third));
  4108. break;
  4109. case IPCOP_semctl: {
  4110. /* The semun argument to semctl is passed by value, so dereference the
  4111. * ptr argument. */
  4112. abi_ulong atptr;
  4113. get_user_ual(atptr, ptr);
  4114. ret = do_semctl(first, second, third, atptr);
  4115. break;
  4116. }
  4117. case IPCOP_msgget:
  4118. ret = get_errno(msgget(first, second));
  4119. break;
  4120. case IPCOP_msgsnd:
  4121. ret = do_msgsnd(first, ptr, second, third);
  4122. break;
  4123. case IPCOP_msgctl:
  4124. ret = do_msgctl(first, second, ptr);
  4125. break;
  4126. case IPCOP_msgrcv:
  4127. switch (version) {
  4128. case 0:
  4129. {
  4130. struct target_ipc_kludge {
  4131. abi_long msgp;
  4132. abi_long msgtyp;
  4133. } *tmp;
  4134. if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
  4135. ret = -TARGET_EFAULT;
  4136. break;
  4137. }
  4138. ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
  4139. unlock_user_struct(tmp, ptr, 0);
  4140. break;
  4141. }
  4142. default:
  4143. ret = do_msgrcv(first, ptr, second, fifth, third);
  4144. }
  4145. break;
  4146. case IPCOP_shmat:
  4147. switch (version) {
  4148. default:
  4149. {
  4150. abi_ulong raddr;
  4151. raddr = target_shmat(cpu_env, first, ptr, second);
  4152. if (is_error(raddr))
  4153. return get_errno(raddr);
  4154. if (put_user_ual(raddr, third))
  4155. return -TARGET_EFAULT;
  4156. break;
  4157. }
  4158. case 1:
  4159. ret = -TARGET_EINVAL;
  4160. break;
  4161. }
  4162. break;
  4163. case IPCOP_shmdt:
  4164. ret = target_shmdt(ptr);
  4165. break;
  4166. case IPCOP_shmget:
  4167. /* IPC_* flag values are the same on all linux platforms */
  4168. ret = get_errno(shmget(first, second, third));
  4169. break;
  4170. /* IPC_* and SHM_* command values are the same on all linux platforms */
  4171. case IPCOP_shmctl:
  4172. ret = do_shmctl(first, second, ptr);
  4173. break;
  4174. default:
  4175. qemu_log_mask(LOG_UNIMP, "Unsupported ipc call: %d (version %d)\n",
  4176. call, version);
  4177. ret = -TARGET_ENOSYS;
  4178. break;
  4179. }
  4180. return ret;
  4181. }
  4182. #endif
  4183. /* kernel structure types definitions */
  4184. #define STRUCT(name, ...) STRUCT_ ## name,
  4185. #define STRUCT_SPECIAL(name) STRUCT_ ## name,
  4186. enum {
  4187. #include "syscall_types.h"
  4188. STRUCT_MAX
  4189. };
  4190. #undef STRUCT
  4191. #undef STRUCT_SPECIAL
  4192. #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
  4193. #define STRUCT_SPECIAL(name)
  4194. #include "syscall_types.h"
  4195. #undef STRUCT
  4196. #undef STRUCT_SPECIAL
  4197. #define MAX_STRUCT_SIZE 4096
  4198. #ifdef CONFIG_FIEMAP
  4199. /* So fiemap access checks don't overflow on 32 bit systems.
  4200. * This is very slightly smaller than the limit imposed by
  4201. * the underlying kernel.
  4202. */
  4203. #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
  4204. / sizeof(struct fiemap_extent))
  4205. static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
  4206. int fd, int cmd, abi_long arg)
  4207. {
  4208. /* The parameter for this ioctl is a struct fiemap followed
  4209. * by an array of struct fiemap_extent whose size is set
  4210. * in fiemap->fm_extent_count. The array is filled in by the
  4211. * ioctl.
  4212. */
  4213. int target_size_in, target_size_out;
  4214. struct fiemap *fm;
  4215. const argtype *arg_type = ie->arg_type;
  4216. const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
  4217. void *argptr, *p;
  4218. abi_long ret;
  4219. int i, extent_size = thunk_type_size(extent_arg_type, 0);
  4220. uint32_t outbufsz;
  4221. int free_fm = 0;
  4222. assert(arg_type[0] == TYPE_PTR);
  4223. assert(ie->access == IOC_RW);
  4224. arg_type++;
  4225. target_size_in = thunk_type_size(arg_type, 0);
  4226. argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
  4227. if (!argptr) {
  4228. return -TARGET_EFAULT;
  4229. }
  4230. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4231. unlock_user(argptr, arg, 0);
  4232. fm = (struct fiemap *)buf_temp;
  4233. if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
  4234. return -TARGET_EINVAL;
  4235. }
  4236. outbufsz = sizeof (*fm) +
  4237. (sizeof(struct fiemap_extent) * fm->fm_extent_count);
  4238. if (outbufsz > MAX_STRUCT_SIZE) {
  4239. /* We can't fit all the extents into the fixed size buffer.
  4240. * Allocate one that is large enough and use it instead.
  4241. */
  4242. fm = g_try_malloc(outbufsz);
  4243. if (!fm) {
  4244. return -TARGET_ENOMEM;
  4245. }
  4246. memcpy(fm, buf_temp, sizeof(struct fiemap));
  4247. free_fm = 1;
  4248. }
  4249. ret = get_errno(safe_ioctl(fd, ie->host_cmd, fm));
  4250. if (!is_error(ret)) {
  4251. target_size_out = target_size_in;
  4252. /* An extent_count of 0 means we were only counting the extents
  4253. * so there are no structs to copy
  4254. */
  4255. if (fm->fm_extent_count != 0) {
  4256. target_size_out += fm->fm_mapped_extents * extent_size;
  4257. }
  4258. argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
  4259. if (!argptr) {
  4260. ret = -TARGET_EFAULT;
  4261. } else {
  4262. /* Convert the struct fiemap */
  4263. thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
  4264. if (fm->fm_extent_count != 0) {
  4265. p = argptr + target_size_in;
  4266. /* ...and then all the struct fiemap_extents */
  4267. for (i = 0; i < fm->fm_mapped_extents; i++) {
  4268. thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
  4269. THUNK_TARGET);
  4270. p += extent_size;
  4271. }
  4272. }
  4273. unlock_user(argptr, arg, target_size_out);
  4274. }
  4275. }
  4276. if (free_fm) {
  4277. g_free(fm);
  4278. }
  4279. return ret;
  4280. }
  4281. #endif
  4282. static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
  4283. int fd, int cmd, abi_long arg)
  4284. {
  4285. const argtype *arg_type = ie->arg_type;
  4286. int target_size;
  4287. void *argptr;
  4288. int ret;
  4289. struct ifconf *host_ifconf;
  4290. uint32_t outbufsz;
  4291. const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
  4292. const argtype ifreq_max_type[] = { MK_STRUCT(STRUCT_ifmap_ifreq) };
  4293. int target_ifreq_size;
  4294. int nb_ifreq;
  4295. int free_buf = 0;
  4296. int i;
  4297. int target_ifc_len;
  4298. abi_long target_ifc_buf;
  4299. int host_ifc_len;
  4300. char *host_ifc_buf;
  4301. assert(arg_type[0] == TYPE_PTR);
  4302. assert(ie->access == IOC_RW);
  4303. arg_type++;
  4304. target_size = thunk_type_size(arg_type, 0);
  4305. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4306. if (!argptr)
  4307. return -TARGET_EFAULT;
  4308. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4309. unlock_user(argptr, arg, 0);
  4310. host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
  4311. target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
  4312. target_ifreq_size = thunk_type_size(ifreq_max_type, 0);
  4313. if (target_ifc_buf != 0) {
  4314. target_ifc_len = host_ifconf->ifc_len;
  4315. nb_ifreq = target_ifc_len / target_ifreq_size;
  4316. host_ifc_len = nb_ifreq * sizeof(struct ifreq);
  4317. outbufsz = sizeof(*host_ifconf) + host_ifc_len;
  4318. if (outbufsz > MAX_STRUCT_SIZE) {
  4319. /*
  4320. * We can't fit all the extents into the fixed size buffer.
  4321. * Allocate one that is large enough and use it instead.
  4322. */
  4323. host_ifconf = g_try_malloc(outbufsz);
  4324. if (!host_ifconf) {
  4325. return -TARGET_ENOMEM;
  4326. }
  4327. memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
  4328. free_buf = 1;
  4329. }
  4330. host_ifc_buf = (char *)host_ifconf + sizeof(*host_ifconf);
  4331. host_ifconf->ifc_len = host_ifc_len;
  4332. } else {
  4333. host_ifc_buf = NULL;
  4334. }
  4335. host_ifconf->ifc_buf = host_ifc_buf;
  4336. ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_ifconf));
  4337. if (!is_error(ret)) {
  4338. /* convert host ifc_len to target ifc_len */
  4339. nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
  4340. target_ifc_len = nb_ifreq * target_ifreq_size;
  4341. host_ifconf->ifc_len = target_ifc_len;
  4342. /* restore target ifc_buf */
  4343. host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
  4344. /* copy struct ifconf to target user */
  4345. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4346. if (!argptr)
  4347. return -TARGET_EFAULT;
  4348. thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
  4349. unlock_user(argptr, arg, target_size);
  4350. if (target_ifc_buf != 0) {
  4351. /* copy ifreq[] to target user */
  4352. argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
  4353. for (i = 0; i < nb_ifreq ; i++) {
  4354. thunk_convert(argptr + i * target_ifreq_size,
  4355. host_ifc_buf + i * sizeof(struct ifreq),
  4356. ifreq_arg_type, THUNK_TARGET);
  4357. }
  4358. unlock_user(argptr, target_ifc_buf, target_ifc_len);
  4359. }
  4360. }
  4361. if (free_buf) {
  4362. g_free(host_ifconf);
  4363. }
  4364. return ret;
  4365. }
  4366. #if defined(CONFIG_USBFS)
  4367. #if HOST_LONG_BITS > 64
  4368. #error USBDEVFS thunks do not support >64 bit hosts yet.
  4369. #endif
  4370. struct live_urb {
  4371. uint64_t target_urb_adr;
  4372. uint64_t target_buf_adr;
  4373. char *target_buf_ptr;
  4374. struct usbdevfs_urb host_urb;
  4375. };
  4376. static GHashTable *usbdevfs_urb_hashtable(void)
  4377. {
  4378. static GHashTable *urb_hashtable;
  4379. if (!urb_hashtable) {
  4380. urb_hashtable = g_hash_table_new(g_int64_hash, g_int64_equal);
  4381. }
  4382. return urb_hashtable;
  4383. }
  4384. static void urb_hashtable_insert(struct live_urb *urb)
  4385. {
  4386. GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
  4387. g_hash_table_insert(urb_hashtable, urb, urb);
  4388. }
  4389. static struct live_urb *urb_hashtable_lookup(uint64_t target_urb_adr)
  4390. {
  4391. GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
  4392. return g_hash_table_lookup(urb_hashtable, &target_urb_adr);
  4393. }
  4394. static void urb_hashtable_remove(struct live_urb *urb)
  4395. {
  4396. GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
  4397. g_hash_table_remove(urb_hashtable, urb);
  4398. }
  4399. static abi_long
  4400. do_ioctl_usbdevfs_reapurb(const IOCTLEntry *ie, uint8_t *buf_temp,
  4401. int fd, int cmd, abi_long arg)
  4402. {
  4403. const argtype usbfsurb_arg_type[] = { MK_STRUCT(STRUCT_usbdevfs_urb) };
  4404. const argtype ptrvoid_arg_type[] = { TYPE_PTRVOID, 0, 0 };
  4405. struct live_urb *lurb;
  4406. void *argptr;
  4407. uint64_t hurb;
  4408. int target_size;
  4409. uintptr_t target_urb_adr;
  4410. abi_long ret;
  4411. target_size = thunk_type_size(usbfsurb_arg_type, THUNK_TARGET);
  4412. memset(buf_temp, 0, sizeof(uint64_t));
  4413. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4414. if (is_error(ret)) {
  4415. return ret;
  4416. }
  4417. memcpy(&hurb, buf_temp, sizeof(uint64_t));
  4418. lurb = (void *)((uintptr_t)hurb - offsetof(struct live_urb, host_urb));
  4419. if (!lurb->target_urb_adr) {
  4420. return -TARGET_EFAULT;
  4421. }
  4422. urb_hashtable_remove(lurb);
  4423. unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr,
  4424. lurb->host_urb.buffer_length);
  4425. lurb->target_buf_ptr = NULL;
  4426. /* restore the guest buffer pointer */
  4427. lurb->host_urb.buffer = (void *)(uintptr_t)lurb->target_buf_adr;
  4428. /* update the guest urb struct */
  4429. argptr = lock_user(VERIFY_WRITE, lurb->target_urb_adr, target_size, 0);
  4430. if (!argptr) {
  4431. g_free(lurb);
  4432. return -TARGET_EFAULT;
  4433. }
  4434. thunk_convert(argptr, &lurb->host_urb, usbfsurb_arg_type, THUNK_TARGET);
  4435. unlock_user(argptr, lurb->target_urb_adr, target_size);
  4436. target_size = thunk_type_size(ptrvoid_arg_type, THUNK_TARGET);
  4437. /* write back the urb handle */
  4438. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4439. if (!argptr) {
  4440. g_free(lurb);
  4441. return -TARGET_EFAULT;
  4442. }
  4443. /* GHashTable uses 64-bit keys but thunk_convert expects uintptr_t */
  4444. target_urb_adr = lurb->target_urb_adr;
  4445. thunk_convert(argptr, &target_urb_adr, ptrvoid_arg_type, THUNK_TARGET);
  4446. unlock_user(argptr, arg, target_size);
  4447. g_free(lurb);
  4448. return ret;
  4449. }
  4450. static abi_long
  4451. do_ioctl_usbdevfs_discardurb(const IOCTLEntry *ie,
  4452. uint8_t *buf_temp __attribute__((unused)),
  4453. int fd, int cmd, abi_long arg)
  4454. {
  4455. struct live_urb *lurb;
  4456. /* map target address back to host URB with metadata. */
  4457. lurb = urb_hashtable_lookup(arg);
  4458. if (!lurb) {
  4459. return -TARGET_EFAULT;
  4460. }
  4461. return get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb));
  4462. }
  4463. static abi_long
  4464. do_ioctl_usbdevfs_submiturb(const IOCTLEntry *ie, uint8_t *buf_temp,
  4465. int fd, int cmd, abi_long arg)
  4466. {
  4467. const argtype *arg_type = ie->arg_type;
  4468. int target_size;
  4469. abi_long ret;
  4470. void *argptr;
  4471. int rw_dir;
  4472. struct live_urb *lurb;
  4473. /*
  4474. * each submitted URB needs to map to a unique ID for the
  4475. * kernel, and that unique ID needs to be a pointer to
  4476. * host memory. hence, we need to malloc for each URB.
  4477. * isochronous transfers have a variable length struct.
  4478. */
  4479. arg_type++;
  4480. target_size = thunk_type_size(arg_type, THUNK_TARGET);
  4481. /* construct host copy of urb and metadata */
  4482. lurb = g_try_new0(struct live_urb, 1);
  4483. if (!lurb) {
  4484. return -TARGET_ENOMEM;
  4485. }
  4486. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4487. if (!argptr) {
  4488. g_free(lurb);
  4489. return -TARGET_EFAULT;
  4490. }
  4491. thunk_convert(&lurb->host_urb, argptr, arg_type, THUNK_HOST);
  4492. unlock_user(argptr, arg, 0);
  4493. lurb->target_urb_adr = arg;
  4494. lurb->target_buf_adr = (uintptr_t)lurb->host_urb.buffer;
  4495. /* buffer space used depends on endpoint type so lock the entire buffer */
  4496. /* control type urbs should check the buffer contents for true direction */
  4497. rw_dir = lurb->host_urb.endpoint & USB_DIR_IN ? VERIFY_WRITE : VERIFY_READ;
  4498. lurb->target_buf_ptr = lock_user(rw_dir, lurb->target_buf_adr,
  4499. lurb->host_urb.buffer_length, 1);
  4500. if (lurb->target_buf_ptr == NULL) {
  4501. g_free(lurb);
  4502. return -TARGET_EFAULT;
  4503. }
  4504. /* update buffer pointer in host copy */
  4505. lurb->host_urb.buffer = lurb->target_buf_ptr;
  4506. ret = get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb));
  4507. if (is_error(ret)) {
  4508. unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr, 0);
  4509. g_free(lurb);
  4510. } else {
  4511. urb_hashtable_insert(lurb);
  4512. }
  4513. return ret;
  4514. }
  4515. #endif /* CONFIG_USBFS */
  4516. static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
  4517. int cmd, abi_long arg)
  4518. {
  4519. void *argptr;
  4520. struct dm_ioctl *host_dm;
  4521. abi_long guest_data;
  4522. uint32_t guest_data_size;
  4523. int target_size;
  4524. const argtype *arg_type = ie->arg_type;
  4525. abi_long ret;
  4526. void *big_buf = NULL;
  4527. char *host_data;
  4528. arg_type++;
  4529. target_size = thunk_type_size(arg_type, 0);
  4530. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4531. if (!argptr) {
  4532. ret = -TARGET_EFAULT;
  4533. goto out;
  4534. }
  4535. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4536. unlock_user(argptr, arg, 0);
  4537. /* buf_temp is too small, so fetch things into a bigger buffer */
  4538. big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
  4539. memcpy(big_buf, buf_temp, target_size);
  4540. buf_temp = big_buf;
  4541. host_dm = big_buf;
  4542. guest_data = arg + host_dm->data_start;
  4543. if ((guest_data - arg) < 0) {
  4544. ret = -TARGET_EINVAL;
  4545. goto out;
  4546. }
  4547. guest_data_size = host_dm->data_size - host_dm->data_start;
  4548. host_data = (char*)host_dm + host_dm->data_start;
  4549. argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
  4550. if (!argptr) {
  4551. ret = -TARGET_EFAULT;
  4552. goto out;
  4553. }
  4554. switch (ie->host_cmd) {
  4555. case DM_REMOVE_ALL:
  4556. case DM_LIST_DEVICES:
  4557. case DM_DEV_CREATE:
  4558. case DM_DEV_REMOVE:
  4559. case DM_DEV_SUSPEND:
  4560. case DM_DEV_STATUS:
  4561. case DM_DEV_WAIT:
  4562. case DM_TABLE_STATUS:
  4563. case DM_TABLE_CLEAR:
  4564. case DM_TABLE_DEPS:
  4565. case DM_LIST_VERSIONS:
  4566. /* no input data */
  4567. break;
  4568. case DM_DEV_RENAME:
  4569. case DM_DEV_SET_GEOMETRY:
  4570. /* data contains only strings */
  4571. memcpy(host_data, argptr, guest_data_size);
  4572. break;
  4573. case DM_TARGET_MSG:
  4574. memcpy(host_data, argptr, guest_data_size);
  4575. *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
  4576. break;
  4577. case DM_TABLE_LOAD:
  4578. {
  4579. void *gspec = argptr;
  4580. void *cur_data = host_data;
  4581. const argtype dm_arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
  4582. int spec_size = thunk_type_size(dm_arg_type, 0);
  4583. int i;
  4584. for (i = 0; i < host_dm->target_count; i++) {
  4585. struct dm_target_spec *spec = cur_data;
  4586. uint32_t next;
  4587. int slen;
  4588. thunk_convert(spec, gspec, dm_arg_type, THUNK_HOST);
  4589. slen = strlen((char*)gspec + spec_size) + 1;
  4590. next = spec->next;
  4591. spec->next = sizeof(*spec) + slen;
  4592. strcpy((char*)&spec[1], gspec + spec_size);
  4593. gspec += next;
  4594. cur_data += spec->next;
  4595. }
  4596. break;
  4597. }
  4598. default:
  4599. ret = -TARGET_EINVAL;
  4600. unlock_user(argptr, guest_data, 0);
  4601. goto out;
  4602. }
  4603. unlock_user(argptr, guest_data, 0);
  4604. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4605. if (!is_error(ret)) {
  4606. guest_data = arg + host_dm->data_start;
  4607. guest_data_size = host_dm->data_size - host_dm->data_start;
  4608. argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
  4609. switch (ie->host_cmd) {
  4610. case DM_REMOVE_ALL:
  4611. case DM_DEV_CREATE:
  4612. case DM_DEV_REMOVE:
  4613. case DM_DEV_RENAME:
  4614. case DM_DEV_SUSPEND:
  4615. case DM_DEV_STATUS:
  4616. case DM_TABLE_LOAD:
  4617. case DM_TABLE_CLEAR:
  4618. case DM_TARGET_MSG:
  4619. case DM_DEV_SET_GEOMETRY:
  4620. /* no return data */
  4621. break;
  4622. case DM_LIST_DEVICES:
  4623. {
  4624. struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
  4625. uint32_t remaining_data = guest_data_size;
  4626. void *cur_data = argptr;
  4627. const argtype dm_arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
  4628. int nl_size = 12; /* can't use thunk_size due to alignment */
  4629. while (1) {
  4630. uint32_t next = nl->next;
  4631. if (next) {
  4632. nl->next = nl_size + (strlen(nl->name) + 1);
  4633. }
  4634. if (remaining_data < nl->next) {
  4635. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  4636. break;
  4637. }
  4638. thunk_convert(cur_data, nl, dm_arg_type, THUNK_TARGET);
  4639. strcpy(cur_data + nl_size, nl->name);
  4640. cur_data += nl->next;
  4641. remaining_data -= nl->next;
  4642. if (!next) {
  4643. break;
  4644. }
  4645. nl = (void*)nl + next;
  4646. }
  4647. break;
  4648. }
  4649. case DM_DEV_WAIT:
  4650. case DM_TABLE_STATUS:
  4651. {
  4652. struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
  4653. void *cur_data = argptr;
  4654. const argtype dm_arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
  4655. int spec_size = thunk_type_size(dm_arg_type, 0);
  4656. int i;
  4657. for (i = 0; i < host_dm->target_count; i++) {
  4658. uint32_t next = spec->next;
  4659. int slen = strlen((char*)&spec[1]) + 1;
  4660. spec->next = (cur_data - argptr) + spec_size + slen;
  4661. if (guest_data_size < spec->next) {
  4662. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  4663. break;
  4664. }
  4665. thunk_convert(cur_data, spec, dm_arg_type, THUNK_TARGET);
  4666. strcpy(cur_data + spec_size, (char*)&spec[1]);
  4667. cur_data = argptr + spec->next;
  4668. spec = (void*)host_dm + host_dm->data_start + next;
  4669. }
  4670. break;
  4671. }
  4672. case DM_TABLE_DEPS:
  4673. {
  4674. void *hdata = (void*)host_dm + host_dm->data_start;
  4675. int count = *(uint32_t*)hdata;
  4676. uint64_t *hdev = hdata + 8;
  4677. uint64_t *gdev = argptr + 8;
  4678. int i;
  4679. *(uint32_t*)argptr = tswap32(count);
  4680. for (i = 0; i < count; i++) {
  4681. *gdev = tswap64(*hdev);
  4682. gdev++;
  4683. hdev++;
  4684. }
  4685. break;
  4686. }
  4687. case DM_LIST_VERSIONS:
  4688. {
  4689. struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
  4690. uint32_t remaining_data = guest_data_size;
  4691. void *cur_data = argptr;
  4692. const argtype dm_arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
  4693. int vers_size = thunk_type_size(dm_arg_type, 0);
  4694. while (1) {
  4695. uint32_t next = vers->next;
  4696. if (next) {
  4697. vers->next = vers_size + (strlen(vers->name) + 1);
  4698. }
  4699. if (remaining_data < vers->next) {
  4700. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  4701. break;
  4702. }
  4703. thunk_convert(cur_data, vers, dm_arg_type, THUNK_TARGET);
  4704. strcpy(cur_data + vers_size, vers->name);
  4705. cur_data += vers->next;
  4706. remaining_data -= vers->next;
  4707. if (!next) {
  4708. break;
  4709. }
  4710. vers = (void*)vers + next;
  4711. }
  4712. break;
  4713. }
  4714. default:
  4715. unlock_user(argptr, guest_data, 0);
  4716. ret = -TARGET_EINVAL;
  4717. goto out;
  4718. }
  4719. unlock_user(argptr, guest_data, guest_data_size);
  4720. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4721. if (!argptr) {
  4722. ret = -TARGET_EFAULT;
  4723. goto out;
  4724. }
  4725. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  4726. unlock_user(argptr, arg, target_size);
  4727. }
  4728. out:
  4729. g_free(big_buf);
  4730. return ret;
  4731. }
  4732. static abi_long do_ioctl_blkpg(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
  4733. int cmd, abi_long arg)
  4734. {
  4735. void *argptr;
  4736. int target_size;
  4737. const argtype *arg_type = ie->arg_type;
  4738. const argtype part_arg_type[] = { MK_STRUCT(STRUCT_blkpg_partition) };
  4739. abi_long ret;
  4740. struct blkpg_ioctl_arg *host_blkpg = (void*)buf_temp;
  4741. struct blkpg_partition host_part;
  4742. /* Read and convert blkpg */
  4743. arg_type++;
  4744. target_size = thunk_type_size(arg_type, 0);
  4745. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4746. if (!argptr) {
  4747. ret = -TARGET_EFAULT;
  4748. goto out;
  4749. }
  4750. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4751. unlock_user(argptr, arg, 0);
  4752. switch (host_blkpg->op) {
  4753. case BLKPG_ADD_PARTITION:
  4754. case BLKPG_DEL_PARTITION:
  4755. /* payload is struct blkpg_partition */
  4756. break;
  4757. default:
  4758. /* Unknown opcode */
  4759. ret = -TARGET_EINVAL;
  4760. goto out;
  4761. }
  4762. /* Read and convert blkpg->data */
  4763. arg = (abi_long)(uintptr_t)host_blkpg->data;
  4764. target_size = thunk_type_size(part_arg_type, 0);
  4765. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4766. if (!argptr) {
  4767. ret = -TARGET_EFAULT;
  4768. goto out;
  4769. }
  4770. thunk_convert(&host_part, argptr, part_arg_type, THUNK_HOST);
  4771. unlock_user(argptr, arg, 0);
  4772. /* Swizzle the data pointer to our local copy and call! */
  4773. host_blkpg->data = &host_part;
  4774. ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_blkpg));
  4775. out:
  4776. return ret;
  4777. }
  4778. static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
  4779. int fd, int cmd, abi_long arg)
  4780. {
  4781. const argtype *arg_type = ie->arg_type;
  4782. const StructEntry *se;
  4783. const argtype *field_types;
  4784. const int *dst_offsets, *src_offsets;
  4785. int target_size;
  4786. void *argptr;
  4787. abi_ulong *target_rt_dev_ptr = NULL;
  4788. unsigned long *host_rt_dev_ptr = NULL;
  4789. abi_long ret;
  4790. int i;
  4791. assert(ie->access == IOC_W);
  4792. assert(*arg_type == TYPE_PTR);
  4793. arg_type++;
  4794. assert(*arg_type == TYPE_STRUCT);
  4795. target_size = thunk_type_size(arg_type, 0);
  4796. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4797. if (!argptr) {
  4798. return -TARGET_EFAULT;
  4799. }
  4800. arg_type++;
  4801. assert(*arg_type == (int)STRUCT_rtentry);
  4802. se = struct_entries + *arg_type++;
  4803. assert(se->convert[0] == NULL);
  4804. /* convert struct here to be able to catch rt_dev string */
  4805. field_types = se->field_types;
  4806. dst_offsets = se->field_offsets[THUNK_HOST];
  4807. src_offsets = se->field_offsets[THUNK_TARGET];
  4808. for (i = 0; i < se->nb_fields; i++) {
  4809. if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) {
  4810. assert(*field_types == TYPE_PTRVOID);
  4811. target_rt_dev_ptr = argptr + src_offsets[i];
  4812. host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]);
  4813. if (*target_rt_dev_ptr != 0) {
  4814. *host_rt_dev_ptr = (unsigned long)lock_user_string(
  4815. tswapal(*target_rt_dev_ptr));
  4816. if (!*host_rt_dev_ptr) {
  4817. unlock_user(argptr, arg, 0);
  4818. return -TARGET_EFAULT;
  4819. }
  4820. } else {
  4821. *host_rt_dev_ptr = 0;
  4822. }
  4823. field_types++;
  4824. continue;
  4825. }
  4826. field_types = thunk_convert(buf_temp + dst_offsets[i],
  4827. argptr + src_offsets[i],
  4828. field_types, THUNK_HOST);
  4829. }
  4830. unlock_user(argptr, arg, 0);
  4831. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4832. assert(host_rt_dev_ptr != NULL);
  4833. assert(target_rt_dev_ptr != NULL);
  4834. if (*host_rt_dev_ptr != 0) {
  4835. unlock_user((void *)*host_rt_dev_ptr,
  4836. *target_rt_dev_ptr, 0);
  4837. }
  4838. return ret;
  4839. }
  4840. static abi_long do_ioctl_kdsigaccept(const IOCTLEntry *ie, uint8_t *buf_temp,
  4841. int fd, int cmd, abi_long arg)
  4842. {
  4843. int sig = target_to_host_signal(arg);
  4844. return get_errno(safe_ioctl(fd, ie->host_cmd, sig));
  4845. }
  4846. static abi_long do_ioctl_SIOCGSTAMP(const IOCTLEntry *ie, uint8_t *buf_temp,
  4847. int fd, int cmd, abi_long arg)
  4848. {
  4849. struct timeval tv;
  4850. abi_long ret;
  4851. ret = get_errno(safe_ioctl(fd, SIOCGSTAMP, &tv));
  4852. if (is_error(ret)) {
  4853. return ret;
  4854. }
  4855. if (cmd == (int)TARGET_SIOCGSTAMP_OLD) {
  4856. if (copy_to_user_timeval(arg, &tv)) {
  4857. return -TARGET_EFAULT;
  4858. }
  4859. } else {
  4860. if (copy_to_user_timeval64(arg, &tv)) {
  4861. return -TARGET_EFAULT;
  4862. }
  4863. }
  4864. return ret;
  4865. }
  4866. static abi_long do_ioctl_SIOCGSTAMPNS(const IOCTLEntry *ie, uint8_t *buf_temp,
  4867. int fd, int cmd, abi_long arg)
  4868. {
  4869. struct timespec ts;
  4870. abi_long ret;
  4871. ret = get_errno(safe_ioctl(fd, SIOCGSTAMPNS, &ts));
  4872. if (is_error(ret)) {
  4873. return ret;
  4874. }
  4875. if (cmd == (int)TARGET_SIOCGSTAMPNS_OLD) {
  4876. if (host_to_target_timespec(arg, &ts)) {
  4877. return -TARGET_EFAULT;
  4878. }
  4879. } else{
  4880. if (host_to_target_timespec64(arg, &ts)) {
  4881. return -TARGET_EFAULT;
  4882. }
  4883. }
  4884. return ret;
  4885. }
  4886. #ifdef TIOCGPTPEER
  4887. static abi_long do_ioctl_tiocgptpeer(const IOCTLEntry *ie, uint8_t *buf_temp,
  4888. int fd, int cmd, abi_long arg)
  4889. {
  4890. int flags = target_to_host_bitmask(arg, fcntl_flags_tbl);
  4891. return get_errno(safe_ioctl(fd, ie->host_cmd, flags));
  4892. }
  4893. #endif
  4894. #ifdef HAVE_DRM_H
  4895. static void unlock_drm_version(struct drm_version *host_ver,
  4896. struct target_drm_version *target_ver,
  4897. bool copy)
  4898. {
  4899. unlock_user(host_ver->name, target_ver->name,
  4900. copy ? host_ver->name_len : 0);
  4901. unlock_user(host_ver->date, target_ver->date,
  4902. copy ? host_ver->date_len : 0);
  4903. unlock_user(host_ver->desc, target_ver->desc,
  4904. copy ? host_ver->desc_len : 0);
  4905. }
  4906. static inline abi_long target_to_host_drmversion(struct drm_version *host_ver,
  4907. struct target_drm_version *target_ver)
  4908. {
  4909. memset(host_ver, 0, sizeof(*host_ver));
  4910. __get_user(host_ver->name_len, &target_ver->name_len);
  4911. if (host_ver->name_len) {
  4912. host_ver->name = lock_user(VERIFY_WRITE, target_ver->name,
  4913. target_ver->name_len, 0);
  4914. if (!host_ver->name) {
  4915. return -EFAULT;
  4916. }
  4917. }
  4918. __get_user(host_ver->date_len, &target_ver->date_len);
  4919. if (host_ver->date_len) {
  4920. host_ver->date = lock_user(VERIFY_WRITE, target_ver->date,
  4921. target_ver->date_len, 0);
  4922. if (!host_ver->date) {
  4923. goto err;
  4924. }
  4925. }
  4926. __get_user(host_ver->desc_len, &target_ver->desc_len);
  4927. if (host_ver->desc_len) {
  4928. host_ver->desc = lock_user(VERIFY_WRITE, target_ver->desc,
  4929. target_ver->desc_len, 0);
  4930. if (!host_ver->desc) {
  4931. goto err;
  4932. }
  4933. }
  4934. return 0;
  4935. err:
  4936. unlock_drm_version(host_ver, target_ver, false);
  4937. return -EFAULT;
  4938. }
  4939. static inline void host_to_target_drmversion(
  4940. struct target_drm_version *target_ver,
  4941. struct drm_version *host_ver)
  4942. {
  4943. __put_user(host_ver->version_major, &target_ver->version_major);
  4944. __put_user(host_ver->version_minor, &target_ver->version_minor);
  4945. __put_user(host_ver->version_patchlevel, &target_ver->version_patchlevel);
  4946. __put_user(host_ver->name_len, &target_ver->name_len);
  4947. __put_user(host_ver->date_len, &target_ver->date_len);
  4948. __put_user(host_ver->desc_len, &target_ver->desc_len);
  4949. unlock_drm_version(host_ver, target_ver, true);
  4950. }
  4951. static abi_long do_ioctl_drm(const IOCTLEntry *ie, uint8_t *buf_temp,
  4952. int fd, int cmd, abi_long arg)
  4953. {
  4954. struct drm_version *ver;
  4955. struct target_drm_version *target_ver;
  4956. abi_long ret;
  4957. switch (ie->host_cmd) {
  4958. case DRM_IOCTL_VERSION:
  4959. if (!lock_user_struct(VERIFY_WRITE, target_ver, arg, 0)) {
  4960. return -TARGET_EFAULT;
  4961. }
  4962. ver = (struct drm_version *)buf_temp;
  4963. ret = target_to_host_drmversion(ver, target_ver);
  4964. if (!is_error(ret)) {
  4965. ret = get_errno(safe_ioctl(fd, ie->host_cmd, ver));
  4966. if (is_error(ret)) {
  4967. unlock_drm_version(ver, target_ver, false);
  4968. } else {
  4969. host_to_target_drmversion(target_ver, ver);
  4970. }
  4971. }
  4972. unlock_user_struct(target_ver, arg, 0);
  4973. return ret;
  4974. }
  4975. return -TARGET_ENOSYS;
  4976. }
  4977. static abi_long do_ioctl_drm_i915_getparam(const IOCTLEntry *ie,
  4978. struct drm_i915_getparam *gparam,
  4979. int fd, abi_long arg)
  4980. {
  4981. abi_long ret;
  4982. int value;
  4983. struct target_drm_i915_getparam *target_gparam;
  4984. if (!lock_user_struct(VERIFY_READ, target_gparam, arg, 0)) {
  4985. return -TARGET_EFAULT;
  4986. }
  4987. __get_user(gparam->param, &target_gparam->param);
  4988. gparam->value = &value;
  4989. ret = get_errno(safe_ioctl(fd, ie->host_cmd, gparam));
  4990. put_user_s32(value, target_gparam->value);
  4991. unlock_user_struct(target_gparam, arg, 0);
  4992. return ret;
  4993. }
  4994. static abi_long do_ioctl_drm_i915(const IOCTLEntry *ie, uint8_t *buf_temp,
  4995. int fd, int cmd, abi_long arg)
  4996. {
  4997. switch (ie->host_cmd) {
  4998. case DRM_IOCTL_I915_GETPARAM:
  4999. return do_ioctl_drm_i915_getparam(ie,
  5000. (struct drm_i915_getparam *)buf_temp,
  5001. fd, arg);
  5002. default:
  5003. return -TARGET_ENOSYS;
  5004. }
  5005. }
  5006. #endif
  5007. static abi_long do_ioctl_TUNSETTXFILTER(const IOCTLEntry *ie, uint8_t *buf_temp,
  5008. int fd, int cmd, abi_long arg)
  5009. {
  5010. struct tun_filter *filter = (struct tun_filter *)buf_temp;
  5011. struct tun_filter *target_filter;
  5012. char *target_addr;
  5013. assert(ie->access == IOC_W);
  5014. target_filter = lock_user(VERIFY_READ, arg, sizeof(*target_filter), 1);
  5015. if (!target_filter) {
  5016. return -TARGET_EFAULT;
  5017. }
  5018. filter->flags = tswap16(target_filter->flags);
  5019. filter->count = tswap16(target_filter->count);
  5020. unlock_user(target_filter, arg, 0);
  5021. if (filter->count) {
  5022. if (offsetof(struct tun_filter, addr) + filter->count * ETH_ALEN >
  5023. MAX_STRUCT_SIZE) {
  5024. return -TARGET_EFAULT;
  5025. }
  5026. target_addr = lock_user(VERIFY_READ,
  5027. arg + offsetof(struct tun_filter, addr),
  5028. filter->count * ETH_ALEN, 1);
  5029. if (!target_addr) {
  5030. return -TARGET_EFAULT;
  5031. }
  5032. memcpy(filter->addr, target_addr, filter->count * ETH_ALEN);
  5033. unlock_user(target_addr, arg + offsetof(struct tun_filter, addr), 0);
  5034. }
  5035. return get_errno(safe_ioctl(fd, ie->host_cmd, filter));
  5036. }
  5037. IOCTLEntry ioctl_entries[] = {
  5038. #define IOCTL(cmd, access, ...) \
  5039. { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
  5040. #define IOCTL_SPECIAL(cmd, access, dofn, ...) \
  5041. { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
  5042. #define IOCTL_IGNORE(cmd) \
  5043. { TARGET_ ## cmd, 0, #cmd },
  5044. #include "ioctls.h"
  5045. { 0, 0, },
  5046. };
  5047. /* ??? Implement proper locking for ioctls. */
  5048. /* do_ioctl() Must return target values and target errnos. */
  5049. static abi_long do_ioctl(int fd, int cmd, abi_long arg)
  5050. {
  5051. const IOCTLEntry *ie;
  5052. const argtype *arg_type;
  5053. abi_long ret;
  5054. uint8_t buf_temp[MAX_STRUCT_SIZE];
  5055. int target_size;
  5056. void *argptr;
  5057. ie = ioctl_entries;
  5058. for(;;) {
  5059. if (ie->target_cmd == 0) {
  5060. qemu_log_mask(
  5061. LOG_UNIMP, "Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
  5062. return -TARGET_ENOTTY;
  5063. }
  5064. if (ie->target_cmd == cmd)
  5065. break;
  5066. ie++;
  5067. }
  5068. arg_type = ie->arg_type;
  5069. if (ie->do_ioctl) {
  5070. return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
  5071. } else if (!ie->host_cmd) {
  5072. /* Some architectures define BSD ioctls in their headers
  5073. that are not implemented in Linux. */
  5074. return -TARGET_ENOTTY;
  5075. }
  5076. switch(arg_type[0]) {
  5077. case TYPE_NULL:
  5078. /* no argument */
  5079. ret = get_errno(safe_ioctl(fd, ie->host_cmd));
  5080. break;
  5081. case TYPE_PTRVOID:
  5082. case TYPE_INT:
  5083. case TYPE_LONG:
  5084. case TYPE_ULONG:
  5085. ret = get_errno(safe_ioctl(fd, ie->host_cmd, arg));
  5086. break;
  5087. case TYPE_PTR:
  5088. arg_type++;
  5089. target_size = thunk_type_size(arg_type, 0);
  5090. switch(ie->access) {
  5091. case IOC_R:
  5092. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  5093. if (!is_error(ret)) {
  5094. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  5095. if (!argptr)
  5096. return -TARGET_EFAULT;
  5097. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  5098. unlock_user(argptr, arg, target_size);
  5099. }
  5100. break;
  5101. case IOC_W:
  5102. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  5103. if (!argptr)
  5104. return -TARGET_EFAULT;
  5105. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  5106. unlock_user(argptr, arg, 0);
  5107. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  5108. break;
  5109. default:
  5110. case IOC_RW:
  5111. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  5112. if (!argptr)
  5113. return -TARGET_EFAULT;
  5114. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  5115. unlock_user(argptr, arg, 0);
  5116. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  5117. if (!is_error(ret)) {
  5118. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  5119. if (!argptr)
  5120. return -TARGET_EFAULT;
  5121. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  5122. unlock_user(argptr, arg, target_size);
  5123. }
  5124. break;
  5125. }
  5126. break;
  5127. default:
  5128. qemu_log_mask(LOG_UNIMP,
  5129. "Unsupported ioctl type: cmd=0x%04lx type=%d\n",
  5130. (long)cmd, arg_type[0]);
  5131. ret = -TARGET_ENOTTY;
  5132. break;
  5133. }
  5134. return ret;
  5135. }
  5136. static const bitmask_transtbl iflag_tbl[] = {
  5137. { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
  5138. { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
  5139. { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
  5140. { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
  5141. { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
  5142. { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
  5143. { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
  5144. { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
  5145. { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
  5146. { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
  5147. { TARGET_IXON, TARGET_IXON, IXON, IXON },
  5148. { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
  5149. { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
  5150. { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
  5151. { TARGET_IUTF8, TARGET_IUTF8, IUTF8, IUTF8},
  5152. };
  5153. static const bitmask_transtbl oflag_tbl[] = {
  5154. { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
  5155. { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
  5156. { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
  5157. { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
  5158. { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
  5159. { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
  5160. { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
  5161. { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
  5162. { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
  5163. { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
  5164. { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
  5165. { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
  5166. { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
  5167. { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
  5168. { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
  5169. { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
  5170. { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
  5171. { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
  5172. { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
  5173. { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
  5174. { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
  5175. { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
  5176. { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
  5177. { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
  5178. };
  5179. static const bitmask_transtbl cflag_tbl[] = {
  5180. { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
  5181. { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
  5182. { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
  5183. { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
  5184. { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
  5185. { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
  5186. { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
  5187. { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
  5188. { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
  5189. { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
  5190. { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
  5191. { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
  5192. { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
  5193. { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
  5194. { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
  5195. { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
  5196. { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
  5197. { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
  5198. { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
  5199. { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
  5200. { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
  5201. { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
  5202. { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
  5203. { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
  5204. { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
  5205. { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
  5206. { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
  5207. { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
  5208. { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
  5209. { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
  5210. { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
  5211. };
  5212. static const bitmask_transtbl lflag_tbl[] = {
  5213. { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
  5214. { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
  5215. { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
  5216. { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
  5217. { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
  5218. { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
  5219. { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
  5220. { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
  5221. { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
  5222. { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
  5223. { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
  5224. { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
  5225. { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
  5226. { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
  5227. { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
  5228. { TARGET_EXTPROC, TARGET_EXTPROC, EXTPROC, EXTPROC},
  5229. };
  5230. static void target_to_host_termios (void *dst, const void *src)
  5231. {
  5232. struct host_termios *host = dst;
  5233. const struct target_termios *target = src;
  5234. host->c_iflag =
  5235. target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
  5236. host->c_oflag =
  5237. target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
  5238. host->c_cflag =
  5239. target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
  5240. host->c_lflag =
  5241. target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
  5242. host->c_line = target->c_line;
  5243. memset(host->c_cc, 0, sizeof(host->c_cc));
  5244. host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
  5245. host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
  5246. host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
  5247. host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
  5248. host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
  5249. host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
  5250. host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
  5251. host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
  5252. host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
  5253. host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
  5254. host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
  5255. host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
  5256. host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
  5257. host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
  5258. host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
  5259. host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
  5260. host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
  5261. }
  5262. static void host_to_target_termios (void *dst, const void *src)
  5263. {
  5264. struct target_termios *target = dst;
  5265. const struct host_termios *host = src;
  5266. target->c_iflag =
  5267. tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
  5268. target->c_oflag =
  5269. tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
  5270. target->c_cflag =
  5271. tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
  5272. target->c_lflag =
  5273. tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
  5274. target->c_line = host->c_line;
  5275. memset(target->c_cc, 0, sizeof(target->c_cc));
  5276. target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
  5277. target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
  5278. target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
  5279. target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
  5280. target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
  5281. target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
  5282. target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
  5283. target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
  5284. target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
  5285. target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
  5286. target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
  5287. target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
  5288. target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
  5289. target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
  5290. target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
  5291. target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
  5292. target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
  5293. }
  5294. static const StructEntry struct_termios_def = {
  5295. .convert = { host_to_target_termios, target_to_host_termios },
  5296. .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
  5297. .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
  5298. .print = print_termios,
  5299. };
  5300. /* If the host does not provide these bits, they may be safely discarded. */
  5301. #ifndef MAP_SYNC
  5302. #define MAP_SYNC 0
  5303. #endif
  5304. #ifndef MAP_UNINITIALIZED
  5305. #define MAP_UNINITIALIZED 0
  5306. #endif
  5307. static const bitmask_transtbl mmap_flags_tbl[] = {
  5308. { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
  5309. { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS,
  5310. MAP_ANONYMOUS, MAP_ANONYMOUS },
  5311. { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN,
  5312. MAP_GROWSDOWN, MAP_GROWSDOWN },
  5313. { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE,
  5314. MAP_DENYWRITE, MAP_DENYWRITE },
  5315. { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE,
  5316. MAP_EXECUTABLE, MAP_EXECUTABLE },
  5317. { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
  5318. { TARGET_MAP_NORESERVE, TARGET_MAP_NORESERVE,
  5319. MAP_NORESERVE, MAP_NORESERVE },
  5320. { TARGET_MAP_HUGETLB, TARGET_MAP_HUGETLB, MAP_HUGETLB, MAP_HUGETLB },
  5321. /* MAP_STACK had been ignored by the kernel for quite some time.
  5322. Recognize it for the target insofar as we do not want to pass
  5323. it through to the host. */
  5324. { TARGET_MAP_STACK, TARGET_MAP_STACK, 0, 0 },
  5325. { TARGET_MAP_NONBLOCK, TARGET_MAP_NONBLOCK, MAP_NONBLOCK, MAP_NONBLOCK },
  5326. { TARGET_MAP_POPULATE, TARGET_MAP_POPULATE, MAP_POPULATE, MAP_POPULATE },
  5327. { TARGET_MAP_FIXED_NOREPLACE, TARGET_MAP_FIXED_NOREPLACE,
  5328. MAP_FIXED_NOREPLACE, MAP_FIXED_NOREPLACE },
  5329. { TARGET_MAP_UNINITIALIZED, TARGET_MAP_UNINITIALIZED,
  5330. MAP_UNINITIALIZED, MAP_UNINITIALIZED },
  5331. };
  5332. /*
  5333. * Arrange for legacy / undefined architecture specific flags to be
  5334. * ignored by mmap handling code.
  5335. */
  5336. #ifndef TARGET_MAP_32BIT
  5337. #define TARGET_MAP_32BIT 0
  5338. #endif
  5339. #ifndef TARGET_MAP_HUGE_2MB
  5340. #define TARGET_MAP_HUGE_2MB 0
  5341. #endif
  5342. #ifndef TARGET_MAP_HUGE_1GB
  5343. #define TARGET_MAP_HUGE_1GB 0
  5344. #endif
  5345. static abi_long do_mmap(abi_ulong addr, abi_ulong len, int prot,
  5346. int target_flags, int fd, off_t offset)
  5347. {
  5348. /*
  5349. * The historical set of flags that all mmap types implicitly support.
  5350. */
  5351. enum {
  5352. TARGET_LEGACY_MAP_MASK = TARGET_MAP_SHARED
  5353. | TARGET_MAP_PRIVATE
  5354. | TARGET_MAP_FIXED
  5355. | TARGET_MAP_ANONYMOUS
  5356. | TARGET_MAP_DENYWRITE
  5357. | TARGET_MAP_EXECUTABLE
  5358. | TARGET_MAP_UNINITIALIZED
  5359. | TARGET_MAP_GROWSDOWN
  5360. | TARGET_MAP_LOCKED
  5361. | TARGET_MAP_NORESERVE
  5362. | TARGET_MAP_POPULATE
  5363. | TARGET_MAP_NONBLOCK
  5364. | TARGET_MAP_STACK
  5365. | TARGET_MAP_HUGETLB
  5366. | TARGET_MAP_32BIT
  5367. | TARGET_MAP_HUGE_2MB
  5368. | TARGET_MAP_HUGE_1GB
  5369. };
  5370. int host_flags;
  5371. switch (target_flags & TARGET_MAP_TYPE) {
  5372. case TARGET_MAP_PRIVATE:
  5373. host_flags = MAP_PRIVATE;
  5374. break;
  5375. case TARGET_MAP_SHARED:
  5376. host_flags = MAP_SHARED;
  5377. break;
  5378. case TARGET_MAP_SHARED_VALIDATE:
  5379. /*
  5380. * MAP_SYNC is only supported for MAP_SHARED_VALIDATE, and is
  5381. * therefore omitted from mmap_flags_tbl and TARGET_LEGACY_MAP_MASK.
  5382. */
  5383. if (target_flags & ~(TARGET_LEGACY_MAP_MASK | TARGET_MAP_SYNC)) {
  5384. return -TARGET_EOPNOTSUPP;
  5385. }
  5386. host_flags = MAP_SHARED_VALIDATE;
  5387. if (target_flags & TARGET_MAP_SYNC) {
  5388. host_flags |= MAP_SYNC;
  5389. }
  5390. break;
  5391. default:
  5392. return -TARGET_EINVAL;
  5393. }
  5394. host_flags |= target_to_host_bitmask(target_flags, mmap_flags_tbl);
  5395. return get_errno(target_mmap(addr, len, prot, host_flags, fd, offset));
  5396. }
  5397. /*
  5398. * NOTE: TARGET_ABI32 is defined for TARGET_I386 (but not for TARGET_X86_64)
  5399. * TARGET_I386 is defined if TARGET_X86_64 is defined
  5400. */
  5401. #if defined(TARGET_I386)
  5402. /* NOTE: there is really one LDT for all the threads */
  5403. static uint8_t *ldt_table;
  5404. static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
  5405. {
  5406. int size;
  5407. void *p;
  5408. if (!ldt_table)
  5409. return 0;
  5410. size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
  5411. if (size > bytecount)
  5412. size = bytecount;
  5413. p = lock_user(VERIFY_WRITE, ptr, size, 0);
  5414. if (!p)
  5415. return -TARGET_EFAULT;
  5416. /* ??? Should this by byteswapped? */
  5417. memcpy(p, ldt_table, size);
  5418. unlock_user(p, ptr, size);
  5419. return size;
  5420. }
  5421. /* XXX: add locking support */
  5422. static abi_long write_ldt(CPUX86State *env,
  5423. abi_ulong ptr, unsigned long bytecount, int oldmode)
  5424. {
  5425. struct target_modify_ldt_ldt_s ldt_info;
  5426. struct target_modify_ldt_ldt_s *target_ldt_info;
  5427. int seg_32bit, contents, read_exec_only, limit_in_pages;
  5428. int seg_not_present, useable, lm;
  5429. uint32_t *lp, entry_1, entry_2;
  5430. if (bytecount != sizeof(ldt_info))
  5431. return -TARGET_EINVAL;
  5432. if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
  5433. return -TARGET_EFAULT;
  5434. ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
  5435. ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
  5436. ldt_info.limit = tswap32(target_ldt_info->limit);
  5437. ldt_info.flags = tswap32(target_ldt_info->flags);
  5438. unlock_user_struct(target_ldt_info, ptr, 0);
  5439. if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
  5440. return -TARGET_EINVAL;
  5441. seg_32bit = ldt_info.flags & 1;
  5442. contents = (ldt_info.flags >> 1) & 3;
  5443. read_exec_only = (ldt_info.flags >> 3) & 1;
  5444. limit_in_pages = (ldt_info.flags >> 4) & 1;
  5445. seg_not_present = (ldt_info.flags >> 5) & 1;
  5446. useable = (ldt_info.flags >> 6) & 1;
  5447. #ifdef TARGET_ABI32
  5448. lm = 0;
  5449. #else
  5450. lm = (ldt_info.flags >> 7) & 1;
  5451. #endif
  5452. if (contents == 3) {
  5453. if (oldmode)
  5454. return -TARGET_EINVAL;
  5455. if (seg_not_present == 0)
  5456. return -TARGET_EINVAL;
  5457. }
  5458. /* allocate the LDT */
  5459. if (!ldt_table) {
  5460. env->ldt.base = target_mmap(0,
  5461. TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
  5462. PROT_READ|PROT_WRITE,
  5463. MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
  5464. if (env->ldt.base == -1)
  5465. return -TARGET_ENOMEM;
  5466. memset(g2h_untagged(env->ldt.base), 0,
  5467. TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
  5468. env->ldt.limit = 0xffff;
  5469. ldt_table = g2h_untagged(env->ldt.base);
  5470. }
  5471. /* NOTE: same code as Linux kernel */
  5472. /* Allow LDTs to be cleared by the user. */
  5473. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  5474. if (oldmode ||
  5475. (contents == 0 &&
  5476. read_exec_only == 1 &&
  5477. seg_32bit == 0 &&
  5478. limit_in_pages == 0 &&
  5479. seg_not_present == 1 &&
  5480. useable == 0 )) {
  5481. entry_1 = 0;
  5482. entry_2 = 0;
  5483. goto install;
  5484. }
  5485. }
  5486. entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
  5487. (ldt_info.limit & 0x0ffff);
  5488. entry_2 = (ldt_info.base_addr & 0xff000000) |
  5489. ((ldt_info.base_addr & 0x00ff0000) >> 16) |
  5490. (ldt_info.limit & 0xf0000) |
  5491. ((read_exec_only ^ 1) << 9) |
  5492. (contents << 10) |
  5493. ((seg_not_present ^ 1) << 15) |
  5494. (seg_32bit << 22) |
  5495. (limit_in_pages << 23) |
  5496. (lm << 21) |
  5497. 0x7000;
  5498. if (!oldmode)
  5499. entry_2 |= (useable << 20);
  5500. /* Install the new entry ... */
  5501. install:
  5502. lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
  5503. lp[0] = tswap32(entry_1);
  5504. lp[1] = tswap32(entry_2);
  5505. return 0;
  5506. }
  5507. /* specific and weird i386 syscalls */
  5508. static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
  5509. unsigned long bytecount)
  5510. {
  5511. abi_long ret;
  5512. switch (func) {
  5513. case 0:
  5514. ret = read_ldt(ptr, bytecount);
  5515. break;
  5516. case 1:
  5517. ret = write_ldt(env, ptr, bytecount, 1);
  5518. break;
  5519. case 0x11:
  5520. ret = write_ldt(env, ptr, bytecount, 0);
  5521. break;
  5522. default:
  5523. ret = -TARGET_ENOSYS;
  5524. break;
  5525. }
  5526. return ret;
  5527. }
  5528. #if defined(TARGET_ABI32)
  5529. abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
  5530. {
  5531. uint64_t *gdt_table = g2h_untagged(env->gdt.base);
  5532. struct target_modify_ldt_ldt_s ldt_info;
  5533. struct target_modify_ldt_ldt_s *target_ldt_info;
  5534. int seg_32bit, contents, read_exec_only, limit_in_pages;
  5535. int seg_not_present, useable, lm;
  5536. uint32_t *lp, entry_1, entry_2;
  5537. int i;
  5538. lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
  5539. if (!target_ldt_info)
  5540. return -TARGET_EFAULT;
  5541. ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
  5542. ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
  5543. ldt_info.limit = tswap32(target_ldt_info->limit);
  5544. ldt_info.flags = tswap32(target_ldt_info->flags);
  5545. if (ldt_info.entry_number == -1) {
  5546. for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
  5547. if (gdt_table[i] == 0) {
  5548. ldt_info.entry_number = i;
  5549. target_ldt_info->entry_number = tswap32(i);
  5550. break;
  5551. }
  5552. }
  5553. }
  5554. unlock_user_struct(target_ldt_info, ptr, 1);
  5555. if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
  5556. ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
  5557. return -TARGET_EINVAL;
  5558. seg_32bit = ldt_info.flags & 1;
  5559. contents = (ldt_info.flags >> 1) & 3;
  5560. read_exec_only = (ldt_info.flags >> 3) & 1;
  5561. limit_in_pages = (ldt_info.flags >> 4) & 1;
  5562. seg_not_present = (ldt_info.flags >> 5) & 1;
  5563. useable = (ldt_info.flags >> 6) & 1;
  5564. #ifdef TARGET_ABI32
  5565. lm = 0;
  5566. #else
  5567. lm = (ldt_info.flags >> 7) & 1;
  5568. #endif
  5569. if (contents == 3) {
  5570. if (seg_not_present == 0)
  5571. return -TARGET_EINVAL;
  5572. }
  5573. /* NOTE: same code as Linux kernel */
  5574. /* Allow LDTs to be cleared by the user. */
  5575. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  5576. if ((contents == 0 &&
  5577. read_exec_only == 1 &&
  5578. seg_32bit == 0 &&
  5579. limit_in_pages == 0 &&
  5580. seg_not_present == 1 &&
  5581. useable == 0 )) {
  5582. entry_1 = 0;
  5583. entry_2 = 0;
  5584. goto install;
  5585. }
  5586. }
  5587. entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
  5588. (ldt_info.limit & 0x0ffff);
  5589. entry_2 = (ldt_info.base_addr & 0xff000000) |
  5590. ((ldt_info.base_addr & 0x00ff0000) >> 16) |
  5591. (ldt_info.limit & 0xf0000) |
  5592. ((read_exec_only ^ 1) << 9) |
  5593. (contents << 10) |
  5594. ((seg_not_present ^ 1) << 15) |
  5595. (seg_32bit << 22) |
  5596. (limit_in_pages << 23) |
  5597. (useable << 20) |
  5598. (lm << 21) |
  5599. 0x7000;
  5600. /* Install the new entry ... */
  5601. install:
  5602. lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
  5603. lp[0] = tswap32(entry_1);
  5604. lp[1] = tswap32(entry_2);
  5605. return 0;
  5606. }
  5607. static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
  5608. {
  5609. struct target_modify_ldt_ldt_s *target_ldt_info;
  5610. uint64_t *gdt_table = g2h_untagged(env->gdt.base);
  5611. uint32_t base_addr, limit, flags;
  5612. int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
  5613. int seg_not_present, useable, lm;
  5614. uint32_t *lp, entry_1, entry_2;
  5615. lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
  5616. if (!target_ldt_info)
  5617. return -TARGET_EFAULT;
  5618. idx = tswap32(target_ldt_info->entry_number);
  5619. if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
  5620. idx > TARGET_GDT_ENTRY_TLS_MAX) {
  5621. unlock_user_struct(target_ldt_info, ptr, 1);
  5622. return -TARGET_EINVAL;
  5623. }
  5624. lp = (uint32_t *)(gdt_table + idx);
  5625. entry_1 = tswap32(lp[0]);
  5626. entry_2 = tswap32(lp[1]);
  5627. read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
  5628. contents = (entry_2 >> 10) & 3;
  5629. seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
  5630. seg_32bit = (entry_2 >> 22) & 1;
  5631. limit_in_pages = (entry_2 >> 23) & 1;
  5632. useable = (entry_2 >> 20) & 1;
  5633. #ifdef TARGET_ABI32
  5634. lm = 0;
  5635. #else
  5636. lm = (entry_2 >> 21) & 1;
  5637. #endif
  5638. flags = (seg_32bit << 0) | (contents << 1) |
  5639. (read_exec_only << 3) | (limit_in_pages << 4) |
  5640. (seg_not_present << 5) | (useable << 6) | (lm << 7);
  5641. limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
  5642. base_addr = (entry_1 >> 16) |
  5643. (entry_2 & 0xff000000) |
  5644. ((entry_2 & 0xff) << 16);
  5645. target_ldt_info->base_addr = tswapal(base_addr);
  5646. target_ldt_info->limit = tswap32(limit);
  5647. target_ldt_info->flags = tswap32(flags);
  5648. unlock_user_struct(target_ldt_info, ptr, 1);
  5649. return 0;
  5650. }
  5651. abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
  5652. {
  5653. return -TARGET_ENOSYS;
  5654. }
  5655. #else
  5656. abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
  5657. {
  5658. abi_long ret = 0;
  5659. abi_ulong val;
  5660. int idx;
  5661. switch(code) {
  5662. case TARGET_ARCH_SET_GS:
  5663. case TARGET_ARCH_SET_FS:
  5664. if (code == TARGET_ARCH_SET_GS)
  5665. idx = R_GS;
  5666. else
  5667. idx = R_FS;
  5668. cpu_x86_load_seg(env, idx, 0);
  5669. env->segs[idx].base = addr;
  5670. break;
  5671. case TARGET_ARCH_GET_GS:
  5672. case TARGET_ARCH_GET_FS:
  5673. if (code == TARGET_ARCH_GET_GS)
  5674. idx = R_GS;
  5675. else
  5676. idx = R_FS;
  5677. val = env->segs[idx].base;
  5678. if (put_user(val, addr, abi_ulong))
  5679. ret = -TARGET_EFAULT;
  5680. break;
  5681. default:
  5682. ret = -TARGET_EINVAL;
  5683. break;
  5684. }
  5685. return ret;
  5686. }
  5687. #endif /* defined(TARGET_ABI32 */
  5688. #endif /* defined(TARGET_I386) */
  5689. /*
  5690. * These constants are generic. Supply any that are missing from the host.
  5691. */
  5692. #ifndef PR_SET_NAME
  5693. # define PR_SET_NAME 15
  5694. # define PR_GET_NAME 16
  5695. #endif
  5696. #ifndef PR_SET_FP_MODE
  5697. # define PR_SET_FP_MODE 45
  5698. # define PR_GET_FP_MODE 46
  5699. # define PR_FP_MODE_FR (1 << 0)
  5700. # define PR_FP_MODE_FRE (1 << 1)
  5701. #endif
  5702. #ifndef PR_SVE_SET_VL
  5703. # define PR_SVE_SET_VL 50
  5704. # define PR_SVE_GET_VL 51
  5705. # define PR_SVE_VL_LEN_MASK 0xffff
  5706. # define PR_SVE_VL_INHERIT (1 << 17)
  5707. #endif
  5708. #ifndef PR_PAC_RESET_KEYS
  5709. # define PR_PAC_RESET_KEYS 54
  5710. # define PR_PAC_APIAKEY (1 << 0)
  5711. # define PR_PAC_APIBKEY (1 << 1)
  5712. # define PR_PAC_APDAKEY (1 << 2)
  5713. # define PR_PAC_APDBKEY (1 << 3)
  5714. # define PR_PAC_APGAKEY (1 << 4)
  5715. #endif
  5716. #ifndef PR_SET_TAGGED_ADDR_CTRL
  5717. # define PR_SET_TAGGED_ADDR_CTRL 55
  5718. # define PR_GET_TAGGED_ADDR_CTRL 56
  5719. # define PR_TAGGED_ADDR_ENABLE (1UL << 0)
  5720. #endif
  5721. #ifndef PR_SET_IO_FLUSHER
  5722. # define PR_SET_IO_FLUSHER 57
  5723. # define PR_GET_IO_FLUSHER 58
  5724. #endif
  5725. #ifndef PR_SET_SYSCALL_USER_DISPATCH
  5726. # define PR_SET_SYSCALL_USER_DISPATCH 59
  5727. #endif
  5728. #ifndef PR_SME_SET_VL
  5729. # define PR_SME_SET_VL 63
  5730. # define PR_SME_GET_VL 64
  5731. # define PR_SME_VL_LEN_MASK 0xffff
  5732. # define PR_SME_VL_INHERIT (1 << 17)
  5733. #endif
  5734. #include "target_prctl.h"
  5735. static abi_long do_prctl_inval0(CPUArchState *env)
  5736. {
  5737. return -TARGET_EINVAL;
  5738. }
  5739. static abi_long do_prctl_inval1(CPUArchState *env, abi_long arg2)
  5740. {
  5741. return -TARGET_EINVAL;
  5742. }
  5743. #ifndef do_prctl_get_fp_mode
  5744. #define do_prctl_get_fp_mode do_prctl_inval0
  5745. #endif
  5746. #ifndef do_prctl_set_fp_mode
  5747. #define do_prctl_set_fp_mode do_prctl_inval1
  5748. #endif
  5749. #ifndef do_prctl_sve_get_vl
  5750. #define do_prctl_sve_get_vl do_prctl_inval0
  5751. #endif
  5752. #ifndef do_prctl_sve_set_vl
  5753. #define do_prctl_sve_set_vl do_prctl_inval1
  5754. #endif
  5755. #ifndef do_prctl_reset_keys
  5756. #define do_prctl_reset_keys do_prctl_inval1
  5757. #endif
  5758. #ifndef do_prctl_set_tagged_addr_ctrl
  5759. #define do_prctl_set_tagged_addr_ctrl do_prctl_inval1
  5760. #endif
  5761. #ifndef do_prctl_get_tagged_addr_ctrl
  5762. #define do_prctl_get_tagged_addr_ctrl do_prctl_inval0
  5763. #endif
  5764. #ifndef do_prctl_get_unalign
  5765. #define do_prctl_get_unalign do_prctl_inval1
  5766. #endif
  5767. #ifndef do_prctl_set_unalign
  5768. #define do_prctl_set_unalign do_prctl_inval1
  5769. #endif
  5770. #ifndef do_prctl_sme_get_vl
  5771. #define do_prctl_sme_get_vl do_prctl_inval0
  5772. #endif
  5773. #ifndef do_prctl_sme_set_vl
  5774. #define do_prctl_sme_set_vl do_prctl_inval1
  5775. #endif
  5776. static abi_long do_prctl(CPUArchState *env, abi_long option, abi_long arg2,
  5777. abi_long arg3, abi_long arg4, abi_long arg5)
  5778. {
  5779. abi_long ret;
  5780. switch (option) {
  5781. case PR_GET_PDEATHSIG:
  5782. {
  5783. int deathsig;
  5784. ret = get_errno(prctl(PR_GET_PDEATHSIG, &deathsig,
  5785. arg3, arg4, arg5));
  5786. if (!is_error(ret) &&
  5787. put_user_s32(host_to_target_signal(deathsig), arg2)) {
  5788. return -TARGET_EFAULT;
  5789. }
  5790. return ret;
  5791. }
  5792. case PR_SET_PDEATHSIG:
  5793. return get_errno(prctl(PR_SET_PDEATHSIG, target_to_host_signal(arg2),
  5794. arg3, arg4, arg5));
  5795. case PR_GET_NAME:
  5796. {
  5797. void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
  5798. if (!name) {
  5799. return -TARGET_EFAULT;
  5800. }
  5801. ret = get_errno(prctl(PR_GET_NAME, (uintptr_t)name,
  5802. arg3, arg4, arg5));
  5803. unlock_user(name, arg2, 16);
  5804. return ret;
  5805. }
  5806. case PR_SET_NAME:
  5807. {
  5808. void *name = lock_user(VERIFY_READ, arg2, 16, 1);
  5809. if (!name) {
  5810. return -TARGET_EFAULT;
  5811. }
  5812. ret = get_errno(prctl(PR_SET_NAME, (uintptr_t)name,
  5813. arg3, arg4, arg5));
  5814. unlock_user(name, arg2, 0);
  5815. return ret;
  5816. }
  5817. case PR_GET_FP_MODE:
  5818. return do_prctl_get_fp_mode(env);
  5819. case PR_SET_FP_MODE:
  5820. return do_prctl_set_fp_mode(env, arg2);
  5821. case PR_SVE_GET_VL:
  5822. return do_prctl_sve_get_vl(env);
  5823. case PR_SVE_SET_VL:
  5824. return do_prctl_sve_set_vl(env, arg2);
  5825. case PR_SME_GET_VL:
  5826. return do_prctl_sme_get_vl(env);
  5827. case PR_SME_SET_VL:
  5828. return do_prctl_sme_set_vl(env, arg2);
  5829. case PR_PAC_RESET_KEYS:
  5830. if (arg3 || arg4 || arg5) {
  5831. return -TARGET_EINVAL;
  5832. }
  5833. return do_prctl_reset_keys(env, arg2);
  5834. case PR_SET_TAGGED_ADDR_CTRL:
  5835. if (arg3 || arg4 || arg5) {
  5836. return -TARGET_EINVAL;
  5837. }
  5838. return do_prctl_set_tagged_addr_ctrl(env, arg2);
  5839. case PR_GET_TAGGED_ADDR_CTRL:
  5840. if (arg2 || arg3 || arg4 || arg5) {
  5841. return -TARGET_EINVAL;
  5842. }
  5843. return do_prctl_get_tagged_addr_ctrl(env);
  5844. case PR_GET_UNALIGN:
  5845. return do_prctl_get_unalign(env, arg2);
  5846. case PR_SET_UNALIGN:
  5847. return do_prctl_set_unalign(env, arg2);
  5848. case PR_CAP_AMBIENT:
  5849. case PR_CAPBSET_READ:
  5850. case PR_CAPBSET_DROP:
  5851. case PR_GET_DUMPABLE:
  5852. case PR_SET_DUMPABLE:
  5853. case PR_GET_KEEPCAPS:
  5854. case PR_SET_KEEPCAPS:
  5855. case PR_GET_SECUREBITS:
  5856. case PR_SET_SECUREBITS:
  5857. case PR_GET_TIMING:
  5858. case PR_SET_TIMING:
  5859. case PR_GET_TIMERSLACK:
  5860. case PR_SET_TIMERSLACK:
  5861. case PR_MCE_KILL:
  5862. case PR_MCE_KILL_GET:
  5863. case PR_GET_NO_NEW_PRIVS:
  5864. case PR_SET_NO_NEW_PRIVS:
  5865. case PR_GET_IO_FLUSHER:
  5866. case PR_SET_IO_FLUSHER:
  5867. case PR_SET_CHILD_SUBREAPER:
  5868. case PR_GET_SPECULATION_CTRL:
  5869. case PR_SET_SPECULATION_CTRL:
  5870. /* Some prctl options have no pointer arguments and we can pass on. */
  5871. return get_errno(prctl(option, arg2, arg3, arg4, arg5));
  5872. case PR_GET_CHILD_SUBREAPER:
  5873. {
  5874. int val;
  5875. ret = get_errno(prctl(PR_GET_CHILD_SUBREAPER, &val,
  5876. arg3, arg4, arg5));
  5877. if (!is_error(ret) && put_user_s32(val, arg2)) {
  5878. return -TARGET_EFAULT;
  5879. }
  5880. return ret;
  5881. }
  5882. case PR_GET_TID_ADDRESS:
  5883. {
  5884. TaskState *ts = get_task_state(env_cpu(env));
  5885. return put_user_ual(ts->child_tidptr, arg2);
  5886. }
  5887. case PR_GET_FPEXC:
  5888. case PR_SET_FPEXC:
  5889. /* Was used for SPE on PowerPC. */
  5890. return -TARGET_EINVAL;
  5891. case PR_GET_ENDIAN:
  5892. case PR_SET_ENDIAN:
  5893. case PR_GET_FPEMU:
  5894. case PR_SET_FPEMU:
  5895. case PR_SET_MM:
  5896. case PR_GET_SECCOMP:
  5897. case PR_SET_SECCOMP:
  5898. case PR_SET_SYSCALL_USER_DISPATCH:
  5899. case PR_GET_THP_DISABLE:
  5900. case PR_SET_THP_DISABLE:
  5901. case PR_GET_TSC:
  5902. case PR_SET_TSC:
  5903. /* Disable to prevent the target disabling stuff we need. */
  5904. return -TARGET_EINVAL;
  5905. default:
  5906. qemu_log_mask(LOG_UNIMP, "Unsupported prctl: " TARGET_ABI_FMT_ld "\n",
  5907. option);
  5908. return -TARGET_EINVAL;
  5909. }
  5910. }
  5911. #define NEW_STACK_SIZE 0x40000
  5912. static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
  5913. typedef struct {
  5914. CPUArchState *env;
  5915. pthread_mutex_t mutex;
  5916. pthread_cond_t cond;
  5917. pthread_t thread;
  5918. uint32_t tid;
  5919. abi_ulong child_tidptr;
  5920. abi_ulong parent_tidptr;
  5921. sigset_t sigmask;
  5922. } new_thread_info;
  5923. static void *clone_func(void *arg)
  5924. {
  5925. new_thread_info *info = arg;
  5926. CPUArchState *env;
  5927. CPUState *cpu;
  5928. TaskState *ts;
  5929. rcu_register_thread();
  5930. tcg_register_thread();
  5931. env = info->env;
  5932. cpu = env_cpu(env);
  5933. thread_cpu = cpu;
  5934. ts = get_task_state(cpu);
  5935. info->tid = sys_gettid();
  5936. task_settid(ts);
  5937. if (info->child_tidptr)
  5938. put_user_u32(info->tid, info->child_tidptr);
  5939. if (info->parent_tidptr)
  5940. put_user_u32(info->tid, info->parent_tidptr);
  5941. qemu_guest_random_seed_thread_part2(cpu->random_seed);
  5942. /* Enable signals. */
  5943. sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
  5944. /* Signal to the parent that we're ready. */
  5945. pthread_mutex_lock(&info->mutex);
  5946. pthread_cond_broadcast(&info->cond);
  5947. pthread_mutex_unlock(&info->mutex);
  5948. /* Wait until the parent has finished initializing the tls state. */
  5949. pthread_mutex_lock(&clone_lock);
  5950. pthread_mutex_unlock(&clone_lock);
  5951. cpu_loop(env);
  5952. /* never exits */
  5953. return NULL;
  5954. }
  5955. /* do_fork() Must return host values and target errnos (unlike most
  5956. do_*() functions). */
  5957. static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
  5958. abi_ulong parent_tidptr, target_ulong newtls,
  5959. abi_ulong child_tidptr)
  5960. {
  5961. CPUState *cpu = env_cpu(env);
  5962. int ret;
  5963. TaskState *ts;
  5964. CPUState *new_cpu;
  5965. CPUArchState *new_env;
  5966. sigset_t sigmask;
  5967. flags &= ~CLONE_IGNORED_FLAGS;
  5968. /* Emulate vfork() with fork() */
  5969. if (flags & CLONE_VFORK)
  5970. flags &= ~(CLONE_VFORK | CLONE_VM);
  5971. if (flags & CLONE_VM) {
  5972. TaskState *parent_ts = get_task_state(cpu);
  5973. new_thread_info info;
  5974. pthread_attr_t attr;
  5975. if (((flags & CLONE_THREAD_FLAGS) != CLONE_THREAD_FLAGS) ||
  5976. (flags & CLONE_INVALID_THREAD_FLAGS)) {
  5977. return -TARGET_EINVAL;
  5978. }
  5979. ts = g_new0(TaskState, 1);
  5980. init_task_state(ts);
  5981. /* Grab a mutex so that thread setup appears atomic. */
  5982. pthread_mutex_lock(&clone_lock);
  5983. /*
  5984. * If this is our first additional thread, we need to ensure we
  5985. * generate code for parallel execution and flush old translations.
  5986. * Do this now so that the copy gets CF_PARALLEL too.
  5987. */
  5988. if (!tcg_cflags_has(cpu, CF_PARALLEL)) {
  5989. tcg_cflags_set(cpu, CF_PARALLEL);
  5990. tb_flush(cpu);
  5991. }
  5992. /* we create a new CPU instance. */
  5993. new_env = cpu_copy(env);
  5994. /* Init regs that differ from the parent. */
  5995. cpu_clone_regs_child(new_env, newsp, flags);
  5996. cpu_clone_regs_parent(env, flags);
  5997. new_cpu = env_cpu(new_env);
  5998. new_cpu->opaque = ts;
  5999. ts->bprm = parent_ts->bprm;
  6000. ts->info = parent_ts->info;
  6001. ts->signal_mask = parent_ts->signal_mask;
  6002. if (flags & CLONE_CHILD_CLEARTID) {
  6003. ts->child_tidptr = child_tidptr;
  6004. }
  6005. if (flags & CLONE_SETTLS) {
  6006. cpu_set_tls (new_env, newtls);
  6007. }
  6008. memset(&info, 0, sizeof(info));
  6009. pthread_mutex_init(&info.mutex, NULL);
  6010. pthread_mutex_lock(&info.mutex);
  6011. pthread_cond_init(&info.cond, NULL);
  6012. info.env = new_env;
  6013. if (flags & CLONE_CHILD_SETTID) {
  6014. info.child_tidptr = child_tidptr;
  6015. }
  6016. if (flags & CLONE_PARENT_SETTID) {
  6017. info.parent_tidptr = parent_tidptr;
  6018. }
  6019. ret = pthread_attr_init(&attr);
  6020. ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
  6021. ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  6022. /* It is not safe to deliver signals until the child has finished
  6023. initializing, so temporarily block all signals. */
  6024. sigfillset(&sigmask);
  6025. sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
  6026. cpu->random_seed = qemu_guest_random_seed_thread_part1();
  6027. ret = pthread_create(&info.thread, &attr, clone_func, &info);
  6028. /* TODO: Free new CPU state if thread creation failed. */
  6029. sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
  6030. pthread_attr_destroy(&attr);
  6031. if (ret == 0) {
  6032. /* Wait for the child to initialize. */
  6033. pthread_cond_wait(&info.cond, &info.mutex);
  6034. ret = info.tid;
  6035. } else {
  6036. ret = -1;
  6037. }
  6038. pthread_mutex_unlock(&info.mutex);
  6039. pthread_cond_destroy(&info.cond);
  6040. pthread_mutex_destroy(&info.mutex);
  6041. pthread_mutex_unlock(&clone_lock);
  6042. } else {
  6043. /* if no CLONE_VM, we consider it is a fork */
  6044. if (flags & CLONE_INVALID_FORK_FLAGS) {
  6045. return -TARGET_EINVAL;
  6046. }
  6047. /* We can't support custom termination signals */
  6048. if ((flags & CSIGNAL) != TARGET_SIGCHLD) {
  6049. return -TARGET_EINVAL;
  6050. }
  6051. #if !defined(__NR_pidfd_open) || !defined(TARGET_NR_pidfd_open)
  6052. if (flags & CLONE_PIDFD) {
  6053. return -TARGET_EINVAL;
  6054. }
  6055. #endif
  6056. /* Can not allow CLONE_PIDFD with CLONE_PARENT_SETTID */
  6057. if ((flags & CLONE_PIDFD) && (flags & CLONE_PARENT_SETTID)) {
  6058. return -TARGET_EINVAL;
  6059. }
  6060. if (block_signals()) {
  6061. return -QEMU_ERESTARTSYS;
  6062. }
  6063. fork_start();
  6064. ret = fork();
  6065. if (ret == 0) {
  6066. /* Child Process. */
  6067. cpu_clone_regs_child(env, newsp, flags);
  6068. fork_end(ret);
  6069. /* There is a race condition here. The parent process could
  6070. theoretically read the TID in the child process before the child
  6071. tid is set. This would require using either ptrace
  6072. (not implemented) or having *_tidptr to point at a shared memory
  6073. mapping. We can't repeat the spinlock hack used above because
  6074. the child process gets its own copy of the lock. */
  6075. if (flags & CLONE_CHILD_SETTID)
  6076. put_user_u32(sys_gettid(), child_tidptr);
  6077. if (flags & CLONE_PARENT_SETTID)
  6078. put_user_u32(sys_gettid(), parent_tidptr);
  6079. ts = get_task_state(cpu);
  6080. if (flags & CLONE_SETTLS)
  6081. cpu_set_tls (env, newtls);
  6082. if (flags & CLONE_CHILD_CLEARTID)
  6083. ts->child_tidptr = child_tidptr;
  6084. } else {
  6085. cpu_clone_regs_parent(env, flags);
  6086. if (flags & CLONE_PIDFD) {
  6087. int pid_fd = 0;
  6088. #if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
  6089. int pid_child = ret;
  6090. pid_fd = pidfd_open(pid_child, 0);
  6091. if (pid_fd >= 0) {
  6092. fcntl(pid_fd, F_SETFD, fcntl(pid_fd, F_GETFL)
  6093. | FD_CLOEXEC);
  6094. } else {
  6095. pid_fd = 0;
  6096. }
  6097. #endif
  6098. put_user_u32(pid_fd, parent_tidptr);
  6099. }
  6100. fork_end(ret);
  6101. }
  6102. g_assert(!cpu_in_exclusive_context(cpu));
  6103. }
  6104. return ret;
  6105. }
  6106. /* warning : doesn't handle linux specific flags... */
  6107. static int target_to_host_fcntl_cmd(int cmd)
  6108. {
  6109. int ret;
  6110. switch(cmd) {
  6111. case TARGET_F_DUPFD:
  6112. case TARGET_F_GETFD:
  6113. case TARGET_F_SETFD:
  6114. case TARGET_F_GETFL:
  6115. case TARGET_F_SETFL:
  6116. case TARGET_F_OFD_GETLK:
  6117. case TARGET_F_OFD_SETLK:
  6118. case TARGET_F_OFD_SETLKW:
  6119. ret = cmd;
  6120. break;
  6121. case TARGET_F_GETLK:
  6122. ret = F_GETLK;
  6123. break;
  6124. case TARGET_F_SETLK:
  6125. ret = F_SETLK;
  6126. break;
  6127. case TARGET_F_SETLKW:
  6128. ret = F_SETLKW;
  6129. break;
  6130. case TARGET_F_GETOWN:
  6131. ret = F_GETOWN;
  6132. break;
  6133. case TARGET_F_SETOWN:
  6134. ret = F_SETOWN;
  6135. break;
  6136. case TARGET_F_GETSIG:
  6137. ret = F_GETSIG;
  6138. break;
  6139. case TARGET_F_SETSIG:
  6140. ret = F_SETSIG;
  6141. break;
  6142. #if TARGET_ABI_BITS == 32
  6143. case TARGET_F_GETLK64:
  6144. ret = F_GETLK;
  6145. break;
  6146. case TARGET_F_SETLK64:
  6147. ret = F_SETLK;
  6148. break;
  6149. case TARGET_F_SETLKW64:
  6150. ret = F_SETLKW;
  6151. break;
  6152. #endif
  6153. case TARGET_F_SETLEASE:
  6154. ret = F_SETLEASE;
  6155. break;
  6156. case TARGET_F_GETLEASE:
  6157. ret = F_GETLEASE;
  6158. break;
  6159. #ifdef F_DUPFD_CLOEXEC
  6160. case TARGET_F_DUPFD_CLOEXEC:
  6161. ret = F_DUPFD_CLOEXEC;
  6162. break;
  6163. #endif
  6164. case TARGET_F_NOTIFY:
  6165. ret = F_NOTIFY;
  6166. break;
  6167. #ifdef F_GETOWN_EX
  6168. case TARGET_F_GETOWN_EX:
  6169. ret = F_GETOWN_EX;
  6170. break;
  6171. #endif
  6172. #ifdef F_SETOWN_EX
  6173. case TARGET_F_SETOWN_EX:
  6174. ret = F_SETOWN_EX;
  6175. break;
  6176. #endif
  6177. #ifdef F_SETPIPE_SZ
  6178. case TARGET_F_SETPIPE_SZ:
  6179. ret = F_SETPIPE_SZ;
  6180. break;
  6181. case TARGET_F_GETPIPE_SZ:
  6182. ret = F_GETPIPE_SZ;
  6183. break;
  6184. #endif
  6185. #ifdef F_ADD_SEALS
  6186. case TARGET_F_ADD_SEALS:
  6187. ret = F_ADD_SEALS;
  6188. break;
  6189. case TARGET_F_GET_SEALS:
  6190. ret = F_GET_SEALS;
  6191. break;
  6192. #endif
  6193. default:
  6194. ret = -TARGET_EINVAL;
  6195. break;
  6196. }
  6197. #if defined(__powerpc64__)
  6198. /* On PPC64, glibc headers has the F_*LK* defined to 12, 13 and 14 and
  6199. * is not supported by kernel. The glibc fcntl call actually adjusts
  6200. * them to 5, 6 and 7 before making the syscall(). Since we make the
  6201. * syscall directly, adjust to what is supported by the kernel.
  6202. */
  6203. if (ret >= F_GETLK && ret <= F_SETLKW) {
  6204. ret -= F_GETLK - 5;
  6205. }
  6206. #endif
  6207. return ret;
  6208. }
  6209. #define FLOCK_TRANSTBL \
  6210. switch (type) { \
  6211. TRANSTBL_CONVERT(F_RDLCK); \
  6212. TRANSTBL_CONVERT(F_WRLCK); \
  6213. TRANSTBL_CONVERT(F_UNLCK); \
  6214. }
  6215. static int target_to_host_flock(int type)
  6216. {
  6217. #define TRANSTBL_CONVERT(a) case TARGET_##a: return a
  6218. FLOCK_TRANSTBL
  6219. #undef TRANSTBL_CONVERT
  6220. return -TARGET_EINVAL;
  6221. }
  6222. static int host_to_target_flock(int type)
  6223. {
  6224. #define TRANSTBL_CONVERT(a) case a: return TARGET_##a
  6225. FLOCK_TRANSTBL
  6226. #undef TRANSTBL_CONVERT
  6227. /* if we don't know how to convert the value coming
  6228. * from the host we copy to the target field as-is
  6229. */
  6230. return type;
  6231. }
  6232. static inline abi_long copy_from_user_flock(struct flock *fl,
  6233. abi_ulong target_flock_addr)
  6234. {
  6235. struct target_flock *target_fl;
  6236. int l_type;
  6237. if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
  6238. return -TARGET_EFAULT;
  6239. }
  6240. __get_user(l_type, &target_fl->l_type);
  6241. l_type = target_to_host_flock(l_type);
  6242. if (l_type < 0) {
  6243. return l_type;
  6244. }
  6245. fl->l_type = l_type;
  6246. __get_user(fl->l_whence, &target_fl->l_whence);
  6247. __get_user(fl->l_start, &target_fl->l_start);
  6248. __get_user(fl->l_len, &target_fl->l_len);
  6249. __get_user(fl->l_pid, &target_fl->l_pid);
  6250. unlock_user_struct(target_fl, target_flock_addr, 0);
  6251. return 0;
  6252. }
  6253. static inline abi_long copy_to_user_flock(abi_ulong target_flock_addr,
  6254. const struct flock *fl)
  6255. {
  6256. struct target_flock *target_fl;
  6257. short l_type;
  6258. if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
  6259. return -TARGET_EFAULT;
  6260. }
  6261. l_type = host_to_target_flock(fl->l_type);
  6262. __put_user(l_type, &target_fl->l_type);
  6263. __put_user(fl->l_whence, &target_fl->l_whence);
  6264. __put_user(fl->l_start, &target_fl->l_start);
  6265. __put_user(fl->l_len, &target_fl->l_len);
  6266. __put_user(fl->l_pid, &target_fl->l_pid);
  6267. unlock_user_struct(target_fl, target_flock_addr, 1);
  6268. return 0;
  6269. }
  6270. typedef abi_long from_flock64_fn(struct flock *fl, abi_ulong target_addr);
  6271. typedef abi_long to_flock64_fn(abi_ulong target_addr, const struct flock *fl);
  6272. #if defined(TARGET_ARM) && TARGET_ABI_BITS == 32
  6273. struct target_oabi_flock64 {
  6274. abi_short l_type;
  6275. abi_short l_whence;
  6276. abi_llong l_start;
  6277. abi_llong l_len;
  6278. abi_int l_pid;
  6279. } QEMU_PACKED;
  6280. static inline abi_long copy_from_user_oabi_flock64(struct flock *fl,
  6281. abi_ulong target_flock_addr)
  6282. {
  6283. struct target_oabi_flock64 *target_fl;
  6284. int l_type;
  6285. if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
  6286. return -TARGET_EFAULT;
  6287. }
  6288. __get_user(l_type, &target_fl->l_type);
  6289. l_type = target_to_host_flock(l_type);
  6290. if (l_type < 0) {
  6291. return l_type;
  6292. }
  6293. fl->l_type = l_type;
  6294. __get_user(fl->l_whence, &target_fl->l_whence);
  6295. __get_user(fl->l_start, &target_fl->l_start);
  6296. __get_user(fl->l_len, &target_fl->l_len);
  6297. __get_user(fl->l_pid, &target_fl->l_pid);
  6298. unlock_user_struct(target_fl, target_flock_addr, 0);
  6299. return 0;
  6300. }
  6301. static inline abi_long copy_to_user_oabi_flock64(abi_ulong target_flock_addr,
  6302. const struct flock *fl)
  6303. {
  6304. struct target_oabi_flock64 *target_fl;
  6305. short l_type;
  6306. if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
  6307. return -TARGET_EFAULT;
  6308. }
  6309. l_type = host_to_target_flock(fl->l_type);
  6310. __put_user(l_type, &target_fl->l_type);
  6311. __put_user(fl->l_whence, &target_fl->l_whence);
  6312. __put_user(fl->l_start, &target_fl->l_start);
  6313. __put_user(fl->l_len, &target_fl->l_len);
  6314. __put_user(fl->l_pid, &target_fl->l_pid);
  6315. unlock_user_struct(target_fl, target_flock_addr, 1);
  6316. return 0;
  6317. }
  6318. #endif
  6319. static inline abi_long copy_from_user_flock64(struct flock *fl,
  6320. abi_ulong target_flock_addr)
  6321. {
  6322. struct target_flock64 *target_fl;
  6323. int l_type;
  6324. if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
  6325. return -TARGET_EFAULT;
  6326. }
  6327. __get_user(l_type, &target_fl->l_type);
  6328. l_type = target_to_host_flock(l_type);
  6329. if (l_type < 0) {
  6330. return l_type;
  6331. }
  6332. fl->l_type = l_type;
  6333. __get_user(fl->l_whence, &target_fl->l_whence);
  6334. __get_user(fl->l_start, &target_fl->l_start);
  6335. __get_user(fl->l_len, &target_fl->l_len);
  6336. __get_user(fl->l_pid, &target_fl->l_pid);
  6337. unlock_user_struct(target_fl, target_flock_addr, 0);
  6338. return 0;
  6339. }
  6340. static inline abi_long copy_to_user_flock64(abi_ulong target_flock_addr,
  6341. const struct flock *fl)
  6342. {
  6343. struct target_flock64 *target_fl;
  6344. short l_type;
  6345. if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
  6346. return -TARGET_EFAULT;
  6347. }
  6348. l_type = host_to_target_flock(fl->l_type);
  6349. __put_user(l_type, &target_fl->l_type);
  6350. __put_user(fl->l_whence, &target_fl->l_whence);
  6351. __put_user(fl->l_start, &target_fl->l_start);
  6352. __put_user(fl->l_len, &target_fl->l_len);
  6353. __put_user(fl->l_pid, &target_fl->l_pid);
  6354. unlock_user_struct(target_fl, target_flock_addr, 1);
  6355. return 0;
  6356. }
  6357. static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
  6358. {
  6359. struct flock fl;
  6360. #ifdef F_GETOWN_EX
  6361. struct f_owner_ex fox;
  6362. struct target_f_owner_ex *target_fox;
  6363. #endif
  6364. abi_long ret;
  6365. int host_cmd = target_to_host_fcntl_cmd(cmd);
  6366. if (host_cmd == -TARGET_EINVAL)
  6367. return host_cmd;
  6368. switch(cmd) {
  6369. case TARGET_F_GETLK:
  6370. ret = copy_from_user_flock(&fl, arg);
  6371. if (ret) {
  6372. return ret;
  6373. }
  6374. ret = get_errno(safe_fcntl(fd, host_cmd, &fl));
  6375. if (ret == 0) {
  6376. ret = copy_to_user_flock(arg, &fl);
  6377. }
  6378. break;
  6379. case TARGET_F_SETLK:
  6380. case TARGET_F_SETLKW:
  6381. ret = copy_from_user_flock(&fl, arg);
  6382. if (ret) {
  6383. return ret;
  6384. }
  6385. ret = get_errno(safe_fcntl(fd, host_cmd, &fl));
  6386. break;
  6387. case TARGET_F_GETLK64:
  6388. case TARGET_F_OFD_GETLK:
  6389. ret = copy_from_user_flock64(&fl, arg);
  6390. if (ret) {
  6391. return ret;
  6392. }
  6393. ret = get_errno(safe_fcntl(fd, host_cmd, &fl));
  6394. if (ret == 0) {
  6395. ret = copy_to_user_flock64(arg, &fl);
  6396. }
  6397. break;
  6398. case TARGET_F_SETLK64:
  6399. case TARGET_F_SETLKW64:
  6400. case TARGET_F_OFD_SETLK:
  6401. case TARGET_F_OFD_SETLKW:
  6402. ret = copy_from_user_flock64(&fl, arg);
  6403. if (ret) {
  6404. return ret;
  6405. }
  6406. ret = get_errno(safe_fcntl(fd, host_cmd, &fl));
  6407. break;
  6408. case TARGET_F_GETFL:
  6409. ret = get_errno(safe_fcntl(fd, host_cmd, arg));
  6410. if (ret >= 0) {
  6411. ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
  6412. /* tell 32-bit guests it uses largefile on 64-bit hosts: */
  6413. if (O_LARGEFILE == 0 && HOST_LONG_BITS == 64) {
  6414. ret |= TARGET_O_LARGEFILE;
  6415. }
  6416. }
  6417. break;
  6418. case TARGET_F_SETFL:
  6419. ret = get_errno(safe_fcntl(fd, host_cmd,
  6420. target_to_host_bitmask(arg,
  6421. fcntl_flags_tbl)));
  6422. break;
  6423. #ifdef F_GETOWN_EX
  6424. case TARGET_F_GETOWN_EX:
  6425. ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
  6426. if (ret >= 0) {
  6427. if (!lock_user_struct(VERIFY_WRITE, target_fox, arg, 0))
  6428. return -TARGET_EFAULT;
  6429. target_fox->type = tswap32(fox.type);
  6430. target_fox->pid = tswap32(fox.pid);
  6431. unlock_user_struct(target_fox, arg, 1);
  6432. }
  6433. break;
  6434. #endif
  6435. #ifdef F_SETOWN_EX
  6436. case TARGET_F_SETOWN_EX:
  6437. if (!lock_user_struct(VERIFY_READ, target_fox, arg, 1))
  6438. return -TARGET_EFAULT;
  6439. fox.type = tswap32(target_fox->type);
  6440. fox.pid = tswap32(target_fox->pid);
  6441. unlock_user_struct(target_fox, arg, 0);
  6442. ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
  6443. break;
  6444. #endif
  6445. case TARGET_F_SETSIG:
  6446. ret = get_errno(safe_fcntl(fd, host_cmd, target_to_host_signal(arg)));
  6447. break;
  6448. case TARGET_F_GETSIG:
  6449. ret = host_to_target_signal(get_errno(safe_fcntl(fd, host_cmd, arg)));
  6450. break;
  6451. case TARGET_F_SETOWN:
  6452. case TARGET_F_GETOWN:
  6453. case TARGET_F_SETLEASE:
  6454. case TARGET_F_GETLEASE:
  6455. case TARGET_F_SETPIPE_SZ:
  6456. case TARGET_F_GETPIPE_SZ:
  6457. case TARGET_F_ADD_SEALS:
  6458. case TARGET_F_GET_SEALS:
  6459. ret = get_errno(safe_fcntl(fd, host_cmd, arg));
  6460. break;
  6461. default:
  6462. ret = get_errno(safe_fcntl(fd, cmd, arg));
  6463. break;
  6464. }
  6465. return ret;
  6466. }
  6467. #ifdef USE_UID16
  6468. static inline int high2lowuid(int uid)
  6469. {
  6470. if (uid > 65535)
  6471. return 65534;
  6472. else
  6473. return uid;
  6474. }
  6475. static inline int high2lowgid(int gid)
  6476. {
  6477. if (gid > 65535)
  6478. return 65534;
  6479. else
  6480. return gid;
  6481. }
  6482. static inline int low2highuid(int uid)
  6483. {
  6484. if ((int16_t)uid == -1)
  6485. return -1;
  6486. else
  6487. return uid;
  6488. }
  6489. static inline int low2highgid(int gid)
  6490. {
  6491. if ((int16_t)gid == -1)
  6492. return -1;
  6493. else
  6494. return gid;
  6495. }
  6496. static inline int tswapid(int id)
  6497. {
  6498. return tswap16(id);
  6499. }
  6500. #define put_user_id(x, gaddr) put_user_u16(x, gaddr)
  6501. #else /* !USE_UID16 */
  6502. static inline int high2lowuid(int uid)
  6503. {
  6504. return uid;
  6505. }
  6506. static inline int high2lowgid(int gid)
  6507. {
  6508. return gid;
  6509. }
  6510. static inline int low2highuid(int uid)
  6511. {
  6512. return uid;
  6513. }
  6514. static inline int low2highgid(int gid)
  6515. {
  6516. return gid;
  6517. }
  6518. static inline int tswapid(int id)
  6519. {
  6520. return tswap32(id);
  6521. }
  6522. #define put_user_id(x, gaddr) put_user_u32(x, gaddr)
  6523. #endif /* USE_UID16 */
  6524. /* We must do direct syscalls for setting UID/GID, because we want to
  6525. * implement the Linux system call semantics of "change only for this thread",
  6526. * not the libc/POSIX semantics of "change for all threads in process".
  6527. * (See http://ewontfix.com/17/ for more details.)
  6528. * We use the 32-bit version of the syscalls if present; if it is not
  6529. * then either the host architecture supports 32-bit UIDs natively with
  6530. * the standard syscall, or the 16-bit UID is the best we can do.
  6531. */
  6532. #ifdef __NR_setuid32
  6533. #define __NR_sys_setuid __NR_setuid32
  6534. #else
  6535. #define __NR_sys_setuid __NR_setuid
  6536. #endif
  6537. #ifdef __NR_setgid32
  6538. #define __NR_sys_setgid __NR_setgid32
  6539. #else
  6540. #define __NR_sys_setgid __NR_setgid
  6541. #endif
  6542. #ifdef __NR_setresuid32
  6543. #define __NR_sys_setresuid __NR_setresuid32
  6544. #else
  6545. #define __NR_sys_setresuid __NR_setresuid
  6546. #endif
  6547. #ifdef __NR_setresgid32
  6548. #define __NR_sys_setresgid __NR_setresgid32
  6549. #else
  6550. #define __NR_sys_setresgid __NR_setresgid
  6551. #endif
  6552. #ifdef __NR_setgroups32
  6553. #define __NR_sys_setgroups __NR_setgroups32
  6554. #else
  6555. #define __NR_sys_setgroups __NR_setgroups
  6556. #endif
  6557. #ifdef __NR_sys_setreuid32
  6558. #define __NR_sys_setreuid __NR_setreuid32
  6559. #else
  6560. #define __NR_sys_setreuid __NR_setreuid
  6561. #endif
  6562. #ifdef __NR_sys_setregid32
  6563. #define __NR_sys_setregid __NR_setregid32
  6564. #else
  6565. #define __NR_sys_setregid __NR_setregid
  6566. #endif
  6567. _syscall1(int, sys_setuid, uid_t, uid)
  6568. _syscall1(int, sys_setgid, gid_t, gid)
  6569. _syscall3(int, sys_setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
  6570. _syscall3(int, sys_setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
  6571. _syscall2(int, sys_setgroups, int, size, gid_t *, grouplist)
  6572. _syscall2(int, sys_setreuid, uid_t, ruid, uid_t, euid);
  6573. _syscall2(int, sys_setregid, gid_t, rgid, gid_t, egid);
  6574. void syscall_init(void)
  6575. {
  6576. IOCTLEntry *ie;
  6577. const argtype *arg_type;
  6578. int size;
  6579. thunk_init(STRUCT_MAX);
  6580. #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
  6581. #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
  6582. #include "syscall_types.h"
  6583. #undef STRUCT
  6584. #undef STRUCT_SPECIAL
  6585. /* we patch the ioctl size if necessary. We rely on the fact that
  6586. no ioctl has all the bits at '1' in the size field */
  6587. ie = ioctl_entries;
  6588. while (ie->target_cmd != 0) {
  6589. if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
  6590. TARGET_IOC_SIZEMASK) {
  6591. arg_type = ie->arg_type;
  6592. if (arg_type[0] != TYPE_PTR) {
  6593. fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
  6594. ie->target_cmd);
  6595. exit(1);
  6596. }
  6597. arg_type++;
  6598. size = thunk_type_size(arg_type, 0);
  6599. ie->target_cmd = (ie->target_cmd &
  6600. ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
  6601. (size << TARGET_IOC_SIZESHIFT);
  6602. }
  6603. /* automatic consistency check if same arch */
  6604. #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
  6605. (defined(__x86_64__) && defined(TARGET_X86_64))
  6606. if (unlikely(ie->target_cmd != ie->host_cmd)) {
  6607. fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
  6608. ie->name, ie->target_cmd, ie->host_cmd);
  6609. }
  6610. #endif
  6611. ie++;
  6612. }
  6613. }
  6614. #ifdef TARGET_NR_truncate64
  6615. static inline abi_long target_truncate64(CPUArchState *cpu_env, const char *arg1,
  6616. abi_long arg2,
  6617. abi_long arg3,
  6618. abi_long arg4)
  6619. {
  6620. if (regpairs_aligned(cpu_env, TARGET_NR_truncate64)) {
  6621. arg2 = arg3;
  6622. arg3 = arg4;
  6623. }
  6624. return get_errno(truncate(arg1, target_offset64(arg2, arg3)));
  6625. }
  6626. #endif
  6627. #ifdef TARGET_NR_ftruncate64
  6628. static inline abi_long target_ftruncate64(CPUArchState *cpu_env, abi_long arg1,
  6629. abi_long arg2,
  6630. abi_long arg3,
  6631. abi_long arg4)
  6632. {
  6633. if (regpairs_aligned(cpu_env, TARGET_NR_ftruncate64)) {
  6634. arg2 = arg3;
  6635. arg3 = arg4;
  6636. }
  6637. return get_errno(ftruncate(arg1, target_offset64(arg2, arg3)));
  6638. }
  6639. #endif
  6640. #if defined(TARGET_NR_timer_settime) || \
  6641. (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
  6642. static inline abi_long target_to_host_itimerspec(struct itimerspec *host_its,
  6643. abi_ulong target_addr)
  6644. {
  6645. if (target_to_host_timespec(&host_its->it_interval, target_addr +
  6646. offsetof(struct target_itimerspec,
  6647. it_interval)) ||
  6648. target_to_host_timespec(&host_its->it_value, target_addr +
  6649. offsetof(struct target_itimerspec,
  6650. it_value))) {
  6651. return -TARGET_EFAULT;
  6652. }
  6653. return 0;
  6654. }
  6655. #endif
  6656. #if defined(TARGET_NR_timer_settime64) || \
  6657. (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD))
  6658. static inline abi_long target_to_host_itimerspec64(struct itimerspec *host_its,
  6659. abi_ulong target_addr)
  6660. {
  6661. if (target_to_host_timespec64(&host_its->it_interval, target_addr +
  6662. offsetof(struct target__kernel_itimerspec,
  6663. it_interval)) ||
  6664. target_to_host_timespec64(&host_its->it_value, target_addr +
  6665. offsetof(struct target__kernel_itimerspec,
  6666. it_value))) {
  6667. return -TARGET_EFAULT;
  6668. }
  6669. return 0;
  6670. }
  6671. #endif
  6672. #if ((defined(TARGET_NR_timerfd_gettime) || \
  6673. defined(TARGET_NR_timerfd_settime)) && defined(CONFIG_TIMERFD)) || \
  6674. defined(TARGET_NR_timer_gettime) || defined(TARGET_NR_timer_settime)
  6675. static inline abi_long host_to_target_itimerspec(abi_ulong target_addr,
  6676. struct itimerspec *host_its)
  6677. {
  6678. if (host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
  6679. it_interval),
  6680. &host_its->it_interval) ||
  6681. host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
  6682. it_value),
  6683. &host_its->it_value)) {
  6684. return -TARGET_EFAULT;
  6685. }
  6686. return 0;
  6687. }
  6688. #endif
  6689. #if ((defined(TARGET_NR_timerfd_gettime64) || \
  6690. defined(TARGET_NR_timerfd_settime64)) && defined(CONFIG_TIMERFD)) || \
  6691. defined(TARGET_NR_timer_gettime64) || defined(TARGET_NR_timer_settime64)
  6692. static inline abi_long host_to_target_itimerspec64(abi_ulong target_addr,
  6693. struct itimerspec *host_its)
  6694. {
  6695. if (host_to_target_timespec64(target_addr +
  6696. offsetof(struct target__kernel_itimerspec,
  6697. it_interval),
  6698. &host_its->it_interval) ||
  6699. host_to_target_timespec64(target_addr +
  6700. offsetof(struct target__kernel_itimerspec,
  6701. it_value),
  6702. &host_its->it_value)) {
  6703. return -TARGET_EFAULT;
  6704. }
  6705. return 0;
  6706. }
  6707. #endif
  6708. #if defined(TARGET_NR_adjtimex) || \
  6709. (defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME))
  6710. static inline abi_long target_to_host_timex(struct timex *host_tx,
  6711. abi_long target_addr)
  6712. {
  6713. struct target_timex *target_tx;
  6714. if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) {
  6715. return -TARGET_EFAULT;
  6716. }
  6717. __get_user(host_tx->modes, &target_tx->modes);
  6718. __get_user(host_tx->offset, &target_tx->offset);
  6719. __get_user(host_tx->freq, &target_tx->freq);
  6720. __get_user(host_tx->maxerror, &target_tx->maxerror);
  6721. __get_user(host_tx->esterror, &target_tx->esterror);
  6722. __get_user(host_tx->status, &target_tx->status);
  6723. __get_user(host_tx->constant, &target_tx->constant);
  6724. __get_user(host_tx->precision, &target_tx->precision);
  6725. __get_user(host_tx->tolerance, &target_tx->tolerance);
  6726. __get_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
  6727. __get_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
  6728. __get_user(host_tx->tick, &target_tx->tick);
  6729. __get_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  6730. __get_user(host_tx->jitter, &target_tx->jitter);
  6731. __get_user(host_tx->shift, &target_tx->shift);
  6732. __get_user(host_tx->stabil, &target_tx->stabil);
  6733. __get_user(host_tx->jitcnt, &target_tx->jitcnt);
  6734. __get_user(host_tx->calcnt, &target_tx->calcnt);
  6735. __get_user(host_tx->errcnt, &target_tx->errcnt);
  6736. __get_user(host_tx->stbcnt, &target_tx->stbcnt);
  6737. __get_user(host_tx->tai, &target_tx->tai);
  6738. unlock_user_struct(target_tx, target_addr, 0);
  6739. return 0;
  6740. }
  6741. static inline abi_long host_to_target_timex(abi_long target_addr,
  6742. struct timex *host_tx)
  6743. {
  6744. struct target_timex *target_tx;
  6745. if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) {
  6746. return -TARGET_EFAULT;
  6747. }
  6748. __put_user(host_tx->modes, &target_tx->modes);
  6749. __put_user(host_tx->offset, &target_tx->offset);
  6750. __put_user(host_tx->freq, &target_tx->freq);
  6751. __put_user(host_tx->maxerror, &target_tx->maxerror);
  6752. __put_user(host_tx->esterror, &target_tx->esterror);
  6753. __put_user(host_tx->status, &target_tx->status);
  6754. __put_user(host_tx->constant, &target_tx->constant);
  6755. __put_user(host_tx->precision, &target_tx->precision);
  6756. __put_user(host_tx->tolerance, &target_tx->tolerance);
  6757. __put_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
  6758. __put_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
  6759. __put_user(host_tx->tick, &target_tx->tick);
  6760. __put_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  6761. __put_user(host_tx->jitter, &target_tx->jitter);
  6762. __put_user(host_tx->shift, &target_tx->shift);
  6763. __put_user(host_tx->stabil, &target_tx->stabil);
  6764. __put_user(host_tx->jitcnt, &target_tx->jitcnt);
  6765. __put_user(host_tx->calcnt, &target_tx->calcnt);
  6766. __put_user(host_tx->errcnt, &target_tx->errcnt);
  6767. __put_user(host_tx->stbcnt, &target_tx->stbcnt);
  6768. __put_user(host_tx->tai, &target_tx->tai);
  6769. unlock_user_struct(target_tx, target_addr, 1);
  6770. return 0;
  6771. }
  6772. #endif
  6773. #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
  6774. static inline abi_long target_to_host_timex64(struct timex *host_tx,
  6775. abi_long target_addr)
  6776. {
  6777. struct target__kernel_timex *target_tx;
  6778. if (copy_from_user_timeval64(&host_tx->time, target_addr +
  6779. offsetof(struct target__kernel_timex,
  6780. time))) {
  6781. return -TARGET_EFAULT;
  6782. }
  6783. if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) {
  6784. return -TARGET_EFAULT;
  6785. }
  6786. __get_user(host_tx->modes, &target_tx->modes);
  6787. __get_user(host_tx->offset, &target_tx->offset);
  6788. __get_user(host_tx->freq, &target_tx->freq);
  6789. __get_user(host_tx->maxerror, &target_tx->maxerror);
  6790. __get_user(host_tx->esterror, &target_tx->esterror);
  6791. __get_user(host_tx->status, &target_tx->status);
  6792. __get_user(host_tx->constant, &target_tx->constant);
  6793. __get_user(host_tx->precision, &target_tx->precision);
  6794. __get_user(host_tx->tolerance, &target_tx->tolerance);
  6795. __get_user(host_tx->tick, &target_tx->tick);
  6796. __get_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  6797. __get_user(host_tx->jitter, &target_tx->jitter);
  6798. __get_user(host_tx->shift, &target_tx->shift);
  6799. __get_user(host_tx->stabil, &target_tx->stabil);
  6800. __get_user(host_tx->jitcnt, &target_tx->jitcnt);
  6801. __get_user(host_tx->calcnt, &target_tx->calcnt);
  6802. __get_user(host_tx->errcnt, &target_tx->errcnt);
  6803. __get_user(host_tx->stbcnt, &target_tx->stbcnt);
  6804. __get_user(host_tx->tai, &target_tx->tai);
  6805. unlock_user_struct(target_tx, target_addr, 0);
  6806. return 0;
  6807. }
  6808. static inline abi_long host_to_target_timex64(abi_long target_addr,
  6809. struct timex *host_tx)
  6810. {
  6811. struct target__kernel_timex *target_tx;
  6812. if (copy_to_user_timeval64(target_addr +
  6813. offsetof(struct target__kernel_timex, time),
  6814. &host_tx->time)) {
  6815. return -TARGET_EFAULT;
  6816. }
  6817. if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) {
  6818. return -TARGET_EFAULT;
  6819. }
  6820. __put_user(host_tx->modes, &target_tx->modes);
  6821. __put_user(host_tx->offset, &target_tx->offset);
  6822. __put_user(host_tx->freq, &target_tx->freq);
  6823. __put_user(host_tx->maxerror, &target_tx->maxerror);
  6824. __put_user(host_tx->esterror, &target_tx->esterror);
  6825. __put_user(host_tx->status, &target_tx->status);
  6826. __put_user(host_tx->constant, &target_tx->constant);
  6827. __put_user(host_tx->precision, &target_tx->precision);
  6828. __put_user(host_tx->tolerance, &target_tx->tolerance);
  6829. __put_user(host_tx->tick, &target_tx->tick);
  6830. __put_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  6831. __put_user(host_tx->jitter, &target_tx->jitter);
  6832. __put_user(host_tx->shift, &target_tx->shift);
  6833. __put_user(host_tx->stabil, &target_tx->stabil);
  6834. __put_user(host_tx->jitcnt, &target_tx->jitcnt);
  6835. __put_user(host_tx->calcnt, &target_tx->calcnt);
  6836. __put_user(host_tx->errcnt, &target_tx->errcnt);
  6837. __put_user(host_tx->stbcnt, &target_tx->stbcnt);
  6838. __put_user(host_tx->tai, &target_tx->tai);
  6839. unlock_user_struct(target_tx, target_addr, 1);
  6840. return 0;
  6841. }
  6842. #endif
  6843. #ifndef HAVE_SIGEV_NOTIFY_THREAD_ID
  6844. #define sigev_notify_thread_id _sigev_un._tid
  6845. #endif
  6846. static inline abi_long target_to_host_sigevent(struct sigevent *host_sevp,
  6847. abi_ulong target_addr)
  6848. {
  6849. struct target_sigevent *target_sevp;
  6850. if (!lock_user_struct(VERIFY_READ, target_sevp, target_addr, 1)) {
  6851. return -TARGET_EFAULT;
  6852. }
  6853. /* This union is awkward on 64 bit systems because it has a 32 bit
  6854. * integer and a pointer in it; we follow the conversion approach
  6855. * used for handling sigval types in signal.c so the guest should get
  6856. * the correct value back even if we did a 64 bit byteswap and it's
  6857. * using the 32 bit integer.
  6858. */
  6859. host_sevp->sigev_value.sival_ptr =
  6860. (void *)(uintptr_t)tswapal(target_sevp->sigev_value.sival_ptr);
  6861. host_sevp->sigev_signo =
  6862. target_to_host_signal(tswap32(target_sevp->sigev_signo));
  6863. host_sevp->sigev_notify = tswap32(target_sevp->sigev_notify);
  6864. host_sevp->sigev_notify_thread_id = tswap32(target_sevp->_sigev_un._tid);
  6865. unlock_user_struct(target_sevp, target_addr, 1);
  6866. return 0;
  6867. }
  6868. #if defined(TARGET_NR_mlockall)
  6869. static inline int target_to_host_mlockall_arg(int arg)
  6870. {
  6871. int result = 0;
  6872. if (arg & TARGET_MCL_CURRENT) {
  6873. result |= MCL_CURRENT;
  6874. }
  6875. if (arg & TARGET_MCL_FUTURE) {
  6876. result |= MCL_FUTURE;
  6877. }
  6878. #ifdef MCL_ONFAULT
  6879. if (arg & TARGET_MCL_ONFAULT) {
  6880. result |= MCL_ONFAULT;
  6881. }
  6882. #endif
  6883. return result;
  6884. }
  6885. #endif
  6886. static inline int target_to_host_msync_arg(abi_long arg)
  6887. {
  6888. return ((arg & TARGET_MS_ASYNC) ? MS_ASYNC : 0) |
  6889. ((arg & TARGET_MS_INVALIDATE) ? MS_INVALIDATE : 0) |
  6890. ((arg & TARGET_MS_SYNC) ? MS_SYNC : 0) |
  6891. (arg & ~(TARGET_MS_ASYNC | TARGET_MS_INVALIDATE | TARGET_MS_SYNC));
  6892. }
  6893. #if (defined(TARGET_NR_stat64) || defined(TARGET_NR_lstat64) || \
  6894. defined(TARGET_NR_fstat64) || defined(TARGET_NR_fstatat64) || \
  6895. defined(TARGET_NR_newfstatat))
  6896. static inline abi_long host_to_target_stat64(CPUArchState *cpu_env,
  6897. abi_ulong target_addr,
  6898. struct stat *host_st)
  6899. {
  6900. #if defined(TARGET_ARM) && defined(TARGET_ABI32)
  6901. if (cpu_env->eabi) {
  6902. struct target_eabi_stat64 *target_st;
  6903. if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
  6904. return -TARGET_EFAULT;
  6905. memset(target_st, 0, sizeof(struct target_eabi_stat64));
  6906. __put_user(host_st->st_dev, &target_st->st_dev);
  6907. __put_user(host_st->st_ino, &target_st->st_ino);
  6908. #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
  6909. __put_user(host_st->st_ino, &target_st->__st_ino);
  6910. #endif
  6911. __put_user(host_st->st_mode, &target_st->st_mode);
  6912. __put_user(host_st->st_nlink, &target_st->st_nlink);
  6913. __put_user(host_st->st_uid, &target_st->st_uid);
  6914. __put_user(host_st->st_gid, &target_st->st_gid);
  6915. __put_user(host_st->st_rdev, &target_st->st_rdev);
  6916. __put_user(host_st->st_size, &target_st->st_size);
  6917. __put_user(host_st->st_blksize, &target_st->st_blksize);
  6918. __put_user(host_st->st_blocks, &target_st->st_blocks);
  6919. __put_user(host_st->st_atime, &target_st->target_st_atime);
  6920. __put_user(host_st->st_mtime, &target_st->target_st_mtime);
  6921. __put_user(host_st->st_ctime, &target_st->target_st_ctime);
  6922. #ifdef HAVE_STRUCT_STAT_ST_ATIM
  6923. __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
  6924. __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
  6925. __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
  6926. #endif
  6927. unlock_user_struct(target_st, target_addr, 1);
  6928. } else
  6929. #endif
  6930. {
  6931. #if defined(TARGET_HAS_STRUCT_STAT64)
  6932. struct target_stat64 *target_st;
  6933. #else
  6934. struct target_stat *target_st;
  6935. #endif
  6936. if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
  6937. return -TARGET_EFAULT;
  6938. memset(target_st, 0, sizeof(*target_st));
  6939. __put_user(host_st->st_dev, &target_st->st_dev);
  6940. __put_user(host_st->st_ino, &target_st->st_ino);
  6941. #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
  6942. __put_user(host_st->st_ino, &target_st->__st_ino);
  6943. #endif
  6944. __put_user(host_st->st_mode, &target_st->st_mode);
  6945. __put_user(host_st->st_nlink, &target_st->st_nlink);
  6946. __put_user(host_st->st_uid, &target_st->st_uid);
  6947. __put_user(host_st->st_gid, &target_st->st_gid);
  6948. __put_user(host_st->st_rdev, &target_st->st_rdev);
  6949. /* XXX: better use of kernel struct */
  6950. __put_user(host_st->st_size, &target_st->st_size);
  6951. __put_user(host_st->st_blksize, &target_st->st_blksize);
  6952. __put_user(host_st->st_blocks, &target_st->st_blocks);
  6953. __put_user(host_st->st_atime, &target_st->target_st_atime);
  6954. __put_user(host_st->st_mtime, &target_st->target_st_mtime);
  6955. __put_user(host_st->st_ctime, &target_st->target_st_ctime);
  6956. #ifdef HAVE_STRUCT_STAT_ST_ATIM
  6957. __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
  6958. __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
  6959. __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
  6960. #endif
  6961. unlock_user_struct(target_st, target_addr, 1);
  6962. }
  6963. return 0;
  6964. }
  6965. #endif
  6966. #if defined(TARGET_NR_statx) && defined(__NR_statx)
  6967. static inline abi_long host_to_target_statx(struct target_statx *host_stx,
  6968. abi_ulong target_addr)
  6969. {
  6970. struct target_statx *target_stx;
  6971. if (!lock_user_struct(VERIFY_WRITE, target_stx, target_addr, 0)) {
  6972. return -TARGET_EFAULT;
  6973. }
  6974. memset(target_stx, 0, sizeof(*target_stx));
  6975. __put_user(host_stx->stx_mask, &target_stx->stx_mask);
  6976. __put_user(host_stx->stx_blksize, &target_stx->stx_blksize);
  6977. __put_user(host_stx->stx_attributes, &target_stx->stx_attributes);
  6978. __put_user(host_stx->stx_nlink, &target_stx->stx_nlink);
  6979. __put_user(host_stx->stx_uid, &target_stx->stx_uid);
  6980. __put_user(host_stx->stx_gid, &target_stx->stx_gid);
  6981. __put_user(host_stx->stx_mode, &target_stx->stx_mode);
  6982. __put_user(host_stx->stx_ino, &target_stx->stx_ino);
  6983. __put_user(host_stx->stx_size, &target_stx->stx_size);
  6984. __put_user(host_stx->stx_blocks, &target_stx->stx_blocks);
  6985. __put_user(host_stx->stx_attributes_mask, &target_stx->stx_attributes_mask);
  6986. __put_user(host_stx->stx_atime.tv_sec, &target_stx->stx_atime.tv_sec);
  6987. __put_user(host_stx->stx_atime.tv_nsec, &target_stx->stx_atime.tv_nsec);
  6988. __put_user(host_stx->stx_btime.tv_sec, &target_stx->stx_btime.tv_sec);
  6989. __put_user(host_stx->stx_btime.tv_nsec, &target_stx->stx_btime.tv_nsec);
  6990. __put_user(host_stx->stx_ctime.tv_sec, &target_stx->stx_ctime.tv_sec);
  6991. __put_user(host_stx->stx_ctime.tv_nsec, &target_stx->stx_ctime.tv_nsec);
  6992. __put_user(host_stx->stx_mtime.tv_sec, &target_stx->stx_mtime.tv_sec);
  6993. __put_user(host_stx->stx_mtime.tv_nsec, &target_stx->stx_mtime.tv_nsec);
  6994. __put_user(host_stx->stx_rdev_major, &target_stx->stx_rdev_major);
  6995. __put_user(host_stx->stx_rdev_minor, &target_stx->stx_rdev_minor);
  6996. __put_user(host_stx->stx_dev_major, &target_stx->stx_dev_major);
  6997. __put_user(host_stx->stx_dev_minor, &target_stx->stx_dev_minor);
  6998. unlock_user_struct(target_stx, target_addr, 1);
  6999. return 0;
  7000. }
  7001. #endif
  7002. static int do_sys_futex(int *uaddr, int op, int val,
  7003. const struct timespec *timeout, int *uaddr2,
  7004. int val3)
  7005. {
  7006. #if HOST_LONG_BITS == 64
  7007. #if defined(__NR_futex)
  7008. /* always a 64-bit time_t, it doesn't define _time64 version */
  7009. return sys_futex(uaddr, op, val, timeout, uaddr2, val3);
  7010. #endif
  7011. #else /* HOST_LONG_BITS == 64 */
  7012. #if defined(__NR_futex_time64)
  7013. if (sizeof(timeout->tv_sec) == 8) {
  7014. /* _time64 function on 32bit arch */
  7015. return sys_futex_time64(uaddr, op, val, timeout, uaddr2, val3);
  7016. }
  7017. #endif
  7018. #if defined(__NR_futex)
  7019. /* old function on 32bit arch */
  7020. return sys_futex(uaddr, op, val, timeout, uaddr2, val3);
  7021. #endif
  7022. #endif /* HOST_LONG_BITS == 64 */
  7023. g_assert_not_reached();
  7024. }
  7025. static int do_safe_futex(int *uaddr, int op, int val,
  7026. const struct timespec *timeout, int *uaddr2,
  7027. int val3)
  7028. {
  7029. #if HOST_LONG_BITS == 64
  7030. #if defined(__NR_futex)
  7031. /* always a 64-bit time_t, it doesn't define _time64 version */
  7032. return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3));
  7033. #endif
  7034. #else /* HOST_LONG_BITS == 64 */
  7035. #if defined(__NR_futex_time64)
  7036. if (sizeof(timeout->tv_sec) == 8) {
  7037. /* _time64 function on 32bit arch */
  7038. return get_errno(safe_futex_time64(uaddr, op, val, timeout, uaddr2,
  7039. val3));
  7040. }
  7041. #endif
  7042. #if defined(__NR_futex)
  7043. /* old function on 32bit arch */
  7044. return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3));
  7045. #endif
  7046. #endif /* HOST_LONG_BITS == 64 */
  7047. return -TARGET_ENOSYS;
  7048. }
  7049. /* ??? Using host futex calls even when target atomic operations
  7050. are not really atomic probably breaks things. However implementing
  7051. futexes locally would make futexes shared between multiple processes
  7052. tricky. However they're probably useless because guest atomic
  7053. operations won't work either. */
  7054. #if defined(TARGET_NR_futex) || defined(TARGET_NR_futex_time64)
  7055. static int do_futex(CPUState *cpu, bool time64, target_ulong uaddr,
  7056. int op, int val, target_ulong timeout,
  7057. target_ulong uaddr2, int val3)
  7058. {
  7059. struct timespec ts, *pts = NULL;
  7060. void *haddr2 = NULL;
  7061. int base_op;
  7062. /* We assume FUTEX_* constants are the same on both host and target. */
  7063. #ifdef FUTEX_CMD_MASK
  7064. base_op = op & FUTEX_CMD_MASK;
  7065. #else
  7066. base_op = op;
  7067. #endif
  7068. switch (base_op) {
  7069. case FUTEX_WAIT:
  7070. case FUTEX_WAIT_BITSET:
  7071. val = tswap32(val);
  7072. break;
  7073. case FUTEX_WAIT_REQUEUE_PI:
  7074. val = tswap32(val);
  7075. haddr2 = g2h(cpu, uaddr2);
  7076. break;
  7077. case FUTEX_LOCK_PI:
  7078. case FUTEX_LOCK_PI2:
  7079. break;
  7080. case FUTEX_WAKE:
  7081. case FUTEX_WAKE_BITSET:
  7082. case FUTEX_TRYLOCK_PI:
  7083. case FUTEX_UNLOCK_PI:
  7084. timeout = 0;
  7085. break;
  7086. case FUTEX_FD:
  7087. val = target_to_host_signal(val);
  7088. timeout = 0;
  7089. break;
  7090. case FUTEX_CMP_REQUEUE:
  7091. case FUTEX_CMP_REQUEUE_PI:
  7092. val3 = tswap32(val3);
  7093. /* fall through */
  7094. case FUTEX_REQUEUE:
  7095. case FUTEX_WAKE_OP:
  7096. /*
  7097. * For these, the 4th argument is not TIMEOUT, but VAL2.
  7098. * But the prototype of do_safe_futex takes a pointer, so
  7099. * insert casts to satisfy the compiler. We do not need
  7100. * to tswap VAL2 since it's not compared to guest memory.
  7101. */
  7102. pts = (struct timespec *)(uintptr_t)timeout;
  7103. timeout = 0;
  7104. haddr2 = g2h(cpu, uaddr2);
  7105. break;
  7106. default:
  7107. return -TARGET_ENOSYS;
  7108. }
  7109. if (timeout) {
  7110. pts = &ts;
  7111. if (time64
  7112. ? target_to_host_timespec64(pts, timeout)
  7113. : target_to_host_timespec(pts, timeout)) {
  7114. return -TARGET_EFAULT;
  7115. }
  7116. }
  7117. return do_safe_futex(g2h(cpu, uaddr), op, val, pts, haddr2, val3);
  7118. }
  7119. #endif
  7120. #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  7121. static abi_long do_name_to_handle_at(abi_long dirfd, abi_long pathname,
  7122. abi_long handle, abi_long mount_id,
  7123. abi_long flags)
  7124. {
  7125. struct file_handle *target_fh;
  7126. struct file_handle *fh;
  7127. int mid = 0;
  7128. abi_long ret;
  7129. char *name;
  7130. unsigned int size, total_size;
  7131. if (get_user_s32(size, handle)) {
  7132. return -TARGET_EFAULT;
  7133. }
  7134. name = lock_user_string(pathname);
  7135. if (!name) {
  7136. return -TARGET_EFAULT;
  7137. }
  7138. total_size = sizeof(struct file_handle) + size;
  7139. target_fh = lock_user(VERIFY_WRITE, handle, total_size, 0);
  7140. if (!target_fh) {
  7141. unlock_user(name, pathname, 0);
  7142. return -TARGET_EFAULT;
  7143. }
  7144. fh = g_malloc0(total_size);
  7145. fh->handle_bytes = size;
  7146. ret = get_errno(name_to_handle_at(dirfd, path(name), fh, &mid, flags));
  7147. unlock_user(name, pathname, 0);
  7148. /* man name_to_handle_at(2):
  7149. * Other than the use of the handle_bytes field, the caller should treat
  7150. * the file_handle structure as an opaque data type
  7151. */
  7152. memcpy(target_fh, fh, total_size);
  7153. target_fh->handle_bytes = tswap32(fh->handle_bytes);
  7154. target_fh->handle_type = tswap32(fh->handle_type);
  7155. g_free(fh);
  7156. unlock_user(target_fh, handle, total_size);
  7157. if (put_user_s32(mid, mount_id)) {
  7158. return -TARGET_EFAULT;
  7159. }
  7160. return ret;
  7161. }
  7162. #endif
  7163. #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  7164. static abi_long do_open_by_handle_at(abi_long mount_fd, abi_long handle,
  7165. abi_long flags)
  7166. {
  7167. struct file_handle *target_fh;
  7168. struct file_handle *fh;
  7169. unsigned int size, total_size;
  7170. abi_long ret;
  7171. if (get_user_s32(size, handle)) {
  7172. return -TARGET_EFAULT;
  7173. }
  7174. total_size = sizeof(struct file_handle) + size;
  7175. target_fh = lock_user(VERIFY_READ, handle, total_size, 1);
  7176. if (!target_fh) {
  7177. return -TARGET_EFAULT;
  7178. }
  7179. fh = g_memdup(target_fh, total_size);
  7180. fh->handle_bytes = size;
  7181. fh->handle_type = tswap32(target_fh->handle_type);
  7182. ret = get_errno(open_by_handle_at(mount_fd, fh,
  7183. target_to_host_bitmask(flags, fcntl_flags_tbl)));
  7184. g_free(fh);
  7185. unlock_user(target_fh, handle, total_size);
  7186. return ret;
  7187. }
  7188. #endif
  7189. #if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4)
  7190. static abi_long do_signalfd4(int fd, abi_long mask, int flags)
  7191. {
  7192. int host_flags;
  7193. target_sigset_t *target_mask;
  7194. sigset_t host_mask;
  7195. abi_long ret;
  7196. if (flags & ~(TARGET_O_NONBLOCK_MASK | TARGET_O_CLOEXEC)) {
  7197. return -TARGET_EINVAL;
  7198. }
  7199. if (!lock_user_struct(VERIFY_READ, target_mask, mask, 1)) {
  7200. return -TARGET_EFAULT;
  7201. }
  7202. target_to_host_sigset(&host_mask, target_mask);
  7203. host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
  7204. ret = get_errno(signalfd(fd, &host_mask, host_flags));
  7205. if (ret >= 0) {
  7206. fd_trans_register(ret, &target_signalfd_trans);
  7207. }
  7208. unlock_user_struct(target_mask, mask, 0);
  7209. return ret;
  7210. }
  7211. #endif
  7212. /* Map host to target signal numbers for the wait family of syscalls.
  7213. Assume all other status bits are the same. */
  7214. int host_to_target_waitstatus(int status)
  7215. {
  7216. if (WIFSIGNALED(status)) {
  7217. return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
  7218. }
  7219. if (WIFSTOPPED(status)) {
  7220. return (host_to_target_signal(WSTOPSIG(status)) << 8)
  7221. | (status & 0xff);
  7222. }
  7223. return status;
  7224. }
  7225. static int open_self_cmdline(CPUArchState *cpu_env, int fd)
  7226. {
  7227. CPUState *cpu = env_cpu(cpu_env);
  7228. struct linux_binprm *bprm = get_task_state(cpu)->bprm;
  7229. int i;
  7230. for (i = 0; i < bprm->argc; i++) {
  7231. size_t len = strlen(bprm->argv[i]) + 1;
  7232. if (write(fd, bprm->argv[i], len) != len) {
  7233. return -1;
  7234. }
  7235. }
  7236. return 0;
  7237. }
  7238. struct open_self_maps_data {
  7239. TaskState *ts;
  7240. IntervalTreeRoot *host_maps;
  7241. int fd;
  7242. bool smaps;
  7243. };
  7244. /*
  7245. * Subroutine to output one line of /proc/self/maps,
  7246. * or one region of /proc/self/smaps.
  7247. */
  7248. #ifdef TARGET_HPPA
  7249. # define test_stack(S, E, L) (E == L)
  7250. #else
  7251. # define test_stack(S, E, L) (S == L)
  7252. #endif
  7253. static void open_self_maps_4(const struct open_self_maps_data *d,
  7254. const MapInfo *mi, abi_ptr start,
  7255. abi_ptr end, unsigned flags)
  7256. {
  7257. const struct image_info *info = d->ts->info;
  7258. const char *path = mi->path;
  7259. uint64_t offset;
  7260. int fd = d->fd;
  7261. int count;
  7262. if (test_stack(start, end, info->stack_limit)) {
  7263. path = "[stack]";
  7264. } else if (start == info->brk) {
  7265. path = "[heap]";
  7266. } else if (start == info->vdso) {
  7267. path = "[vdso]";
  7268. #ifdef TARGET_X86_64
  7269. } else if (start == TARGET_VSYSCALL_PAGE) {
  7270. path = "[vsyscall]";
  7271. #endif
  7272. }
  7273. /* Except null device (MAP_ANON), adjust offset for this fragment. */
  7274. offset = mi->offset;
  7275. if (mi->dev) {
  7276. uintptr_t hstart = (uintptr_t)g2h_untagged(start);
  7277. offset += hstart - mi->itree.start;
  7278. }
  7279. count = dprintf(fd, TARGET_ABI_FMT_ptr "-" TARGET_ABI_FMT_ptr
  7280. " %c%c%c%c %08" PRIx64 " %02x:%02x %"PRId64,
  7281. start, end,
  7282. (flags & PAGE_READ) ? 'r' : '-',
  7283. (flags & PAGE_WRITE_ORG) ? 'w' : '-',
  7284. (flags & PAGE_EXEC) ? 'x' : '-',
  7285. mi->is_priv ? 'p' : 's',
  7286. offset, major(mi->dev), minor(mi->dev),
  7287. (uint64_t)mi->inode);
  7288. if (path) {
  7289. dprintf(fd, "%*s%s\n", 73 - count, "", path);
  7290. } else {
  7291. dprintf(fd, "\n");
  7292. }
  7293. if (d->smaps) {
  7294. unsigned long size = end - start;
  7295. unsigned long page_size_kb = TARGET_PAGE_SIZE >> 10;
  7296. unsigned long size_kb = size >> 10;
  7297. dprintf(fd, "Size: %lu kB\n"
  7298. "KernelPageSize: %lu kB\n"
  7299. "MMUPageSize: %lu kB\n"
  7300. "Rss: 0 kB\n"
  7301. "Pss: 0 kB\n"
  7302. "Pss_Dirty: 0 kB\n"
  7303. "Shared_Clean: 0 kB\n"
  7304. "Shared_Dirty: 0 kB\n"
  7305. "Private_Clean: 0 kB\n"
  7306. "Private_Dirty: 0 kB\n"
  7307. "Referenced: 0 kB\n"
  7308. "Anonymous: %lu kB\n"
  7309. "LazyFree: 0 kB\n"
  7310. "AnonHugePages: 0 kB\n"
  7311. "ShmemPmdMapped: 0 kB\n"
  7312. "FilePmdMapped: 0 kB\n"
  7313. "Shared_Hugetlb: 0 kB\n"
  7314. "Private_Hugetlb: 0 kB\n"
  7315. "Swap: 0 kB\n"
  7316. "SwapPss: 0 kB\n"
  7317. "Locked: 0 kB\n"
  7318. "THPeligible: 0\n"
  7319. "VmFlags:%s%s%s%s%s%s%s%s\n",
  7320. size_kb, page_size_kb, page_size_kb,
  7321. (flags & PAGE_ANON ? size_kb : 0),
  7322. (flags & PAGE_READ) ? " rd" : "",
  7323. (flags & PAGE_WRITE_ORG) ? " wr" : "",
  7324. (flags & PAGE_EXEC) ? " ex" : "",
  7325. mi->is_priv ? "" : " sh",
  7326. (flags & PAGE_READ) ? " mr" : "",
  7327. (flags & PAGE_WRITE_ORG) ? " mw" : "",
  7328. (flags & PAGE_EXEC) ? " me" : "",
  7329. mi->is_priv ? "" : " ms");
  7330. }
  7331. }
  7332. /*
  7333. * Callback for walk_memory_regions, when read_self_maps() fails.
  7334. * Proceed without the benefit of host /proc/self/maps cross-check.
  7335. */
  7336. static int open_self_maps_3(void *opaque, target_ulong guest_start,
  7337. target_ulong guest_end, unsigned long flags)
  7338. {
  7339. static const MapInfo mi = { .is_priv = true };
  7340. open_self_maps_4(opaque, &mi, guest_start, guest_end, flags);
  7341. return 0;
  7342. }
  7343. /*
  7344. * Callback for walk_memory_regions, when read_self_maps() succeeds.
  7345. */
  7346. static int open_self_maps_2(void *opaque, target_ulong guest_start,
  7347. target_ulong guest_end, unsigned long flags)
  7348. {
  7349. const struct open_self_maps_data *d = opaque;
  7350. uintptr_t host_start = (uintptr_t)g2h_untagged(guest_start);
  7351. uintptr_t host_last = (uintptr_t)g2h_untagged(guest_end - 1);
  7352. #ifdef TARGET_X86_64
  7353. /*
  7354. * Because of the extremely high position of the page within the guest
  7355. * virtual address space, this is not backed by host memory at all.
  7356. * Therefore the loop below would fail. This is the only instance
  7357. * of not having host backing memory.
  7358. */
  7359. if (guest_start == TARGET_VSYSCALL_PAGE) {
  7360. return open_self_maps_3(opaque, guest_start, guest_end, flags);
  7361. }
  7362. #endif
  7363. while (1) {
  7364. IntervalTreeNode *n =
  7365. interval_tree_iter_first(d->host_maps, host_start, host_start);
  7366. MapInfo *mi = container_of(n, MapInfo, itree);
  7367. uintptr_t this_hlast = MIN(host_last, n->last);
  7368. target_ulong this_gend = h2g(this_hlast) + 1;
  7369. open_self_maps_4(d, mi, guest_start, this_gend, flags);
  7370. if (this_hlast == host_last) {
  7371. return 0;
  7372. }
  7373. host_start = this_hlast + 1;
  7374. guest_start = h2g(host_start);
  7375. }
  7376. }
  7377. static int open_self_maps_1(CPUArchState *env, int fd, bool smaps)
  7378. {
  7379. struct open_self_maps_data d = {
  7380. .ts = get_task_state(env_cpu(env)),
  7381. .fd = fd,
  7382. .smaps = smaps
  7383. };
  7384. mmap_lock();
  7385. d.host_maps = read_self_maps();
  7386. if (d.host_maps) {
  7387. walk_memory_regions(&d, open_self_maps_2);
  7388. free_self_maps(d.host_maps);
  7389. } else {
  7390. walk_memory_regions(&d, open_self_maps_3);
  7391. }
  7392. mmap_unlock();
  7393. return 0;
  7394. }
  7395. static int open_self_maps(CPUArchState *cpu_env, int fd)
  7396. {
  7397. return open_self_maps_1(cpu_env, fd, false);
  7398. }
  7399. static int open_self_smaps(CPUArchState *cpu_env, int fd)
  7400. {
  7401. return open_self_maps_1(cpu_env, fd, true);
  7402. }
  7403. static int open_self_stat(CPUArchState *cpu_env, int fd)
  7404. {
  7405. CPUState *cpu = env_cpu(cpu_env);
  7406. TaskState *ts = get_task_state(cpu);
  7407. g_autoptr(GString) buf = g_string_new(NULL);
  7408. int i;
  7409. for (i = 0; i < 44; i++) {
  7410. if (i == 0) {
  7411. /* pid */
  7412. g_string_printf(buf, FMT_pid " ", getpid());
  7413. } else if (i == 1) {
  7414. /* app name */
  7415. gchar *bin = g_strrstr(ts->bprm->argv[0], "/");
  7416. bin = bin ? bin + 1 : ts->bprm->argv[0];
  7417. g_string_printf(buf, "(%.15s) ", bin);
  7418. } else if (i == 2) {
  7419. /* task state */
  7420. g_string_assign(buf, "R "); /* we are running right now */
  7421. } else if (i == 3) {
  7422. /* ppid */
  7423. g_string_printf(buf, FMT_pid " ", getppid());
  7424. } else if (i == 19) {
  7425. /* num_threads */
  7426. int cpus = 0;
  7427. WITH_RCU_READ_LOCK_GUARD() {
  7428. CPUState *cpu_iter;
  7429. CPU_FOREACH(cpu_iter) {
  7430. cpus++;
  7431. }
  7432. }
  7433. g_string_printf(buf, "%d ", cpus);
  7434. } else if (i == 21) {
  7435. /* starttime */
  7436. g_string_printf(buf, "%" PRIu64 " ", ts->start_boottime);
  7437. } else if (i == 27) {
  7438. /* stack bottom */
  7439. g_string_printf(buf, TARGET_ABI_FMT_ld " ", ts->info->start_stack);
  7440. } else {
  7441. /* for the rest, there is MasterCard */
  7442. g_string_printf(buf, "0%c", i == 43 ? '\n' : ' ');
  7443. }
  7444. if (write(fd, buf->str, buf->len) != buf->len) {
  7445. return -1;
  7446. }
  7447. }
  7448. return 0;
  7449. }
  7450. static int open_self_auxv(CPUArchState *cpu_env, int fd)
  7451. {
  7452. CPUState *cpu = env_cpu(cpu_env);
  7453. TaskState *ts = get_task_state(cpu);
  7454. abi_ulong auxv = ts->info->saved_auxv;
  7455. abi_ulong len = ts->info->auxv_len;
  7456. char *ptr;
  7457. /*
  7458. * Auxiliary vector is stored in target process stack.
  7459. * read in whole auxv vector and copy it to file
  7460. */
  7461. ptr = lock_user(VERIFY_READ, auxv, len, 0);
  7462. if (ptr != NULL) {
  7463. while (len > 0) {
  7464. ssize_t r;
  7465. r = write(fd, ptr, len);
  7466. if (r <= 0) {
  7467. break;
  7468. }
  7469. len -= r;
  7470. ptr += r;
  7471. }
  7472. lseek(fd, 0, SEEK_SET);
  7473. unlock_user(ptr, auxv, len);
  7474. }
  7475. return 0;
  7476. }
  7477. static int is_proc_myself(const char *filename, const char *entry)
  7478. {
  7479. if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
  7480. filename += strlen("/proc/");
  7481. if (!strncmp(filename, "self/", strlen("self/"))) {
  7482. filename += strlen("self/");
  7483. } else if (*filename >= '1' && *filename <= '9') {
  7484. char myself[80];
  7485. snprintf(myself, sizeof(myself), "%d/", getpid());
  7486. if (!strncmp(filename, myself, strlen(myself))) {
  7487. filename += strlen(myself);
  7488. } else {
  7489. return 0;
  7490. }
  7491. } else {
  7492. return 0;
  7493. }
  7494. if (!strcmp(filename, entry)) {
  7495. return 1;
  7496. }
  7497. }
  7498. return 0;
  7499. }
  7500. static void excp_dump_file(FILE *logfile, CPUArchState *env,
  7501. const char *fmt, int code)
  7502. {
  7503. if (logfile) {
  7504. CPUState *cs = env_cpu(env);
  7505. fprintf(logfile, fmt, code);
  7506. fprintf(logfile, "Failing executable: %s\n", exec_path);
  7507. cpu_dump_state(cs, logfile, 0);
  7508. open_self_maps(env, fileno(logfile));
  7509. }
  7510. }
  7511. void target_exception_dump(CPUArchState *env, const char *fmt, int code)
  7512. {
  7513. /* dump to console */
  7514. excp_dump_file(stderr, env, fmt, code);
  7515. /* dump to log file */
  7516. if (qemu_log_separate()) {
  7517. FILE *logfile = qemu_log_trylock();
  7518. excp_dump_file(logfile, env, fmt, code);
  7519. qemu_log_unlock(logfile);
  7520. }
  7521. }
  7522. #include "target_proc.h"
  7523. #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN || \
  7524. defined(HAVE_ARCH_PROC_CPUINFO) || \
  7525. defined(HAVE_ARCH_PROC_HARDWARE)
  7526. static int is_proc(const char *filename, const char *entry)
  7527. {
  7528. return strcmp(filename, entry) == 0;
  7529. }
  7530. #endif
  7531. #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
  7532. static int open_net_route(CPUArchState *cpu_env, int fd)
  7533. {
  7534. FILE *fp;
  7535. char *line = NULL;
  7536. size_t len = 0;
  7537. ssize_t read;
  7538. fp = fopen("/proc/net/route", "r");
  7539. if (fp == NULL) {
  7540. return -1;
  7541. }
  7542. /* read header */
  7543. read = getline(&line, &len, fp);
  7544. dprintf(fd, "%s", line);
  7545. /* read routes */
  7546. while ((read = getline(&line, &len, fp)) != -1) {
  7547. char iface[16];
  7548. uint32_t dest, gw, mask;
  7549. unsigned int flags, refcnt, use, metric, mtu, window, irtt;
  7550. int fields;
  7551. fields = sscanf(line,
  7552. "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
  7553. iface, &dest, &gw, &flags, &refcnt, &use, &metric,
  7554. &mask, &mtu, &window, &irtt);
  7555. if (fields != 11) {
  7556. continue;
  7557. }
  7558. dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
  7559. iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
  7560. metric, tswap32(mask), mtu, window, irtt);
  7561. }
  7562. free(line);
  7563. fclose(fp);
  7564. return 0;
  7565. }
  7566. #endif
  7567. static int maybe_do_fake_open(CPUArchState *cpu_env, int dirfd,
  7568. const char *fname, int flags, mode_t mode,
  7569. int openat2_resolve, bool safe)
  7570. {
  7571. g_autofree char *proc_name = NULL;
  7572. const char *pathname;
  7573. struct fake_open {
  7574. const char *filename;
  7575. int (*fill)(CPUArchState *cpu_env, int fd);
  7576. int (*cmp)(const char *s1, const char *s2);
  7577. };
  7578. const struct fake_open *fake_open;
  7579. static const struct fake_open fakes[] = {
  7580. { "maps", open_self_maps, is_proc_myself },
  7581. { "smaps", open_self_smaps, is_proc_myself },
  7582. { "stat", open_self_stat, is_proc_myself },
  7583. { "auxv", open_self_auxv, is_proc_myself },
  7584. { "cmdline", open_self_cmdline, is_proc_myself },
  7585. #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
  7586. { "/proc/net/route", open_net_route, is_proc },
  7587. #endif
  7588. #if defined(HAVE_ARCH_PROC_CPUINFO)
  7589. { "/proc/cpuinfo", open_cpuinfo, is_proc },
  7590. #endif
  7591. #if defined(HAVE_ARCH_PROC_HARDWARE)
  7592. { "/proc/hardware", open_hardware, is_proc },
  7593. #endif
  7594. { NULL, NULL, NULL }
  7595. };
  7596. /* if this is a file from /proc/ filesystem, expand full name */
  7597. proc_name = realpath(fname, NULL);
  7598. if (proc_name && strncmp(proc_name, "/proc/", 6) == 0) {
  7599. pathname = proc_name;
  7600. } else {
  7601. pathname = fname;
  7602. }
  7603. if (is_proc_myself(pathname, "exe")) {
  7604. /* Honor openat2 resolve flags */
  7605. if ((openat2_resolve & RESOLVE_NO_MAGICLINKS) ||
  7606. (openat2_resolve & RESOLVE_NO_SYMLINKS)) {
  7607. errno = ELOOP;
  7608. return -1;
  7609. }
  7610. if (safe) {
  7611. return safe_openat(dirfd, exec_path, flags, mode);
  7612. } else {
  7613. return openat(dirfd, exec_path, flags, mode);
  7614. }
  7615. }
  7616. for (fake_open = fakes; fake_open->filename; fake_open++) {
  7617. if (fake_open->cmp(pathname, fake_open->filename)) {
  7618. break;
  7619. }
  7620. }
  7621. if (fake_open->filename) {
  7622. const char *tmpdir;
  7623. char filename[PATH_MAX];
  7624. int fd, r;
  7625. fd = memfd_create("qemu-open", 0);
  7626. if (fd < 0) {
  7627. if (errno != ENOSYS) {
  7628. return fd;
  7629. }
  7630. /* create temporary file to map stat to */
  7631. tmpdir = getenv("TMPDIR");
  7632. if (!tmpdir)
  7633. tmpdir = "/tmp";
  7634. snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
  7635. fd = mkstemp(filename);
  7636. if (fd < 0) {
  7637. return fd;
  7638. }
  7639. unlink(filename);
  7640. }
  7641. if ((r = fake_open->fill(cpu_env, fd))) {
  7642. int e = errno;
  7643. close(fd);
  7644. errno = e;
  7645. return r;
  7646. }
  7647. lseek(fd, 0, SEEK_SET);
  7648. return fd;
  7649. }
  7650. return -2;
  7651. }
  7652. int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *pathname,
  7653. int flags, mode_t mode, bool safe)
  7654. {
  7655. int fd = maybe_do_fake_open(cpu_env, dirfd, pathname, flags, mode, 0, safe);
  7656. if (fd > -2) {
  7657. return fd;
  7658. }
  7659. if (safe) {
  7660. return safe_openat(dirfd, path(pathname), flags, mode);
  7661. } else {
  7662. return openat(dirfd, path(pathname), flags, mode);
  7663. }
  7664. }
  7665. static int do_openat2(CPUArchState *cpu_env, abi_long dirfd,
  7666. abi_ptr guest_pathname, abi_ptr guest_open_how,
  7667. abi_ulong guest_size)
  7668. {
  7669. struct open_how_ver0 how = {0};
  7670. char *pathname;
  7671. int ret;
  7672. if (guest_size < sizeof(struct target_open_how_ver0)) {
  7673. return -TARGET_EINVAL;
  7674. }
  7675. ret = copy_struct_from_user(&how, sizeof(how), guest_open_how, guest_size);
  7676. if (ret) {
  7677. if (ret == -TARGET_E2BIG) {
  7678. qemu_log_mask(LOG_UNIMP,
  7679. "Unimplemented openat2 open_how size: "
  7680. TARGET_ABI_FMT_lu "\n", guest_size);
  7681. }
  7682. return ret;
  7683. }
  7684. pathname = lock_user_string(guest_pathname);
  7685. if (!pathname) {
  7686. return -TARGET_EFAULT;
  7687. }
  7688. how.flags = target_to_host_bitmask(tswap64(how.flags), fcntl_flags_tbl);
  7689. how.mode = tswap64(how.mode);
  7690. how.resolve = tswap64(how.resolve);
  7691. int fd = maybe_do_fake_open(cpu_env, dirfd, pathname, how.flags, how.mode,
  7692. how.resolve, true);
  7693. if (fd > -2) {
  7694. ret = get_errno(fd);
  7695. } else {
  7696. ret = get_errno(safe_openat2(dirfd, pathname, &how,
  7697. sizeof(struct open_how_ver0)));
  7698. }
  7699. fd_trans_unregister(ret);
  7700. unlock_user(pathname, guest_pathname, 0);
  7701. return ret;
  7702. }
  7703. ssize_t do_guest_readlink(const char *pathname, char *buf, size_t bufsiz)
  7704. {
  7705. ssize_t ret;
  7706. if (!pathname || !buf) {
  7707. errno = EFAULT;
  7708. return -1;
  7709. }
  7710. if (!bufsiz) {
  7711. /* Short circuit this for the magic exe check. */
  7712. errno = EINVAL;
  7713. return -1;
  7714. }
  7715. if (is_proc_myself((const char *)pathname, "exe")) {
  7716. /*
  7717. * Don't worry about sign mismatch as earlier mapping
  7718. * logic would have thrown a bad address error.
  7719. */
  7720. ret = MIN(strlen(exec_path), bufsiz);
  7721. /* We cannot NUL terminate the string. */
  7722. memcpy(buf, exec_path, ret);
  7723. } else {
  7724. ret = readlink(path(pathname), buf, bufsiz);
  7725. }
  7726. return ret;
  7727. }
  7728. static int do_execv(CPUArchState *cpu_env, int dirfd,
  7729. abi_long pathname, abi_long guest_argp,
  7730. abi_long guest_envp, int flags, bool is_execveat)
  7731. {
  7732. int ret;
  7733. char **argp, **envp;
  7734. int argc, envc;
  7735. abi_ulong gp;
  7736. abi_ulong addr;
  7737. char **q;
  7738. void *p;
  7739. argc = 0;
  7740. for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
  7741. if (get_user_ual(addr, gp)) {
  7742. return -TARGET_EFAULT;
  7743. }
  7744. if (!addr) {
  7745. break;
  7746. }
  7747. argc++;
  7748. }
  7749. envc = 0;
  7750. for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
  7751. if (get_user_ual(addr, gp)) {
  7752. return -TARGET_EFAULT;
  7753. }
  7754. if (!addr) {
  7755. break;
  7756. }
  7757. envc++;
  7758. }
  7759. argp = g_new0(char *, argc + 1);
  7760. envp = g_new0(char *, envc + 1);
  7761. for (gp = guest_argp, q = argp; gp; gp += sizeof(abi_ulong), q++) {
  7762. if (get_user_ual(addr, gp)) {
  7763. goto execve_efault;
  7764. }
  7765. if (!addr) {
  7766. break;
  7767. }
  7768. *q = lock_user_string(addr);
  7769. if (!*q) {
  7770. goto execve_efault;
  7771. }
  7772. }
  7773. *q = NULL;
  7774. for (gp = guest_envp, q = envp; gp; gp += sizeof(abi_ulong), q++) {
  7775. if (get_user_ual(addr, gp)) {
  7776. goto execve_efault;
  7777. }
  7778. if (!addr) {
  7779. break;
  7780. }
  7781. *q = lock_user_string(addr);
  7782. if (!*q) {
  7783. goto execve_efault;
  7784. }
  7785. }
  7786. *q = NULL;
  7787. /*
  7788. * Although execve() is not an interruptible syscall it is
  7789. * a special case where we must use the safe_syscall wrapper:
  7790. * if we allow a signal to happen before we make the host
  7791. * syscall then we will 'lose' it, because at the point of
  7792. * execve the process leaves QEMU's control. So we use the
  7793. * safe syscall wrapper to ensure that we either take the
  7794. * signal as a guest signal, or else it does not happen
  7795. * before the execve completes and makes it the other
  7796. * program's problem.
  7797. */
  7798. p = lock_user_string(pathname);
  7799. if (!p) {
  7800. goto execve_efault;
  7801. }
  7802. const char *exe = p;
  7803. if (is_proc_myself(p, "exe")) {
  7804. exe = exec_path;
  7805. }
  7806. ret = is_execveat
  7807. ? safe_execveat(dirfd, exe, argp, envp, flags)
  7808. : safe_execve(exe, argp, envp);
  7809. ret = get_errno(ret);
  7810. unlock_user(p, pathname, 0);
  7811. goto execve_end;
  7812. execve_efault:
  7813. ret = -TARGET_EFAULT;
  7814. execve_end:
  7815. for (gp = guest_argp, q = argp; *q; gp += sizeof(abi_ulong), q++) {
  7816. if (get_user_ual(addr, gp) || !addr) {
  7817. break;
  7818. }
  7819. unlock_user(*q, addr, 0);
  7820. }
  7821. for (gp = guest_envp, q = envp; *q; gp += sizeof(abi_ulong), q++) {
  7822. if (get_user_ual(addr, gp) || !addr) {
  7823. break;
  7824. }
  7825. unlock_user(*q, addr, 0);
  7826. }
  7827. g_free(argp);
  7828. g_free(envp);
  7829. return ret;
  7830. }
  7831. #define TIMER_MAGIC 0x0caf0000
  7832. #define TIMER_MAGIC_MASK 0xffff0000
  7833. /* Convert QEMU provided timer ID back to internal 16bit index format */
  7834. static target_timer_t get_timer_id(abi_long arg)
  7835. {
  7836. target_timer_t timerid = arg;
  7837. if ((timerid & TIMER_MAGIC_MASK) != TIMER_MAGIC) {
  7838. return -TARGET_EINVAL;
  7839. }
  7840. timerid &= 0xffff;
  7841. if (timerid >= ARRAY_SIZE(g_posix_timers)) {
  7842. return -TARGET_EINVAL;
  7843. }
  7844. return timerid;
  7845. }
  7846. static int target_to_host_cpu_mask(unsigned long *host_mask,
  7847. size_t host_size,
  7848. abi_ulong target_addr,
  7849. size_t target_size)
  7850. {
  7851. unsigned target_bits = sizeof(abi_ulong) * 8;
  7852. unsigned host_bits = sizeof(*host_mask) * 8;
  7853. abi_ulong *target_mask;
  7854. unsigned i, j;
  7855. assert(host_size >= target_size);
  7856. target_mask = lock_user(VERIFY_READ, target_addr, target_size, 1);
  7857. if (!target_mask) {
  7858. return -TARGET_EFAULT;
  7859. }
  7860. memset(host_mask, 0, host_size);
  7861. for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
  7862. unsigned bit = i * target_bits;
  7863. abi_ulong val;
  7864. __get_user(val, &target_mask[i]);
  7865. for (j = 0; j < target_bits; j++, bit++) {
  7866. if (val & (1UL << j)) {
  7867. host_mask[bit / host_bits] |= 1UL << (bit % host_bits);
  7868. }
  7869. }
  7870. }
  7871. unlock_user(target_mask, target_addr, 0);
  7872. return 0;
  7873. }
  7874. static int host_to_target_cpu_mask(const unsigned long *host_mask,
  7875. size_t host_size,
  7876. abi_ulong target_addr,
  7877. size_t target_size)
  7878. {
  7879. unsigned target_bits = sizeof(abi_ulong) * 8;
  7880. unsigned host_bits = sizeof(*host_mask) * 8;
  7881. abi_ulong *target_mask;
  7882. unsigned i, j;
  7883. assert(host_size >= target_size);
  7884. target_mask = lock_user(VERIFY_WRITE, target_addr, target_size, 0);
  7885. if (!target_mask) {
  7886. return -TARGET_EFAULT;
  7887. }
  7888. for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
  7889. unsigned bit = i * target_bits;
  7890. abi_ulong val = 0;
  7891. for (j = 0; j < target_bits; j++, bit++) {
  7892. if (host_mask[bit / host_bits] & (1UL << (bit % host_bits))) {
  7893. val |= 1UL << j;
  7894. }
  7895. }
  7896. __put_user(val, &target_mask[i]);
  7897. }
  7898. unlock_user(target_mask, target_addr, target_size);
  7899. return 0;
  7900. }
  7901. #ifdef TARGET_NR_getdents
  7902. static int do_getdents(abi_long dirfd, abi_long arg2, abi_long count)
  7903. {
  7904. g_autofree void *hdirp = NULL;
  7905. void *tdirp;
  7906. int hlen, hoff, toff;
  7907. int hreclen, treclen;
  7908. off_t prev_diroff = 0;
  7909. hdirp = g_try_malloc(count);
  7910. if (!hdirp) {
  7911. return -TARGET_ENOMEM;
  7912. }
  7913. #ifdef EMULATE_GETDENTS_WITH_GETDENTS
  7914. hlen = sys_getdents(dirfd, hdirp, count);
  7915. #else
  7916. hlen = sys_getdents64(dirfd, hdirp, count);
  7917. #endif
  7918. hlen = get_errno(hlen);
  7919. if (is_error(hlen)) {
  7920. return hlen;
  7921. }
  7922. tdirp = lock_user(VERIFY_WRITE, arg2, count, 0);
  7923. if (!tdirp) {
  7924. return -TARGET_EFAULT;
  7925. }
  7926. for (hoff = toff = 0; hoff < hlen; hoff += hreclen, toff += treclen) {
  7927. #ifdef EMULATE_GETDENTS_WITH_GETDENTS
  7928. struct linux_dirent *hde = hdirp + hoff;
  7929. #else
  7930. struct linux_dirent64 *hde = hdirp + hoff;
  7931. #endif
  7932. struct target_dirent *tde = tdirp + toff;
  7933. int namelen;
  7934. uint8_t type;
  7935. namelen = strlen(hde->d_name);
  7936. hreclen = hde->d_reclen;
  7937. treclen = offsetof(struct target_dirent, d_name) + namelen + 2;
  7938. treclen = QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent));
  7939. if (toff + treclen > count) {
  7940. /*
  7941. * If the host struct is smaller than the target struct, or
  7942. * requires less alignment and thus packs into less space,
  7943. * then the host can return more entries than we can pass
  7944. * on to the guest.
  7945. */
  7946. if (toff == 0) {
  7947. toff = -TARGET_EINVAL; /* result buffer is too small */
  7948. break;
  7949. }
  7950. /*
  7951. * Return what we have, resetting the file pointer to the
  7952. * location of the first record not returned.
  7953. */
  7954. lseek(dirfd, prev_diroff, SEEK_SET);
  7955. break;
  7956. }
  7957. prev_diroff = hde->d_off;
  7958. tde->d_ino = tswapal(hde->d_ino);
  7959. tde->d_off = tswapal(hde->d_off);
  7960. tde->d_reclen = tswap16(treclen);
  7961. memcpy(tde->d_name, hde->d_name, namelen + 1);
  7962. /*
  7963. * The getdents type is in what was formerly a padding byte at the
  7964. * end of the structure.
  7965. */
  7966. #ifdef EMULATE_GETDENTS_WITH_GETDENTS
  7967. type = *((uint8_t *)hde + hreclen - 1);
  7968. #else
  7969. type = hde->d_type;
  7970. #endif
  7971. *((uint8_t *)tde + treclen - 1) = type;
  7972. }
  7973. unlock_user(tdirp, arg2, toff);
  7974. return toff;
  7975. }
  7976. #endif /* TARGET_NR_getdents */
  7977. #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
  7978. static int do_getdents64(abi_long dirfd, abi_long arg2, abi_long count)
  7979. {
  7980. g_autofree void *hdirp = NULL;
  7981. void *tdirp;
  7982. int hlen, hoff, toff;
  7983. int hreclen, treclen;
  7984. off_t prev_diroff = 0;
  7985. hdirp = g_try_malloc(count);
  7986. if (!hdirp) {
  7987. return -TARGET_ENOMEM;
  7988. }
  7989. hlen = get_errno(sys_getdents64(dirfd, hdirp, count));
  7990. if (is_error(hlen)) {
  7991. return hlen;
  7992. }
  7993. tdirp = lock_user(VERIFY_WRITE, arg2, count, 0);
  7994. if (!tdirp) {
  7995. return -TARGET_EFAULT;
  7996. }
  7997. for (hoff = toff = 0; hoff < hlen; hoff += hreclen, toff += treclen) {
  7998. struct linux_dirent64 *hde = hdirp + hoff;
  7999. struct target_dirent64 *tde = tdirp + toff;
  8000. int namelen;
  8001. namelen = strlen(hde->d_name) + 1;
  8002. hreclen = hde->d_reclen;
  8003. treclen = offsetof(struct target_dirent64, d_name) + namelen;
  8004. treclen = QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent64));
  8005. if (toff + treclen > count) {
  8006. /*
  8007. * If the host struct is smaller than the target struct, or
  8008. * requires less alignment and thus packs into less space,
  8009. * then the host can return more entries than we can pass
  8010. * on to the guest.
  8011. */
  8012. if (toff == 0) {
  8013. toff = -TARGET_EINVAL; /* result buffer is too small */
  8014. break;
  8015. }
  8016. /*
  8017. * Return what we have, resetting the file pointer to the
  8018. * location of the first record not returned.
  8019. */
  8020. lseek(dirfd, prev_diroff, SEEK_SET);
  8021. break;
  8022. }
  8023. prev_diroff = hde->d_off;
  8024. tde->d_ino = tswap64(hde->d_ino);
  8025. tde->d_off = tswap64(hde->d_off);
  8026. tde->d_reclen = tswap16(treclen);
  8027. tde->d_type = hde->d_type;
  8028. memcpy(tde->d_name, hde->d_name, namelen);
  8029. }
  8030. unlock_user(tdirp, arg2, toff);
  8031. return toff;
  8032. }
  8033. #endif /* TARGET_NR_getdents64 */
  8034. #if defined(TARGET_NR_riscv_hwprobe)
  8035. #define RISCV_HWPROBE_KEY_MVENDORID 0
  8036. #define RISCV_HWPROBE_KEY_MARCHID 1
  8037. #define RISCV_HWPROBE_KEY_MIMPID 2
  8038. #define RISCV_HWPROBE_KEY_BASE_BEHAVIOR 3
  8039. #define RISCV_HWPROBE_BASE_BEHAVIOR_IMA (1 << 0)
  8040. #define RISCV_HWPROBE_KEY_IMA_EXT_0 4
  8041. #define RISCV_HWPROBE_IMA_FD (1 << 0)
  8042. #define RISCV_HWPROBE_IMA_C (1 << 1)
  8043. #define RISCV_HWPROBE_IMA_V (1 << 2)
  8044. #define RISCV_HWPROBE_EXT_ZBA (1 << 3)
  8045. #define RISCV_HWPROBE_EXT_ZBB (1 << 4)
  8046. #define RISCV_HWPROBE_EXT_ZBS (1 << 5)
  8047. #define RISCV_HWPROBE_EXT_ZICBOZ (1 << 6)
  8048. #define RISCV_HWPROBE_EXT_ZBC (1 << 7)
  8049. #define RISCV_HWPROBE_EXT_ZBKB (1 << 8)
  8050. #define RISCV_HWPROBE_EXT_ZBKC (1 << 9)
  8051. #define RISCV_HWPROBE_EXT_ZBKX (1 << 10)
  8052. #define RISCV_HWPROBE_EXT_ZKND (1 << 11)
  8053. #define RISCV_HWPROBE_EXT_ZKNE (1 << 12)
  8054. #define RISCV_HWPROBE_EXT_ZKNH (1 << 13)
  8055. #define RISCV_HWPROBE_EXT_ZKSED (1 << 14)
  8056. #define RISCV_HWPROBE_EXT_ZKSH (1 << 15)
  8057. #define RISCV_HWPROBE_EXT_ZKT (1 << 16)
  8058. #define RISCV_HWPROBE_EXT_ZVBB (1 << 17)
  8059. #define RISCV_HWPROBE_EXT_ZVBC (1 << 18)
  8060. #define RISCV_HWPROBE_EXT_ZVKB (1 << 19)
  8061. #define RISCV_HWPROBE_EXT_ZVKG (1 << 20)
  8062. #define RISCV_HWPROBE_EXT_ZVKNED (1 << 21)
  8063. #define RISCV_HWPROBE_EXT_ZVKNHA (1 << 22)
  8064. #define RISCV_HWPROBE_EXT_ZVKNHB (1 << 23)
  8065. #define RISCV_HWPROBE_EXT_ZVKSED (1 << 24)
  8066. #define RISCV_HWPROBE_EXT_ZVKSH (1 << 25)
  8067. #define RISCV_HWPROBE_EXT_ZVKT (1 << 26)
  8068. #define RISCV_HWPROBE_EXT_ZFH (1 << 27)
  8069. #define RISCV_HWPROBE_EXT_ZFHMIN (1 << 28)
  8070. #define RISCV_HWPROBE_EXT_ZIHINTNTL (1 << 29)
  8071. #define RISCV_HWPROBE_EXT_ZVFH (1 << 30)
  8072. #define RISCV_HWPROBE_EXT_ZVFHMIN (1ULL << 31)
  8073. #define RISCV_HWPROBE_EXT_ZFA (1ULL << 32)
  8074. #define RISCV_HWPROBE_EXT_ZTSO (1ULL << 33)
  8075. #define RISCV_HWPROBE_EXT_ZACAS (1ULL << 34)
  8076. #define RISCV_HWPROBE_EXT_ZICOND (1ULL << 35)
  8077. #define RISCV_HWPROBE_KEY_CPUPERF_0 5
  8078. #define RISCV_HWPROBE_MISALIGNED_UNKNOWN (0 << 0)
  8079. #define RISCV_HWPROBE_MISALIGNED_EMULATED (1 << 0)
  8080. #define RISCV_HWPROBE_MISALIGNED_SLOW (2 << 0)
  8081. #define RISCV_HWPROBE_MISALIGNED_FAST (3 << 0)
  8082. #define RISCV_HWPROBE_MISALIGNED_UNSUPPORTED (4 << 0)
  8083. #define RISCV_HWPROBE_MISALIGNED_MASK (7 << 0)
  8084. #define RISCV_HWPROBE_KEY_ZICBOZ_BLOCK_SIZE 6
  8085. struct riscv_hwprobe {
  8086. abi_llong key;
  8087. abi_ullong value;
  8088. };
  8089. static void risc_hwprobe_fill_pairs(CPURISCVState *env,
  8090. struct riscv_hwprobe *pair,
  8091. size_t pair_count)
  8092. {
  8093. const RISCVCPUConfig *cfg = riscv_cpu_cfg(env);
  8094. for (; pair_count > 0; pair_count--, pair++) {
  8095. abi_llong key;
  8096. abi_ullong value;
  8097. __put_user(0, &pair->value);
  8098. __get_user(key, &pair->key);
  8099. switch (key) {
  8100. case RISCV_HWPROBE_KEY_MVENDORID:
  8101. __put_user(cfg->mvendorid, &pair->value);
  8102. break;
  8103. case RISCV_HWPROBE_KEY_MARCHID:
  8104. __put_user(cfg->marchid, &pair->value);
  8105. break;
  8106. case RISCV_HWPROBE_KEY_MIMPID:
  8107. __put_user(cfg->mimpid, &pair->value);
  8108. break;
  8109. case RISCV_HWPROBE_KEY_BASE_BEHAVIOR:
  8110. value = riscv_has_ext(env, RVI) &&
  8111. riscv_has_ext(env, RVM) &&
  8112. riscv_has_ext(env, RVA) ?
  8113. RISCV_HWPROBE_BASE_BEHAVIOR_IMA : 0;
  8114. __put_user(value, &pair->value);
  8115. break;
  8116. case RISCV_HWPROBE_KEY_IMA_EXT_0:
  8117. value = riscv_has_ext(env, RVF) &&
  8118. riscv_has_ext(env, RVD) ?
  8119. RISCV_HWPROBE_IMA_FD : 0;
  8120. value |= riscv_has_ext(env, RVC) ?
  8121. RISCV_HWPROBE_IMA_C : 0;
  8122. value |= riscv_has_ext(env, RVV) ?
  8123. RISCV_HWPROBE_IMA_V : 0;
  8124. value |= cfg->ext_zba ?
  8125. RISCV_HWPROBE_EXT_ZBA : 0;
  8126. value |= cfg->ext_zbb ?
  8127. RISCV_HWPROBE_EXT_ZBB : 0;
  8128. value |= cfg->ext_zbs ?
  8129. RISCV_HWPROBE_EXT_ZBS : 0;
  8130. value |= cfg->ext_zicboz ?
  8131. RISCV_HWPROBE_EXT_ZICBOZ : 0;
  8132. value |= cfg->ext_zbc ?
  8133. RISCV_HWPROBE_EXT_ZBC : 0;
  8134. value |= cfg->ext_zbkb ?
  8135. RISCV_HWPROBE_EXT_ZBKB : 0;
  8136. value |= cfg->ext_zbkc ?
  8137. RISCV_HWPROBE_EXT_ZBKC : 0;
  8138. value |= cfg->ext_zbkx ?
  8139. RISCV_HWPROBE_EXT_ZBKX : 0;
  8140. value |= cfg->ext_zknd ?
  8141. RISCV_HWPROBE_EXT_ZKND : 0;
  8142. value |= cfg->ext_zkne ?
  8143. RISCV_HWPROBE_EXT_ZKNE : 0;
  8144. value |= cfg->ext_zknh ?
  8145. RISCV_HWPROBE_EXT_ZKNH : 0;
  8146. value |= cfg->ext_zksed ?
  8147. RISCV_HWPROBE_EXT_ZKSED : 0;
  8148. value |= cfg->ext_zksh ?
  8149. RISCV_HWPROBE_EXT_ZKSH : 0;
  8150. value |= cfg->ext_zkt ?
  8151. RISCV_HWPROBE_EXT_ZKT : 0;
  8152. value |= cfg->ext_zvbb ?
  8153. RISCV_HWPROBE_EXT_ZVBB : 0;
  8154. value |= cfg->ext_zvbc ?
  8155. RISCV_HWPROBE_EXT_ZVBC : 0;
  8156. value |= cfg->ext_zvkb ?
  8157. RISCV_HWPROBE_EXT_ZVKB : 0;
  8158. value |= cfg->ext_zvkg ?
  8159. RISCV_HWPROBE_EXT_ZVKG : 0;
  8160. value |= cfg->ext_zvkned ?
  8161. RISCV_HWPROBE_EXT_ZVKNED : 0;
  8162. value |= cfg->ext_zvknha ?
  8163. RISCV_HWPROBE_EXT_ZVKNHA : 0;
  8164. value |= cfg->ext_zvknhb ?
  8165. RISCV_HWPROBE_EXT_ZVKNHB : 0;
  8166. value |= cfg->ext_zvksed ?
  8167. RISCV_HWPROBE_EXT_ZVKSED : 0;
  8168. value |= cfg->ext_zvksh ?
  8169. RISCV_HWPROBE_EXT_ZVKSH : 0;
  8170. value |= cfg->ext_zvkt ?
  8171. RISCV_HWPROBE_EXT_ZVKT : 0;
  8172. value |= cfg->ext_zfh ?
  8173. RISCV_HWPROBE_EXT_ZFH : 0;
  8174. value |= cfg->ext_zfhmin ?
  8175. RISCV_HWPROBE_EXT_ZFHMIN : 0;
  8176. value |= cfg->ext_zihintntl ?
  8177. RISCV_HWPROBE_EXT_ZIHINTNTL : 0;
  8178. value |= cfg->ext_zvfh ?
  8179. RISCV_HWPROBE_EXT_ZVFH : 0;
  8180. value |= cfg->ext_zvfhmin ?
  8181. RISCV_HWPROBE_EXT_ZVFHMIN : 0;
  8182. value |= cfg->ext_zfa ?
  8183. RISCV_HWPROBE_EXT_ZFA : 0;
  8184. value |= cfg->ext_ztso ?
  8185. RISCV_HWPROBE_EXT_ZTSO : 0;
  8186. value |= cfg->ext_zacas ?
  8187. RISCV_HWPROBE_EXT_ZACAS : 0;
  8188. value |= cfg->ext_zicond ?
  8189. RISCV_HWPROBE_EXT_ZICOND : 0;
  8190. __put_user(value, &pair->value);
  8191. break;
  8192. case RISCV_HWPROBE_KEY_CPUPERF_0:
  8193. __put_user(RISCV_HWPROBE_MISALIGNED_FAST, &pair->value);
  8194. break;
  8195. case RISCV_HWPROBE_KEY_ZICBOZ_BLOCK_SIZE:
  8196. value = cfg->ext_zicboz ? cfg->cboz_blocksize : 0;
  8197. __put_user(value, &pair->value);
  8198. break;
  8199. default:
  8200. __put_user(-1, &pair->key);
  8201. break;
  8202. }
  8203. }
  8204. }
  8205. static int cpu_set_valid(abi_long arg3, abi_long arg4)
  8206. {
  8207. int ret, i, tmp;
  8208. size_t host_mask_size, target_mask_size;
  8209. unsigned long *host_mask;
  8210. /*
  8211. * cpu_set_t represent CPU masks as bit masks of type unsigned long *.
  8212. * arg3 contains the cpu count.
  8213. */
  8214. tmp = (8 * sizeof(abi_ulong));
  8215. target_mask_size = ((arg3 + tmp - 1) / tmp) * sizeof(abi_ulong);
  8216. host_mask_size = (target_mask_size + (sizeof(*host_mask) - 1)) &
  8217. ~(sizeof(*host_mask) - 1);
  8218. host_mask = alloca(host_mask_size);
  8219. ret = target_to_host_cpu_mask(host_mask, host_mask_size,
  8220. arg4, target_mask_size);
  8221. if (ret != 0) {
  8222. return ret;
  8223. }
  8224. for (i = 0 ; i < host_mask_size / sizeof(*host_mask); i++) {
  8225. if (host_mask[i] != 0) {
  8226. return 0;
  8227. }
  8228. }
  8229. return -TARGET_EINVAL;
  8230. }
  8231. static abi_long do_riscv_hwprobe(CPUArchState *cpu_env, abi_long arg1,
  8232. abi_long arg2, abi_long arg3,
  8233. abi_long arg4, abi_long arg5)
  8234. {
  8235. int ret;
  8236. struct riscv_hwprobe *host_pairs;
  8237. /* flags must be 0 */
  8238. if (arg5 != 0) {
  8239. return -TARGET_EINVAL;
  8240. }
  8241. /* check cpu_set */
  8242. if (arg3 != 0) {
  8243. ret = cpu_set_valid(arg3, arg4);
  8244. if (ret != 0) {
  8245. return ret;
  8246. }
  8247. } else if (arg4 != 0) {
  8248. return -TARGET_EINVAL;
  8249. }
  8250. /* no pairs */
  8251. if (arg2 == 0) {
  8252. return 0;
  8253. }
  8254. host_pairs = lock_user(VERIFY_WRITE, arg1,
  8255. sizeof(*host_pairs) * (size_t)arg2, 0);
  8256. if (host_pairs == NULL) {
  8257. return -TARGET_EFAULT;
  8258. }
  8259. risc_hwprobe_fill_pairs(cpu_env, host_pairs, arg2);
  8260. unlock_user(host_pairs, arg1, sizeof(*host_pairs) * (size_t)arg2);
  8261. return 0;
  8262. }
  8263. #endif /* TARGET_NR_riscv_hwprobe */
  8264. #if defined(TARGET_NR_pivot_root) && defined(__NR_pivot_root)
  8265. _syscall2(int, pivot_root, const char *, new_root, const char *, put_old)
  8266. #endif
  8267. #if defined(TARGET_NR_open_tree) && defined(__NR_open_tree)
  8268. #define __NR_sys_open_tree __NR_open_tree
  8269. _syscall3(int, sys_open_tree, int, __dfd, const char *, __filename,
  8270. unsigned int, __flags)
  8271. #endif
  8272. #if defined(TARGET_NR_move_mount) && defined(__NR_move_mount)
  8273. #define __NR_sys_move_mount __NR_move_mount
  8274. _syscall5(int, sys_move_mount, int, __from_dfd, const char *, __from_pathname,
  8275. int, __to_dfd, const char *, __to_pathname, unsigned int, flag)
  8276. #endif
  8277. /* This is an internal helper for do_syscall so that it is easier
  8278. * to have a single return point, so that actions, such as logging
  8279. * of syscall results, can be performed.
  8280. * All errnos that do_syscall() returns must be -TARGET_<errcode>.
  8281. */
  8282. static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
  8283. abi_long arg2, abi_long arg3, abi_long arg4,
  8284. abi_long arg5, abi_long arg6, abi_long arg7,
  8285. abi_long arg8)
  8286. {
  8287. CPUState *cpu = env_cpu(cpu_env);
  8288. abi_long ret;
  8289. #if defined(TARGET_NR_stat) || defined(TARGET_NR_stat64) \
  8290. || defined(TARGET_NR_lstat) || defined(TARGET_NR_lstat64) \
  8291. || defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64) \
  8292. || defined(TARGET_NR_statx)
  8293. struct stat st;
  8294. #endif
  8295. #if defined(TARGET_NR_statfs) || defined(TARGET_NR_statfs64) \
  8296. || defined(TARGET_NR_fstatfs)
  8297. struct statfs stfs;
  8298. #endif
  8299. void *p;
  8300. switch(num) {
  8301. case TARGET_NR_exit:
  8302. /* In old applications this may be used to implement _exit(2).
  8303. However in threaded applications it is used for thread termination,
  8304. and _exit_group is used for application termination.
  8305. Do thread termination if we have more then one thread. */
  8306. if (block_signals()) {
  8307. return -QEMU_ERESTARTSYS;
  8308. }
  8309. pthread_mutex_lock(&clone_lock);
  8310. if (CPU_NEXT(first_cpu)) {
  8311. TaskState *ts = get_task_state(cpu);
  8312. if (ts->child_tidptr) {
  8313. put_user_u32(0, ts->child_tidptr);
  8314. do_sys_futex(g2h(cpu, ts->child_tidptr),
  8315. FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
  8316. }
  8317. object_unparent(OBJECT(cpu));
  8318. object_unref(OBJECT(cpu));
  8319. /*
  8320. * At this point the CPU should be unrealized and removed
  8321. * from cpu lists. We can clean-up the rest of the thread
  8322. * data without the lock held.
  8323. */
  8324. pthread_mutex_unlock(&clone_lock);
  8325. thread_cpu = NULL;
  8326. g_free(ts);
  8327. rcu_unregister_thread();
  8328. pthread_exit(NULL);
  8329. }
  8330. pthread_mutex_unlock(&clone_lock);
  8331. preexit_cleanup(cpu_env, arg1);
  8332. _exit(arg1);
  8333. return 0; /* avoid warning */
  8334. case TARGET_NR_read:
  8335. if (arg2 == 0 && arg3 == 0) {
  8336. return get_errno(safe_read(arg1, 0, 0));
  8337. } else {
  8338. if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
  8339. return -TARGET_EFAULT;
  8340. ret = get_errno(safe_read(arg1, p, arg3));
  8341. if (ret >= 0 &&
  8342. fd_trans_host_to_target_data(arg1)) {
  8343. ret = fd_trans_host_to_target_data(arg1)(p, ret);
  8344. }
  8345. unlock_user(p, arg2, ret);
  8346. }
  8347. return ret;
  8348. case TARGET_NR_write:
  8349. if (arg2 == 0 && arg3 == 0) {
  8350. return get_errno(safe_write(arg1, 0, 0));
  8351. }
  8352. if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
  8353. return -TARGET_EFAULT;
  8354. if (fd_trans_target_to_host_data(arg1)) {
  8355. void *copy = g_malloc(arg3);
  8356. memcpy(copy, p, arg3);
  8357. ret = fd_trans_target_to_host_data(arg1)(copy, arg3);
  8358. if (ret >= 0) {
  8359. ret = get_errno(safe_write(arg1, copy, ret));
  8360. }
  8361. g_free(copy);
  8362. } else {
  8363. ret = get_errno(safe_write(arg1, p, arg3));
  8364. }
  8365. unlock_user(p, arg2, 0);
  8366. return ret;
  8367. #ifdef TARGET_NR_open
  8368. case TARGET_NR_open:
  8369. if (!(p = lock_user_string(arg1)))
  8370. return -TARGET_EFAULT;
  8371. ret = get_errno(do_guest_openat(cpu_env, AT_FDCWD, p,
  8372. target_to_host_bitmask(arg2, fcntl_flags_tbl),
  8373. arg3, true));
  8374. fd_trans_unregister(ret);
  8375. unlock_user(p, arg1, 0);
  8376. return ret;
  8377. #endif
  8378. case TARGET_NR_openat:
  8379. if (!(p = lock_user_string(arg2)))
  8380. return -TARGET_EFAULT;
  8381. ret = get_errno(do_guest_openat(cpu_env, arg1, p,
  8382. target_to_host_bitmask(arg3, fcntl_flags_tbl),
  8383. arg4, true));
  8384. fd_trans_unregister(ret);
  8385. unlock_user(p, arg2, 0);
  8386. return ret;
  8387. case TARGET_NR_openat2:
  8388. ret = do_openat2(cpu_env, arg1, arg2, arg3, arg4);
  8389. return ret;
  8390. #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  8391. case TARGET_NR_name_to_handle_at:
  8392. ret = do_name_to_handle_at(arg1, arg2, arg3, arg4, arg5);
  8393. return ret;
  8394. #endif
  8395. #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  8396. case TARGET_NR_open_by_handle_at:
  8397. ret = do_open_by_handle_at(arg1, arg2, arg3);
  8398. fd_trans_unregister(ret);
  8399. return ret;
  8400. #endif
  8401. #if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
  8402. case TARGET_NR_pidfd_open:
  8403. return get_errno(pidfd_open(arg1, arg2));
  8404. #endif
  8405. #if defined(__NR_pidfd_send_signal) && defined(TARGET_NR_pidfd_send_signal)
  8406. case TARGET_NR_pidfd_send_signal:
  8407. {
  8408. siginfo_t uinfo, *puinfo;
  8409. if (arg3) {
  8410. p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1);
  8411. if (!p) {
  8412. return -TARGET_EFAULT;
  8413. }
  8414. target_to_host_siginfo(&uinfo, p);
  8415. unlock_user(p, arg3, 0);
  8416. puinfo = &uinfo;
  8417. } else {
  8418. puinfo = NULL;
  8419. }
  8420. ret = get_errno(pidfd_send_signal(arg1, target_to_host_signal(arg2),
  8421. puinfo, arg4));
  8422. }
  8423. return ret;
  8424. #endif
  8425. #if defined(__NR_pidfd_getfd) && defined(TARGET_NR_pidfd_getfd)
  8426. case TARGET_NR_pidfd_getfd:
  8427. return get_errno(pidfd_getfd(arg1, arg2, arg3));
  8428. #endif
  8429. case TARGET_NR_close:
  8430. fd_trans_unregister(arg1);
  8431. return get_errno(close(arg1));
  8432. #if defined(__NR_close_range) && defined(TARGET_NR_close_range)
  8433. case TARGET_NR_close_range:
  8434. ret = get_errno(sys_close_range(arg1, arg2, arg3));
  8435. if (ret == 0 && !(arg3 & CLOSE_RANGE_CLOEXEC)) {
  8436. abi_long fd, maxfd;
  8437. maxfd = MIN(arg2, target_fd_max);
  8438. for (fd = arg1; fd < maxfd; fd++) {
  8439. fd_trans_unregister(fd);
  8440. }
  8441. }
  8442. return ret;
  8443. #endif
  8444. case TARGET_NR_brk:
  8445. return do_brk(arg1);
  8446. #ifdef TARGET_NR_fork
  8447. case TARGET_NR_fork:
  8448. return get_errno(do_fork(cpu_env, TARGET_SIGCHLD, 0, 0, 0, 0));
  8449. #endif
  8450. #ifdef TARGET_NR_waitpid
  8451. case TARGET_NR_waitpid:
  8452. {
  8453. int status;
  8454. ret = get_errno(safe_wait4(arg1, &status, arg3, 0));
  8455. if (!is_error(ret) && arg2 && ret
  8456. && put_user_s32(host_to_target_waitstatus(status), arg2))
  8457. return -TARGET_EFAULT;
  8458. }
  8459. return ret;
  8460. #endif
  8461. #ifdef TARGET_NR_waitid
  8462. case TARGET_NR_waitid:
  8463. {
  8464. struct rusage ru;
  8465. siginfo_t info;
  8466. ret = get_errno(safe_waitid(arg1, arg2, (arg3 ? &info : NULL),
  8467. arg4, (arg5 ? &ru : NULL)));
  8468. if (!is_error(ret)) {
  8469. if (arg3) {
  8470. p = lock_user(VERIFY_WRITE, arg3,
  8471. sizeof(target_siginfo_t), 0);
  8472. if (!p) {
  8473. return -TARGET_EFAULT;
  8474. }
  8475. host_to_target_siginfo(p, &info);
  8476. unlock_user(p, arg3, sizeof(target_siginfo_t));
  8477. }
  8478. if (arg5 && host_to_target_rusage(arg5, &ru)) {
  8479. return -TARGET_EFAULT;
  8480. }
  8481. }
  8482. }
  8483. return ret;
  8484. #endif
  8485. #ifdef TARGET_NR_creat /* not on alpha */
  8486. case TARGET_NR_creat:
  8487. if (!(p = lock_user_string(arg1)))
  8488. return -TARGET_EFAULT;
  8489. ret = get_errno(creat(p, arg2));
  8490. fd_trans_unregister(ret);
  8491. unlock_user(p, arg1, 0);
  8492. return ret;
  8493. #endif
  8494. #ifdef TARGET_NR_link
  8495. case TARGET_NR_link:
  8496. {
  8497. void * p2;
  8498. p = lock_user_string(arg1);
  8499. p2 = lock_user_string(arg2);
  8500. if (!p || !p2)
  8501. ret = -TARGET_EFAULT;
  8502. else
  8503. ret = get_errno(link(p, p2));
  8504. unlock_user(p2, arg2, 0);
  8505. unlock_user(p, arg1, 0);
  8506. }
  8507. return ret;
  8508. #endif
  8509. #if defined(TARGET_NR_linkat)
  8510. case TARGET_NR_linkat:
  8511. {
  8512. void * p2 = NULL;
  8513. if (!arg2 || !arg4)
  8514. return -TARGET_EFAULT;
  8515. p = lock_user_string(arg2);
  8516. p2 = lock_user_string(arg4);
  8517. if (!p || !p2)
  8518. ret = -TARGET_EFAULT;
  8519. else
  8520. ret = get_errno(linkat(arg1, p, arg3, p2, arg5));
  8521. unlock_user(p, arg2, 0);
  8522. unlock_user(p2, arg4, 0);
  8523. }
  8524. return ret;
  8525. #endif
  8526. #ifdef TARGET_NR_unlink
  8527. case TARGET_NR_unlink:
  8528. if (!(p = lock_user_string(arg1)))
  8529. return -TARGET_EFAULT;
  8530. ret = get_errno(unlink(p));
  8531. unlock_user(p, arg1, 0);
  8532. return ret;
  8533. #endif
  8534. #if defined(TARGET_NR_unlinkat)
  8535. case TARGET_NR_unlinkat:
  8536. if (!(p = lock_user_string(arg2)))
  8537. return -TARGET_EFAULT;
  8538. ret = get_errno(unlinkat(arg1, p, arg3));
  8539. unlock_user(p, arg2, 0);
  8540. return ret;
  8541. #endif
  8542. case TARGET_NR_execveat:
  8543. return do_execv(cpu_env, arg1, arg2, arg3, arg4, arg5, true);
  8544. case TARGET_NR_execve:
  8545. return do_execv(cpu_env, AT_FDCWD, arg1, arg2, arg3, 0, false);
  8546. case TARGET_NR_chdir:
  8547. if (!(p = lock_user_string(arg1)))
  8548. return -TARGET_EFAULT;
  8549. ret = get_errno(chdir(p));
  8550. unlock_user(p, arg1, 0);
  8551. return ret;
  8552. #ifdef TARGET_NR_time
  8553. case TARGET_NR_time:
  8554. {
  8555. time_t host_time;
  8556. ret = get_errno(time(&host_time));
  8557. if (!is_error(ret)
  8558. && arg1
  8559. && put_user_sal(host_time, arg1))
  8560. return -TARGET_EFAULT;
  8561. }
  8562. return ret;
  8563. #endif
  8564. #ifdef TARGET_NR_mknod
  8565. case TARGET_NR_mknod:
  8566. if (!(p = lock_user_string(arg1)))
  8567. return -TARGET_EFAULT;
  8568. ret = get_errno(mknod(p, arg2, arg3));
  8569. unlock_user(p, arg1, 0);
  8570. return ret;
  8571. #endif
  8572. #if defined(TARGET_NR_mknodat)
  8573. case TARGET_NR_mknodat:
  8574. if (!(p = lock_user_string(arg2)))
  8575. return -TARGET_EFAULT;
  8576. ret = get_errno(mknodat(arg1, p, arg3, arg4));
  8577. unlock_user(p, arg2, 0);
  8578. return ret;
  8579. #endif
  8580. #ifdef TARGET_NR_chmod
  8581. case TARGET_NR_chmod:
  8582. if (!(p = lock_user_string(arg1)))
  8583. return -TARGET_EFAULT;
  8584. ret = get_errno(chmod(p, arg2));
  8585. unlock_user(p, arg1, 0);
  8586. return ret;
  8587. #endif
  8588. #ifdef TARGET_NR_lseek
  8589. case TARGET_NR_lseek:
  8590. return get_errno(lseek(arg1, arg2, arg3));
  8591. #endif
  8592. #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
  8593. /* Alpha specific */
  8594. case TARGET_NR_getxpid:
  8595. cpu_env->ir[IR_A4] = getppid();
  8596. return get_errno(getpid());
  8597. #endif
  8598. #ifdef TARGET_NR_getpid
  8599. case TARGET_NR_getpid:
  8600. return get_errno(getpid());
  8601. #endif
  8602. case TARGET_NR_mount:
  8603. {
  8604. /* need to look at the data field */
  8605. void *p2, *p3;
  8606. if (arg1) {
  8607. p = lock_user_string(arg1);
  8608. if (!p) {
  8609. return -TARGET_EFAULT;
  8610. }
  8611. } else {
  8612. p = NULL;
  8613. }
  8614. p2 = lock_user_string(arg2);
  8615. if (!p2) {
  8616. if (arg1) {
  8617. unlock_user(p, arg1, 0);
  8618. }
  8619. return -TARGET_EFAULT;
  8620. }
  8621. if (arg3) {
  8622. p3 = lock_user_string(arg3);
  8623. if (!p3) {
  8624. if (arg1) {
  8625. unlock_user(p, arg1, 0);
  8626. }
  8627. unlock_user(p2, arg2, 0);
  8628. return -TARGET_EFAULT;
  8629. }
  8630. } else {
  8631. p3 = NULL;
  8632. }
  8633. /* FIXME - arg5 should be locked, but it isn't clear how to
  8634. * do that since it's not guaranteed to be a NULL-terminated
  8635. * string.
  8636. */
  8637. if (!arg5) {
  8638. ret = mount(p, p2, p3, (unsigned long)arg4, NULL);
  8639. } else {
  8640. ret = mount(p, p2, p3, (unsigned long)arg4, g2h(cpu, arg5));
  8641. }
  8642. ret = get_errno(ret);
  8643. if (arg1) {
  8644. unlock_user(p, arg1, 0);
  8645. }
  8646. unlock_user(p2, arg2, 0);
  8647. if (arg3) {
  8648. unlock_user(p3, arg3, 0);
  8649. }
  8650. }
  8651. return ret;
  8652. #if defined(TARGET_NR_umount) || defined(TARGET_NR_oldumount)
  8653. #if defined(TARGET_NR_umount)
  8654. case TARGET_NR_umount:
  8655. #endif
  8656. #if defined(TARGET_NR_oldumount)
  8657. case TARGET_NR_oldumount:
  8658. #endif
  8659. if (!(p = lock_user_string(arg1)))
  8660. return -TARGET_EFAULT;
  8661. ret = get_errno(umount(p));
  8662. unlock_user(p, arg1, 0);
  8663. return ret;
  8664. #endif
  8665. #if defined(TARGET_NR_move_mount) && defined(__NR_move_mount)
  8666. case TARGET_NR_move_mount:
  8667. {
  8668. void *p2, *p4;
  8669. if (!arg2 || !arg4) {
  8670. return -TARGET_EFAULT;
  8671. }
  8672. p2 = lock_user_string(arg2);
  8673. if (!p2) {
  8674. return -TARGET_EFAULT;
  8675. }
  8676. p4 = lock_user_string(arg4);
  8677. if (!p4) {
  8678. unlock_user(p2, arg2, 0);
  8679. return -TARGET_EFAULT;
  8680. }
  8681. ret = get_errno(sys_move_mount(arg1, p2, arg3, p4, arg5));
  8682. unlock_user(p2, arg2, 0);
  8683. unlock_user(p4, arg4, 0);
  8684. return ret;
  8685. }
  8686. #endif
  8687. #if defined(TARGET_NR_open_tree) && defined(__NR_open_tree)
  8688. case TARGET_NR_open_tree:
  8689. {
  8690. void *p2;
  8691. int host_flags;
  8692. if (!arg2) {
  8693. return -TARGET_EFAULT;
  8694. }
  8695. p2 = lock_user_string(arg2);
  8696. if (!p2) {
  8697. return -TARGET_EFAULT;
  8698. }
  8699. host_flags = arg3 & ~TARGET_O_CLOEXEC;
  8700. if (arg3 & TARGET_O_CLOEXEC) {
  8701. host_flags |= O_CLOEXEC;
  8702. }
  8703. ret = get_errno(sys_open_tree(arg1, p2, host_flags));
  8704. unlock_user(p2, arg2, 0);
  8705. return ret;
  8706. }
  8707. #endif
  8708. #ifdef TARGET_NR_stime /* not on alpha */
  8709. case TARGET_NR_stime:
  8710. {
  8711. struct timespec ts;
  8712. ts.tv_nsec = 0;
  8713. if (get_user_sal(ts.tv_sec, arg1)) {
  8714. return -TARGET_EFAULT;
  8715. }
  8716. return get_errno(clock_settime(CLOCK_REALTIME, &ts));
  8717. }
  8718. #endif
  8719. #ifdef TARGET_NR_alarm /* not on alpha */
  8720. case TARGET_NR_alarm:
  8721. return alarm(arg1);
  8722. #endif
  8723. #ifdef TARGET_NR_pause /* not on alpha */
  8724. case TARGET_NR_pause:
  8725. if (!block_signals()) {
  8726. sigsuspend(&get_task_state(cpu)->signal_mask);
  8727. }
  8728. return -TARGET_EINTR;
  8729. #endif
  8730. #ifdef TARGET_NR_utime
  8731. case TARGET_NR_utime:
  8732. {
  8733. struct utimbuf tbuf, *host_tbuf;
  8734. struct target_utimbuf *target_tbuf;
  8735. if (arg2) {
  8736. if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
  8737. return -TARGET_EFAULT;
  8738. tbuf.actime = tswapal(target_tbuf->actime);
  8739. tbuf.modtime = tswapal(target_tbuf->modtime);
  8740. unlock_user_struct(target_tbuf, arg2, 0);
  8741. host_tbuf = &tbuf;
  8742. } else {
  8743. host_tbuf = NULL;
  8744. }
  8745. if (!(p = lock_user_string(arg1)))
  8746. return -TARGET_EFAULT;
  8747. ret = get_errno(utime(p, host_tbuf));
  8748. unlock_user(p, arg1, 0);
  8749. }
  8750. return ret;
  8751. #endif
  8752. #ifdef TARGET_NR_utimes
  8753. case TARGET_NR_utimes:
  8754. {
  8755. struct timeval *tvp, tv[2];
  8756. if (arg2) {
  8757. if (copy_from_user_timeval(&tv[0], arg2)
  8758. || copy_from_user_timeval(&tv[1],
  8759. arg2 + sizeof(struct target_timeval)))
  8760. return -TARGET_EFAULT;
  8761. tvp = tv;
  8762. } else {
  8763. tvp = NULL;
  8764. }
  8765. if (!(p = lock_user_string(arg1)))
  8766. return -TARGET_EFAULT;
  8767. ret = get_errno(utimes(p, tvp));
  8768. unlock_user(p, arg1, 0);
  8769. }
  8770. return ret;
  8771. #endif
  8772. #if defined(TARGET_NR_futimesat)
  8773. case TARGET_NR_futimesat:
  8774. {
  8775. struct timeval *tvp, tv[2];
  8776. if (arg3) {
  8777. if (copy_from_user_timeval(&tv[0], arg3)
  8778. || copy_from_user_timeval(&tv[1],
  8779. arg3 + sizeof(struct target_timeval)))
  8780. return -TARGET_EFAULT;
  8781. tvp = tv;
  8782. } else {
  8783. tvp = NULL;
  8784. }
  8785. if (!(p = lock_user_string(arg2))) {
  8786. return -TARGET_EFAULT;
  8787. }
  8788. ret = get_errno(futimesat(arg1, path(p), tvp));
  8789. unlock_user(p, arg2, 0);
  8790. }
  8791. return ret;
  8792. #endif
  8793. #ifdef TARGET_NR_access
  8794. case TARGET_NR_access:
  8795. if (!(p = lock_user_string(arg1))) {
  8796. return -TARGET_EFAULT;
  8797. }
  8798. ret = get_errno(access(path(p), arg2));
  8799. unlock_user(p, arg1, 0);
  8800. return ret;
  8801. #endif
  8802. #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
  8803. case TARGET_NR_faccessat:
  8804. if (!(p = lock_user_string(arg2))) {
  8805. return -TARGET_EFAULT;
  8806. }
  8807. ret = get_errno(faccessat(arg1, p, arg3, 0));
  8808. unlock_user(p, arg2, 0);
  8809. return ret;
  8810. #endif
  8811. #if defined(TARGET_NR_faccessat2)
  8812. case TARGET_NR_faccessat2:
  8813. if (!(p = lock_user_string(arg2))) {
  8814. return -TARGET_EFAULT;
  8815. }
  8816. ret = get_errno(faccessat(arg1, p, arg3, arg4));
  8817. unlock_user(p, arg2, 0);
  8818. return ret;
  8819. #endif
  8820. #ifdef TARGET_NR_nice /* not on alpha */
  8821. case TARGET_NR_nice:
  8822. return get_errno(nice(arg1));
  8823. #endif
  8824. case TARGET_NR_sync:
  8825. sync();
  8826. return 0;
  8827. #if defined(TARGET_NR_syncfs) && defined(CONFIG_SYNCFS)
  8828. case TARGET_NR_syncfs:
  8829. return get_errno(syncfs(arg1));
  8830. #endif
  8831. case TARGET_NR_kill:
  8832. return get_errno(safe_kill(arg1, target_to_host_signal(arg2)));
  8833. #ifdef TARGET_NR_rename
  8834. case TARGET_NR_rename:
  8835. {
  8836. void *p2;
  8837. p = lock_user_string(arg1);
  8838. p2 = lock_user_string(arg2);
  8839. if (!p || !p2)
  8840. ret = -TARGET_EFAULT;
  8841. else
  8842. ret = get_errno(rename(p, p2));
  8843. unlock_user(p2, arg2, 0);
  8844. unlock_user(p, arg1, 0);
  8845. }
  8846. return ret;
  8847. #endif
  8848. #if defined(TARGET_NR_renameat)
  8849. case TARGET_NR_renameat:
  8850. {
  8851. void *p2;
  8852. p = lock_user_string(arg2);
  8853. p2 = lock_user_string(arg4);
  8854. if (!p || !p2)
  8855. ret = -TARGET_EFAULT;
  8856. else
  8857. ret = get_errno(renameat(arg1, p, arg3, p2));
  8858. unlock_user(p2, arg4, 0);
  8859. unlock_user(p, arg2, 0);
  8860. }
  8861. return ret;
  8862. #endif
  8863. #if defined(TARGET_NR_renameat2)
  8864. case TARGET_NR_renameat2:
  8865. {
  8866. void *p2;
  8867. p = lock_user_string(arg2);
  8868. p2 = lock_user_string(arg4);
  8869. if (!p || !p2) {
  8870. ret = -TARGET_EFAULT;
  8871. } else {
  8872. ret = get_errno(sys_renameat2(arg1, p, arg3, p2, arg5));
  8873. }
  8874. unlock_user(p2, arg4, 0);
  8875. unlock_user(p, arg2, 0);
  8876. }
  8877. return ret;
  8878. #endif
  8879. #ifdef TARGET_NR_mkdir
  8880. case TARGET_NR_mkdir:
  8881. if (!(p = lock_user_string(arg1)))
  8882. return -TARGET_EFAULT;
  8883. ret = get_errno(mkdir(p, arg2));
  8884. unlock_user(p, arg1, 0);
  8885. return ret;
  8886. #endif
  8887. #if defined(TARGET_NR_mkdirat)
  8888. case TARGET_NR_mkdirat:
  8889. if (!(p = lock_user_string(arg2)))
  8890. return -TARGET_EFAULT;
  8891. ret = get_errno(mkdirat(arg1, p, arg3));
  8892. unlock_user(p, arg2, 0);
  8893. return ret;
  8894. #endif
  8895. #ifdef TARGET_NR_rmdir
  8896. case TARGET_NR_rmdir:
  8897. if (!(p = lock_user_string(arg1)))
  8898. return -TARGET_EFAULT;
  8899. ret = get_errno(rmdir(p));
  8900. unlock_user(p, arg1, 0);
  8901. return ret;
  8902. #endif
  8903. case TARGET_NR_dup:
  8904. ret = get_errno(dup(arg1));
  8905. if (ret >= 0) {
  8906. fd_trans_dup(arg1, ret);
  8907. }
  8908. return ret;
  8909. #ifdef TARGET_NR_pipe
  8910. case TARGET_NR_pipe:
  8911. return do_pipe(cpu_env, arg1, 0, 0);
  8912. #endif
  8913. #ifdef TARGET_NR_pipe2
  8914. case TARGET_NR_pipe2:
  8915. return do_pipe(cpu_env, arg1,
  8916. target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
  8917. #endif
  8918. case TARGET_NR_times:
  8919. {
  8920. struct target_tms *tmsp;
  8921. struct tms tms;
  8922. ret = get_errno(times(&tms));
  8923. if (arg1) {
  8924. tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
  8925. if (!tmsp)
  8926. return -TARGET_EFAULT;
  8927. tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
  8928. tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
  8929. tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
  8930. tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
  8931. }
  8932. if (!is_error(ret))
  8933. ret = host_to_target_clock_t(ret);
  8934. }
  8935. return ret;
  8936. case TARGET_NR_acct:
  8937. if (arg1 == 0) {
  8938. ret = get_errno(acct(NULL));
  8939. } else {
  8940. if (!(p = lock_user_string(arg1))) {
  8941. return -TARGET_EFAULT;
  8942. }
  8943. ret = get_errno(acct(path(p)));
  8944. unlock_user(p, arg1, 0);
  8945. }
  8946. return ret;
  8947. #ifdef TARGET_NR_umount2
  8948. case TARGET_NR_umount2:
  8949. if (!(p = lock_user_string(arg1)))
  8950. return -TARGET_EFAULT;
  8951. ret = get_errno(umount2(p, arg2));
  8952. unlock_user(p, arg1, 0);
  8953. return ret;
  8954. #endif
  8955. case TARGET_NR_ioctl:
  8956. return do_ioctl(arg1, arg2, arg3);
  8957. #ifdef TARGET_NR_fcntl
  8958. case TARGET_NR_fcntl:
  8959. return do_fcntl(arg1, arg2, arg3);
  8960. #endif
  8961. case TARGET_NR_setpgid:
  8962. return get_errno(setpgid(arg1, arg2));
  8963. case TARGET_NR_umask:
  8964. return get_errno(umask(arg1));
  8965. case TARGET_NR_chroot:
  8966. if (!(p = lock_user_string(arg1)))
  8967. return -TARGET_EFAULT;
  8968. ret = get_errno(chroot(p));
  8969. unlock_user(p, arg1, 0);
  8970. return ret;
  8971. #ifdef TARGET_NR_dup2
  8972. case TARGET_NR_dup2:
  8973. ret = get_errno(dup2(arg1, arg2));
  8974. if (ret >= 0) {
  8975. fd_trans_dup(arg1, arg2);
  8976. }
  8977. return ret;
  8978. #endif
  8979. #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
  8980. case TARGET_NR_dup3:
  8981. {
  8982. int host_flags;
  8983. if ((arg3 & ~TARGET_O_CLOEXEC) != 0) {
  8984. return -EINVAL;
  8985. }
  8986. host_flags = target_to_host_bitmask(arg3, fcntl_flags_tbl);
  8987. ret = get_errno(dup3(arg1, arg2, host_flags));
  8988. if (ret >= 0) {
  8989. fd_trans_dup(arg1, arg2);
  8990. }
  8991. return ret;
  8992. }
  8993. #endif
  8994. #ifdef TARGET_NR_getppid /* not on alpha */
  8995. case TARGET_NR_getppid:
  8996. return get_errno(getppid());
  8997. #endif
  8998. #ifdef TARGET_NR_getpgrp
  8999. case TARGET_NR_getpgrp:
  9000. return get_errno(getpgrp());
  9001. #endif
  9002. case TARGET_NR_setsid:
  9003. return get_errno(setsid());
  9004. #ifdef TARGET_NR_sigaction
  9005. case TARGET_NR_sigaction:
  9006. {
  9007. #if defined(TARGET_MIPS)
  9008. struct target_sigaction act, oact, *pact, *old_act;
  9009. if (arg2) {
  9010. if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
  9011. return -TARGET_EFAULT;
  9012. act._sa_handler = old_act->_sa_handler;
  9013. target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
  9014. act.sa_flags = old_act->sa_flags;
  9015. unlock_user_struct(old_act, arg2, 0);
  9016. pact = &act;
  9017. } else {
  9018. pact = NULL;
  9019. }
  9020. ret = get_errno(do_sigaction(arg1, pact, &oact, 0));
  9021. if (!is_error(ret) && arg3) {
  9022. if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
  9023. return -TARGET_EFAULT;
  9024. old_act->_sa_handler = oact._sa_handler;
  9025. old_act->sa_flags = oact.sa_flags;
  9026. old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
  9027. old_act->sa_mask.sig[1] = 0;
  9028. old_act->sa_mask.sig[2] = 0;
  9029. old_act->sa_mask.sig[3] = 0;
  9030. unlock_user_struct(old_act, arg3, 1);
  9031. }
  9032. #else
  9033. struct target_old_sigaction *old_act;
  9034. struct target_sigaction act, oact, *pact;
  9035. if (arg2) {
  9036. if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
  9037. return -TARGET_EFAULT;
  9038. act._sa_handler = old_act->_sa_handler;
  9039. target_siginitset(&act.sa_mask, old_act->sa_mask);
  9040. act.sa_flags = old_act->sa_flags;
  9041. #ifdef TARGET_ARCH_HAS_SA_RESTORER
  9042. act.sa_restorer = old_act->sa_restorer;
  9043. #endif
  9044. unlock_user_struct(old_act, arg2, 0);
  9045. pact = &act;
  9046. } else {
  9047. pact = NULL;
  9048. }
  9049. ret = get_errno(do_sigaction(arg1, pact, &oact, 0));
  9050. if (!is_error(ret) && arg3) {
  9051. if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
  9052. return -TARGET_EFAULT;
  9053. old_act->_sa_handler = oact._sa_handler;
  9054. old_act->sa_mask = oact.sa_mask.sig[0];
  9055. old_act->sa_flags = oact.sa_flags;
  9056. #ifdef TARGET_ARCH_HAS_SA_RESTORER
  9057. old_act->sa_restorer = oact.sa_restorer;
  9058. #endif
  9059. unlock_user_struct(old_act, arg3, 1);
  9060. }
  9061. #endif
  9062. }
  9063. return ret;
  9064. #endif
  9065. case TARGET_NR_rt_sigaction:
  9066. {
  9067. /*
  9068. * For Alpha and SPARC this is a 5 argument syscall, with
  9069. * a 'restorer' parameter which must be copied into the
  9070. * sa_restorer field of the sigaction struct.
  9071. * For Alpha that 'restorer' is arg5; for SPARC it is arg4,
  9072. * and arg5 is the sigsetsize.
  9073. */
  9074. #if defined(TARGET_ALPHA)
  9075. target_ulong sigsetsize = arg4;
  9076. target_ulong restorer = arg5;
  9077. #elif defined(TARGET_SPARC)
  9078. target_ulong restorer = arg4;
  9079. target_ulong sigsetsize = arg5;
  9080. #else
  9081. target_ulong sigsetsize = arg4;
  9082. target_ulong restorer = 0;
  9083. #endif
  9084. struct target_sigaction *act = NULL;
  9085. struct target_sigaction *oact = NULL;
  9086. if (sigsetsize != sizeof(target_sigset_t)) {
  9087. return -TARGET_EINVAL;
  9088. }
  9089. if (arg2 && !lock_user_struct(VERIFY_READ, act, arg2, 1)) {
  9090. return -TARGET_EFAULT;
  9091. }
  9092. if (arg3 && !lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
  9093. ret = -TARGET_EFAULT;
  9094. } else {
  9095. ret = get_errno(do_sigaction(arg1, act, oact, restorer));
  9096. if (oact) {
  9097. unlock_user_struct(oact, arg3, 1);
  9098. }
  9099. }
  9100. if (act) {
  9101. unlock_user_struct(act, arg2, 0);
  9102. }
  9103. }
  9104. return ret;
  9105. #ifdef TARGET_NR_sgetmask /* not on alpha */
  9106. case TARGET_NR_sgetmask:
  9107. {
  9108. sigset_t cur_set;
  9109. abi_ulong target_set;
  9110. ret = do_sigprocmask(0, NULL, &cur_set);
  9111. if (!ret) {
  9112. host_to_target_old_sigset(&target_set, &cur_set);
  9113. ret = target_set;
  9114. }
  9115. }
  9116. return ret;
  9117. #endif
  9118. #ifdef TARGET_NR_ssetmask /* not on alpha */
  9119. case TARGET_NR_ssetmask:
  9120. {
  9121. sigset_t set, oset;
  9122. abi_ulong target_set = arg1;
  9123. target_to_host_old_sigset(&set, &target_set);
  9124. ret = do_sigprocmask(SIG_SETMASK, &set, &oset);
  9125. if (!ret) {
  9126. host_to_target_old_sigset(&target_set, &oset);
  9127. ret = target_set;
  9128. }
  9129. }
  9130. return ret;
  9131. #endif
  9132. #ifdef TARGET_NR_sigprocmask
  9133. case TARGET_NR_sigprocmask:
  9134. {
  9135. #if defined(TARGET_ALPHA)
  9136. sigset_t set, oldset;
  9137. abi_ulong mask;
  9138. int how;
  9139. switch (arg1) {
  9140. case TARGET_SIG_BLOCK:
  9141. how = SIG_BLOCK;
  9142. break;
  9143. case TARGET_SIG_UNBLOCK:
  9144. how = SIG_UNBLOCK;
  9145. break;
  9146. case TARGET_SIG_SETMASK:
  9147. how = SIG_SETMASK;
  9148. break;
  9149. default:
  9150. return -TARGET_EINVAL;
  9151. }
  9152. mask = arg2;
  9153. target_to_host_old_sigset(&set, &mask);
  9154. ret = do_sigprocmask(how, &set, &oldset);
  9155. if (!is_error(ret)) {
  9156. host_to_target_old_sigset(&mask, &oldset);
  9157. ret = mask;
  9158. cpu_env->ir[IR_V0] = 0; /* force no error */
  9159. }
  9160. #else
  9161. sigset_t set, oldset, *set_ptr;
  9162. int how;
  9163. if (arg2) {
  9164. p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1);
  9165. if (!p) {
  9166. return -TARGET_EFAULT;
  9167. }
  9168. target_to_host_old_sigset(&set, p);
  9169. unlock_user(p, arg2, 0);
  9170. set_ptr = &set;
  9171. switch (arg1) {
  9172. case TARGET_SIG_BLOCK:
  9173. how = SIG_BLOCK;
  9174. break;
  9175. case TARGET_SIG_UNBLOCK:
  9176. how = SIG_UNBLOCK;
  9177. break;
  9178. case TARGET_SIG_SETMASK:
  9179. how = SIG_SETMASK;
  9180. break;
  9181. default:
  9182. return -TARGET_EINVAL;
  9183. }
  9184. } else {
  9185. how = 0;
  9186. set_ptr = NULL;
  9187. }
  9188. ret = do_sigprocmask(how, set_ptr, &oldset);
  9189. if (!is_error(ret) && arg3) {
  9190. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
  9191. return -TARGET_EFAULT;
  9192. host_to_target_old_sigset(p, &oldset);
  9193. unlock_user(p, arg3, sizeof(target_sigset_t));
  9194. }
  9195. #endif
  9196. }
  9197. return ret;
  9198. #endif
  9199. case TARGET_NR_rt_sigprocmask:
  9200. {
  9201. int how = arg1;
  9202. sigset_t set, oldset, *set_ptr;
  9203. if (arg4 != sizeof(target_sigset_t)) {
  9204. return -TARGET_EINVAL;
  9205. }
  9206. if (arg2) {
  9207. p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1);
  9208. if (!p) {
  9209. return -TARGET_EFAULT;
  9210. }
  9211. target_to_host_sigset(&set, p);
  9212. unlock_user(p, arg2, 0);
  9213. set_ptr = &set;
  9214. switch(how) {
  9215. case TARGET_SIG_BLOCK:
  9216. how = SIG_BLOCK;
  9217. break;
  9218. case TARGET_SIG_UNBLOCK:
  9219. how = SIG_UNBLOCK;
  9220. break;
  9221. case TARGET_SIG_SETMASK:
  9222. how = SIG_SETMASK;
  9223. break;
  9224. default:
  9225. return -TARGET_EINVAL;
  9226. }
  9227. } else {
  9228. how = 0;
  9229. set_ptr = NULL;
  9230. }
  9231. ret = do_sigprocmask(how, set_ptr, &oldset);
  9232. if (!is_error(ret) && arg3) {
  9233. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
  9234. return -TARGET_EFAULT;
  9235. host_to_target_sigset(p, &oldset);
  9236. unlock_user(p, arg3, sizeof(target_sigset_t));
  9237. }
  9238. }
  9239. return ret;
  9240. #ifdef TARGET_NR_sigpending
  9241. case TARGET_NR_sigpending:
  9242. {
  9243. sigset_t set;
  9244. ret = get_errno(sigpending(&set));
  9245. if (!is_error(ret)) {
  9246. if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
  9247. return -TARGET_EFAULT;
  9248. host_to_target_old_sigset(p, &set);
  9249. unlock_user(p, arg1, sizeof(target_sigset_t));
  9250. }
  9251. }
  9252. return ret;
  9253. #endif
  9254. case TARGET_NR_rt_sigpending:
  9255. {
  9256. sigset_t set;
  9257. /* Yes, this check is >, not != like most. We follow the kernel's
  9258. * logic and it does it like this because it implements
  9259. * NR_sigpending through the same code path, and in that case
  9260. * the old_sigset_t is smaller in size.
  9261. */
  9262. if (arg2 > sizeof(target_sigset_t)) {
  9263. return -TARGET_EINVAL;
  9264. }
  9265. ret = get_errno(sigpending(&set));
  9266. if (!is_error(ret)) {
  9267. if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
  9268. return -TARGET_EFAULT;
  9269. host_to_target_sigset(p, &set);
  9270. unlock_user(p, arg1, sizeof(target_sigset_t));
  9271. }
  9272. }
  9273. return ret;
  9274. #ifdef TARGET_NR_sigsuspend
  9275. case TARGET_NR_sigsuspend:
  9276. {
  9277. sigset_t *set;
  9278. #if defined(TARGET_ALPHA)
  9279. TaskState *ts = get_task_state(cpu);
  9280. /* target_to_host_old_sigset will bswap back */
  9281. abi_ulong mask = tswapal(arg1);
  9282. set = &ts->sigsuspend_mask;
  9283. target_to_host_old_sigset(set, &mask);
  9284. #else
  9285. ret = process_sigsuspend_mask(&set, arg1, sizeof(target_sigset_t));
  9286. if (ret != 0) {
  9287. return ret;
  9288. }
  9289. #endif
  9290. ret = get_errno(safe_rt_sigsuspend(set, SIGSET_T_SIZE));
  9291. finish_sigsuspend_mask(ret);
  9292. }
  9293. return ret;
  9294. #endif
  9295. case TARGET_NR_rt_sigsuspend:
  9296. {
  9297. sigset_t *set;
  9298. ret = process_sigsuspend_mask(&set, arg1, arg2);
  9299. if (ret != 0) {
  9300. return ret;
  9301. }
  9302. ret = get_errno(safe_rt_sigsuspend(set, SIGSET_T_SIZE));
  9303. finish_sigsuspend_mask(ret);
  9304. }
  9305. return ret;
  9306. #ifdef TARGET_NR_rt_sigtimedwait
  9307. case TARGET_NR_rt_sigtimedwait:
  9308. {
  9309. sigset_t set;
  9310. struct timespec uts, *puts;
  9311. siginfo_t uinfo;
  9312. if (arg4 != sizeof(target_sigset_t)) {
  9313. return -TARGET_EINVAL;
  9314. }
  9315. if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
  9316. return -TARGET_EFAULT;
  9317. target_to_host_sigset(&set, p);
  9318. unlock_user(p, arg1, 0);
  9319. if (arg3) {
  9320. puts = &uts;
  9321. if (target_to_host_timespec(puts, arg3)) {
  9322. return -TARGET_EFAULT;
  9323. }
  9324. } else {
  9325. puts = NULL;
  9326. }
  9327. ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts,
  9328. SIGSET_T_SIZE));
  9329. if (!is_error(ret)) {
  9330. if (arg2) {
  9331. p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t),
  9332. 0);
  9333. if (!p) {
  9334. return -TARGET_EFAULT;
  9335. }
  9336. host_to_target_siginfo(p, &uinfo);
  9337. unlock_user(p, arg2, sizeof(target_siginfo_t));
  9338. }
  9339. ret = host_to_target_signal(ret);
  9340. }
  9341. }
  9342. return ret;
  9343. #endif
  9344. #ifdef TARGET_NR_rt_sigtimedwait_time64
  9345. case TARGET_NR_rt_sigtimedwait_time64:
  9346. {
  9347. sigset_t set;
  9348. struct timespec uts, *puts;
  9349. siginfo_t uinfo;
  9350. if (arg4 != sizeof(target_sigset_t)) {
  9351. return -TARGET_EINVAL;
  9352. }
  9353. p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1);
  9354. if (!p) {
  9355. return -TARGET_EFAULT;
  9356. }
  9357. target_to_host_sigset(&set, p);
  9358. unlock_user(p, arg1, 0);
  9359. if (arg3) {
  9360. puts = &uts;
  9361. if (target_to_host_timespec64(puts, arg3)) {
  9362. return -TARGET_EFAULT;
  9363. }
  9364. } else {
  9365. puts = NULL;
  9366. }
  9367. ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts,
  9368. SIGSET_T_SIZE));
  9369. if (!is_error(ret)) {
  9370. if (arg2) {
  9371. p = lock_user(VERIFY_WRITE, arg2,
  9372. sizeof(target_siginfo_t), 0);
  9373. if (!p) {
  9374. return -TARGET_EFAULT;
  9375. }
  9376. host_to_target_siginfo(p, &uinfo);
  9377. unlock_user(p, arg2, sizeof(target_siginfo_t));
  9378. }
  9379. ret = host_to_target_signal(ret);
  9380. }
  9381. }
  9382. return ret;
  9383. #endif
  9384. case TARGET_NR_rt_sigqueueinfo:
  9385. {
  9386. siginfo_t uinfo;
  9387. p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1);
  9388. if (!p) {
  9389. return -TARGET_EFAULT;
  9390. }
  9391. target_to_host_siginfo(&uinfo, p);
  9392. unlock_user(p, arg3, 0);
  9393. ret = get_errno(sys_rt_sigqueueinfo(arg1, target_to_host_signal(arg2), &uinfo));
  9394. }
  9395. return ret;
  9396. case TARGET_NR_rt_tgsigqueueinfo:
  9397. {
  9398. siginfo_t uinfo;
  9399. p = lock_user(VERIFY_READ, arg4, sizeof(target_siginfo_t), 1);
  9400. if (!p) {
  9401. return -TARGET_EFAULT;
  9402. }
  9403. target_to_host_siginfo(&uinfo, p);
  9404. unlock_user(p, arg4, 0);
  9405. ret = get_errno(sys_rt_tgsigqueueinfo(arg1, arg2, target_to_host_signal(arg3), &uinfo));
  9406. }
  9407. return ret;
  9408. #ifdef TARGET_NR_sigreturn
  9409. case TARGET_NR_sigreturn:
  9410. if (block_signals()) {
  9411. return -QEMU_ERESTARTSYS;
  9412. }
  9413. return do_sigreturn(cpu_env);
  9414. #endif
  9415. case TARGET_NR_rt_sigreturn:
  9416. if (block_signals()) {
  9417. return -QEMU_ERESTARTSYS;
  9418. }
  9419. return do_rt_sigreturn(cpu_env);
  9420. case TARGET_NR_sethostname:
  9421. if (!(p = lock_user_string(arg1)))
  9422. return -TARGET_EFAULT;
  9423. ret = get_errno(sethostname(p, arg2));
  9424. unlock_user(p, arg1, 0);
  9425. return ret;
  9426. #ifdef TARGET_NR_setrlimit
  9427. case TARGET_NR_setrlimit:
  9428. {
  9429. int resource = target_to_host_resource(arg1);
  9430. struct target_rlimit *target_rlim;
  9431. struct rlimit rlim;
  9432. if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
  9433. return -TARGET_EFAULT;
  9434. rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
  9435. rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
  9436. unlock_user_struct(target_rlim, arg2, 0);
  9437. /*
  9438. * If we just passed through resource limit settings for memory then
  9439. * they would also apply to QEMU's own allocations, and QEMU will
  9440. * crash or hang or die if its allocations fail. Ideally we would
  9441. * track the guest allocations in QEMU and apply the limits ourselves.
  9442. * For now, just tell the guest the call succeeded but don't actually
  9443. * limit anything.
  9444. */
  9445. if (resource != RLIMIT_AS &&
  9446. resource != RLIMIT_DATA &&
  9447. resource != RLIMIT_STACK) {
  9448. return get_errno(setrlimit(resource, &rlim));
  9449. } else {
  9450. return 0;
  9451. }
  9452. }
  9453. #endif
  9454. #ifdef TARGET_NR_getrlimit
  9455. case TARGET_NR_getrlimit:
  9456. {
  9457. int resource = target_to_host_resource(arg1);
  9458. struct target_rlimit *target_rlim;
  9459. struct rlimit rlim;
  9460. ret = get_errno(getrlimit(resource, &rlim));
  9461. if (!is_error(ret)) {
  9462. if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
  9463. return -TARGET_EFAULT;
  9464. target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
  9465. target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
  9466. unlock_user_struct(target_rlim, arg2, 1);
  9467. }
  9468. }
  9469. return ret;
  9470. #endif
  9471. case TARGET_NR_getrusage:
  9472. {
  9473. struct rusage rusage;
  9474. ret = get_errno(getrusage(arg1, &rusage));
  9475. if (!is_error(ret)) {
  9476. ret = host_to_target_rusage(arg2, &rusage);
  9477. }
  9478. }
  9479. return ret;
  9480. #if defined(TARGET_NR_gettimeofday)
  9481. case TARGET_NR_gettimeofday:
  9482. {
  9483. struct timeval tv;
  9484. struct timezone tz;
  9485. ret = get_errno(gettimeofday(&tv, &tz));
  9486. if (!is_error(ret)) {
  9487. if (arg1 && copy_to_user_timeval(arg1, &tv)) {
  9488. return -TARGET_EFAULT;
  9489. }
  9490. if (arg2 && copy_to_user_timezone(arg2, &tz)) {
  9491. return -TARGET_EFAULT;
  9492. }
  9493. }
  9494. }
  9495. return ret;
  9496. #endif
  9497. #if defined(TARGET_NR_settimeofday)
  9498. case TARGET_NR_settimeofday:
  9499. {
  9500. struct timeval tv, *ptv = NULL;
  9501. struct timezone tz, *ptz = NULL;
  9502. if (arg1) {
  9503. if (copy_from_user_timeval(&tv, arg1)) {
  9504. return -TARGET_EFAULT;
  9505. }
  9506. ptv = &tv;
  9507. }
  9508. if (arg2) {
  9509. if (copy_from_user_timezone(&tz, arg2)) {
  9510. return -TARGET_EFAULT;
  9511. }
  9512. ptz = &tz;
  9513. }
  9514. return get_errno(settimeofday(ptv, ptz));
  9515. }
  9516. #endif
  9517. #if defined(TARGET_NR_select)
  9518. case TARGET_NR_select:
  9519. #if defined(TARGET_WANT_NI_OLD_SELECT)
  9520. /* some architectures used to have old_select here
  9521. * but now ENOSYS it.
  9522. */
  9523. ret = -TARGET_ENOSYS;
  9524. #elif defined(TARGET_WANT_OLD_SYS_SELECT)
  9525. ret = do_old_select(arg1);
  9526. #else
  9527. ret = do_select(arg1, arg2, arg3, arg4, arg5);
  9528. #endif
  9529. return ret;
  9530. #endif
  9531. #ifdef TARGET_NR_pselect6
  9532. case TARGET_NR_pselect6:
  9533. return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, false);
  9534. #endif
  9535. #ifdef TARGET_NR_pselect6_time64
  9536. case TARGET_NR_pselect6_time64:
  9537. return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, true);
  9538. #endif
  9539. #ifdef TARGET_NR_symlink
  9540. case TARGET_NR_symlink:
  9541. {
  9542. void *p2;
  9543. p = lock_user_string(arg1);
  9544. p2 = lock_user_string(arg2);
  9545. if (!p || !p2)
  9546. ret = -TARGET_EFAULT;
  9547. else
  9548. ret = get_errno(symlink(p, p2));
  9549. unlock_user(p2, arg2, 0);
  9550. unlock_user(p, arg1, 0);
  9551. }
  9552. return ret;
  9553. #endif
  9554. #if defined(TARGET_NR_symlinkat)
  9555. case TARGET_NR_symlinkat:
  9556. {
  9557. void *p2;
  9558. p = lock_user_string(arg1);
  9559. p2 = lock_user_string(arg3);
  9560. if (!p || !p2)
  9561. ret = -TARGET_EFAULT;
  9562. else
  9563. ret = get_errno(symlinkat(p, arg2, p2));
  9564. unlock_user(p2, arg3, 0);
  9565. unlock_user(p, arg1, 0);
  9566. }
  9567. return ret;
  9568. #endif
  9569. #ifdef TARGET_NR_readlink
  9570. case TARGET_NR_readlink:
  9571. {
  9572. void *p2;
  9573. p = lock_user_string(arg1);
  9574. p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  9575. ret = get_errno(do_guest_readlink(p, p2, arg3));
  9576. unlock_user(p2, arg2, ret);
  9577. unlock_user(p, arg1, 0);
  9578. }
  9579. return ret;
  9580. #endif
  9581. #if defined(TARGET_NR_readlinkat)
  9582. case TARGET_NR_readlinkat:
  9583. {
  9584. void *p2;
  9585. p = lock_user_string(arg2);
  9586. p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  9587. if (!p || !p2) {
  9588. ret = -TARGET_EFAULT;
  9589. } else if (!arg4) {
  9590. /* Short circuit this for the magic exe check. */
  9591. ret = -TARGET_EINVAL;
  9592. } else if (is_proc_myself((const char *)p, "exe")) {
  9593. /*
  9594. * Don't worry about sign mismatch as earlier mapping
  9595. * logic would have thrown a bad address error.
  9596. */
  9597. ret = MIN(strlen(exec_path), arg4);
  9598. /* We cannot NUL terminate the string. */
  9599. memcpy(p2, exec_path, ret);
  9600. } else {
  9601. ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
  9602. }
  9603. unlock_user(p2, arg3, ret);
  9604. unlock_user(p, arg2, 0);
  9605. }
  9606. return ret;
  9607. #endif
  9608. #ifdef TARGET_NR_swapon
  9609. case TARGET_NR_swapon:
  9610. if (!(p = lock_user_string(arg1)))
  9611. return -TARGET_EFAULT;
  9612. ret = get_errno(swapon(p, arg2));
  9613. unlock_user(p, arg1, 0);
  9614. return ret;
  9615. #endif
  9616. case TARGET_NR_reboot:
  9617. if (arg3 == LINUX_REBOOT_CMD_RESTART2) {
  9618. /* arg4 must be ignored in all other cases */
  9619. p = lock_user_string(arg4);
  9620. if (!p) {
  9621. return -TARGET_EFAULT;
  9622. }
  9623. ret = get_errno(reboot(arg1, arg2, arg3, p));
  9624. unlock_user(p, arg4, 0);
  9625. } else {
  9626. ret = get_errno(reboot(arg1, arg2, arg3, NULL));
  9627. }
  9628. return ret;
  9629. #ifdef TARGET_NR_mmap
  9630. case TARGET_NR_mmap:
  9631. #ifdef TARGET_ARCH_WANT_SYS_OLD_MMAP
  9632. {
  9633. abi_ulong *v;
  9634. abi_ulong v1, v2, v3, v4, v5, v6;
  9635. if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
  9636. return -TARGET_EFAULT;
  9637. v1 = tswapal(v[0]);
  9638. v2 = tswapal(v[1]);
  9639. v3 = tswapal(v[2]);
  9640. v4 = tswapal(v[3]);
  9641. v5 = tswapal(v[4]);
  9642. v6 = tswapal(v[5]);
  9643. unlock_user(v, arg1, 0);
  9644. return do_mmap(v1, v2, v3, v4, v5, v6);
  9645. }
  9646. #else
  9647. /* mmap pointers are always untagged */
  9648. return do_mmap(arg1, arg2, arg3, arg4, arg5, arg6);
  9649. #endif
  9650. #endif
  9651. #ifdef TARGET_NR_mmap2
  9652. case TARGET_NR_mmap2:
  9653. #ifndef MMAP_SHIFT
  9654. #define MMAP_SHIFT 12
  9655. #endif
  9656. return do_mmap(arg1, arg2, arg3, arg4, arg5,
  9657. (off_t)(abi_ulong)arg6 << MMAP_SHIFT);
  9658. #endif
  9659. case TARGET_NR_munmap:
  9660. arg1 = cpu_untagged_addr(cpu, arg1);
  9661. return get_errno(target_munmap(arg1, arg2));
  9662. case TARGET_NR_mprotect:
  9663. arg1 = cpu_untagged_addr(cpu, arg1);
  9664. {
  9665. TaskState *ts = get_task_state(cpu);
  9666. /* Special hack to detect libc making the stack executable. */
  9667. if ((arg3 & PROT_GROWSDOWN)
  9668. && arg1 >= ts->info->stack_limit
  9669. && arg1 <= ts->info->start_stack) {
  9670. arg3 &= ~PROT_GROWSDOWN;
  9671. arg2 = arg2 + arg1 - ts->info->stack_limit;
  9672. arg1 = ts->info->stack_limit;
  9673. }
  9674. }
  9675. return get_errno(target_mprotect(arg1, arg2, arg3));
  9676. #ifdef TARGET_NR_mremap
  9677. case TARGET_NR_mremap:
  9678. arg1 = cpu_untagged_addr(cpu, arg1);
  9679. /* mremap new_addr (arg5) is always untagged */
  9680. return get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
  9681. #endif
  9682. /* ??? msync/mlock/munlock are broken for softmmu. */
  9683. #ifdef TARGET_NR_msync
  9684. case TARGET_NR_msync:
  9685. return get_errno(msync(g2h(cpu, arg1), arg2,
  9686. target_to_host_msync_arg(arg3)));
  9687. #endif
  9688. #ifdef TARGET_NR_mlock
  9689. case TARGET_NR_mlock:
  9690. return get_errno(mlock(g2h(cpu, arg1), arg2));
  9691. #endif
  9692. #ifdef TARGET_NR_munlock
  9693. case TARGET_NR_munlock:
  9694. return get_errno(munlock(g2h(cpu, arg1), arg2));
  9695. #endif
  9696. #ifdef TARGET_NR_mlockall
  9697. case TARGET_NR_mlockall:
  9698. return get_errno(mlockall(target_to_host_mlockall_arg(arg1)));
  9699. #endif
  9700. #ifdef TARGET_NR_munlockall
  9701. case TARGET_NR_munlockall:
  9702. return get_errno(munlockall());
  9703. #endif
  9704. #ifdef TARGET_NR_truncate
  9705. case TARGET_NR_truncate:
  9706. if (!(p = lock_user_string(arg1)))
  9707. return -TARGET_EFAULT;
  9708. ret = get_errno(truncate(p, arg2));
  9709. unlock_user(p, arg1, 0);
  9710. return ret;
  9711. #endif
  9712. #ifdef TARGET_NR_ftruncate
  9713. case TARGET_NR_ftruncate:
  9714. return get_errno(ftruncate(arg1, arg2));
  9715. #endif
  9716. case TARGET_NR_fchmod:
  9717. return get_errno(fchmod(arg1, arg2));
  9718. #if defined(TARGET_NR_fchmodat)
  9719. case TARGET_NR_fchmodat:
  9720. if (!(p = lock_user_string(arg2)))
  9721. return -TARGET_EFAULT;
  9722. ret = get_errno(fchmodat(arg1, p, arg3, 0));
  9723. unlock_user(p, arg2, 0);
  9724. return ret;
  9725. #endif
  9726. case TARGET_NR_getpriority:
  9727. /* Note that negative values are valid for getpriority, so we must
  9728. differentiate based on errno settings. */
  9729. errno = 0;
  9730. ret = getpriority(arg1, arg2);
  9731. if (ret == -1 && errno != 0) {
  9732. return -host_to_target_errno(errno);
  9733. }
  9734. #ifdef TARGET_ALPHA
  9735. /* Return value is the unbiased priority. Signal no error. */
  9736. cpu_env->ir[IR_V0] = 0;
  9737. #else
  9738. /* Return value is a biased priority to avoid negative numbers. */
  9739. ret = 20 - ret;
  9740. #endif
  9741. return ret;
  9742. case TARGET_NR_setpriority:
  9743. return get_errno(setpriority(arg1, arg2, arg3));
  9744. #ifdef TARGET_NR_statfs
  9745. case TARGET_NR_statfs:
  9746. if (!(p = lock_user_string(arg1))) {
  9747. return -TARGET_EFAULT;
  9748. }
  9749. ret = get_errno(statfs(path(p), &stfs));
  9750. unlock_user(p, arg1, 0);
  9751. convert_statfs:
  9752. if (!is_error(ret)) {
  9753. struct target_statfs *target_stfs;
  9754. if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
  9755. return -TARGET_EFAULT;
  9756. __put_user(stfs.f_type, &target_stfs->f_type);
  9757. __put_user(stfs.f_bsize, &target_stfs->f_bsize);
  9758. __put_user(stfs.f_blocks, &target_stfs->f_blocks);
  9759. __put_user(stfs.f_bfree, &target_stfs->f_bfree);
  9760. __put_user(stfs.f_bavail, &target_stfs->f_bavail);
  9761. __put_user(stfs.f_files, &target_stfs->f_files);
  9762. __put_user(stfs.f_ffree, &target_stfs->f_ffree);
  9763. __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
  9764. __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
  9765. __put_user(stfs.f_namelen, &target_stfs->f_namelen);
  9766. __put_user(stfs.f_frsize, &target_stfs->f_frsize);
  9767. #ifdef _STATFS_F_FLAGS
  9768. __put_user(stfs.f_flags, &target_stfs->f_flags);
  9769. #else
  9770. __put_user(0, &target_stfs->f_flags);
  9771. #endif
  9772. memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
  9773. unlock_user_struct(target_stfs, arg2, 1);
  9774. }
  9775. return ret;
  9776. #endif
  9777. #ifdef TARGET_NR_fstatfs
  9778. case TARGET_NR_fstatfs:
  9779. ret = get_errno(fstatfs(arg1, &stfs));
  9780. goto convert_statfs;
  9781. #endif
  9782. #ifdef TARGET_NR_statfs64
  9783. case TARGET_NR_statfs64:
  9784. if (!(p = lock_user_string(arg1))) {
  9785. return -TARGET_EFAULT;
  9786. }
  9787. ret = get_errno(statfs(path(p), &stfs));
  9788. unlock_user(p, arg1, 0);
  9789. convert_statfs64:
  9790. if (!is_error(ret)) {
  9791. struct target_statfs64 *target_stfs;
  9792. if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
  9793. return -TARGET_EFAULT;
  9794. __put_user(stfs.f_type, &target_stfs->f_type);
  9795. __put_user(stfs.f_bsize, &target_stfs->f_bsize);
  9796. __put_user(stfs.f_blocks, &target_stfs->f_blocks);
  9797. __put_user(stfs.f_bfree, &target_stfs->f_bfree);
  9798. __put_user(stfs.f_bavail, &target_stfs->f_bavail);
  9799. __put_user(stfs.f_files, &target_stfs->f_files);
  9800. __put_user(stfs.f_ffree, &target_stfs->f_ffree);
  9801. __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
  9802. __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
  9803. __put_user(stfs.f_namelen, &target_stfs->f_namelen);
  9804. __put_user(stfs.f_frsize, &target_stfs->f_frsize);
  9805. #ifdef _STATFS_F_FLAGS
  9806. __put_user(stfs.f_flags, &target_stfs->f_flags);
  9807. #else
  9808. __put_user(0, &target_stfs->f_flags);
  9809. #endif
  9810. memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
  9811. unlock_user_struct(target_stfs, arg3, 1);
  9812. }
  9813. return ret;
  9814. case TARGET_NR_fstatfs64:
  9815. ret = get_errno(fstatfs(arg1, &stfs));
  9816. goto convert_statfs64;
  9817. #endif
  9818. #ifdef TARGET_NR_socketcall
  9819. case TARGET_NR_socketcall:
  9820. return do_socketcall(arg1, arg2);
  9821. #endif
  9822. #ifdef TARGET_NR_accept
  9823. case TARGET_NR_accept:
  9824. return do_accept4(arg1, arg2, arg3, 0);
  9825. #endif
  9826. #ifdef TARGET_NR_accept4
  9827. case TARGET_NR_accept4:
  9828. return do_accept4(arg1, arg2, arg3, arg4);
  9829. #endif
  9830. #ifdef TARGET_NR_bind
  9831. case TARGET_NR_bind:
  9832. return do_bind(arg1, arg2, arg3);
  9833. #endif
  9834. #ifdef TARGET_NR_connect
  9835. case TARGET_NR_connect:
  9836. return do_connect(arg1, arg2, arg3);
  9837. #endif
  9838. #ifdef TARGET_NR_getpeername
  9839. case TARGET_NR_getpeername:
  9840. return do_getpeername(arg1, arg2, arg3);
  9841. #endif
  9842. #ifdef TARGET_NR_getsockname
  9843. case TARGET_NR_getsockname:
  9844. return do_getsockname(arg1, arg2, arg3);
  9845. #endif
  9846. #ifdef TARGET_NR_getsockopt
  9847. case TARGET_NR_getsockopt:
  9848. return do_getsockopt(arg1, arg2, arg3, arg4, arg5);
  9849. #endif
  9850. #ifdef TARGET_NR_listen
  9851. case TARGET_NR_listen:
  9852. return get_errno(listen(arg1, arg2));
  9853. #endif
  9854. #ifdef TARGET_NR_recv
  9855. case TARGET_NR_recv:
  9856. return do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
  9857. #endif
  9858. #ifdef TARGET_NR_recvfrom
  9859. case TARGET_NR_recvfrom:
  9860. return do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
  9861. #endif
  9862. #ifdef TARGET_NR_recvmsg
  9863. case TARGET_NR_recvmsg:
  9864. return do_sendrecvmsg(arg1, arg2, arg3, 0);
  9865. #endif
  9866. #ifdef TARGET_NR_send
  9867. case TARGET_NR_send:
  9868. return do_sendto(arg1, arg2, arg3, arg4, 0, 0);
  9869. #endif
  9870. #ifdef TARGET_NR_sendmsg
  9871. case TARGET_NR_sendmsg:
  9872. return do_sendrecvmsg(arg1, arg2, arg3, 1);
  9873. #endif
  9874. #ifdef TARGET_NR_sendmmsg
  9875. case TARGET_NR_sendmmsg:
  9876. return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 1);
  9877. #endif
  9878. #ifdef TARGET_NR_recvmmsg
  9879. case TARGET_NR_recvmmsg:
  9880. return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 0);
  9881. #endif
  9882. #ifdef TARGET_NR_sendto
  9883. case TARGET_NR_sendto:
  9884. return do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
  9885. #endif
  9886. #ifdef TARGET_NR_shutdown
  9887. case TARGET_NR_shutdown:
  9888. return get_errno(shutdown(arg1, arg2));
  9889. #endif
  9890. #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
  9891. case TARGET_NR_getrandom:
  9892. p = lock_user(VERIFY_WRITE, arg1, arg2, 0);
  9893. if (!p) {
  9894. return -TARGET_EFAULT;
  9895. }
  9896. ret = get_errno(getrandom(p, arg2, arg3));
  9897. unlock_user(p, arg1, ret);
  9898. return ret;
  9899. #endif
  9900. #ifdef TARGET_NR_socket
  9901. case TARGET_NR_socket:
  9902. return do_socket(arg1, arg2, arg3);
  9903. #endif
  9904. #ifdef TARGET_NR_socketpair
  9905. case TARGET_NR_socketpair:
  9906. return do_socketpair(arg1, arg2, arg3, arg4);
  9907. #endif
  9908. #ifdef TARGET_NR_setsockopt
  9909. case TARGET_NR_setsockopt:
  9910. return do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
  9911. #endif
  9912. #if defined(TARGET_NR_syslog)
  9913. case TARGET_NR_syslog:
  9914. {
  9915. int len = arg2;
  9916. switch (arg1) {
  9917. case TARGET_SYSLOG_ACTION_CLOSE: /* Close log */
  9918. case TARGET_SYSLOG_ACTION_OPEN: /* Open log */
  9919. case TARGET_SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
  9920. case TARGET_SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging */
  9921. case TARGET_SYSLOG_ACTION_CONSOLE_ON: /* Enable logging */
  9922. case TARGET_SYSLOG_ACTION_CONSOLE_LEVEL: /* Set messages level */
  9923. case TARGET_SYSLOG_ACTION_SIZE_UNREAD: /* Number of chars */
  9924. case TARGET_SYSLOG_ACTION_SIZE_BUFFER: /* Size of the buffer */
  9925. return get_errno(sys_syslog((int)arg1, NULL, (int)arg3));
  9926. case TARGET_SYSLOG_ACTION_READ: /* Read from log */
  9927. case TARGET_SYSLOG_ACTION_READ_CLEAR: /* Read/clear msgs */
  9928. case TARGET_SYSLOG_ACTION_READ_ALL: /* Read last messages */
  9929. {
  9930. if (len < 0) {
  9931. return -TARGET_EINVAL;
  9932. }
  9933. if (len == 0) {
  9934. return 0;
  9935. }
  9936. p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  9937. if (!p) {
  9938. return -TARGET_EFAULT;
  9939. }
  9940. ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
  9941. unlock_user(p, arg2, arg3);
  9942. }
  9943. return ret;
  9944. default:
  9945. return -TARGET_EINVAL;
  9946. }
  9947. }
  9948. break;
  9949. #endif
  9950. case TARGET_NR_setitimer:
  9951. {
  9952. struct itimerval value, ovalue, *pvalue;
  9953. if (arg2) {
  9954. pvalue = &value;
  9955. if (copy_from_user_timeval(&pvalue->it_interval, arg2)
  9956. || copy_from_user_timeval(&pvalue->it_value,
  9957. arg2 + sizeof(struct target_timeval)))
  9958. return -TARGET_EFAULT;
  9959. } else {
  9960. pvalue = NULL;
  9961. }
  9962. ret = get_errno(setitimer(arg1, pvalue, &ovalue));
  9963. if (!is_error(ret) && arg3) {
  9964. if (copy_to_user_timeval(arg3,
  9965. &ovalue.it_interval)
  9966. || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
  9967. &ovalue.it_value))
  9968. return -TARGET_EFAULT;
  9969. }
  9970. }
  9971. return ret;
  9972. case TARGET_NR_getitimer:
  9973. {
  9974. struct itimerval value;
  9975. ret = get_errno(getitimer(arg1, &value));
  9976. if (!is_error(ret) && arg2) {
  9977. if (copy_to_user_timeval(arg2,
  9978. &value.it_interval)
  9979. || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
  9980. &value.it_value))
  9981. return -TARGET_EFAULT;
  9982. }
  9983. }
  9984. return ret;
  9985. #ifdef TARGET_NR_stat
  9986. case TARGET_NR_stat:
  9987. if (!(p = lock_user_string(arg1))) {
  9988. return -TARGET_EFAULT;
  9989. }
  9990. ret = get_errno(stat(path(p), &st));
  9991. unlock_user(p, arg1, 0);
  9992. goto do_stat;
  9993. #endif
  9994. #ifdef TARGET_NR_lstat
  9995. case TARGET_NR_lstat:
  9996. if (!(p = lock_user_string(arg1))) {
  9997. return -TARGET_EFAULT;
  9998. }
  9999. ret = get_errno(lstat(path(p), &st));
  10000. unlock_user(p, arg1, 0);
  10001. goto do_stat;
  10002. #endif
  10003. #ifdef TARGET_NR_fstat
  10004. case TARGET_NR_fstat:
  10005. {
  10006. ret = get_errno(fstat(arg1, &st));
  10007. #if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat)
  10008. do_stat:
  10009. #endif
  10010. if (!is_error(ret)) {
  10011. struct target_stat *target_st;
  10012. if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
  10013. return -TARGET_EFAULT;
  10014. memset(target_st, 0, sizeof(*target_st));
  10015. __put_user(st.st_dev, &target_st->st_dev);
  10016. __put_user(st.st_ino, &target_st->st_ino);
  10017. __put_user(st.st_mode, &target_st->st_mode);
  10018. __put_user(st.st_uid, &target_st->st_uid);
  10019. __put_user(st.st_gid, &target_st->st_gid);
  10020. __put_user(st.st_nlink, &target_st->st_nlink);
  10021. __put_user(st.st_rdev, &target_st->st_rdev);
  10022. __put_user(st.st_size, &target_st->st_size);
  10023. __put_user(st.st_blksize, &target_st->st_blksize);
  10024. __put_user(st.st_blocks, &target_st->st_blocks);
  10025. __put_user(st.st_atime, &target_st->target_st_atime);
  10026. __put_user(st.st_mtime, &target_st->target_st_mtime);
  10027. __put_user(st.st_ctime, &target_st->target_st_ctime);
  10028. #if defined(HAVE_STRUCT_STAT_ST_ATIM) && defined(TARGET_STAT_HAVE_NSEC)
  10029. __put_user(st.st_atim.tv_nsec,
  10030. &target_st->target_st_atime_nsec);
  10031. __put_user(st.st_mtim.tv_nsec,
  10032. &target_st->target_st_mtime_nsec);
  10033. __put_user(st.st_ctim.tv_nsec,
  10034. &target_st->target_st_ctime_nsec);
  10035. #endif
  10036. unlock_user_struct(target_st, arg2, 1);
  10037. }
  10038. }
  10039. return ret;
  10040. #endif
  10041. case TARGET_NR_vhangup:
  10042. return get_errno(vhangup());
  10043. #ifdef TARGET_NR_syscall
  10044. case TARGET_NR_syscall:
  10045. return do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
  10046. arg6, arg7, arg8, 0);
  10047. #endif
  10048. #if defined(TARGET_NR_wait4)
  10049. case TARGET_NR_wait4:
  10050. {
  10051. int status;
  10052. abi_long status_ptr = arg2;
  10053. struct rusage rusage, *rusage_ptr;
  10054. abi_ulong target_rusage = arg4;
  10055. abi_long rusage_err;
  10056. if (target_rusage)
  10057. rusage_ptr = &rusage;
  10058. else
  10059. rusage_ptr = NULL;
  10060. ret = get_errno(safe_wait4(arg1, &status, arg3, rusage_ptr));
  10061. if (!is_error(ret)) {
  10062. if (status_ptr && ret) {
  10063. status = host_to_target_waitstatus(status);
  10064. if (put_user_s32(status, status_ptr))
  10065. return -TARGET_EFAULT;
  10066. }
  10067. if (target_rusage) {
  10068. rusage_err = host_to_target_rusage(target_rusage, &rusage);
  10069. if (rusage_err) {
  10070. ret = rusage_err;
  10071. }
  10072. }
  10073. }
  10074. }
  10075. return ret;
  10076. #endif
  10077. #ifdef TARGET_NR_swapoff
  10078. case TARGET_NR_swapoff:
  10079. if (!(p = lock_user_string(arg1)))
  10080. return -TARGET_EFAULT;
  10081. ret = get_errno(swapoff(p));
  10082. unlock_user(p, arg1, 0);
  10083. return ret;
  10084. #endif
  10085. case TARGET_NR_sysinfo:
  10086. {
  10087. struct target_sysinfo *target_value;
  10088. struct sysinfo value;
  10089. ret = get_errno(sysinfo(&value));
  10090. if (!is_error(ret) && arg1)
  10091. {
  10092. if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
  10093. return -TARGET_EFAULT;
  10094. __put_user(value.uptime, &target_value->uptime);
  10095. __put_user(value.loads[0], &target_value->loads[0]);
  10096. __put_user(value.loads[1], &target_value->loads[1]);
  10097. __put_user(value.loads[2], &target_value->loads[2]);
  10098. __put_user(value.totalram, &target_value->totalram);
  10099. __put_user(value.freeram, &target_value->freeram);
  10100. __put_user(value.sharedram, &target_value->sharedram);
  10101. __put_user(value.bufferram, &target_value->bufferram);
  10102. __put_user(value.totalswap, &target_value->totalswap);
  10103. __put_user(value.freeswap, &target_value->freeswap);
  10104. __put_user(value.procs, &target_value->procs);
  10105. __put_user(value.totalhigh, &target_value->totalhigh);
  10106. __put_user(value.freehigh, &target_value->freehigh);
  10107. __put_user(value.mem_unit, &target_value->mem_unit);
  10108. unlock_user_struct(target_value, arg1, 1);
  10109. }
  10110. }
  10111. return ret;
  10112. #ifdef TARGET_NR_ipc
  10113. case TARGET_NR_ipc:
  10114. return do_ipc(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6);
  10115. #endif
  10116. #ifdef TARGET_NR_semget
  10117. case TARGET_NR_semget:
  10118. return get_errno(semget(arg1, arg2, arg3));
  10119. #endif
  10120. #ifdef TARGET_NR_semop
  10121. case TARGET_NR_semop:
  10122. return do_semtimedop(arg1, arg2, arg3, 0, false);
  10123. #endif
  10124. #ifdef TARGET_NR_semtimedop
  10125. case TARGET_NR_semtimedop:
  10126. return do_semtimedop(arg1, arg2, arg3, arg4, false);
  10127. #endif
  10128. #ifdef TARGET_NR_semtimedop_time64
  10129. case TARGET_NR_semtimedop_time64:
  10130. return do_semtimedop(arg1, arg2, arg3, arg4, true);
  10131. #endif
  10132. #ifdef TARGET_NR_semctl
  10133. case TARGET_NR_semctl:
  10134. return do_semctl(arg1, arg2, arg3, arg4);
  10135. #endif
  10136. #ifdef TARGET_NR_msgctl
  10137. case TARGET_NR_msgctl:
  10138. return do_msgctl(arg1, arg2, arg3);
  10139. #endif
  10140. #ifdef TARGET_NR_msgget
  10141. case TARGET_NR_msgget:
  10142. return get_errno(msgget(arg1, arg2));
  10143. #endif
  10144. #ifdef TARGET_NR_msgrcv
  10145. case TARGET_NR_msgrcv:
  10146. return do_msgrcv(arg1, arg2, arg3, arg4, arg5);
  10147. #endif
  10148. #ifdef TARGET_NR_msgsnd
  10149. case TARGET_NR_msgsnd:
  10150. return do_msgsnd(arg1, arg2, arg3, arg4);
  10151. #endif
  10152. #ifdef TARGET_NR_shmget
  10153. case TARGET_NR_shmget:
  10154. return get_errno(shmget(arg1, arg2, arg3));
  10155. #endif
  10156. #ifdef TARGET_NR_shmctl
  10157. case TARGET_NR_shmctl:
  10158. return do_shmctl(arg1, arg2, arg3);
  10159. #endif
  10160. #ifdef TARGET_NR_shmat
  10161. case TARGET_NR_shmat:
  10162. return target_shmat(cpu_env, arg1, arg2, arg3);
  10163. #endif
  10164. #ifdef TARGET_NR_shmdt
  10165. case TARGET_NR_shmdt:
  10166. return target_shmdt(arg1);
  10167. #endif
  10168. case TARGET_NR_fsync:
  10169. return get_errno(fsync(arg1));
  10170. case TARGET_NR_clone:
  10171. /* Linux manages to have three different orderings for its
  10172. * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
  10173. * match the kernel's CONFIG_CLONE_* settings.
  10174. * Microblaze is further special in that it uses a sixth
  10175. * implicit argument to clone for the TLS pointer.
  10176. */
  10177. #if defined(TARGET_MICROBLAZE)
  10178. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5));
  10179. #elif defined(TARGET_CLONE_BACKWARDS)
  10180. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
  10181. #elif defined(TARGET_CLONE_BACKWARDS2)
  10182. ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
  10183. #else
  10184. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
  10185. #endif
  10186. return ret;
  10187. #ifdef __NR_exit_group
  10188. /* new thread calls */
  10189. case TARGET_NR_exit_group:
  10190. preexit_cleanup(cpu_env, arg1);
  10191. return get_errno(exit_group(arg1));
  10192. #endif
  10193. case TARGET_NR_setdomainname:
  10194. if (!(p = lock_user_string(arg1)))
  10195. return -TARGET_EFAULT;
  10196. ret = get_errno(setdomainname(p, arg2));
  10197. unlock_user(p, arg1, 0);
  10198. return ret;
  10199. case TARGET_NR_uname:
  10200. /* no need to transcode because we use the linux syscall */
  10201. {
  10202. struct new_utsname * buf;
  10203. if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
  10204. return -TARGET_EFAULT;
  10205. ret = get_errno(sys_uname(buf));
  10206. if (!is_error(ret)) {
  10207. /* Overwrite the native machine name with whatever is being
  10208. emulated. */
  10209. g_strlcpy(buf->machine, cpu_to_uname_machine(cpu_env),
  10210. sizeof(buf->machine));
  10211. /* Allow the user to override the reported release. */
  10212. if (qemu_uname_release && *qemu_uname_release) {
  10213. g_strlcpy(buf->release, qemu_uname_release,
  10214. sizeof(buf->release));
  10215. }
  10216. }
  10217. unlock_user_struct(buf, arg1, 1);
  10218. }
  10219. return ret;
  10220. #ifdef TARGET_I386
  10221. case TARGET_NR_modify_ldt:
  10222. return do_modify_ldt(cpu_env, arg1, arg2, arg3);
  10223. #if !defined(TARGET_X86_64)
  10224. case TARGET_NR_vm86:
  10225. return do_vm86(cpu_env, arg1, arg2);
  10226. #endif
  10227. #endif
  10228. #if defined(TARGET_NR_adjtimex)
  10229. case TARGET_NR_adjtimex:
  10230. {
  10231. struct timex host_buf;
  10232. if (target_to_host_timex(&host_buf, arg1) != 0) {
  10233. return -TARGET_EFAULT;
  10234. }
  10235. ret = get_errno(adjtimex(&host_buf));
  10236. if (!is_error(ret)) {
  10237. if (host_to_target_timex(arg1, &host_buf) != 0) {
  10238. return -TARGET_EFAULT;
  10239. }
  10240. }
  10241. }
  10242. return ret;
  10243. #endif
  10244. #if defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME)
  10245. case TARGET_NR_clock_adjtime:
  10246. {
  10247. struct timex htx;
  10248. if (target_to_host_timex(&htx, arg2) != 0) {
  10249. return -TARGET_EFAULT;
  10250. }
  10251. ret = get_errno(clock_adjtime(arg1, &htx));
  10252. if (!is_error(ret) && host_to_target_timex(arg2, &htx)) {
  10253. return -TARGET_EFAULT;
  10254. }
  10255. }
  10256. return ret;
  10257. #endif
  10258. #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
  10259. case TARGET_NR_clock_adjtime64:
  10260. {
  10261. struct timex htx;
  10262. if (target_to_host_timex64(&htx, arg2) != 0) {
  10263. return -TARGET_EFAULT;
  10264. }
  10265. ret = get_errno(clock_adjtime(arg1, &htx));
  10266. if (!is_error(ret) && host_to_target_timex64(arg2, &htx)) {
  10267. return -TARGET_EFAULT;
  10268. }
  10269. }
  10270. return ret;
  10271. #endif
  10272. case TARGET_NR_getpgid:
  10273. return get_errno(getpgid(arg1));
  10274. case TARGET_NR_fchdir:
  10275. return get_errno(fchdir(arg1));
  10276. case TARGET_NR_personality:
  10277. return get_errno(personality(arg1));
  10278. #ifdef TARGET_NR__llseek /* Not on alpha */
  10279. case TARGET_NR__llseek:
  10280. {
  10281. int64_t res;
  10282. #if !defined(__NR_llseek)
  10283. res = lseek(arg1, ((uint64_t)arg2 << 32) | (abi_ulong)arg3, arg5);
  10284. if (res == -1) {
  10285. ret = get_errno(res);
  10286. } else {
  10287. ret = 0;
  10288. }
  10289. #else
  10290. ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
  10291. #endif
  10292. if ((ret == 0) && put_user_s64(res, arg4)) {
  10293. return -TARGET_EFAULT;
  10294. }
  10295. }
  10296. return ret;
  10297. #endif
  10298. #ifdef TARGET_NR_getdents
  10299. case TARGET_NR_getdents:
  10300. return do_getdents(arg1, arg2, arg3);
  10301. #endif /* TARGET_NR_getdents */
  10302. #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
  10303. case TARGET_NR_getdents64:
  10304. return do_getdents64(arg1, arg2, arg3);
  10305. #endif /* TARGET_NR_getdents64 */
  10306. #if defined(TARGET_NR__newselect)
  10307. case TARGET_NR__newselect:
  10308. return do_select(arg1, arg2, arg3, arg4, arg5);
  10309. #endif
  10310. #ifdef TARGET_NR_poll
  10311. case TARGET_NR_poll:
  10312. return do_ppoll(arg1, arg2, arg3, arg4, arg5, false, false);
  10313. #endif
  10314. #ifdef TARGET_NR_ppoll
  10315. case TARGET_NR_ppoll:
  10316. return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, false);
  10317. #endif
  10318. #ifdef TARGET_NR_ppoll_time64
  10319. case TARGET_NR_ppoll_time64:
  10320. return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, true);
  10321. #endif
  10322. case TARGET_NR_flock:
  10323. /* NOTE: the flock constant seems to be the same for every
  10324. Linux platform */
  10325. return get_errno(safe_flock(arg1, arg2));
  10326. case TARGET_NR_readv:
  10327. {
  10328. struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
  10329. if (vec != NULL) {
  10330. ret = get_errno(safe_readv(arg1, vec, arg3));
  10331. unlock_iovec(vec, arg2, arg3, 1);
  10332. } else {
  10333. ret = -host_to_target_errno(errno);
  10334. }
  10335. }
  10336. return ret;
  10337. case TARGET_NR_writev:
  10338. {
  10339. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  10340. if (vec != NULL) {
  10341. ret = get_errno(safe_writev(arg1, vec, arg3));
  10342. unlock_iovec(vec, arg2, arg3, 0);
  10343. } else {
  10344. ret = -host_to_target_errno(errno);
  10345. }
  10346. }
  10347. return ret;
  10348. #if defined(TARGET_NR_preadv)
  10349. case TARGET_NR_preadv:
  10350. {
  10351. struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
  10352. if (vec != NULL) {
  10353. unsigned long low, high;
  10354. target_to_host_low_high(arg4, arg5, &low, &high);
  10355. ret = get_errno(safe_preadv(arg1, vec, arg3, low, high));
  10356. unlock_iovec(vec, arg2, arg3, 1);
  10357. } else {
  10358. ret = -host_to_target_errno(errno);
  10359. }
  10360. }
  10361. return ret;
  10362. #endif
  10363. #if defined(TARGET_NR_pwritev)
  10364. case TARGET_NR_pwritev:
  10365. {
  10366. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  10367. if (vec != NULL) {
  10368. unsigned long low, high;
  10369. target_to_host_low_high(arg4, arg5, &low, &high);
  10370. ret = get_errno(safe_pwritev(arg1, vec, arg3, low, high));
  10371. unlock_iovec(vec, arg2, arg3, 0);
  10372. } else {
  10373. ret = -host_to_target_errno(errno);
  10374. }
  10375. }
  10376. return ret;
  10377. #endif
  10378. case TARGET_NR_getsid:
  10379. return get_errno(getsid(arg1));
  10380. #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
  10381. case TARGET_NR_fdatasync:
  10382. return get_errno(fdatasync(arg1));
  10383. #endif
  10384. case TARGET_NR_sched_getaffinity:
  10385. {
  10386. unsigned int mask_size;
  10387. unsigned long *mask;
  10388. /*
  10389. * sched_getaffinity needs multiples of ulong, so need to take
  10390. * care of mismatches between target ulong and host ulong sizes.
  10391. */
  10392. if (arg2 & (sizeof(abi_ulong) - 1)) {
  10393. return -TARGET_EINVAL;
  10394. }
  10395. mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
  10396. mask = alloca(mask_size);
  10397. memset(mask, 0, mask_size);
  10398. ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
  10399. if (!is_error(ret)) {
  10400. if (ret > arg2) {
  10401. /* More data returned than the caller's buffer will fit.
  10402. * This only happens if sizeof(abi_long) < sizeof(long)
  10403. * and the caller passed us a buffer holding an odd number
  10404. * of abi_longs. If the host kernel is actually using the
  10405. * extra 4 bytes then fail EINVAL; otherwise we can just
  10406. * ignore them and only copy the interesting part.
  10407. */
  10408. int numcpus = sysconf(_SC_NPROCESSORS_CONF);
  10409. if (numcpus > arg2 * 8) {
  10410. return -TARGET_EINVAL;
  10411. }
  10412. ret = arg2;
  10413. }
  10414. if (host_to_target_cpu_mask(mask, mask_size, arg3, ret)) {
  10415. return -TARGET_EFAULT;
  10416. }
  10417. }
  10418. }
  10419. return ret;
  10420. case TARGET_NR_sched_setaffinity:
  10421. {
  10422. unsigned int mask_size;
  10423. unsigned long *mask;
  10424. /*
  10425. * sched_setaffinity needs multiples of ulong, so need to take
  10426. * care of mismatches between target ulong and host ulong sizes.
  10427. */
  10428. if (arg2 & (sizeof(abi_ulong) - 1)) {
  10429. return -TARGET_EINVAL;
  10430. }
  10431. mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
  10432. mask = alloca(mask_size);
  10433. ret = target_to_host_cpu_mask(mask, mask_size, arg3, arg2);
  10434. if (ret) {
  10435. return ret;
  10436. }
  10437. return get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
  10438. }
  10439. case TARGET_NR_getcpu:
  10440. {
  10441. unsigned cpuid, node;
  10442. ret = get_errno(sys_getcpu(arg1 ? &cpuid : NULL,
  10443. arg2 ? &node : NULL,
  10444. NULL));
  10445. if (is_error(ret)) {
  10446. return ret;
  10447. }
  10448. if (arg1 && put_user_u32(cpuid, arg1)) {
  10449. return -TARGET_EFAULT;
  10450. }
  10451. if (arg2 && put_user_u32(node, arg2)) {
  10452. return -TARGET_EFAULT;
  10453. }
  10454. }
  10455. return ret;
  10456. case TARGET_NR_sched_setparam:
  10457. {
  10458. struct target_sched_param *target_schp;
  10459. struct sched_param schp;
  10460. if (arg2 == 0) {
  10461. return -TARGET_EINVAL;
  10462. }
  10463. if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1)) {
  10464. return -TARGET_EFAULT;
  10465. }
  10466. schp.sched_priority = tswap32(target_schp->sched_priority);
  10467. unlock_user_struct(target_schp, arg2, 0);
  10468. return get_errno(sys_sched_setparam(arg1, &schp));
  10469. }
  10470. case TARGET_NR_sched_getparam:
  10471. {
  10472. struct target_sched_param *target_schp;
  10473. struct sched_param schp;
  10474. if (arg2 == 0) {
  10475. return -TARGET_EINVAL;
  10476. }
  10477. ret = get_errno(sys_sched_getparam(arg1, &schp));
  10478. if (!is_error(ret)) {
  10479. if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0)) {
  10480. return -TARGET_EFAULT;
  10481. }
  10482. target_schp->sched_priority = tswap32(schp.sched_priority);
  10483. unlock_user_struct(target_schp, arg2, 1);
  10484. }
  10485. }
  10486. return ret;
  10487. case TARGET_NR_sched_setscheduler:
  10488. {
  10489. struct target_sched_param *target_schp;
  10490. struct sched_param schp;
  10491. if (arg3 == 0) {
  10492. return -TARGET_EINVAL;
  10493. }
  10494. if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1)) {
  10495. return -TARGET_EFAULT;
  10496. }
  10497. schp.sched_priority = tswap32(target_schp->sched_priority);
  10498. unlock_user_struct(target_schp, arg3, 0);
  10499. return get_errno(sys_sched_setscheduler(arg1, arg2, &schp));
  10500. }
  10501. case TARGET_NR_sched_getscheduler:
  10502. return get_errno(sys_sched_getscheduler(arg1));
  10503. case TARGET_NR_sched_getattr:
  10504. {
  10505. struct target_sched_attr *target_scha;
  10506. struct sched_attr scha;
  10507. if (arg2 == 0) {
  10508. return -TARGET_EINVAL;
  10509. }
  10510. if (arg3 > sizeof(scha)) {
  10511. arg3 = sizeof(scha);
  10512. }
  10513. ret = get_errno(sys_sched_getattr(arg1, &scha, arg3, arg4));
  10514. if (!is_error(ret)) {
  10515. target_scha = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  10516. if (!target_scha) {
  10517. return -TARGET_EFAULT;
  10518. }
  10519. target_scha->size = tswap32(scha.size);
  10520. target_scha->sched_policy = tswap32(scha.sched_policy);
  10521. target_scha->sched_flags = tswap64(scha.sched_flags);
  10522. target_scha->sched_nice = tswap32(scha.sched_nice);
  10523. target_scha->sched_priority = tswap32(scha.sched_priority);
  10524. target_scha->sched_runtime = tswap64(scha.sched_runtime);
  10525. target_scha->sched_deadline = tswap64(scha.sched_deadline);
  10526. target_scha->sched_period = tswap64(scha.sched_period);
  10527. if (scha.size > offsetof(struct sched_attr, sched_util_min)) {
  10528. target_scha->sched_util_min = tswap32(scha.sched_util_min);
  10529. target_scha->sched_util_max = tswap32(scha.sched_util_max);
  10530. }
  10531. unlock_user(target_scha, arg2, arg3);
  10532. }
  10533. return ret;
  10534. }
  10535. case TARGET_NR_sched_setattr:
  10536. {
  10537. struct target_sched_attr *target_scha;
  10538. struct sched_attr scha;
  10539. uint32_t size;
  10540. int zeroed;
  10541. if (arg2 == 0) {
  10542. return -TARGET_EINVAL;
  10543. }
  10544. if (get_user_u32(size, arg2)) {
  10545. return -TARGET_EFAULT;
  10546. }
  10547. if (!size) {
  10548. size = offsetof(struct target_sched_attr, sched_util_min);
  10549. }
  10550. if (size < offsetof(struct target_sched_attr, sched_util_min)) {
  10551. if (put_user_u32(sizeof(struct target_sched_attr), arg2)) {
  10552. return -TARGET_EFAULT;
  10553. }
  10554. return -TARGET_E2BIG;
  10555. }
  10556. zeroed = check_zeroed_user(arg2, sizeof(struct target_sched_attr), size);
  10557. if (zeroed < 0) {
  10558. return zeroed;
  10559. } else if (zeroed == 0) {
  10560. if (put_user_u32(sizeof(struct target_sched_attr), arg2)) {
  10561. return -TARGET_EFAULT;
  10562. }
  10563. return -TARGET_E2BIG;
  10564. }
  10565. if (size > sizeof(struct target_sched_attr)) {
  10566. size = sizeof(struct target_sched_attr);
  10567. }
  10568. target_scha = lock_user(VERIFY_READ, arg2, size, 1);
  10569. if (!target_scha) {
  10570. return -TARGET_EFAULT;
  10571. }
  10572. scha.size = size;
  10573. scha.sched_policy = tswap32(target_scha->sched_policy);
  10574. scha.sched_flags = tswap64(target_scha->sched_flags);
  10575. scha.sched_nice = tswap32(target_scha->sched_nice);
  10576. scha.sched_priority = tswap32(target_scha->sched_priority);
  10577. scha.sched_runtime = tswap64(target_scha->sched_runtime);
  10578. scha.sched_deadline = tswap64(target_scha->sched_deadline);
  10579. scha.sched_period = tswap64(target_scha->sched_period);
  10580. if (size > offsetof(struct target_sched_attr, sched_util_min)) {
  10581. scha.sched_util_min = tswap32(target_scha->sched_util_min);
  10582. scha.sched_util_max = tswap32(target_scha->sched_util_max);
  10583. }
  10584. unlock_user(target_scha, arg2, 0);
  10585. return get_errno(sys_sched_setattr(arg1, &scha, arg3));
  10586. }
  10587. case TARGET_NR_sched_yield:
  10588. return get_errno(sched_yield());
  10589. case TARGET_NR_sched_get_priority_max:
  10590. return get_errno(sched_get_priority_max(arg1));
  10591. case TARGET_NR_sched_get_priority_min:
  10592. return get_errno(sched_get_priority_min(arg1));
  10593. #ifdef TARGET_NR_sched_rr_get_interval
  10594. case TARGET_NR_sched_rr_get_interval:
  10595. {
  10596. struct timespec ts;
  10597. ret = get_errno(sched_rr_get_interval(arg1, &ts));
  10598. if (!is_error(ret)) {
  10599. ret = host_to_target_timespec(arg2, &ts);
  10600. }
  10601. }
  10602. return ret;
  10603. #endif
  10604. #ifdef TARGET_NR_sched_rr_get_interval_time64
  10605. case TARGET_NR_sched_rr_get_interval_time64:
  10606. {
  10607. struct timespec ts;
  10608. ret = get_errno(sched_rr_get_interval(arg1, &ts));
  10609. if (!is_error(ret)) {
  10610. ret = host_to_target_timespec64(arg2, &ts);
  10611. }
  10612. }
  10613. return ret;
  10614. #endif
  10615. #if defined(TARGET_NR_nanosleep)
  10616. case TARGET_NR_nanosleep:
  10617. {
  10618. struct timespec req, rem;
  10619. target_to_host_timespec(&req, arg1);
  10620. ret = get_errno(safe_nanosleep(&req, &rem));
  10621. if (is_error(ret) && arg2) {
  10622. host_to_target_timespec(arg2, &rem);
  10623. }
  10624. }
  10625. return ret;
  10626. #endif
  10627. case TARGET_NR_prctl:
  10628. return do_prctl(cpu_env, arg1, arg2, arg3, arg4, arg5);
  10629. break;
  10630. #ifdef TARGET_NR_arch_prctl
  10631. case TARGET_NR_arch_prctl:
  10632. return do_arch_prctl(cpu_env, arg1, arg2);
  10633. #endif
  10634. #ifdef TARGET_NR_pread64
  10635. case TARGET_NR_pread64:
  10636. if (regpairs_aligned(cpu_env, num)) {
  10637. arg4 = arg5;
  10638. arg5 = arg6;
  10639. }
  10640. if (arg2 == 0 && arg3 == 0) {
  10641. /* Special-case NULL buffer and zero length, which should succeed */
  10642. p = 0;
  10643. } else {
  10644. p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  10645. if (!p) {
  10646. return -TARGET_EFAULT;
  10647. }
  10648. }
  10649. ret = get_errno(pread(arg1, p, arg3, target_offset64(arg4, arg5)));
  10650. unlock_user(p, arg2, ret);
  10651. return ret;
  10652. case TARGET_NR_pwrite64:
  10653. if (regpairs_aligned(cpu_env, num)) {
  10654. arg4 = arg5;
  10655. arg5 = arg6;
  10656. }
  10657. if (arg2 == 0 && arg3 == 0) {
  10658. /* Special-case NULL buffer and zero length, which should succeed */
  10659. p = 0;
  10660. } else {
  10661. p = lock_user(VERIFY_READ, arg2, arg3, 1);
  10662. if (!p) {
  10663. return -TARGET_EFAULT;
  10664. }
  10665. }
  10666. ret = get_errno(pwrite(arg1, p, arg3, target_offset64(arg4, arg5)));
  10667. unlock_user(p, arg2, 0);
  10668. return ret;
  10669. #endif
  10670. case TARGET_NR_getcwd:
  10671. if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
  10672. return -TARGET_EFAULT;
  10673. ret = get_errno(sys_getcwd1(p, arg2));
  10674. unlock_user(p, arg1, ret);
  10675. return ret;
  10676. case TARGET_NR_capget:
  10677. case TARGET_NR_capset:
  10678. {
  10679. struct target_user_cap_header *target_header;
  10680. struct target_user_cap_data *target_data = NULL;
  10681. struct __user_cap_header_struct header;
  10682. struct __user_cap_data_struct data[2];
  10683. struct __user_cap_data_struct *dataptr = NULL;
  10684. int i, target_datalen;
  10685. int data_items = 1;
  10686. if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) {
  10687. return -TARGET_EFAULT;
  10688. }
  10689. header.version = tswap32(target_header->version);
  10690. header.pid = tswap32(target_header->pid);
  10691. if (header.version != _LINUX_CAPABILITY_VERSION) {
  10692. /* Version 2 and up takes pointer to two user_data structs */
  10693. data_items = 2;
  10694. }
  10695. target_datalen = sizeof(*target_data) * data_items;
  10696. if (arg2) {
  10697. if (num == TARGET_NR_capget) {
  10698. target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0);
  10699. } else {
  10700. target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1);
  10701. }
  10702. if (!target_data) {
  10703. unlock_user_struct(target_header, arg1, 0);
  10704. return -TARGET_EFAULT;
  10705. }
  10706. if (num == TARGET_NR_capset) {
  10707. for (i = 0; i < data_items; i++) {
  10708. data[i].effective = tswap32(target_data[i].effective);
  10709. data[i].permitted = tswap32(target_data[i].permitted);
  10710. data[i].inheritable = tswap32(target_data[i].inheritable);
  10711. }
  10712. }
  10713. dataptr = data;
  10714. }
  10715. if (num == TARGET_NR_capget) {
  10716. ret = get_errno(capget(&header, dataptr));
  10717. } else {
  10718. ret = get_errno(capset(&header, dataptr));
  10719. }
  10720. /* The kernel always updates version for both capget and capset */
  10721. target_header->version = tswap32(header.version);
  10722. unlock_user_struct(target_header, arg1, 1);
  10723. if (arg2) {
  10724. if (num == TARGET_NR_capget) {
  10725. for (i = 0; i < data_items; i++) {
  10726. target_data[i].effective = tswap32(data[i].effective);
  10727. target_data[i].permitted = tswap32(data[i].permitted);
  10728. target_data[i].inheritable = tswap32(data[i].inheritable);
  10729. }
  10730. unlock_user(target_data, arg2, target_datalen);
  10731. } else {
  10732. unlock_user(target_data, arg2, 0);
  10733. }
  10734. }
  10735. return ret;
  10736. }
  10737. case TARGET_NR_sigaltstack:
  10738. return do_sigaltstack(arg1, arg2, cpu_env);
  10739. #ifdef CONFIG_SENDFILE
  10740. #ifdef TARGET_NR_sendfile
  10741. case TARGET_NR_sendfile:
  10742. {
  10743. off_t *offp = NULL;
  10744. off_t off;
  10745. if (arg3) {
  10746. ret = get_user_sal(off, arg3);
  10747. if (is_error(ret)) {
  10748. return ret;
  10749. }
  10750. offp = &off;
  10751. }
  10752. ret = get_errno(sendfile(arg1, arg2, offp, arg4));
  10753. if (!is_error(ret) && arg3) {
  10754. abi_long ret2 = put_user_sal(off, arg3);
  10755. if (is_error(ret2)) {
  10756. ret = ret2;
  10757. }
  10758. }
  10759. return ret;
  10760. }
  10761. #endif
  10762. #ifdef TARGET_NR_sendfile64
  10763. case TARGET_NR_sendfile64:
  10764. {
  10765. off_t *offp = NULL;
  10766. off_t off;
  10767. if (arg3) {
  10768. ret = get_user_s64(off, arg3);
  10769. if (is_error(ret)) {
  10770. return ret;
  10771. }
  10772. offp = &off;
  10773. }
  10774. ret = get_errno(sendfile(arg1, arg2, offp, arg4));
  10775. if (!is_error(ret) && arg3) {
  10776. abi_long ret2 = put_user_s64(off, arg3);
  10777. if (is_error(ret2)) {
  10778. ret = ret2;
  10779. }
  10780. }
  10781. return ret;
  10782. }
  10783. #endif
  10784. #endif
  10785. #ifdef TARGET_NR_vfork
  10786. case TARGET_NR_vfork:
  10787. return get_errno(do_fork(cpu_env,
  10788. CLONE_VFORK | CLONE_VM | TARGET_SIGCHLD,
  10789. 0, 0, 0, 0));
  10790. #endif
  10791. #ifdef TARGET_NR_ugetrlimit
  10792. case TARGET_NR_ugetrlimit:
  10793. {
  10794. struct rlimit rlim;
  10795. int resource = target_to_host_resource(arg1);
  10796. ret = get_errno(getrlimit(resource, &rlim));
  10797. if (!is_error(ret)) {
  10798. struct target_rlimit *target_rlim;
  10799. if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
  10800. return -TARGET_EFAULT;
  10801. target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
  10802. target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
  10803. unlock_user_struct(target_rlim, arg2, 1);
  10804. }
  10805. return ret;
  10806. }
  10807. #endif
  10808. #ifdef TARGET_NR_truncate64
  10809. case TARGET_NR_truncate64:
  10810. if (!(p = lock_user_string(arg1)))
  10811. return -TARGET_EFAULT;
  10812. ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
  10813. unlock_user(p, arg1, 0);
  10814. return ret;
  10815. #endif
  10816. #ifdef TARGET_NR_ftruncate64
  10817. case TARGET_NR_ftruncate64:
  10818. return target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
  10819. #endif
  10820. #ifdef TARGET_NR_stat64
  10821. case TARGET_NR_stat64:
  10822. if (!(p = lock_user_string(arg1))) {
  10823. return -TARGET_EFAULT;
  10824. }
  10825. ret = get_errno(stat(path(p), &st));
  10826. unlock_user(p, arg1, 0);
  10827. if (!is_error(ret))
  10828. ret = host_to_target_stat64(cpu_env, arg2, &st);
  10829. return ret;
  10830. #endif
  10831. #ifdef TARGET_NR_lstat64
  10832. case TARGET_NR_lstat64:
  10833. if (!(p = lock_user_string(arg1))) {
  10834. return -TARGET_EFAULT;
  10835. }
  10836. ret = get_errno(lstat(path(p), &st));
  10837. unlock_user(p, arg1, 0);
  10838. if (!is_error(ret))
  10839. ret = host_to_target_stat64(cpu_env, arg2, &st);
  10840. return ret;
  10841. #endif
  10842. #ifdef TARGET_NR_fstat64
  10843. case TARGET_NR_fstat64:
  10844. ret = get_errno(fstat(arg1, &st));
  10845. if (!is_error(ret))
  10846. ret = host_to_target_stat64(cpu_env, arg2, &st);
  10847. return ret;
  10848. #endif
  10849. #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
  10850. #ifdef TARGET_NR_fstatat64
  10851. case TARGET_NR_fstatat64:
  10852. #endif
  10853. #ifdef TARGET_NR_newfstatat
  10854. case TARGET_NR_newfstatat:
  10855. #endif
  10856. if (!(p = lock_user_string(arg2))) {
  10857. return -TARGET_EFAULT;
  10858. }
  10859. ret = get_errno(fstatat(arg1, path(p), &st, arg4));
  10860. unlock_user(p, arg2, 0);
  10861. if (!is_error(ret))
  10862. ret = host_to_target_stat64(cpu_env, arg3, &st);
  10863. return ret;
  10864. #endif
  10865. #if defined(TARGET_NR_statx)
  10866. case TARGET_NR_statx:
  10867. {
  10868. struct target_statx *target_stx;
  10869. int dirfd = arg1;
  10870. int flags = arg3;
  10871. p = lock_user_string(arg2);
  10872. if (p == NULL) {
  10873. return -TARGET_EFAULT;
  10874. }
  10875. #if defined(__NR_statx)
  10876. {
  10877. /*
  10878. * It is assumed that struct statx is architecture independent.
  10879. */
  10880. struct target_statx host_stx;
  10881. int mask = arg4;
  10882. ret = get_errno(sys_statx(dirfd, p, flags, mask, &host_stx));
  10883. if (!is_error(ret)) {
  10884. if (host_to_target_statx(&host_stx, arg5) != 0) {
  10885. unlock_user(p, arg2, 0);
  10886. return -TARGET_EFAULT;
  10887. }
  10888. }
  10889. if (ret != -TARGET_ENOSYS) {
  10890. unlock_user(p, arg2, 0);
  10891. return ret;
  10892. }
  10893. }
  10894. #endif
  10895. ret = get_errno(fstatat(dirfd, path(p), &st, flags));
  10896. unlock_user(p, arg2, 0);
  10897. if (!is_error(ret)) {
  10898. if (!lock_user_struct(VERIFY_WRITE, target_stx, arg5, 0)) {
  10899. return -TARGET_EFAULT;
  10900. }
  10901. memset(target_stx, 0, sizeof(*target_stx));
  10902. __put_user(major(st.st_dev), &target_stx->stx_dev_major);
  10903. __put_user(minor(st.st_dev), &target_stx->stx_dev_minor);
  10904. __put_user(st.st_ino, &target_stx->stx_ino);
  10905. __put_user(st.st_mode, &target_stx->stx_mode);
  10906. __put_user(st.st_uid, &target_stx->stx_uid);
  10907. __put_user(st.st_gid, &target_stx->stx_gid);
  10908. __put_user(st.st_nlink, &target_stx->stx_nlink);
  10909. __put_user(major(st.st_rdev), &target_stx->stx_rdev_major);
  10910. __put_user(minor(st.st_rdev), &target_stx->stx_rdev_minor);
  10911. __put_user(st.st_size, &target_stx->stx_size);
  10912. __put_user(st.st_blksize, &target_stx->stx_blksize);
  10913. __put_user(st.st_blocks, &target_stx->stx_blocks);
  10914. __put_user(st.st_atime, &target_stx->stx_atime.tv_sec);
  10915. __put_user(st.st_mtime, &target_stx->stx_mtime.tv_sec);
  10916. __put_user(st.st_ctime, &target_stx->stx_ctime.tv_sec);
  10917. unlock_user_struct(target_stx, arg5, 1);
  10918. }
  10919. }
  10920. return ret;
  10921. #endif
  10922. #ifdef TARGET_NR_lchown
  10923. case TARGET_NR_lchown:
  10924. if (!(p = lock_user_string(arg1)))
  10925. return -TARGET_EFAULT;
  10926. ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
  10927. unlock_user(p, arg1, 0);
  10928. return ret;
  10929. #endif
  10930. #ifdef TARGET_NR_getuid
  10931. case TARGET_NR_getuid:
  10932. return get_errno(high2lowuid(getuid()));
  10933. #endif
  10934. #ifdef TARGET_NR_getgid
  10935. case TARGET_NR_getgid:
  10936. return get_errno(high2lowgid(getgid()));
  10937. #endif
  10938. #ifdef TARGET_NR_geteuid
  10939. case TARGET_NR_geteuid:
  10940. return get_errno(high2lowuid(geteuid()));
  10941. #endif
  10942. #ifdef TARGET_NR_getegid
  10943. case TARGET_NR_getegid:
  10944. return get_errno(high2lowgid(getegid()));
  10945. #endif
  10946. case TARGET_NR_setreuid:
  10947. return get_errno(sys_setreuid(low2highuid(arg1), low2highuid(arg2)));
  10948. case TARGET_NR_setregid:
  10949. return get_errno(sys_setregid(low2highgid(arg1), low2highgid(arg2)));
  10950. case TARGET_NR_getgroups:
  10951. { /* the same code as for TARGET_NR_getgroups32 */
  10952. int gidsetsize = arg1;
  10953. target_id *target_grouplist;
  10954. g_autofree gid_t *grouplist = NULL;
  10955. int i;
  10956. if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) {
  10957. return -TARGET_EINVAL;
  10958. }
  10959. if (gidsetsize > 0) {
  10960. grouplist = g_try_new(gid_t, gidsetsize);
  10961. if (!grouplist) {
  10962. return -TARGET_ENOMEM;
  10963. }
  10964. }
  10965. ret = get_errno(getgroups(gidsetsize, grouplist));
  10966. if (!is_error(ret) && gidsetsize > 0) {
  10967. target_grouplist = lock_user(VERIFY_WRITE, arg2,
  10968. gidsetsize * sizeof(target_id), 0);
  10969. if (!target_grouplist) {
  10970. return -TARGET_EFAULT;
  10971. }
  10972. for (i = 0; i < ret; i++) {
  10973. target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
  10974. }
  10975. unlock_user(target_grouplist, arg2,
  10976. gidsetsize * sizeof(target_id));
  10977. }
  10978. return ret;
  10979. }
  10980. case TARGET_NR_setgroups:
  10981. { /* the same code as for TARGET_NR_setgroups32 */
  10982. int gidsetsize = arg1;
  10983. target_id *target_grouplist;
  10984. g_autofree gid_t *grouplist = NULL;
  10985. int i;
  10986. if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) {
  10987. return -TARGET_EINVAL;
  10988. }
  10989. if (gidsetsize > 0) {
  10990. grouplist = g_try_new(gid_t, gidsetsize);
  10991. if (!grouplist) {
  10992. return -TARGET_ENOMEM;
  10993. }
  10994. target_grouplist = lock_user(VERIFY_READ, arg2,
  10995. gidsetsize * sizeof(target_id), 1);
  10996. if (!target_grouplist) {
  10997. return -TARGET_EFAULT;
  10998. }
  10999. for (i = 0; i < gidsetsize; i++) {
  11000. grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
  11001. }
  11002. unlock_user(target_grouplist, arg2,
  11003. gidsetsize * sizeof(target_id));
  11004. }
  11005. return get_errno(sys_setgroups(gidsetsize, grouplist));
  11006. }
  11007. case TARGET_NR_fchown:
  11008. return get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
  11009. #if defined(TARGET_NR_fchownat)
  11010. case TARGET_NR_fchownat:
  11011. if (!(p = lock_user_string(arg2)))
  11012. return -TARGET_EFAULT;
  11013. ret = get_errno(fchownat(arg1, p, low2highuid(arg3),
  11014. low2highgid(arg4), arg5));
  11015. unlock_user(p, arg2, 0);
  11016. return ret;
  11017. #endif
  11018. #ifdef TARGET_NR_setresuid
  11019. case TARGET_NR_setresuid:
  11020. return get_errno(sys_setresuid(low2highuid(arg1),
  11021. low2highuid(arg2),
  11022. low2highuid(arg3)));
  11023. #endif
  11024. #ifdef TARGET_NR_getresuid
  11025. case TARGET_NR_getresuid:
  11026. {
  11027. uid_t ruid, euid, suid;
  11028. ret = get_errno(getresuid(&ruid, &euid, &suid));
  11029. if (!is_error(ret)) {
  11030. if (put_user_id(high2lowuid(ruid), arg1)
  11031. || put_user_id(high2lowuid(euid), arg2)
  11032. || put_user_id(high2lowuid(suid), arg3))
  11033. return -TARGET_EFAULT;
  11034. }
  11035. }
  11036. return ret;
  11037. #endif
  11038. #ifdef TARGET_NR_getresgid
  11039. case TARGET_NR_setresgid:
  11040. return get_errno(sys_setresgid(low2highgid(arg1),
  11041. low2highgid(arg2),
  11042. low2highgid(arg3)));
  11043. #endif
  11044. #ifdef TARGET_NR_getresgid
  11045. case TARGET_NR_getresgid:
  11046. {
  11047. gid_t rgid, egid, sgid;
  11048. ret = get_errno(getresgid(&rgid, &egid, &sgid));
  11049. if (!is_error(ret)) {
  11050. if (put_user_id(high2lowgid(rgid), arg1)
  11051. || put_user_id(high2lowgid(egid), arg2)
  11052. || put_user_id(high2lowgid(sgid), arg3))
  11053. return -TARGET_EFAULT;
  11054. }
  11055. }
  11056. return ret;
  11057. #endif
  11058. #ifdef TARGET_NR_chown
  11059. case TARGET_NR_chown:
  11060. if (!(p = lock_user_string(arg1)))
  11061. return -TARGET_EFAULT;
  11062. ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
  11063. unlock_user(p, arg1, 0);
  11064. return ret;
  11065. #endif
  11066. case TARGET_NR_setuid:
  11067. return get_errno(sys_setuid(low2highuid(arg1)));
  11068. case TARGET_NR_setgid:
  11069. return get_errno(sys_setgid(low2highgid(arg1)));
  11070. case TARGET_NR_setfsuid:
  11071. return get_errno(setfsuid(arg1));
  11072. case TARGET_NR_setfsgid:
  11073. return get_errno(setfsgid(arg1));
  11074. #ifdef TARGET_NR_lchown32
  11075. case TARGET_NR_lchown32:
  11076. if (!(p = lock_user_string(arg1)))
  11077. return -TARGET_EFAULT;
  11078. ret = get_errno(lchown(p, arg2, arg3));
  11079. unlock_user(p, arg1, 0);
  11080. return ret;
  11081. #endif
  11082. #ifdef TARGET_NR_getuid32
  11083. case TARGET_NR_getuid32:
  11084. return get_errno(getuid());
  11085. #endif
  11086. #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
  11087. /* Alpha specific */
  11088. case TARGET_NR_getxuid:
  11089. {
  11090. uid_t euid;
  11091. euid=geteuid();
  11092. cpu_env->ir[IR_A4]=euid;
  11093. }
  11094. return get_errno(getuid());
  11095. #endif
  11096. #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
  11097. /* Alpha specific */
  11098. case TARGET_NR_getxgid:
  11099. {
  11100. uid_t egid;
  11101. egid=getegid();
  11102. cpu_env->ir[IR_A4]=egid;
  11103. }
  11104. return get_errno(getgid());
  11105. #endif
  11106. #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
  11107. /* Alpha specific */
  11108. case TARGET_NR_osf_getsysinfo:
  11109. ret = -TARGET_EOPNOTSUPP;
  11110. switch (arg1) {
  11111. case TARGET_GSI_IEEE_FP_CONTROL:
  11112. {
  11113. uint64_t fpcr = cpu_alpha_load_fpcr(cpu_env);
  11114. uint64_t swcr = cpu_env->swcr;
  11115. swcr &= ~SWCR_STATUS_MASK;
  11116. swcr |= (fpcr >> 35) & SWCR_STATUS_MASK;
  11117. if (put_user_u64 (swcr, arg2))
  11118. return -TARGET_EFAULT;
  11119. ret = 0;
  11120. }
  11121. break;
  11122. /* case GSI_IEEE_STATE_AT_SIGNAL:
  11123. -- Not implemented in linux kernel.
  11124. case GSI_UACPROC:
  11125. -- Retrieves current unaligned access state; not much used.
  11126. case GSI_PROC_TYPE:
  11127. -- Retrieves implver information; surely not used.
  11128. case GSI_GET_HWRPB:
  11129. -- Grabs a copy of the HWRPB; surely not used.
  11130. */
  11131. }
  11132. return ret;
  11133. #endif
  11134. #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
  11135. /* Alpha specific */
  11136. case TARGET_NR_osf_setsysinfo:
  11137. ret = -TARGET_EOPNOTSUPP;
  11138. switch (arg1) {
  11139. case TARGET_SSI_IEEE_FP_CONTROL:
  11140. {
  11141. uint64_t swcr, fpcr;
  11142. if (get_user_u64 (swcr, arg2)) {
  11143. return -TARGET_EFAULT;
  11144. }
  11145. /*
  11146. * The kernel calls swcr_update_status to update the
  11147. * status bits from the fpcr at every point that it
  11148. * could be queried. Therefore, we store the status
  11149. * bits only in FPCR.
  11150. */
  11151. cpu_env->swcr = swcr & (SWCR_TRAP_ENABLE_MASK | SWCR_MAP_MASK);
  11152. fpcr = cpu_alpha_load_fpcr(cpu_env);
  11153. fpcr &= ((uint64_t)FPCR_DYN_MASK << 32);
  11154. fpcr |= alpha_ieee_swcr_to_fpcr(swcr);
  11155. cpu_alpha_store_fpcr(cpu_env, fpcr);
  11156. ret = 0;
  11157. }
  11158. break;
  11159. case TARGET_SSI_IEEE_RAISE_EXCEPTION:
  11160. {
  11161. uint64_t exc, fpcr, fex;
  11162. if (get_user_u64(exc, arg2)) {
  11163. return -TARGET_EFAULT;
  11164. }
  11165. exc &= SWCR_STATUS_MASK;
  11166. fpcr = cpu_alpha_load_fpcr(cpu_env);
  11167. /* Old exceptions are not signaled. */
  11168. fex = alpha_ieee_fpcr_to_swcr(fpcr);
  11169. fex = exc & ~fex;
  11170. fex >>= SWCR_STATUS_TO_EXCSUM_SHIFT;
  11171. fex &= (cpu_env)->swcr;
  11172. /* Update the hardware fpcr. */
  11173. fpcr |= alpha_ieee_swcr_to_fpcr(exc);
  11174. cpu_alpha_store_fpcr(cpu_env, fpcr);
  11175. if (fex) {
  11176. int si_code = TARGET_FPE_FLTUNK;
  11177. target_siginfo_t info;
  11178. if (fex & SWCR_TRAP_ENABLE_DNO) {
  11179. si_code = TARGET_FPE_FLTUND;
  11180. }
  11181. if (fex & SWCR_TRAP_ENABLE_INE) {
  11182. si_code = TARGET_FPE_FLTRES;
  11183. }
  11184. if (fex & SWCR_TRAP_ENABLE_UNF) {
  11185. si_code = TARGET_FPE_FLTUND;
  11186. }
  11187. if (fex & SWCR_TRAP_ENABLE_OVF) {
  11188. si_code = TARGET_FPE_FLTOVF;
  11189. }
  11190. if (fex & SWCR_TRAP_ENABLE_DZE) {
  11191. si_code = TARGET_FPE_FLTDIV;
  11192. }
  11193. if (fex & SWCR_TRAP_ENABLE_INV) {
  11194. si_code = TARGET_FPE_FLTINV;
  11195. }
  11196. info.si_signo = SIGFPE;
  11197. info.si_errno = 0;
  11198. info.si_code = si_code;
  11199. info._sifields._sigfault._addr = (cpu_env)->pc;
  11200. queue_signal(cpu_env, info.si_signo,
  11201. QEMU_SI_FAULT, &info);
  11202. }
  11203. ret = 0;
  11204. }
  11205. break;
  11206. /* case SSI_NVPAIRS:
  11207. -- Used with SSIN_UACPROC to enable unaligned accesses.
  11208. case SSI_IEEE_STATE_AT_SIGNAL:
  11209. case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
  11210. -- Not implemented in linux kernel
  11211. */
  11212. }
  11213. return ret;
  11214. #endif
  11215. #ifdef TARGET_NR_osf_sigprocmask
  11216. /* Alpha specific. */
  11217. case TARGET_NR_osf_sigprocmask:
  11218. {
  11219. abi_ulong mask;
  11220. int how;
  11221. sigset_t set, oldset;
  11222. switch(arg1) {
  11223. case TARGET_SIG_BLOCK:
  11224. how = SIG_BLOCK;
  11225. break;
  11226. case TARGET_SIG_UNBLOCK:
  11227. how = SIG_UNBLOCK;
  11228. break;
  11229. case TARGET_SIG_SETMASK:
  11230. how = SIG_SETMASK;
  11231. break;
  11232. default:
  11233. return -TARGET_EINVAL;
  11234. }
  11235. mask = arg2;
  11236. target_to_host_old_sigset(&set, &mask);
  11237. ret = do_sigprocmask(how, &set, &oldset);
  11238. if (!ret) {
  11239. host_to_target_old_sigset(&mask, &oldset);
  11240. ret = mask;
  11241. }
  11242. }
  11243. return ret;
  11244. #endif
  11245. #ifdef TARGET_NR_getgid32
  11246. case TARGET_NR_getgid32:
  11247. return get_errno(getgid());
  11248. #endif
  11249. #ifdef TARGET_NR_geteuid32
  11250. case TARGET_NR_geteuid32:
  11251. return get_errno(geteuid());
  11252. #endif
  11253. #ifdef TARGET_NR_getegid32
  11254. case TARGET_NR_getegid32:
  11255. return get_errno(getegid());
  11256. #endif
  11257. #ifdef TARGET_NR_setreuid32
  11258. case TARGET_NR_setreuid32:
  11259. return get_errno(sys_setreuid(arg1, arg2));
  11260. #endif
  11261. #ifdef TARGET_NR_setregid32
  11262. case TARGET_NR_setregid32:
  11263. return get_errno(sys_setregid(arg1, arg2));
  11264. #endif
  11265. #ifdef TARGET_NR_getgroups32
  11266. case TARGET_NR_getgroups32:
  11267. { /* the same code as for TARGET_NR_getgroups */
  11268. int gidsetsize = arg1;
  11269. uint32_t *target_grouplist;
  11270. g_autofree gid_t *grouplist = NULL;
  11271. int i;
  11272. if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) {
  11273. return -TARGET_EINVAL;
  11274. }
  11275. if (gidsetsize > 0) {
  11276. grouplist = g_try_new(gid_t, gidsetsize);
  11277. if (!grouplist) {
  11278. return -TARGET_ENOMEM;
  11279. }
  11280. }
  11281. ret = get_errno(getgroups(gidsetsize, grouplist));
  11282. if (!is_error(ret) && gidsetsize > 0) {
  11283. target_grouplist = lock_user(VERIFY_WRITE, arg2,
  11284. gidsetsize * 4, 0);
  11285. if (!target_grouplist) {
  11286. return -TARGET_EFAULT;
  11287. }
  11288. for (i = 0; i < ret; i++) {
  11289. target_grouplist[i] = tswap32(grouplist[i]);
  11290. }
  11291. unlock_user(target_grouplist, arg2, gidsetsize * 4);
  11292. }
  11293. return ret;
  11294. }
  11295. #endif
  11296. #ifdef TARGET_NR_setgroups32
  11297. case TARGET_NR_setgroups32:
  11298. { /* the same code as for TARGET_NR_setgroups */
  11299. int gidsetsize = arg1;
  11300. uint32_t *target_grouplist;
  11301. g_autofree gid_t *grouplist = NULL;
  11302. int i;
  11303. if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) {
  11304. return -TARGET_EINVAL;
  11305. }
  11306. if (gidsetsize > 0) {
  11307. grouplist = g_try_new(gid_t, gidsetsize);
  11308. if (!grouplist) {
  11309. return -TARGET_ENOMEM;
  11310. }
  11311. target_grouplist = lock_user(VERIFY_READ, arg2,
  11312. gidsetsize * 4, 1);
  11313. if (!target_grouplist) {
  11314. return -TARGET_EFAULT;
  11315. }
  11316. for (i = 0; i < gidsetsize; i++) {
  11317. grouplist[i] = tswap32(target_grouplist[i]);
  11318. }
  11319. unlock_user(target_grouplist, arg2, 0);
  11320. }
  11321. return get_errno(sys_setgroups(gidsetsize, grouplist));
  11322. }
  11323. #endif
  11324. #ifdef TARGET_NR_fchown32
  11325. case TARGET_NR_fchown32:
  11326. return get_errno(fchown(arg1, arg2, arg3));
  11327. #endif
  11328. #ifdef TARGET_NR_setresuid32
  11329. case TARGET_NR_setresuid32:
  11330. return get_errno(sys_setresuid(arg1, arg2, arg3));
  11331. #endif
  11332. #ifdef TARGET_NR_getresuid32
  11333. case TARGET_NR_getresuid32:
  11334. {
  11335. uid_t ruid, euid, suid;
  11336. ret = get_errno(getresuid(&ruid, &euid, &suid));
  11337. if (!is_error(ret)) {
  11338. if (put_user_u32(ruid, arg1)
  11339. || put_user_u32(euid, arg2)
  11340. || put_user_u32(suid, arg3))
  11341. return -TARGET_EFAULT;
  11342. }
  11343. }
  11344. return ret;
  11345. #endif
  11346. #ifdef TARGET_NR_setresgid32
  11347. case TARGET_NR_setresgid32:
  11348. return get_errno(sys_setresgid(arg1, arg2, arg3));
  11349. #endif
  11350. #ifdef TARGET_NR_getresgid32
  11351. case TARGET_NR_getresgid32:
  11352. {
  11353. gid_t rgid, egid, sgid;
  11354. ret = get_errno(getresgid(&rgid, &egid, &sgid));
  11355. if (!is_error(ret)) {
  11356. if (put_user_u32(rgid, arg1)
  11357. || put_user_u32(egid, arg2)
  11358. || put_user_u32(sgid, arg3))
  11359. return -TARGET_EFAULT;
  11360. }
  11361. }
  11362. return ret;
  11363. #endif
  11364. #ifdef TARGET_NR_chown32
  11365. case TARGET_NR_chown32:
  11366. if (!(p = lock_user_string(arg1)))
  11367. return -TARGET_EFAULT;
  11368. ret = get_errno(chown(p, arg2, arg3));
  11369. unlock_user(p, arg1, 0);
  11370. return ret;
  11371. #endif
  11372. #ifdef TARGET_NR_setuid32
  11373. case TARGET_NR_setuid32:
  11374. return get_errno(sys_setuid(arg1));
  11375. #endif
  11376. #ifdef TARGET_NR_setgid32
  11377. case TARGET_NR_setgid32:
  11378. return get_errno(sys_setgid(arg1));
  11379. #endif
  11380. #ifdef TARGET_NR_setfsuid32
  11381. case TARGET_NR_setfsuid32:
  11382. return get_errno(setfsuid(arg1));
  11383. #endif
  11384. #ifdef TARGET_NR_setfsgid32
  11385. case TARGET_NR_setfsgid32:
  11386. return get_errno(setfsgid(arg1));
  11387. #endif
  11388. #ifdef TARGET_NR_mincore
  11389. case TARGET_NR_mincore:
  11390. {
  11391. void *a = lock_user(VERIFY_NONE, arg1, arg2, 0);
  11392. if (!a) {
  11393. return -TARGET_ENOMEM;
  11394. }
  11395. p = lock_user_string(arg3);
  11396. if (!p) {
  11397. ret = -TARGET_EFAULT;
  11398. } else {
  11399. ret = get_errno(mincore(a, arg2, p));
  11400. unlock_user(p, arg3, ret);
  11401. }
  11402. unlock_user(a, arg1, 0);
  11403. }
  11404. return ret;
  11405. #endif
  11406. #ifdef TARGET_NR_arm_fadvise64_64
  11407. case TARGET_NR_arm_fadvise64_64:
  11408. /* arm_fadvise64_64 looks like fadvise64_64 but
  11409. * with different argument order: fd, advice, offset, len
  11410. * rather than the usual fd, offset, len, advice.
  11411. * Note that offset and len are both 64-bit so appear as
  11412. * pairs of 32-bit registers.
  11413. */
  11414. ret = posix_fadvise(arg1, target_offset64(arg3, arg4),
  11415. target_offset64(arg5, arg6), arg2);
  11416. return -host_to_target_errno(ret);
  11417. #endif
  11418. #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
  11419. #ifdef TARGET_NR_fadvise64_64
  11420. case TARGET_NR_fadvise64_64:
  11421. #if defined(TARGET_PPC) || defined(TARGET_XTENSA)
  11422. /* 6 args: fd, advice, offset (high, low), len (high, low) */
  11423. ret = arg2;
  11424. arg2 = arg3;
  11425. arg3 = arg4;
  11426. arg4 = arg5;
  11427. arg5 = arg6;
  11428. arg6 = ret;
  11429. #else
  11430. /* 6 args: fd, offset (high, low), len (high, low), advice */
  11431. if (regpairs_aligned(cpu_env, num)) {
  11432. /* offset is in (3,4), len in (5,6) and advice in 7 */
  11433. arg2 = arg3;
  11434. arg3 = arg4;
  11435. arg4 = arg5;
  11436. arg5 = arg6;
  11437. arg6 = arg7;
  11438. }
  11439. #endif
  11440. ret = posix_fadvise(arg1, target_offset64(arg2, arg3),
  11441. target_offset64(arg4, arg5), arg6);
  11442. return -host_to_target_errno(ret);
  11443. #endif
  11444. #ifdef TARGET_NR_fadvise64
  11445. case TARGET_NR_fadvise64:
  11446. /* 5 args: fd, offset (high, low), len, advice */
  11447. if (regpairs_aligned(cpu_env, num)) {
  11448. /* offset is in (3,4), len in 5 and advice in 6 */
  11449. arg2 = arg3;
  11450. arg3 = arg4;
  11451. arg4 = arg5;
  11452. arg5 = arg6;
  11453. }
  11454. ret = posix_fadvise(arg1, target_offset64(arg2, arg3), arg4, arg5);
  11455. return -host_to_target_errno(ret);
  11456. #endif
  11457. #else /* not a 32-bit ABI */
  11458. #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_fadvise64)
  11459. #ifdef TARGET_NR_fadvise64_64
  11460. case TARGET_NR_fadvise64_64:
  11461. #endif
  11462. #ifdef TARGET_NR_fadvise64
  11463. case TARGET_NR_fadvise64:
  11464. #endif
  11465. #ifdef TARGET_S390X
  11466. switch (arg4) {
  11467. case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
  11468. case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
  11469. case 6: arg4 = POSIX_FADV_DONTNEED; break;
  11470. case 7: arg4 = POSIX_FADV_NOREUSE; break;
  11471. default: break;
  11472. }
  11473. #endif
  11474. return -host_to_target_errno(posix_fadvise(arg1, arg2, arg3, arg4));
  11475. #endif
  11476. #endif /* end of 64-bit ABI fadvise handling */
  11477. #ifdef TARGET_NR_madvise
  11478. case TARGET_NR_madvise:
  11479. return target_madvise(arg1, arg2, arg3);
  11480. #endif
  11481. #ifdef TARGET_NR_fcntl64
  11482. case TARGET_NR_fcntl64:
  11483. {
  11484. int cmd;
  11485. struct flock fl;
  11486. from_flock64_fn *copyfrom = copy_from_user_flock64;
  11487. to_flock64_fn *copyto = copy_to_user_flock64;
  11488. #ifdef TARGET_ARM
  11489. if (!cpu_env->eabi) {
  11490. copyfrom = copy_from_user_oabi_flock64;
  11491. copyto = copy_to_user_oabi_flock64;
  11492. }
  11493. #endif
  11494. cmd = target_to_host_fcntl_cmd(arg2);
  11495. if (cmd == -TARGET_EINVAL) {
  11496. return cmd;
  11497. }
  11498. switch(arg2) {
  11499. case TARGET_F_GETLK64:
  11500. ret = copyfrom(&fl, arg3);
  11501. if (ret) {
  11502. break;
  11503. }
  11504. ret = get_errno(safe_fcntl(arg1, cmd, &fl));
  11505. if (ret == 0) {
  11506. ret = copyto(arg3, &fl);
  11507. }
  11508. break;
  11509. case TARGET_F_SETLK64:
  11510. case TARGET_F_SETLKW64:
  11511. ret = copyfrom(&fl, arg3);
  11512. if (ret) {
  11513. break;
  11514. }
  11515. ret = get_errno(safe_fcntl(arg1, cmd, &fl));
  11516. break;
  11517. default:
  11518. ret = do_fcntl(arg1, arg2, arg3);
  11519. break;
  11520. }
  11521. return ret;
  11522. }
  11523. #endif
  11524. #ifdef TARGET_NR_cacheflush
  11525. case TARGET_NR_cacheflush:
  11526. /* self-modifying code is handled automatically, so nothing needed */
  11527. return 0;
  11528. #endif
  11529. #ifdef TARGET_NR_getpagesize
  11530. case TARGET_NR_getpagesize:
  11531. return TARGET_PAGE_SIZE;
  11532. #endif
  11533. case TARGET_NR_gettid:
  11534. return get_errno(sys_gettid());
  11535. #ifdef TARGET_NR_readahead
  11536. case TARGET_NR_readahead:
  11537. #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
  11538. if (regpairs_aligned(cpu_env, num)) {
  11539. arg2 = arg3;
  11540. arg3 = arg4;
  11541. arg4 = arg5;
  11542. }
  11543. ret = get_errno(readahead(arg1, target_offset64(arg2, arg3) , arg4));
  11544. #else
  11545. ret = get_errno(readahead(arg1, arg2, arg3));
  11546. #endif
  11547. return ret;
  11548. #endif
  11549. #ifdef CONFIG_ATTR
  11550. #ifdef TARGET_NR_setxattr
  11551. case TARGET_NR_listxattr:
  11552. case TARGET_NR_llistxattr:
  11553. {
  11554. void *b = 0;
  11555. if (arg2) {
  11556. b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  11557. if (!b) {
  11558. return -TARGET_EFAULT;
  11559. }
  11560. }
  11561. p = lock_user_string(arg1);
  11562. if (p) {
  11563. if (num == TARGET_NR_listxattr) {
  11564. ret = get_errno(listxattr(p, b, arg3));
  11565. } else {
  11566. ret = get_errno(llistxattr(p, b, arg3));
  11567. }
  11568. } else {
  11569. ret = -TARGET_EFAULT;
  11570. }
  11571. unlock_user(p, arg1, 0);
  11572. unlock_user(b, arg2, arg3);
  11573. return ret;
  11574. }
  11575. case TARGET_NR_flistxattr:
  11576. {
  11577. void *b = 0;
  11578. if (arg2) {
  11579. b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  11580. if (!b) {
  11581. return -TARGET_EFAULT;
  11582. }
  11583. }
  11584. ret = get_errno(flistxattr(arg1, b, arg3));
  11585. unlock_user(b, arg2, arg3);
  11586. return ret;
  11587. }
  11588. case TARGET_NR_setxattr:
  11589. case TARGET_NR_lsetxattr:
  11590. {
  11591. void *n, *v = 0;
  11592. if (arg3) {
  11593. v = lock_user(VERIFY_READ, arg3, arg4, 1);
  11594. if (!v) {
  11595. return -TARGET_EFAULT;
  11596. }
  11597. }
  11598. p = lock_user_string(arg1);
  11599. n = lock_user_string(arg2);
  11600. if (p && n) {
  11601. if (num == TARGET_NR_setxattr) {
  11602. ret = get_errno(setxattr(p, n, v, arg4, arg5));
  11603. } else {
  11604. ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
  11605. }
  11606. } else {
  11607. ret = -TARGET_EFAULT;
  11608. }
  11609. unlock_user(p, arg1, 0);
  11610. unlock_user(n, arg2, 0);
  11611. unlock_user(v, arg3, 0);
  11612. }
  11613. return ret;
  11614. case TARGET_NR_fsetxattr:
  11615. {
  11616. void *n, *v = 0;
  11617. if (arg3) {
  11618. v = lock_user(VERIFY_READ, arg3, arg4, 1);
  11619. if (!v) {
  11620. return -TARGET_EFAULT;
  11621. }
  11622. }
  11623. n = lock_user_string(arg2);
  11624. if (n) {
  11625. ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
  11626. } else {
  11627. ret = -TARGET_EFAULT;
  11628. }
  11629. unlock_user(n, arg2, 0);
  11630. unlock_user(v, arg3, 0);
  11631. }
  11632. return ret;
  11633. case TARGET_NR_getxattr:
  11634. case TARGET_NR_lgetxattr:
  11635. {
  11636. void *n, *v = 0;
  11637. if (arg3) {
  11638. v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  11639. if (!v) {
  11640. return -TARGET_EFAULT;
  11641. }
  11642. }
  11643. p = lock_user_string(arg1);
  11644. n = lock_user_string(arg2);
  11645. if (p && n) {
  11646. if (num == TARGET_NR_getxattr) {
  11647. ret = get_errno(getxattr(p, n, v, arg4));
  11648. } else {
  11649. ret = get_errno(lgetxattr(p, n, v, arg4));
  11650. }
  11651. } else {
  11652. ret = -TARGET_EFAULT;
  11653. }
  11654. unlock_user(p, arg1, 0);
  11655. unlock_user(n, arg2, 0);
  11656. unlock_user(v, arg3, arg4);
  11657. }
  11658. return ret;
  11659. case TARGET_NR_fgetxattr:
  11660. {
  11661. void *n, *v = 0;
  11662. if (arg3) {
  11663. v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  11664. if (!v) {
  11665. return -TARGET_EFAULT;
  11666. }
  11667. }
  11668. n = lock_user_string(arg2);
  11669. if (n) {
  11670. ret = get_errno(fgetxattr(arg1, n, v, arg4));
  11671. } else {
  11672. ret = -TARGET_EFAULT;
  11673. }
  11674. unlock_user(n, arg2, 0);
  11675. unlock_user(v, arg3, arg4);
  11676. }
  11677. return ret;
  11678. case TARGET_NR_removexattr:
  11679. case TARGET_NR_lremovexattr:
  11680. {
  11681. void *n;
  11682. p = lock_user_string(arg1);
  11683. n = lock_user_string(arg2);
  11684. if (p && n) {
  11685. if (num == TARGET_NR_removexattr) {
  11686. ret = get_errno(removexattr(p, n));
  11687. } else {
  11688. ret = get_errno(lremovexattr(p, n));
  11689. }
  11690. } else {
  11691. ret = -TARGET_EFAULT;
  11692. }
  11693. unlock_user(p, arg1, 0);
  11694. unlock_user(n, arg2, 0);
  11695. }
  11696. return ret;
  11697. case TARGET_NR_fremovexattr:
  11698. {
  11699. void *n;
  11700. n = lock_user_string(arg2);
  11701. if (n) {
  11702. ret = get_errno(fremovexattr(arg1, n));
  11703. } else {
  11704. ret = -TARGET_EFAULT;
  11705. }
  11706. unlock_user(n, arg2, 0);
  11707. }
  11708. return ret;
  11709. #endif
  11710. #endif /* CONFIG_ATTR */
  11711. #ifdef TARGET_NR_set_thread_area
  11712. case TARGET_NR_set_thread_area:
  11713. #if defined(TARGET_MIPS)
  11714. cpu_env->active_tc.CP0_UserLocal = arg1;
  11715. return 0;
  11716. #elif defined(TARGET_I386) && defined(TARGET_ABI32)
  11717. return do_set_thread_area(cpu_env, arg1);
  11718. #elif defined(TARGET_M68K)
  11719. {
  11720. TaskState *ts = get_task_state(cpu);
  11721. ts->tp_value = arg1;
  11722. return 0;
  11723. }
  11724. #else
  11725. return -TARGET_ENOSYS;
  11726. #endif
  11727. #endif
  11728. #ifdef TARGET_NR_get_thread_area
  11729. case TARGET_NR_get_thread_area:
  11730. #if defined(TARGET_I386) && defined(TARGET_ABI32)
  11731. return do_get_thread_area(cpu_env, arg1);
  11732. #elif defined(TARGET_M68K)
  11733. {
  11734. TaskState *ts = get_task_state(cpu);
  11735. return ts->tp_value;
  11736. }
  11737. #else
  11738. return -TARGET_ENOSYS;
  11739. #endif
  11740. #endif
  11741. #ifdef TARGET_NR_getdomainname
  11742. case TARGET_NR_getdomainname:
  11743. return -TARGET_ENOSYS;
  11744. #endif
  11745. #ifdef TARGET_NR_clock_settime
  11746. case TARGET_NR_clock_settime:
  11747. {
  11748. struct timespec ts;
  11749. ret = target_to_host_timespec(&ts, arg2);
  11750. if (!is_error(ret)) {
  11751. ret = get_errno(clock_settime(arg1, &ts));
  11752. }
  11753. return ret;
  11754. }
  11755. #endif
  11756. #ifdef TARGET_NR_clock_settime64
  11757. case TARGET_NR_clock_settime64:
  11758. {
  11759. struct timespec ts;
  11760. ret = target_to_host_timespec64(&ts, arg2);
  11761. if (!is_error(ret)) {
  11762. ret = get_errno(clock_settime(arg1, &ts));
  11763. }
  11764. return ret;
  11765. }
  11766. #endif
  11767. #ifdef TARGET_NR_clock_gettime
  11768. case TARGET_NR_clock_gettime:
  11769. {
  11770. struct timespec ts;
  11771. ret = get_errno(clock_gettime(arg1, &ts));
  11772. if (!is_error(ret)) {
  11773. ret = host_to_target_timespec(arg2, &ts);
  11774. }
  11775. return ret;
  11776. }
  11777. #endif
  11778. #ifdef TARGET_NR_clock_gettime64
  11779. case TARGET_NR_clock_gettime64:
  11780. {
  11781. struct timespec ts;
  11782. ret = get_errno(clock_gettime(arg1, &ts));
  11783. if (!is_error(ret)) {
  11784. ret = host_to_target_timespec64(arg2, &ts);
  11785. }
  11786. return ret;
  11787. }
  11788. #endif
  11789. #ifdef TARGET_NR_clock_getres
  11790. case TARGET_NR_clock_getres:
  11791. {
  11792. struct timespec ts;
  11793. ret = get_errno(clock_getres(arg1, &ts));
  11794. if (!is_error(ret)) {
  11795. host_to_target_timespec(arg2, &ts);
  11796. }
  11797. return ret;
  11798. }
  11799. #endif
  11800. #ifdef TARGET_NR_clock_getres_time64
  11801. case TARGET_NR_clock_getres_time64:
  11802. {
  11803. struct timespec ts;
  11804. ret = get_errno(clock_getres(arg1, &ts));
  11805. if (!is_error(ret)) {
  11806. host_to_target_timespec64(arg2, &ts);
  11807. }
  11808. return ret;
  11809. }
  11810. #endif
  11811. #ifdef TARGET_NR_clock_nanosleep
  11812. case TARGET_NR_clock_nanosleep:
  11813. {
  11814. struct timespec ts;
  11815. if (target_to_host_timespec(&ts, arg3)) {
  11816. return -TARGET_EFAULT;
  11817. }
  11818. ret = get_errno(safe_clock_nanosleep(arg1, arg2,
  11819. &ts, arg4 ? &ts : NULL));
  11820. /*
  11821. * if the call is interrupted by a signal handler, it fails
  11822. * with error -TARGET_EINTR and if arg4 is not NULL and arg2 is not
  11823. * TIMER_ABSTIME, it returns the remaining unslept time in arg4.
  11824. */
  11825. if (ret == -TARGET_EINTR && arg4 && arg2 != TIMER_ABSTIME &&
  11826. host_to_target_timespec(arg4, &ts)) {
  11827. return -TARGET_EFAULT;
  11828. }
  11829. return ret;
  11830. }
  11831. #endif
  11832. #ifdef TARGET_NR_clock_nanosleep_time64
  11833. case TARGET_NR_clock_nanosleep_time64:
  11834. {
  11835. struct timespec ts;
  11836. if (target_to_host_timespec64(&ts, arg3)) {
  11837. return -TARGET_EFAULT;
  11838. }
  11839. ret = get_errno(safe_clock_nanosleep(arg1, arg2,
  11840. &ts, arg4 ? &ts : NULL));
  11841. if (ret == -TARGET_EINTR && arg4 && arg2 != TIMER_ABSTIME &&
  11842. host_to_target_timespec64(arg4, &ts)) {
  11843. return -TARGET_EFAULT;
  11844. }
  11845. return ret;
  11846. }
  11847. #endif
  11848. #if defined(TARGET_NR_set_tid_address)
  11849. case TARGET_NR_set_tid_address:
  11850. {
  11851. TaskState *ts = get_task_state(cpu);
  11852. ts->child_tidptr = arg1;
  11853. /* do not call host set_tid_address() syscall, instead return tid() */
  11854. return get_errno(sys_gettid());
  11855. }
  11856. #endif
  11857. case TARGET_NR_tkill:
  11858. return get_errno(safe_tkill((int)arg1, target_to_host_signal(arg2)));
  11859. case TARGET_NR_tgkill:
  11860. return get_errno(safe_tgkill((int)arg1, (int)arg2,
  11861. target_to_host_signal(arg3)));
  11862. #ifdef TARGET_NR_set_robust_list
  11863. case TARGET_NR_set_robust_list:
  11864. case TARGET_NR_get_robust_list:
  11865. /* The ABI for supporting robust futexes has userspace pass
  11866. * the kernel a pointer to a linked list which is updated by
  11867. * userspace after the syscall; the list is walked by the kernel
  11868. * when the thread exits. Since the linked list in QEMU guest
  11869. * memory isn't a valid linked list for the host and we have
  11870. * no way to reliably intercept the thread-death event, we can't
  11871. * support these. Silently return ENOSYS so that guest userspace
  11872. * falls back to a non-robust futex implementation (which should
  11873. * be OK except in the corner case of the guest crashing while
  11874. * holding a mutex that is shared with another process via
  11875. * shared memory).
  11876. */
  11877. return -TARGET_ENOSYS;
  11878. #endif
  11879. #if defined(TARGET_NR_utimensat)
  11880. case TARGET_NR_utimensat:
  11881. {
  11882. struct timespec *tsp, ts[2];
  11883. if (!arg3) {
  11884. tsp = NULL;
  11885. } else {
  11886. if (target_to_host_timespec(ts, arg3)) {
  11887. return -TARGET_EFAULT;
  11888. }
  11889. if (target_to_host_timespec(ts + 1, arg3 +
  11890. sizeof(struct target_timespec))) {
  11891. return -TARGET_EFAULT;
  11892. }
  11893. tsp = ts;
  11894. }
  11895. if (!arg2)
  11896. ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
  11897. else {
  11898. if (!(p = lock_user_string(arg2))) {
  11899. return -TARGET_EFAULT;
  11900. }
  11901. ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
  11902. unlock_user(p, arg2, 0);
  11903. }
  11904. }
  11905. return ret;
  11906. #endif
  11907. #ifdef TARGET_NR_utimensat_time64
  11908. case TARGET_NR_utimensat_time64:
  11909. {
  11910. struct timespec *tsp, ts[2];
  11911. if (!arg3) {
  11912. tsp = NULL;
  11913. } else {
  11914. if (target_to_host_timespec64(ts, arg3)) {
  11915. return -TARGET_EFAULT;
  11916. }
  11917. if (target_to_host_timespec64(ts + 1, arg3 +
  11918. sizeof(struct target__kernel_timespec))) {
  11919. return -TARGET_EFAULT;
  11920. }
  11921. tsp = ts;
  11922. }
  11923. if (!arg2)
  11924. ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
  11925. else {
  11926. p = lock_user_string(arg2);
  11927. if (!p) {
  11928. return -TARGET_EFAULT;
  11929. }
  11930. ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
  11931. unlock_user(p, arg2, 0);
  11932. }
  11933. }
  11934. return ret;
  11935. #endif
  11936. #ifdef TARGET_NR_futex
  11937. case TARGET_NR_futex:
  11938. return do_futex(cpu, false, arg1, arg2, arg3, arg4, arg5, arg6);
  11939. #endif
  11940. #ifdef TARGET_NR_futex_time64
  11941. case TARGET_NR_futex_time64:
  11942. return do_futex(cpu, true, arg1, arg2, arg3, arg4, arg5, arg6);
  11943. #endif
  11944. #ifdef CONFIG_INOTIFY
  11945. #if defined(TARGET_NR_inotify_init)
  11946. case TARGET_NR_inotify_init:
  11947. ret = get_errno(inotify_init());
  11948. if (ret >= 0) {
  11949. fd_trans_register(ret, &target_inotify_trans);
  11950. }
  11951. return ret;
  11952. #endif
  11953. #if defined(TARGET_NR_inotify_init1) && defined(CONFIG_INOTIFY1)
  11954. case TARGET_NR_inotify_init1:
  11955. ret = get_errno(inotify_init1(target_to_host_bitmask(arg1,
  11956. fcntl_flags_tbl)));
  11957. if (ret >= 0) {
  11958. fd_trans_register(ret, &target_inotify_trans);
  11959. }
  11960. return ret;
  11961. #endif
  11962. #if defined(TARGET_NR_inotify_add_watch)
  11963. case TARGET_NR_inotify_add_watch:
  11964. p = lock_user_string(arg2);
  11965. ret = get_errno(inotify_add_watch(arg1, path(p), arg3));
  11966. unlock_user(p, arg2, 0);
  11967. return ret;
  11968. #endif
  11969. #if defined(TARGET_NR_inotify_rm_watch)
  11970. case TARGET_NR_inotify_rm_watch:
  11971. return get_errno(inotify_rm_watch(arg1, arg2));
  11972. #endif
  11973. #endif
  11974. #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
  11975. case TARGET_NR_mq_open:
  11976. {
  11977. struct mq_attr posix_mq_attr;
  11978. struct mq_attr *pposix_mq_attr;
  11979. int host_flags;
  11980. host_flags = target_to_host_bitmask(arg2, fcntl_flags_tbl);
  11981. pposix_mq_attr = NULL;
  11982. if (arg4) {
  11983. if (copy_from_user_mq_attr(&posix_mq_attr, arg4) != 0) {
  11984. return -TARGET_EFAULT;
  11985. }
  11986. pposix_mq_attr = &posix_mq_attr;
  11987. }
  11988. p = lock_user_string(arg1 - 1);
  11989. if (!p) {
  11990. return -TARGET_EFAULT;
  11991. }
  11992. ret = get_errno(mq_open(p, host_flags, arg3, pposix_mq_attr));
  11993. unlock_user (p, arg1, 0);
  11994. }
  11995. return ret;
  11996. case TARGET_NR_mq_unlink:
  11997. p = lock_user_string(arg1 - 1);
  11998. if (!p) {
  11999. return -TARGET_EFAULT;
  12000. }
  12001. ret = get_errno(mq_unlink(p));
  12002. unlock_user (p, arg1, 0);
  12003. return ret;
  12004. #ifdef TARGET_NR_mq_timedsend
  12005. case TARGET_NR_mq_timedsend:
  12006. {
  12007. struct timespec ts;
  12008. p = lock_user (VERIFY_READ, arg2, arg3, 1);
  12009. if (arg5 != 0) {
  12010. if (target_to_host_timespec(&ts, arg5)) {
  12011. return -TARGET_EFAULT;
  12012. }
  12013. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts));
  12014. if (!is_error(ret) && host_to_target_timespec(arg5, &ts)) {
  12015. return -TARGET_EFAULT;
  12016. }
  12017. } else {
  12018. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL));
  12019. }
  12020. unlock_user (p, arg2, arg3);
  12021. }
  12022. return ret;
  12023. #endif
  12024. #ifdef TARGET_NR_mq_timedsend_time64
  12025. case TARGET_NR_mq_timedsend_time64:
  12026. {
  12027. struct timespec ts;
  12028. p = lock_user(VERIFY_READ, arg2, arg3, 1);
  12029. if (arg5 != 0) {
  12030. if (target_to_host_timespec64(&ts, arg5)) {
  12031. return -TARGET_EFAULT;
  12032. }
  12033. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts));
  12034. if (!is_error(ret) && host_to_target_timespec64(arg5, &ts)) {
  12035. return -TARGET_EFAULT;
  12036. }
  12037. } else {
  12038. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL));
  12039. }
  12040. unlock_user(p, arg2, arg3);
  12041. }
  12042. return ret;
  12043. #endif
  12044. #ifdef TARGET_NR_mq_timedreceive
  12045. case TARGET_NR_mq_timedreceive:
  12046. {
  12047. struct timespec ts;
  12048. unsigned int prio;
  12049. p = lock_user (VERIFY_READ, arg2, arg3, 1);
  12050. if (arg5 != 0) {
  12051. if (target_to_host_timespec(&ts, arg5)) {
  12052. return -TARGET_EFAULT;
  12053. }
  12054. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  12055. &prio, &ts));
  12056. if (!is_error(ret) && host_to_target_timespec(arg5, &ts)) {
  12057. return -TARGET_EFAULT;
  12058. }
  12059. } else {
  12060. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  12061. &prio, NULL));
  12062. }
  12063. unlock_user (p, arg2, arg3);
  12064. if (arg4 != 0)
  12065. put_user_u32(prio, arg4);
  12066. }
  12067. return ret;
  12068. #endif
  12069. #ifdef TARGET_NR_mq_timedreceive_time64
  12070. case TARGET_NR_mq_timedreceive_time64:
  12071. {
  12072. struct timespec ts;
  12073. unsigned int prio;
  12074. p = lock_user(VERIFY_READ, arg2, arg3, 1);
  12075. if (arg5 != 0) {
  12076. if (target_to_host_timespec64(&ts, arg5)) {
  12077. return -TARGET_EFAULT;
  12078. }
  12079. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  12080. &prio, &ts));
  12081. if (!is_error(ret) && host_to_target_timespec64(arg5, &ts)) {
  12082. return -TARGET_EFAULT;
  12083. }
  12084. } else {
  12085. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  12086. &prio, NULL));
  12087. }
  12088. unlock_user(p, arg2, arg3);
  12089. if (arg4 != 0) {
  12090. put_user_u32(prio, arg4);
  12091. }
  12092. }
  12093. return ret;
  12094. #endif
  12095. /* Not implemented for now... */
  12096. /* case TARGET_NR_mq_notify: */
  12097. /* break; */
  12098. case TARGET_NR_mq_getsetattr:
  12099. {
  12100. struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
  12101. ret = 0;
  12102. if (arg2 != 0) {
  12103. copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
  12104. ret = get_errno(mq_setattr(arg1, &posix_mq_attr_in,
  12105. &posix_mq_attr_out));
  12106. } else if (arg3 != 0) {
  12107. ret = get_errno(mq_getattr(arg1, &posix_mq_attr_out));
  12108. }
  12109. if (ret == 0 && arg3 != 0) {
  12110. copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
  12111. }
  12112. }
  12113. return ret;
  12114. #endif
  12115. #ifdef CONFIG_SPLICE
  12116. #ifdef TARGET_NR_tee
  12117. case TARGET_NR_tee:
  12118. {
  12119. ret = get_errno(tee(arg1,arg2,arg3,arg4));
  12120. }
  12121. return ret;
  12122. #endif
  12123. #ifdef TARGET_NR_splice
  12124. case TARGET_NR_splice:
  12125. {
  12126. loff_t loff_in, loff_out;
  12127. loff_t *ploff_in = NULL, *ploff_out = NULL;
  12128. if (arg2) {
  12129. if (get_user_u64(loff_in, arg2)) {
  12130. return -TARGET_EFAULT;
  12131. }
  12132. ploff_in = &loff_in;
  12133. }
  12134. if (arg4) {
  12135. if (get_user_u64(loff_out, arg4)) {
  12136. return -TARGET_EFAULT;
  12137. }
  12138. ploff_out = &loff_out;
  12139. }
  12140. ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
  12141. if (arg2) {
  12142. if (put_user_u64(loff_in, arg2)) {
  12143. return -TARGET_EFAULT;
  12144. }
  12145. }
  12146. if (arg4) {
  12147. if (put_user_u64(loff_out, arg4)) {
  12148. return -TARGET_EFAULT;
  12149. }
  12150. }
  12151. }
  12152. return ret;
  12153. #endif
  12154. #ifdef TARGET_NR_vmsplice
  12155. case TARGET_NR_vmsplice:
  12156. {
  12157. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  12158. if (vec != NULL) {
  12159. ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
  12160. unlock_iovec(vec, arg2, arg3, 0);
  12161. } else {
  12162. ret = -host_to_target_errno(errno);
  12163. }
  12164. }
  12165. return ret;
  12166. #endif
  12167. #endif /* CONFIG_SPLICE */
  12168. #ifdef CONFIG_EVENTFD
  12169. #if defined(TARGET_NR_eventfd)
  12170. case TARGET_NR_eventfd:
  12171. ret = get_errno(eventfd(arg1, 0));
  12172. if (ret >= 0) {
  12173. fd_trans_register(ret, &target_eventfd_trans);
  12174. }
  12175. return ret;
  12176. #endif
  12177. #if defined(TARGET_NR_eventfd2)
  12178. case TARGET_NR_eventfd2:
  12179. {
  12180. int host_flags = arg2 & (~(TARGET_O_NONBLOCK_MASK | TARGET_O_CLOEXEC));
  12181. if (arg2 & TARGET_O_NONBLOCK) {
  12182. host_flags |= O_NONBLOCK;
  12183. }
  12184. if (arg2 & TARGET_O_CLOEXEC) {
  12185. host_flags |= O_CLOEXEC;
  12186. }
  12187. ret = get_errno(eventfd(arg1, host_flags));
  12188. if (ret >= 0) {
  12189. fd_trans_register(ret, &target_eventfd_trans);
  12190. }
  12191. return ret;
  12192. }
  12193. #endif
  12194. #endif /* CONFIG_EVENTFD */
  12195. #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
  12196. case TARGET_NR_fallocate:
  12197. #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
  12198. ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
  12199. target_offset64(arg5, arg6)));
  12200. #else
  12201. ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
  12202. #endif
  12203. return ret;
  12204. #endif
  12205. #if defined(CONFIG_SYNC_FILE_RANGE)
  12206. #if defined(TARGET_NR_sync_file_range)
  12207. case TARGET_NR_sync_file_range:
  12208. #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
  12209. #if defined(TARGET_MIPS)
  12210. ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
  12211. target_offset64(arg5, arg6), arg7));
  12212. #else
  12213. ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
  12214. target_offset64(arg4, arg5), arg6));
  12215. #endif /* !TARGET_MIPS */
  12216. #else
  12217. ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
  12218. #endif
  12219. return ret;
  12220. #endif
  12221. #if defined(TARGET_NR_sync_file_range2) || \
  12222. defined(TARGET_NR_arm_sync_file_range)
  12223. #if defined(TARGET_NR_sync_file_range2)
  12224. case TARGET_NR_sync_file_range2:
  12225. #endif
  12226. #if defined(TARGET_NR_arm_sync_file_range)
  12227. case TARGET_NR_arm_sync_file_range:
  12228. #endif
  12229. /* This is like sync_file_range but the arguments are reordered */
  12230. #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
  12231. ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
  12232. target_offset64(arg5, arg6), arg2));
  12233. #else
  12234. ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
  12235. #endif
  12236. return ret;
  12237. #endif
  12238. #endif
  12239. #if defined(TARGET_NR_signalfd4)
  12240. case TARGET_NR_signalfd4:
  12241. return do_signalfd4(arg1, arg2, arg4);
  12242. #endif
  12243. #if defined(TARGET_NR_signalfd)
  12244. case TARGET_NR_signalfd:
  12245. return do_signalfd4(arg1, arg2, 0);
  12246. #endif
  12247. #if defined(CONFIG_EPOLL)
  12248. #if defined(TARGET_NR_epoll_create)
  12249. case TARGET_NR_epoll_create:
  12250. return get_errno(epoll_create(arg1));
  12251. #endif
  12252. #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
  12253. case TARGET_NR_epoll_create1:
  12254. return get_errno(epoll_create1(target_to_host_bitmask(arg1, fcntl_flags_tbl)));
  12255. #endif
  12256. #if defined(TARGET_NR_epoll_ctl)
  12257. case TARGET_NR_epoll_ctl:
  12258. {
  12259. struct epoll_event ep;
  12260. struct epoll_event *epp = 0;
  12261. if (arg4) {
  12262. if (arg2 != EPOLL_CTL_DEL) {
  12263. struct target_epoll_event *target_ep;
  12264. if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
  12265. return -TARGET_EFAULT;
  12266. }
  12267. ep.events = tswap32(target_ep->events);
  12268. /*
  12269. * The epoll_data_t union is just opaque data to the kernel,
  12270. * so we transfer all 64 bits across and need not worry what
  12271. * actual data type it is.
  12272. */
  12273. ep.data.u64 = tswap64(target_ep->data.u64);
  12274. unlock_user_struct(target_ep, arg4, 0);
  12275. }
  12276. /*
  12277. * before kernel 2.6.9, EPOLL_CTL_DEL operation required a
  12278. * non-null pointer, even though this argument is ignored.
  12279. *
  12280. */
  12281. epp = &ep;
  12282. }
  12283. return get_errno(epoll_ctl(arg1, arg2, arg3, epp));
  12284. }
  12285. #endif
  12286. #if defined(TARGET_NR_epoll_wait) || defined(TARGET_NR_epoll_pwait)
  12287. #if defined(TARGET_NR_epoll_wait)
  12288. case TARGET_NR_epoll_wait:
  12289. #endif
  12290. #if defined(TARGET_NR_epoll_pwait)
  12291. case TARGET_NR_epoll_pwait:
  12292. #endif
  12293. {
  12294. struct target_epoll_event *target_ep;
  12295. struct epoll_event *ep;
  12296. int epfd = arg1;
  12297. int maxevents = arg3;
  12298. int timeout = arg4;
  12299. if (maxevents <= 0 || maxevents > TARGET_EP_MAX_EVENTS) {
  12300. return -TARGET_EINVAL;
  12301. }
  12302. target_ep = lock_user(VERIFY_WRITE, arg2,
  12303. maxevents * sizeof(struct target_epoll_event), 1);
  12304. if (!target_ep) {
  12305. return -TARGET_EFAULT;
  12306. }
  12307. ep = g_try_new(struct epoll_event, maxevents);
  12308. if (!ep) {
  12309. unlock_user(target_ep, arg2, 0);
  12310. return -TARGET_ENOMEM;
  12311. }
  12312. switch (num) {
  12313. #if defined(TARGET_NR_epoll_pwait)
  12314. case TARGET_NR_epoll_pwait:
  12315. {
  12316. sigset_t *set = NULL;
  12317. if (arg5) {
  12318. ret = process_sigsuspend_mask(&set, arg5, arg6);
  12319. if (ret != 0) {
  12320. break;
  12321. }
  12322. }
  12323. ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
  12324. set, SIGSET_T_SIZE));
  12325. if (set) {
  12326. finish_sigsuspend_mask(ret);
  12327. }
  12328. break;
  12329. }
  12330. #endif
  12331. #if defined(TARGET_NR_epoll_wait)
  12332. case TARGET_NR_epoll_wait:
  12333. ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
  12334. NULL, 0));
  12335. break;
  12336. #endif
  12337. default:
  12338. ret = -TARGET_ENOSYS;
  12339. }
  12340. if (!is_error(ret)) {
  12341. int i;
  12342. for (i = 0; i < ret; i++) {
  12343. target_ep[i].events = tswap32(ep[i].events);
  12344. target_ep[i].data.u64 = tswap64(ep[i].data.u64);
  12345. }
  12346. unlock_user(target_ep, arg2,
  12347. ret * sizeof(struct target_epoll_event));
  12348. } else {
  12349. unlock_user(target_ep, arg2, 0);
  12350. }
  12351. g_free(ep);
  12352. return ret;
  12353. }
  12354. #endif
  12355. #endif
  12356. #ifdef TARGET_NR_prlimit64
  12357. case TARGET_NR_prlimit64:
  12358. {
  12359. /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
  12360. struct target_rlimit64 *target_rnew, *target_rold;
  12361. struct host_rlimit64 rnew, rold, *rnewp = 0;
  12362. int resource = target_to_host_resource(arg2);
  12363. if (arg3 && (resource != RLIMIT_AS &&
  12364. resource != RLIMIT_DATA &&
  12365. resource != RLIMIT_STACK)) {
  12366. if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
  12367. return -TARGET_EFAULT;
  12368. }
  12369. __get_user(rnew.rlim_cur, &target_rnew->rlim_cur);
  12370. __get_user(rnew.rlim_max, &target_rnew->rlim_max);
  12371. unlock_user_struct(target_rnew, arg3, 0);
  12372. rnewp = &rnew;
  12373. }
  12374. ret = get_errno(sys_prlimit64(arg1, resource, rnewp, arg4 ? &rold : 0));
  12375. if (!is_error(ret) && arg4) {
  12376. if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
  12377. return -TARGET_EFAULT;
  12378. }
  12379. __put_user(rold.rlim_cur, &target_rold->rlim_cur);
  12380. __put_user(rold.rlim_max, &target_rold->rlim_max);
  12381. unlock_user_struct(target_rold, arg4, 1);
  12382. }
  12383. return ret;
  12384. }
  12385. #endif
  12386. #ifdef TARGET_NR_gethostname
  12387. case TARGET_NR_gethostname:
  12388. {
  12389. char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
  12390. if (name) {
  12391. ret = get_errno(gethostname(name, arg2));
  12392. unlock_user(name, arg1, arg2);
  12393. } else {
  12394. ret = -TARGET_EFAULT;
  12395. }
  12396. return ret;
  12397. }
  12398. #endif
  12399. #ifdef TARGET_NR_atomic_cmpxchg_32
  12400. case TARGET_NR_atomic_cmpxchg_32:
  12401. {
  12402. /* should use start_exclusive from main.c */
  12403. abi_ulong mem_value;
  12404. if (get_user_u32(mem_value, arg6)) {
  12405. target_siginfo_t info;
  12406. info.si_signo = SIGSEGV;
  12407. info.si_errno = 0;
  12408. info.si_code = TARGET_SEGV_MAPERR;
  12409. info._sifields._sigfault._addr = arg6;
  12410. queue_signal(cpu_env, info.si_signo, QEMU_SI_FAULT, &info);
  12411. ret = 0xdeadbeef;
  12412. }
  12413. if (mem_value == arg2)
  12414. put_user_u32(arg1, arg6);
  12415. return mem_value;
  12416. }
  12417. #endif
  12418. #ifdef TARGET_NR_atomic_barrier
  12419. case TARGET_NR_atomic_barrier:
  12420. /* Like the kernel implementation and the
  12421. qemu arm barrier, no-op this? */
  12422. return 0;
  12423. #endif
  12424. #ifdef TARGET_NR_timer_create
  12425. case TARGET_NR_timer_create:
  12426. {
  12427. /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
  12428. struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL;
  12429. int clkid = arg1;
  12430. int timer_index = next_free_host_timer();
  12431. if (timer_index < 0) {
  12432. ret = -TARGET_EAGAIN;
  12433. } else {
  12434. timer_t *phtimer = g_posix_timers + timer_index;
  12435. if (arg2) {
  12436. phost_sevp = &host_sevp;
  12437. ret = target_to_host_sigevent(phost_sevp, arg2);
  12438. if (ret != 0) {
  12439. free_host_timer_slot(timer_index);
  12440. return ret;
  12441. }
  12442. }
  12443. ret = get_errno(timer_create(clkid, phost_sevp, phtimer));
  12444. if (ret) {
  12445. free_host_timer_slot(timer_index);
  12446. } else {
  12447. if (put_user(TIMER_MAGIC | timer_index, arg3, target_timer_t)) {
  12448. timer_delete(*phtimer);
  12449. free_host_timer_slot(timer_index);
  12450. return -TARGET_EFAULT;
  12451. }
  12452. }
  12453. }
  12454. return ret;
  12455. }
  12456. #endif
  12457. #ifdef TARGET_NR_timer_settime
  12458. case TARGET_NR_timer_settime:
  12459. {
  12460. /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
  12461. * struct itimerspec * old_value */
  12462. target_timer_t timerid = get_timer_id(arg1);
  12463. if (timerid < 0) {
  12464. ret = timerid;
  12465. } else if (arg3 == 0) {
  12466. ret = -TARGET_EINVAL;
  12467. } else {
  12468. timer_t htimer = g_posix_timers[timerid];
  12469. struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
  12470. if (target_to_host_itimerspec(&hspec_new, arg3)) {
  12471. return -TARGET_EFAULT;
  12472. }
  12473. ret = get_errno(
  12474. timer_settime(htimer, arg2, &hspec_new, &hspec_old));
  12475. if (arg4 && host_to_target_itimerspec(arg4, &hspec_old)) {
  12476. return -TARGET_EFAULT;
  12477. }
  12478. }
  12479. return ret;
  12480. }
  12481. #endif
  12482. #ifdef TARGET_NR_timer_settime64
  12483. case TARGET_NR_timer_settime64:
  12484. {
  12485. target_timer_t timerid = get_timer_id(arg1);
  12486. if (timerid < 0) {
  12487. ret = timerid;
  12488. } else if (arg3 == 0) {
  12489. ret = -TARGET_EINVAL;
  12490. } else {
  12491. timer_t htimer = g_posix_timers[timerid];
  12492. struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
  12493. if (target_to_host_itimerspec64(&hspec_new, arg3)) {
  12494. return -TARGET_EFAULT;
  12495. }
  12496. ret = get_errno(
  12497. timer_settime(htimer, arg2, &hspec_new, &hspec_old));
  12498. if (arg4 && host_to_target_itimerspec64(arg4, &hspec_old)) {
  12499. return -TARGET_EFAULT;
  12500. }
  12501. }
  12502. return ret;
  12503. }
  12504. #endif
  12505. #ifdef TARGET_NR_timer_gettime
  12506. case TARGET_NR_timer_gettime:
  12507. {
  12508. /* args: timer_t timerid, struct itimerspec *curr_value */
  12509. target_timer_t timerid = get_timer_id(arg1);
  12510. if (timerid < 0) {
  12511. ret = timerid;
  12512. } else if (!arg2) {
  12513. ret = -TARGET_EFAULT;
  12514. } else {
  12515. timer_t htimer = g_posix_timers[timerid];
  12516. struct itimerspec hspec;
  12517. ret = get_errno(timer_gettime(htimer, &hspec));
  12518. if (host_to_target_itimerspec(arg2, &hspec)) {
  12519. ret = -TARGET_EFAULT;
  12520. }
  12521. }
  12522. return ret;
  12523. }
  12524. #endif
  12525. #ifdef TARGET_NR_timer_gettime64
  12526. case TARGET_NR_timer_gettime64:
  12527. {
  12528. /* args: timer_t timerid, struct itimerspec64 *curr_value */
  12529. target_timer_t timerid = get_timer_id(arg1);
  12530. if (timerid < 0) {
  12531. ret = timerid;
  12532. } else if (!arg2) {
  12533. ret = -TARGET_EFAULT;
  12534. } else {
  12535. timer_t htimer = g_posix_timers[timerid];
  12536. struct itimerspec hspec;
  12537. ret = get_errno(timer_gettime(htimer, &hspec));
  12538. if (host_to_target_itimerspec64(arg2, &hspec)) {
  12539. ret = -TARGET_EFAULT;
  12540. }
  12541. }
  12542. return ret;
  12543. }
  12544. #endif
  12545. #ifdef TARGET_NR_timer_getoverrun
  12546. case TARGET_NR_timer_getoverrun:
  12547. {
  12548. /* args: timer_t timerid */
  12549. target_timer_t timerid = get_timer_id(arg1);
  12550. if (timerid < 0) {
  12551. ret = timerid;
  12552. } else {
  12553. timer_t htimer = g_posix_timers[timerid];
  12554. ret = get_errno(timer_getoverrun(htimer));
  12555. }
  12556. return ret;
  12557. }
  12558. #endif
  12559. #ifdef TARGET_NR_timer_delete
  12560. case TARGET_NR_timer_delete:
  12561. {
  12562. /* args: timer_t timerid */
  12563. target_timer_t timerid = get_timer_id(arg1);
  12564. if (timerid < 0) {
  12565. ret = timerid;
  12566. } else {
  12567. timer_t htimer = g_posix_timers[timerid];
  12568. ret = get_errno(timer_delete(htimer));
  12569. free_host_timer_slot(timerid);
  12570. }
  12571. return ret;
  12572. }
  12573. #endif
  12574. #if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
  12575. case TARGET_NR_timerfd_create:
  12576. ret = get_errno(timerfd_create(arg1,
  12577. target_to_host_bitmask(arg2, fcntl_flags_tbl)));
  12578. if (ret >= 0) {
  12579. fd_trans_register(ret, &target_timerfd_trans);
  12580. }
  12581. return ret;
  12582. #endif
  12583. #if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
  12584. case TARGET_NR_timerfd_gettime:
  12585. {
  12586. struct itimerspec its_curr;
  12587. ret = get_errno(timerfd_gettime(arg1, &its_curr));
  12588. if (arg2 && host_to_target_itimerspec(arg2, &its_curr)) {
  12589. return -TARGET_EFAULT;
  12590. }
  12591. }
  12592. return ret;
  12593. #endif
  12594. #if defined(TARGET_NR_timerfd_gettime64) && defined(CONFIG_TIMERFD)
  12595. case TARGET_NR_timerfd_gettime64:
  12596. {
  12597. struct itimerspec its_curr;
  12598. ret = get_errno(timerfd_gettime(arg1, &its_curr));
  12599. if (arg2 && host_to_target_itimerspec64(arg2, &its_curr)) {
  12600. return -TARGET_EFAULT;
  12601. }
  12602. }
  12603. return ret;
  12604. #endif
  12605. #if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
  12606. case TARGET_NR_timerfd_settime:
  12607. {
  12608. struct itimerspec its_new, its_old, *p_new;
  12609. if (arg3) {
  12610. if (target_to_host_itimerspec(&its_new, arg3)) {
  12611. return -TARGET_EFAULT;
  12612. }
  12613. p_new = &its_new;
  12614. } else {
  12615. p_new = NULL;
  12616. }
  12617. ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
  12618. if (arg4 && host_to_target_itimerspec(arg4, &its_old)) {
  12619. return -TARGET_EFAULT;
  12620. }
  12621. }
  12622. return ret;
  12623. #endif
  12624. #if defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)
  12625. case TARGET_NR_timerfd_settime64:
  12626. {
  12627. struct itimerspec its_new, its_old, *p_new;
  12628. if (arg3) {
  12629. if (target_to_host_itimerspec64(&its_new, arg3)) {
  12630. return -TARGET_EFAULT;
  12631. }
  12632. p_new = &its_new;
  12633. } else {
  12634. p_new = NULL;
  12635. }
  12636. ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
  12637. if (arg4 && host_to_target_itimerspec64(arg4, &its_old)) {
  12638. return -TARGET_EFAULT;
  12639. }
  12640. }
  12641. return ret;
  12642. #endif
  12643. #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
  12644. case TARGET_NR_ioprio_get:
  12645. return get_errno(ioprio_get(arg1, arg2));
  12646. #endif
  12647. #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
  12648. case TARGET_NR_ioprio_set:
  12649. return get_errno(ioprio_set(arg1, arg2, arg3));
  12650. #endif
  12651. #if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
  12652. case TARGET_NR_setns:
  12653. return get_errno(setns(arg1, arg2));
  12654. #endif
  12655. #if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
  12656. case TARGET_NR_unshare:
  12657. return get_errno(unshare(arg1));
  12658. #endif
  12659. #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
  12660. case TARGET_NR_kcmp:
  12661. return get_errno(kcmp(arg1, arg2, arg3, arg4, arg5));
  12662. #endif
  12663. #ifdef TARGET_NR_swapcontext
  12664. case TARGET_NR_swapcontext:
  12665. /* PowerPC specific. */
  12666. return do_swapcontext(cpu_env, arg1, arg2, arg3);
  12667. #endif
  12668. #ifdef TARGET_NR_memfd_create
  12669. case TARGET_NR_memfd_create:
  12670. p = lock_user_string(arg1);
  12671. if (!p) {
  12672. return -TARGET_EFAULT;
  12673. }
  12674. ret = get_errno(memfd_create(p, arg2));
  12675. fd_trans_unregister(ret);
  12676. unlock_user(p, arg1, 0);
  12677. return ret;
  12678. #endif
  12679. #if defined TARGET_NR_membarrier && defined __NR_membarrier
  12680. case TARGET_NR_membarrier:
  12681. return get_errno(membarrier(arg1, arg2));
  12682. #endif
  12683. #if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range)
  12684. case TARGET_NR_copy_file_range:
  12685. {
  12686. loff_t inoff, outoff;
  12687. loff_t *pinoff = NULL, *poutoff = NULL;
  12688. if (arg2) {
  12689. if (get_user_u64(inoff, arg2)) {
  12690. return -TARGET_EFAULT;
  12691. }
  12692. pinoff = &inoff;
  12693. }
  12694. if (arg4) {
  12695. if (get_user_u64(outoff, arg4)) {
  12696. return -TARGET_EFAULT;
  12697. }
  12698. poutoff = &outoff;
  12699. }
  12700. /* Do not sign-extend the count parameter. */
  12701. ret = get_errno(safe_copy_file_range(arg1, pinoff, arg3, poutoff,
  12702. (abi_ulong)arg5, arg6));
  12703. if (!is_error(ret) && ret > 0) {
  12704. if (arg2) {
  12705. if (put_user_u64(inoff, arg2)) {
  12706. return -TARGET_EFAULT;
  12707. }
  12708. }
  12709. if (arg4) {
  12710. if (put_user_u64(outoff, arg4)) {
  12711. return -TARGET_EFAULT;
  12712. }
  12713. }
  12714. }
  12715. }
  12716. return ret;
  12717. #endif
  12718. #if defined(TARGET_NR_pivot_root)
  12719. case TARGET_NR_pivot_root:
  12720. {
  12721. void *p2;
  12722. p = lock_user_string(arg1); /* new_root */
  12723. p2 = lock_user_string(arg2); /* put_old */
  12724. if (!p || !p2) {
  12725. ret = -TARGET_EFAULT;
  12726. } else {
  12727. ret = get_errno(pivot_root(p, p2));
  12728. }
  12729. unlock_user(p2, arg2, 0);
  12730. unlock_user(p, arg1, 0);
  12731. }
  12732. return ret;
  12733. #endif
  12734. #if defined(TARGET_NR_riscv_hwprobe)
  12735. case TARGET_NR_riscv_hwprobe:
  12736. return do_riscv_hwprobe(cpu_env, arg1, arg2, arg3, arg4, arg5);
  12737. #endif
  12738. default:
  12739. qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num);
  12740. return -TARGET_ENOSYS;
  12741. }
  12742. return ret;
  12743. }
  12744. abi_long do_syscall(CPUArchState *cpu_env, int num, abi_long arg1,
  12745. abi_long arg2, abi_long arg3, abi_long arg4,
  12746. abi_long arg5, abi_long arg6, abi_long arg7,
  12747. abi_long arg8)
  12748. {
  12749. CPUState *cpu = env_cpu(cpu_env);
  12750. abi_long ret;
  12751. #ifdef DEBUG_ERESTARTSYS
  12752. /* Debug-only code for exercising the syscall-restart code paths
  12753. * in the per-architecture cpu main loops: restart every syscall
  12754. * the guest makes once before letting it through.
  12755. */
  12756. {
  12757. static bool flag;
  12758. flag = !flag;
  12759. if (flag) {
  12760. return -QEMU_ERESTARTSYS;
  12761. }
  12762. }
  12763. #endif
  12764. record_syscall_start(cpu, num, arg1,
  12765. arg2, arg3, arg4, arg5, arg6, arg7, arg8);
  12766. if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
  12767. print_syscall(cpu_env, num, arg1, arg2, arg3, arg4, arg5, arg6);
  12768. }
  12769. ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4,
  12770. arg5, arg6, arg7, arg8);
  12771. if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
  12772. print_syscall_ret(cpu_env, num, ret, arg1, arg2,
  12773. arg3, arg4, arg5, arg6);
  12774. }
  12775. record_syscall_return(cpu, num, ret);
  12776. return ret;
  12777. }