syscall.c 418 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105
  1. /*
  2. * Linux syscalls
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #define _ATFILE_SOURCE
  20. #include "qemu/osdep.h"
  21. #include "qemu/cutils.h"
  22. #include "qemu/path.h"
  23. #include "qemu/memfd.h"
  24. #include "qemu/queue.h"
  25. #include <elf.h>
  26. #include <endian.h>
  27. #include <grp.h>
  28. #include <sys/ipc.h>
  29. #include <sys/msg.h>
  30. #include <sys/wait.h>
  31. #include <sys/mount.h>
  32. #include <sys/file.h>
  33. #include <sys/fsuid.h>
  34. #include <sys/personality.h>
  35. #include <sys/prctl.h>
  36. #include <sys/resource.h>
  37. #include <sys/swap.h>
  38. #include <linux/capability.h>
  39. #include <sched.h>
  40. #include <sys/timex.h>
  41. #include <sys/socket.h>
  42. #include <linux/sockios.h>
  43. #include <sys/un.h>
  44. #include <sys/uio.h>
  45. #include <poll.h>
  46. #include <sys/times.h>
  47. #include <sys/shm.h>
  48. #include <sys/sem.h>
  49. #include <sys/statfs.h>
  50. #include <utime.h>
  51. #include <sys/sysinfo.h>
  52. #include <sys/signalfd.h>
  53. //#include <sys/user.h>
  54. #include <netinet/ip.h>
  55. #include <netinet/tcp.h>
  56. #include <linux/wireless.h>
  57. #include <linux/icmp.h>
  58. #include <linux/icmpv6.h>
  59. #include <linux/errqueue.h>
  60. #include <linux/random.h>
  61. #ifdef CONFIG_TIMERFD
  62. #include <sys/timerfd.h>
  63. #endif
  64. #ifdef CONFIG_EVENTFD
  65. #include <sys/eventfd.h>
  66. #endif
  67. #ifdef CONFIG_EPOLL
  68. #include <sys/epoll.h>
  69. #endif
  70. #ifdef CONFIG_ATTR
  71. #include "qemu/xattr.h"
  72. #endif
  73. #ifdef CONFIG_SENDFILE
  74. #include <sys/sendfile.h>
  75. #endif
  76. #ifdef CONFIG_KCOV
  77. #include <sys/kcov.h>
  78. #endif
  79. #define termios host_termios
  80. #define winsize host_winsize
  81. #define termio host_termio
  82. #define sgttyb host_sgttyb /* same as target */
  83. #define tchars host_tchars /* same as target */
  84. #define ltchars host_ltchars /* same as target */
  85. #include <linux/termios.h>
  86. #include <linux/unistd.h>
  87. #include <linux/cdrom.h>
  88. #include <linux/hdreg.h>
  89. #include <linux/soundcard.h>
  90. #include <linux/kd.h>
  91. #include <linux/mtio.h>
  92. #include <linux/fs.h>
  93. #include <linux/fd.h>
  94. #if defined(CONFIG_FIEMAP)
  95. #include <linux/fiemap.h>
  96. #endif
  97. #include <linux/fb.h>
  98. #if defined(CONFIG_USBFS)
  99. #include <linux/usbdevice_fs.h>
  100. #include <linux/usb/ch9.h>
  101. #endif
  102. #include <linux/vt.h>
  103. #include <linux/dm-ioctl.h>
  104. #include <linux/reboot.h>
  105. #include <linux/route.h>
  106. #include <linux/filter.h>
  107. #include <linux/blkpg.h>
  108. #include <netpacket/packet.h>
  109. #include <linux/netlink.h>
  110. #include <linux/if_alg.h>
  111. #include <linux/rtc.h>
  112. #include <sound/asound.h>
  113. #ifdef CONFIG_BTRFS
  114. #include <linux/btrfs.h>
  115. #endif
  116. #ifdef HAVE_DRM_H
  117. #include <libdrm/drm.h>
  118. #include <libdrm/i915_drm.h>
  119. #endif
  120. #include "linux_loop.h"
  121. #include "uname.h"
  122. #include "qemu.h"
  123. #include "qemu/guest-random.h"
  124. #include "qemu/selfmap.h"
  125. #include "user/syscall-trace.h"
  126. #include "qapi/error.h"
  127. #include "fd-trans.h"
  128. #include "tcg/tcg.h"
  129. #ifndef CLONE_IO
  130. #define CLONE_IO 0x80000000 /* Clone io context */
  131. #endif
  132. /* We can't directly call the host clone syscall, because this will
  133. * badly confuse libc (breaking mutexes, for example). So we must
  134. * divide clone flags into:
  135. * * flag combinations that look like pthread_create()
  136. * * flag combinations that look like fork()
  137. * * flags we can implement within QEMU itself
  138. * * flags we can't support and will return an error for
  139. */
  140. /* For thread creation, all these flags must be present; for
  141. * fork, none must be present.
  142. */
  143. #define CLONE_THREAD_FLAGS \
  144. (CLONE_VM | CLONE_FS | CLONE_FILES | \
  145. CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM)
  146. /* These flags are ignored:
  147. * CLONE_DETACHED is now ignored by the kernel;
  148. * CLONE_IO is just an optimisation hint to the I/O scheduler
  149. */
  150. #define CLONE_IGNORED_FLAGS \
  151. (CLONE_DETACHED | CLONE_IO)
  152. /* Flags for fork which we can implement within QEMU itself */
  153. #define CLONE_OPTIONAL_FORK_FLAGS \
  154. (CLONE_SETTLS | CLONE_PARENT_SETTID | \
  155. CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID)
  156. /* Flags for thread creation which we can implement within QEMU itself */
  157. #define CLONE_OPTIONAL_THREAD_FLAGS \
  158. (CLONE_SETTLS | CLONE_PARENT_SETTID | \
  159. CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | CLONE_PARENT)
  160. #define CLONE_INVALID_FORK_FLAGS \
  161. (~(CSIGNAL | CLONE_OPTIONAL_FORK_FLAGS | CLONE_IGNORED_FLAGS))
  162. #define CLONE_INVALID_THREAD_FLAGS \
  163. (~(CSIGNAL | CLONE_THREAD_FLAGS | CLONE_OPTIONAL_THREAD_FLAGS | \
  164. CLONE_IGNORED_FLAGS))
  165. /* CLONE_VFORK is special cased early in do_fork(). The other flag bits
  166. * have almost all been allocated. We cannot support any of
  167. * CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC,
  168. * CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, CLONE_PTRACE, CLONE_UNTRACED.
  169. * The checks against the invalid thread masks above will catch these.
  170. * (The one remaining unallocated bit is 0x1000 which used to be CLONE_PID.)
  171. */
  172. /* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
  173. * once. This exercises the codepaths for restart.
  174. */
  175. //#define DEBUG_ERESTARTSYS
  176. //#include <linux/msdos_fs.h>
  177. #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
  178. #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
  179. #undef _syscall0
  180. #undef _syscall1
  181. #undef _syscall2
  182. #undef _syscall3
  183. #undef _syscall4
  184. #undef _syscall5
  185. #undef _syscall6
  186. #define _syscall0(type,name) \
  187. static type name (void) \
  188. { \
  189. return syscall(__NR_##name); \
  190. }
  191. #define _syscall1(type,name,type1,arg1) \
  192. static type name (type1 arg1) \
  193. { \
  194. return syscall(__NR_##name, arg1); \
  195. }
  196. #define _syscall2(type,name,type1,arg1,type2,arg2) \
  197. static type name (type1 arg1,type2 arg2) \
  198. { \
  199. return syscall(__NR_##name, arg1, arg2); \
  200. }
  201. #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
  202. static type name (type1 arg1,type2 arg2,type3 arg3) \
  203. { \
  204. return syscall(__NR_##name, arg1, arg2, arg3); \
  205. }
  206. #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
  207. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
  208. { \
  209. return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
  210. }
  211. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  212. type5,arg5) \
  213. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
  214. { \
  215. return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
  216. }
  217. #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  218. type5,arg5,type6,arg6) \
  219. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
  220. type6 arg6) \
  221. { \
  222. return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
  223. }
  224. #define __NR_sys_uname __NR_uname
  225. #define __NR_sys_getcwd1 __NR_getcwd
  226. #define __NR_sys_getdents __NR_getdents
  227. #define __NR_sys_getdents64 __NR_getdents64
  228. #define __NR_sys_getpriority __NR_getpriority
  229. #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
  230. #define __NR_sys_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo
  231. #define __NR_sys_syslog __NR_syslog
  232. #if defined(__NR_futex)
  233. # define __NR_sys_futex __NR_futex
  234. #endif
  235. #if defined(__NR_futex_time64)
  236. # define __NR_sys_futex_time64 __NR_futex_time64
  237. #endif
  238. #define __NR_sys_inotify_init __NR_inotify_init
  239. #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
  240. #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
  241. #define __NR_sys_statx __NR_statx
  242. #if defined(__alpha__) || defined(__x86_64__) || defined(__s390x__)
  243. #define __NR__llseek __NR_lseek
  244. #endif
  245. /* Newer kernel ports have llseek() instead of _llseek() */
  246. #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
  247. #define TARGET_NR__llseek TARGET_NR_llseek
  248. #endif
  249. #define __NR_sys_gettid __NR_gettid
  250. _syscall0(int, sys_gettid)
  251. /* For the 64-bit guest on 32-bit host case we must emulate
  252. * getdents using getdents64, because otherwise the host
  253. * might hand us back more dirent records than we can fit
  254. * into the guest buffer after structure format conversion.
  255. * Otherwise we emulate getdents with getdents if the host has it.
  256. */
  257. #if defined(__NR_getdents) && HOST_LONG_BITS >= TARGET_ABI_BITS
  258. #define EMULATE_GETDENTS_WITH_GETDENTS
  259. #endif
  260. #if defined(TARGET_NR_getdents) && defined(EMULATE_GETDENTS_WITH_GETDENTS)
  261. _syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
  262. #endif
  263. #if (defined(TARGET_NR_getdents) && \
  264. !defined(EMULATE_GETDENTS_WITH_GETDENTS)) || \
  265. (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
  266. _syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
  267. #endif
  268. #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
  269. _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
  270. loff_t *, res, uint, wh);
  271. #endif
  272. _syscall3(int, sys_rt_sigqueueinfo, pid_t, pid, int, sig, siginfo_t *, uinfo)
  273. _syscall4(int, sys_rt_tgsigqueueinfo, pid_t, pid, pid_t, tid, int, sig,
  274. siginfo_t *, uinfo)
  275. _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
  276. #ifdef __NR_exit_group
  277. _syscall1(int,exit_group,int,error_code)
  278. #endif
  279. #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
  280. _syscall1(int,set_tid_address,int *,tidptr)
  281. #endif
  282. #if defined(__NR_futex)
  283. _syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
  284. const struct timespec *,timeout,int *,uaddr2,int,val3)
  285. #endif
  286. #if defined(__NR_futex_time64)
  287. _syscall6(int,sys_futex_time64,int *,uaddr,int,op,int,val,
  288. const struct timespec *,timeout,int *,uaddr2,int,val3)
  289. #endif
  290. #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
  291. _syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
  292. unsigned long *, user_mask_ptr);
  293. #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
  294. _syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
  295. unsigned long *, user_mask_ptr);
  296. #define __NR_sys_getcpu __NR_getcpu
  297. _syscall3(int, sys_getcpu, unsigned *, cpu, unsigned *, node, void *, tcache);
  298. _syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
  299. void *, arg);
  300. _syscall2(int, capget, struct __user_cap_header_struct *, header,
  301. struct __user_cap_data_struct *, data);
  302. _syscall2(int, capset, struct __user_cap_header_struct *, header,
  303. struct __user_cap_data_struct *, data);
  304. #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
  305. _syscall2(int, ioprio_get, int, which, int, who)
  306. #endif
  307. #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
  308. _syscall3(int, ioprio_set, int, which, int, who, int, ioprio)
  309. #endif
  310. #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
  311. _syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags)
  312. #endif
  313. #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
  314. _syscall5(int, kcmp, pid_t, pid1, pid_t, pid2, int, type,
  315. unsigned long, idx1, unsigned long, idx2)
  316. #endif
  317. /*
  318. * It is assumed that struct statx is architecture independent.
  319. */
  320. #if defined(TARGET_NR_statx) && defined(__NR_statx)
  321. _syscall5(int, sys_statx, int, dirfd, const char *, pathname, int, flags,
  322. unsigned int, mask, struct target_statx *, statxbuf)
  323. #endif
  324. #if defined(TARGET_NR_membarrier) && defined(__NR_membarrier)
  325. _syscall2(int, membarrier, int, cmd, int, flags)
  326. #endif
  327. static bitmask_transtbl fcntl_flags_tbl[] = {
  328. { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
  329. { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
  330. { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
  331. { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
  332. { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
  333. { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
  334. { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
  335. { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
  336. { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
  337. { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
  338. { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
  339. { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
  340. { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
  341. #if defined(O_DIRECT)
  342. { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
  343. #endif
  344. #if defined(O_NOATIME)
  345. { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
  346. #endif
  347. #if defined(O_CLOEXEC)
  348. { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
  349. #endif
  350. #if defined(O_PATH)
  351. { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
  352. #endif
  353. #if defined(O_TMPFILE)
  354. { TARGET_O_TMPFILE, TARGET_O_TMPFILE, O_TMPFILE, O_TMPFILE },
  355. #endif
  356. /* Don't terminate the list prematurely on 64-bit host+guest. */
  357. #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
  358. { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
  359. #endif
  360. { 0, 0, 0, 0 }
  361. };
  362. _syscall2(int, sys_getcwd1, char *, buf, size_t, size)
  363. #if defined(TARGET_NR_utimensat) || defined(TARGET_NR_utimensat_time64)
  364. #if defined(__NR_utimensat)
  365. #define __NR_sys_utimensat __NR_utimensat
  366. _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
  367. const struct timespec *,tsp,int,flags)
  368. #else
  369. static int sys_utimensat(int dirfd, const char *pathname,
  370. const struct timespec times[2], int flags)
  371. {
  372. errno = ENOSYS;
  373. return -1;
  374. }
  375. #endif
  376. #endif /* TARGET_NR_utimensat */
  377. #ifdef TARGET_NR_renameat2
  378. #if defined(__NR_renameat2)
  379. #define __NR_sys_renameat2 __NR_renameat2
  380. _syscall5(int, sys_renameat2, int, oldfd, const char *, old, int, newfd,
  381. const char *, new, unsigned int, flags)
  382. #else
  383. static int sys_renameat2(int oldfd, const char *old,
  384. int newfd, const char *new, int flags)
  385. {
  386. if (flags == 0) {
  387. return renameat(oldfd, old, newfd, new);
  388. }
  389. errno = ENOSYS;
  390. return -1;
  391. }
  392. #endif
  393. #endif /* TARGET_NR_renameat2 */
  394. #ifdef CONFIG_INOTIFY
  395. #include <sys/inotify.h>
  396. #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
  397. static int sys_inotify_init(void)
  398. {
  399. return (inotify_init());
  400. }
  401. #endif
  402. #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
  403. static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
  404. {
  405. return (inotify_add_watch(fd, pathname, mask));
  406. }
  407. #endif
  408. #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
  409. static int sys_inotify_rm_watch(int fd, int32_t wd)
  410. {
  411. return (inotify_rm_watch(fd, wd));
  412. }
  413. #endif
  414. #ifdef CONFIG_INOTIFY1
  415. #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
  416. static int sys_inotify_init1(int flags)
  417. {
  418. return (inotify_init1(flags));
  419. }
  420. #endif
  421. #endif
  422. #else
  423. /* Userspace can usually survive runtime without inotify */
  424. #undef TARGET_NR_inotify_init
  425. #undef TARGET_NR_inotify_init1
  426. #undef TARGET_NR_inotify_add_watch
  427. #undef TARGET_NR_inotify_rm_watch
  428. #endif /* CONFIG_INOTIFY */
  429. #if defined(TARGET_NR_prlimit64)
  430. #ifndef __NR_prlimit64
  431. # define __NR_prlimit64 -1
  432. #endif
  433. #define __NR_sys_prlimit64 __NR_prlimit64
  434. /* The glibc rlimit structure may not be that used by the underlying syscall */
  435. struct host_rlimit64 {
  436. uint64_t rlim_cur;
  437. uint64_t rlim_max;
  438. };
  439. _syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
  440. const struct host_rlimit64 *, new_limit,
  441. struct host_rlimit64 *, old_limit)
  442. #endif
  443. #if defined(TARGET_NR_timer_create)
  444. /* Maximum of 32 active POSIX timers allowed at any one time. */
  445. static timer_t g_posix_timers[32] = { 0, } ;
  446. static inline int next_free_host_timer(void)
  447. {
  448. int k ;
  449. /* FIXME: Does finding the next free slot require a lock? */
  450. for (k = 0; k < ARRAY_SIZE(g_posix_timers); k++) {
  451. if (g_posix_timers[k] == 0) {
  452. g_posix_timers[k] = (timer_t) 1;
  453. return k;
  454. }
  455. }
  456. return -1;
  457. }
  458. #endif
  459. #define ERRNO_TABLE_SIZE 1200
  460. /* target_to_host_errno_table[] is initialized from
  461. * host_to_target_errno_table[] in syscall_init(). */
  462. static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
  463. };
  464. /*
  465. * This list is the union of errno values overridden in asm-<arch>/errno.h
  466. * minus the errnos that are not actually generic to all archs.
  467. */
  468. static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
  469. [EAGAIN] = TARGET_EAGAIN,
  470. [EIDRM] = TARGET_EIDRM,
  471. [ECHRNG] = TARGET_ECHRNG,
  472. [EL2NSYNC] = TARGET_EL2NSYNC,
  473. [EL3HLT] = TARGET_EL3HLT,
  474. [EL3RST] = TARGET_EL3RST,
  475. [ELNRNG] = TARGET_ELNRNG,
  476. [EUNATCH] = TARGET_EUNATCH,
  477. [ENOCSI] = TARGET_ENOCSI,
  478. [EL2HLT] = TARGET_EL2HLT,
  479. [EDEADLK] = TARGET_EDEADLK,
  480. [ENOLCK] = TARGET_ENOLCK,
  481. [EBADE] = TARGET_EBADE,
  482. [EBADR] = TARGET_EBADR,
  483. [EXFULL] = TARGET_EXFULL,
  484. [ENOANO] = TARGET_ENOANO,
  485. [EBADRQC] = TARGET_EBADRQC,
  486. [EBADSLT] = TARGET_EBADSLT,
  487. [EBFONT] = TARGET_EBFONT,
  488. [ENOSTR] = TARGET_ENOSTR,
  489. [ENODATA] = TARGET_ENODATA,
  490. [ETIME] = TARGET_ETIME,
  491. [ENOSR] = TARGET_ENOSR,
  492. [ENONET] = TARGET_ENONET,
  493. [ENOPKG] = TARGET_ENOPKG,
  494. [EREMOTE] = TARGET_EREMOTE,
  495. [ENOLINK] = TARGET_ENOLINK,
  496. [EADV] = TARGET_EADV,
  497. [ESRMNT] = TARGET_ESRMNT,
  498. [ECOMM] = TARGET_ECOMM,
  499. [EPROTO] = TARGET_EPROTO,
  500. [EDOTDOT] = TARGET_EDOTDOT,
  501. [EMULTIHOP] = TARGET_EMULTIHOP,
  502. [EBADMSG] = TARGET_EBADMSG,
  503. [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
  504. [EOVERFLOW] = TARGET_EOVERFLOW,
  505. [ENOTUNIQ] = TARGET_ENOTUNIQ,
  506. [EBADFD] = TARGET_EBADFD,
  507. [EREMCHG] = TARGET_EREMCHG,
  508. [ELIBACC] = TARGET_ELIBACC,
  509. [ELIBBAD] = TARGET_ELIBBAD,
  510. [ELIBSCN] = TARGET_ELIBSCN,
  511. [ELIBMAX] = TARGET_ELIBMAX,
  512. [ELIBEXEC] = TARGET_ELIBEXEC,
  513. [EILSEQ] = TARGET_EILSEQ,
  514. [ENOSYS] = TARGET_ENOSYS,
  515. [ELOOP] = TARGET_ELOOP,
  516. [ERESTART] = TARGET_ERESTART,
  517. [ESTRPIPE] = TARGET_ESTRPIPE,
  518. [ENOTEMPTY] = TARGET_ENOTEMPTY,
  519. [EUSERS] = TARGET_EUSERS,
  520. [ENOTSOCK] = TARGET_ENOTSOCK,
  521. [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
  522. [EMSGSIZE] = TARGET_EMSGSIZE,
  523. [EPROTOTYPE] = TARGET_EPROTOTYPE,
  524. [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
  525. [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
  526. [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
  527. [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
  528. [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
  529. [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
  530. [EADDRINUSE] = TARGET_EADDRINUSE,
  531. [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
  532. [ENETDOWN] = TARGET_ENETDOWN,
  533. [ENETUNREACH] = TARGET_ENETUNREACH,
  534. [ENETRESET] = TARGET_ENETRESET,
  535. [ECONNABORTED] = TARGET_ECONNABORTED,
  536. [ECONNRESET] = TARGET_ECONNRESET,
  537. [ENOBUFS] = TARGET_ENOBUFS,
  538. [EISCONN] = TARGET_EISCONN,
  539. [ENOTCONN] = TARGET_ENOTCONN,
  540. [EUCLEAN] = TARGET_EUCLEAN,
  541. [ENOTNAM] = TARGET_ENOTNAM,
  542. [ENAVAIL] = TARGET_ENAVAIL,
  543. [EISNAM] = TARGET_EISNAM,
  544. [EREMOTEIO] = TARGET_EREMOTEIO,
  545. [EDQUOT] = TARGET_EDQUOT,
  546. [ESHUTDOWN] = TARGET_ESHUTDOWN,
  547. [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
  548. [ETIMEDOUT] = TARGET_ETIMEDOUT,
  549. [ECONNREFUSED] = TARGET_ECONNREFUSED,
  550. [EHOSTDOWN] = TARGET_EHOSTDOWN,
  551. [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
  552. [EALREADY] = TARGET_EALREADY,
  553. [EINPROGRESS] = TARGET_EINPROGRESS,
  554. [ESTALE] = TARGET_ESTALE,
  555. [ECANCELED] = TARGET_ECANCELED,
  556. [ENOMEDIUM] = TARGET_ENOMEDIUM,
  557. [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
  558. #ifdef ENOKEY
  559. [ENOKEY] = TARGET_ENOKEY,
  560. #endif
  561. #ifdef EKEYEXPIRED
  562. [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
  563. #endif
  564. #ifdef EKEYREVOKED
  565. [EKEYREVOKED] = TARGET_EKEYREVOKED,
  566. #endif
  567. #ifdef EKEYREJECTED
  568. [EKEYREJECTED] = TARGET_EKEYREJECTED,
  569. #endif
  570. #ifdef EOWNERDEAD
  571. [EOWNERDEAD] = TARGET_EOWNERDEAD,
  572. #endif
  573. #ifdef ENOTRECOVERABLE
  574. [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
  575. #endif
  576. #ifdef ENOMSG
  577. [ENOMSG] = TARGET_ENOMSG,
  578. #endif
  579. #ifdef ERKFILL
  580. [ERFKILL] = TARGET_ERFKILL,
  581. #endif
  582. #ifdef EHWPOISON
  583. [EHWPOISON] = TARGET_EHWPOISON,
  584. #endif
  585. };
  586. static inline int host_to_target_errno(int err)
  587. {
  588. if (err >= 0 && err < ERRNO_TABLE_SIZE &&
  589. host_to_target_errno_table[err]) {
  590. return host_to_target_errno_table[err];
  591. }
  592. return err;
  593. }
  594. static inline int target_to_host_errno(int err)
  595. {
  596. if (err >= 0 && err < ERRNO_TABLE_SIZE &&
  597. target_to_host_errno_table[err]) {
  598. return target_to_host_errno_table[err];
  599. }
  600. return err;
  601. }
  602. static inline abi_long get_errno(abi_long ret)
  603. {
  604. if (ret == -1)
  605. return -host_to_target_errno(errno);
  606. else
  607. return ret;
  608. }
  609. const char *target_strerror(int err)
  610. {
  611. if (err == TARGET_ERESTARTSYS) {
  612. return "To be restarted";
  613. }
  614. if (err == TARGET_QEMU_ESIGRETURN) {
  615. return "Successful exit from sigreturn";
  616. }
  617. if ((err >= ERRNO_TABLE_SIZE) || (err < 0)) {
  618. return NULL;
  619. }
  620. return strerror(target_to_host_errno(err));
  621. }
  622. #define safe_syscall0(type, name) \
  623. static type safe_##name(void) \
  624. { \
  625. return safe_syscall(__NR_##name); \
  626. }
  627. #define safe_syscall1(type, name, type1, arg1) \
  628. static type safe_##name(type1 arg1) \
  629. { \
  630. return safe_syscall(__NR_##name, arg1); \
  631. }
  632. #define safe_syscall2(type, name, type1, arg1, type2, arg2) \
  633. static type safe_##name(type1 arg1, type2 arg2) \
  634. { \
  635. return safe_syscall(__NR_##name, arg1, arg2); \
  636. }
  637. #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
  638. static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
  639. { \
  640. return safe_syscall(__NR_##name, arg1, arg2, arg3); \
  641. }
  642. #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
  643. type4, arg4) \
  644. static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
  645. { \
  646. return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
  647. }
  648. #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
  649. type4, arg4, type5, arg5) \
  650. static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  651. type5 arg5) \
  652. { \
  653. return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
  654. }
  655. #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
  656. type4, arg4, type5, arg5, type6, arg6) \
  657. static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  658. type5 arg5, type6 arg6) \
  659. { \
  660. return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
  661. }
  662. safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count)
  663. safe_syscall3(ssize_t, write, int, fd, const void *, buff, size_t, count)
  664. safe_syscall4(int, openat, int, dirfd, const char *, pathname, \
  665. int, flags, mode_t, mode)
  666. #if defined(TARGET_NR_wait4) || defined(TARGET_NR_waitpid)
  667. safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \
  668. struct rusage *, rusage)
  669. #endif
  670. safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \
  671. int, options, struct rusage *, rusage)
  672. safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp)
  673. #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
  674. defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
  675. safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \
  676. fd_set *, exceptfds, struct timespec *, timeout, void *, sig)
  677. #endif
  678. #if defined(TARGET_NR_ppoll) || defined(TARGET_NR_ppoll_time64)
  679. safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds,
  680. struct timespec *, tsp, const sigset_t *, sigmask,
  681. size_t, sigsetsize)
  682. #endif
  683. safe_syscall6(int, epoll_pwait, int, epfd, struct epoll_event *, events,
  684. int, maxevents, int, timeout, const sigset_t *, sigmask,
  685. size_t, sigsetsize)
  686. #if defined(__NR_futex)
  687. safe_syscall6(int,futex,int *,uaddr,int,op,int,val, \
  688. const struct timespec *,timeout,int *,uaddr2,int,val3)
  689. #endif
  690. #if defined(__NR_futex_time64)
  691. safe_syscall6(int,futex_time64,int *,uaddr,int,op,int,val, \
  692. const struct timespec *,timeout,int *,uaddr2,int,val3)
  693. #endif
  694. safe_syscall2(int, rt_sigsuspend, sigset_t *, newset, size_t, sigsetsize)
  695. safe_syscall2(int, kill, pid_t, pid, int, sig)
  696. safe_syscall2(int, tkill, int, tid, int, sig)
  697. safe_syscall3(int, tgkill, int, tgid, int, pid, int, sig)
  698. safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt)
  699. safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt)
  700. safe_syscall5(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt,
  701. unsigned long, pos_l, unsigned long, pos_h)
  702. safe_syscall5(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, iovcnt,
  703. unsigned long, pos_l, unsigned long, pos_h)
  704. safe_syscall3(int, connect, int, fd, const struct sockaddr *, addr,
  705. socklen_t, addrlen)
  706. safe_syscall6(ssize_t, sendto, int, fd, const void *, buf, size_t, len,
  707. int, flags, const struct sockaddr *, addr, socklen_t, addrlen)
  708. safe_syscall6(ssize_t, recvfrom, int, fd, void *, buf, size_t, len,
  709. int, flags, struct sockaddr *, addr, socklen_t *, addrlen)
  710. safe_syscall3(ssize_t, sendmsg, int, fd, const struct msghdr *, msg, int, flags)
  711. safe_syscall3(ssize_t, recvmsg, int, fd, struct msghdr *, msg, int, flags)
  712. safe_syscall2(int, flock, int, fd, int, operation)
  713. #if defined(TARGET_NR_rt_sigtimedwait) || defined(TARGET_NR_rt_sigtimedwait_time64)
  714. safe_syscall4(int, rt_sigtimedwait, const sigset_t *, these, siginfo_t *, uinfo,
  715. const struct timespec *, uts, size_t, sigsetsize)
  716. #endif
  717. safe_syscall4(int, accept4, int, fd, struct sockaddr *, addr, socklen_t *, len,
  718. int, flags)
  719. #if defined(TARGET_NR_nanosleep)
  720. safe_syscall2(int, nanosleep, const struct timespec *, req,
  721. struct timespec *, rem)
  722. #endif
  723. #if defined(TARGET_NR_clock_nanosleep) || \
  724. defined(TARGET_NR_clock_nanosleep_time64)
  725. safe_syscall4(int, clock_nanosleep, const clockid_t, clock, int, flags,
  726. const struct timespec *, req, struct timespec *, rem)
  727. #endif
  728. #ifdef __NR_ipc
  729. #ifdef __s390x__
  730. safe_syscall5(int, ipc, int, call, long, first, long, second, long, third,
  731. void *, ptr)
  732. #else
  733. safe_syscall6(int, ipc, int, call, long, first, long, second, long, third,
  734. void *, ptr, long, fifth)
  735. #endif
  736. #endif
  737. #ifdef __NR_msgsnd
  738. safe_syscall4(int, msgsnd, int, msgid, const void *, msgp, size_t, sz,
  739. int, flags)
  740. #endif
  741. #ifdef __NR_msgrcv
  742. safe_syscall5(int, msgrcv, int, msgid, void *, msgp, size_t, sz,
  743. long, msgtype, int, flags)
  744. #endif
  745. #ifdef __NR_semtimedop
  746. safe_syscall4(int, semtimedop, int, semid, struct sembuf *, tsops,
  747. unsigned, nsops, const struct timespec *, timeout)
  748. #endif
  749. #if defined(TARGET_NR_mq_timedsend) || \
  750. defined(TARGET_NR_mq_timedsend_time64)
  751. safe_syscall5(int, mq_timedsend, int, mqdes, const char *, msg_ptr,
  752. size_t, len, unsigned, prio, const struct timespec *, timeout)
  753. #endif
  754. #if defined(TARGET_NR_mq_timedreceive) || \
  755. defined(TARGET_NR_mq_timedreceive_time64)
  756. safe_syscall5(int, mq_timedreceive, int, mqdes, char *, msg_ptr,
  757. size_t, len, unsigned *, prio, const struct timespec *, timeout)
  758. #endif
  759. /* We do ioctl like this rather than via safe_syscall3 to preserve the
  760. * "third argument might be integer or pointer or not present" behaviour of
  761. * the libc function.
  762. */
  763. #define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
  764. /* Similarly for fcntl. Note that callers must always:
  765. * pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK
  766. * use the flock64 struct rather than unsuffixed flock
  767. * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts.
  768. */
  769. #ifdef __NR_fcntl64
  770. #define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
  771. #else
  772. #define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
  773. #endif
  774. static inline int host_to_target_sock_type(int host_type)
  775. {
  776. int target_type;
  777. switch (host_type & 0xf /* SOCK_TYPE_MASK */) {
  778. case SOCK_DGRAM:
  779. target_type = TARGET_SOCK_DGRAM;
  780. break;
  781. case SOCK_STREAM:
  782. target_type = TARGET_SOCK_STREAM;
  783. break;
  784. default:
  785. target_type = host_type & 0xf /* SOCK_TYPE_MASK */;
  786. break;
  787. }
  788. #if defined(SOCK_CLOEXEC)
  789. if (host_type & SOCK_CLOEXEC) {
  790. target_type |= TARGET_SOCK_CLOEXEC;
  791. }
  792. #endif
  793. #if defined(SOCK_NONBLOCK)
  794. if (host_type & SOCK_NONBLOCK) {
  795. target_type |= TARGET_SOCK_NONBLOCK;
  796. }
  797. #endif
  798. return target_type;
  799. }
  800. static abi_ulong target_brk;
  801. static abi_ulong target_original_brk;
  802. static abi_ulong brk_page;
  803. void target_set_brk(abi_ulong new_brk)
  804. {
  805. target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
  806. brk_page = HOST_PAGE_ALIGN(target_brk);
  807. }
  808. //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
  809. #define DEBUGF_BRK(message, args...)
  810. /* do_brk() must return target values and target errnos. */
  811. abi_long do_brk(abi_ulong new_brk)
  812. {
  813. abi_long mapped_addr;
  814. abi_ulong new_alloc_size;
  815. DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
  816. if (!new_brk) {
  817. DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
  818. return target_brk;
  819. }
  820. if (new_brk < target_original_brk) {
  821. DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
  822. target_brk);
  823. return target_brk;
  824. }
  825. /* If the new brk is less than the highest page reserved to the
  826. * target heap allocation, set it and we're almost done... */
  827. if (new_brk <= brk_page) {
  828. /* Heap contents are initialized to zero, as for anonymous
  829. * mapped pages. */
  830. if (new_brk > target_brk) {
  831. memset(g2h(target_brk), 0, new_brk - target_brk);
  832. }
  833. target_brk = new_brk;
  834. DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
  835. return target_brk;
  836. }
  837. /* We need to allocate more memory after the brk... Note that
  838. * we don't use MAP_FIXED because that will map over the top of
  839. * any existing mapping (like the one with the host libc or qemu
  840. * itself); instead we treat "mapped but at wrong address" as
  841. * a failure and unmap again.
  842. */
  843. new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
  844. mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
  845. PROT_READ|PROT_WRITE,
  846. MAP_ANON|MAP_PRIVATE, 0, 0));
  847. if (mapped_addr == brk_page) {
  848. /* Heap contents are initialized to zero, as for anonymous
  849. * mapped pages. Technically the new pages are already
  850. * initialized to zero since they *are* anonymous mapped
  851. * pages, however we have to take care with the contents that
  852. * come from the remaining part of the previous page: it may
  853. * contains garbage data due to a previous heap usage (grown
  854. * then shrunken). */
  855. memset(g2h(target_brk), 0, brk_page - target_brk);
  856. target_brk = new_brk;
  857. brk_page = HOST_PAGE_ALIGN(target_brk);
  858. DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
  859. target_brk);
  860. return target_brk;
  861. } else if (mapped_addr != -1) {
  862. /* Mapped but at wrong address, meaning there wasn't actually
  863. * enough space for this brk.
  864. */
  865. target_munmap(mapped_addr, new_alloc_size);
  866. mapped_addr = -1;
  867. DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
  868. }
  869. else {
  870. DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
  871. }
  872. #if defined(TARGET_ALPHA)
  873. /* We (partially) emulate OSF/1 on Alpha, which requires we
  874. return a proper errno, not an unchanged brk value. */
  875. return -TARGET_ENOMEM;
  876. #endif
  877. /* For everything else, return the previous break. */
  878. return target_brk;
  879. }
  880. #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
  881. defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
  882. static inline abi_long copy_from_user_fdset(fd_set *fds,
  883. abi_ulong target_fds_addr,
  884. int n)
  885. {
  886. int i, nw, j, k;
  887. abi_ulong b, *target_fds;
  888. nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
  889. if (!(target_fds = lock_user(VERIFY_READ,
  890. target_fds_addr,
  891. sizeof(abi_ulong) * nw,
  892. 1)))
  893. return -TARGET_EFAULT;
  894. FD_ZERO(fds);
  895. k = 0;
  896. for (i = 0; i < nw; i++) {
  897. /* grab the abi_ulong */
  898. __get_user(b, &target_fds[i]);
  899. for (j = 0; j < TARGET_ABI_BITS; j++) {
  900. /* check the bit inside the abi_ulong */
  901. if ((b >> j) & 1)
  902. FD_SET(k, fds);
  903. k++;
  904. }
  905. }
  906. unlock_user(target_fds, target_fds_addr, 0);
  907. return 0;
  908. }
  909. static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
  910. abi_ulong target_fds_addr,
  911. int n)
  912. {
  913. if (target_fds_addr) {
  914. if (copy_from_user_fdset(fds, target_fds_addr, n))
  915. return -TARGET_EFAULT;
  916. *fds_ptr = fds;
  917. } else {
  918. *fds_ptr = NULL;
  919. }
  920. return 0;
  921. }
  922. static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
  923. const fd_set *fds,
  924. int n)
  925. {
  926. int i, nw, j, k;
  927. abi_long v;
  928. abi_ulong *target_fds;
  929. nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
  930. if (!(target_fds = lock_user(VERIFY_WRITE,
  931. target_fds_addr,
  932. sizeof(abi_ulong) * nw,
  933. 0)))
  934. return -TARGET_EFAULT;
  935. k = 0;
  936. for (i = 0; i < nw; i++) {
  937. v = 0;
  938. for (j = 0; j < TARGET_ABI_BITS; j++) {
  939. v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
  940. k++;
  941. }
  942. __put_user(v, &target_fds[i]);
  943. }
  944. unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
  945. return 0;
  946. }
  947. #endif
  948. #if defined(__alpha__)
  949. #define HOST_HZ 1024
  950. #else
  951. #define HOST_HZ 100
  952. #endif
  953. static inline abi_long host_to_target_clock_t(long ticks)
  954. {
  955. #if HOST_HZ == TARGET_HZ
  956. return ticks;
  957. #else
  958. return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
  959. #endif
  960. }
  961. static inline abi_long host_to_target_rusage(abi_ulong target_addr,
  962. const struct rusage *rusage)
  963. {
  964. struct target_rusage *target_rusage;
  965. if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
  966. return -TARGET_EFAULT;
  967. target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
  968. target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
  969. target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
  970. target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
  971. target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
  972. target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
  973. target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
  974. target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
  975. target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
  976. target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
  977. target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
  978. target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
  979. target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
  980. target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
  981. target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
  982. target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
  983. target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
  984. target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
  985. unlock_user_struct(target_rusage, target_addr, 1);
  986. return 0;
  987. }
  988. #ifdef TARGET_NR_setrlimit
  989. static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
  990. {
  991. abi_ulong target_rlim_swap;
  992. rlim_t result;
  993. target_rlim_swap = tswapal(target_rlim);
  994. if (target_rlim_swap == TARGET_RLIM_INFINITY)
  995. return RLIM_INFINITY;
  996. result = target_rlim_swap;
  997. if (target_rlim_swap != (rlim_t)result)
  998. return RLIM_INFINITY;
  999. return result;
  1000. }
  1001. #endif
  1002. #if defined(TARGET_NR_getrlimit) || defined(TARGET_NR_ugetrlimit)
  1003. static inline abi_ulong host_to_target_rlim(rlim_t rlim)
  1004. {
  1005. abi_ulong target_rlim_swap;
  1006. abi_ulong result;
  1007. if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
  1008. target_rlim_swap = TARGET_RLIM_INFINITY;
  1009. else
  1010. target_rlim_swap = rlim;
  1011. result = tswapal(target_rlim_swap);
  1012. return result;
  1013. }
  1014. #endif
  1015. static inline int target_to_host_resource(int code)
  1016. {
  1017. switch (code) {
  1018. case TARGET_RLIMIT_AS:
  1019. return RLIMIT_AS;
  1020. case TARGET_RLIMIT_CORE:
  1021. return RLIMIT_CORE;
  1022. case TARGET_RLIMIT_CPU:
  1023. return RLIMIT_CPU;
  1024. case TARGET_RLIMIT_DATA:
  1025. return RLIMIT_DATA;
  1026. case TARGET_RLIMIT_FSIZE:
  1027. return RLIMIT_FSIZE;
  1028. case TARGET_RLIMIT_LOCKS:
  1029. return RLIMIT_LOCKS;
  1030. case TARGET_RLIMIT_MEMLOCK:
  1031. return RLIMIT_MEMLOCK;
  1032. case TARGET_RLIMIT_MSGQUEUE:
  1033. return RLIMIT_MSGQUEUE;
  1034. case TARGET_RLIMIT_NICE:
  1035. return RLIMIT_NICE;
  1036. case TARGET_RLIMIT_NOFILE:
  1037. return RLIMIT_NOFILE;
  1038. case TARGET_RLIMIT_NPROC:
  1039. return RLIMIT_NPROC;
  1040. case TARGET_RLIMIT_RSS:
  1041. return RLIMIT_RSS;
  1042. case TARGET_RLIMIT_RTPRIO:
  1043. return RLIMIT_RTPRIO;
  1044. case TARGET_RLIMIT_SIGPENDING:
  1045. return RLIMIT_SIGPENDING;
  1046. case TARGET_RLIMIT_STACK:
  1047. return RLIMIT_STACK;
  1048. default:
  1049. return code;
  1050. }
  1051. }
  1052. static inline abi_long copy_from_user_timeval(struct timeval *tv,
  1053. abi_ulong target_tv_addr)
  1054. {
  1055. struct target_timeval *target_tv;
  1056. if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) {
  1057. return -TARGET_EFAULT;
  1058. }
  1059. __get_user(tv->tv_sec, &target_tv->tv_sec);
  1060. __get_user(tv->tv_usec, &target_tv->tv_usec);
  1061. unlock_user_struct(target_tv, target_tv_addr, 0);
  1062. return 0;
  1063. }
  1064. static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
  1065. const struct timeval *tv)
  1066. {
  1067. struct target_timeval *target_tv;
  1068. if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
  1069. return -TARGET_EFAULT;
  1070. }
  1071. __put_user(tv->tv_sec, &target_tv->tv_sec);
  1072. __put_user(tv->tv_usec, &target_tv->tv_usec);
  1073. unlock_user_struct(target_tv, target_tv_addr, 1);
  1074. return 0;
  1075. }
  1076. #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
  1077. static inline abi_long copy_from_user_timeval64(struct timeval *tv,
  1078. abi_ulong target_tv_addr)
  1079. {
  1080. struct target__kernel_sock_timeval *target_tv;
  1081. if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) {
  1082. return -TARGET_EFAULT;
  1083. }
  1084. __get_user(tv->tv_sec, &target_tv->tv_sec);
  1085. __get_user(tv->tv_usec, &target_tv->tv_usec);
  1086. unlock_user_struct(target_tv, target_tv_addr, 0);
  1087. return 0;
  1088. }
  1089. #endif
  1090. static inline abi_long copy_to_user_timeval64(abi_ulong target_tv_addr,
  1091. const struct timeval *tv)
  1092. {
  1093. struct target__kernel_sock_timeval *target_tv;
  1094. if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
  1095. return -TARGET_EFAULT;
  1096. }
  1097. __put_user(tv->tv_sec, &target_tv->tv_sec);
  1098. __put_user(tv->tv_usec, &target_tv->tv_usec);
  1099. unlock_user_struct(target_tv, target_tv_addr, 1);
  1100. return 0;
  1101. }
  1102. #if defined(TARGET_NR_futex) || \
  1103. defined(TARGET_NR_rt_sigtimedwait) || \
  1104. defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6) || \
  1105. defined(TARGET_NR_nanosleep) || defined(TARGET_NR_clock_settime) || \
  1106. defined(TARGET_NR_utimensat) || defined(TARGET_NR_mq_timedsend) || \
  1107. defined(TARGET_NR_mq_timedreceive) || defined(TARGET_NR_ipc) || \
  1108. defined(TARGET_NR_semop) || defined(TARGET_NR_semtimedop) || \
  1109. defined(TARGET_NR_timer_settime) || \
  1110. (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
  1111. static inline abi_long target_to_host_timespec(struct timespec *host_ts,
  1112. abi_ulong target_addr)
  1113. {
  1114. struct target_timespec *target_ts;
  1115. if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
  1116. return -TARGET_EFAULT;
  1117. }
  1118. __get_user(host_ts->tv_sec, &target_ts->tv_sec);
  1119. __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1120. unlock_user_struct(target_ts, target_addr, 0);
  1121. return 0;
  1122. }
  1123. #endif
  1124. #if defined(TARGET_NR_clock_settime64) || defined(TARGET_NR_futex_time64) || \
  1125. defined(TARGET_NR_timer_settime64) || \
  1126. defined(TARGET_NR_mq_timedsend_time64) || \
  1127. defined(TARGET_NR_mq_timedreceive_time64) || \
  1128. (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)) || \
  1129. defined(TARGET_NR_clock_nanosleep_time64) || \
  1130. defined(TARGET_NR_rt_sigtimedwait_time64) || \
  1131. defined(TARGET_NR_utimensat) || \
  1132. defined(TARGET_NR_utimensat_time64) || \
  1133. defined(TARGET_NR_semtimedop_time64) || \
  1134. defined(TARGET_NR_pselect6_time64) || defined(TARGET_NR_ppoll_time64)
  1135. static inline abi_long target_to_host_timespec64(struct timespec *host_ts,
  1136. abi_ulong target_addr)
  1137. {
  1138. struct target__kernel_timespec *target_ts;
  1139. if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
  1140. return -TARGET_EFAULT;
  1141. }
  1142. __get_user(host_ts->tv_sec, &target_ts->tv_sec);
  1143. __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1144. /* in 32bit mode, this drops the padding */
  1145. host_ts->tv_nsec = (long)(abi_long)host_ts->tv_nsec;
  1146. unlock_user_struct(target_ts, target_addr, 0);
  1147. return 0;
  1148. }
  1149. #endif
  1150. static inline abi_long host_to_target_timespec(abi_ulong target_addr,
  1151. struct timespec *host_ts)
  1152. {
  1153. struct target_timespec *target_ts;
  1154. if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
  1155. return -TARGET_EFAULT;
  1156. }
  1157. __put_user(host_ts->tv_sec, &target_ts->tv_sec);
  1158. __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1159. unlock_user_struct(target_ts, target_addr, 1);
  1160. return 0;
  1161. }
  1162. static inline abi_long host_to_target_timespec64(abi_ulong target_addr,
  1163. struct timespec *host_ts)
  1164. {
  1165. struct target__kernel_timespec *target_ts;
  1166. if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
  1167. return -TARGET_EFAULT;
  1168. }
  1169. __put_user(host_ts->tv_sec, &target_ts->tv_sec);
  1170. __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1171. unlock_user_struct(target_ts, target_addr, 1);
  1172. return 0;
  1173. }
  1174. #if defined(TARGET_NR_gettimeofday)
  1175. static inline abi_long copy_to_user_timezone(abi_ulong target_tz_addr,
  1176. struct timezone *tz)
  1177. {
  1178. struct target_timezone *target_tz;
  1179. if (!lock_user_struct(VERIFY_WRITE, target_tz, target_tz_addr, 1)) {
  1180. return -TARGET_EFAULT;
  1181. }
  1182. __put_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
  1183. __put_user(tz->tz_dsttime, &target_tz->tz_dsttime);
  1184. unlock_user_struct(target_tz, target_tz_addr, 1);
  1185. return 0;
  1186. }
  1187. #endif
  1188. #if defined(TARGET_NR_settimeofday)
  1189. static inline abi_long copy_from_user_timezone(struct timezone *tz,
  1190. abi_ulong target_tz_addr)
  1191. {
  1192. struct target_timezone *target_tz;
  1193. if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) {
  1194. return -TARGET_EFAULT;
  1195. }
  1196. __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
  1197. __get_user(tz->tz_dsttime, &target_tz->tz_dsttime);
  1198. unlock_user_struct(target_tz, target_tz_addr, 0);
  1199. return 0;
  1200. }
  1201. #endif
  1202. #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
  1203. #include <mqueue.h>
  1204. static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
  1205. abi_ulong target_mq_attr_addr)
  1206. {
  1207. struct target_mq_attr *target_mq_attr;
  1208. if (!lock_user_struct(VERIFY_READ, target_mq_attr,
  1209. target_mq_attr_addr, 1))
  1210. return -TARGET_EFAULT;
  1211. __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
  1212. __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
  1213. __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
  1214. __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
  1215. unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
  1216. return 0;
  1217. }
  1218. static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
  1219. const struct mq_attr *attr)
  1220. {
  1221. struct target_mq_attr *target_mq_attr;
  1222. if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
  1223. target_mq_attr_addr, 0))
  1224. return -TARGET_EFAULT;
  1225. __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
  1226. __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
  1227. __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
  1228. __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
  1229. unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
  1230. return 0;
  1231. }
  1232. #endif
  1233. #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
  1234. /* do_select() must return target values and target errnos. */
  1235. static abi_long do_select(int n,
  1236. abi_ulong rfd_addr, abi_ulong wfd_addr,
  1237. abi_ulong efd_addr, abi_ulong target_tv_addr)
  1238. {
  1239. fd_set rfds, wfds, efds;
  1240. fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
  1241. struct timeval tv;
  1242. struct timespec ts, *ts_ptr;
  1243. abi_long ret;
  1244. ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
  1245. if (ret) {
  1246. return ret;
  1247. }
  1248. ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
  1249. if (ret) {
  1250. return ret;
  1251. }
  1252. ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
  1253. if (ret) {
  1254. return ret;
  1255. }
  1256. if (target_tv_addr) {
  1257. if (copy_from_user_timeval(&tv, target_tv_addr))
  1258. return -TARGET_EFAULT;
  1259. ts.tv_sec = tv.tv_sec;
  1260. ts.tv_nsec = tv.tv_usec * 1000;
  1261. ts_ptr = &ts;
  1262. } else {
  1263. ts_ptr = NULL;
  1264. }
  1265. ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
  1266. ts_ptr, NULL));
  1267. if (!is_error(ret)) {
  1268. if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
  1269. return -TARGET_EFAULT;
  1270. if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
  1271. return -TARGET_EFAULT;
  1272. if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
  1273. return -TARGET_EFAULT;
  1274. if (target_tv_addr) {
  1275. tv.tv_sec = ts.tv_sec;
  1276. tv.tv_usec = ts.tv_nsec / 1000;
  1277. if (copy_to_user_timeval(target_tv_addr, &tv)) {
  1278. return -TARGET_EFAULT;
  1279. }
  1280. }
  1281. }
  1282. return ret;
  1283. }
  1284. #if defined(TARGET_WANT_OLD_SYS_SELECT)
  1285. static abi_long do_old_select(abi_ulong arg1)
  1286. {
  1287. struct target_sel_arg_struct *sel;
  1288. abi_ulong inp, outp, exp, tvp;
  1289. long nsel;
  1290. if (!lock_user_struct(VERIFY_READ, sel, arg1, 1)) {
  1291. return -TARGET_EFAULT;
  1292. }
  1293. nsel = tswapal(sel->n);
  1294. inp = tswapal(sel->inp);
  1295. outp = tswapal(sel->outp);
  1296. exp = tswapal(sel->exp);
  1297. tvp = tswapal(sel->tvp);
  1298. unlock_user_struct(sel, arg1, 0);
  1299. return do_select(nsel, inp, outp, exp, tvp);
  1300. }
  1301. #endif
  1302. #endif
  1303. #if defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
  1304. static abi_long do_pselect6(abi_long arg1, abi_long arg2, abi_long arg3,
  1305. abi_long arg4, abi_long arg5, abi_long arg6,
  1306. bool time64)
  1307. {
  1308. abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
  1309. fd_set rfds, wfds, efds;
  1310. fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
  1311. struct timespec ts, *ts_ptr;
  1312. abi_long ret;
  1313. /*
  1314. * The 6th arg is actually two args smashed together,
  1315. * so we cannot use the C library.
  1316. */
  1317. sigset_t set;
  1318. struct {
  1319. sigset_t *set;
  1320. size_t size;
  1321. } sig, *sig_ptr;
  1322. abi_ulong arg_sigset, arg_sigsize, *arg7;
  1323. target_sigset_t *target_sigset;
  1324. n = arg1;
  1325. rfd_addr = arg2;
  1326. wfd_addr = arg3;
  1327. efd_addr = arg4;
  1328. ts_addr = arg5;
  1329. ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
  1330. if (ret) {
  1331. return ret;
  1332. }
  1333. ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
  1334. if (ret) {
  1335. return ret;
  1336. }
  1337. ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
  1338. if (ret) {
  1339. return ret;
  1340. }
  1341. /*
  1342. * This takes a timespec, and not a timeval, so we cannot
  1343. * use the do_select() helper ...
  1344. */
  1345. if (ts_addr) {
  1346. if (time64) {
  1347. if (target_to_host_timespec64(&ts, ts_addr)) {
  1348. return -TARGET_EFAULT;
  1349. }
  1350. } else {
  1351. if (target_to_host_timespec(&ts, ts_addr)) {
  1352. return -TARGET_EFAULT;
  1353. }
  1354. }
  1355. ts_ptr = &ts;
  1356. } else {
  1357. ts_ptr = NULL;
  1358. }
  1359. /* Extract the two packed args for the sigset */
  1360. if (arg6) {
  1361. sig_ptr = &sig;
  1362. sig.size = SIGSET_T_SIZE;
  1363. arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
  1364. if (!arg7) {
  1365. return -TARGET_EFAULT;
  1366. }
  1367. arg_sigset = tswapal(arg7[0]);
  1368. arg_sigsize = tswapal(arg7[1]);
  1369. unlock_user(arg7, arg6, 0);
  1370. if (arg_sigset) {
  1371. sig.set = &set;
  1372. if (arg_sigsize != sizeof(*target_sigset)) {
  1373. /* Like the kernel, we enforce correct size sigsets */
  1374. return -TARGET_EINVAL;
  1375. }
  1376. target_sigset = lock_user(VERIFY_READ, arg_sigset,
  1377. sizeof(*target_sigset), 1);
  1378. if (!target_sigset) {
  1379. return -TARGET_EFAULT;
  1380. }
  1381. target_to_host_sigset(&set, target_sigset);
  1382. unlock_user(target_sigset, arg_sigset, 0);
  1383. } else {
  1384. sig.set = NULL;
  1385. }
  1386. } else {
  1387. sig_ptr = NULL;
  1388. }
  1389. ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
  1390. ts_ptr, sig_ptr));
  1391. if (!is_error(ret)) {
  1392. if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) {
  1393. return -TARGET_EFAULT;
  1394. }
  1395. if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) {
  1396. return -TARGET_EFAULT;
  1397. }
  1398. if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) {
  1399. return -TARGET_EFAULT;
  1400. }
  1401. if (time64) {
  1402. if (ts_addr && host_to_target_timespec64(ts_addr, &ts)) {
  1403. return -TARGET_EFAULT;
  1404. }
  1405. } else {
  1406. if (ts_addr && host_to_target_timespec(ts_addr, &ts)) {
  1407. return -TARGET_EFAULT;
  1408. }
  1409. }
  1410. }
  1411. return ret;
  1412. }
  1413. #endif
  1414. #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) || \
  1415. defined(TARGET_NR_ppoll_time64)
  1416. static abi_long do_ppoll(abi_long arg1, abi_long arg2, abi_long arg3,
  1417. abi_long arg4, abi_long arg5, bool ppoll, bool time64)
  1418. {
  1419. struct target_pollfd *target_pfd;
  1420. unsigned int nfds = arg2;
  1421. struct pollfd *pfd;
  1422. unsigned int i;
  1423. abi_long ret;
  1424. pfd = NULL;
  1425. target_pfd = NULL;
  1426. if (nfds) {
  1427. if (nfds > (INT_MAX / sizeof(struct target_pollfd))) {
  1428. return -TARGET_EINVAL;
  1429. }
  1430. target_pfd = lock_user(VERIFY_WRITE, arg1,
  1431. sizeof(struct target_pollfd) * nfds, 1);
  1432. if (!target_pfd) {
  1433. return -TARGET_EFAULT;
  1434. }
  1435. pfd = alloca(sizeof(struct pollfd) * nfds);
  1436. for (i = 0; i < nfds; i++) {
  1437. pfd[i].fd = tswap32(target_pfd[i].fd);
  1438. pfd[i].events = tswap16(target_pfd[i].events);
  1439. }
  1440. }
  1441. if (ppoll) {
  1442. struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
  1443. target_sigset_t *target_set;
  1444. sigset_t _set, *set = &_set;
  1445. if (arg3) {
  1446. if (time64) {
  1447. if (target_to_host_timespec64(timeout_ts, arg3)) {
  1448. unlock_user(target_pfd, arg1, 0);
  1449. return -TARGET_EFAULT;
  1450. }
  1451. } else {
  1452. if (target_to_host_timespec(timeout_ts, arg3)) {
  1453. unlock_user(target_pfd, arg1, 0);
  1454. return -TARGET_EFAULT;
  1455. }
  1456. }
  1457. } else {
  1458. timeout_ts = NULL;
  1459. }
  1460. if (arg4) {
  1461. if (arg5 != sizeof(target_sigset_t)) {
  1462. unlock_user(target_pfd, arg1, 0);
  1463. return -TARGET_EINVAL;
  1464. }
  1465. target_set = lock_user(VERIFY_READ, arg4,
  1466. sizeof(target_sigset_t), 1);
  1467. if (!target_set) {
  1468. unlock_user(target_pfd, arg1, 0);
  1469. return -TARGET_EFAULT;
  1470. }
  1471. target_to_host_sigset(set, target_set);
  1472. } else {
  1473. set = NULL;
  1474. }
  1475. ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts,
  1476. set, SIGSET_T_SIZE));
  1477. if (!is_error(ret) && arg3) {
  1478. if (time64) {
  1479. if (host_to_target_timespec64(arg3, timeout_ts)) {
  1480. return -TARGET_EFAULT;
  1481. }
  1482. } else {
  1483. if (host_to_target_timespec(arg3, timeout_ts)) {
  1484. return -TARGET_EFAULT;
  1485. }
  1486. }
  1487. }
  1488. if (arg4) {
  1489. unlock_user(target_set, arg4, 0);
  1490. }
  1491. } else {
  1492. struct timespec ts, *pts;
  1493. if (arg3 >= 0) {
  1494. /* Convert ms to secs, ns */
  1495. ts.tv_sec = arg3 / 1000;
  1496. ts.tv_nsec = (arg3 % 1000) * 1000000LL;
  1497. pts = &ts;
  1498. } else {
  1499. /* -ve poll() timeout means "infinite" */
  1500. pts = NULL;
  1501. }
  1502. ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0));
  1503. }
  1504. if (!is_error(ret)) {
  1505. for (i = 0; i < nfds; i++) {
  1506. target_pfd[i].revents = tswap16(pfd[i].revents);
  1507. }
  1508. }
  1509. unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
  1510. return ret;
  1511. }
  1512. #endif
  1513. static abi_long do_pipe2(int host_pipe[], int flags)
  1514. {
  1515. #ifdef CONFIG_PIPE2
  1516. return pipe2(host_pipe, flags);
  1517. #else
  1518. return -ENOSYS;
  1519. #endif
  1520. }
  1521. static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
  1522. int flags, int is_pipe2)
  1523. {
  1524. int host_pipe[2];
  1525. abi_long ret;
  1526. ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
  1527. if (is_error(ret))
  1528. return get_errno(ret);
  1529. /* Several targets have special calling conventions for the original
  1530. pipe syscall, but didn't replicate this into the pipe2 syscall. */
  1531. if (!is_pipe2) {
  1532. #if defined(TARGET_ALPHA)
  1533. ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
  1534. return host_pipe[0];
  1535. #elif defined(TARGET_MIPS)
  1536. ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
  1537. return host_pipe[0];
  1538. #elif defined(TARGET_SH4)
  1539. ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
  1540. return host_pipe[0];
  1541. #elif defined(TARGET_SPARC)
  1542. ((CPUSPARCState*)cpu_env)->regwptr[1] = host_pipe[1];
  1543. return host_pipe[0];
  1544. #endif
  1545. }
  1546. if (put_user_s32(host_pipe[0], pipedes)
  1547. || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
  1548. return -TARGET_EFAULT;
  1549. return get_errno(ret);
  1550. }
  1551. static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
  1552. abi_ulong target_addr,
  1553. socklen_t len)
  1554. {
  1555. struct target_ip_mreqn *target_smreqn;
  1556. target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
  1557. if (!target_smreqn)
  1558. return -TARGET_EFAULT;
  1559. mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
  1560. mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
  1561. if (len == sizeof(struct target_ip_mreqn))
  1562. mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
  1563. unlock_user(target_smreqn, target_addr, 0);
  1564. return 0;
  1565. }
  1566. static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr,
  1567. abi_ulong target_addr,
  1568. socklen_t len)
  1569. {
  1570. const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
  1571. sa_family_t sa_family;
  1572. struct target_sockaddr *target_saddr;
  1573. if (fd_trans_target_to_host_addr(fd)) {
  1574. return fd_trans_target_to_host_addr(fd)(addr, target_addr, len);
  1575. }
  1576. target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
  1577. if (!target_saddr)
  1578. return -TARGET_EFAULT;
  1579. sa_family = tswap16(target_saddr->sa_family);
  1580. /* Oops. The caller might send a incomplete sun_path; sun_path
  1581. * must be terminated by \0 (see the manual page), but
  1582. * unfortunately it is quite common to specify sockaddr_un
  1583. * length as "strlen(x->sun_path)" while it should be
  1584. * "strlen(...) + 1". We'll fix that here if needed.
  1585. * Linux kernel has a similar feature.
  1586. */
  1587. if (sa_family == AF_UNIX) {
  1588. if (len < unix_maxlen && len > 0) {
  1589. char *cp = (char*)target_saddr;
  1590. if ( cp[len-1] && !cp[len] )
  1591. len++;
  1592. }
  1593. if (len > unix_maxlen)
  1594. len = unix_maxlen;
  1595. }
  1596. memcpy(addr, target_saddr, len);
  1597. addr->sa_family = sa_family;
  1598. if (sa_family == AF_NETLINK) {
  1599. struct sockaddr_nl *nladdr;
  1600. nladdr = (struct sockaddr_nl *)addr;
  1601. nladdr->nl_pid = tswap32(nladdr->nl_pid);
  1602. nladdr->nl_groups = tswap32(nladdr->nl_groups);
  1603. } else if (sa_family == AF_PACKET) {
  1604. struct target_sockaddr_ll *lladdr;
  1605. lladdr = (struct target_sockaddr_ll *)addr;
  1606. lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex);
  1607. lladdr->sll_hatype = tswap16(lladdr->sll_hatype);
  1608. }
  1609. unlock_user(target_saddr, target_addr, 0);
  1610. return 0;
  1611. }
  1612. static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
  1613. struct sockaddr *addr,
  1614. socklen_t len)
  1615. {
  1616. struct target_sockaddr *target_saddr;
  1617. if (len == 0) {
  1618. return 0;
  1619. }
  1620. assert(addr);
  1621. target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
  1622. if (!target_saddr)
  1623. return -TARGET_EFAULT;
  1624. memcpy(target_saddr, addr, len);
  1625. if (len >= offsetof(struct target_sockaddr, sa_family) +
  1626. sizeof(target_saddr->sa_family)) {
  1627. target_saddr->sa_family = tswap16(addr->sa_family);
  1628. }
  1629. if (addr->sa_family == AF_NETLINK &&
  1630. len >= sizeof(struct target_sockaddr_nl)) {
  1631. struct target_sockaddr_nl *target_nl =
  1632. (struct target_sockaddr_nl *)target_saddr;
  1633. target_nl->nl_pid = tswap32(target_nl->nl_pid);
  1634. target_nl->nl_groups = tswap32(target_nl->nl_groups);
  1635. } else if (addr->sa_family == AF_PACKET) {
  1636. struct sockaddr_ll *target_ll = (struct sockaddr_ll *)target_saddr;
  1637. target_ll->sll_ifindex = tswap32(target_ll->sll_ifindex);
  1638. target_ll->sll_hatype = tswap16(target_ll->sll_hatype);
  1639. } else if (addr->sa_family == AF_INET6 &&
  1640. len >= sizeof(struct target_sockaddr_in6)) {
  1641. struct target_sockaddr_in6 *target_in6 =
  1642. (struct target_sockaddr_in6 *)target_saddr;
  1643. target_in6->sin6_scope_id = tswap16(target_in6->sin6_scope_id);
  1644. }
  1645. unlock_user(target_saddr, target_addr, len);
  1646. return 0;
  1647. }
  1648. static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
  1649. struct target_msghdr *target_msgh)
  1650. {
  1651. struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
  1652. abi_long msg_controllen;
  1653. abi_ulong target_cmsg_addr;
  1654. struct target_cmsghdr *target_cmsg, *target_cmsg_start;
  1655. socklen_t space = 0;
  1656. msg_controllen = tswapal(target_msgh->msg_controllen);
  1657. if (msg_controllen < sizeof (struct target_cmsghdr))
  1658. goto the_end;
  1659. target_cmsg_addr = tswapal(target_msgh->msg_control);
  1660. target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
  1661. target_cmsg_start = target_cmsg;
  1662. if (!target_cmsg)
  1663. return -TARGET_EFAULT;
  1664. while (cmsg && target_cmsg) {
  1665. void *data = CMSG_DATA(cmsg);
  1666. void *target_data = TARGET_CMSG_DATA(target_cmsg);
  1667. int len = tswapal(target_cmsg->cmsg_len)
  1668. - sizeof(struct target_cmsghdr);
  1669. space += CMSG_SPACE(len);
  1670. if (space > msgh->msg_controllen) {
  1671. space -= CMSG_SPACE(len);
  1672. /* This is a QEMU bug, since we allocated the payload
  1673. * area ourselves (unlike overflow in host-to-target
  1674. * conversion, which is just the guest giving us a buffer
  1675. * that's too small). It can't happen for the payload types
  1676. * we currently support; if it becomes an issue in future
  1677. * we would need to improve our allocation strategy to
  1678. * something more intelligent than "twice the size of the
  1679. * target buffer we're reading from".
  1680. */
  1681. qemu_log_mask(LOG_UNIMP,
  1682. ("Unsupported ancillary data %d/%d: "
  1683. "unhandled msg size\n"),
  1684. tswap32(target_cmsg->cmsg_level),
  1685. tswap32(target_cmsg->cmsg_type));
  1686. break;
  1687. }
  1688. if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
  1689. cmsg->cmsg_level = SOL_SOCKET;
  1690. } else {
  1691. cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
  1692. }
  1693. cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
  1694. cmsg->cmsg_len = CMSG_LEN(len);
  1695. if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
  1696. int *fd = (int *)data;
  1697. int *target_fd = (int *)target_data;
  1698. int i, numfds = len / sizeof(int);
  1699. for (i = 0; i < numfds; i++) {
  1700. __get_user(fd[i], target_fd + i);
  1701. }
  1702. } else if (cmsg->cmsg_level == SOL_SOCKET
  1703. && cmsg->cmsg_type == SCM_CREDENTIALS) {
  1704. struct ucred *cred = (struct ucred *)data;
  1705. struct target_ucred *target_cred =
  1706. (struct target_ucred *)target_data;
  1707. __get_user(cred->pid, &target_cred->pid);
  1708. __get_user(cred->uid, &target_cred->uid);
  1709. __get_user(cred->gid, &target_cred->gid);
  1710. } else {
  1711. qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n",
  1712. cmsg->cmsg_level, cmsg->cmsg_type);
  1713. memcpy(data, target_data, len);
  1714. }
  1715. cmsg = CMSG_NXTHDR(msgh, cmsg);
  1716. target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
  1717. target_cmsg_start);
  1718. }
  1719. unlock_user(target_cmsg, target_cmsg_addr, 0);
  1720. the_end:
  1721. msgh->msg_controllen = space;
  1722. return 0;
  1723. }
  1724. static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
  1725. struct msghdr *msgh)
  1726. {
  1727. struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
  1728. abi_long msg_controllen;
  1729. abi_ulong target_cmsg_addr;
  1730. struct target_cmsghdr *target_cmsg, *target_cmsg_start;
  1731. socklen_t space = 0;
  1732. msg_controllen = tswapal(target_msgh->msg_controllen);
  1733. if (msg_controllen < sizeof (struct target_cmsghdr))
  1734. goto the_end;
  1735. target_cmsg_addr = tswapal(target_msgh->msg_control);
  1736. target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
  1737. target_cmsg_start = target_cmsg;
  1738. if (!target_cmsg)
  1739. return -TARGET_EFAULT;
  1740. while (cmsg && target_cmsg) {
  1741. void *data = CMSG_DATA(cmsg);
  1742. void *target_data = TARGET_CMSG_DATA(target_cmsg);
  1743. int len = cmsg->cmsg_len - sizeof(struct cmsghdr);
  1744. int tgt_len, tgt_space;
  1745. /* We never copy a half-header but may copy half-data;
  1746. * this is Linux's behaviour in put_cmsg(). Note that
  1747. * truncation here is a guest problem (which we report
  1748. * to the guest via the CTRUNC bit), unlike truncation
  1749. * in target_to_host_cmsg, which is a QEMU bug.
  1750. */
  1751. if (msg_controllen < sizeof(struct target_cmsghdr)) {
  1752. target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
  1753. break;
  1754. }
  1755. if (cmsg->cmsg_level == SOL_SOCKET) {
  1756. target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
  1757. } else {
  1758. target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
  1759. }
  1760. target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
  1761. /* Payload types which need a different size of payload on
  1762. * the target must adjust tgt_len here.
  1763. */
  1764. tgt_len = len;
  1765. switch (cmsg->cmsg_level) {
  1766. case SOL_SOCKET:
  1767. switch (cmsg->cmsg_type) {
  1768. case SO_TIMESTAMP:
  1769. tgt_len = sizeof(struct target_timeval);
  1770. break;
  1771. default:
  1772. break;
  1773. }
  1774. break;
  1775. default:
  1776. break;
  1777. }
  1778. if (msg_controllen < TARGET_CMSG_LEN(tgt_len)) {
  1779. target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
  1780. tgt_len = msg_controllen - sizeof(struct target_cmsghdr);
  1781. }
  1782. /* We must now copy-and-convert len bytes of payload
  1783. * into tgt_len bytes of destination space. Bear in mind
  1784. * that in both source and destination we may be dealing
  1785. * with a truncated value!
  1786. */
  1787. switch (cmsg->cmsg_level) {
  1788. case SOL_SOCKET:
  1789. switch (cmsg->cmsg_type) {
  1790. case SCM_RIGHTS:
  1791. {
  1792. int *fd = (int *)data;
  1793. int *target_fd = (int *)target_data;
  1794. int i, numfds = tgt_len / sizeof(int);
  1795. for (i = 0; i < numfds; i++) {
  1796. __put_user(fd[i], target_fd + i);
  1797. }
  1798. break;
  1799. }
  1800. case SO_TIMESTAMP:
  1801. {
  1802. struct timeval *tv = (struct timeval *)data;
  1803. struct target_timeval *target_tv =
  1804. (struct target_timeval *)target_data;
  1805. if (len != sizeof(struct timeval) ||
  1806. tgt_len != sizeof(struct target_timeval)) {
  1807. goto unimplemented;
  1808. }
  1809. /* copy struct timeval to target */
  1810. __put_user(tv->tv_sec, &target_tv->tv_sec);
  1811. __put_user(tv->tv_usec, &target_tv->tv_usec);
  1812. break;
  1813. }
  1814. case SCM_CREDENTIALS:
  1815. {
  1816. struct ucred *cred = (struct ucred *)data;
  1817. struct target_ucred *target_cred =
  1818. (struct target_ucred *)target_data;
  1819. __put_user(cred->pid, &target_cred->pid);
  1820. __put_user(cred->uid, &target_cred->uid);
  1821. __put_user(cred->gid, &target_cred->gid);
  1822. break;
  1823. }
  1824. default:
  1825. goto unimplemented;
  1826. }
  1827. break;
  1828. case SOL_IP:
  1829. switch (cmsg->cmsg_type) {
  1830. case IP_TTL:
  1831. {
  1832. uint32_t *v = (uint32_t *)data;
  1833. uint32_t *t_int = (uint32_t *)target_data;
  1834. if (len != sizeof(uint32_t) ||
  1835. tgt_len != sizeof(uint32_t)) {
  1836. goto unimplemented;
  1837. }
  1838. __put_user(*v, t_int);
  1839. break;
  1840. }
  1841. case IP_RECVERR:
  1842. {
  1843. struct errhdr_t {
  1844. struct sock_extended_err ee;
  1845. struct sockaddr_in offender;
  1846. };
  1847. struct errhdr_t *errh = (struct errhdr_t *)data;
  1848. struct errhdr_t *target_errh =
  1849. (struct errhdr_t *)target_data;
  1850. if (len != sizeof(struct errhdr_t) ||
  1851. tgt_len != sizeof(struct errhdr_t)) {
  1852. goto unimplemented;
  1853. }
  1854. __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
  1855. __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
  1856. __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
  1857. __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
  1858. __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
  1859. __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
  1860. __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
  1861. host_to_target_sockaddr((unsigned long) &target_errh->offender,
  1862. (void *) &errh->offender, sizeof(errh->offender));
  1863. break;
  1864. }
  1865. default:
  1866. goto unimplemented;
  1867. }
  1868. break;
  1869. case SOL_IPV6:
  1870. switch (cmsg->cmsg_type) {
  1871. case IPV6_HOPLIMIT:
  1872. {
  1873. uint32_t *v = (uint32_t *)data;
  1874. uint32_t *t_int = (uint32_t *)target_data;
  1875. if (len != sizeof(uint32_t) ||
  1876. tgt_len != sizeof(uint32_t)) {
  1877. goto unimplemented;
  1878. }
  1879. __put_user(*v, t_int);
  1880. break;
  1881. }
  1882. case IPV6_RECVERR:
  1883. {
  1884. struct errhdr6_t {
  1885. struct sock_extended_err ee;
  1886. struct sockaddr_in6 offender;
  1887. };
  1888. struct errhdr6_t *errh = (struct errhdr6_t *)data;
  1889. struct errhdr6_t *target_errh =
  1890. (struct errhdr6_t *)target_data;
  1891. if (len != sizeof(struct errhdr6_t) ||
  1892. tgt_len != sizeof(struct errhdr6_t)) {
  1893. goto unimplemented;
  1894. }
  1895. __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
  1896. __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
  1897. __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
  1898. __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
  1899. __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
  1900. __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
  1901. __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
  1902. host_to_target_sockaddr((unsigned long) &target_errh->offender,
  1903. (void *) &errh->offender, sizeof(errh->offender));
  1904. break;
  1905. }
  1906. default:
  1907. goto unimplemented;
  1908. }
  1909. break;
  1910. default:
  1911. unimplemented:
  1912. qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n",
  1913. cmsg->cmsg_level, cmsg->cmsg_type);
  1914. memcpy(target_data, data, MIN(len, tgt_len));
  1915. if (tgt_len > len) {
  1916. memset(target_data + len, 0, tgt_len - len);
  1917. }
  1918. }
  1919. target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(tgt_len));
  1920. tgt_space = TARGET_CMSG_SPACE(tgt_len);
  1921. if (msg_controllen < tgt_space) {
  1922. tgt_space = msg_controllen;
  1923. }
  1924. msg_controllen -= tgt_space;
  1925. space += tgt_space;
  1926. cmsg = CMSG_NXTHDR(msgh, cmsg);
  1927. target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
  1928. target_cmsg_start);
  1929. }
  1930. unlock_user(target_cmsg, target_cmsg_addr, space);
  1931. the_end:
  1932. target_msgh->msg_controllen = tswapal(space);
  1933. return 0;
  1934. }
  1935. /* do_setsockopt() Must return target values and target errnos. */
  1936. static abi_long do_setsockopt(int sockfd, int level, int optname,
  1937. abi_ulong optval_addr, socklen_t optlen)
  1938. {
  1939. abi_long ret;
  1940. int val;
  1941. struct ip_mreqn *ip_mreq;
  1942. struct ip_mreq_source *ip_mreq_source;
  1943. switch(level) {
  1944. case SOL_TCP:
  1945. /* TCP options all take an 'int' value. */
  1946. if (optlen < sizeof(uint32_t))
  1947. return -TARGET_EINVAL;
  1948. if (get_user_u32(val, optval_addr))
  1949. return -TARGET_EFAULT;
  1950. ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
  1951. break;
  1952. case SOL_IP:
  1953. switch(optname) {
  1954. case IP_TOS:
  1955. case IP_TTL:
  1956. case IP_HDRINCL:
  1957. case IP_ROUTER_ALERT:
  1958. case IP_RECVOPTS:
  1959. case IP_RETOPTS:
  1960. case IP_PKTINFO:
  1961. case IP_MTU_DISCOVER:
  1962. case IP_RECVERR:
  1963. case IP_RECVTTL:
  1964. case IP_RECVTOS:
  1965. #ifdef IP_FREEBIND
  1966. case IP_FREEBIND:
  1967. #endif
  1968. case IP_MULTICAST_TTL:
  1969. case IP_MULTICAST_LOOP:
  1970. val = 0;
  1971. if (optlen >= sizeof(uint32_t)) {
  1972. if (get_user_u32(val, optval_addr))
  1973. return -TARGET_EFAULT;
  1974. } else if (optlen >= 1) {
  1975. if (get_user_u8(val, optval_addr))
  1976. return -TARGET_EFAULT;
  1977. }
  1978. ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
  1979. break;
  1980. case IP_ADD_MEMBERSHIP:
  1981. case IP_DROP_MEMBERSHIP:
  1982. if (optlen < sizeof (struct target_ip_mreq) ||
  1983. optlen > sizeof (struct target_ip_mreqn))
  1984. return -TARGET_EINVAL;
  1985. ip_mreq = (struct ip_mreqn *) alloca(optlen);
  1986. target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
  1987. ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
  1988. break;
  1989. case IP_BLOCK_SOURCE:
  1990. case IP_UNBLOCK_SOURCE:
  1991. case IP_ADD_SOURCE_MEMBERSHIP:
  1992. case IP_DROP_SOURCE_MEMBERSHIP:
  1993. if (optlen != sizeof (struct target_ip_mreq_source))
  1994. return -TARGET_EINVAL;
  1995. ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
  1996. ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
  1997. unlock_user (ip_mreq_source, optval_addr, 0);
  1998. break;
  1999. default:
  2000. goto unimplemented;
  2001. }
  2002. break;
  2003. case SOL_IPV6:
  2004. switch (optname) {
  2005. case IPV6_MTU_DISCOVER:
  2006. case IPV6_MTU:
  2007. case IPV6_V6ONLY:
  2008. case IPV6_RECVPKTINFO:
  2009. case IPV6_UNICAST_HOPS:
  2010. case IPV6_MULTICAST_HOPS:
  2011. case IPV6_MULTICAST_LOOP:
  2012. case IPV6_RECVERR:
  2013. case IPV6_RECVHOPLIMIT:
  2014. case IPV6_2292HOPLIMIT:
  2015. case IPV6_CHECKSUM:
  2016. case IPV6_ADDRFORM:
  2017. case IPV6_2292PKTINFO:
  2018. case IPV6_RECVTCLASS:
  2019. case IPV6_RECVRTHDR:
  2020. case IPV6_2292RTHDR:
  2021. case IPV6_RECVHOPOPTS:
  2022. case IPV6_2292HOPOPTS:
  2023. case IPV6_RECVDSTOPTS:
  2024. case IPV6_2292DSTOPTS:
  2025. case IPV6_TCLASS:
  2026. #ifdef IPV6_RECVPATHMTU
  2027. case IPV6_RECVPATHMTU:
  2028. #endif
  2029. #ifdef IPV6_TRANSPARENT
  2030. case IPV6_TRANSPARENT:
  2031. #endif
  2032. #ifdef IPV6_FREEBIND
  2033. case IPV6_FREEBIND:
  2034. #endif
  2035. #ifdef IPV6_RECVORIGDSTADDR
  2036. case IPV6_RECVORIGDSTADDR:
  2037. #endif
  2038. val = 0;
  2039. if (optlen < sizeof(uint32_t)) {
  2040. return -TARGET_EINVAL;
  2041. }
  2042. if (get_user_u32(val, optval_addr)) {
  2043. return -TARGET_EFAULT;
  2044. }
  2045. ret = get_errno(setsockopt(sockfd, level, optname,
  2046. &val, sizeof(val)));
  2047. break;
  2048. case IPV6_PKTINFO:
  2049. {
  2050. struct in6_pktinfo pki;
  2051. if (optlen < sizeof(pki)) {
  2052. return -TARGET_EINVAL;
  2053. }
  2054. if (copy_from_user(&pki, optval_addr, sizeof(pki))) {
  2055. return -TARGET_EFAULT;
  2056. }
  2057. pki.ipi6_ifindex = tswap32(pki.ipi6_ifindex);
  2058. ret = get_errno(setsockopt(sockfd, level, optname,
  2059. &pki, sizeof(pki)));
  2060. break;
  2061. }
  2062. case IPV6_ADD_MEMBERSHIP:
  2063. case IPV6_DROP_MEMBERSHIP:
  2064. {
  2065. struct ipv6_mreq ipv6mreq;
  2066. if (optlen < sizeof(ipv6mreq)) {
  2067. return -TARGET_EINVAL;
  2068. }
  2069. if (copy_from_user(&ipv6mreq, optval_addr, sizeof(ipv6mreq))) {
  2070. return -TARGET_EFAULT;
  2071. }
  2072. ipv6mreq.ipv6mr_interface = tswap32(ipv6mreq.ipv6mr_interface);
  2073. ret = get_errno(setsockopt(sockfd, level, optname,
  2074. &ipv6mreq, sizeof(ipv6mreq)));
  2075. break;
  2076. }
  2077. default:
  2078. goto unimplemented;
  2079. }
  2080. break;
  2081. case SOL_ICMPV6:
  2082. switch (optname) {
  2083. case ICMPV6_FILTER:
  2084. {
  2085. struct icmp6_filter icmp6f;
  2086. if (optlen > sizeof(icmp6f)) {
  2087. optlen = sizeof(icmp6f);
  2088. }
  2089. if (copy_from_user(&icmp6f, optval_addr, optlen)) {
  2090. return -TARGET_EFAULT;
  2091. }
  2092. for (val = 0; val < 8; val++) {
  2093. icmp6f.data[val] = tswap32(icmp6f.data[val]);
  2094. }
  2095. ret = get_errno(setsockopt(sockfd, level, optname,
  2096. &icmp6f, optlen));
  2097. break;
  2098. }
  2099. default:
  2100. goto unimplemented;
  2101. }
  2102. break;
  2103. case SOL_RAW:
  2104. switch (optname) {
  2105. case ICMP_FILTER:
  2106. case IPV6_CHECKSUM:
  2107. /* those take an u32 value */
  2108. if (optlen < sizeof(uint32_t)) {
  2109. return -TARGET_EINVAL;
  2110. }
  2111. if (get_user_u32(val, optval_addr)) {
  2112. return -TARGET_EFAULT;
  2113. }
  2114. ret = get_errno(setsockopt(sockfd, level, optname,
  2115. &val, sizeof(val)));
  2116. break;
  2117. default:
  2118. goto unimplemented;
  2119. }
  2120. break;
  2121. #if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE)
  2122. case SOL_ALG:
  2123. switch (optname) {
  2124. case ALG_SET_KEY:
  2125. {
  2126. char *alg_key = g_malloc(optlen);
  2127. if (!alg_key) {
  2128. return -TARGET_ENOMEM;
  2129. }
  2130. if (copy_from_user(alg_key, optval_addr, optlen)) {
  2131. g_free(alg_key);
  2132. return -TARGET_EFAULT;
  2133. }
  2134. ret = get_errno(setsockopt(sockfd, level, optname,
  2135. alg_key, optlen));
  2136. g_free(alg_key);
  2137. break;
  2138. }
  2139. case ALG_SET_AEAD_AUTHSIZE:
  2140. {
  2141. ret = get_errno(setsockopt(sockfd, level, optname,
  2142. NULL, optlen));
  2143. break;
  2144. }
  2145. default:
  2146. goto unimplemented;
  2147. }
  2148. break;
  2149. #endif
  2150. case TARGET_SOL_SOCKET:
  2151. switch (optname) {
  2152. case TARGET_SO_RCVTIMEO:
  2153. {
  2154. struct timeval tv;
  2155. optname = SO_RCVTIMEO;
  2156. set_timeout:
  2157. if (optlen != sizeof(struct target_timeval)) {
  2158. return -TARGET_EINVAL;
  2159. }
  2160. if (copy_from_user_timeval(&tv, optval_addr)) {
  2161. return -TARGET_EFAULT;
  2162. }
  2163. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
  2164. &tv, sizeof(tv)));
  2165. return ret;
  2166. }
  2167. case TARGET_SO_SNDTIMEO:
  2168. optname = SO_SNDTIMEO;
  2169. goto set_timeout;
  2170. case TARGET_SO_ATTACH_FILTER:
  2171. {
  2172. struct target_sock_fprog *tfprog;
  2173. struct target_sock_filter *tfilter;
  2174. struct sock_fprog fprog;
  2175. struct sock_filter *filter;
  2176. int i;
  2177. if (optlen != sizeof(*tfprog)) {
  2178. return -TARGET_EINVAL;
  2179. }
  2180. if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
  2181. return -TARGET_EFAULT;
  2182. }
  2183. if (!lock_user_struct(VERIFY_READ, tfilter,
  2184. tswapal(tfprog->filter), 0)) {
  2185. unlock_user_struct(tfprog, optval_addr, 1);
  2186. return -TARGET_EFAULT;
  2187. }
  2188. fprog.len = tswap16(tfprog->len);
  2189. filter = g_try_new(struct sock_filter, fprog.len);
  2190. if (filter == NULL) {
  2191. unlock_user_struct(tfilter, tfprog->filter, 1);
  2192. unlock_user_struct(tfprog, optval_addr, 1);
  2193. return -TARGET_ENOMEM;
  2194. }
  2195. for (i = 0; i < fprog.len; i++) {
  2196. filter[i].code = tswap16(tfilter[i].code);
  2197. filter[i].jt = tfilter[i].jt;
  2198. filter[i].jf = tfilter[i].jf;
  2199. filter[i].k = tswap32(tfilter[i].k);
  2200. }
  2201. fprog.filter = filter;
  2202. ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
  2203. SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
  2204. g_free(filter);
  2205. unlock_user_struct(tfilter, tfprog->filter, 1);
  2206. unlock_user_struct(tfprog, optval_addr, 1);
  2207. return ret;
  2208. }
  2209. case TARGET_SO_BINDTODEVICE:
  2210. {
  2211. char *dev_ifname, *addr_ifname;
  2212. if (optlen > IFNAMSIZ - 1) {
  2213. optlen = IFNAMSIZ - 1;
  2214. }
  2215. dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1);
  2216. if (!dev_ifname) {
  2217. return -TARGET_EFAULT;
  2218. }
  2219. optname = SO_BINDTODEVICE;
  2220. addr_ifname = alloca(IFNAMSIZ);
  2221. memcpy(addr_ifname, dev_ifname, optlen);
  2222. addr_ifname[optlen] = 0;
  2223. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
  2224. addr_ifname, optlen));
  2225. unlock_user (dev_ifname, optval_addr, 0);
  2226. return ret;
  2227. }
  2228. case TARGET_SO_LINGER:
  2229. {
  2230. struct linger lg;
  2231. struct target_linger *tlg;
  2232. if (optlen != sizeof(struct target_linger)) {
  2233. return -TARGET_EINVAL;
  2234. }
  2235. if (!lock_user_struct(VERIFY_READ, tlg, optval_addr, 1)) {
  2236. return -TARGET_EFAULT;
  2237. }
  2238. __get_user(lg.l_onoff, &tlg->l_onoff);
  2239. __get_user(lg.l_linger, &tlg->l_linger);
  2240. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, SO_LINGER,
  2241. &lg, sizeof(lg)));
  2242. unlock_user_struct(tlg, optval_addr, 0);
  2243. return ret;
  2244. }
  2245. /* Options with 'int' argument. */
  2246. case TARGET_SO_DEBUG:
  2247. optname = SO_DEBUG;
  2248. break;
  2249. case TARGET_SO_REUSEADDR:
  2250. optname = SO_REUSEADDR;
  2251. break;
  2252. #ifdef SO_REUSEPORT
  2253. case TARGET_SO_REUSEPORT:
  2254. optname = SO_REUSEPORT;
  2255. break;
  2256. #endif
  2257. case TARGET_SO_TYPE:
  2258. optname = SO_TYPE;
  2259. break;
  2260. case TARGET_SO_ERROR:
  2261. optname = SO_ERROR;
  2262. break;
  2263. case TARGET_SO_DONTROUTE:
  2264. optname = SO_DONTROUTE;
  2265. break;
  2266. case TARGET_SO_BROADCAST:
  2267. optname = SO_BROADCAST;
  2268. break;
  2269. case TARGET_SO_SNDBUF:
  2270. optname = SO_SNDBUF;
  2271. break;
  2272. case TARGET_SO_SNDBUFFORCE:
  2273. optname = SO_SNDBUFFORCE;
  2274. break;
  2275. case TARGET_SO_RCVBUF:
  2276. optname = SO_RCVBUF;
  2277. break;
  2278. case TARGET_SO_RCVBUFFORCE:
  2279. optname = SO_RCVBUFFORCE;
  2280. break;
  2281. case TARGET_SO_KEEPALIVE:
  2282. optname = SO_KEEPALIVE;
  2283. break;
  2284. case TARGET_SO_OOBINLINE:
  2285. optname = SO_OOBINLINE;
  2286. break;
  2287. case TARGET_SO_NO_CHECK:
  2288. optname = SO_NO_CHECK;
  2289. break;
  2290. case TARGET_SO_PRIORITY:
  2291. optname = SO_PRIORITY;
  2292. break;
  2293. #ifdef SO_BSDCOMPAT
  2294. case TARGET_SO_BSDCOMPAT:
  2295. optname = SO_BSDCOMPAT;
  2296. break;
  2297. #endif
  2298. case TARGET_SO_PASSCRED:
  2299. optname = SO_PASSCRED;
  2300. break;
  2301. case TARGET_SO_PASSSEC:
  2302. optname = SO_PASSSEC;
  2303. break;
  2304. case TARGET_SO_TIMESTAMP:
  2305. optname = SO_TIMESTAMP;
  2306. break;
  2307. case TARGET_SO_RCVLOWAT:
  2308. optname = SO_RCVLOWAT;
  2309. break;
  2310. default:
  2311. goto unimplemented;
  2312. }
  2313. if (optlen < sizeof(uint32_t))
  2314. return -TARGET_EINVAL;
  2315. if (get_user_u32(val, optval_addr))
  2316. return -TARGET_EFAULT;
  2317. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
  2318. break;
  2319. #ifdef SOL_NETLINK
  2320. case SOL_NETLINK:
  2321. switch (optname) {
  2322. case NETLINK_PKTINFO:
  2323. case NETLINK_ADD_MEMBERSHIP:
  2324. case NETLINK_DROP_MEMBERSHIP:
  2325. case NETLINK_BROADCAST_ERROR:
  2326. case NETLINK_NO_ENOBUFS:
  2327. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
  2328. case NETLINK_LISTEN_ALL_NSID:
  2329. case NETLINK_CAP_ACK:
  2330. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
  2331. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
  2332. case NETLINK_EXT_ACK:
  2333. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2334. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
  2335. case NETLINK_GET_STRICT_CHK:
  2336. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2337. break;
  2338. default:
  2339. goto unimplemented;
  2340. }
  2341. val = 0;
  2342. if (optlen < sizeof(uint32_t)) {
  2343. return -TARGET_EINVAL;
  2344. }
  2345. if (get_user_u32(val, optval_addr)) {
  2346. return -TARGET_EFAULT;
  2347. }
  2348. ret = get_errno(setsockopt(sockfd, SOL_NETLINK, optname, &val,
  2349. sizeof(val)));
  2350. break;
  2351. #endif /* SOL_NETLINK */
  2352. default:
  2353. unimplemented:
  2354. qemu_log_mask(LOG_UNIMP, "Unsupported setsockopt level=%d optname=%d\n",
  2355. level, optname);
  2356. ret = -TARGET_ENOPROTOOPT;
  2357. }
  2358. return ret;
  2359. }
  2360. /* do_getsockopt() Must return target values and target errnos. */
  2361. static abi_long do_getsockopt(int sockfd, int level, int optname,
  2362. abi_ulong optval_addr, abi_ulong optlen)
  2363. {
  2364. abi_long ret;
  2365. int len, val;
  2366. socklen_t lv;
  2367. switch(level) {
  2368. case TARGET_SOL_SOCKET:
  2369. level = SOL_SOCKET;
  2370. switch (optname) {
  2371. /* These don't just return a single integer */
  2372. case TARGET_SO_PEERNAME:
  2373. goto unimplemented;
  2374. case TARGET_SO_RCVTIMEO: {
  2375. struct timeval tv;
  2376. socklen_t tvlen;
  2377. optname = SO_RCVTIMEO;
  2378. get_timeout:
  2379. if (get_user_u32(len, optlen)) {
  2380. return -TARGET_EFAULT;
  2381. }
  2382. if (len < 0) {
  2383. return -TARGET_EINVAL;
  2384. }
  2385. tvlen = sizeof(tv);
  2386. ret = get_errno(getsockopt(sockfd, level, optname,
  2387. &tv, &tvlen));
  2388. if (ret < 0) {
  2389. return ret;
  2390. }
  2391. if (len > sizeof(struct target_timeval)) {
  2392. len = sizeof(struct target_timeval);
  2393. }
  2394. if (copy_to_user_timeval(optval_addr, &tv)) {
  2395. return -TARGET_EFAULT;
  2396. }
  2397. if (put_user_u32(len, optlen)) {
  2398. return -TARGET_EFAULT;
  2399. }
  2400. break;
  2401. }
  2402. case TARGET_SO_SNDTIMEO:
  2403. optname = SO_SNDTIMEO;
  2404. goto get_timeout;
  2405. case TARGET_SO_PEERCRED: {
  2406. struct ucred cr;
  2407. socklen_t crlen;
  2408. struct target_ucred *tcr;
  2409. if (get_user_u32(len, optlen)) {
  2410. return -TARGET_EFAULT;
  2411. }
  2412. if (len < 0) {
  2413. return -TARGET_EINVAL;
  2414. }
  2415. crlen = sizeof(cr);
  2416. ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
  2417. &cr, &crlen));
  2418. if (ret < 0) {
  2419. return ret;
  2420. }
  2421. if (len > crlen) {
  2422. len = crlen;
  2423. }
  2424. if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
  2425. return -TARGET_EFAULT;
  2426. }
  2427. __put_user(cr.pid, &tcr->pid);
  2428. __put_user(cr.uid, &tcr->uid);
  2429. __put_user(cr.gid, &tcr->gid);
  2430. unlock_user_struct(tcr, optval_addr, 1);
  2431. if (put_user_u32(len, optlen)) {
  2432. return -TARGET_EFAULT;
  2433. }
  2434. break;
  2435. }
  2436. case TARGET_SO_PEERSEC: {
  2437. char *name;
  2438. if (get_user_u32(len, optlen)) {
  2439. return -TARGET_EFAULT;
  2440. }
  2441. if (len < 0) {
  2442. return -TARGET_EINVAL;
  2443. }
  2444. name = lock_user(VERIFY_WRITE, optval_addr, len, 0);
  2445. if (!name) {
  2446. return -TARGET_EFAULT;
  2447. }
  2448. lv = len;
  2449. ret = get_errno(getsockopt(sockfd, level, SO_PEERSEC,
  2450. name, &lv));
  2451. if (put_user_u32(lv, optlen)) {
  2452. ret = -TARGET_EFAULT;
  2453. }
  2454. unlock_user(name, optval_addr, lv);
  2455. break;
  2456. }
  2457. case TARGET_SO_LINGER:
  2458. {
  2459. struct linger lg;
  2460. socklen_t lglen;
  2461. struct target_linger *tlg;
  2462. if (get_user_u32(len, optlen)) {
  2463. return -TARGET_EFAULT;
  2464. }
  2465. if (len < 0) {
  2466. return -TARGET_EINVAL;
  2467. }
  2468. lglen = sizeof(lg);
  2469. ret = get_errno(getsockopt(sockfd, level, SO_LINGER,
  2470. &lg, &lglen));
  2471. if (ret < 0) {
  2472. return ret;
  2473. }
  2474. if (len > lglen) {
  2475. len = lglen;
  2476. }
  2477. if (!lock_user_struct(VERIFY_WRITE, tlg, optval_addr, 0)) {
  2478. return -TARGET_EFAULT;
  2479. }
  2480. __put_user(lg.l_onoff, &tlg->l_onoff);
  2481. __put_user(lg.l_linger, &tlg->l_linger);
  2482. unlock_user_struct(tlg, optval_addr, 1);
  2483. if (put_user_u32(len, optlen)) {
  2484. return -TARGET_EFAULT;
  2485. }
  2486. break;
  2487. }
  2488. /* Options with 'int' argument. */
  2489. case TARGET_SO_DEBUG:
  2490. optname = SO_DEBUG;
  2491. goto int_case;
  2492. case TARGET_SO_REUSEADDR:
  2493. optname = SO_REUSEADDR;
  2494. goto int_case;
  2495. #ifdef SO_REUSEPORT
  2496. case TARGET_SO_REUSEPORT:
  2497. optname = SO_REUSEPORT;
  2498. goto int_case;
  2499. #endif
  2500. case TARGET_SO_TYPE:
  2501. optname = SO_TYPE;
  2502. goto int_case;
  2503. case TARGET_SO_ERROR:
  2504. optname = SO_ERROR;
  2505. goto int_case;
  2506. case TARGET_SO_DONTROUTE:
  2507. optname = SO_DONTROUTE;
  2508. goto int_case;
  2509. case TARGET_SO_BROADCAST:
  2510. optname = SO_BROADCAST;
  2511. goto int_case;
  2512. case TARGET_SO_SNDBUF:
  2513. optname = SO_SNDBUF;
  2514. goto int_case;
  2515. case TARGET_SO_RCVBUF:
  2516. optname = SO_RCVBUF;
  2517. goto int_case;
  2518. case TARGET_SO_KEEPALIVE:
  2519. optname = SO_KEEPALIVE;
  2520. goto int_case;
  2521. case TARGET_SO_OOBINLINE:
  2522. optname = SO_OOBINLINE;
  2523. goto int_case;
  2524. case TARGET_SO_NO_CHECK:
  2525. optname = SO_NO_CHECK;
  2526. goto int_case;
  2527. case TARGET_SO_PRIORITY:
  2528. optname = SO_PRIORITY;
  2529. goto int_case;
  2530. #ifdef SO_BSDCOMPAT
  2531. case TARGET_SO_BSDCOMPAT:
  2532. optname = SO_BSDCOMPAT;
  2533. goto int_case;
  2534. #endif
  2535. case TARGET_SO_PASSCRED:
  2536. optname = SO_PASSCRED;
  2537. goto int_case;
  2538. case TARGET_SO_TIMESTAMP:
  2539. optname = SO_TIMESTAMP;
  2540. goto int_case;
  2541. case TARGET_SO_RCVLOWAT:
  2542. optname = SO_RCVLOWAT;
  2543. goto int_case;
  2544. case TARGET_SO_ACCEPTCONN:
  2545. optname = SO_ACCEPTCONN;
  2546. goto int_case;
  2547. default:
  2548. goto int_case;
  2549. }
  2550. break;
  2551. case SOL_TCP:
  2552. /* TCP options all take an 'int' value. */
  2553. int_case:
  2554. if (get_user_u32(len, optlen))
  2555. return -TARGET_EFAULT;
  2556. if (len < 0)
  2557. return -TARGET_EINVAL;
  2558. lv = sizeof(lv);
  2559. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2560. if (ret < 0)
  2561. return ret;
  2562. if (optname == SO_TYPE) {
  2563. val = host_to_target_sock_type(val);
  2564. }
  2565. if (len > lv)
  2566. len = lv;
  2567. if (len == 4) {
  2568. if (put_user_u32(val, optval_addr))
  2569. return -TARGET_EFAULT;
  2570. } else {
  2571. if (put_user_u8(val, optval_addr))
  2572. return -TARGET_EFAULT;
  2573. }
  2574. if (put_user_u32(len, optlen))
  2575. return -TARGET_EFAULT;
  2576. break;
  2577. case SOL_IP:
  2578. switch(optname) {
  2579. case IP_TOS:
  2580. case IP_TTL:
  2581. case IP_HDRINCL:
  2582. case IP_ROUTER_ALERT:
  2583. case IP_RECVOPTS:
  2584. case IP_RETOPTS:
  2585. case IP_PKTINFO:
  2586. case IP_MTU_DISCOVER:
  2587. case IP_RECVERR:
  2588. case IP_RECVTOS:
  2589. #ifdef IP_FREEBIND
  2590. case IP_FREEBIND:
  2591. #endif
  2592. case IP_MULTICAST_TTL:
  2593. case IP_MULTICAST_LOOP:
  2594. if (get_user_u32(len, optlen))
  2595. return -TARGET_EFAULT;
  2596. if (len < 0)
  2597. return -TARGET_EINVAL;
  2598. lv = sizeof(lv);
  2599. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2600. if (ret < 0)
  2601. return ret;
  2602. if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
  2603. len = 1;
  2604. if (put_user_u32(len, optlen)
  2605. || put_user_u8(val, optval_addr))
  2606. return -TARGET_EFAULT;
  2607. } else {
  2608. if (len > sizeof(int))
  2609. len = sizeof(int);
  2610. if (put_user_u32(len, optlen)
  2611. || put_user_u32(val, optval_addr))
  2612. return -TARGET_EFAULT;
  2613. }
  2614. break;
  2615. default:
  2616. ret = -TARGET_ENOPROTOOPT;
  2617. break;
  2618. }
  2619. break;
  2620. case SOL_IPV6:
  2621. switch (optname) {
  2622. case IPV6_MTU_DISCOVER:
  2623. case IPV6_MTU:
  2624. case IPV6_V6ONLY:
  2625. case IPV6_RECVPKTINFO:
  2626. case IPV6_UNICAST_HOPS:
  2627. case IPV6_MULTICAST_HOPS:
  2628. case IPV6_MULTICAST_LOOP:
  2629. case IPV6_RECVERR:
  2630. case IPV6_RECVHOPLIMIT:
  2631. case IPV6_2292HOPLIMIT:
  2632. case IPV6_CHECKSUM:
  2633. case IPV6_ADDRFORM:
  2634. case IPV6_2292PKTINFO:
  2635. case IPV6_RECVTCLASS:
  2636. case IPV6_RECVRTHDR:
  2637. case IPV6_2292RTHDR:
  2638. case IPV6_RECVHOPOPTS:
  2639. case IPV6_2292HOPOPTS:
  2640. case IPV6_RECVDSTOPTS:
  2641. case IPV6_2292DSTOPTS:
  2642. case IPV6_TCLASS:
  2643. #ifdef IPV6_RECVPATHMTU
  2644. case IPV6_RECVPATHMTU:
  2645. #endif
  2646. #ifdef IPV6_TRANSPARENT
  2647. case IPV6_TRANSPARENT:
  2648. #endif
  2649. #ifdef IPV6_FREEBIND
  2650. case IPV6_FREEBIND:
  2651. #endif
  2652. #ifdef IPV6_RECVORIGDSTADDR
  2653. case IPV6_RECVORIGDSTADDR:
  2654. #endif
  2655. if (get_user_u32(len, optlen))
  2656. return -TARGET_EFAULT;
  2657. if (len < 0)
  2658. return -TARGET_EINVAL;
  2659. lv = sizeof(lv);
  2660. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2661. if (ret < 0)
  2662. return ret;
  2663. if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
  2664. len = 1;
  2665. if (put_user_u32(len, optlen)
  2666. || put_user_u8(val, optval_addr))
  2667. return -TARGET_EFAULT;
  2668. } else {
  2669. if (len > sizeof(int))
  2670. len = sizeof(int);
  2671. if (put_user_u32(len, optlen)
  2672. || put_user_u32(val, optval_addr))
  2673. return -TARGET_EFAULT;
  2674. }
  2675. break;
  2676. default:
  2677. ret = -TARGET_ENOPROTOOPT;
  2678. break;
  2679. }
  2680. break;
  2681. #ifdef SOL_NETLINK
  2682. case SOL_NETLINK:
  2683. switch (optname) {
  2684. case NETLINK_PKTINFO:
  2685. case NETLINK_BROADCAST_ERROR:
  2686. case NETLINK_NO_ENOBUFS:
  2687. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
  2688. case NETLINK_LISTEN_ALL_NSID:
  2689. case NETLINK_CAP_ACK:
  2690. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
  2691. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
  2692. case NETLINK_EXT_ACK:
  2693. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2694. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
  2695. case NETLINK_GET_STRICT_CHK:
  2696. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2697. if (get_user_u32(len, optlen)) {
  2698. return -TARGET_EFAULT;
  2699. }
  2700. if (len != sizeof(val)) {
  2701. return -TARGET_EINVAL;
  2702. }
  2703. lv = len;
  2704. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2705. if (ret < 0) {
  2706. return ret;
  2707. }
  2708. if (put_user_u32(lv, optlen)
  2709. || put_user_u32(val, optval_addr)) {
  2710. return -TARGET_EFAULT;
  2711. }
  2712. break;
  2713. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
  2714. case NETLINK_LIST_MEMBERSHIPS:
  2715. {
  2716. uint32_t *results;
  2717. int i;
  2718. if (get_user_u32(len, optlen)) {
  2719. return -TARGET_EFAULT;
  2720. }
  2721. if (len < 0) {
  2722. return -TARGET_EINVAL;
  2723. }
  2724. results = lock_user(VERIFY_WRITE, optval_addr, len, 1);
  2725. if (!results) {
  2726. return -TARGET_EFAULT;
  2727. }
  2728. lv = len;
  2729. ret = get_errno(getsockopt(sockfd, level, optname, results, &lv));
  2730. if (ret < 0) {
  2731. unlock_user(results, optval_addr, 0);
  2732. return ret;
  2733. }
  2734. /* swap host endianess to target endianess. */
  2735. for (i = 0; i < (len / sizeof(uint32_t)); i++) {
  2736. results[i] = tswap32(results[i]);
  2737. }
  2738. if (put_user_u32(lv, optlen)) {
  2739. return -TARGET_EFAULT;
  2740. }
  2741. unlock_user(results, optval_addr, 0);
  2742. break;
  2743. }
  2744. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
  2745. default:
  2746. goto unimplemented;
  2747. }
  2748. break;
  2749. #endif /* SOL_NETLINK */
  2750. default:
  2751. unimplemented:
  2752. qemu_log_mask(LOG_UNIMP,
  2753. "getsockopt level=%d optname=%d not yet supported\n",
  2754. level, optname);
  2755. ret = -TARGET_EOPNOTSUPP;
  2756. break;
  2757. }
  2758. return ret;
  2759. }
  2760. /* Convert target low/high pair representing file offset into the host
  2761. * low/high pair. This function doesn't handle offsets bigger than 64 bits
  2762. * as the kernel doesn't handle them either.
  2763. */
  2764. static void target_to_host_low_high(abi_ulong tlow,
  2765. abi_ulong thigh,
  2766. unsigned long *hlow,
  2767. unsigned long *hhigh)
  2768. {
  2769. uint64_t off = tlow |
  2770. ((unsigned long long)thigh << TARGET_LONG_BITS / 2) <<
  2771. TARGET_LONG_BITS / 2;
  2772. *hlow = off;
  2773. *hhigh = (off >> HOST_LONG_BITS / 2) >> HOST_LONG_BITS / 2;
  2774. }
  2775. static struct iovec *lock_iovec(int type, abi_ulong target_addr,
  2776. abi_ulong count, int copy)
  2777. {
  2778. struct target_iovec *target_vec;
  2779. struct iovec *vec;
  2780. abi_ulong total_len, max_len;
  2781. int i;
  2782. int err = 0;
  2783. bool bad_address = false;
  2784. if (count == 0) {
  2785. errno = 0;
  2786. return NULL;
  2787. }
  2788. if (count > IOV_MAX) {
  2789. errno = EINVAL;
  2790. return NULL;
  2791. }
  2792. vec = g_try_new0(struct iovec, count);
  2793. if (vec == NULL) {
  2794. errno = ENOMEM;
  2795. return NULL;
  2796. }
  2797. target_vec = lock_user(VERIFY_READ, target_addr,
  2798. count * sizeof(struct target_iovec), 1);
  2799. if (target_vec == NULL) {
  2800. err = EFAULT;
  2801. goto fail2;
  2802. }
  2803. /* ??? If host page size > target page size, this will result in a
  2804. value larger than what we can actually support. */
  2805. max_len = 0x7fffffff & TARGET_PAGE_MASK;
  2806. total_len = 0;
  2807. for (i = 0; i < count; i++) {
  2808. abi_ulong base = tswapal(target_vec[i].iov_base);
  2809. abi_long len = tswapal(target_vec[i].iov_len);
  2810. if (len < 0) {
  2811. err = EINVAL;
  2812. goto fail;
  2813. } else if (len == 0) {
  2814. /* Zero length pointer is ignored. */
  2815. vec[i].iov_base = 0;
  2816. } else {
  2817. vec[i].iov_base = lock_user(type, base, len, copy);
  2818. /* If the first buffer pointer is bad, this is a fault. But
  2819. * subsequent bad buffers will result in a partial write; this
  2820. * is realized by filling the vector with null pointers and
  2821. * zero lengths. */
  2822. if (!vec[i].iov_base) {
  2823. if (i == 0) {
  2824. err = EFAULT;
  2825. goto fail;
  2826. } else {
  2827. bad_address = true;
  2828. }
  2829. }
  2830. if (bad_address) {
  2831. len = 0;
  2832. }
  2833. if (len > max_len - total_len) {
  2834. len = max_len - total_len;
  2835. }
  2836. }
  2837. vec[i].iov_len = len;
  2838. total_len += len;
  2839. }
  2840. unlock_user(target_vec, target_addr, 0);
  2841. return vec;
  2842. fail:
  2843. while (--i >= 0) {
  2844. if (tswapal(target_vec[i].iov_len) > 0) {
  2845. unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0);
  2846. }
  2847. }
  2848. unlock_user(target_vec, target_addr, 0);
  2849. fail2:
  2850. g_free(vec);
  2851. errno = err;
  2852. return NULL;
  2853. }
  2854. static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
  2855. abi_ulong count, int copy)
  2856. {
  2857. struct target_iovec *target_vec;
  2858. int i;
  2859. target_vec = lock_user(VERIFY_READ, target_addr,
  2860. count * sizeof(struct target_iovec), 1);
  2861. if (target_vec) {
  2862. for (i = 0; i < count; i++) {
  2863. abi_ulong base = tswapal(target_vec[i].iov_base);
  2864. abi_long len = tswapal(target_vec[i].iov_len);
  2865. if (len < 0) {
  2866. break;
  2867. }
  2868. unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
  2869. }
  2870. unlock_user(target_vec, target_addr, 0);
  2871. }
  2872. g_free(vec);
  2873. }
  2874. static inline int target_to_host_sock_type(int *type)
  2875. {
  2876. int host_type = 0;
  2877. int target_type = *type;
  2878. switch (target_type & TARGET_SOCK_TYPE_MASK) {
  2879. case TARGET_SOCK_DGRAM:
  2880. host_type = SOCK_DGRAM;
  2881. break;
  2882. case TARGET_SOCK_STREAM:
  2883. host_type = SOCK_STREAM;
  2884. break;
  2885. default:
  2886. host_type = target_type & TARGET_SOCK_TYPE_MASK;
  2887. break;
  2888. }
  2889. if (target_type & TARGET_SOCK_CLOEXEC) {
  2890. #if defined(SOCK_CLOEXEC)
  2891. host_type |= SOCK_CLOEXEC;
  2892. #else
  2893. return -TARGET_EINVAL;
  2894. #endif
  2895. }
  2896. if (target_type & TARGET_SOCK_NONBLOCK) {
  2897. #if defined(SOCK_NONBLOCK)
  2898. host_type |= SOCK_NONBLOCK;
  2899. #elif !defined(O_NONBLOCK)
  2900. return -TARGET_EINVAL;
  2901. #endif
  2902. }
  2903. *type = host_type;
  2904. return 0;
  2905. }
  2906. /* Try to emulate socket type flags after socket creation. */
  2907. static int sock_flags_fixup(int fd, int target_type)
  2908. {
  2909. #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
  2910. if (target_type & TARGET_SOCK_NONBLOCK) {
  2911. int flags = fcntl(fd, F_GETFL);
  2912. if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) {
  2913. close(fd);
  2914. return -TARGET_EINVAL;
  2915. }
  2916. }
  2917. #endif
  2918. return fd;
  2919. }
  2920. /* do_socket() Must return target values and target errnos. */
  2921. static abi_long do_socket(int domain, int type, int protocol)
  2922. {
  2923. int target_type = type;
  2924. int ret;
  2925. ret = target_to_host_sock_type(&type);
  2926. if (ret) {
  2927. return ret;
  2928. }
  2929. if (domain == PF_NETLINK && !(
  2930. #ifdef CONFIG_RTNETLINK
  2931. protocol == NETLINK_ROUTE ||
  2932. #endif
  2933. protocol == NETLINK_KOBJECT_UEVENT ||
  2934. protocol == NETLINK_AUDIT)) {
  2935. return -TARGET_EPROTONOSUPPORT;
  2936. }
  2937. if (domain == AF_PACKET ||
  2938. (domain == AF_INET && type == SOCK_PACKET)) {
  2939. protocol = tswap16(protocol);
  2940. }
  2941. ret = get_errno(socket(domain, type, protocol));
  2942. if (ret >= 0) {
  2943. ret = sock_flags_fixup(ret, target_type);
  2944. if (type == SOCK_PACKET) {
  2945. /* Manage an obsolete case :
  2946. * if socket type is SOCK_PACKET, bind by name
  2947. */
  2948. fd_trans_register(ret, &target_packet_trans);
  2949. } else if (domain == PF_NETLINK) {
  2950. switch (protocol) {
  2951. #ifdef CONFIG_RTNETLINK
  2952. case NETLINK_ROUTE:
  2953. fd_trans_register(ret, &target_netlink_route_trans);
  2954. break;
  2955. #endif
  2956. case NETLINK_KOBJECT_UEVENT:
  2957. /* nothing to do: messages are strings */
  2958. break;
  2959. case NETLINK_AUDIT:
  2960. fd_trans_register(ret, &target_netlink_audit_trans);
  2961. break;
  2962. default:
  2963. g_assert_not_reached();
  2964. }
  2965. }
  2966. }
  2967. return ret;
  2968. }
  2969. /* do_bind() Must return target values and target errnos. */
  2970. static abi_long do_bind(int sockfd, abi_ulong target_addr,
  2971. socklen_t addrlen)
  2972. {
  2973. void *addr;
  2974. abi_long ret;
  2975. if ((int)addrlen < 0) {
  2976. return -TARGET_EINVAL;
  2977. }
  2978. addr = alloca(addrlen+1);
  2979. ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
  2980. if (ret)
  2981. return ret;
  2982. return get_errno(bind(sockfd, addr, addrlen));
  2983. }
  2984. /* do_connect() Must return target values and target errnos. */
  2985. static abi_long do_connect(int sockfd, abi_ulong target_addr,
  2986. socklen_t addrlen)
  2987. {
  2988. void *addr;
  2989. abi_long ret;
  2990. if ((int)addrlen < 0) {
  2991. return -TARGET_EINVAL;
  2992. }
  2993. addr = alloca(addrlen+1);
  2994. ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
  2995. if (ret)
  2996. return ret;
  2997. return get_errno(safe_connect(sockfd, addr, addrlen));
  2998. }
  2999. /* do_sendrecvmsg_locked() Must return target values and target errnos. */
  3000. static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
  3001. int flags, int send)
  3002. {
  3003. abi_long ret, len;
  3004. struct msghdr msg;
  3005. abi_ulong count;
  3006. struct iovec *vec;
  3007. abi_ulong target_vec;
  3008. if (msgp->msg_name) {
  3009. msg.msg_namelen = tswap32(msgp->msg_namelen);
  3010. msg.msg_name = alloca(msg.msg_namelen+1);
  3011. ret = target_to_host_sockaddr(fd, msg.msg_name,
  3012. tswapal(msgp->msg_name),
  3013. msg.msg_namelen);
  3014. if (ret == -TARGET_EFAULT) {
  3015. /* For connected sockets msg_name and msg_namelen must
  3016. * be ignored, so returning EFAULT immediately is wrong.
  3017. * Instead, pass a bad msg_name to the host kernel, and
  3018. * let it decide whether to return EFAULT or not.
  3019. */
  3020. msg.msg_name = (void *)-1;
  3021. } else if (ret) {
  3022. goto out2;
  3023. }
  3024. } else {
  3025. msg.msg_name = NULL;
  3026. msg.msg_namelen = 0;
  3027. }
  3028. msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
  3029. msg.msg_control = alloca(msg.msg_controllen);
  3030. memset(msg.msg_control, 0, msg.msg_controllen);
  3031. msg.msg_flags = tswap32(msgp->msg_flags);
  3032. count = tswapal(msgp->msg_iovlen);
  3033. target_vec = tswapal(msgp->msg_iov);
  3034. if (count > IOV_MAX) {
  3035. /* sendrcvmsg returns a different errno for this condition than
  3036. * readv/writev, so we must catch it here before lock_iovec() does.
  3037. */
  3038. ret = -TARGET_EMSGSIZE;
  3039. goto out2;
  3040. }
  3041. vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
  3042. target_vec, count, send);
  3043. if (vec == NULL) {
  3044. ret = -host_to_target_errno(errno);
  3045. goto out2;
  3046. }
  3047. msg.msg_iovlen = count;
  3048. msg.msg_iov = vec;
  3049. if (send) {
  3050. if (fd_trans_target_to_host_data(fd)) {
  3051. void *host_msg;
  3052. host_msg = g_malloc(msg.msg_iov->iov_len);
  3053. memcpy(host_msg, msg.msg_iov->iov_base, msg.msg_iov->iov_len);
  3054. ret = fd_trans_target_to_host_data(fd)(host_msg,
  3055. msg.msg_iov->iov_len);
  3056. if (ret >= 0) {
  3057. msg.msg_iov->iov_base = host_msg;
  3058. ret = get_errno(safe_sendmsg(fd, &msg, flags));
  3059. }
  3060. g_free(host_msg);
  3061. } else {
  3062. ret = target_to_host_cmsg(&msg, msgp);
  3063. if (ret == 0) {
  3064. ret = get_errno(safe_sendmsg(fd, &msg, flags));
  3065. }
  3066. }
  3067. } else {
  3068. ret = get_errno(safe_recvmsg(fd, &msg, flags));
  3069. if (!is_error(ret)) {
  3070. len = ret;
  3071. if (fd_trans_host_to_target_data(fd)) {
  3072. ret = fd_trans_host_to_target_data(fd)(msg.msg_iov->iov_base,
  3073. MIN(msg.msg_iov->iov_len, len));
  3074. } else {
  3075. ret = host_to_target_cmsg(msgp, &msg);
  3076. }
  3077. if (!is_error(ret)) {
  3078. msgp->msg_namelen = tswap32(msg.msg_namelen);
  3079. msgp->msg_flags = tswap32(msg.msg_flags);
  3080. if (msg.msg_name != NULL && msg.msg_name != (void *)-1) {
  3081. ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
  3082. msg.msg_name, msg.msg_namelen);
  3083. if (ret) {
  3084. goto out;
  3085. }
  3086. }
  3087. ret = len;
  3088. }
  3089. }
  3090. }
  3091. out:
  3092. unlock_iovec(vec, target_vec, count, !send);
  3093. out2:
  3094. return ret;
  3095. }
  3096. static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
  3097. int flags, int send)
  3098. {
  3099. abi_long ret;
  3100. struct target_msghdr *msgp;
  3101. if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
  3102. msgp,
  3103. target_msg,
  3104. send ? 1 : 0)) {
  3105. return -TARGET_EFAULT;
  3106. }
  3107. ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
  3108. unlock_user_struct(msgp, target_msg, send ? 0 : 1);
  3109. return ret;
  3110. }
  3111. /* We don't rely on the C library to have sendmmsg/recvmmsg support,
  3112. * so it might not have this *mmsg-specific flag either.
  3113. */
  3114. #ifndef MSG_WAITFORONE
  3115. #define MSG_WAITFORONE 0x10000
  3116. #endif
  3117. static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec,
  3118. unsigned int vlen, unsigned int flags,
  3119. int send)
  3120. {
  3121. struct target_mmsghdr *mmsgp;
  3122. abi_long ret = 0;
  3123. int i;
  3124. if (vlen > UIO_MAXIOV) {
  3125. vlen = UIO_MAXIOV;
  3126. }
  3127. mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1);
  3128. if (!mmsgp) {
  3129. return -TARGET_EFAULT;
  3130. }
  3131. for (i = 0; i < vlen; i++) {
  3132. ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send);
  3133. if (is_error(ret)) {
  3134. break;
  3135. }
  3136. mmsgp[i].msg_len = tswap32(ret);
  3137. /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
  3138. if (flags & MSG_WAITFORONE) {
  3139. flags |= MSG_DONTWAIT;
  3140. }
  3141. }
  3142. unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i);
  3143. /* Return number of datagrams sent if we sent any at all;
  3144. * otherwise return the error.
  3145. */
  3146. if (i) {
  3147. return i;
  3148. }
  3149. return ret;
  3150. }
  3151. /* do_accept4() Must return target values and target errnos. */
  3152. static abi_long do_accept4(int fd, abi_ulong target_addr,
  3153. abi_ulong target_addrlen_addr, int flags)
  3154. {
  3155. socklen_t addrlen, ret_addrlen;
  3156. void *addr;
  3157. abi_long ret;
  3158. int host_flags;
  3159. host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
  3160. if (target_addr == 0) {
  3161. return get_errno(safe_accept4(fd, NULL, NULL, host_flags));
  3162. }
  3163. /* linux returns EINVAL if addrlen pointer is invalid */
  3164. if (get_user_u32(addrlen, target_addrlen_addr))
  3165. return -TARGET_EINVAL;
  3166. if ((int)addrlen < 0) {
  3167. return -TARGET_EINVAL;
  3168. }
  3169. if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
  3170. return -TARGET_EINVAL;
  3171. addr = alloca(addrlen);
  3172. ret_addrlen = addrlen;
  3173. ret = get_errno(safe_accept4(fd, addr, &ret_addrlen, host_flags));
  3174. if (!is_error(ret)) {
  3175. host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
  3176. if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
  3177. ret = -TARGET_EFAULT;
  3178. }
  3179. }
  3180. return ret;
  3181. }
  3182. /* do_getpeername() Must return target values and target errnos. */
  3183. static abi_long do_getpeername(int fd, abi_ulong target_addr,
  3184. abi_ulong target_addrlen_addr)
  3185. {
  3186. socklen_t addrlen, ret_addrlen;
  3187. void *addr;
  3188. abi_long ret;
  3189. if (get_user_u32(addrlen, target_addrlen_addr))
  3190. return -TARGET_EFAULT;
  3191. if ((int)addrlen < 0) {
  3192. return -TARGET_EINVAL;
  3193. }
  3194. if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
  3195. return -TARGET_EFAULT;
  3196. addr = alloca(addrlen);
  3197. ret_addrlen = addrlen;
  3198. ret = get_errno(getpeername(fd, addr, &ret_addrlen));
  3199. if (!is_error(ret)) {
  3200. host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
  3201. if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
  3202. ret = -TARGET_EFAULT;
  3203. }
  3204. }
  3205. return ret;
  3206. }
  3207. /* do_getsockname() Must return target values and target errnos. */
  3208. static abi_long do_getsockname(int fd, abi_ulong target_addr,
  3209. abi_ulong target_addrlen_addr)
  3210. {
  3211. socklen_t addrlen, ret_addrlen;
  3212. void *addr;
  3213. abi_long ret;
  3214. if (get_user_u32(addrlen, target_addrlen_addr))
  3215. return -TARGET_EFAULT;
  3216. if ((int)addrlen < 0) {
  3217. return -TARGET_EINVAL;
  3218. }
  3219. if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
  3220. return -TARGET_EFAULT;
  3221. addr = alloca(addrlen);
  3222. ret_addrlen = addrlen;
  3223. ret = get_errno(getsockname(fd, addr, &ret_addrlen));
  3224. if (!is_error(ret)) {
  3225. host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
  3226. if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
  3227. ret = -TARGET_EFAULT;
  3228. }
  3229. }
  3230. return ret;
  3231. }
  3232. /* do_socketpair() Must return target values and target errnos. */
  3233. static abi_long do_socketpair(int domain, int type, int protocol,
  3234. abi_ulong target_tab_addr)
  3235. {
  3236. int tab[2];
  3237. abi_long ret;
  3238. target_to_host_sock_type(&type);
  3239. ret = get_errno(socketpair(domain, type, protocol, tab));
  3240. if (!is_error(ret)) {
  3241. if (put_user_s32(tab[0], target_tab_addr)
  3242. || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
  3243. ret = -TARGET_EFAULT;
  3244. }
  3245. return ret;
  3246. }
  3247. /* do_sendto() Must return target values and target errnos. */
  3248. static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
  3249. abi_ulong target_addr, socklen_t addrlen)
  3250. {
  3251. void *addr;
  3252. void *host_msg;
  3253. void *copy_msg = NULL;
  3254. abi_long ret;
  3255. if ((int)addrlen < 0) {
  3256. return -TARGET_EINVAL;
  3257. }
  3258. host_msg = lock_user(VERIFY_READ, msg, len, 1);
  3259. if (!host_msg)
  3260. return -TARGET_EFAULT;
  3261. if (fd_trans_target_to_host_data(fd)) {
  3262. copy_msg = host_msg;
  3263. host_msg = g_malloc(len);
  3264. memcpy(host_msg, copy_msg, len);
  3265. ret = fd_trans_target_to_host_data(fd)(host_msg, len);
  3266. if (ret < 0) {
  3267. goto fail;
  3268. }
  3269. }
  3270. if (target_addr) {
  3271. addr = alloca(addrlen+1);
  3272. ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen);
  3273. if (ret) {
  3274. goto fail;
  3275. }
  3276. ret = get_errno(safe_sendto(fd, host_msg, len, flags, addr, addrlen));
  3277. } else {
  3278. ret = get_errno(safe_sendto(fd, host_msg, len, flags, NULL, 0));
  3279. }
  3280. fail:
  3281. if (copy_msg) {
  3282. g_free(host_msg);
  3283. host_msg = copy_msg;
  3284. }
  3285. unlock_user(host_msg, msg, 0);
  3286. return ret;
  3287. }
  3288. /* do_recvfrom() Must return target values and target errnos. */
  3289. static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
  3290. abi_ulong target_addr,
  3291. abi_ulong target_addrlen)
  3292. {
  3293. socklen_t addrlen, ret_addrlen;
  3294. void *addr;
  3295. void *host_msg;
  3296. abi_long ret;
  3297. host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
  3298. if (!host_msg)
  3299. return -TARGET_EFAULT;
  3300. if (target_addr) {
  3301. if (get_user_u32(addrlen, target_addrlen)) {
  3302. ret = -TARGET_EFAULT;
  3303. goto fail;
  3304. }
  3305. if ((int)addrlen < 0) {
  3306. ret = -TARGET_EINVAL;
  3307. goto fail;
  3308. }
  3309. addr = alloca(addrlen);
  3310. ret_addrlen = addrlen;
  3311. ret = get_errno(safe_recvfrom(fd, host_msg, len, flags,
  3312. addr, &ret_addrlen));
  3313. } else {
  3314. addr = NULL; /* To keep compiler quiet. */
  3315. addrlen = 0; /* To keep compiler quiet. */
  3316. ret = get_errno(safe_recvfrom(fd, host_msg, len, flags, NULL, 0));
  3317. }
  3318. if (!is_error(ret)) {
  3319. if (fd_trans_host_to_target_data(fd)) {
  3320. abi_long trans;
  3321. trans = fd_trans_host_to_target_data(fd)(host_msg, MIN(ret, len));
  3322. if (is_error(trans)) {
  3323. ret = trans;
  3324. goto fail;
  3325. }
  3326. }
  3327. if (target_addr) {
  3328. host_to_target_sockaddr(target_addr, addr,
  3329. MIN(addrlen, ret_addrlen));
  3330. if (put_user_u32(ret_addrlen, target_addrlen)) {
  3331. ret = -TARGET_EFAULT;
  3332. goto fail;
  3333. }
  3334. }
  3335. unlock_user(host_msg, msg, len);
  3336. } else {
  3337. fail:
  3338. unlock_user(host_msg, msg, 0);
  3339. }
  3340. return ret;
  3341. }
  3342. #ifdef TARGET_NR_socketcall
  3343. /* do_socketcall() must return target values and target errnos. */
  3344. static abi_long do_socketcall(int num, abi_ulong vptr)
  3345. {
  3346. static const unsigned nargs[] = { /* number of arguments per operation */
  3347. [TARGET_SYS_SOCKET] = 3, /* domain, type, protocol */
  3348. [TARGET_SYS_BIND] = 3, /* fd, addr, addrlen */
  3349. [TARGET_SYS_CONNECT] = 3, /* fd, addr, addrlen */
  3350. [TARGET_SYS_LISTEN] = 2, /* fd, backlog */
  3351. [TARGET_SYS_ACCEPT] = 3, /* fd, addr, addrlen */
  3352. [TARGET_SYS_GETSOCKNAME] = 3, /* fd, addr, addrlen */
  3353. [TARGET_SYS_GETPEERNAME] = 3, /* fd, addr, addrlen */
  3354. [TARGET_SYS_SOCKETPAIR] = 4, /* domain, type, protocol, tab */
  3355. [TARGET_SYS_SEND] = 4, /* fd, msg, len, flags */
  3356. [TARGET_SYS_RECV] = 4, /* fd, msg, len, flags */
  3357. [TARGET_SYS_SENDTO] = 6, /* fd, msg, len, flags, addr, addrlen */
  3358. [TARGET_SYS_RECVFROM] = 6, /* fd, msg, len, flags, addr, addrlen */
  3359. [TARGET_SYS_SHUTDOWN] = 2, /* fd, how */
  3360. [TARGET_SYS_SETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
  3361. [TARGET_SYS_GETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
  3362. [TARGET_SYS_SENDMSG] = 3, /* fd, msg, flags */
  3363. [TARGET_SYS_RECVMSG] = 3, /* fd, msg, flags */
  3364. [TARGET_SYS_ACCEPT4] = 4, /* fd, addr, addrlen, flags */
  3365. [TARGET_SYS_RECVMMSG] = 4, /* fd, msgvec, vlen, flags */
  3366. [TARGET_SYS_SENDMMSG] = 4, /* fd, msgvec, vlen, flags */
  3367. };
  3368. abi_long a[6]; /* max 6 args */
  3369. unsigned i;
  3370. /* check the range of the first argument num */
  3371. /* (TARGET_SYS_SENDMMSG is the highest among TARGET_SYS_xxx) */
  3372. if (num < 1 || num > TARGET_SYS_SENDMMSG) {
  3373. return -TARGET_EINVAL;
  3374. }
  3375. /* ensure we have space for args */
  3376. if (nargs[num] > ARRAY_SIZE(a)) {
  3377. return -TARGET_EINVAL;
  3378. }
  3379. /* collect the arguments in a[] according to nargs[] */
  3380. for (i = 0; i < nargs[num]; ++i) {
  3381. if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) {
  3382. return -TARGET_EFAULT;
  3383. }
  3384. }
  3385. /* now when we have the args, invoke the appropriate underlying function */
  3386. switch (num) {
  3387. case TARGET_SYS_SOCKET: /* domain, type, protocol */
  3388. return do_socket(a[0], a[1], a[2]);
  3389. case TARGET_SYS_BIND: /* sockfd, addr, addrlen */
  3390. return do_bind(a[0], a[1], a[2]);
  3391. case TARGET_SYS_CONNECT: /* sockfd, addr, addrlen */
  3392. return do_connect(a[0], a[1], a[2]);
  3393. case TARGET_SYS_LISTEN: /* sockfd, backlog */
  3394. return get_errno(listen(a[0], a[1]));
  3395. case TARGET_SYS_ACCEPT: /* sockfd, addr, addrlen */
  3396. return do_accept4(a[0], a[1], a[2], 0);
  3397. case TARGET_SYS_GETSOCKNAME: /* sockfd, addr, addrlen */
  3398. return do_getsockname(a[0], a[1], a[2]);
  3399. case TARGET_SYS_GETPEERNAME: /* sockfd, addr, addrlen */
  3400. return do_getpeername(a[0], a[1], a[2]);
  3401. case TARGET_SYS_SOCKETPAIR: /* domain, type, protocol, tab */
  3402. return do_socketpair(a[0], a[1], a[2], a[3]);
  3403. case TARGET_SYS_SEND: /* sockfd, msg, len, flags */
  3404. return do_sendto(a[0], a[1], a[2], a[3], 0, 0);
  3405. case TARGET_SYS_RECV: /* sockfd, msg, len, flags */
  3406. return do_recvfrom(a[0], a[1], a[2], a[3], 0, 0);
  3407. case TARGET_SYS_SENDTO: /* sockfd, msg, len, flags, addr, addrlen */
  3408. return do_sendto(a[0], a[1], a[2], a[3], a[4], a[5]);
  3409. case TARGET_SYS_RECVFROM: /* sockfd, msg, len, flags, addr, addrlen */
  3410. return do_recvfrom(a[0], a[1], a[2], a[3], a[4], a[5]);
  3411. case TARGET_SYS_SHUTDOWN: /* sockfd, how */
  3412. return get_errno(shutdown(a[0], a[1]));
  3413. case TARGET_SYS_SETSOCKOPT: /* sockfd, level, optname, optval, optlen */
  3414. return do_setsockopt(a[0], a[1], a[2], a[3], a[4]);
  3415. case TARGET_SYS_GETSOCKOPT: /* sockfd, level, optname, optval, optlen */
  3416. return do_getsockopt(a[0], a[1], a[2], a[3], a[4]);
  3417. case TARGET_SYS_SENDMSG: /* sockfd, msg, flags */
  3418. return do_sendrecvmsg(a[0], a[1], a[2], 1);
  3419. case TARGET_SYS_RECVMSG: /* sockfd, msg, flags */
  3420. return do_sendrecvmsg(a[0], a[1], a[2], 0);
  3421. case TARGET_SYS_ACCEPT4: /* sockfd, addr, addrlen, flags */
  3422. return do_accept4(a[0], a[1], a[2], a[3]);
  3423. case TARGET_SYS_RECVMMSG: /* sockfd, msgvec, vlen, flags */
  3424. return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 0);
  3425. case TARGET_SYS_SENDMMSG: /* sockfd, msgvec, vlen, flags */
  3426. return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 1);
  3427. default:
  3428. qemu_log_mask(LOG_UNIMP, "Unsupported socketcall: %d\n", num);
  3429. return -TARGET_EINVAL;
  3430. }
  3431. }
  3432. #endif
  3433. #define N_SHM_REGIONS 32
  3434. static struct shm_region {
  3435. abi_ulong start;
  3436. abi_ulong size;
  3437. bool in_use;
  3438. } shm_regions[N_SHM_REGIONS];
  3439. #ifndef TARGET_SEMID64_DS
  3440. /* asm-generic version of this struct */
  3441. struct target_semid64_ds
  3442. {
  3443. struct target_ipc_perm sem_perm;
  3444. abi_ulong sem_otime;
  3445. #if TARGET_ABI_BITS == 32
  3446. abi_ulong __unused1;
  3447. #endif
  3448. abi_ulong sem_ctime;
  3449. #if TARGET_ABI_BITS == 32
  3450. abi_ulong __unused2;
  3451. #endif
  3452. abi_ulong sem_nsems;
  3453. abi_ulong __unused3;
  3454. abi_ulong __unused4;
  3455. };
  3456. #endif
  3457. static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
  3458. abi_ulong target_addr)
  3459. {
  3460. struct target_ipc_perm *target_ip;
  3461. struct target_semid64_ds *target_sd;
  3462. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  3463. return -TARGET_EFAULT;
  3464. target_ip = &(target_sd->sem_perm);
  3465. host_ip->__key = tswap32(target_ip->__key);
  3466. host_ip->uid = tswap32(target_ip->uid);
  3467. host_ip->gid = tswap32(target_ip->gid);
  3468. host_ip->cuid = tswap32(target_ip->cuid);
  3469. host_ip->cgid = tswap32(target_ip->cgid);
  3470. #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
  3471. host_ip->mode = tswap32(target_ip->mode);
  3472. #else
  3473. host_ip->mode = tswap16(target_ip->mode);
  3474. #endif
  3475. #if defined(TARGET_PPC)
  3476. host_ip->__seq = tswap32(target_ip->__seq);
  3477. #else
  3478. host_ip->__seq = tswap16(target_ip->__seq);
  3479. #endif
  3480. unlock_user_struct(target_sd, target_addr, 0);
  3481. return 0;
  3482. }
  3483. static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
  3484. struct ipc_perm *host_ip)
  3485. {
  3486. struct target_ipc_perm *target_ip;
  3487. struct target_semid64_ds *target_sd;
  3488. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  3489. return -TARGET_EFAULT;
  3490. target_ip = &(target_sd->sem_perm);
  3491. target_ip->__key = tswap32(host_ip->__key);
  3492. target_ip->uid = tswap32(host_ip->uid);
  3493. target_ip->gid = tswap32(host_ip->gid);
  3494. target_ip->cuid = tswap32(host_ip->cuid);
  3495. target_ip->cgid = tswap32(host_ip->cgid);
  3496. #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
  3497. target_ip->mode = tswap32(host_ip->mode);
  3498. #else
  3499. target_ip->mode = tswap16(host_ip->mode);
  3500. #endif
  3501. #if defined(TARGET_PPC)
  3502. target_ip->__seq = tswap32(host_ip->__seq);
  3503. #else
  3504. target_ip->__seq = tswap16(host_ip->__seq);
  3505. #endif
  3506. unlock_user_struct(target_sd, target_addr, 1);
  3507. return 0;
  3508. }
  3509. static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
  3510. abi_ulong target_addr)
  3511. {
  3512. struct target_semid64_ds *target_sd;
  3513. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  3514. return -TARGET_EFAULT;
  3515. if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
  3516. return -TARGET_EFAULT;
  3517. host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
  3518. host_sd->sem_otime = tswapal(target_sd->sem_otime);
  3519. host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
  3520. unlock_user_struct(target_sd, target_addr, 0);
  3521. return 0;
  3522. }
  3523. static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
  3524. struct semid_ds *host_sd)
  3525. {
  3526. struct target_semid64_ds *target_sd;
  3527. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  3528. return -TARGET_EFAULT;
  3529. if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
  3530. return -TARGET_EFAULT;
  3531. target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
  3532. target_sd->sem_otime = tswapal(host_sd->sem_otime);
  3533. target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
  3534. unlock_user_struct(target_sd, target_addr, 1);
  3535. return 0;
  3536. }
  3537. struct target_seminfo {
  3538. int semmap;
  3539. int semmni;
  3540. int semmns;
  3541. int semmnu;
  3542. int semmsl;
  3543. int semopm;
  3544. int semume;
  3545. int semusz;
  3546. int semvmx;
  3547. int semaem;
  3548. };
  3549. static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
  3550. struct seminfo *host_seminfo)
  3551. {
  3552. struct target_seminfo *target_seminfo;
  3553. if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
  3554. return -TARGET_EFAULT;
  3555. __put_user(host_seminfo->semmap, &target_seminfo->semmap);
  3556. __put_user(host_seminfo->semmni, &target_seminfo->semmni);
  3557. __put_user(host_seminfo->semmns, &target_seminfo->semmns);
  3558. __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
  3559. __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
  3560. __put_user(host_seminfo->semopm, &target_seminfo->semopm);
  3561. __put_user(host_seminfo->semume, &target_seminfo->semume);
  3562. __put_user(host_seminfo->semusz, &target_seminfo->semusz);
  3563. __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
  3564. __put_user(host_seminfo->semaem, &target_seminfo->semaem);
  3565. unlock_user_struct(target_seminfo, target_addr, 1);
  3566. return 0;
  3567. }
  3568. union semun {
  3569. int val;
  3570. struct semid_ds *buf;
  3571. unsigned short *array;
  3572. struct seminfo *__buf;
  3573. };
  3574. union target_semun {
  3575. int val;
  3576. abi_ulong buf;
  3577. abi_ulong array;
  3578. abi_ulong __buf;
  3579. };
  3580. static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
  3581. abi_ulong target_addr)
  3582. {
  3583. int nsems;
  3584. unsigned short *array;
  3585. union semun semun;
  3586. struct semid_ds semid_ds;
  3587. int i, ret;
  3588. semun.buf = &semid_ds;
  3589. ret = semctl(semid, 0, IPC_STAT, semun);
  3590. if (ret == -1)
  3591. return get_errno(ret);
  3592. nsems = semid_ds.sem_nsems;
  3593. *host_array = g_try_new(unsigned short, nsems);
  3594. if (!*host_array) {
  3595. return -TARGET_ENOMEM;
  3596. }
  3597. array = lock_user(VERIFY_READ, target_addr,
  3598. nsems*sizeof(unsigned short), 1);
  3599. if (!array) {
  3600. g_free(*host_array);
  3601. return -TARGET_EFAULT;
  3602. }
  3603. for(i=0; i<nsems; i++) {
  3604. __get_user((*host_array)[i], &array[i]);
  3605. }
  3606. unlock_user(array, target_addr, 0);
  3607. return 0;
  3608. }
  3609. static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
  3610. unsigned short **host_array)
  3611. {
  3612. int nsems;
  3613. unsigned short *array;
  3614. union semun semun;
  3615. struct semid_ds semid_ds;
  3616. int i, ret;
  3617. semun.buf = &semid_ds;
  3618. ret = semctl(semid, 0, IPC_STAT, semun);
  3619. if (ret == -1)
  3620. return get_errno(ret);
  3621. nsems = semid_ds.sem_nsems;
  3622. array = lock_user(VERIFY_WRITE, target_addr,
  3623. nsems*sizeof(unsigned short), 0);
  3624. if (!array)
  3625. return -TARGET_EFAULT;
  3626. for(i=0; i<nsems; i++) {
  3627. __put_user((*host_array)[i], &array[i]);
  3628. }
  3629. g_free(*host_array);
  3630. unlock_user(array, target_addr, 1);
  3631. return 0;
  3632. }
  3633. static inline abi_long do_semctl(int semid, int semnum, int cmd,
  3634. abi_ulong target_arg)
  3635. {
  3636. union target_semun target_su = { .buf = target_arg };
  3637. union semun arg;
  3638. struct semid_ds dsarg;
  3639. unsigned short *array = NULL;
  3640. struct seminfo seminfo;
  3641. abi_long ret = -TARGET_EINVAL;
  3642. abi_long err;
  3643. cmd &= 0xff;
  3644. switch( cmd ) {
  3645. case GETVAL:
  3646. case SETVAL:
  3647. /* In 64 bit cross-endian situations, we will erroneously pick up
  3648. * the wrong half of the union for the "val" element. To rectify
  3649. * this, the entire 8-byte structure is byteswapped, followed by
  3650. * a swap of the 4 byte val field. In other cases, the data is
  3651. * already in proper host byte order. */
  3652. if (sizeof(target_su.val) != (sizeof(target_su.buf))) {
  3653. target_su.buf = tswapal(target_su.buf);
  3654. arg.val = tswap32(target_su.val);
  3655. } else {
  3656. arg.val = target_su.val;
  3657. }
  3658. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3659. break;
  3660. case GETALL:
  3661. case SETALL:
  3662. err = target_to_host_semarray(semid, &array, target_su.array);
  3663. if (err)
  3664. return err;
  3665. arg.array = array;
  3666. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3667. err = host_to_target_semarray(semid, target_su.array, &array);
  3668. if (err)
  3669. return err;
  3670. break;
  3671. case IPC_STAT:
  3672. case IPC_SET:
  3673. case SEM_STAT:
  3674. err = target_to_host_semid_ds(&dsarg, target_su.buf);
  3675. if (err)
  3676. return err;
  3677. arg.buf = &dsarg;
  3678. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3679. err = host_to_target_semid_ds(target_su.buf, &dsarg);
  3680. if (err)
  3681. return err;
  3682. break;
  3683. case IPC_INFO:
  3684. case SEM_INFO:
  3685. arg.__buf = &seminfo;
  3686. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3687. err = host_to_target_seminfo(target_su.__buf, &seminfo);
  3688. if (err)
  3689. return err;
  3690. break;
  3691. case IPC_RMID:
  3692. case GETPID:
  3693. case GETNCNT:
  3694. case GETZCNT:
  3695. ret = get_errno(semctl(semid, semnum, cmd, NULL));
  3696. break;
  3697. }
  3698. return ret;
  3699. }
  3700. struct target_sembuf {
  3701. unsigned short sem_num;
  3702. short sem_op;
  3703. short sem_flg;
  3704. };
  3705. static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
  3706. abi_ulong target_addr,
  3707. unsigned nsops)
  3708. {
  3709. struct target_sembuf *target_sembuf;
  3710. int i;
  3711. target_sembuf = lock_user(VERIFY_READ, target_addr,
  3712. nsops*sizeof(struct target_sembuf), 1);
  3713. if (!target_sembuf)
  3714. return -TARGET_EFAULT;
  3715. for(i=0; i<nsops; i++) {
  3716. __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
  3717. __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
  3718. __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
  3719. }
  3720. unlock_user(target_sembuf, target_addr, 0);
  3721. return 0;
  3722. }
  3723. #if defined(TARGET_NR_ipc) || defined(TARGET_NR_semop) || \
  3724. defined(TARGET_NR_semtimedop) || defined(TARGET_NR_semtimedop_time64)
  3725. /*
  3726. * This macro is required to handle the s390 variants, which passes the
  3727. * arguments in a different order than default.
  3728. */
  3729. #ifdef __s390x__
  3730. #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
  3731. (__nsops), (__timeout), (__sops)
  3732. #else
  3733. #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
  3734. (__nsops), 0, (__sops), (__timeout)
  3735. #endif
  3736. static inline abi_long do_semtimedop(int semid,
  3737. abi_long ptr,
  3738. unsigned nsops,
  3739. abi_long timeout, bool time64)
  3740. {
  3741. struct sembuf *sops;
  3742. struct timespec ts, *pts = NULL;
  3743. abi_long ret;
  3744. if (timeout) {
  3745. pts = &ts;
  3746. if (time64) {
  3747. if (target_to_host_timespec64(pts, timeout)) {
  3748. return -TARGET_EFAULT;
  3749. }
  3750. } else {
  3751. if (target_to_host_timespec(pts, timeout)) {
  3752. return -TARGET_EFAULT;
  3753. }
  3754. }
  3755. }
  3756. if (nsops > TARGET_SEMOPM) {
  3757. return -TARGET_E2BIG;
  3758. }
  3759. sops = g_new(struct sembuf, nsops);
  3760. if (target_to_host_sembuf(sops, ptr, nsops)) {
  3761. g_free(sops);
  3762. return -TARGET_EFAULT;
  3763. }
  3764. ret = -TARGET_ENOSYS;
  3765. #ifdef __NR_semtimedop
  3766. ret = get_errno(safe_semtimedop(semid, sops, nsops, pts));
  3767. #endif
  3768. #ifdef __NR_ipc
  3769. if (ret == -TARGET_ENOSYS) {
  3770. ret = get_errno(safe_ipc(IPCOP_semtimedop, semid,
  3771. SEMTIMEDOP_IPC_ARGS(nsops, sops, (long)pts)));
  3772. }
  3773. #endif
  3774. g_free(sops);
  3775. return ret;
  3776. }
  3777. #endif
  3778. struct target_msqid_ds
  3779. {
  3780. struct target_ipc_perm msg_perm;
  3781. abi_ulong msg_stime;
  3782. #if TARGET_ABI_BITS == 32
  3783. abi_ulong __unused1;
  3784. #endif
  3785. abi_ulong msg_rtime;
  3786. #if TARGET_ABI_BITS == 32
  3787. abi_ulong __unused2;
  3788. #endif
  3789. abi_ulong msg_ctime;
  3790. #if TARGET_ABI_BITS == 32
  3791. abi_ulong __unused3;
  3792. #endif
  3793. abi_ulong __msg_cbytes;
  3794. abi_ulong msg_qnum;
  3795. abi_ulong msg_qbytes;
  3796. abi_ulong msg_lspid;
  3797. abi_ulong msg_lrpid;
  3798. abi_ulong __unused4;
  3799. abi_ulong __unused5;
  3800. };
  3801. static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
  3802. abi_ulong target_addr)
  3803. {
  3804. struct target_msqid_ds *target_md;
  3805. if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
  3806. return -TARGET_EFAULT;
  3807. if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
  3808. return -TARGET_EFAULT;
  3809. host_md->msg_stime = tswapal(target_md->msg_stime);
  3810. host_md->msg_rtime = tswapal(target_md->msg_rtime);
  3811. host_md->msg_ctime = tswapal(target_md->msg_ctime);
  3812. host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
  3813. host_md->msg_qnum = tswapal(target_md->msg_qnum);
  3814. host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
  3815. host_md->msg_lspid = tswapal(target_md->msg_lspid);
  3816. host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
  3817. unlock_user_struct(target_md, target_addr, 0);
  3818. return 0;
  3819. }
  3820. static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
  3821. struct msqid_ds *host_md)
  3822. {
  3823. struct target_msqid_ds *target_md;
  3824. if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
  3825. return -TARGET_EFAULT;
  3826. if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
  3827. return -TARGET_EFAULT;
  3828. target_md->msg_stime = tswapal(host_md->msg_stime);
  3829. target_md->msg_rtime = tswapal(host_md->msg_rtime);
  3830. target_md->msg_ctime = tswapal(host_md->msg_ctime);
  3831. target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
  3832. target_md->msg_qnum = tswapal(host_md->msg_qnum);
  3833. target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
  3834. target_md->msg_lspid = tswapal(host_md->msg_lspid);
  3835. target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
  3836. unlock_user_struct(target_md, target_addr, 1);
  3837. return 0;
  3838. }
  3839. struct target_msginfo {
  3840. int msgpool;
  3841. int msgmap;
  3842. int msgmax;
  3843. int msgmnb;
  3844. int msgmni;
  3845. int msgssz;
  3846. int msgtql;
  3847. unsigned short int msgseg;
  3848. };
  3849. static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
  3850. struct msginfo *host_msginfo)
  3851. {
  3852. struct target_msginfo *target_msginfo;
  3853. if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
  3854. return -TARGET_EFAULT;
  3855. __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
  3856. __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
  3857. __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
  3858. __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
  3859. __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
  3860. __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
  3861. __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
  3862. __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
  3863. unlock_user_struct(target_msginfo, target_addr, 1);
  3864. return 0;
  3865. }
  3866. static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
  3867. {
  3868. struct msqid_ds dsarg;
  3869. struct msginfo msginfo;
  3870. abi_long ret = -TARGET_EINVAL;
  3871. cmd &= 0xff;
  3872. switch (cmd) {
  3873. case IPC_STAT:
  3874. case IPC_SET:
  3875. case MSG_STAT:
  3876. if (target_to_host_msqid_ds(&dsarg,ptr))
  3877. return -TARGET_EFAULT;
  3878. ret = get_errno(msgctl(msgid, cmd, &dsarg));
  3879. if (host_to_target_msqid_ds(ptr,&dsarg))
  3880. return -TARGET_EFAULT;
  3881. break;
  3882. case IPC_RMID:
  3883. ret = get_errno(msgctl(msgid, cmd, NULL));
  3884. break;
  3885. case IPC_INFO:
  3886. case MSG_INFO:
  3887. ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
  3888. if (host_to_target_msginfo(ptr, &msginfo))
  3889. return -TARGET_EFAULT;
  3890. break;
  3891. }
  3892. return ret;
  3893. }
  3894. struct target_msgbuf {
  3895. abi_long mtype;
  3896. char mtext[1];
  3897. };
  3898. static inline abi_long do_msgsnd(int msqid, abi_long msgp,
  3899. ssize_t msgsz, int msgflg)
  3900. {
  3901. struct target_msgbuf *target_mb;
  3902. struct msgbuf *host_mb;
  3903. abi_long ret = 0;
  3904. if (msgsz < 0) {
  3905. return -TARGET_EINVAL;
  3906. }
  3907. if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
  3908. return -TARGET_EFAULT;
  3909. host_mb = g_try_malloc(msgsz + sizeof(long));
  3910. if (!host_mb) {
  3911. unlock_user_struct(target_mb, msgp, 0);
  3912. return -TARGET_ENOMEM;
  3913. }
  3914. host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
  3915. memcpy(host_mb->mtext, target_mb->mtext, msgsz);
  3916. ret = -TARGET_ENOSYS;
  3917. #ifdef __NR_msgsnd
  3918. ret = get_errno(safe_msgsnd(msqid, host_mb, msgsz, msgflg));
  3919. #endif
  3920. #ifdef __NR_ipc
  3921. if (ret == -TARGET_ENOSYS) {
  3922. #ifdef __s390x__
  3923. ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
  3924. host_mb));
  3925. #else
  3926. ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
  3927. host_mb, 0));
  3928. #endif
  3929. }
  3930. #endif
  3931. g_free(host_mb);
  3932. unlock_user_struct(target_mb, msgp, 0);
  3933. return ret;
  3934. }
  3935. #ifdef __NR_ipc
  3936. #if defined(__sparc__)
  3937. /* SPARC for msgrcv it does not use the kludge on final 2 arguments. */
  3938. #define MSGRCV_ARGS(__msgp, __msgtyp) __msgp, __msgtyp
  3939. #elif defined(__s390x__)
  3940. /* The s390 sys_ipc variant has only five parameters. */
  3941. #define MSGRCV_ARGS(__msgp, __msgtyp) \
  3942. ((long int[]){(long int)__msgp, __msgtyp})
  3943. #else
  3944. #define MSGRCV_ARGS(__msgp, __msgtyp) \
  3945. ((long int[]){(long int)__msgp, __msgtyp}), 0
  3946. #endif
  3947. #endif
  3948. static inline abi_long do_msgrcv(int msqid, abi_long msgp,
  3949. ssize_t msgsz, abi_long msgtyp,
  3950. int msgflg)
  3951. {
  3952. struct target_msgbuf *target_mb;
  3953. char *target_mtext;
  3954. struct msgbuf *host_mb;
  3955. abi_long ret = 0;
  3956. if (msgsz < 0) {
  3957. return -TARGET_EINVAL;
  3958. }
  3959. if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
  3960. return -TARGET_EFAULT;
  3961. host_mb = g_try_malloc(msgsz + sizeof(long));
  3962. if (!host_mb) {
  3963. ret = -TARGET_ENOMEM;
  3964. goto end;
  3965. }
  3966. ret = -TARGET_ENOSYS;
  3967. #ifdef __NR_msgrcv
  3968. ret = get_errno(safe_msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
  3969. #endif
  3970. #ifdef __NR_ipc
  3971. if (ret == -TARGET_ENOSYS) {
  3972. ret = get_errno(safe_ipc(IPCOP_CALL(1, IPCOP_msgrcv), msqid, msgsz,
  3973. msgflg, MSGRCV_ARGS(host_mb, msgtyp)));
  3974. }
  3975. #endif
  3976. if (ret > 0) {
  3977. abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
  3978. target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
  3979. if (!target_mtext) {
  3980. ret = -TARGET_EFAULT;
  3981. goto end;
  3982. }
  3983. memcpy(target_mb->mtext, host_mb->mtext, ret);
  3984. unlock_user(target_mtext, target_mtext_addr, ret);
  3985. }
  3986. target_mb->mtype = tswapal(host_mb->mtype);
  3987. end:
  3988. if (target_mb)
  3989. unlock_user_struct(target_mb, msgp, 1);
  3990. g_free(host_mb);
  3991. return ret;
  3992. }
  3993. static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
  3994. abi_ulong target_addr)
  3995. {
  3996. struct target_shmid_ds *target_sd;
  3997. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  3998. return -TARGET_EFAULT;
  3999. if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
  4000. return -TARGET_EFAULT;
  4001. __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
  4002. __get_user(host_sd->shm_atime, &target_sd->shm_atime);
  4003. __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
  4004. __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
  4005. __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
  4006. __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
  4007. __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
  4008. unlock_user_struct(target_sd, target_addr, 0);
  4009. return 0;
  4010. }
  4011. static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
  4012. struct shmid_ds *host_sd)
  4013. {
  4014. struct target_shmid_ds *target_sd;
  4015. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  4016. return -TARGET_EFAULT;
  4017. if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
  4018. return -TARGET_EFAULT;
  4019. __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
  4020. __put_user(host_sd->shm_atime, &target_sd->shm_atime);
  4021. __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
  4022. __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
  4023. __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
  4024. __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
  4025. __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
  4026. unlock_user_struct(target_sd, target_addr, 1);
  4027. return 0;
  4028. }
  4029. struct target_shminfo {
  4030. abi_ulong shmmax;
  4031. abi_ulong shmmin;
  4032. abi_ulong shmmni;
  4033. abi_ulong shmseg;
  4034. abi_ulong shmall;
  4035. };
  4036. static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
  4037. struct shminfo *host_shminfo)
  4038. {
  4039. struct target_shminfo *target_shminfo;
  4040. if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
  4041. return -TARGET_EFAULT;
  4042. __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
  4043. __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
  4044. __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
  4045. __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
  4046. __put_user(host_shminfo->shmall, &target_shminfo->shmall);
  4047. unlock_user_struct(target_shminfo, target_addr, 1);
  4048. return 0;
  4049. }
  4050. struct target_shm_info {
  4051. int used_ids;
  4052. abi_ulong shm_tot;
  4053. abi_ulong shm_rss;
  4054. abi_ulong shm_swp;
  4055. abi_ulong swap_attempts;
  4056. abi_ulong swap_successes;
  4057. };
  4058. static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
  4059. struct shm_info *host_shm_info)
  4060. {
  4061. struct target_shm_info *target_shm_info;
  4062. if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
  4063. return -TARGET_EFAULT;
  4064. __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
  4065. __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
  4066. __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
  4067. __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
  4068. __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
  4069. __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
  4070. unlock_user_struct(target_shm_info, target_addr, 1);
  4071. return 0;
  4072. }
  4073. static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
  4074. {
  4075. struct shmid_ds dsarg;
  4076. struct shminfo shminfo;
  4077. struct shm_info shm_info;
  4078. abi_long ret = -TARGET_EINVAL;
  4079. cmd &= 0xff;
  4080. switch(cmd) {
  4081. case IPC_STAT:
  4082. case IPC_SET:
  4083. case SHM_STAT:
  4084. if (target_to_host_shmid_ds(&dsarg, buf))
  4085. return -TARGET_EFAULT;
  4086. ret = get_errno(shmctl(shmid, cmd, &dsarg));
  4087. if (host_to_target_shmid_ds(buf, &dsarg))
  4088. return -TARGET_EFAULT;
  4089. break;
  4090. case IPC_INFO:
  4091. ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
  4092. if (host_to_target_shminfo(buf, &shminfo))
  4093. return -TARGET_EFAULT;
  4094. break;
  4095. case SHM_INFO:
  4096. ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
  4097. if (host_to_target_shm_info(buf, &shm_info))
  4098. return -TARGET_EFAULT;
  4099. break;
  4100. case IPC_RMID:
  4101. case SHM_LOCK:
  4102. case SHM_UNLOCK:
  4103. ret = get_errno(shmctl(shmid, cmd, NULL));
  4104. break;
  4105. }
  4106. return ret;
  4107. }
  4108. #ifndef TARGET_FORCE_SHMLBA
  4109. /* For most architectures, SHMLBA is the same as the page size;
  4110. * some architectures have larger values, in which case they should
  4111. * define TARGET_FORCE_SHMLBA and provide a target_shmlba() function.
  4112. * This corresponds to the kernel arch code defining __ARCH_FORCE_SHMLBA
  4113. * and defining its own value for SHMLBA.
  4114. *
  4115. * The kernel also permits SHMLBA to be set by the architecture to a
  4116. * value larger than the page size without setting __ARCH_FORCE_SHMLBA;
  4117. * this means that addresses are rounded to the large size if
  4118. * SHM_RND is set but addresses not aligned to that size are not rejected
  4119. * as long as they are at least page-aligned. Since the only architecture
  4120. * which uses this is ia64 this code doesn't provide for that oddity.
  4121. */
  4122. static inline abi_ulong target_shmlba(CPUArchState *cpu_env)
  4123. {
  4124. return TARGET_PAGE_SIZE;
  4125. }
  4126. #endif
  4127. static inline abi_ulong do_shmat(CPUArchState *cpu_env,
  4128. int shmid, abi_ulong shmaddr, int shmflg)
  4129. {
  4130. abi_long raddr;
  4131. void *host_raddr;
  4132. struct shmid_ds shm_info;
  4133. int i,ret;
  4134. abi_ulong shmlba;
  4135. /* find out the length of the shared memory segment */
  4136. ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
  4137. if (is_error(ret)) {
  4138. /* can't get length, bail out */
  4139. return ret;
  4140. }
  4141. shmlba = target_shmlba(cpu_env);
  4142. if (shmaddr & (shmlba - 1)) {
  4143. if (shmflg & SHM_RND) {
  4144. shmaddr &= ~(shmlba - 1);
  4145. } else {
  4146. return -TARGET_EINVAL;
  4147. }
  4148. }
  4149. if (!guest_range_valid(shmaddr, shm_info.shm_segsz)) {
  4150. return -TARGET_EINVAL;
  4151. }
  4152. mmap_lock();
  4153. if (shmaddr)
  4154. host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
  4155. else {
  4156. abi_ulong mmap_start;
  4157. /* In order to use the host shmat, we need to honor host SHMLBA. */
  4158. mmap_start = mmap_find_vma(0, shm_info.shm_segsz, MAX(SHMLBA, shmlba));
  4159. if (mmap_start == -1) {
  4160. errno = ENOMEM;
  4161. host_raddr = (void *)-1;
  4162. } else
  4163. host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
  4164. }
  4165. if (host_raddr == (void *)-1) {
  4166. mmap_unlock();
  4167. return get_errno((long)host_raddr);
  4168. }
  4169. raddr=h2g((unsigned long)host_raddr);
  4170. page_set_flags(raddr, raddr + shm_info.shm_segsz,
  4171. PAGE_VALID | PAGE_READ |
  4172. ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
  4173. for (i = 0; i < N_SHM_REGIONS; i++) {
  4174. if (!shm_regions[i].in_use) {
  4175. shm_regions[i].in_use = true;
  4176. shm_regions[i].start = raddr;
  4177. shm_regions[i].size = shm_info.shm_segsz;
  4178. break;
  4179. }
  4180. }
  4181. mmap_unlock();
  4182. return raddr;
  4183. }
  4184. static inline abi_long do_shmdt(abi_ulong shmaddr)
  4185. {
  4186. int i;
  4187. abi_long rv;
  4188. mmap_lock();
  4189. for (i = 0; i < N_SHM_REGIONS; ++i) {
  4190. if (shm_regions[i].in_use && shm_regions[i].start == shmaddr) {
  4191. shm_regions[i].in_use = false;
  4192. page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
  4193. break;
  4194. }
  4195. }
  4196. rv = get_errno(shmdt(g2h(shmaddr)));
  4197. mmap_unlock();
  4198. return rv;
  4199. }
  4200. #ifdef TARGET_NR_ipc
  4201. /* ??? This only works with linear mappings. */
  4202. /* do_ipc() must return target values and target errnos. */
  4203. static abi_long do_ipc(CPUArchState *cpu_env,
  4204. unsigned int call, abi_long first,
  4205. abi_long second, abi_long third,
  4206. abi_long ptr, abi_long fifth)
  4207. {
  4208. int version;
  4209. abi_long ret = 0;
  4210. version = call >> 16;
  4211. call &= 0xffff;
  4212. switch (call) {
  4213. case IPCOP_semop:
  4214. ret = do_semtimedop(first, ptr, second, 0, false);
  4215. break;
  4216. case IPCOP_semtimedop:
  4217. /*
  4218. * The s390 sys_ipc variant has only five parameters instead of six
  4219. * (as for default variant) and the only difference is the handling of
  4220. * SEMTIMEDOP where on s390 the third parameter is used as a pointer
  4221. * to a struct timespec where the generic variant uses fifth parameter.
  4222. */
  4223. #if defined(TARGET_S390X)
  4224. ret = do_semtimedop(first, ptr, second, third, TARGET_ABI_BITS == 64);
  4225. #else
  4226. ret = do_semtimedop(first, ptr, second, fifth, TARGET_ABI_BITS == 64);
  4227. #endif
  4228. break;
  4229. case IPCOP_semget:
  4230. ret = get_errno(semget(first, second, third));
  4231. break;
  4232. case IPCOP_semctl: {
  4233. /* The semun argument to semctl is passed by value, so dereference the
  4234. * ptr argument. */
  4235. abi_ulong atptr;
  4236. get_user_ual(atptr, ptr);
  4237. ret = do_semctl(first, second, third, atptr);
  4238. break;
  4239. }
  4240. case IPCOP_msgget:
  4241. ret = get_errno(msgget(first, second));
  4242. break;
  4243. case IPCOP_msgsnd:
  4244. ret = do_msgsnd(first, ptr, second, third);
  4245. break;
  4246. case IPCOP_msgctl:
  4247. ret = do_msgctl(first, second, ptr);
  4248. break;
  4249. case IPCOP_msgrcv:
  4250. switch (version) {
  4251. case 0:
  4252. {
  4253. struct target_ipc_kludge {
  4254. abi_long msgp;
  4255. abi_long msgtyp;
  4256. } *tmp;
  4257. if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
  4258. ret = -TARGET_EFAULT;
  4259. break;
  4260. }
  4261. ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
  4262. unlock_user_struct(tmp, ptr, 0);
  4263. break;
  4264. }
  4265. default:
  4266. ret = do_msgrcv(first, ptr, second, fifth, third);
  4267. }
  4268. break;
  4269. case IPCOP_shmat:
  4270. switch (version) {
  4271. default:
  4272. {
  4273. abi_ulong raddr;
  4274. raddr = do_shmat(cpu_env, first, ptr, second);
  4275. if (is_error(raddr))
  4276. return get_errno(raddr);
  4277. if (put_user_ual(raddr, third))
  4278. return -TARGET_EFAULT;
  4279. break;
  4280. }
  4281. case 1:
  4282. ret = -TARGET_EINVAL;
  4283. break;
  4284. }
  4285. break;
  4286. case IPCOP_shmdt:
  4287. ret = do_shmdt(ptr);
  4288. break;
  4289. case IPCOP_shmget:
  4290. /* IPC_* flag values are the same on all linux platforms */
  4291. ret = get_errno(shmget(first, second, third));
  4292. break;
  4293. /* IPC_* and SHM_* command values are the same on all linux platforms */
  4294. case IPCOP_shmctl:
  4295. ret = do_shmctl(first, second, ptr);
  4296. break;
  4297. default:
  4298. qemu_log_mask(LOG_UNIMP, "Unsupported ipc call: %d (version %d)\n",
  4299. call, version);
  4300. ret = -TARGET_ENOSYS;
  4301. break;
  4302. }
  4303. return ret;
  4304. }
  4305. #endif
  4306. /* kernel structure types definitions */
  4307. #define STRUCT(name, ...) STRUCT_ ## name,
  4308. #define STRUCT_SPECIAL(name) STRUCT_ ## name,
  4309. enum {
  4310. #include "syscall_types.h"
  4311. STRUCT_MAX
  4312. };
  4313. #undef STRUCT
  4314. #undef STRUCT_SPECIAL
  4315. #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
  4316. #define STRUCT_SPECIAL(name)
  4317. #include "syscall_types.h"
  4318. #undef STRUCT
  4319. #undef STRUCT_SPECIAL
  4320. #define MAX_STRUCT_SIZE 4096
  4321. #ifdef CONFIG_FIEMAP
  4322. /* So fiemap access checks don't overflow on 32 bit systems.
  4323. * This is very slightly smaller than the limit imposed by
  4324. * the underlying kernel.
  4325. */
  4326. #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
  4327. / sizeof(struct fiemap_extent))
  4328. static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
  4329. int fd, int cmd, abi_long arg)
  4330. {
  4331. /* The parameter for this ioctl is a struct fiemap followed
  4332. * by an array of struct fiemap_extent whose size is set
  4333. * in fiemap->fm_extent_count. The array is filled in by the
  4334. * ioctl.
  4335. */
  4336. int target_size_in, target_size_out;
  4337. struct fiemap *fm;
  4338. const argtype *arg_type = ie->arg_type;
  4339. const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
  4340. void *argptr, *p;
  4341. abi_long ret;
  4342. int i, extent_size = thunk_type_size(extent_arg_type, 0);
  4343. uint32_t outbufsz;
  4344. int free_fm = 0;
  4345. assert(arg_type[0] == TYPE_PTR);
  4346. assert(ie->access == IOC_RW);
  4347. arg_type++;
  4348. target_size_in = thunk_type_size(arg_type, 0);
  4349. argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
  4350. if (!argptr) {
  4351. return -TARGET_EFAULT;
  4352. }
  4353. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4354. unlock_user(argptr, arg, 0);
  4355. fm = (struct fiemap *)buf_temp;
  4356. if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
  4357. return -TARGET_EINVAL;
  4358. }
  4359. outbufsz = sizeof (*fm) +
  4360. (sizeof(struct fiemap_extent) * fm->fm_extent_count);
  4361. if (outbufsz > MAX_STRUCT_SIZE) {
  4362. /* We can't fit all the extents into the fixed size buffer.
  4363. * Allocate one that is large enough and use it instead.
  4364. */
  4365. fm = g_try_malloc(outbufsz);
  4366. if (!fm) {
  4367. return -TARGET_ENOMEM;
  4368. }
  4369. memcpy(fm, buf_temp, sizeof(struct fiemap));
  4370. free_fm = 1;
  4371. }
  4372. ret = get_errno(safe_ioctl(fd, ie->host_cmd, fm));
  4373. if (!is_error(ret)) {
  4374. target_size_out = target_size_in;
  4375. /* An extent_count of 0 means we were only counting the extents
  4376. * so there are no structs to copy
  4377. */
  4378. if (fm->fm_extent_count != 0) {
  4379. target_size_out += fm->fm_mapped_extents * extent_size;
  4380. }
  4381. argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
  4382. if (!argptr) {
  4383. ret = -TARGET_EFAULT;
  4384. } else {
  4385. /* Convert the struct fiemap */
  4386. thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
  4387. if (fm->fm_extent_count != 0) {
  4388. p = argptr + target_size_in;
  4389. /* ...and then all the struct fiemap_extents */
  4390. for (i = 0; i < fm->fm_mapped_extents; i++) {
  4391. thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
  4392. THUNK_TARGET);
  4393. p += extent_size;
  4394. }
  4395. }
  4396. unlock_user(argptr, arg, target_size_out);
  4397. }
  4398. }
  4399. if (free_fm) {
  4400. g_free(fm);
  4401. }
  4402. return ret;
  4403. }
  4404. #endif
  4405. static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
  4406. int fd, int cmd, abi_long arg)
  4407. {
  4408. const argtype *arg_type = ie->arg_type;
  4409. int target_size;
  4410. void *argptr;
  4411. int ret;
  4412. struct ifconf *host_ifconf;
  4413. uint32_t outbufsz;
  4414. const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
  4415. int target_ifreq_size;
  4416. int nb_ifreq;
  4417. int free_buf = 0;
  4418. int i;
  4419. int target_ifc_len;
  4420. abi_long target_ifc_buf;
  4421. int host_ifc_len;
  4422. char *host_ifc_buf;
  4423. assert(arg_type[0] == TYPE_PTR);
  4424. assert(ie->access == IOC_RW);
  4425. arg_type++;
  4426. target_size = thunk_type_size(arg_type, 0);
  4427. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4428. if (!argptr)
  4429. return -TARGET_EFAULT;
  4430. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4431. unlock_user(argptr, arg, 0);
  4432. host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
  4433. target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
  4434. target_ifreq_size = thunk_type_size(ifreq_arg_type, 0);
  4435. if (target_ifc_buf != 0) {
  4436. target_ifc_len = host_ifconf->ifc_len;
  4437. nb_ifreq = target_ifc_len / target_ifreq_size;
  4438. host_ifc_len = nb_ifreq * sizeof(struct ifreq);
  4439. outbufsz = sizeof(*host_ifconf) + host_ifc_len;
  4440. if (outbufsz > MAX_STRUCT_SIZE) {
  4441. /*
  4442. * We can't fit all the extents into the fixed size buffer.
  4443. * Allocate one that is large enough and use it instead.
  4444. */
  4445. host_ifconf = malloc(outbufsz);
  4446. if (!host_ifconf) {
  4447. return -TARGET_ENOMEM;
  4448. }
  4449. memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
  4450. free_buf = 1;
  4451. }
  4452. host_ifc_buf = (char *)host_ifconf + sizeof(*host_ifconf);
  4453. host_ifconf->ifc_len = host_ifc_len;
  4454. } else {
  4455. host_ifc_buf = NULL;
  4456. }
  4457. host_ifconf->ifc_buf = host_ifc_buf;
  4458. ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_ifconf));
  4459. if (!is_error(ret)) {
  4460. /* convert host ifc_len to target ifc_len */
  4461. nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
  4462. target_ifc_len = nb_ifreq * target_ifreq_size;
  4463. host_ifconf->ifc_len = target_ifc_len;
  4464. /* restore target ifc_buf */
  4465. host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
  4466. /* copy struct ifconf to target user */
  4467. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4468. if (!argptr)
  4469. return -TARGET_EFAULT;
  4470. thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
  4471. unlock_user(argptr, arg, target_size);
  4472. if (target_ifc_buf != 0) {
  4473. /* copy ifreq[] to target user */
  4474. argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
  4475. for (i = 0; i < nb_ifreq ; i++) {
  4476. thunk_convert(argptr + i * target_ifreq_size,
  4477. host_ifc_buf + i * sizeof(struct ifreq),
  4478. ifreq_arg_type, THUNK_TARGET);
  4479. }
  4480. unlock_user(argptr, target_ifc_buf, target_ifc_len);
  4481. }
  4482. }
  4483. if (free_buf) {
  4484. free(host_ifconf);
  4485. }
  4486. return ret;
  4487. }
  4488. #if defined(CONFIG_USBFS)
  4489. #if HOST_LONG_BITS > 64
  4490. #error USBDEVFS thunks do not support >64 bit hosts yet.
  4491. #endif
  4492. struct live_urb {
  4493. uint64_t target_urb_adr;
  4494. uint64_t target_buf_adr;
  4495. char *target_buf_ptr;
  4496. struct usbdevfs_urb host_urb;
  4497. };
  4498. static GHashTable *usbdevfs_urb_hashtable(void)
  4499. {
  4500. static GHashTable *urb_hashtable;
  4501. if (!urb_hashtable) {
  4502. urb_hashtable = g_hash_table_new(g_int64_hash, g_int64_equal);
  4503. }
  4504. return urb_hashtable;
  4505. }
  4506. static void urb_hashtable_insert(struct live_urb *urb)
  4507. {
  4508. GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
  4509. g_hash_table_insert(urb_hashtable, urb, urb);
  4510. }
  4511. static struct live_urb *urb_hashtable_lookup(uint64_t target_urb_adr)
  4512. {
  4513. GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
  4514. return g_hash_table_lookup(urb_hashtable, &target_urb_adr);
  4515. }
  4516. static void urb_hashtable_remove(struct live_urb *urb)
  4517. {
  4518. GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
  4519. g_hash_table_remove(urb_hashtable, urb);
  4520. }
  4521. static abi_long
  4522. do_ioctl_usbdevfs_reapurb(const IOCTLEntry *ie, uint8_t *buf_temp,
  4523. int fd, int cmd, abi_long arg)
  4524. {
  4525. const argtype usbfsurb_arg_type[] = { MK_STRUCT(STRUCT_usbdevfs_urb) };
  4526. const argtype ptrvoid_arg_type[] = { TYPE_PTRVOID, 0, 0 };
  4527. struct live_urb *lurb;
  4528. void *argptr;
  4529. uint64_t hurb;
  4530. int target_size;
  4531. uintptr_t target_urb_adr;
  4532. abi_long ret;
  4533. target_size = thunk_type_size(usbfsurb_arg_type, THUNK_TARGET);
  4534. memset(buf_temp, 0, sizeof(uint64_t));
  4535. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4536. if (is_error(ret)) {
  4537. return ret;
  4538. }
  4539. memcpy(&hurb, buf_temp, sizeof(uint64_t));
  4540. lurb = (void *)((uintptr_t)hurb - offsetof(struct live_urb, host_urb));
  4541. if (!lurb->target_urb_adr) {
  4542. return -TARGET_EFAULT;
  4543. }
  4544. urb_hashtable_remove(lurb);
  4545. unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr,
  4546. lurb->host_urb.buffer_length);
  4547. lurb->target_buf_ptr = NULL;
  4548. /* restore the guest buffer pointer */
  4549. lurb->host_urb.buffer = (void *)(uintptr_t)lurb->target_buf_adr;
  4550. /* update the guest urb struct */
  4551. argptr = lock_user(VERIFY_WRITE, lurb->target_urb_adr, target_size, 0);
  4552. if (!argptr) {
  4553. g_free(lurb);
  4554. return -TARGET_EFAULT;
  4555. }
  4556. thunk_convert(argptr, &lurb->host_urb, usbfsurb_arg_type, THUNK_TARGET);
  4557. unlock_user(argptr, lurb->target_urb_adr, target_size);
  4558. target_size = thunk_type_size(ptrvoid_arg_type, THUNK_TARGET);
  4559. /* write back the urb handle */
  4560. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4561. if (!argptr) {
  4562. g_free(lurb);
  4563. return -TARGET_EFAULT;
  4564. }
  4565. /* GHashTable uses 64-bit keys but thunk_convert expects uintptr_t */
  4566. target_urb_adr = lurb->target_urb_adr;
  4567. thunk_convert(argptr, &target_urb_adr, ptrvoid_arg_type, THUNK_TARGET);
  4568. unlock_user(argptr, arg, target_size);
  4569. g_free(lurb);
  4570. return ret;
  4571. }
  4572. static abi_long
  4573. do_ioctl_usbdevfs_discardurb(const IOCTLEntry *ie,
  4574. uint8_t *buf_temp __attribute__((unused)),
  4575. int fd, int cmd, abi_long arg)
  4576. {
  4577. struct live_urb *lurb;
  4578. /* map target address back to host URB with metadata. */
  4579. lurb = urb_hashtable_lookup(arg);
  4580. if (!lurb) {
  4581. return -TARGET_EFAULT;
  4582. }
  4583. return get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb));
  4584. }
  4585. static abi_long
  4586. do_ioctl_usbdevfs_submiturb(const IOCTLEntry *ie, uint8_t *buf_temp,
  4587. int fd, int cmd, abi_long arg)
  4588. {
  4589. const argtype *arg_type = ie->arg_type;
  4590. int target_size;
  4591. abi_long ret;
  4592. void *argptr;
  4593. int rw_dir;
  4594. struct live_urb *lurb;
  4595. /*
  4596. * each submitted URB needs to map to a unique ID for the
  4597. * kernel, and that unique ID needs to be a pointer to
  4598. * host memory. hence, we need to malloc for each URB.
  4599. * isochronous transfers have a variable length struct.
  4600. */
  4601. arg_type++;
  4602. target_size = thunk_type_size(arg_type, THUNK_TARGET);
  4603. /* construct host copy of urb and metadata */
  4604. lurb = g_try_malloc0(sizeof(struct live_urb));
  4605. if (!lurb) {
  4606. return -TARGET_ENOMEM;
  4607. }
  4608. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4609. if (!argptr) {
  4610. g_free(lurb);
  4611. return -TARGET_EFAULT;
  4612. }
  4613. thunk_convert(&lurb->host_urb, argptr, arg_type, THUNK_HOST);
  4614. unlock_user(argptr, arg, 0);
  4615. lurb->target_urb_adr = arg;
  4616. lurb->target_buf_adr = (uintptr_t)lurb->host_urb.buffer;
  4617. /* buffer space used depends on endpoint type so lock the entire buffer */
  4618. /* control type urbs should check the buffer contents for true direction */
  4619. rw_dir = lurb->host_urb.endpoint & USB_DIR_IN ? VERIFY_WRITE : VERIFY_READ;
  4620. lurb->target_buf_ptr = lock_user(rw_dir, lurb->target_buf_adr,
  4621. lurb->host_urb.buffer_length, 1);
  4622. if (lurb->target_buf_ptr == NULL) {
  4623. g_free(lurb);
  4624. return -TARGET_EFAULT;
  4625. }
  4626. /* update buffer pointer in host copy */
  4627. lurb->host_urb.buffer = lurb->target_buf_ptr;
  4628. ret = get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb));
  4629. if (is_error(ret)) {
  4630. unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr, 0);
  4631. g_free(lurb);
  4632. } else {
  4633. urb_hashtable_insert(lurb);
  4634. }
  4635. return ret;
  4636. }
  4637. #endif /* CONFIG_USBFS */
  4638. static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
  4639. int cmd, abi_long arg)
  4640. {
  4641. void *argptr;
  4642. struct dm_ioctl *host_dm;
  4643. abi_long guest_data;
  4644. uint32_t guest_data_size;
  4645. int target_size;
  4646. const argtype *arg_type = ie->arg_type;
  4647. abi_long ret;
  4648. void *big_buf = NULL;
  4649. char *host_data;
  4650. arg_type++;
  4651. target_size = thunk_type_size(arg_type, 0);
  4652. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4653. if (!argptr) {
  4654. ret = -TARGET_EFAULT;
  4655. goto out;
  4656. }
  4657. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4658. unlock_user(argptr, arg, 0);
  4659. /* buf_temp is too small, so fetch things into a bigger buffer */
  4660. big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
  4661. memcpy(big_buf, buf_temp, target_size);
  4662. buf_temp = big_buf;
  4663. host_dm = big_buf;
  4664. guest_data = arg + host_dm->data_start;
  4665. if ((guest_data - arg) < 0) {
  4666. ret = -TARGET_EINVAL;
  4667. goto out;
  4668. }
  4669. guest_data_size = host_dm->data_size - host_dm->data_start;
  4670. host_data = (char*)host_dm + host_dm->data_start;
  4671. argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
  4672. if (!argptr) {
  4673. ret = -TARGET_EFAULT;
  4674. goto out;
  4675. }
  4676. switch (ie->host_cmd) {
  4677. case DM_REMOVE_ALL:
  4678. case DM_LIST_DEVICES:
  4679. case DM_DEV_CREATE:
  4680. case DM_DEV_REMOVE:
  4681. case DM_DEV_SUSPEND:
  4682. case DM_DEV_STATUS:
  4683. case DM_DEV_WAIT:
  4684. case DM_TABLE_STATUS:
  4685. case DM_TABLE_CLEAR:
  4686. case DM_TABLE_DEPS:
  4687. case DM_LIST_VERSIONS:
  4688. /* no input data */
  4689. break;
  4690. case DM_DEV_RENAME:
  4691. case DM_DEV_SET_GEOMETRY:
  4692. /* data contains only strings */
  4693. memcpy(host_data, argptr, guest_data_size);
  4694. break;
  4695. case DM_TARGET_MSG:
  4696. memcpy(host_data, argptr, guest_data_size);
  4697. *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
  4698. break;
  4699. case DM_TABLE_LOAD:
  4700. {
  4701. void *gspec = argptr;
  4702. void *cur_data = host_data;
  4703. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
  4704. int spec_size = thunk_type_size(arg_type, 0);
  4705. int i;
  4706. for (i = 0; i < host_dm->target_count; i++) {
  4707. struct dm_target_spec *spec = cur_data;
  4708. uint32_t next;
  4709. int slen;
  4710. thunk_convert(spec, gspec, arg_type, THUNK_HOST);
  4711. slen = strlen((char*)gspec + spec_size) + 1;
  4712. next = spec->next;
  4713. spec->next = sizeof(*spec) + slen;
  4714. strcpy((char*)&spec[1], gspec + spec_size);
  4715. gspec += next;
  4716. cur_data += spec->next;
  4717. }
  4718. break;
  4719. }
  4720. default:
  4721. ret = -TARGET_EINVAL;
  4722. unlock_user(argptr, guest_data, 0);
  4723. goto out;
  4724. }
  4725. unlock_user(argptr, guest_data, 0);
  4726. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4727. if (!is_error(ret)) {
  4728. guest_data = arg + host_dm->data_start;
  4729. guest_data_size = host_dm->data_size - host_dm->data_start;
  4730. argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
  4731. switch (ie->host_cmd) {
  4732. case DM_REMOVE_ALL:
  4733. case DM_DEV_CREATE:
  4734. case DM_DEV_REMOVE:
  4735. case DM_DEV_RENAME:
  4736. case DM_DEV_SUSPEND:
  4737. case DM_DEV_STATUS:
  4738. case DM_TABLE_LOAD:
  4739. case DM_TABLE_CLEAR:
  4740. case DM_TARGET_MSG:
  4741. case DM_DEV_SET_GEOMETRY:
  4742. /* no return data */
  4743. break;
  4744. case DM_LIST_DEVICES:
  4745. {
  4746. struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
  4747. uint32_t remaining_data = guest_data_size;
  4748. void *cur_data = argptr;
  4749. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
  4750. int nl_size = 12; /* can't use thunk_size due to alignment */
  4751. while (1) {
  4752. uint32_t next = nl->next;
  4753. if (next) {
  4754. nl->next = nl_size + (strlen(nl->name) + 1);
  4755. }
  4756. if (remaining_data < nl->next) {
  4757. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  4758. break;
  4759. }
  4760. thunk_convert(cur_data, nl, arg_type, THUNK_TARGET);
  4761. strcpy(cur_data + nl_size, nl->name);
  4762. cur_data += nl->next;
  4763. remaining_data -= nl->next;
  4764. if (!next) {
  4765. break;
  4766. }
  4767. nl = (void*)nl + next;
  4768. }
  4769. break;
  4770. }
  4771. case DM_DEV_WAIT:
  4772. case DM_TABLE_STATUS:
  4773. {
  4774. struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
  4775. void *cur_data = argptr;
  4776. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
  4777. int spec_size = thunk_type_size(arg_type, 0);
  4778. int i;
  4779. for (i = 0; i < host_dm->target_count; i++) {
  4780. uint32_t next = spec->next;
  4781. int slen = strlen((char*)&spec[1]) + 1;
  4782. spec->next = (cur_data - argptr) + spec_size + slen;
  4783. if (guest_data_size < spec->next) {
  4784. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  4785. break;
  4786. }
  4787. thunk_convert(cur_data, spec, arg_type, THUNK_TARGET);
  4788. strcpy(cur_data + spec_size, (char*)&spec[1]);
  4789. cur_data = argptr + spec->next;
  4790. spec = (void*)host_dm + host_dm->data_start + next;
  4791. }
  4792. break;
  4793. }
  4794. case DM_TABLE_DEPS:
  4795. {
  4796. void *hdata = (void*)host_dm + host_dm->data_start;
  4797. int count = *(uint32_t*)hdata;
  4798. uint64_t *hdev = hdata + 8;
  4799. uint64_t *gdev = argptr + 8;
  4800. int i;
  4801. *(uint32_t*)argptr = tswap32(count);
  4802. for (i = 0; i < count; i++) {
  4803. *gdev = tswap64(*hdev);
  4804. gdev++;
  4805. hdev++;
  4806. }
  4807. break;
  4808. }
  4809. case DM_LIST_VERSIONS:
  4810. {
  4811. struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
  4812. uint32_t remaining_data = guest_data_size;
  4813. void *cur_data = argptr;
  4814. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
  4815. int vers_size = thunk_type_size(arg_type, 0);
  4816. while (1) {
  4817. uint32_t next = vers->next;
  4818. if (next) {
  4819. vers->next = vers_size + (strlen(vers->name) + 1);
  4820. }
  4821. if (remaining_data < vers->next) {
  4822. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  4823. break;
  4824. }
  4825. thunk_convert(cur_data, vers, arg_type, THUNK_TARGET);
  4826. strcpy(cur_data + vers_size, vers->name);
  4827. cur_data += vers->next;
  4828. remaining_data -= vers->next;
  4829. if (!next) {
  4830. break;
  4831. }
  4832. vers = (void*)vers + next;
  4833. }
  4834. break;
  4835. }
  4836. default:
  4837. unlock_user(argptr, guest_data, 0);
  4838. ret = -TARGET_EINVAL;
  4839. goto out;
  4840. }
  4841. unlock_user(argptr, guest_data, guest_data_size);
  4842. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4843. if (!argptr) {
  4844. ret = -TARGET_EFAULT;
  4845. goto out;
  4846. }
  4847. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  4848. unlock_user(argptr, arg, target_size);
  4849. }
  4850. out:
  4851. g_free(big_buf);
  4852. return ret;
  4853. }
  4854. static abi_long do_ioctl_blkpg(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
  4855. int cmd, abi_long arg)
  4856. {
  4857. void *argptr;
  4858. int target_size;
  4859. const argtype *arg_type = ie->arg_type;
  4860. const argtype part_arg_type[] = { MK_STRUCT(STRUCT_blkpg_partition) };
  4861. abi_long ret;
  4862. struct blkpg_ioctl_arg *host_blkpg = (void*)buf_temp;
  4863. struct blkpg_partition host_part;
  4864. /* Read and convert blkpg */
  4865. arg_type++;
  4866. target_size = thunk_type_size(arg_type, 0);
  4867. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4868. if (!argptr) {
  4869. ret = -TARGET_EFAULT;
  4870. goto out;
  4871. }
  4872. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4873. unlock_user(argptr, arg, 0);
  4874. switch (host_blkpg->op) {
  4875. case BLKPG_ADD_PARTITION:
  4876. case BLKPG_DEL_PARTITION:
  4877. /* payload is struct blkpg_partition */
  4878. break;
  4879. default:
  4880. /* Unknown opcode */
  4881. ret = -TARGET_EINVAL;
  4882. goto out;
  4883. }
  4884. /* Read and convert blkpg->data */
  4885. arg = (abi_long)(uintptr_t)host_blkpg->data;
  4886. target_size = thunk_type_size(part_arg_type, 0);
  4887. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4888. if (!argptr) {
  4889. ret = -TARGET_EFAULT;
  4890. goto out;
  4891. }
  4892. thunk_convert(&host_part, argptr, part_arg_type, THUNK_HOST);
  4893. unlock_user(argptr, arg, 0);
  4894. /* Swizzle the data pointer to our local copy and call! */
  4895. host_blkpg->data = &host_part;
  4896. ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_blkpg));
  4897. out:
  4898. return ret;
  4899. }
  4900. static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
  4901. int fd, int cmd, abi_long arg)
  4902. {
  4903. const argtype *arg_type = ie->arg_type;
  4904. const StructEntry *se;
  4905. const argtype *field_types;
  4906. const int *dst_offsets, *src_offsets;
  4907. int target_size;
  4908. void *argptr;
  4909. abi_ulong *target_rt_dev_ptr = NULL;
  4910. unsigned long *host_rt_dev_ptr = NULL;
  4911. abi_long ret;
  4912. int i;
  4913. assert(ie->access == IOC_W);
  4914. assert(*arg_type == TYPE_PTR);
  4915. arg_type++;
  4916. assert(*arg_type == TYPE_STRUCT);
  4917. target_size = thunk_type_size(arg_type, 0);
  4918. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4919. if (!argptr) {
  4920. return -TARGET_EFAULT;
  4921. }
  4922. arg_type++;
  4923. assert(*arg_type == (int)STRUCT_rtentry);
  4924. se = struct_entries + *arg_type++;
  4925. assert(se->convert[0] == NULL);
  4926. /* convert struct here to be able to catch rt_dev string */
  4927. field_types = se->field_types;
  4928. dst_offsets = se->field_offsets[THUNK_HOST];
  4929. src_offsets = se->field_offsets[THUNK_TARGET];
  4930. for (i = 0; i < se->nb_fields; i++) {
  4931. if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) {
  4932. assert(*field_types == TYPE_PTRVOID);
  4933. target_rt_dev_ptr = (abi_ulong *)(argptr + src_offsets[i]);
  4934. host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]);
  4935. if (*target_rt_dev_ptr != 0) {
  4936. *host_rt_dev_ptr = (unsigned long)lock_user_string(
  4937. tswapal(*target_rt_dev_ptr));
  4938. if (!*host_rt_dev_ptr) {
  4939. unlock_user(argptr, arg, 0);
  4940. return -TARGET_EFAULT;
  4941. }
  4942. } else {
  4943. *host_rt_dev_ptr = 0;
  4944. }
  4945. field_types++;
  4946. continue;
  4947. }
  4948. field_types = thunk_convert(buf_temp + dst_offsets[i],
  4949. argptr + src_offsets[i],
  4950. field_types, THUNK_HOST);
  4951. }
  4952. unlock_user(argptr, arg, 0);
  4953. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4954. assert(host_rt_dev_ptr != NULL);
  4955. assert(target_rt_dev_ptr != NULL);
  4956. if (*host_rt_dev_ptr != 0) {
  4957. unlock_user((void *)*host_rt_dev_ptr,
  4958. *target_rt_dev_ptr, 0);
  4959. }
  4960. return ret;
  4961. }
  4962. static abi_long do_ioctl_kdsigaccept(const IOCTLEntry *ie, uint8_t *buf_temp,
  4963. int fd, int cmd, abi_long arg)
  4964. {
  4965. int sig = target_to_host_signal(arg);
  4966. return get_errno(safe_ioctl(fd, ie->host_cmd, sig));
  4967. }
  4968. static abi_long do_ioctl_SIOCGSTAMP(const IOCTLEntry *ie, uint8_t *buf_temp,
  4969. int fd, int cmd, abi_long arg)
  4970. {
  4971. struct timeval tv;
  4972. abi_long ret;
  4973. ret = get_errno(safe_ioctl(fd, SIOCGSTAMP, &tv));
  4974. if (is_error(ret)) {
  4975. return ret;
  4976. }
  4977. if (cmd == (int)TARGET_SIOCGSTAMP_OLD) {
  4978. if (copy_to_user_timeval(arg, &tv)) {
  4979. return -TARGET_EFAULT;
  4980. }
  4981. } else {
  4982. if (copy_to_user_timeval64(arg, &tv)) {
  4983. return -TARGET_EFAULT;
  4984. }
  4985. }
  4986. return ret;
  4987. }
  4988. static abi_long do_ioctl_SIOCGSTAMPNS(const IOCTLEntry *ie, uint8_t *buf_temp,
  4989. int fd, int cmd, abi_long arg)
  4990. {
  4991. struct timespec ts;
  4992. abi_long ret;
  4993. ret = get_errno(safe_ioctl(fd, SIOCGSTAMPNS, &ts));
  4994. if (is_error(ret)) {
  4995. return ret;
  4996. }
  4997. if (cmd == (int)TARGET_SIOCGSTAMPNS_OLD) {
  4998. if (host_to_target_timespec(arg, &ts)) {
  4999. return -TARGET_EFAULT;
  5000. }
  5001. } else{
  5002. if (host_to_target_timespec64(arg, &ts)) {
  5003. return -TARGET_EFAULT;
  5004. }
  5005. }
  5006. return ret;
  5007. }
  5008. #ifdef TIOCGPTPEER
  5009. static abi_long do_ioctl_tiocgptpeer(const IOCTLEntry *ie, uint8_t *buf_temp,
  5010. int fd, int cmd, abi_long arg)
  5011. {
  5012. int flags = target_to_host_bitmask(arg, fcntl_flags_tbl);
  5013. return get_errno(safe_ioctl(fd, ie->host_cmd, flags));
  5014. }
  5015. #endif
  5016. #ifdef HAVE_DRM_H
  5017. static void unlock_drm_version(struct drm_version *host_ver,
  5018. struct target_drm_version *target_ver,
  5019. bool copy)
  5020. {
  5021. unlock_user(host_ver->name, target_ver->name,
  5022. copy ? host_ver->name_len : 0);
  5023. unlock_user(host_ver->date, target_ver->date,
  5024. copy ? host_ver->date_len : 0);
  5025. unlock_user(host_ver->desc, target_ver->desc,
  5026. copy ? host_ver->desc_len : 0);
  5027. }
  5028. static inline abi_long target_to_host_drmversion(struct drm_version *host_ver,
  5029. struct target_drm_version *target_ver)
  5030. {
  5031. memset(host_ver, 0, sizeof(*host_ver));
  5032. __get_user(host_ver->name_len, &target_ver->name_len);
  5033. if (host_ver->name_len) {
  5034. host_ver->name = lock_user(VERIFY_WRITE, target_ver->name,
  5035. target_ver->name_len, 0);
  5036. if (!host_ver->name) {
  5037. return -EFAULT;
  5038. }
  5039. }
  5040. __get_user(host_ver->date_len, &target_ver->date_len);
  5041. if (host_ver->date_len) {
  5042. host_ver->date = lock_user(VERIFY_WRITE, target_ver->date,
  5043. target_ver->date_len, 0);
  5044. if (!host_ver->date) {
  5045. goto err;
  5046. }
  5047. }
  5048. __get_user(host_ver->desc_len, &target_ver->desc_len);
  5049. if (host_ver->desc_len) {
  5050. host_ver->desc = lock_user(VERIFY_WRITE, target_ver->desc,
  5051. target_ver->desc_len, 0);
  5052. if (!host_ver->desc) {
  5053. goto err;
  5054. }
  5055. }
  5056. return 0;
  5057. err:
  5058. unlock_drm_version(host_ver, target_ver, false);
  5059. return -EFAULT;
  5060. }
  5061. static inline void host_to_target_drmversion(
  5062. struct target_drm_version *target_ver,
  5063. struct drm_version *host_ver)
  5064. {
  5065. __put_user(host_ver->version_major, &target_ver->version_major);
  5066. __put_user(host_ver->version_minor, &target_ver->version_minor);
  5067. __put_user(host_ver->version_patchlevel, &target_ver->version_patchlevel);
  5068. __put_user(host_ver->name_len, &target_ver->name_len);
  5069. __put_user(host_ver->date_len, &target_ver->date_len);
  5070. __put_user(host_ver->desc_len, &target_ver->desc_len);
  5071. unlock_drm_version(host_ver, target_ver, true);
  5072. }
  5073. static abi_long do_ioctl_drm(const IOCTLEntry *ie, uint8_t *buf_temp,
  5074. int fd, int cmd, abi_long arg)
  5075. {
  5076. struct drm_version *ver;
  5077. struct target_drm_version *target_ver;
  5078. abi_long ret;
  5079. switch (ie->host_cmd) {
  5080. case DRM_IOCTL_VERSION:
  5081. if (!lock_user_struct(VERIFY_WRITE, target_ver, arg, 0)) {
  5082. return -TARGET_EFAULT;
  5083. }
  5084. ver = (struct drm_version *)buf_temp;
  5085. ret = target_to_host_drmversion(ver, target_ver);
  5086. if (!is_error(ret)) {
  5087. ret = get_errno(safe_ioctl(fd, ie->host_cmd, ver));
  5088. if (is_error(ret)) {
  5089. unlock_drm_version(ver, target_ver, false);
  5090. } else {
  5091. host_to_target_drmversion(target_ver, ver);
  5092. }
  5093. }
  5094. unlock_user_struct(target_ver, arg, 0);
  5095. return ret;
  5096. }
  5097. return -TARGET_ENOSYS;
  5098. }
  5099. static abi_long do_ioctl_drm_i915_getparam(const IOCTLEntry *ie,
  5100. struct drm_i915_getparam *gparam,
  5101. int fd, abi_long arg)
  5102. {
  5103. abi_long ret;
  5104. int value;
  5105. struct target_drm_i915_getparam *target_gparam;
  5106. if (!lock_user_struct(VERIFY_READ, target_gparam, arg, 0)) {
  5107. return -TARGET_EFAULT;
  5108. }
  5109. __get_user(gparam->param, &target_gparam->param);
  5110. gparam->value = &value;
  5111. ret = get_errno(safe_ioctl(fd, ie->host_cmd, gparam));
  5112. put_user_s32(value, target_gparam->value);
  5113. unlock_user_struct(target_gparam, arg, 0);
  5114. return ret;
  5115. }
  5116. static abi_long do_ioctl_drm_i915(const IOCTLEntry *ie, uint8_t *buf_temp,
  5117. int fd, int cmd, abi_long arg)
  5118. {
  5119. switch (ie->host_cmd) {
  5120. case DRM_IOCTL_I915_GETPARAM:
  5121. return do_ioctl_drm_i915_getparam(ie,
  5122. (struct drm_i915_getparam *)buf_temp,
  5123. fd, arg);
  5124. default:
  5125. return -TARGET_ENOSYS;
  5126. }
  5127. }
  5128. #endif
  5129. IOCTLEntry ioctl_entries[] = {
  5130. #define IOCTL(cmd, access, ...) \
  5131. { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
  5132. #define IOCTL_SPECIAL(cmd, access, dofn, ...) \
  5133. { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
  5134. #define IOCTL_IGNORE(cmd) \
  5135. { TARGET_ ## cmd, 0, #cmd },
  5136. #include "ioctls.h"
  5137. { 0, 0, },
  5138. };
  5139. /* ??? Implement proper locking for ioctls. */
  5140. /* do_ioctl() Must return target values and target errnos. */
  5141. static abi_long do_ioctl(int fd, int cmd, abi_long arg)
  5142. {
  5143. const IOCTLEntry *ie;
  5144. const argtype *arg_type;
  5145. abi_long ret;
  5146. uint8_t buf_temp[MAX_STRUCT_SIZE];
  5147. int target_size;
  5148. void *argptr;
  5149. ie = ioctl_entries;
  5150. for(;;) {
  5151. if (ie->target_cmd == 0) {
  5152. qemu_log_mask(
  5153. LOG_UNIMP, "Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
  5154. return -TARGET_ENOSYS;
  5155. }
  5156. if (ie->target_cmd == cmd)
  5157. break;
  5158. ie++;
  5159. }
  5160. arg_type = ie->arg_type;
  5161. if (ie->do_ioctl) {
  5162. return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
  5163. } else if (!ie->host_cmd) {
  5164. /* Some architectures define BSD ioctls in their headers
  5165. that are not implemented in Linux. */
  5166. return -TARGET_ENOSYS;
  5167. }
  5168. switch(arg_type[0]) {
  5169. case TYPE_NULL:
  5170. /* no argument */
  5171. ret = get_errno(safe_ioctl(fd, ie->host_cmd));
  5172. break;
  5173. case TYPE_PTRVOID:
  5174. case TYPE_INT:
  5175. case TYPE_LONG:
  5176. case TYPE_ULONG:
  5177. ret = get_errno(safe_ioctl(fd, ie->host_cmd, arg));
  5178. break;
  5179. case TYPE_PTR:
  5180. arg_type++;
  5181. target_size = thunk_type_size(arg_type, 0);
  5182. switch(ie->access) {
  5183. case IOC_R:
  5184. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  5185. if (!is_error(ret)) {
  5186. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  5187. if (!argptr)
  5188. return -TARGET_EFAULT;
  5189. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  5190. unlock_user(argptr, arg, target_size);
  5191. }
  5192. break;
  5193. case IOC_W:
  5194. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  5195. if (!argptr)
  5196. return -TARGET_EFAULT;
  5197. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  5198. unlock_user(argptr, arg, 0);
  5199. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  5200. break;
  5201. default:
  5202. case IOC_RW:
  5203. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  5204. if (!argptr)
  5205. return -TARGET_EFAULT;
  5206. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  5207. unlock_user(argptr, arg, 0);
  5208. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  5209. if (!is_error(ret)) {
  5210. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  5211. if (!argptr)
  5212. return -TARGET_EFAULT;
  5213. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  5214. unlock_user(argptr, arg, target_size);
  5215. }
  5216. break;
  5217. }
  5218. break;
  5219. default:
  5220. qemu_log_mask(LOG_UNIMP,
  5221. "Unsupported ioctl type: cmd=0x%04lx type=%d\n",
  5222. (long)cmd, arg_type[0]);
  5223. ret = -TARGET_ENOSYS;
  5224. break;
  5225. }
  5226. return ret;
  5227. }
  5228. static const bitmask_transtbl iflag_tbl[] = {
  5229. { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
  5230. { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
  5231. { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
  5232. { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
  5233. { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
  5234. { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
  5235. { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
  5236. { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
  5237. { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
  5238. { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
  5239. { TARGET_IXON, TARGET_IXON, IXON, IXON },
  5240. { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
  5241. { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
  5242. { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
  5243. { TARGET_IUTF8, TARGET_IUTF8, IUTF8, IUTF8},
  5244. { 0, 0, 0, 0 }
  5245. };
  5246. static const bitmask_transtbl oflag_tbl[] = {
  5247. { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
  5248. { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
  5249. { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
  5250. { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
  5251. { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
  5252. { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
  5253. { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
  5254. { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
  5255. { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
  5256. { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
  5257. { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
  5258. { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
  5259. { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
  5260. { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
  5261. { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
  5262. { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
  5263. { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
  5264. { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
  5265. { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
  5266. { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
  5267. { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
  5268. { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
  5269. { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
  5270. { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
  5271. { 0, 0, 0, 0 }
  5272. };
  5273. static const bitmask_transtbl cflag_tbl[] = {
  5274. { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
  5275. { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
  5276. { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
  5277. { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
  5278. { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
  5279. { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
  5280. { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
  5281. { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
  5282. { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
  5283. { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
  5284. { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
  5285. { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
  5286. { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
  5287. { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
  5288. { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
  5289. { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
  5290. { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
  5291. { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
  5292. { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
  5293. { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
  5294. { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
  5295. { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
  5296. { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
  5297. { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
  5298. { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
  5299. { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
  5300. { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
  5301. { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
  5302. { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
  5303. { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
  5304. { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
  5305. { 0, 0, 0, 0 }
  5306. };
  5307. static const bitmask_transtbl lflag_tbl[] = {
  5308. { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
  5309. { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
  5310. { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
  5311. { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
  5312. { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
  5313. { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
  5314. { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
  5315. { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
  5316. { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
  5317. { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
  5318. { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
  5319. { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
  5320. { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
  5321. { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
  5322. { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
  5323. { TARGET_EXTPROC, TARGET_EXTPROC, EXTPROC, EXTPROC},
  5324. { 0, 0, 0, 0 }
  5325. };
  5326. static void target_to_host_termios (void *dst, const void *src)
  5327. {
  5328. struct host_termios *host = dst;
  5329. const struct target_termios *target = src;
  5330. host->c_iflag =
  5331. target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
  5332. host->c_oflag =
  5333. target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
  5334. host->c_cflag =
  5335. target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
  5336. host->c_lflag =
  5337. target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
  5338. host->c_line = target->c_line;
  5339. memset(host->c_cc, 0, sizeof(host->c_cc));
  5340. host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
  5341. host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
  5342. host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
  5343. host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
  5344. host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
  5345. host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
  5346. host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
  5347. host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
  5348. host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
  5349. host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
  5350. host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
  5351. host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
  5352. host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
  5353. host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
  5354. host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
  5355. host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
  5356. host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
  5357. }
  5358. static void host_to_target_termios (void *dst, const void *src)
  5359. {
  5360. struct target_termios *target = dst;
  5361. const struct host_termios *host = src;
  5362. target->c_iflag =
  5363. tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
  5364. target->c_oflag =
  5365. tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
  5366. target->c_cflag =
  5367. tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
  5368. target->c_lflag =
  5369. tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
  5370. target->c_line = host->c_line;
  5371. memset(target->c_cc, 0, sizeof(target->c_cc));
  5372. target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
  5373. target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
  5374. target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
  5375. target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
  5376. target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
  5377. target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
  5378. target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
  5379. target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
  5380. target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
  5381. target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
  5382. target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
  5383. target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
  5384. target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
  5385. target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
  5386. target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
  5387. target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
  5388. target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
  5389. }
  5390. static const StructEntry struct_termios_def = {
  5391. .convert = { host_to_target_termios, target_to_host_termios },
  5392. .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
  5393. .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
  5394. .print = print_termios,
  5395. };
  5396. static bitmask_transtbl mmap_flags_tbl[] = {
  5397. { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
  5398. { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
  5399. { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
  5400. { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS,
  5401. MAP_ANONYMOUS, MAP_ANONYMOUS },
  5402. { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN,
  5403. MAP_GROWSDOWN, MAP_GROWSDOWN },
  5404. { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE,
  5405. MAP_DENYWRITE, MAP_DENYWRITE },
  5406. { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE,
  5407. MAP_EXECUTABLE, MAP_EXECUTABLE },
  5408. { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
  5409. { TARGET_MAP_NORESERVE, TARGET_MAP_NORESERVE,
  5410. MAP_NORESERVE, MAP_NORESERVE },
  5411. { TARGET_MAP_HUGETLB, TARGET_MAP_HUGETLB, MAP_HUGETLB, MAP_HUGETLB },
  5412. /* MAP_STACK had been ignored by the kernel for quite some time.
  5413. Recognize it for the target insofar as we do not want to pass
  5414. it through to the host. */
  5415. { TARGET_MAP_STACK, TARGET_MAP_STACK, 0, 0 },
  5416. { 0, 0, 0, 0 }
  5417. };
  5418. /*
  5419. * NOTE: TARGET_ABI32 is defined for TARGET_I386 (but not for TARGET_X86_64)
  5420. * TARGET_I386 is defined if TARGET_X86_64 is defined
  5421. */
  5422. #if defined(TARGET_I386)
  5423. /* NOTE: there is really one LDT for all the threads */
  5424. static uint8_t *ldt_table;
  5425. static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
  5426. {
  5427. int size;
  5428. void *p;
  5429. if (!ldt_table)
  5430. return 0;
  5431. size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
  5432. if (size > bytecount)
  5433. size = bytecount;
  5434. p = lock_user(VERIFY_WRITE, ptr, size, 0);
  5435. if (!p)
  5436. return -TARGET_EFAULT;
  5437. /* ??? Should this by byteswapped? */
  5438. memcpy(p, ldt_table, size);
  5439. unlock_user(p, ptr, size);
  5440. return size;
  5441. }
  5442. /* XXX: add locking support */
  5443. static abi_long write_ldt(CPUX86State *env,
  5444. abi_ulong ptr, unsigned long bytecount, int oldmode)
  5445. {
  5446. struct target_modify_ldt_ldt_s ldt_info;
  5447. struct target_modify_ldt_ldt_s *target_ldt_info;
  5448. int seg_32bit, contents, read_exec_only, limit_in_pages;
  5449. int seg_not_present, useable, lm;
  5450. uint32_t *lp, entry_1, entry_2;
  5451. if (bytecount != sizeof(ldt_info))
  5452. return -TARGET_EINVAL;
  5453. if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
  5454. return -TARGET_EFAULT;
  5455. ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
  5456. ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
  5457. ldt_info.limit = tswap32(target_ldt_info->limit);
  5458. ldt_info.flags = tswap32(target_ldt_info->flags);
  5459. unlock_user_struct(target_ldt_info, ptr, 0);
  5460. if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
  5461. return -TARGET_EINVAL;
  5462. seg_32bit = ldt_info.flags & 1;
  5463. contents = (ldt_info.flags >> 1) & 3;
  5464. read_exec_only = (ldt_info.flags >> 3) & 1;
  5465. limit_in_pages = (ldt_info.flags >> 4) & 1;
  5466. seg_not_present = (ldt_info.flags >> 5) & 1;
  5467. useable = (ldt_info.flags >> 6) & 1;
  5468. #ifdef TARGET_ABI32
  5469. lm = 0;
  5470. #else
  5471. lm = (ldt_info.flags >> 7) & 1;
  5472. #endif
  5473. if (contents == 3) {
  5474. if (oldmode)
  5475. return -TARGET_EINVAL;
  5476. if (seg_not_present == 0)
  5477. return -TARGET_EINVAL;
  5478. }
  5479. /* allocate the LDT */
  5480. if (!ldt_table) {
  5481. env->ldt.base = target_mmap(0,
  5482. TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
  5483. PROT_READ|PROT_WRITE,
  5484. MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
  5485. if (env->ldt.base == -1)
  5486. return -TARGET_ENOMEM;
  5487. memset(g2h(env->ldt.base), 0,
  5488. TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
  5489. env->ldt.limit = 0xffff;
  5490. ldt_table = g2h(env->ldt.base);
  5491. }
  5492. /* NOTE: same code as Linux kernel */
  5493. /* Allow LDTs to be cleared by the user. */
  5494. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  5495. if (oldmode ||
  5496. (contents == 0 &&
  5497. read_exec_only == 1 &&
  5498. seg_32bit == 0 &&
  5499. limit_in_pages == 0 &&
  5500. seg_not_present == 1 &&
  5501. useable == 0 )) {
  5502. entry_1 = 0;
  5503. entry_2 = 0;
  5504. goto install;
  5505. }
  5506. }
  5507. entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
  5508. (ldt_info.limit & 0x0ffff);
  5509. entry_2 = (ldt_info.base_addr & 0xff000000) |
  5510. ((ldt_info.base_addr & 0x00ff0000) >> 16) |
  5511. (ldt_info.limit & 0xf0000) |
  5512. ((read_exec_only ^ 1) << 9) |
  5513. (contents << 10) |
  5514. ((seg_not_present ^ 1) << 15) |
  5515. (seg_32bit << 22) |
  5516. (limit_in_pages << 23) |
  5517. (lm << 21) |
  5518. 0x7000;
  5519. if (!oldmode)
  5520. entry_2 |= (useable << 20);
  5521. /* Install the new entry ... */
  5522. install:
  5523. lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
  5524. lp[0] = tswap32(entry_1);
  5525. lp[1] = tswap32(entry_2);
  5526. return 0;
  5527. }
  5528. /* specific and weird i386 syscalls */
  5529. static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
  5530. unsigned long bytecount)
  5531. {
  5532. abi_long ret;
  5533. switch (func) {
  5534. case 0:
  5535. ret = read_ldt(ptr, bytecount);
  5536. break;
  5537. case 1:
  5538. ret = write_ldt(env, ptr, bytecount, 1);
  5539. break;
  5540. case 0x11:
  5541. ret = write_ldt(env, ptr, bytecount, 0);
  5542. break;
  5543. default:
  5544. ret = -TARGET_ENOSYS;
  5545. break;
  5546. }
  5547. return ret;
  5548. }
  5549. #if defined(TARGET_ABI32)
  5550. abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
  5551. {
  5552. uint64_t *gdt_table = g2h(env->gdt.base);
  5553. struct target_modify_ldt_ldt_s ldt_info;
  5554. struct target_modify_ldt_ldt_s *target_ldt_info;
  5555. int seg_32bit, contents, read_exec_only, limit_in_pages;
  5556. int seg_not_present, useable, lm;
  5557. uint32_t *lp, entry_1, entry_2;
  5558. int i;
  5559. lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
  5560. if (!target_ldt_info)
  5561. return -TARGET_EFAULT;
  5562. ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
  5563. ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
  5564. ldt_info.limit = tswap32(target_ldt_info->limit);
  5565. ldt_info.flags = tswap32(target_ldt_info->flags);
  5566. if (ldt_info.entry_number == -1) {
  5567. for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
  5568. if (gdt_table[i] == 0) {
  5569. ldt_info.entry_number = i;
  5570. target_ldt_info->entry_number = tswap32(i);
  5571. break;
  5572. }
  5573. }
  5574. }
  5575. unlock_user_struct(target_ldt_info, ptr, 1);
  5576. if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
  5577. ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
  5578. return -TARGET_EINVAL;
  5579. seg_32bit = ldt_info.flags & 1;
  5580. contents = (ldt_info.flags >> 1) & 3;
  5581. read_exec_only = (ldt_info.flags >> 3) & 1;
  5582. limit_in_pages = (ldt_info.flags >> 4) & 1;
  5583. seg_not_present = (ldt_info.flags >> 5) & 1;
  5584. useable = (ldt_info.flags >> 6) & 1;
  5585. #ifdef TARGET_ABI32
  5586. lm = 0;
  5587. #else
  5588. lm = (ldt_info.flags >> 7) & 1;
  5589. #endif
  5590. if (contents == 3) {
  5591. if (seg_not_present == 0)
  5592. return -TARGET_EINVAL;
  5593. }
  5594. /* NOTE: same code as Linux kernel */
  5595. /* Allow LDTs to be cleared by the user. */
  5596. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  5597. if ((contents == 0 &&
  5598. read_exec_only == 1 &&
  5599. seg_32bit == 0 &&
  5600. limit_in_pages == 0 &&
  5601. seg_not_present == 1 &&
  5602. useable == 0 )) {
  5603. entry_1 = 0;
  5604. entry_2 = 0;
  5605. goto install;
  5606. }
  5607. }
  5608. entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
  5609. (ldt_info.limit & 0x0ffff);
  5610. entry_2 = (ldt_info.base_addr & 0xff000000) |
  5611. ((ldt_info.base_addr & 0x00ff0000) >> 16) |
  5612. (ldt_info.limit & 0xf0000) |
  5613. ((read_exec_only ^ 1) << 9) |
  5614. (contents << 10) |
  5615. ((seg_not_present ^ 1) << 15) |
  5616. (seg_32bit << 22) |
  5617. (limit_in_pages << 23) |
  5618. (useable << 20) |
  5619. (lm << 21) |
  5620. 0x7000;
  5621. /* Install the new entry ... */
  5622. install:
  5623. lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
  5624. lp[0] = tswap32(entry_1);
  5625. lp[1] = tswap32(entry_2);
  5626. return 0;
  5627. }
  5628. static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
  5629. {
  5630. struct target_modify_ldt_ldt_s *target_ldt_info;
  5631. uint64_t *gdt_table = g2h(env->gdt.base);
  5632. uint32_t base_addr, limit, flags;
  5633. int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
  5634. int seg_not_present, useable, lm;
  5635. uint32_t *lp, entry_1, entry_2;
  5636. lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
  5637. if (!target_ldt_info)
  5638. return -TARGET_EFAULT;
  5639. idx = tswap32(target_ldt_info->entry_number);
  5640. if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
  5641. idx > TARGET_GDT_ENTRY_TLS_MAX) {
  5642. unlock_user_struct(target_ldt_info, ptr, 1);
  5643. return -TARGET_EINVAL;
  5644. }
  5645. lp = (uint32_t *)(gdt_table + idx);
  5646. entry_1 = tswap32(lp[0]);
  5647. entry_2 = tswap32(lp[1]);
  5648. read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
  5649. contents = (entry_2 >> 10) & 3;
  5650. seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
  5651. seg_32bit = (entry_2 >> 22) & 1;
  5652. limit_in_pages = (entry_2 >> 23) & 1;
  5653. useable = (entry_2 >> 20) & 1;
  5654. #ifdef TARGET_ABI32
  5655. lm = 0;
  5656. #else
  5657. lm = (entry_2 >> 21) & 1;
  5658. #endif
  5659. flags = (seg_32bit << 0) | (contents << 1) |
  5660. (read_exec_only << 3) | (limit_in_pages << 4) |
  5661. (seg_not_present << 5) | (useable << 6) | (lm << 7);
  5662. limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
  5663. base_addr = (entry_1 >> 16) |
  5664. (entry_2 & 0xff000000) |
  5665. ((entry_2 & 0xff) << 16);
  5666. target_ldt_info->base_addr = tswapal(base_addr);
  5667. target_ldt_info->limit = tswap32(limit);
  5668. target_ldt_info->flags = tswap32(flags);
  5669. unlock_user_struct(target_ldt_info, ptr, 1);
  5670. return 0;
  5671. }
  5672. abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
  5673. {
  5674. return -TARGET_ENOSYS;
  5675. }
  5676. #else
  5677. abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
  5678. {
  5679. abi_long ret = 0;
  5680. abi_ulong val;
  5681. int idx;
  5682. switch(code) {
  5683. case TARGET_ARCH_SET_GS:
  5684. case TARGET_ARCH_SET_FS:
  5685. if (code == TARGET_ARCH_SET_GS)
  5686. idx = R_GS;
  5687. else
  5688. idx = R_FS;
  5689. cpu_x86_load_seg(env, idx, 0);
  5690. env->segs[idx].base = addr;
  5691. break;
  5692. case TARGET_ARCH_GET_GS:
  5693. case TARGET_ARCH_GET_FS:
  5694. if (code == TARGET_ARCH_GET_GS)
  5695. idx = R_GS;
  5696. else
  5697. idx = R_FS;
  5698. val = env->segs[idx].base;
  5699. if (put_user(val, addr, abi_ulong))
  5700. ret = -TARGET_EFAULT;
  5701. break;
  5702. default:
  5703. ret = -TARGET_EINVAL;
  5704. break;
  5705. }
  5706. return ret;
  5707. }
  5708. #endif /* defined(TARGET_ABI32 */
  5709. #endif /* defined(TARGET_I386) */
  5710. #define NEW_STACK_SIZE 0x40000
  5711. static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
  5712. typedef struct {
  5713. CPUArchState *env;
  5714. pthread_mutex_t mutex;
  5715. pthread_cond_t cond;
  5716. pthread_t thread;
  5717. uint32_t tid;
  5718. abi_ulong child_tidptr;
  5719. abi_ulong parent_tidptr;
  5720. sigset_t sigmask;
  5721. } new_thread_info;
  5722. static void *clone_func(void *arg)
  5723. {
  5724. new_thread_info *info = arg;
  5725. CPUArchState *env;
  5726. CPUState *cpu;
  5727. TaskState *ts;
  5728. rcu_register_thread();
  5729. tcg_register_thread();
  5730. env = info->env;
  5731. cpu = env_cpu(env);
  5732. thread_cpu = cpu;
  5733. ts = (TaskState *)cpu->opaque;
  5734. info->tid = sys_gettid();
  5735. task_settid(ts);
  5736. if (info->child_tidptr)
  5737. put_user_u32(info->tid, info->child_tidptr);
  5738. if (info->parent_tidptr)
  5739. put_user_u32(info->tid, info->parent_tidptr);
  5740. qemu_guest_random_seed_thread_part2(cpu->random_seed);
  5741. /* Enable signals. */
  5742. sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
  5743. /* Signal to the parent that we're ready. */
  5744. pthread_mutex_lock(&info->mutex);
  5745. pthread_cond_broadcast(&info->cond);
  5746. pthread_mutex_unlock(&info->mutex);
  5747. /* Wait until the parent has finished initializing the tls state. */
  5748. pthread_mutex_lock(&clone_lock);
  5749. pthread_mutex_unlock(&clone_lock);
  5750. cpu_loop(env);
  5751. /* never exits */
  5752. return NULL;
  5753. }
  5754. /* do_fork() Must return host values and target errnos (unlike most
  5755. do_*() functions). */
  5756. static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
  5757. abi_ulong parent_tidptr, target_ulong newtls,
  5758. abi_ulong child_tidptr)
  5759. {
  5760. CPUState *cpu = env_cpu(env);
  5761. int ret;
  5762. TaskState *ts;
  5763. CPUState *new_cpu;
  5764. CPUArchState *new_env;
  5765. sigset_t sigmask;
  5766. flags &= ~CLONE_IGNORED_FLAGS;
  5767. /* Emulate vfork() with fork() */
  5768. if (flags & CLONE_VFORK)
  5769. flags &= ~(CLONE_VFORK | CLONE_VM);
  5770. if (flags & CLONE_VM) {
  5771. TaskState *parent_ts = (TaskState *)cpu->opaque;
  5772. new_thread_info info;
  5773. pthread_attr_t attr;
  5774. if (((flags & CLONE_THREAD_FLAGS) != CLONE_THREAD_FLAGS) ||
  5775. (flags & CLONE_INVALID_THREAD_FLAGS)) {
  5776. return -TARGET_EINVAL;
  5777. }
  5778. ts = g_new0(TaskState, 1);
  5779. init_task_state(ts);
  5780. /* Grab a mutex so that thread setup appears atomic. */
  5781. pthread_mutex_lock(&clone_lock);
  5782. /* we create a new CPU instance. */
  5783. new_env = cpu_copy(env);
  5784. /* Init regs that differ from the parent. */
  5785. cpu_clone_regs_child(new_env, newsp, flags);
  5786. cpu_clone_regs_parent(env, flags);
  5787. new_cpu = env_cpu(new_env);
  5788. new_cpu->opaque = ts;
  5789. ts->bprm = parent_ts->bprm;
  5790. ts->info = parent_ts->info;
  5791. ts->signal_mask = parent_ts->signal_mask;
  5792. if (flags & CLONE_CHILD_CLEARTID) {
  5793. ts->child_tidptr = child_tidptr;
  5794. }
  5795. if (flags & CLONE_SETTLS) {
  5796. cpu_set_tls (new_env, newtls);
  5797. }
  5798. memset(&info, 0, sizeof(info));
  5799. pthread_mutex_init(&info.mutex, NULL);
  5800. pthread_mutex_lock(&info.mutex);
  5801. pthread_cond_init(&info.cond, NULL);
  5802. info.env = new_env;
  5803. if (flags & CLONE_CHILD_SETTID) {
  5804. info.child_tidptr = child_tidptr;
  5805. }
  5806. if (flags & CLONE_PARENT_SETTID) {
  5807. info.parent_tidptr = parent_tidptr;
  5808. }
  5809. ret = pthread_attr_init(&attr);
  5810. ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
  5811. ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  5812. /* It is not safe to deliver signals until the child has finished
  5813. initializing, so temporarily block all signals. */
  5814. sigfillset(&sigmask);
  5815. sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
  5816. cpu->random_seed = qemu_guest_random_seed_thread_part1();
  5817. /* If this is our first additional thread, we need to ensure we
  5818. * generate code for parallel execution and flush old translations.
  5819. */
  5820. if (!parallel_cpus) {
  5821. parallel_cpus = true;
  5822. tb_flush(cpu);
  5823. }
  5824. ret = pthread_create(&info.thread, &attr, clone_func, &info);
  5825. /* TODO: Free new CPU state if thread creation failed. */
  5826. sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
  5827. pthread_attr_destroy(&attr);
  5828. if (ret == 0) {
  5829. /* Wait for the child to initialize. */
  5830. pthread_cond_wait(&info.cond, &info.mutex);
  5831. ret = info.tid;
  5832. } else {
  5833. ret = -1;
  5834. }
  5835. pthread_mutex_unlock(&info.mutex);
  5836. pthread_cond_destroy(&info.cond);
  5837. pthread_mutex_destroy(&info.mutex);
  5838. pthread_mutex_unlock(&clone_lock);
  5839. } else {
  5840. /* if no CLONE_VM, we consider it is a fork */
  5841. if (flags & CLONE_INVALID_FORK_FLAGS) {
  5842. return -TARGET_EINVAL;
  5843. }
  5844. /* We can't support custom termination signals */
  5845. if ((flags & CSIGNAL) != TARGET_SIGCHLD) {
  5846. return -TARGET_EINVAL;
  5847. }
  5848. if (block_signals()) {
  5849. return -TARGET_ERESTARTSYS;
  5850. }
  5851. fork_start();
  5852. ret = fork();
  5853. if (ret == 0) {
  5854. /* Child Process. */
  5855. cpu_clone_regs_child(env, newsp, flags);
  5856. fork_end(1);
  5857. /* There is a race condition here. The parent process could
  5858. theoretically read the TID in the child process before the child
  5859. tid is set. This would require using either ptrace
  5860. (not implemented) or having *_tidptr to point at a shared memory
  5861. mapping. We can't repeat the spinlock hack used above because
  5862. the child process gets its own copy of the lock. */
  5863. if (flags & CLONE_CHILD_SETTID)
  5864. put_user_u32(sys_gettid(), child_tidptr);
  5865. if (flags & CLONE_PARENT_SETTID)
  5866. put_user_u32(sys_gettid(), parent_tidptr);
  5867. ts = (TaskState *)cpu->opaque;
  5868. if (flags & CLONE_SETTLS)
  5869. cpu_set_tls (env, newtls);
  5870. if (flags & CLONE_CHILD_CLEARTID)
  5871. ts->child_tidptr = child_tidptr;
  5872. } else {
  5873. cpu_clone_regs_parent(env, flags);
  5874. fork_end(0);
  5875. }
  5876. }
  5877. return ret;
  5878. }
  5879. /* warning : doesn't handle linux specific flags... */
  5880. static int target_to_host_fcntl_cmd(int cmd)
  5881. {
  5882. int ret;
  5883. switch(cmd) {
  5884. case TARGET_F_DUPFD:
  5885. case TARGET_F_GETFD:
  5886. case TARGET_F_SETFD:
  5887. case TARGET_F_GETFL:
  5888. case TARGET_F_SETFL:
  5889. case TARGET_F_OFD_GETLK:
  5890. case TARGET_F_OFD_SETLK:
  5891. case TARGET_F_OFD_SETLKW:
  5892. ret = cmd;
  5893. break;
  5894. case TARGET_F_GETLK:
  5895. ret = F_GETLK64;
  5896. break;
  5897. case TARGET_F_SETLK:
  5898. ret = F_SETLK64;
  5899. break;
  5900. case TARGET_F_SETLKW:
  5901. ret = F_SETLKW64;
  5902. break;
  5903. case TARGET_F_GETOWN:
  5904. ret = F_GETOWN;
  5905. break;
  5906. case TARGET_F_SETOWN:
  5907. ret = F_SETOWN;
  5908. break;
  5909. case TARGET_F_GETSIG:
  5910. ret = F_GETSIG;
  5911. break;
  5912. case TARGET_F_SETSIG:
  5913. ret = F_SETSIG;
  5914. break;
  5915. #if TARGET_ABI_BITS == 32
  5916. case TARGET_F_GETLK64:
  5917. ret = F_GETLK64;
  5918. break;
  5919. case TARGET_F_SETLK64:
  5920. ret = F_SETLK64;
  5921. break;
  5922. case TARGET_F_SETLKW64:
  5923. ret = F_SETLKW64;
  5924. break;
  5925. #endif
  5926. case TARGET_F_SETLEASE:
  5927. ret = F_SETLEASE;
  5928. break;
  5929. case TARGET_F_GETLEASE:
  5930. ret = F_GETLEASE;
  5931. break;
  5932. #ifdef F_DUPFD_CLOEXEC
  5933. case TARGET_F_DUPFD_CLOEXEC:
  5934. ret = F_DUPFD_CLOEXEC;
  5935. break;
  5936. #endif
  5937. case TARGET_F_NOTIFY:
  5938. ret = F_NOTIFY;
  5939. break;
  5940. #ifdef F_GETOWN_EX
  5941. case TARGET_F_GETOWN_EX:
  5942. ret = F_GETOWN_EX;
  5943. break;
  5944. #endif
  5945. #ifdef F_SETOWN_EX
  5946. case TARGET_F_SETOWN_EX:
  5947. ret = F_SETOWN_EX;
  5948. break;
  5949. #endif
  5950. #ifdef F_SETPIPE_SZ
  5951. case TARGET_F_SETPIPE_SZ:
  5952. ret = F_SETPIPE_SZ;
  5953. break;
  5954. case TARGET_F_GETPIPE_SZ:
  5955. ret = F_GETPIPE_SZ;
  5956. break;
  5957. #endif
  5958. default:
  5959. ret = -TARGET_EINVAL;
  5960. break;
  5961. }
  5962. #if defined(__powerpc64__)
  5963. /* On PPC64, glibc headers has the F_*LK* defined to 12, 13 and 14 and
  5964. * is not supported by kernel. The glibc fcntl call actually adjusts
  5965. * them to 5, 6 and 7 before making the syscall(). Since we make the
  5966. * syscall directly, adjust to what is supported by the kernel.
  5967. */
  5968. if (ret >= F_GETLK64 && ret <= F_SETLKW64) {
  5969. ret -= F_GETLK64 - 5;
  5970. }
  5971. #endif
  5972. return ret;
  5973. }
  5974. #define FLOCK_TRANSTBL \
  5975. switch (type) { \
  5976. TRANSTBL_CONVERT(F_RDLCK); \
  5977. TRANSTBL_CONVERT(F_WRLCK); \
  5978. TRANSTBL_CONVERT(F_UNLCK); \
  5979. TRANSTBL_CONVERT(F_EXLCK); \
  5980. TRANSTBL_CONVERT(F_SHLCK); \
  5981. }
  5982. static int target_to_host_flock(int type)
  5983. {
  5984. #define TRANSTBL_CONVERT(a) case TARGET_##a: return a
  5985. FLOCK_TRANSTBL
  5986. #undef TRANSTBL_CONVERT
  5987. return -TARGET_EINVAL;
  5988. }
  5989. static int host_to_target_flock(int type)
  5990. {
  5991. #define TRANSTBL_CONVERT(a) case a: return TARGET_##a
  5992. FLOCK_TRANSTBL
  5993. #undef TRANSTBL_CONVERT
  5994. /* if we don't know how to convert the value coming
  5995. * from the host we copy to the target field as-is
  5996. */
  5997. return type;
  5998. }
  5999. static inline abi_long copy_from_user_flock(struct flock64 *fl,
  6000. abi_ulong target_flock_addr)
  6001. {
  6002. struct target_flock *target_fl;
  6003. int l_type;
  6004. if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
  6005. return -TARGET_EFAULT;
  6006. }
  6007. __get_user(l_type, &target_fl->l_type);
  6008. l_type = target_to_host_flock(l_type);
  6009. if (l_type < 0) {
  6010. return l_type;
  6011. }
  6012. fl->l_type = l_type;
  6013. __get_user(fl->l_whence, &target_fl->l_whence);
  6014. __get_user(fl->l_start, &target_fl->l_start);
  6015. __get_user(fl->l_len, &target_fl->l_len);
  6016. __get_user(fl->l_pid, &target_fl->l_pid);
  6017. unlock_user_struct(target_fl, target_flock_addr, 0);
  6018. return 0;
  6019. }
  6020. static inline abi_long copy_to_user_flock(abi_ulong target_flock_addr,
  6021. const struct flock64 *fl)
  6022. {
  6023. struct target_flock *target_fl;
  6024. short l_type;
  6025. if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
  6026. return -TARGET_EFAULT;
  6027. }
  6028. l_type = host_to_target_flock(fl->l_type);
  6029. __put_user(l_type, &target_fl->l_type);
  6030. __put_user(fl->l_whence, &target_fl->l_whence);
  6031. __put_user(fl->l_start, &target_fl->l_start);
  6032. __put_user(fl->l_len, &target_fl->l_len);
  6033. __put_user(fl->l_pid, &target_fl->l_pid);
  6034. unlock_user_struct(target_fl, target_flock_addr, 1);
  6035. return 0;
  6036. }
  6037. typedef abi_long from_flock64_fn(struct flock64 *fl, abi_ulong target_addr);
  6038. typedef abi_long to_flock64_fn(abi_ulong target_addr, const struct flock64 *fl);
  6039. #if defined(TARGET_ARM) && TARGET_ABI_BITS == 32
  6040. static inline abi_long copy_from_user_oabi_flock64(struct flock64 *fl,
  6041. abi_ulong target_flock_addr)
  6042. {
  6043. struct target_oabi_flock64 *target_fl;
  6044. int l_type;
  6045. if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
  6046. return -TARGET_EFAULT;
  6047. }
  6048. __get_user(l_type, &target_fl->l_type);
  6049. l_type = target_to_host_flock(l_type);
  6050. if (l_type < 0) {
  6051. return l_type;
  6052. }
  6053. fl->l_type = l_type;
  6054. __get_user(fl->l_whence, &target_fl->l_whence);
  6055. __get_user(fl->l_start, &target_fl->l_start);
  6056. __get_user(fl->l_len, &target_fl->l_len);
  6057. __get_user(fl->l_pid, &target_fl->l_pid);
  6058. unlock_user_struct(target_fl, target_flock_addr, 0);
  6059. return 0;
  6060. }
  6061. static inline abi_long copy_to_user_oabi_flock64(abi_ulong target_flock_addr,
  6062. const struct flock64 *fl)
  6063. {
  6064. struct target_oabi_flock64 *target_fl;
  6065. short l_type;
  6066. if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
  6067. return -TARGET_EFAULT;
  6068. }
  6069. l_type = host_to_target_flock(fl->l_type);
  6070. __put_user(l_type, &target_fl->l_type);
  6071. __put_user(fl->l_whence, &target_fl->l_whence);
  6072. __put_user(fl->l_start, &target_fl->l_start);
  6073. __put_user(fl->l_len, &target_fl->l_len);
  6074. __put_user(fl->l_pid, &target_fl->l_pid);
  6075. unlock_user_struct(target_fl, target_flock_addr, 1);
  6076. return 0;
  6077. }
  6078. #endif
  6079. static inline abi_long copy_from_user_flock64(struct flock64 *fl,
  6080. abi_ulong target_flock_addr)
  6081. {
  6082. struct target_flock64 *target_fl;
  6083. int l_type;
  6084. if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
  6085. return -TARGET_EFAULT;
  6086. }
  6087. __get_user(l_type, &target_fl->l_type);
  6088. l_type = target_to_host_flock(l_type);
  6089. if (l_type < 0) {
  6090. return l_type;
  6091. }
  6092. fl->l_type = l_type;
  6093. __get_user(fl->l_whence, &target_fl->l_whence);
  6094. __get_user(fl->l_start, &target_fl->l_start);
  6095. __get_user(fl->l_len, &target_fl->l_len);
  6096. __get_user(fl->l_pid, &target_fl->l_pid);
  6097. unlock_user_struct(target_fl, target_flock_addr, 0);
  6098. return 0;
  6099. }
  6100. static inline abi_long copy_to_user_flock64(abi_ulong target_flock_addr,
  6101. const struct flock64 *fl)
  6102. {
  6103. struct target_flock64 *target_fl;
  6104. short l_type;
  6105. if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
  6106. return -TARGET_EFAULT;
  6107. }
  6108. l_type = host_to_target_flock(fl->l_type);
  6109. __put_user(l_type, &target_fl->l_type);
  6110. __put_user(fl->l_whence, &target_fl->l_whence);
  6111. __put_user(fl->l_start, &target_fl->l_start);
  6112. __put_user(fl->l_len, &target_fl->l_len);
  6113. __put_user(fl->l_pid, &target_fl->l_pid);
  6114. unlock_user_struct(target_fl, target_flock_addr, 1);
  6115. return 0;
  6116. }
  6117. static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
  6118. {
  6119. struct flock64 fl64;
  6120. #ifdef F_GETOWN_EX
  6121. struct f_owner_ex fox;
  6122. struct target_f_owner_ex *target_fox;
  6123. #endif
  6124. abi_long ret;
  6125. int host_cmd = target_to_host_fcntl_cmd(cmd);
  6126. if (host_cmd == -TARGET_EINVAL)
  6127. return host_cmd;
  6128. switch(cmd) {
  6129. case TARGET_F_GETLK:
  6130. ret = copy_from_user_flock(&fl64, arg);
  6131. if (ret) {
  6132. return ret;
  6133. }
  6134. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  6135. if (ret == 0) {
  6136. ret = copy_to_user_flock(arg, &fl64);
  6137. }
  6138. break;
  6139. case TARGET_F_SETLK:
  6140. case TARGET_F_SETLKW:
  6141. ret = copy_from_user_flock(&fl64, arg);
  6142. if (ret) {
  6143. return ret;
  6144. }
  6145. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  6146. break;
  6147. case TARGET_F_GETLK64:
  6148. case TARGET_F_OFD_GETLK:
  6149. ret = copy_from_user_flock64(&fl64, arg);
  6150. if (ret) {
  6151. return ret;
  6152. }
  6153. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  6154. if (ret == 0) {
  6155. ret = copy_to_user_flock64(arg, &fl64);
  6156. }
  6157. break;
  6158. case TARGET_F_SETLK64:
  6159. case TARGET_F_SETLKW64:
  6160. case TARGET_F_OFD_SETLK:
  6161. case TARGET_F_OFD_SETLKW:
  6162. ret = copy_from_user_flock64(&fl64, arg);
  6163. if (ret) {
  6164. return ret;
  6165. }
  6166. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  6167. break;
  6168. case TARGET_F_GETFL:
  6169. ret = get_errno(safe_fcntl(fd, host_cmd, arg));
  6170. if (ret >= 0) {
  6171. ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
  6172. }
  6173. break;
  6174. case TARGET_F_SETFL:
  6175. ret = get_errno(safe_fcntl(fd, host_cmd,
  6176. target_to_host_bitmask(arg,
  6177. fcntl_flags_tbl)));
  6178. break;
  6179. #ifdef F_GETOWN_EX
  6180. case TARGET_F_GETOWN_EX:
  6181. ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
  6182. if (ret >= 0) {
  6183. if (!lock_user_struct(VERIFY_WRITE, target_fox, arg, 0))
  6184. return -TARGET_EFAULT;
  6185. target_fox->type = tswap32(fox.type);
  6186. target_fox->pid = tswap32(fox.pid);
  6187. unlock_user_struct(target_fox, arg, 1);
  6188. }
  6189. break;
  6190. #endif
  6191. #ifdef F_SETOWN_EX
  6192. case TARGET_F_SETOWN_EX:
  6193. if (!lock_user_struct(VERIFY_READ, target_fox, arg, 1))
  6194. return -TARGET_EFAULT;
  6195. fox.type = tswap32(target_fox->type);
  6196. fox.pid = tswap32(target_fox->pid);
  6197. unlock_user_struct(target_fox, arg, 0);
  6198. ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
  6199. break;
  6200. #endif
  6201. case TARGET_F_SETSIG:
  6202. ret = get_errno(safe_fcntl(fd, host_cmd, target_to_host_signal(arg)));
  6203. break;
  6204. case TARGET_F_GETSIG:
  6205. ret = host_to_target_signal(get_errno(safe_fcntl(fd, host_cmd, arg)));
  6206. break;
  6207. case TARGET_F_SETOWN:
  6208. case TARGET_F_GETOWN:
  6209. case TARGET_F_SETLEASE:
  6210. case TARGET_F_GETLEASE:
  6211. case TARGET_F_SETPIPE_SZ:
  6212. case TARGET_F_GETPIPE_SZ:
  6213. ret = get_errno(safe_fcntl(fd, host_cmd, arg));
  6214. break;
  6215. default:
  6216. ret = get_errno(safe_fcntl(fd, cmd, arg));
  6217. break;
  6218. }
  6219. return ret;
  6220. }
  6221. #ifdef USE_UID16
  6222. static inline int high2lowuid(int uid)
  6223. {
  6224. if (uid > 65535)
  6225. return 65534;
  6226. else
  6227. return uid;
  6228. }
  6229. static inline int high2lowgid(int gid)
  6230. {
  6231. if (gid > 65535)
  6232. return 65534;
  6233. else
  6234. return gid;
  6235. }
  6236. static inline int low2highuid(int uid)
  6237. {
  6238. if ((int16_t)uid == -1)
  6239. return -1;
  6240. else
  6241. return uid;
  6242. }
  6243. static inline int low2highgid(int gid)
  6244. {
  6245. if ((int16_t)gid == -1)
  6246. return -1;
  6247. else
  6248. return gid;
  6249. }
  6250. static inline int tswapid(int id)
  6251. {
  6252. return tswap16(id);
  6253. }
  6254. #define put_user_id(x, gaddr) put_user_u16(x, gaddr)
  6255. #else /* !USE_UID16 */
  6256. static inline int high2lowuid(int uid)
  6257. {
  6258. return uid;
  6259. }
  6260. static inline int high2lowgid(int gid)
  6261. {
  6262. return gid;
  6263. }
  6264. static inline int low2highuid(int uid)
  6265. {
  6266. return uid;
  6267. }
  6268. static inline int low2highgid(int gid)
  6269. {
  6270. return gid;
  6271. }
  6272. static inline int tswapid(int id)
  6273. {
  6274. return tswap32(id);
  6275. }
  6276. #define put_user_id(x, gaddr) put_user_u32(x, gaddr)
  6277. #endif /* USE_UID16 */
  6278. /* We must do direct syscalls for setting UID/GID, because we want to
  6279. * implement the Linux system call semantics of "change only for this thread",
  6280. * not the libc/POSIX semantics of "change for all threads in process".
  6281. * (See http://ewontfix.com/17/ for more details.)
  6282. * We use the 32-bit version of the syscalls if present; if it is not
  6283. * then either the host architecture supports 32-bit UIDs natively with
  6284. * the standard syscall, or the 16-bit UID is the best we can do.
  6285. */
  6286. #ifdef __NR_setuid32
  6287. #define __NR_sys_setuid __NR_setuid32
  6288. #else
  6289. #define __NR_sys_setuid __NR_setuid
  6290. #endif
  6291. #ifdef __NR_setgid32
  6292. #define __NR_sys_setgid __NR_setgid32
  6293. #else
  6294. #define __NR_sys_setgid __NR_setgid
  6295. #endif
  6296. #ifdef __NR_setresuid32
  6297. #define __NR_sys_setresuid __NR_setresuid32
  6298. #else
  6299. #define __NR_sys_setresuid __NR_setresuid
  6300. #endif
  6301. #ifdef __NR_setresgid32
  6302. #define __NR_sys_setresgid __NR_setresgid32
  6303. #else
  6304. #define __NR_sys_setresgid __NR_setresgid
  6305. #endif
  6306. _syscall1(int, sys_setuid, uid_t, uid)
  6307. _syscall1(int, sys_setgid, gid_t, gid)
  6308. _syscall3(int, sys_setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
  6309. _syscall3(int, sys_setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
  6310. void syscall_init(void)
  6311. {
  6312. IOCTLEntry *ie;
  6313. const argtype *arg_type;
  6314. int size;
  6315. int i;
  6316. thunk_init(STRUCT_MAX);
  6317. #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
  6318. #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
  6319. #include "syscall_types.h"
  6320. #undef STRUCT
  6321. #undef STRUCT_SPECIAL
  6322. /* Build target_to_host_errno_table[] table from
  6323. * host_to_target_errno_table[]. */
  6324. for (i = 0; i < ERRNO_TABLE_SIZE; i++) {
  6325. target_to_host_errno_table[host_to_target_errno_table[i]] = i;
  6326. }
  6327. /* we patch the ioctl size if necessary. We rely on the fact that
  6328. no ioctl has all the bits at '1' in the size field */
  6329. ie = ioctl_entries;
  6330. while (ie->target_cmd != 0) {
  6331. if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
  6332. TARGET_IOC_SIZEMASK) {
  6333. arg_type = ie->arg_type;
  6334. if (arg_type[0] != TYPE_PTR) {
  6335. fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
  6336. ie->target_cmd);
  6337. exit(1);
  6338. }
  6339. arg_type++;
  6340. size = thunk_type_size(arg_type, 0);
  6341. ie->target_cmd = (ie->target_cmd &
  6342. ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
  6343. (size << TARGET_IOC_SIZESHIFT);
  6344. }
  6345. /* automatic consistency check if same arch */
  6346. #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
  6347. (defined(__x86_64__) && defined(TARGET_X86_64))
  6348. if (unlikely(ie->target_cmd != ie->host_cmd)) {
  6349. fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
  6350. ie->name, ie->target_cmd, ie->host_cmd);
  6351. }
  6352. #endif
  6353. ie++;
  6354. }
  6355. }
  6356. #ifdef TARGET_NR_truncate64
  6357. static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
  6358. abi_long arg2,
  6359. abi_long arg3,
  6360. abi_long arg4)
  6361. {
  6362. if (regpairs_aligned(cpu_env, TARGET_NR_truncate64)) {
  6363. arg2 = arg3;
  6364. arg3 = arg4;
  6365. }
  6366. return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
  6367. }
  6368. #endif
  6369. #ifdef TARGET_NR_ftruncate64
  6370. static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
  6371. abi_long arg2,
  6372. abi_long arg3,
  6373. abi_long arg4)
  6374. {
  6375. if (regpairs_aligned(cpu_env, TARGET_NR_ftruncate64)) {
  6376. arg2 = arg3;
  6377. arg3 = arg4;
  6378. }
  6379. return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
  6380. }
  6381. #endif
  6382. #if defined(TARGET_NR_timer_settime) || \
  6383. (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
  6384. static inline abi_long target_to_host_itimerspec(struct itimerspec *host_its,
  6385. abi_ulong target_addr)
  6386. {
  6387. if (target_to_host_timespec(&host_its->it_interval, target_addr +
  6388. offsetof(struct target_itimerspec,
  6389. it_interval)) ||
  6390. target_to_host_timespec(&host_its->it_value, target_addr +
  6391. offsetof(struct target_itimerspec,
  6392. it_value))) {
  6393. return -TARGET_EFAULT;
  6394. }
  6395. return 0;
  6396. }
  6397. #endif
  6398. #if defined(TARGET_NR_timer_settime64) || \
  6399. (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD))
  6400. static inline abi_long target_to_host_itimerspec64(struct itimerspec *host_its,
  6401. abi_ulong target_addr)
  6402. {
  6403. if (target_to_host_timespec64(&host_its->it_interval, target_addr +
  6404. offsetof(struct target__kernel_itimerspec,
  6405. it_interval)) ||
  6406. target_to_host_timespec64(&host_its->it_value, target_addr +
  6407. offsetof(struct target__kernel_itimerspec,
  6408. it_value))) {
  6409. return -TARGET_EFAULT;
  6410. }
  6411. return 0;
  6412. }
  6413. #endif
  6414. #if ((defined(TARGET_NR_timerfd_gettime) || \
  6415. defined(TARGET_NR_timerfd_settime)) && defined(CONFIG_TIMERFD)) || \
  6416. defined(TARGET_NR_timer_gettime) || defined(TARGET_NR_timer_settime)
  6417. static inline abi_long host_to_target_itimerspec(abi_ulong target_addr,
  6418. struct itimerspec *host_its)
  6419. {
  6420. if (host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
  6421. it_interval),
  6422. &host_its->it_interval) ||
  6423. host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
  6424. it_value),
  6425. &host_its->it_value)) {
  6426. return -TARGET_EFAULT;
  6427. }
  6428. return 0;
  6429. }
  6430. #endif
  6431. #if ((defined(TARGET_NR_timerfd_gettime64) || \
  6432. defined(TARGET_NR_timerfd_settime64)) && defined(CONFIG_TIMERFD)) || \
  6433. defined(TARGET_NR_timer_gettime64) || defined(TARGET_NR_timer_settime64)
  6434. static inline abi_long host_to_target_itimerspec64(abi_ulong target_addr,
  6435. struct itimerspec *host_its)
  6436. {
  6437. if (host_to_target_timespec64(target_addr +
  6438. offsetof(struct target__kernel_itimerspec,
  6439. it_interval),
  6440. &host_its->it_interval) ||
  6441. host_to_target_timespec64(target_addr +
  6442. offsetof(struct target__kernel_itimerspec,
  6443. it_value),
  6444. &host_its->it_value)) {
  6445. return -TARGET_EFAULT;
  6446. }
  6447. return 0;
  6448. }
  6449. #endif
  6450. #if defined(TARGET_NR_adjtimex) || \
  6451. (defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME))
  6452. static inline abi_long target_to_host_timex(struct timex *host_tx,
  6453. abi_long target_addr)
  6454. {
  6455. struct target_timex *target_tx;
  6456. if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) {
  6457. return -TARGET_EFAULT;
  6458. }
  6459. __get_user(host_tx->modes, &target_tx->modes);
  6460. __get_user(host_tx->offset, &target_tx->offset);
  6461. __get_user(host_tx->freq, &target_tx->freq);
  6462. __get_user(host_tx->maxerror, &target_tx->maxerror);
  6463. __get_user(host_tx->esterror, &target_tx->esterror);
  6464. __get_user(host_tx->status, &target_tx->status);
  6465. __get_user(host_tx->constant, &target_tx->constant);
  6466. __get_user(host_tx->precision, &target_tx->precision);
  6467. __get_user(host_tx->tolerance, &target_tx->tolerance);
  6468. __get_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
  6469. __get_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
  6470. __get_user(host_tx->tick, &target_tx->tick);
  6471. __get_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  6472. __get_user(host_tx->jitter, &target_tx->jitter);
  6473. __get_user(host_tx->shift, &target_tx->shift);
  6474. __get_user(host_tx->stabil, &target_tx->stabil);
  6475. __get_user(host_tx->jitcnt, &target_tx->jitcnt);
  6476. __get_user(host_tx->calcnt, &target_tx->calcnt);
  6477. __get_user(host_tx->errcnt, &target_tx->errcnt);
  6478. __get_user(host_tx->stbcnt, &target_tx->stbcnt);
  6479. __get_user(host_tx->tai, &target_tx->tai);
  6480. unlock_user_struct(target_tx, target_addr, 0);
  6481. return 0;
  6482. }
  6483. static inline abi_long host_to_target_timex(abi_long target_addr,
  6484. struct timex *host_tx)
  6485. {
  6486. struct target_timex *target_tx;
  6487. if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) {
  6488. return -TARGET_EFAULT;
  6489. }
  6490. __put_user(host_tx->modes, &target_tx->modes);
  6491. __put_user(host_tx->offset, &target_tx->offset);
  6492. __put_user(host_tx->freq, &target_tx->freq);
  6493. __put_user(host_tx->maxerror, &target_tx->maxerror);
  6494. __put_user(host_tx->esterror, &target_tx->esterror);
  6495. __put_user(host_tx->status, &target_tx->status);
  6496. __put_user(host_tx->constant, &target_tx->constant);
  6497. __put_user(host_tx->precision, &target_tx->precision);
  6498. __put_user(host_tx->tolerance, &target_tx->tolerance);
  6499. __put_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
  6500. __put_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
  6501. __put_user(host_tx->tick, &target_tx->tick);
  6502. __put_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  6503. __put_user(host_tx->jitter, &target_tx->jitter);
  6504. __put_user(host_tx->shift, &target_tx->shift);
  6505. __put_user(host_tx->stabil, &target_tx->stabil);
  6506. __put_user(host_tx->jitcnt, &target_tx->jitcnt);
  6507. __put_user(host_tx->calcnt, &target_tx->calcnt);
  6508. __put_user(host_tx->errcnt, &target_tx->errcnt);
  6509. __put_user(host_tx->stbcnt, &target_tx->stbcnt);
  6510. __put_user(host_tx->tai, &target_tx->tai);
  6511. unlock_user_struct(target_tx, target_addr, 1);
  6512. return 0;
  6513. }
  6514. #endif
  6515. #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
  6516. static inline abi_long target_to_host_timex64(struct timex *host_tx,
  6517. abi_long target_addr)
  6518. {
  6519. struct target__kernel_timex *target_tx;
  6520. if (copy_from_user_timeval64(&host_tx->time, target_addr +
  6521. offsetof(struct target__kernel_timex,
  6522. time))) {
  6523. return -TARGET_EFAULT;
  6524. }
  6525. if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) {
  6526. return -TARGET_EFAULT;
  6527. }
  6528. __get_user(host_tx->modes, &target_tx->modes);
  6529. __get_user(host_tx->offset, &target_tx->offset);
  6530. __get_user(host_tx->freq, &target_tx->freq);
  6531. __get_user(host_tx->maxerror, &target_tx->maxerror);
  6532. __get_user(host_tx->esterror, &target_tx->esterror);
  6533. __get_user(host_tx->status, &target_tx->status);
  6534. __get_user(host_tx->constant, &target_tx->constant);
  6535. __get_user(host_tx->precision, &target_tx->precision);
  6536. __get_user(host_tx->tolerance, &target_tx->tolerance);
  6537. __get_user(host_tx->tick, &target_tx->tick);
  6538. __get_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  6539. __get_user(host_tx->jitter, &target_tx->jitter);
  6540. __get_user(host_tx->shift, &target_tx->shift);
  6541. __get_user(host_tx->stabil, &target_tx->stabil);
  6542. __get_user(host_tx->jitcnt, &target_tx->jitcnt);
  6543. __get_user(host_tx->calcnt, &target_tx->calcnt);
  6544. __get_user(host_tx->errcnt, &target_tx->errcnt);
  6545. __get_user(host_tx->stbcnt, &target_tx->stbcnt);
  6546. __get_user(host_tx->tai, &target_tx->tai);
  6547. unlock_user_struct(target_tx, target_addr, 0);
  6548. return 0;
  6549. }
  6550. static inline abi_long host_to_target_timex64(abi_long target_addr,
  6551. struct timex *host_tx)
  6552. {
  6553. struct target__kernel_timex *target_tx;
  6554. if (copy_to_user_timeval64(target_addr +
  6555. offsetof(struct target__kernel_timex, time),
  6556. &host_tx->time)) {
  6557. return -TARGET_EFAULT;
  6558. }
  6559. if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) {
  6560. return -TARGET_EFAULT;
  6561. }
  6562. __put_user(host_tx->modes, &target_tx->modes);
  6563. __put_user(host_tx->offset, &target_tx->offset);
  6564. __put_user(host_tx->freq, &target_tx->freq);
  6565. __put_user(host_tx->maxerror, &target_tx->maxerror);
  6566. __put_user(host_tx->esterror, &target_tx->esterror);
  6567. __put_user(host_tx->status, &target_tx->status);
  6568. __put_user(host_tx->constant, &target_tx->constant);
  6569. __put_user(host_tx->precision, &target_tx->precision);
  6570. __put_user(host_tx->tolerance, &target_tx->tolerance);
  6571. __put_user(host_tx->tick, &target_tx->tick);
  6572. __put_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  6573. __put_user(host_tx->jitter, &target_tx->jitter);
  6574. __put_user(host_tx->shift, &target_tx->shift);
  6575. __put_user(host_tx->stabil, &target_tx->stabil);
  6576. __put_user(host_tx->jitcnt, &target_tx->jitcnt);
  6577. __put_user(host_tx->calcnt, &target_tx->calcnt);
  6578. __put_user(host_tx->errcnt, &target_tx->errcnt);
  6579. __put_user(host_tx->stbcnt, &target_tx->stbcnt);
  6580. __put_user(host_tx->tai, &target_tx->tai);
  6581. unlock_user_struct(target_tx, target_addr, 1);
  6582. return 0;
  6583. }
  6584. #endif
  6585. static inline abi_long target_to_host_sigevent(struct sigevent *host_sevp,
  6586. abi_ulong target_addr)
  6587. {
  6588. struct target_sigevent *target_sevp;
  6589. if (!lock_user_struct(VERIFY_READ, target_sevp, target_addr, 1)) {
  6590. return -TARGET_EFAULT;
  6591. }
  6592. /* This union is awkward on 64 bit systems because it has a 32 bit
  6593. * integer and a pointer in it; we follow the conversion approach
  6594. * used for handling sigval types in signal.c so the guest should get
  6595. * the correct value back even if we did a 64 bit byteswap and it's
  6596. * using the 32 bit integer.
  6597. */
  6598. host_sevp->sigev_value.sival_ptr =
  6599. (void *)(uintptr_t)tswapal(target_sevp->sigev_value.sival_ptr);
  6600. host_sevp->sigev_signo =
  6601. target_to_host_signal(tswap32(target_sevp->sigev_signo));
  6602. host_sevp->sigev_notify = tswap32(target_sevp->sigev_notify);
  6603. host_sevp->_sigev_un._tid = tswap32(target_sevp->_sigev_un._tid);
  6604. unlock_user_struct(target_sevp, target_addr, 1);
  6605. return 0;
  6606. }
  6607. #if defined(TARGET_NR_mlockall)
  6608. static inline int target_to_host_mlockall_arg(int arg)
  6609. {
  6610. int result = 0;
  6611. if (arg & TARGET_MCL_CURRENT) {
  6612. result |= MCL_CURRENT;
  6613. }
  6614. if (arg & TARGET_MCL_FUTURE) {
  6615. result |= MCL_FUTURE;
  6616. }
  6617. #ifdef MCL_ONFAULT
  6618. if (arg & TARGET_MCL_ONFAULT) {
  6619. result |= MCL_ONFAULT;
  6620. }
  6621. #endif
  6622. return result;
  6623. }
  6624. #endif
  6625. #if (defined(TARGET_NR_stat64) || defined(TARGET_NR_lstat64) || \
  6626. defined(TARGET_NR_fstat64) || defined(TARGET_NR_fstatat64) || \
  6627. defined(TARGET_NR_newfstatat))
  6628. static inline abi_long host_to_target_stat64(void *cpu_env,
  6629. abi_ulong target_addr,
  6630. struct stat *host_st)
  6631. {
  6632. #if defined(TARGET_ARM) && defined(TARGET_ABI32)
  6633. if (((CPUARMState *)cpu_env)->eabi) {
  6634. struct target_eabi_stat64 *target_st;
  6635. if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
  6636. return -TARGET_EFAULT;
  6637. memset(target_st, 0, sizeof(struct target_eabi_stat64));
  6638. __put_user(host_st->st_dev, &target_st->st_dev);
  6639. __put_user(host_st->st_ino, &target_st->st_ino);
  6640. #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
  6641. __put_user(host_st->st_ino, &target_st->__st_ino);
  6642. #endif
  6643. __put_user(host_st->st_mode, &target_st->st_mode);
  6644. __put_user(host_st->st_nlink, &target_st->st_nlink);
  6645. __put_user(host_st->st_uid, &target_st->st_uid);
  6646. __put_user(host_st->st_gid, &target_st->st_gid);
  6647. __put_user(host_st->st_rdev, &target_st->st_rdev);
  6648. __put_user(host_st->st_size, &target_st->st_size);
  6649. __put_user(host_st->st_blksize, &target_st->st_blksize);
  6650. __put_user(host_st->st_blocks, &target_st->st_blocks);
  6651. __put_user(host_st->st_atime, &target_st->target_st_atime);
  6652. __put_user(host_st->st_mtime, &target_st->target_st_mtime);
  6653. __put_user(host_st->st_ctime, &target_st->target_st_ctime);
  6654. #if _POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700
  6655. __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
  6656. __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
  6657. __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
  6658. #endif
  6659. unlock_user_struct(target_st, target_addr, 1);
  6660. } else
  6661. #endif
  6662. {
  6663. #if defined(TARGET_HAS_STRUCT_STAT64)
  6664. struct target_stat64 *target_st;
  6665. #else
  6666. struct target_stat *target_st;
  6667. #endif
  6668. if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
  6669. return -TARGET_EFAULT;
  6670. memset(target_st, 0, sizeof(*target_st));
  6671. __put_user(host_st->st_dev, &target_st->st_dev);
  6672. __put_user(host_st->st_ino, &target_st->st_ino);
  6673. #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
  6674. __put_user(host_st->st_ino, &target_st->__st_ino);
  6675. #endif
  6676. __put_user(host_st->st_mode, &target_st->st_mode);
  6677. __put_user(host_st->st_nlink, &target_st->st_nlink);
  6678. __put_user(host_st->st_uid, &target_st->st_uid);
  6679. __put_user(host_st->st_gid, &target_st->st_gid);
  6680. __put_user(host_st->st_rdev, &target_st->st_rdev);
  6681. /* XXX: better use of kernel struct */
  6682. __put_user(host_st->st_size, &target_st->st_size);
  6683. __put_user(host_st->st_blksize, &target_st->st_blksize);
  6684. __put_user(host_st->st_blocks, &target_st->st_blocks);
  6685. __put_user(host_st->st_atime, &target_st->target_st_atime);
  6686. __put_user(host_st->st_mtime, &target_st->target_st_mtime);
  6687. __put_user(host_st->st_ctime, &target_st->target_st_ctime);
  6688. #if _POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700
  6689. __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
  6690. __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
  6691. __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
  6692. #endif
  6693. unlock_user_struct(target_st, target_addr, 1);
  6694. }
  6695. return 0;
  6696. }
  6697. #endif
  6698. #if defined(TARGET_NR_statx) && defined(__NR_statx)
  6699. static inline abi_long host_to_target_statx(struct target_statx *host_stx,
  6700. abi_ulong target_addr)
  6701. {
  6702. struct target_statx *target_stx;
  6703. if (!lock_user_struct(VERIFY_WRITE, target_stx, target_addr, 0)) {
  6704. return -TARGET_EFAULT;
  6705. }
  6706. memset(target_stx, 0, sizeof(*target_stx));
  6707. __put_user(host_stx->stx_mask, &target_stx->stx_mask);
  6708. __put_user(host_stx->stx_blksize, &target_stx->stx_blksize);
  6709. __put_user(host_stx->stx_attributes, &target_stx->stx_attributes);
  6710. __put_user(host_stx->stx_nlink, &target_stx->stx_nlink);
  6711. __put_user(host_stx->stx_uid, &target_stx->stx_uid);
  6712. __put_user(host_stx->stx_gid, &target_stx->stx_gid);
  6713. __put_user(host_stx->stx_mode, &target_stx->stx_mode);
  6714. __put_user(host_stx->stx_ino, &target_stx->stx_ino);
  6715. __put_user(host_stx->stx_size, &target_stx->stx_size);
  6716. __put_user(host_stx->stx_blocks, &target_stx->stx_blocks);
  6717. __put_user(host_stx->stx_attributes_mask, &target_stx->stx_attributes_mask);
  6718. __put_user(host_stx->stx_atime.tv_sec, &target_stx->stx_atime.tv_sec);
  6719. __put_user(host_stx->stx_atime.tv_nsec, &target_stx->stx_atime.tv_nsec);
  6720. __put_user(host_stx->stx_btime.tv_sec, &target_stx->stx_btime.tv_sec);
  6721. __put_user(host_stx->stx_btime.tv_nsec, &target_stx->stx_btime.tv_nsec);
  6722. __put_user(host_stx->stx_ctime.tv_sec, &target_stx->stx_ctime.tv_sec);
  6723. __put_user(host_stx->stx_ctime.tv_nsec, &target_stx->stx_ctime.tv_nsec);
  6724. __put_user(host_stx->stx_mtime.tv_sec, &target_stx->stx_mtime.tv_sec);
  6725. __put_user(host_stx->stx_mtime.tv_nsec, &target_stx->stx_mtime.tv_nsec);
  6726. __put_user(host_stx->stx_rdev_major, &target_stx->stx_rdev_major);
  6727. __put_user(host_stx->stx_rdev_minor, &target_stx->stx_rdev_minor);
  6728. __put_user(host_stx->stx_dev_major, &target_stx->stx_dev_major);
  6729. __put_user(host_stx->stx_dev_minor, &target_stx->stx_dev_minor);
  6730. unlock_user_struct(target_stx, target_addr, 1);
  6731. return 0;
  6732. }
  6733. #endif
  6734. static int do_sys_futex(int *uaddr, int op, int val,
  6735. const struct timespec *timeout, int *uaddr2,
  6736. int val3)
  6737. {
  6738. #if HOST_LONG_BITS == 64
  6739. #if defined(__NR_futex)
  6740. /* always a 64-bit time_t, it doesn't define _time64 version */
  6741. return sys_futex(uaddr, op, val, timeout, uaddr2, val3);
  6742. #endif
  6743. #else /* HOST_LONG_BITS == 64 */
  6744. #if defined(__NR_futex_time64)
  6745. if (sizeof(timeout->tv_sec) == 8) {
  6746. /* _time64 function on 32bit arch */
  6747. return sys_futex_time64(uaddr, op, val, timeout, uaddr2, val3);
  6748. }
  6749. #endif
  6750. #if defined(__NR_futex)
  6751. /* old function on 32bit arch */
  6752. return sys_futex(uaddr, op, val, timeout, uaddr2, val3);
  6753. #endif
  6754. #endif /* HOST_LONG_BITS == 64 */
  6755. g_assert_not_reached();
  6756. }
  6757. static int do_safe_futex(int *uaddr, int op, int val,
  6758. const struct timespec *timeout, int *uaddr2,
  6759. int val3)
  6760. {
  6761. #if HOST_LONG_BITS == 64
  6762. #if defined(__NR_futex)
  6763. /* always a 64-bit time_t, it doesn't define _time64 version */
  6764. return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3));
  6765. #endif
  6766. #else /* HOST_LONG_BITS == 64 */
  6767. #if defined(__NR_futex_time64)
  6768. if (sizeof(timeout->tv_sec) == 8) {
  6769. /* _time64 function on 32bit arch */
  6770. return get_errno(safe_futex_time64(uaddr, op, val, timeout, uaddr2,
  6771. val3));
  6772. }
  6773. #endif
  6774. #if defined(__NR_futex)
  6775. /* old function on 32bit arch */
  6776. return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3));
  6777. #endif
  6778. #endif /* HOST_LONG_BITS == 64 */
  6779. return -TARGET_ENOSYS;
  6780. }
  6781. /* ??? Using host futex calls even when target atomic operations
  6782. are not really atomic probably breaks things. However implementing
  6783. futexes locally would make futexes shared between multiple processes
  6784. tricky. However they're probably useless because guest atomic
  6785. operations won't work either. */
  6786. #if defined(TARGET_NR_futex)
  6787. static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
  6788. target_ulong uaddr2, int val3)
  6789. {
  6790. struct timespec ts, *pts;
  6791. int base_op;
  6792. /* ??? We assume FUTEX_* constants are the same on both host
  6793. and target. */
  6794. #ifdef FUTEX_CMD_MASK
  6795. base_op = op & FUTEX_CMD_MASK;
  6796. #else
  6797. base_op = op;
  6798. #endif
  6799. switch (base_op) {
  6800. case FUTEX_WAIT:
  6801. case FUTEX_WAIT_BITSET:
  6802. if (timeout) {
  6803. pts = &ts;
  6804. target_to_host_timespec(pts, timeout);
  6805. } else {
  6806. pts = NULL;
  6807. }
  6808. return do_safe_futex(g2h(uaddr), op, tswap32(val), pts, NULL, val3);
  6809. case FUTEX_WAKE:
  6810. return do_safe_futex(g2h(uaddr), op, val, NULL, NULL, 0);
  6811. case FUTEX_FD:
  6812. return do_safe_futex(g2h(uaddr), op, val, NULL, NULL, 0);
  6813. case FUTEX_REQUEUE:
  6814. case FUTEX_CMP_REQUEUE:
  6815. case FUTEX_WAKE_OP:
  6816. /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
  6817. TIMEOUT parameter is interpreted as a uint32_t by the kernel.
  6818. But the prototype takes a `struct timespec *'; insert casts
  6819. to satisfy the compiler. We do not need to tswap TIMEOUT
  6820. since it's not compared to guest memory. */
  6821. pts = (struct timespec *)(uintptr_t) timeout;
  6822. return do_safe_futex(g2h(uaddr), op, val, pts, g2h(uaddr2),
  6823. (base_op == FUTEX_CMP_REQUEUE
  6824. ? tswap32(val3)
  6825. : val3));
  6826. default:
  6827. return -TARGET_ENOSYS;
  6828. }
  6829. }
  6830. #endif
  6831. #if defined(TARGET_NR_futex_time64)
  6832. static int do_futex_time64(target_ulong uaddr, int op, int val, target_ulong timeout,
  6833. target_ulong uaddr2, int val3)
  6834. {
  6835. struct timespec ts, *pts;
  6836. int base_op;
  6837. /* ??? We assume FUTEX_* constants are the same on both host
  6838. and target. */
  6839. #ifdef FUTEX_CMD_MASK
  6840. base_op = op & FUTEX_CMD_MASK;
  6841. #else
  6842. base_op = op;
  6843. #endif
  6844. switch (base_op) {
  6845. case FUTEX_WAIT:
  6846. case FUTEX_WAIT_BITSET:
  6847. if (timeout) {
  6848. pts = &ts;
  6849. target_to_host_timespec64(pts, timeout);
  6850. } else {
  6851. pts = NULL;
  6852. }
  6853. return do_safe_futex(g2h(uaddr), op, tswap32(val), pts, NULL, val3);
  6854. case FUTEX_WAKE:
  6855. return do_safe_futex(g2h(uaddr), op, val, NULL, NULL, 0);
  6856. case FUTEX_FD:
  6857. return do_safe_futex(g2h(uaddr), op, val, NULL, NULL, 0);
  6858. case FUTEX_REQUEUE:
  6859. case FUTEX_CMP_REQUEUE:
  6860. case FUTEX_WAKE_OP:
  6861. /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
  6862. TIMEOUT parameter is interpreted as a uint32_t by the kernel.
  6863. But the prototype takes a `struct timespec *'; insert casts
  6864. to satisfy the compiler. We do not need to tswap TIMEOUT
  6865. since it's not compared to guest memory. */
  6866. pts = (struct timespec *)(uintptr_t) timeout;
  6867. return do_safe_futex(g2h(uaddr), op, val, pts, g2h(uaddr2),
  6868. (base_op == FUTEX_CMP_REQUEUE
  6869. ? tswap32(val3)
  6870. : val3));
  6871. default:
  6872. return -TARGET_ENOSYS;
  6873. }
  6874. }
  6875. #endif
  6876. #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  6877. static abi_long do_name_to_handle_at(abi_long dirfd, abi_long pathname,
  6878. abi_long handle, abi_long mount_id,
  6879. abi_long flags)
  6880. {
  6881. struct file_handle *target_fh;
  6882. struct file_handle *fh;
  6883. int mid = 0;
  6884. abi_long ret;
  6885. char *name;
  6886. unsigned int size, total_size;
  6887. if (get_user_s32(size, handle)) {
  6888. return -TARGET_EFAULT;
  6889. }
  6890. name = lock_user_string(pathname);
  6891. if (!name) {
  6892. return -TARGET_EFAULT;
  6893. }
  6894. total_size = sizeof(struct file_handle) + size;
  6895. target_fh = lock_user(VERIFY_WRITE, handle, total_size, 0);
  6896. if (!target_fh) {
  6897. unlock_user(name, pathname, 0);
  6898. return -TARGET_EFAULT;
  6899. }
  6900. fh = g_malloc0(total_size);
  6901. fh->handle_bytes = size;
  6902. ret = get_errno(name_to_handle_at(dirfd, path(name), fh, &mid, flags));
  6903. unlock_user(name, pathname, 0);
  6904. /* man name_to_handle_at(2):
  6905. * Other than the use of the handle_bytes field, the caller should treat
  6906. * the file_handle structure as an opaque data type
  6907. */
  6908. memcpy(target_fh, fh, total_size);
  6909. target_fh->handle_bytes = tswap32(fh->handle_bytes);
  6910. target_fh->handle_type = tswap32(fh->handle_type);
  6911. g_free(fh);
  6912. unlock_user(target_fh, handle, total_size);
  6913. if (put_user_s32(mid, mount_id)) {
  6914. return -TARGET_EFAULT;
  6915. }
  6916. return ret;
  6917. }
  6918. #endif
  6919. #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  6920. static abi_long do_open_by_handle_at(abi_long mount_fd, abi_long handle,
  6921. abi_long flags)
  6922. {
  6923. struct file_handle *target_fh;
  6924. struct file_handle *fh;
  6925. unsigned int size, total_size;
  6926. abi_long ret;
  6927. if (get_user_s32(size, handle)) {
  6928. return -TARGET_EFAULT;
  6929. }
  6930. total_size = sizeof(struct file_handle) + size;
  6931. target_fh = lock_user(VERIFY_READ, handle, total_size, 1);
  6932. if (!target_fh) {
  6933. return -TARGET_EFAULT;
  6934. }
  6935. fh = g_memdup(target_fh, total_size);
  6936. fh->handle_bytes = size;
  6937. fh->handle_type = tswap32(target_fh->handle_type);
  6938. ret = get_errno(open_by_handle_at(mount_fd, fh,
  6939. target_to_host_bitmask(flags, fcntl_flags_tbl)));
  6940. g_free(fh);
  6941. unlock_user(target_fh, handle, total_size);
  6942. return ret;
  6943. }
  6944. #endif
  6945. #if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4)
  6946. static abi_long do_signalfd4(int fd, abi_long mask, int flags)
  6947. {
  6948. int host_flags;
  6949. target_sigset_t *target_mask;
  6950. sigset_t host_mask;
  6951. abi_long ret;
  6952. if (flags & ~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC)) {
  6953. return -TARGET_EINVAL;
  6954. }
  6955. if (!lock_user_struct(VERIFY_READ, target_mask, mask, 1)) {
  6956. return -TARGET_EFAULT;
  6957. }
  6958. target_to_host_sigset(&host_mask, target_mask);
  6959. host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
  6960. ret = get_errno(signalfd(fd, &host_mask, host_flags));
  6961. if (ret >= 0) {
  6962. fd_trans_register(ret, &target_signalfd_trans);
  6963. }
  6964. unlock_user_struct(target_mask, mask, 0);
  6965. return ret;
  6966. }
  6967. #endif
  6968. /* Map host to target signal numbers for the wait family of syscalls.
  6969. Assume all other status bits are the same. */
  6970. int host_to_target_waitstatus(int status)
  6971. {
  6972. if (WIFSIGNALED(status)) {
  6973. return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
  6974. }
  6975. if (WIFSTOPPED(status)) {
  6976. return (host_to_target_signal(WSTOPSIG(status)) << 8)
  6977. | (status & 0xff);
  6978. }
  6979. return status;
  6980. }
  6981. static int open_self_cmdline(void *cpu_env, int fd)
  6982. {
  6983. CPUState *cpu = env_cpu((CPUArchState *)cpu_env);
  6984. struct linux_binprm *bprm = ((TaskState *)cpu->opaque)->bprm;
  6985. int i;
  6986. for (i = 0; i < bprm->argc; i++) {
  6987. size_t len = strlen(bprm->argv[i]) + 1;
  6988. if (write(fd, bprm->argv[i], len) != len) {
  6989. return -1;
  6990. }
  6991. }
  6992. return 0;
  6993. }
  6994. static int open_self_maps(void *cpu_env, int fd)
  6995. {
  6996. CPUState *cpu = env_cpu((CPUArchState *)cpu_env);
  6997. TaskState *ts = cpu->opaque;
  6998. GSList *map_info = read_self_maps();
  6999. GSList *s;
  7000. int count;
  7001. for (s = map_info; s; s = g_slist_next(s)) {
  7002. MapInfo *e = (MapInfo *) s->data;
  7003. if (h2g_valid(e->start)) {
  7004. unsigned long min = e->start;
  7005. unsigned long max = e->end;
  7006. int flags = page_get_flags(h2g(min));
  7007. const char *path;
  7008. max = h2g_valid(max - 1) ?
  7009. max : (uintptr_t) g2h(GUEST_ADDR_MAX) + 1;
  7010. if (page_check_range(h2g(min), max - min, flags) == -1) {
  7011. continue;
  7012. }
  7013. if (h2g(min) == ts->info->stack_limit) {
  7014. path = "[stack]";
  7015. } else {
  7016. path = e->path;
  7017. }
  7018. count = dprintf(fd, TARGET_ABI_FMT_ptr "-" TARGET_ABI_FMT_ptr
  7019. " %c%c%c%c %08" PRIx64 " %s %"PRId64,
  7020. h2g(min), h2g(max - 1) + 1,
  7021. e->is_read ? 'r' : '-',
  7022. e->is_write ? 'w' : '-',
  7023. e->is_exec ? 'x' : '-',
  7024. e->is_priv ? 'p' : '-',
  7025. (uint64_t) e->offset, e->dev, e->inode);
  7026. if (path) {
  7027. dprintf(fd, "%*s%s\n", 73 - count, "", path);
  7028. } else {
  7029. dprintf(fd, "\n");
  7030. }
  7031. }
  7032. }
  7033. free_self_maps(map_info);
  7034. #ifdef TARGET_VSYSCALL_PAGE
  7035. /*
  7036. * We only support execution from the vsyscall page.
  7037. * This is as if CONFIG_LEGACY_VSYSCALL_XONLY=y from v5.3.
  7038. */
  7039. count = dprintf(fd, TARGET_FMT_lx "-" TARGET_FMT_lx
  7040. " --xp 00000000 00:00 0",
  7041. TARGET_VSYSCALL_PAGE, TARGET_VSYSCALL_PAGE + TARGET_PAGE_SIZE);
  7042. dprintf(fd, "%*s%s\n", 73 - count, "", "[vsyscall]");
  7043. #endif
  7044. return 0;
  7045. }
  7046. static int open_self_stat(void *cpu_env, int fd)
  7047. {
  7048. CPUState *cpu = env_cpu((CPUArchState *)cpu_env);
  7049. TaskState *ts = cpu->opaque;
  7050. g_autoptr(GString) buf = g_string_new(NULL);
  7051. int i;
  7052. for (i = 0; i < 44; i++) {
  7053. if (i == 0) {
  7054. /* pid */
  7055. g_string_printf(buf, FMT_pid " ", getpid());
  7056. } else if (i == 1) {
  7057. /* app name */
  7058. gchar *bin = g_strrstr(ts->bprm->argv[0], "/");
  7059. bin = bin ? bin + 1 : ts->bprm->argv[0];
  7060. g_string_printf(buf, "(%.15s) ", bin);
  7061. } else if (i == 27) {
  7062. /* stack bottom */
  7063. g_string_printf(buf, TARGET_ABI_FMT_ld " ", ts->info->start_stack);
  7064. } else {
  7065. /* for the rest, there is MasterCard */
  7066. g_string_printf(buf, "0%c", i == 43 ? '\n' : ' ');
  7067. }
  7068. if (write(fd, buf->str, buf->len) != buf->len) {
  7069. return -1;
  7070. }
  7071. }
  7072. return 0;
  7073. }
  7074. static int open_self_auxv(void *cpu_env, int fd)
  7075. {
  7076. CPUState *cpu = env_cpu((CPUArchState *)cpu_env);
  7077. TaskState *ts = cpu->opaque;
  7078. abi_ulong auxv = ts->info->saved_auxv;
  7079. abi_ulong len = ts->info->auxv_len;
  7080. char *ptr;
  7081. /*
  7082. * Auxiliary vector is stored in target process stack.
  7083. * read in whole auxv vector and copy it to file
  7084. */
  7085. ptr = lock_user(VERIFY_READ, auxv, len, 0);
  7086. if (ptr != NULL) {
  7087. while (len > 0) {
  7088. ssize_t r;
  7089. r = write(fd, ptr, len);
  7090. if (r <= 0) {
  7091. break;
  7092. }
  7093. len -= r;
  7094. ptr += r;
  7095. }
  7096. lseek(fd, 0, SEEK_SET);
  7097. unlock_user(ptr, auxv, len);
  7098. }
  7099. return 0;
  7100. }
  7101. static int is_proc_myself(const char *filename, const char *entry)
  7102. {
  7103. if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
  7104. filename += strlen("/proc/");
  7105. if (!strncmp(filename, "self/", strlen("self/"))) {
  7106. filename += strlen("self/");
  7107. } else if (*filename >= '1' && *filename <= '9') {
  7108. char myself[80];
  7109. snprintf(myself, sizeof(myself), "%d/", getpid());
  7110. if (!strncmp(filename, myself, strlen(myself))) {
  7111. filename += strlen(myself);
  7112. } else {
  7113. return 0;
  7114. }
  7115. } else {
  7116. return 0;
  7117. }
  7118. if (!strcmp(filename, entry)) {
  7119. return 1;
  7120. }
  7121. }
  7122. return 0;
  7123. }
  7124. #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN) || \
  7125. defined(TARGET_SPARC) || defined(TARGET_M68K) || defined(TARGET_HPPA)
  7126. static int is_proc(const char *filename, const char *entry)
  7127. {
  7128. return strcmp(filename, entry) == 0;
  7129. }
  7130. #endif
  7131. #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
  7132. static int open_net_route(void *cpu_env, int fd)
  7133. {
  7134. FILE *fp;
  7135. char *line = NULL;
  7136. size_t len = 0;
  7137. ssize_t read;
  7138. fp = fopen("/proc/net/route", "r");
  7139. if (fp == NULL) {
  7140. return -1;
  7141. }
  7142. /* read header */
  7143. read = getline(&line, &len, fp);
  7144. dprintf(fd, "%s", line);
  7145. /* read routes */
  7146. while ((read = getline(&line, &len, fp)) != -1) {
  7147. char iface[16];
  7148. uint32_t dest, gw, mask;
  7149. unsigned int flags, refcnt, use, metric, mtu, window, irtt;
  7150. int fields;
  7151. fields = sscanf(line,
  7152. "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
  7153. iface, &dest, &gw, &flags, &refcnt, &use, &metric,
  7154. &mask, &mtu, &window, &irtt);
  7155. if (fields != 11) {
  7156. continue;
  7157. }
  7158. dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
  7159. iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
  7160. metric, tswap32(mask), mtu, window, irtt);
  7161. }
  7162. free(line);
  7163. fclose(fp);
  7164. return 0;
  7165. }
  7166. #endif
  7167. #if defined(TARGET_SPARC)
  7168. static int open_cpuinfo(void *cpu_env, int fd)
  7169. {
  7170. dprintf(fd, "type\t\t: sun4u\n");
  7171. return 0;
  7172. }
  7173. #endif
  7174. #if defined(TARGET_HPPA)
  7175. static int open_cpuinfo(void *cpu_env, int fd)
  7176. {
  7177. dprintf(fd, "cpu family\t: PA-RISC 1.1e\n");
  7178. dprintf(fd, "cpu\t\t: PA7300LC (PCX-L2)\n");
  7179. dprintf(fd, "capabilities\t: os32\n");
  7180. dprintf(fd, "model\t\t: 9000/778/B160L\n");
  7181. dprintf(fd, "model name\t: Merlin L2 160 QEMU (9000/778/B160L)\n");
  7182. return 0;
  7183. }
  7184. #endif
  7185. #if defined(TARGET_M68K)
  7186. static int open_hardware(void *cpu_env, int fd)
  7187. {
  7188. dprintf(fd, "Model:\t\tqemu-m68k\n");
  7189. return 0;
  7190. }
  7191. #endif
  7192. static int do_openat(void *cpu_env, int dirfd, const char *pathname, int flags, mode_t mode)
  7193. {
  7194. struct fake_open {
  7195. const char *filename;
  7196. int (*fill)(void *cpu_env, int fd);
  7197. int (*cmp)(const char *s1, const char *s2);
  7198. };
  7199. const struct fake_open *fake_open;
  7200. static const struct fake_open fakes[] = {
  7201. { "maps", open_self_maps, is_proc_myself },
  7202. { "stat", open_self_stat, is_proc_myself },
  7203. { "auxv", open_self_auxv, is_proc_myself },
  7204. { "cmdline", open_self_cmdline, is_proc_myself },
  7205. #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
  7206. { "/proc/net/route", open_net_route, is_proc },
  7207. #endif
  7208. #if defined(TARGET_SPARC) || defined(TARGET_HPPA)
  7209. { "/proc/cpuinfo", open_cpuinfo, is_proc },
  7210. #endif
  7211. #if defined(TARGET_M68K)
  7212. { "/proc/hardware", open_hardware, is_proc },
  7213. #endif
  7214. { NULL, NULL, NULL }
  7215. };
  7216. if (is_proc_myself(pathname, "exe")) {
  7217. int execfd = qemu_getauxval(AT_EXECFD);
  7218. return execfd ? execfd : safe_openat(dirfd, exec_path, flags, mode);
  7219. }
  7220. for (fake_open = fakes; fake_open->filename; fake_open++) {
  7221. if (fake_open->cmp(pathname, fake_open->filename)) {
  7222. break;
  7223. }
  7224. }
  7225. if (fake_open->filename) {
  7226. const char *tmpdir;
  7227. char filename[PATH_MAX];
  7228. int fd, r;
  7229. /* create temporary file to map stat to */
  7230. tmpdir = getenv("TMPDIR");
  7231. if (!tmpdir)
  7232. tmpdir = "/tmp";
  7233. snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
  7234. fd = mkstemp(filename);
  7235. if (fd < 0) {
  7236. return fd;
  7237. }
  7238. unlink(filename);
  7239. if ((r = fake_open->fill(cpu_env, fd))) {
  7240. int e = errno;
  7241. close(fd);
  7242. errno = e;
  7243. return r;
  7244. }
  7245. lseek(fd, 0, SEEK_SET);
  7246. return fd;
  7247. }
  7248. return safe_openat(dirfd, path(pathname), flags, mode);
  7249. }
  7250. #define TIMER_MAGIC 0x0caf0000
  7251. #define TIMER_MAGIC_MASK 0xffff0000
  7252. /* Convert QEMU provided timer ID back to internal 16bit index format */
  7253. static target_timer_t get_timer_id(abi_long arg)
  7254. {
  7255. target_timer_t timerid = arg;
  7256. if ((timerid & TIMER_MAGIC_MASK) != TIMER_MAGIC) {
  7257. return -TARGET_EINVAL;
  7258. }
  7259. timerid &= 0xffff;
  7260. if (timerid >= ARRAY_SIZE(g_posix_timers)) {
  7261. return -TARGET_EINVAL;
  7262. }
  7263. return timerid;
  7264. }
  7265. static int target_to_host_cpu_mask(unsigned long *host_mask,
  7266. size_t host_size,
  7267. abi_ulong target_addr,
  7268. size_t target_size)
  7269. {
  7270. unsigned target_bits = sizeof(abi_ulong) * 8;
  7271. unsigned host_bits = sizeof(*host_mask) * 8;
  7272. abi_ulong *target_mask;
  7273. unsigned i, j;
  7274. assert(host_size >= target_size);
  7275. target_mask = lock_user(VERIFY_READ, target_addr, target_size, 1);
  7276. if (!target_mask) {
  7277. return -TARGET_EFAULT;
  7278. }
  7279. memset(host_mask, 0, host_size);
  7280. for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
  7281. unsigned bit = i * target_bits;
  7282. abi_ulong val;
  7283. __get_user(val, &target_mask[i]);
  7284. for (j = 0; j < target_bits; j++, bit++) {
  7285. if (val & (1UL << j)) {
  7286. host_mask[bit / host_bits] |= 1UL << (bit % host_bits);
  7287. }
  7288. }
  7289. }
  7290. unlock_user(target_mask, target_addr, 0);
  7291. return 0;
  7292. }
  7293. static int host_to_target_cpu_mask(const unsigned long *host_mask,
  7294. size_t host_size,
  7295. abi_ulong target_addr,
  7296. size_t target_size)
  7297. {
  7298. unsigned target_bits = sizeof(abi_ulong) * 8;
  7299. unsigned host_bits = sizeof(*host_mask) * 8;
  7300. abi_ulong *target_mask;
  7301. unsigned i, j;
  7302. assert(host_size >= target_size);
  7303. target_mask = lock_user(VERIFY_WRITE, target_addr, target_size, 0);
  7304. if (!target_mask) {
  7305. return -TARGET_EFAULT;
  7306. }
  7307. for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
  7308. unsigned bit = i * target_bits;
  7309. abi_ulong val = 0;
  7310. for (j = 0; j < target_bits; j++, bit++) {
  7311. if (host_mask[bit / host_bits] & (1UL << (bit % host_bits))) {
  7312. val |= 1UL << j;
  7313. }
  7314. }
  7315. __put_user(val, &target_mask[i]);
  7316. }
  7317. unlock_user(target_mask, target_addr, target_size);
  7318. return 0;
  7319. }
  7320. /* This is an internal helper for do_syscall so that it is easier
  7321. * to have a single return point, so that actions, such as logging
  7322. * of syscall results, can be performed.
  7323. * All errnos that do_syscall() returns must be -TARGET_<errcode>.
  7324. */
  7325. static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
  7326. abi_long arg2, abi_long arg3, abi_long arg4,
  7327. abi_long arg5, abi_long arg6, abi_long arg7,
  7328. abi_long arg8)
  7329. {
  7330. CPUState *cpu = env_cpu(cpu_env);
  7331. abi_long ret;
  7332. #if defined(TARGET_NR_stat) || defined(TARGET_NR_stat64) \
  7333. || defined(TARGET_NR_lstat) || defined(TARGET_NR_lstat64) \
  7334. || defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64) \
  7335. || defined(TARGET_NR_statx)
  7336. struct stat st;
  7337. #endif
  7338. #if defined(TARGET_NR_statfs) || defined(TARGET_NR_statfs64) \
  7339. || defined(TARGET_NR_fstatfs)
  7340. struct statfs stfs;
  7341. #endif
  7342. void *p;
  7343. switch(num) {
  7344. case TARGET_NR_exit:
  7345. /* In old applications this may be used to implement _exit(2).
  7346. However in threaded applications it is used for thread termination,
  7347. and _exit_group is used for application termination.
  7348. Do thread termination if we have more then one thread. */
  7349. if (block_signals()) {
  7350. return -TARGET_ERESTARTSYS;
  7351. }
  7352. pthread_mutex_lock(&clone_lock);
  7353. if (CPU_NEXT(first_cpu)) {
  7354. TaskState *ts = cpu->opaque;
  7355. object_property_set_bool(OBJECT(cpu), "realized", false, NULL);
  7356. object_unref(OBJECT(cpu));
  7357. /*
  7358. * At this point the CPU should be unrealized and removed
  7359. * from cpu lists. We can clean-up the rest of the thread
  7360. * data without the lock held.
  7361. */
  7362. pthread_mutex_unlock(&clone_lock);
  7363. if (ts->child_tidptr) {
  7364. put_user_u32(0, ts->child_tidptr);
  7365. do_sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
  7366. NULL, NULL, 0);
  7367. }
  7368. thread_cpu = NULL;
  7369. g_free(ts);
  7370. rcu_unregister_thread();
  7371. pthread_exit(NULL);
  7372. }
  7373. pthread_mutex_unlock(&clone_lock);
  7374. preexit_cleanup(cpu_env, arg1);
  7375. _exit(arg1);
  7376. return 0; /* avoid warning */
  7377. case TARGET_NR_read:
  7378. if (arg2 == 0 && arg3 == 0) {
  7379. return get_errno(safe_read(arg1, 0, 0));
  7380. } else {
  7381. if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
  7382. return -TARGET_EFAULT;
  7383. ret = get_errno(safe_read(arg1, p, arg3));
  7384. if (ret >= 0 &&
  7385. fd_trans_host_to_target_data(arg1)) {
  7386. ret = fd_trans_host_to_target_data(arg1)(p, ret);
  7387. }
  7388. unlock_user(p, arg2, ret);
  7389. }
  7390. return ret;
  7391. case TARGET_NR_write:
  7392. if (arg2 == 0 && arg3 == 0) {
  7393. return get_errno(safe_write(arg1, 0, 0));
  7394. }
  7395. if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
  7396. return -TARGET_EFAULT;
  7397. if (fd_trans_target_to_host_data(arg1)) {
  7398. void *copy = g_malloc(arg3);
  7399. memcpy(copy, p, arg3);
  7400. ret = fd_trans_target_to_host_data(arg1)(copy, arg3);
  7401. if (ret >= 0) {
  7402. ret = get_errno(safe_write(arg1, copy, ret));
  7403. }
  7404. g_free(copy);
  7405. } else {
  7406. ret = get_errno(safe_write(arg1, p, arg3));
  7407. }
  7408. unlock_user(p, arg2, 0);
  7409. return ret;
  7410. #ifdef TARGET_NR_open
  7411. case TARGET_NR_open:
  7412. if (!(p = lock_user_string(arg1)))
  7413. return -TARGET_EFAULT;
  7414. ret = get_errno(do_openat(cpu_env, AT_FDCWD, p,
  7415. target_to_host_bitmask(arg2, fcntl_flags_tbl),
  7416. arg3));
  7417. fd_trans_unregister(ret);
  7418. unlock_user(p, arg1, 0);
  7419. return ret;
  7420. #endif
  7421. case TARGET_NR_openat:
  7422. if (!(p = lock_user_string(arg2)))
  7423. return -TARGET_EFAULT;
  7424. ret = get_errno(do_openat(cpu_env, arg1, p,
  7425. target_to_host_bitmask(arg3, fcntl_flags_tbl),
  7426. arg4));
  7427. fd_trans_unregister(ret);
  7428. unlock_user(p, arg2, 0);
  7429. return ret;
  7430. #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  7431. case TARGET_NR_name_to_handle_at:
  7432. ret = do_name_to_handle_at(arg1, arg2, arg3, arg4, arg5);
  7433. return ret;
  7434. #endif
  7435. #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  7436. case TARGET_NR_open_by_handle_at:
  7437. ret = do_open_by_handle_at(arg1, arg2, arg3);
  7438. fd_trans_unregister(ret);
  7439. return ret;
  7440. #endif
  7441. case TARGET_NR_close:
  7442. fd_trans_unregister(arg1);
  7443. return get_errno(close(arg1));
  7444. case TARGET_NR_brk:
  7445. return do_brk(arg1);
  7446. #ifdef TARGET_NR_fork
  7447. case TARGET_NR_fork:
  7448. return get_errno(do_fork(cpu_env, TARGET_SIGCHLD, 0, 0, 0, 0));
  7449. #endif
  7450. #ifdef TARGET_NR_waitpid
  7451. case TARGET_NR_waitpid:
  7452. {
  7453. int status;
  7454. ret = get_errno(safe_wait4(arg1, &status, arg3, 0));
  7455. if (!is_error(ret) && arg2 && ret
  7456. && put_user_s32(host_to_target_waitstatus(status), arg2))
  7457. return -TARGET_EFAULT;
  7458. }
  7459. return ret;
  7460. #endif
  7461. #ifdef TARGET_NR_waitid
  7462. case TARGET_NR_waitid:
  7463. {
  7464. siginfo_t info;
  7465. info.si_pid = 0;
  7466. ret = get_errno(safe_waitid(arg1, arg2, &info, arg4, NULL));
  7467. if (!is_error(ret) && arg3 && info.si_pid != 0) {
  7468. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
  7469. return -TARGET_EFAULT;
  7470. host_to_target_siginfo(p, &info);
  7471. unlock_user(p, arg3, sizeof(target_siginfo_t));
  7472. }
  7473. }
  7474. return ret;
  7475. #endif
  7476. #ifdef TARGET_NR_creat /* not on alpha */
  7477. case TARGET_NR_creat:
  7478. if (!(p = lock_user_string(arg1)))
  7479. return -TARGET_EFAULT;
  7480. ret = get_errno(creat(p, arg2));
  7481. fd_trans_unregister(ret);
  7482. unlock_user(p, arg1, 0);
  7483. return ret;
  7484. #endif
  7485. #ifdef TARGET_NR_link
  7486. case TARGET_NR_link:
  7487. {
  7488. void * p2;
  7489. p = lock_user_string(arg1);
  7490. p2 = lock_user_string(arg2);
  7491. if (!p || !p2)
  7492. ret = -TARGET_EFAULT;
  7493. else
  7494. ret = get_errno(link(p, p2));
  7495. unlock_user(p2, arg2, 0);
  7496. unlock_user(p, arg1, 0);
  7497. }
  7498. return ret;
  7499. #endif
  7500. #if defined(TARGET_NR_linkat)
  7501. case TARGET_NR_linkat:
  7502. {
  7503. void * p2 = NULL;
  7504. if (!arg2 || !arg4)
  7505. return -TARGET_EFAULT;
  7506. p = lock_user_string(arg2);
  7507. p2 = lock_user_string(arg4);
  7508. if (!p || !p2)
  7509. ret = -TARGET_EFAULT;
  7510. else
  7511. ret = get_errno(linkat(arg1, p, arg3, p2, arg5));
  7512. unlock_user(p, arg2, 0);
  7513. unlock_user(p2, arg4, 0);
  7514. }
  7515. return ret;
  7516. #endif
  7517. #ifdef TARGET_NR_unlink
  7518. case TARGET_NR_unlink:
  7519. if (!(p = lock_user_string(arg1)))
  7520. return -TARGET_EFAULT;
  7521. ret = get_errno(unlink(p));
  7522. unlock_user(p, arg1, 0);
  7523. return ret;
  7524. #endif
  7525. #if defined(TARGET_NR_unlinkat)
  7526. case TARGET_NR_unlinkat:
  7527. if (!(p = lock_user_string(arg2)))
  7528. return -TARGET_EFAULT;
  7529. ret = get_errno(unlinkat(arg1, p, arg3));
  7530. unlock_user(p, arg2, 0);
  7531. return ret;
  7532. #endif
  7533. case TARGET_NR_execve:
  7534. {
  7535. char **argp, **envp;
  7536. int argc, envc;
  7537. abi_ulong gp;
  7538. abi_ulong guest_argp;
  7539. abi_ulong guest_envp;
  7540. abi_ulong addr;
  7541. char **q;
  7542. int total_size = 0;
  7543. argc = 0;
  7544. guest_argp = arg2;
  7545. for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
  7546. if (get_user_ual(addr, gp))
  7547. return -TARGET_EFAULT;
  7548. if (!addr)
  7549. break;
  7550. argc++;
  7551. }
  7552. envc = 0;
  7553. guest_envp = arg3;
  7554. for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
  7555. if (get_user_ual(addr, gp))
  7556. return -TARGET_EFAULT;
  7557. if (!addr)
  7558. break;
  7559. envc++;
  7560. }
  7561. argp = g_new0(char *, argc + 1);
  7562. envp = g_new0(char *, envc + 1);
  7563. for (gp = guest_argp, q = argp; gp;
  7564. gp += sizeof(abi_ulong), q++) {
  7565. if (get_user_ual(addr, gp))
  7566. goto execve_efault;
  7567. if (!addr)
  7568. break;
  7569. if (!(*q = lock_user_string(addr)))
  7570. goto execve_efault;
  7571. total_size += strlen(*q) + 1;
  7572. }
  7573. *q = NULL;
  7574. for (gp = guest_envp, q = envp; gp;
  7575. gp += sizeof(abi_ulong), q++) {
  7576. if (get_user_ual(addr, gp))
  7577. goto execve_efault;
  7578. if (!addr)
  7579. break;
  7580. if (!(*q = lock_user_string(addr)))
  7581. goto execve_efault;
  7582. total_size += strlen(*q) + 1;
  7583. }
  7584. *q = NULL;
  7585. if (!(p = lock_user_string(arg1)))
  7586. goto execve_efault;
  7587. /* Although execve() is not an interruptible syscall it is
  7588. * a special case where we must use the safe_syscall wrapper:
  7589. * if we allow a signal to happen before we make the host
  7590. * syscall then we will 'lose' it, because at the point of
  7591. * execve the process leaves QEMU's control. So we use the
  7592. * safe syscall wrapper to ensure that we either take the
  7593. * signal as a guest signal, or else it does not happen
  7594. * before the execve completes and makes it the other
  7595. * program's problem.
  7596. */
  7597. ret = get_errno(safe_execve(p, argp, envp));
  7598. unlock_user(p, arg1, 0);
  7599. goto execve_end;
  7600. execve_efault:
  7601. ret = -TARGET_EFAULT;
  7602. execve_end:
  7603. for (gp = guest_argp, q = argp; *q;
  7604. gp += sizeof(abi_ulong), q++) {
  7605. if (get_user_ual(addr, gp)
  7606. || !addr)
  7607. break;
  7608. unlock_user(*q, addr, 0);
  7609. }
  7610. for (gp = guest_envp, q = envp; *q;
  7611. gp += sizeof(abi_ulong), q++) {
  7612. if (get_user_ual(addr, gp)
  7613. || !addr)
  7614. break;
  7615. unlock_user(*q, addr, 0);
  7616. }
  7617. g_free(argp);
  7618. g_free(envp);
  7619. }
  7620. return ret;
  7621. case TARGET_NR_chdir:
  7622. if (!(p = lock_user_string(arg1)))
  7623. return -TARGET_EFAULT;
  7624. ret = get_errno(chdir(p));
  7625. unlock_user(p, arg1, 0);
  7626. return ret;
  7627. #ifdef TARGET_NR_time
  7628. case TARGET_NR_time:
  7629. {
  7630. time_t host_time;
  7631. ret = get_errno(time(&host_time));
  7632. if (!is_error(ret)
  7633. && arg1
  7634. && put_user_sal(host_time, arg1))
  7635. return -TARGET_EFAULT;
  7636. }
  7637. return ret;
  7638. #endif
  7639. #ifdef TARGET_NR_mknod
  7640. case TARGET_NR_mknod:
  7641. if (!(p = lock_user_string(arg1)))
  7642. return -TARGET_EFAULT;
  7643. ret = get_errno(mknod(p, arg2, arg3));
  7644. unlock_user(p, arg1, 0);
  7645. return ret;
  7646. #endif
  7647. #if defined(TARGET_NR_mknodat)
  7648. case TARGET_NR_mknodat:
  7649. if (!(p = lock_user_string(arg2)))
  7650. return -TARGET_EFAULT;
  7651. ret = get_errno(mknodat(arg1, p, arg3, arg4));
  7652. unlock_user(p, arg2, 0);
  7653. return ret;
  7654. #endif
  7655. #ifdef TARGET_NR_chmod
  7656. case TARGET_NR_chmod:
  7657. if (!(p = lock_user_string(arg1)))
  7658. return -TARGET_EFAULT;
  7659. ret = get_errno(chmod(p, arg2));
  7660. unlock_user(p, arg1, 0);
  7661. return ret;
  7662. #endif
  7663. #ifdef TARGET_NR_lseek
  7664. case TARGET_NR_lseek:
  7665. return get_errno(lseek(arg1, arg2, arg3));
  7666. #endif
  7667. #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
  7668. /* Alpha specific */
  7669. case TARGET_NR_getxpid:
  7670. ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
  7671. return get_errno(getpid());
  7672. #endif
  7673. #ifdef TARGET_NR_getpid
  7674. case TARGET_NR_getpid:
  7675. return get_errno(getpid());
  7676. #endif
  7677. case TARGET_NR_mount:
  7678. {
  7679. /* need to look at the data field */
  7680. void *p2, *p3;
  7681. if (arg1) {
  7682. p = lock_user_string(arg1);
  7683. if (!p) {
  7684. return -TARGET_EFAULT;
  7685. }
  7686. } else {
  7687. p = NULL;
  7688. }
  7689. p2 = lock_user_string(arg2);
  7690. if (!p2) {
  7691. if (arg1) {
  7692. unlock_user(p, arg1, 0);
  7693. }
  7694. return -TARGET_EFAULT;
  7695. }
  7696. if (arg3) {
  7697. p3 = lock_user_string(arg3);
  7698. if (!p3) {
  7699. if (arg1) {
  7700. unlock_user(p, arg1, 0);
  7701. }
  7702. unlock_user(p2, arg2, 0);
  7703. return -TARGET_EFAULT;
  7704. }
  7705. } else {
  7706. p3 = NULL;
  7707. }
  7708. /* FIXME - arg5 should be locked, but it isn't clear how to
  7709. * do that since it's not guaranteed to be a NULL-terminated
  7710. * string.
  7711. */
  7712. if (!arg5) {
  7713. ret = mount(p, p2, p3, (unsigned long)arg4, NULL);
  7714. } else {
  7715. ret = mount(p, p2, p3, (unsigned long)arg4, g2h(arg5));
  7716. }
  7717. ret = get_errno(ret);
  7718. if (arg1) {
  7719. unlock_user(p, arg1, 0);
  7720. }
  7721. unlock_user(p2, arg2, 0);
  7722. if (arg3) {
  7723. unlock_user(p3, arg3, 0);
  7724. }
  7725. }
  7726. return ret;
  7727. #if defined(TARGET_NR_umount) || defined(TARGET_NR_oldumount)
  7728. #if defined(TARGET_NR_umount)
  7729. case TARGET_NR_umount:
  7730. #endif
  7731. #if defined(TARGET_NR_oldumount)
  7732. case TARGET_NR_oldumount:
  7733. #endif
  7734. if (!(p = lock_user_string(arg1)))
  7735. return -TARGET_EFAULT;
  7736. ret = get_errno(umount(p));
  7737. unlock_user(p, arg1, 0);
  7738. return ret;
  7739. #endif
  7740. #ifdef TARGET_NR_stime /* not on alpha */
  7741. case TARGET_NR_stime:
  7742. {
  7743. struct timespec ts;
  7744. ts.tv_nsec = 0;
  7745. if (get_user_sal(ts.tv_sec, arg1)) {
  7746. return -TARGET_EFAULT;
  7747. }
  7748. return get_errno(clock_settime(CLOCK_REALTIME, &ts));
  7749. }
  7750. #endif
  7751. #ifdef TARGET_NR_alarm /* not on alpha */
  7752. case TARGET_NR_alarm:
  7753. return alarm(arg1);
  7754. #endif
  7755. #ifdef TARGET_NR_pause /* not on alpha */
  7756. case TARGET_NR_pause:
  7757. if (!block_signals()) {
  7758. sigsuspend(&((TaskState *)cpu->opaque)->signal_mask);
  7759. }
  7760. return -TARGET_EINTR;
  7761. #endif
  7762. #ifdef TARGET_NR_utime
  7763. case TARGET_NR_utime:
  7764. {
  7765. struct utimbuf tbuf, *host_tbuf;
  7766. struct target_utimbuf *target_tbuf;
  7767. if (arg2) {
  7768. if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
  7769. return -TARGET_EFAULT;
  7770. tbuf.actime = tswapal(target_tbuf->actime);
  7771. tbuf.modtime = tswapal(target_tbuf->modtime);
  7772. unlock_user_struct(target_tbuf, arg2, 0);
  7773. host_tbuf = &tbuf;
  7774. } else {
  7775. host_tbuf = NULL;
  7776. }
  7777. if (!(p = lock_user_string(arg1)))
  7778. return -TARGET_EFAULT;
  7779. ret = get_errno(utime(p, host_tbuf));
  7780. unlock_user(p, arg1, 0);
  7781. }
  7782. return ret;
  7783. #endif
  7784. #ifdef TARGET_NR_utimes
  7785. case TARGET_NR_utimes:
  7786. {
  7787. struct timeval *tvp, tv[2];
  7788. if (arg2) {
  7789. if (copy_from_user_timeval(&tv[0], arg2)
  7790. || copy_from_user_timeval(&tv[1],
  7791. arg2 + sizeof(struct target_timeval)))
  7792. return -TARGET_EFAULT;
  7793. tvp = tv;
  7794. } else {
  7795. tvp = NULL;
  7796. }
  7797. if (!(p = lock_user_string(arg1)))
  7798. return -TARGET_EFAULT;
  7799. ret = get_errno(utimes(p, tvp));
  7800. unlock_user(p, arg1, 0);
  7801. }
  7802. return ret;
  7803. #endif
  7804. #if defined(TARGET_NR_futimesat)
  7805. case TARGET_NR_futimesat:
  7806. {
  7807. struct timeval *tvp, tv[2];
  7808. if (arg3) {
  7809. if (copy_from_user_timeval(&tv[0], arg3)
  7810. || copy_from_user_timeval(&tv[1],
  7811. arg3 + sizeof(struct target_timeval)))
  7812. return -TARGET_EFAULT;
  7813. tvp = tv;
  7814. } else {
  7815. tvp = NULL;
  7816. }
  7817. if (!(p = lock_user_string(arg2))) {
  7818. return -TARGET_EFAULT;
  7819. }
  7820. ret = get_errno(futimesat(arg1, path(p), tvp));
  7821. unlock_user(p, arg2, 0);
  7822. }
  7823. return ret;
  7824. #endif
  7825. #ifdef TARGET_NR_access
  7826. case TARGET_NR_access:
  7827. if (!(p = lock_user_string(arg1))) {
  7828. return -TARGET_EFAULT;
  7829. }
  7830. ret = get_errno(access(path(p), arg2));
  7831. unlock_user(p, arg1, 0);
  7832. return ret;
  7833. #endif
  7834. #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
  7835. case TARGET_NR_faccessat:
  7836. if (!(p = lock_user_string(arg2))) {
  7837. return -TARGET_EFAULT;
  7838. }
  7839. ret = get_errno(faccessat(arg1, p, arg3, 0));
  7840. unlock_user(p, arg2, 0);
  7841. return ret;
  7842. #endif
  7843. #ifdef TARGET_NR_nice /* not on alpha */
  7844. case TARGET_NR_nice:
  7845. return get_errno(nice(arg1));
  7846. #endif
  7847. case TARGET_NR_sync:
  7848. sync();
  7849. return 0;
  7850. #if defined(TARGET_NR_syncfs) && defined(CONFIG_SYNCFS)
  7851. case TARGET_NR_syncfs:
  7852. return get_errno(syncfs(arg1));
  7853. #endif
  7854. case TARGET_NR_kill:
  7855. return get_errno(safe_kill(arg1, target_to_host_signal(arg2)));
  7856. #ifdef TARGET_NR_rename
  7857. case TARGET_NR_rename:
  7858. {
  7859. void *p2;
  7860. p = lock_user_string(arg1);
  7861. p2 = lock_user_string(arg2);
  7862. if (!p || !p2)
  7863. ret = -TARGET_EFAULT;
  7864. else
  7865. ret = get_errno(rename(p, p2));
  7866. unlock_user(p2, arg2, 0);
  7867. unlock_user(p, arg1, 0);
  7868. }
  7869. return ret;
  7870. #endif
  7871. #if defined(TARGET_NR_renameat)
  7872. case TARGET_NR_renameat:
  7873. {
  7874. void *p2;
  7875. p = lock_user_string(arg2);
  7876. p2 = lock_user_string(arg4);
  7877. if (!p || !p2)
  7878. ret = -TARGET_EFAULT;
  7879. else
  7880. ret = get_errno(renameat(arg1, p, arg3, p2));
  7881. unlock_user(p2, arg4, 0);
  7882. unlock_user(p, arg2, 0);
  7883. }
  7884. return ret;
  7885. #endif
  7886. #if defined(TARGET_NR_renameat2)
  7887. case TARGET_NR_renameat2:
  7888. {
  7889. void *p2;
  7890. p = lock_user_string(arg2);
  7891. p2 = lock_user_string(arg4);
  7892. if (!p || !p2) {
  7893. ret = -TARGET_EFAULT;
  7894. } else {
  7895. ret = get_errno(sys_renameat2(arg1, p, arg3, p2, arg5));
  7896. }
  7897. unlock_user(p2, arg4, 0);
  7898. unlock_user(p, arg2, 0);
  7899. }
  7900. return ret;
  7901. #endif
  7902. #ifdef TARGET_NR_mkdir
  7903. case TARGET_NR_mkdir:
  7904. if (!(p = lock_user_string(arg1)))
  7905. return -TARGET_EFAULT;
  7906. ret = get_errno(mkdir(p, arg2));
  7907. unlock_user(p, arg1, 0);
  7908. return ret;
  7909. #endif
  7910. #if defined(TARGET_NR_mkdirat)
  7911. case TARGET_NR_mkdirat:
  7912. if (!(p = lock_user_string(arg2)))
  7913. return -TARGET_EFAULT;
  7914. ret = get_errno(mkdirat(arg1, p, arg3));
  7915. unlock_user(p, arg2, 0);
  7916. return ret;
  7917. #endif
  7918. #ifdef TARGET_NR_rmdir
  7919. case TARGET_NR_rmdir:
  7920. if (!(p = lock_user_string(arg1)))
  7921. return -TARGET_EFAULT;
  7922. ret = get_errno(rmdir(p));
  7923. unlock_user(p, arg1, 0);
  7924. return ret;
  7925. #endif
  7926. case TARGET_NR_dup:
  7927. ret = get_errno(dup(arg1));
  7928. if (ret >= 0) {
  7929. fd_trans_dup(arg1, ret);
  7930. }
  7931. return ret;
  7932. #ifdef TARGET_NR_pipe
  7933. case TARGET_NR_pipe:
  7934. return do_pipe(cpu_env, arg1, 0, 0);
  7935. #endif
  7936. #ifdef TARGET_NR_pipe2
  7937. case TARGET_NR_pipe2:
  7938. return do_pipe(cpu_env, arg1,
  7939. target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
  7940. #endif
  7941. case TARGET_NR_times:
  7942. {
  7943. struct target_tms *tmsp;
  7944. struct tms tms;
  7945. ret = get_errno(times(&tms));
  7946. if (arg1) {
  7947. tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
  7948. if (!tmsp)
  7949. return -TARGET_EFAULT;
  7950. tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
  7951. tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
  7952. tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
  7953. tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
  7954. }
  7955. if (!is_error(ret))
  7956. ret = host_to_target_clock_t(ret);
  7957. }
  7958. return ret;
  7959. case TARGET_NR_acct:
  7960. if (arg1 == 0) {
  7961. ret = get_errno(acct(NULL));
  7962. } else {
  7963. if (!(p = lock_user_string(arg1))) {
  7964. return -TARGET_EFAULT;
  7965. }
  7966. ret = get_errno(acct(path(p)));
  7967. unlock_user(p, arg1, 0);
  7968. }
  7969. return ret;
  7970. #ifdef TARGET_NR_umount2
  7971. case TARGET_NR_umount2:
  7972. if (!(p = lock_user_string(arg1)))
  7973. return -TARGET_EFAULT;
  7974. ret = get_errno(umount2(p, arg2));
  7975. unlock_user(p, arg1, 0);
  7976. return ret;
  7977. #endif
  7978. case TARGET_NR_ioctl:
  7979. return do_ioctl(arg1, arg2, arg3);
  7980. #ifdef TARGET_NR_fcntl
  7981. case TARGET_NR_fcntl:
  7982. return do_fcntl(arg1, arg2, arg3);
  7983. #endif
  7984. case TARGET_NR_setpgid:
  7985. return get_errno(setpgid(arg1, arg2));
  7986. case TARGET_NR_umask:
  7987. return get_errno(umask(arg1));
  7988. case TARGET_NR_chroot:
  7989. if (!(p = lock_user_string(arg1)))
  7990. return -TARGET_EFAULT;
  7991. ret = get_errno(chroot(p));
  7992. unlock_user(p, arg1, 0);
  7993. return ret;
  7994. #ifdef TARGET_NR_dup2
  7995. case TARGET_NR_dup2:
  7996. ret = get_errno(dup2(arg1, arg2));
  7997. if (ret >= 0) {
  7998. fd_trans_dup(arg1, arg2);
  7999. }
  8000. return ret;
  8001. #endif
  8002. #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
  8003. case TARGET_NR_dup3:
  8004. {
  8005. int host_flags;
  8006. if ((arg3 & ~TARGET_O_CLOEXEC) != 0) {
  8007. return -EINVAL;
  8008. }
  8009. host_flags = target_to_host_bitmask(arg3, fcntl_flags_tbl);
  8010. ret = get_errno(dup3(arg1, arg2, host_flags));
  8011. if (ret >= 0) {
  8012. fd_trans_dup(arg1, arg2);
  8013. }
  8014. return ret;
  8015. }
  8016. #endif
  8017. #ifdef TARGET_NR_getppid /* not on alpha */
  8018. case TARGET_NR_getppid:
  8019. return get_errno(getppid());
  8020. #endif
  8021. #ifdef TARGET_NR_getpgrp
  8022. case TARGET_NR_getpgrp:
  8023. return get_errno(getpgrp());
  8024. #endif
  8025. case TARGET_NR_setsid:
  8026. return get_errno(setsid());
  8027. #ifdef TARGET_NR_sigaction
  8028. case TARGET_NR_sigaction:
  8029. {
  8030. #if defined(TARGET_ALPHA)
  8031. struct target_sigaction act, oact, *pact = 0;
  8032. struct target_old_sigaction *old_act;
  8033. if (arg2) {
  8034. if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
  8035. return -TARGET_EFAULT;
  8036. act._sa_handler = old_act->_sa_handler;
  8037. target_siginitset(&act.sa_mask, old_act->sa_mask);
  8038. act.sa_flags = old_act->sa_flags;
  8039. act.sa_restorer = 0;
  8040. unlock_user_struct(old_act, arg2, 0);
  8041. pact = &act;
  8042. }
  8043. ret = get_errno(do_sigaction(arg1, pact, &oact));
  8044. if (!is_error(ret) && arg3) {
  8045. if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
  8046. return -TARGET_EFAULT;
  8047. old_act->_sa_handler = oact._sa_handler;
  8048. old_act->sa_mask = oact.sa_mask.sig[0];
  8049. old_act->sa_flags = oact.sa_flags;
  8050. unlock_user_struct(old_act, arg3, 1);
  8051. }
  8052. #elif defined(TARGET_MIPS)
  8053. struct target_sigaction act, oact, *pact, *old_act;
  8054. if (arg2) {
  8055. if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
  8056. return -TARGET_EFAULT;
  8057. act._sa_handler = old_act->_sa_handler;
  8058. target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
  8059. act.sa_flags = old_act->sa_flags;
  8060. unlock_user_struct(old_act, arg2, 0);
  8061. pact = &act;
  8062. } else {
  8063. pact = NULL;
  8064. }
  8065. ret = get_errno(do_sigaction(arg1, pact, &oact));
  8066. if (!is_error(ret) && arg3) {
  8067. if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
  8068. return -TARGET_EFAULT;
  8069. old_act->_sa_handler = oact._sa_handler;
  8070. old_act->sa_flags = oact.sa_flags;
  8071. old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
  8072. old_act->sa_mask.sig[1] = 0;
  8073. old_act->sa_mask.sig[2] = 0;
  8074. old_act->sa_mask.sig[3] = 0;
  8075. unlock_user_struct(old_act, arg3, 1);
  8076. }
  8077. #else
  8078. struct target_old_sigaction *old_act;
  8079. struct target_sigaction act, oact, *pact;
  8080. if (arg2) {
  8081. if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
  8082. return -TARGET_EFAULT;
  8083. act._sa_handler = old_act->_sa_handler;
  8084. target_siginitset(&act.sa_mask, old_act->sa_mask);
  8085. act.sa_flags = old_act->sa_flags;
  8086. act.sa_restorer = old_act->sa_restorer;
  8087. #ifdef TARGET_ARCH_HAS_KA_RESTORER
  8088. act.ka_restorer = 0;
  8089. #endif
  8090. unlock_user_struct(old_act, arg2, 0);
  8091. pact = &act;
  8092. } else {
  8093. pact = NULL;
  8094. }
  8095. ret = get_errno(do_sigaction(arg1, pact, &oact));
  8096. if (!is_error(ret) && arg3) {
  8097. if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
  8098. return -TARGET_EFAULT;
  8099. old_act->_sa_handler = oact._sa_handler;
  8100. old_act->sa_mask = oact.sa_mask.sig[0];
  8101. old_act->sa_flags = oact.sa_flags;
  8102. old_act->sa_restorer = oact.sa_restorer;
  8103. unlock_user_struct(old_act, arg3, 1);
  8104. }
  8105. #endif
  8106. }
  8107. return ret;
  8108. #endif
  8109. case TARGET_NR_rt_sigaction:
  8110. {
  8111. #if defined(TARGET_ALPHA)
  8112. /* For Alpha and SPARC this is a 5 argument syscall, with
  8113. * a 'restorer' parameter which must be copied into the
  8114. * sa_restorer field of the sigaction struct.
  8115. * For Alpha that 'restorer' is arg5; for SPARC it is arg4,
  8116. * and arg5 is the sigsetsize.
  8117. * Alpha also has a separate rt_sigaction struct that it uses
  8118. * here; SPARC uses the usual sigaction struct.
  8119. */
  8120. struct target_rt_sigaction *rt_act;
  8121. struct target_sigaction act, oact, *pact = 0;
  8122. if (arg4 != sizeof(target_sigset_t)) {
  8123. return -TARGET_EINVAL;
  8124. }
  8125. if (arg2) {
  8126. if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
  8127. return -TARGET_EFAULT;
  8128. act._sa_handler = rt_act->_sa_handler;
  8129. act.sa_mask = rt_act->sa_mask;
  8130. act.sa_flags = rt_act->sa_flags;
  8131. act.sa_restorer = arg5;
  8132. unlock_user_struct(rt_act, arg2, 0);
  8133. pact = &act;
  8134. }
  8135. ret = get_errno(do_sigaction(arg1, pact, &oact));
  8136. if (!is_error(ret) && arg3) {
  8137. if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
  8138. return -TARGET_EFAULT;
  8139. rt_act->_sa_handler = oact._sa_handler;
  8140. rt_act->sa_mask = oact.sa_mask;
  8141. rt_act->sa_flags = oact.sa_flags;
  8142. unlock_user_struct(rt_act, arg3, 1);
  8143. }
  8144. #else
  8145. #ifdef TARGET_SPARC
  8146. target_ulong restorer = arg4;
  8147. target_ulong sigsetsize = arg5;
  8148. #else
  8149. target_ulong sigsetsize = arg4;
  8150. #endif
  8151. struct target_sigaction *act;
  8152. struct target_sigaction *oact;
  8153. if (sigsetsize != sizeof(target_sigset_t)) {
  8154. return -TARGET_EINVAL;
  8155. }
  8156. if (arg2) {
  8157. if (!lock_user_struct(VERIFY_READ, act, arg2, 1)) {
  8158. return -TARGET_EFAULT;
  8159. }
  8160. #ifdef TARGET_ARCH_HAS_KA_RESTORER
  8161. act->ka_restorer = restorer;
  8162. #endif
  8163. } else {
  8164. act = NULL;
  8165. }
  8166. if (arg3) {
  8167. if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
  8168. ret = -TARGET_EFAULT;
  8169. goto rt_sigaction_fail;
  8170. }
  8171. } else
  8172. oact = NULL;
  8173. ret = get_errno(do_sigaction(arg1, act, oact));
  8174. rt_sigaction_fail:
  8175. if (act)
  8176. unlock_user_struct(act, arg2, 0);
  8177. if (oact)
  8178. unlock_user_struct(oact, arg3, 1);
  8179. #endif
  8180. }
  8181. return ret;
  8182. #ifdef TARGET_NR_sgetmask /* not on alpha */
  8183. case TARGET_NR_sgetmask:
  8184. {
  8185. sigset_t cur_set;
  8186. abi_ulong target_set;
  8187. ret = do_sigprocmask(0, NULL, &cur_set);
  8188. if (!ret) {
  8189. host_to_target_old_sigset(&target_set, &cur_set);
  8190. ret = target_set;
  8191. }
  8192. }
  8193. return ret;
  8194. #endif
  8195. #ifdef TARGET_NR_ssetmask /* not on alpha */
  8196. case TARGET_NR_ssetmask:
  8197. {
  8198. sigset_t set, oset;
  8199. abi_ulong target_set = arg1;
  8200. target_to_host_old_sigset(&set, &target_set);
  8201. ret = do_sigprocmask(SIG_SETMASK, &set, &oset);
  8202. if (!ret) {
  8203. host_to_target_old_sigset(&target_set, &oset);
  8204. ret = target_set;
  8205. }
  8206. }
  8207. return ret;
  8208. #endif
  8209. #ifdef TARGET_NR_sigprocmask
  8210. case TARGET_NR_sigprocmask:
  8211. {
  8212. #if defined(TARGET_ALPHA)
  8213. sigset_t set, oldset;
  8214. abi_ulong mask;
  8215. int how;
  8216. switch (arg1) {
  8217. case TARGET_SIG_BLOCK:
  8218. how = SIG_BLOCK;
  8219. break;
  8220. case TARGET_SIG_UNBLOCK:
  8221. how = SIG_UNBLOCK;
  8222. break;
  8223. case TARGET_SIG_SETMASK:
  8224. how = SIG_SETMASK;
  8225. break;
  8226. default:
  8227. return -TARGET_EINVAL;
  8228. }
  8229. mask = arg2;
  8230. target_to_host_old_sigset(&set, &mask);
  8231. ret = do_sigprocmask(how, &set, &oldset);
  8232. if (!is_error(ret)) {
  8233. host_to_target_old_sigset(&mask, &oldset);
  8234. ret = mask;
  8235. ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0; /* force no error */
  8236. }
  8237. #else
  8238. sigset_t set, oldset, *set_ptr;
  8239. int how;
  8240. if (arg2) {
  8241. switch (arg1) {
  8242. case TARGET_SIG_BLOCK:
  8243. how = SIG_BLOCK;
  8244. break;
  8245. case TARGET_SIG_UNBLOCK:
  8246. how = SIG_UNBLOCK;
  8247. break;
  8248. case TARGET_SIG_SETMASK:
  8249. how = SIG_SETMASK;
  8250. break;
  8251. default:
  8252. return -TARGET_EINVAL;
  8253. }
  8254. if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
  8255. return -TARGET_EFAULT;
  8256. target_to_host_old_sigset(&set, p);
  8257. unlock_user(p, arg2, 0);
  8258. set_ptr = &set;
  8259. } else {
  8260. how = 0;
  8261. set_ptr = NULL;
  8262. }
  8263. ret = do_sigprocmask(how, set_ptr, &oldset);
  8264. if (!is_error(ret) && arg3) {
  8265. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
  8266. return -TARGET_EFAULT;
  8267. host_to_target_old_sigset(p, &oldset);
  8268. unlock_user(p, arg3, sizeof(target_sigset_t));
  8269. }
  8270. #endif
  8271. }
  8272. return ret;
  8273. #endif
  8274. case TARGET_NR_rt_sigprocmask:
  8275. {
  8276. int how = arg1;
  8277. sigset_t set, oldset, *set_ptr;
  8278. if (arg4 != sizeof(target_sigset_t)) {
  8279. return -TARGET_EINVAL;
  8280. }
  8281. if (arg2) {
  8282. switch(how) {
  8283. case TARGET_SIG_BLOCK:
  8284. how = SIG_BLOCK;
  8285. break;
  8286. case TARGET_SIG_UNBLOCK:
  8287. how = SIG_UNBLOCK;
  8288. break;
  8289. case TARGET_SIG_SETMASK:
  8290. how = SIG_SETMASK;
  8291. break;
  8292. default:
  8293. return -TARGET_EINVAL;
  8294. }
  8295. if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
  8296. return -TARGET_EFAULT;
  8297. target_to_host_sigset(&set, p);
  8298. unlock_user(p, arg2, 0);
  8299. set_ptr = &set;
  8300. } else {
  8301. how = 0;
  8302. set_ptr = NULL;
  8303. }
  8304. ret = do_sigprocmask(how, set_ptr, &oldset);
  8305. if (!is_error(ret) && arg3) {
  8306. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
  8307. return -TARGET_EFAULT;
  8308. host_to_target_sigset(p, &oldset);
  8309. unlock_user(p, arg3, sizeof(target_sigset_t));
  8310. }
  8311. }
  8312. return ret;
  8313. #ifdef TARGET_NR_sigpending
  8314. case TARGET_NR_sigpending:
  8315. {
  8316. sigset_t set;
  8317. ret = get_errno(sigpending(&set));
  8318. if (!is_error(ret)) {
  8319. if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
  8320. return -TARGET_EFAULT;
  8321. host_to_target_old_sigset(p, &set);
  8322. unlock_user(p, arg1, sizeof(target_sigset_t));
  8323. }
  8324. }
  8325. return ret;
  8326. #endif
  8327. case TARGET_NR_rt_sigpending:
  8328. {
  8329. sigset_t set;
  8330. /* Yes, this check is >, not != like most. We follow the kernel's
  8331. * logic and it does it like this because it implements
  8332. * NR_sigpending through the same code path, and in that case
  8333. * the old_sigset_t is smaller in size.
  8334. */
  8335. if (arg2 > sizeof(target_sigset_t)) {
  8336. return -TARGET_EINVAL;
  8337. }
  8338. ret = get_errno(sigpending(&set));
  8339. if (!is_error(ret)) {
  8340. if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
  8341. return -TARGET_EFAULT;
  8342. host_to_target_sigset(p, &set);
  8343. unlock_user(p, arg1, sizeof(target_sigset_t));
  8344. }
  8345. }
  8346. return ret;
  8347. #ifdef TARGET_NR_sigsuspend
  8348. case TARGET_NR_sigsuspend:
  8349. {
  8350. TaskState *ts = cpu->opaque;
  8351. #if defined(TARGET_ALPHA)
  8352. abi_ulong mask = arg1;
  8353. target_to_host_old_sigset(&ts->sigsuspend_mask, &mask);
  8354. #else
  8355. if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
  8356. return -TARGET_EFAULT;
  8357. target_to_host_old_sigset(&ts->sigsuspend_mask, p);
  8358. unlock_user(p, arg1, 0);
  8359. #endif
  8360. ret = get_errno(safe_rt_sigsuspend(&ts->sigsuspend_mask,
  8361. SIGSET_T_SIZE));
  8362. if (ret != -TARGET_ERESTARTSYS) {
  8363. ts->in_sigsuspend = 1;
  8364. }
  8365. }
  8366. return ret;
  8367. #endif
  8368. case TARGET_NR_rt_sigsuspend:
  8369. {
  8370. TaskState *ts = cpu->opaque;
  8371. if (arg2 != sizeof(target_sigset_t)) {
  8372. return -TARGET_EINVAL;
  8373. }
  8374. if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
  8375. return -TARGET_EFAULT;
  8376. target_to_host_sigset(&ts->sigsuspend_mask, p);
  8377. unlock_user(p, arg1, 0);
  8378. ret = get_errno(safe_rt_sigsuspend(&ts->sigsuspend_mask,
  8379. SIGSET_T_SIZE));
  8380. if (ret != -TARGET_ERESTARTSYS) {
  8381. ts->in_sigsuspend = 1;
  8382. }
  8383. }
  8384. return ret;
  8385. #ifdef TARGET_NR_rt_sigtimedwait
  8386. case TARGET_NR_rt_sigtimedwait:
  8387. {
  8388. sigset_t set;
  8389. struct timespec uts, *puts;
  8390. siginfo_t uinfo;
  8391. if (arg4 != sizeof(target_sigset_t)) {
  8392. return -TARGET_EINVAL;
  8393. }
  8394. if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
  8395. return -TARGET_EFAULT;
  8396. target_to_host_sigset(&set, p);
  8397. unlock_user(p, arg1, 0);
  8398. if (arg3) {
  8399. puts = &uts;
  8400. if (target_to_host_timespec(puts, arg3)) {
  8401. return -TARGET_EFAULT;
  8402. }
  8403. } else {
  8404. puts = NULL;
  8405. }
  8406. ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts,
  8407. SIGSET_T_SIZE));
  8408. if (!is_error(ret)) {
  8409. if (arg2) {
  8410. p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t),
  8411. 0);
  8412. if (!p) {
  8413. return -TARGET_EFAULT;
  8414. }
  8415. host_to_target_siginfo(p, &uinfo);
  8416. unlock_user(p, arg2, sizeof(target_siginfo_t));
  8417. }
  8418. ret = host_to_target_signal(ret);
  8419. }
  8420. }
  8421. return ret;
  8422. #endif
  8423. #ifdef TARGET_NR_rt_sigtimedwait_time64
  8424. case TARGET_NR_rt_sigtimedwait_time64:
  8425. {
  8426. sigset_t set;
  8427. struct timespec uts, *puts;
  8428. siginfo_t uinfo;
  8429. if (arg4 != sizeof(target_sigset_t)) {
  8430. return -TARGET_EINVAL;
  8431. }
  8432. p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1);
  8433. if (!p) {
  8434. return -TARGET_EFAULT;
  8435. }
  8436. target_to_host_sigset(&set, p);
  8437. unlock_user(p, arg1, 0);
  8438. if (arg3) {
  8439. puts = &uts;
  8440. if (target_to_host_timespec64(puts, arg3)) {
  8441. return -TARGET_EFAULT;
  8442. }
  8443. } else {
  8444. puts = NULL;
  8445. }
  8446. ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts,
  8447. SIGSET_T_SIZE));
  8448. if (!is_error(ret)) {
  8449. if (arg2) {
  8450. p = lock_user(VERIFY_WRITE, arg2,
  8451. sizeof(target_siginfo_t), 0);
  8452. if (!p) {
  8453. return -TARGET_EFAULT;
  8454. }
  8455. host_to_target_siginfo(p, &uinfo);
  8456. unlock_user(p, arg2, sizeof(target_siginfo_t));
  8457. }
  8458. ret = host_to_target_signal(ret);
  8459. }
  8460. }
  8461. return ret;
  8462. #endif
  8463. case TARGET_NR_rt_sigqueueinfo:
  8464. {
  8465. siginfo_t uinfo;
  8466. p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1);
  8467. if (!p) {
  8468. return -TARGET_EFAULT;
  8469. }
  8470. target_to_host_siginfo(&uinfo, p);
  8471. unlock_user(p, arg3, 0);
  8472. ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
  8473. }
  8474. return ret;
  8475. case TARGET_NR_rt_tgsigqueueinfo:
  8476. {
  8477. siginfo_t uinfo;
  8478. p = lock_user(VERIFY_READ, arg4, sizeof(target_siginfo_t), 1);
  8479. if (!p) {
  8480. return -TARGET_EFAULT;
  8481. }
  8482. target_to_host_siginfo(&uinfo, p);
  8483. unlock_user(p, arg4, 0);
  8484. ret = get_errno(sys_rt_tgsigqueueinfo(arg1, arg2, arg3, &uinfo));
  8485. }
  8486. return ret;
  8487. #ifdef TARGET_NR_sigreturn
  8488. case TARGET_NR_sigreturn:
  8489. if (block_signals()) {
  8490. return -TARGET_ERESTARTSYS;
  8491. }
  8492. return do_sigreturn(cpu_env);
  8493. #endif
  8494. case TARGET_NR_rt_sigreturn:
  8495. if (block_signals()) {
  8496. return -TARGET_ERESTARTSYS;
  8497. }
  8498. return do_rt_sigreturn(cpu_env);
  8499. case TARGET_NR_sethostname:
  8500. if (!(p = lock_user_string(arg1)))
  8501. return -TARGET_EFAULT;
  8502. ret = get_errno(sethostname(p, arg2));
  8503. unlock_user(p, arg1, 0);
  8504. return ret;
  8505. #ifdef TARGET_NR_setrlimit
  8506. case TARGET_NR_setrlimit:
  8507. {
  8508. int resource = target_to_host_resource(arg1);
  8509. struct target_rlimit *target_rlim;
  8510. struct rlimit rlim;
  8511. if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
  8512. return -TARGET_EFAULT;
  8513. rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
  8514. rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
  8515. unlock_user_struct(target_rlim, arg2, 0);
  8516. /*
  8517. * If we just passed through resource limit settings for memory then
  8518. * they would also apply to QEMU's own allocations, and QEMU will
  8519. * crash or hang or die if its allocations fail. Ideally we would
  8520. * track the guest allocations in QEMU and apply the limits ourselves.
  8521. * For now, just tell the guest the call succeeded but don't actually
  8522. * limit anything.
  8523. */
  8524. if (resource != RLIMIT_AS &&
  8525. resource != RLIMIT_DATA &&
  8526. resource != RLIMIT_STACK) {
  8527. return get_errno(setrlimit(resource, &rlim));
  8528. } else {
  8529. return 0;
  8530. }
  8531. }
  8532. #endif
  8533. #ifdef TARGET_NR_getrlimit
  8534. case TARGET_NR_getrlimit:
  8535. {
  8536. int resource = target_to_host_resource(arg1);
  8537. struct target_rlimit *target_rlim;
  8538. struct rlimit rlim;
  8539. ret = get_errno(getrlimit(resource, &rlim));
  8540. if (!is_error(ret)) {
  8541. if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
  8542. return -TARGET_EFAULT;
  8543. target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
  8544. target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
  8545. unlock_user_struct(target_rlim, arg2, 1);
  8546. }
  8547. }
  8548. return ret;
  8549. #endif
  8550. case TARGET_NR_getrusage:
  8551. {
  8552. struct rusage rusage;
  8553. ret = get_errno(getrusage(arg1, &rusage));
  8554. if (!is_error(ret)) {
  8555. ret = host_to_target_rusage(arg2, &rusage);
  8556. }
  8557. }
  8558. return ret;
  8559. #if defined(TARGET_NR_gettimeofday)
  8560. case TARGET_NR_gettimeofday:
  8561. {
  8562. struct timeval tv;
  8563. struct timezone tz;
  8564. ret = get_errno(gettimeofday(&tv, &tz));
  8565. if (!is_error(ret)) {
  8566. if (arg1 && copy_to_user_timeval(arg1, &tv)) {
  8567. return -TARGET_EFAULT;
  8568. }
  8569. if (arg2 && copy_to_user_timezone(arg2, &tz)) {
  8570. return -TARGET_EFAULT;
  8571. }
  8572. }
  8573. }
  8574. return ret;
  8575. #endif
  8576. #if defined(TARGET_NR_settimeofday)
  8577. case TARGET_NR_settimeofday:
  8578. {
  8579. struct timeval tv, *ptv = NULL;
  8580. struct timezone tz, *ptz = NULL;
  8581. if (arg1) {
  8582. if (copy_from_user_timeval(&tv, arg1)) {
  8583. return -TARGET_EFAULT;
  8584. }
  8585. ptv = &tv;
  8586. }
  8587. if (arg2) {
  8588. if (copy_from_user_timezone(&tz, arg2)) {
  8589. return -TARGET_EFAULT;
  8590. }
  8591. ptz = &tz;
  8592. }
  8593. return get_errno(settimeofday(ptv, ptz));
  8594. }
  8595. #endif
  8596. #if defined(TARGET_NR_select)
  8597. case TARGET_NR_select:
  8598. #if defined(TARGET_WANT_NI_OLD_SELECT)
  8599. /* some architectures used to have old_select here
  8600. * but now ENOSYS it.
  8601. */
  8602. ret = -TARGET_ENOSYS;
  8603. #elif defined(TARGET_WANT_OLD_SYS_SELECT)
  8604. ret = do_old_select(arg1);
  8605. #else
  8606. ret = do_select(arg1, arg2, arg3, arg4, arg5);
  8607. #endif
  8608. return ret;
  8609. #endif
  8610. #ifdef TARGET_NR_pselect6
  8611. case TARGET_NR_pselect6:
  8612. return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, false);
  8613. #endif
  8614. #ifdef TARGET_NR_pselect6_time64
  8615. case TARGET_NR_pselect6_time64:
  8616. return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, true);
  8617. #endif
  8618. #ifdef TARGET_NR_symlink
  8619. case TARGET_NR_symlink:
  8620. {
  8621. void *p2;
  8622. p = lock_user_string(arg1);
  8623. p2 = lock_user_string(arg2);
  8624. if (!p || !p2)
  8625. ret = -TARGET_EFAULT;
  8626. else
  8627. ret = get_errno(symlink(p, p2));
  8628. unlock_user(p2, arg2, 0);
  8629. unlock_user(p, arg1, 0);
  8630. }
  8631. return ret;
  8632. #endif
  8633. #if defined(TARGET_NR_symlinkat)
  8634. case TARGET_NR_symlinkat:
  8635. {
  8636. void *p2;
  8637. p = lock_user_string(arg1);
  8638. p2 = lock_user_string(arg3);
  8639. if (!p || !p2)
  8640. ret = -TARGET_EFAULT;
  8641. else
  8642. ret = get_errno(symlinkat(p, arg2, p2));
  8643. unlock_user(p2, arg3, 0);
  8644. unlock_user(p, arg1, 0);
  8645. }
  8646. return ret;
  8647. #endif
  8648. #ifdef TARGET_NR_readlink
  8649. case TARGET_NR_readlink:
  8650. {
  8651. void *p2;
  8652. p = lock_user_string(arg1);
  8653. p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  8654. if (!p || !p2) {
  8655. ret = -TARGET_EFAULT;
  8656. } else if (!arg3) {
  8657. /* Short circuit this for the magic exe check. */
  8658. ret = -TARGET_EINVAL;
  8659. } else if (is_proc_myself((const char *)p, "exe")) {
  8660. char real[PATH_MAX], *temp;
  8661. temp = realpath(exec_path, real);
  8662. /* Return value is # of bytes that we wrote to the buffer. */
  8663. if (temp == NULL) {
  8664. ret = get_errno(-1);
  8665. } else {
  8666. /* Don't worry about sign mismatch as earlier mapping
  8667. * logic would have thrown a bad address error. */
  8668. ret = MIN(strlen(real), arg3);
  8669. /* We cannot NUL terminate the string. */
  8670. memcpy(p2, real, ret);
  8671. }
  8672. } else {
  8673. ret = get_errno(readlink(path(p), p2, arg3));
  8674. }
  8675. unlock_user(p2, arg2, ret);
  8676. unlock_user(p, arg1, 0);
  8677. }
  8678. return ret;
  8679. #endif
  8680. #if defined(TARGET_NR_readlinkat)
  8681. case TARGET_NR_readlinkat:
  8682. {
  8683. void *p2;
  8684. p = lock_user_string(arg2);
  8685. p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  8686. if (!p || !p2) {
  8687. ret = -TARGET_EFAULT;
  8688. } else if (is_proc_myself((const char *)p, "exe")) {
  8689. char real[PATH_MAX], *temp;
  8690. temp = realpath(exec_path, real);
  8691. ret = temp == NULL ? get_errno(-1) : strlen(real) ;
  8692. snprintf((char *)p2, arg4, "%s", real);
  8693. } else {
  8694. ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
  8695. }
  8696. unlock_user(p2, arg3, ret);
  8697. unlock_user(p, arg2, 0);
  8698. }
  8699. return ret;
  8700. #endif
  8701. #ifdef TARGET_NR_swapon
  8702. case TARGET_NR_swapon:
  8703. if (!(p = lock_user_string(arg1)))
  8704. return -TARGET_EFAULT;
  8705. ret = get_errno(swapon(p, arg2));
  8706. unlock_user(p, arg1, 0);
  8707. return ret;
  8708. #endif
  8709. case TARGET_NR_reboot:
  8710. if (arg3 == LINUX_REBOOT_CMD_RESTART2) {
  8711. /* arg4 must be ignored in all other cases */
  8712. p = lock_user_string(arg4);
  8713. if (!p) {
  8714. return -TARGET_EFAULT;
  8715. }
  8716. ret = get_errno(reboot(arg1, arg2, arg3, p));
  8717. unlock_user(p, arg4, 0);
  8718. } else {
  8719. ret = get_errno(reboot(arg1, arg2, arg3, NULL));
  8720. }
  8721. return ret;
  8722. #ifdef TARGET_NR_mmap
  8723. case TARGET_NR_mmap:
  8724. #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
  8725. (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
  8726. defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
  8727. || defined(TARGET_S390X)
  8728. {
  8729. abi_ulong *v;
  8730. abi_ulong v1, v2, v3, v4, v5, v6;
  8731. if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
  8732. return -TARGET_EFAULT;
  8733. v1 = tswapal(v[0]);
  8734. v2 = tswapal(v[1]);
  8735. v3 = tswapal(v[2]);
  8736. v4 = tswapal(v[3]);
  8737. v5 = tswapal(v[4]);
  8738. v6 = tswapal(v[5]);
  8739. unlock_user(v, arg1, 0);
  8740. ret = get_errno(target_mmap(v1, v2, v3,
  8741. target_to_host_bitmask(v4, mmap_flags_tbl),
  8742. v5, v6));
  8743. }
  8744. #else
  8745. ret = get_errno(target_mmap(arg1, arg2, arg3,
  8746. target_to_host_bitmask(arg4, mmap_flags_tbl),
  8747. arg5,
  8748. arg6));
  8749. #endif
  8750. return ret;
  8751. #endif
  8752. #ifdef TARGET_NR_mmap2
  8753. case TARGET_NR_mmap2:
  8754. #ifndef MMAP_SHIFT
  8755. #define MMAP_SHIFT 12
  8756. #endif
  8757. ret = target_mmap(arg1, arg2, arg3,
  8758. target_to_host_bitmask(arg4, mmap_flags_tbl),
  8759. arg5, arg6 << MMAP_SHIFT);
  8760. return get_errno(ret);
  8761. #endif
  8762. case TARGET_NR_munmap:
  8763. return get_errno(target_munmap(arg1, arg2));
  8764. case TARGET_NR_mprotect:
  8765. {
  8766. TaskState *ts = cpu->opaque;
  8767. /* Special hack to detect libc making the stack executable. */
  8768. if ((arg3 & PROT_GROWSDOWN)
  8769. && arg1 >= ts->info->stack_limit
  8770. && arg1 <= ts->info->start_stack) {
  8771. arg3 &= ~PROT_GROWSDOWN;
  8772. arg2 = arg2 + arg1 - ts->info->stack_limit;
  8773. arg1 = ts->info->stack_limit;
  8774. }
  8775. }
  8776. return get_errno(target_mprotect(arg1, arg2, arg3));
  8777. #ifdef TARGET_NR_mremap
  8778. case TARGET_NR_mremap:
  8779. return get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
  8780. #endif
  8781. /* ??? msync/mlock/munlock are broken for softmmu. */
  8782. #ifdef TARGET_NR_msync
  8783. case TARGET_NR_msync:
  8784. return get_errno(msync(g2h(arg1), arg2, arg3));
  8785. #endif
  8786. #ifdef TARGET_NR_mlock
  8787. case TARGET_NR_mlock:
  8788. return get_errno(mlock(g2h(arg1), arg2));
  8789. #endif
  8790. #ifdef TARGET_NR_munlock
  8791. case TARGET_NR_munlock:
  8792. return get_errno(munlock(g2h(arg1), arg2));
  8793. #endif
  8794. #ifdef TARGET_NR_mlockall
  8795. case TARGET_NR_mlockall:
  8796. return get_errno(mlockall(target_to_host_mlockall_arg(arg1)));
  8797. #endif
  8798. #ifdef TARGET_NR_munlockall
  8799. case TARGET_NR_munlockall:
  8800. return get_errno(munlockall());
  8801. #endif
  8802. #ifdef TARGET_NR_truncate
  8803. case TARGET_NR_truncate:
  8804. if (!(p = lock_user_string(arg1)))
  8805. return -TARGET_EFAULT;
  8806. ret = get_errno(truncate(p, arg2));
  8807. unlock_user(p, arg1, 0);
  8808. return ret;
  8809. #endif
  8810. #ifdef TARGET_NR_ftruncate
  8811. case TARGET_NR_ftruncate:
  8812. return get_errno(ftruncate(arg1, arg2));
  8813. #endif
  8814. case TARGET_NR_fchmod:
  8815. return get_errno(fchmod(arg1, arg2));
  8816. #if defined(TARGET_NR_fchmodat)
  8817. case TARGET_NR_fchmodat:
  8818. if (!(p = lock_user_string(arg2)))
  8819. return -TARGET_EFAULT;
  8820. ret = get_errno(fchmodat(arg1, p, arg3, 0));
  8821. unlock_user(p, arg2, 0);
  8822. return ret;
  8823. #endif
  8824. case TARGET_NR_getpriority:
  8825. /* Note that negative values are valid for getpriority, so we must
  8826. differentiate based on errno settings. */
  8827. errno = 0;
  8828. ret = getpriority(arg1, arg2);
  8829. if (ret == -1 && errno != 0) {
  8830. return -host_to_target_errno(errno);
  8831. }
  8832. #ifdef TARGET_ALPHA
  8833. /* Return value is the unbiased priority. Signal no error. */
  8834. ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0;
  8835. #else
  8836. /* Return value is a biased priority to avoid negative numbers. */
  8837. ret = 20 - ret;
  8838. #endif
  8839. return ret;
  8840. case TARGET_NR_setpriority:
  8841. return get_errno(setpriority(arg1, arg2, arg3));
  8842. #ifdef TARGET_NR_statfs
  8843. case TARGET_NR_statfs:
  8844. if (!(p = lock_user_string(arg1))) {
  8845. return -TARGET_EFAULT;
  8846. }
  8847. ret = get_errno(statfs(path(p), &stfs));
  8848. unlock_user(p, arg1, 0);
  8849. convert_statfs:
  8850. if (!is_error(ret)) {
  8851. struct target_statfs *target_stfs;
  8852. if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
  8853. return -TARGET_EFAULT;
  8854. __put_user(stfs.f_type, &target_stfs->f_type);
  8855. __put_user(stfs.f_bsize, &target_stfs->f_bsize);
  8856. __put_user(stfs.f_blocks, &target_stfs->f_blocks);
  8857. __put_user(stfs.f_bfree, &target_stfs->f_bfree);
  8858. __put_user(stfs.f_bavail, &target_stfs->f_bavail);
  8859. __put_user(stfs.f_files, &target_stfs->f_files);
  8860. __put_user(stfs.f_ffree, &target_stfs->f_ffree);
  8861. __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
  8862. __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
  8863. __put_user(stfs.f_namelen, &target_stfs->f_namelen);
  8864. __put_user(stfs.f_frsize, &target_stfs->f_frsize);
  8865. #ifdef _STATFS_F_FLAGS
  8866. __put_user(stfs.f_flags, &target_stfs->f_flags);
  8867. #else
  8868. __put_user(0, &target_stfs->f_flags);
  8869. #endif
  8870. memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
  8871. unlock_user_struct(target_stfs, arg2, 1);
  8872. }
  8873. return ret;
  8874. #endif
  8875. #ifdef TARGET_NR_fstatfs
  8876. case TARGET_NR_fstatfs:
  8877. ret = get_errno(fstatfs(arg1, &stfs));
  8878. goto convert_statfs;
  8879. #endif
  8880. #ifdef TARGET_NR_statfs64
  8881. case TARGET_NR_statfs64:
  8882. if (!(p = lock_user_string(arg1))) {
  8883. return -TARGET_EFAULT;
  8884. }
  8885. ret = get_errno(statfs(path(p), &stfs));
  8886. unlock_user(p, arg1, 0);
  8887. convert_statfs64:
  8888. if (!is_error(ret)) {
  8889. struct target_statfs64 *target_stfs;
  8890. if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
  8891. return -TARGET_EFAULT;
  8892. __put_user(stfs.f_type, &target_stfs->f_type);
  8893. __put_user(stfs.f_bsize, &target_stfs->f_bsize);
  8894. __put_user(stfs.f_blocks, &target_stfs->f_blocks);
  8895. __put_user(stfs.f_bfree, &target_stfs->f_bfree);
  8896. __put_user(stfs.f_bavail, &target_stfs->f_bavail);
  8897. __put_user(stfs.f_files, &target_stfs->f_files);
  8898. __put_user(stfs.f_ffree, &target_stfs->f_ffree);
  8899. __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
  8900. __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
  8901. __put_user(stfs.f_namelen, &target_stfs->f_namelen);
  8902. __put_user(stfs.f_frsize, &target_stfs->f_frsize);
  8903. memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
  8904. unlock_user_struct(target_stfs, arg3, 1);
  8905. }
  8906. return ret;
  8907. case TARGET_NR_fstatfs64:
  8908. ret = get_errno(fstatfs(arg1, &stfs));
  8909. goto convert_statfs64;
  8910. #endif
  8911. #ifdef TARGET_NR_socketcall
  8912. case TARGET_NR_socketcall:
  8913. return do_socketcall(arg1, arg2);
  8914. #endif
  8915. #ifdef TARGET_NR_accept
  8916. case TARGET_NR_accept:
  8917. return do_accept4(arg1, arg2, arg3, 0);
  8918. #endif
  8919. #ifdef TARGET_NR_accept4
  8920. case TARGET_NR_accept4:
  8921. return do_accept4(arg1, arg2, arg3, arg4);
  8922. #endif
  8923. #ifdef TARGET_NR_bind
  8924. case TARGET_NR_bind:
  8925. return do_bind(arg1, arg2, arg3);
  8926. #endif
  8927. #ifdef TARGET_NR_connect
  8928. case TARGET_NR_connect:
  8929. return do_connect(arg1, arg2, arg3);
  8930. #endif
  8931. #ifdef TARGET_NR_getpeername
  8932. case TARGET_NR_getpeername:
  8933. return do_getpeername(arg1, arg2, arg3);
  8934. #endif
  8935. #ifdef TARGET_NR_getsockname
  8936. case TARGET_NR_getsockname:
  8937. return do_getsockname(arg1, arg2, arg3);
  8938. #endif
  8939. #ifdef TARGET_NR_getsockopt
  8940. case TARGET_NR_getsockopt:
  8941. return do_getsockopt(arg1, arg2, arg3, arg4, arg5);
  8942. #endif
  8943. #ifdef TARGET_NR_listen
  8944. case TARGET_NR_listen:
  8945. return get_errno(listen(arg1, arg2));
  8946. #endif
  8947. #ifdef TARGET_NR_recv
  8948. case TARGET_NR_recv:
  8949. return do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
  8950. #endif
  8951. #ifdef TARGET_NR_recvfrom
  8952. case TARGET_NR_recvfrom:
  8953. return do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
  8954. #endif
  8955. #ifdef TARGET_NR_recvmsg
  8956. case TARGET_NR_recvmsg:
  8957. return do_sendrecvmsg(arg1, arg2, arg3, 0);
  8958. #endif
  8959. #ifdef TARGET_NR_send
  8960. case TARGET_NR_send:
  8961. return do_sendto(arg1, arg2, arg3, arg4, 0, 0);
  8962. #endif
  8963. #ifdef TARGET_NR_sendmsg
  8964. case TARGET_NR_sendmsg:
  8965. return do_sendrecvmsg(arg1, arg2, arg3, 1);
  8966. #endif
  8967. #ifdef TARGET_NR_sendmmsg
  8968. case TARGET_NR_sendmmsg:
  8969. return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 1);
  8970. #endif
  8971. #ifdef TARGET_NR_recvmmsg
  8972. case TARGET_NR_recvmmsg:
  8973. return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 0);
  8974. #endif
  8975. #ifdef TARGET_NR_sendto
  8976. case TARGET_NR_sendto:
  8977. return do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
  8978. #endif
  8979. #ifdef TARGET_NR_shutdown
  8980. case TARGET_NR_shutdown:
  8981. return get_errno(shutdown(arg1, arg2));
  8982. #endif
  8983. #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
  8984. case TARGET_NR_getrandom:
  8985. p = lock_user(VERIFY_WRITE, arg1, arg2, 0);
  8986. if (!p) {
  8987. return -TARGET_EFAULT;
  8988. }
  8989. ret = get_errno(getrandom(p, arg2, arg3));
  8990. unlock_user(p, arg1, ret);
  8991. return ret;
  8992. #endif
  8993. #ifdef TARGET_NR_socket
  8994. case TARGET_NR_socket:
  8995. return do_socket(arg1, arg2, arg3);
  8996. #endif
  8997. #ifdef TARGET_NR_socketpair
  8998. case TARGET_NR_socketpair:
  8999. return do_socketpair(arg1, arg2, arg3, arg4);
  9000. #endif
  9001. #ifdef TARGET_NR_setsockopt
  9002. case TARGET_NR_setsockopt:
  9003. return do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
  9004. #endif
  9005. #if defined(TARGET_NR_syslog)
  9006. case TARGET_NR_syslog:
  9007. {
  9008. int len = arg2;
  9009. switch (arg1) {
  9010. case TARGET_SYSLOG_ACTION_CLOSE: /* Close log */
  9011. case TARGET_SYSLOG_ACTION_OPEN: /* Open log */
  9012. case TARGET_SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
  9013. case TARGET_SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging */
  9014. case TARGET_SYSLOG_ACTION_CONSOLE_ON: /* Enable logging */
  9015. case TARGET_SYSLOG_ACTION_CONSOLE_LEVEL: /* Set messages level */
  9016. case TARGET_SYSLOG_ACTION_SIZE_UNREAD: /* Number of chars */
  9017. case TARGET_SYSLOG_ACTION_SIZE_BUFFER: /* Size of the buffer */
  9018. return get_errno(sys_syslog((int)arg1, NULL, (int)arg3));
  9019. case TARGET_SYSLOG_ACTION_READ: /* Read from log */
  9020. case TARGET_SYSLOG_ACTION_READ_CLEAR: /* Read/clear msgs */
  9021. case TARGET_SYSLOG_ACTION_READ_ALL: /* Read last messages */
  9022. {
  9023. if (len < 0) {
  9024. return -TARGET_EINVAL;
  9025. }
  9026. if (len == 0) {
  9027. return 0;
  9028. }
  9029. p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  9030. if (!p) {
  9031. return -TARGET_EFAULT;
  9032. }
  9033. ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
  9034. unlock_user(p, arg2, arg3);
  9035. }
  9036. return ret;
  9037. default:
  9038. return -TARGET_EINVAL;
  9039. }
  9040. }
  9041. break;
  9042. #endif
  9043. case TARGET_NR_setitimer:
  9044. {
  9045. struct itimerval value, ovalue, *pvalue;
  9046. if (arg2) {
  9047. pvalue = &value;
  9048. if (copy_from_user_timeval(&pvalue->it_interval, arg2)
  9049. || copy_from_user_timeval(&pvalue->it_value,
  9050. arg2 + sizeof(struct target_timeval)))
  9051. return -TARGET_EFAULT;
  9052. } else {
  9053. pvalue = NULL;
  9054. }
  9055. ret = get_errno(setitimer(arg1, pvalue, &ovalue));
  9056. if (!is_error(ret) && arg3) {
  9057. if (copy_to_user_timeval(arg3,
  9058. &ovalue.it_interval)
  9059. || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
  9060. &ovalue.it_value))
  9061. return -TARGET_EFAULT;
  9062. }
  9063. }
  9064. return ret;
  9065. case TARGET_NR_getitimer:
  9066. {
  9067. struct itimerval value;
  9068. ret = get_errno(getitimer(arg1, &value));
  9069. if (!is_error(ret) && arg2) {
  9070. if (copy_to_user_timeval(arg2,
  9071. &value.it_interval)
  9072. || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
  9073. &value.it_value))
  9074. return -TARGET_EFAULT;
  9075. }
  9076. }
  9077. return ret;
  9078. #ifdef TARGET_NR_stat
  9079. case TARGET_NR_stat:
  9080. if (!(p = lock_user_string(arg1))) {
  9081. return -TARGET_EFAULT;
  9082. }
  9083. ret = get_errno(stat(path(p), &st));
  9084. unlock_user(p, arg1, 0);
  9085. goto do_stat;
  9086. #endif
  9087. #ifdef TARGET_NR_lstat
  9088. case TARGET_NR_lstat:
  9089. if (!(p = lock_user_string(arg1))) {
  9090. return -TARGET_EFAULT;
  9091. }
  9092. ret = get_errno(lstat(path(p), &st));
  9093. unlock_user(p, arg1, 0);
  9094. goto do_stat;
  9095. #endif
  9096. #ifdef TARGET_NR_fstat
  9097. case TARGET_NR_fstat:
  9098. {
  9099. ret = get_errno(fstat(arg1, &st));
  9100. #if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat)
  9101. do_stat:
  9102. #endif
  9103. if (!is_error(ret)) {
  9104. struct target_stat *target_st;
  9105. if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
  9106. return -TARGET_EFAULT;
  9107. memset(target_st, 0, sizeof(*target_st));
  9108. __put_user(st.st_dev, &target_st->st_dev);
  9109. __put_user(st.st_ino, &target_st->st_ino);
  9110. __put_user(st.st_mode, &target_st->st_mode);
  9111. __put_user(st.st_uid, &target_st->st_uid);
  9112. __put_user(st.st_gid, &target_st->st_gid);
  9113. __put_user(st.st_nlink, &target_st->st_nlink);
  9114. __put_user(st.st_rdev, &target_st->st_rdev);
  9115. __put_user(st.st_size, &target_st->st_size);
  9116. __put_user(st.st_blksize, &target_st->st_blksize);
  9117. __put_user(st.st_blocks, &target_st->st_blocks);
  9118. __put_user(st.st_atime, &target_st->target_st_atime);
  9119. __put_user(st.st_mtime, &target_st->target_st_mtime);
  9120. __put_user(st.st_ctime, &target_st->target_st_ctime);
  9121. #if (_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700) && \
  9122. defined(TARGET_STAT_HAVE_NSEC)
  9123. __put_user(st.st_atim.tv_nsec,
  9124. &target_st->target_st_atime_nsec);
  9125. __put_user(st.st_mtim.tv_nsec,
  9126. &target_st->target_st_mtime_nsec);
  9127. __put_user(st.st_ctim.tv_nsec,
  9128. &target_st->target_st_ctime_nsec);
  9129. #endif
  9130. unlock_user_struct(target_st, arg2, 1);
  9131. }
  9132. }
  9133. return ret;
  9134. #endif
  9135. case TARGET_NR_vhangup:
  9136. return get_errno(vhangup());
  9137. #ifdef TARGET_NR_syscall
  9138. case TARGET_NR_syscall:
  9139. return do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
  9140. arg6, arg7, arg8, 0);
  9141. #endif
  9142. #if defined(TARGET_NR_wait4)
  9143. case TARGET_NR_wait4:
  9144. {
  9145. int status;
  9146. abi_long status_ptr = arg2;
  9147. struct rusage rusage, *rusage_ptr;
  9148. abi_ulong target_rusage = arg4;
  9149. abi_long rusage_err;
  9150. if (target_rusage)
  9151. rusage_ptr = &rusage;
  9152. else
  9153. rusage_ptr = NULL;
  9154. ret = get_errno(safe_wait4(arg1, &status, arg3, rusage_ptr));
  9155. if (!is_error(ret)) {
  9156. if (status_ptr && ret) {
  9157. status = host_to_target_waitstatus(status);
  9158. if (put_user_s32(status, status_ptr))
  9159. return -TARGET_EFAULT;
  9160. }
  9161. if (target_rusage) {
  9162. rusage_err = host_to_target_rusage(target_rusage, &rusage);
  9163. if (rusage_err) {
  9164. ret = rusage_err;
  9165. }
  9166. }
  9167. }
  9168. }
  9169. return ret;
  9170. #endif
  9171. #ifdef TARGET_NR_swapoff
  9172. case TARGET_NR_swapoff:
  9173. if (!(p = lock_user_string(arg1)))
  9174. return -TARGET_EFAULT;
  9175. ret = get_errno(swapoff(p));
  9176. unlock_user(p, arg1, 0);
  9177. return ret;
  9178. #endif
  9179. case TARGET_NR_sysinfo:
  9180. {
  9181. struct target_sysinfo *target_value;
  9182. struct sysinfo value;
  9183. ret = get_errno(sysinfo(&value));
  9184. if (!is_error(ret) && arg1)
  9185. {
  9186. if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
  9187. return -TARGET_EFAULT;
  9188. __put_user(value.uptime, &target_value->uptime);
  9189. __put_user(value.loads[0], &target_value->loads[0]);
  9190. __put_user(value.loads[1], &target_value->loads[1]);
  9191. __put_user(value.loads[2], &target_value->loads[2]);
  9192. __put_user(value.totalram, &target_value->totalram);
  9193. __put_user(value.freeram, &target_value->freeram);
  9194. __put_user(value.sharedram, &target_value->sharedram);
  9195. __put_user(value.bufferram, &target_value->bufferram);
  9196. __put_user(value.totalswap, &target_value->totalswap);
  9197. __put_user(value.freeswap, &target_value->freeswap);
  9198. __put_user(value.procs, &target_value->procs);
  9199. __put_user(value.totalhigh, &target_value->totalhigh);
  9200. __put_user(value.freehigh, &target_value->freehigh);
  9201. __put_user(value.mem_unit, &target_value->mem_unit);
  9202. unlock_user_struct(target_value, arg1, 1);
  9203. }
  9204. }
  9205. return ret;
  9206. #ifdef TARGET_NR_ipc
  9207. case TARGET_NR_ipc:
  9208. return do_ipc(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6);
  9209. #endif
  9210. #ifdef TARGET_NR_semget
  9211. case TARGET_NR_semget:
  9212. return get_errno(semget(arg1, arg2, arg3));
  9213. #endif
  9214. #ifdef TARGET_NR_semop
  9215. case TARGET_NR_semop:
  9216. return do_semtimedop(arg1, arg2, arg3, 0, false);
  9217. #endif
  9218. #ifdef TARGET_NR_semtimedop
  9219. case TARGET_NR_semtimedop:
  9220. return do_semtimedop(arg1, arg2, arg3, arg4, false);
  9221. #endif
  9222. #ifdef TARGET_NR_semtimedop_time64
  9223. case TARGET_NR_semtimedop_time64:
  9224. return do_semtimedop(arg1, arg2, arg3, arg4, true);
  9225. #endif
  9226. #ifdef TARGET_NR_semctl
  9227. case TARGET_NR_semctl:
  9228. return do_semctl(arg1, arg2, arg3, arg4);
  9229. #endif
  9230. #ifdef TARGET_NR_msgctl
  9231. case TARGET_NR_msgctl:
  9232. return do_msgctl(arg1, arg2, arg3);
  9233. #endif
  9234. #ifdef TARGET_NR_msgget
  9235. case TARGET_NR_msgget:
  9236. return get_errno(msgget(arg1, arg2));
  9237. #endif
  9238. #ifdef TARGET_NR_msgrcv
  9239. case TARGET_NR_msgrcv:
  9240. return do_msgrcv(arg1, arg2, arg3, arg4, arg5);
  9241. #endif
  9242. #ifdef TARGET_NR_msgsnd
  9243. case TARGET_NR_msgsnd:
  9244. return do_msgsnd(arg1, arg2, arg3, arg4);
  9245. #endif
  9246. #ifdef TARGET_NR_shmget
  9247. case TARGET_NR_shmget:
  9248. return get_errno(shmget(arg1, arg2, arg3));
  9249. #endif
  9250. #ifdef TARGET_NR_shmctl
  9251. case TARGET_NR_shmctl:
  9252. return do_shmctl(arg1, arg2, arg3);
  9253. #endif
  9254. #ifdef TARGET_NR_shmat
  9255. case TARGET_NR_shmat:
  9256. return do_shmat(cpu_env, arg1, arg2, arg3);
  9257. #endif
  9258. #ifdef TARGET_NR_shmdt
  9259. case TARGET_NR_shmdt:
  9260. return do_shmdt(arg1);
  9261. #endif
  9262. case TARGET_NR_fsync:
  9263. return get_errno(fsync(arg1));
  9264. case TARGET_NR_clone:
  9265. /* Linux manages to have three different orderings for its
  9266. * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
  9267. * match the kernel's CONFIG_CLONE_* settings.
  9268. * Microblaze is further special in that it uses a sixth
  9269. * implicit argument to clone for the TLS pointer.
  9270. */
  9271. #if defined(TARGET_MICROBLAZE)
  9272. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5));
  9273. #elif defined(TARGET_CLONE_BACKWARDS)
  9274. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
  9275. #elif defined(TARGET_CLONE_BACKWARDS2)
  9276. ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
  9277. #else
  9278. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
  9279. #endif
  9280. return ret;
  9281. #ifdef __NR_exit_group
  9282. /* new thread calls */
  9283. case TARGET_NR_exit_group:
  9284. preexit_cleanup(cpu_env, arg1);
  9285. return get_errno(exit_group(arg1));
  9286. #endif
  9287. case TARGET_NR_setdomainname:
  9288. if (!(p = lock_user_string(arg1)))
  9289. return -TARGET_EFAULT;
  9290. ret = get_errno(setdomainname(p, arg2));
  9291. unlock_user(p, arg1, 0);
  9292. return ret;
  9293. case TARGET_NR_uname:
  9294. /* no need to transcode because we use the linux syscall */
  9295. {
  9296. struct new_utsname * buf;
  9297. if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
  9298. return -TARGET_EFAULT;
  9299. ret = get_errno(sys_uname(buf));
  9300. if (!is_error(ret)) {
  9301. /* Overwrite the native machine name with whatever is being
  9302. emulated. */
  9303. g_strlcpy(buf->machine, cpu_to_uname_machine(cpu_env),
  9304. sizeof(buf->machine));
  9305. /* Allow the user to override the reported release. */
  9306. if (qemu_uname_release && *qemu_uname_release) {
  9307. g_strlcpy(buf->release, qemu_uname_release,
  9308. sizeof(buf->release));
  9309. }
  9310. }
  9311. unlock_user_struct(buf, arg1, 1);
  9312. }
  9313. return ret;
  9314. #ifdef TARGET_I386
  9315. case TARGET_NR_modify_ldt:
  9316. return do_modify_ldt(cpu_env, arg1, arg2, arg3);
  9317. #if !defined(TARGET_X86_64)
  9318. case TARGET_NR_vm86:
  9319. return do_vm86(cpu_env, arg1, arg2);
  9320. #endif
  9321. #endif
  9322. #if defined(TARGET_NR_adjtimex)
  9323. case TARGET_NR_adjtimex:
  9324. {
  9325. struct timex host_buf;
  9326. if (target_to_host_timex(&host_buf, arg1) != 0) {
  9327. return -TARGET_EFAULT;
  9328. }
  9329. ret = get_errno(adjtimex(&host_buf));
  9330. if (!is_error(ret)) {
  9331. if (host_to_target_timex(arg1, &host_buf) != 0) {
  9332. return -TARGET_EFAULT;
  9333. }
  9334. }
  9335. }
  9336. return ret;
  9337. #endif
  9338. #if defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME)
  9339. case TARGET_NR_clock_adjtime:
  9340. {
  9341. struct timex htx, *phtx = &htx;
  9342. if (target_to_host_timex(phtx, arg2) != 0) {
  9343. return -TARGET_EFAULT;
  9344. }
  9345. ret = get_errno(clock_adjtime(arg1, phtx));
  9346. if (!is_error(ret) && phtx) {
  9347. if (host_to_target_timex(arg2, phtx) != 0) {
  9348. return -TARGET_EFAULT;
  9349. }
  9350. }
  9351. }
  9352. return ret;
  9353. #endif
  9354. #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
  9355. case TARGET_NR_clock_adjtime64:
  9356. {
  9357. struct timex htx;
  9358. if (target_to_host_timex64(&htx, arg2) != 0) {
  9359. return -TARGET_EFAULT;
  9360. }
  9361. ret = get_errno(clock_adjtime(arg1, &htx));
  9362. if (!is_error(ret) && host_to_target_timex64(arg2, &htx)) {
  9363. return -TARGET_EFAULT;
  9364. }
  9365. }
  9366. return ret;
  9367. #endif
  9368. case TARGET_NR_getpgid:
  9369. return get_errno(getpgid(arg1));
  9370. case TARGET_NR_fchdir:
  9371. return get_errno(fchdir(arg1));
  9372. case TARGET_NR_personality:
  9373. return get_errno(personality(arg1));
  9374. #ifdef TARGET_NR__llseek /* Not on alpha */
  9375. case TARGET_NR__llseek:
  9376. {
  9377. int64_t res;
  9378. #if !defined(__NR_llseek)
  9379. res = lseek(arg1, ((uint64_t)arg2 << 32) | (abi_ulong)arg3, arg5);
  9380. if (res == -1) {
  9381. ret = get_errno(res);
  9382. } else {
  9383. ret = 0;
  9384. }
  9385. #else
  9386. ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
  9387. #endif
  9388. if ((ret == 0) && put_user_s64(res, arg4)) {
  9389. return -TARGET_EFAULT;
  9390. }
  9391. }
  9392. return ret;
  9393. #endif
  9394. #ifdef TARGET_NR_getdents
  9395. case TARGET_NR_getdents:
  9396. #ifdef EMULATE_GETDENTS_WITH_GETDENTS
  9397. #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
  9398. {
  9399. struct target_dirent *target_dirp;
  9400. struct linux_dirent *dirp;
  9401. abi_long count = arg3;
  9402. dirp = g_try_malloc(count);
  9403. if (!dirp) {
  9404. return -TARGET_ENOMEM;
  9405. }
  9406. ret = get_errno(sys_getdents(arg1, dirp, count));
  9407. if (!is_error(ret)) {
  9408. struct linux_dirent *de;
  9409. struct target_dirent *tde;
  9410. int len = ret;
  9411. int reclen, treclen;
  9412. int count1, tnamelen;
  9413. count1 = 0;
  9414. de = dirp;
  9415. if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
  9416. return -TARGET_EFAULT;
  9417. tde = target_dirp;
  9418. while (len > 0) {
  9419. reclen = de->d_reclen;
  9420. tnamelen = reclen - offsetof(struct linux_dirent, d_name);
  9421. assert(tnamelen >= 0);
  9422. treclen = tnamelen + offsetof(struct target_dirent, d_name);
  9423. assert(count1 + treclen <= count);
  9424. tde->d_reclen = tswap16(treclen);
  9425. tde->d_ino = tswapal(de->d_ino);
  9426. tde->d_off = tswapal(de->d_off);
  9427. memcpy(tde->d_name, de->d_name, tnamelen);
  9428. de = (struct linux_dirent *)((char *)de + reclen);
  9429. len -= reclen;
  9430. tde = (struct target_dirent *)((char *)tde + treclen);
  9431. count1 += treclen;
  9432. }
  9433. ret = count1;
  9434. unlock_user(target_dirp, arg2, ret);
  9435. }
  9436. g_free(dirp);
  9437. }
  9438. #else
  9439. {
  9440. struct linux_dirent *dirp;
  9441. abi_long count = arg3;
  9442. if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
  9443. return -TARGET_EFAULT;
  9444. ret = get_errno(sys_getdents(arg1, dirp, count));
  9445. if (!is_error(ret)) {
  9446. struct linux_dirent *de;
  9447. int len = ret;
  9448. int reclen;
  9449. de = dirp;
  9450. while (len > 0) {
  9451. reclen = de->d_reclen;
  9452. if (reclen > len)
  9453. break;
  9454. de->d_reclen = tswap16(reclen);
  9455. tswapls(&de->d_ino);
  9456. tswapls(&de->d_off);
  9457. de = (struct linux_dirent *)((char *)de + reclen);
  9458. len -= reclen;
  9459. }
  9460. }
  9461. unlock_user(dirp, arg2, ret);
  9462. }
  9463. #endif
  9464. #else
  9465. /* Implement getdents in terms of getdents64 */
  9466. {
  9467. struct linux_dirent64 *dirp;
  9468. abi_long count = arg3;
  9469. dirp = lock_user(VERIFY_WRITE, arg2, count, 0);
  9470. if (!dirp) {
  9471. return -TARGET_EFAULT;
  9472. }
  9473. ret = get_errno(sys_getdents64(arg1, dirp, count));
  9474. if (!is_error(ret)) {
  9475. /* Convert the dirent64 structs to target dirent. We do this
  9476. * in-place, since we can guarantee that a target_dirent is no
  9477. * larger than a dirent64; however this means we have to be
  9478. * careful to read everything before writing in the new format.
  9479. */
  9480. struct linux_dirent64 *de;
  9481. struct target_dirent *tde;
  9482. int len = ret;
  9483. int tlen = 0;
  9484. de = dirp;
  9485. tde = (struct target_dirent *)dirp;
  9486. while (len > 0) {
  9487. int namelen, treclen;
  9488. int reclen = de->d_reclen;
  9489. uint64_t ino = de->d_ino;
  9490. int64_t off = de->d_off;
  9491. uint8_t type = de->d_type;
  9492. namelen = strlen(de->d_name);
  9493. treclen = offsetof(struct target_dirent, d_name)
  9494. + namelen + 2;
  9495. treclen = QEMU_ALIGN_UP(treclen, sizeof(abi_long));
  9496. memmove(tde->d_name, de->d_name, namelen + 1);
  9497. tde->d_ino = tswapal(ino);
  9498. tde->d_off = tswapal(off);
  9499. tde->d_reclen = tswap16(treclen);
  9500. /* The target_dirent type is in what was formerly a padding
  9501. * byte at the end of the structure:
  9502. */
  9503. *(((char *)tde) + treclen - 1) = type;
  9504. de = (struct linux_dirent64 *)((char *)de + reclen);
  9505. tde = (struct target_dirent *)((char *)tde + treclen);
  9506. len -= reclen;
  9507. tlen += treclen;
  9508. }
  9509. ret = tlen;
  9510. }
  9511. unlock_user(dirp, arg2, ret);
  9512. }
  9513. #endif
  9514. return ret;
  9515. #endif /* TARGET_NR_getdents */
  9516. #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
  9517. case TARGET_NR_getdents64:
  9518. {
  9519. struct linux_dirent64 *dirp;
  9520. abi_long count = arg3;
  9521. if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
  9522. return -TARGET_EFAULT;
  9523. ret = get_errno(sys_getdents64(arg1, dirp, count));
  9524. if (!is_error(ret)) {
  9525. struct linux_dirent64 *de;
  9526. int len = ret;
  9527. int reclen;
  9528. de = dirp;
  9529. while (len > 0) {
  9530. reclen = de->d_reclen;
  9531. if (reclen > len)
  9532. break;
  9533. de->d_reclen = tswap16(reclen);
  9534. tswap64s((uint64_t *)&de->d_ino);
  9535. tswap64s((uint64_t *)&de->d_off);
  9536. de = (struct linux_dirent64 *)((char *)de + reclen);
  9537. len -= reclen;
  9538. }
  9539. }
  9540. unlock_user(dirp, arg2, ret);
  9541. }
  9542. return ret;
  9543. #endif /* TARGET_NR_getdents64 */
  9544. #if defined(TARGET_NR__newselect)
  9545. case TARGET_NR__newselect:
  9546. return do_select(arg1, arg2, arg3, arg4, arg5);
  9547. #endif
  9548. #ifdef TARGET_NR_poll
  9549. case TARGET_NR_poll:
  9550. return do_ppoll(arg1, arg2, arg3, arg4, arg5, false, false);
  9551. #endif
  9552. #ifdef TARGET_NR_ppoll
  9553. case TARGET_NR_ppoll:
  9554. return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, false);
  9555. #endif
  9556. #ifdef TARGET_NR_ppoll_time64
  9557. case TARGET_NR_ppoll_time64:
  9558. return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, true);
  9559. #endif
  9560. case TARGET_NR_flock:
  9561. /* NOTE: the flock constant seems to be the same for every
  9562. Linux platform */
  9563. return get_errno(safe_flock(arg1, arg2));
  9564. case TARGET_NR_readv:
  9565. {
  9566. struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
  9567. if (vec != NULL) {
  9568. ret = get_errno(safe_readv(arg1, vec, arg3));
  9569. unlock_iovec(vec, arg2, arg3, 1);
  9570. } else {
  9571. ret = -host_to_target_errno(errno);
  9572. }
  9573. }
  9574. return ret;
  9575. case TARGET_NR_writev:
  9576. {
  9577. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  9578. if (vec != NULL) {
  9579. ret = get_errno(safe_writev(arg1, vec, arg3));
  9580. unlock_iovec(vec, arg2, arg3, 0);
  9581. } else {
  9582. ret = -host_to_target_errno(errno);
  9583. }
  9584. }
  9585. return ret;
  9586. #if defined(TARGET_NR_preadv)
  9587. case TARGET_NR_preadv:
  9588. {
  9589. struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
  9590. if (vec != NULL) {
  9591. unsigned long low, high;
  9592. target_to_host_low_high(arg4, arg5, &low, &high);
  9593. ret = get_errno(safe_preadv(arg1, vec, arg3, low, high));
  9594. unlock_iovec(vec, arg2, arg3, 1);
  9595. } else {
  9596. ret = -host_to_target_errno(errno);
  9597. }
  9598. }
  9599. return ret;
  9600. #endif
  9601. #if defined(TARGET_NR_pwritev)
  9602. case TARGET_NR_pwritev:
  9603. {
  9604. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  9605. if (vec != NULL) {
  9606. unsigned long low, high;
  9607. target_to_host_low_high(arg4, arg5, &low, &high);
  9608. ret = get_errno(safe_pwritev(arg1, vec, arg3, low, high));
  9609. unlock_iovec(vec, arg2, arg3, 0);
  9610. } else {
  9611. ret = -host_to_target_errno(errno);
  9612. }
  9613. }
  9614. return ret;
  9615. #endif
  9616. case TARGET_NR_getsid:
  9617. return get_errno(getsid(arg1));
  9618. #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
  9619. case TARGET_NR_fdatasync:
  9620. return get_errno(fdatasync(arg1));
  9621. #endif
  9622. #ifdef TARGET_NR__sysctl
  9623. case TARGET_NR__sysctl:
  9624. /* We don't implement this, but ENOTDIR is always a safe
  9625. return value. */
  9626. return -TARGET_ENOTDIR;
  9627. #endif
  9628. case TARGET_NR_sched_getaffinity:
  9629. {
  9630. unsigned int mask_size;
  9631. unsigned long *mask;
  9632. /*
  9633. * sched_getaffinity needs multiples of ulong, so need to take
  9634. * care of mismatches between target ulong and host ulong sizes.
  9635. */
  9636. if (arg2 & (sizeof(abi_ulong) - 1)) {
  9637. return -TARGET_EINVAL;
  9638. }
  9639. mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
  9640. mask = alloca(mask_size);
  9641. memset(mask, 0, mask_size);
  9642. ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
  9643. if (!is_error(ret)) {
  9644. if (ret > arg2) {
  9645. /* More data returned than the caller's buffer will fit.
  9646. * This only happens if sizeof(abi_long) < sizeof(long)
  9647. * and the caller passed us a buffer holding an odd number
  9648. * of abi_longs. If the host kernel is actually using the
  9649. * extra 4 bytes then fail EINVAL; otherwise we can just
  9650. * ignore them and only copy the interesting part.
  9651. */
  9652. int numcpus = sysconf(_SC_NPROCESSORS_CONF);
  9653. if (numcpus > arg2 * 8) {
  9654. return -TARGET_EINVAL;
  9655. }
  9656. ret = arg2;
  9657. }
  9658. if (host_to_target_cpu_mask(mask, mask_size, arg3, ret)) {
  9659. return -TARGET_EFAULT;
  9660. }
  9661. }
  9662. }
  9663. return ret;
  9664. case TARGET_NR_sched_setaffinity:
  9665. {
  9666. unsigned int mask_size;
  9667. unsigned long *mask;
  9668. /*
  9669. * sched_setaffinity needs multiples of ulong, so need to take
  9670. * care of mismatches between target ulong and host ulong sizes.
  9671. */
  9672. if (arg2 & (sizeof(abi_ulong) - 1)) {
  9673. return -TARGET_EINVAL;
  9674. }
  9675. mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
  9676. mask = alloca(mask_size);
  9677. ret = target_to_host_cpu_mask(mask, mask_size, arg3, arg2);
  9678. if (ret) {
  9679. return ret;
  9680. }
  9681. return get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
  9682. }
  9683. case TARGET_NR_getcpu:
  9684. {
  9685. unsigned cpu, node;
  9686. ret = get_errno(sys_getcpu(arg1 ? &cpu : NULL,
  9687. arg2 ? &node : NULL,
  9688. NULL));
  9689. if (is_error(ret)) {
  9690. return ret;
  9691. }
  9692. if (arg1 && put_user_u32(cpu, arg1)) {
  9693. return -TARGET_EFAULT;
  9694. }
  9695. if (arg2 && put_user_u32(node, arg2)) {
  9696. return -TARGET_EFAULT;
  9697. }
  9698. }
  9699. return ret;
  9700. case TARGET_NR_sched_setparam:
  9701. {
  9702. struct sched_param *target_schp;
  9703. struct sched_param schp;
  9704. if (arg2 == 0) {
  9705. return -TARGET_EINVAL;
  9706. }
  9707. if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
  9708. return -TARGET_EFAULT;
  9709. schp.sched_priority = tswap32(target_schp->sched_priority);
  9710. unlock_user_struct(target_schp, arg2, 0);
  9711. return get_errno(sched_setparam(arg1, &schp));
  9712. }
  9713. case TARGET_NR_sched_getparam:
  9714. {
  9715. struct sched_param *target_schp;
  9716. struct sched_param schp;
  9717. if (arg2 == 0) {
  9718. return -TARGET_EINVAL;
  9719. }
  9720. ret = get_errno(sched_getparam(arg1, &schp));
  9721. if (!is_error(ret)) {
  9722. if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
  9723. return -TARGET_EFAULT;
  9724. target_schp->sched_priority = tswap32(schp.sched_priority);
  9725. unlock_user_struct(target_schp, arg2, 1);
  9726. }
  9727. }
  9728. return ret;
  9729. case TARGET_NR_sched_setscheduler:
  9730. {
  9731. struct sched_param *target_schp;
  9732. struct sched_param schp;
  9733. if (arg3 == 0) {
  9734. return -TARGET_EINVAL;
  9735. }
  9736. if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
  9737. return -TARGET_EFAULT;
  9738. schp.sched_priority = tswap32(target_schp->sched_priority);
  9739. unlock_user_struct(target_schp, arg3, 0);
  9740. return get_errno(sched_setscheduler(arg1, arg2, &schp));
  9741. }
  9742. case TARGET_NR_sched_getscheduler:
  9743. return get_errno(sched_getscheduler(arg1));
  9744. case TARGET_NR_sched_yield:
  9745. return get_errno(sched_yield());
  9746. case TARGET_NR_sched_get_priority_max:
  9747. return get_errno(sched_get_priority_max(arg1));
  9748. case TARGET_NR_sched_get_priority_min:
  9749. return get_errno(sched_get_priority_min(arg1));
  9750. #ifdef TARGET_NR_sched_rr_get_interval
  9751. case TARGET_NR_sched_rr_get_interval:
  9752. {
  9753. struct timespec ts;
  9754. ret = get_errno(sched_rr_get_interval(arg1, &ts));
  9755. if (!is_error(ret)) {
  9756. ret = host_to_target_timespec(arg2, &ts);
  9757. }
  9758. }
  9759. return ret;
  9760. #endif
  9761. #ifdef TARGET_NR_sched_rr_get_interval_time64
  9762. case TARGET_NR_sched_rr_get_interval_time64:
  9763. {
  9764. struct timespec ts;
  9765. ret = get_errno(sched_rr_get_interval(arg1, &ts));
  9766. if (!is_error(ret)) {
  9767. ret = host_to_target_timespec64(arg2, &ts);
  9768. }
  9769. }
  9770. return ret;
  9771. #endif
  9772. #if defined(TARGET_NR_nanosleep)
  9773. case TARGET_NR_nanosleep:
  9774. {
  9775. struct timespec req, rem;
  9776. target_to_host_timespec(&req, arg1);
  9777. ret = get_errno(safe_nanosleep(&req, &rem));
  9778. if (is_error(ret) && arg2) {
  9779. host_to_target_timespec(arg2, &rem);
  9780. }
  9781. }
  9782. return ret;
  9783. #endif
  9784. case TARGET_NR_prctl:
  9785. switch (arg1) {
  9786. case PR_GET_PDEATHSIG:
  9787. {
  9788. int deathsig;
  9789. ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
  9790. if (!is_error(ret) && arg2
  9791. && put_user_ual(deathsig, arg2)) {
  9792. return -TARGET_EFAULT;
  9793. }
  9794. return ret;
  9795. }
  9796. #ifdef PR_GET_NAME
  9797. case PR_GET_NAME:
  9798. {
  9799. void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
  9800. if (!name) {
  9801. return -TARGET_EFAULT;
  9802. }
  9803. ret = get_errno(prctl(arg1, (unsigned long)name,
  9804. arg3, arg4, arg5));
  9805. unlock_user(name, arg2, 16);
  9806. return ret;
  9807. }
  9808. case PR_SET_NAME:
  9809. {
  9810. void *name = lock_user(VERIFY_READ, arg2, 16, 1);
  9811. if (!name) {
  9812. return -TARGET_EFAULT;
  9813. }
  9814. ret = get_errno(prctl(arg1, (unsigned long)name,
  9815. arg3, arg4, arg5));
  9816. unlock_user(name, arg2, 0);
  9817. return ret;
  9818. }
  9819. #endif
  9820. #ifdef TARGET_MIPS
  9821. case TARGET_PR_GET_FP_MODE:
  9822. {
  9823. CPUMIPSState *env = ((CPUMIPSState *)cpu_env);
  9824. ret = 0;
  9825. if (env->CP0_Status & (1 << CP0St_FR)) {
  9826. ret |= TARGET_PR_FP_MODE_FR;
  9827. }
  9828. if (env->CP0_Config5 & (1 << CP0C5_FRE)) {
  9829. ret |= TARGET_PR_FP_MODE_FRE;
  9830. }
  9831. return ret;
  9832. }
  9833. case TARGET_PR_SET_FP_MODE:
  9834. {
  9835. CPUMIPSState *env = ((CPUMIPSState *)cpu_env);
  9836. bool old_fr = env->CP0_Status & (1 << CP0St_FR);
  9837. bool old_fre = env->CP0_Config5 & (1 << CP0C5_FRE);
  9838. bool new_fr = arg2 & TARGET_PR_FP_MODE_FR;
  9839. bool new_fre = arg2 & TARGET_PR_FP_MODE_FRE;
  9840. const unsigned int known_bits = TARGET_PR_FP_MODE_FR |
  9841. TARGET_PR_FP_MODE_FRE;
  9842. /* If nothing to change, return right away, successfully. */
  9843. if (old_fr == new_fr && old_fre == new_fre) {
  9844. return 0;
  9845. }
  9846. /* Check the value is valid */
  9847. if (arg2 & ~known_bits) {
  9848. return -TARGET_EOPNOTSUPP;
  9849. }
  9850. /* Setting FRE without FR is not supported. */
  9851. if (new_fre && !new_fr) {
  9852. return -TARGET_EOPNOTSUPP;
  9853. }
  9854. if (new_fr && !(env->active_fpu.fcr0 & (1 << FCR0_F64))) {
  9855. /* FR1 is not supported */
  9856. return -TARGET_EOPNOTSUPP;
  9857. }
  9858. if (!new_fr && (env->active_fpu.fcr0 & (1 << FCR0_F64))
  9859. && !(env->CP0_Status_rw_bitmask & (1 << CP0St_FR))) {
  9860. /* cannot set FR=0 */
  9861. return -TARGET_EOPNOTSUPP;
  9862. }
  9863. if (new_fre && !(env->active_fpu.fcr0 & (1 << FCR0_FREP))) {
  9864. /* Cannot set FRE=1 */
  9865. return -TARGET_EOPNOTSUPP;
  9866. }
  9867. int i;
  9868. fpr_t *fpr = env->active_fpu.fpr;
  9869. for (i = 0; i < 32 ; i += 2) {
  9870. if (!old_fr && new_fr) {
  9871. fpr[i].w[!FP_ENDIAN_IDX] = fpr[i + 1].w[FP_ENDIAN_IDX];
  9872. } else if (old_fr && !new_fr) {
  9873. fpr[i + 1].w[FP_ENDIAN_IDX] = fpr[i].w[!FP_ENDIAN_IDX];
  9874. }
  9875. }
  9876. if (new_fr) {
  9877. env->CP0_Status |= (1 << CP0St_FR);
  9878. env->hflags |= MIPS_HFLAG_F64;
  9879. } else {
  9880. env->CP0_Status &= ~(1 << CP0St_FR);
  9881. env->hflags &= ~MIPS_HFLAG_F64;
  9882. }
  9883. if (new_fre) {
  9884. env->CP0_Config5 |= (1 << CP0C5_FRE);
  9885. if (env->active_fpu.fcr0 & (1 << FCR0_FREP)) {
  9886. env->hflags |= MIPS_HFLAG_FRE;
  9887. }
  9888. } else {
  9889. env->CP0_Config5 &= ~(1 << CP0C5_FRE);
  9890. env->hflags &= ~MIPS_HFLAG_FRE;
  9891. }
  9892. return 0;
  9893. }
  9894. #endif /* MIPS */
  9895. #ifdef TARGET_AARCH64
  9896. case TARGET_PR_SVE_SET_VL:
  9897. /*
  9898. * We cannot support either PR_SVE_SET_VL_ONEXEC or
  9899. * PR_SVE_VL_INHERIT. Note the kernel definition
  9900. * of sve_vl_valid allows for VQ=512, i.e. VL=8192,
  9901. * even though the current architectural maximum is VQ=16.
  9902. */
  9903. ret = -TARGET_EINVAL;
  9904. if (cpu_isar_feature(aa64_sve, env_archcpu(cpu_env))
  9905. && arg2 >= 0 && arg2 <= 512 * 16 && !(arg2 & 15)) {
  9906. CPUARMState *env = cpu_env;
  9907. ARMCPU *cpu = env_archcpu(env);
  9908. uint32_t vq, old_vq;
  9909. old_vq = (env->vfp.zcr_el[1] & 0xf) + 1;
  9910. vq = MAX(arg2 / 16, 1);
  9911. vq = MIN(vq, cpu->sve_max_vq);
  9912. if (vq < old_vq) {
  9913. aarch64_sve_narrow_vq(env, vq);
  9914. }
  9915. env->vfp.zcr_el[1] = vq - 1;
  9916. arm_rebuild_hflags(env);
  9917. ret = vq * 16;
  9918. }
  9919. return ret;
  9920. case TARGET_PR_SVE_GET_VL:
  9921. ret = -TARGET_EINVAL;
  9922. {
  9923. ARMCPU *cpu = env_archcpu(cpu_env);
  9924. if (cpu_isar_feature(aa64_sve, cpu)) {
  9925. ret = ((cpu->env.vfp.zcr_el[1] & 0xf) + 1) * 16;
  9926. }
  9927. }
  9928. return ret;
  9929. case TARGET_PR_PAC_RESET_KEYS:
  9930. {
  9931. CPUARMState *env = cpu_env;
  9932. ARMCPU *cpu = env_archcpu(env);
  9933. if (arg3 || arg4 || arg5) {
  9934. return -TARGET_EINVAL;
  9935. }
  9936. if (cpu_isar_feature(aa64_pauth, cpu)) {
  9937. int all = (TARGET_PR_PAC_APIAKEY | TARGET_PR_PAC_APIBKEY |
  9938. TARGET_PR_PAC_APDAKEY | TARGET_PR_PAC_APDBKEY |
  9939. TARGET_PR_PAC_APGAKEY);
  9940. int ret = 0;
  9941. Error *err = NULL;
  9942. if (arg2 == 0) {
  9943. arg2 = all;
  9944. } else if (arg2 & ~all) {
  9945. return -TARGET_EINVAL;
  9946. }
  9947. if (arg2 & TARGET_PR_PAC_APIAKEY) {
  9948. ret |= qemu_guest_getrandom(&env->keys.apia,
  9949. sizeof(ARMPACKey), &err);
  9950. }
  9951. if (arg2 & TARGET_PR_PAC_APIBKEY) {
  9952. ret |= qemu_guest_getrandom(&env->keys.apib,
  9953. sizeof(ARMPACKey), &err);
  9954. }
  9955. if (arg2 & TARGET_PR_PAC_APDAKEY) {
  9956. ret |= qemu_guest_getrandom(&env->keys.apda,
  9957. sizeof(ARMPACKey), &err);
  9958. }
  9959. if (arg2 & TARGET_PR_PAC_APDBKEY) {
  9960. ret |= qemu_guest_getrandom(&env->keys.apdb,
  9961. sizeof(ARMPACKey), &err);
  9962. }
  9963. if (arg2 & TARGET_PR_PAC_APGAKEY) {
  9964. ret |= qemu_guest_getrandom(&env->keys.apga,
  9965. sizeof(ARMPACKey), &err);
  9966. }
  9967. if (ret != 0) {
  9968. /*
  9969. * Some unknown failure in the crypto. The best
  9970. * we can do is log it and fail the syscall.
  9971. * The real syscall cannot fail this way.
  9972. */
  9973. qemu_log_mask(LOG_UNIMP,
  9974. "PR_PAC_RESET_KEYS: Crypto failure: %s",
  9975. error_get_pretty(err));
  9976. error_free(err);
  9977. return -TARGET_EIO;
  9978. }
  9979. return 0;
  9980. }
  9981. }
  9982. return -TARGET_EINVAL;
  9983. #endif /* AARCH64 */
  9984. case PR_GET_SECCOMP:
  9985. case PR_SET_SECCOMP:
  9986. /* Disable seccomp to prevent the target disabling syscalls we
  9987. * need. */
  9988. return -TARGET_EINVAL;
  9989. default:
  9990. /* Most prctl options have no pointer arguments */
  9991. return get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
  9992. }
  9993. break;
  9994. #ifdef TARGET_NR_arch_prctl
  9995. case TARGET_NR_arch_prctl:
  9996. return do_arch_prctl(cpu_env, arg1, arg2);
  9997. #endif
  9998. #ifdef TARGET_NR_pread64
  9999. case TARGET_NR_pread64:
  10000. if (regpairs_aligned(cpu_env, num)) {
  10001. arg4 = arg5;
  10002. arg5 = arg6;
  10003. }
  10004. if (arg2 == 0 && arg3 == 0) {
  10005. /* Special-case NULL buffer and zero length, which should succeed */
  10006. p = 0;
  10007. } else {
  10008. p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  10009. if (!p) {
  10010. return -TARGET_EFAULT;
  10011. }
  10012. }
  10013. ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
  10014. unlock_user(p, arg2, ret);
  10015. return ret;
  10016. case TARGET_NR_pwrite64:
  10017. if (regpairs_aligned(cpu_env, num)) {
  10018. arg4 = arg5;
  10019. arg5 = arg6;
  10020. }
  10021. if (arg2 == 0 && arg3 == 0) {
  10022. /* Special-case NULL buffer and zero length, which should succeed */
  10023. p = 0;
  10024. } else {
  10025. p = lock_user(VERIFY_READ, arg2, arg3, 1);
  10026. if (!p) {
  10027. return -TARGET_EFAULT;
  10028. }
  10029. }
  10030. ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
  10031. unlock_user(p, arg2, 0);
  10032. return ret;
  10033. #endif
  10034. case TARGET_NR_getcwd:
  10035. if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
  10036. return -TARGET_EFAULT;
  10037. ret = get_errno(sys_getcwd1(p, arg2));
  10038. unlock_user(p, arg1, ret);
  10039. return ret;
  10040. case TARGET_NR_capget:
  10041. case TARGET_NR_capset:
  10042. {
  10043. struct target_user_cap_header *target_header;
  10044. struct target_user_cap_data *target_data = NULL;
  10045. struct __user_cap_header_struct header;
  10046. struct __user_cap_data_struct data[2];
  10047. struct __user_cap_data_struct *dataptr = NULL;
  10048. int i, target_datalen;
  10049. int data_items = 1;
  10050. if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) {
  10051. return -TARGET_EFAULT;
  10052. }
  10053. header.version = tswap32(target_header->version);
  10054. header.pid = tswap32(target_header->pid);
  10055. if (header.version != _LINUX_CAPABILITY_VERSION) {
  10056. /* Version 2 and up takes pointer to two user_data structs */
  10057. data_items = 2;
  10058. }
  10059. target_datalen = sizeof(*target_data) * data_items;
  10060. if (arg2) {
  10061. if (num == TARGET_NR_capget) {
  10062. target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0);
  10063. } else {
  10064. target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1);
  10065. }
  10066. if (!target_data) {
  10067. unlock_user_struct(target_header, arg1, 0);
  10068. return -TARGET_EFAULT;
  10069. }
  10070. if (num == TARGET_NR_capset) {
  10071. for (i = 0; i < data_items; i++) {
  10072. data[i].effective = tswap32(target_data[i].effective);
  10073. data[i].permitted = tswap32(target_data[i].permitted);
  10074. data[i].inheritable = tswap32(target_data[i].inheritable);
  10075. }
  10076. }
  10077. dataptr = data;
  10078. }
  10079. if (num == TARGET_NR_capget) {
  10080. ret = get_errno(capget(&header, dataptr));
  10081. } else {
  10082. ret = get_errno(capset(&header, dataptr));
  10083. }
  10084. /* The kernel always updates version for both capget and capset */
  10085. target_header->version = tswap32(header.version);
  10086. unlock_user_struct(target_header, arg1, 1);
  10087. if (arg2) {
  10088. if (num == TARGET_NR_capget) {
  10089. for (i = 0; i < data_items; i++) {
  10090. target_data[i].effective = tswap32(data[i].effective);
  10091. target_data[i].permitted = tswap32(data[i].permitted);
  10092. target_data[i].inheritable = tswap32(data[i].inheritable);
  10093. }
  10094. unlock_user(target_data, arg2, target_datalen);
  10095. } else {
  10096. unlock_user(target_data, arg2, 0);
  10097. }
  10098. }
  10099. return ret;
  10100. }
  10101. case TARGET_NR_sigaltstack:
  10102. return do_sigaltstack(arg1, arg2,
  10103. get_sp_from_cpustate((CPUArchState *)cpu_env));
  10104. #ifdef CONFIG_SENDFILE
  10105. #ifdef TARGET_NR_sendfile
  10106. case TARGET_NR_sendfile:
  10107. {
  10108. off_t *offp = NULL;
  10109. off_t off;
  10110. if (arg3) {
  10111. ret = get_user_sal(off, arg3);
  10112. if (is_error(ret)) {
  10113. return ret;
  10114. }
  10115. offp = &off;
  10116. }
  10117. ret = get_errno(sendfile(arg1, arg2, offp, arg4));
  10118. if (!is_error(ret) && arg3) {
  10119. abi_long ret2 = put_user_sal(off, arg3);
  10120. if (is_error(ret2)) {
  10121. ret = ret2;
  10122. }
  10123. }
  10124. return ret;
  10125. }
  10126. #endif
  10127. #ifdef TARGET_NR_sendfile64
  10128. case TARGET_NR_sendfile64:
  10129. {
  10130. off_t *offp = NULL;
  10131. off_t off;
  10132. if (arg3) {
  10133. ret = get_user_s64(off, arg3);
  10134. if (is_error(ret)) {
  10135. return ret;
  10136. }
  10137. offp = &off;
  10138. }
  10139. ret = get_errno(sendfile(arg1, arg2, offp, arg4));
  10140. if (!is_error(ret) && arg3) {
  10141. abi_long ret2 = put_user_s64(off, arg3);
  10142. if (is_error(ret2)) {
  10143. ret = ret2;
  10144. }
  10145. }
  10146. return ret;
  10147. }
  10148. #endif
  10149. #endif
  10150. #ifdef TARGET_NR_vfork
  10151. case TARGET_NR_vfork:
  10152. return get_errno(do_fork(cpu_env,
  10153. CLONE_VFORK | CLONE_VM | TARGET_SIGCHLD,
  10154. 0, 0, 0, 0));
  10155. #endif
  10156. #ifdef TARGET_NR_ugetrlimit
  10157. case TARGET_NR_ugetrlimit:
  10158. {
  10159. struct rlimit rlim;
  10160. int resource = target_to_host_resource(arg1);
  10161. ret = get_errno(getrlimit(resource, &rlim));
  10162. if (!is_error(ret)) {
  10163. struct target_rlimit *target_rlim;
  10164. if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
  10165. return -TARGET_EFAULT;
  10166. target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
  10167. target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
  10168. unlock_user_struct(target_rlim, arg2, 1);
  10169. }
  10170. return ret;
  10171. }
  10172. #endif
  10173. #ifdef TARGET_NR_truncate64
  10174. case TARGET_NR_truncate64:
  10175. if (!(p = lock_user_string(arg1)))
  10176. return -TARGET_EFAULT;
  10177. ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
  10178. unlock_user(p, arg1, 0);
  10179. return ret;
  10180. #endif
  10181. #ifdef TARGET_NR_ftruncate64
  10182. case TARGET_NR_ftruncate64:
  10183. return target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
  10184. #endif
  10185. #ifdef TARGET_NR_stat64
  10186. case TARGET_NR_stat64:
  10187. if (!(p = lock_user_string(arg1))) {
  10188. return -TARGET_EFAULT;
  10189. }
  10190. ret = get_errno(stat(path(p), &st));
  10191. unlock_user(p, arg1, 0);
  10192. if (!is_error(ret))
  10193. ret = host_to_target_stat64(cpu_env, arg2, &st);
  10194. return ret;
  10195. #endif
  10196. #ifdef TARGET_NR_lstat64
  10197. case TARGET_NR_lstat64:
  10198. if (!(p = lock_user_string(arg1))) {
  10199. return -TARGET_EFAULT;
  10200. }
  10201. ret = get_errno(lstat(path(p), &st));
  10202. unlock_user(p, arg1, 0);
  10203. if (!is_error(ret))
  10204. ret = host_to_target_stat64(cpu_env, arg2, &st);
  10205. return ret;
  10206. #endif
  10207. #ifdef TARGET_NR_fstat64
  10208. case TARGET_NR_fstat64:
  10209. ret = get_errno(fstat(arg1, &st));
  10210. if (!is_error(ret))
  10211. ret = host_to_target_stat64(cpu_env, arg2, &st);
  10212. return ret;
  10213. #endif
  10214. #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
  10215. #ifdef TARGET_NR_fstatat64
  10216. case TARGET_NR_fstatat64:
  10217. #endif
  10218. #ifdef TARGET_NR_newfstatat
  10219. case TARGET_NR_newfstatat:
  10220. #endif
  10221. if (!(p = lock_user_string(arg2))) {
  10222. return -TARGET_EFAULT;
  10223. }
  10224. ret = get_errno(fstatat(arg1, path(p), &st, arg4));
  10225. unlock_user(p, arg2, 0);
  10226. if (!is_error(ret))
  10227. ret = host_to_target_stat64(cpu_env, arg3, &st);
  10228. return ret;
  10229. #endif
  10230. #if defined(TARGET_NR_statx)
  10231. case TARGET_NR_statx:
  10232. {
  10233. struct target_statx *target_stx;
  10234. int dirfd = arg1;
  10235. int flags = arg3;
  10236. p = lock_user_string(arg2);
  10237. if (p == NULL) {
  10238. return -TARGET_EFAULT;
  10239. }
  10240. #if defined(__NR_statx)
  10241. {
  10242. /*
  10243. * It is assumed that struct statx is architecture independent.
  10244. */
  10245. struct target_statx host_stx;
  10246. int mask = arg4;
  10247. ret = get_errno(sys_statx(dirfd, p, flags, mask, &host_stx));
  10248. if (!is_error(ret)) {
  10249. if (host_to_target_statx(&host_stx, arg5) != 0) {
  10250. unlock_user(p, arg2, 0);
  10251. return -TARGET_EFAULT;
  10252. }
  10253. }
  10254. if (ret != -TARGET_ENOSYS) {
  10255. unlock_user(p, arg2, 0);
  10256. return ret;
  10257. }
  10258. }
  10259. #endif
  10260. ret = get_errno(fstatat(dirfd, path(p), &st, flags));
  10261. unlock_user(p, arg2, 0);
  10262. if (!is_error(ret)) {
  10263. if (!lock_user_struct(VERIFY_WRITE, target_stx, arg5, 0)) {
  10264. return -TARGET_EFAULT;
  10265. }
  10266. memset(target_stx, 0, sizeof(*target_stx));
  10267. __put_user(major(st.st_dev), &target_stx->stx_dev_major);
  10268. __put_user(minor(st.st_dev), &target_stx->stx_dev_minor);
  10269. __put_user(st.st_ino, &target_stx->stx_ino);
  10270. __put_user(st.st_mode, &target_stx->stx_mode);
  10271. __put_user(st.st_uid, &target_stx->stx_uid);
  10272. __put_user(st.st_gid, &target_stx->stx_gid);
  10273. __put_user(st.st_nlink, &target_stx->stx_nlink);
  10274. __put_user(major(st.st_rdev), &target_stx->stx_rdev_major);
  10275. __put_user(minor(st.st_rdev), &target_stx->stx_rdev_minor);
  10276. __put_user(st.st_size, &target_stx->stx_size);
  10277. __put_user(st.st_blksize, &target_stx->stx_blksize);
  10278. __put_user(st.st_blocks, &target_stx->stx_blocks);
  10279. __put_user(st.st_atime, &target_stx->stx_atime.tv_sec);
  10280. __put_user(st.st_mtime, &target_stx->stx_mtime.tv_sec);
  10281. __put_user(st.st_ctime, &target_stx->stx_ctime.tv_sec);
  10282. unlock_user_struct(target_stx, arg5, 1);
  10283. }
  10284. }
  10285. return ret;
  10286. #endif
  10287. #ifdef TARGET_NR_lchown
  10288. case TARGET_NR_lchown:
  10289. if (!(p = lock_user_string(arg1)))
  10290. return -TARGET_EFAULT;
  10291. ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
  10292. unlock_user(p, arg1, 0);
  10293. return ret;
  10294. #endif
  10295. #ifdef TARGET_NR_getuid
  10296. case TARGET_NR_getuid:
  10297. return get_errno(high2lowuid(getuid()));
  10298. #endif
  10299. #ifdef TARGET_NR_getgid
  10300. case TARGET_NR_getgid:
  10301. return get_errno(high2lowgid(getgid()));
  10302. #endif
  10303. #ifdef TARGET_NR_geteuid
  10304. case TARGET_NR_geteuid:
  10305. return get_errno(high2lowuid(geteuid()));
  10306. #endif
  10307. #ifdef TARGET_NR_getegid
  10308. case TARGET_NR_getegid:
  10309. return get_errno(high2lowgid(getegid()));
  10310. #endif
  10311. case TARGET_NR_setreuid:
  10312. return get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
  10313. case TARGET_NR_setregid:
  10314. return get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
  10315. case TARGET_NR_getgroups:
  10316. {
  10317. int gidsetsize = arg1;
  10318. target_id *target_grouplist;
  10319. gid_t *grouplist;
  10320. int i;
  10321. grouplist = alloca(gidsetsize * sizeof(gid_t));
  10322. ret = get_errno(getgroups(gidsetsize, grouplist));
  10323. if (gidsetsize == 0)
  10324. return ret;
  10325. if (!is_error(ret)) {
  10326. target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * sizeof(target_id), 0);
  10327. if (!target_grouplist)
  10328. return -TARGET_EFAULT;
  10329. for(i = 0;i < ret; i++)
  10330. target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
  10331. unlock_user(target_grouplist, arg2, gidsetsize * sizeof(target_id));
  10332. }
  10333. }
  10334. return ret;
  10335. case TARGET_NR_setgroups:
  10336. {
  10337. int gidsetsize = arg1;
  10338. target_id *target_grouplist;
  10339. gid_t *grouplist = NULL;
  10340. int i;
  10341. if (gidsetsize) {
  10342. grouplist = alloca(gidsetsize * sizeof(gid_t));
  10343. target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * sizeof(target_id), 1);
  10344. if (!target_grouplist) {
  10345. return -TARGET_EFAULT;
  10346. }
  10347. for (i = 0; i < gidsetsize; i++) {
  10348. grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
  10349. }
  10350. unlock_user(target_grouplist, arg2, 0);
  10351. }
  10352. return get_errno(setgroups(gidsetsize, grouplist));
  10353. }
  10354. case TARGET_NR_fchown:
  10355. return get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
  10356. #if defined(TARGET_NR_fchownat)
  10357. case TARGET_NR_fchownat:
  10358. if (!(p = lock_user_string(arg2)))
  10359. return -TARGET_EFAULT;
  10360. ret = get_errno(fchownat(arg1, p, low2highuid(arg3),
  10361. low2highgid(arg4), arg5));
  10362. unlock_user(p, arg2, 0);
  10363. return ret;
  10364. #endif
  10365. #ifdef TARGET_NR_setresuid
  10366. case TARGET_NR_setresuid:
  10367. return get_errno(sys_setresuid(low2highuid(arg1),
  10368. low2highuid(arg2),
  10369. low2highuid(arg3)));
  10370. #endif
  10371. #ifdef TARGET_NR_getresuid
  10372. case TARGET_NR_getresuid:
  10373. {
  10374. uid_t ruid, euid, suid;
  10375. ret = get_errno(getresuid(&ruid, &euid, &suid));
  10376. if (!is_error(ret)) {
  10377. if (put_user_id(high2lowuid(ruid), arg1)
  10378. || put_user_id(high2lowuid(euid), arg2)
  10379. || put_user_id(high2lowuid(suid), arg3))
  10380. return -TARGET_EFAULT;
  10381. }
  10382. }
  10383. return ret;
  10384. #endif
  10385. #ifdef TARGET_NR_getresgid
  10386. case TARGET_NR_setresgid:
  10387. return get_errno(sys_setresgid(low2highgid(arg1),
  10388. low2highgid(arg2),
  10389. low2highgid(arg3)));
  10390. #endif
  10391. #ifdef TARGET_NR_getresgid
  10392. case TARGET_NR_getresgid:
  10393. {
  10394. gid_t rgid, egid, sgid;
  10395. ret = get_errno(getresgid(&rgid, &egid, &sgid));
  10396. if (!is_error(ret)) {
  10397. if (put_user_id(high2lowgid(rgid), arg1)
  10398. || put_user_id(high2lowgid(egid), arg2)
  10399. || put_user_id(high2lowgid(sgid), arg3))
  10400. return -TARGET_EFAULT;
  10401. }
  10402. }
  10403. return ret;
  10404. #endif
  10405. #ifdef TARGET_NR_chown
  10406. case TARGET_NR_chown:
  10407. if (!(p = lock_user_string(arg1)))
  10408. return -TARGET_EFAULT;
  10409. ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
  10410. unlock_user(p, arg1, 0);
  10411. return ret;
  10412. #endif
  10413. case TARGET_NR_setuid:
  10414. return get_errno(sys_setuid(low2highuid(arg1)));
  10415. case TARGET_NR_setgid:
  10416. return get_errno(sys_setgid(low2highgid(arg1)));
  10417. case TARGET_NR_setfsuid:
  10418. return get_errno(setfsuid(arg1));
  10419. case TARGET_NR_setfsgid:
  10420. return get_errno(setfsgid(arg1));
  10421. #ifdef TARGET_NR_lchown32
  10422. case TARGET_NR_lchown32:
  10423. if (!(p = lock_user_string(arg1)))
  10424. return -TARGET_EFAULT;
  10425. ret = get_errno(lchown(p, arg2, arg3));
  10426. unlock_user(p, arg1, 0);
  10427. return ret;
  10428. #endif
  10429. #ifdef TARGET_NR_getuid32
  10430. case TARGET_NR_getuid32:
  10431. return get_errno(getuid());
  10432. #endif
  10433. #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
  10434. /* Alpha specific */
  10435. case TARGET_NR_getxuid:
  10436. {
  10437. uid_t euid;
  10438. euid=geteuid();
  10439. ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
  10440. }
  10441. return get_errno(getuid());
  10442. #endif
  10443. #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
  10444. /* Alpha specific */
  10445. case TARGET_NR_getxgid:
  10446. {
  10447. uid_t egid;
  10448. egid=getegid();
  10449. ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
  10450. }
  10451. return get_errno(getgid());
  10452. #endif
  10453. #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
  10454. /* Alpha specific */
  10455. case TARGET_NR_osf_getsysinfo:
  10456. ret = -TARGET_EOPNOTSUPP;
  10457. switch (arg1) {
  10458. case TARGET_GSI_IEEE_FP_CONTROL:
  10459. {
  10460. uint64_t fpcr = cpu_alpha_load_fpcr(cpu_env);
  10461. uint64_t swcr = ((CPUAlphaState *)cpu_env)->swcr;
  10462. swcr &= ~SWCR_STATUS_MASK;
  10463. swcr |= (fpcr >> 35) & SWCR_STATUS_MASK;
  10464. if (put_user_u64 (swcr, arg2))
  10465. return -TARGET_EFAULT;
  10466. ret = 0;
  10467. }
  10468. break;
  10469. /* case GSI_IEEE_STATE_AT_SIGNAL:
  10470. -- Not implemented in linux kernel.
  10471. case GSI_UACPROC:
  10472. -- Retrieves current unaligned access state; not much used.
  10473. case GSI_PROC_TYPE:
  10474. -- Retrieves implver information; surely not used.
  10475. case GSI_GET_HWRPB:
  10476. -- Grabs a copy of the HWRPB; surely not used.
  10477. */
  10478. }
  10479. return ret;
  10480. #endif
  10481. #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
  10482. /* Alpha specific */
  10483. case TARGET_NR_osf_setsysinfo:
  10484. ret = -TARGET_EOPNOTSUPP;
  10485. switch (arg1) {
  10486. case TARGET_SSI_IEEE_FP_CONTROL:
  10487. {
  10488. uint64_t swcr, fpcr;
  10489. if (get_user_u64 (swcr, arg2)) {
  10490. return -TARGET_EFAULT;
  10491. }
  10492. /*
  10493. * The kernel calls swcr_update_status to update the
  10494. * status bits from the fpcr at every point that it
  10495. * could be queried. Therefore, we store the status
  10496. * bits only in FPCR.
  10497. */
  10498. ((CPUAlphaState *)cpu_env)->swcr
  10499. = swcr & (SWCR_TRAP_ENABLE_MASK | SWCR_MAP_MASK);
  10500. fpcr = cpu_alpha_load_fpcr(cpu_env);
  10501. fpcr &= ((uint64_t)FPCR_DYN_MASK << 32);
  10502. fpcr |= alpha_ieee_swcr_to_fpcr(swcr);
  10503. cpu_alpha_store_fpcr(cpu_env, fpcr);
  10504. ret = 0;
  10505. }
  10506. break;
  10507. case TARGET_SSI_IEEE_RAISE_EXCEPTION:
  10508. {
  10509. uint64_t exc, fpcr, fex;
  10510. if (get_user_u64(exc, arg2)) {
  10511. return -TARGET_EFAULT;
  10512. }
  10513. exc &= SWCR_STATUS_MASK;
  10514. fpcr = cpu_alpha_load_fpcr(cpu_env);
  10515. /* Old exceptions are not signaled. */
  10516. fex = alpha_ieee_fpcr_to_swcr(fpcr);
  10517. fex = exc & ~fex;
  10518. fex >>= SWCR_STATUS_TO_EXCSUM_SHIFT;
  10519. fex &= ((CPUArchState *)cpu_env)->swcr;
  10520. /* Update the hardware fpcr. */
  10521. fpcr |= alpha_ieee_swcr_to_fpcr(exc);
  10522. cpu_alpha_store_fpcr(cpu_env, fpcr);
  10523. if (fex) {
  10524. int si_code = TARGET_FPE_FLTUNK;
  10525. target_siginfo_t info;
  10526. if (fex & SWCR_TRAP_ENABLE_DNO) {
  10527. si_code = TARGET_FPE_FLTUND;
  10528. }
  10529. if (fex & SWCR_TRAP_ENABLE_INE) {
  10530. si_code = TARGET_FPE_FLTRES;
  10531. }
  10532. if (fex & SWCR_TRAP_ENABLE_UNF) {
  10533. si_code = TARGET_FPE_FLTUND;
  10534. }
  10535. if (fex & SWCR_TRAP_ENABLE_OVF) {
  10536. si_code = TARGET_FPE_FLTOVF;
  10537. }
  10538. if (fex & SWCR_TRAP_ENABLE_DZE) {
  10539. si_code = TARGET_FPE_FLTDIV;
  10540. }
  10541. if (fex & SWCR_TRAP_ENABLE_INV) {
  10542. si_code = TARGET_FPE_FLTINV;
  10543. }
  10544. info.si_signo = SIGFPE;
  10545. info.si_errno = 0;
  10546. info.si_code = si_code;
  10547. info._sifields._sigfault._addr
  10548. = ((CPUArchState *)cpu_env)->pc;
  10549. queue_signal((CPUArchState *)cpu_env, info.si_signo,
  10550. QEMU_SI_FAULT, &info);
  10551. }
  10552. ret = 0;
  10553. }
  10554. break;
  10555. /* case SSI_NVPAIRS:
  10556. -- Used with SSIN_UACPROC to enable unaligned accesses.
  10557. case SSI_IEEE_STATE_AT_SIGNAL:
  10558. case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
  10559. -- Not implemented in linux kernel
  10560. */
  10561. }
  10562. return ret;
  10563. #endif
  10564. #ifdef TARGET_NR_osf_sigprocmask
  10565. /* Alpha specific. */
  10566. case TARGET_NR_osf_sigprocmask:
  10567. {
  10568. abi_ulong mask;
  10569. int how;
  10570. sigset_t set, oldset;
  10571. switch(arg1) {
  10572. case TARGET_SIG_BLOCK:
  10573. how = SIG_BLOCK;
  10574. break;
  10575. case TARGET_SIG_UNBLOCK:
  10576. how = SIG_UNBLOCK;
  10577. break;
  10578. case TARGET_SIG_SETMASK:
  10579. how = SIG_SETMASK;
  10580. break;
  10581. default:
  10582. return -TARGET_EINVAL;
  10583. }
  10584. mask = arg2;
  10585. target_to_host_old_sigset(&set, &mask);
  10586. ret = do_sigprocmask(how, &set, &oldset);
  10587. if (!ret) {
  10588. host_to_target_old_sigset(&mask, &oldset);
  10589. ret = mask;
  10590. }
  10591. }
  10592. return ret;
  10593. #endif
  10594. #ifdef TARGET_NR_getgid32
  10595. case TARGET_NR_getgid32:
  10596. return get_errno(getgid());
  10597. #endif
  10598. #ifdef TARGET_NR_geteuid32
  10599. case TARGET_NR_geteuid32:
  10600. return get_errno(geteuid());
  10601. #endif
  10602. #ifdef TARGET_NR_getegid32
  10603. case TARGET_NR_getegid32:
  10604. return get_errno(getegid());
  10605. #endif
  10606. #ifdef TARGET_NR_setreuid32
  10607. case TARGET_NR_setreuid32:
  10608. return get_errno(setreuid(arg1, arg2));
  10609. #endif
  10610. #ifdef TARGET_NR_setregid32
  10611. case TARGET_NR_setregid32:
  10612. return get_errno(setregid(arg1, arg2));
  10613. #endif
  10614. #ifdef TARGET_NR_getgroups32
  10615. case TARGET_NR_getgroups32:
  10616. {
  10617. int gidsetsize = arg1;
  10618. uint32_t *target_grouplist;
  10619. gid_t *grouplist;
  10620. int i;
  10621. grouplist = alloca(gidsetsize * sizeof(gid_t));
  10622. ret = get_errno(getgroups(gidsetsize, grouplist));
  10623. if (gidsetsize == 0)
  10624. return ret;
  10625. if (!is_error(ret)) {
  10626. target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
  10627. if (!target_grouplist) {
  10628. return -TARGET_EFAULT;
  10629. }
  10630. for(i = 0;i < ret; i++)
  10631. target_grouplist[i] = tswap32(grouplist[i]);
  10632. unlock_user(target_grouplist, arg2, gidsetsize * 4);
  10633. }
  10634. }
  10635. return ret;
  10636. #endif
  10637. #ifdef TARGET_NR_setgroups32
  10638. case TARGET_NR_setgroups32:
  10639. {
  10640. int gidsetsize = arg1;
  10641. uint32_t *target_grouplist;
  10642. gid_t *grouplist;
  10643. int i;
  10644. grouplist = alloca(gidsetsize * sizeof(gid_t));
  10645. target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
  10646. if (!target_grouplist) {
  10647. return -TARGET_EFAULT;
  10648. }
  10649. for(i = 0;i < gidsetsize; i++)
  10650. grouplist[i] = tswap32(target_grouplist[i]);
  10651. unlock_user(target_grouplist, arg2, 0);
  10652. return get_errno(setgroups(gidsetsize, grouplist));
  10653. }
  10654. #endif
  10655. #ifdef TARGET_NR_fchown32
  10656. case TARGET_NR_fchown32:
  10657. return get_errno(fchown(arg1, arg2, arg3));
  10658. #endif
  10659. #ifdef TARGET_NR_setresuid32
  10660. case TARGET_NR_setresuid32:
  10661. return get_errno(sys_setresuid(arg1, arg2, arg3));
  10662. #endif
  10663. #ifdef TARGET_NR_getresuid32
  10664. case TARGET_NR_getresuid32:
  10665. {
  10666. uid_t ruid, euid, suid;
  10667. ret = get_errno(getresuid(&ruid, &euid, &suid));
  10668. if (!is_error(ret)) {
  10669. if (put_user_u32(ruid, arg1)
  10670. || put_user_u32(euid, arg2)
  10671. || put_user_u32(suid, arg3))
  10672. return -TARGET_EFAULT;
  10673. }
  10674. }
  10675. return ret;
  10676. #endif
  10677. #ifdef TARGET_NR_setresgid32
  10678. case TARGET_NR_setresgid32:
  10679. return get_errno(sys_setresgid(arg1, arg2, arg3));
  10680. #endif
  10681. #ifdef TARGET_NR_getresgid32
  10682. case TARGET_NR_getresgid32:
  10683. {
  10684. gid_t rgid, egid, sgid;
  10685. ret = get_errno(getresgid(&rgid, &egid, &sgid));
  10686. if (!is_error(ret)) {
  10687. if (put_user_u32(rgid, arg1)
  10688. || put_user_u32(egid, arg2)
  10689. || put_user_u32(sgid, arg3))
  10690. return -TARGET_EFAULT;
  10691. }
  10692. }
  10693. return ret;
  10694. #endif
  10695. #ifdef TARGET_NR_chown32
  10696. case TARGET_NR_chown32:
  10697. if (!(p = lock_user_string(arg1)))
  10698. return -TARGET_EFAULT;
  10699. ret = get_errno(chown(p, arg2, arg3));
  10700. unlock_user(p, arg1, 0);
  10701. return ret;
  10702. #endif
  10703. #ifdef TARGET_NR_setuid32
  10704. case TARGET_NR_setuid32:
  10705. return get_errno(sys_setuid(arg1));
  10706. #endif
  10707. #ifdef TARGET_NR_setgid32
  10708. case TARGET_NR_setgid32:
  10709. return get_errno(sys_setgid(arg1));
  10710. #endif
  10711. #ifdef TARGET_NR_setfsuid32
  10712. case TARGET_NR_setfsuid32:
  10713. return get_errno(setfsuid(arg1));
  10714. #endif
  10715. #ifdef TARGET_NR_setfsgid32
  10716. case TARGET_NR_setfsgid32:
  10717. return get_errno(setfsgid(arg1));
  10718. #endif
  10719. #ifdef TARGET_NR_mincore
  10720. case TARGET_NR_mincore:
  10721. {
  10722. void *a = lock_user(VERIFY_READ, arg1, arg2, 0);
  10723. if (!a) {
  10724. return -TARGET_ENOMEM;
  10725. }
  10726. p = lock_user_string(arg3);
  10727. if (!p) {
  10728. ret = -TARGET_EFAULT;
  10729. } else {
  10730. ret = get_errno(mincore(a, arg2, p));
  10731. unlock_user(p, arg3, ret);
  10732. }
  10733. unlock_user(a, arg1, 0);
  10734. }
  10735. return ret;
  10736. #endif
  10737. #ifdef TARGET_NR_arm_fadvise64_64
  10738. case TARGET_NR_arm_fadvise64_64:
  10739. /* arm_fadvise64_64 looks like fadvise64_64 but
  10740. * with different argument order: fd, advice, offset, len
  10741. * rather than the usual fd, offset, len, advice.
  10742. * Note that offset and len are both 64-bit so appear as
  10743. * pairs of 32-bit registers.
  10744. */
  10745. ret = posix_fadvise(arg1, target_offset64(arg3, arg4),
  10746. target_offset64(arg5, arg6), arg2);
  10747. return -host_to_target_errno(ret);
  10748. #endif
  10749. #if TARGET_ABI_BITS == 32
  10750. #ifdef TARGET_NR_fadvise64_64
  10751. case TARGET_NR_fadvise64_64:
  10752. #if defined(TARGET_PPC) || defined(TARGET_XTENSA)
  10753. /* 6 args: fd, advice, offset (high, low), len (high, low) */
  10754. ret = arg2;
  10755. arg2 = arg3;
  10756. arg3 = arg4;
  10757. arg4 = arg5;
  10758. arg5 = arg6;
  10759. arg6 = ret;
  10760. #else
  10761. /* 6 args: fd, offset (high, low), len (high, low), advice */
  10762. if (regpairs_aligned(cpu_env, num)) {
  10763. /* offset is in (3,4), len in (5,6) and advice in 7 */
  10764. arg2 = arg3;
  10765. arg3 = arg4;
  10766. arg4 = arg5;
  10767. arg5 = arg6;
  10768. arg6 = arg7;
  10769. }
  10770. #endif
  10771. ret = posix_fadvise(arg1, target_offset64(arg2, arg3),
  10772. target_offset64(arg4, arg5), arg6);
  10773. return -host_to_target_errno(ret);
  10774. #endif
  10775. #ifdef TARGET_NR_fadvise64
  10776. case TARGET_NR_fadvise64:
  10777. /* 5 args: fd, offset (high, low), len, advice */
  10778. if (regpairs_aligned(cpu_env, num)) {
  10779. /* offset is in (3,4), len in 5 and advice in 6 */
  10780. arg2 = arg3;
  10781. arg3 = arg4;
  10782. arg4 = arg5;
  10783. arg5 = arg6;
  10784. }
  10785. ret = posix_fadvise(arg1, target_offset64(arg2, arg3), arg4, arg5);
  10786. return -host_to_target_errno(ret);
  10787. #endif
  10788. #else /* not a 32-bit ABI */
  10789. #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_fadvise64)
  10790. #ifdef TARGET_NR_fadvise64_64
  10791. case TARGET_NR_fadvise64_64:
  10792. #endif
  10793. #ifdef TARGET_NR_fadvise64
  10794. case TARGET_NR_fadvise64:
  10795. #endif
  10796. #ifdef TARGET_S390X
  10797. switch (arg4) {
  10798. case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
  10799. case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
  10800. case 6: arg4 = POSIX_FADV_DONTNEED; break;
  10801. case 7: arg4 = POSIX_FADV_NOREUSE; break;
  10802. default: break;
  10803. }
  10804. #endif
  10805. return -host_to_target_errno(posix_fadvise(arg1, arg2, arg3, arg4));
  10806. #endif
  10807. #endif /* end of 64-bit ABI fadvise handling */
  10808. #ifdef TARGET_NR_madvise
  10809. case TARGET_NR_madvise:
  10810. /* A straight passthrough may not be safe because qemu sometimes
  10811. turns private file-backed mappings into anonymous mappings.
  10812. This will break MADV_DONTNEED.
  10813. This is a hint, so ignoring and returning success is ok. */
  10814. return 0;
  10815. #endif
  10816. #ifdef TARGET_NR_fcntl64
  10817. case TARGET_NR_fcntl64:
  10818. {
  10819. int cmd;
  10820. struct flock64 fl;
  10821. from_flock64_fn *copyfrom = copy_from_user_flock64;
  10822. to_flock64_fn *copyto = copy_to_user_flock64;
  10823. #ifdef TARGET_ARM
  10824. if (!((CPUARMState *)cpu_env)->eabi) {
  10825. copyfrom = copy_from_user_oabi_flock64;
  10826. copyto = copy_to_user_oabi_flock64;
  10827. }
  10828. #endif
  10829. cmd = target_to_host_fcntl_cmd(arg2);
  10830. if (cmd == -TARGET_EINVAL) {
  10831. return cmd;
  10832. }
  10833. switch(arg2) {
  10834. case TARGET_F_GETLK64:
  10835. ret = copyfrom(&fl, arg3);
  10836. if (ret) {
  10837. break;
  10838. }
  10839. ret = get_errno(safe_fcntl(arg1, cmd, &fl));
  10840. if (ret == 0) {
  10841. ret = copyto(arg3, &fl);
  10842. }
  10843. break;
  10844. case TARGET_F_SETLK64:
  10845. case TARGET_F_SETLKW64:
  10846. ret = copyfrom(&fl, arg3);
  10847. if (ret) {
  10848. break;
  10849. }
  10850. ret = get_errno(safe_fcntl(arg1, cmd, &fl));
  10851. break;
  10852. default:
  10853. ret = do_fcntl(arg1, arg2, arg3);
  10854. break;
  10855. }
  10856. return ret;
  10857. }
  10858. #endif
  10859. #ifdef TARGET_NR_cacheflush
  10860. case TARGET_NR_cacheflush:
  10861. /* self-modifying code is handled automatically, so nothing needed */
  10862. return 0;
  10863. #endif
  10864. #ifdef TARGET_NR_getpagesize
  10865. case TARGET_NR_getpagesize:
  10866. return TARGET_PAGE_SIZE;
  10867. #endif
  10868. case TARGET_NR_gettid:
  10869. return get_errno(sys_gettid());
  10870. #ifdef TARGET_NR_readahead
  10871. case TARGET_NR_readahead:
  10872. #if TARGET_ABI_BITS == 32
  10873. if (regpairs_aligned(cpu_env, num)) {
  10874. arg2 = arg3;
  10875. arg3 = arg4;
  10876. arg4 = arg5;
  10877. }
  10878. ret = get_errno(readahead(arg1, target_offset64(arg2, arg3) , arg4));
  10879. #else
  10880. ret = get_errno(readahead(arg1, arg2, arg3));
  10881. #endif
  10882. return ret;
  10883. #endif
  10884. #ifdef CONFIG_ATTR
  10885. #ifdef TARGET_NR_setxattr
  10886. case TARGET_NR_listxattr:
  10887. case TARGET_NR_llistxattr:
  10888. {
  10889. void *p, *b = 0;
  10890. if (arg2) {
  10891. b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  10892. if (!b) {
  10893. return -TARGET_EFAULT;
  10894. }
  10895. }
  10896. p = lock_user_string(arg1);
  10897. if (p) {
  10898. if (num == TARGET_NR_listxattr) {
  10899. ret = get_errno(listxattr(p, b, arg3));
  10900. } else {
  10901. ret = get_errno(llistxattr(p, b, arg3));
  10902. }
  10903. } else {
  10904. ret = -TARGET_EFAULT;
  10905. }
  10906. unlock_user(p, arg1, 0);
  10907. unlock_user(b, arg2, arg3);
  10908. return ret;
  10909. }
  10910. case TARGET_NR_flistxattr:
  10911. {
  10912. void *b = 0;
  10913. if (arg2) {
  10914. b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  10915. if (!b) {
  10916. return -TARGET_EFAULT;
  10917. }
  10918. }
  10919. ret = get_errno(flistxattr(arg1, b, arg3));
  10920. unlock_user(b, arg2, arg3);
  10921. return ret;
  10922. }
  10923. case TARGET_NR_setxattr:
  10924. case TARGET_NR_lsetxattr:
  10925. {
  10926. void *p, *n, *v = 0;
  10927. if (arg3) {
  10928. v = lock_user(VERIFY_READ, arg3, arg4, 1);
  10929. if (!v) {
  10930. return -TARGET_EFAULT;
  10931. }
  10932. }
  10933. p = lock_user_string(arg1);
  10934. n = lock_user_string(arg2);
  10935. if (p && n) {
  10936. if (num == TARGET_NR_setxattr) {
  10937. ret = get_errno(setxattr(p, n, v, arg4, arg5));
  10938. } else {
  10939. ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
  10940. }
  10941. } else {
  10942. ret = -TARGET_EFAULT;
  10943. }
  10944. unlock_user(p, arg1, 0);
  10945. unlock_user(n, arg2, 0);
  10946. unlock_user(v, arg3, 0);
  10947. }
  10948. return ret;
  10949. case TARGET_NR_fsetxattr:
  10950. {
  10951. void *n, *v = 0;
  10952. if (arg3) {
  10953. v = lock_user(VERIFY_READ, arg3, arg4, 1);
  10954. if (!v) {
  10955. return -TARGET_EFAULT;
  10956. }
  10957. }
  10958. n = lock_user_string(arg2);
  10959. if (n) {
  10960. ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
  10961. } else {
  10962. ret = -TARGET_EFAULT;
  10963. }
  10964. unlock_user(n, arg2, 0);
  10965. unlock_user(v, arg3, 0);
  10966. }
  10967. return ret;
  10968. case TARGET_NR_getxattr:
  10969. case TARGET_NR_lgetxattr:
  10970. {
  10971. void *p, *n, *v = 0;
  10972. if (arg3) {
  10973. v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  10974. if (!v) {
  10975. return -TARGET_EFAULT;
  10976. }
  10977. }
  10978. p = lock_user_string(arg1);
  10979. n = lock_user_string(arg2);
  10980. if (p && n) {
  10981. if (num == TARGET_NR_getxattr) {
  10982. ret = get_errno(getxattr(p, n, v, arg4));
  10983. } else {
  10984. ret = get_errno(lgetxattr(p, n, v, arg4));
  10985. }
  10986. } else {
  10987. ret = -TARGET_EFAULT;
  10988. }
  10989. unlock_user(p, arg1, 0);
  10990. unlock_user(n, arg2, 0);
  10991. unlock_user(v, arg3, arg4);
  10992. }
  10993. return ret;
  10994. case TARGET_NR_fgetxattr:
  10995. {
  10996. void *n, *v = 0;
  10997. if (arg3) {
  10998. v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  10999. if (!v) {
  11000. return -TARGET_EFAULT;
  11001. }
  11002. }
  11003. n = lock_user_string(arg2);
  11004. if (n) {
  11005. ret = get_errno(fgetxattr(arg1, n, v, arg4));
  11006. } else {
  11007. ret = -TARGET_EFAULT;
  11008. }
  11009. unlock_user(n, arg2, 0);
  11010. unlock_user(v, arg3, arg4);
  11011. }
  11012. return ret;
  11013. case TARGET_NR_removexattr:
  11014. case TARGET_NR_lremovexattr:
  11015. {
  11016. void *p, *n;
  11017. p = lock_user_string(arg1);
  11018. n = lock_user_string(arg2);
  11019. if (p && n) {
  11020. if (num == TARGET_NR_removexattr) {
  11021. ret = get_errno(removexattr(p, n));
  11022. } else {
  11023. ret = get_errno(lremovexattr(p, n));
  11024. }
  11025. } else {
  11026. ret = -TARGET_EFAULT;
  11027. }
  11028. unlock_user(p, arg1, 0);
  11029. unlock_user(n, arg2, 0);
  11030. }
  11031. return ret;
  11032. case TARGET_NR_fremovexattr:
  11033. {
  11034. void *n;
  11035. n = lock_user_string(arg2);
  11036. if (n) {
  11037. ret = get_errno(fremovexattr(arg1, n));
  11038. } else {
  11039. ret = -TARGET_EFAULT;
  11040. }
  11041. unlock_user(n, arg2, 0);
  11042. }
  11043. return ret;
  11044. #endif
  11045. #endif /* CONFIG_ATTR */
  11046. #ifdef TARGET_NR_set_thread_area
  11047. case TARGET_NR_set_thread_area:
  11048. #if defined(TARGET_MIPS)
  11049. ((CPUMIPSState *) cpu_env)->active_tc.CP0_UserLocal = arg1;
  11050. return 0;
  11051. #elif defined(TARGET_CRIS)
  11052. if (arg1 & 0xff)
  11053. ret = -TARGET_EINVAL;
  11054. else {
  11055. ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
  11056. ret = 0;
  11057. }
  11058. return ret;
  11059. #elif defined(TARGET_I386) && defined(TARGET_ABI32)
  11060. return do_set_thread_area(cpu_env, arg1);
  11061. #elif defined(TARGET_M68K)
  11062. {
  11063. TaskState *ts = cpu->opaque;
  11064. ts->tp_value = arg1;
  11065. return 0;
  11066. }
  11067. #else
  11068. return -TARGET_ENOSYS;
  11069. #endif
  11070. #endif
  11071. #ifdef TARGET_NR_get_thread_area
  11072. case TARGET_NR_get_thread_area:
  11073. #if defined(TARGET_I386) && defined(TARGET_ABI32)
  11074. return do_get_thread_area(cpu_env, arg1);
  11075. #elif defined(TARGET_M68K)
  11076. {
  11077. TaskState *ts = cpu->opaque;
  11078. return ts->tp_value;
  11079. }
  11080. #else
  11081. return -TARGET_ENOSYS;
  11082. #endif
  11083. #endif
  11084. #ifdef TARGET_NR_getdomainname
  11085. case TARGET_NR_getdomainname:
  11086. return -TARGET_ENOSYS;
  11087. #endif
  11088. #ifdef TARGET_NR_clock_settime
  11089. case TARGET_NR_clock_settime:
  11090. {
  11091. struct timespec ts;
  11092. ret = target_to_host_timespec(&ts, arg2);
  11093. if (!is_error(ret)) {
  11094. ret = get_errno(clock_settime(arg1, &ts));
  11095. }
  11096. return ret;
  11097. }
  11098. #endif
  11099. #ifdef TARGET_NR_clock_settime64
  11100. case TARGET_NR_clock_settime64:
  11101. {
  11102. struct timespec ts;
  11103. ret = target_to_host_timespec64(&ts, arg2);
  11104. if (!is_error(ret)) {
  11105. ret = get_errno(clock_settime(arg1, &ts));
  11106. }
  11107. return ret;
  11108. }
  11109. #endif
  11110. #ifdef TARGET_NR_clock_gettime
  11111. case TARGET_NR_clock_gettime:
  11112. {
  11113. struct timespec ts;
  11114. ret = get_errno(clock_gettime(arg1, &ts));
  11115. if (!is_error(ret)) {
  11116. ret = host_to_target_timespec(arg2, &ts);
  11117. }
  11118. return ret;
  11119. }
  11120. #endif
  11121. #ifdef TARGET_NR_clock_gettime64
  11122. case TARGET_NR_clock_gettime64:
  11123. {
  11124. struct timespec ts;
  11125. ret = get_errno(clock_gettime(arg1, &ts));
  11126. if (!is_error(ret)) {
  11127. ret = host_to_target_timespec64(arg2, &ts);
  11128. }
  11129. return ret;
  11130. }
  11131. #endif
  11132. #ifdef TARGET_NR_clock_getres
  11133. case TARGET_NR_clock_getres:
  11134. {
  11135. struct timespec ts;
  11136. ret = get_errno(clock_getres(arg1, &ts));
  11137. if (!is_error(ret)) {
  11138. host_to_target_timespec(arg2, &ts);
  11139. }
  11140. return ret;
  11141. }
  11142. #endif
  11143. #ifdef TARGET_NR_clock_getres_time64
  11144. case TARGET_NR_clock_getres_time64:
  11145. {
  11146. struct timespec ts;
  11147. ret = get_errno(clock_getres(arg1, &ts));
  11148. if (!is_error(ret)) {
  11149. host_to_target_timespec64(arg2, &ts);
  11150. }
  11151. return ret;
  11152. }
  11153. #endif
  11154. #ifdef TARGET_NR_clock_nanosleep
  11155. case TARGET_NR_clock_nanosleep:
  11156. {
  11157. struct timespec ts;
  11158. if (target_to_host_timespec(&ts, arg3)) {
  11159. return -TARGET_EFAULT;
  11160. }
  11161. ret = get_errno(safe_clock_nanosleep(arg1, arg2,
  11162. &ts, arg4 ? &ts : NULL));
  11163. /*
  11164. * if the call is interrupted by a signal handler, it fails
  11165. * with error -TARGET_EINTR and if arg4 is not NULL and arg2 is not
  11166. * TIMER_ABSTIME, it returns the remaining unslept time in arg4.
  11167. */
  11168. if (ret == -TARGET_EINTR && arg4 && arg2 != TIMER_ABSTIME &&
  11169. host_to_target_timespec(arg4, &ts)) {
  11170. return -TARGET_EFAULT;
  11171. }
  11172. return ret;
  11173. }
  11174. #endif
  11175. #ifdef TARGET_NR_clock_nanosleep_time64
  11176. case TARGET_NR_clock_nanosleep_time64:
  11177. {
  11178. struct timespec ts;
  11179. if (target_to_host_timespec64(&ts, arg3)) {
  11180. return -TARGET_EFAULT;
  11181. }
  11182. ret = get_errno(safe_clock_nanosleep(arg1, arg2,
  11183. &ts, arg4 ? &ts : NULL));
  11184. if (ret == -TARGET_EINTR && arg4 && arg2 != TIMER_ABSTIME &&
  11185. host_to_target_timespec64(arg4, &ts)) {
  11186. return -TARGET_EFAULT;
  11187. }
  11188. return ret;
  11189. }
  11190. #endif
  11191. #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
  11192. case TARGET_NR_set_tid_address:
  11193. return get_errno(set_tid_address((int *)g2h(arg1)));
  11194. #endif
  11195. case TARGET_NR_tkill:
  11196. return get_errno(safe_tkill((int)arg1, target_to_host_signal(arg2)));
  11197. case TARGET_NR_tgkill:
  11198. return get_errno(safe_tgkill((int)arg1, (int)arg2,
  11199. target_to_host_signal(arg3)));
  11200. #ifdef TARGET_NR_set_robust_list
  11201. case TARGET_NR_set_robust_list:
  11202. case TARGET_NR_get_robust_list:
  11203. /* The ABI for supporting robust futexes has userspace pass
  11204. * the kernel a pointer to a linked list which is updated by
  11205. * userspace after the syscall; the list is walked by the kernel
  11206. * when the thread exits. Since the linked list in QEMU guest
  11207. * memory isn't a valid linked list for the host and we have
  11208. * no way to reliably intercept the thread-death event, we can't
  11209. * support these. Silently return ENOSYS so that guest userspace
  11210. * falls back to a non-robust futex implementation (which should
  11211. * be OK except in the corner case of the guest crashing while
  11212. * holding a mutex that is shared with another process via
  11213. * shared memory).
  11214. */
  11215. return -TARGET_ENOSYS;
  11216. #endif
  11217. #if defined(TARGET_NR_utimensat)
  11218. case TARGET_NR_utimensat:
  11219. {
  11220. struct timespec *tsp, ts[2];
  11221. if (!arg3) {
  11222. tsp = NULL;
  11223. } else {
  11224. if (target_to_host_timespec(ts, arg3)) {
  11225. return -TARGET_EFAULT;
  11226. }
  11227. if (target_to_host_timespec(ts + 1, arg3 +
  11228. sizeof(struct target_timespec))) {
  11229. return -TARGET_EFAULT;
  11230. }
  11231. tsp = ts;
  11232. }
  11233. if (!arg2)
  11234. ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
  11235. else {
  11236. if (!(p = lock_user_string(arg2))) {
  11237. return -TARGET_EFAULT;
  11238. }
  11239. ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
  11240. unlock_user(p, arg2, 0);
  11241. }
  11242. }
  11243. return ret;
  11244. #endif
  11245. #ifdef TARGET_NR_utimensat_time64
  11246. case TARGET_NR_utimensat_time64:
  11247. {
  11248. struct timespec *tsp, ts[2];
  11249. if (!arg3) {
  11250. tsp = NULL;
  11251. } else {
  11252. if (target_to_host_timespec64(ts, arg3)) {
  11253. return -TARGET_EFAULT;
  11254. }
  11255. if (target_to_host_timespec64(ts + 1, arg3 +
  11256. sizeof(struct target__kernel_timespec))) {
  11257. return -TARGET_EFAULT;
  11258. }
  11259. tsp = ts;
  11260. }
  11261. if (!arg2)
  11262. ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
  11263. else {
  11264. p = lock_user_string(arg2);
  11265. if (!p) {
  11266. return -TARGET_EFAULT;
  11267. }
  11268. ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
  11269. unlock_user(p, arg2, 0);
  11270. }
  11271. }
  11272. return ret;
  11273. #endif
  11274. #ifdef TARGET_NR_futex
  11275. case TARGET_NR_futex:
  11276. return do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
  11277. #endif
  11278. #ifdef TARGET_NR_futex_time64
  11279. case TARGET_NR_futex_time64:
  11280. return do_futex_time64(arg1, arg2, arg3, arg4, arg5, arg6);
  11281. #endif
  11282. #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
  11283. case TARGET_NR_inotify_init:
  11284. ret = get_errno(sys_inotify_init());
  11285. if (ret >= 0) {
  11286. fd_trans_register(ret, &target_inotify_trans);
  11287. }
  11288. return ret;
  11289. #endif
  11290. #ifdef CONFIG_INOTIFY1
  11291. #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
  11292. case TARGET_NR_inotify_init1:
  11293. ret = get_errno(sys_inotify_init1(target_to_host_bitmask(arg1,
  11294. fcntl_flags_tbl)));
  11295. if (ret >= 0) {
  11296. fd_trans_register(ret, &target_inotify_trans);
  11297. }
  11298. return ret;
  11299. #endif
  11300. #endif
  11301. #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
  11302. case TARGET_NR_inotify_add_watch:
  11303. p = lock_user_string(arg2);
  11304. ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
  11305. unlock_user(p, arg2, 0);
  11306. return ret;
  11307. #endif
  11308. #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
  11309. case TARGET_NR_inotify_rm_watch:
  11310. return get_errno(sys_inotify_rm_watch(arg1, arg2));
  11311. #endif
  11312. #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
  11313. case TARGET_NR_mq_open:
  11314. {
  11315. struct mq_attr posix_mq_attr;
  11316. struct mq_attr *pposix_mq_attr;
  11317. int host_flags;
  11318. host_flags = target_to_host_bitmask(arg2, fcntl_flags_tbl);
  11319. pposix_mq_attr = NULL;
  11320. if (arg4) {
  11321. if (copy_from_user_mq_attr(&posix_mq_attr, arg4) != 0) {
  11322. return -TARGET_EFAULT;
  11323. }
  11324. pposix_mq_attr = &posix_mq_attr;
  11325. }
  11326. p = lock_user_string(arg1 - 1);
  11327. if (!p) {
  11328. return -TARGET_EFAULT;
  11329. }
  11330. ret = get_errno(mq_open(p, host_flags, arg3, pposix_mq_attr));
  11331. unlock_user (p, arg1, 0);
  11332. }
  11333. return ret;
  11334. case TARGET_NR_mq_unlink:
  11335. p = lock_user_string(arg1 - 1);
  11336. if (!p) {
  11337. return -TARGET_EFAULT;
  11338. }
  11339. ret = get_errno(mq_unlink(p));
  11340. unlock_user (p, arg1, 0);
  11341. return ret;
  11342. #ifdef TARGET_NR_mq_timedsend
  11343. case TARGET_NR_mq_timedsend:
  11344. {
  11345. struct timespec ts;
  11346. p = lock_user (VERIFY_READ, arg2, arg3, 1);
  11347. if (arg5 != 0) {
  11348. if (target_to_host_timespec(&ts, arg5)) {
  11349. return -TARGET_EFAULT;
  11350. }
  11351. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts));
  11352. if (!is_error(ret) && host_to_target_timespec(arg5, &ts)) {
  11353. return -TARGET_EFAULT;
  11354. }
  11355. } else {
  11356. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL));
  11357. }
  11358. unlock_user (p, arg2, arg3);
  11359. }
  11360. return ret;
  11361. #endif
  11362. #ifdef TARGET_NR_mq_timedsend_time64
  11363. case TARGET_NR_mq_timedsend_time64:
  11364. {
  11365. struct timespec ts;
  11366. p = lock_user(VERIFY_READ, arg2, arg3, 1);
  11367. if (arg5 != 0) {
  11368. if (target_to_host_timespec64(&ts, arg5)) {
  11369. return -TARGET_EFAULT;
  11370. }
  11371. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts));
  11372. if (!is_error(ret) && host_to_target_timespec64(arg5, &ts)) {
  11373. return -TARGET_EFAULT;
  11374. }
  11375. } else {
  11376. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL));
  11377. }
  11378. unlock_user(p, arg2, arg3);
  11379. }
  11380. return ret;
  11381. #endif
  11382. #ifdef TARGET_NR_mq_timedreceive
  11383. case TARGET_NR_mq_timedreceive:
  11384. {
  11385. struct timespec ts;
  11386. unsigned int prio;
  11387. p = lock_user (VERIFY_READ, arg2, arg3, 1);
  11388. if (arg5 != 0) {
  11389. if (target_to_host_timespec(&ts, arg5)) {
  11390. return -TARGET_EFAULT;
  11391. }
  11392. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  11393. &prio, &ts));
  11394. if (!is_error(ret) && host_to_target_timespec(arg5, &ts)) {
  11395. return -TARGET_EFAULT;
  11396. }
  11397. } else {
  11398. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  11399. &prio, NULL));
  11400. }
  11401. unlock_user (p, arg2, arg3);
  11402. if (arg4 != 0)
  11403. put_user_u32(prio, arg4);
  11404. }
  11405. return ret;
  11406. #endif
  11407. #ifdef TARGET_NR_mq_timedreceive_time64
  11408. case TARGET_NR_mq_timedreceive_time64:
  11409. {
  11410. struct timespec ts;
  11411. unsigned int prio;
  11412. p = lock_user(VERIFY_READ, arg2, arg3, 1);
  11413. if (arg5 != 0) {
  11414. if (target_to_host_timespec64(&ts, arg5)) {
  11415. return -TARGET_EFAULT;
  11416. }
  11417. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  11418. &prio, &ts));
  11419. if (!is_error(ret) && host_to_target_timespec64(arg5, &ts)) {
  11420. return -TARGET_EFAULT;
  11421. }
  11422. } else {
  11423. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  11424. &prio, NULL));
  11425. }
  11426. unlock_user(p, arg2, arg3);
  11427. if (arg4 != 0) {
  11428. put_user_u32(prio, arg4);
  11429. }
  11430. }
  11431. return ret;
  11432. #endif
  11433. /* Not implemented for now... */
  11434. /* case TARGET_NR_mq_notify: */
  11435. /* break; */
  11436. case TARGET_NR_mq_getsetattr:
  11437. {
  11438. struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
  11439. ret = 0;
  11440. if (arg2 != 0) {
  11441. copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
  11442. ret = get_errno(mq_setattr(arg1, &posix_mq_attr_in,
  11443. &posix_mq_attr_out));
  11444. } else if (arg3 != 0) {
  11445. ret = get_errno(mq_getattr(arg1, &posix_mq_attr_out));
  11446. }
  11447. if (ret == 0 && arg3 != 0) {
  11448. copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
  11449. }
  11450. }
  11451. return ret;
  11452. #endif
  11453. #ifdef CONFIG_SPLICE
  11454. #ifdef TARGET_NR_tee
  11455. case TARGET_NR_tee:
  11456. {
  11457. ret = get_errno(tee(arg1,arg2,arg3,arg4));
  11458. }
  11459. return ret;
  11460. #endif
  11461. #ifdef TARGET_NR_splice
  11462. case TARGET_NR_splice:
  11463. {
  11464. loff_t loff_in, loff_out;
  11465. loff_t *ploff_in = NULL, *ploff_out = NULL;
  11466. if (arg2) {
  11467. if (get_user_u64(loff_in, arg2)) {
  11468. return -TARGET_EFAULT;
  11469. }
  11470. ploff_in = &loff_in;
  11471. }
  11472. if (arg4) {
  11473. if (get_user_u64(loff_out, arg4)) {
  11474. return -TARGET_EFAULT;
  11475. }
  11476. ploff_out = &loff_out;
  11477. }
  11478. ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
  11479. if (arg2) {
  11480. if (put_user_u64(loff_in, arg2)) {
  11481. return -TARGET_EFAULT;
  11482. }
  11483. }
  11484. if (arg4) {
  11485. if (put_user_u64(loff_out, arg4)) {
  11486. return -TARGET_EFAULT;
  11487. }
  11488. }
  11489. }
  11490. return ret;
  11491. #endif
  11492. #ifdef TARGET_NR_vmsplice
  11493. case TARGET_NR_vmsplice:
  11494. {
  11495. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  11496. if (vec != NULL) {
  11497. ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
  11498. unlock_iovec(vec, arg2, arg3, 0);
  11499. } else {
  11500. ret = -host_to_target_errno(errno);
  11501. }
  11502. }
  11503. return ret;
  11504. #endif
  11505. #endif /* CONFIG_SPLICE */
  11506. #ifdef CONFIG_EVENTFD
  11507. #if defined(TARGET_NR_eventfd)
  11508. case TARGET_NR_eventfd:
  11509. ret = get_errno(eventfd(arg1, 0));
  11510. if (ret >= 0) {
  11511. fd_trans_register(ret, &target_eventfd_trans);
  11512. }
  11513. return ret;
  11514. #endif
  11515. #if defined(TARGET_NR_eventfd2)
  11516. case TARGET_NR_eventfd2:
  11517. {
  11518. int host_flags = arg2 & (~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC));
  11519. if (arg2 & TARGET_O_NONBLOCK) {
  11520. host_flags |= O_NONBLOCK;
  11521. }
  11522. if (arg2 & TARGET_O_CLOEXEC) {
  11523. host_flags |= O_CLOEXEC;
  11524. }
  11525. ret = get_errno(eventfd(arg1, host_flags));
  11526. if (ret >= 0) {
  11527. fd_trans_register(ret, &target_eventfd_trans);
  11528. }
  11529. return ret;
  11530. }
  11531. #endif
  11532. #endif /* CONFIG_EVENTFD */
  11533. #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
  11534. case TARGET_NR_fallocate:
  11535. #if TARGET_ABI_BITS == 32
  11536. ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
  11537. target_offset64(arg5, arg6)));
  11538. #else
  11539. ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
  11540. #endif
  11541. return ret;
  11542. #endif
  11543. #if defined(CONFIG_SYNC_FILE_RANGE)
  11544. #if defined(TARGET_NR_sync_file_range)
  11545. case TARGET_NR_sync_file_range:
  11546. #if TARGET_ABI_BITS == 32
  11547. #if defined(TARGET_MIPS)
  11548. ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
  11549. target_offset64(arg5, arg6), arg7));
  11550. #else
  11551. ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
  11552. target_offset64(arg4, arg5), arg6));
  11553. #endif /* !TARGET_MIPS */
  11554. #else
  11555. ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
  11556. #endif
  11557. return ret;
  11558. #endif
  11559. #if defined(TARGET_NR_sync_file_range2) || \
  11560. defined(TARGET_NR_arm_sync_file_range)
  11561. #if defined(TARGET_NR_sync_file_range2)
  11562. case TARGET_NR_sync_file_range2:
  11563. #endif
  11564. #if defined(TARGET_NR_arm_sync_file_range)
  11565. case TARGET_NR_arm_sync_file_range:
  11566. #endif
  11567. /* This is like sync_file_range but the arguments are reordered */
  11568. #if TARGET_ABI_BITS == 32
  11569. ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
  11570. target_offset64(arg5, arg6), arg2));
  11571. #else
  11572. ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
  11573. #endif
  11574. return ret;
  11575. #endif
  11576. #endif
  11577. #if defined(TARGET_NR_signalfd4)
  11578. case TARGET_NR_signalfd4:
  11579. return do_signalfd4(arg1, arg2, arg4);
  11580. #endif
  11581. #if defined(TARGET_NR_signalfd)
  11582. case TARGET_NR_signalfd:
  11583. return do_signalfd4(arg1, arg2, 0);
  11584. #endif
  11585. #if defined(CONFIG_EPOLL)
  11586. #if defined(TARGET_NR_epoll_create)
  11587. case TARGET_NR_epoll_create:
  11588. return get_errno(epoll_create(arg1));
  11589. #endif
  11590. #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
  11591. case TARGET_NR_epoll_create1:
  11592. return get_errno(epoll_create1(target_to_host_bitmask(arg1, fcntl_flags_tbl)));
  11593. #endif
  11594. #if defined(TARGET_NR_epoll_ctl)
  11595. case TARGET_NR_epoll_ctl:
  11596. {
  11597. struct epoll_event ep;
  11598. struct epoll_event *epp = 0;
  11599. if (arg4) {
  11600. struct target_epoll_event *target_ep;
  11601. if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
  11602. return -TARGET_EFAULT;
  11603. }
  11604. ep.events = tswap32(target_ep->events);
  11605. /* The epoll_data_t union is just opaque data to the kernel,
  11606. * so we transfer all 64 bits across and need not worry what
  11607. * actual data type it is.
  11608. */
  11609. ep.data.u64 = tswap64(target_ep->data.u64);
  11610. unlock_user_struct(target_ep, arg4, 0);
  11611. epp = &ep;
  11612. }
  11613. return get_errno(epoll_ctl(arg1, arg2, arg3, epp));
  11614. }
  11615. #endif
  11616. #if defined(TARGET_NR_epoll_wait) || defined(TARGET_NR_epoll_pwait)
  11617. #if defined(TARGET_NR_epoll_wait)
  11618. case TARGET_NR_epoll_wait:
  11619. #endif
  11620. #if defined(TARGET_NR_epoll_pwait)
  11621. case TARGET_NR_epoll_pwait:
  11622. #endif
  11623. {
  11624. struct target_epoll_event *target_ep;
  11625. struct epoll_event *ep;
  11626. int epfd = arg1;
  11627. int maxevents = arg3;
  11628. int timeout = arg4;
  11629. if (maxevents <= 0 || maxevents > TARGET_EP_MAX_EVENTS) {
  11630. return -TARGET_EINVAL;
  11631. }
  11632. target_ep = lock_user(VERIFY_WRITE, arg2,
  11633. maxevents * sizeof(struct target_epoll_event), 1);
  11634. if (!target_ep) {
  11635. return -TARGET_EFAULT;
  11636. }
  11637. ep = g_try_new(struct epoll_event, maxevents);
  11638. if (!ep) {
  11639. unlock_user(target_ep, arg2, 0);
  11640. return -TARGET_ENOMEM;
  11641. }
  11642. switch (num) {
  11643. #if defined(TARGET_NR_epoll_pwait)
  11644. case TARGET_NR_epoll_pwait:
  11645. {
  11646. target_sigset_t *target_set;
  11647. sigset_t _set, *set = &_set;
  11648. if (arg5) {
  11649. if (arg6 != sizeof(target_sigset_t)) {
  11650. ret = -TARGET_EINVAL;
  11651. break;
  11652. }
  11653. target_set = lock_user(VERIFY_READ, arg5,
  11654. sizeof(target_sigset_t), 1);
  11655. if (!target_set) {
  11656. ret = -TARGET_EFAULT;
  11657. break;
  11658. }
  11659. target_to_host_sigset(set, target_set);
  11660. unlock_user(target_set, arg5, 0);
  11661. } else {
  11662. set = NULL;
  11663. }
  11664. ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
  11665. set, SIGSET_T_SIZE));
  11666. break;
  11667. }
  11668. #endif
  11669. #if defined(TARGET_NR_epoll_wait)
  11670. case TARGET_NR_epoll_wait:
  11671. ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
  11672. NULL, 0));
  11673. break;
  11674. #endif
  11675. default:
  11676. ret = -TARGET_ENOSYS;
  11677. }
  11678. if (!is_error(ret)) {
  11679. int i;
  11680. for (i = 0; i < ret; i++) {
  11681. target_ep[i].events = tswap32(ep[i].events);
  11682. target_ep[i].data.u64 = tswap64(ep[i].data.u64);
  11683. }
  11684. unlock_user(target_ep, arg2,
  11685. ret * sizeof(struct target_epoll_event));
  11686. } else {
  11687. unlock_user(target_ep, arg2, 0);
  11688. }
  11689. g_free(ep);
  11690. return ret;
  11691. }
  11692. #endif
  11693. #endif
  11694. #ifdef TARGET_NR_prlimit64
  11695. case TARGET_NR_prlimit64:
  11696. {
  11697. /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
  11698. struct target_rlimit64 *target_rnew, *target_rold;
  11699. struct host_rlimit64 rnew, rold, *rnewp = 0;
  11700. int resource = target_to_host_resource(arg2);
  11701. if (arg3 && (resource != RLIMIT_AS &&
  11702. resource != RLIMIT_DATA &&
  11703. resource != RLIMIT_STACK)) {
  11704. if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
  11705. return -TARGET_EFAULT;
  11706. }
  11707. rnew.rlim_cur = tswap64(target_rnew->rlim_cur);
  11708. rnew.rlim_max = tswap64(target_rnew->rlim_max);
  11709. unlock_user_struct(target_rnew, arg3, 0);
  11710. rnewp = &rnew;
  11711. }
  11712. ret = get_errno(sys_prlimit64(arg1, resource, rnewp, arg4 ? &rold : 0));
  11713. if (!is_error(ret) && arg4) {
  11714. if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
  11715. return -TARGET_EFAULT;
  11716. }
  11717. target_rold->rlim_cur = tswap64(rold.rlim_cur);
  11718. target_rold->rlim_max = tswap64(rold.rlim_max);
  11719. unlock_user_struct(target_rold, arg4, 1);
  11720. }
  11721. return ret;
  11722. }
  11723. #endif
  11724. #ifdef TARGET_NR_gethostname
  11725. case TARGET_NR_gethostname:
  11726. {
  11727. char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
  11728. if (name) {
  11729. ret = get_errno(gethostname(name, arg2));
  11730. unlock_user(name, arg1, arg2);
  11731. } else {
  11732. ret = -TARGET_EFAULT;
  11733. }
  11734. return ret;
  11735. }
  11736. #endif
  11737. #ifdef TARGET_NR_atomic_cmpxchg_32
  11738. case TARGET_NR_atomic_cmpxchg_32:
  11739. {
  11740. /* should use start_exclusive from main.c */
  11741. abi_ulong mem_value;
  11742. if (get_user_u32(mem_value, arg6)) {
  11743. target_siginfo_t info;
  11744. info.si_signo = SIGSEGV;
  11745. info.si_errno = 0;
  11746. info.si_code = TARGET_SEGV_MAPERR;
  11747. info._sifields._sigfault._addr = arg6;
  11748. queue_signal((CPUArchState *)cpu_env, info.si_signo,
  11749. QEMU_SI_FAULT, &info);
  11750. ret = 0xdeadbeef;
  11751. }
  11752. if (mem_value == arg2)
  11753. put_user_u32(arg1, arg6);
  11754. return mem_value;
  11755. }
  11756. #endif
  11757. #ifdef TARGET_NR_atomic_barrier
  11758. case TARGET_NR_atomic_barrier:
  11759. /* Like the kernel implementation and the
  11760. qemu arm barrier, no-op this? */
  11761. return 0;
  11762. #endif
  11763. #ifdef TARGET_NR_timer_create
  11764. case TARGET_NR_timer_create:
  11765. {
  11766. /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
  11767. struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL;
  11768. int clkid = arg1;
  11769. int timer_index = next_free_host_timer();
  11770. if (timer_index < 0) {
  11771. ret = -TARGET_EAGAIN;
  11772. } else {
  11773. timer_t *phtimer = g_posix_timers + timer_index;
  11774. if (arg2) {
  11775. phost_sevp = &host_sevp;
  11776. ret = target_to_host_sigevent(phost_sevp, arg2);
  11777. if (ret != 0) {
  11778. return ret;
  11779. }
  11780. }
  11781. ret = get_errno(timer_create(clkid, phost_sevp, phtimer));
  11782. if (ret) {
  11783. phtimer = NULL;
  11784. } else {
  11785. if (put_user(TIMER_MAGIC | timer_index, arg3, target_timer_t)) {
  11786. return -TARGET_EFAULT;
  11787. }
  11788. }
  11789. }
  11790. return ret;
  11791. }
  11792. #endif
  11793. #ifdef TARGET_NR_timer_settime
  11794. case TARGET_NR_timer_settime:
  11795. {
  11796. /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
  11797. * struct itimerspec * old_value */
  11798. target_timer_t timerid = get_timer_id(arg1);
  11799. if (timerid < 0) {
  11800. ret = timerid;
  11801. } else if (arg3 == 0) {
  11802. ret = -TARGET_EINVAL;
  11803. } else {
  11804. timer_t htimer = g_posix_timers[timerid];
  11805. struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
  11806. if (target_to_host_itimerspec(&hspec_new, arg3)) {
  11807. return -TARGET_EFAULT;
  11808. }
  11809. ret = get_errno(
  11810. timer_settime(htimer, arg2, &hspec_new, &hspec_old));
  11811. if (arg4 && host_to_target_itimerspec(arg4, &hspec_old)) {
  11812. return -TARGET_EFAULT;
  11813. }
  11814. }
  11815. return ret;
  11816. }
  11817. #endif
  11818. #ifdef TARGET_NR_timer_settime64
  11819. case TARGET_NR_timer_settime64:
  11820. {
  11821. target_timer_t timerid = get_timer_id(arg1);
  11822. if (timerid < 0) {
  11823. ret = timerid;
  11824. } else if (arg3 == 0) {
  11825. ret = -TARGET_EINVAL;
  11826. } else {
  11827. timer_t htimer = g_posix_timers[timerid];
  11828. struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
  11829. if (target_to_host_itimerspec64(&hspec_new, arg3)) {
  11830. return -TARGET_EFAULT;
  11831. }
  11832. ret = get_errno(
  11833. timer_settime(htimer, arg2, &hspec_new, &hspec_old));
  11834. if (arg4 && host_to_target_itimerspec64(arg4, &hspec_old)) {
  11835. return -TARGET_EFAULT;
  11836. }
  11837. }
  11838. return ret;
  11839. }
  11840. #endif
  11841. #ifdef TARGET_NR_timer_gettime
  11842. case TARGET_NR_timer_gettime:
  11843. {
  11844. /* args: timer_t timerid, struct itimerspec *curr_value */
  11845. target_timer_t timerid = get_timer_id(arg1);
  11846. if (timerid < 0) {
  11847. ret = timerid;
  11848. } else if (!arg2) {
  11849. ret = -TARGET_EFAULT;
  11850. } else {
  11851. timer_t htimer = g_posix_timers[timerid];
  11852. struct itimerspec hspec;
  11853. ret = get_errno(timer_gettime(htimer, &hspec));
  11854. if (host_to_target_itimerspec(arg2, &hspec)) {
  11855. ret = -TARGET_EFAULT;
  11856. }
  11857. }
  11858. return ret;
  11859. }
  11860. #endif
  11861. #ifdef TARGET_NR_timer_gettime64
  11862. case TARGET_NR_timer_gettime64:
  11863. {
  11864. /* args: timer_t timerid, struct itimerspec64 *curr_value */
  11865. target_timer_t timerid = get_timer_id(arg1);
  11866. if (timerid < 0) {
  11867. ret = timerid;
  11868. } else if (!arg2) {
  11869. ret = -TARGET_EFAULT;
  11870. } else {
  11871. timer_t htimer = g_posix_timers[timerid];
  11872. struct itimerspec hspec;
  11873. ret = get_errno(timer_gettime(htimer, &hspec));
  11874. if (host_to_target_itimerspec64(arg2, &hspec)) {
  11875. ret = -TARGET_EFAULT;
  11876. }
  11877. }
  11878. return ret;
  11879. }
  11880. #endif
  11881. #ifdef TARGET_NR_timer_getoverrun
  11882. case TARGET_NR_timer_getoverrun:
  11883. {
  11884. /* args: timer_t timerid */
  11885. target_timer_t timerid = get_timer_id(arg1);
  11886. if (timerid < 0) {
  11887. ret = timerid;
  11888. } else {
  11889. timer_t htimer = g_posix_timers[timerid];
  11890. ret = get_errno(timer_getoverrun(htimer));
  11891. }
  11892. return ret;
  11893. }
  11894. #endif
  11895. #ifdef TARGET_NR_timer_delete
  11896. case TARGET_NR_timer_delete:
  11897. {
  11898. /* args: timer_t timerid */
  11899. target_timer_t timerid = get_timer_id(arg1);
  11900. if (timerid < 0) {
  11901. ret = timerid;
  11902. } else {
  11903. timer_t htimer = g_posix_timers[timerid];
  11904. ret = get_errno(timer_delete(htimer));
  11905. g_posix_timers[timerid] = 0;
  11906. }
  11907. return ret;
  11908. }
  11909. #endif
  11910. #if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
  11911. case TARGET_NR_timerfd_create:
  11912. return get_errno(timerfd_create(arg1,
  11913. target_to_host_bitmask(arg2, fcntl_flags_tbl)));
  11914. #endif
  11915. #if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
  11916. case TARGET_NR_timerfd_gettime:
  11917. {
  11918. struct itimerspec its_curr;
  11919. ret = get_errno(timerfd_gettime(arg1, &its_curr));
  11920. if (arg2 && host_to_target_itimerspec(arg2, &its_curr)) {
  11921. return -TARGET_EFAULT;
  11922. }
  11923. }
  11924. return ret;
  11925. #endif
  11926. #if defined(TARGET_NR_timerfd_gettime64) && defined(CONFIG_TIMERFD)
  11927. case TARGET_NR_timerfd_gettime64:
  11928. {
  11929. struct itimerspec its_curr;
  11930. ret = get_errno(timerfd_gettime(arg1, &its_curr));
  11931. if (arg2 && host_to_target_itimerspec64(arg2, &its_curr)) {
  11932. return -TARGET_EFAULT;
  11933. }
  11934. }
  11935. return ret;
  11936. #endif
  11937. #if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
  11938. case TARGET_NR_timerfd_settime:
  11939. {
  11940. struct itimerspec its_new, its_old, *p_new;
  11941. if (arg3) {
  11942. if (target_to_host_itimerspec(&its_new, arg3)) {
  11943. return -TARGET_EFAULT;
  11944. }
  11945. p_new = &its_new;
  11946. } else {
  11947. p_new = NULL;
  11948. }
  11949. ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
  11950. if (arg4 && host_to_target_itimerspec(arg4, &its_old)) {
  11951. return -TARGET_EFAULT;
  11952. }
  11953. }
  11954. return ret;
  11955. #endif
  11956. #if defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)
  11957. case TARGET_NR_timerfd_settime64:
  11958. {
  11959. struct itimerspec its_new, its_old, *p_new;
  11960. if (arg3) {
  11961. if (target_to_host_itimerspec64(&its_new, arg3)) {
  11962. return -TARGET_EFAULT;
  11963. }
  11964. p_new = &its_new;
  11965. } else {
  11966. p_new = NULL;
  11967. }
  11968. ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
  11969. if (arg4 && host_to_target_itimerspec64(arg4, &its_old)) {
  11970. return -TARGET_EFAULT;
  11971. }
  11972. }
  11973. return ret;
  11974. #endif
  11975. #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
  11976. case TARGET_NR_ioprio_get:
  11977. return get_errno(ioprio_get(arg1, arg2));
  11978. #endif
  11979. #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
  11980. case TARGET_NR_ioprio_set:
  11981. return get_errno(ioprio_set(arg1, arg2, arg3));
  11982. #endif
  11983. #if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
  11984. case TARGET_NR_setns:
  11985. return get_errno(setns(arg1, arg2));
  11986. #endif
  11987. #if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
  11988. case TARGET_NR_unshare:
  11989. return get_errno(unshare(arg1));
  11990. #endif
  11991. #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
  11992. case TARGET_NR_kcmp:
  11993. return get_errno(kcmp(arg1, arg2, arg3, arg4, arg5));
  11994. #endif
  11995. #ifdef TARGET_NR_swapcontext
  11996. case TARGET_NR_swapcontext:
  11997. /* PowerPC specific. */
  11998. return do_swapcontext(cpu_env, arg1, arg2, arg3);
  11999. #endif
  12000. #ifdef TARGET_NR_memfd_create
  12001. case TARGET_NR_memfd_create:
  12002. p = lock_user_string(arg1);
  12003. if (!p) {
  12004. return -TARGET_EFAULT;
  12005. }
  12006. ret = get_errno(memfd_create(p, arg2));
  12007. fd_trans_unregister(ret);
  12008. unlock_user(p, arg1, 0);
  12009. return ret;
  12010. #endif
  12011. #if defined TARGET_NR_membarrier && defined __NR_membarrier
  12012. case TARGET_NR_membarrier:
  12013. return get_errno(membarrier(arg1, arg2));
  12014. #endif
  12015. default:
  12016. qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num);
  12017. return -TARGET_ENOSYS;
  12018. }
  12019. return ret;
  12020. }
  12021. abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
  12022. abi_long arg2, abi_long arg3, abi_long arg4,
  12023. abi_long arg5, abi_long arg6, abi_long arg7,
  12024. abi_long arg8)
  12025. {
  12026. CPUState *cpu = env_cpu(cpu_env);
  12027. abi_long ret;
  12028. #ifdef DEBUG_ERESTARTSYS
  12029. /* Debug-only code for exercising the syscall-restart code paths
  12030. * in the per-architecture cpu main loops: restart every syscall
  12031. * the guest makes once before letting it through.
  12032. */
  12033. {
  12034. static bool flag;
  12035. flag = !flag;
  12036. if (flag) {
  12037. return -TARGET_ERESTARTSYS;
  12038. }
  12039. }
  12040. #endif
  12041. record_syscall_start(cpu, num, arg1,
  12042. arg2, arg3, arg4, arg5, arg6, arg7, arg8);
  12043. if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
  12044. print_syscall(cpu_env, num, arg1, arg2, arg3, arg4, arg5, arg6);
  12045. }
  12046. ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4,
  12047. arg5, arg6, arg7, arg8);
  12048. if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
  12049. print_syscall_ret(cpu_env, num, ret, arg1, arg2,
  12050. arg3, arg4, arg5, arg6);
  12051. }
  12052. record_syscall_return(cpu, num, ret);
  12053. return ret;
  12054. }