syscall.c 420 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336
  1. /*
  2. * Linux syscalls
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #define _ATFILE_SOURCE
  20. #include "qemu/osdep.h"
  21. #include "qemu/cutils.h"
  22. #include "qemu/path.h"
  23. #include "qemu/memfd.h"
  24. #include "qemu/queue.h"
  25. #include <elf.h>
  26. #include <endian.h>
  27. #include <grp.h>
  28. #include <sys/ipc.h>
  29. #include <sys/msg.h>
  30. #include <sys/wait.h>
  31. #include <sys/mount.h>
  32. #include <sys/file.h>
  33. #include <sys/fsuid.h>
  34. #include <sys/personality.h>
  35. #include <sys/prctl.h>
  36. #include <sys/resource.h>
  37. #include <sys/swap.h>
  38. #include <linux/capability.h>
  39. #include <sched.h>
  40. #include <sys/timex.h>
  41. #include <sys/socket.h>
  42. #include <linux/sockios.h>
  43. #include <sys/un.h>
  44. #include <sys/uio.h>
  45. #include <poll.h>
  46. #include <sys/times.h>
  47. #include <sys/shm.h>
  48. #include <sys/sem.h>
  49. #include <sys/statfs.h>
  50. #include <utime.h>
  51. #include <sys/sysinfo.h>
  52. #include <sys/signalfd.h>
  53. //#include <sys/user.h>
  54. #include <netinet/in.h>
  55. #include <netinet/ip.h>
  56. #include <netinet/tcp.h>
  57. #include <netinet/udp.h>
  58. #include <linux/wireless.h>
  59. #include <linux/icmp.h>
  60. #include <linux/icmpv6.h>
  61. #include <linux/if_tun.h>
  62. #include <linux/in6.h>
  63. #include <linux/errqueue.h>
  64. #include <linux/random.h>
  65. #ifdef CONFIG_TIMERFD
  66. #include <sys/timerfd.h>
  67. #endif
  68. #ifdef CONFIG_EVENTFD
  69. #include <sys/eventfd.h>
  70. #endif
  71. #ifdef CONFIG_EPOLL
  72. #include <sys/epoll.h>
  73. #endif
  74. #ifdef CONFIG_ATTR
  75. #include "qemu/xattr.h"
  76. #endif
  77. #ifdef CONFIG_SENDFILE
  78. #include <sys/sendfile.h>
  79. #endif
  80. #ifdef HAVE_SYS_KCOV_H
  81. #include <sys/kcov.h>
  82. #endif
  83. #define termios host_termios
  84. #define winsize host_winsize
  85. #define termio host_termio
  86. #define sgttyb host_sgttyb /* same as target */
  87. #define tchars host_tchars /* same as target */
  88. #define ltchars host_ltchars /* same as target */
  89. #include <linux/termios.h>
  90. #include <linux/unistd.h>
  91. #include <linux/cdrom.h>
  92. #include <linux/hdreg.h>
  93. #include <linux/soundcard.h>
  94. #include <linux/kd.h>
  95. #include <linux/mtio.h>
  96. #include <linux/fs.h>
  97. #include <linux/fd.h>
  98. #if defined(CONFIG_FIEMAP)
  99. #include <linux/fiemap.h>
  100. #endif
  101. #include <linux/fb.h>
  102. #if defined(CONFIG_USBFS)
  103. #include <linux/usbdevice_fs.h>
  104. #include <linux/usb/ch9.h>
  105. #endif
  106. #include <linux/vt.h>
  107. #include <linux/dm-ioctl.h>
  108. #include <linux/reboot.h>
  109. #include <linux/route.h>
  110. #include <linux/filter.h>
  111. #include <linux/blkpg.h>
  112. #include <netpacket/packet.h>
  113. #include <linux/netlink.h>
  114. #include <linux/if_alg.h>
  115. #include <linux/rtc.h>
  116. #include <sound/asound.h>
  117. #ifdef HAVE_BTRFS_H
  118. #include <linux/btrfs.h>
  119. #endif
  120. #ifdef HAVE_DRM_H
  121. #include <libdrm/drm.h>
  122. #include <libdrm/i915_drm.h>
  123. #endif
  124. #include "linux_loop.h"
  125. #include "uname.h"
  126. #include "qemu.h"
  127. #include "user-internals.h"
  128. #include "strace.h"
  129. #include "signal-common.h"
  130. #include "loader.h"
  131. #include "user-mmap.h"
  132. #include "user/safe-syscall.h"
  133. #include "qemu/guest-random.h"
  134. #include "qemu/selfmap.h"
  135. #include "user/syscall-trace.h"
  136. #include "special-errno.h"
  137. #include "qapi/error.h"
  138. #include "fd-trans.h"
  139. #include "tcg/tcg.h"
  140. #include "cpu_loop-common.h"
  141. #ifndef CLONE_IO
  142. #define CLONE_IO 0x80000000 /* Clone io context */
  143. #endif
  144. /* We can't directly call the host clone syscall, because this will
  145. * badly confuse libc (breaking mutexes, for example). So we must
  146. * divide clone flags into:
  147. * * flag combinations that look like pthread_create()
  148. * * flag combinations that look like fork()
  149. * * flags we can implement within QEMU itself
  150. * * flags we can't support and will return an error for
  151. */
  152. /* For thread creation, all these flags must be present; for
  153. * fork, none must be present.
  154. */
  155. #define CLONE_THREAD_FLAGS \
  156. (CLONE_VM | CLONE_FS | CLONE_FILES | \
  157. CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM)
  158. /* These flags are ignored:
  159. * CLONE_DETACHED is now ignored by the kernel;
  160. * CLONE_IO is just an optimisation hint to the I/O scheduler
  161. */
  162. #define CLONE_IGNORED_FLAGS \
  163. (CLONE_DETACHED | CLONE_IO)
  164. /* Flags for fork which we can implement within QEMU itself */
  165. #define CLONE_OPTIONAL_FORK_FLAGS \
  166. (CLONE_SETTLS | CLONE_PARENT_SETTID | \
  167. CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID)
  168. /* Flags for thread creation which we can implement within QEMU itself */
  169. #define CLONE_OPTIONAL_THREAD_FLAGS \
  170. (CLONE_SETTLS | CLONE_PARENT_SETTID | \
  171. CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | CLONE_PARENT)
  172. #define CLONE_INVALID_FORK_FLAGS \
  173. (~(CSIGNAL | CLONE_OPTIONAL_FORK_FLAGS | CLONE_IGNORED_FLAGS))
  174. #define CLONE_INVALID_THREAD_FLAGS \
  175. (~(CSIGNAL | CLONE_THREAD_FLAGS | CLONE_OPTIONAL_THREAD_FLAGS | \
  176. CLONE_IGNORED_FLAGS))
  177. /* CLONE_VFORK is special cased early in do_fork(). The other flag bits
  178. * have almost all been allocated. We cannot support any of
  179. * CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC,
  180. * CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, CLONE_PTRACE, CLONE_UNTRACED.
  181. * The checks against the invalid thread masks above will catch these.
  182. * (The one remaining unallocated bit is 0x1000 which used to be CLONE_PID.)
  183. */
  184. /* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
  185. * once. This exercises the codepaths for restart.
  186. */
  187. //#define DEBUG_ERESTARTSYS
  188. //#include <linux/msdos_fs.h>
  189. #define VFAT_IOCTL_READDIR_BOTH \
  190. _IOC(_IOC_READ, 'r', 1, (sizeof(struct linux_dirent) + 256) * 2)
  191. #define VFAT_IOCTL_READDIR_SHORT \
  192. _IOC(_IOC_READ, 'r', 2, (sizeof(struct linux_dirent) + 256) * 2)
  193. #undef _syscall0
  194. #undef _syscall1
  195. #undef _syscall2
  196. #undef _syscall3
  197. #undef _syscall4
  198. #undef _syscall5
  199. #undef _syscall6
  200. #define _syscall0(type,name) \
  201. static type name (void) \
  202. { \
  203. return syscall(__NR_##name); \
  204. }
  205. #define _syscall1(type,name,type1,arg1) \
  206. static type name (type1 arg1) \
  207. { \
  208. return syscall(__NR_##name, arg1); \
  209. }
  210. #define _syscall2(type,name,type1,arg1,type2,arg2) \
  211. static type name (type1 arg1,type2 arg2) \
  212. { \
  213. return syscall(__NR_##name, arg1, arg2); \
  214. }
  215. #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
  216. static type name (type1 arg1,type2 arg2,type3 arg3) \
  217. { \
  218. return syscall(__NR_##name, arg1, arg2, arg3); \
  219. }
  220. #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
  221. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
  222. { \
  223. return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
  224. }
  225. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  226. type5,arg5) \
  227. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
  228. { \
  229. return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
  230. }
  231. #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  232. type5,arg5,type6,arg6) \
  233. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
  234. type6 arg6) \
  235. { \
  236. return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
  237. }
  238. #define __NR_sys_uname __NR_uname
  239. #define __NR_sys_getcwd1 __NR_getcwd
  240. #define __NR_sys_getdents __NR_getdents
  241. #define __NR_sys_getdents64 __NR_getdents64
  242. #define __NR_sys_getpriority __NR_getpriority
  243. #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
  244. #define __NR_sys_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo
  245. #define __NR_sys_syslog __NR_syslog
  246. #if defined(__NR_futex)
  247. # define __NR_sys_futex __NR_futex
  248. #endif
  249. #if defined(__NR_futex_time64)
  250. # define __NR_sys_futex_time64 __NR_futex_time64
  251. #endif
  252. #define __NR_sys_statx __NR_statx
  253. #if defined(__alpha__) || defined(__x86_64__) || defined(__s390x__)
  254. #define __NR__llseek __NR_lseek
  255. #endif
  256. /* Newer kernel ports have llseek() instead of _llseek() */
  257. #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
  258. #define TARGET_NR__llseek TARGET_NR_llseek
  259. #endif
  260. /* some platforms need to mask more bits than just TARGET_O_NONBLOCK */
  261. #ifndef TARGET_O_NONBLOCK_MASK
  262. #define TARGET_O_NONBLOCK_MASK TARGET_O_NONBLOCK
  263. #endif
  264. #define __NR_sys_gettid __NR_gettid
  265. _syscall0(int, sys_gettid)
  266. /* For the 64-bit guest on 32-bit host case we must emulate
  267. * getdents using getdents64, because otherwise the host
  268. * might hand us back more dirent records than we can fit
  269. * into the guest buffer after structure format conversion.
  270. * Otherwise we emulate getdents with getdents if the host has it.
  271. */
  272. #if defined(__NR_getdents) && HOST_LONG_BITS >= TARGET_ABI_BITS
  273. #define EMULATE_GETDENTS_WITH_GETDENTS
  274. #endif
  275. #if defined(TARGET_NR_getdents) && defined(EMULATE_GETDENTS_WITH_GETDENTS)
  276. _syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
  277. #endif
  278. #if (defined(TARGET_NR_getdents) && \
  279. !defined(EMULATE_GETDENTS_WITH_GETDENTS)) || \
  280. (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
  281. _syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
  282. #endif
  283. #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
  284. _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
  285. loff_t *, res, uint, wh);
  286. #endif
  287. _syscall3(int, sys_rt_sigqueueinfo, pid_t, pid, int, sig, siginfo_t *, uinfo)
  288. _syscall4(int, sys_rt_tgsigqueueinfo, pid_t, pid, pid_t, tid, int, sig,
  289. siginfo_t *, uinfo)
  290. _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
  291. #ifdef __NR_exit_group
  292. _syscall1(int,exit_group,int,error_code)
  293. #endif
  294. #if defined(__NR_close_range) && defined(TARGET_NR_close_range)
  295. #define __NR_sys_close_range __NR_close_range
  296. _syscall3(int,sys_close_range,int,first,int,last,int,flags)
  297. #ifndef CLOSE_RANGE_CLOEXEC
  298. #define CLOSE_RANGE_CLOEXEC (1U << 2)
  299. #endif
  300. #endif
  301. #if defined(__NR_futex)
  302. _syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
  303. const struct timespec *,timeout,int *,uaddr2,int,val3)
  304. #endif
  305. #if defined(__NR_futex_time64)
  306. _syscall6(int,sys_futex_time64,int *,uaddr,int,op,int,val,
  307. const struct timespec *,timeout,int *,uaddr2,int,val3)
  308. #endif
  309. #if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
  310. _syscall2(int, pidfd_open, pid_t, pid, unsigned int, flags);
  311. #endif
  312. #if defined(__NR_pidfd_send_signal) && defined(TARGET_NR_pidfd_send_signal)
  313. _syscall4(int, pidfd_send_signal, int, pidfd, int, sig, siginfo_t *, info,
  314. unsigned int, flags);
  315. #endif
  316. #if defined(__NR_pidfd_getfd) && defined(TARGET_NR_pidfd_getfd)
  317. _syscall3(int, pidfd_getfd, int, pidfd, int, targetfd, unsigned int, flags);
  318. #endif
  319. #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
  320. _syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
  321. unsigned long *, user_mask_ptr);
  322. #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
  323. _syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
  324. unsigned long *, user_mask_ptr);
  325. /* sched_attr is not defined in glibc */
  326. struct sched_attr {
  327. uint32_t size;
  328. uint32_t sched_policy;
  329. uint64_t sched_flags;
  330. int32_t sched_nice;
  331. uint32_t sched_priority;
  332. uint64_t sched_runtime;
  333. uint64_t sched_deadline;
  334. uint64_t sched_period;
  335. uint32_t sched_util_min;
  336. uint32_t sched_util_max;
  337. };
  338. #define __NR_sys_sched_getattr __NR_sched_getattr
  339. _syscall4(int, sys_sched_getattr, pid_t, pid, struct sched_attr *, attr,
  340. unsigned int, size, unsigned int, flags);
  341. #define __NR_sys_sched_setattr __NR_sched_setattr
  342. _syscall3(int, sys_sched_setattr, pid_t, pid, struct sched_attr *, attr,
  343. unsigned int, flags);
  344. #define __NR_sys_sched_getscheduler __NR_sched_getscheduler
  345. _syscall1(int, sys_sched_getscheduler, pid_t, pid);
  346. #define __NR_sys_sched_setscheduler __NR_sched_setscheduler
  347. _syscall3(int, sys_sched_setscheduler, pid_t, pid, int, policy,
  348. const struct sched_param *, param);
  349. #define __NR_sys_sched_getparam __NR_sched_getparam
  350. _syscall2(int, sys_sched_getparam, pid_t, pid,
  351. struct sched_param *, param);
  352. #define __NR_sys_sched_setparam __NR_sched_setparam
  353. _syscall2(int, sys_sched_setparam, pid_t, pid,
  354. const struct sched_param *, param);
  355. #define __NR_sys_getcpu __NR_getcpu
  356. _syscall3(int, sys_getcpu, unsigned *, cpu, unsigned *, node, void *, tcache);
  357. _syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
  358. void *, arg);
  359. _syscall2(int, capget, struct __user_cap_header_struct *, header,
  360. struct __user_cap_data_struct *, data);
  361. _syscall2(int, capset, struct __user_cap_header_struct *, header,
  362. struct __user_cap_data_struct *, data);
  363. #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
  364. _syscall2(int, ioprio_get, int, which, int, who)
  365. #endif
  366. #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
  367. _syscall3(int, ioprio_set, int, which, int, who, int, ioprio)
  368. #endif
  369. #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
  370. _syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags)
  371. #endif
  372. #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
  373. _syscall5(int, kcmp, pid_t, pid1, pid_t, pid2, int, type,
  374. unsigned long, idx1, unsigned long, idx2)
  375. #endif
  376. /*
  377. * It is assumed that struct statx is architecture independent.
  378. */
  379. #if defined(TARGET_NR_statx) && defined(__NR_statx)
  380. _syscall5(int, sys_statx, int, dirfd, const char *, pathname, int, flags,
  381. unsigned int, mask, struct target_statx *, statxbuf)
  382. #endif
  383. #if defined(TARGET_NR_membarrier) && defined(__NR_membarrier)
  384. _syscall2(int, membarrier, int, cmd, int, flags)
  385. #endif
  386. static const bitmask_transtbl fcntl_flags_tbl[] = {
  387. { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
  388. { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
  389. { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
  390. { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
  391. { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
  392. { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
  393. { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
  394. { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
  395. { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
  396. { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
  397. { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
  398. { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
  399. { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
  400. #if defined(O_DIRECT)
  401. { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
  402. #endif
  403. #if defined(O_NOATIME)
  404. { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
  405. #endif
  406. #if defined(O_CLOEXEC)
  407. { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
  408. #endif
  409. #if defined(O_PATH)
  410. { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
  411. #endif
  412. #if defined(O_TMPFILE)
  413. { TARGET_O_TMPFILE, TARGET_O_TMPFILE, O_TMPFILE, O_TMPFILE },
  414. #endif
  415. /* Don't terminate the list prematurely on 64-bit host+guest. */
  416. #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
  417. { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
  418. #endif
  419. { 0, 0, 0, 0 }
  420. };
  421. _syscall2(int, sys_getcwd1, char *, buf, size_t, size)
  422. #if defined(TARGET_NR_utimensat) || defined(TARGET_NR_utimensat_time64)
  423. #if defined(__NR_utimensat)
  424. #define __NR_sys_utimensat __NR_utimensat
  425. _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
  426. const struct timespec *,tsp,int,flags)
  427. #else
  428. static int sys_utimensat(int dirfd, const char *pathname,
  429. const struct timespec times[2], int flags)
  430. {
  431. errno = ENOSYS;
  432. return -1;
  433. }
  434. #endif
  435. #endif /* TARGET_NR_utimensat */
  436. #ifdef TARGET_NR_renameat2
  437. #if defined(__NR_renameat2)
  438. #define __NR_sys_renameat2 __NR_renameat2
  439. _syscall5(int, sys_renameat2, int, oldfd, const char *, old, int, newfd,
  440. const char *, new, unsigned int, flags)
  441. #else
  442. static int sys_renameat2(int oldfd, const char *old,
  443. int newfd, const char *new, int flags)
  444. {
  445. if (flags == 0) {
  446. return renameat(oldfd, old, newfd, new);
  447. }
  448. errno = ENOSYS;
  449. return -1;
  450. }
  451. #endif
  452. #endif /* TARGET_NR_renameat2 */
  453. #ifdef CONFIG_INOTIFY
  454. #include <sys/inotify.h>
  455. #else
  456. /* Userspace can usually survive runtime without inotify */
  457. #undef TARGET_NR_inotify_init
  458. #undef TARGET_NR_inotify_init1
  459. #undef TARGET_NR_inotify_add_watch
  460. #undef TARGET_NR_inotify_rm_watch
  461. #endif /* CONFIG_INOTIFY */
  462. #if defined(TARGET_NR_prlimit64)
  463. #ifndef __NR_prlimit64
  464. # define __NR_prlimit64 -1
  465. #endif
  466. #define __NR_sys_prlimit64 __NR_prlimit64
  467. /* The glibc rlimit structure may not be that used by the underlying syscall */
  468. struct host_rlimit64 {
  469. uint64_t rlim_cur;
  470. uint64_t rlim_max;
  471. };
  472. _syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
  473. const struct host_rlimit64 *, new_limit,
  474. struct host_rlimit64 *, old_limit)
  475. #endif
  476. #if defined(TARGET_NR_timer_create)
  477. /* Maximum of 32 active POSIX timers allowed at any one time. */
  478. #define GUEST_TIMER_MAX 32
  479. static timer_t g_posix_timers[GUEST_TIMER_MAX];
  480. static int g_posix_timer_allocated[GUEST_TIMER_MAX];
  481. static inline int next_free_host_timer(void)
  482. {
  483. int k;
  484. for (k = 0; k < ARRAY_SIZE(g_posix_timer_allocated); k++) {
  485. if (qatomic_xchg(g_posix_timer_allocated + k, 1) == 0) {
  486. return k;
  487. }
  488. }
  489. return -1;
  490. }
  491. static inline void free_host_timer_slot(int id)
  492. {
  493. qatomic_store_release(g_posix_timer_allocated + id, 0);
  494. }
  495. #endif
  496. static inline int host_to_target_errno(int host_errno)
  497. {
  498. switch (host_errno) {
  499. #define E(X) case X: return TARGET_##X;
  500. #include "errnos.c.inc"
  501. #undef E
  502. default:
  503. return host_errno;
  504. }
  505. }
  506. static inline int target_to_host_errno(int target_errno)
  507. {
  508. switch (target_errno) {
  509. #define E(X) case TARGET_##X: return X;
  510. #include "errnos.c.inc"
  511. #undef E
  512. default:
  513. return target_errno;
  514. }
  515. }
  516. abi_long get_errno(abi_long ret)
  517. {
  518. if (ret == -1)
  519. return -host_to_target_errno(errno);
  520. else
  521. return ret;
  522. }
  523. const char *target_strerror(int err)
  524. {
  525. if (err == QEMU_ERESTARTSYS) {
  526. return "To be restarted";
  527. }
  528. if (err == QEMU_ESIGRETURN) {
  529. return "Successful exit from sigreturn";
  530. }
  531. return strerror(target_to_host_errno(err));
  532. }
  533. static int check_zeroed_user(abi_long addr, size_t ksize, size_t usize)
  534. {
  535. int i;
  536. uint8_t b;
  537. if (usize <= ksize) {
  538. return 1;
  539. }
  540. for (i = ksize; i < usize; i++) {
  541. if (get_user_u8(b, addr + i)) {
  542. return -TARGET_EFAULT;
  543. }
  544. if (b != 0) {
  545. return 0;
  546. }
  547. }
  548. return 1;
  549. }
  550. #define safe_syscall0(type, name) \
  551. static type safe_##name(void) \
  552. { \
  553. return safe_syscall(__NR_##name); \
  554. }
  555. #define safe_syscall1(type, name, type1, arg1) \
  556. static type safe_##name(type1 arg1) \
  557. { \
  558. return safe_syscall(__NR_##name, arg1); \
  559. }
  560. #define safe_syscall2(type, name, type1, arg1, type2, arg2) \
  561. static type safe_##name(type1 arg1, type2 arg2) \
  562. { \
  563. return safe_syscall(__NR_##name, arg1, arg2); \
  564. }
  565. #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
  566. static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
  567. { \
  568. return safe_syscall(__NR_##name, arg1, arg2, arg3); \
  569. }
  570. #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
  571. type4, arg4) \
  572. static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
  573. { \
  574. return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
  575. }
  576. #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
  577. type4, arg4, type5, arg5) \
  578. static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  579. type5 arg5) \
  580. { \
  581. return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
  582. }
  583. #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
  584. type4, arg4, type5, arg5, type6, arg6) \
  585. static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  586. type5 arg5, type6 arg6) \
  587. { \
  588. return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
  589. }
  590. safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count)
  591. safe_syscall3(ssize_t, write, int, fd, const void *, buff, size_t, count)
  592. safe_syscall4(int, openat, int, dirfd, const char *, pathname, \
  593. int, flags, mode_t, mode)
  594. #if defined(TARGET_NR_wait4) || defined(TARGET_NR_waitpid)
  595. safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \
  596. struct rusage *, rusage)
  597. #endif
  598. safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \
  599. int, options, struct rusage *, rusage)
  600. safe_syscall5(int, execveat, int, dirfd, const char *, filename,
  601. char **, argv, char **, envp, int, flags)
  602. #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
  603. defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
  604. safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \
  605. fd_set *, exceptfds, struct timespec *, timeout, void *, sig)
  606. #endif
  607. #if defined(TARGET_NR_ppoll) || defined(TARGET_NR_ppoll_time64)
  608. safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds,
  609. struct timespec *, tsp, const sigset_t *, sigmask,
  610. size_t, sigsetsize)
  611. #endif
  612. safe_syscall6(int, epoll_pwait, int, epfd, struct epoll_event *, events,
  613. int, maxevents, int, timeout, const sigset_t *, sigmask,
  614. size_t, sigsetsize)
  615. #if defined(__NR_futex)
  616. safe_syscall6(int,futex,int *,uaddr,int,op,int,val, \
  617. const struct timespec *,timeout,int *,uaddr2,int,val3)
  618. #endif
  619. #if defined(__NR_futex_time64)
  620. safe_syscall6(int,futex_time64,int *,uaddr,int,op,int,val, \
  621. const struct timespec *,timeout,int *,uaddr2,int,val3)
  622. #endif
  623. safe_syscall2(int, rt_sigsuspend, sigset_t *, newset, size_t, sigsetsize)
  624. safe_syscall2(int, kill, pid_t, pid, int, sig)
  625. safe_syscall2(int, tkill, int, tid, int, sig)
  626. safe_syscall3(int, tgkill, int, tgid, int, pid, int, sig)
  627. safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt)
  628. safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt)
  629. safe_syscall5(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt,
  630. unsigned long, pos_l, unsigned long, pos_h)
  631. safe_syscall5(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, iovcnt,
  632. unsigned long, pos_l, unsigned long, pos_h)
  633. safe_syscall3(int, connect, int, fd, const struct sockaddr *, addr,
  634. socklen_t, addrlen)
  635. safe_syscall6(ssize_t, sendto, int, fd, const void *, buf, size_t, len,
  636. int, flags, const struct sockaddr *, addr, socklen_t, addrlen)
  637. safe_syscall6(ssize_t, recvfrom, int, fd, void *, buf, size_t, len,
  638. int, flags, struct sockaddr *, addr, socklen_t *, addrlen)
  639. safe_syscall3(ssize_t, sendmsg, int, fd, const struct msghdr *, msg, int, flags)
  640. safe_syscall3(ssize_t, recvmsg, int, fd, struct msghdr *, msg, int, flags)
  641. safe_syscall2(int, flock, int, fd, int, operation)
  642. #if defined(TARGET_NR_rt_sigtimedwait) || defined(TARGET_NR_rt_sigtimedwait_time64)
  643. safe_syscall4(int, rt_sigtimedwait, const sigset_t *, these, siginfo_t *, uinfo,
  644. const struct timespec *, uts, size_t, sigsetsize)
  645. #endif
  646. safe_syscall4(int, accept4, int, fd, struct sockaddr *, addr, socklen_t *, len,
  647. int, flags)
  648. #if defined(TARGET_NR_nanosleep)
  649. safe_syscall2(int, nanosleep, const struct timespec *, req,
  650. struct timespec *, rem)
  651. #endif
  652. #if defined(TARGET_NR_clock_nanosleep) || \
  653. defined(TARGET_NR_clock_nanosleep_time64)
  654. safe_syscall4(int, clock_nanosleep, const clockid_t, clock, int, flags,
  655. const struct timespec *, req, struct timespec *, rem)
  656. #endif
  657. #ifdef __NR_ipc
  658. #ifdef __s390x__
  659. safe_syscall5(int, ipc, int, call, long, first, long, second, long, third,
  660. void *, ptr)
  661. #else
  662. safe_syscall6(int, ipc, int, call, long, first, long, second, long, third,
  663. void *, ptr, long, fifth)
  664. #endif
  665. #endif
  666. #ifdef __NR_msgsnd
  667. safe_syscall4(int, msgsnd, int, msgid, const void *, msgp, size_t, sz,
  668. int, flags)
  669. #endif
  670. #ifdef __NR_msgrcv
  671. safe_syscall5(int, msgrcv, int, msgid, void *, msgp, size_t, sz,
  672. long, msgtype, int, flags)
  673. #endif
  674. #ifdef __NR_semtimedop
  675. safe_syscall4(int, semtimedop, int, semid, struct sembuf *, tsops,
  676. unsigned, nsops, const struct timespec *, timeout)
  677. #endif
  678. #if defined(TARGET_NR_mq_timedsend) || \
  679. defined(TARGET_NR_mq_timedsend_time64)
  680. safe_syscall5(int, mq_timedsend, int, mqdes, const char *, msg_ptr,
  681. size_t, len, unsigned, prio, const struct timespec *, timeout)
  682. #endif
  683. #if defined(TARGET_NR_mq_timedreceive) || \
  684. defined(TARGET_NR_mq_timedreceive_time64)
  685. safe_syscall5(int, mq_timedreceive, int, mqdes, char *, msg_ptr,
  686. size_t, len, unsigned *, prio, const struct timespec *, timeout)
  687. #endif
  688. #if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range)
  689. safe_syscall6(ssize_t, copy_file_range, int, infd, loff_t *, pinoff,
  690. int, outfd, loff_t *, poutoff, size_t, length,
  691. unsigned int, flags)
  692. #endif
  693. /* We do ioctl like this rather than via safe_syscall3 to preserve the
  694. * "third argument might be integer or pointer or not present" behaviour of
  695. * the libc function.
  696. */
  697. #define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
  698. /* Similarly for fcntl. Note that callers must always:
  699. * pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK
  700. * use the flock64 struct rather than unsuffixed flock
  701. * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts.
  702. */
  703. #ifdef __NR_fcntl64
  704. #define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
  705. #else
  706. #define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
  707. #endif
  708. static inline int host_to_target_sock_type(int host_type)
  709. {
  710. int target_type;
  711. switch (host_type & 0xf /* SOCK_TYPE_MASK */) {
  712. case SOCK_DGRAM:
  713. target_type = TARGET_SOCK_DGRAM;
  714. break;
  715. case SOCK_STREAM:
  716. target_type = TARGET_SOCK_STREAM;
  717. break;
  718. default:
  719. target_type = host_type & 0xf /* SOCK_TYPE_MASK */;
  720. break;
  721. }
  722. #if defined(SOCK_CLOEXEC)
  723. if (host_type & SOCK_CLOEXEC) {
  724. target_type |= TARGET_SOCK_CLOEXEC;
  725. }
  726. #endif
  727. #if defined(SOCK_NONBLOCK)
  728. if (host_type & SOCK_NONBLOCK) {
  729. target_type |= TARGET_SOCK_NONBLOCK;
  730. }
  731. #endif
  732. return target_type;
  733. }
  734. static abi_ulong target_brk;
  735. static abi_ulong target_original_brk;
  736. static abi_ulong brk_page;
  737. void target_set_brk(abi_ulong new_brk)
  738. {
  739. target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
  740. brk_page = HOST_PAGE_ALIGN(target_brk);
  741. }
  742. //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
  743. #define DEBUGF_BRK(message, args...)
  744. /* do_brk() must return target values and target errnos. */
  745. abi_long do_brk(abi_ulong new_brk)
  746. {
  747. abi_long mapped_addr;
  748. abi_ulong new_alloc_size;
  749. /* brk pointers are always untagged */
  750. DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
  751. if (!new_brk) {
  752. DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
  753. return target_brk;
  754. }
  755. if (new_brk < target_original_brk) {
  756. DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
  757. target_brk);
  758. return target_brk;
  759. }
  760. /* If the new brk is less than the highest page reserved to the
  761. * target heap allocation, set it and we're almost done... */
  762. if (new_brk <= brk_page) {
  763. /* Heap contents are initialized to zero, as for anonymous
  764. * mapped pages. */
  765. if (new_brk > target_brk) {
  766. memset(g2h_untagged(target_brk), 0, new_brk - target_brk);
  767. }
  768. target_brk = new_brk;
  769. DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
  770. return target_brk;
  771. }
  772. /* We need to allocate more memory after the brk... Note that
  773. * we don't use MAP_FIXED because that will map over the top of
  774. * any existing mapping (like the one with the host libc or qemu
  775. * itself); instead we treat "mapped but at wrong address" as
  776. * a failure and unmap again.
  777. */
  778. new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
  779. mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
  780. PROT_READ|PROT_WRITE,
  781. MAP_ANON|MAP_PRIVATE, 0, 0));
  782. if (mapped_addr == brk_page) {
  783. /* Heap contents are initialized to zero, as for anonymous
  784. * mapped pages. Technically the new pages are already
  785. * initialized to zero since they *are* anonymous mapped
  786. * pages, however we have to take care with the contents that
  787. * come from the remaining part of the previous page: it may
  788. * contains garbage data due to a previous heap usage (grown
  789. * then shrunken). */
  790. memset(g2h_untagged(target_brk), 0, brk_page - target_brk);
  791. target_brk = new_brk;
  792. brk_page = HOST_PAGE_ALIGN(target_brk);
  793. DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
  794. target_brk);
  795. return target_brk;
  796. } else if (mapped_addr != -1) {
  797. /* Mapped but at wrong address, meaning there wasn't actually
  798. * enough space for this brk.
  799. */
  800. target_munmap(mapped_addr, new_alloc_size);
  801. mapped_addr = -1;
  802. DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
  803. }
  804. else {
  805. DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
  806. }
  807. #if defined(TARGET_ALPHA)
  808. /* We (partially) emulate OSF/1 on Alpha, which requires we
  809. return a proper errno, not an unchanged brk value. */
  810. return -TARGET_ENOMEM;
  811. #endif
  812. /* For everything else, return the previous break. */
  813. return target_brk;
  814. }
  815. #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
  816. defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
  817. static inline abi_long copy_from_user_fdset(fd_set *fds,
  818. abi_ulong target_fds_addr,
  819. int n)
  820. {
  821. int i, nw, j, k;
  822. abi_ulong b, *target_fds;
  823. nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
  824. if (!(target_fds = lock_user(VERIFY_READ,
  825. target_fds_addr,
  826. sizeof(abi_ulong) * nw,
  827. 1)))
  828. return -TARGET_EFAULT;
  829. FD_ZERO(fds);
  830. k = 0;
  831. for (i = 0; i < nw; i++) {
  832. /* grab the abi_ulong */
  833. __get_user(b, &target_fds[i]);
  834. for (j = 0; j < TARGET_ABI_BITS; j++) {
  835. /* check the bit inside the abi_ulong */
  836. if ((b >> j) & 1)
  837. FD_SET(k, fds);
  838. k++;
  839. }
  840. }
  841. unlock_user(target_fds, target_fds_addr, 0);
  842. return 0;
  843. }
  844. static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
  845. abi_ulong target_fds_addr,
  846. int n)
  847. {
  848. if (target_fds_addr) {
  849. if (copy_from_user_fdset(fds, target_fds_addr, n))
  850. return -TARGET_EFAULT;
  851. *fds_ptr = fds;
  852. } else {
  853. *fds_ptr = NULL;
  854. }
  855. return 0;
  856. }
  857. static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
  858. const fd_set *fds,
  859. int n)
  860. {
  861. int i, nw, j, k;
  862. abi_long v;
  863. abi_ulong *target_fds;
  864. nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
  865. if (!(target_fds = lock_user(VERIFY_WRITE,
  866. target_fds_addr,
  867. sizeof(abi_ulong) * nw,
  868. 0)))
  869. return -TARGET_EFAULT;
  870. k = 0;
  871. for (i = 0; i < nw; i++) {
  872. v = 0;
  873. for (j = 0; j < TARGET_ABI_BITS; j++) {
  874. v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
  875. k++;
  876. }
  877. __put_user(v, &target_fds[i]);
  878. }
  879. unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
  880. return 0;
  881. }
  882. #endif
  883. #if defined(__alpha__)
  884. #define HOST_HZ 1024
  885. #else
  886. #define HOST_HZ 100
  887. #endif
  888. static inline abi_long host_to_target_clock_t(long ticks)
  889. {
  890. #if HOST_HZ == TARGET_HZ
  891. return ticks;
  892. #else
  893. return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
  894. #endif
  895. }
  896. static inline abi_long host_to_target_rusage(abi_ulong target_addr,
  897. const struct rusage *rusage)
  898. {
  899. struct target_rusage *target_rusage;
  900. if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
  901. return -TARGET_EFAULT;
  902. target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
  903. target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
  904. target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
  905. target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
  906. target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
  907. target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
  908. target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
  909. target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
  910. target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
  911. target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
  912. target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
  913. target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
  914. target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
  915. target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
  916. target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
  917. target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
  918. target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
  919. target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
  920. unlock_user_struct(target_rusage, target_addr, 1);
  921. return 0;
  922. }
  923. #ifdef TARGET_NR_setrlimit
  924. static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
  925. {
  926. abi_ulong target_rlim_swap;
  927. rlim_t result;
  928. target_rlim_swap = tswapal(target_rlim);
  929. if (target_rlim_swap == TARGET_RLIM_INFINITY)
  930. return RLIM_INFINITY;
  931. result = target_rlim_swap;
  932. if (target_rlim_swap != (rlim_t)result)
  933. return RLIM_INFINITY;
  934. return result;
  935. }
  936. #endif
  937. #if defined(TARGET_NR_getrlimit) || defined(TARGET_NR_ugetrlimit)
  938. static inline abi_ulong host_to_target_rlim(rlim_t rlim)
  939. {
  940. abi_ulong target_rlim_swap;
  941. abi_ulong result;
  942. if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
  943. target_rlim_swap = TARGET_RLIM_INFINITY;
  944. else
  945. target_rlim_swap = rlim;
  946. result = tswapal(target_rlim_swap);
  947. return result;
  948. }
  949. #endif
  950. static inline int target_to_host_resource(int code)
  951. {
  952. switch (code) {
  953. case TARGET_RLIMIT_AS:
  954. return RLIMIT_AS;
  955. case TARGET_RLIMIT_CORE:
  956. return RLIMIT_CORE;
  957. case TARGET_RLIMIT_CPU:
  958. return RLIMIT_CPU;
  959. case TARGET_RLIMIT_DATA:
  960. return RLIMIT_DATA;
  961. case TARGET_RLIMIT_FSIZE:
  962. return RLIMIT_FSIZE;
  963. case TARGET_RLIMIT_LOCKS:
  964. return RLIMIT_LOCKS;
  965. case TARGET_RLIMIT_MEMLOCK:
  966. return RLIMIT_MEMLOCK;
  967. case TARGET_RLIMIT_MSGQUEUE:
  968. return RLIMIT_MSGQUEUE;
  969. case TARGET_RLIMIT_NICE:
  970. return RLIMIT_NICE;
  971. case TARGET_RLIMIT_NOFILE:
  972. return RLIMIT_NOFILE;
  973. case TARGET_RLIMIT_NPROC:
  974. return RLIMIT_NPROC;
  975. case TARGET_RLIMIT_RSS:
  976. return RLIMIT_RSS;
  977. case TARGET_RLIMIT_RTPRIO:
  978. return RLIMIT_RTPRIO;
  979. #ifdef RLIMIT_RTTIME
  980. case TARGET_RLIMIT_RTTIME:
  981. return RLIMIT_RTTIME;
  982. #endif
  983. case TARGET_RLIMIT_SIGPENDING:
  984. return RLIMIT_SIGPENDING;
  985. case TARGET_RLIMIT_STACK:
  986. return RLIMIT_STACK;
  987. default:
  988. return code;
  989. }
  990. }
  991. static inline abi_long copy_from_user_timeval(struct timeval *tv,
  992. abi_ulong target_tv_addr)
  993. {
  994. struct target_timeval *target_tv;
  995. if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) {
  996. return -TARGET_EFAULT;
  997. }
  998. __get_user(tv->tv_sec, &target_tv->tv_sec);
  999. __get_user(tv->tv_usec, &target_tv->tv_usec);
  1000. unlock_user_struct(target_tv, target_tv_addr, 0);
  1001. return 0;
  1002. }
  1003. static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
  1004. const struct timeval *tv)
  1005. {
  1006. struct target_timeval *target_tv;
  1007. if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
  1008. return -TARGET_EFAULT;
  1009. }
  1010. __put_user(tv->tv_sec, &target_tv->tv_sec);
  1011. __put_user(tv->tv_usec, &target_tv->tv_usec);
  1012. unlock_user_struct(target_tv, target_tv_addr, 1);
  1013. return 0;
  1014. }
  1015. #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
  1016. static inline abi_long copy_from_user_timeval64(struct timeval *tv,
  1017. abi_ulong target_tv_addr)
  1018. {
  1019. struct target__kernel_sock_timeval *target_tv;
  1020. if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) {
  1021. return -TARGET_EFAULT;
  1022. }
  1023. __get_user(tv->tv_sec, &target_tv->tv_sec);
  1024. __get_user(tv->tv_usec, &target_tv->tv_usec);
  1025. unlock_user_struct(target_tv, target_tv_addr, 0);
  1026. return 0;
  1027. }
  1028. #endif
  1029. static inline abi_long copy_to_user_timeval64(abi_ulong target_tv_addr,
  1030. const struct timeval *tv)
  1031. {
  1032. struct target__kernel_sock_timeval *target_tv;
  1033. if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
  1034. return -TARGET_EFAULT;
  1035. }
  1036. __put_user(tv->tv_sec, &target_tv->tv_sec);
  1037. __put_user(tv->tv_usec, &target_tv->tv_usec);
  1038. unlock_user_struct(target_tv, target_tv_addr, 1);
  1039. return 0;
  1040. }
  1041. #if defined(TARGET_NR_futex) || \
  1042. defined(TARGET_NR_rt_sigtimedwait) || \
  1043. defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6) || \
  1044. defined(TARGET_NR_nanosleep) || defined(TARGET_NR_clock_settime) || \
  1045. defined(TARGET_NR_utimensat) || defined(TARGET_NR_mq_timedsend) || \
  1046. defined(TARGET_NR_mq_timedreceive) || defined(TARGET_NR_ipc) || \
  1047. defined(TARGET_NR_semop) || defined(TARGET_NR_semtimedop) || \
  1048. defined(TARGET_NR_timer_settime) || \
  1049. (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
  1050. static inline abi_long target_to_host_timespec(struct timespec *host_ts,
  1051. abi_ulong target_addr)
  1052. {
  1053. struct target_timespec *target_ts;
  1054. if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
  1055. return -TARGET_EFAULT;
  1056. }
  1057. __get_user(host_ts->tv_sec, &target_ts->tv_sec);
  1058. __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1059. unlock_user_struct(target_ts, target_addr, 0);
  1060. return 0;
  1061. }
  1062. #endif
  1063. #if defined(TARGET_NR_clock_settime64) || defined(TARGET_NR_futex_time64) || \
  1064. defined(TARGET_NR_timer_settime64) || \
  1065. defined(TARGET_NR_mq_timedsend_time64) || \
  1066. defined(TARGET_NR_mq_timedreceive_time64) || \
  1067. (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)) || \
  1068. defined(TARGET_NR_clock_nanosleep_time64) || \
  1069. defined(TARGET_NR_rt_sigtimedwait_time64) || \
  1070. defined(TARGET_NR_utimensat) || \
  1071. defined(TARGET_NR_utimensat_time64) || \
  1072. defined(TARGET_NR_semtimedop_time64) || \
  1073. defined(TARGET_NR_pselect6_time64) || defined(TARGET_NR_ppoll_time64)
  1074. static inline abi_long target_to_host_timespec64(struct timespec *host_ts,
  1075. abi_ulong target_addr)
  1076. {
  1077. struct target__kernel_timespec *target_ts;
  1078. if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
  1079. return -TARGET_EFAULT;
  1080. }
  1081. __get_user(host_ts->tv_sec, &target_ts->tv_sec);
  1082. __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1083. /* in 32bit mode, this drops the padding */
  1084. host_ts->tv_nsec = (long)(abi_long)host_ts->tv_nsec;
  1085. unlock_user_struct(target_ts, target_addr, 0);
  1086. return 0;
  1087. }
  1088. #endif
  1089. static inline abi_long host_to_target_timespec(abi_ulong target_addr,
  1090. struct timespec *host_ts)
  1091. {
  1092. struct target_timespec *target_ts;
  1093. if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
  1094. return -TARGET_EFAULT;
  1095. }
  1096. __put_user(host_ts->tv_sec, &target_ts->tv_sec);
  1097. __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1098. unlock_user_struct(target_ts, target_addr, 1);
  1099. return 0;
  1100. }
  1101. static inline abi_long host_to_target_timespec64(abi_ulong target_addr,
  1102. struct timespec *host_ts)
  1103. {
  1104. struct target__kernel_timespec *target_ts;
  1105. if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
  1106. return -TARGET_EFAULT;
  1107. }
  1108. __put_user(host_ts->tv_sec, &target_ts->tv_sec);
  1109. __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1110. unlock_user_struct(target_ts, target_addr, 1);
  1111. return 0;
  1112. }
  1113. #if defined(TARGET_NR_gettimeofday)
  1114. static inline abi_long copy_to_user_timezone(abi_ulong target_tz_addr,
  1115. struct timezone *tz)
  1116. {
  1117. struct target_timezone *target_tz;
  1118. if (!lock_user_struct(VERIFY_WRITE, target_tz, target_tz_addr, 1)) {
  1119. return -TARGET_EFAULT;
  1120. }
  1121. __put_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
  1122. __put_user(tz->tz_dsttime, &target_tz->tz_dsttime);
  1123. unlock_user_struct(target_tz, target_tz_addr, 1);
  1124. return 0;
  1125. }
  1126. #endif
  1127. #if defined(TARGET_NR_settimeofday)
  1128. static inline abi_long copy_from_user_timezone(struct timezone *tz,
  1129. abi_ulong target_tz_addr)
  1130. {
  1131. struct target_timezone *target_tz;
  1132. if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) {
  1133. return -TARGET_EFAULT;
  1134. }
  1135. __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
  1136. __get_user(tz->tz_dsttime, &target_tz->tz_dsttime);
  1137. unlock_user_struct(target_tz, target_tz_addr, 0);
  1138. return 0;
  1139. }
  1140. #endif
  1141. #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
  1142. #include <mqueue.h>
  1143. static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
  1144. abi_ulong target_mq_attr_addr)
  1145. {
  1146. struct target_mq_attr *target_mq_attr;
  1147. if (!lock_user_struct(VERIFY_READ, target_mq_attr,
  1148. target_mq_attr_addr, 1))
  1149. return -TARGET_EFAULT;
  1150. __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
  1151. __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
  1152. __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
  1153. __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
  1154. unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
  1155. return 0;
  1156. }
  1157. static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
  1158. const struct mq_attr *attr)
  1159. {
  1160. struct target_mq_attr *target_mq_attr;
  1161. if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
  1162. target_mq_attr_addr, 0))
  1163. return -TARGET_EFAULT;
  1164. __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
  1165. __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
  1166. __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
  1167. __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
  1168. unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
  1169. return 0;
  1170. }
  1171. #endif
  1172. #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
  1173. /* do_select() must return target values and target errnos. */
  1174. static abi_long do_select(int n,
  1175. abi_ulong rfd_addr, abi_ulong wfd_addr,
  1176. abi_ulong efd_addr, abi_ulong target_tv_addr)
  1177. {
  1178. fd_set rfds, wfds, efds;
  1179. fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
  1180. struct timeval tv;
  1181. struct timespec ts, *ts_ptr;
  1182. abi_long ret;
  1183. ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
  1184. if (ret) {
  1185. return ret;
  1186. }
  1187. ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
  1188. if (ret) {
  1189. return ret;
  1190. }
  1191. ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
  1192. if (ret) {
  1193. return ret;
  1194. }
  1195. if (target_tv_addr) {
  1196. if (copy_from_user_timeval(&tv, target_tv_addr))
  1197. return -TARGET_EFAULT;
  1198. ts.tv_sec = tv.tv_sec;
  1199. ts.tv_nsec = tv.tv_usec * 1000;
  1200. ts_ptr = &ts;
  1201. } else {
  1202. ts_ptr = NULL;
  1203. }
  1204. ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
  1205. ts_ptr, NULL));
  1206. if (!is_error(ret)) {
  1207. if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
  1208. return -TARGET_EFAULT;
  1209. if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
  1210. return -TARGET_EFAULT;
  1211. if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
  1212. return -TARGET_EFAULT;
  1213. if (target_tv_addr) {
  1214. tv.tv_sec = ts.tv_sec;
  1215. tv.tv_usec = ts.tv_nsec / 1000;
  1216. if (copy_to_user_timeval(target_tv_addr, &tv)) {
  1217. return -TARGET_EFAULT;
  1218. }
  1219. }
  1220. }
  1221. return ret;
  1222. }
  1223. #if defined(TARGET_WANT_OLD_SYS_SELECT)
  1224. static abi_long do_old_select(abi_ulong arg1)
  1225. {
  1226. struct target_sel_arg_struct *sel;
  1227. abi_ulong inp, outp, exp, tvp;
  1228. long nsel;
  1229. if (!lock_user_struct(VERIFY_READ, sel, arg1, 1)) {
  1230. return -TARGET_EFAULT;
  1231. }
  1232. nsel = tswapal(sel->n);
  1233. inp = tswapal(sel->inp);
  1234. outp = tswapal(sel->outp);
  1235. exp = tswapal(sel->exp);
  1236. tvp = tswapal(sel->tvp);
  1237. unlock_user_struct(sel, arg1, 0);
  1238. return do_select(nsel, inp, outp, exp, tvp);
  1239. }
  1240. #endif
  1241. #endif
  1242. #if defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
  1243. static abi_long do_pselect6(abi_long arg1, abi_long arg2, abi_long arg3,
  1244. abi_long arg4, abi_long arg5, abi_long arg6,
  1245. bool time64)
  1246. {
  1247. abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
  1248. fd_set rfds, wfds, efds;
  1249. fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
  1250. struct timespec ts, *ts_ptr;
  1251. abi_long ret;
  1252. /*
  1253. * The 6th arg is actually two args smashed together,
  1254. * so we cannot use the C library.
  1255. */
  1256. struct {
  1257. sigset_t *set;
  1258. size_t size;
  1259. } sig, *sig_ptr;
  1260. abi_ulong arg_sigset, arg_sigsize, *arg7;
  1261. n = arg1;
  1262. rfd_addr = arg2;
  1263. wfd_addr = arg3;
  1264. efd_addr = arg4;
  1265. ts_addr = arg5;
  1266. ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
  1267. if (ret) {
  1268. return ret;
  1269. }
  1270. ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
  1271. if (ret) {
  1272. return ret;
  1273. }
  1274. ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
  1275. if (ret) {
  1276. return ret;
  1277. }
  1278. /*
  1279. * This takes a timespec, and not a timeval, so we cannot
  1280. * use the do_select() helper ...
  1281. */
  1282. if (ts_addr) {
  1283. if (time64) {
  1284. if (target_to_host_timespec64(&ts, ts_addr)) {
  1285. return -TARGET_EFAULT;
  1286. }
  1287. } else {
  1288. if (target_to_host_timespec(&ts, ts_addr)) {
  1289. return -TARGET_EFAULT;
  1290. }
  1291. }
  1292. ts_ptr = &ts;
  1293. } else {
  1294. ts_ptr = NULL;
  1295. }
  1296. /* Extract the two packed args for the sigset */
  1297. sig_ptr = NULL;
  1298. if (arg6) {
  1299. arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
  1300. if (!arg7) {
  1301. return -TARGET_EFAULT;
  1302. }
  1303. arg_sigset = tswapal(arg7[0]);
  1304. arg_sigsize = tswapal(arg7[1]);
  1305. unlock_user(arg7, arg6, 0);
  1306. if (arg_sigset) {
  1307. ret = process_sigsuspend_mask(&sig.set, arg_sigset, arg_sigsize);
  1308. if (ret != 0) {
  1309. return ret;
  1310. }
  1311. sig_ptr = &sig;
  1312. sig.size = SIGSET_T_SIZE;
  1313. }
  1314. }
  1315. ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
  1316. ts_ptr, sig_ptr));
  1317. if (sig_ptr) {
  1318. finish_sigsuspend_mask(ret);
  1319. }
  1320. if (!is_error(ret)) {
  1321. if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) {
  1322. return -TARGET_EFAULT;
  1323. }
  1324. if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) {
  1325. return -TARGET_EFAULT;
  1326. }
  1327. if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) {
  1328. return -TARGET_EFAULT;
  1329. }
  1330. if (time64) {
  1331. if (ts_addr && host_to_target_timespec64(ts_addr, &ts)) {
  1332. return -TARGET_EFAULT;
  1333. }
  1334. } else {
  1335. if (ts_addr && host_to_target_timespec(ts_addr, &ts)) {
  1336. return -TARGET_EFAULT;
  1337. }
  1338. }
  1339. }
  1340. return ret;
  1341. }
  1342. #endif
  1343. #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) || \
  1344. defined(TARGET_NR_ppoll_time64)
  1345. static abi_long do_ppoll(abi_long arg1, abi_long arg2, abi_long arg3,
  1346. abi_long arg4, abi_long arg5, bool ppoll, bool time64)
  1347. {
  1348. struct target_pollfd *target_pfd;
  1349. unsigned int nfds = arg2;
  1350. struct pollfd *pfd;
  1351. unsigned int i;
  1352. abi_long ret;
  1353. pfd = NULL;
  1354. target_pfd = NULL;
  1355. if (nfds) {
  1356. if (nfds > (INT_MAX / sizeof(struct target_pollfd))) {
  1357. return -TARGET_EINVAL;
  1358. }
  1359. target_pfd = lock_user(VERIFY_WRITE, arg1,
  1360. sizeof(struct target_pollfd) * nfds, 1);
  1361. if (!target_pfd) {
  1362. return -TARGET_EFAULT;
  1363. }
  1364. pfd = alloca(sizeof(struct pollfd) * nfds);
  1365. for (i = 0; i < nfds; i++) {
  1366. pfd[i].fd = tswap32(target_pfd[i].fd);
  1367. pfd[i].events = tswap16(target_pfd[i].events);
  1368. }
  1369. }
  1370. if (ppoll) {
  1371. struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
  1372. sigset_t *set = NULL;
  1373. if (arg3) {
  1374. if (time64) {
  1375. if (target_to_host_timespec64(timeout_ts, arg3)) {
  1376. unlock_user(target_pfd, arg1, 0);
  1377. return -TARGET_EFAULT;
  1378. }
  1379. } else {
  1380. if (target_to_host_timespec(timeout_ts, arg3)) {
  1381. unlock_user(target_pfd, arg1, 0);
  1382. return -TARGET_EFAULT;
  1383. }
  1384. }
  1385. } else {
  1386. timeout_ts = NULL;
  1387. }
  1388. if (arg4) {
  1389. ret = process_sigsuspend_mask(&set, arg4, arg5);
  1390. if (ret != 0) {
  1391. unlock_user(target_pfd, arg1, 0);
  1392. return ret;
  1393. }
  1394. }
  1395. ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts,
  1396. set, SIGSET_T_SIZE));
  1397. if (set) {
  1398. finish_sigsuspend_mask(ret);
  1399. }
  1400. if (!is_error(ret) && arg3) {
  1401. if (time64) {
  1402. if (host_to_target_timespec64(arg3, timeout_ts)) {
  1403. return -TARGET_EFAULT;
  1404. }
  1405. } else {
  1406. if (host_to_target_timespec(arg3, timeout_ts)) {
  1407. return -TARGET_EFAULT;
  1408. }
  1409. }
  1410. }
  1411. } else {
  1412. struct timespec ts, *pts;
  1413. if (arg3 >= 0) {
  1414. /* Convert ms to secs, ns */
  1415. ts.tv_sec = arg3 / 1000;
  1416. ts.tv_nsec = (arg3 % 1000) * 1000000LL;
  1417. pts = &ts;
  1418. } else {
  1419. /* -ve poll() timeout means "infinite" */
  1420. pts = NULL;
  1421. }
  1422. ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0));
  1423. }
  1424. if (!is_error(ret)) {
  1425. for (i = 0; i < nfds; i++) {
  1426. target_pfd[i].revents = tswap16(pfd[i].revents);
  1427. }
  1428. }
  1429. unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
  1430. return ret;
  1431. }
  1432. #endif
  1433. static abi_long do_pipe(CPUArchState *cpu_env, abi_ulong pipedes,
  1434. int flags, int is_pipe2)
  1435. {
  1436. int host_pipe[2];
  1437. abi_long ret;
  1438. ret = pipe2(host_pipe, flags);
  1439. if (is_error(ret))
  1440. return get_errno(ret);
  1441. /* Several targets have special calling conventions for the original
  1442. pipe syscall, but didn't replicate this into the pipe2 syscall. */
  1443. if (!is_pipe2) {
  1444. #if defined(TARGET_ALPHA)
  1445. cpu_env->ir[IR_A4] = host_pipe[1];
  1446. return host_pipe[0];
  1447. #elif defined(TARGET_MIPS)
  1448. cpu_env->active_tc.gpr[3] = host_pipe[1];
  1449. return host_pipe[0];
  1450. #elif defined(TARGET_SH4)
  1451. cpu_env->gregs[1] = host_pipe[1];
  1452. return host_pipe[0];
  1453. #elif defined(TARGET_SPARC)
  1454. cpu_env->regwptr[1] = host_pipe[1];
  1455. return host_pipe[0];
  1456. #endif
  1457. }
  1458. if (put_user_s32(host_pipe[0], pipedes)
  1459. || put_user_s32(host_pipe[1], pipedes + sizeof(abi_int)))
  1460. return -TARGET_EFAULT;
  1461. return get_errno(ret);
  1462. }
  1463. static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
  1464. abi_ulong target_addr,
  1465. socklen_t len)
  1466. {
  1467. struct target_ip_mreqn *target_smreqn;
  1468. target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
  1469. if (!target_smreqn)
  1470. return -TARGET_EFAULT;
  1471. mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
  1472. mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
  1473. if (len == sizeof(struct target_ip_mreqn))
  1474. mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
  1475. unlock_user(target_smreqn, target_addr, 0);
  1476. return 0;
  1477. }
  1478. static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr,
  1479. abi_ulong target_addr,
  1480. socklen_t len)
  1481. {
  1482. const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
  1483. sa_family_t sa_family;
  1484. struct target_sockaddr *target_saddr;
  1485. if (fd_trans_target_to_host_addr(fd)) {
  1486. return fd_trans_target_to_host_addr(fd)(addr, target_addr, len);
  1487. }
  1488. target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
  1489. if (!target_saddr)
  1490. return -TARGET_EFAULT;
  1491. sa_family = tswap16(target_saddr->sa_family);
  1492. /* Oops. The caller might send a incomplete sun_path; sun_path
  1493. * must be terminated by \0 (see the manual page), but
  1494. * unfortunately it is quite common to specify sockaddr_un
  1495. * length as "strlen(x->sun_path)" while it should be
  1496. * "strlen(...) + 1". We'll fix that here if needed.
  1497. * Linux kernel has a similar feature.
  1498. */
  1499. if (sa_family == AF_UNIX) {
  1500. if (len < unix_maxlen && len > 0) {
  1501. char *cp = (char*)target_saddr;
  1502. if ( cp[len-1] && !cp[len] )
  1503. len++;
  1504. }
  1505. if (len > unix_maxlen)
  1506. len = unix_maxlen;
  1507. }
  1508. memcpy(addr, target_saddr, len);
  1509. addr->sa_family = sa_family;
  1510. if (sa_family == AF_NETLINK) {
  1511. struct sockaddr_nl *nladdr;
  1512. nladdr = (struct sockaddr_nl *)addr;
  1513. nladdr->nl_pid = tswap32(nladdr->nl_pid);
  1514. nladdr->nl_groups = tswap32(nladdr->nl_groups);
  1515. } else if (sa_family == AF_PACKET) {
  1516. struct target_sockaddr_ll *lladdr;
  1517. lladdr = (struct target_sockaddr_ll *)addr;
  1518. lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex);
  1519. lladdr->sll_hatype = tswap16(lladdr->sll_hatype);
  1520. }
  1521. unlock_user(target_saddr, target_addr, 0);
  1522. return 0;
  1523. }
  1524. static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
  1525. struct sockaddr *addr,
  1526. socklen_t len)
  1527. {
  1528. struct target_sockaddr *target_saddr;
  1529. if (len == 0) {
  1530. return 0;
  1531. }
  1532. assert(addr);
  1533. target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
  1534. if (!target_saddr)
  1535. return -TARGET_EFAULT;
  1536. memcpy(target_saddr, addr, len);
  1537. if (len >= offsetof(struct target_sockaddr, sa_family) +
  1538. sizeof(target_saddr->sa_family)) {
  1539. target_saddr->sa_family = tswap16(addr->sa_family);
  1540. }
  1541. if (addr->sa_family == AF_NETLINK &&
  1542. len >= sizeof(struct target_sockaddr_nl)) {
  1543. struct target_sockaddr_nl *target_nl =
  1544. (struct target_sockaddr_nl *)target_saddr;
  1545. target_nl->nl_pid = tswap32(target_nl->nl_pid);
  1546. target_nl->nl_groups = tswap32(target_nl->nl_groups);
  1547. } else if (addr->sa_family == AF_PACKET) {
  1548. struct sockaddr_ll *target_ll = (struct sockaddr_ll *)target_saddr;
  1549. target_ll->sll_ifindex = tswap32(target_ll->sll_ifindex);
  1550. target_ll->sll_hatype = tswap16(target_ll->sll_hatype);
  1551. } else if (addr->sa_family == AF_INET6 &&
  1552. len >= sizeof(struct target_sockaddr_in6)) {
  1553. struct target_sockaddr_in6 *target_in6 =
  1554. (struct target_sockaddr_in6 *)target_saddr;
  1555. target_in6->sin6_scope_id = tswap16(target_in6->sin6_scope_id);
  1556. }
  1557. unlock_user(target_saddr, target_addr, len);
  1558. return 0;
  1559. }
  1560. static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
  1561. struct target_msghdr *target_msgh)
  1562. {
  1563. struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
  1564. abi_long msg_controllen;
  1565. abi_ulong target_cmsg_addr;
  1566. struct target_cmsghdr *target_cmsg, *target_cmsg_start;
  1567. socklen_t space = 0;
  1568. msg_controllen = tswapal(target_msgh->msg_controllen);
  1569. if (msg_controllen < sizeof (struct target_cmsghdr))
  1570. goto the_end;
  1571. target_cmsg_addr = tswapal(target_msgh->msg_control);
  1572. target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
  1573. target_cmsg_start = target_cmsg;
  1574. if (!target_cmsg)
  1575. return -TARGET_EFAULT;
  1576. while (cmsg && target_cmsg) {
  1577. void *data = CMSG_DATA(cmsg);
  1578. void *target_data = TARGET_CMSG_DATA(target_cmsg);
  1579. int len = tswapal(target_cmsg->cmsg_len)
  1580. - sizeof(struct target_cmsghdr);
  1581. space += CMSG_SPACE(len);
  1582. if (space > msgh->msg_controllen) {
  1583. space -= CMSG_SPACE(len);
  1584. /* This is a QEMU bug, since we allocated the payload
  1585. * area ourselves (unlike overflow in host-to-target
  1586. * conversion, which is just the guest giving us a buffer
  1587. * that's too small). It can't happen for the payload types
  1588. * we currently support; if it becomes an issue in future
  1589. * we would need to improve our allocation strategy to
  1590. * something more intelligent than "twice the size of the
  1591. * target buffer we're reading from".
  1592. */
  1593. qemu_log_mask(LOG_UNIMP,
  1594. ("Unsupported ancillary data %d/%d: "
  1595. "unhandled msg size\n"),
  1596. tswap32(target_cmsg->cmsg_level),
  1597. tswap32(target_cmsg->cmsg_type));
  1598. break;
  1599. }
  1600. if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
  1601. cmsg->cmsg_level = SOL_SOCKET;
  1602. } else {
  1603. cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
  1604. }
  1605. cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
  1606. cmsg->cmsg_len = CMSG_LEN(len);
  1607. if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
  1608. int *fd = (int *)data;
  1609. int *target_fd = (int *)target_data;
  1610. int i, numfds = len / sizeof(int);
  1611. for (i = 0; i < numfds; i++) {
  1612. __get_user(fd[i], target_fd + i);
  1613. }
  1614. } else if (cmsg->cmsg_level == SOL_SOCKET
  1615. && cmsg->cmsg_type == SCM_CREDENTIALS) {
  1616. struct ucred *cred = (struct ucred *)data;
  1617. struct target_ucred *target_cred =
  1618. (struct target_ucred *)target_data;
  1619. __get_user(cred->pid, &target_cred->pid);
  1620. __get_user(cred->uid, &target_cred->uid);
  1621. __get_user(cred->gid, &target_cred->gid);
  1622. } else if (cmsg->cmsg_level == SOL_ALG) {
  1623. uint32_t *dst = (uint32_t *)data;
  1624. memcpy(dst, target_data, len);
  1625. /* fix endianess of first 32-bit word */
  1626. if (len >= sizeof(uint32_t)) {
  1627. *dst = tswap32(*dst);
  1628. }
  1629. } else {
  1630. qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n",
  1631. cmsg->cmsg_level, cmsg->cmsg_type);
  1632. memcpy(data, target_data, len);
  1633. }
  1634. cmsg = CMSG_NXTHDR(msgh, cmsg);
  1635. target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
  1636. target_cmsg_start);
  1637. }
  1638. unlock_user(target_cmsg, target_cmsg_addr, 0);
  1639. the_end:
  1640. msgh->msg_controllen = space;
  1641. return 0;
  1642. }
  1643. static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
  1644. struct msghdr *msgh)
  1645. {
  1646. struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
  1647. abi_long msg_controllen;
  1648. abi_ulong target_cmsg_addr;
  1649. struct target_cmsghdr *target_cmsg, *target_cmsg_start;
  1650. socklen_t space = 0;
  1651. msg_controllen = tswapal(target_msgh->msg_controllen);
  1652. if (msg_controllen < sizeof (struct target_cmsghdr))
  1653. goto the_end;
  1654. target_cmsg_addr = tswapal(target_msgh->msg_control);
  1655. target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
  1656. target_cmsg_start = target_cmsg;
  1657. if (!target_cmsg)
  1658. return -TARGET_EFAULT;
  1659. while (cmsg && target_cmsg) {
  1660. void *data = CMSG_DATA(cmsg);
  1661. void *target_data = TARGET_CMSG_DATA(target_cmsg);
  1662. int len = cmsg->cmsg_len - sizeof(struct cmsghdr);
  1663. int tgt_len, tgt_space;
  1664. /* We never copy a half-header but may copy half-data;
  1665. * this is Linux's behaviour in put_cmsg(). Note that
  1666. * truncation here is a guest problem (which we report
  1667. * to the guest via the CTRUNC bit), unlike truncation
  1668. * in target_to_host_cmsg, which is a QEMU bug.
  1669. */
  1670. if (msg_controllen < sizeof(struct target_cmsghdr)) {
  1671. target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
  1672. break;
  1673. }
  1674. if (cmsg->cmsg_level == SOL_SOCKET) {
  1675. target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
  1676. } else {
  1677. target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
  1678. }
  1679. target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
  1680. /* Payload types which need a different size of payload on
  1681. * the target must adjust tgt_len here.
  1682. */
  1683. tgt_len = len;
  1684. switch (cmsg->cmsg_level) {
  1685. case SOL_SOCKET:
  1686. switch (cmsg->cmsg_type) {
  1687. case SO_TIMESTAMP:
  1688. tgt_len = sizeof(struct target_timeval);
  1689. break;
  1690. default:
  1691. break;
  1692. }
  1693. break;
  1694. default:
  1695. break;
  1696. }
  1697. if (msg_controllen < TARGET_CMSG_LEN(tgt_len)) {
  1698. target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
  1699. tgt_len = msg_controllen - sizeof(struct target_cmsghdr);
  1700. }
  1701. /* We must now copy-and-convert len bytes of payload
  1702. * into tgt_len bytes of destination space. Bear in mind
  1703. * that in both source and destination we may be dealing
  1704. * with a truncated value!
  1705. */
  1706. switch (cmsg->cmsg_level) {
  1707. case SOL_SOCKET:
  1708. switch (cmsg->cmsg_type) {
  1709. case SCM_RIGHTS:
  1710. {
  1711. int *fd = (int *)data;
  1712. int *target_fd = (int *)target_data;
  1713. int i, numfds = tgt_len / sizeof(int);
  1714. for (i = 0; i < numfds; i++) {
  1715. __put_user(fd[i], target_fd + i);
  1716. }
  1717. break;
  1718. }
  1719. case SO_TIMESTAMP:
  1720. {
  1721. struct timeval *tv = (struct timeval *)data;
  1722. struct target_timeval *target_tv =
  1723. (struct target_timeval *)target_data;
  1724. if (len != sizeof(struct timeval) ||
  1725. tgt_len != sizeof(struct target_timeval)) {
  1726. goto unimplemented;
  1727. }
  1728. /* copy struct timeval to target */
  1729. __put_user(tv->tv_sec, &target_tv->tv_sec);
  1730. __put_user(tv->tv_usec, &target_tv->tv_usec);
  1731. break;
  1732. }
  1733. case SCM_CREDENTIALS:
  1734. {
  1735. struct ucred *cred = (struct ucred *)data;
  1736. struct target_ucred *target_cred =
  1737. (struct target_ucred *)target_data;
  1738. __put_user(cred->pid, &target_cred->pid);
  1739. __put_user(cred->uid, &target_cred->uid);
  1740. __put_user(cred->gid, &target_cred->gid);
  1741. break;
  1742. }
  1743. default:
  1744. goto unimplemented;
  1745. }
  1746. break;
  1747. case SOL_IP:
  1748. switch (cmsg->cmsg_type) {
  1749. case IP_TTL:
  1750. {
  1751. uint32_t *v = (uint32_t *)data;
  1752. uint32_t *t_int = (uint32_t *)target_data;
  1753. if (len != sizeof(uint32_t) ||
  1754. tgt_len != sizeof(uint32_t)) {
  1755. goto unimplemented;
  1756. }
  1757. __put_user(*v, t_int);
  1758. break;
  1759. }
  1760. case IP_RECVERR:
  1761. {
  1762. struct errhdr_t {
  1763. struct sock_extended_err ee;
  1764. struct sockaddr_in offender;
  1765. };
  1766. struct errhdr_t *errh = (struct errhdr_t *)data;
  1767. struct errhdr_t *target_errh =
  1768. (struct errhdr_t *)target_data;
  1769. if (len != sizeof(struct errhdr_t) ||
  1770. tgt_len != sizeof(struct errhdr_t)) {
  1771. goto unimplemented;
  1772. }
  1773. __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
  1774. __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
  1775. __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
  1776. __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
  1777. __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
  1778. __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
  1779. __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
  1780. host_to_target_sockaddr((unsigned long) &target_errh->offender,
  1781. (void *) &errh->offender, sizeof(errh->offender));
  1782. break;
  1783. }
  1784. default:
  1785. goto unimplemented;
  1786. }
  1787. break;
  1788. case SOL_IPV6:
  1789. switch (cmsg->cmsg_type) {
  1790. case IPV6_HOPLIMIT:
  1791. {
  1792. uint32_t *v = (uint32_t *)data;
  1793. uint32_t *t_int = (uint32_t *)target_data;
  1794. if (len != sizeof(uint32_t) ||
  1795. tgt_len != sizeof(uint32_t)) {
  1796. goto unimplemented;
  1797. }
  1798. __put_user(*v, t_int);
  1799. break;
  1800. }
  1801. case IPV6_RECVERR:
  1802. {
  1803. struct errhdr6_t {
  1804. struct sock_extended_err ee;
  1805. struct sockaddr_in6 offender;
  1806. };
  1807. struct errhdr6_t *errh = (struct errhdr6_t *)data;
  1808. struct errhdr6_t *target_errh =
  1809. (struct errhdr6_t *)target_data;
  1810. if (len != sizeof(struct errhdr6_t) ||
  1811. tgt_len != sizeof(struct errhdr6_t)) {
  1812. goto unimplemented;
  1813. }
  1814. __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
  1815. __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
  1816. __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
  1817. __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
  1818. __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
  1819. __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
  1820. __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
  1821. host_to_target_sockaddr((unsigned long) &target_errh->offender,
  1822. (void *) &errh->offender, sizeof(errh->offender));
  1823. break;
  1824. }
  1825. default:
  1826. goto unimplemented;
  1827. }
  1828. break;
  1829. default:
  1830. unimplemented:
  1831. qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n",
  1832. cmsg->cmsg_level, cmsg->cmsg_type);
  1833. memcpy(target_data, data, MIN(len, tgt_len));
  1834. if (tgt_len > len) {
  1835. memset(target_data + len, 0, tgt_len - len);
  1836. }
  1837. }
  1838. target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(tgt_len));
  1839. tgt_space = TARGET_CMSG_SPACE(tgt_len);
  1840. if (msg_controllen < tgt_space) {
  1841. tgt_space = msg_controllen;
  1842. }
  1843. msg_controllen -= tgt_space;
  1844. space += tgt_space;
  1845. cmsg = CMSG_NXTHDR(msgh, cmsg);
  1846. target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
  1847. target_cmsg_start);
  1848. }
  1849. unlock_user(target_cmsg, target_cmsg_addr, space);
  1850. the_end:
  1851. target_msgh->msg_controllen = tswapal(space);
  1852. return 0;
  1853. }
  1854. /* do_setsockopt() Must return target values and target errnos. */
  1855. static abi_long do_setsockopt(int sockfd, int level, int optname,
  1856. abi_ulong optval_addr, socklen_t optlen)
  1857. {
  1858. abi_long ret;
  1859. int val;
  1860. struct ip_mreqn *ip_mreq;
  1861. struct ip_mreq_source *ip_mreq_source;
  1862. switch(level) {
  1863. case SOL_TCP:
  1864. case SOL_UDP:
  1865. /* TCP and UDP options all take an 'int' value. */
  1866. if (optlen < sizeof(uint32_t))
  1867. return -TARGET_EINVAL;
  1868. if (get_user_u32(val, optval_addr))
  1869. return -TARGET_EFAULT;
  1870. ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
  1871. break;
  1872. case SOL_IP:
  1873. switch(optname) {
  1874. case IP_TOS:
  1875. case IP_TTL:
  1876. case IP_HDRINCL:
  1877. case IP_ROUTER_ALERT:
  1878. case IP_RECVOPTS:
  1879. case IP_RETOPTS:
  1880. case IP_PKTINFO:
  1881. case IP_MTU_DISCOVER:
  1882. case IP_RECVERR:
  1883. case IP_RECVTTL:
  1884. case IP_RECVTOS:
  1885. #ifdef IP_FREEBIND
  1886. case IP_FREEBIND:
  1887. #endif
  1888. case IP_MULTICAST_TTL:
  1889. case IP_MULTICAST_LOOP:
  1890. val = 0;
  1891. if (optlen >= sizeof(uint32_t)) {
  1892. if (get_user_u32(val, optval_addr))
  1893. return -TARGET_EFAULT;
  1894. } else if (optlen >= 1) {
  1895. if (get_user_u8(val, optval_addr))
  1896. return -TARGET_EFAULT;
  1897. }
  1898. ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
  1899. break;
  1900. case IP_ADD_MEMBERSHIP:
  1901. case IP_DROP_MEMBERSHIP:
  1902. if (optlen < sizeof (struct target_ip_mreq) ||
  1903. optlen > sizeof (struct target_ip_mreqn))
  1904. return -TARGET_EINVAL;
  1905. ip_mreq = (struct ip_mreqn *) alloca(optlen);
  1906. target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
  1907. ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
  1908. break;
  1909. case IP_BLOCK_SOURCE:
  1910. case IP_UNBLOCK_SOURCE:
  1911. case IP_ADD_SOURCE_MEMBERSHIP:
  1912. case IP_DROP_SOURCE_MEMBERSHIP:
  1913. if (optlen != sizeof (struct target_ip_mreq_source))
  1914. return -TARGET_EINVAL;
  1915. ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
  1916. if (!ip_mreq_source) {
  1917. return -TARGET_EFAULT;
  1918. }
  1919. ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
  1920. unlock_user (ip_mreq_source, optval_addr, 0);
  1921. break;
  1922. default:
  1923. goto unimplemented;
  1924. }
  1925. break;
  1926. case SOL_IPV6:
  1927. switch (optname) {
  1928. case IPV6_MTU_DISCOVER:
  1929. case IPV6_MTU:
  1930. case IPV6_V6ONLY:
  1931. case IPV6_RECVPKTINFO:
  1932. case IPV6_UNICAST_HOPS:
  1933. case IPV6_MULTICAST_HOPS:
  1934. case IPV6_MULTICAST_LOOP:
  1935. case IPV6_RECVERR:
  1936. case IPV6_RECVHOPLIMIT:
  1937. case IPV6_2292HOPLIMIT:
  1938. case IPV6_CHECKSUM:
  1939. case IPV6_ADDRFORM:
  1940. case IPV6_2292PKTINFO:
  1941. case IPV6_RECVTCLASS:
  1942. case IPV6_RECVRTHDR:
  1943. case IPV6_2292RTHDR:
  1944. case IPV6_RECVHOPOPTS:
  1945. case IPV6_2292HOPOPTS:
  1946. case IPV6_RECVDSTOPTS:
  1947. case IPV6_2292DSTOPTS:
  1948. case IPV6_TCLASS:
  1949. case IPV6_ADDR_PREFERENCES:
  1950. #ifdef IPV6_RECVPATHMTU
  1951. case IPV6_RECVPATHMTU:
  1952. #endif
  1953. #ifdef IPV6_TRANSPARENT
  1954. case IPV6_TRANSPARENT:
  1955. #endif
  1956. #ifdef IPV6_FREEBIND
  1957. case IPV6_FREEBIND:
  1958. #endif
  1959. #ifdef IPV6_RECVORIGDSTADDR
  1960. case IPV6_RECVORIGDSTADDR:
  1961. #endif
  1962. val = 0;
  1963. if (optlen < sizeof(uint32_t)) {
  1964. return -TARGET_EINVAL;
  1965. }
  1966. if (get_user_u32(val, optval_addr)) {
  1967. return -TARGET_EFAULT;
  1968. }
  1969. ret = get_errno(setsockopt(sockfd, level, optname,
  1970. &val, sizeof(val)));
  1971. break;
  1972. case IPV6_PKTINFO:
  1973. {
  1974. struct in6_pktinfo pki;
  1975. if (optlen < sizeof(pki)) {
  1976. return -TARGET_EINVAL;
  1977. }
  1978. if (copy_from_user(&pki, optval_addr, sizeof(pki))) {
  1979. return -TARGET_EFAULT;
  1980. }
  1981. pki.ipi6_ifindex = tswap32(pki.ipi6_ifindex);
  1982. ret = get_errno(setsockopt(sockfd, level, optname,
  1983. &pki, sizeof(pki)));
  1984. break;
  1985. }
  1986. case IPV6_ADD_MEMBERSHIP:
  1987. case IPV6_DROP_MEMBERSHIP:
  1988. {
  1989. struct ipv6_mreq ipv6mreq;
  1990. if (optlen < sizeof(ipv6mreq)) {
  1991. return -TARGET_EINVAL;
  1992. }
  1993. if (copy_from_user(&ipv6mreq, optval_addr, sizeof(ipv6mreq))) {
  1994. return -TARGET_EFAULT;
  1995. }
  1996. ipv6mreq.ipv6mr_interface = tswap32(ipv6mreq.ipv6mr_interface);
  1997. ret = get_errno(setsockopt(sockfd, level, optname,
  1998. &ipv6mreq, sizeof(ipv6mreq)));
  1999. break;
  2000. }
  2001. default:
  2002. goto unimplemented;
  2003. }
  2004. break;
  2005. case SOL_ICMPV6:
  2006. switch (optname) {
  2007. case ICMPV6_FILTER:
  2008. {
  2009. struct icmp6_filter icmp6f;
  2010. if (optlen > sizeof(icmp6f)) {
  2011. optlen = sizeof(icmp6f);
  2012. }
  2013. if (copy_from_user(&icmp6f, optval_addr, optlen)) {
  2014. return -TARGET_EFAULT;
  2015. }
  2016. for (val = 0; val < 8; val++) {
  2017. icmp6f.data[val] = tswap32(icmp6f.data[val]);
  2018. }
  2019. ret = get_errno(setsockopt(sockfd, level, optname,
  2020. &icmp6f, optlen));
  2021. break;
  2022. }
  2023. default:
  2024. goto unimplemented;
  2025. }
  2026. break;
  2027. case SOL_RAW:
  2028. switch (optname) {
  2029. case ICMP_FILTER:
  2030. case IPV6_CHECKSUM:
  2031. /* those take an u32 value */
  2032. if (optlen < sizeof(uint32_t)) {
  2033. return -TARGET_EINVAL;
  2034. }
  2035. if (get_user_u32(val, optval_addr)) {
  2036. return -TARGET_EFAULT;
  2037. }
  2038. ret = get_errno(setsockopt(sockfd, level, optname,
  2039. &val, sizeof(val)));
  2040. break;
  2041. default:
  2042. goto unimplemented;
  2043. }
  2044. break;
  2045. #if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE)
  2046. case SOL_ALG:
  2047. switch (optname) {
  2048. case ALG_SET_KEY:
  2049. {
  2050. char *alg_key = g_malloc(optlen);
  2051. if (!alg_key) {
  2052. return -TARGET_ENOMEM;
  2053. }
  2054. if (copy_from_user(alg_key, optval_addr, optlen)) {
  2055. g_free(alg_key);
  2056. return -TARGET_EFAULT;
  2057. }
  2058. ret = get_errno(setsockopt(sockfd, level, optname,
  2059. alg_key, optlen));
  2060. g_free(alg_key);
  2061. break;
  2062. }
  2063. case ALG_SET_AEAD_AUTHSIZE:
  2064. {
  2065. ret = get_errno(setsockopt(sockfd, level, optname,
  2066. NULL, optlen));
  2067. break;
  2068. }
  2069. default:
  2070. goto unimplemented;
  2071. }
  2072. break;
  2073. #endif
  2074. case TARGET_SOL_SOCKET:
  2075. switch (optname) {
  2076. case TARGET_SO_RCVTIMEO:
  2077. {
  2078. struct timeval tv;
  2079. optname = SO_RCVTIMEO;
  2080. set_timeout:
  2081. if (optlen != sizeof(struct target_timeval)) {
  2082. return -TARGET_EINVAL;
  2083. }
  2084. if (copy_from_user_timeval(&tv, optval_addr)) {
  2085. return -TARGET_EFAULT;
  2086. }
  2087. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
  2088. &tv, sizeof(tv)));
  2089. return ret;
  2090. }
  2091. case TARGET_SO_SNDTIMEO:
  2092. optname = SO_SNDTIMEO;
  2093. goto set_timeout;
  2094. case TARGET_SO_ATTACH_FILTER:
  2095. {
  2096. struct target_sock_fprog *tfprog;
  2097. struct target_sock_filter *tfilter;
  2098. struct sock_fprog fprog;
  2099. struct sock_filter *filter;
  2100. int i;
  2101. if (optlen != sizeof(*tfprog)) {
  2102. return -TARGET_EINVAL;
  2103. }
  2104. if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
  2105. return -TARGET_EFAULT;
  2106. }
  2107. if (!lock_user_struct(VERIFY_READ, tfilter,
  2108. tswapal(tfprog->filter), 0)) {
  2109. unlock_user_struct(tfprog, optval_addr, 1);
  2110. return -TARGET_EFAULT;
  2111. }
  2112. fprog.len = tswap16(tfprog->len);
  2113. filter = g_try_new(struct sock_filter, fprog.len);
  2114. if (filter == NULL) {
  2115. unlock_user_struct(tfilter, tfprog->filter, 1);
  2116. unlock_user_struct(tfprog, optval_addr, 1);
  2117. return -TARGET_ENOMEM;
  2118. }
  2119. for (i = 0; i < fprog.len; i++) {
  2120. filter[i].code = tswap16(tfilter[i].code);
  2121. filter[i].jt = tfilter[i].jt;
  2122. filter[i].jf = tfilter[i].jf;
  2123. filter[i].k = tswap32(tfilter[i].k);
  2124. }
  2125. fprog.filter = filter;
  2126. ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
  2127. SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
  2128. g_free(filter);
  2129. unlock_user_struct(tfilter, tfprog->filter, 1);
  2130. unlock_user_struct(tfprog, optval_addr, 1);
  2131. return ret;
  2132. }
  2133. case TARGET_SO_BINDTODEVICE:
  2134. {
  2135. char *dev_ifname, *addr_ifname;
  2136. if (optlen > IFNAMSIZ - 1) {
  2137. optlen = IFNAMSIZ - 1;
  2138. }
  2139. dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1);
  2140. if (!dev_ifname) {
  2141. return -TARGET_EFAULT;
  2142. }
  2143. optname = SO_BINDTODEVICE;
  2144. addr_ifname = alloca(IFNAMSIZ);
  2145. memcpy(addr_ifname, dev_ifname, optlen);
  2146. addr_ifname[optlen] = 0;
  2147. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
  2148. addr_ifname, optlen));
  2149. unlock_user (dev_ifname, optval_addr, 0);
  2150. return ret;
  2151. }
  2152. case TARGET_SO_LINGER:
  2153. {
  2154. struct linger lg;
  2155. struct target_linger *tlg;
  2156. if (optlen != sizeof(struct target_linger)) {
  2157. return -TARGET_EINVAL;
  2158. }
  2159. if (!lock_user_struct(VERIFY_READ, tlg, optval_addr, 1)) {
  2160. return -TARGET_EFAULT;
  2161. }
  2162. __get_user(lg.l_onoff, &tlg->l_onoff);
  2163. __get_user(lg.l_linger, &tlg->l_linger);
  2164. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, SO_LINGER,
  2165. &lg, sizeof(lg)));
  2166. unlock_user_struct(tlg, optval_addr, 0);
  2167. return ret;
  2168. }
  2169. /* Options with 'int' argument. */
  2170. case TARGET_SO_DEBUG:
  2171. optname = SO_DEBUG;
  2172. break;
  2173. case TARGET_SO_REUSEADDR:
  2174. optname = SO_REUSEADDR;
  2175. break;
  2176. #ifdef SO_REUSEPORT
  2177. case TARGET_SO_REUSEPORT:
  2178. optname = SO_REUSEPORT;
  2179. break;
  2180. #endif
  2181. case TARGET_SO_TYPE:
  2182. optname = SO_TYPE;
  2183. break;
  2184. case TARGET_SO_ERROR:
  2185. optname = SO_ERROR;
  2186. break;
  2187. case TARGET_SO_DONTROUTE:
  2188. optname = SO_DONTROUTE;
  2189. break;
  2190. case TARGET_SO_BROADCAST:
  2191. optname = SO_BROADCAST;
  2192. break;
  2193. case TARGET_SO_SNDBUF:
  2194. optname = SO_SNDBUF;
  2195. break;
  2196. case TARGET_SO_SNDBUFFORCE:
  2197. optname = SO_SNDBUFFORCE;
  2198. break;
  2199. case TARGET_SO_RCVBUF:
  2200. optname = SO_RCVBUF;
  2201. break;
  2202. case TARGET_SO_RCVBUFFORCE:
  2203. optname = SO_RCVBUFFORCE;
  2204. break;
  2205. case TARGET_SO_KEEPALIVE:
  2206. optname = SO_KEEPALIVE;
  2207. break;
  2208. case TARGET_SO_OOBINLINE:
  2209. optname = SO_OOBINLINE;
  2210. break;
  2211. case TARGET_SO_NO_CHECK:
  2212. optname = SO_NO_CHECK;
  2213. break;
  2214. case TARGET_SO_PRIORITY:
  2215. optname = SO_PRIORITY;
  2216. break;
  2217. #ifdef SO_BSDCOMPAT
  2218. case TARGET_SO_BSDCOMPAT:
  2219. optname = SO_BSDCOMPAT;
  2220. break;
  2221. #endif
  2222. case TARGET_SO_PASSCRED:
  2223. optname = SO_PASSCRED;
  2224. break;
  2225. case TARGET_SO_PASSSEC:
  2226. optname = SO_PASSSEC;
  2227. break;
  2228. case TARGET_SO_TIMESTAMP:
  2229. optname = SO_TIMESTAMP;
  2230. break;
  2231. case TARGET_SO_RCVLOWAT:
  2232. optname = SO_RCVLOWAT;
  2233. break;
  2234. default:
  2235. goto unimplemented;
  2236. }
  2237. if (optlen < sizeof(uint32_t))
  2238. return -TARGET_EINVAL;
  2239. if (get_user_u32(val, optval_addr))
  2240. return -TARGET_EFAULT;
  2241. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
  2242. break;
  2243. #ifdef SOL_NETLINK
  2244. case SOL_NETLINK:
  2245. switch (optname) {
  2246. case NETLINK_PKTINFO:
  2247. case NETLINK_ADD_MEMBERSHIP:
  2248. case NETLINK_DROP_MEMBERSHIP:
  2249. case NETLINK_BROADCAST_ERROR:
  2250. case NETLINK_NO_ENOBUFS:
  2251. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
  2252. case NETLINK_LISTEN_ALL_NSID:
  2253. case NETLINK_CAP_ACK:
  2254. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
  2255. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
  2256. case NETLINK_EXT_ACK:
  2257. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2258. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
  2259. case NETLINK_GET_STRICT_CHK:
  2260. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2261. break;
  2262. default:
  2263. goto unimplemented;
  2264. }
  2265. val = 0;
  2266. if (optlen < sizeof(uint32_t)) {
  2267. return -TARGET_EINVAL;
  2268. }
  2269. if (get_user_u32(val, optval_addr)) {
  2270. return -TARGET_EFAULT;
  2271. }
  2272. ret = get_errno(setsockopt(sockfd, SOL_NETLINK, optname, &val,
  2273. sizeof(val)));
  2274. break;
  2275. #endif /* SOL_NETLINK */
  2276. default:
  2277. unimplemented:
  2278. qemu_log_mask(LOG_UNIMP, "Unsupported setsockopt level=%d optname=%d\n",
  2279. level, optname);
  2280. ret = -TARGET_ENOPROTOOPT;
  2281. }
  2282. return ret;
  2283. }
  2284. /* do_getsockopt() Must return target values and target errnos. */
  2285. static abi_long do_getsockopt(int sockfd, int level, int optname,
  2286. abi_ulong optval_addr, abi_ulong optlen)
  2287. {
  2288. abi_long ret;
  2289. int len, val;
  2290. socklen_t lv;
  2291. switch(level) {
  2292. case TARGET_SOL_SOCKET:
  2293. level = SOL_SOCKET;
  2294. switch (optname) {
  2295. /* These don't just return a single integer */
  2296. case TARGET_SO_PEERNAME:
  2297. goto unimplemented;
  2298. case TARGET_SO_RCVTIMEO: {
  2299. struct timeval tv;
  2300. socklen_t tvlen;
  2301. optname = SO_RCVTIMEO;
  2302. get_timeout:
  2303. if (get_user_u32(len, optlen)) {
  2304. return -TARGET_EFAULT;
  2305. }
  2306. if (len < 0) {
  2307. return -TARGET_EINVAL;
  2308. }
  2309. tvlen = sizeof(tv);
  2310. ret = get_errno(getsockopt(sockfd, level, optname,
  2311. &tv, &tvlen));
  2312. if (ret < 0) {
  2313. return ret;
  2314. }
  2315. if (len > sizeof(struct target_timeval)) {
  2316. len = sizeof(struct target_timeval);
  2317. }
  2318. if (copy_to_user_timeval(optval_addr, &tv)) {
  2319. return -TARGET_EFAULT;
  2320. }
  2321. if (put_user_u32(len, optlen)) {
  2322. return -TARGET_EFAULT;
  2323. }
  2324. break;
  2325. }
  2326. case TARGET_SO_SNDTIMEO:
  2327. optname = SO_SNDTIMEO;
  2328. goto get_timeout;
  2329. case TARGET_SO_PEERCRED: {
  2330. struct ucred cr;
  2331. socklen_t crlen;
  2332. struct target_ucred *tcr;
  2333. if (get_user_u32(len, optlen)) {
  2334. return -TARGET_EFAULT;
  2335. }
  2336. if (len < 0) {
  2337. return -TARGET_EINVAL;
  2338. }
  2339. crlen = sizeof(cr);
  2340. ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
  2341. &cr, &crlen));
  2342. if (ret < 0) {
  2343. return ret;
  2344. }
  2345. if (len > crlen) {
  2346. len = crlen;
  2347. }
  2348. if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
  2349. return -TARGET_EFAULT;
  2350. }
  2351. __put_user(cr.pid, &tcr->pid);
  2352. __put_user(cr.uid, &tcr->uid);
  2353. __put_user(cr.gid, &tcr->gid);
  2354. unlock_user_struct(tcr, optval_addr, 1);
  2355. if (put_user_u32(len, optlen)) {
  2356. return -TARGET_EFAULT;
  2357. }
  2358. break;
  2359. }
  2360. case TARGET_SO_PEERSEC: {
  2361. char *name;
  2362. if (get_user_u32(len, optlen)) {
  2363. return -TARGET_EFAULT;
  2364. }
  2365. if (len < 0) {
  2366. return -TARGET_EINVAL;
  2367. }
  2368. name = lock_user(VERIFY_WRITE, optval_addr, len, 0);
  2369. if (!name) {
  2370. return -TARGET_EFAULT;
  2371. }
  2372. lv = len;
  2373. ret = get_errno(getsockopt(sockfd, level, SO_PEERSEC,
  2374. name, &lv));
  2375. if (put_user_u32(lv, optlen)) {
  2376. ret = -TARGET_EFAULT;
  2377. }
  2378. unlock_user(name, optval_addr, lv);
  2379. break;
  2380. }
  2381. case TARGET_SO_LINGER:
  2382. {
  2383. struct linger lg;
  2384. socklen_t lglen;
  2385. struct target_linger *tlg;
  2386. if (get_user_u32(len, optlen)) {
  2387. return -TARGET_EFAULT;
  2388. }
  2389. if (len < 0) {
  2390. return -TARGET_EINVAL;
  2391. }
  2392. lglen = sizeof(lg);
  2393. ret = get_errno(getsockopt(sockfd, level, SO_LINGER,
  2394. &lg, &lglen));
  2395. if (ret < 0) {
  2396. return ret;
  2397. }
  2398. if (len > lglen) {
  2399. len = lglen;
  2400. }
  2401. if (!lock_user_struct(VERIFY_WRITE, tlg, optval_addr, 0)) {
  2402. return -TARGET_EFAULT;
  2403. }
  2404. __put_user(lg.l_onoff, &tlg->l_onoff);
  2405. __put_user(lg.l_linger, &tlg->l_linger);
  2406. unlock_user_struct(tlg, optval_addr, 1);
  2407. if (put_user_u32(len, optlen)) {
  2408. return -TARGET_EFAULT;
  2409. }
  2410. break;
  2411. }
  2412. /* Options with 'int' argument. */
  2413. case TARGET_SO_DEBUG:
  2414. optname = SO_DEBUG;
  2415. goto int_case;
  2416. case TARGET_SO_REUSEADDR:
  2417. optname = SO_REUSEADDR;
  2418. goto int_case;
  2419. #ifdef SO_REUSEPORT
  2420. case TARGET_SO_REUSEPORT:
  2421. optname = SO_REUSEPORT;
  2422. goto int_case;
  2423. #endif
  2424. case TARGET_SO_TYPE:
  2425. optname = SO_TYPE;
  2426. goto int_case;
  2427. case TARGET_SO_ERROR:
  2428. optname = SO_ERROR;
  2429. goto int_case;
  2430. case TARGET_SO_DONTROUTE:
  2431. optname = SO_DONTROUTE;
  2432. goto int_case;
  2433. case TARGET_SO_BROADCAST:
  2434. optname = SO_BROADCAST;
  2435. goto int_case;
  2436. case TARGET_SO_SNDBUF:
  2437. optname = SO_SNDBUF;
  2438. goto int_case;
  2439. case TARGET_SO_RCVBUF:
  2440. optname = SO_RCVBUF;
  2441. goto int_case;
  2442. case TARGET_SO_KEEPALIVE:
  2443. optname = SO_KEEPALIVE;
  2444. goto int_case;
  2445. case TARGET_SO_OOBINLINE:
  2446. optname = SO_OOBINLINE;
  2447. goto int_case;
  2448. case TARGET_SO_NO_CHECK:
  2449. optname = SO_NO_CHECK;
  2450. goto int_case;
  2451. case TARGET_SO_PRIORITY:
  2452. optname = SO_PRIORITY;
  2453. goto int_case;
  2454. #ifdef SO_BSDCOMPAT
  2455. case TARGET_SO_BSDCOMPAT:
  2456. optname = SO_BSDCOMPAT;
  2457. goto int_case;
  2458. #endif
  2459. case TARGET_SO_PASSCRED:
  2460. optname = SO_PASSCRED;
  2461. goto int_case;
  2462. case TARGET_SO_TIMESTAMP:
  2463. optname = SO_TIMESTAMP;
  2464. goto int_case;
  2465. case TARGET_SO_RCVLOWAT:
  2466. optname = SO_RCVLOWAT;
  2467. goto int_case;
  2468. case TARGET_SO_ACCEPTCONN:
  2469. optname = SO_ACCEPTCONN;
  2470. goto int_case;
  2471. case TARGET_SO_PROTOCOL:
  2472. optname = SO_PROTOCOL;
  2473. goto int_case;
  2474. case TARGET_SO_DOMAIN:
  2475. optname = SO_DOMAIN;
  2476. goto int_case;
  2477. default:
  2478. goto int_case;
  2479. }
  2480. break;
  2481. case SOL_TCP:
  2482. case SOL_UDP:
  2483. /* TCP and UDP options all take an 'int' value. */
  2484. int_case:
  2485. if (get_user_u32(len, optlen))
  2486. return -TARGET_EFAULT;
  2487. if (len < 0)
  2488. return -TARGET_EINVAL;
  2489. lv = sizeof(lv);
  2490. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2491. if (ret < 0)
  2492. return ret;
  2493. switch (optname) {
  2494. case SO_TYPE:
  2495. val = host_to_target_sock_type(val);
  2496. break;
  2497. case SO_ERROR:
  2498. val = host_to_target_errno(val);
  2499. break;
  2500. }
  2501. if (len > lv)
  2502. len = lv;
  2503. if (len == 4) {
  2504. if (put_user_u32(val, optval_addr))
  2505. return -TARGET_EFAULT;
  2506. } else {
  2507. if (put_user_u8(val, optval_addr))
  2508. return -TARGET_EFAULT;
  2509. }
  2510. if (put_user_u32(len, optlen))
  2511. return -TARGET_EFAULT;
  2512. break;
  2513. case SOL_IP:
  2514. switch(optname) {
  2515. case IP_TOS:
  2516. case IP_TTL:
  2517. case IP_HDRINCL:
  2518. case IP_ROUTER_ALERT:
  2519. case IP_RECVOPTS:
  2520. case IP_RETOPTS:
  2521. case IP_PKTINFO:
  2522. case IP_MTU_DISCOVER:
  2523. case IP_RECVERR:
  2524. case IP_RECVTOS:
  2525. #ifdef IP_FREEBIND
  2526. case IP_FREEBIND:
  2527. #endif
  2528. case IP_MULTICAST_TTL:
  2529. case IP_MULTICAST_LOOP:
  2530. if (get_user_u32(len, optlen))
  2531. return -TARGET_EFAULT;
  2532. if (len < 0)
  2533. return -TARGET_EINVAL;
  2534. lv = sizeof(lv);
  2535. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2536. if (ret < 0)
  2537. return ret;
  2538. if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
  2539. len = 1;
  2540. if (put_user_u32(len, optlen)
  2541. || put_user_u8(val, optval_addr))
  2542. return -TARGET_EFAULT;
  2543. } else {
  2544. if (len > sizeof(int))
  2545. len = sizeof(int);
  2546. if (put_user_u32(len, optlen)
  2547. || put_user_u32(val, optval_addr))
  2548. return -TARGET_EFAULT;
  2549. }
  2550. break;
  2551. default:
  2552. ret = -TARGET_ENOPROTOOPT;
  2553. break;
  2554. }
  2555. break;
  2556. case SOL_IPV6:
  2557. switch (optname) {
  2558. case IPV6_MTU_DISCOVER:
  2559. case IPV6_MTU:
  2560. case IPV6_V6ONLY:
  2561. case IPV6_RECVPKTINFO:
  2562. case IPV6_UNICAST_HOPS:
  2563. case IPV6_MULTICAST_HOPS:
  2564. case IPV6_MULTICAST_LOOP:
  2565. case IPV6_RECVERR:
  2566. case IPV6_RECVHOPLIMIT:
  2567. case IPV6_2292HOPLIMIT:
  2568. case IPV6_CHECKSUM:
  2569. case IPV6_ADDRFORM:
  2570. case IPV6_2292PKTINFO:
  2571. case IPV6_RECVTCLASS:
  2572. case IPV6_RECVRTHDR:
  2573. case IPV6_2292RTHDR:
  2574. case IPV6_RECVHOPOPTS:
  2575. case IPV6_2292HOPOPTS:
  2576. case IPV6_RECVDSTOPTS:
  2577. case IPV6_2292DSTOPTS:
  2578. case IPV6_TCLASS:
  2579. case IPV6_ADDR_PREFERENCES:
  2580. #ifdef IPV6_RECVPATHMTU
  2581. case IPV6_RECVPATHMTU:
  2582. #endif
  2583. #ifdef IPV6_TRANSPARENT
  2584. case IPV6_TRANSPARENT:
  2585. #endif
  2586. #ifdef IPV6_FREEBIND
  2587. case IPV6_FREEBIND:
  2588. #endif
  2589. #ifdef IPV6_RECVORIGDSTADDR
  2590. case IPV6_RECVORIGDSTADDR:
  2591. #endif
  2592. if (get_user_u32(len, optlen))
  2593. return -TARGET_EFAULT;
  2594. if (len < 0)
  2595. return -TARGET_EINVAL;
  2596. lv = sizeof(lv);
  2597. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2598. if (ret < 0)
  2599. return ret;
  2600. if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
  2601. len = 1;
  2602. if (put_user_u32(len, optlen)
  2603. || put_user_u8(val, optval_addr))
  2604. return -TARGET_EFAULT;
  2605. } else {
  2606. if (len > sizeof(int))
  2607. len = sizeof(int);
  2608. if (put_user_u32(len, optlen)
  2609. || put_user_u32(val, optval_addr))
  2610. return -TARGET_EFAULT;
  2611. }
  2612. break;
  2613. default:
  2614. ret = -TARGET_ENOPROTOOPT;
  2615. break;
  2616. }
  2617. break;
  2618. #ifdef SOL_NETLINK
  2619. case SOL_NETLINK:
  2620. switch (optname) {
  2621. case NETLINK_PKTINFO:
  2622. case NETLINK_BROADCAST_ERROR:
  2623. case NETLINK_NO_ENOBUFS:
  2624. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
  2625. case NETLINK_LISTEN_ALL_NSID:
  2626. case NETLINK_CAP_ACK:
  2627. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
  2628. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
  2629. case NETLINK_EXT_ACK:
  2630. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2631. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
  2632. case NETLINK_GET_STRICT_CHK:
  2633. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2634. if (get_user_u32(len, optlen)) {
  2635. return -TARGET_EFAULT;
  2636. }
  2637. if (len != sizeof(val)) {
  2638. return -TARGET_EINVAL;
  2639. }
  2640. lv = len;
  2641. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2642. if (ret < 0) {
  2643. return ret;
  2644. }
  2645. if (put_user_u32(lv, optlen)
  2646. || put_user_u32(val, optval_addr)) {
  2647. return -TARGET_EFAULT;
  2648. }
  2649. break;
  2650. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
  2651. case NETLINK_LIST_MEMBERSHIPS:
  2652. {
  2653. uint32_t *results;
  2654. int i;
  2655. if (get_user_u32(len, optlen)) {
  2656. return -TARGET_EFAULT;
  2657. }
  2658. if (len < 0) {
  2659. return -TARGET_EINVAL;
  2660. }
  2661. results = lock_user(VERIFY_WRITE, optval_addr, len, 1);
  2662. if (!results && len > 0) {
  2663. return -TARGET_EFAULT;
  2664. }
  2665. lv = len;
  2666. ret = get_errno(getsockopt(sockfd, level, optname, results, &lv));
  2667. if (ret < 0) {
  2668. unlock_user(results, optval_addr, 0);
  2669. return ret;
  2670. }
  2671. /* swap host endianess to target endianess. */
  2672. for (i = 0; i < (len / sizeof(uint32_t)); i++) {
  2673. results[i] = tswap32(results[i]);
  2674. }
  2675. if (put_user_u32(lv, optlen)) {
  2676. return -TARGET_EFAULT;
  2677. }
  2678. unlock_user(results, optval_addr, 0);
  2679. break;
  2680. }
  2681. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
  2682. default:
  2683. goto unimplemented;
  2684. }
  2685. break;
  2686. #endif /* SOL_NETLINK */
  2687. default:
  2688. unimplemented:
  2689. qemu_log_mask(LOG_UNIMP,
  2690. "getsockopt level=%d optname=%d not yet supported\n",
  2691. level, optname);
  2692. ret = -TARGET_EOPNOTSUPP;
  2693. break;
  2694. }
  2695. return ret;
  2696. }
  2697. /* Convert target low/high pair representing file offset into the host
  2698. * low/high pair. This function doesn't handle offsets bigger than 64 bits
  2699. * as the kernel doesn't handle them either.
  2700. */
  2701. static void target_to_host_low_high(abi_ulong tlow,
  2702. abi_ulong thigh,
  2703. unsigned long *hlow,
  2704. unsigned long *hhigh)
  2705. {
  2706. uint64_t off = tlow |
  2707. ((unsigned long long)thigh << TARGET_LONG_BITS / 2) <<
  2708. TARGET_LONG_BITS / 2;
  2709. *hlow = off;
  2710. *hhigh = (off >> HOST_LONG_BITS / 2) >> HOST_LONG_BITS / 2;
  2711. }
  2712. static struct iovec *lock_iovec(int type, abi_ulong target_addr,
  2713. abi_ulong count, int copy)
  2714. {
  2715. struct target_iovec *target_vec;
  2716. struct iovec *vec;
  2717. abi_ulong total_len, max_len;
  2718. int i;
  2719. int err = 0;
  2720. bool bad_address = false;
  2721. if (count == 0) {
  2722. errno = 0;
  2723. return NULL;
  2724. }
  2725. if (count > IOV_MAX) {
  2726. errno = EINVAL;
  2727. return NULL;
  2728. }
  2729. vec = g_try_new0(struct iovec, count);
  2730. if (vec == NULL) {
  2731. errno = ENOMEM;
  2732. return NULL;
  2733. }
  2734. target_vec = lock_user(VERIFY_READ, target_addr,
  2735. count * sizeof(struct target_iovec), 1);
  2736. if (target_vec == NULL) {
  2737. err = EFAULT;
  2738. goto fail2;
  2739. }
  2740. /* ??? If host page size > target page size, this will result in a
  2741. value larger than what we can actually support. */
  2742. max_len = 0x7fffffff & TARGET_PAGE_MASK;
  2743. total_len = 0;
  2744. for (i = 0; i < count; i++) {
  2745. abi_ulong base = tswapal(target_vec[i].iov_base);
  2746. abi_long len = tswapal(target_vec[i].iov_len);
  2747. if (len < 0) {
  2748. err = EINVAL;
  2749. goto fail;
  2750. } else if (len == 0) {
  2751. /* Zero length pointer is ignored. */
  2752. vec[i].iov_base = 0;
  2753. } else {
  2754. vec[i].iov_base = lock_user(type, base, len, copy);
  2755. /* If the first buffer pointer is bad, this is a fault. But
  2756. * subsequent bad buffers will result in a partial write; this
  2757. * is realized by filling the vector with null pointers and
  2758. * zero lengths. */
  2759. if (!vec[i].iov_base) {
  2760. if (i == 0) {
  2761. err = EFAULT;
  2762. goto fail;
  2763. } else {
  2764. bad_address = true;
  2765. }
  2766. }
  2767. if (bad_address) {
  2768. len = 0;
  2769. }
  2770. if (len > max_len - total_len) {
  2771. len = max_len - total_len;
  2772. }
  2773. }
  2774. vec[i].iov_len = len;
  2775. total_len += len;
  2776. }
  2777. unlock_user(target_vec, target_addr, 0);
  2778. return vec;
  2779. fail:
  2780. while (--i >= 0) {
  2781. if (tswapal(target_vec[i].iov_len) > 0) {
  2782. unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0);
  2783. }
  2784. }
  2785. unlock_user(target_vec, target_addr, 0);
  2786. fail2:
  2787. g_free(vec);
  2788. errno = err;
  2789. return NULL;
  2790. }
  2791. static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
  2792. abi_ulong count, int copy)
  2793. {
  2794. struct target_iovec *target_vec;
  2795. int i;
  2796. target_vec = lock_user(VERIFY_READ, target_addr,
  2797. count * sizeof(struct target_iovec), 1);
  2798. if (target_vec) {
  2799. for (i = 0; i < count; i++) {
  2800. abi_ulong base = tswapal(target_vec[i].iov_base);
  2801. abi_long len = tswapal(target_vec[i].iov_len);
  2802. if (len < 0) {
  2803. break;
  2804. }
  2805. unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
  2806. }
  2807. unlock_user(target_vec, target_addr, 0);
  2808. }
  2809. g_free(vec);
  2810. }
  2811. static inline int target_to_host_sock_type(int *type)
  2812. {
  2813. int host_type = 0;
  2814. int target_type = *type;
  2815. switch (target_type & TARGET_SOCK_TYPE_MASK) {
  2816. case TARGET_SOCK_DGRAM:
  2817. host_type = SOCK_DGRAM;
  2818. break;
  2819. case TARGET_SOCK_STREAM:
  2820. host_type = SOCK_STREAM;
  2821. break;
  2822. default:
  2823. host_type = target_type & TARGET_SOCK_TYPE_MASK;
  2824. break;
  2825. }
  2826. if (target_type & TARGET_SOCK_CLOEXEC) {
  2827. #if defined(SOCK_CLOEXEC)
  2828. host_type |= SOCK_CLOEXEC;
  2829. #else
  2830. return -TARGET_EINVAL;
  2831. #endif
  2832. }
  2833. if (target_type & TARGET_SOCK_NONBLOCK) {
  2834. #if defined(SOCK_NONBLOCK)
  2835. host_type |= SOCK_NONBLOCK;
  2836. #elif !defined(O_NONBLOCK)
  2837. return -TARGET_EINVAL;
  2838. #endif
  2839. }
  2840. *type = host_type;
  2841. return 0;
  2842. }
  2843. /* Try to emulate socket type flags after socket creation. */
  2844. static int sock_flags_fixup(int fd, int target_type)
  2845. {
  2846. #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
  2847. if (target_type & TARGET_SOCK_NONBLOCK) {
  2848. int flags = fcntl(fd, F_GETFL);
  2849. if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) {
  2850. close(fd);
  2851. return -TARGET_EINVAL;
  2852. }
  2853. }
  2854. #endif
  2855. return fd;
  2856. }
  2857. /* do_socket() Must return target values and target errnos. */
  2858. static abi_long do_socket(int domain, int type, int protocol)
  2859. {
  2860. int target_type = type;
  2861. int ret;
  2862. ret = target_to_host_sock_type(&type);
  2863. if (ret) {
  2864. return ret;
  2865. }
  2866. if (domain == PF_NETLINK && !(
  2867. #ifdef CONFIG_RTNETLINK
  2868. protocol == NETLINK_ROUTE ||
  2869. #endif
  2870. protocol == NETLINK_KOBJECT_UEVENT ||
  2871. protocol == NETLINK_AUDIT)) {
  2872. return -TARGET_EPROTONOSUPPORT;
  2873. }
  2874. if (domain == AF_PACKET ||
  2875. (domain == AF_INET && type == SOCK_PACKET)) {
  2876. protocol = tswap16(protocol);
  2877. }
  2878. ret = get_errno(socket(domain, type, protocol));
  2879. if (ret >= 0) {
  2880. ret = sock_flags_fixup(ret, target_type);
  2881. if (type == SOCK_PACKET) {
  2882. /* Manage an obsolete case :
  2883. * if socket type is SOCK_PACKET, bind by name
  2884. */
  2885. fd_trans_register(ret, &target_packet_trans);
  2886. } else if (domain == PF_NETLINK) {
  2887. switch (protocol) {
  2888. #ifdef CONFIG_RTNETLINK
  2889. case NETLINK_ROUTE:
  2890. fd_trans_register(ret, &target_netlink_route_trans);
  2891. break;
  2892. #endif
  2893. case NETLINK_KOBJECT_UEVENT:
  2894. /* nothing to do: messages are strings */
  2895. break;
  2896. case NETLINK_AUDIT:
  2897. fd_trans_register(ret, &target_netlink_audit_trans);
  2898. break;
  2899. default:
  2900. g_assert_not_reached();
  2901. }
  2902. }
  2903. }
  2904. return ret;
  2905. }
  2906. /* do_bind() Must return target values and target errnos. */
  2907. static abi_long do_bind(int sockfd, abi_ulong target_addr,
  2908. socklen_t addrlen)
  2909. {
  2910. void *addr;
  2911. abi_long ret;
  2912. if ((int)addrlen < 0) {
  2913. return -TARGET_EINVAL;
  2914. }
  2915. addr = alloca(addrlen+1);
  2916. ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
  2917. if (ret)
  2918. return ret;
  2919. return get_errno(bind(sockfd, addr, addrlen));
  2920. }
  2921. /* do_connect() Must return target values and target errnos. */
  2922. static abi_long do_connect(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(safe_connect(sockfd, addr, addrlen));
  2935. }
  2936. /* do_sendrecvmsg_locked() Must return target values and target errnos. */
  2937. static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
  2938. int flags, int send)
  2939. {
  2940. abi_long ret, len;
  2941. struct msghdr msg;
  2942. abi_ulong count;
  2943. struct iovec *vec;
  2944. abi_ulong target_vec;
  2945. if (msgp->msg_name) {
  2946. msg.msg_namelen = tswap32(msgp->msg_namelen);
  2947. msg.msg_name = alloca(msg.msg_namelen+1);
  2948. ret = target_to_host_sockaddr(fd, msg.msg_name,
  2949. tswapal(msgp->msg_name),
  2950. msg.msg_namelen);
  2951. if (ret == -TARGET_EFAULT) {
  2952. /* For connected sockets msg_name and msg_namelen must
  2953. * be ignored, so returning EFAULT immediately is wrong.
  2954. * Instead, pass a bad msg_name to the host kernel, and
  2955. * let it decide whether to return EFAULT or not.
  2956. */
  2957. msg.msg_name = (void *)-1;
  2958. } else if (ret) {
  2959. goto out2;
  2960. }
  2961. } else {
  2962. msg.msg_name = NULL;
  2963. msg.msg_namelen = 0;
  2964. }
  2965. msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
  2966. msg.msg_control = alloca(msg.msg_controllen);
  2967. memset(msg.msg_control, 0, msg.msg_controllen);
  2968. msg.msg_flags = tswap32(msgp->msg_flags);
  2969. count = tswapal(msgp->msg_iovlen);
  2970. target_vec = tswapal(msgp->msg_iov);
  2971. if (count > IOV_MAX) {
  2972. /* sendrcvmsg returns a different errno for this condition than
  2973. * readv/writev, so we must catch it here before lock_iovec() does.
  2974. */
  2975. ret = -TARGET_EMSGSIZE;
  2976. goto out2;
  2977. }
  2978. vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
  2979. target_vec, count, send);
  2980. if (vec == NULL) {
  2981. ret = -host_to_target_errno(errno);
  2982. /* allow sending packet without any iov, e.g. with MSG_MORE flag */
  2983. if (!send || ret) {
  2984. goto out2;
  2985. }
  2986. }
  2987. msg.msg_iovlen = count;
  2988. msg.msg_iov = vec;
  2989. if (send) {
  2990. if (fd_trans_target_to_host_data(fd)) {
  2991. void *host_msg;
  2992. host_msg = g_malloc(msg.msg_iov->iov_len);
  2993. memcpy(host_msg, msg.msg_iov->iov_base, msg.msg_iov->iov_len);
  2994. ret = fd_trans_target_to_host_data(fd)(host_msg,
  2995. msg.msg_iov->iov_len);
  2996. if (ret >= 0) {
  2997. msg.msg_iov->iov_base = host_msg;
  2998. ret = get_errno(safe_sendmsg(fd, &msg, flags));
  2999. }
  3000. g_free(host_msg);
  3001. } else {
  3002. ret = target_to_host_cmsg(&msg, msgp);
  3003. if (ret == 0) {
  3004. ret = get_errno(safe_sendmsg(fd, &msg, flags));
  3005. }
  3006. }
  3007. } else {
  3008. ret = get_errno(safe_recvmsg(fd, &msg, flags));
  3009. if (!is_error(ret)) {
  3010. len = ret;
  3011. if (fd_trans_host_to_target_data(fd)) {
  3012. ret = fd_trans_host_to_target_data(fd)(msg.msg_iov->iov_base,
  3013. MIN(msg.msg_iov->iov_len, len));
  3014. }
  3015. if (!is_error(ret)) {
  3016. ret = host_to_target_cmsg(msgp, &msg);
  3017. }
  3018. if (!is_error(ret)) {
  3019. msgp->msg_namelen = tswap32(msg.msg_namelen);
  3020. msgp->msg_flags = tswap32(msg.msg_flags);
  3021. if (msg.msg_name != NULL && msg.msg_name != (void *)-1) {
  3022. ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
  3023. msg.msg_name, msg.msg_namelen);
  3024. if (ret) {
  3025. goto out;
  3026. }
  3027. }
  3028. ret = len;
  3029. }
  3030. }
  3031. }
  3032. out:
  3033. if (vec) {
  3034. unlock_iovec(vec, target_vec, count, !send);
  3035. }
  3036. out2:
  3037. return ret;
  3038. }
  3039. static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
  3040. int flags, int send)
  3041. {
  3042. abi_long ret;
  3043. struct target_msghdr *msgp;
  3044. if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
  3045. msgp,
  3046. target_msg,
  3047. send ? 1 : 0)) {
  3048. return -TARGET_EFAULT;
  3049. }
  3050. ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
  3051. unlock_user_struct(msgp, target_msg, send ? 0 : 1);
  3052. return ret;
  3053. }
  3054. /* We don't rely on the C library to have sendmmsg/recvmmsg support,
  3055. * so it might not have this *mmsg-specific flag either.
  3056. */
  3057. #ifndef MSG_WAITFORONE
  3058. #define MSG_WAITFORONE 0x10000
  3059. #endif
  3060. static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec,
  3061. unsigned int vlen, unsigned int flags,
  3062. int send)
  3063. {
  3064. struct target_mmsghdr *mmsgp;
  3065. abi_long ret = 0;
  3066. int i;
  3067. if (vlen > UIO_MAXIOV) {
  3068. vlen = UIO_MAXIOV;
  3069. }
  3070. mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1);
  3071. if (!mmsgp) {
  3072. return -TARGET_EFAULT;
  3073. }
  3074. for (i = 0; i < vlen; i++) {
  3075. ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send);
  3076. if (is_error(ret)) {
  3077. break;
  3078. }
  3079. mmsgp[i].msg_len = tswap32(ret);
  3080. /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
  3081. if (flags & MSG_WAITFORONE) {
  3082. flags |= MSG_DONTWAIT;
  3083. }
  3084. }
  3085. unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i);
  3086. /* Return number of datagrams sent if we sent any at all;
  3087. * otherwise return the error.
  3088. */
  3089. if (i) {
  3090. return i;
  3091. }
  3092. return ret;
  3093. }
  3094. /* do_accept4() Must return target values and target errnos. */
  3095. static abi_long do_accept4(int fd, abi_ulong target_addr,
  3096. abi_ulong target_addrlen_addr, int flags)
  3097. {
  3098. socklen_t addrlen, ret_addrlen;
  3099. void *addr;
  3100. abi_long ret;
  3101. int host_flags;
  3102. host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
  3103. if (target_addr == 0) {
  3104. return get_errno(safe_accept4(fd, NULL, NULL, host_flags));
  3105. }
  3106. /* linux returns EFAULT if addrlen pointer is invalid */
  3107. if (get_user_u32(addrlen, target_addrlen_addr))
  3108. return -TARGET_EFAULT;
  3109. if ((int)addrlen < 0) {
  3110. return -TARGET_EINVAL;
  3111. }
  3112. if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
  3113. return -TARGET_EFAULT;
  3114. }
  3115. addr = alloca(addrlen);
  3116. ret_addrlen = addrlen;
  3117. ret = get_errno(safe_accept4(fd, addr, &ret_addrlen, host_flags));
  3118. if (!is_error(ret)) {
  3119. host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
  3120. if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
  3121. ret = -TARGET_EFAULT;
  3122. }
  3123. }
  3124. return ret;
  3125. }
  3126. /* do_getpeername() Must return target values and target errnos. */
  3127. static abi_long do_getpeername(int fd, abi_ulong target_addr,
  3128. abi_ulong target_addrlen_addr)
  3129. {
  3130. socklen_t addrlen, ret_addrlen;
  3131. void *addr;
  3132. abi_long ret;
  3133. if (get_user_u32(addrlen, target_addrlen_addr))
  3134. return -TARGET_EFAULT;
  3135. if ((int)addrlen < 0) {
  3136. return -TARGET_EINVAL;
  3137. }
  3138. if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
  3139. return -TARGET_EFAULT;
  3140. }
  3141. addr = alloca(addrlen);
  3142. ret_addrlen = addrlen;
  3143. ret = get_errno(getpeername(fd, addr, &ret_addrlen));
  3144. if (!is_error(ret)) {
  3145. host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
  3146. if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
  3147. ret = -TARGET_EFAULT;
  3148. }
  3149. }
  3150. return ret;
  3151. }
  3152. /* do_getsockname() Must return target values and target errnos. */
  3153. static abi_long do_getsockname(int fd, abi_ulong target_addr,
  3154. abi_ulong target_addrlen_addr)
  3155. {
  3156. socklen_t addrlen, ret_addrlen;
  3157. void *addr;
  3158. abi_long ret;
  3159. if (get_user_u32(addrlen, target_addrlen_addr))
  3160. return -TARGET_EFAULT;
  3161. if ((int)addrlen < 0) {
  3162. return -TARGET_EINVAL;
  3163. }
  3164. if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
  3165. return -TARGET_EFAULT;
  3166. }
  3167. addr = alloca(addrlen);
  3168. ret_addrlen = addrlen;
  3169. ret = get_errno(getsockname(fd, addr, &ret_addrlen));
  3170. if (!is_error(ret)) {
  3171. host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
  3172. if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
  3173. ret = -TARGET_EFAULT;
  3174. }
  3175. }
  3176. return ret;
  3177. }
  3178. /* do_socketpair() Must return target values and target errnos. */
  3179. static abi_long do_socketpair(int domain, int type, int protocol,
  3180. abi_ulong target_tab_addr)
  3181. {
  3182. int tab[2];
  3183. abi_long ret;
  3184. target_to_host_sock_type(&type);
  3185. ret = get_errno(socketpair(domain, type, protocol, tab));
  3186. if (!is_error(ret)) {
  3187. if (put_user_s32(tab[0], target_tab_addr)
  3188. || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
  3189. ret = -TARGET_EFAULT;
  3190. }
  3191. return ret;
  3192. }
  3193. /* do_sendto() Must return target values and target errnos. */
  3194. static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
  3195. abi_ulong target_addr, socklen_t addrlen)
  3196. {
  3197. void *addr;
  3198. void *host_msg;
  3199. void *copy_msg = NULL;
  3200. abi_long ret;
  3201. if ((int)addrlen < 0) {
  3202. return -TARGET_EINVAL;
  3203. }
  3204. host_msg = lock_user(VERIFY_READ, msg, len, 1);
  3205. if (!host_msg)
  3206. return -TARGET_EFAULT;
  3207. if (fd_trans_target_to_host_data(fd)) {
  3208. copy_msg = host_msg;
  3209. host_msg = g_malloc(len);
  3210. memcpy(host_msg, copy_msg, len);
  3211. ret = fd_trans_target_to_host_data(fd)(host_msg, len);
  3212. if (ret < 0) {
  3213. goto fail;
  3214. }
  3215. }
  3216. if (target_addr) {
  3217. addr = alloca(addrlen+1);
  3218. ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen);
  3219. if (ret) {
  3220. goto fail;
  3221. }
  3222. ret = get_errno(safe_sendto(fd, host_msg, len, flags, addr, addrlen));
  3223. } else {
  3224. ret = get_errno(safe_sendto(fd, host_msg, len, flags, NULL, 0));
  3225. }
  3226. fail:
  3227. if (copy_msg) {
  3228. g_free(host_msg);
  3229. host_msg = copy_msg;
  3230. }
  3231. unlock_user(host_msg, msg, 0);
  3232. return ret;
  3233. }
  3234. /* do_recvfrom() Must return target values and target errnos. */
  3235. static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
  3236. abi_ulong target_addr,
  3237. abi_ulong target_addrlen)
  3238. {
  3239. socklen_t addrlen, ret_addrlen;
  3240. void *addr;
  3241. void *host_msg;
  3242. abi_long ret;
  3243. if (!msg) {
  3244. host_msg = NULL;
  3245. } else {
  3246. host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
  3247. if (!host_msg) {
  3248. return -TARGET_EFAULT;
  3249. }
  3250. }
  3251. if (target_addr) {
  3252. if (get_user_u32(addrlen, target_addrlen)) {
  3253. ret = -TARGET_EFAULT;
  3254. goto fail;
  3255. }
  3256. if ((int)addrlen < 0) {
  3257. ret = -TARGET_EINVAL;
  3258. goto fail;
  3259. }
  3260. addr = alloca(addrlen);
  3261. ret_addrlen = addrlen;
  3262. ret = get_errno(safe_recvfrom(fd, host_msg, len, flags,
  3263. addr, &ret_addrlen));
  3264. } else {
  3265. addr = NULL; /* To keep compiler quiet. */
  3266. addrlen = 0; /* To keep compiler quiet. */
  3267. ret = get_errno(safe_recvfrom(fd, host_msg, len, flags, NULL, 0));
  3268. }
  3269. if (!is_error(ret)) {
  3270. if (fd_trans_host_to_target_data(fd)) {
  3271. abi_long trans;
  3272. trans = fd_trans_host_to_target_data(fd)(host_msg, MIN(ret, len));
  3273. if (is_error(trans)) {
  3274. ret = trans;
  3275. goto fail;
  3276. }
  3277. }
  3278. if (target_addr) {
  3279. host_to_target_sockaddr(target_addr, addr,
  3280. MIN(addrlen, ret_addrlen));
  3281. if (put_user_u32(ret_addrlen, target_addrlen)) {
  3282. ret = -TARGET_EFAULT;
  3283. goto fail;
  3284. }
  3285. }
  3286. unlock_user(host_msg, msg, len);
  3287. } else {
  3288. fail:
  3289. unlock_user(host_msg, msg, 0);
  3290. }
  3291. return ret;
  3292. }
  3293. #ifdef TARGET_NR_socketcall
  3294. /* do_socketcall() must return target values and target errnos. */
  3295. static abi_long do_socketcall(int num, abi_ulong vptr)
  3296. {
  3297. static const unsigned nargs[] = { /* number of arguments per operation */
  3298. [TARGET_SYS_SOCKET] = 3, /* domain, type, protocol */
  3299. [TARGET_SYS_BIND] = 3, /* fd, addr, addrlen */
  3300. [TARGET_SYS_CONNECT] = 3, /* fd, addr, addrlen */
  3301. [TARGET_SYS_LISTEN] = 2, /* fd, backlog */
  3302. [TARGET_SYS_ACCEPT] = 3, /* fd, addr, addrlen */
  3303. [TARGET_SYS_GETSOCKNAME] = 3, /* fd, addr, addrlen */
  3304. [TARGET_SYS_GETPEERNAME] = 3, /* fd, addr, addrlen */
  3305. [TARGET_SYS_SOCKETPAIR] = 4, /* domain, type, protocol, tab */
  3306. [TARGET_SYS_SEND] = 4, /* fd, msg, len, flags */
  3307. [TARGET_SYS_RECV] = 4, /* fd, msg, len, flags */
  3308. [TARGET_SYS_SENDTO] = 6, /* fd, msg, len, flags, addr, addrlen */
  3309. [TARGET_SYS_RECVFROM] = 6, /* fd, msg, len, flags, addr, addrlen */
  3310. [TARGET_SYS_SHUTDOWN] = 2, /* fd, how */
  3311. [TARGET_SYS_SETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
  3312. [TARGET_SYS_GETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
  3313. [TARGET_SYS_SENDMSG] = 3, /* fd, msg, flags */
  3314. [TARGET_SYS_RECVMSG] = 3, /* fd, msg, flags */
  3315. [TARGET_SYS_ACCEPT4] = 4, /* fd, addr, addrlen, flags */
  3316. [TARGET_SYS_RECVMMSG] = 4, /* fd, msgvec, vlen, flags */
  3317. [TARGET_SYS_SENDMMSG] = 4, /* fd, msgvec, vlen, flags */
  3318. };
  3319. abi_long a[6]; /* max 6 args */
  3320. unsigned i;
  3321. /* check the range of the first argument num */
  3322. /* (TARGET_SYS_SENDMMSG is the highest among TARGET_SYS_xxx) */
  3323. if (num < 1 || num > TARGET_SYS_SENDMMSG) {
  3324. return -TARGET_EINVAL;
  3325. }
  3326. /* ensure we have space for args */
  3327. if (nargs[num] > ARRAY_SIZE(a)) {
  3328. return -TARGET_EINVAL;
  3329. }
  3330. /* collect the arguments in a[] according to nargs[] */
  3331. for (i = 0; i < nargs[num]; ++i) {
  3332. if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) {
  3333. return -TARGET_EFAULT;
  3334. }
  3335. }
  3336. /* now when we have the args, invoke the appropriate underlying function */
  3337. switch (num) {
  3338. case TARGET_SYS_SOCKET: /* domain, type, protocol */
  3339. return do_socket(a[0], a[1], a[2]);
  3340. case TARGET_SYS_BIND: /* sockfd, addr, addrlen */
  3341. return do_bind(a[0], a[1], a[2]);
  3342. case TARGET_SYS_CONNECT: /* sockfd, addr, addrlen */
  3343. return do_connect(a[0], a[1], a[2]);
  3344. case TARGET_SYS_LISTEN: /* sockfd, backlog */
  3345. return get_errno(listen(a[0], a[1]));
  3346. case TARGET_SYS_ACCEPT: /* sockfd, addr, addrlen */
  3347. return do_accept4(a[0], a[1], a[2], 0);
  3348. case TARGET_SYS_GETSOCKNAME: /* sockfd, addr, addrlen */
  3349. return do_getsockname(a[0], a[1], a[2]);
  3350. case TARGET_SYS_GETPEERNAME: /* sockfd, addr, addrlen */
  3351. return do_getpeername(a[0], a[1], a[2]);
  3352. case TARGET_SYS_SOCKETPAIR: /* domain, type, protocol, tab */
  3353. return do_socketpair(a[0], a[1], a[2], a[3]);
  3354. case TARGET_SYS_SEND: /* sockfd, msg, len, flags */
  3355. return do_sendto(a[0], a[1], a[2], a[3], 0, 0);
  3356. case TARGET_SYS_RECV: /* sockfd, msg, len, flags */
  3357. return do_recvfrom(a[0], a[1], a[2], a[3], 0, 0);
  3358. case TARGET_SYS_SENDTO: /* sockfd, msg, len, flags, addr, addrlen */
  3359. return do_sendto(a[0], a[1], a[2], a[3], a[4], a[5]);
  3360. case TARGET_SYS_RECVFROM: /* sockfd, msg, len, flags, addr, addrlen */
  3361. return do_recvfrom(a[0], a[1], a[2], a[3], a[4], a[5]);
  3362. case TARGET_SYS_SHUTDOWN: /* sockfd, how */
  3363. return get_errno(shutdown(a[0], a[1]));
  3364. case TARGET_SYS_SETSOCKOPT: /* sockfd, level, optname, optval, optlen */
  3365. return do_setsockopt(a[0], a[1], a[2], a[3], a[4]);
  3366. case TARGET_SYS_GETSOCKOPT: /* sockfd, level, optname, optval, optlen */
  3367. return do_getsockopt(a[0], a[1], a[2], a[3], a[4]);
  3368. case TARGET_SYS_SENDMSG: /* sockfd, msg, flags */
  3369. return do_sendrecvmsg(a[0], a[1], a[2], 1);
  3370. case TARGET_SYS_RECVMSG: /* sockfd, msg, flags */
  3371. return do_sendrecvmsg(a[0], a[1], a[2], 0);
  3372. case TARGET_SYS_ACCEPT4: /* sockfd, addr, addrlen, flags */
  3373. return do_accept4(a[0], a[1], a[2], a[3]);
  3374. case TARGET_SYS_RECVMMSG: /* sockfd, msgvec, vlen, flags */
  3375. return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 0);
  3376. case TARGET_SYS_SENDMMSG: /* sockfd, msgvec, vlen, flags */
  3377. return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 1);
  3378. default:
  3379. qemu_log_mask(LOG_UNIMP, "Unsupported socketcall: %d\n", num);
  3380. return -TARGET_EINVAL;
  3381. }
  3382. }
  3383. #endif
  3384. #define N_SHM_REGIONS 32
  3385. static struct shm_region {
  3386. abi_ulong start;
  3387. abi_ulong size;
  3388. bool in_use;
  3389. } shm_regions[N_SHM_REGIONS];
  3390. #ifndef TARGET_SEMID64_DS
  3391. /* asm-generic version of this struct */
  3392. struct target_semid64_ds
  3393. {
  3394. struct target_ipc_perm sem_perm;
  3395. abi_ulong sem_otime;
  3396. #if TARGET_ABI_BITS == 32
  3397. abi_ulong __unused1;
  3398. #endif
  3399. abi_ulong sem_ctime;
  3400. #if TARGET_ABI_BITS == 32
  3401. abi_ulong __unused2;
  3402. #endif
  3403. abi_ulong sem_nsems;
  3404. abi_ulong __unused3;
  3405. abi_ulong __unused4;
  3406. };
  3407. #endif
  3408. static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
  3409. abi_ulong target_addr)
  3410. {
  3411. struct target_ipc_perm *target_ip;
  3412. struct target_semid64_ds *target_sd;
  3413. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  3414. return -TARGET_EFAULT;
  3415. target_ip = &(target_sd->sem_perm);
  3416. host_ip->__key = tswap32(target_ip->__key);
  3417. host_ip->uid = tswap32(target_ip->uid);
  3418. host_ip->gid = tswap32(target_ip->gid);
  3419. host_ip->cuid = tswap32(target_ip->cuid);
  3420. host_ip->cgid = tswap32(target_ip->cgid);
  3421. #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
  3422. host_ip->mode = tswap32(target_ip->mode);
  3423. #else
  3424. host_ip->mode = tswap16(target_ip->mode);
  3425. #endif
  3426. #if defined(TARGET_PPC)
  3427. host_ip->__seq = tswap32(target_ip->__seq);
  3428. #else
  3429. host_ip->__seq = tswap16(target_ip->__seq);
  3430. #endif
  3431. unlock_user_struct(target_sd, target_addr, 0);
  3432. return 0;
  3433. }
  3434. static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
  3435. struct ipc_perm *host_ip)
  3436. {
  3437. struct target_ipc_perm *target_ip;
  3438. struct target_semid64_ds *target_sd;
  3439. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  3440. return -TARGET_EFAULT;
  3441. target_ip = &(target_sd->sem_perm);
  3442. target_ip->__key = tswap32(host_ip->__key);
  3443. target_ip->uid = tswap32(host_ip->uid);
  3444. target_ip->gid = tswap32(host_ip->gid);
  3445. target_ip->cuid = tswap32(host_ip->cuid);
  3446. target_ip->cgid = tswap32(host_ip->cgid);
  3447. #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
  3448. target_ip->mode = tswap32(host_ip->mode);
  3449. #else
  3450. target_ip->mode = tswap16(host_ip->mode);
  3451. #endif
  3452. #if defined(TARGET_PPC)
  3453. target_ip->__seq = tswap32(host_ip->__seq);
  3454. #else
  3455. target_ip->__seq = tswap16(host_ip->__seq);
  3456. #endif
  3457. unlock_user_struct(target_sd, target_addr, 1);
  3458. return 0;
  3459. }
  3460. static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
  3461. abi_ulong target_addr)
  3462. {
  3463. struct target_semid64_ds *target_sd;
  3464. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  3465. return -TARGET_EFAULT;
  3466. if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
  3467. return -TARGET_EFAULT;
  3468. host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
  3469. host_sd->sem_otime = tswapal(target_sd->sem_otime);
  3470. host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
  3471. unlock_user_struct(target_sd, target_addr, 0);
  3472. return 0;
  3473. }
  3474. static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
  3475. struct semid_ds *host_sd)
  3476. {
  3477. struct target_semid64_ds *target_sd;
  3478. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  3479. return -TARGET_EFAULT;
  3480. if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
  3481. return -TARGET_EFAULT;
  3482. target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
  3483. target_sd->sem_otime = tswapal(host_sd->sem_otime);
  3484. target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
  3485. unlock_user_struct(target_sd, target_addr, 1);
  3486. return 0;
  3487. }
  3488. struct target_seminfo {
  3489. int semmap;
  3490. int semmni;
  3491. int semmns;
  3492. int semmnu;
  3493. int semmsl;
  3494. int semopm;
  3495. int semume;
  3496. int semusz;
  3497. int semvmx;
  3498. int semaem;
  3499. };
  3500. static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
  3501. struct seminfo *host_seminfo)
  3502. {
  3503. struct target_seminfo *target_seminfo;
  3504. if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
  3505. return -TARGET_EFAULT;
  3506. __put_user(host_seminfo->semmap, &target_seminfo->semmap);
  3507. __put_user(host_seminfo->semmni, &target_seminfo->semmni);
  3508. __put_user(host_seminfo->semmns, &target_seminfo->semmns);
  3509. __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
  3510. __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
  3511. __put_user(host_seminfo->semopm, &target_seminfo->semopm);
  3512. __put_user(host_seminfo->semume, &target_seminfo->semume);
  3513. __put_user(host_seminfo->semusz, &target_seminfo->semusz);
  3514. __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
  3515. __put_user(host_seminfo->semaem, &target_seminfo->semaem);
  3516. unlock_user_struct(target_seminfo, target_addr, 1);
  3517. return 0;
  3518. }
  3519. union semun {
  3520. int val;
  3521. struct semid_ds *buf;
  3522. unsigned short *array;
  3523. struct seminfo *__buf;
  3524. };
  3525. union target_semun {
  3526. int val;
  3527. abi_ulong buf;
  3528. abi_ulong array;
  3529. abi_ulong __buf;
  3530. };
  3531. static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
  3532. abi_ulong target_addr)
  3533. {
  3534. int nsems;
  3535. unsigned short *array;
  3536. union semun semun;
  3537. struct semid_ds semid_ds;
  3538. int i, ret;
  3539. semun.buf = &semid_ds;
  3540. ret = semctl(semid, 0, IPC_STAT, semun);
  3541. if (ret == -1)
  3542. return get_errno(ret);
  3543. nsems = semid_ds.sem_nsems;
  3544. *host_array = g_try_new(unsigned short, nsems);
  3545. if (!*host_array) {
  3546. return -TARGET_ENOMEM;
  3547. }
  3548. array = lock_user(VERIFY_READ, target_addr,
  3549. nsems*sizeof(unsigned short), 1);
  3550. if (!array) {
  3551. g_free(*host_array);
  3552. return -TARGET_EFAULT;
  3553. }
  3554. for(i=0; i<nsems; i++) {
  3555. __get_user((*host_array)[i], &array[i]);
  3556. }
  3557. unlock_user(array, target_addr, 0);
  3558. return 0;
  3559. }
  3560. static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
  3561. unsigned short **host_array)
  3562. {
  3563. int nsems;
  3564. unsigned short *array;
  3565. union semun semun;
  3566. struct semid_ds semid_ds;
  3567. int i, ret;
  3568. semun.buf = &semid_ds;
  3569. ret = semctl(semid, 0, IPC_STAT, semun);
  3570. if (ret == -1)
  3571. return get_errno(ret);
  3572. nsems = semid_ds.sem_nsems;
  3573. array = lock_user(VERIFY_WRITE, target_addr,
  3574. nsems*sizeof(unsigned short), 0);
  3575. if (!array)
  3576. return -TARGET_EFAULT;
  3577. for(i=0; i<nsems; i++) {
  3578. __put_user((*host_array)[i], &array[i]);
  3579. }
  3580. g_free(*host_array);
  3581. unlock_user(array, target_addr, 1);
  3582. return 0;
  3583. }
  3584. static inline abi_long do_semctl(int semid, int semnum, int cmd,
  3585. abi_ulong target_arg)
  3586. {
  3587. union target_semun target_su = { .buf = target_arg };
  3588. union semun arg;
  3589. struct semid_ds dsarg;
  3590. unsigned short *array = NULL;
  3591. struct seminfo seminfo;
  3592. abi_long ret = -TARGET_EINVAL;
  3593. abi_long err;
  3594. cmd &= 0xff;
  3595. switch( cmd ) {
  3596. case GETVAL:
  3597. case SETVAL:
  3598. /* In 64 bit cross-endian situations, we will erroneously pick up
  3599. * the wrong half of the union for the "val" element. To rectify
  3600. * this, the entire 8-byte structure is byteswapped, followed by
  3601. * a swap of the 4 byte val field. In other cases, the data is
  3602. * already in proper host byte order. */
  3603. if (sizeof(target_su.val) != (sizeof(target_su.buf))) {
  3604. target_su.buf = tswapal(target_su.buf);
  3605. arg.val = tswap32(target_su.val);
  3606. } else {
  3607. arg.val = target_su.val;
  3608. }
  3609. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3610. break;
  3611. case GETALL:
  3612. case SETALL:
  3613. err = target_to_host_semarray(semid, &array, target_su.array);
  3614. if (err)
  3615. return err;
  3616. arg.array = array;
  3617. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3618. err = host_to_target_semarray(semid, target_su.array, &array);
  3619. if (err)
  3620. return err;
  3621. break;
  3622. case IPC_STAT:
  3623. case IPC_SET:
  3624. case SEM_STAT:
  3625. err = target_to_host_semid_ds(&dsarg, target_su.buf);
  3626. if (err)
  3627. return err;
  3628. arg.buf = &dsarg;
  3629. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3630. err = host_to_target_semid_ds(target_su.buf, &dsarg);
  3631. if (err)
  3632. return err;
  3633. break;
  3634. case IPC_INFO:
  3635. case SEM_INFO:
  3636. arg.__buf = &seminfo;
  3637. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3638. err = host_to_target_seminfo(target_su.__buf, &seminfo);
  3639. if (err)
  3640. return err;
  3641. break;
  3642. case IPC_RMID:
  3643. case GETPID:
  3644. case GETNCNT:
  3645. case GETZCNT:
  3646. ret = get_errno(semctl(semid, semnum, cmd, NULL));
  3647. break;
  3648. }
  3649. return ret;
  3650. }
  3651. struct target_sembuf {
  3652. unsigned short sem_num;
  3653. short sem_op;
  3654. short sem_flg;
  3655. };
  3656. static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
  3657. abi_ulong target_addr,
  3658. unsigned nsops)
  3659. {
  3660. struct target_sembuf *target_sembuf;
  3661. int i;
  3662. target_sembuf = lock_user(VERIFY_READ, target_addr,
  3663. nsops*sizeof(struct target_sembuf), 1);
  3664. if (!target_sembuf)
  3665. return -TARGET_EFAULT;
  3666. for(i=0; i<nsops; i++) {
  3667. __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
  3668. __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
  3669. __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
  3670. }
  3671. unlock_user(target_sembuf, target_addr, 0);
  3672. return 0;
  3673. }
  3674. #if defined(TARGET_NR_ipc) || defined(TARGET_NR_semop) || \
  3675. defined(TARGET_NR_semtimedop) || defined(TARGET_NR_semtimedop_time64)
  3676. /*
  3677. * This macro is required to handle the s390 variants, which passes the
  3678. * arguments in a different order than default.
  3679. */
  3680. #ifdef __s390x__
  3681. #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
  3682. (__nsops), (__timeout), (__sops)
  3683. #else
  3684. #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
  3685. (__nsops), 0, (__sops), (__timeout)
  3686. #endif
  3687. static inline abi_long do_semtimedop(int semid,
  3688. abi_long ptr,
  3689. unsigned nsops,
  3690. abi_long timeout, bool time64)
  3691. {
  3692. struct sembuf *sops;
  3693. struct timespec ts, *pts = NULL;
  3694. abi_long ret;
  3695. if (timeout) {
  3696. pts = &ts;
  3697. if (time64) {
  3698. if (target_to_host_timespec64(pts, timeout)) {
  3699. return -TARGET_EFAULT;
  3700. }
  3701. } else {
  3702. if (target_to_host_timespec(pts, timeout)) {
  3703. return -TARGET_EFAULT;
  3704. }
  3705. }
  3706. }
  3707. if (nsops > TARGET_SEMOPM) {
  3708. return -TARGET_E2BIG;
  3709. }
  3710. sops = g_new(struct sembuf, nsops);
  3711. if (target_to_host_sembuf(sops, ptr, nsops)) {
  3712. g_free(sops);
  3713. return -TARGET_EFAULT;
  3714. }
  3715. ret = -TARGET_ENOSYS;
  3716. #ifdef __NR_semtimedop
  3717. ret = get_errno(safe_semtimedop(semid, sops, nsops, pts));
  3718. #endif
  3719. #ifdef __NR_ipc
  3720. if (ret == -TARGET_ENOSYS) {
  3721. ret = get_errno(safe_ipc(IPCOP_semtimedop, semid,
  3722. SEMTIMEDOP_IPC_ARGS(nsops, sops, (long)pts)));
  3723. }
  3724. #endif
  3725. g_free(sops);
  3726. return ret;
  3727. }
  3728. #endif
  3729. struct target_msqid_ds
  3730. {
  3731. struct target_ipc_perm msg_perm;
  3732. abi_ulong msg_stime;
  3733. #if TARGET_ABI_BITS == 32
  3734. abi_ulong __unused1;
  3735. #endif
  3736. abi_ulong msg_rtime;
  3737. #if TARGET_ABI_BITS == 32
  3738. abi_ulong __unused2;
  3739. #endif
  3740. abi_ulong msg_ctime;
  3741. #if TARGET_ABI_BITS == 32
  3742. abi_ulong __unused3;
  3743. #endif
  3744. abi_ulong __msg_cbytes;
  3745. abi_ulong msg_qnum;
  3746. abi_ulong msg_qbytes;
  3747. abi_ulong msg_lspid;
  3748. abi_ulong msg_lrpid;
  3749. abi_ulong __unused4;
  3750. abi_ulong __unused5;
  3751. };
  3752. static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
  3753. abi_ulong target_addr)
  3754. {
  3755. struct target_msqid_ds *target_md;
  3756. if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
  3757. return -TARGET_EFAULT;
  3758. if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
  3759. return -TARGET_EFAULT;
  3760. host_md->msg_stime = tswapal(target_md->msg_stime);
  3761. host_md->msg_rtime = tswapal(target_md->msg_rtime);
  3762. host_md->msg_ctime = tswapal(target_md->msg_ctime);
  3763. host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
  3764. host_md->msg_qnum = tswapal(target_md->msg_qnum);
  3765. host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
  3766. host_md->msg_lspid = tswapal(target_md->msg_lspid);
  3767. host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
  3768. unlock_user_struct(target_md, target_addr, 0);
  3769. return 0;
  3770. }
  3771. static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
  3772. struct msqid_ds *host_md)
  3773. {
  3774. struct target_msqid_ds *target_md;
  3775. if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
  3776. return -TARGET_EFAULT;
  3777. if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
  3778. return -TARGET_EFAULT;
  3779. target_md->msg_stime = tswapal(host_md->msg_stime);
  3780. target_md->msg_rtime = tswapal(host_md->msg_rtime);
  3781. target_md->msg_ctime = tswapal(host_md->msg_ctime);
  3782. target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
  3783. target_md->msg_qnum = tswapal(host_md->msg_qnum);
  3784. target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
  3785. target_md->msg_lspid = tswapal(host_md->msg_lspid);
  3786. target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
  3787. unlock_user_struct(target_md, target_addr, 1);
  3788. return 0;
  3789. }
  3790. struct target_msginfo {
  3791. int msgpool;
  3792. int msgmap;
  3793. int msgmax;
  3794. int msgmnb;
  3795. int msgmni;
  3796. int msgssz;
  3797. int msgtql;
  3798. unsigned short int msgseg;
  3799. };
  3800. static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
  3801. struct msginfo *host_msginfo)
  3802. {
  3803. struct target_msginfo *target_msginfo;
  3804. if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
  3805. return -TARGET_EFAULT;
  3806. __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
  3807. __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
  3808. __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
  3809. __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
  3810. __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
  3811. __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
  3812. __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
  3813. __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
  3814. unlock_user_struct(target_msginfo, target_addr, 1);
  3815. return 0;
  3816. }
  3817. static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
  3818. {
  3819. struct msqid_ds dsarg;
  3820. struct msginfo msginfo;
  3821. abi_long ret = -TARGET_EINVAL;
  3822. cmd &= 0xff;
  3823. switch (cmd) {
  3824. case IPC_STAT:
  3825. case IPC_SET:
  3826. case MSG_STAT:
  3827. if (target_to_host_msqid_ds(&dsarg,ptr))
  3828. return -TARGET_EFAULT;
  3829. ret = get_errno(msgctl(msgid, cmd, &dsarg));
  3830. if (host_to_target_msqid_ds(ptr,&dsarg))
  3831. return -TARGET_EFAULT;
  3832. break;
  3833. case IPC_RMID:
  3834. ret = get_errno(msgctl(msgid, cmd, NULL));
  3835. break;
  3836. case IPC_INFO:
  3837. case MSG_INFO:
  3838. ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
  3839. if (host_to_target_msginfo(ptr, &msginfo))
  3840. return -TARGET_EFAULT;
  3841. break;
  3842. }
  3843. return ret;
  3844. }
  3845. struct target_msgbuf {
  3846. abi_long mtype;
  3847. char mtext[1];
  3848. };
  3849. static inline abi_long do_msgsnd(int msqid, abi_long msgp,
  3850. ssize_t msgsz, int msgflg)
  3851. {
  3852. struct target_msgbuf *target_mb;
  3853. struct msgbuf *host_mb;
  3854. abi_long ret = 0;
  3855. if (msgsz < 0) {
  3856. return -TARGET_EINVAL;
  3857. }
  3858. if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
  3859. return -TARGET_EFAULT;
  3860. host_mb = g_try_malloc(msgsz + sizeof(long));
  3861. if (!host_mb) {
  3862. unlock_user_struct(target_mb, msgp, 0);
  3863. return -TARGET_ENOMEM;
  3864. }
  3865. host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
  3866. memcpy(host_mb->mtext, target_mb->mtext, msgsz);
  3867. ret = -TARGET_ENOSYS;
  3868. #ifdef __NR_msgsnd
  3869. ret = get_errno(safe_msgsnd(msqid, host_mb, msgsz, msgflg));
  3870. #endif
  3871. #ifdef __NR_ipc
  3872. if (ret == -TARGET_ENOSYS) {
  3873. #ifdef __s390x__
  3874. ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
  3875. host_mb));
  3876. #else
  3877. ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
  3878. host_mb, 0));
  3879. #endif
  3880. }
  3881. #endif
  3882. g_free(host_mb);
  3883. unlock_user_struct(target_mb, msgp, 0);
  3884. return ret;
  3885. }
  3886. #ifdef __NR_ipc
  3887. #if defined(__sparc__)
  3888. /* SPARC for msgrcv it does not use the kludge on final 2 arguments. */
  3889. #define MSGRCV_ARGS(__msgp, __msgtyp) __msgp, __msgtyp
  3890. #elif defined(__s390x__)
  3891. /* The s390 sys_ipc variant has only five parameters. */
  3892. #define MSGRCV_ARGS(__msgp, __msgtyp) \
  3893. ((long int[]){(long int)__msgp, __msgtyp})
  3894. #else
  3895. #define MSGRCV_ARGS(__msgp, __msgtyp) \
  3896. ((long int[]){(long int)__msgp, __msgtyp}), 0
  3897. #endif
  3898. #endif
  3899. static inline abi_long do_msgrcv(int msqid, abi_long msgp,
  3900. ssize_t msgsz, abi_long msgtyp,
  3901. int msgflg)
  3902. {
  3903. struct target_msgbuf *target_mb;
  3904. char *target_mtext;
  3905. struct msgbuf *host_mb;
  3906. abi_long ret = 0;
  3907. if (msgsz < 0) {
  3908. return -TARGET_EINVAL;
  3909. }
  3910. if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
  3911. return -TARGET_EFAULT;
  3912. host_mb = g_try_malloc(msgsz + sizeof(long));
  3913. if (!host_mb) {
  3914. ret = -TARGET_ENOMEM;
  3915. goto end;
  3916. }
  3917. ret = -TARGET_ENOSYS;
  3918. #ifdef __NR_msgrcv
  3919. ret = get_errno(safe_msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
  3920. #endif
  3921. #ifdef __NR_ipc
  3922. if (ret == -TARGET_ENOSYS) {
  3923. ret = get_errno(safe_ipc(IPCOP_CALL(1, IPCOP_msgrcv), msqid, msgsz,
  3924. msgflg, MSGRCV_ARGS(host_mb, msgtyp)));
  3925. }
  3926. #endif
  3927. if (ret > 0) {
  3928. abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
  3929. target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
  3930. if (!target_mtext) {
  3931. ret = -TARGET_EFAULT;
  3932. goto end;
  3933. }
  3934. memcpy(target_mb->mtext, host_mb->mtext, ret);
  3935. unlock_user(target_mtext, target_mtext_addr, ret);
  3936. }
  3937. target_mb->mtype = tswapal(host_mb->mtype);
  3938. end:
  3939. if (target_mb)
  3940. unlock_user_struct(target_mb, msgp, 1);
  3941. g_free(host_mb);
  3942. return ret;
  3943. }
  3944. static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
  3945. abi_ulong target_addr)
  3946. {
  3947. struct target_shmid_ds *target_sd;
  3948. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  3949. return -TARGET_EFAULT;
  3950. if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
  3951. return -TARGET_EFAULT;
  3952. __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
  3953. __get_user(host_sd->shm_atime, &target_sd->shm_atime);
  3954. __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
  3955. __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
  3956. __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
  3957. __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
  3958. __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
  3959. unlock_user_struct(target_sd, target_addr, 0);
  3960. return 0;
  3961. }
  3962. static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
  3963. struct shmid_ds *host_sd)
  3964. {
  3965. struct target_shmid_ds *target_sd;
  3966. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  3967. return -TARGET_EFAULT;
  3968. if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
  3969. return -TARGET_EFAULT;
  3970. __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
  3971. __put_user(host_sd->shm_atime, &target_sd->shm_atime);
  3972. __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
  3973. __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
  3974. __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
  3975. __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
  3976. __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
  3977. unlock_user_struct(target_sd, target_addr, 1);
  3978. return 0;
  3979. }
  3980. struct target_shminfo {
  3981. abi_ulong shmmax;
  3982. abi_ulong shmmin;
  3983. abi_ulong shmmni;
  3984. abi_ulong shmseg;
  3985. abi_ulong shmall;
  3986. };
  3987. static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
  3988. struct shminfo *host_shminfo)
  3989. {
  3990. struct target_shminfo *target_shminfo;
  3991. if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
  3992. return -TARGET_EFAULT;
  3993. __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
  3994. __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
  3995. __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
  3996. __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
  3997. __put_user(host_shminfo->shmall, &target_shminfo->shmall);
  3998. unlock_user_struct(target_shminfo, target_addr, 1);
  3999. return 0;
  4000. }
  4001. struct target_shm_info {
  4002. int used_ids;
  4003. abi_ulong shm_tot;
  4004. abi_ulong shm_rss;
  4005. abi_ulong shm_swp;
  4006. abi_ulong swap_attempts;
  4007. abi_ulong swap_successes;
  4008. };
  4009. static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
  4010. struct shm_info *host_shm_info)
  4011. {
  4012. struct target_shm_info *target_shm_info;
  4013. if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
  4014. return -TARGET_EFAULT;
  4015. __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
  4016. __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
  4017. __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
  4018. __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
  4019. __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
  4020. __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
  4021. unlock_user_struct(target_shm_info, target_addr, 1);
  4022. return 0;
  4023. }
  4024. static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
  4025. {
  4026. struct shmid_ds dsarg;
  4027. struct shminfo shminfo;
  4028. struct shm_info shm_info;
  4029. abi_long ret = -TARGET_EINVAL;
  4030. cmd &= 0xff;
  4031. switch(cmd) {
  4032. case IPC_STAT:
  4033. case IPC_SET:
  4034. case SHM_STAT:
  4035. if (target_to_host_shmid_ds(&dsarg, buf))
  4036. return -TARGET_EFAULT;
  4037. ret = get_errno(shmctl(shmid, cmd, &dsarg));
  4038. if (host_to_target_shmid_ds(buf, &dsarg))
  4039. return -TARGET_EFAULT;
  4040. break;
  4041. case IPC_INFO:
  4042. ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
  4043. if (host_to_target_shminfo(buf, &shminfo))
  4044. return -TARGET_EFAULT;
  4045. break;
  4046. case SHM_INFO:
  4047. ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
  4048. if (host_to_target_shm_info(buf, &shm_info))
  4049. return -TARGET_EFAULT;
  4050. break;
  4051. case IPC_RMID:
  4052. case SHM_LOCK:
  4053. case SHM_UNLOCK:
  4054. ret = get_errno(shmctl(shmid, cmd, NULL));
  4055. break;
  4056. }
  4057. return ret;
  4058. }
  4059. #ifndef TARGET_FORCE_SHMLBA
  4060. /* For most architectures, SHMLBA is the same as the page size;
  4061. * some architectures have larger values, in which case they should
  4062. * define TARGET_FORCE_SHMLBA and provide a target_shmlba() function.
  4063. * This corresponds to the kernel arch code defining __ARCH_FORCE_SHMLBA
  4064. * and defining its own value for SHMLBA.
  4065. *
  4066. * The kernel also permits SHMLBA to be set by the architecture to a
  4067. * value larger than the page size without setting __ARCH_FORCE_SHMLBA;
  4068. * this means that addresses are rounded to the large size if
  4069. * SHM_RND is set but addresses not aligned to that size are not rejected
  4070. * as long as they are at least page-aligned. Since the only architecture
  4071. * which uses this is ia64 this code doesn't provide for that oddity.
  4072. */
  4073. static inline abi_ulong target_shmlba(CPUArchState *cpu_env)
  4074. {
  4075. return TARGET_PAGE_SIZE;
  4076. }
  4077. #endif
  4078. static inline abi_ulong do_shmat(CPUArchState *cpu_env,
  4079. int shmid, abi_ulong shmaddr, int shmflg)
  4080. {
  4081. CPUState *cpu = env_cpu(cpu_env);
  4082. abi_long raddr;
  4083. void *host_raddr;
  4084. struct shmid_ds shm_info;
  4085. int i,ret;
  4086. abi_ulong shmlba;
  4087. /* shmat pointers are always untagged */
  4088. /* find out the length of the shared memory segment */
  4089. ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
  4090. if (is_error(ret)) {
  4091. /* can't get length, bail out */
  4092. return ret;
  4093. }
  4094. shmlba = target_shmlba(cpu_env);
  4095. if (shmaddr & (shmlba - 1)) {
  4096. if (shmflg & SHM_RND) {
  4097. shmaddr &= ~(shmlba - 1);
  4098. } else {
  4099. return -TARGET_EINVAL;
  4100. }
  4101. }
  4102. if (!guest_range_valid_untagged(shmaddr, shm_info.shm_segsz)) {
  4103. return -TARGET_EINVAL;
  4104. }
  4105. mmap_lock();
  4106. /*
  4107. * We're mapping shared memory, so ensure we generate code for parallel
  4108. * execution and flush old translations. This will work up to the level
  4109. * supported by the host -- anything that requires EXCP_ATOMIC will not
  4110. * be atomic with respect to an external process.
  4111. */
  4112. if (!(cpu->tcg_cflags & CF_PARALLEL)) {
  4113. cpu->tcg_cflags |= CF_PARALLEL;
  4114. tb_flush(cpu);
  4115. }
  4116. if (shmaddr)
  4117. host_raddr = shmat(shmid, (void *)g2h_untagged(shmaddr), shmflg);
  4118. else {
  4119. abi_ulong mmap_start;
  4120. /* In order to use the host shmat, we need to honor host SHMLBA. */
  4121. mmap_start = mmap_find_vma(0, shm_info.shm_segsz, MAX(SHMLBA, shmlba));
  4122. if (mmap_start == -1) {
  4123. errno = ENOMEM;
  4124. host_raddr = (void *)-1;
  4125. } else
  4126. host_raddr = shmat(shmid, g2h_untagged(mmap_start),
  4127. shmflg | SHM_REMAP);
  4128. }
  4129. if (host_raddr == (void *)-1) {
  4130. mmap_unlock();
  4131. return get_errno((long)host_raddr);
  4132. }
  4133. raddr=h2g((unsigned long)host_raddr);
  4134. page_set_flags(raddr, raddr + shm_info.shm_segsz,
  4135. PAGE_VALID | PAGE_RESET | PAGE_READ |
  4136. (shmflg & SHM_RDONLY ? 0 : PAGE_WRITE));
  4137. for (i = 0; i < N_SHM_REGIONS; i++) {
  4138. if (!shm_regions[i].in_use) {
  4139. shm_regions[i].in_use = true;
  4140. shm_regions[i].start = raddr;
  4141. shm_regions[i].size = shm_info.shm_segsz;
  4142. break;
  4143. }
  4144. }
  4145. mmap_unlock();
  4146. return raddr;
  4147. }
  4148. static inline abi_long do_shmdt(abi_ulong shmaddr)
  4149. {
  4150. int i;
  4151. abi_long rv;
  4152. /* shmdt pointers are always untagged */
  4153. mmap_lock();
  4154. for (i = 0; i < N_SHM_REGIONS; ++i) {
  4155. if (shm_regions[i].in_use && shm_regions[i].start == shmaddr) {
  4156. shm_regions[i].in_use = false;
  4157. page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
  4158. break;
  4159. }
  4160. }
  4161. rv = get_errno(shmdt(g2h_untagged(shmaddr)));
  4162. mmap_unlock();
  4163. return rv;
  4164. }
  4165. #ifdef TARGET_NR_ipc
  4166. /* ??? This only works with linear mappings. */
  4167. /* do_ipc() must return target values and target errnos. */
  4168. static abi_long do_ipc(CPUArchState *cpu_env,
  4169. unsigned int call, abi_long first,
  4170. abi_long second, abi_long third,
  4171. abi_long ptr, abi_long fifth)
  4172. {
  4173. int version;
  4174. abi_long ret = 0;
  4175. version = call >> 16;
  4176. call &= 0xffff;
  4177. switch (call) {
  4178. case IPCOP_semop:
  4179. ret = do_semtimedop(first, ptr, second, 0, false);
  4180. break;
  4181. case IPCOP_semtimedop:
  4182. /*
  4183. * The s390 sys_ipc variant has only five parameters instead of six
  4184. * (as for default variant) and the only difference is the handling of
  4185. * SEMTIMEDOP where on s390 the third parameter is used as a pointer
  4186. * to a struct timespec where the generic variant uses fifth parameter.
  4187. */
  4188. #if defined(TARGET_S390X)
  4189. ret = do_semtimedop(first, ptr, second, third, TARGET_ABI_BITS == 64);
  4190. #else
  4191. ret = do_semtimedop(first, ptr, second, fifth, TARGET_ABI_BITS == 64);
  4192. #endif
  4193. break;
  4194. case IPCOP_semget:
  4195. ret = get_errno(semget(first, second, third));
  4196. break;
  4197. case IPCOP_semctl: {
  4198. /* The semun argument to semctl is passed by value, so dereference the
  4199. * ptr argument. */
  4200. abi_ulong atptr;
  4201. get_user_ual(atptr, ptr);
  4202. ret = do_semctl(first, second, third, atptr);
  4203. break;
  4204. }
  4205. case IPCOP_msgget:
  4206. ret = get_errno(msgget(first, second));
  4207. break;
  4208. case IPCOP_msgsnd:
  4209. ret = do_msgsnd(first, ptr, second, third);
  4210. break;
  4211. case IPCOP_msgctl:
  4212. ret = do_msgctl(first, second, ptr);
  4213. break;
  4214. case IPCOP_msgrcv:
  4215. switch (version) {
  4216. case 0:
  4217. {
  4218. struct target_ipc_kludge {
  4219. abi_long msgp;
  4220. abi_long msgtyp;
  4221. } *tmp;
  4222. if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
  4223. ret = -TARGET_EFAULT;
  4224. break;
  4225. }
  4226. ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
  4227. unlock_user_struct(tmp, ptr, 0);
  4228. break;
  4229. }
  4230. default:
  4231. ret = do_msgrcv(first, ptr, second, fifth, third);
  4232. }
  4233. break;
  4234. case IPCOP_shmat:
  4235. switch (version) {
  4236. default:
  4237. {
  4238. abi_ulong raddr;
  4239. raddr = do_shmat(cpu_env, first, ptr, second);
  4240. if (is_error(raddr))
  4241. return get_errno(raddr);
  4242. if (put_user_ual(raddr, third))
  4243. return -TARGET_EFAULT;
  4244. break;
  4245. }
  4246. case 1:
  4247. ret = -TARGET_EINVAL;
  4248. break;
  4249. }
  4250. break;
  4251. case IPCOP_shmdt:
  4252. ret = do_shmdt(ptr);
  4253. break;
  4254. case IPCOP_shmget:
  4255. /* IPC_* flag values are the same on all linux platforms */
  4256. ret = get_errno(shmget(first, second, third));
  4257. break;
  4258. /* IPC_* and SHM_* command values are the same on all linux platforms */
  4259. case IPCOP_shmctl:
  4260. ret = do_shmctl(first, second, ptr);
  4261. break;
  4262. default:
  4263. qemu_log_mask(LOG_UNIMP, "Unsupported ipc call: %d (version %d)\n",
  4264. call, version);
  4265. ret = -TARGET_ENOSYS;
  4266. break;
  4267. }
  4268. return ret;
  4269. }
  4270. #endif
  4271. /* kernel structure types definitions */
  4272. #define STRUCT(name, ...) STRUCT_ ## name,
  4273. #define STRUCT_SPECIAL(name) STRUCT_ ## name,
  4274. enum {
  4275. #include "syscall_types.h"
  4276. STRUCT_MAX
  4277. };
  4278. #undef STRUCT
  4279. #undef STRUCT_SPECIAL
  4280. #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
  4281. #define STRUCT_SPECIAL(name)
  4282. #include "syscall_types.h"
  4283. #undef STRUCT
  4284. #undef STRUCT_SPECIAL
  4285. #define MAX_STRUCT_SIZE 4096
  4286. #ifdef CONFIG_FIEMAP
  4287. /* So fiemap access checks don't overflow on 32 bit systems.
  4288. * This is very slightly smaller than the limit imposed by
  4289. * the underlying kernel.
  4290. */
  4291. #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
  4292. / sizeof(struct fiemap_extent))
  4293. static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
  4294. int fd, int cmd, abi_long arg)
  4295. {
  4296. /* The parameter for this ioctl is a struct fiemap followed
  4297. * by an array of struct fiemap_extent whose size is set
  4298. * in fiemap->fm_extent_count. The array is filled in by the
  4299. * ioctl.
  4300. */
  4301. int target_size_in, target_size_out;
  4302. struct fiemap *fm;
  4303. const argtype *arg_type = ie->arg_type;
  4304. const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
  4305. void *argptr, *p;
  4306. abi_long ret;
  4307. int i, extent_size = thunk_type_size(extent_arg_type, 0);
  4308. uint32_t outbufsz;
  4309. int free_fm = 0;
  4310. assert(arg_type[0] == TYPE_PTR);
  4311. assert(ie->access == IOC_RW);
  4312. arg_type++;
  4313. target_size_in = thunk_type_size(arg_type, 0);
  4314. argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
  4315. if (!argptr) {
  4316. return -TARGET_EFAULT;
  4317. }
  4318. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4319. unlock_user(argptr, arg, 0);
  4320. fm = (struct fiemap *)buf_temp;
  4321. if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
  4322. return -TARGET_EINVAL;
  4323. }
  4324. outbufsz = sizeof (*fm) +
  4325. (sizeof(struct fiemap_extent) * fm->fm_extent_count);
  4326. if (outbufsz > MAX_STRUCT_SIZE) {
  4327. /* We can't fit all the extents into the fixed size buffer.
  4328. * Allocate one that is large enough and use it instead.
  4329. */
  4330. fm = g_try_malloc(outbufsz);
  4331. if (!fm) {
  4332. return -TARGET_ENOMEM;
  4333. }
  4334. memcpy(fm, buf_temp, sizeof(struct fiemap));
  4335. free_fm = 1;
  4336. }
  4337. ret = get_errno(safe_ioctl(fd, ie->host_cmd, fm));
  4338. if (!is_error(ret)) {
  4339. target_size_out = target_size_in;
  4340. /* An extent_count of 0 means we were only counting the extents
  4341. * so there are no structs to copy
  4342. */
  4343. if (fm->fm_extent_count != 0) {
  4344. target_size_out += fm->fm_mapped_extents * extent_size;
  4345. }
  4346. argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
  4347. if (!argptr) {
  4348. ret = -TARGET_EFAULT;
  4349. } else {
  4350. /* Convert the struct fiemap */
  4351. thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
  4352. if (fm->fm_extent_count != 0) {
  4353. p = argptr + target_size_in;
  4354. /* ...and then all the struct fiemap_extents */
  4355. for (i = 0; i < fm->fm_mapped_extents; i++) {
  4356. thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
  4357. THUNK_TARGET);
  4358. p += extent_size;
  4359. }
  4360. }
  4361. unlock_user(argptr, arg, target_size_out);
  4362. }
  4363. }
  4364. if (free_fm) {
  4365. g_free(fm);
  4366. }
  4367. return ret;
  4368. }
  4369. #endif
  4370. static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
  4371. int fd, int cmd, abi_long arg)
  4372. {
  4373. const argtype *arg_type = ie->arg_type;
  4374. int target_size;
  4375. void *argptr;
  4376. int ret;
  4377. struct ifconf *host_ifconf;
  4378. uint32_t outbufsz;
  4379. const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
  4380. const argtype ifreq_max_type[] = { MK_STRUCT(STRUCT_ifmap_ifreq) };
  4381. int target_ifreq_size;
  4382. int nb_ifreq;
  4383. int free_buf = 0;
  4384. int i;
  4385. int target_ifc_len;
  4386. abi_long target_ifc_buf;
  4387. int host_ifc_len;
  4388. char *host_ifc_buf;
  4389. assert(arg_type[0] == TYPE_PTR);
  4390. assert(ie->access == IOC_RW);
  4391. arg_type++;
  4392. target_size = thunk_type_size(arg_type, 0);
  4393. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4394. if (!argptr)
  4395. return -TARGET_EFAULT;
  4396. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4397. unlock_user(argptr, arg, 0);
  4398. host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
  4399. target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
  4400. target_ifreq_size = thunk_type_size(ifreq_max_type, 0);
  4401. if (target_ifc_buf != 0) {
  4402. target_ifc_len = host_ifconf->ifc_len;
  4403. nb_ifreq = target_ifc_len / target_ifreq_size;
  4404. host_ifc_len = nb_ifreq * sizeof(struct ifreq);
  4405. outbufsz = sizeof(*host_ifconf) + host_ifc_len;
  4406. if (outbufsz > MAX_STRUCT_SIZE) {
  4407. /*
  4408. * We can't fit all the extents into the fixed size buffer.
  4409. * Allocate one that is large enough and use it instead.
  4410. */
  4411. host_ifconf = g_try_malloc(outbufsz);
  4412. if (!host_ifconf) {
  4413. return -TARGET_ENOMEM;
  4414. }
  4415. memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
  4416. free_buf = 1;
  4417. }
  4418. host_ifc_buf = (char *)host_ifconf + sizeof(*host_ifconf);
  4419. host_ifconf->ifc_len = host_ifc_len;
  4420. } else {
  4421. host_ifc_buf = NULL;
  4422. }
  4423. host_ifconf->ifc_buf = host_ifc_buf;
  4424. ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_ifconf));
  4425. if (!is_error(ret)) {
  4426. /* convert host ifc_len to target ifc_len */
  4427. nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
  4428. target_ifc_len = nb_ifreq * target_ifreq_size;
  4429. host_ifconf->ifc_len = target_ifc_len;
  4430. /* restore target ifc_buf */
  4431. host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
  4432. /* copy struct ifconf to target user */
  4433. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4434. if (!argptr)
  4435. return -TARGET_EFAULT;
  4436. thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
  4437. unlock_user(argptr, arg, target_size);
  4438. if (target_ifc_buf != 0) {
  4439. /* copy ifreq[] to target user */
  4440. argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
  4441. for (i = 0; i < nb_ifreq ; i++) {
  4442. thunk_convert(argptr + i * target_ifreq_size,
  4443. host_ifc_buf + i * sizeof(struct ifreq),
  4444. ifreq_arg_type, THUNK_TARGET);
  4445. }
  4446. unlock_user(argptr, target_ifc_buf, target_ifc_len);
  4447. }
  4448. }
  4449. if (free_buf) {
  4450. g_free(host_ifconf);
  4451. }
  4452. return ret;
  4453. }
  4454. #if defined(CONFIG_USBFS)
  4455. #if HOST_LONG_BITS > 64
  4456. #error USBDEVFS thunks do not support >64 bit hosts yet.
  4457. #endif
  4458. struct live_urb {
  4459. uint64_t target_urb_adr;
  4460. uint64_t target_buf_adr;
  4461. char *target_buf_ptr;
  4462. struct usbdevfs_urb host_urb;
  4463. };
  4464. static GHashTable *usbdevfs_urb_hashtable(void)
  4465. {
  4466. static GHashTable *urb_hashtable;
  4467. if (!urb_hashtable) {
  4468. urb_hashtable = g_hash_table_new(g_int64_hash, g_int64_equal);
  4469. }
  4470. return urb_hashtable;
  4471. }
  4472. static void urb_hashtable_insert(struct live_urb *urb)
  4473. {
  4474. GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
  4475. g_hash_table_insert(urb_hashtable, urb, urb);
  4476. }
  4477. static struct live_urb *urb_hashtable_lookup(uint64_t target_urb_adr)
  4478. {
  4479. GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
  4480. return g_hash_table_lookup(urb_hashtable, &target_urb_adr);
  4481. }
  4482. static void urb_hashtable_remove(struct live_urb *urb)
  4483. {
  4484. GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
  4485. g_hash_table_remove(urb_hashtable, urb);
  4486. }
  4487. static abi_long
  4488. do_ioctl_usbdevfs_reapurb(const IOCTLEntry *ie, uint8_t *buf_temp,
  4489. int fd, int cmd, abi_long arg)
  4490. {
  4491. const argtype usbfsurb_arg_type[] = { MK_STRUCT(STRUCT_usbdevfs_urb) };
  4492. const argtype ptrvoid_arg_type[] = { TYPE_PTRVOID, 0, 0 };
  4493. struct live_urb *lurb;
  4494. void *argptr;
  4495. uint64_t hurb;
  4496. int target_size;
  4497. uintptr_t target_urb_adr;
  4498. abi_long ret;
  4499. target_size = thunk_type_size(usbfsurb_arg_type, THUNK_TARGET);
  4500. memset(buf_temp, 0, sizeof(uint64_t));
  4501. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4502. if (is_error(ret)) {
  4503. return ret;
  4504. }
  4505. memcpy(&hurb, buf_temp, sizeof(uint64_t));
  4506. lurb = (void *)((uintptr_t)hurb - offsetof(struct live_urb, host_urb));
  4507. if (!lurb->target_urb_adr) {
  4508. return -TARGET_EFAULT;
  4509. }
  4510. urb_hashtable_remove(lurb);
  4511. unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr,
  4512. lurb->host_urb.buffer_length);
  4513. lurb->target_buf_ptr = NULL;
  4514. /* restore the guest buffer pointer */
  4515. lurb->host_urb.buffer = (void *)(uintptr_t)lurb->target_buf_adr;
  4516. /* update the guest urb struct */
  4517. argptr = lock_user(VERIFY_WRITE, lurb->target_urb_adr, target_size, 0);
  4518. if (!argptr) {
  4519. g_free(lurb);
  4520. return -TARGET_EFAULT;
  4521. }
  4522. thunk_convert(argptr, &lurb->host_urb, usbfsurb_arg_type, THUNK_TARGET);
  4523. unlock_user(argptr, lurb->target_urb_adr, target_size);
  4524. target_size = thunk_type_size(ptrvoid_arg_type, THUNK_TARGET);
  4525. /* write back the urb handle */
  4526. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4527. if (!argptr) {
  4528. g_free(lurb);
  4529. return -TARGET_EFAULT;
  4530. }
  4531. /* GHashTable uses 64-bit keys but thunk_convert expects uintptr_t */
  4532. target_urb_adr = lurb->target_urb_adr;
  4533. thunk_convert(argptr, &target_urb_adr, ptrvoid_arg_type, THUNK_TARGET);
  4534. unlock_user(argptr, arg, target_size);
  4535. g_free(lurb);
  4536. return ret;
  4537. }
  4538. static abi_long
  4539. do_ioctl_usbdevfs_discardurb(const IOCTLEntry *ie,
  4540. uint8_t *buf_temp __attribute__((unused)),
  4541. int fd, int cmd, abi_long arg)
  4542. {
  4543. struct live_urb *lurb;
  4544. /* map target address back to host URB with metadata. */
  4545. lurb = urb_hashtable_lookup(arg);
  4546. if (!lurb) {
  4547. return -TARGET_EFAULT;
  4548. }
  4549. return get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb));
  4550. }
  4551. static abi_long
  4552. do_ioctl_usbdevfs_submiturb(const IOCTLEntry *ie, uint8_t *buf_temp,
  4553. int fd, int cmd, abi_long arg)
  4554. {
  4555. const argtype *arg_type = ie->arg_type;
  4556. int target_size;
  4557. abi_long ret;
  4558. void *argptr;
  4559. int rw_dir;
  4560. struct live_urb *lurb;
  4561. /*
  4562. * each submitted URB needs to map to a unique ID for the
  4563. * kernel, and that unique ID needs to be a pointer to
  4564. * host memory. hence, we need to malloc for each URB.
  4565. * isochronous transfers have a variable length struct.
  4566. */
  4567. arg_type++;
  4568. target_size = thunk_type_size(arg_type, THUNK_TARGET);
  4569. /* construct host copy of urb and metadata */
  4570. lurb = g_try_new0(struct live_urb, 1);
  4571. if (!lurb) {
  4572. return -TARGET_ENOMEM;
  4573. }
  4574. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4575. if (!argptr) {
  4576. g_free(lurb);
  4577. return -TARGET_EFAULT;
  4578. }
  4579. thunk_convert(&lurb->host_urb, argptr, arg_type, THUNK_HOST);
  4580. unlock_user(argptr, arg, 0);
  4581. lurb->target_urb_adr = arg;
  4582. lurb->target_buf_adr = (uintptr_t)lurb->host_urb.buffer;
  4583. /* buffer space used depends on endpoint type so lock the entire buffer */
  4584. /* control type urbs should check the buffer contents for true direction */
  4585. rw_dir = lurb->host_urb.endpoint & USB_DIR_IN ? VERIFY_WRITE : VERIFY_READ;
  4586. lurb->target_buf_ptr = lock_user(rw_dir, lurb->target_buf_adr,
  4587. lurb->host_urb.buffer_length, 1);
  4588. if (lurb->target_buf_ptr == NULL) {
  4589. g_free(lurb);
  4590. return -TARGET_EFAULT;
  4591. }
  4592. /* update buffer pointer in host copy */
  4593. lurb->host_urb.buffer = lurb->target_buf_ptr;
  4594. ret = get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb));
  4595. if (is_error(ret)) {
  4596. unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr, 0);
  4597. g_free(lurb);
  4598. } else {
  4599. urb_hashtable_insert(lurb);
  4600. }
  4601. return ret;
  4602. }
  4603. #endif /* CONFIG_USBFS */
  4604. static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
  4605. int cmd, abi_long arg)
  4606. {
  4607. void *argptr;
  4608. struct dm_ioctl *host_dm;
  4609. abi_long guest_data;
  4610. uint32_t guest_data_size;
  4611. int target_size;
  4612. const argtype *arg_type = ie->arg_type;
  4613. abi_long ret;
  4614. void *big_buf = NULL;
  4615. char *host_data;
  4616. arg_type++;
  4617. target_size = thunk_type_size(arg_type, 0);
  4618. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4619. if (!argptr) {
  4620. ret = -TARGET_EFAULT;
  4621. goto out;
  4622. }
  4623. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4624. unlock_user(argptr, arg, 0);
  4625. /* buf_temp is too small, so fetch things into a bigger buffer */
  4626. big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
  4627. memcpy(big_buf, buf_temp, target_size);
  4628. buf_temp = big_buf;
  4629. host_dm = big_buf;
  4630. guest_data = arg + host_dm->data_start;
  4631. if ((guest_data - arg) < 0) {
  4632. ret = -TARGET_EINVAL;
  4633. goto out;
  4634. }
  4635. guest_data_size = host_dm->data_size - host_dm->data_start;
  4636. host_data = (char*)host_dm + host_dm->data_start;
  4637. argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
  4638. if (!argptr) {
  4639. ret = -TARGET_EFAULT;
  4640. goto out;
  4641. }
  4642. switch (ie->host_cmd) {
  4643. case DM_REMOVE_ALL:
  4644. case DM_LIST_DEVICES:
  4645. case DM_DEV_CREATE:
  4646. case DM_DEV_REMOVE:
  4647. case DM_DEV_SUSPEND:
  4648. case DM_DEV_STATUS:
  4649. case DM_DEV_WAIT:
  4650. case DM_TABLE_STATUS:
  4651. case DM_TABLE_CLEAR:
  4652. case DM_TABLE_DEPS:
  4653. case DM_LIST_VERSIONS:
  4654. /* no input data */
  4655. break;
  4656. case DM_DEV_RENAME:
  4657. case DM_DEV_SET_GEOMETRY:
  4658. /* data contains only strings */
  4659. memcpy(host_data, argptr, guest_data_size);
  4660. break;
  4661. case DM_TARGET_MSG:
  4662. memcpy(host_data, argptr, guest_data_size);
  4663. *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
  4664. break;
  4665. case DM_TABLE_LOAD:
  4666. {
  4667. void *gspec = argptr;
  4668. void *cur_data = host_data;
  4669. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
  4670. int spec_size = thunk_type_size(arg_type, 0);
  4671. int i;
  4672. for (i = 0; i < host_dm->target_count; i++) {
  4673. struct dm_target_spec *spec = cur_data;
  4674. uint32_t next;
  4675. int slen;
  4676. thunk_convert(spec, gspec, arg_type, THUNK_HOST);
  4677. slen = strlen((char*)gspec + spec_size) + 1;
  4678. next = spec->next;
  4679. spec->next = sizeof(*spec) + slen;
  4680. strcpy((char*)&spec[1], gspec + spec_size);
  4681. gspec += next;
  4682. cur_data += spec->next;
  4683. }
  4684. break;
  4685. }
  4686. default:
  4687. ret = -TARGET_EINVAL;
  4688. unlock_user(argptr, guest_data, 0);
  4689. goto out;
  4690. }
  4691. unlock_user(argptr, guest_data, 0);
  4692. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4693. if (!is_error(ret)) {
  4694. guest_data = arg + host_dm->data_start;
  4695. guest_data_size = host_dm->data_size - host_dm->data_start;
  4696. argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
  4697. switch (ie->host_cmd) {
  4698. case DM_REMOVE_ALL:
  4699. case DM_DEV_CREATE:
  4700. case DM_DEV_REMOVE:
  4701. case DM_DEV_RENAME:
  4702. case DM_DEV_SUSPEND:
  4703. case DM_DEV_STATUS:
  4704. case DM_TABLE_LOAD:
  4705. case DM_TABLE_CLEAR:
  4706. case DM_TARGET_MSG:
  4707. case DM_DEV_SET_GEOMETRY:
  4708. /* no return data */
  4709. break;
  4710. case DM_LIST_DEVICES:
  4711. {
  4712. struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
  4713. uint32_t remaining_data = guest_data_size;
  4714. void *cur_data = argptr;
  4715. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
  4716. int nl_size = 12; /* can't use thunk_size due to alignment */
  4717. while (1) {
  4718. uint32_t next = nl->next;
  4719. if (next) {
  4720. nl->next = nl_size + (strlen(nl->name) + 1);
  4721. }
  4722. if (remaining_data < nl->next) {
  4723. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  4724. break;
  4725. }
  4726. thunk_convert(cur_data, nl, arg_type, THUNK_TARGET);
  4727. strcpy(cur_data + nl_size, nl->name);
  4728. cur_data += nl->next;
  4729. remaining_data -= nl->next;
  4730. if (!next) {
  4731. break;
  4732. }
  4733. nl = (void*)nl + next;
  4734. }
  4735. break;
  4736. }
  4737. case DM_DEV_WAIT:
  4738. case DM_TABLE_STATUS:
  4739. {
  4740. struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
  4741. void *cur_data = argptr;
  4742. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
  4743. int spec_size = thunk_type_size(arg_type, 0);
  4744. int i;
  4745. for (i = 0; i < host_dm->target_count; i++) {
  4746. uint32_t next = spec->next;
  4747. int slen = strlen((char*)&spec[1]) + 1;
  4748. spec->next = (cur_data - argptr) + spec_size + slen;
  4749. if (guest_data_size < spec->next) {
  4750. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  4751. break;
  4752. }
  4753. thunk_convert(cur_data, spec, arg_type, THUNK_TARGET);
  4754. strcpy(cur_data + spec_size, (char*)&spec[1]);
  4755. cur_data = argptr + spec->next;
  4756. spec = (void*)host_dm + host_dm->data_start + next;
  4757. }
  4758. break;
  4759. }
  4760. case DM_TABLE_DEPS:
  4761. {
  4762. void *hdata = (void*)host_dm + host_dm->data_start;
  4763. int count = *(uint32_t*)hdata;
  4764. uint64_t *hdev = hdata + 8;
  4765. uint64_t *gdev = argptr + 8;
  4766. int i;
  4767. *(uint32_t*)argptr = tswap32(count);
  4768. for (i = 0; i < count; i++) {
  4769. *gdev = tswap64(*hdev);
  4770. gdev++;
  4771. hdev++;
  4772. }
  4773. break;
  4774. }
  4775. case DM_LIST_VERSIONS:
  4776. {
  4777. struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
  4778. uint32_t remaining_data = guest_data_size;
  4779. void *cur_data = argptr;
  4780. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
  4781. int vers_size = thunk_type_size(arg_type, 0);
  4782. while (1) {
  4783. uint32_t next = vers->next;
  4784. if (next) {
  4785. vers->next = vers_size + (strlen(vers->name) + 1);
  4786. }
  4787. if (remaining_data < vers->next) {
  4788. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  4789. break;
  4790. }
  4791. thunk_convert(cur_data, vers, arg_type, THUNK_TARGET);
  4792. strcpy(cur_data + vers_size, vers->name);
  4793. cur_data += vers->next;
  4794. remaining_data -= vers->next;
  4795. if (!next) {
  4796. break;
  4797. }
  4798. vers = (void*)vers + next;
  4799. }
  4800. break;
  4801. }
  4802. default:
  4803. unlock_user(argptr, guest_data, 0);
  4804. ret = -TARGET_EINVAL;
  4805. goto out;
  4806. }
  4807. unlock_user(argptr, guest_data, guest_data_size);
  4808. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4809. if (!argptr) {
  4810. ret = -TARGET_EFAULT;
  4811. goto out;
  4812. }
  4813. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  4814. unlock_user(argptr, arg, target_size);
  4815. }
  4816. out:
  4817. g_free(big_buf);
  4818. return ret;
  4819. }
  4820. static abi_long do_ioctl_blkpg(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
  4821. int cmd, abi_long arg)
  4822. {
  4823. void *argptr;
  4824. int target_size;
  4825. const argtype *arg_type = ie->arg_type;
  4826. const argtype part_arg_type[] = { MK_STRUCT(STRUCT_blkpg_partition) };
  4827. abi_long ret;
  4828. struct blkpg_ioctl_arg *host_blkpg = (void*)buf_temp;
  4829. struct blkpg_partition host_part;
  4830. /* Read and convert blkpg */
  4831. arg_type++;
  4832. target_size = thunk_type_size(arg_type, 0);
  4833. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4834. if (!argptr) {
  4835. ret = -TARGET_EFAULT;
  4836. goto out;
  4837. }
  4838. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4839. unlock_user(argptr, arg, 0);
  4840. switch (host_blkpg->op) {
  4841. case BLKPG_ADD_PARTITION:
  4842. case BLKPG_DEL_PARTITION:
  4843. /* payload is struct blkpg_partition */
  4844. break;
  4845. default:
  4846. /* Unknown opcode */
  4847. ret = -TARGET_EINVAL;
  4848. goto out;
  4849. }
  4850. /* Read and convert blkpg->data */
  4851. arg = (abi_long)(uintptr_t)host_blkpg->data;
  4852. target_size = thunk_type_size(part_arg_type, 0);
  4853. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4854. if (!argptr) {
  4855. ret = -TARGET_EFAULT;
  4856. goto out;
  4857. }
  4858. thunk_convert(&host_part, argptr, part_arg_type, THUNK_HOST);
  4859. unlock_user(argptr, arg, 0);
  4860. /* Swizzle the data pointer to our local copy and call! */
  4861. host_blkpg->data = &host_part;
  4862. ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_blkpg));
  4863. out:
  4864. return ret;
  4865. }
  4866. static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
  4867. int fd, int cmd, abi_long arg)
  4868. {
  4869. const argtype *arg_type = ie->arg_type;
  4870. const StructEntry *se;
  4871. const argtype *field_types;
  4872. const int *dst_offsets, *src_offsets;
  4873. int target_size;
  4874. void *argptr;
  4875. abi_ulong *target_rt_dev_ptr = NULL;
  4876. unsigned long *host_rt_dev_ptr = NULL;
  4877. abi_long ret;
  4878. int i;
  4879. assert(ie->access == IOC_W);
  4880. assert(*arg_type == TYPE_PTR);
  4881. arg_type++;
  4882. assert(*arg_type == TYPE_STRUCT);
  4883. target_size = thunk_type_size(arg_type, 0);
  4884. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4885. if (!argptr) {
  4886. return -TARGET_EFAULT;
  4887. }
  4888. arg_type++;
  4889. assert(*arg_type == (int)STRUCT_rtentry);
  4890. se = struct_entries + *arg_type++;
  4891. assert(se->convert[0] == NULL);
  4892. /* convert struct here to be able to catch rt_dev string */
  4893. field_types = se->field_types;
  4894. dst_offsets = se->field_offsets[THUNK_HOST];
  4895. src_offsets = se->field_offsets[THUNK_TARGET];
  4896. for (i = 0; i < se->nb_fields; i++) {
  4897. if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) {
  4898. assert(*field_types == TYPE_PTRVOID);
  4899. target_rt_dev_ptr = argptr + src_offsets[i];
  4900. host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]);
  4901. if (*target_rt_dev_ptr != 0) {
  4902. *host_rt_dev_ptr = (unsigned long)lock_user_string(
  4903. tswapal(*target_rt_dev_ptr));
  4904. if (!*host_rt_dev_ptr) {
  4905. unlock_user(argptr, arg, 0);
  4906. return -TARGET_EFAULT;
  4907. }
  4908. } else {
  4909. *host_rt_dev_ptr = 0;
  4910. }
  4911. field_types++;
  4912. continue;
  4913. }
  4914. field_types = thunk_convert(buf_temp + dst_offsets[i],
  4915. argptr + src_offsets[i],
  4916. field_types, THUNK_HOST);
  4917. }
  4918. unlock_user(argptr, arg, 0);
  4919. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4920. assert(host_rt_dev_ptr != NULL);
  4921. assert(target_rt_dev_ptr != NULL);
  4922. if (*host_rt_dev_ptr != 0) {
  4923. unlock_user((void *)*host_rt_dev_ptr,
  4924. *target_rt_dev_ptr, 0);
  4925. }
  4926. return ret;
  4927. }
  4928. static abi_long do_ioctl_kdsigaccept(const IOCTLEntry *ie, uint8_t *buf_temp,
  4929. int fd, int cmd, abi_long arg)
  4930. {
  4931. int sig = target_to_host_signal(arg);
  4932. return get_errno(safe_ioctl(fd, ie->host_cmd, sig));
  4933. }
  4934. static abi_long do_ioctl_SIOCGSTAMP(const IOCTLEntry *ie, uint8_t *buf_temp,
  4935. int fd, int cmd, abi_long arg)
  4936. {
  4937. struct timeval tv;
  4938. abi_long ret;
  4939. ret = get_errno(safe_ioctl(fd, SIOCGSTAMP, &tv));
  4940. if (is_error(ret)) {
  4941. return ret;
  4942. }
  4943. if (cmd == (int)TARGET_SIOCGSTAMP_OLD) {
  4944. if (copy_to_user_timeval(arg, &tv)) {
  4945. return -TARGET_EFAULT;
  4946. }
  4947. } else {
  4948. if (copy_to_user_timeval64(arg, &tv)) {
  4949. return -TARGET_EFAULT;
  4950. }
  4951. }
  4952. return ret;
  4953. }
  4954. static abi_long do_ioctl_SIOCGSTAMPNS(const IOCTLEntry *ie, uint8_t *buf_temp,
  4955. int fd, int cmd, abi_long arg)
  4956. {
  4957. struct timespec ts;
  4958. abi_long ret;
  4959. ret = get_errno(safe_ioctl(fd, SIOCGSTAMPNS, &ts));
  4960. if (is_error(ret)) {
  4961. return ret;
  4962. }
  4963. if (cmd == (int)TARGET_SIOCGSTAMPNS_OLD) {
  4964. if (host_to_target_timespec(arg, &ts)) {
  4965. return -TARGET_EFAULT;
  4966. }
  4967. } else{
  4968. if (host_to_target_timespec64(arg, &ts)) {
  4969. return -TARGET_EFAULT;
  4970. }
  4971. }
  4972. return ret;
  4973. }
  4974. #ifdef TIOCGPTPEER
  4975. static abi_long do_ioctl_tiocgptpeer(const IOCTLEntry *ie, uint8_t *buf_temp,
  4976. int fd, int cmd, abi_long arg)
  4977. {
  4978. int flags = target_to_host_bitmask(arg, fcntl_flags_tbl);
  4979. return get_errno(safe_ioctl(fd, ie->host_cmd, flags));
  4980. }
  4981. #endif
  4982. #ifdef HAVE_DRM_H
  4983. static void unlock_drm_version(struct drm_version *host_ver,
  4984. struct target_drm_version *target_ver,
  4985. bool copy)
  4986. {
  4987. unlock_user(host_ver->name, target_ver->name,
  4988. copy ? host_ver->name_len : 0);
  4989. unlock_user(host_ver->date, target_ver->date,
  4990. copy ? host_ver->date_len : 0);
  4991. unlock_user(host_ver->desc, target_ver->desc,
  4992. copy ? host_ver->desc_len : 0);
  4993. }
  4994. static inline abi_long target_to_host_drmversion(struct drm_version *host_ver,
  4995. struct target_drm_version *target_ver)
  4996. {
  4997. memset(host_ver, 0, sizeof(*host_ver));
  4998. __get_user(host_ver->name_len, &target_ver->name_len);
  4999. if (host_ver->name_len) {
  5000. host_ver->name = lock_user(VERIFY_WRITE, target_ver->name,
  5001. target_ver->name_len, 0);
  5002. if (!host_ver->name) {
  5003. return -EFAULT;
  5004. }
  5005. }
  5006. __get_user(host_ver->date_len, &target_ver->date_len);
  5007. if (host_ver->date_len) {
  5008. host_ver->date = lock_user(VERIFY_WRITE, target_ver->date,
  5009. target_ver->date_len, 0);
  5010. if (!host_ver->date) {
  5011. goto err;
  5012. }
  5013. }
  5014. __get_user(host_ver->desc_len, &target_ver->desc_len);
  5015. if (host_ver->desc_len) {
  5016. host_ver->desc = lock_user(VERIFY_WRITE, target_ver->desc,
  5017. target_ver->desc_len, 0);
  5018. if (!host_ver->desc) {
  5019. goto err;
  5020. }
  5021. }
  5022. return 0;
  5023. err:
  5024. unlock_drm_version(host_ver, target_ver, false);
  5025. return -EFAULT;
  5026. }
  5027. static inline void host_to_target_drmversion(
  5028. struct target_drm_version *target_ver,
  5029. struct drm_version *host_ver)
  5030. {
  5031. __put_user(host_ver->version_major, &target_ver->version_major);
  5032. __put_user(host_ver->version_minor, &target_ver->version_minor);
  5033. __put_user(host_ver->version_patchlevel, &target_ver->version_patchlevel);
  5034. __put_user(host_ver->name_len, &target_ver->name_len);
  5035. __put_user(host_ver->date_len, &target_ver->date_len);
  5036. __put_user(host_ver->desc_len, &target_ver->desc_len);
  5037. unlock_drm_version(host_ver, target_ver, true);
  5038. }
  5039. static abi_long do_ioctl_drm(const IOCTLEntry *ie, uint8_t *buf_temp,
  5040. int fd, int cmd, abi_long arg)
  5041. {
  5042. struct drm_version *ver;
  5043. struct target_drm_version *target_ver;
  5044. abi_long ret;
  5045. switch (ie->host_cmd) {
  5046. case DRM_IOCTL_VERSION:
  5047. if (!lock_user_struct(VERIFY_WRITE, target_ver, arg, 0)) {
  5048. return -TARGET_EFAULT;
  5049. }
  5050. ver = (struct drm_version *)buf_temp;
  5051. ret = target_to_host_drmversion(ver, target_ver);
  5052. if (!is_error(ret)) {
  5053. ret = get_errno(safe_ioctl(fd, ie->host_cmd, ver));
  5054. if (is_error(ret)) {
  5055. unlock_drm_version(ver, target_ver, false);
  5056. } else {
  5057. host_to_target_drmversion(target_ver, ver);
  5058. }
  5059. }
  5060. unlock_user_struct(target_ver, arg, 0);
  5061. return ret;
  5062. }
  5063. return -TARGET_ENOSYS;
  5064. }
  5065. static abi_long do_ioctl_drm_i915_getparam(const IOCTLEntry *ie,
  5066. struct drm_i915_getparam *gparam,
  5067. int fd, abi_long arg)
  5068. {
  5069. abi_long ret;
  5070. int value;
  5071. struct target_drm_i915_getparam *target_gparam;
  5072. if (!lock_user_struct(VERIFY_READ, target_gparam, arg, 0)) {
  5073. return -TARGET_EFAULT;
  5074. }
  5075. __get_user(gparam->param, &target_gparam->param);
  5076. gparam->value = &value;
  5077. ret = get_errno(safe_ioctl(fd, ie->host_cmd, gparam));
  5078. put_user_s32(value, target_gparam->value);
  5079. unlock_user_struct(target_gparam, arg, 0);
  5080. return ret;
  5081. }
  5082. static abi_long do_ioctl_drm_i915(const IOCTLEntry *ie, uint8_t *buf_temp,
  5083. int fd, int cmd, abi_long arg)
  5084. {
  5085. switch (ie->host_cmd) {
  5086. case DRM_IOCTL_I915_GETPARAM:
  5087. return do_ioctl_drm_i915_getparam(ie,
  5088. (struct drm_i915_getparam *)buf_temp,
  5089. fd, arg);
  5090. default:
  5091. return -TARGET_ENOSYS;
  5092. }
  5093. }
  5094. #endif
  5095. static abi_long do_ioctl_TUNSETTXFILTER(const IOCTLEntry *ie, uint8_t *buf_temp,
  5096. int fd, int cmd, abi_long arg)
  5097. {
  5098. struct tun_filter *filter = (struct tun_filter *)buf_temp;
  5099. struct tun_filter *target_filter;
  5100. char *target_addr;
  5101. assert(ie->access == IOC_W);
  5102. target_filter = lock_user(VERIFY_READ, arg, sizeof(*target_filter), 1);
  5103. if (!target_filter) {
  5104. return -TARGET_EFAULT;
  5105. }
  5106. filter->flags = tswap16(target_filter->flags);
  5107. filter->count = tswap16(target_filter->count);
  5108. unlock_user(target_filter, arg, 0);
  5109. if (filter->count) {
  5110. if (offsetof(struct tun_filter, addr) + filter->count * ETH_ALEN >
  5111. MAX_STRUCT_SIZE) {
  5112. return -TARGET_EFAULT;
  5113. }
  5114. target_addr = lock_user(VERIFY_READ,
  5115. arg + offsetof(struct tun_filter, addr),
  5116. filter->count * ETH_ALEN, 1);
  5117. if (!target_addr) {
  5118. return -TARGET_EFAULT;
  5119. }
  5120. memcpy(filter->addr, target_addr, filter->count * ETH_ALEN);
  5121. unlock_user(target_addr, arg + offsetof(struct tun_filter, addr), 0);
  5122. }
  5123. return get_errno(safe_ioctl(fd, ie->host_cmd, filter));
  5124. }
  5125. IOCTLEntry ioctl_entries[] = {
  5126. #define IOCTL(cmd, access, ...) \
  5127. { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
  5128. #define IOCTL_SPECIAL(cmd, access, dofn, ...) \
  5129. { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
  5130. #define IOCTL_IGNORE(cmd) \
  5131. { TARGET_ ## cmd, 0, #cmd },
  5132. #include "ioctls.h"
  5133. { 0, 0, },
  5134. };
  5135. /* ??? Implement proper locking for ioctls. */
  5136. /* do_ioctl() Must return target values and target errnos. */
  5137. static abi_long do_ioctl(int fd, int cmd, abi_long arg)
  5138. {
  5139. const IOCTLEntry *ie;
  5140. const argtype *arg_type;
  5141. abi_long ret;
  5142. uint8_t buf_temp[MAX_STRUCT_SIZE];
  5143. int target_size;
  5144. void *argptr;
  5145. ie = ioctl_entries;
  5146. for(;;) {
  5147. if (ie->target_cmd == 0) {
  5148. qemu_log_mask(
  5149. LOG_UNIMP, "Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
  5150. return -TARGET_ENOSYS;
  5151. }
  5152. if (ie->target_cmd == cmd)
  5153. break;
  5154. ie++;
  5155. }
  5156. arg_type = ie->arg_type;
  5157. if (ie->do_ioctl) {
  5158. return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
  5159. } else if (!ie->host_cmd) {
  5160. /* Some architectures define BSD ioctls in their headers
  5161. that are not implemented in Linux. */
  5162. return -TARGET_ENOSYS;
  5163. }
  5164. switch(arg_type[0]) {
  5165. case TYPE_NULL:
  5166. /* no argument */
  5167. ret = get_errno(safe_ioctl(fd, ie->host_cmd));
  5168. break;
  5169. case TYPE_PTRVOID:
  5170. case TYPE_INT:
  5171. case TYPE_LONG:
  5172. case TYPE_ULONG:
  5173. ret = get_errno(safe_ioctl(fd, ie->host_cmd, arg));
  5174. break;
  5175. case TYPE_PTR:
  5176. arg_type++;
  5177. target_size = thunk_type_size(arg_type, 0);
  5178. switch(ie->access) {
  5179. case IOC_R:
  5180. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  5181. if (!is_error(ret)) {
  5182. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  5183. if (!argptr)
  5184. return -TARGET_EFAULT;
  5185. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  5186. unlock_user(argptr, arg, target_size);
  5187. }
  5188. break;
  5189. case IOC_W:
  5190. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  5191. if (!argptr)
  5192. return -TARGET_EFAULT;
  5193. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  5194. unlock_user(argptr, arg, 0);
  5195. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  5196. break;
  5197. default:
  5198. case IOC_RW:
  5199. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  5200. if (!argptr)
  5201. return -TARGET_EFAULT;
  5202. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  5203. unlock_user(argptr, arg, 0);
  5204. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  5205. if (!is_error(ret)) {
  5206. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  5207. if (!argptr)
  5208. return -TARGET_EFAULT;
  5209. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  5210. unlock_user(argptr, arg, target_size);
  5211. }
  5212. break;
  5213. }
  5214. break;
  5215. default:
  5216. qemu_log_mask(LOG_UNIMP,
  5217. "Unsupported ioctl type: cmd=0x%04lx type=%d\n",
  5218. (long)cmd, arg_type[0]);
  5219. ret = -TARGET_ENOSYS;
  5220. break;
  5221. }
  5222. return ret;
  5223. }
  5224. static const bitmask_transtbl iflag_tbl[] = {
  5225. { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
  5226. { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
  5227. { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
  5228. { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
  5229. { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
  5230. { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
  5231. { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
  5232. { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
  5233. { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
  5234. { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
  5235. { TARGET_IXON, TARGET_IXON, IXON, IXON },
  5236. { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
  5237. { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
  5238. { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
  5239. { TARGET_IUTF8, TARGET_IUTF8, IUTF8, IUTF8},
  5240. { 0, 0, 0, 0 }
  5241. };
  5242. static const bitmask_transtbl oflag_tbl[] = {
  5243. { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
  5244. { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
  5245. { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
  5246. { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
  5247. { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
  5248. { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
  5249. { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
  5250. { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
  5251. { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
  5252. { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
  5253. { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
  5254. { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
  5255. { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
  5256. { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
  5257. { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
  5258. { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
  5259. { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
  5260. { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
  5261. { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
  5262. { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
  5263. { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
  5264. { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
  5265. { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
  5266. { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
  5267. { 0, 0, 0, 0 }
  5268. };
  5269. static const bitmask_transtbl cflag_tbl[] = {
  5270. { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
  5271. { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
  5272. { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
  5273. { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
  5274. { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
  5275. { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
  5276. { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
  5277. { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
  5278. { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
  5279. { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
  5280. { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
  5281. { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
  5282. { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
  5283. { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
  5284. { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
  5285. { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
  5286. { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
  5287. { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
  5288. { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
  5289. { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
  5290. { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
  5291. { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
  5292. { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
  5293. { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
  5294. { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
  5295. { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
  5296. { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
  5297. { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
  5298. { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
  5299. { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
  5300. { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
  5301. { 0, 0, 0, 0 }
  5302. };
  5303. static const bitmask_transtbl lflag_tbl[] = {
  5304. { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
  5305. { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
  5306. { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
  5307. { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
  5308. { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
  5309. { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
  5310. { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
  5311. { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
  5312. { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
  5313. { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
  5314. { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
  5315. { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
  5316. { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
  5317. { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
  5318. { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
  5319. { TARGET_EXTPROC, TARGET_EXTPROC, EXTPROC, EXTPROC},
  5320. { 0, 0, 0, 0 }
  5321. };
  5322. static void target_to_host_termios (void *dst, const void *src)
  5323. {
  5324. struct host_termios *host = dst;
  5325. const struct target_termios *target = src;
  5326. host->c_iflag =
  5327. target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
  5328. host->c_oflag =
  5329. target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
  5330. host->c_cflag =
  5331. target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
  5332. host->c_lflag =
  5333. target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
  5334. host->c_line = target->c_line;
  5335. memset(host->c_cc, 0, sizeof(host->c_cc));
  5336. host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
  5337. host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
  5338. host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
  5339. host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
  5340. host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
  5341. host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
  5342. host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
  5343. host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
  5344. host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
  5345. host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
  5346. host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
  5347. host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
  5348. host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
  5349. host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
  5350. host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
  5351. host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
  5352. host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
  5353. }
  5354. static void host_to_target_termios (void *dst, const void *src)
  5355. {
  5356. struct target_termios *target = dst;
  5357. const struct host_termios *host = src;
  5358. target->c_iflag =
  5359. tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
  5360. target->c_oflag =
  5361. tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
  5362. target->c_cflag =
  5363. tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
  5364. target->c_lflag =
  5365. tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
  5366. target->c_line = host->c_line;
  5367. memset(target->c_cc, 0, sizeof(target->c_cc));
  5368. target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
  5369. target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
  5370. target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
  5371. target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
  5372. target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
  5373. target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
  5374. target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
  5375. target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
  5376. target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
  5377. target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
  5378. target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
  5379. target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
  5380. target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
  5381. target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
  5382. target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
  5383. target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
  5384. target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
  5385. }
  5386. static const StructEntry struct_termios_def = {
  5387. .convert = { host_to_target_termios, target_to_host_termios },
  5388. .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
  5389. .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
  5390. .print = print_termios,
  5391. };
  5392. static const bitmask_transtbl mmap_flags_tbl[] = {
  5393. { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
  5394. { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
  5395. { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
  5396. { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS,
  5397. MAP_ANONYMOUS, MAP_ANONYMOUS },
  5398. { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN,
  5399. MAP_GROWSDOWN, MAP_GROWSDOWN },
  5400. { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE,
  5401. MAP_DENYWRITE, MAP_DENYWRITE },
  5402. { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE,
  5403. MAP_EXECUTABLE, MAP_EXECUTABLE },
  5404. { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
  5405. { TARGET_MAP_NORESERVE, TARGET_MAP_NORESERVE,
  5406. MAP_NORESERVE, MAP_NORESERVE },
  5407. { TARGET_MAP_HUGETLB, TARGET_MAP_HUGETLB, MAP_HUGETLB, MAP_HUGETLB },
  5408. /* MAP_STACK had been ignored by the kernel for quite some time.
  5409. Recognize it for the target insofar as we do not want to pass
  5410. it through to the host. */
  5411. { TARGET_MAP_STACK, TARGET_MAP_STACK, 0, 0 },
  5412. { 0, 0, 0, 0 }
  5413. };
  5414. /*
  5415. * NOTE: TARGET_ABI32 is defined for TARGET_I386 (but not for TARGET_X86_64)
  5416. * TARGET_I386 is defined if TARGET_X86_64 is defined
  5417. */
  5418. #if defined(TARGET_I386)
  5419. /* NOTE: there is really one LDT for all the threads */
  5420. static uint8_t *ldt_table;
  5421. static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
  5422. {
  5423. int size;
  5424. void *p;
  5425. if (!ldt_table)
  5426. return 0;
  5427. size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
  5428. if (size > bytecount)
  5429. size = bytecount;
  5430. p = lock_user(VERIFY_WRITE, ptr, size, 0);
  5431. if (!p)
  5432. return -TARGET_EFAULT;
  5433. /* ??? Should this by byteswapped? */
  5434. memcpy(p, ldt_table, size);
  5435. unlock_user(p, ptr, size);
  5436. return size;
  5437. }
  5438. /* XXX: add locking support */
  5439. static abi_long write_ldt(CPUX86State *env,
  5440. abi_ulong ptr, unsigned long bytecount, int oldmode)
  5441. {
  5442. struct target_modify_ldt_ldt_s ldt_info;
  5443. struct target_modify_ldt_ldt_s *target_ldt_info;
  5444. int seg_32bit, contents, read_exec_only, limit_in_pages;
  5445. int seg_not_present, useable, lm;
  5446. uint32_t *lp, entry_1, entry_2;
  5447. if (bytecount != sizeof(ldt_info))
  5448. return -TARGET_EINVAL;
  5449. if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
  5450. return -TARGET_EFAULT;
  5451. ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
  5452. ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
  5453. ldt_info.limit = tswap32(target_ldt_info->limit);
  5454. ldt_info.flags = tswap32(target_ldt_info->flags);
  5455. unlock_user_struct(target_ldt_info, ptr, 0);
  5456. if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
  5457. return -TARGET_EINVAL;
  5458. seg_32bit = ldt_info.flags & 1;
  5459. contents = (ldt_info.flags >> 1) & 3;
  5460. read_exec_only = (ldt_info.flags >> 3) & 1;
  5461. limit_in_pages = (ldt_info.flags >> 4) & 1;
  5462. seg_not_present = (ldt_info.flags >> 5) & 1;
  5463. useable = (ldt_info.flags >> 6) & 1;
  5464. #ifdef TARGET_ABI32
  5465. lm = 0;
  5466. #else
  5467. lm = (ldt_info.flags >> 7) & 1;
  5468. #endif
  5469. if (contents == 3) {
  5470. if (oldmode)
  5471. return -TARGET_EINVAL;
  5472. if (seg_not_present == 0)
  5473. return -TARGET_EINVAL;
  5474. }
  5475. /* allocate the LDT */
  5476. if (!ldt_table) {
  5477. env->ldt.base = target_mmap(0,
  5478. TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
  5479. PROT_READ|PROT_WRITE,
  5480. MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
  5481. if (env->ldt.base == -1)
  5482. return -TARGET_ENOMEM;
  5483. memset(g2h_untagged(env->ldt.base), 0,
  5484. TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
  5485. env->ldt.limit = 0xffff;
  5486. ldt_table = g2h_untagged(env->ldt.base);
  5487. }
  5488. /* NOTE: same code as Linux kernel */
  5489. /* Allow LDTs to be cleared by the user. */
  5490. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  5491. if (oldmode ||
  5492. (contents == 0 &&
  5493. read_exec_only == 1 &&
  5494. seg_32bit == 0 &&
  5495. limit_in_pages == 0 &&
  5496. seg_not_present == 1 &&
  5497. useable == 0 )) {
  5498. entry_1 = 0;
  5499. entry_2 = 0;
  5500. goto install;
  5501. }
  5502. }
  5503. entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
  5504. (ldt_info.limit & 0x0ffff);
  5505. entry_2 = (ldt_info.base_addr & 0xff000000) |
  5506. ((ldt_info.base_addr & 0x00ff0000) >> 16) |
  5507. (ldt_info.limit & 0xf0000) |
  5508. ((read_exec_only ^ 1) << 9) |
  5509. (contents << 10) |
  5510. ((seg_not_present ^ 1) << 15) |
  5511. (seg_32bit << 22) |
  5512. (limit_in_pages << 23) |
  5513. (lm << 21) |
  5514. 0x7000;
  5515. if (!oldmode)
  5516. entry_2 |= (useable << 20);
  5517. /* Install the new entry ... */
  5518. install:
  5519. lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
  5520. lp[0] = tswap32(entry_1);
  5521. lp[1] = tswap32(entry_2);
  5522. return 0;
  5523. }
  5524. /* specific and weird i386 syscalls */
  5525. static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
  5526. unsigned long bytecount)
  5527. {
  5528. abi_long ret;
  5529. switch (func) {
  5530. case 0:
  5531. ret = read_ldt(ptr, bytecount);
  5532. break;
  5533. case 1:
  5534. ret = write_ldt(env, ptr, bytecount, 1);
  5535. break;
  5536. case 0x11:
  5537. ret = write_ldt(env, ptr, bytecount, 0);
  5538. break;
  5539. default:
  5540. ret = -TARGET_ENOSYS;
  5541. break;
  5542. }
  5543. return ret;
  5544. }
  5545. #if defined(TARGET_ABI32)
  5546. abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
  5547. {
  5548. uint64_t *gdt_table = g2h_untagged(env->gdt.base);
  5549. struct target_modify_ldt_ldt_s ldt_info;
  5550. struct target_modify_ldt_ldt_s *target_ldt_info;
  5551. int seg_32bit, contents, read_exec_only, limit_in_pages;
  5552. int seg_not_present, useable, lm;
  5553. uint32_t *lp, entry_1, entry_2;
  5554. int i;
  5555. lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
  5556. if (!target_ldt_info)
  5557. return -TARGET_EFAULT;
  5558. ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
  5559. ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
  5560. ldt_info.limit = tswap32(target_ldt_info->limit);
  5561. ldt_info.flags = tswap32(target_ldt_info->flags);
  5562. if (ldt_info.entry_number == -1) {
  5563. for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
  5564. if (gdt_table[i] == 0) {
  5565. ldt_info.entry_number = i;
  5566. target_ldt_info->entry_number = tswap32(i);
  5567. break;
  5568. }
  5569. }
  5570. }
  5571. unlock_user_struct(target_ldt_info, ptr, 1);
  5572. if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
  5573. ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
  5574. return -TARGET_EINVAL;
  5575. seg_32bit = ldt_info.flags & 1;
  5576. contents = (ldt_info.flags >> 1) & 3;
  5577. read_exec_only = (ldt_info.flags >> 3) & 1;
  5578. limit_in_pages = (ldt_info.flags >> 4) & 1;
  5579. seg_not_present = (ldt_info.flags >> 5) & 1;
  5580. useable = (ldt_info.flags >> 6) & 1;
  5581. #ifdef TARGET_ABI32
  5582. lm = 0;
  5583. #else
  5584. lm = (ldt_info.flags >> 7) & 1;
  5585. #endif
  5586. if (contents == 3) {
  5587. if (seg_not_present == 0)
  5588. return -TARGET_EINVAL;
  5589. }
  5590. /* NOTE: same code as Linux kernel */
  5591. /* Allow LDTs to be cleared by the user. */
  5592. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  5593. if ((contents == 0 &&
  5594. read_exec_only == 1 &&
  5595. seg_32bit == 0 &&
  5596. limit_in_pages == 0 &&
  5597. seg_not_present == 1 &&
  5598. useable == 0 )) {
  5599. entry_1 = 0;
  5600. entry_2 = 0;
  5601. goto install;
  5602. }
  5603. }
  5604. entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
  5605. (ldt_info.limit & 0x0ffff);
  5606. entry_2 = (ldt_info.base_addr & 0xff000000) |
  5607. ((ldt_info.base_addr & 0x00ff0000) >> 16) |
  5608. (ldt_info.limit & 0xf0000) |
  5609. ((read_exec_only ^ 1) << 9) |
  5610. (contents << 10) |
  5611. ((seg_not_present ^ 1) << 15) |
  5612. (seg_32bit << 22) |
  5613. (limit_in_pages << 23) |
  5614. (useable << 20) |
  5615. (lm << 21) |
  5616. 0x7000;
  5617. /* Install the new entry ... */
  5618. install:
  5619. lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
  5620. lp[0] = tswap32(entry_1);
  5621. lp[1] = tswap32(entry_2);
  5622. return 0;
  5623. }
  5624. static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
  5625. {
  5626. struct target_modify_ldt_ldt_s *target_ldt_info;
  5627. uint64_t *gdt_table = g2h_untagged(env->gdt.base);
  5628. uint32_t base_addr, limit, flags;
  5629. int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
  5630. int seg_not_present, useable, lm;
  5631. uint32_t *lp, entry_1, entry_2;
  5632. lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
  5633. if (!target_ldt_info)
  5634. return -TARGET_EFAULT;
  5635. idx = tswap32(target_ldt_info->entry_number);
  5636. if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
  5637. idx > TARGET_GDT_ENTRY_TLS_MAX) {
  5638. unlock_user_struct(target_ldt_info, ptr, 1);
  5639. return -TARGET_EINVAL;
  5640. }
  5641. lp = (uint32_t *)(gdt_table + idx);
  5642. entry_1 = tswap32(lp[0]);
  5643. entry_2 = tswap32(lp[1]);
  5644. read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
  5645. contents = (entry_2 >> 10) & 3;
  5646. seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
  5647. seg_32bit = (entry_2 >> 22) & 1;
  5648. limit_in_pages = (entry_2 >> 23) & 1;
  5649. useable = (entry_2 >> 20) & 1;
  5650. #ifdef TARGET_ABI32
  5651. lm = 0;
  5652. #else
  5653. lm = (entry_2 >> 21) & 1;
  5654. #endif
  5655. flags = (seg_32bit << 0) | (contents << 1) |
  5656. (read_exec_only << 3) | (limit_in_pages << 4) |
  5657. (seg_not_present << 5) | (useable << 6) | (lm << 7);
  5658. limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
  5659. base_addr = (entry_1 >> 16) |
  5660. (entry_2 & 0xff000000) |
  5661. ((entry_2 & 0xff) << 16);
  5662. target_ldt_info->base_addr = tswapal(base_addr);
  5663. target_ldt_info->limit = tswap32(limit);
  5664. target_ldt_info->flags = tswap32(flags);
  5665. unlock_user_struct(target_ldt_info, ptr, 1);
  5666. return 0;
  5667. }
  5668. abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
  5669. {
  5670. return -TARGET_ENOSYS;
  5671. }
  5672. #else
  5673. abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
  5674. {
  5675. abi_long ret = 0;
  5676. abi_ulong val;
  5677. int idx;
  5678. switch(code) {
  5679. case TARGET_ARCH_SET_GS:
  5680. case TARGET_ARCH_SET_FS:
  5681. if (code == TARGET_ARCH_SET_GS)
  5682. idx = R_GS;
  5683. else
  5684. idx = R_FS;
  5685. cpu_x86_load_seg(env, idx, 0);
  5686. env->segs[idx].base = addr;
  5687. break;
  5688. case TARGET_ARCH_GET_GS:
  5689. case TARGET_ARCH_GET_FS:
  5690. if (code == TARGET_ARCH_GET_GS)
  5691. idx = R_GS;
  5692. else
  5693. idx = R_FS;
  5694. val = env->segs[idx].base;
  5695. if (put_user(val, addr, abi_ulong))
  5696. ret = -TARGET_EFAULT;
  5697. break;
  5698. default:
  5699. ret = -TARGET_EINVAL;
  5700. break;
  5701. }
  5702. return ret;
  5703. }
  5704. #endif /* defined(TARGET_ABI32 */
  5705. #endif /* defined(TARGET_I386) */
  5706. /*
  5707. * These constants are generic. Supply any that are missing from the host.
  5708. */
  5709. #ifndef PR_SET_NAME
  5710. # define PR_SET_NAME 15
  5711. # define PR_GET_NAME 16
  5712. #endif
  5713. #ifndef PR_SET_FP_MODE
  5714. # define PR_SET_FP_MODE 45
  5715. # define PR_GET_FP_MODE 46
  5716. # define PR_FP_MODE_FR (1 << 0)
  5717. # define PR_FP_MODE_FRE (1 << 1)
  5718. #endif
  5719. #ifndef PR_SVE_SET_VL
  5720. # define PR_SVE_SET_VL 50
  5721. # define PR_SVE_GET_VL 51
  5722. # define PR_SVE_VL_LEN_MASK 0xffff
  5723. # define PR_SVE_VL_INHERIT (1 << 17)
  5724. #endif
  5725. #ifndef PR_PAC_RESET_KEYS
  5726. # define PR_PAC_RESET_KEYS 54
  5727. # define PR_PAC_APIAKEY (1 << 0)
  5728. # define PR_PAC_APIBKEY (1 << 1)
  5729. # define PR_PAC_APDAKEY (1 << 2)
  5730. # define PR_PAC_APDBKEY (1 << 3)
  5731. # define PR_PAC_APGAKEY (1 << 4)
  5732. #endif
  5733. #ifndef PR_SET_TAGGED_ADDR_CTRL
  5734. # define PR_SET_TAGGED_ADDR_CTRL 55
  5735. # define PR_GET_TAGGED_ADDR_CTRL 56
  5736. # define PR_TAGGED_ADDR_ENABLE (1UL << 0)
  5737. #endif
  5738. #ifndef PR_MTE_TCF_SHIFT
  5739. # define PR_MTE_TCF_SHIFT 1
  5740. # define PR_MTE_TCF_NONE (0UL << PR_MTE_TCF_SHIFT)
  5741. # define PR_MTE_TCF_SYNC (1UL << PR_MTE_TCF_SHIFT)
  5742. # define PR_MTE_TCF_ASYNC (2UL << PR_MTE_TCF_SHIFT)
  5743. # define PR_MTE_TCF_MASK (3UL << PR_MTE_TCF_SHIFT)
  5744. # define PR_MTE_TAG_SHIFT 3
  5745. # define PR_MTE_TAG_MASK (0xffffUL << PR_MTE_TAG_SHIFT)
  5746. #endif
  5747. #ifndef PR_SET_IO_FLUSHER
  5748. # define PR_SET_IO_FLUSHER 57
  5749. # define PR_GET_IO_FLUSHER 58
  5750. #endif
  5751. #ifndef PR_SET_SYSCALL_USER_DISPATCH
  5752. # define PR_SET_SYSCALL_USER_DISPATCH 59
  5753. #endif
  5754. #ifndef PR_SME_SET_VL
  5755. # define PR_SME_SET_VL 63
  5756. # define PR_SME_GET_VL 64
  5757. # define PR_SME_VL_LEN_MASK 0xffff
  5758. # define PR_SME_VL_INHERIT (1 << 17)
  5759. #endif
  5760. #include "target_prctl.h"
  5761. static abi_long do_prctl_inval0(CPUArchState *env)
  5762. {
  5763. return -TARGET_EINVAL;
  5764. }
  5765. static abi_long do_prctl_inval1(CPUArchState *env, abi_long arg2)
  5766. {
  5767. return -TARGET_EINVAL;
  5768. }
  5769. #ifndef do_prctl_get_fp_mode
  5770. #define do_prctl_get_fp_mode do_prctl_inval0
  5771. #endif
  5772. #ifndef do_prctl_set_fp_mode
  5773. #define do_prctl_set_fp_mode do_prctl_inval1
  5774. #endif
  5775. #ifndef do_prctl_sve_get_vl
  5776. #define do_prctl_sve_get_vl do_prctl_inval0
  5777. #endif
  5778. #ifndef do_prctl_sve_set_vl
  5779. #define do_prctl_sve_set_vl do_prctl_inval1
  5780. #endif
  5781. #ifndef do_prctl_reset_keys
  5782. #define do_prctl_reset_keys do_prctl_inval1
  5783. #endif
  5784. #ifndef do_prctl_set_tagged_addr_ctrl
  5785. #define do_prctl_set_tagged_addr_ctrl do_prctl_inval1
  5786. #endif
  5787. #ifndef do_prctl_get_tagged_addr_ctrl
  5788. #define do_prctl_get_tagged_addr_ctrl do_prctl_inval0
  5789. #endif
  5790. #ifndef do_prctl_get_unalign
  5791. #define do_prctl_get_unalign do_prctl_inval1
  5792. #endif
  5793. #ifndef do_prctl_set_unalign
  5794. #define do_prctl_set_unalign do_prctl_inval1
  5795. #endif
  5796. #ifndef do_prctl_sme_get_vl
  5797. #define do_prctl_sme_get_vl do_prctl_inval0
  5798. #endif
  5799. #ifndef do_prctl_sme_set_vl
  5800. #define do_prctl_sme_set_vl do_prctl_inval1
  5801. #endif
  5802. static abi_long do_prctl(CPUArchState *env, abi_long option, abi_long arg2,
  5803. abi_long arg3, abi_long arg4, abi_long arg5)
  5804. {
  5805. abi_long ret;
  5806. switch (option) {
  5807. case PR_GET_PDEATHSIG:
  5808. {
  5809. int deathsig;
  5810. ret = get_errno(prctl(PR_GET_PDEATHSIG, &deathsig,
  5811. arg3, arg4, arg5));
  5812. if (!is_error(ret) &&
  5813. put_user_s32(host_to_target_signal(deathsig), arg2)) {
  5814. return -TARGET_EFAULT;
  5815. }
  5816. return ret;
  5817. }
  5818. case PR_SET_PDEATHSIG:
  5819. return get_errno(prctl(PR_SET_PDEATHSIG, target_to_host_signal(arg2),
  5820. arg3, arg4, arg5));
  5821. case PR_GET_NAME:
  5822. {
  5823. void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
  5824. if (!name) {
  5825. return -TARGET_EFAULT;
  5826. }
  5827. ret = get_errno(prctl(PR_GET_NAME, (uintptr_t)name,
  5828. arg3, arg4, arg5));
  5829. unlock_user(name, arg2, 16);
  5830. return ret;
  5831. }
  5832. case PR_SET_NAME:
  5833. {
  5834. void *name = lock_user(VERIFY_READ, arg2, 16, 1);
  5835. if (!name) {
  5836. return -TARGET_EFAULT;
  5837. }
  5838. ret = get_errno(prctl(PR_SET_NAME, (uintptr_t)name,
  5839. arg3, arg4, arg5));
  5840. unlock_user(name, arg2, 0);
  5841. return ret;
  5842. }
  5843. case PR_GET_FP_MODE:
  5844. return do_prctl_get_fp_mode(env);
  5845. case PR_SET_FP_MODE:
  5846. return do_prctl_set_fp_mode(env, arg2);
  5847. case PR_SVE_GET_VL:
  5848. return do_prctl_sve_get_vl(env);
  5849. case PR_SVE_SET_VL:
  5850. return do_prctl_sve_set_vl(env, arg2);
  5851. case PR_SME_GET_VL:
  5852. return do_prctl_sme_get_vl(env);
  5853. case PR_SME_SET_VL:
  5854. return do_prctl_sme_set_vl(env, arg2);
  5855. case PR_PAC_RESET_KEYS:
  5856. if (arg3 || arg4 || arg5) {
  5857. return -TARGET_EINVAL;
  5858. }
  5859. return do_prctl_reset_keys(env, arg2);
  5860. case PR_SET_TAGGED_ADDR_CTRL:
  5861. if (arg3 || arg4 || arg5) {
  5862. return -TARGET_EINVAL;
  5863. }
  5864. return do_prctl_set_tagged_addr_ctrl(env, arg2);
  5865. case PR_GET_TAGGED_ADDR_CTRL:
  5866. if (arg2 || arg3 || arg4 || arg5) {
  5867. return -TARGET_EINVAL;
  5868. }
  5869. return do_prctl_get_tagged_addr_ctrl(env);
  5870. case PR_GET_UNALIGN:
  5871. return do_prctl_get_unalign(env, arg2);
  5872. case PR_SET_UNALIGN:
  5873. return do_prctl_set_unalign(env, arg2);
  5874. case PR_CAP_AMBIENT:
  5875. case PR_CAPBSET_READ:
  5876. case PR_CAPBSET_DROP:
  5877. case PR_GET_DUMPABLE:
  5878. case PR_SET_DUMPABLE:
  5879. case PR_GET_KEEPCAPS:
  5880. case PR_SET_KEEPCAPS:
  5881. case PR_GET_SECUREBITS:
  5882. case PR_SET_SECUREBITS:
  5883. case PR_GET_TIMING:
  5884. case PR_SET_TIMING:
  5885. case PR_GET_TIMERSLACK:
  5886. case PR_SET_TIMERSLACK:
  5887. case PR_MCE_KILL:
  5888. case PR_MCE_KILL_GET:
  5889. case PR_GET_NO_NEW_PRIVS:
  5890. case PR_SET_NO_NEW_PRIVS:
  5891. case PR_GET_IO_FLUSHER:
  5892. case PR_SET_IO_FLUSHER:
  5893. /* Some prctl options have no pointer arguments and we can pass on. */
  5894. return get_errno(prctl(option, arg2, arg3, arg4, arg5));
  5895. case PR_GET_CHILD_SUBREAPER:
  5896. case PR_SET_CHILD_SUBREAPER:
  5897. case PR_GET_SPECULATION_CTRL:
  5898. case PR_SET_SPECULATION_CTRL:
  5899. case PR_GET_TID_ADDRESS:
  5900. /* TODO */
  5901. return -TARGET_EINVAL;
  5902. case PR_GET_FPEXC:
  5903. case PR_SET_FPEXC:
  5904. /* Was used for SPE on PowerPC. */
  5905. return -TARGET_EINVAL;
  5906. case PR_GET_ENDIAN:
  5907. case PR_SET_ENDIAN:
  5908. case PR_GET_FPEMU:
  5909. case PR_SET_FPEMU:
  5910. case PR_SET_MM:
  5911. case PR_GET_SECCOMP:
  5912. case PR_SET_SECCOMP:
  5913. case PR_SET_SYSCALL_USER_DISPATCH:
  5914. case PR_GET_THP_DISABLE:
  5915. case PR_SET_THP_DISABLE:
  5916. case PR_GET_TSC:
  5917. case PR_SET_TSC:
  5918. /* Disable to prevent the target disabling stuff we need. */
  5919. return -TARGET_EINVAL;
  5920. default:
  5921. qemu_log_mask(LOG_UNIMP, "Unsupported prctl: " TARGET_ABI_FMT_ld "\n",
  5922. option);
  5923. return -TARGET_EINVAL;
  5924. }
  5925. }
  5926. #define NEW_STACK_SIZE 0x40000
  5927. static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
  5928. typedef struct {
  5929. CPUArchState *env;
  5930. pthread_mutex_t mutex;
  5931. pthread_cond_t cond;
  5932. pthread_t thread;
  5933. uint32_t tid;
  5934. abi_ulong child_tidptr;
  5935. abi_ulong parent_tidptr;
  5936. sigset_t sigmask;
  5937. } new_thread_info;
  5938. static void *clone_func(void *arg)
  5939. {
  5940. new_thread_info *info = arg;
  5941. CPUArchState *env;
  5942. CPUState *cpu;
  5943. TaskState *ts;
  5944. rcu_register_thread();
  5945. tcg_register_thread();
  5946. env = info->env;
  5947. cpu = env_cpu(env);
  5948. thread_cpu = cpu;
  5949. ts = (TaskState *)cpu->opaque;
  5950. info->tid = sys_gettid();
  5951. task_settid(ts);
  5952. if (info->child_tidptr)
  5953. put_user_u32(info->tid, info->child_tidptr);
  5954. if (info->parent_tidptr)
  5955. put_user_u32(info->tid, info->parent_tidptr);
  5956. qemu_guest_random_seed_thread_part2(cpu->random_seed);
  5957. /* Enable signals. */
  5958. sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
  5959. /* Signal to the parent that we're ready. */
  5960. pthread_mutex_lock(&info->mutex);
  5961. pthread_cond_broadcast(&info->cond);
  5962. pthread_mutex_unlock(&info->mutex);
  5963. /* Wait until the parent has finished initializing the tls state. */
  5964. pthread_mutex_lock(&clone_lock);
  5965. pthread_mutex_unlock(&clone_lock);
  5966. cpu_loop(env);
  5967. /* never exits */
  5968. return NULL;
  5969. }
  5970. /* do_fork() Must return host values and target errnos (unlike most
  5971. do_*() functions). */
  5972. static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
  5973. abi_ulong parent_tidptr, target_ulong newtls,
  5974. abi_ulong child_tidptr)
  5975. {
  5976. CPUState *cpu = env_cpu(env);
  5977. int ret;
  5978. TaskState *ts;
  5979. CPUState *new_cpu;
  5980. CPUArchState *new_env;
  5981. sigset_t sigmask;
  5982. flags &= ~CLONE_IGNORED_FLAGS;
  5983. /* Emulate vfork() with fork() */
  5984. if (flags & CLONE_VFORK)
  5985. flags &= ~(CLONE_VFORK | CLONE_VM);
  5986. if (flags & CLONE_VM) {
  5987. TaskState *parent_ts = (TaskState *)cpu->opaque;
  5988. new_thread_info info;
  5989. pthread_attr_t attr;
  5990. if (((flags & CLONE_THREAD_FLAGS) != CLONE_THREAD_FLAGS) ||
  5991. (flags & CLONE_INVALID_THREAD_FLAGS)) {
  5992. return -TARGET_EINVAL;
  5993. }
  5994. ts = g_new0(TaskState, 1);
  5995. init_task_state(ts);
  5996. /* Grab a mutex so that thread setup appears atomic. */
  5997. pthread_mutex_lock(&clone_lock);
  5998. /*
  5999. * If this is our first additional thread, we need to ensure we
  6000. * generate code for parallel execution and flush old translations.
  6001. * Do this now so that the copy gets CF_PARALLEL too.
  6002. */
  6003. if (!(cpu->tcg_cflags & CF_PARALLEL)) {
  6004. cpu->tcg_cflags |= CF_PARALLEL;
  6005. tb_flush(cpu);
  6006. }
  6007. /* we create a new CPU instance. */
  6008. new_env = cpu_copy(env);
  6009. /* Init regs that differ from the parent. */
  6010. cpu_clone_regs_child(new_env, newsp, flags);
  6011. cpu_clone_regs_parent(env, flags);
  6012. new_cpu = env_cpu(new_env);
  6013. new_cpu->opaque = ts;
  6014. ts->bprm = parent_ts->bprm;
  6015. ts->info = parent_ts->info;
  6016. ts->signal_mask = parent_ts->signal_mask;
  6017. if (flags & CLONE_CHILD_CLEARTID) {
  6018. ts->child_tidptr = child_tidptr;
  6019. }
  6020. if (flags & CLONE_SETTLS) {
  6021. cpu_set_tls (new_env, newtls);
  6022. }
  6023. memset(&info, 0, sizeof(info));
  6024. pthread_mutex_init(&info.mutex, NULL);
  6025. pthread_mutex_lock(&info.mutex);
  6026. pthread_cond_init(&info.cond, NULL);
  6027. info.env = new_env;
  6028. if (flags & CLONE_CHILD_SETTID) {
  6029. info.child_tidptr = child_tidptr;
  6030. }
  6031. if (flags & CLONE_PARENT_SETTID) {
  6032. info.parent_tidptr = parent_tidptr;
  6033. }
  6034. ret = pthread_attr_init(&attr);
  6035. ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
  6036. ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  6037. /* It is not safe to deliver signals until the child has finished
  6038. initializing, so temporarily block all signals. */
  6039. sigfillset(&sigmask);
  6040. sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
  6041. cpu->random_seed = qemu_guest_random_seed_thread_part1();
  6042. ret = pthread_create(&info.thread, &attr, clone_func, &info);
  6043. /* TODO: Free new CPU state if thread creation failed. */
  6044. sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
  6045. pthread_attr_destroy(&attr);
  6046. if (ret == 0) {
  6047. /* Wait for the child to initialize. */
  6048. pthread_cond_wait(&info.cond, &info.mutex);
  6049. ret = info.tid;
  6050. } else {
  6051. ret = -1;
  6052. }
  6053. pthread_mutex_unlock(&info.mutex);
  6054. pthread_cond_destroy(&info.cond);
  6055. pthread_mutex_destroy(&info.mutex);
  6056. pthread_mutex_unlock(&clone_lock);
  6057. } else {
  6058. /* if no CLONE_VM, we consider it is a fork */
  6059. if (flags & CLONE_INVALID_FORK_FLAGS) {
  6060. return -TARGET_EINVAL;
  6061. }
  6062. /* We can't support custom termination signals */
  6063. if ((flags & CSIGNAL) != TARGET_SIGCHLD) {
  6064. return -TARGET_EINVAL;
  6065. }
  6066. if (block_signals()) {
  6067. return -QEMU_ERESTARTSYS;
  6068. }
  6069. fork_start();
  6070. ret = fork();
  6071. if (ret == 0) {
  6072. /* Child Process. */
  6073. cpu_clone_regs_child(env, newsp, flags);
  6074. fork_end(1);
  6075. /* There is a race condition here. The parent process could
  6076. theoretically read the TID in the child process before the child
  6077. tid is set. This would require using either ptrace
  6078. (not implemented) or having *_tidptr to point at a shared memory
  6079. mapping. We can't repeat the spinlock hack used above because
  6080. the child process gets its own copy of the lock. */
  6081. if (flags & CLONE_CHILD_SETTID)
  6082. put_user_u32(sys_gettid(), child_tidptr);
  6083. if (flags & CLONE_PARENT_SETTID)
  6084. put_user_u32(sys_gettid(), parent_tidptr);
  6085. ts = (TaskState *)cpu->opaque;
  6086. if (flags & CLONE_SETTLS)
  6087. cpu_set_tls (env, newtls);
  6088. if (flags & CLONE_CHILD_CLEARTID)
  6089. ts->child_tidptr = child_tidptr;
  6090. } else {
  6091. cpu_clone_regs_parent(env, flags);
  6092. fork_end(0);
  6093. }
  6094. g_assert(!cpu_in_exclusive_context(cpu));
  6095. }
  6096. return ret;
  6097. }
  6098. /* warning : doesn't handle linux specific flags... */
  6099. static int target_to_host_fcntl_cmd(int cmd)
  6100. {
  6101. int ret;
  6102. switch(cmd) {
  6103. case TARGET_F_DUPFD:
  6104. case TARGET_F_GETFD:
  6105. case TARGET_F_SETFD:
  6106. case TARGET_F_GETFL:
  6107. case TARGET_F_SETFL:
  6108. case TARGET_F_OFD_GETLK:
  6109. case TARGET_F_OFD_SETLK:
  6110. case TARGET_F_OFD_SETLKW:
  6111. ret = cmd;
  6112. break;
  6113. case TARGET_F_GETLK:
  6114. ret = F_GETLK64;
  6115. break;
  6116. case TARGET_F_SETLK:
  6117. ret = F_SETLK64;
  6118. break;
  6119. case TARGET_F_SETLKW:
  6120. ret = F_SETLKW64;
  6121. break;
  6122. case TARGET_F_GETOWN:
  6123. ret = F_GETOWN;
  6124. break;
  6125. case TARGET_F_SETOWN:
  6126. ret = F_SETOWN;
  6127. break;
  6128. case TARGET_F_GETSIG:
  6129. ret = F_GETSIG;
  6130. break;
  6131. case TARGET_F_SETSIG:
  6132. ret = F_SETSIG;
  6133. break;
  6134. #if TARGET_ABI_BITS == 32
  6135. case TARGET_F_GETLK64:
  6136. ret = F_GETLK64;
  6137. break;
  6138. case TARGET_F_SETLK64:
  6139. ret = F_SETLK64;
  6140. break;
  6141. case TARGET_F_SETLKW64:
  6142. ret = F_SETLKW64;
  6143. break;
  6144. #endif
  6145. case TARGET_F_SETLEASE:
  6146. ret = F_SETLEASE;
  6147. break;
  6148. case TARGET_F_GETLEASE:
  6149. ret = F_GETLEASE;
  6150. break;
  6151. #ifdef F_DUPFD_CLOEXEC
  6152. case TARGET_F_DUPFD_CLOEXEC:
  6153. ret = F_DUPFD_CLOEXEC;
  6154. break;
  6155. #endif
  6156. case TARGET_F_NOTIFY:
  6157. ret = F_NOTIFY;
  6158. break;
  6159. #ifdef F_GETOWN_EX
  6160. case TARGET_F_GETOWN_EX:
  6161. ret = F_GETOWN_EX;
  6162. break;
  6163. #endif
  6164. #ifdef F_SETOWN_EX
  6165. case TARGET_F_SETOWN_EX:
  6166. ret = F_SETOWN_EX;
  6167. break;
  6168. #endif
  6169. #ifdef F_SETPIPE_SZ
  6170. case TARGET_F_SETPIPE_SZ:
  6171. ret = F_SETPIPE_SZ;
  6172. break;
  6173. case TARGET_F_GETPIPE_SZ:
  6174. ret = F_GETPIPE_SZ;
  6175. break;
  6176. #endif
  6177. #ifdef F_ADD_SEALS
  6178. case TARGET_F_ADD_SEALS:
  6179. ret = F_ADD_SEALS;
  6180. break;
  6181. case TARGET_F_GET_SEALS:
  6182. ret = F_GET_SEALS;
  6183. break;
  6184. #endif
  6185. default:
  6186. ret = -TARGET_EINVAL;
  6187. break;
  6188. }
  6189. #if defined(__powerpc64__)
  6190. /* On PPC64, glibc headers has the F_*LK* defined to 12, 13 and 14 and
  6191. * is not supported by kernel. The glibc fcntl call actually adjusts
  6192. * them to 5, 6 and 7 before making the syscall(). Since we make the
  6193. * syscall directly, adjust to what is supported by the kernel.
  6194. */
  6195. if (ret >= F_GETLK64 && ret <= F_SETLKW64) {
  6196. ret -= F_GETLK64 - 5;
  6197. }
  6198. #endif
  6199. return ret;
  6200. }
  6201. #define FLOCK_TRANSTBL \
  6202. switch (type) { \
  6203. TRANSTBL_CONVERT(F_RDLCK); \
  6204. TRANSTBL_CONVERT(F_WRLCK); \
  6205. TRANSTBL_CONVERT(F_UNLCK); \
  6206. }
  6207. static int target_to_host_flock(int type)
  6208. {
  6209. #define TRANSTBL_CONVERT(a) case TARGET_##a: return a
  6210. FLOCK_TRANSTBL
  6211. #undef TRANSTBL_CONVERT
  6212. return -TARGET_EINVAL;
  6213. }
  6214. static int host_to_target_flock(int type)
  6215. {
  6216. #define TRANSTBL_CONVERT(a) case a: return TARGET_##a
  6217. FLOCK_TRANSTBL
  6218. #undef TRANSTBL_CONVERT
  6219. /* if we don't know how to convert the value coming
  6220. * from the host we copy to the target field as-is
  6221. */
  6222. return type;
  6223. }
  6224. static inline abi_long copy_from_user_flock(struct flock64 *fl,
  6225. abi_ulong target_flock_addr)
  6226. {
  6227. struct target_flock *target_fl;
  6228. int l_type;
  6229. if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
  6230. return -TARGET_EFAULT;
  6231. }
  6232. __get_user(l_type, &target_fl->l_type);
  6233. l_type = target_to_host_flock(l_type);
  6234. if (l_type < 0) {
  6235. return l_type;
  6236. }
  6237. fl->l_type = l_type;
  6238. __get_user(fl->l_whence, &target_fl->l_whence);
  6239. __get_user(fl->l_start, &target_fl->l_start);
  6240. __get_user(fl->l_len, &target_fl->l_len);
  6241. __get_user(fl->l_pid, &target_fl->l_pid);
  6242. unlock_user_struct(target_fl, target_flock_addr, 0);
  6243. return 0;
  6244. }
  6245. static inline abi_long copy_to_user_flock(abi_ulong target_flock_addr,
  6246. const struct flock64 *fl)
  6247. {
  6248. struct target_flock *target_fl;
  6249. short l_type;
  6250. if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
  6251. return -TARGET_EFAULT;
  6252. }
  6253. l_type = host_to_target_flock(fl->l_type);
  6254. __put_user(l_type, &target_fl->l_type);
  6255. __put_user(fl->l_whence, &target_fl->l_whence);
  6256. __put_user(fl->l_start, &target_fl->l_start);
  6257. __put_user(fl->l_len, &target_fl->l_len);
  6258. __put_user(fl->l_pid, &target_fl->l_pid);
  6259. unlock_user_struct(target_fl, target_flock_addr, 1);
  6260. return 0;
  6261. }
  6262. typedef abi_long from_flock64_fn(struct flock64 *fl, abi_ulong target_addr);
  6263. typedef abi_long to_flock64_fn(abi_ulong target_addr, const struct flock64 *fl);
  6264. #if defined(TARGET_ARM) && TARGET_ABI_BITS == 32
  6265. struct target_oabi_flock64 {
  6266. abi_short l_type;
  6267. abi_short l_whence;
  6268. abi_llong l_start;
  6269. abi_llong l_len;
  6270. abi_int l_pid;
  6271. } QEMU_PACKED;
  6272. static inline abi_long copy_from_user_oabi_flock64(struct flock64 *fl,
  6273. abi_ulong target_flock_addr)
  6274. {
  6275. struct target_oabi_flock64 *target_fl;
  6276. int l_type;
  6277. if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
  6278. return -TARGET_EFAULT;
  6279. }
  6280. __get_user(l_type, &target_fl->l_type);
  6281. l_type = target_to_host_flock(l_type);
  6282. if (l_type < 0) {
  6283. return l_type;
  6284. }
  6285. fl->l_type = l_type;
  6286. __get_user(fl->l_whence, &target_fl->l_whence);
  6287. __get_user(fl->l_start, &target_fl->l_start);
  6288. __get_user(fl->l_len, &target_fl->l_len);
  6289. __get_user(fl->l_pid, &target_fl->l_pid);
  6290. unlock_user_struct(target_fl, target_flock_addr, 0);
  6291. return 0;
  6292. }
  6293. static inline abi_long copy_to_user_oabi_flock64(abi_ulong target_flock_addr,
  6294. const struct flock64 *fl)
  6295. {
  6296. struct target_oabi_flock64 *target_fl;
  6297. short l_type;
  6298. if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
  6299. return -TARGET_EFAULT;
  6300. }
  6301. l_type = host_to_target_flock(fl->l_type);
  6302. __put_user(l_type, &target_fl->l_type);
  6303. __put_user(fl->l_whence, &target_fl->l_whence);
  6304. __put_user(fl->l_start, &target_fl->l_start);
  6305. __put_user(fl->l_len, &target_fl->l_len);
  6306. __put_user(fl->l_pid, &target_fl->l_pid);
  6307. unlock_user_struct(target_fl, target_flock_addr, 1);
  6308. return 0;
  6309. }
  6310. #endif
  6311. static inline abi_long copy_from_user_flock64(struct flock64 *fl,
  6312. abi_ulong target_flock_addr)
  6313. {
  6314. struct target_flock64 *target_fl;
  6315. int l_type;
  6316. if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
  6317. return -TARGET_EFAULT;
  6318. }
  6319. __get_user(l_type, &target_fl->l_type);
  6320. l_type = target_to_host_flock(l_type);
  6321. if (l_type < 0) {
  6322. return l_type;
  6323. }
  6324. fl->l_type = l_type;
  6325. __get_user(fl->l_whence, &target_fl->l_whence);
  6326. __get_user(fl->l_start, &target_fl->l_start);
  6327. __get_user(fl->l_len, &target_fl->l_len);
  6328. __get_user(fl->l_pid, &target_fl->l_pid);
  6329. unlock_user_struct(target_fl, target_flock_addr, 0);
  6330. return 0;
  6331. }
  6332. static inline abi_long copy_to_user_flock64(abi_ulong target_flock_addr,
  6333. const struct flock64 *fl)
  6334. {
  6335. struct target_flock64 *target_fl;
  6336. short l_type;
  6337. if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
  6338. return -TARGET_EFAULT;
  6339. }
  6340. l_type = host_to_target_flock(fl->l_type);
  6341. __put_user(l_type, &target_fl->l_type);
  6342. __put_user(fl->l_whence, &target_fl->l_whence);
  6343. __put_user(fl->l_start, &target_fl->l_start);
  6344. __put_user(fl->l_len, &target_fl->l_len);
  6345. __put_user(fl->l_pid, &target_fl->l_pid);
  6346. unlock_user_struct(target_fl, target_flock_addr, 1);
  6347. return 0;
  6348. }
  6349. static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
  6350. {
  6351. struct flock64 fl64;
  6352. #ifdef F_GETOWN_EX
  6353. struct f_owner_ex fox;
  6354. struct target_f_owner_ex *target_fox;
  6355. #endif
  6356. abi_long ret;
  6357. int host_cmd = target_to_host_fcntl_cmd(cmd);
  6358. if (host_cmd == -TARGET_EINVAL)
  6359. return host_cmd;
  6360. switch(cmd) {
  6361. case TARGET_F_GETLK:
  6362. ret = copy_from_user_flock(&fl64, arg);
  6363. if (ret) {
  6364. return ret;
  6365. }
  6366. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  6367. if (ret == 0) {
  6368. ret = copy_to_user_flock(arg, &fl64);
  6369. }
  6370. break;
  6371. case TARGET_F_SETLK:
  6372. case TARGET_F_SETLKW:
  6373. ret = copy_from_user_flock(&fl64, arg);
  6374. if (ret) {
  6375. return ret;
  6376. }
  6377. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  6378. break;
  6379. case TARGET_F_GETLK64:
  6380. case TARGET_F_OFD_GETLK:
  6381. ret = copy_from_user_flock64(&fl64, arg);
  6382. if (ret) {
  6383. return ret;
  6384. }
  6385. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  6386. if (ret == 0) {
  6387. ret = copy_to_user_flock64(arg, &fl64);
  6388. }
  6389. break;
  6390. case TARGET_F_SETLK64:
  6391. case TARGET_F_SETLKW64:
  6392. case TARGET_F_OFD_SETLK:
  6393. case TARGET_F_OFD_SETLKW:
  6394. ret = copy_from_user_flock64(&fl64, arg);
  6395. if (ret) {
  6396. return ret;
  6397. }
  6398. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  6399. break;
  6400. case TARGET_F_GETFL:
  6401. ret = get_errno(safe_fcntl(fd, host_cmd, arg));
  6402. if (ret >= 0) {
  6403. ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
  6404. }
  6405. break;
  6406. case TARGET_F_SETFL:
  6407. ret = get_errno(safe_fcntl(fd, host_cmd,
  6408. target_to_host_bitmask(arg,
  6409. fcntl_flags_tbl)));
  6410. break;
  6411. #ifdef F_GETOWN_EX
  6412. case TARGET_F_GETOWN_EX:
  6413. ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
  6414. if (ret >= 0) {
  6415. if (!lock_user_struct(VERIFY_WRITE, target_fox, arg, 0))
  6416. return -TARGET_EFAULT;
  6417. target_fox->type = tswap32(fox.type);
  6418. target_fox->pid = tswap32(fox.pid);
  6419. unlock_user_struct(target_fox, arg, 1);
  6420. }
  6421. break;
  6422. #endif
  6423. #ifdef F_SETOWN_EX
  6424. case TARGET_F_SETOWN_EX:
  6425. if (!lock_user_struct(VERIFY_READ, target_fox, arg, 1))
  6426. return -TARGET_EFAULT;
  6427. fox.type = tswap32(target_fox->type);
  6428. fox.pid = tswap32(target_fox->pid);
  6429. unlock_user_struct(target_fox, arg, 0);
  6430. ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
  6431. break;
  6432. #endif
  6433. case TARGET_F_SETSIG:
  6434. ret = get_errno(safe_fcntl(fd, host_cmd, target_to_host_signal(arg)));
  6435. break;
  6436. case TARGET_F_GETSIG:
  6437. ret = host_to_target_signal(get_errno(safe_fcntl(fd, host_cmd, arg)));
  6438. break;
  6439. case TARGET_F_SETOWN:
  6440. case TARGET_F_GETOWN:
  6441. case TARGET_F_SETLEASE:
  6442. case TARGET_F_GETLEASE:
  6443. case TARGET_F_SETPIPE_SZ:
  6444. case TARGET_F_GETPIPE_SZ:
  6445. case TARGET_F_ADD_SEALS:
  6446. case TARGET_F_GET_SEALS:
  6447. ret = get_errno(safe_fcntl(fd, host_cmd, arg));
  6448. break;
  6449. default:
  6450. ret = get_errno(safe_fcntl(fd, cmd, arg));
  6451. break;
  6452. }
  6453. return ret;
  6454. }
  6455. #ifdef USE_UID16
  6456. static inline int high2lowuid(int uid)
  6457. {
  6458. if (uid > 65535)
  6459. return 65534;
  6460. else
  6461. return uid;
  6462. }
  6463. static inline int high2lowgid(int gid)
  6464. {
  6465. if (gid > 65535)
  6466. return 65534;
  6467. else
  6468. return gid;
  6469. }
  6470. static inline int low2highuid(int uid)
  6471. {
  6472. if ((int16_t)uid == -1)
  6473. return -1;
  6474. else
  6475. return uid;
  6476. }
  6477. static inline int low2highgid(int gid)
  6478. {
  6479. if ((int16_t)gid == -1)
  6480. return -1;
  6481. else
  6482. return gid;
  6483. }
  6484. static inline int tswapid(int id)
  6485. {
  6486. return tswap16(id);
  6487. }
  6488. #define put_user_id(x, gaddr) put_user_u16(x, gaddr)
  6489. #else /* !USE_UID16 */
  6490. static inline int high2lowuid(int uid)
  6491. {
  6492. return uid;
  6493. }
  6494. static inline int high2lowgid(int gid)
  6495. {
  6496. return gid;
  6497. }
  6498. static inline int low2highuid(int uid)
  6499. {
  6500. return uid;
  6501. }
  6502. static inline int low2highgid(int gid)
  6503. {
  6504. return gid;
  6505. }
  6506. static inline int tswapid(int id)
  6507. {
  6508. return tswap32(id);
  6509. }
  6510. #define put_user_id(x, gaddr) put_user_u32(x, gaddr)
  6511. #endif /* USE_UID16 */
  6512. /* We must do direct syscalls for setting UID/GID, because we want to
  6513. * implement the Linux system call semantics of "change only for this thread",
  6514. * not the libc/POSIX semantics of "change for all threads in process".
  6515. * (See http://ewontfix.com/17/ for more details.)
  6516. * We use the 32-bit version of the syscalls if present; if it is not
  6517. * then either the host architecture supports 32-bit UIDs natively with
  6518. * the standard syscall, or the 16-bit UID is the best we can do.
  6519. */
  6520. #ifdef __NR_setuid32
  6521. #define __NR_sys_setuid __NR_setuid32
  6522. #else
  6523. #define __NR_sys_setuid __NR_setuid
  6524. #endif
  6525. #ifdef __NR_setgid32
  6526. #define __NR_sys_setgid __NR_setgid32
  6527. #else
  6528. #define __NR_sys_setgid __NR_setgid
  6529. #endif
  6530. #ifdef __NR_setresuid32
  6531. #define __NR_sys_setresuid __NR_setresuid32
  6532. #else
  6533. #define __NR_sys_setresuid __NR_setresuid
  6534. #endif
  6535. #ifdef __NR_setresgid32
  6536. #define __NR_sys_setresgid __NR_setresgid32
  6537. #else
  6538. #define __NR_sys_setresgid __NR_setresgid
  6539. #endif
  6540. _syscall1(int, sys_setuid, uid_t, uid)
  6541. _syscall1(int, sys_setgid, gid_t, gid)
  6542. _syscall3(int, sys_setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
  6543. _syscall3(int, sys_setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
  6544. void syscall_init(void)
  6545. {
  6546. IOCTLEntry *ie;
  6547. const argtype *arg_type;
  6548. int size;
  6549. thunk_init(STRUCT_MAX);
  6550. #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
  6551. #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
  6552. #include "syscall_types.h"
  6553. #undef STRUCT
  6554. #undef STRUCT_SPECIAL
  6555. /* we patch the ioctl size if necessary. We rely on the fact that
  6556. no ioctl has all the bits at '1' in the size field */
  6557. ie = ioctl_entries;
  6558. while (ie->target_cmd != 0) {
  6559. if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
  6560. TARGET_IOC_SIZEMASK) {
  6561. arg_type = ie->arg_type;
  6562. if (arg_type[0] != TYPE_PTR) {
  6563. fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
  6564. ie->target_cmd);
  6565. exit(1);
  6566. }
  6567. arg_type++;
  6568. size = thunk_type_size(arg_type, 0);
  6569. ie->target_cmd = (ie->target_cmd &
  6570. ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
  6571. (size << TARGET_IOC_SIZESHIFT);
  6572. }
  6573. /* automatic consistency check if same arch */
  6574. #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
  6575. (defined(__x86_64__) && defined(TARGET_X86_64))
  6576. if (unlikely(ie->target_cmd != ie->host_cmd)) {
  6577. fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
  6578. ie->name, ie->target_cmd, ie->host_cmd);
  6579. }
  6580. #endif
  6581. ie++;
  6582. }
  6583. }
  6584. #ifdef TARGET_NR_truncate64
  6585. static inline abi_long target_truncate64(CPUArchState *cpu_env, const char *arg1,
  6586. abi_long arg2,
  6587. abi_long arg3,
  6588. abi_long arg4)
  6589. {
  6590. if (regpairs_aligned(cpu_env, TARGET_NR_truncate64)) {
  6591. arg2 = arg3;
  6592. arg3 = arg4;
  6593. }
  6594. return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
  6595. }
  6596. #endif
  6597. #ifdef TARGET_NR_ftruncate64
  6598. static inline abi_long target_ftruncate64(CPUArchState *cpu_env, abi_long arg1,
  6599. abi_long arg2,
  6600. abi_long arg3,
  6601. abi_long arg4)
  6602. {
  6603. if (regpairs_aligned(cpu_env, TARGET_NR_ftruncate64)) {
  6604. arg2 = arg3;
  6605. arg3 = arg4;
  6606. }
  6607. return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
  6608. }
  6609. #endif
  6610. #if defined(TARGET_NR_timer_settime) || \
  6611. (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
  6612. static inline abi_long target_to_host_itimerspec(struct itimerspec *host_its,
  6613. abi_ulong target_addr)
  6614. {
  6615. if (target_to_host_timespec(&host_its->it_interval, target_addr +
  6616. offsetof(struct target_itimerspec,
  6617. it_interval)) ||
  6618. target_to_host_timespec(&host_its->it_value, target_addr +
  6619. offsetof(struct target_itimerspec,
  6620. it_value))) {
  6621. return -TARGET_EFAULT;
  6622. }
  6623. return 0;
  6624. }
  6625. #endif
  6626. #if defined(TARGET_NR_timer_settime64) || \
  6627. (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD))
  6628. static inline abi_long target_to_host_itimerspec64(struct itimerspec *host_its,
  6629. abi_ulong target_addr)
  6630. {
  6631. if (target_to_host_timespec64(&host_its->it_interval, target_addr +
  6632. offsetof(struct target__kernel_itimerspec,
  6633. it_interval)) ||
  6634. target_to_host_timespec64(&host_its->it_value, target_addr +
  6635. offsetof(struct target__kernel_itimerspec,
  6636. it_value))) {
  6637. return -TARGET_EFAULT;
  6638. }
  6639. return 0;
  6640. }
  6641. #endif
  6642. #if ((defined(TARGET_NR_timerfd_gettime) || \
  6643. defined(TARGET_NR_timerfd_settime)) && defined(CONFIG_TIMERFD)) || \
  6644. defined(TARGET_NR_timer_gettime) || defined(TARGET_NR_timer_settime)
  6645. static inline abi_long host_to_target_itimerspec(abi_ulong target_addr,
  6646. struct itimerspec *host_its)
  6647. {
  6648. if (host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
  6649. it_interval),
  6650. &host_its->it_interval) ||
  6651. host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
  6652. it_value),
  6653. &host_its->it_value)) {
  6654. return -TARGET_EFAULT;
  6655. }
  6656. return 0;
  6657. }
  6658. #endif
  6659. #if ((defined(TARGET_NR_timerfd_gettime64) || \
  6660. defined(TARGET_NR_timerfd_settime64)) && defined(CONFIG_TIMERFD)) || \
  6661. defined(TARGET_NR_timer_gettime64) || defined(TARGET_NR_timer_settime64)
  6662. static inline abi_long host_to_target_itimerspec64(abi_ulong target_addr,
  6663. struct itimerspec *host_its)
  6664. {
  6665. if (host_to_target_timespec64(target_addr +
  6666. offsetof(struct target__kernel_itimerspec,
  6667. it_interval),
  6668. &host_its->it_interval) ||
  6669. host_to_target_timespec64(target_addr +
  6670. offsetof(struct target__kernel_itimerspec,
  6671. it_value),
  6672. &host_its->it_value)) {
  6673. return -TARGET_EFAULT;
  6674. }
  6675. return 0;
  6676. }
  6677. #endif
  6678. #if defined(TARGET_NR_adjtimex) || \
  6679. (defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME))
  6680. static inline abi_long target_to_host_timex(struct timex *host_tx,
  6681. abi_long target_addr)
  6682. {
  6683. struct target_timex *target_tx;
  6684. if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) {
  6685. return -TARGET_EFAULT;
  6686. }
  6687. __get_user(host_tx->modes, &target_tx->modes);
  6688. __get_user(host_tx->offset, &target_tx->offset);
  6689. __get_user(host_tx->freq, &target_tx->freq);
  6690. __get_user(host_tx->maxerror, &target_tx->maxerror);
  6691. __get_user(host_tx->esterror, &target_tx->esterror);
  6692. __get_user(host_tx->status, &target_tx->status);
  6693. __get_user(host_tx->constant, &target_tx->constant);
  6694. __get_user(host_tx->precision, &target_tx->precision);
  6695. __get_user(host_tx->tolerance, &target_tx->tolerance);
  6696. __get_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
  6697. __get_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
  6698. __get_user(host_tx->tick, &target_tx->tick);
  6699. __get_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  6700. __get_user(host_tx->jitter, &target_tx->jitter);
  6701. __get_user(host_tx->shift, &target_tx->shift);
  6702. __get_user(host_tx->stabil, &target_tx->stabil);
  6703. __get_user(host_tx->jitcnt, &target_tx->jitcnt);
  6704. __get_user(host_tx->calcnt, &target_tx->calcnt);
  6705. __get_user(host_tx->errcnt, &target_tx->errcnt);
  6706. __get_user(host_tx->stbcnt, &target_tx->stbcnt);
  6707. __get_user(host_tx->tai, &target_tx->tai);
  6708. unlock_user_struct(target_tx, target_addr, 0);
  6709. return 0;
  6710. }
  6711. static inline abi_long host_to_target_timex(abi_long target_addr,
  6712. struct timex *host_tx)
  6713. {
  6714. struct target_timex *target_tx;
  6715. if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) {
  6716. return -TARGET_EFAULT;
  6717. }
  6718. __put_user(host_tx->modes, &target_tx->modes);
  6719. __put_user(host_tx->offset, &target_tx->offset);
  6720. __put_user(host_tx->freq, &target_tx->freq);
  6721. __put_user(host_tx->maxerror, &target_tx->maxerror);
  6722. __put_user(host_tx->esterror, &target_tx->esterror);
  6723. __put_user(host_tx->status, &target_tx->status);
  6724. __put_user(host_tx->constant, &target_tx->constant);
  6725. __put_user(host_tx->precision, &target_tx->precision);
  6726. __put_user(host_tx->tolerance, &target_tx->tolerance);
  6727. __put_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
  6728. __put_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
  6729. __put_user(host_tx->tick, &target_tx->tick);
  6730. __put_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  6731. __put_user(host_tx->jitter, &target_tx->jitter);
  6732. __put_user(host_tx->shift, &target_tx->shift);
  6733. __put_user(host_tx->stabil, &target_tx->stabil);
  6734. __put_user(host_tx->jitcnt, &target_tx->jitcnt);
  6735. __put_user(host_tx->calcnt, &target_tx->calcnt);
  6736. __put_user(host_tx->errcnt, &target_tx->errcnt);
  6737. __put_user(host_tx->stbcnt, &target_tx->stbcnt);
  6738. __put_user(host_tx->tai, &target_tx->tai);
  6739. unlock_user_struct(target_tx, target_addr, 1);
  6740. return 0;
  6741. }
  6742. #endif
  6743. #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
  6744. static inline abi_long target_to_host_timex64(struct timex *host_tx,
  6745. abi_long target_addr)
  6746. {
  6747. struct target__kernel_timex *target_tx;
  6748. if (copy_from_user_timeval64(&host_tx->time, target_addr +
  6749. offsetof(struct target__kernel_timex,
  6750. time))) {
  6751. return -TARGET_EFAULT;
  6752. }
  6753. if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) {
  6754. return -TARGET_EFAULT;
  6755. }
  6756. __get_user(host_tx->modes, &target_tx->modes);
  6757. __get_user(host_tx->offset, &target_tx->offset);
  6758. __get_user(host_tx->freq, &target_tx->freq);
  6759. __get_user(host_tx->maxerror, &target_tx->maxerror);
  6760. __get_user(host_tx->esterror, &target_tx->esterror);
  6761. __get_user(host_tx->status, &target_tx->status);
  6762. __get_user(host_tx->constant, &target_tx->constant);
  6763. __get_user(host_tx->precision, &target_tx->precision);
  6764. __get_user(host_tx->tolerance, &target_tx->tolerance);
  6765. __get_user(host_tx->tick, &target_tx->tick);
  6766. __get_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  6767. __get_user(host_tx->jitter, &target_tx->jitter);
  6768. __get_user(host_tx->shift, &target_tx->shift);
  6769. __get_user(host_tx->stabil, &target_tx->stabil);
  6770. __get_user(host_tx->jitcnt, &target_tx->jitcnt);
  6771. __get_user(host_tx->calcnt, &target_tx->calcnt);
  6772. __get_user(host_tx->errcnt, &target_tx->errcnt);
  6773. __get_user(host_tx->stbcnt, &target_tx->stbcnt);
  6774. __get_user(host_tx->tai, &target_tx->tai);
  6775. unlock_user_struct(target_tx, target_addr, 0);
  6776. return 0;
  6777. }
  6778. static inline abi_long host_to_target_timex64(abi_long target_addr,
  6779. struct timex *host_tx)
  6780. {
  6781. struct target__kernel_timex *target_tx;
  6782. if (copy_to_user_timeval64(target_addr +
  6783. offsetof(struct target__kernel_timex, time),
  6784. &host_tx->time)) {
  6785. return -TARGET_EFAULT;
  6786. }
  6787. if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) {
  6788. return -TARGET_EFAULT;
  6789. }
  6790. __put_user(host_tx->modes, &target_tx->modes);
  6791. __put_user(host_tx->offset, &target_tx->offset);
  6792. __put_user(host_tx->freq, &target_tx->freq);
  6793. __put_user(host_tx->maxerror, &target_tx->maxerror);
  6794. __put_user(host_tx->esterror, &target_tx->esterror);
  6795. __put_user(host_tx->status, &target_tx->status);
  6796. __put_user(host_tx->constant, &target_tx->constant);
  6797. __put_user(host_tx->precision, &target_tx->precision);
  6798. __put_user(host_tx->tolerance, &target_tx->tolerance);
  6799. __put_user(host_tx->tick, &target_tx->tick);
  6800. __put_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  6801. __put_user(host_tx->jitter, &target_tx->jitter);
  6802. __put_user(host_tx->shift, &target_tx->shift);
  6803. __put_user(host_tx->stabil, &target_tx->stabil);
  6804. __put_user(host_tx->jitcnt, &target_tx->jitcnt);
  6805. __put_user(host_tx->calcnt, &target_tx->calcnt);
  6806. __put_user(host_tx->errcnt, &target_tx->errcnt);
  6807. __put_user(host_tx->stbcnt, &target_tx->stbcnt);
  6808. __put_user(host_tx->tai, &target_tx->tai);
  6809. unlock_user_struct(target_tx, target_addr, 1);
  6810. return 0;
  6811. }
  6812. #endif
  6813. #ifndef HAVE_SIGEV_NOTIFY_THREAD_ID
  6814. #define sigev_notify_thread_id _sigev_un._tid
  6815. #endif
  6816. static inline abi_long target_to_host_sigevent(struct sigevent *host_sevp,
  6817. abi_ulong target_addr)
  6818. {
  6819. struct target_sigevent *target_sevp;
  6820. if (!lock_user_struct(VERIFY_READ, target_sevp, target_addr, 1)) {
  6821. return -TARGET_EFAULT;
  6822. }
  6823. /* This union is awkward on 64 bit systems because it has a 32 bit
  6824. * integer and a pointer in it; we follow the conversion approach
  6825. * used for handling sigval types in signal.c so the guest should get
  6826. * the correct value back even if we did a 64 bit byteswap and it's
  6827. * using the 32 bit integer.
  6828. */
  6829. host_sevp->sigev_value.sival_ptr =
  6830. (void *)(uintptr_t)tswapal(target_sevp->sigev_value.sival_ptr);
  6831. host_sevp->sigev_signo =
  6832. target_to_host_signal(tswap32(target_sevp->sigev_signo));
  6833. host_sevp->sigev_notify = tswap32(target_sevp->sigev_notify);
  6834. host_sevp->sigev_notify_thread_id = tswap32(target_sevp->_sigev_un._tid);
  6835. unlock_user_struct(target_sevp, target_addr, 1);
  6836. return 0;
  6837. }
  6838. #if defined(TARGET_NR_mlockall)
  6839. static inline int target_to_host_mlockall_arg(int arg)
  6840. {
  6841. int result = 0;
  6842. if (arg & TARGET_MCL_CURRENT) {
  6843. result |= MCL_CURRENT;
  6844. }
  6845. if (arg & TARGET_MCL_FUTURE) {
  6846. result |= MCL_FUTURE;
  6847. }
  6848. #ifdef MCL_ONFAULT
  6849. if (arg & TARGET_MCL_ONFAULT) {
  6850. result |= MCL_ONFAULT;
  6851. }
  6852. #endif
  6853. return result;
  6854. }
  6855. #endif
  6856. #if (defined(TARGET_NR_stat64) || defined(TARGET_NR_lstat64) || \
  6857. defined(TARGET_NR_fstat64) || defined(TARGET_NR_fstatat64) || \
  6858. defined(TARGET_NR_newfstatat))
  6859. static inline abi_long host_to_target_stat64(CPUArchState *cpu_env,
  6860. abi_ulong target_addr,
  6861. struct stat *host_st)
  6862. {
  6863. #if defined(TARGET_ARM) && defined(TARGET_ABI32)
  6864. if (cpu_env->eabi) {
  6865. struct target_eabi_stat64 *target_st;
  6866. if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
  6867. return -TARGET_EFAULT;
  6868. memset(target_st, 0, sizeof(struct target_eabi_stat64));
  6869. __put_user(host_st->st_dev, &target_st->st_dev);
  6870. __put_user(host_st->st_ino, &target_st->st_ino);
  6871. #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
  6872. __put_user(host_st->st_ino, &target_st->__st_ino);
  6873. #endif
  6874. __put_user(host_st->st_mode, &target_st->st_mode);
  6875. __put_user(host_st->st_nlink, &target_st->st_nlink);
  6876. __put_user(host_st->st_uid, &target_st->st_uid);
  6877. __put_user(host_st->st_gid, &target_st->st_gid);
  6878. __put_user(host_st->st_rdev, &target_st->st_rdev);
  6879. __put_user(host_st->st_size, &target_st->st_size);
  6880. __put_user(host_st->st_blksize, &target_st->st_blksize);
  6881. __put_user(host_st->st_blocks, &target_st->st_blocks);
  6882. __put_user(host_st->st_atime, &target_st->target_st_atime);
  6883. __put_user(host_st->st_mtime, &target_st->target_st_mtime);
  6884. __put_user(host_st->st_ctime, &target_st->target_st_ctime);
  6885. #ifdef HAVE_STRUCT_STAT_ST_ATIM
  6886. __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
  6887. __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
  6888. __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
  6889. #endif
  6890. unlock_user_struct(target_st, target_addr, 1);
  6891. } else
  6892. #endif
  6893. {
  6894. #if defined(TARGET_HAS_STRUCT_STAT64)
  6895. struct target_stat64 *target_st;
  6896. #else
  6897. struct target_stat *target_st;
  6898. #endif
  6899. if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
  6900. return -TARGET_EFAULT;
  6901. memset(target_st, 0, sizeof(*target_st));
  6902. __put_user(host_st->st_dev, &target_st->st_dev);
  6903. __put_user(host_st->st_ino, &target_st->st_ino);
  6904. #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
  6905. __put_user(host_st->st_ino, &target_st->__st_ino);
  6906. #endif
  6907. __put_user(host_st->st_mode, &target_st->st_mode);
  6908. __put_user(host_st->st_nlink, &target_st->st_nlink);
  6909. __put_user(host_st->st_uid, &target_st->st_uid);
  6910. __put_user(host_st->st_gid, &target_st->st_gid);
  6911. __put_user(host_st->st_rdev, &target_st->st_rdev);
  6912. /* XXX: better use of kernel struct */
  6913. __put_user(host_st->st_size, &target_st->st_size);
  6914. __put_user(host_st->st_blksize, &target_st->st_blksize);
  6915. __put_user(host_st->st_blocks, &target_st->st_blocks);
  6916. __put_user(host_st->st_atime, &target_st->target_st_atime);
  6917. __put_user(host_st->st_mtime, &target_st->target_st_mtime);
  6918. __put_user(host_st->st_ctime, &target_st->target_st_ctime);
  6919. #ifdef HAVE_STRUCT_STAT_ST_ATIM
  6920. __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
  6921. __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
  6922. __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
  6923. #endif
  6924. unlock_user_struct(target_st, target_addr, 1);
  6925. }
  6926. return 0;
  6927. }
  6928. #endif
  6929. #if defined(TARGET_NR_statx) && defined(__NR_statx)
  6930. static inline abi_long host_to_target_statx(struct target_statx *host_stx,
  6931. abi_ulong target_addr)
  6932. {
  6933. struct target_statx *target_stx;
  6934. if (!lock_user_struct(VERIFY_WRITE, target_stx, target_addr, 0)) {
  6935. return -TARGET_EFAULT;
  6936. }
  6937. memset(target_stx, 0, sizeof(*target_stx));
  6938. __put_user(host_stx->stx_mask, &target_stx->stx_mask);
  6939. __put_user(host_stx->stx_blksize, &target_stx->stx_blksize);
  6940. __put_user(host_stx->stx_attributes, &target_stx->stx_attributes);
  6941. __put_user(host_stx->stx_nlink, &target_stx->stx_nlink);
  6942. __put_user(host_stx->stx_uid, &target_stx->stx_uid);
  6943. __put_user(host_stx->stx_gid, &target_stx->stx_gid);
  6944. __put_user(host_stx->stx_mode, &target_stx->stx_mode);
  6945. __put_user(host_stx->stx_ino, &target_stx->stx_ino);
  6946. __put_user(host_stx->stx_size, &target_stx->stx_size);
  6947. __put_user(host_stx->stx_blocks, &target_stx->stx_blocks);
  6948. __put_user(host_stx->stx_attributes_mask, &target_stx->stx_attributes_mask);
  6949. __put_user(host_stx->stx_atime.tv_sec, &target_stx->stx_atime.tv_sec);
  6950. __put_user(host_stx->stx_atime.tv_nsec, &target_stx->stx_atime.tv_nsec);
  6951. __put_user(host_stx->stx_btime.tv_sec, &target_stx->stx_btime.tv_sec);
  6952. __put_user(host_stx->stx_btime.tv_nsec, &target_stx->stx_btime.tv_nsec);
  6953. __put_user(host_stx->stx_ctime.tv_sec, &target_stx->stx_ctime.tv_sec);
  6954. __put_user(host_stx->stx_ctime.tv_nsec, &target_stx->stx_ctime.tv_nsec);
  6955. __put_user(host_stx->stx_mtime.tv_sec, &target_stx->stx_mtime.tv_sec);
  6956. __put_user(host_stx->stx_mtime.tv_nsec, &target_stx->stx_mtime.tv_nsec);
  6957. __put_user(host_stx->stx_rdev_major, &target_stx->stx_rdev_major);
  6958. __put_user(host_stx->stx_rdev_minor, &target_stx->stx_rdev_minor);
  6959. __put_user(host_stx->stx_dev_major, &target_stx->stx_dev_major);
  6960. __put_user(host_stx->stx_dev_minor, &target_stx->stx_dev_minor);
  6961. unlock_user_struct(target_stx, target_addr, 1);
  6962. return 0;
  6963. }
  6964. #endif
  6965. static int do_sys_futex(int *uaddr, int op, int val,
  6966. const struct timespec *timeout, int *uaddr2,
  6967. int val3)
  6968. {
  6969. #if HOST_LONG_BITS == 64
  6970. #if defined(__NR_futex)
  6971. /* always a 64-bit time_t, it doesn't define _time64 version */
  6972. return sys_futex(uaddr, op, val, timeout, uaddr2, val3);
  6973. #endif
  6974. #else /* HOST_LONG_BITS == 64 */
  6975. #if defined(__NR_futex_time64)
  6976. if (sizeof(timeout->tv_sec) == 8) {
  6977. /* _time64 function on 32bit arch */
  6978. return sys_futex_time64(uaddr, op, val, timeout, uaddr2, val3);
  6979. }
  6980. #endif
  6981. #if defined(__NR_futex)
  6982. /* old function on 32bit arch */
  6983. return sys_futex(uaddr, op, val, timeout, uaddr2, val3);
  6984. #endif
  6985. #endif /* HOST_LONG_BITS == 64 */
  6986. g_assert_not_reached();
  6987. }
  6988. static int do_safe_futex(int *uaddr, int op, int val,
  6989. const struct timespec *timeout, int *uaddr2,
  6990. int val3)
  6991. {
  6992. #if HOST_LONG_BITS == 64
  6993. #if defined(__NR_futex)
  6994. /* always a 64-bit time_t, it doesn't define _time64 version */
  6995. return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3));
  6996. #endif
  6997. #else /* HOST_LONG_BITS == 64 */
  6998. #if defined(__NR_futex_time64)
  6999. if (sizeof(timeout->tv_sec) == 8) {
  7000. /* _time64 function on 32bit arch */
  7001. return get_errno(safe_futex_time64(uaddr, op, val, timeout, uaddr2,
  7002. val3));
  7003. }
  7004. #endif
  7005. #if defined(__NR_futex)
  7006. /* old function on 32bit arch */
  7007. return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3));
  7008. #endif
  7009. #endif /* HOST_LONG_BITS == 64 */
  7010. return -TARGET_ENOSYS;
  7011. }
  7012. /* ??? Using host futex calls even when target atomic operations
  7013. are not really atomic probably breaks things. However implementing
  7014. futexes locally would make futexes shared between multiple processes
  7015. tricky. However they're probably useless because guest atomic
  7016. operations won't work either. */
  7017. #if defined(TARGET_NR_futex) || defined(TARGET_NR_futex_time64)
  7018. static int do_futex(CPUState *cpu, bool time64, target_ulong uaddr,
  7019. int op, int val, target_ulong timeout,
  7020. target_ulong uaddr2, int val3)
  7021. {
  7022. struct timespec ts, *pts = NULL;
  7023. void *haddr2 = NULL;
  7024. int base_op;
  7025. /* We assume FUTEX_* constants are the same on both host and target. */
  7026. #ifdef FUTEX_CMD_MASK
  7027. base_op = op & FUTEX_CMD_MASK;
  7028. #else
  7029. base_op = op;
  7030. #endif
  7031. switch (base_op) {
  7032. case FUTEX_WAIT:
  7033. case FUTEX_WAIT_BITSET:
  7034. val = tswap32(val);
  7035. break;
  7036. case FUTEX_WAIT_REQUEUE_PI:
  7037. val = tswap32(val);
  7038. haddr2 = g2h(cpu, uaddr2);
  7039. break;
  7040. case FUTEX_LOCK_PI:
  7041. case FUTEX_LOCK_PI2:
  7042. break;
  7043. case FUTEX_WAKE:
  7044. case FUTEX_WAKE_BITSET:
  7045. case FUTEX_TRYLOCK_PI:
  7046. case FUTEX_UNLOCK_PI:
  7047. timeout = 0;
  7048. break;
  7049. case FUTEX_FD:
  7050. val = target_to_host_signal(val);
  7051. timeout = 0;
  7052. break;
  7053. case FUTEX_CMP_REQUEUE:
  7054. case FUTEX_CMP_REQUEUE_PI:
  7055. val3 = tswap32(val3);
  7056. /* fall through */
  7057. case FUTEX_REQUEUE:
  7058. case FUTEX_WAKE_OP:
  7059. /*
  7060. * For these, the 4th argument is not TIMEOUT, but VAL2.
  7061. * But the prototype of do_safe_futex takes a pointer, so
  7062. * insert casts to satisfy the compiler. We do not need
  7063. * to tswap VAL2 since it's not compared to guest memory.
  7064. */
  7065. pts = (struct timespec *)(uintptr_t)timeout;
  7066. timeout = 0;
  7067. haddr2 = g2h(cpu, uaddr2);
  7068. break;
  7069. default:
  7070. return -TARGET_ENOSYS;
  7071. }
  7072. if (timeout) {
  7073. pts = &ts;
  7074. if (time64
  7075. ? target_to_host_timespec64(pts, timeout)
  7076. : target_to_host_timespec(pts, timeout)) {
  7077. return -TARGET_EFAULT;
  7078. }
  7079. }
  7080. return do_safe_futex(g2h(cpu, uaddr), op, val, pts, haddr2, val3);
  7081. }
  7082. #endif
  7083. #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  7084. static abi_long do_name_to_handle_at(abi_long dirfd, abi_long pathname,
  7085. abi_long handle, abi_long mount_id,
  7086. abi_long flags)
  7087. {
  7088. struct file_handle *target_fh;
  7089. struct file_handle *fh;
  7090. int mid = 0;
  7091. abi_long ret;
  7092. char *name;
  7093. unsigned int size, total_size;
  7094. if (get_user_s32(size, handle)) {
  7095. return -TARGET_EFAULT;
  7096. }
  7097. name = lock_user_string(pathname);
  7098. if (!name) {
  7099. return -TARGET_EFAULT;
  7100. }
  7101. total_size = sizeof(struct file_handle) + size;
  7102. target_fh = lock_user(VERIFY_WRITE, handle, total_size, 0);
  7103. if (!target_fh) {
  7104. unlock_user(name, pathname, 0);
  7105. return -TARGET_EFAULT;
  7106. }
  7107. fh = g_malloc0(total_size);
  7108. fh->handle_bytes = size;
  7109. ret = get_errno(name_to_handle_at(dirfd, path(name), fh, &mid, flags));
  7110. unlock_user(name, pathname, 0);
  7111. /* man name_to_handle_at(2):
  7112. * Other than the use of the handle_bytes field, the caller should treat
  7113. * the file_handle structure as an opaque data type
  7114. */
  7115. memcpy(target_fh, fh, total_size);
  7116. target_fh->handle_bytes = tswap32(fh->handle_bytes);
  7117. target_fh->handle_type = tswap32(fh->handle_type);
  7118. g_free(fh);
  7119. unlock_user(target_fh, handle, total_size);
  7120. if (put_user_s32(mid, mount_id)) {
  7121. return -TARGET_EFAULT;
  7122. }
  7123. return ret;
  7124. }
  7125. #endif
  7126. #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  7127. static abi_long do_open_by_handle_at(abi_long mount_fd, abi_long handle,
  7128. abi_long flags)
  7129. {
  7130. struct file_handle *target_fh;
  7131. struct file_handle *fh;
  7132. unsigned int size, total_size;
  7133. abi_long ret;
  7134. if (get_user_s32(size, handle)) {
  7135. return -TARGET_EFAULT;
  7136. }
  7137. total_size = sizeof(struct file_handle) + size;
  7138. target_fh = lock_user(VERIFY_READ, handle, total_size, 1);
  7139. if (!target_fh) {
  7140. return -TARGET_EFAULT;
  7141. }
  7142. fh = g_memdup(target_fh, total_size);
  7143. fh->handle_bytes = size;
  7144. fh->handle_type = tswap32(target_fh->handle_type);
  7145. ret = get_errno(open_by_handle_at(mount_fd, fh,
  7146. target_to_host_bitmask(flags, fcntl_flags_tbl)));
  7147. g_free(fh);
  7148. unlock_user(target_fh, handle, total_size);
  7149. return ret;
  7150. }
  7151. #endif
  7152. #if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4)
  7153. static abi_long do_signalfd4(int fd, abi_long mask, int flags)
  7154. {
  7155. int host_flags;
  7156. target_sigset_t *target_mask;
  7157. sigset_t host_mask;
  7158. abi_long ret;
  7159. if (flags & ~(TARGET_O_NONBLOCK_MASK | TARGET_O_CLOEXEC)) {
  7160. return -TARGET_EINVAL;
  7161. }
  7162. if (!lock_user_struct(VERIFY_READ, target_mask, mask, 1)) {
  7163. return -TARGET_EFAULT;
  7164. }
  7165. target_to_host_sigset(&host_mask, target_mask);
  7166. host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
  7167. ret = get_errno(signalfd(fd, &host_mask, host_flags));
  7168. if (ret >= 0) {
  7169. fd_trans_register(ret, &target_signalfd_trans);
  7170. }
  7171. unlock_user_struct(target_mask, mask, 0);
  7172. return ret;
  7173. }
  7174. #endif
  7175. /* Map host to target signal numbers for the wait family of syscalls.
  7176. Assume all other status bits are the same. */
  7177. int host_to_target_waitstatus(int status)
  7178. {
  7179. if (WIFSIGNALED(status)) {
  7180. return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
  7181. }
  7182. if (WIFSTOPPED(status)) {
  7183. return (host_to_target_signal(WSTOPSIG(status)) << 8)
  7184. | (status & 0xff);
  7185. }
  7186. return status;
  7187. }
  7188. static int open_self_cmdline(CPUArchState *cpu_env, int fd)
  7189. {
  7190. CPUState *cpu = env_cpu(cpu_env);
  7191. struct linux_binprm *bprm = ((TaskState *)cpu->opaque)->bprm;
  7192. int i;
  7193. for (i = 0; i < bprm->argc; i++) {
  7194. size_t len = strlen(bprm->argv[i]) + 1;
  7195. if (write(fd, bprm->argv[i], len) != len) {
  7196. return -1;
  7197. }
  7198. }
  7199. return 0;
  7200. }
  7201. static int open_self_maps(CPUArchState *cpu_env, int fd)
  7202. {
  7203. CPUState *cpu = env_cpu(cpu_env);
  7204. TaskState *ts = cpu->opaque;
  7205. GSList *map_info = read_self_maps();
  7206. GSList *s;
  7207. int count;
  7208. for (s = map_info; s; s = g_slist_next(s)) {
  7209. MapInfo *e = (MapInfo *) s->data;
  7210. if (h2g_valid(e->start)) {
  7211. unsigned long min = e->start;
  7212. unsigned long max = e->end;
  7213. int flags = page_get_flags(h2g(min));
  7214. const char *path;
  7215. max = h2g_valid(max - 1) ?
  7216. max : (uintptr_t) g2h_untagged(GUEST_ADDR_MAX) + 1;
  7217. if (page_check_range(h2g(min), max - min, flags) == -1) {
  7218. continue;
  7219. }
  7220. #ifdef TARGET_HPPA
  7221. if (h2g(max) == ts->info->stack_limit) {
  7222. #else
  7223. if (h2g(min) == ts->info->stack_limit) {
  7224. #endif
  7225. path = "[stack]";
  7226. } else {
  7227. path = e->path;
  7228. }
  7229. count = dprintf(fd, TARGET_ABI_FMT_ptr "-" TARGET_ABI_FMT_ptr
  7230. " %c%c%c%c %08" PRIx64 " %s %"PRId64,
  7231. h2g(min), h2g(max - 1) + 1,
  7232. (flags & PAGE_READ) ? 'r' : '-',
  7233. (flags & PAGE_WRITE_ORG) ? 'w' : '-',
  7234. (flags & PAGE_EXEC) ? 'x' : '-',
  7235. e->is_priv ? 'p' : 's',
  7236. (uint64_t) e->offset, e->dev, e->inode);
  7237. if (path) {
  7238. dprintf(fd, "%*s%s\n", 73 - count, "", path);
  7239. } else {
  7240. dprintf(fd, "\n");
  7241. }
  7242. }
  7243. }
  7244. free_self_maps(map_info);
  7245. #ifdef TARGET_VSYSCALL_PAGE
  7246. /*
  7247. * We only support execution from the vsyscall page.
  7248. * This is as if CONFIG_LEGACY_VSYSCALL_XONLY=y from v5.3.
  7249. */
  7250. count = dprintf(fd, TARGET_FMT_lx "-" TARGET_FMT_lx
  7251. " --xp 00000000 00:00 0",
  7252. TARGET_VSYSCALL_PAGE, TARGET_VSYSCALL_PAGE + TARGET_PAGE_SIZE);
  7253. dprintf(fd, "%*s%s\n", 73 - count, "", "[vsyscall]");
  7254. #endif
  7255. return 0;
  7256. }
  7257. static int open_self_stat(CPUArchState *cpu_env, int fd)
  7258. {
  7259. CPUState *cpu = env_cpu(cpu_env);
  7260. TaskState *ts = cpu->opaque;
  7261. g_autoptr(GString) buf = g_string_new(NULL);
  7262. int i;
  7263. for (i = 0; i < 44; i++) {
  7264. if (i == 0) {
  7265. /* pid */
  7266. g_string_printf(buf, FMT_pid " ", getpid());
  7267. } else if (i == 1) {
  7268. /* app name */
  7269. gchar *bin = g_strrstr(ts->bprm->argv[0], "/");
  7270. bin = bin ? bin + 1 : ts->bprm->argv[0];
  7271. g_string_printf(buf, "(%.15s) ", bin);
  7272. } else if (i == 3) {
  7273. /* ppid */
  7274. g_string_printf(buf, FMT_pid " ", getppid());
  7275. } else if (i == 21) {
  7276. /* starttime */
  7277. g_string_printf(buf, "%" PRIu64 " ", ts->start_boottime);
  7278. } else if (i == 27) {
  7279. /* stack bottom */
  7280. g_string_printf(buf, TARGET_ABI_FMT_ld " ", ts->info->start_stack);
  7281. } else {
  7282. /* for the rest, there is MasterCard */
  7283. g_string_printf(buf, "0%c", i == 43 ? '\n' : ' ');
  7284. }
  7285. if (write(fd, buf->str, buf->len) != buf->len) {
  7286. return -1;
  7287. }
  7288. }
  7289. return 0;
  7290. }
  7291. static int open_self_auxv(CPUArchState *cpu_env, int fd)
  7292. {
  7293. CPUState *cpu = env_cpu(cpu_env);
  7294. TaskState *ts = cpu->opaque;
  7295. abi_ulong auxv = ts->info->saved_auxv;
  7296. abi_ulong len = ts->info->auxv_len;
  7297. char *ptr;
  7298. /*
  7299. * Auxiliary vector is stored in target process stack.
  7300. * read in whole auxv vector and copy it to file
  7301. */
  7302. ptr = lock_user(VERIFY_READ, auxv, len, 0);
  7303. if (ptr != NULL) {
  7304. while (len > 0) {
  7305. ssize_t r;
  7306. r = write(fd, ptr, len);
  7307. if (r <= 0) {
  7308. break;
  7309. }
  7310. len -= r;
  7311. ptr += r;
  7312. }
  7313. lseek(fd, 0, SEEK_SET);
  7314. unlock_user(ptr, auxv, len);
  7315. }
  7316. return 0;
  7317. }
  7318. static int is_proc_myself(const char *filename, const char *entry)
  7319. {
  7320. if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
  7321. filename += strlen("/proc/");
  7322. if (!strncmp(filename, "self/", strlen("self/"))) {
  7323. filename += strlen("self/");
  7324. } else if (*filename >= '1' && *filename <= '9') {
  7325. char myself[80];
  7326. snprintf(myself, sizeof(myself), "%d/", getpid());
  7327. if (!strncmp(filename, myself, strlen(myself))) {
  7328. filename += strlen(myself);
  7329. } else {
  7330. return 0;
  7331. }
  7332. } else {
  7333. return 0;
  7334. }
  7335. if (!strcmp(filename, entry)) {
  7336. return 1;
  7337. }
  7338. }
  7339. return 0;
  7340. }
  7341. static void excp_dump_file(FILE *logfile, CPUArchState *env,
  7342. const char *fmt, int code)
  7343. {
  7344. if (logfile) {
  7345. CPUState *cs = env_cpu(env);
  7346. fprintf(logfile, fmt, code);
  7347. fprintf(logfile, "Failing executable: %s\n", exec_path);
  7348. cpu_dump_state(cs, logfile, 0);
  7349. open_self_maps(env, fileno(logfile));
  7350. }
  7351. }
  7352. void target_exception_dump(CPUArchState *env, const char *fmt, int code)
  7353. {
  7354. /* dump to console */
  7355. excp_dump_file(stderr, env, fmt, code);
  7356. /* dump to log file */
  7357. if (qemu_log_separate()) {
  7358. FILE *logfile = qemu_log_trylock();
  7359. excp_dump_file(logfile, env, fmt, code);
  7360. qemu_log_unlock(logfile);
  7361. }
  7362. }
  7363. #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN || \
  7364. defined(TARGET_SPARC) || defined(TARGET_M68K) || defined(TARGET_HPPA)
  7365. static int is_proc(const char *filename, const char *entry)
  7366. {
  7367. return strcmp(filename, entry) == 0;
  7368. }
  7369. #endif
  7370. #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
  7371. static int open_net_route(CPUArchState *cpu_env, int fd)
  7372. {
  7373. FILE *fp;
  7374. char *line = NULL;
  7375. size_t len = 0;
  7376. ssize_t read;
  7377. fp = fopen("/proc/net/route", "r");
  7378. if (fp == NULL) {
  7379. return -1;
  7380. }
  7381. /* read header */
  7382. read = getline(&line, &len, fp);
  7383. dprintf(fd, "%s", line);
  7384. /* read routes */
  7385. while ((read = getline(&line, &len, fp)) != -1) {
  7386. char iface[16];
  7387. uint32_t dest, gw, mask;
  7388. unsigned int flags, refcnt, use, metric, mtu, window, irtt;
  7389. int fields;
  7390. fields = sscanf(line,
  7391. "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
  7392. iface, &dest, &gw, &flags, &refcnt, &use, &metric,
  7393. &mask, &mtu, &window, &irtt);
  7394. if (fields != 11) {
  7395. continue;
  7396. }
  7397. dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
  7398. iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
  7399. metric, tswap32(mask), mtu, window, irtt);
  7400. }
  7401. free(line);
  7402. fclose(fp);
  7403. return 0;
  7404. }
  7405. #endif
  7406. #if defined(TARGET_SPARC)
  7407. static int open_cpuinfo(CPUArchState *cpu_env, int fd)
  7408. {
  7409. dprintf(fd, "type\t\t: sun4u\n");
  7410. return 0;
  7411. }
  7412. #endif
  7413. #if defined(TARGET_HPPA)
  7414. static int open_cpuinfo(CPUArchState *cpu_env, int fd)
  7415. {
  7416. int i, num_cpus;
  7417. num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
  7418. for (i = 0; i < num_cpus; i++) {
  7419. dprintf(fd, "processor\t: %d\n", i);
  7420. dprintf(fd, "cpu family\t: PA-RISC 1.1e\n");
  7421. dprintf(fd, "cpu\t\t: PA7300LC (PCX-L2)\n");
  7422. dprintf(fd, "capabilities\t: os32\n");
  7423. dprintf(fd, "model\t\t: 9000/778/B160L - "
  7424. "Merlin L2 160 QEMU (9000/778/B160L)\n\n");
  7425. }
  7426. return 0;
  7427. }
  7428. #endif
  7429. #if defined(TARGET_M68K)
  7430. static int open_hardware(CPUArchState *cpu_env, int fd)
  7431. {
  7432. dprintf(fd, "Model:\t\tqemu-m68k\n");
  7433. return 0;
  7434. }
  7435. #endif
  7436. static int do_openat(CPUArchState *cpu_env, int dirfd, const char *pathname, int flags, mode_t mode)
  7437. {
  7438. struct fake_open {
  7439. const char *filename;
  7440. int (*fill)(CPUArchState *cpu_env, int fd);
  7441. int (*cmp)(const char *s1, const char *s2);
  7442. };
  7443. const struct fake_open *fake_open;
  7444. static const struct fake_open fakes[] = {
  7445. { "maps", open_self_maps, is_proc_myself },
  7446. { "stat", open_self_stat, is_proc_myself },
  7447. { "auxv", open_self_auxv, is_proc_myself },
  7448. { "cmdline", open_self_cmdline, is_proc_myself },
  7449. #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
  7450. { "/proc/net/route", open_net_route, is_proc },
  7451. #endif
  7452. #if defined(TARGET_SPARC) || defined(TARGET_HPPA)
  7453. { "/proc/cpuinfo", open_cpuinfo, is_proc },
  7454. #endif
  7455. #if defined(TARGET_M68K)
  7456. { "/proc/hardware", open_hardware, is_proc },
  7457. #endif
  7458. { NULL, NULL, NULL }
  7459. };
  7460. if (is_proc_myself(pathname, "exe")) {
  7461. return safe_openat(dirfd, exec_path, flags, mode);
  7462. }
  7463. for (fake_open = fakes; fake_open->filename; fake_open++) {
  7464. if (fake_open->cmp(pathname, fake_open->filename)) {
  7465. break;
  7466. }
  7467. }
  7468. if (fake_open->filename) {
  7469. const char *tmpdir;
  7470. char filename[PATH_MAX];
  7471. int fd, r;
  7472. fd = memfd_create("qemu-open", 0);
  7473. if (fd < 0) {
  7474. if (errno != ENOSYS) {
  7475. return fd;
  7476. }
  7477. /* create temporary file to map stat to */
  7478. tmpdir = getenv("TMPDIR");
  7479. if (!tmpdir)
  7480. tmpdir = "/tmp";
  7481. snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
  7482. fd = mkstemp(filename);
  7483. if (fd < 0) {
  7484. return fd;
  7485. }
  7486. unlink(filename);
  7487. }
  7488. if ((r = fake_open->fill(cpu_env, fd))) {
  7489. int e = errno;
  7490. close(fd);
  7491. errno = e;
  7492. return r;
  7493. }
  7494. lseek(fd, 0, SEEK_SET);
  7495. return fd;
  7496. }
  7497. return safe_openat(dirfd, path(pathname), flags, mode);
  7498. }
  7499. static int do_execveat(CPUArchState *cpu_env, int dirfd,
  7500. abi_long pathname, abi_long guest_argp,
  7501. abi_long guest_envp, int flags)
  7502. {
  7503. int ret;
  7504. char **argp, **envp;
  7505. int argc, envc;
  7506. abi_ulong gp;
  7507. abi_ulong addr;
  7508. char **q;
  7509. void *p;
  7510. argc = 0;
  7511. for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
  7512. if (get_user_ual(addr, gp)) {
  7513. return -TARGET_EFAULT;
  7514. }
  7515. if (!addr) {
  7516. break;
  7517. }
  7518. argc++;
  7519. }
  7520. envc = 0;
  7521. for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
  7522. if (get_user_ual(addr, gp)) {
  7523. return -TARGET_EFAULT;
  7524. }
  7525. if (!addr) {
  7526. break;
  7527. }
  7528. envc++;
  7529. }
  7530. argp = g_new0(char *, argc + 1);
  7531. envp = g_new0(char *, envc + 1);
  7532. for (gp = guest_argp, q = argp; gp; gp += sizeof(abi_ulong), q++) {
  7533. if (get_user_ual(addr, gp)) {
  7534. goto execve_efault;
  7535. }
  7536. if (!addr) {
  7537. break;
  7538. }
  7539. *q = lock_user_string(addr);
  7540. if (!*q) {
  7541. goto execve_efault;
  7542. }
  7543. }
  7544. *q = NULL;
  7545. for (gp = guest_envp, q = envp; gp; gp += sizeof(abi_ulong), q++) {
  7546. if (get_user_ual(addr, gp)) {
  7547. goto execve_efault;
  7548. }
  7549. if (!addr) {
  7550. break;
  7551. }
  7552. *q = lock_user_string(addr);
  7553. if (!*q) {
  7554. goto execve_efault;
  7555. }
  7556. }
  7557. *q = NULL;
  7558. /*
  7559. * Although execve() is not an interruptible syscall it is
  7560. * a special case where we must use the safe_syscall wrapper:
  7561. * if we allow a signal to happen before we make the host
  7562. * syscall then we will 'lose' it, because at the point of
  7563. * execve the process leaves QEMU's control. So we use the
  7564. * safe syscall wrapper to ensure that we either take the
  7565. * signal as a guest signal, or else it does not happen
  7566. * before the execve completes and makes it the other
  7567. * program's problem.
  7568. */
  7569. p = lock_user_string(pathname);
  7570. if (!p) {
  7571. goto execve_efault;
  7572. }
  7573. if (is_proc_myself(p, "exe")) {
  7574. ret = get_errno(safe_execveat(dirfd, exec_path, argp, envp, flags));
  7575. } else {
  7576. ret = get_errno(safe_execveat(dirfd, p, argp, envp, flags));
  7577. }
  7578. unlock_user(p, pathname, 0);
  7579. goto execve_end;
  7580. execve_efault:
  7581. ret = -TARGET_EFAULT;
  7582. execve_end:
  7583. for (gp = guest_argp, q = argp; *q; gp += sizeof(abi_ulong), q++) {
  7584. if (get_user_ual(addr, gp) || !addr) {
  7585. break;
  7586. }
  7587. unlock_user(*q, addr, 0);
  7588. }
  7589. for (gp = guest_envp, q = envp; *q; gp += sizeof(abi_ulong), q++) {
  7590. if (get_user_ual(addr, gp) || !addr) {
  7591. break;
  7592. }
  7593. unlock_user(*q, addr, 0);
  7594. }
  7595. g_free(argp);
  7596. g_free(envp);
  7597. return ret;
  7598. }
  7599. #define TIMER_MAGIC 0x0caf0000
  7600. #define TIMER_MAGIC_MASK 0xffff0000
  7601. /* Convert QEMU provided timer ID back to internal 16bit index format */
  7602. static target_timer_t get_timer_id(abi_long arg)
  7603. {
  7604. target_timer_t timerid = arg;
  7605. if ((timerid & TIMER_MAGIC_MASK) != TIMER_MAGIC) {
  7606. return -TARGET_EINVAL;
  7607. }
  7608. timerid &= 0xffff;
  7609. if (timerid >= ARRAY_SIZE(g_posix_timers)) {
  7610. return -TARGET_EINVAL;
  7611. }
  7612. return timerid;
  7613. }
  7614. static int target_to_host_cpu_mask(unsigned long *host_mask,
  7615. size_t host_size,
  7616. abi_ulong target_addr,
  7617. size_t target_size)
  7618. {
  7619. unsigned target_bits = sizeof(abi_ulong) * 8;
  7620. unsigned host_bits = sizeof(*host_mask) * 8;
  7621. abi_ulong *target_mask;
  7622. unsigned i, j;
  7623. assert(host_size >= target_size);
  7624. target_mask = lock_user(VERIFY_READ, target_addr, target_size, 1);
  7625. if (!target_mask) {
  7626. return -TARGET_EFAULT;
  7627. }
  7628. memset(host_mask, 0, host_size);
  7629. for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
  7630. unsigned bit = i * target_bits;
  7631. abi_ulong val;
  7632. __get_user(val, &target_mask[i]);
  7633. for (j = 0; j < target_bits; j++, bit++) {
  7634. if (val & (1UL << j)) {
  7635. host_mask[bit / host_bits] |= 1UL << (bit % host_bits);
  7636. }
  7637. }
  7638. }
  7639. unlock_user(target_mask, target_addr, 0);
  7640. return 0;
  7641. }
  7642. static int host_to_target_cpu_mask(const unsigned long *host_mask,
  7643. size_t host_size,
  7644. abi_ulong target_addr,
  7645. size_t target_size)
  7646. {
  7647. unsigned target_bits = sizeof(abi_ulong) * 8;
  7648. unsigned host_bits = sizeof(*host_mask) * 8;
  7649. abi_ulong *target_mask;
  7650. unsigned i, j;
  7651. assert(host_size >= target_size);
  7652. target_mask = lock_user(VERIFY_WRITE, target_addr, target_size, 0);
  7653. if (!target_mask) {
  7654. return -TARGET_EFAULT;
  7655. }
  7656. for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
  7657. unsigned bit = i * target_bits;
  7658. abi_ulong val = 0;
  7659. for (j = 0; j < target_bits; j++, bit++) {
  7660. if (host_mask[bit / host_bits] & (1UL << (bit % host_bits))) {
  7661. val |= 1UL << j;
  7662. }
  7663. }
  7664. __put_user(val, &target_mask[i]);
  7665. }
  7666. unlock_user(target_mask, target_addr, target_size);
  7667. return 0;
  7668. }
  7669. #ifdef TARGET_NR_getdents
  7670. static int do_getdents(abi_long dirfd, abi_long arg2, abi_long count)
  7671. {
  7672. g_autofree void *hdirp = NULL;
  7673. void *tdirp;
  7674. int hlen, hoff, toff;
  7675. int hreclen, treclen;
  7676. off64_t prev_diroff = 0;
  7677. hdirp = g_try_malloc(count);
  7678. if (!hdirp) {
  7679. return -TARGET_ENOMEM;
  7680. }
  7681. #ifdef EMULATE_GETDENTS_WITH_GETDENTS
  7682. hlen = sys_getdents(dirfd, hdirp, count);
  7683. #else
  7684. hlen = sys_getdents64(dirfd, hdirp, count);
  7685. #endif
  7686. hlen = get_errno(hlen);
  7687. if (is_error(hlen)) {
  7688. return hlen;
  7689. }
  7690. tdirp = lock_user(VERIFY_WRITE, arg2, count, 0);
  7691. if (!tdirp) {
  7692. return -TARGET_EFAULT;
  7693. }
  7694. for (hoff = toff = 0; hoff < hlen; hoff += hreclen, toff += treclen) {
  7695. #ifdef EMULATE_GETDENTS_WITH_GETDENTS
  7696. struct linux_dirent *hde = hdirp + hoff;
  7697. #else
  7698. struct linux_dirent64 *hde = hdirp + hoff;
  7699. #endif
  7700. struct target_dirent *tde = tdirp + toff;
  7701. int namelen;
  7702. uint8_t type;
  7703. namelen = strlen(hde->d_name);
  7704. hreclen = hde->d_reclen;
  7705. treclen = offsetof(struct target_dirent, d_name) + namelen + 2;
  7706. treclen = QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent));
  7707. if (toff + treclen > count) {
  7708. /*
  7709. * If the host struct is smaller than the target struct, or
  7710. * requires less alignment and thus packs into less space,
  7711. * then the host can return more entries than we can pass
  7712. * on to the guest.
  7713. */
  7714. if (toff == 0) {
  7715. toff = -TARGET_EINVAL; /* result buffer is too small */
  7716. break;
  7717. }
  7718. /*
  7719. * Return what we have, resetting the file pointer to the
  7720. * location of the first record not returned.
  7721. */
  7722. lseek64(dirfd, prev_diroff, SEEK_SET);
  7723. break;
  7724. }
  7725. prev_diroff = hde->d_off;
  7726. tde->d_ino = tswapal(hde->d_ino);
  7727. tde->d_off = tswapal(hde->d_off);
  7728. tde->d_reclen = tswap16(treclen);
  7729. memcpy(tde->d_name, hde->d_name, namelen + 1);
  7730. /*
  7731. * The getdents type is in what was formerly a padding byte at the
  7732. * end of the structure.
  7733. */
  7734. #ifdef EMULATE_GETDENTS_WITH_GETDENTS
  7735. type = *((uint8_t *)hde + hreclen - 1);
  7736. #else
  7737. type = hde->d_type;
  7738. #endif
  7739. *((uint8_t *)tde + treclen - 1) = type;
  7740. }
  7741. unlock_user(tdirp, arg2, toff);
  7742. return toff;
  7743. }
  7744. #endif /* TARGET_NR_getdents */
  7745. #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
  7746. static int do_getdents64(abi_long dirfd, abi_long arg2, abi_long count)
  7747. {
  7748. g_autofree void *hdirp = NULL;
  7749. void *tdirp;
  7750. int hlen, hoff, toff;
  7751. int hreclen, treclen;
  7752. off64_t prev_diroff = 0;
  7753. hdirp = g_try_malloc(count);
  7754. if (!hdirp) {
  7755. return -TARGET_ENOMEM;
  7756. }
  7757. hlen = get_errno(sys_getdents64(dirfd, hdirp, count));
  7758. if (is_error(hlen)) {
  7759. return hlen;
  7760. }
  7761. tdirp = lock_user(VERIFY_WRITE, arg2, count, 0);
  7762. if (!tdirp) {
  7763. return -TARGET_EFAULT;
  7764. }
  7765. for (hoff = toff = 0; hoff < hlen; hoff += hreclen, toff += treclen) {
  7766. struct linux_dirent64 *hde = hdirp + hoff;
  7767. struct target_dirent64 *tde = tdirp + toff;
  7768. int namelen;
  7769. namelen = strlen(hde->d_name) + 1;
  7770. hreclen = hde->d_reclen;
  7771. treclen = offsetof(struct target_dirent64, d_name) + namelen;
  7772. treclen = QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent64));
  7773. if (toff + treclen > count) {
  7774. /*
  7775. * If the host struct is smaller than the target struct, or
  7776. * requires less alignment and thus packs into less space,
  7777. * then the host can return more entries than we can pass
  7778. * on to the guest.
  7779. */
  7780. if (toff == 0) {
  7781. toff = -TARGET_EINVAL; /* result buffer is too small */
  7782. break;
  7783. }
  7784. /*
  7785. * Return what we have, resetting the file pointer to the
  7786. * location of the first record not returned.
  7787. */
  7788. lseek64(dirfd, prev_diroff, SEEK_SET);
  7789. break;
  7790. }
  7791. prev_diroff = hde->d_off;
  7792. tde->d_ino = tswap64(hde->d_ino);
  7793. tde->d_off = tswap64(hde->d_off);
  7794. tde->d_reclen = tswap16(treclen);
  7795. tde->d_type = hde->d_type;
  7796. memcpy(tde->d_name, hde->d_name, namelen);
  7797. }
  7798. unlock_user(tdirp, arg2, toff);
  7799. return toff;
  7800. }
  7801. #endif /* TARGET_NR_getdents64 */
  7802. #if defined(TARGET_NR_pivot_root) && defined(__NR_pivot_root)
  7803. _syscall2(int, pivot_root, const char *, new_root, const char *, put_old)
  7804. #endif
  7805. /* This is an internal helper for do_syscall so that it is easier
  7806. * to have a single return point, so that actions, such as logging
  7807. * of syscall results, can be performed.
  7808. * All errnos that do_syscall() returns must be -TARGET_<errcode>.
  7809. */
  7810. static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
  7811. abi_long arg2, abi_long arg3, abi_long arg4,
  7812. abi_long arg5, abi_long arg6, abi_long arg7,
  7813. abi_long arg8)
  7814. {
  7815. CPUState *cpu = env_cpu(cpu_env);
  7816. abi_long ret;
  7817. #if defined(TARGET_NR_stat) || defined(TARGET_NR_stat64) \
  7818. || defined(TARGET_NR_lstat) || defined(TARGET_NR_lstat64) \
  7819. || defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64) \
  7820. || defined(TARGET_NR_statx)
  7821. struct stat st;
  7822. #endif
  7823. #if defined(TARGET_NR_statfs) || defined(TARGET_NR_statfs64) \
  7824. || defined(TARGET_NR_fstatfs)
  7825. struct statfs stfs;
  7826. #endif
  7827. void *p;
  7828. switch(num) {
  7829. case TARGET_NR_exit:
  7830. /* In old applications this may be used to implement _exit(2).
  7831. However in threaded applications it is used for thread termination,
  7832. and _exit_group is used for application termination.
  7833. Do thread termination if we have more then one thread. */
  7834. if (block_signals()) {
  7835. return -QEMU_ERESTARTSYS;
  7836. }
  7837. pthread_mutex_lock(&clone_lock);
  7838. if (CPU_NEXT(first_cpu)) {
  7839. TaskState *ts = cpu->opaque;
  7840. if (ts->child_tidptr) {
  7841. put_user_u32(0, ts->child_tidptr);
  7842. do_sys_futex(g2h(cpu, ts->child_tidptr),
  7843. FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
  7844. }
  7845. object_unparent(OBJECT(cpu));
  7846. object_unref(OBJECT(cpu));
  7847. /*
  7848. * At this point the CPU should be unrealized and removed
  7849. * from cpu lists. We can clean-up the rest of the thread
  7850. * data without the lock held.
  7851. */
  7852. pthread_mutex_unlock(&clone_lock);
  7853. thread_cpu = NULL;
  7854. g_free(ts);
  7855. rcu_unregister_thread();
  7856. pthread_exit(NULL);
  7857. }
  7858. pthread_mutex_unlock(&clone_lock);
  7859. preexit_cleanup(cpu_env, arg1);
  7860. _exit(arg1);
  7861. return 0; /* avoid warning */
  7862. case TARGET_NR_read:
  7863. if (arg2 == 0 && arg3 == 0) {
  7864. return get_errno(safe_read(arg1, 0, 0));
  7865. } else {
  7866. if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
  7867. return -TARGET_EFAULT;
  7868. ret = get_errno(safe_read(arg1, p, arg3));
  7869. if (ret >= 0 &&
  7870. fd_trans_host_to_target_data(arg1)) {
  7871. ret = fd_trans_host_to_target_data(arg1)(p, ret);
  7872. }
  7873. unlock_user(p, arg2, ret);
  7874. }
  7875. return ret;
  7876. case TARGET_NR_write:
  7877. if (arg2 == 0 && arg3 == 0) {
  7878. return get_errno(safe_write(arg1, 0, 0));
  7879. }
  7880. if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
  7881. return -TARGET_EFAULT;
  7882. if (fd_trans_target_to_host_data(arg1)) {
  7883. void *copy = g_malloc(arg3);
  7884. memcpy(copy, p, arg3);
  7885. ret = fd_trans_target_to_host_data(arg1)(copy, arg3);
  7886. if (ret >= 0) {
  7887. ret = get_errno(safe_write(arg1, copy, ret));
  7888. }
  7889. g_free(copy);
  7890. } else {
  7891. ret = get_errno(safe_write(arg1, p, arg3));
  7892. }
  7893. unlock_user(p, arg2, 0);
  7894. return ret;
  7895. #ifdef TARGET_NR_open
  7896. case TARGET_NR_open:
  7897. if (!(p = lock_user_string(arg1)))
  7898. return -TARGET_EFAULT;
  7899. ret = get_errno(do_openat(cpu_env, AT_FDCWD, p,
  7900. target_to_host_bitmask(arg2, fcntl_flags_tbl),
  7901. arg3));
  7902. fd_trans_unregister(ret);
  7903. unlock_user(p, arg1, 0);
  7904. return ret;
  7905. #endif
  7906. case TARGET_NR_openat:
  7907. if (!(p = lock_user_string(arg2)))
  7908. return -TARGET_EFAULT;
  7909. ret = get_errno(do_openat(cpu_env, arg1, p,
  7910. target_to_host_bitmask(arg3, fcntl_flags_tbl),
  7911. arg4));
  7912. fd_trans_unregister(ret);
  7913. unlock_user(p, arg2, 0);
  7914. return ret;
  7915. #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  7916. case TARGET_NR_name_to_handle_at:
  7917. ret = do_name_to_handle_at(arg1, arg2, arg3, arg4, arg5);
  7918. return ret;
  7919. #endif
  7920. #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  7921. case TARGET_NR_open_by_handle_at:
  7922. ret = do_open_by_handle_at(arg1, arg2, arg3);
  7923. fd_trans_unregister(ret);
  7924. return ret;
  7925. #endif
  7926. #if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
  7927. case TARGET_NR_pidfd_open:
  7928. return get_errno(pidfd_open(arg1, arg2));
  7929. #endif
  7930. #if defined(__NR_pidfd_send_signal) && defined(TARGET_NR_pidfd_send_signal)
  7931. case TARGET_NR_pidfd_send_signal:
  7932. {
  7933. siginfo_t uinfo, *puinfo;
  7934. if (arg3) {
  7935. p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1);
  7936. if (!p) {
  7937. return -TARGET_EFAULT;
  7938. }
  7939. target_to_host_siginfo(&uinfo, p);
  7940. unlock_user(p, arg3, 0);
  7941. puinfo = &uinfo;
  7942. } else {
  7943. puinfo = NULL;
  7944. }
  7945. ret = get_errno(pidfd_send_signal(arg1, target_to_host_signal(arg2),
  7946. puinfo, arg4));
  7947. }
  7948. return ret;
  7949. #endif
  7950. #if defined(__NR_pidfd_getfd) && defined(TARGET_NR_pidfd_getfd)
  7951. case TARGET_NR_pidfd_getfd:
  7952. return get_errno(pidfd_getfd(arg1, arg2, arg3));
  7953. #endif
  7954. case TARGET_NR_close:
  7955. fd_trans_unregister(arg1);
  7956. return get_errno(close(arg1));
  7957. #if defined(__NR_close_range) && defined(TARGET_NR_close_range)
  7958. case TARGET_NR_close_range:
  7959. ret = get_errno(sys_close_range(arg1, arg2, arg3));
  7960. if (ret == 0 && !(arg3 & CLOSE_RANGE_CLOEXEC)) {
  7961. abi_long fd, maxfd;
  7962. maxfd = MIN(arg2, target_fd_max);
  7963. for (fd = arg1; fd < maxfd; fd++) {
  7964. fd_trans_unregister(fd);
  7965. }
  7966. }
  7967. return ret;
  7968. #endif
  7969. case TARGET_NR_brk:
  7970. return do_brk(arg1);
  7971. #ifdef TARGET_NR_fork
  7972. case TARGET_NR_fork:
  7973. return get_errno(do_fork(cpu_env, TARGET_SIGCHLD, 0, 0, 0, 0));
  7974. #endif
  7975. #ifdef TARGET_NR_waitpid
  7976. case TARGET_NR_waitpid:
  7977. {
  7978. int status;
  7979. ret = get_errno(safe_wait4(arg1, &status, arg3, 0));
  7980. if (!is_error(ret) && arg2 && ret
  7981. && put_user_s32(host_to_target_waitstatus(status), arg2))
  7982. return -TARGET_EFAULT;
  7983. }
  7984. return ret;
  7985. #endif
  7986. #ifdef TARGET_NR_waitid
  7987. case TARGET_NR_waitid:
  7988. {
  7989. siginfo_t info;
  7990. info.si_pid = 0;
  7991. ret = get_errno(safe_waitid(arg1, arg2, &info, arg4, NULL));
  7992. if (!is_error(ret) && arg3 && info.si_pid != 0) {
  7993. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
  7994. return -TARGET_EFAULT;
  7995. host_to_target_siginfo(p, &info);
  7996. unlock_user(p, arg3, sizeof(target_siginfo_t));
  7997. }
  7998. }
  7999. return ret;
  8000. #endif
  8001. #ifdef TARGET_NR_creat /* not on alpha */
  8002. case TARGET_NR_creat:
  8003. if (!(p = lock_user_string(arg1)))
  8004. return -TARGET_EFAULT;
  8005. ret = get_errno(creat(p, arg2));
  8006. fd_trans_unregister(ret);
  8007. unlock_user(p, arg1, 0);
  8008. return ret;
  8009. #endif
  8010. #ifdef TARGET_NR_link
  8011. case TARGET_NR_link:
  8012. {
  8013. void * p2;
  8014. p = lock_user_string(arg1);
  8015. p2 = lock_user_string(arg2);
  8016. if (!p || !p2)
  8017. ret = -TARGET_EFAULT;
  8018. else
  8019. ret = get_errno(link(p, p2));
  8020. unlock_user(p2, arg2, 0);
  8021. unlock_user(p, arg1, 0);
  8022. }
  8023. return ret;
  8024. #endif
  8025. #if defined(TARGET_NR_linkat)
  8026. case TARGET_NR_linkat:
  8027. {
  8028. void * p2 = NULL;
  8029. if (!arg2 || !arg4)
  8030. return -TARGET_EFAULT;
  8031. p = lock_user_string(arg2);
  8032. p2 = lock_user_string(arg4);
  8033. if (!p || !p2)
  8034. ret = -TARGET_EFAULT;
  8035. else
  8036. ret = get_errno(linkat(arg1, p, arg3, p2, arg5));
  8037. unlock_user(p, arg2, 0);
  8038. unlock_user(p2, arg4, 0);
  8039. }
  8040. return ret;
  8041. #endif
  8042. #ifdef TARGET_NR_unlink
  8043. case TARGET_NR_unlink:
  8044. if (!(p = lock_user_string(arg1)))
  8045. return -TARGET_EFAULT;
  8046. ret = get_errno(unlink(p));
  8047. unlock_user(p, arg1, 0);
  8048. return ret;
  8049. #endif
  8050. #if defined(TARGET_NR_unlinkat)
  8051. case TARGET_NR_unlinkat:
  8052. if (!(p = lock_user_string(arg2)))
  8053. return -TARGET_EFAULT;
  8054. ret = get_errno(unlinkat(arg1, p, arg3));
  8055. unlock_user(p, arg2, 0);
  8056. return ret;
  8057. #endif
  8058. case TARGET_NR_execveat:
  8059. return do_execveat(cpu_env, arg1, arg2, arg3, arg4, arg5);
  8060. case TARGET_NR_execve:
  8061. return do_execveat(cpu_env, AT_FDCWD, arg1, arg2, arg3, 0);
  8062. case TARGET_NR_chdir:
  8063. if (!(p = lock_user_string(arg1)))
  8064. return -TARGET_EFAULT;
  8065. ret = get_errno(chdir(p));
  8066. unlock_user(p, arg1, 0);
  8067. return ret;
  8068. #ifdef TARGET_NR_time
  8069. case TARGET_NR_time:
  8070. {
  8071. time_t host_time;
  8072. ret = get_errno(time(&host_time));
  8073. if (!is_error(ret)
  8074. && arg1
  8075. && put_user_sal(host_time, arg1))
  8076. return -TARGET_EFAULT;
  8077. }
  8078. return ret;
  8079. #endif
  8080. #ifdef TARGET_NR_mknod
  8081. case TARGET_NR_mknod:
  8082. if (!(p = lock_user_string(arg1)))
  8083. return -TARGET_EFAULT;
  8084. ret = get_errno(mknod(p, arg2, arg3));
  8085. unlock_user(p, arg1, 0);
  8086. return ret;
  8087. #endif
  8088. #if defined(TARGET_NR_mknodat)
  8089. case TARGET_NR_mknodat:
  8090. if (!(p = lock_user_string(arg2)))
  8091. return -TARGET_EFAULT;
  8092. ret = get_errno(mknodat(arg1, p, arg3, arg4));
  8093. unlock_user(p, arg2, 0);
  8094. return ret;
  8095. #endif
  8096. #ifdef TARGET_NR_chmod
  8097. case TARGET_NR_chmod:
  8098. if (!(p = lock_user_string(arg1)))
  8099. return -TARGET_EFAULT;
  8100. ret = get_errno(chmod(p, arg2));
  8101. unlock_user(p, arg1, 0);
  8102. return ret;
  8103. #endif
  8104. #ifdef TARGET_NR_lseek
  8105. case TARGET_NR_lseek:
  8106. return get_errno(lseek(arg1, arg2, arg3));
  8107. #endif
  8108. #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
  8109. /* Alpha specific */
  8110. case TARGET_NR_getxpid:
  8111. cpu_env->ir[IR_A4] = getppid();
  8112. return get_errno(getpid());
  8113. #endif
  8114. #ifdef TARGET_NR_getpid
  8115. case TARGET_NR_getpid:
  8116. return get_errno(getpid());
  8117. #endif
  8118. case TARGET_NR_mount:
  8119. {
  8120. /* need to look at the data field */
  8121. void *p2, *p3;
  8122. if (arg1) {
  8123. p = lock_user_string(arg1);
  8124. if (!p) {
  8125. return -TARGET_EFAULT;
  8126. }
  8127. } else {
  8128. p = NULL;
  8129. }
  8130. p2 = lock_user_string(arg2);
  8131. if (!p2) {
  8132. if (arg1) {
  8133. unlock_user(p, arg1, 0);
  8134. }
  8135. return -TARGET_EFAULT;
  8136. }
  8137. if (arg3) {
  8138. p3 = lock_user_string(arg3);
  8139. if (!p3) {
  8140. if (arg1) {
  8141. unlock_user(p, arg1, 0);
  8142. }
  8143. unlock_user(p2, arg2, 0);
  8144. return -TARGET_EFAULT;
  8145. }
  8146. } else {
  8147. p3 = NULL;
  8148. }
  8149. /* FIXME - arg5 should be locked, but it isn't clear how to
  8150. * do that since it's not guaranteed to be a NULL-terminated
  8151. * string.
  8152. */
  8153. if (!arg5) {
  8154. ret = mount(p, p2, p3, (unsigned long)arg4, NULL);
  8155. } else {
  8156. ret = mount(p, p2, p3, (unsigned long)arg4, g2h(cpu, arg5));
  8157. }
  8158. ret = get_errno(ret);
  8159. if (arg1) {
  8160. unlock_user(p, arg1, 0);
  8161. }
  8162. unlock_user(p2, arg2, 0);
  8163. if (arg3) {
  8164. unlock_user(p3, arg3, 0);
  8165. }
  8166. }
  8167. return ret;
  8168. #if defined(TARGET_NR_umount) || defined(TARGET_NR_oldumount)
  8169. #if defined(TARGET_NR_umount)
  8170. case TARGET_NR_umount:
  8171. #endif
  8172. #if defined(TARGET_NR_oldumount)
  8173. case TARGET_NR_oldumount:
  8174. #endif
  8175. if (!(p = lock_user_string(arg1)))
  8176. return -TARGET_EFAULT;
  8177. ret = get_errno(umount(p));
  8178. unlock_user(p, arg1, 0);
  8179. return ret;
  8180. #endif
  8181. #ifdef TARGET_NR_stime /* not on alpha */
  8182. case TARGET_NR_stime:
  8183. {
  8184. struct timespec ts;
  8185. ts.tv_nsec = 0;
  8186. if (get_user_sal(ts.tv_sec, arg1)) {
  8187. return -TARGET_EFAULT;
  8188. }
  8189. return get_errno(clock_settime(CLOCK_REALTIME, &ts));
  8190. }
  8191. #endif
  8192. #ifdef TARGET_NR_alarm /* not on alpha */
  8193. case TARGET_NR_alarm:
  8194. return alarm(arg1);
  8195. #endif
  8196. #ifdef TARGET_NR_pause /* not on alpha */
  8197. case TARGET_NR_pause:
  8198. if (!block_signals()) {
  8199. sigsuspend(&((TaskState *)cpu->opaque)->signal_mask);
  8200. }
  8201. return -TARGET_EINTR;
  8202. #endif
  8203. #ifdef TARGET_NR_utime
  8204. case TARGET_NR_utime:
  8205. {
  8206. struct utimbuf tbuf, *host_tbuf;
  8207. struct target_utimbuf *target_tbuf;
  8208. if (arg2) {
  8209. if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
  8210. return -TARGET_EFAULT;
  8211. tbuf.actime = tswapal(target_tbuf->actime);
  8212. tbuf.modtime = tswapal(target_tbuf->modtime);
  8213. unlock_user_struct(target_tbuf, arg2, 0);
  8214. host_tbuf = &tbuf;
  8215. } else {
  8216. host_tbuf = NULL;
  8217. }
  8218. if (!(p = lock_user_string(arg1)))
  8219. return -TARGET_EFAULT;
  8220. ret = get_errno(utime(p, host_tbuf));
  8221. unlock_user(p, arg1, 0);
  8222. }
  8223. return ret;
  8224. #endif
  8225. #ifdef TARGET_NR_utimes
  8226. case TARGET_NR_utimes:
  8227. {
  8228. struct timeval *tvp, tv[2];
  8229. if (arg2) {
  8230. if (copy_from_user_timeval(&tv[0], arg2)
  8231. || copy_from_user_timeval(&tv[1],
  8232. arg2 + sizeof(struct target_timeval)))
  8233. return -TARGET_EFAULT;
  8234. tvp = tv;
  8235. } else {
  8236. tvp = NULL;
  8237. }
  8238. if (!(p = lock_user_string(arg1)))
  8239. return -TARGET_EFAULT;
  8240. ret = get_errno(utimes(p, tvp));
  8241. unlock_user(p, arg1, 0);
  8242. }
  8243. return ret;
  8244. #endif
  8245. #if defined(TARGET_NR_futimesat)
  8246. case TARGET_NR_futimesat:
  8247. {
  8248. struct timeval *tvp, tv[2];
  8249. if (arg3) {
  8250. if (copy_from_user_timeval(&tv[0], arg3)
  8251. || copy_from_user_timeval(&tv[1],
  8252. arg3 + sizeof(struct target_timeval)))
  8253. return -TARGET_EFAULT;
  8254. tvp = tv;
  8255. } else {
  8256. tvp = NULL;
  8257. }
  8258. if (!(p = lock_user_string(arg2))) {
  8259. return -TARGET_EFAULT;
  8260. }
  8261. ret = get_errno(futimesat(arg1, path(p), tvp));
  8262. unlock_user(p, arg2, 0);
  8263. }
  8264. return ret;
  8265. #endif
  8266. #ifdef TARGET_NR_access
  8267. case TARGET_NR_access:
  8268. if (!(p = lock_user_string(arg1))) {
  8269. return -TARGET_EFAULT;
  8270. }
  8271. ret = get_errno(access(path(p), arg2));
  8272. unlock_user(p, arg1, 0);
  8273. return ret;
  8274. #endif
  8275. #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
  8276. case TARGET_NR_faccessat:
  8277. if (!(p = lock_user_string(arg2))) {
  8278. return -TARGET_EFAULT;
  8279. }
  8280. ret = get_errno(faccessat(arg1, p, arg3, 0));
  8281. unlock_user(p, arg2, 0);
  8282. return ret;
  8283. #endif
  8284. #if defined(TARGET_NR_faccessat2)
  8285. case TARGET_NR_faccessat2:
  8286. if (!(p = lock_user_string(arg2))) {
  8287. return -TARGET_EFAULT;
  8288. }
  8289. ret = get_errno(faccessat(arg1, p, arg3, arg4));
  8290. unlock_user(p, arg2, 0);
  8291. return ret;
  8292. #endif
  8293. #ifdef TARGET_NR_nice /* not on alpha */
  8294. case TARGET_NR_nice:
  8295. return get_errno(nice(arg1));
  8296. #endif
  8297. case TARGET_NR_sync:
  8298. sync();
  8299. return 0;
  8300. #if defined(TARGET_NR_syncfs) && defined(CONFIG_SYNCFS)
  8301. case TARGET_NR_syncfs:
  8302. return get_errno(syncfs(arg1));
  8303. #endif
  8304. case TARGET_NR_kill:
  8305. return get_errno(safe_kill(arg1, target_to_host_signal(arg2)));
  8306. #ifdef TARGET_NR_rename
  8307. case TARGET_NR_rename:
  8308. {
  8309. void *p2;
  8310. p = lock_user_string(arg1);
  8311. p2 = lock_user_string(arg2);
  8312. if (!p || !p2)
  8313. ret = -TARGET_EFAULT;
  8314. else
  8315. ret = get_errno(rename(p, p2));
  8316. unlock_user(p2, arg2, 0);
  8317. unlock_user(p, arg1, 0);
  8318. }
  8319. return ret;
  8320. #endif
  8321. #if defined(TARGET_NR_renameat)
  8322. case TARGET_NR_renameat:
  8323. {
  8324. void *p2;
  8325. p = lock_user_string(arg2);
  8326. p2 = lock_user_string(arg4);
  8327. if (!p || !p2)
  8328. ret = -TARGET_EFAULT;
  8329. else
  8330. ret = get_errno(renameat(arg1, p, arg3, p2));
  8331. unlock_user(p2, arg4, 0);
  8332. unlock_user(p, arg2, 0);
  8333. }
  8334. return ret;
  8335. #endif
  8336. #if defined(TARGET_NR_renameat2)
  8337. case TARGET_NR_renameat2:
  8338. {
  8339. void *p2;
  8340. p = lock_user_string(arg2);
  8341. p2 = lock_user_string(arg4);
  8342. if (!p || !p2) {
  8343. ret = -TARGET_EFAULT;
  8344. } else {
  8345. ret = get_errno(sys_renameat2(arg1, p, arg3, p2, arg5));
  8346. }
  8347. unlock_user(p2, arg4, 0);
  8348. unlock_user(p, arg2, 0);
  8349. }
  8350. return ret;
  8351. #endif
  8352. #ifdef TARGET_NR_mkdir
  8353. case TARGET_NR_mkdir:
  8354. if (!(p = lock_user_string(arg1)))
  8355. return -TARGET_EFAULT;
  8356. ret = get_errno(mkdir(p, arg2));
  8357. unlock_user(p, arg1, 0);
  8358. return ret;
  8359. #endif
  8360. #if defined(TARGET_NR_mkdirat)
  8361. case TARGET_NR_mkdirat:
  8362. if (!(p = lock_user_string(arg2)))
  8363. return -TARGET_EFAULT;
  8364. ret = get_errno(mkdirat(arg1, p, arg3));
  8365. unlock_user(p, arg2, 0);
  8366. return ret;
  8367. #endif
  8368. #ifdef TARGET_NR_rmdir
  8369. case TARGET_NR_rmdir:
  8370. if (!(p = lock_user_string(arg1)))
  8371. return -TARGET_EFAULT;
  8372. ret = get_errno(rmdir(p));
  8373. unlock_user(p, arg1, 0);
  8374. return ret;
  8375. #endif
  8376. case TARGET_NR_dup:
  8377. ret = get_errno(dup(arg1));
  8378. if (ret >= 0) {
  8379. fd_trans_dup(arg1, ret);
  8380. }
  8381. return ret;
  8382. #ifdef TARGET_NR_pipe
  8383. case TARGET_NR_pipe:
  8384. return do_pipe(cpu_env, arg1, 0, 0);
  8385. #endif
  8386. #ifdef TARGET_NR_pipe2
  8387. case TARGET_NR_pipe2:
  8388. return do_pipe(cpu_env, arg1,
  8389. target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
  8390. #endif
  8391. case TARGET_NR_times:
  8392. {
  8393. struct target_tms *tmsp;
  8394. struct tms tms;
  8395. ret = get_errno(times(&tms));
  8396. if (arg1) {
  8397. tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
  8398. if (!tmsp)
  8399. return -TARGET_EFAULT;
  8400. tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
  8401. tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
  8402. tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
  8403. tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
  8404. }
  8405. if (!is_error(ret))
  8406. ret = host_to_target_clock_t(ret);
  8407. }
  8408. return ret;
  8409. case TARGET_NR_acct:
  8410. if (arg1 == 0) {
  8411. ret = get_errno(acct(NULL));
  8412. } else {
  8413. if (!(p = lock_user_string(arg1))) {
  8414. return -TARGET_EFAULT;
  8415. }
  8416. ret = get_errno(acct(path(p)));
  8417. unlock_user(p, arg1, 0);
  8418. }
  8419. return ret;
  8420. #ifdef TARGET_NR_umount2
  8421. case TARGET_NR_umount2:
  8422. if (!(p = lock_user_string(arg1)))
  8423. return -TARGET_EFAULT;
  8424. ret = get_errno(umount2(p, arg2));
  8425. unlock_user(p, arg1, 0);
  8426. return ret;
  8427. #endif
  8428. case TARGET_NR_ioctl:
  8429. return do_ioctl(arg1, arg2, arg3);
  8430. #ifdef TARGET_NR_fcntl
  8431. case TARGET_NR_fcntl:
  8432. return do_fcntl(arg1, arg2, arg3);
  8433. #endif
  8434. case TARGET_NR_setpgid:
  8435. return get_errno(setpgid(arg1, arg2));
  8436. case TARGET_NR_umask:
  8437. return get_errno(umask(arg1));
  8438. case TARGET_NR_chroot:
  8439. if (!(p = lock_user_string(arg1)))
  8440. return -TARGET_EFAULT;
  8441. ret = get_errno(chroot(p));
  8442. unlock_user(p, arg1, 0);
  8443. return ret;
  8444. #ifdef TARGET_NR_dup2
  8445. case TARGET_NR_dup2:
  8446. ret = get_errno(dup2(arg1, arg2));
  8447. if (ret >= 0) {
  8448. fd_trans_dup(arg1, arg2);
  8449. }
  8450. return ret;
  8451. #endif
  8452. #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
  8453. case TARGET_NR_dup3:
  8454. {
  8455. int host_flags;
  8456. if ((arg3 & ~TARGET_O_CLOEXEC) != 0) {
  8457. return -EINVAL;
  8458. }
  8459. host_flags = target_to_host_bitmask(arg3, fcntl_flags_tbl);
  8460. ret = get_errno(dup3(arg1, arg2, host_flags));
  8461. if (ret >= 0) {
  8462. fd_trans_dup(arg1, arg2);
  8463. }
  8464. return ret;
  8465. }
  8466. #endif
  8467. #ifdef TARGET_NR_getppid /* not on alpha */
  8468. case TARGET_NR_getppid:
  8469. return get_errno(getppid());
  8470. #endif
  8471. #ifdef TARGET_NR_getpgrp
  8472. case TARGET_NR_getpgrp:
  8473. return get_errno(getpgrp());
  8474. #endif
  8475. case TARGET_NR_setsid:
  8476. return get_errno(setsid());
  8477. #ifdef TARGET_NR_sigaction
  8478. case TARGET_NR_sigaction:
  8479. {
  8480. #if defined(TARGET_MIPS)
  8481. struct target_sigaction act, oact, *pact, *old_act;
  8482. if (arg2) {
  8483. if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
  8484. return -TARGET_EFAULT;
  8485. act._sa_handler = old_act->_sa_handler;
  8486. target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
  8487. act.sa_flags = old_act->sa_flags;
  8488. unlock_user_struct(old_act, arg2, 0);
  8489. pact = &act;
  8490. } else {
  8491. pact = NULL;
  8492. }
  8493. ret = get_errno(do_sigaction(arg1, pact, &oact, 0));
  8494. if (!is_error(ret) && arg3) {
  8495. if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
  8496. return -TARGET_EFAULT;
  8497. old_act->_sa_handler = oact._sa_handler;
  8498. old_act->sa_flags = oact.sa_flags;
  8499. old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
  8500. old_act->sa_mask.sig[1] = 0;
  8501. old_act->sa_mask.sig[2] = 0;
  8502. old_act->sa_mask.sig[3] = 0;
  8503. unlock_user_struct(old_act, arg3, 1);
  8504. }
  8505. #else
  8506. struct target_old_sigaction *old_act;
  8507. struct target_sigaction act, oact, *pact;
  8508. if (arg2) {
  8509. if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
  8510. return -TARGET_EFAULT;
  8511. act._sa_handler = old_act->_sa_handler;
  8512. target_siginitset(&act.sa_mask, old_act->sa_mask);
  8513. act.sa_flags = old_act->sa_flags;
  8514. #ifdef TARGET_ARCH_HAS_SA_RESTORER
  8515. act.sa_restorer = old_act->sa_restorer;
  8516. #endif
  8517. unlock_user_struct(old_act, arg2, 0);
  8518. pact = &act;
  8519. } else {
  8520. pact = NULL;
  8521. }
  8522. ret = get_errno(do_sigaction(arg1, pact, &oact, 0));
  8523. if (!is_error(ret) && arg3) {
  8524. if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
  8525. return -TARGET_EFAULT;
  8526. old_act->_sa_handler = oact._sa_handler;
  8527. old_act->sa_mask = oact.sa_mask.sig[0];
  8528. old_act->sa_flags = oact.sa_flags;
  8529. #ifdef TARGET_ARCH_HAS_SA_RESTORER
  8530. old_act->sa_restorer = oact.sa_restorer;
  8531. #endif
  8532. unlock_user_struct(old_act, arg3, 1);
  8533. }
  8534. #endif
  8535. }
  8536. return ret;
  8537. #endif
  8538. case TARGET_NR_rt_sigaction:
  8539. {
  8540. /*
  8541. * For Alpha and SPARC this is a 5 argument syscall, with
  8542. * a 'restorer' parameter which must be copied into the
  8543. * sa_restorer field of the sigaction struct.
  8544. * For Alpha that 'restorer' is arg5; for SPARC it is arg4,
  8545. * and arg5 is the sigsetsize.
  8546. */
  8547. #if defined(TARGET_ALPHA)
  8548. target_ulong sigsetsize = arg4;
  8549. target_ulong restorer = arg5;
  8550. #elif defined(TARGET_SPARC)
  8551. target_ulong restorer = arg4;
  8552. target_ulong sigsetsize = arg5;
  8553. #else
  8554. target_ulong sigsetsize = arg4;
  8555. target_ulong restorer = 0;
  8556. #endif
  8557. struct target_sigaction *act = NULL;
  8558. struct target_sigaction *oact = NULL;
  8559. if (sigsetsize != sizeof(target_sigset_t)) {
  8560. return -TARGET_EINVAL;
  8561. }
  8562. if (arg2 && !lock_user_struct(VERIFY_READ, act, arg2, 1)) {
  8563. return -TARGET_EFAULT;
  8564. }
  8565. if (arg3 && !lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
  8566. ret = -TARGET_EFAULT;
  8567. } else {
  8568. ret = get_errno(do_sigaction(arg1, act, oact, restorer));
  8569. if (oact) {
  8570. unlock_user_struct(oact, arg3, 1);
  8571. }
  8572. }
  8573. if (act) {
  8574. unlock_user_struct(act, arg2, 0);
  8575. }
  8576. }
  8577. return ret;
  8578. #ifdef TARGET_NR_sgetmask /* not on alpha */
  8579. case TARGET_NR_sgetmask:
  8580. {
  8581. sigset_t cur_set;
  8582. abi_ulong target_set;
  8583. ret = do_sigprocmask(0, NULL, &cur_set);
  8584. if (!ret) {
  8585. host_to_target_old_sigset(&target_set, &cur_set);
  8586. ret = target_set;
  8587. }
  8588. }
  8589. return ret;
  8590. #endif
  8591. #ifdef TARGET_NR_ssetmask /* not on alpha */
  8592. case TARGET_NR_ssetmask:
  8593. {
  8594. sigset_t set, oset;
  8595. abi_ulong target_set = arg1;
  8596. target_to_host_old_sigset(&set, &target_set);
  8597. ret = do_sigprocmask(SIG_SETMASK, &set, &oset);
  8598. if (!ret) {
  8599. host_to_target_old_sigset(&target_set, &oset);
  8600. ret = target_set;
  8601. }
  8602. }
  8603. return ret;
  8604. #endif
  8605. #ifdef TARGET_NR_sigprocmask
  8606. case TARGET_NR_sigprocmask:
  8607. {
  8608. #if defined(TARGET_ALPHA)
  8609. sigset_t set, oldset;
  8610. abi_ulong mask;
  8611. int how;
  8612. switch (arg1) {
  8613. case TARGET_SIG_BLOCK:
  8614. how = SIG_BLOCK;
  8615. break;
  8616. case TARGET_SIG_UNBLOCK:
  8617. how = SIG_UNBLOCK;
  8618. break;
  8619. case TARGET_SIG_SETMASK:
  8620. how = SIG_SETMASK;
  8621. break;
  8622. default:
  8623. return -TARGET_EINVAL;
  8624. }
  8625. mask = arg2;
  8626. target_to_host_old_sigset(&set, &mask);
  8627. ret = do_sigprocmask(how, &set, &oldset);
  8628. if (!is_error(ret)) {
  8629. host_to_target_old_sigset(&mask, &oldset);
  8630. ret = mask;
  8631. cpu_env->ir[IR_V0] = 0; /* force no error */
  8632. }
  8633. #else
  8634. sigset_t set, oldset, *set_ptr;
  8635. int how;
  8636. if (arg2) {
  8637. p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1);
  8638. if (!p) {
  8639. return -TARGET_EFAULT;
  8640. }
  8641. target_to_host_old_sigset(&set, p);
  8642. unlock_user(p, arg2, 0);
  8643. set_ptr = &set;
  8644. switch (arg1) {
  8645. case TARGET_SIG_BLOCK:
  8646. how = SIG_BLOCK;
  8647. break;
  8648. case TARGET_SIG_UNBLOCK:
  8649. how = SIG_UNBLOCK;
  8650. break;
  8651. case TARGET_SIG_SETMASK:
  8652. how = SIG_SETMASK;
  8653. break;
  8654. default:
  8655. return -TARGET_EINVAL;
  8656. }
  8657. } else {
  8658. how = 0;
  8659. set_ptr = NULL;
  8660. }
  8661. ret = do_sigprocmask(how, set_ptr, &oldset);
  8662. if (!is_error(ret) && arg3) {
  8663. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
  8664. return -TARGET_EFAULT;
  8665. host_to_target_old_sigset(p, &oldset);
  8666. unlock_user(p, arg3, sizeof(target_sigset_t));
  8667. }
  8668. #endif
  8669. }
  8670. return ret;
  8671. #endif
  8672. case TARGET_NR_rt_sigprocmask:
  8673. {
  8674. int how = arg1;
  8675. sigset_t set, oldset, *set_ptr;
  8676. if (arg4 != sizeof(target_sigset_t)) {
  8677. return -TARGET_EINVAL;
  8678. }
  8679. if (arg2) {
  8680. p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1);
  8681. if (!p) {
  8682. return -TARGET_EFAULT;
  8683. }
  8684. target_to_host_sigset(&set, p);
  8685. unlock_user(p, arg2, 0);
  8686. set_ptr = &set;
  8687. switch(how) {
  8688. case TARGET_SIG_BLOCK:
  8689. how = SIG_BLOCK;
  8690. break;
  8691. case TARGET_SIG_UNBLOCK:
  8692. how = SIG_UNBLOCK;
  8693. break;
  8694. case TARGET_SIG_SETMASK:
  8695. how = SIG_SETMASK;
  8696. break;
  8697. default:
  8698. return -TARGET_EINVAL;
  8699. }
  8700. } else {
  8701. how = 0;
  8702. set_ptr = NULL;
  8703. }
  8704. ret = do_sigprocmask(how, set_ptr, &oldset);
  8705. if (!is_error(ret) && arg3) {
  8706. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
  8707. return -TARGET_EFAULT;
  8708. host_to_target_sigset(p, &oldset);
  8709. unlock_user(p, arg3, sizeof(target_sigset_t));
  8710. }
  8711. }
  8712. return ret;
  8713. #ifdef TARGET_NR_sigpending
  8714. case TARGET_NR_sigpending:
  8715. {
  8716. sigset_t set;
  8717. ret = get_errno(sigpending(&set));
  8718. if (!is_error(ret)) {
  8719. if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
  8720. return -TARGET_EFAULT;
  8721. host_to_target_old_sigset(p, &set);
  8722. unlock_user(p, arg1, sizeof(target_sigset_t));
  8723. }
  8724. }
  8725. return ret;
  8726. #endif
  8727. case TARGET_NR_rt_sigpending:
  8728. {
  8729. sigset_t set;
  8730. /* Yes, this check is >, not != like most. We follow the kernel's
  8731. * logic and it does it like this because it implements
  8732. * NR_sigpending through the same code path, and in that case
  8733. * the old_sigset_t is smaller in size.
  8734. */
  8735. if (arg2 > sizeof(target_sigset_t)) {
  8736. return -TARGET_EINVAL;
  8737. }
  8738. ret = get_errno(sigpending(&set));
  8739. if (!is_error(ret)) {
  8740. if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
  8741. return -TARGET_EFAULT;
  8742. host_to_target_sigset(p, &set);
  8743. unlock_user(p, arg1, sizeof(target_sigset_t));
  8744. }
  8745. }
  8746. return ret;
  8747. #ifdef TARGET_NR_sigsuspend
  8748. case TARGET_NR_sigsuspend:
  8749. {
  8750. sigset_t *set;
  8751. #if defined(TARGET_ALPHA)
  8752. TaskState *ts = cpu->opaque;
  8753. /* target_to_host_old_sigset will bswap back */
  8754. abi_ulong mask = tswapal(arg1);
  8755. set = &ts->sigsuspend_mask;
  8756. target_to_host_old_sigset(set, &mask);
  8757. #else
  8758. ret = process_sigsuspend_mask(&set, arg1, sizeof(target_sigset_t));
  8759. if (ret != 0) {
  8760. return ret;
  8761. }
  8762. #endif
  8763. ret = get_errno(safe_rt_sigsuspend(set, SIGSET_T_SIZE));
  8764. finish_sigsuspend_mask(ret);
  8765. }
  8766. return ret;
  8767. #endif
  8768. case TARGET_NR_rt_sigsuspend:
  8769. {
  8770. sigset_t *set;
  8771. ret = process_sigsuspend_mask(&set, arg1, arg2);
  8772. if (ret != 0) {
  8773. return ret;
  8774. }
  8775. ret = get_errno(safe_rt_sigsuspend(set, SIGSET_T_SIZE));
  8776. finish_sigsuspend_mask(ret);
  8777. }
  8778. return ret;
  8779. #ifdef TARGET_NR_rt_sigtimedwait
  8780. case TARGET_NR_rt_sigtimedwait:
  8781. {
  8782. sigset_t set;
  8783. struct timespec uts, *puts;
  8784. siginfo_t uinfo;
  8785. if (arg4 != sizeof(target_sigset_t)) {
  8786. return -TARGET_EINVAL;
  8787. }
  8788. if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
  8789. return -TARGET_EFAULT;
  8790. target_to_host_sigset(&set, p);
  8791. unlock_user(p, arg1, 0);
  8792. if (arg3) {
  8793. puts = &uts;
  8794. if (target_to_host_timespec(puts, arg3)) {
  8795. return -TARGET_EFAULT;
  8796. }
  8797. } else {
  8798. puts = NULL;
  8799. }
  8800. ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts,
  8801. SIGSET_T_SIZE));
  8802. if (!is_error(ret)) {
  8803. if (arg2) {
  8804. p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t),
  8805. 0);
  8806. if (!p) {
  8807. return -TARGET_EFAULT;
  8808. }
  8809. host_to_target_siginfo(p, &uinfo);
  8810. unlock_user(p, arg2, sizeof(target_siginfo_t));
  8811. }
  8812. ret = host_to_target_signal(ret);
  8813. }
  8814. }
  8815. return ret;
  8816. #endif
  8817. #ifdef TARGET_NR_rt_sigtimedwait_time64
  8818. case TARGET_NR_rt_sigtimedwait_time64:
  8819. {
  8820. sigset_t set;
  8821. struct timespec uts, *puts;
  8822. siginfo_t uinfo;
  8823. if (arg4 != sizeof(target_sigset_t)) {
  8824. return -TARGET_EINVAL;
  8825. }
  8826. p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1);
  8827. if (!p) {
  8828. return -TARGET_EFAULT;
  8829. }
  8830. target_to_host_sigset(&set, p);
  8831. unlock_user(p, arg1, 0);
  8832. if (arg3) {
  8833. puts = &uts;
  8834. if (target_to_host_timespec64(puts, arg3)) {
  8835. return -TARGET_EFAULT;
  8836. }
  8837. } else {
  8838. puts = NULL;
  8839. }
  8840. ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts,
  8841. SIGSET_T_SIZE));
  8842. if (!is_error(ret)) {
  8843. if (arg2) {
  8844. p = lock_user(VERIFY_WRITE, arg2,
  8845. sizeof(target_siginfo_t), 0);
  8846. if (!p) {
  8847. return -TARGET_EFAULT;
  8848. }
  8849. host_to_target_siginfo(p, &uinfo);
  8850. unlock_user(p, arg2, sizeof(target_siginfo_t));
  8851. }
  8852. ret = host_to_target_signal(ret);
  8853. }
  8854. }
  8855. return ret;
  8856. #endif
  8857. case TARGET_NR_rt_sigqueueinfo:
  8858. {
  8859. siginfo_t uinfo;
  8860. p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1);
  8861. if (!p) {
  8862. return -TARGET_EFAULT;
  8863. }
  8864. target_to_host_siginfo(&uinfo, p);
  8865. unlock_user(p, arg3, 0);
  8866. ret = get_errno(sys_rt_sigqueueinfo(arg1, target_to_host_signal(arg2), &uinfo));
  8867. }
  8868. return ret;
  8869. case TARGET_NR_rt_tgsigqueueinfo:
  8870. {
  8871. siginfo_t uinfo;
  8872. p = lock_user(VERIFY_READ, arg4, sizeof(target_siginfo_t), 1);
  8873. if (!p) {
  8874. return -TARGET_EFAULT;
  8875. }
  8876. target_to_host_siginfo(&uinfo, p);
  8877. unlock_user(p, arg4, 0);
  8878. ret = get_errno(sys_rt_tgsigqueueinfo(arg1, arg2, target_to_host_signal(arg3), &uinfo));
  8879. }
  8880. return ret;
  8881. #ifdef TARGET_NR_sigreturn
  8882. case TARGET_NR_sigreturn:
  8883. if (block_signals()) {
  8884. return -QEMU_ERESTARTSYS;
  8885. }
  8886. return do_sigreturn(cpu_env);
  8887. #endif
  8888. case TARGET_NR_rt_sigreturn:
  8889. if (block_signals()) {
  8890. return -QEMU_ERESTARTSYS;
  8891. }
  8892. return do_rt_sigreturn(cpu_env);
  8893. case TARGET_NR_sethostname:
  8894. if (!(p = lock_user_string(arg1)))
  8895. return -TARGET_EFAULT;
  8896. ret = get_errno(sethostname(p, arg2));
  8897. unlock_user(p, arg1, 0);
  8898. return ret;
  8899. #ifdef TARGET_NR_setrlimit
  8900. case TARGET_NR_setrlimit:
  8901. {
  8902. int resource = target_to_host_resource(arg1);
  8903. struct target_rlimit *target_rlim;
  8904. struct rlimit rlim;
  8905. if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
  8906. return -TARGET_EFAULT;
  8907. rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
  8908. rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
  8909. unlock_user_struct(target_rlim, arg2, 0);
  8910. /*
  8911. * If we just passed through resource limit settings for memory then
  8912. * they would also apply to QEMU's own allocations, and QEMU will
  8913. * crash or hang or die if its allocations fail. Ideally we would
  8914. * track the guest allocations in QEMU and apply the limits ourselves.
  8915. * For now, just tell the guest the call succeeded but don't actually
  8916. * limit anything.
  8917. */
  8918. if (resource != RLIMIT_AS &&
  8919. resource != RLIMIT_DATA &&
  8920. resource != RLIMIT_STACK) {
  8921. return get_errno(setrlimit(resource, &rlim));
  8922. } else {
  8923. return 0;
  8924. }
  8925. }
  8926. #endif
  8927. #ifdef TARGET_NR_getrlimit
  8928. case TARGET_NR_getrlimit:
  8929. {
  8930. int resource = target_to_host_resource(arg1);
  8931. struct target_rlimit *target_rlim;
  8932. struct rlimit rlim;
  8933. ret = get_errno(getrlimit(resource, &rlim));
  8934. if (!is_error(ret)) {
  8935. if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
  8936. return -TARGET_EFAULT;
  8937. target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
  8938. target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
  8939. unlock_user_struct(target_rlim, arg2, 1);
  8940. }
  8941. }
  8942. return ret;
  8943. #endif
  8944. case TARGET_NR_getrusage:
  8945. {
  8946. struct rusage rusage;
  8947. ret = get_errno(getrusage(arg1, &rusage));
  8948. if (!is_error(ret)) {
  8949. ret = host_to_target_rusage(arg2, &rusage);
  8950. }
  8951. }
  8952. return ret;
  8953. #if defined(TARGET_NR_gettimeofday)
  8954. case TARGET_NR_gettimeofday:
  8955. {
  8956. struct timeval tv;
  8957. struct timezone tz;
  8958. ret = get_errno(gettimeofday(&tv, &tz));
  8959. if (!is_error(ret)) {
  8960. if (arg1 && copy_to_user_timeval(arg1, &tv)) {
  8961. return -TARGET_EFAULT;
  8962. }
  8963. if (arg2 && copy_to_user_timezone(arg2, &tz)) {
  8964. return -TARGET_EFAULT;
  8965. }
  8966. }
  8967. }
  8968. return ret;
  8969. #endif
  8970. #if defined(TARGET_NR_settimeofday)
  8971. case TARGET_NR_settimeofday:
  8972. {
  8973. struct timeval tv, *ptv = NULL;
  8974. struct timezone tz, *ptz = NULL;
  8975. if (arg1) {
  8976. if (copy_from_user_timeval(&tv, arg1)) {
  8977. return -TARGET_EFAULT;
  8978. }
  8979. ptv = &tv;
  8980. }
  8981. if (arg2) {
  8982. if (copy_from_user_timezone(&tz, arg2)) {
  8983. return -TARGET_EFAULT;
  8984. }
  8985. ptz = &tz;
  8986. }
  8987. return get_errno(settimeofday(ptv, ptz));
  8988. }
  8989. #endif
  8990. #if defined(TARGET_NR_select)
  8991. case TARGET_NR_select:
  8992. #if defined(TARGET_WANT_NI_OLD_SELECT)
  8993. /* some architectures used to have old_select here
  8994. * but now ENOSYS it.
  8995. */
  8996. ret = -TARGET_ENOSYS;
  8997. #elif defined(TARGET_WANT_OLD_SYS_SELECT)
  8998. ret = do_old_select(arg1);
  8999. #else
  9000. ret = do_select(arg1, arg2, arg3, arg4, arg5);
  9001. #endif
  9002. return ret;
  9003. #endif
  9004. #ifdef TARGET_NR_pselect6
  9005. case TARGET_NR_pselect6:
  9006. return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, false);
  9007. #endif
  9008. #ifdef TARGET_NR_pselect6_time64
  9009. case TARGET_NR_pselect6_time64:
  9010. return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, true);
  9011. #endif
  9012. #ifdef TARGET_NR_symlink
  9013. case TARGET_NR_symlink:
  9014. {
  9015. void *p2;
  9016. p = lock_user_string(arg1);
  9017. p2 = lock_user_string(arg2);
  9018. if (!p || !p2)
  9019. ret = -TARGET_EFAULT;
  9020. else
  9021. ret = get_errno(symlink(p, p2));
  9022. unlock_user(p2, arg2, 0);
  9023. unlock_user(p, arg1, 0);
  9024. }
  9025. return ret;
  9026. #endif
  9027. #if defined(TARGET_NR_symlinkat)
  9028. case TARGET_NR_symlinkat:
  9029. {
  9030. void *p2;
  9031. p = lock_user_string(arg1);
  9032. p2 = lock_user_string(arg3);
  9033. if (!p || !p2)
  9034. ret = -TARGET_EFAULT;
  9035. else
  9036. ret = get_errno(symlinkat(p, arg2, p2));
  9037. unlock_user(p2, arg3, 0);
  9038. unlock_user(p, arg1, 0);
  9039. }
  9040. return ret;
  9041. #endif
  9042. #ifdef TARGET_NR_readlink
  9043. case TARGET_NR_readlink:
  9044. {
  9045. void *p2;
  9046. p = lock_user_string(arg1);
  9047. p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  9048. if (!p || !p2) {
  9049. ret = -TARGET_EFAULT;
  9050. } else if (!arg3) {
  9051. /* Short circuit this for the magic exe check. */
  9052. ret = -TARGET_EINVAL;
  9053. } else if (is_proc_myself((const char *)p, "exe")) {
  9054. /*
  9055. * Don't worry about sign mismatch as earlier mapping
  9056. * logic would have thrown a bad address error.
  9057. */
  9058. ret = MIN(strlen(exec_path), arg3);
  9059. /* We cannot NUL terminate the string. */
  9060. memcpy(p2, exec_path, ret);
  9061. } else {
  9062. ret = get_errno(readlink(path(p), p2, arg3));
  9063. }
  9064. unlock_user(p2, arg2, ret);
  9065. unlock_user(p, arg1, 0);
  9066. }
  9067. return ret;
  9068. #endif
  9069. #if defined(TARGET_NR_readlinkat)
  9070. case TARGET_NR_readlinkat:
  9071. {
  9072. void *p2;
  9073. p = lock_user_string(arg2);
  9074. p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  9075. if (!p || !p2) {
  9076. ret = -TARGET_EFAULT;
  9077. } else if (!arg4) {
  9078. /* Short circuit this for the magic exe check. */
  9079. ret = -TARGET_EINVAL;
  9080. } else if (is_proc_myself((const char *)p, "exe")) {
  9081. /*
  9082. * Don't worry about sign mismatch as earlier mapping
  9083. * logic would have thrown a bad address error.
  9084. */
  9085. ret = MIN(strlen(exec_path), arg4);
  9086. /* We cannot NUL terminate the string. */
  9087. memcpy(p2, exec_path, ret);
  9088. } else {
  9089. ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
  9090. }
  9091. unlock_user(p2, arg3, ret);
  9092. unlock_user(p, arg2, 0);
  9093. }
  9094. return ret;
  9095. #endif
  9096. #ifdef TARGET_NR_swapon
  9097. case TARGET_NR_swapon:
  9098. if (!(p = lock_user_string(arg1)))
  9099. return -TARGET_EFAULT;
  9100. ret = get_errno(swapon(p, arg2));
  9101. unlock_user(p, arg1, 0);
  9102. return ret;
  9103. #endif
  9104. case TARGET_NR_reboot:
  9105. if (arg3 == LINUX_REBOOT_CMD_RESTART2) {
  9106. /* arg4 must be ignored in all other cases */
  9107. p = lock_user_string(arg4);
  9108. if (!p) {
  9109. return -TARGET_EFAULT;
  9110. }
  9111. ret = get_errno(reboot(arg1, arg2, arg3, p));
  9112. unlock_user(p, arg4, 0);
  9113. } else {
  9114. ret = get_errno(reboot(arg1, arg2, arg3, NULL));
  9115. }
  9116. return ret;
  9117. #ifdef TARGET_NR_mmap
  9118. case TARGET_NR_mmap:
  9119. #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
  9120. (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
  9121. defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
  9122. || defined(TARGET_S390X)
  9123. {
  9124. abi_ulong *v;
  9125. abi_ulong v1, v2, v3, v4, v5, v6;
  9126. if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
  9127. return -TARGET_EFAULT;
  9128. v1 = tswapal(v[0]);
  9129. v2 = tswapal(v[1]);
  9130. v3 = tswapal(v[2]);
  9131. v4 = tswapal(v[3]);
  9132. v5 = tswapal(v[4]);
  9133. v6 = tswapal(v[5]);
  9134. unlock_user(v, arg1, 0);
  9135. ret = get_errno(target_mmap(v1, v2, v3,
  9136. target_to_host_bitmask(v4, mmap_flags_tbl),
  9137. v5, v6));
  9138. }
  9139. #else
  9140. /* mmap pointers are always untagged */
  9141. ret = get_errno(target_mmap(arg1, arg2, arg3,
  9142. target_to_host_bitmask(arg4, mmap_flags_tbl),
  9143. arg5,
  9144. arg6));
  9145. #endif
  9146. return ret;
  9147. #endif
  9148. #ifdef TARGET_NR_mmap2
  9149. case TARGET_NR_mmap2:
  9150. #ifndef MMAP_SHIFT
  9151. #define MMAP_SHIFT 12
  9152. #endif
  9153. ret = target_mmap(arg1, arg2, arg3,
  9154. target_to_host_bitmask(arg4, mmap_flags_tbl),
  9155. arg5, arg6 << MMAP_SHIFT);
  9156. return get_errno(ret);
  9157. #endif
  9158. case TARGET_NR_munmap:
  9159. arg1 = cpu_untagged_addr(cpu, arg1);
  9160. return get_errno(target_munmap(arg1, arg2));
  9161. case TARGET_NR_mprotect:
  9162. arg1 = cpu_untagged_addr(cpu, arg1);
  9163. {
  9164. TaskState *ts = cpu->opaque;
  9165. /* Special hack to detect libc making the stack executable. */
  9166. if ((arg3 & PROT_GROWSDOWN)
  9167. && arg1 >= ts->info->stack_limit
  9168. && arg1 <= ts->info->start_stack) {
  9169. arg3 &= ~PROT_GROWSDOWN;
  9170. arg2 = arg2 + arg1 - ts->info->stack_limit;
  9171. arg1 = ts->info->stack_limit;
  9172. }
  9173. }
  9174. return get_errno(target_mprotect(arg1, arg2, arg3));
  9175. #ifdef TARGET_NR_mremap
  9176. case TARGET_NR_mremap:
  9177. arg1 = cpu_untagged_addr(cpu, arg1);
  9178. /* mremap new_addr (arg5) is always untagged */
  9179. return get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
  9180. #endif
  9181. /* ??? msync/mlock/munlock are broken for softmmu. */
  9182. #ifdef TARGET_NR_msync
  9183. case TARGET_NR_msync:
  9184. return get_errno(msync(g2h(cpu, arg1), arg2, arg3));
  9185. #endif
  9186. #ifdef TARGET_NR_mlock
  9187. case TARGET_NR_mlock:
  9188. return get_errno(mlock(g2h(cpu, arg1), arg2));
  9189. #endif
  9190. #ifdef TARGET_NR_munlock
  9191. case TARGET_NR_munlock:
  9192. return get_errno(munlock(g2h(cpu, arg1), arg2));
  9193. #endif
  9194. #ifdef TARGET_NR_mlockall
  9195. case TARGET_NR_mlockall:
  9196. return get_errno(mlockall(target_to_host_mlockall_arg(arg1)));
  9197. #endif
  9198. #ifdef TARGET_NR_munlockall
  9199. case TARGET_NR_munlockall:
  9200. return get_errno(munlockall());
  9201. #endif
  9202. #ifdef TARGET_NR_truncate
  9203. case TARGET_NR_truncate:
  9204. if (!(p = lock_user_string(arg1)))
  9205. return -TARGET_EFAULT;
  9206. ret = get_errno(truncate(p, arg2));
  9207. unlock_user(p, arg1, 0);
  9208. return ret;
  9209. #endif
  9210. #ifdef TARGET_NR_ftruncate
  9211. case TARGET_NR_ftruncate:
  9212. return get_errno(ftruncate(arg1, arg2));
  9213. #endif
  9214. case TARGET_NR_fchmod:
  9215. return get_errno(fchmod(arg1, arg2));
  9216. #if defined(TARGET_NR_fchmodat)
  9217. case TARGET_NR_fchmodat:
  9218. if (!(p = lock_user_string(arg2)))
  9219. return -TARGET_EFAULT;
  9220. ret = get_errno(fchmodat(arg1, p, arg3, 0));
  9221. unlock_user(p, arg2, 0);
  9222. return ret;
  9223. #endif
  9224. case TARGET_NR_getpriority:
  9225. /* Note that negative values are valid for getpriority, so we must
  9226. differentiate based on errno settings. */
  9227. errno = 0;
  9228. ret = getpriority(arg1, arg2);
  9229. if (ret == -1 && errno != 0) {
  9230. return -host_to_target_errno(errno);
  9231. }
  9232. #ifdef TARGET_ALPHA
  9233. /* Return value is the unbiased priority. Signal no error. */
  9234. cpu_env->ir[IR_V0] = 0;
  9235. #else
  9236. /* Return value is a biased priority to avoid negative numbers. */
  9237. ret = 20 - ret;
  9238. #endif
  9239. return ret;
  9240. case TARGET_NR_setpriority:
  9241. return get_errno(setpriority(arg1, arg2, arg3));
  9242. #ifdef TARGET_NR_statfs
  9243. case TARGET_NR_statfs:
  9244. if (!(p = lock_user_string(arg1))) {
  9245. return -TARGET_EFAULT;
  9246. }
  9247. ret = get_errno(statfs(path(p), &stfs));
  9248. unlock_user(p, arg1, 0);
  9249. convert_statfs:
  9250. if (!is_error(ret)) {
  9251. struct target_statfs *target_stfs;
  9252. if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
  9253. return -TARGET_EFAULT;
  9254. __put_user(stfs.f_type, &target_stfs->f_type);
  9255. __put_user(stfs.f_bsize, &target_stfs->f_bsize);
  9256. __put_user(stfs.f_blocks, &target_stfs->f_blocks);
  9257. __put_user(stfs.f_bfree, &target_stfs->f_bfree);
  9258. __put_user(stfs.f_bavail, &target_stfs->f_bavail);
  9259. __put_user(stfs.f_files, &target_stfs->f_files);
  9260. __put_user(stfs.f_ffree, &target_stfs->f_ffree);
  9261. __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
  9262. __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
  9263. __put_user(stfs.f_namelen, &target_stfs->f_namelen);
  9264. __put_user(stfs.f_frsize, &target_stfs->f_frsize);
  9265. #ifdef _STATFS_F_FLAGS
  9266. __put_user(stfs.f_flags, &target_stfs->f_flags);
  9267. #else
  9268. __put_user(0, &target_stfs->f_flags);
  9269. #endif
  9270. memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
  9271. unlock_user_struct(target_stfs, arg2, 1);
  9272. }
  9273. return ret;
  9274. #endif
  9275. #ifdef TARGET_NR_fstatfs
  9276. case TARGET_NR_fstatfs:
  9277. ret = get_errno(fstatfs(arg1, &stfs));
  9278. goto convert_statfs;
  9279. #endif
  9280. #ifdef TARGET_NR_statfs64
  9281. case TARGET_NR_statfs64:
  9282. if (!(p = lock_user_string(arg1))) {
  9283. return -TARGET_EFAULT;
  9284. }
  9285. ret = get_errno(statfs(path(p), &stfs));
  9286. unlock_user(p, arg1, 0);
  9287. convert_statfs64:
  9288. if (!is_error(ret)) {
  9289. struct target_statfs64 *target_stfs;
  9290. if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
  9291. return -TARGET_EFAULT;
  9292. __put_user(stfs.f_type, &target_stfs->f_type);
  9293. __put_user(stfs.f_bsize, &target_stfs->f_bsize);
  9294. __put_user(stfs.f_blocks, &target_stfs->f_blocks);
  9295. __put_user(stfs.f_bfree, &target_stfs->f_bfree);
  9296. __put_user(stfs.f_bavail, &target_stfs->f_bavail);
  9297. __put_user(stfs.f_files, &target_stfs->f_files);
  9298. __put_user(stfs.f_ffree, &target_stfs->f_ffree);
  9299. __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
  9300. __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
  9301. __put_user(stfs.f_namelen, &target_stfs->f_namelen);
  9302. __put_user(stfs.f_frsize, &target_stfs->f_frsize);
  9303. #ifdef _STATFS_F_FLAGS
  9304. __put_user(stfs.f_flags, &target_stfs->f_flags);
  9305. #else
  9306. __put_user(0, &target_stfs->f_flags);
  9307. #endif
  9308. memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
  9309. unlock_user_struct(target_stfs, arg3, 1);
  9310. }
  9311. return ret;
  9312. case TARGET_NR_fstatfs64:
  9313. ret = get_errno(fstatfs(arg1, &stfs));
  9314. goto convert_statfs64;
  9315. #endif
  9316. #ifdef TARGET_NR_socketcall
  9317. case TARGET_NR_socketcall:
  9318. return do_socketcall(arg1, arg2);
  9319. #endif
  9320. #ifdef TARGET_NR_accept
  9321. case TARGET_NR_accept:
  9322. return do_accept4(arg1, arg2, arg3, 0);
  9323. #endif
  9324. #ifdef TARGET_NR_accept4
  9325. case TARGET_NR_accept4:
  9326. return do_accept4(arg1, arg2, arg3, arg4);
  9327. #endif
  9328. #ifdef TARGET_NR_bind
  9329. case TARGET_NR_bind:
  9330. return do_bind(arg1, arg2, arg3);
  9331. #endif
  9332. #ifdef TARGET_NR_connect
  9333. case TARGET_NR_connect:
  9334. return do_connect(arg1, arg2, arg3);
  9335. #endif
  9336. #ifdef TARGET_NR_getpeername
  9337. case TARGET_NR_getpeername:
  9338. return do_getpeername(arg1, arg2, arg3);
  9339. #endif
  9340. #ifdef TARGET_NR_getsockname
  9341. case TARGET_NR_getsockname:
  9342. return do_getsockname(arg1, arg2, arg3);
  9343. #endif
  9344. #ifdef TARGET_NR_getsockopt
  9345. case TARGET_NR_getsockopt:
  9346. return do_getsockopt(arg1, arg2, arg3, arg4, arg5);
  9347. #endif
  9348. #ifdef TARGET_NR_listen
  9349. case TARGET_NR_listen:
  9350. return get_errno(listen(arg1, arg2));
  9351. #endif
  9352. #ifdef TARGET_NR_recv
  9353. case TARGET_NR_recv:
  9354. return do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
  9355. #endif
  9356. #ifdef TARGET_NR_recvfrom
  9357. case TARGET_NR_recvfrom:
  9358. return do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
  9359. #endif
  9360. #ifdef TARGET_NR_recvmsg
  9361. case TARGET_NR_recvmsg:
  9362. return do_sendrecvmsg(arg1, arg2, arg3, 0);
  9363. #endif
  9364. #ifdef TARGET_NR_send
  9365. case TARGET_NR_send:
  9366. return do_sendto(arg1, arg2, arg3, arg4, 0, 0);
  9367. #endif
  9368. #ifdef TARGET_NR_sendmsg
  9369. case TARGET_NR_sendmsg:
  9370. return do_sendrecvmsg(arg1, arg2, arg3, 1);
  9371. #endif
  9372. #ifdef TARGET_NR_sendmmsg
  9373. case TARGET_NR_sendmmsg:
  9374. return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 1);
  9375. #endif
  9376. #ifdef TARGET_NR_recvmmsg
  9377. case TARGET_NR_recvmmsg:
  9378. return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 0);
  9379. #endif
  9380. #ifdef TARGET_NR_sendto
  9381. case TARGET_NR_sendto:
  9382. return do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
  9383. #endif
  9384. #ifdef TARGET_NR_shutdown
  9385. case TARGET_NR_shutdown:
  9386. return get_errno(shutdown(arg1, arg2));
  9387. #endif
  9388. #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
  9389. case TARGET_NR_getrandom:
  9390. p = lock_user(VERIFY_WRITE, arg1, arg2, 0);
  9391. if (!p) {
  9392. return -TARGET_EFAULT;
  9393. }
  9394. ret = get_errno(getrandom(p, arg2, arg3));
  9395. unlock_user(p, arg1, ret);
  9396. return ret;
  9397. #endif
  9398. #ifdef TARGET_NR_socket
  9399. case TARGET_NR_socket:
  9400. return do_socket(arg1, arg2, arg3);
  9401. #endif
  9402. #ifdef TARGET_NR_socketpair
  9403. case TARGET_NR_socketpair:
  9404. return do_socketpair(arg1, arg2, arg3, arg4);
  9405. #endif
  9406. #ifdef TARGET_NR_setsockopt
  9407. case TARGET_NR_setsockopt:
  9408. return do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
  9409. #endif
  9410. #if defined(TARGET_NR_syslog)
  9411. case TARGET_NR_syslog:
  9412. {
  9413. int len = arg2;
  9414. switch (arg1) {
  9415. case TARGET_SYSLOG_ACTION_CLOSE: /* Close log */
  9416. case TARGET_SYSLOG_ACTION_OPEN: /* Open log */
  9417. case TARGET_SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
  9418. case TARGET_SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging */
  9419. case TARGET_SYSLOG_ACTION_CONSOLE_ON: /* Enable logging */
  9420. case TARGET_SYSLOG_ACTION_CONSOLE_LEVEL: /* Set messages level */
  9421. case TARGET_SYSLOG_ACTION_SIZE_UNREAD: /* Number of chars */
  9422. case TARGET_SYSLOG_ACTION_SIZE_BUFFER: /* Size of the buffer */
  9423. return get_errno(sys_syslog((int)arg1, NULL, (int)arg3));
  9424. case TARGET_SYSLOG_ACTION_READ: /* Read from log */
  9425. case TARGET_SYSLOG_ACTION_READ_CLEAR: /* Read/clear msgs */
  9426. case TARGET_SYSLOG_ACTION_READ_ALL: /* Read last messages */
  9427. {
  9428. if (len < 0) {
  9429. return -TARGET_EINVAL;
  9430. }
  9431. if (len == 0) {
  9432. return 0;
  9433. }
  9434. p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  9435. if (!p) {
  9436. return -TARGET_EFAULT;
  9437. }
  9438. ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
  9439. unlock_user(p, arg2, arg3);
  9440. }
  9441. return ret;
  9442. default:
  9443. return -TARGET_EINVAL;
  9444. }
  9445. }
  9446. break;
  9447. #endif
  9448. case TARGET_NR_setitimer:
  9449. {
  9450. struct itimerval value, ovalue, *pvalue;
  9451. if (arg2) {
  9452. pvalue = &value;
  9453. if (copy_from_user_timeval(&pvalue->it_interval, arg2)
  9454. || copy_from_user_timeval(&pvalue->it_value,
  9455. arg2 + sizeof(struct target_timeval)))
  9456. return -TARGET_EFAULT;
  9457. } else {
  9458. pvalue = NULL;
  9459. }
  9460. ret = get_errno(setitimer(arg1, pvalue, &ovalue));
  9461. if (!is_error(ret) && arg3) {
  9462. if (copy_to_user_timeval(arg3,
  9463. &ovalue.it_interval)
  9464. || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
  9465. &ovalue.it_value))
  9466. return -TARGET_EFAULT;
  9467. }
  9468. }
  9469. return ret;
  9470. case TARGET_NR_getitimer:
  9471. {
  9472. struct itimerval value;
  9473. ret = get_errno(getitimer(arg1, &value));
  9474. if (!is_error(ret) && arg2) {
  9475. if (copy_to_user_timeval(arg2,
  9476. &value.it_interval)
  9477. || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
  9478. &value.it_value))
  9479. return -TARGET_EFAULT;
  9480. }
  9481. }
  9482. return ret;
  9483. #ifdef TARGET_NR_stat
  9484. case TARGET_NR_stat:
  9485. if (!(p = lock_user_string(arg1))) {
  9486. return -TARGET_EFAULT;
  9487. }
  9488. ret = get_errno(stat(path(p), &st));
  9489. unlock_user(p, arg1, 0);
  9490. goto do_stat;
  9491. #endif
  9492. #ifdef TARGET_NR_lstat
  9493. case TARGET_NR_lstat:
  9494. if (!(p = lock_user_string(arg1))) {
  9495. return -TARGET_EFAULT;
  9496. }
  9497. ret = get_errno(lstat(path(p), &st));
  9498. unlock_user(p, arg1, 0);
  9499. goto do_stat;
  9500. #endif
  9501. #ifdef TARGET_NR_fstat
  9502. case TARGET_NR_fstat:
  9503. {
  9504. ret = get_errno(fstat(arg1, &st));
  9505. #if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat)
  9506. do_stat:
  9507. #endif
  9508. if (!is_error(ret)) {
  9509. struct target_stat *target_st;
  9510. if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
  9511. return -TARGET_EFAULT;
  9512. memset(target_st, 0, sizeof(*target_st));
  9513. __put_user(st.st_dev, &target_st->st_dev);
  9514. __put_user(st.st_ino, &target_st->st_ino);
  9515. __put_user(st.st_mode, &target_st->st_mode);
  9516. __put_user(st.st_uid, &target_st->st_uid);
  9517. __put_user(st.st_gid, &target_st->st_gid);
  9518. __put_user(st.st_nlink, &target_st->st_nlink);
  9519. __put_user(st.st_rdev, &target_st->st_rdev);
  9520. __put_user(st.st_size, &target_st->st_size);
  9521. __put_user(st.st_blksize, &target_st->st_blksize);
  9522. __put_user(st.st_blocks, &target_st->st_blocks);
  9523. __put_user(st.st_atime, &target_st->target_st_atime);
  9524. __put_user(st.st_mtime, &target_st->target_st_mtime);
  9525. __put_user(st.st_ctime, &target_st->target_st_ctime);
  9526. #if defined(HAVE_STRUCT_STAT_ST_ATIM) && defined(TARGET_STAT_HAVE_NSEC)
  9527. __put_user(st.st_atim.tv_nsec,
  9528. &target_st->target_st_atime_nsec);
  9529. __put_user(st.st_mtim.tv_nsec,
  9530. &target_st->target_st_mtime_nsec);
  9531. __put_user(st.st_ctim.tv_nsec,
  9532. &target_st->target_st_ctime_nsec);
  9533. #endif
  9534. unlock_user_struct(target_st, arg2, 1);
  9535. }
  9536. }
  9537. return ret;
  9538. #endif
  9539. case TARGET_NR_vhangup:
  9540. return get_errno(vhangup());
  9541. #ifdef TARGET_NR_syscall
  9542. case TARGET_NR_syscall:
  9543. return do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
  9544. arg6, arg7, arg8, 0);
  9545. #endif
  9546. #if defined(TARGET_NR_wait4)
  9547. case TARGET_NR_wait4:
  9548. {
  9549. int status;
  9550. abi_long status_ptr = arg2;
  9551. struct rusage rusage, *rusage_ptr;
  9552. abi_ulong target_rusage = arg4;
  9553. abi_long rusage_err;
  9554. if (target_rusage)
  9555. rusage_ptr = &rusage;
  9556. else
  9557. rusage_ptr = NULL;
  9558. ret = get_errno(safe_wait4(arg1, &status, arg3, rusage_ptr));
  9559. if (!is_error(ret)) {
  9560. if (status_ptr && ret) {
  9561. status = host_to_target_waitstatus(status);
  9562. if (put_user_s32(status, status_ptr))
  9563. return -TARGET_EFAULT;
  9564. }
  9565. if (target_rusage) {
  9566. rusage_err = host_to_target_rusage(target_rusage, &rusage);
  9567. if (rusage_err) {
  9568. ret = rusage_err;
  9569. }
  9570. }
  9571. }
  9572. }
  9573. return ret;
  9574. #endif
  9575. #ifdef TARGET_NR_swapoff
  9576. case TARGET_NR_swapoff:
  9577. if (!(p = lock_user_string(arg1)))
  9578. return -TARGET_EFAULT;
  9579. ret = get_errno(swapoff(p));
  9580. unlock_user(p, arg1, 0);
  9581. return ret;
  9582. #endif
  9583. case TARGET_NR_sysinfo:
  9584. {
  9585. struct target_sysinfo *target_value;
  9586. struct sysinfo value;
  9587. ret = get_errno(sysinfo(&value));
  9588. if (!is_error(ret) && arg1)
  9589. {
  9590. if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
  9591. return -TARGET_EFAULT;
  9592. __put_user(value.uptime, &target_value->uptime);
  9593. __put_user(value.loads[0], &target_value->loads[0]);
  9594. __put_user(value.loads[1], &target_value->loads[1]);
  9595. __put_user(value.loads[2], &target_value->loads[2]);
  9596. __put_user(value.totalram, &target_value->totalram);
  9597. __put_user(value.freeram, &target_value->freeram);
  9598. __put_user(value.sharedram, &target_value->sharedram);
  9599. __put_user(value.bufferram, &target_value->bufferram);
  9600. __put_user(value.totalswap, &target_value->totalswap);
  9601. __put_user(value.freeswap, &target_value->freeswap);
  9602. __put_user(value.procs, &target_value->procs);
  9603. __put_user(value.totalhigh, &target_value->totalhigh);
  9604. __put_user(value.freehigh, &target_value->freehigh);
  9605. __put_user(value.mem_unit, &target_value->mem_unit);
  9606. unlock_user_struct(target_value, arg1, 1);
  9607. }
  9608. }
  9609. return ret;
  9610. #ifdef TARGET_NR_ipc
  9611. case TARGET_NR_ipc:
  9612. return do_ipc(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6);
  9613. #endif
  9614. #ifdef TARGET_NR_semget
  9615. case TARGET_NR_semget:
  9616. return get_errno(semget(arg1, arg2, arg3));
  9617. #endif
  9618. #ifdef TARGET_NR_semop
  9619. case TARGET_NR_semop:
  9620. return do_semtimedop(arg1, arg2, arg3, 0, false);
  9621. #endif
  9622. #ifdef TARGET_NR_semtimedop
  9623. case TARGET_NR_semtimedop:
  9624. return do_semtimedop(arg1, arg2, arg3, arg4, false);
  9625. #endif
  9626. #ifdef TARGET_NR_semtimedop_time64
  9627. case TARGET_NR_semtimedop_time64:
  9628. return do_semtimedop(arg1, arg2, arg3, arg4, true);
  9629. #endif
  9630. #ifdef TARGET_NR_semctl
  9631. case TARGET_NR_semctl:
  9632. return do_semctl(arg1, arg2, arg3, arg4);
  9633. #endif
  9634. #ifdef TARGET_NR_msgctl
  9635. case TARGET_NR_msgctl:
  9636. return do_msgctl(arg1, arg2, arg3);
  9637. #endif
  9638. #ifdef TARGET_NR_msgget
  9639. case TARGET_NR_msgget:
  9640. return get_errno(msgget(arg1, arg2));
  9641. #endif
  9642. #ifdef TARGET_NR_msgrcv
  9643. case TARGET_NR_msgrcv:
  9644. return do_msgrcv(arg1, arg2, arg3, arg4, arg5);
  9645. #endif
  9646. #ifdef TARGET_NR_msgsnd
  9647. case TARGET_NR_msgsnd:
  9648. return do_msgsnd(arg1, arg2, arg3, arg4);
  9649. #endif
  9650. #ifdef TARGET_NR_shmget
  9651. case TARGET_NR_shmget:
  9652. return get_errno(shmget(arg1, arg2, arg3));
  9653. #endif
  9654. #ifdef TARGET_NR_shmctl
  9655. case TARGET_NR_shmctl:
  9656. return do_shmctl(arg1, arg2, arg3);
  9657. #endif
  9658. #ifdef TARGET_NR_shmat
  9659. case TARGET_NR_shmat:
  9660. return do_shmat(cpu_env, arg1, arg2, arg3);
  9661. #endif
  9662. #ifdef TARGET_NR_shmdt
  9663. case TARGET_NR_shmdt:
  9664. return do_shmdt(arg1);
  9665. #endif
  9666. case TARGET_NR_fsync:
  9667. return get_errno(fsync(arg1));
  9668. case TARGET_NR_clone:
  9669. /* Linux manages to have three different orderings for its
  9670. * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
  9671. * match the kernel's CONFIG_CLONE_* settings.
  9672. * Microblaze is further special in that it uses a sixth
  9673. * implicit argument to clone for the TLS pointer.
  9674. */
  9675. #if defined(TARGET_MICROBLAZE)
  9676. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5));
  9677. #elif defined(TARGET_CLONE_BACKWARDS)
  9678. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
  9679. #elif defined(TARGET_CLONE_BACKWARDS2)
  9680. ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
  9681. #else
  9682. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
  9683. #endif
  9684. return ret;
  9685. #ifdef __NR_exit_group
  9686. /* new thread calls */
  9687. case TARGET_NR_exit_group:
  9688. preexit_cleanup(cpu_env, arg1);
  9689. return get_errno(exit_group(arg1));
  9690. #endif
  9691. case TARGET_NR_setdomainname:
  9692. if (!(p = lock_user_string(arg1)))
  9693. return -TARGET_EFAULT;
  9694. ret = get_errno(setdomainname(p, arg2));
  9695. unlock_user(p, arg1, 0);
  9696. return ret;
  9697. case TARGET_NR_uname:
  9698. /* no need to transcode because we use the linux syscall */
  9699. {
  9700. struct new_utsname * buf;
  9701. if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
  9702. return -TARGET_EFAULT;
  9703. ret = get_errno(sys_uname(buf));
  9704. if (!is_error(ret)) {
  9705. /* Overwrite the native machine name with whatever is being
  9706. emulated. */
  9707. g_strlcpy(buf->machine, cpu_to_uname_machine(cpu_env),
  9708. sizeof(buf->machine));
  9709. /* Allow the user to override the reported release. */
  9710. if (qemu_uname_release && *qemu_uname_release) {
  9711. g_strlcpy(buf->release, qemu_uname_release,
  9712. sizeof(buf->release));
  9713. }
  9714. }
  9715. unlock_user_struct(buf, arg1, 1);
  9716. }
  9717. return ret;
  9718. #ifdef TARGET_I386
  9719. case TARGET_NR_modify_ldt:
  9720. return do_modify_ldt(cpu_env, arg1, arg2, arg3);
  9721. #if !defined(TARGET_X86_64)
  9722. case TARGET_NR_vm86:
  9723. return do_vm86(cpu_env, arg1, arg2);
  9724. #endif
  9725. #endif
  9726. #if defined(TARGET_NR_adjtimex)
  9727. case TARGET_NR_adjtimex:
  9728. {
  9729. struct timex host_buf;
  9730. if (target_to_host_timex(&host_buf, arg1) != 0) {
  9731. return -TARGET_EFAULT;
  9732. }
  9733. ret = get_errno(adjtimex(&host_buf));
  9734. if (!is_error(ret)) {
  9735. if (host_to_target_timex(arg1, &host_buf) != 0) {
  9736. return -TARGET_EFAULT;
  9737. }
  9738. }
  9739. }
  9740. return ret;
  9741. #endif
  9742. #if defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME)
  9743. case TARGET_NR_clock_adjtime:
  9744. {
  9745. struct timex htx, *phtx = &htx;
  9746. if (target_to_host_timex(phtx, arg2) != 0) {
  9747. return -TARGET_EFAULT;
  9748. }
  9749. ret = get_errno(clock_adjtime(arg1, phtx));
  9750. if (!is_error(ret) && phtx) {
  9751. if (host_to_target_timex(arg2, phtx) != 0) {
  9752. return -TARGET_EFAULT;
  9753. }
  9754. }
  9755. }
  9756. return ret;
  9757. #endif
  9758. #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
  9759. case TARGET_NR_clock_adjtime64:
  9760. {
  9761. struct timex htx;
  9762. if (target_to_host_timex64(&htx, arg2) != 0) {
  9763. return -TARGET_EFAULT;
  9764. }
  9765. ret = get_errno(clock_adjtime(arg1, &htx));
  9766. if (!is_error(ret) && host_to_target_timex64(arg2, &htx)) {
  9767. return -TARGET_EFAULT;
  9768. }
  9769. }
  9770. return ret;
  9771. #endif
  9772. case TARGET_NR_getpgid:
  9773. return get_errno(getpgid(arg1));
  9774. case TARGET_NR_fchdir:
  9775. return get_errno(fchdir(arg1));
  9776. case TARGET_NR_personality:
  9777. return get_errno(personality(arg1));
  9778. #ifdef TARGET_NR__llseek /* Not on alpha */
  9779. case TARGET_NR__llseek:
  9780. {
  9781. int64_t res;
  9782. #if !defined(__NR_llseek)
  9783. res = lseek(arg1, ((uint64_t)arg2 << 32) | (abi_ulong)arg3, arg5);
  9784. if (res == -1) {
  9785. ret = get_errno(res);
  9786. } else {
  9787. ret = 0;
  9788. }
  9789. #else
  9790. ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
  9791. #endif
  9792. if ((ret == 0) && put_user_s64(res, arg4)) {
  9793. return -TARGET_EFAULT;
  9794. }
  9795. }
  9796. return ret;
  9797. #endif
  9798. #ifdef TARGET_NR_getdents
  9799. case TARGET_NR_getdents:
  9800. return do_getdents(arg1, arg2, arg3);
  9801. #endif /* TARGET_NR_getdents */
  9802. #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
  9803. case TARGET_NR_getdents64:
  9804. return do_getdents64(arg1, arg2, arg3);
  9805. #endif /* TARGET_NR_getdents64 */
  9806. #if defined(TARGET_NR__newselect)
  9807. case TARGET_NR__newselect:
  9808. return do_select(arg1, arg2, arg3, arg4, arg5);
  9809. #endif
  9810. #ifdef TARGET_NR_poll
  9811. case TARGET_NR_poll:
  9812. return do_ppoll(arg1, arg2, arg3, arg4, arg5, false, false);
  9813. #endif
  9814. #ifdef TARGET_NR_ppoll
  9815. case TARGET_NR_ppoll:
  9816. return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, false);
  9817. #endif
  9818. #ifdef TARGET_NR_ppoll_time64
  9819. case TARGET_NR_ppoll_time64:
  9820. return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, true);
  9821. #endif
  9822. case TARGET_NR_flock:
  9823. /* NOTE: the flock constant seems to be the same for every
  9824. Linux platform */
  9825. return get_errno(safe_flock(arg1, arg2));
  9826. case TARGET_NR_readv:
  9827. {
  9828. struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
  9829. if (vec != NULL) {
  9830. ret = get_errno(safe_readv(arg1, vec, arg3));
  9831. unlock_iovec(vec, arg2, arg3, 1);
  9832. } else {
  9833. ret = -host_to_target_errno(errno);
  9834. }
  9835. }
  9836. return ret;
  9837. case TARGET_NR_writev:
  9838. {
  9839. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  9840. if (vec != NULL) {
  9841. ret = get_errno(safe_writev(arg1, vec, arg3));
  9842. unlock_iovec(vec, arg2, arg3, 0);
  9843. } else {
  9844. ret = -host_to_target_errno(errno);
  9845. }
  9846. }
  9847. return ret;
  9848. #if defined(TARGET_NR_preadv)
  9849. case TARGET_NR_preadv:
  9850. {
  9851. struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
  9852. if (vec != NULL) {
  9853. unsigned long low, high;
  9854. target_to_host_low_high(arg4, arg5, &low, &high);
  9855. ret = get_errno(safe_preadv(arg1, vec, arg3, low, high));
  9856. unlock_iovec(vec, arg2, arg3, 1);
  9857. } else {
  9858. ret = -host_to_target_errno(errno);
  9859. }
  9860. }
  9861. return ret;
  9862. #endif
  9863. #if defined(TARGET_NR_pwritev)
  9864. case TARGET_NR_pwritev:
  9865. {
  9866. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  9867. if (vec != NULL) {
  9868. unsigned long low, high;
  9869. target_to_host_low_high(arg4, arg5, &low, &high);
  9870. ret = get_errno(safe_pwritev(arg1, vec, arg3, low, high));
  9871. unlock_iovec(vec, arg2, arg3, 0);
  9872. } else {
  9873. ret = -host_to_target_errno(errno);
  9874. }
  9875. }
  9876. return ret;
  9877. #endif
  9878. case TARGET_NR_getsid:
  9879. return get_errno(getsid(arg1));
  9880. #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
  9881. case TARGET_NR_fdatasync:
  9882. return get_errno(fdatasync(arg1));
  9883. #endif
  9884. case TARGET_NR_sched_getaffinity:
  9885. {
  9886. unsigned int mask_size;
  9887. unsigned long *mask;
  9888. /*
  9889. * sched_getaffinity needs multiples of ulong, so need to take
  9890. * care of mismatches between target ulong and host ulong sizes.
  9891. */
  9892. if (arg2 & (sizeof(abi_ulong) - 1)) {
  9893. return -TARGET_EINVAL;
  9894. }
  9895. mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
  9896. mask = alloca(mask_size);
  9897. memset(mask, 0, mask_size);
  9898. ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
  9899. if (!is_error(ret)) {
  9900. if (ret > arg2) {
  9901. /* More data returned than the caller's buffer will fit.
  9902. * This only happens if sizeof(abi_long) < sizeof(long)
  9903. * and the caller passed us a buffer holding an odd number
  9904. * of abi_longs. If the host kernel is actually using the
  9905. * extra 4 bytes then fail EINVAL; otherwise we can just
  9906. * ignore them and only copy the interesting part.
  9907. */
  9908. int numcpus = sysconf(_SC_NPROCESSORS_CONF);
  9909. if (numcpus > arg2 * 8) {
  9910. return -TARGET_EINVAL;
  9911. }
  9912. ret = arg2;
  9913. }
  9914. if (host_to_target_cpu_mask(mask, mask_size, arg3, ret)) {
  9915. return -TARGET_EFAULT;
  9916. }
  9917. }
  9918. }
  9919. return ret;
  9920. case TARGET_NR_sched_setaffinity:
  9921. {
  9922. unsigned int mask_size;
  9923. unsigned long *mask;
  9924. /*
  9925. * sched_setaffinity needs multiples of ulong, so need to take
  9926. * care of mismatches between target ulong and host ulong sizes.
  9927. */
  9928. if (arg2 & (sizeof(abi_ulong) - 1)) {
  9929. return -TARGET_EINVAL;
  9930. }
  9931. mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
  9932. mask = alloca(mask_size);
  9933. ret = target_to_host_cpu_mask(mask, mask_size, arg3, arg2);
  9934. if (ret) {
  9935. return ret;
  9936. }
  9937. return get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
  9938. }
  9939. case TARGET_NR_getcpu:
  9940. {
  9941. unsigned cpu, node;
  9942. ret = get_errno(sys_getcpu(arg1 ? &cpu : NULL,
  9943. arg2 ? &node : NULL,
  9944. NULL));
  9945. if (is_error(ret)) {
  9946. return ret;
  9947. }
  9948. if (arg1 && put_user_u32(cpu, arg1)) {
  9949. return -TARGET_EFAULT;
  9950. }
  9951. if (arg2 && put_user_u32(node, arg2)) {
  9952. return -TARGET_EFAULT;
  9953. }
  9954. }
  9955. return ret;
  9956. case TARGET_NR_sched_setparam:
  9957. {
  9958. struct target_sched_param *target_schp;
  9959. struct sched_param schp;
  9960. if (arg2 == 0) {
  9961. return -TARGET_EINVAL;
  9962. }
  9963. if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1)) {
  9964. return -TARGET_EFAULT;
  9965. }
  9966. schp.sched_priority = tswap32(target_schp->sched_priority);
  9967. unlock_user_struct(target_schp, arg2, 0);
  9968. return get_errno(sys_sched_setparam(arg1, &schp));
  9969. }
  9970. case TARGET_NR_sched_getparam:
  9971. {
  9972. struct target_sched_param *target_schp;
  9973. struct sched_param schp;
  9974. if (arg2 == 0) {
  9975. return -TARGET_EINVAL;
  9976. }
  9977. ret = get_errno(sys_sched_getparam(arg1, &schp));
  9978. if (!is_error(ret)) {
  9979. if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0)) {
  9980. return -TARGET_EFAULT;
  9981. }
  9982. target_schp->sched_priority = tswap32(schp.sched_priority);
  9983. unlock_user_struct(target_schp, arg2, 1);
  9984. }
  9985. }
  9986. return ret;
  9987. case TARGET_NR_sched_setscheduler:
  9988. {
  9989. struct target_sched_param *target_schp;
  9990. struct sched_param schp;
  9991. if (arg3 == 0) {
  9992. return -TARGET_EINVAL;
  9993. }
  9994. if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1)) {
  9995. return -TARGET_EFAULT;
  9996. }
  9997. schp.sched_priority = tswap32(target_schp->sched_priority);
  9998. unlock_user_struct(target_schp, arg3, 0);
  9999. return get_errno(sys_sched_setscheduler(arg1, arg2, &schp));
  10000. }
  10001. case TARGET_NR_sched_getscheduler:
  10002. return get_errno(sys_sched_getscheduler(arg1));
  10003. case TARGET_NR_sched_getattr:
  10004. {
  10005. struct target_sched_attr *target_scha;
  10006. struct sched_attr scha;
  10007. if (arg2 == 0) {
  10008. return -TARGET_EINVAL;
  10009. }
  10010. if (arg3 > sizeof(scha)) {
  10011. arg3 = sizeof(scha);
  10012. }
  10013. ret = get_errno(sys_sched_getattr(arg1, &scha, arg3, arg4));
  10014. if (!is_error(ret)) {
  10015. target_scha = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  10016. if (!target_scha) {
  10017. return -TARGET_EFAULT;
  10018. }
  10019. target_scha->size = tswap32(scha.size);
  10020. target_scha->sched_policy = tswap32(scha.sched_policy);
  10021. target_scha->sched_flags = tswap64(scha.sched_flags);
  10022. target_scha->sched_nice = tswap32(scha.sched_nice);
  10023. target_scha->sched_priority = tswap32(scha.sched_priority);
  10024. target_scha->sched_runtime = tswap64(scha.sched_runtime);
  10025. target_scha->sched_deadline = tswap64(scha.sched_deadline);
  10026. target_scha->sched_period = tswap64(scha.sched_period);
  10027. if (scha.size > offsetof(struct sched_attr, sched_util_min)) {
  10028. target_scha->sched_util_min = tswap32(scha.sched_util_min);
  10029. target_scha->sched_util_max = tswap32(scha.sched_util_max);
  10030. }
  10031. unlock_user(target_scha, arg2, arg3);
  10032. }
  10033. return ret;
  10034. }
  10035. case TARGET_NR_sched_setattr:
  10036. {
  10037. struct target_sched_attr *target_scha;
  10038. struct sched_attr scha;
  10039. uint32_t size;
  10040. int zeroed;
  10041. if (arg2 == 0) {
  10042. return -TARGET_EINVAL;
  10043. }
  10044. if (get_user_u32(size, arg2)) {
  10045. return -TARGET_EFAULT;
  10046. }
  10047. if (!size) {
  10048. size = offsetof(struct target_sched_attr, sched_util_min);
  10049. }
  10050. if (size < offsetof(struct target_sched_attr, sched_util_min)) {
  10051. if (put_user_u32(sizeof(struct target_sched_attr), arg2)) {
  10052. return -TARGET_EFAULT;
  10053. }
  10054. return -TARGET_E2BIG;
  10055. }
  10056. zeroed = check_zeroed_user(arg2, sizeof(struct target_sched_attr), size);
  10057. if (zeroed < 0) {
  10058. return zeroed;
  10059. } else if (zeroed == 0) {
  10060. if (put_user_u32(sizeof(struct target_sched_attr), arg2)) {
  10061. return -TARGET_EFAULT;
  10062. }
  10063. return -TARGET_E2BIG;
  10064. }
  10065. if (size > sizeof(struct target_sched_attr)) {
  10066. size = sizeof(struct target_sched_attr);
  10067. }
  10068. target_scha = lock_user(VERIFY_READ, arg2, size, 1);
  10069. if (!target_scha) {
  10070. return -TARGET_EFAULT;
  10071. }
  10072. scha.size = size;
  10073. scha.sched_policy = tswap32(target_scha->sched_policy);
  10074. scha.sched_flags = tswap64(target_scha->sched_flags);
  10075. scha.sched_nice = tswap32(target_scha->sched_nice);
  10076. scha.sched_priority = tswap32(target_scha->sched_priority);
  10077. scha.sched_runtime = tswap64(target_scha->sched_runtime);
  10078. scha.sched_deadline = tswap64(target_scha->sched_deadline);
  10079. scha.sched_period = tswap64(target_scha->sched_period);
  10080. if (size > offsetof(struct target_sched_attr, sched_util_min)) {
  10081. scha.sched_util_min = tswap32(target_scha->sched_util_min);
  10082. scha.sched_util_max = tswap32(target_scha->sched_util_max);
  10083. }
  10084. unlock_user(target_scha, arg2, 0);
  10085. return get_errno(sys_sched_setattr(arg1, &scha, arg3));
  10086. }
  10087. case TARGET_NR_sched_yield:
  10088. return get_errno(sched_yield());
  10089. case TARGET_NR_sched_get_priority_max:
  10090. return get_errno(sched_get_priority_max(arg1));
  10091. case TARGET_NR_sched_get_priority_min:
  10092. return get_errno(sched_get_priority_min(arg1));
  10093. #ifdef TARGET_NR_sched_rr_get_interval
  10094. case TARGET_NR_sched_rr_get_interval:
  10095. {
  10096. struct timespec ts;
  10097. ret = get_errno(sched_rr_get_interval(arg1, &ts));
  10098. if (!is_error(ret)) {
  10099. ret = host_to_target_timespec(arg2, &ts);
  10100. }
  10101. }
  10102. return ret;
  10103. #endif
  10104. #ifdef TARGET_NR_sched_rr_get_interval_time64
  10105. case TARGET_NR_sched_rr_get_interval_time64:
  10106. {
  10107. struct timespec ts;
  10108. ret = get_errno(sched_rr_get_interval(arg1, &ts));
  10109. if (!is_error(ret)) {
  10110. ret = host_to_target_timespec64(arg2, &ts);
  10111. }
  10112. }
  10113. return ret;
  10114. #endif
  10115. #if defined(TARGET_NR_nanosleep)
  10116. case TARGET_NR_nanosleep:
  10117. {
  10118. struct timespec req, rem;
  10119. target_to_host_timespec(&req, arg1);
  10120. ret = get_errno(safe_nanosleep(&req, &rem));
  10121. if (is_error(ret) && arg2) {
  10122. host_to_target_timespec(arg2, &rem);
  10123. }
  10124. }
  10125. return ret;
  10126. #endif
  10127. case TARGET_NR_prctl:
  10128. return do_prctl(cpu_env, arg1, arg2, arg3, arg4, arg5);
  10129. break;
  10130. #ifdef TARGET_NR_arch_prctl
  10131. case TARGET_NR_arch_prctl:
  10132. return do_arch_prctl(cpu_env, arg1, arg2);
  10133. #endif
  10134. #ifdef TARGET_NR_pread64
  10135. case TARGET_NR_pread64:
  10136. if (regpairs_aligned(cpu_env, num)) {
  10137. arg4 = arg5;
  10138. arg5 = arg6;
  10139. }
  10140. if (arg2 == 0 && arg3 == 0) {
  10141. /* Special-case NULL buffer and zero length, which should succeed */
  10142. p = 0;
  10143. } else {
  10144. p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  10145. if (!p) {
  10146. return -TARGET_EFAULT;
  10147. }
  10148. }
  10149. ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
  10150. unlock_user(p, arg2, ret);
  10151. return ret;
  10152. case TARGET_NR_pwrite64:
  10153. if (regpairs_aligned(cpu_env, num)) {
  10154. arg4 = arg5;
  10155. arg5 = arg6;
  10156. }
  10157. if (arg2 == 0 && arg3 == 0) {
  10158. /* Special-case NULL buffer and zero length, which should succeed */
  10159. p = 0;
  10160. } else {
  10161. p = lock_user(VERIFY_READ, arg2, arg3, 1);
  10162. if (!p) {
  10163. return -TARGET_EFAULT;
  10164. }
  10165. }
  10166. ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
  10167. unlock_user(p, arg2, 0);
  10168. return ret;
  10169. #endif
  10170. case TARGET_NR_getcwd:
  10171. if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
  10172. return -TARGET_EFAULT;
  10173. ret = get_errno(sys_getcwd1(p, arg2));
  10174. unlock_user(p, arg1, ret);
  10175. return ret;
  10176. case TARGET_NR_capget:
  10177. case TARGET_NR_capset:
  10178. {
  10179. struct target_user_cap_header *target_header;
  10180. struct target_user_cap_data *target_data = NULL;
  10181. struct __user_cap_header_struct header;
  10182. struct __user_cap_data_struct data[2];
  10183. struct __user_cap_data_struct *dataptr = NULL;
  10184. int i, target_datalen;
  10185. int data_items = 1;
  10186. if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) {
  10187. return -TARGET_EFAULT;
  10188. }
  10189. header.version = tswap32(target_header->version);
  10190. header.pid = tswap32(target_header->pid);
  10191. if (header.version != _LINUX_CAPABILITY_VERSION) {
  10192. /* Version 2 and up takes pointer to two user_data structs */
  10193. data_items = 2;
  10194. }
  10195. target_datalen = sizeof(*target_data) * data_items;
  10196. if (arg2) {
  10197. if (num == TARGET_NR_capget) {
  10198. target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0);
  10199. } else {
  10200. target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1);
  10201. }
  10202. if (!target_data) {
  10203. unlock_user_struct(target_header, arg1, 0);
  10204. return -TARGET_EFAULT;
  10205. }
  10206. if (num == TARGET_NR_capset) {
  10207. for (i = 0; i < data_items; i++) {
  10208. data[i].effective = tswap32(target_data[i].effective);
  10209. data[i].permitted = tswap32(target_data[i].permitted);
  10210. data[i].inheritable = tswap32(target_data[i].inheritable);
  10211. }
  10212. }
  10213. dataptr = data;
  10214. }
  10215. if (num == TARGET_NR_capget) {
  10216. ret = get_errno(capget(&header, dataptr));
  10217. } else {
  10218. ret = get_errno(capset(&header, dataptr));
  10219. }
  10220. /* The kernel always updates version for both capget and capset */
  10221. target_header->version = tswap32(header.version);
  10222. unlock_user_struct(target_header, arg1, 1);
  10223. if (arg2) {
  10224. if (num == TARGET_NR_capget) {
  10225. for (i = 0; i < data_items; i++) {
  10226. target_data[i].effective = tswap32(data[i].effective);
  10227. target_data[i].permitted = tswap32(data[i].permitted);
  10228. target_data[i].inheritable = tswap32(data[i].inheritable);
  10229. }
  10230. unlock_user(target_data, arg2, target_datalen);
  10231. } else {
  10232. unlock_user(target_data, arg2, 0);
  10233. }
  10234. }
  10235. return ret;
  10236. }
  10237. case TARGET_NR_sigaltstack:
  10238. return do_sigaltstack(arg1, arg2, cpu_env);
  10239. #ifdef CONFIG_SENDFILE
  10240. #ifdef TARGET_NR_sendfile
  10241. case TARGET_NR_sendfile:
  10242. {
  10243. off_t *offp = NULL;
  10244. off_t off;
  10245. if (arg3) {
  10246. ret = get_user_sal(off, arg3);
  10247. if (is_error(ret)) {
  10248. return ret;
  10249. }
  10250. offp = &off;
  10251. }
  10252. ret = get_errno(sendfile(arg1, arg2, offp, arg4));
  10253. if (!is_error(ret) && arg3) {
  10254. abi_long ret2 = put_user_sal(off, arg3);
  10255. if (is_error(ret2)) {
  10256. ret = ret2;
  10257. }
  10258. }
  10259. return ret;
  10260. }
  10261. #endif
  10262. #ifdef TARGET_NR_sendfile64
  10263. case TARGET_NR_sendfile64:
  10264. {
  10265. off_t *offp = NULL;
  10266. off_t off;
  10267. if (arg3) {
  10268. ret = get_user_s64(off, arg3);
  10269. if (is_error(ret)) {
  10270. return ret;
  10271. }
  10272. offp = &off;
  10273. }
  10274. ret = get_errno(sendfile(arg1, arg2, offp, arg4));
  10275. if (!is_error(ret) && arg3) {
  10276. abi_long ret2 = put_user_s64(off, arg3);
  10277. if (is_error(ret2)) {
  10278. ret = ret2;
  10279. }
  10280. }
  10281. return ret;
  10282. }
  10283. #endif
  10284. #endif
  10285. #ifdef TARGET_NR_vfork
  10286. case TARGET_NR_vfork:
  10287. return get_errno(do_fork(cpu_env,
  10288. CLONE_VFORK | CLONE_VM | TARGET_SIGCHLD,
  10289. 0, 0, 0, 0));
  10290. #endif
  10291. #ifdef TARGET_NR_ugetrlimit
  10292. case TARGET_NR_ugetrlimit:
  10293. {
  10294. struct rlimit rlim;
  10295. int resource = target_to_host_resource(arg1);
  10296. ret = get_errno(getrlimit(resource, &rlim));
  10297. if (!is_error(ret)) {
  10298. struct target_rlimit *target_rlim;
  10299. if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
  10300. return -TARGET_EFAULT;
  10301. target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
  10302. target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
  10303. unlock_user_struct(target_rlim, arg2, 1);
  10304. }
  10305. return ret;
  10306. }
  10307. #endif
  10308. #ifdef TARGET_NR_truncate64
  10309. case TARGET_NR_truncate64:
  10310. if (!(p = lock_user_string(arg1)))
  10311. return -TARGET_EFAULT;
  10312. ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
  10313. unlock_user(p, arg1, 0);
  10314. return ret;
  10315. #endif
  10316. #ifdef TARGET_NR_ftruncate64
  10317. case TARGET_NR_ftruncate64:
  10318. return target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
  10319. #endif
  10320. #ifdef TARGET_NR_stat64
  10321. case TARGET_NR_stat64:
  10322. if (!(p = lock_user_string(arg1))) {
  10323. return -TARGET_EFAULT;
  10324. }
  10325. ret = get_errno(stat(path(p), &st));
  10326. unlock_user(p, arg1, 0);
  10327. if (!is_error(ret))
  10328. ret = host_to_target_stat64(cpu_env, arg2, &st);
  10329. return ret;
  10330. #endif
  10331. #ifdef TARGET_NR_lstat64
  10332. case TARGET_NR_lstat64:
  10333. if (!(p = lock_user_string(arg1))) {
  10334. return -TARGET_EFAULT;
  10335. }
  10336. ret = get_errno(lstat(path(p), &st));
  10337. unlock_user(p, arg1, 0);
  10338. if (!is_error(ret))
  10339. ret = host_to_target_stat64(cpu_env, arg2, &st);
  10340. return ret;
  10341. #endif
  10342. #ifdef TARGET_NR_fstat64
  10343. case TARGET_NR_fstat64:
  10344. ret = get_errno(fstat(arg1, &st));
  10345. if (!is_error(ret))
  10346. ret = host_to_target_stat64(cpu_env, arg2, &st);
  10347. return ret;
  10348. #endif
  10349. #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
  10350. #ifdef TARGET_NR_fstatat64
  10351. case TARGET_NR_fstatat64:
  10352. #endif
  10353. #ifdef TARGET_NR_newfstatat
  10354. case TARGET_NR_newfstatat:
  10355. #endif
  10356. if (!(p = lock_user_string(arg2))) {
  10357. return -TARGET_EFAULT;
  10358. }
  10359. ret = get_errno(fstatat(arg1, path(p), &st, arg4));
  10360. unlock_user(p, arg2, 0);
  10361. if (!is_error(ret))
  10362. ret = host_to_target_stat64(cpu_env, arg3, &st);
  10363. return ret;
  10364. #endif
  10365. #if defined(TARGET_NR_statx)
  10366. case TARGET_NR_statx:
  10367. {
  10368. struct target_statx *target_stx;
  10369. int dirfd = arg1;
  10370. int flags = arg3;
  10371. p = lock_user_string(arg2);
  10372. if (p == NULL) {
  10373. return -TARGET_EFAULT;
  10374. }
  10375. #if defined(__NR_statx)
  10376. {
  10377. /*
  10378. * It is assumed that struct statx is architecture independent.
  10379. */
  10380. struct target_statx host_stx;
  10381. int mask = arg4;
  10382. ret = get_errno(sys_statx(dirfd, p, flags, mask, &host_stx));
  10383. if (!is_error(ret)) {
  10384. if (host_to_target_statx(&host_stx, arg5) != 0) {
  10385. unlock_user(p, arg2, 0);
  10386. return -TARGET_EFAULT;
  10387. }
  10388. }
  10389. if (ret != -TARGET_ENOSYS) {
  10390. unlock_user(p, arg2, 0);
  10391. return ret;
  10392. }
  10393. }
  10394. #endif
  10395. ret = get_errno(fstatat(dirfd, path(p), &st, flags));
  10396. unlock_user(p, arg2, 0);
  10397. if (!is_error(ret)) {
  10398. if (!lock_user_struct(VERIFY_WRITE, target_stx, arg5, 0)) {
  10399. return -TARGET_EFAULT;
  10400. }
  10401. memset(target_stx, 0, sizeof(*target_stx));
  10402. __put_user(major(st.st_dev), &target_stx->stx_dev_major);
  10403. __put_user(minor(st.st_dev), &target_stx->stx_dev_minor);
  10404. __put_user(st.st_ino, &target_stx->stx_ino);
  10405. __put_user(st.st_mode, &target_stx->stx_mode);
  10406. __put_user(st.st_uid, &target_stx->stx_uid);
  10407. __put_user(st.st_gid, &target_stx->stx_gid);
  10408. __put_user(st.st_nlink, &target_stx->stx_nlink);
  10409. __put_user(major(st.st_rdev), &target_stx->stx_rdev_major);
  10410. __put_user(minor(st.st_rdev), &target_stx->stx_rdev_minor);
  10411. __put_user(st.st_size, &target_stx->stx_size);
  10412. __put_user(st.st_blksize, &target_stx->stx_blksize);
  10413. __put_user(st.st_blocks, &target_stx->stx_blocks);
  10414. __put_user(st.st_atime, &target_stx->stx_atime.tv_sec);
  10415. __put_user(st.st_mtime, &target_stx->stx_mtime.tv_sec);
  10416. __put_user(st.st_ctime, &target_stx->stx_ctime.tv_sec);
  10417. unlock_user_struct(target_stx, arg5, 1);
  10418. }
  10419. }
  10420. return ret;
  10421. #endif
  10422. #ifdef TARGET_NR_lchown
  10423. case TARGET_NR_lchown:
  10424. if (!(p = lock_user_string(arg1)))
  10425. return -TARGET_EFAULT;
  10426. ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
  10427. unlock_user(p, arg1, 0);
  10428. return ret;
  10429. #endif
  10430. #ifdef TARGET_NR_getuid
  10431. case TARGET_NR_getuid:
  10432. return get_errno(high2lowuid(getuid()));
  10433. #endif
  10434. #ifdef TARGET_NR_getgid
  10435. case TARGET_NR_getgid:
  10436. return get_errno(high2lowgid(getgid()));
  10437. #endif
  10438. #ifdef TARGET_NR_geteuid
  10439. case TARGET_NR_geteuid:
  10440. return get_errno(high2lowuid(geteuid()));
  10441. #endif
  10442. #ifdef TARGET_NR_getegid
  10443. case TARGET_NR_getegid:
  10444. return get_errno(high2lowgid(getegid()));
  10445. #endif
  10446. case TARGET_NR_setreuid:
  10447. return get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
  10448. case TARGET_NR_setregid:
  10449. return get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
  10450. case TARGET_NR_getgroups:
  10451. {
  10452. int gidsetsize = arg1;
  10453. target_id *target_grouplist;
  10454. gid_t *grouplist;
  10455. int i;
  10456. grouplist = alloca(gidsetsize * sizeof(gid_t));
  10457. ret = get_errno(getgroups(gidsetsize, grouplist));
  10458. if (gidsetsize == 0)
  10459. return ret;
  10460. if (!is_error(ret)) {
  10461. target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * sizeof(target_id), 0);
  10462. if (!target_grouplist)
  10463. return -TARGET_EFAULT;
  10464. for(i = 0;i < ret; i++)
  10465. target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
  10466. unlock_user(target_grouplist, arg2, gidsetsize * sizeof(target_id));
  10467. }
  10468. }
  10469. return ret;
  10470. case TARGET_NR_setgroups:
  10471. {
  10472. int gidsetsize = arg1;
  10473. target_id *target_grouplist;
  10474. gid_t *grouplist = NULL;
  10475. int i;
  10476. if (gidsetsize) {
  10477. grouplist = alloca(gidsetsize * sizeof(gid_t));
  10478. target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * sizeof(target_id), 1);
  10479. if (!target_grouplist) {
  10480. return -TARGET_EFAULT;
  10481. }
  10482. for (i = 0; i < gidsetsize; i++) {
  10483. grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
  10484. }
  10485. unlock_user(target_grouplist, arg2, 0);
  10486. }
  10487. return get_errno(setgroups(gidsetsize, grouplist));
  10488. }
  10489. case TARGET_NR_fchown:
  10490. return get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
  10491. #if defined(TARGET_NR_fchownat)
  10492. case TARGET_NR_fchownat:
  10493. if (!(p = lock_user_string(arg2)))
  10494. return -TARGET_EFAULT;
  10495. ret = get_errno(fchownat(arg1, p, low2highuid(arg3),
  10496. low2highgid(arg4), arg5));
  10497. unlock_user(p, arg2, 0);
  10498. return ret;
  10499. #endif
  10500. #ifdef TARGET_NR_setresuid
  10501. case TARGET_NR_setresuid:
  10502. return get_errno(sys_setresuid(low2highuid(arg1),
  10503. low2highuid(arg2),
  10504. low2highuid(arg3)));
  10505. #endif
  10506. #ifdef TARGET_NR_getresuid
  10507. case TARGET_NR_getresuid:
  10508. {
  10509. uid_t ruid, euid, suid;
  10510. ret = get_errno(getresuid(&ruid, &euid, &suid));
  10511. if (!is_error(ret)) {
  10512. if (put_user_id(high2lowuid(ruid), arg1)
  10513. || put_user_id(high2lowuid(euid), arg2)
  10514. || put_user_id(high2lowuid(suid), arg3))
  10515. return -TARGET_EFAULT;
  10516. }
  10517. }
  10518. return ret;
  10519. #endif
  10520. #ifdef TARGET_NR_getresgid
  10521. case TARGET_NR_setresgid:
  10522. return get_errno(sys_setresgid(low2highgid(arg1),
  10523. low2highgid(arg2),
  10524. low2highgid(arg3)));
  10525. #endif
  10526. #ifdef TARGET_NR_getresgid
  10527. case TARGET_NR_getresgid:
  10528. {
  10529. gid_t rgid, egid, sgid;
  10530. ret = get_errno(getresgid(&rgid, &egid, &sgid));
  10531. if (!is_error(ret)) {
  10532. if (put_user_id(high2lowgid(rgid), arg1)
  10533. || put_user_id(high2lowgid(egid), arg2)
  10534. || put_user_id(high2lowgid(sgid), arg3))
  10535. return -TARGET_EFAULT;
  10536. }
  10537. }
  10538. return ret;
  10539. #endif
  10540. #ifdef TARGET_NR_chown
  10541. case TARGET_NR_chown:
  10542. if (!(p = lock_user_string(arg1)))
  10543. return -TARGET_EFAULT;
  10544. ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
  10545. unlock_user(p, arg1, 0);
  10546. return ret;
  10547. #endif
  10548. case TARGET_NR_setuid:
  10549. return get_errno(sys_setuid(low2highuid(arg1)));
  10550. case TARGET_NR_setgid:
  10551. return get_errno(sys_setgid(low2highgid(arg1)));
  10552. case TARGET_NR_setfsuid:
  10553. return get_errno(setfsuid(arg1));
  10554. case TARGET_NR_setfsgid:
  10555. return get_errno(setfsgid(arg1));
  10556. #ifdef TARGET_NR_lchown32
  10557. case TARGET_NR_lchown32:
  10558. if (!(p = lock_user_string(arg1)))
  10559. return -TARGET_EFAULT;
  10560. ret = get_errno(lchown(p, arg2, arg3));
  10561. unlock_user(p, arg1, 0);
  10562. return ret;
  10563. #endif
  10564. #ifdef TARGET_NR_getuid32
  10565. case TARGET_NR_getuid32:
  10566. return get_errno(getuid());
  10567. #endif
  10568. #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
  10569. /* Alpha specific */
  10570. case TARGET_NR_getxuid:
  10571. {
  10572. uid_t euid;
  10573. euid=geteuid();
  10574. cpu_env->ir[IR_A4]=euid;
  10575. }
  10576. return get_errno(getuid());
  10577. #endif
  10578. #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
  10579. /* Alpha specific */
  10580. case TARGET_NR_getxgid:
  10581. {
  10582. uid_t egid;
  10583. egid=getegid();
  10584. cpu_env->ir[IR_A4]=egid;
  10585. }
  10586. return get_errno(getgid());
  10587. #endif
  10588. #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
  10589. /* Alpha specific */
  10590. case TARGET_NR_osf_getsysinfo:
  10591. ret = -TARGET_EOPNOTSUPP;
  10592. switch (arg1) {
  10593. case TARGET_GSI_IEEE_FP_CONTROL:
  10594. {
  10595. uint64_t fpcr = cpu_alpha_load_fpcr(cpu_env);
  10596. uint64_t swcr = cpu_env->swcr;
  10597. swcr &= ~SWCR_STATUS_MASK;
  10598. swcr |= (fpcr >> 35) & SWCR_STATUS_MASK;
  10599. if (put_user_u64 (swcr, arg2))
  10600. return -TARGET_EFAULT;
  10601. ret = 0;
  10602. }
  10603. break;
  10604. /* case GSI_IEEE_STATE_AT_SIGNAL:
  10605. -- Not implemented in linux kernel.
  10606. case GSI_UACPROC:
  10607. -- Retrieves current unaligned access state; not much used.
  10608. case GSI_PROC_TYPE:
  10609. -- Retrieves implver information; surely not used.
  10610. case GSI_GET_HWRPB:
  10611. -- Grabs a copy of the HWRPB; surely not used.
  10612. */
  10613. }
  10614. return ret;
  10615. #endif
  10616. #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
  10617. /* Alpha specific */
  10618. case TARGET_NR_osf_setsysinfo:
  10619. ret = -TARGET_EOPNOTSUPP;
  10620. switch (arg1) {
  10621. case TARGET_SSI_IEEE_FP_CONTROL:
  10622. {
  10623. uint64_t swcr, fpcr;
  10624. if (get_user_u64 (swcr, arg2)) {
  10625. return -TARGET_EFAULT;
  10626. }
  10627. /*
  10628. * The kernel calls swcr_update_status to update the
  10629. * status bits from the fpcr at every point that it
  10630. * could be queried. Therefore, we store the status
  10631. * bits only in FPCR.
  10632. */
  10633. cpu_env->swcr = swcr & (SWCR_TRAP_ENABLE_MASK | SWCR_MAP_MASK);
  10634. fpcr = cpu_alpha_load_fpcr(cpu_env);
  10635. fpcr &= ((uint64_t)FPCR_DYN_MASK << 32);
  10636. fpcr |= alpha_ieee_swcr_to_fpcr(swcr);
  10637. cpu_alpha_store_fpcr(cpu_env, fpcr);
  10638. ret = 0;
  10639. }
  10640. break;
  10641. case TARGET_SSI_IEEE_RAISE_EXCEPTION:
  10642. {
  10643. uint64_t exc, fpcr, fex;
  10644. if (get_user_u64(exc, arg2)) {
  10645. return -TARGET_EFAULT;
  10646. }
  10647. exc &= SWCR_STATUS_MASK;
  10648. fpcr = cpu_alpha_load_fpcr(cpu_env);
  10649. /* Old exceptions are not signaled. */
  10650. fex = alpha_ieee_fpcr_to_swcr(fpcr);
  10651. fex = exc & ~fex;
  10652. fex >>= SWCR_STATUS_TO_EXCSUM_SHIFT;
  10653. fex &= (cpu_env)->swcr;
  10654. /* Update the hardware fpcr. */
  10655. fpcr |= alpha_ieee_swcr_to_fpcr(exc);
  10656. cpu_alpha_store_fpcr(cpu_env, fpcr);
  10657. if (fex) {
  10658. int si_code = TARGET_FPE_FLTUNK;
  10659. target_siginfo_t info;
  10660. if (fex & SWCR_TRAP_ENABLE_DNO) {
  10661. si_code = TARGET_FPE_FLTUND;
  10662. }
  10663. if (fex & SWCR_TRAP_ENABLE_INE) {
  10664. si_code = TARGET_FPE_FLTRES;
  10665. }
  10666. if (fex & SWCR_TRAP_ENABLE_UNF) {
  10667. si_code = TARGET_FPE_FLTUND;
  10668. }
  10669. if (fex & SWCR_TRAP_ENABLE_OVF) {
  10670. si_code = TARGET_FPE_FLTOVF;
  10671. }
  10672. if (fex & SWCR_TRAP_ENABLE_DZE) {
  10673. si_code = TARGET_FPE_FLTDIV;
  10674. }
  10675. if (fex & SWCR_TRAP_ENABLE_INV) {
  10676. si_code = TARGET_FPE_FLTINV;
  10677. }
  10678. info.si_signo = SIGFPE;
  10679. info.si_errno = 0;
  10680. info.si_code = si_code;
  10681. info._sifields._sigfault._addr = (cpu_env)->pc;
  10682. queue_signal(cpu_env, info.si_signo,
  10683. QEMU_SI_FAULT, &info);
  10684. }
  10685. ret = 0;
  10686. }
  10687. break;
  10688. /* case SSI_NVPAIRS:
  10689. -- Used with SSIN_UACPROC to enable unaligned accesses.
  10690. case SSI_IEEE_STATE_AT_SIGNAL:
  10691. case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
  10692. -- Not implemented in linux kernel
  10693. */
  10694. }
  10695. return ret;
  10696. #endif
  10697. #ifdef TARGET_NR_osf_sigprocmask
  10698. /* Alpha specific. */
  10699. case TARGET_NR_osf_sigprocmask:
  10700. {
  10701. abi_ulong mask;
  10702. int how;
  10703. sigset_t set, oldset;
  10704. switch(arg1) {
  10705. case TARGET_SIG_BLOCK:
  10706. how = SIG_BLOCK;
  10707. break;
  10708. case TARGET_SIG_UNBLOCK:
  10709. how = SIG_UNBLOCK;
  10710. break;
  10711. case TARGET_SIG_SETMASK:
  10712. how = SIG_SETMASK;
  10713. break;
  10714. default:
  10715. return -TARGET_EINVAL;
  10716. }
  10717. mask = arg2;
  10718. target_to_host_old_sigset(&set, &mask);
  10719. ret = do_sigprocmask(how, &set, &oldset);
  10720. if (!ret) {
  10721. host_to_target_old_sigset(&mask, &oldset);
  10722. ret = mask;
  10723. }
  10724. }
  10725. return ret;
  10726. #endif
  10727. #ifdef TARGET_NR_getgid32
  10728. case TARGET_NR_getgid32:
  10729. return get_errno(getgid());
  10730. #endif
  10731. #ifdef TARGET_NR_geteuid32
  10732. case TARGET_NR_geteuid32:
  10733. return get_errno(geteuid());
  10734. #endif
  10735. #ifdef TARGET_NR_getegid32
  10736. case TARGET_NR_getegid32:
  10737. return get_errno(getegid());
  10738. #endif
  10739. #ifdef TARGET_NR_setreuid32
  10740. case TARGET_NR_setreuid32:
  10741. return get_errno(setreuid(arg1, arg2));
  10742. #endif
  10743. #ifdef TARGET_NR_setregid32
  10744. case TARGET_NR_setregid32:
  10745. return get_errno(setregid(arg1, arg2));
  10746. #endif
  10747. #ifdef TARGET_NR_getgroups32
  10748. case TARGET_NR_getgroups32:
  10749. {
  10750. int gidsetsize = arg1;
  10751. uint32_t *target_grouplist;
  10752. gid_t *grouplist;
  10753. int i;
  10754. grouplist = alloca(gidsetsize * sizeof(gid_t));
  10755. ret = get_errno(getgroups(gidsetsize, grouplist));
  10756. if (gidsetsize == 0)
  10757. return ret;
  10758. if (!is_error(ret)) {
  10759. target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
  10760. if (!target_grouplist) {
  10761. return -TARGET_EFAULT;
  10762. }
  10763. for(i = 0;i < ret; i++)
  10764. target_grouplist[i] = tswap32(grouplist[i]);
  10765. unlock_user(target_grouplist, arg2, gidsetsize * 4);
  10766. }
  10767. }
  10768. return ret;
  10769. #endif
  10770. #ifdef TARGET_NR_setgroups32
  10771. case TARGET_NR_setgroups32:
  10772. {
  10773. int gidsetsize = arg1;
  10774. uint32_t *target_grouplist;
  10775. gid_t *grouplist;
  10776. int i;
  10777. grouplist = alloca(gidsetsize * sizeof(gid_t));
  10778. target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
  10779. if (!target_grouplist) {
  10780. return -TARGET_EFAULT;
  10781. }
  10782. for(i = 0;i < gidsetsize; i++)
  10783. grouplist[i] = tswap32(target_grouplist[i]);
  10784. unlock_user(target_grouplist, arg2, 0);
  10785. return get_errno(setgroups(gidsetsize, grouplist));
  10786. }
  10787. #endif
  10788. #ifdef TARGET_NR_fchown32
  10789. case TARGET_NR_fchown32:
  10790. return get_errno(fchown(arg1, arg2, arg3));
  10791. #endif
  10792. #ifdef TARGET_NR_setresuid32
  10793. case TARGET_NR_setresuid32:
  10794. return get_errno(sys_setresuid(arg1, arg2, arg3));
  10795. #endif
  10796. #ifdef TARGET_NR_getresuid32
  10797. case TARGET_NR_getresuid32:
  10798. {
  10799. uid_t ruid, euid, suid;
  10800. ret = get_errno(getresuid(&ruid, &euid, &suid));
  10801. if (!is_error(ret)) {
  10802. if (put_user_u32(ruid, arg1)
  10803. || put_user_u32(euid, arg2)
  10804. || put_user_u32(suid, arg3))
  10805. return -TARGET_EFAULT;
  10806. }
  10807. }
  10808. return ret;
  10809. #endif
  10810. #ifdef TARGET_NR_setresgid32
  10811. case TARGET_NR_setresgid32:
  10812. return get_errno(sys_setresgid(arg1, arg2, arg3));
  10813. #endif
  10814. #ifdef TARGET_NR_getresgid32
  10815. case TARGET_NR_getresgid32:
  10816. {
  10817. gid_t rgid, egid, sgid;
  10818. ret = get_errno(getresgid(&rgid, &egid, &sgid));
  10819. if (!is_error(ret)) {
  10820. if (put_user_u32(rgid, arg1)
  10821. || put_user_u32(egid, arg2)
  10822. || put_user_u32(sgid, arg3))
  10823. return -TARGET_EFAULT;
  10824. }
  10825. }
  10826. return ret;
  10827. #endif
  10828. #ifdef TARGET_NR_chown32
  10829. case TARGET_NR_chown32:
  10830. if (!(p = lock_user_string(arg1)))
  10831. return -TARGET_EFAULT;
  10832. ret = get_errno(chown(p, arg2, arg3));
  10833. unlock_user(p, arg1, 0);
  10834. return ret;
  10835. #endif
  10836. #ifdef TARGET_NR_setuid32
  10837. case TARGET_NR_setuid32:
  10838. return get_errno(sys_setuid(arg1));
  10839. #endif
  10840. #ifdef TARGET_NR_setgid32
  10841. case TARGET_NR_setgid32:
  10842. return get_errno(sys_setgid(arg1));
  10843. #endif
  10844. #ifdef TARGET_NR_setfsuid32
  10845. case TARGET_NR_setfsuid32:
  10846. return get_errno(setfsuid(arg1));
  10847. #endif
  10848. #ifdef TARGET_NR_setfsgid32
  10849. case TARGET_NR_setfsgid32:
  10850. return get_errno(setfsgid(arg1));
  10851. #endif
  10852. #ifdef TARGET_NR_mincore
  10853. case TARGET_NR_mincore:
  10854. {
  10855. void *a = lock_user(VERIFY_READ, arg1, arg2, 0);
  10856. if (!a) {
  10857. return -TARGET_ENOMEM;
  10858. }
  10859. p = lock_user_string(arg3);
  10860. if (!p) {
  10861. ret = -TARGET_EFAULT;
  10862. } else {
  10863. ret = get_errno(mincore(a, arg2, p));
  10864. unlock_user(p, arg3, ret);
  10865. }
  10866. unlock_user(a, arg1, 0);
  10867. }
  10868. return ret;
  10869. #endif
  10870. #ifdef TARGET_NR_arm_fadvise64_64
  10871. case TARGET_NR_arm_fadvise64_64:
  10872. /* arm_fadvise64_64 looks like fadvise64_64 but
  10873. * with different argument order: fd, advice, offset, len
  10874. * rather than the usual fd, offset, len, advice.
  10875. * Note that offset and len are both 64-bit so appear as
  10876. * pairs of 32-bit registers.
  10877. */
  10878. ret = posix_fadvise(arg1, target_offset64(arg3, arg4),
  10879. target_offset64(arg5, arg6), arg2);
  10880. return -host_to_target_errno(ret);
  10881. #endif
  10882. #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
  10883. #ifdef TARGET_NR_fadvise64_64
  10884. case TARGET_NR_fadvise64_64:
  10885. #if defined(TARGET_PPC) || defined(TARGET_XTENSA)
  10886. /* 6 args: fd, advice, offset (high, low), len (high, low) */
  10887. ret = arg2;
  10888. arg2 = arg3;
  10889. arg3 = arg4;
  10890. arg4 = arg5;
  10891. arg5 = arg6;
  10892. arg6 = ret;
  10893. #else
  10894. /* 6 args: fd, offset (high, low), len (high, low), advice */
  10895. if (regpairs_aligned(cpu_env, num)) {
  10896. /* offset is in (3,4), len in (5,6) and advice in 7 */
  10897. arg2 = arg3;
  10898. arg3 = arg4;
  10899. arg4 = arg5;
  10900. arg5 = arg6;
  10901. arg6 = arg7;
  10902. }
  10903. #endif
  10904. ret = posix_fadvise(arg1, target_offset64(arg2, arg3),
  10905. target_offset64(arg4, arg5), arg6);
  10906. return -host_to_target_errno(ret);
  10907. #endif
  10908. #ifdef TARGET_NR_fadvise64
  10909. case TARGET_NR_fadvise64:
  10910. /* 5 args: fd, offset (high, low), len, advice */
  10911. if (regpairs_aligned(cpu_env, num)) {
  10912. /* offset is in (3,4), len in 5 and advice in 6 */
  10913. arg2 = arg3;
  10914. arg3 = arg4;
  10915. arg4 = arg5;
  10916. arg5 = arg6;
  10917. }
  10918. ret = posix_fadvise(arg1, target_offset64(arg2, arg3), arg4, arg5);
  10919. return -host_to_target_errno(ret);
  10920. #endif
  10921. #else /* not a 32-bit ABI */
  10922. #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_fadvise64)
  10923. #ifdef TARGET_NR_fadvise64_64
  10924. case TARGET_NR_fadvise64_64:
  10925. #endif
  10926. #ifdef TARGET_NR_fadvise64
  10927. case TARGET_NR_fadvise64:
  10928. #endif
  10929. #ifdef TARGET_S390X
  10930. switch (arg4) {
  10931. case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
  10932. case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
  10933. case 6: arg4 = POSIX_FADV_DONTNEED; break;
  10934. case 7: arg4 = POSIX_FADV_NOREUSE; break;
  10935. default: break;
  10936. }
  10937. #endif
  10938. return -host_to_target_errno(posix_fadvise(arg1, arg2, arg3, arg4));
  10939. #endif
  10940. #endif /* end of 64-bit ABI fadvise handling */
  10941. #ifdef TARGET_NR_madvise
  10942. case TARGET_NR_madvise:
  10943. return target_madvise(arg1, arg2, arg3);
  10944. #endif
  10945. #ifdef TARGET_NR_fcntl64
  10946. case TARGET_NR_fcntl64:
  10947. {
  10948. int cmd;
  10949. struct flock64 fl;
  10950. from_flock64_fn *copyfrom = copy_from_user_flock64;
  10951. to_flock64_fn *copyto = copy_to_user_flock64;
  10952. #ifdef TARGET_ARM
  10953. if (!cpu_env->eabi) {
  10954. copyfrom = copy_from_user_oabi_flock64;
  10955. copyto = copy_to_user_oabi_flock64;
  10956. }
  10957. #endif
  10958. cmd = target_to_host_fcntl_cmd(arg2);
  10959. if (cmd == -TARGET_EINVAL) {
  10960. return cmd;
  10961. }
  10962. switch(arg2) {
  10963. case TARGET_F_GETLK64:
  10964. ret = copyfrom(&fl, arg3);
  10965. if (ret) {
  10966. break;
  10967. }
  10968. ret = get_errno(safe_fcntl(arg1, cmd, &fl));
  10969. if (ret == 0) {
  10970. ret = copyto(arg3, &fl);
  10971. }
  10972. break;
  10973. case TARGET_F_SETLK64:
  10974. case TARGET_F_SETLKW64:
  10975. ret = copyfrom(&fl, arg3);
  10976. if (ret) {
  10977. break;
  10978. }
  10979. ret = get_errno(safe_fcntl(arg1, cmd, &fl));
  10980. break;
  10981. default:
  10982. ret = do_fcntl(arg1, arg2, arg3);
  10983. break;
  10984. }
  10985. return ret;
  10986. }
  10987. #endif
  10988. #ifdef TARGET_NR_cacheflush
  10989. case TARGET_NR_cacheflush:
  10990. /* self-modifying code is handled automatically, so nothing needed */
  10991. return 0;
  10992. #endif
  10993. #ifdef TARGET_NR_getpagesize
  10994. case TARGET_NR_getpagesize:
  10995. return TARGET_PAGE_SIZE;
  10996. #endif
  10997. case TARGET_NR_gettid:
  10998. return get_errno(sys_gettid());
  10999. #ifdef TARGET_NR_readahead
  11000. case TARGET_NR_readahead:
  11001. #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
  11002. if (regpairs_aligned(cpu_env, num)) {
  11003. arg2 = arg3;
  11004. arg3 = arg4;
  11005. arg4 = arg5;
  11006. }
  11007. ret = get_errno(readahead(arg1, target_offset64(arg2, arg3) , arg4));
  11008. #else
  11009. ret = get_errno(readahead(arg1, arg2, arg3));
  11010. #endif
  11011. return ret;
  11012. #endif
  11013. #ifdef CONFIG_ATTR
  11014. #ifdef TARGET_NR_setxattr
  11015. case TARGET_NR_listxattr:
  11016. case TARGET_NR_llistxattr:
  11017. {
  11018. void *p, *b = 0;
  11019. if (arg2) {
  11020. b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  11021. if (!b) {
  11022. return -TARGET_EFAULT;
  11023. }
  11024. }
  11025. p = lock_user_string(arg1);
  11026. if (p) {
  11027. if (num == TARGET_NR_listxattr) {
  11028. ret = get_errno(listxattr(p, b, arg3));
  11029. } else {
  11030. ret = get_errno(llistxattr(p, b, arg3));
  11031. }
  11032. } else {
  11033. ret = -TARGET_EFAULT;
  11034. }
  11035. unlock_user(p, arg1, 0);
  11036. unlock_user(b, arg2, arg3);
  11037. return ret;
  11038. }
  11039. case TARGET_NR_flistxattr:
  11040. {
  11041. void *b = 0;
  11042. if (arg2) {
  11043. b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  11044. if (!b) {
  11045. return -TARGET_EFAULT;
  11046. }
  11047. }
  11048. ret = get_errno(flistxattr(arg1, b, arg3));
  11049. unlock_user(b, arg2, arg3);
  11050. return ret;
  11051. }
  11052. case TARGET_NR_setxattr:
  11053. case TARGET_NR_lsetxattr:
  11054. {
  11055. void *p, *n, *v = 0;
  11056. if (arg3) {
  11057. v = lock_user(VERIFY_READ, arg3, arg4, 1);
  11058. if (!v) {
  11059. return -TARGET_EFAULT;
  11060. }
  11061. }
  11062. p = lock_user_string(arg1);
  11063. n = lock_user_string(arg2);
  11064. if (p && n) {
  11065. if (num == TARGET_NR_setxattr) {
  11066. ret = get_errno(setxattr(p, n, v, arg4, arg5));
  11067. } else {
  11068. ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
  11069. }
  11070. } else {
  11071. ret = -TARGET_EFAULT;
  11072. }
  11073. unlock_user(p, arg1, 0);
  11074. unlock_user(n, arg2, 0);
  11075. unlock_user(v, arg3, 0);
  11076. }
  11077. return ret;
  11078. case TARGET_NR_fsetxattr:
  11079. {
  11080. void *n, *v = 0;
  11081. if (arg3) {
  11082. v = lock_user(VERIFY_READ, arg3, arg4, 1);
  11083. if (!v) {
  11084. return -TARGET_EFAULT;
  11085. }
  11086. }
  11087. n = lock_user_string(arg2);
  11088. if (n) {
  11089. ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
  11090. } else {
  11091. ret = -TARGET_EFAULT;
  11092. }
  11093. unlock_user(n, arg2, 0);
  11094. unlock_user(v, arg3, 0);
  11095. }
  11096. return ret;
  11097. case TARGET_NR_getxattr:
  11098. case TARGET_NR_lgetxattr:
  11099. {
  11100. void *p, *n, *v = 0;
  11101. if (arg3) {
  11102. v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  11103. if (!v) {
  11104. return -TARGET_EFAULT;
  11105. }
  11106. }
  11107. p = lock_user_string(arg1);
  11108. n = lock_user_string(arg2);
  11109. if (p && n) {
  11110. if (num == TARGET_NR_getxattr) {
  11111. ret = get_errno(getxattr(p, n, v, arg4));
  11112. } else {
  11113. ret = get_errno(lgetxattr(p, n, v, arg4));
  11114. }
  11115. } else {
  11116. ret = -TARGET_EFAULT;
  11117. }
  11118. unlock_user(p, arg1, 0);
  11119. unlock_user(n, arg2, 0);
  11120. unlock_user(v, arg3, arg4);
  11121. }
  11122. return ret;
  11123. case TARGET_NR_fgetxattr:
  11124. {
  11125. void *n, *v = 0;
  11126. if (arg3) {
  11127. v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  11128. if (!v) {
  11129. return -TARGET_EFAULT;
  11130. }
  11131. }
  11132. n = lock_user_string(arg2);
  11133. if (n) {
  11134. ret = get_errno(fgetxattr(arg1, n, v, arg4));
  11135. } else {
  11136. ret = -TARGET_EFAULT;
  11137. }
  11138. unlock_user(n, arg2, 0);
  11139. unlock_user(v, arg3, arg4);
  11140. }
  11141. return ret;
  11142. case TARGET_NR_removexattr:
  11143. case TARGET_NR_lremovexattr:
  11144. {
  11145. void *p, *n;
  11146. p = lock_user_string(arg1);
  11147. n = lock_user_string(arg2);
  11148. if (p && n) {
  11149. if (num == TARGET_NR_removexattr) {
  11150. ret = get_errno(removexattr(p, n));
  11151. } else {
  11152. ret = get_errno(lremovexattr(p, n));
  11153. }
  11154. } else {
  11155. ret = -TARGET_EFAULT;
  11156. }
  11157. unlock_user(p, arg1, 0);
  11158. unlock_user(n, arg2, 0);
  11159. }
  11160. return ret;
  11161. case TARGET_NR_fremovexattr:
  11162. {
  11163. void *n;
  11164. n = lock_user_string(arg2);
  11165. if (n) {
  11166. ret = get_errno(fremovexattr(arg1, n));
  11167. } else {
  11168. ret = -TARGET_EFAULT;
  11169. }
  11170. unlock_user(n, arg2, 0);
  11171. }
  11172. return ret;
  11173. #endif
  11174. #endif /* CONFIG_ATTR */
  11175. #ifdef TARGET_NR_set_thread_area
  11176. case TARGET_NR_set_thread_area:
  11177. #if defined(TARGET_MIPS)
  11178. cpu_env->active_tc.CP0_UserLocal = arg1;
  11179. return 0;
  11180. #elif defined(TARGET_CRIS)
  11181. if (arg1 & 0xff)
  11182. ret = -TARGET_EINVAL;
  11183. else {
  11184. cpu_env->pregs[PR_PID] = arg1;
  11185. ret = 0;
  11186. }
  11187. return ret;
  11188. #elif defined(TARGET_I386) && defined(TARGET_ABI32)
  11189. return do_set_thread_area(cpu_env, arg1);
  11190. #elif defined(TARGET_M68K)
  11191. {
  11192. TaskState *ts = cpu->opaque;
  11193. ts->tp_value = arg1;
  11194. return 0;
  11195. }
  11196. #else
  11197. return -TARGET_ENOSYS;
  11198. #endif
  11199. #endif
  11200. #ifdef TARGET_NR_get_thread_area
  11201. case TARGET_NR_get_thread_area:
  11202. #if defined(TARGET_I386) && defined(TARGET_ABI32)
  11203. return do_get_thread_area(cpu_env, arg1);
  11204. #elif defined(TARGET_M68K)
  11205. {
  11206. TaskState *ts = cpu->opaque;
  11207. return ts->tp_value;
  11208. }
  11209. #else
  11210. return -TARGET_ENOSYS;
  11211. #endif
  11212. #endif
  11213. #ifdef TARGET_NR_getdomainname
  11214. case TARGET_NR_getdomainname:
  11215. return -TARGET_ENOSYS;
  11216. #endif
  11217. #ifdef TARGET_NR_clock_settime
  11218. case TARGET_NR_clock_settime:
  11219. {
  11220. struct timespec ts;
  11221. ret = target_to_host_timespec(&ts, arg2);
  11222. if (!is_error(ret)) {
  11223. ret = get_errno(clock_settime(arg1, &ts));
  11224. }
  11225. return ret;
  11226. }
  11227. #endif
  11228. #ifdef TARGET_NR_clock_settime64
  11229. case TARGET_NR_clock_settime64:
  11230. {
  11231. struct timespec ts;
  11232. ret = target_to_host_timespec64(&ts, arg2);
  11233. if (!is_error(ret)) {
  11234. ret = get_errno(clock_settime(arg1, &ts));
  11235. }
  11236. return ret;
  11237. }
  11238. #endif
  11239. #ifdef TARGET_NR_clock_gettime
  11240. case TARGET_NR_clock_gettime:
  11241. {
  11242. struct timespec ts;
  11243. ret = get_errno(clock_gettime(arg1, &ts));
  11244. if (!is_error(ret)) {
  11245. ret = host_to_target_timespec(arg2, &ts);
  11246. }
  11247. return ret;
  11248. }
  11249. #endif
  11250. #ifdef TARGET_NR_clock_gettime64
  11251. case TARGET_NR_clock_gettime64:
  11252. {
  11253. struct timespec ts;
  11254. ret = get_errno(clock_gettime(arg1, &ts));
  11255. if (!is_error(ret)) {
  11256. ret = host_to_target_timespec64(arg2, &ts);
  11257. }
  11258. return ret;
  11259. }
  11260. #endif
  11261. #ifdef TARGET_NR_clock_getres
  11262. case TARGET_NR_clock_getres:
  11263. {
  11264. struct timespec ts;
  11265. ret = get_errno(clock_getres(arg1, &ts));
  11266. if (!is_error(ret)) {
  11267. host_to_target_timespec(arg2, &ts);
  11268. }
  11269. return ret;
  11270. }
  11271. #endif
  11272. #ifdef TARGET_NR_clock_getres_time64
  11273. case TARGET_NR_clock_getres_time64:
  11274. {
  11275. struct timespec ts;
  11276. ret = get_errno(clock_getres(arg1, &ts));
  11277. if (!is_error(ret)) {
  11278. host_to_target_timespec64(arg2, &ts);
  11279. }
  11280. return ret;
  11281. }
  11282. #endif
  11283. #ifdef TARGET_NR_clock_nanosleep
  11284. case TARGET_NR_clock_nanosleep:
  11285. {
  11286. struct timespec ts;
  11287. if (target_to_host_timespec(&ts, arg3)) {
  11288. return -TARGET_EFAULT;
  11289. }
  11290. ret = get_errno(safe_clock_nanosleep(arg1, arg2,
  11291. &ts, arg4 ? &ts : NULL));
  11292. /*
  11293. * if the call is interrupted by a signal handler, it fails
  11294. * with error -TARGET_EINTR and if arg4 is not NULL and arg2 is not
  11295. * TIMER_ABSTIME, it returns the remaining unslept time in arg4.
  11296. */
  11297. if (ret == -TARGET_EINTR && arg4 && arg2 != TIMER_ABSTIME &&
  11298. host_to_target_timespec(arg4, &ts)) {
  11299. return -TARGET_EFAULT;
  11300. }
  11301. return ret;
  11302. }
  11303. #endif
  11304. #ifdef TARGET_NR_clock_nanosleep_time64
  11305. case TARGET_NR_clock_nanosleep_time64:
  11306. {
  11307. struct timespec ts;
  11308. if (target_to_host_timespec64(&ts, arg3)) {
  11309. return -TARGET_EFAULT;
  11310. }
  11311. ret = get_errno(safe_clock_nanosleep(arg1, arg2,
  11312. &ts, arg4 ? &ts : NULL));
  11313. if (ret == -TARGET_EINTR && arg4 && arg2 != TIMER_ABSTIME &&
  11314. host_to_target_timespec64(arg4, &ts)) {
  11315. return -TARGET_EFAULT;
  11316. }
  11317. return ret;
  11318. }
  11319. #endif
  11320. #if defined(TARGET_NR_set_tid_address)
  11321. case TARGET_NR_set_tid_address:
  11322. {
  11323. TaskState *ts = cpu->opaque;
  11324. ts->child_tidptr = arg1;
  11325. /* do not call host set_tid_address() syscall, instead return tid() */
  11326. return get_errno(sys_gettid());
  11327. }
  11328. #endif
  11329. case TARGET_NR_tkill:
  11330. return get_errno(safe_tkill((int)arg1, target_to_host_signal(arg2)));
  11331. case TARGET_NR_tgkill:
  11332. return get_errno(safe_tgkill((int)arg1, (int)arg2,
  11333. target_to_host_signal(arg3)));
  11334. #ifdef TARGET_NR_set_robust_list
  11335. case TARGET_NR_set_robust_list:
  11336. case TARGET_NR_get_robust_list:
  11337. /* The ABI for supporting robust futexes has userspace pass
  11338. * the kernel a pointer to a linked list which is updated by
  11339. * userspace after the syscall; the list is walked by the kernel
  11340. * when the thread exits. Since the linked list in QEMU guest
  11341. * memory isn't a valid linked list for the host and we have
  11342. * no way to reliably intercept the thread-death event, we can't
  11343. * support these. Silently return ENOSYS so that guest userspace
  11344. * falls back to a non-robust futex implementation (which should
  11345. * be OK except in the corner case of the guest crashing while
  11346. * holding a mutex that is shared with another process via
  11347. * shared memory).
  11348. */
  11349. return -TARGET_ENOSYS;
  11350. #endif
  11351. #if defined(TARGET_NR_utimensat)
  11352. case TARGET_NR_utimensat:
  11353. {
  11354. struct timespec *tsp, ts[2];
  11355. if (!arg3) {
  11356. tsp = NULL;
  11357. } else {
  11358. if (target_to_host_timespec(ts, arg3)) {
  11359. return -TARGET_EFAULT;
  11360. }
  11361. if (target_to_host_timespec(ts + 1, arg3 +
  11362. sizeof(struct target_timespec))) {
  11363. return -TARGET_EFAULT;
  11364. }
  11365. tsp = ts;
  11366. }
  11367. if (!arg2)
  11368. ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
  11369. else {
  11370. if (!(p = lock_user_string(arg2))) {
  11371. return -TARGET_EFAULT;
  11372. }
  11373. ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
  11374. unlock_user(p, arg2, 0);
  11375. }
  11376. }
  11377. return ret;
  11378. #endif
  11379. #ifdef TARGET_NR_utimensat_time64
  11380. case TARGET_NR_utimensat_time64:
  11381. {
  11382. struct timespec *tsp, ts[2];
  11383. if (!arg3) {
  11384. tsp = NULL;
  11385. } else {
  11386. if (target_to_host_timespec64(ts, arg3)) {
  11387. return -TARGET_EFAULT;
  11388. }
  11389. if (target_to_host_timespec64(ts + 1, arg3 +
  11390. sizeof(struct target__kernel_timespec))) {
  11391. return -TARGET_EFAULT;
  11392. }
  11393. tsp = ts;
  11394. }
  11395. if (!arg2)
  11396. ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
  11397. else {
  11398. p = lock_user_string(arg2);
  11399. if (!p) {
  11400. return -TARGET_EFAULT;
  11401. }
  11402. ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
  11403. unlock_user(p, arg2, 0);
  11404. }
  11405. }
  11406. return ret;
  11407. #endif
  11408. #ifdef TARGET_NR_futex
  11409. case TARGET_NR_futex:
  11410. return do_futex(cpu, false, arg1, arg2, arg3, arg4, arg5, arg6);
  11411. #endif
  11412. #ifdef TARGET_NR_futex_time64
  11413. case TARGET_NR_futex_time64:
  11414. return do_futex(cpu, true, arg1, arg2, arg3, arg4, arg5, arg6);
  11415. #endif
  11416. #ifdef CONFIG_INOTIFY
  11417. #if defined(TARGET_NR_inotify_init)
  11418. case TARGET_NR_inotify_init:
  11419. ret = get_errno(inotify_init());
  11420. if (ret >= 0) {
  11421. fd_trans_register(ret, &target_inotify_trans);
  11422. }
  11423. return ret;
  11424. #endif
  11425. #if defined(TARGET_NR_inotify_init1) && defined(CONFIG_INOTIFY1)
  11426. case TARGET_NR_inotify_init1:
  11427. ret = get_errno(inotify_init1(target_to_host_bitmask(arg1,
  11428. fcntl_flags_tbl)));
  11429. if (ret >= 0) {
  11430. fd_trans_register(ret, &target_inotify_trans);
  11431. }
  11432. return ret;
  11433. #endif
  11434. #if defined(TARGET_NR_inotify_add_watch)
  11435. case TARGET_NR_inotify_add_watch:
  11436. p = lock_user_string(arg2);
  11437. ret = get_errno(inotify_add_watch(arg1, path(p), arg3));
  11438. unlock_user(p, arg2, 0);
  11439. return ret;
  11440. #endif
  11441. #if defined(TARGET_NR_inotify_rm_watch)
  11442. case TARGET_NR_inotify_rm_watch:
  11443. return get_errno(inotify_rm_watch(arg1, arg2));
  11444. #endif
  11445. #endif
  11446. #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
  11447. case TARGET_NR_mq_open:
  11448. {
  11449. struct mq_attr posix_mq_attr;
  11450. struct mq_attr *pposix_mq_attr;
  11451. int host_flags;
  11452. host_flags = target_to_host_bitmask(arg2, fcntl_flags_tbl);
  11453. pposix_mq_attr = NULL;
  11454. if (arg4) {
  11455. if (copy_from_user_mq_attr(&posix_mq_attr, arg4) != 0) {
  11456. return -TARGET_EFAULT;
  11457. }
  11458. pposix_mq_attr = &posix_mq_attr;
  11459. }
  11460. p = lock_user_string(arg1 - 1);
  11461. if (!p) {
  11462. return -TARGET_EFAULT;
  11463. }
  11464. ret = get_errno(mq_open(p, host_flags, arg3, pposix_mq_attr));
  11465. unlock_user (p, arg1, 0);
  11466. }
  11467. return ret;
  11468. case TARGET_NR_mq_unlink:
  11469. p = lock_user_string(arg1 - 1);
  11470. if (!p) {
  11471. return -TARGET_EFAULT;
  11472. }
  11473. ret = get_errno(mq_unlink(p));
  11474. unlock_user (p, arg1, 0);
  11475. return ret;
  11476. #ifdef TARGET_NR_mq_timedsend
  11477. case TARGET_NR_mq_timedsend:
  11478. {
  11479. struct timespec ts;
  11480. p = lock_user (VERIFY_READ, arg2, arg3, 1);
  11481. if (arg5 != 0) {
  11482. if (target_to_host_timespec(&ts, arg5)) {
  11483. return -TARGET_EFAULT;
  11484. }
  11485. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts));
  11486. if (!is_error(ret) && host_to_target_timespec(arg5, &ts)) {
  11487. return -TARGET_EFAULT;
  11488. }
  11489. } else {
  11490. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL));
  11491. }
  11492. unlock_user (p, arg2, arg3);
  11493. }
  11494. return ret;
  11495. #endif
  11496. #ifdef TARGET_NR_mq_timedsend_time64
  11497. case TARGET_NR_mq_timedsend_time64:
  11498. {
  11499. struct timespec ts;
  11500. p = lock_user(VERIFY_READ, arg2, arg3, 1);
  11501. if (arg5 != 0) {
  11502. if (target_to_host_timespec64(&ts, arg5)) {
  11503. return -TARGET_EFAULT;
  11504. }
  11505. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts));
  11506. if (!is_error(ret) && host_to_target_timespec64(arg5, &ts)) {
  11507. return -TARGET_EFAULT;
  11508. }
  11509. } else {
  11510. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL));
  11511. }
  11512. unlock_user(p, arg2, arg3);
  11513. }
  11514. return ret;
  11515. #endif
  11516. #ifdef TARGET_NR_mq_timedreceive
  11517. case TARGET_NR_mq_timedreceive:
  11518. {
  11519. struct timespec ts;
  11520. unsigned int prio;
  11521. p = lock_user (VERIFY_READ, arg2, arg3, 1);
  11522. if (arg5 != 0) {
  11523. if (target_to_host_timespec(&ts, arg5)) {
  11524. return -TARGET_EFAULT;
  11525. }
  11526. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  11527. &prio, &ts));
  11528. if (!is_error(ret) && host_to_target_timespec(arg5, &ts)) {
  11529. return -TARGET_EFAULT;
  11530. }
  11531. } else {
  11532. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  11533. &prio, NULL));
  11534. }
  11535. unlock_user (p, arg2, arg3);
  11536. if (arg4 != 0)
  11537. put_user_u32(prio, arg4);
  11538. }
  11539. return ret;
  11540. #endif
  11541. #ifdef TARGET_NR_mq_timedreceive_time64
  11542. case TARGET_NR_mq_timedreceive_time64:
  11543. {
  11544. struct timespec ts;
  11545. unsigned int prio;
  11546. p = lock_user(VERIFY_READ, arg2, arg3, 1);
  11547. if (arg5 != 0) {
  11548. if (target_to_host_timespec64(&ts, arg5)) {
  11549. return -TARGET_EFAULT;
  11550. }
  11551. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  11552. &prio, &ts));
  11553. if (!is_error(ret) && host_to_target_timespec64(arg5, &ts)) {
  11554. return -TARGET_EFAULT;
  11555. }
  11556. } else {
  11557. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  11558. &prio, NULL));
  11559. }
  11560. unlock_user(p, arg2, arg3);
  11561. if (arg4 != 0) {
  11562. put_user_u32(prio, arg4);
  11563. }
  11564. }
  11565. return ret;
  11566. #endif
  11567. /* Not implemented for now... */
  11568. /* case TARGET_NR_mq_notify: */
  11569. /* break; */
  11570. case TARGET_NR_mq_getsetattr:
  11571. {
  11572. struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
  11573. ret = 0;
  11574. if (arg2 != 0) {
  11575. copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
  11576. ret = get_errno(mq_setattr(arg1, &posix_mq_attr_in,
  11577. &posix_mq_attr_out));
  11578. } else if (arg3 != 0) {
  11579. ret = get_errno(mq_getattr(arg1, &posix_mq_attr_out));
  11580. }
  11581. if (ret == 0 && arg3 != 0) {
  11582. copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
  11583. }
  11584. }
  11585. return ret;
  11586. #endif
  11587. #ifdef CONFIG_SPLICE
  11588. #ifdef TARGET_NR_tee
  11589. case TARGET_NR_tee:
  11590. {
  11591. ret = get_errno(tee(arg1,arg2,arg3,arg4));
  11592. }
  11593. return ret;
  11594. #endif
  11595. #ifdef TARGET_NR_splice
  11596. case TARGET_NR_splice:
  11597. {
  11598. loff_t loff_in, loff_out;
  11599. loff_t *ploff_in = NULL, *ploff_out = NULL;
  11600. if (arg2) {
  11601. if (get_user_u64(loff_in, arg2)) {
  11602. return -TARGET_EFAULT;
  11603. }
  11604. ploff_in = &loff_in;
  11605. }
  11606. if (arg4) {
  11607. if (get_user_u64(loff_out, arg4)) {
  11608. return -TARGET_EFAULT;
  11609. }
  11610. ploff_out = &loff_out;
  11611. }
  11612. ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
  11613. if (arg2) {
  11614. if (put_user_u64(loff_in, arg2)) {
  11615. return -TARGET_EFAULT;
  11616. }
  11617. }
  11618. if (arg4) {
  11619. if (put_user_u64(loff_out, arg4)) {
  11620. return -TARGET_EFAULT;
  11621. }
  11622. }
  11623. }
  11624. return ret;
  11625. #endif
  11626. #ifdef TARGET_NR_vmsplice
  11627. case TARGET_NR_vmsplice:
  11628. {
  11629. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  11630. if (vec != NULL) {
  11631. ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
  11632. unlock_iovec(vec, arg2, arg3, 0);
  11633. } else {
  11634. ret = -host_to_target_errno(errno);
  11635. }
  11636. }
  11637. return ret;
  11638. #endif
  11639. #endif /* CONFIG_SPLICE */
  11640. #ifdef CONFIG_EVENTFD
  11641. #if defined(TARGET_NR_eventfd)
  11642. case TARGET_NR_eventfd:
  11643. ret = get_errno(eventfd(arg1, 0));
  11644. if (ret >= 0) {
  11645. fd_trans_register(ret, &target_eventfd_trans);
  11646. }
  11647. return ret;
  11648. #endif
  11649. #if defined(TARGET_NR_eventfd2)
  11650. case TARGET_NR_eventfd2:
  11651. {
  11652. int host_flags = arg2 & (~(TARGET_O_NONBLOCK_MASK | TARGET_O_CLOEXEC));
  11653. if (arg2 & TARGET_O_NONBLOCK) {
  11654. host_flags |= O_NONBLOCK;
  11655. }
  11656. if (arg2 & TARGET_O_CLOEXEC) {
  11657. host_flags |= O_CLOEXEC;
  11658. }
  11659. ret = get_errno(eventfd(arg1, host_flags));
  11660. if (ret >= 0) {
  11661. fd_trans_register(ret, &target_eventfd_trans);
  11662. }
  11663. return ret;
  11664. }
  11665. #endif
  11666. #endif /* CONFIG_EVENTFD */
  11667. #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
  11668. case TARGET_NR_fallocate:
  11669. #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
  11670. ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
  11671. target_offset64(arg5, arg6)));
  11672. #else
  11673. ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
  11674. #endif
  11675. return ret;
  11676. #endif
  11677. #if defined(CONFIG_SYNC_FILE_RANGE)
  11678. #if defined(TARGET_NR_sync_file_range)
  11679. case TARGET_NR_sync_file_range:
  11680. #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
  11681. #if defined(TARGET_MIPS)
  11682. ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
  11683. target_offset64(arg5, arg6), arg7));
  11684. #else
  11685. ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
  11686. target_offset64(arg4, arg5), arg6));
  11687. #endif /* !TARGET_MIPS */
  11688. #else
  11689. ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
  11690. #endif
  11691. return ret;
  11692. #endif
  11693. #if defined(TARGET_NR_sync_file_range2) || \
  11694. defined(TARGET_NR_arm_sync_file_range)
  11695. #if defined(TARGET_NR_sync_file_range2)
  11696. case TARGET_NR_sync_file_range2:
  11697. #endif
  11698. #if defined(TARGET_NR_arm_sync_file_range)
  11699. case TARGET_NR_arm_sync_file_range:
  11700. #endif
  11701. /* This is like sync_file_range but the arguments are reordered */
  11702. #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
  11703. ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
  11704. target_offset64(arg5, arg6), arg2));
  11705. #else
  11706. ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
  11707. #endif
  11708. return ret;
  11709. #endif
  11710. #endif
  11711. #if defined(TARGET_NR_signalfd4)
  11712. case TARGET_NR_signalfd4:
  11713. return do_signalfd4(arg1, arg2, arg4);
  11714. #endif
  11715. #if defined(TARGET_NR_signalfd)
  11716. case TARGET_NR_signalfd:
  11717. return do_signalfd4(arg1, arg2, 0);
  11718. #endif
  11719. #if defined(CONFIG_EPOLL)
  11720. #if defined(TARGET_NR_epoll_create)
  11721. case TARGET_NR_epoll_create:
  11722. return get_errno(epoll_create(arg1));
  11723. #endif
  11724. #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
  11725. case TARGET_NR_epoll_create1:
  11726. return get_errno(epoll_create1(target_to_host_bitmask(arg1, fcntl_flags_tbl)));
  11727. #endif
  11728. #if defined(TARGET_NR_epoll_ctl)
  11729. case TARGET_NR_epoll_ctl:
  11730. {
  11731. struct epoll_event ep;
  11732. struct epoll_event *epp = 0;
  11733. if (arg4) {
  11734. if (arg2 != EPOLL_CTL_DEL) {
  11735. struct target_epoll_event *target_ep;
  11736. if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
  11737. return -TARGET_EFAULT;
  11738. }
  11739. ep.events = tswap32(target_ep->events);
  11740. /*
  11741. * The epoll_data_t union is just opaque data to the kernel,
  11742. * so we transfer all 64 bits across and need not worry what
  11743. * actual data type it is.
  11744. */
  11745. ep.data.u64 = tswap64(target_ep->data.u64);
  11746. unlock_user_struct(target_ep, arg4, 0);
  11747. }
  11748. /*
  11749. * before kernel 2.6.9, EPOLL_CTL_DEL operation required a
  11750. * non-null pointer, even though this argument is ignored.
  11751. *
  11752. */
  11753. epp = &ep;
  11754. }
  11755. return get_errno(epoll_ctl(arg1, arg2, arg3, epp));
  11756. }
  11757. #endif
  11758. #if defined(TARGET_NR_epoll_wait) || defined(TARGET_NR_epoll_pwait)
  11759. #if defined(TARGET_NR_epoll_wait)
  11760. case TARGET_NR_epoll_wait:
  11761. #endif
  11762. #if defined(TARGET_NR_epoll_pwait)
  11763. case TARGET_NR_epoll_pwait:
  11764. #endif
  11765. {
  11766. struct target_epoll_event *target_ep;
  11767. struct epoll_event *ep;
  11768. int epfd = arg1;
  11769. int maxevents = arg3;
  11770. int timeout = arg4;
  11771. if (maxevents <= 0 || maxevents > TARGET_EP_MAX_EVENTS) {
  11772. return -TARGET_EINVAL;
  11773. }
  11774. target_ep = lock_user(VERIFY_WRITE, arg2,
  11775. maxevents * sizeof(struct target_epoll_event), 1);
  11776. if (!target_ep) {
  11777. return -TARGET_EFAULT;
  11778. }
  11779. ep = g_try_new(struct epoll_event, maxevents);
  11780. if (!ep) {
  11781. unlock_user(target_ep, arg2, 0);
  11782. return -TARGET_ENOMEM;
  11783. }
  11784. switch (num) {
  11785. #if defined(TARGET_NR_epoll_pwait)
  11786. case TARGET_NR_epoll_pwait:
  11787. {
  11788. sigset_t *set = NULL;
  11789. if (arg5) {
  11790. ret = process_sigsuspend_mask(&set, arg5, arg6);
  11791. if (ret != 0) {
  11792. break;
  11793. }
  11794. }
  11795. ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
  11796. set, SIGSET_T_SIZE));
  11797. if (set) {
  11798. finish_sigsuspend_mask(ret);
  11799. }
  11800. break;
  11801. }
  11802. #endif
  11803. #if defined(TARGET_NR_epoll_wait)
  11804. case TARGET_NR_epoll_wait:
  11805. ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
  11806. NULL, 0));
  11807. break;
  11808. #endif
  11809. default:
  11810. ret = -TARGET_ENOSYS;
  11811. }
  11812. if (!is_error(ret)) {
  11813. int i;
  11814. for (i = 0; i < ret; i++) {
  11815. target_ep[i].events = tswap32(ep[i].events);
  11816. target_ep[i].data.u64 = tswap64(ep[i].data.u64);
  11817. }
  11818. unlock_user(target_ep, arg2,
  11819. ret * sizeof(struct target_epoll_event));
  11820. } else {
  11821. unlock_user(target_ep, arg2, 0);
  11822. }
  11823. g_free(ep);
  11824. return ret;
  11825. }
  11826. #endif
  11827. #endif
  11828. #ifdef TARGET_NR_prlimit64
  11829. case TARGET_NR_prlimit64:
  11830. {
  11831. /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
  11832. struct target_rlimit64 *target_rnew, *target_rold;
  11833. struct host_rlimit64 rnew, rold, *rnewp = 0;
  11834. int resource = target_to_host_resource(arg2);
  11835. if (arg3 && (resource != RLIMIT_AS &&
  11836. resource != RLIMIT_DATA &&
  11837. resource != RLIMIT_STACK)) {
  11838. if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
  11839. return -TARGET_EFAULT;
  11840. }
  11841. rnew.rlim_cur = tswap64(target_rnew->rlim_cur);
  11842. rnew.rlim_max = tswap64(target_rnew->rlim_max);
  11843. unlock_user_struct(target_rnew, arg3, 0);
  11844. rnewp = &rnew;
  11845. }
  11846. ret = get_errno(sys_prlimit64(arg1, resource, rnewp, arg4 ? &rold : 0));
  11847. if (!is_error(ret) && arg4) {
  11848. if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
  11849. return -TARGET_EFAULT;
  11850. }
  11851. target_rold->rlim_cur = tswap64(rold.rlim_cur);
  11852. target_rold->rlim_max = tswap64(rold.rlim_max);
  11853. unlock_user_struct(target_rold, arg4, 1);
  11854. }
  11855. return ret;
  11856. }
  11857. #endif
  11858. #ifdef TARGET_NR_gethostname
  11859. case TARGET_NR_gethostname:
  11860. {
  11861. char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
  11862. if (name) {
  11863. ret = get_errno(gethostname(name, arg2));
  11864. unlock_user(name, arg1, arg2);
  11865. } else {
  11866. ret = -TARGET_EFAULT;
  11867. }
  11868. return ret;
  11869. }
  11870. #endif
  11871. #ifdef TARGET_NR_atomic_cmpxchg_32
  11872. case TARGET_NR_atomic_cmpxchg_32:
  11873. {
  11874. /* should use start_exclusive from main.c */
  11875. abi_ulong mem_value;
  11876. if (get_user_u32(mem_value, arg6)) {
  11877. target_siginfo_t info;
  11878. info.si_signo = SIGSEGV;
  11879. info.si_errno = 0;
  11880. info.si_code = TARGET_SEGV_MAPERR;
  11881. info._sifields._sigfault._addr = arg6;
  11882. queue_signal(cpu_env, info.si_signo, QEMU_SI_FAULT, &info);
  11883. ret = 0xdeadbeef;
  11884. }
  11885. if (mem_value == arg2)
  11886. put_user_u32(arg1, arg6);
  11887. return mem_value;
  11888. }
  11889. #endif
  11890. #ifdef TARGET_NR_atomic_barrier
  11891. case TARGET_NR_atomic_barrier:
  11892. /* Like the kernel implementation and the
  11893. qemu arm barrier, no-op this? */
  11894. return 0;
  11895. #endif
  11896. #ifdef TARGET_NR_timer_create
  11897. case TARGET_NR_timer_create:
  11898. {
  11899. /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
  11900. struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL;
  11901. int clkid = arg1;
  11902. int timer_index = next_free_host_timer();
  11903. if (timer_index < 0) {
  11904. ret = -TARGET_EAGAIN;
  11905. } else {
  11906. timer_t *phtimer = g_posix_timers + timer_index;
  11907. if (arg2) {
  11908. phost_sevp = &host_sevp;
  11909. ret = target_to_host_sigevent(phost_sevp, arg2);
  11910. if (ret != 0) {
  11911. free_host_timer_slot(timer_index);
  11912. return ret;
  11913. }
  11914. }
  11915. ret = get_errno(timer_create(clkid, phost_sevp, phtimer));
  11916. if (ret) {
  11917. free_host_timer_slot(timer_index);
  11918. } else {
  11919. if (put_user(TIMER_MAGIC | timer_index, arg3, target_timer_t)) {
  11920. timer_delete(*phtimer);
  11921. free_host_timer_slot(timer_index);
  11922. return -TARGET_EFAULT;
  11923. }
  11924. }
  11925. }
  11926. return ret;
  11927. }
  11928. #endif
  11929. #ifdef TARGET_NR_timer_settime
  11930. case TARGET_NR_timer_settime:
  11931. {
  11932. /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
  11933. * struct itimerspec * old_value */
  11934. target_timer_t timerid = get_timer_id(arg1);
  11935. if (timerid < 0) {
  11936. ret = timerid;
  11937. } else if (arg3 == 0) {
  11938. ret = -TARGET_EINVAL;
  11939. } else {
  11940. timer_t htimer = g_posix_timers[timerid];
  11941. struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
  11942. if (target_to_host_itimerspec(&hspec_new, arg3)) {
  11943. return -TARGET_EFAULT;
  11944. }
  11945. ret = get_errno(
  11946. timer_settime(htimer, arg2, &hspec_new, &hspec_old));
  11947. if (arg4 && host_to_target_itimerspec(arg4, &hspec_old)) {
  11948. return -TARGET_EFAULT;
  11949. }
  11950. }
  11951. return ret;
  11952. }
  11953. #endif
  11954. #ifdef TARGET_NR_timer_settime64
  11955. case TARGET_NR_timer_settime64:
  11956. {
  11957. target_timer_t timerid = get_timer_id(arg1);
  11958. if (timerid < 0) {
  11959. ret = timerid;
  11960. } else if (arg3 == 0) {
  11961. ret = -TARGET_EINVAL;
  11962. } else {
  11963. timer_t htimer = g_posix_timers[timerid];
  11964. struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
  11965. if (target_to_host_itimerspec64(&hspec_new, arg3)) {
  11966. return -TARGET_EFAULT;
  11967. }
  11968. ret = get_errno(
  11969. timer_settime(htimer, arg2, &hspec_new, &hspec_old));
  11970. if (arg4 && host_to_target_itimerspec64(arg4, &hspec_old)) {
  11971. return -TARGET_EFAULT;
  11972. }
  11973. }
  11974. return ret;
  11975. }
  11976. #endif
  11977. #ifdef TARGET_NR_timer_gettime
  11978. case TARGET_NR_timer_gettime:
  11979. {
  11980. /* args: timer_t timerid, struct itimerspec *curr_value */
  11981. target_timer_t timerid = get_timer_id(arg1);
  11982. if (timerid < 0) {
  11983. ret = timerid;
  11984. } else if (!arg2) {
  11985. ret = -TARGET_EFAULT;
  11986. } else {
  11987. timer_t htimer = g_posix_timers[timerid];
  11988. struct itimerspec hspec;
  11989. ret = get_errno(timer_gettime(htimer, &hspec));
  11990. if (host_to_target_itimerspec(arg2, &hspec)) {
  11991. ret = -TARGET_EFAULT;
  11992. }
  11993. }
  11994. return ret;
  11995. }
  11996. #endif
  11997. #ifdef TARGET_NR_timer_gettime64
  11998. case TARGET_NR_timer_gettime64:
  11999. {
  12000. /* args: timer_t timerid, struct itimerspec64 *curr_value */
  12001. target_timer_t timerid = get_timer_id(arg1);
  12002. if (timerid < 0) {
  12003. ret = timerid;
  12004. } else if (!arg2) {
  12005. ret = -TARGET_EFAULT;
  12006. } else {
  12007. timer_t htimer = g_posix_timers[timerid];
  12008. struct itimerspec hspec;
  12009. ret = get_errno(timer_gettime(htimer, &hspec));
  12010. if (host_to_target_itimerspec64(arg2, &hspec)) {
  12011. ret = -TARGET_EFAULT;
  12012. }
  12013. }
  12014. return ret;
  12015. }
  12016. #endif
  12017. #ifdef TARGET_NR_timer_getoverrun
  12018. case TARGET_NR_timer_getoverrun:
  12019. {
  12020. /* args: timer_t timerid */
  12021. target_timer_t timerid = get_timer_id(arg1);
  12022. if (timerid < 0) {
  12023. ret = timerid;
  12024. } else {
  12025. timer_t htimer = g_posix_timers[timerid];
  12026. ret = get_errno(timer_getoverrun(htimer));
  12027. }
  12028. return ret;
  12029. }
  12030. #endif
  12031. #ifdef TARGET_NR_timer_delete
  12032. case TARGET_NR_timer_delete:
  12033. {
  12034. /* args: timer_t timerid */
  12035. target_timer_t timerid = get_timer_id(arg1);
  12036. if (timerid < 0) {
  12037. ret = timerid;
  12038. } else {
  12039. timer_t htimer = g_posix_timers[timerid];
  12040. ret = get_errno(timer_delete(htimer));
  12041. free_host_timer_slot(timerid);
  12042. }
  12043. return ret;
  12044. }
  12045. #endif
  12046. #if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
  12047. case TARGET_NR_timerfd_create:
  12048. ret = get_errno(timerfd_create(arg1,
  12049. target_to_host_bitmask(arg2, fcntl_flags_tbl)));
  12050. if (ret >= 0) {
  12051. fd_trans_register(ret, &target_timerfd_trans);
  12052. }
  12053. return ret;
  12054. #endif
  12055. #if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
  12056. case TARGET_NR_timerfd_gettime:
  12057. {
  12058. struct itimerspec its_curr;
  12059. ret = get_errno(timerfd_gettime(arg1, &its_curr));
  12060. if (arg2 && host_to_target_itimerspec(arg2, &its_curr)) {
  12061. return -TARGET_EFAULT;
  12062. }
  12063. }
  12064. return ret;
  12065. #endif
  12066. #if defined(TARGET_NR_timerfd_gettime64) && defined(CONFIG_TIMERFD)
  12067. case TARGET_NR_timerfd_gettime64:
  12068. {
  12069. struct itimerspec its_curr;
  12070. ret = get_errno(timerfd_gettime(arg1, &its_curr));
  12071. if (arg2 && host_to_target_itimerspec64(arg2, &its_curr)) {
  12072. return -TARGET_EFAULT;
  12073. }
  12074. }
  12075. return ret;
  12076. #endif
  12077. #if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
  12078. case TARGET_NR_timerfd_settime:
  12079. {
  12080. struct itimerspec its_new, its_old, *p_new;
  12081. if (arg3) {
  12082. if (target_to_host_itimerspec(&its_new, arg3)) {
  12083. return -TARGET_EFAULT;
  12084. }
  12085. p_new = &its_new;
  12086. } else {
  12087. p_new = NULL;
  12088. }
  12089. ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
  12090. if (arg4 && host_to_target_itimerspec(arg4, &its_old)) {
  12091. return -TARGET_EFAULT;
  12092. }
  12093. }
  12094. return ret;
  12095. #endif
  12096. #if defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)
  12097. case TARGET_NR_timerfd_settime64:
  12098. {
  12099. struct itimerspec its_new, its_old, *p_new;
  12100. if (arg3) {
  12101. if (target_to_host_itimerspec64(&its_new, arg3)) {
  12102. return -TARGET_EFAULT;
  12103. }
  12104. p_new = &its_new;
  12105. } else {
  12106. p_new = NULL;
  12107. }
  12108. ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
  12109. if (arg4 && host_to_target_itimerspec64(arg4, &its_old)) {
  12110. return -TARGET_EFAULT;
  12111. }
  12112. }
  12113. return ret;
  12114. #endif
  12115. #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
  12116. case TARGET_NR_ioprio_get:
  12117. return get_errno(ioprio_get(arg1, arg2));
  12118. #endif
  12119. #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
  12120. case TARGET_NR_ioprio_set:
  12121. return get_errno(ioprio_set(arg1, arg2, arg3));
  12122. #endif
  12123. #if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
  12124. case TARGET_NR_setns:
  12125. return get_errno(setns(arg1, arg2));
  12126. #endif
  12127. #if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
  12128. case TARGET_NR_unshare:
  12129. return get_errno(unshare(arg1));
  12130. #endif
  12131. #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
  12132. case TARGET_NR_kcmp:
  12133. return get_errno(kcmp(arg1, arg2, arg3, arg4, arg5));
  12134. #endif
  12135. #ifdef TARGET_NR_swapcontext
  12136. case TARGET_NR_swapcontext:
  12137. /* PowerPC specific. */
  12138. return do_swapcontext(cpu_env, arg1, arg2, arg3);
  12139. #endif
  12140. #ifdef TARGET_NR_memfd_create
  12141. case TARGET_NR_memfd_create:
  12142. p = lock_user_string(arg1);
  12143. if (!p) {
  12144. return -TARGET_EFAULT;
  12145. }
  12146. ret = get_errno(memfd_create(p, arg2));
  12147. fd_trans_unregister(ret);
  12148. unlock_user(p, arg1, 0);
  12149. return ret;
  12150. #endif
  12151. #if defined TARGET_NR_membarrier && defined __NR_membarrier
  12152. case TARGET_NR_membarrier:
  12153. return get_errno(membarrier(arg1, arg2));
  12154. #endif
  12155. #if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range)
  12156. case TARGET_NR_copy_file_range:
  12157. {
  12158. loff_t inoff, outoff;
  12159. loff_t *pinoff = NULL, *poutoff = NULL;
  12160. if (arg2) {
  12161. if (get_user_u64(inoff, arg2)) {
  12162. return -TARGET_EFAULT;
  12163. }
  12164. pinoff = &inoff;
  12165. }
  12166. if (arg4) {
  12167. if (get_user_u64(outoff, arg4)) {
  12168. return -TARGET_EFAULT;
  12169. }
  12170. poutoff = &outoff;
  12171. }
  12172. /* Do not sign-extend the count parameter. */
  12173. ret = get_errno(safe_copy_file_range(arg1, pinoff, arg3, poutoff,
  12174. (abi_ulong)arg5, arg6));
  12175. if (!is_error(ret) && ret > 0) {
  12176. if (arg2) {
  12177. if (put_user_u64(inoff, arg2)) {
  12178. return -TARGET_EFAULT;
  12179. }
  12180. }
  12181. if (arg4) {
  12182. if (put_user_u64(outoff, arg4)) {
  12183. return -TARGET_EFAULT;
  12184. }
  12185. }
  12186. }
  12187. }
  12188. return ret;
  12189. #endif
  12190. #if defined(TARGET_NR_pivot_root)
  12191. case TARGET_NR_pivot_root:
  12192. {
  12193. void *p2;
  12194. p = lock_user_string(arg1); /* new_root */
  12195. p2 = lock_user_string(arg2); /* put_old */
  12196. if (!p || !p2) {
  12197. ret = -TARGET_EFAULT;
  12198. } else {
  12199. ret = get_errno(pivot_root(p, p2));
  12200. }
  12201. unlock_user(p2, arg2, 0);
  12202. unlock_user(p, arg1, 0);
  12203. }
  12204. return ret;
  12205. #endif
  12206. default:
  12207. qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num);
  12208. return -TARGET_ENOSYS;
  12209. }
  12210. return ret;
  12211. }
  12212. abi_long do_syscall(CPUArchState *cpu_env, int num, abi_long arg1,
  12213. abi_long arg2, abi_long arg3, abi_long arg4,
  12214. abi_long arg5, abi_long arg6, abi_long arg7,
  12215. abi_long arg8)
  12216. {
  12217. CPUState *cpu = env_cpu(cpu_env);
  12218. abi_long ret;
  12219. #ifdef DEBUG_ERESTARTSYS
  12220. /* Debug-only code for exercising the syscall-restart code paths
  12221. * in the per-architecture cpu main loops: restart every syscall
  12222. * the guest makes once before letting it through.
  12223. */
  12224. {
  12225. static bool flag;
  12226. flag = !flag;
  12227. if (flag) {
  12228. return -QEMU_ERESTARTSYS;
  12229. }
  12230. }
  12231. #endif
  12232. record_syscall_start(cpu, num, arg1,
  12233. arg2, arg3, arg4, arg5, arg6, arg7, arg8);
  12234. if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
  12235. print_syscall(cpu_env, num, arg1, arg2, arg3, arg4, arg5, arg6);
  12236. }
  12237. ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4,
  12238. arg5, arg6, arg7, arg8);
  12239. if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
  12240. print_syscall_ret(cpu_env, num, ret, arg1, arg2,
  12241. arg3, arg4, arg5, arg6);
  12242. }
  12243. record_syscall_return(cpu, num, ret);
  12244. return ret;
  12245. }