syscall.c 436 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811
  1. /*
  2. * Linux syscalls
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #define _ATFILE_SOURCE
  20. #include "qemu/osdep.h"
  21. #include "qemu/cutils.h"
  22. #include "qemu/path.h"
  23. #include "qemu/memfd.h"
  24. #include "qemu/queue.h"
  25. #include "qemu/plugin.h"
  26. #include "tcg/startup.h"
  27. #include "target_mman.h"
  28. #include "exec/page-protection.h"
  29. #include <elf.h>
  30. #include <endian.h>
  31. #include <grp.h>
  32. #include <sys/ipc.h>
  33. #include <sys/msg.h>
  34. #include <sys/wait.h>
  35. #include <sys/mount.h>
  36. #include <sys/file.h>
  37. #include <sys/fsuid.h>
  38. #include <sys/personality.h>
  39. #include <sys/prctl.h>
  40. #include <sys/resource.h>
  41. #include <sys/swap.h>
  42. #include <linux/capability.h>
  43. #include <sched.h>
  44. #include <sys/timex.h>
  45. #include <sys/socket.h>
  46. #include <linux/sockios.h>
  47. #include <sys/un.h>
  48. #include <sys/uio.h>
  49. #include <poll.h>
  50. #include <sys/times.h>
  51. #include <sys/shm.h>
  52. #include <sys/sem.h>
  53. #include <sys/statfs.h>
  54. #include <utime.h>
  55. #include <sys/sysinfo.h>
  56. #include <sys/signalfd.h>
  57. //#include <sys/user.h>
  58. #include <netinet/in.h>
  59. #include <netinet/ip.h>
  60. #include <netinet/tcp.h>
  61. #include <netinet/udp.h>
  62. #include <linux/wireless.h>
  63. #include <linux/icmp.h>
  64. #include <linux/icmpv6.h>
  65. #include <linux/if_tun.h>
  66. #include <linux/in6.h>
  67. #include <linux/errqueue.h>
  68. #include <linux/random.h>
  69. #ifdef CONFIG_TIMERFD
  70. #include <sys/timerfd.h>
  71. #endif
  72. #ifdef CONFIG_EVENTFD
  73. #include <sys/eventfd.h>
  74. #endif
  75. #ifdef CONFIG_EPOLL
  76. #include <sys/epoll.h>
  77. #endif
  78. #ifdef CONFIG_ATTR
  79. #include "qemu/xattr.h"
  80. #endif
  81. #ifdef CONFIG_SENDFILE
  82. #include <sys/sendfile.h>
  83. #endif
  84. #ifdef HAVE_SYS_KCOV_H
  85. #include <sys/kcov.h>
  86. #endif
  87. #define termios host_termios
  88. #define winsize host_winsize
  89. #define termio host_termio
  90. #define sgttyb host_sgttyb /* same as target */
  91. #define tchars host_tchars /* same as target */
  92. #define ltchars host_ltchars /* same as target */
  93. #include <linux/termios.h>
  94. #include <linux/unistd.h>
  95. #include <linux/cdrom.h>
  96. #include <linux/hdreg.h>
  97. #include <linux/soundcard.h>
  98. #include <linux/kd.h>
  99. #include <linux/mtio.h>
  100. #include <linux/fs.h>
  101. #include <linux/fd.h>
  102. #if defined(CONFIG_FIEMAP)
  103. #include <linux/fiemap.h>
  104. #endif
  105. #include <linux/fb.h>
  106. #if defined(CONFIG_USBFS)
  107. #include <linux/usbdevice_fs.h>
  108. #include <linux/usb/ch9.h>
  109. #endif
  110. #include <linux/vt.h>
  111. #include <linux/dm-ioctl.h>
  112. #include <linux/reboot.h>
  113. #include <linux/route.h>
  114. #include <linux/filter.h>
  115. #include <linux/blkpg.h>
  116. #include <netpacket/packet.h>
  117. #include <linux/netlink.h>
  118. #include <linux/if_alg.h>
  119. #include <linux/rtc.h>
  120. #include <sound/asound.h>
  121. #ifdef HAVE_BTRFS_H
  122. #include <linux/btrfs.h>
  123. #endif
  124. #ifdef HAVE_DRM_H
  125. #include <libdrm/drm.h>
  126. #include <libdrm/i915_drm.h>
  127. #endif
  128. #include "linux_loop.h"
  129. #include "uname.h"
  130. #include "qemu.h"
  131. #include "user-internals.h"
  132. #include "strace.h"
  133. #include "signal-common.h"
  134. #include "loader.h"
  135. #include "user-mmap.h"
  136. #include "user/safe-syscall.h"
  137. #include "qemu/guest-random.h"
  138. #include "qemu/selfmap.h"
  139. #include "user/syscall-trace.h"
  140. #include "special-errno.h"
  141. #include "qapi/error.h"
  142. #include "fd-trans.h"
  143. #include "cpu_loop-common.h"
  144. #ifndef CLONE_IO
  145. #define CLONE_IO 0x80000000 /* Clone io context */
  146. #endif
  147. /* We can't directly call the host clone syscall, because this will
  148. * badly confuse libc (breaking mutexes, for example). So we must
  149. * divide clone flags into:
  150. * * flag combinations that look like pthread_create()
  151. * * flag combinations that look like fork()
  152. * * flags we can implement within QEMU itself
  153. * * flags we can't support and will return an error for
  154. */
  155. /* For thread creation, all these flags must be present; for
  156. * fork, none must be present.
  157. */
  158. #define CLONE_THREAD_FLAGS \
  159. (CLONE_VM | CLONE_FS | CLONE_FILES | \
  160. CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM)
  161. /* These flags are ignored:
  162. * CLONE_DETACHED is now ignored by the kernel;
  163. * CLONE_IO is just an optimisation hint to the I/O scheduler
  164. */
  165. #define CLONE_IGNORED_FLAGS \
  166. (CLONE_DETACHED | CLONE_IO)
  167. #ifndef CLONE_PIDFD
  168. # define CLONE_PIDFD 0x00001000
  169. #endif
  170. /* Flags for fork which we can implement within QEMU itself */
  171. #define CLONE_OPTIONAL_FORK_FLAGS \
  172. (CLONE_SETTLS | CLONE_PARENT_SETTID | CLONE_PIDFD | \
  173. CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID)
  174. /* Flags for thread creation which we can implement within QEMU itself */
  175. #define CLONE_OPTIONAL_THREAD_FLAGS \
  176. (CLONE_SETTLS | CLONE_PARENT_SETTID | \
  177. CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | CLONE_PARENT)
  178. #define CLONE_INVALID_FORK_FLAGS \
  179. (~(CSIGNAL | CLONE_OPTIONAL_FORK_FLAGS | CLONE_IGNORED_FLAGS))
  180. #define CLONE_INVALID_THREAD_FLAGS \
  181. (~(CSIGNAL | CLONE_THREAD_FLAGS | CLONE_OPTIONAL_THREAD_FLAGS | \
  182. CLONE_IGNORED_FLAGS))
  183. /* CLONE_VFORK is special cased early in do_fork(). The other flag bits
  184. * have almost all been allocated. We cannot support any of
  185. * CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC,
  186. * CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, CLONE_PTRACE, CLONE_UNTRACED.
  187. * The checks against the invalid thread masks above will catch these.
  188. * (The one remaining unallocated bit is 0x1000 which used to be CLONE_PID.)
  189. */
  190. /* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
  191. * once. This exercises the codepaths for restart.
  192. */
  193. //#define DEBUG_ERESTARTSYS
  194. //#include <linux/msdos_fs.h>
  195. #define VFAT_IOCTL_READDIR_BOTH \
  196. _IOC(_IOC_READ, 'r', 1, (sizeof(struct linux_dirent) + 256) * 2)
  197. #define VFAT_IOCTL_READDIR_SHORT \
  198. _IOC(_IOC_READ, 'r', 2, (sizeof(struct linux_dirent) + 256) * 2)
  199. #undef _syscall0
  200. #undef _syscall1
  201. #undef _syscall2
  202. #undef _syscall3
  203. #undef _syscall4
  204. #undef _syscall5
  205. #undef _syscall6
  206. #define _syscall0(type,name) \
  207. static type name (void) \
  208. { \
  209. return syscall(__NR_##name); \
  210. }
  211. #define _syscall1(type,name,type1,arg1) \
  212. static type name (type1 arg1) \
  213. { \
  214. return syscall(__NR_##name, arg1); \
  215. }
  216. #define _syscall2(type,name,type1,arg1,type2,arg2) \
  217. static type name (type1 arg1,type2 arg2) \
  218. { \
  219. return syscall(__NR_##name, arg1, arg2); \
  220. }
  221. #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
  222. static type name (type1 arg1,type2 arg2,type3 arg3) \
  223. { \
  224. return syscall(__NR_##name, arg1, arg2, arg3); \
  225. }
  226. #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
  227. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
  228. { \
  229. return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
  230. }
  231. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  232. type5,arg5) \
  233. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
  234. { \
  235. return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
  236. }
  237. #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  238. type5,arg5,type6,arg6) \
  239. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
  240. type6 arg6) \
  241. { \
  242. return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
  243. }
  244. #define __NR_sys_uname __NR_uname
  245. #define __NR_sys_getcwd1 __NR_getcwd
  246. #define __NR_sys_getdents __NR_getdents
  247. #define __NR_sys_getdents64 __NR_getdents64
  248. #define __NR_sys_getpriority __NR_getpriority
  249. #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
  250. #define __NR_sys_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo
  251. #define __NR_sys_syslog __NR_syslog
  252. #if defined(__NR_futex)
  253. # define __NR_sys_futex __NR_futex
  254. #endif
  255. #if defined(__NR_futex_time64)
  256. # define __NR_sys_futex_time64 __NR_futex_time64
  257. #endif
  258. #define __NR_sys_statx __NR_statx
  259. #if defined(__alpha__) || defined(__x86_64__) || defined(__s390x__)
  260. #define __NR__llseek __NR_lseek
  261. #endif
  262. /* Newer kernel ports have llseek() instead of _llseek() */
  263. #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
  264. #define TARGET_NR__llseek TARGET_NR_llseek
  265. #endif
  266. /* some platforms need to mask more bits than just TARGET_O_NONBLOCK */
  267. #ifndef TARGET_O_NONBLOCK_MASK
  268. #define TARGET_O_NONBLOCK_MASK TARGET_O_NONBLOCK
  269. #endif
  270. #define __NR_sys_gettid __NR_gettid
  271. _syscall0(int, sys_gettid)
  272. /* For the 64-bit guest on 32-bit host case we must emulate
  273. * getdents using getdents64, because otherwise the host
  274. * might hand us back more dirent records than we can fit
  275. * into the guest buffer after structure format conversion.
  276. * Otherwise we emulate getdents with getdents if the host has it.
  277. */
  278. #if defined(__NR_getdents) && HOST_LONG_BITS >= TARGET_ABI_BITS
  279. #define EMULATE_GETDENTS_WITH_GETDENTS
  280. #endif
  281. #if defined(TARGET_NR_getdents) && defined(EMULATE_GETDENTS_WITH_GETDENTS)
  282. _syscall3(int, sys_getdents, unsigned int, fd, struct linux_dirent *, dirp, unsigned int, count);
  283. #endif
  284. #if (defined(TARGET_NR_getdents) && \
  285. !defined(EMULATE_GETDENTS_WITH_GETDENTS)) || \
  286. (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
  287. _syscall3(int, sys_getdents64, unsigned int, fd, struct linux_dirent64 *, dirp, unsigned int, count);
  288. #endif
  289. #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
  290. _syscall5(int, _llseek, unsigned int, fd, unsigned long, hi, unsigned long, lo,
  291. loff_t *, res, unsigned int, wh);
  292. #endif
  293. _syscall3(int, sys_rt_sigqueueinfo, pid_t, pid, int, sig, siginfo_t *, uinfo)
  294. _syscall4(int, sys_rt_tgsigqueueinfo, pid_t, pid, pid_t, tid, int, sig,
  295. siginfo_t *, uinfo)
  296. _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
  297. #ifdef __NR_exit_group
  298. _syscall1(int,exit_group,int,error_code)
  299. #endif
  300. #if defined(__NR_close_range) && defined(TARGET_NR_close_range)
  301. #define __NR_sys_close_range __NR_close_range
  302. _syscall3(int,sys_close_range,int,first,int,last,int,flags)
  303. #ifndef CLOSE_RANGE_CLOEXEC
  304. #define CLOSE_RANGE_CLOEXEC (1U << 2)
  305. #endif
  306. #endif
  307. #if defined(__NR_futex)
  308. _syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
  309. const struct timespec *,timeout,int *,uaddr2,int,val3)
  310. #endif
  311. #if defined(__NR_futex_time64)
  312. _syscall6(int,sys_futex_time64,int *,uaddr,int,op,int,val,
  313. const struct timespec *,timeout,int *,uaddr2,int,val3)
  314. #endif
  315. #if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
  316. _syscall2(int, pidfd_open, pid_t, pid, unsigned int, flags);
  317. #endif
  318. #if defined(__NR_pidfd_send_signal) && defined(TARGET_NR_pidfd_send_signal)
  319. _syscall4(int, pidfd_send_signal, int, pidfd, int, sig, siginfo_t *, info,
  320. unsigned int, flags);
  321. #endif
  322. #if defined(__NR_pidfd_getfd) && defined(TARGET_NR_pidfd_getfd)
  323. _syscall3(int, pidfd_getfd, int, pidfd, int, targetfd, unsigned int, flags);
  324. #endif
  325. #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
  326. _syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
  327. unsigned long *, user_mask_ptr);
  328. #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
  329. _syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
  330. unsigned long *, user_mask_ptr);
  331. /* sched_attr is not defined in glibc */
  332. struct sched_attr {
  333. uint32_t size;
  334. uint32_t sched_policy;
  335. uint64_t sched_flags;
  336. int32_t sched_nice;
  337. uint32_t sched_priority;
  338. uint64_t sched_runtime;
  339. uint64_t sched_deadline;
  340. uint64_t sched_period;
  341. uint32_t sched_util_min;
  342. uint32_t sched_util_max;
  343. };
  344. #define __NR_sys_sched_getattr __NR_sched_getattr
  345. _syscall4(int, sys_sched_getattr, pid_t, pid, struct sched_attr *, attr,
  346. unsigned int, size, unsigned int, flags);
  347. #define __NR_sys_sched_setattr __NR_sched_setattr
  348. _syscall3(int, sys_sched_setattr, pid_t, pid, struct sched_attr *, attr,
  349. unsigned int, flags);
  350. #define __NR_sys_sched_getscheduler __NR_sched_getscheduler
  351. _syscall1(int, sys_sched_getscheduler, pid_t, pid);
  352. #define __NR_sys_sched_setscheduler __NR_sched_setscheduler
  353. _syscall3(int, sys_sched_setscheduler, pid_t, pid, int, policy,
  354. const struct sched_param *, param);
  355. #define __NR_sys_sched_getparam __NR_sched_getparam
  356. _syscall2(int, sys_sched_getparam, pid_t, pid,
  357. struct sched_param *, param);
  358. #define __NR_sys_sched_setparam __NR_sched_setparam
  359. _syscall2(int, sys_sched_setparam, pid_t, pid,
  360. const struct sched_param *, param);
  361. #define __NR_sys_getcpu __NR_getcpu
  362. _syscall3(int, sys_getcpu, unsigned *, cpu, unsigned *, node, void *, tcache);
  363. _syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
  364. void *, arg);
  365. _syscall2(int, capget, struct __user_cap_header_struct *, header,
  366. struct __user_cap_data_struct *, data);
  367. _syscall2(int, capset, struct __user_cap_header_struct *, header,
  368. struct __user_cap_data_struct *, data);
  369. #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
  370. _syscall2(int, ioprio_get, int, which, int, who)
  371. #endif
  372. #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
  373. _syscall3(int, ioprio_set, int, which, int, who, int, ioprio)
  374. #endif
  375. #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
  376. _syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags)
  377. #endif
  378. #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
  379. _syscall5(int, kcmp, pid_t, pid1, pid_t, pid2, int, type,
  380. unsigned long, idx1, unsigned long, idx2)
  381. #endif
  382. /*
  383. * It is assumed that struct statx is architecture independent.
  384. */
  385. #if defined(TARGET_NR_statx) && defined(__NR_statx)
  386. _syscall5(int, sys_statx, int, dirfd, const char *, pathname, int, flags,
  387. unsigned int, mask, struct target_statx *, statxbuf)
  388. #endif
  389. #if defined(TARGET_NR_membarrier) && defined(__NR_membarrier)
  390. _syscall2(int, membarrier, int, cmd, int, flags)
  391. #endif
  392. static const bitmask_transtbl fcntl_flags_tbl[] = {
  393. { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
  394. { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
  395. { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
  396. { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
  397. { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
  398. { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
  399. { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
  400. { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
  401. { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
  402. { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
  403. { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
  404. { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
  405. { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
  406. #if defined(O_DIRECT)
  407. { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
  408. #endif
  409. #if defined(O_NOATIME)
  410. { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
  411. #endif
  412. #if defined(O_CLOEXEC)
  413. { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
  414. #endif
  415. #if defined(O_PATH)
  416. { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
  417. #endif
  418. #if defined(O_TMPFILE)
  419. { TARGET_O_TMPFILE, TARGET_O_TMPFILE, O_TMPFILE, O_TMPFILE },
  420. #endif
  421. /* Don't terminate the list prematurely on 64-bit host+guest. */
  422. #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
  423. { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
  424. #endif
  425. };
  426. _syscall2(int, sys_getcwd1, char *, buf, size_t, size)
  427. #if defined(TARGET_NR_utimensat) || defined(TARGET_NR_utimensat_time64)
  428. #if defined(__NR_utimensat)
  429. #define __NR_sys_utimensat __NR_utimensat
  430. _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
  431. const struct timespec *,tsp,int,flags)
  432. #else
  433. static int sys_utimensat(int dirfd, const char *pathname,
  434. const struct timespec times[2], int flags)
  435. {
  436. errno = ENOSYS;
  437. return -1;
  438. }
  439. #endif
  440. #endif /* TARGET_NR_utimensat */
  441. #ifdef TARGET_NR_renameat2
  442. #if defined(__NR_renameat2)
  443. #define __NR_sys_renameat2 __NR_renameat2
  444. _syscall5(int, sys_renameat2, int, oldfd, const char *, old, int, newfd,
  445. const char *, new, unsigned int, flags)
  446. #else
  447. static int sys_renameat2(int oldfd, const char *old,
  448. int newfd, const char *new, int flags)
  449. {
  450. if (flags == 0) {
  451. return renameat(oldfd, old, newfd, new);
  452. }
  453. errno = ENOSYS;
  454. return -1;
  455. }
  456. #endif
  457. #endif /* TARGET_NR_renameat2 */
  458. #ifdef CONFIG_INOTIFY
  459. #include <sys/inotify.h>
  460. #else
  461. /* Userspace can usually survive runtime without inotify */
  462. #undef TARGET_NR_inotify_init
  463. #undef TARGET_NR_inotify_init1
  464. #undef TARGET_NR_inotify_add_watch
  465. #undef TARGET_NR_inotify_rm_watch
  466. #endif /* CONFIG_INOTIFY */
  467. #if defined(TARGET_NR_prlimit64)
  468. #ifndef __NR_prlimit64
  469. # define __NR_prlimit64 -1
  470. #endif
  471. #define __NR_sys_prlimit64 __NR_prlimit64
  472. /* The glibc rlimit structure may not be that used by the underlying syscall */
  473. struct host_rlimit64 {
  474. uint64_t rlim_cur;
  475. uint64_t rlim_max;
  476. };
  477. _syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
  478. const struct host_rlimit64 *, new_limit,
  479. struct host_rlimit64 *, old_limit)
  480. #endif
  481. #if defined(TARGET_NR_timer_create)
  482. /* Maximum of 32 active POSIX timers allowed at any one time. */
  483. #define GUEST_TIMER_MAX 32
  484. static timer_t g_posix_timers[GUEST_TIMER_MAX];
  485. static int g_posix_timer_allocated[GUEST_TIMER_MAX];
  486. static inline int next_free_host_timer(void)
  487. {
  488. int k;
  489. for (k = 0; k < ARRAY_SIZE(g_posix_timer_allocated); k++) {
  490. if (qatomic_xchg(g_posix_timer_allocated + k, 1) == 0) {
  491. return k;
  492. }
  493. }
  494. return -1;
  495. }
  496. static inline void free_host_timer_slot(int id)
  497. {
  498. qatomic_store_release(g_posix_timer_allocated + id, 0);
  499. }
  500. #endif
  501. static inline int host_to_target_errno(int host_errno)
  502. {
  503. switch (host_errno) {
  504. #define E(X) case X: return TARGET_##X;
  505. #include "errnos.c.inc"
  506. #undef E
  507. default:
  508. return host_errno;
  509. }
  510. }
  511. static inline int target_to_host_errno(int target_errno)
  512. {
  513. switch (target_errno) {
  514. #define E(X) case TARGET_##X: return X;
  515. #include "errnos.c.inc"
  516. #undef E
  517. default:
  518. return target_errno;
  519. }
  520. }
  521. abi_long get_errno(abi_long ret)
  522. {
  523. if (ret == -1)
  524. return -host_to_target_errno(errno);
  525. else
  526. return ret;
  527. }
  528. const char *target_strerror(int err)
  529. {
  530. if (err == QEMU_ERESTARTSYS) {
  531. return "To be restarted";
  532. }
  533. if (err == QEMU_ESIGRETURN) {
  534. return "Successful exit from sigreturn";
  535. }
  536. return strerror(target_to_host_errno(err));
  537. }
  538. static int check_zeroed_user(abi_long addr, size_t ksize, size_t usize)
  539. {
  540. int i;
  541. uint8_t b;
  542. if (usize <= ksize) {
  543. return 1;
  544. }
  545. for (i = ksize; i < usize; i++) {
  546. if (get_user_u8(b, addr + i)) {
  547. return -TARGET_EFAULT;
  548. }
  549. if (b != 0) {
  550. return 0;
  551. }
  552. }
  553. return 1;
  554. }
  555. #define safe_syscall0(type, name) \
  556. static type safe_##name(void) \
  557. { \
  558. return safe_syscall(__NR_##name); \
  559. }
  560. #define safe_syscall1(type, name, type1, arg1) \
  561. static type safe_##name(type1 arg1) \
  562. { \
  563. return safe_syscall(__NR_##name, arg1); \
  564. }
  565. #define safe_syscall2(type, name, type1, arg1, type2, arg2) \
  566. static type safe_##name(type1 arg1, type2 arg2) \
  567. { \
  568. return safe_syscall(__NR_##name, arg1, arg2); \
  569. }
  570. #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
  571. static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
  572. { \
  573. return safe_syscall(__NR_##name, arg1, arg2, arg3); \
  574. }
  575. #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
  576. type4, arg4) \
  577. static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
  578. { \
  579. return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
  580. }
  581. #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
  582. type4, arg4, type5, arg5) \
  583. static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  584. type5 arg5) \
  585. { \
  586. return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
  587. }
  588. #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
  589. type4, arg4, type5, arg5, type6, arg6) \
  590. static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  591. type5 arg5, type6 arg6) \
  592. { \
  593. return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
  594. }
  595. safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count)
  596. safe_syscall3(ssize_t, write, int, fd, const void *, buff, size_t, count)
  597. safe_syscall4(int, openat, int, dirfd, const char *, pathname, \
  598. int, flags, mode_t, mode)
  599. #if defined(TARGET_NR_wait4) || defined(TARGET_NR_waitpid)
  600. safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \
  601. struct rusage *, rusage)
  602. #endif
  603. safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \
  604. int, options, struct rusage *, rusage)
  605. safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp)
  606. safe_syscall5(int, execveat, int, dirfd, const char *, filename,
  607. char **, argv, char **, envp, int, flags)
  608. #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
  609. defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
  610. safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \
  611. fd_set *, exceptfds, struct timespec *, timeout, void *, sig)
  612. #endif
  613. #if defined(TARGET_NR_ppoll) || defined(TARGET_NR_ppoll_time64)
  614. safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds,
  615. struct timespec *, tsp, const sigset_t *, sigmask,
  616. size_t, sigsetsize)
  617. #endif
  618. safe_syscall6(int, epoll_pwait, int, epfd, struct epoll_event *, events,
  619. int, maxevents, int, timeout, const sigset_t *, sigmask,
  620. size_t, sigsetsize)
  621. #if defined(__NR_futex)
  622. safe_syscall6(int,futex,int *,uaddr,int,op,int,val, \
  623. const struct timespec *,timeout,int *,uaddr2,int,val3)
  624. #endif
  625. #if defined(__NR_futex_time64)
  626. safe_syscall6(int,futex_time64,int *,uaddr,int,op,int,val, \
  627. const struct timespec *,timeout,int *,uaddr2,int,val3)
  628. #endif
  629. safe_syscall2(int, rt_sigsuspend, sigset_t *, newset, size_t, sigsetsize)
  630. safe_syscall2(int, kill, pid_t, pid, int, sig)
  631. safe_syscall2(int, tkill, int, tid, int, sig)
  632. safe_syscall3(int, tgkill, int, tgid, int, pid, int, sig)
  633. safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt)
  634. safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt)
  635. safe_syscall5(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt,
  636. unsigned long, pos_l, unsigned long, pos_h)
  637. safe_syscall5(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, iovcnt,
  638. unsigned long, pos_l, unsigned long, pos_h)
  639. safe_syscall3(int, connect, int, fd, const struct sockaddr *, addr,
  640. socklen_t, addrlen)
  641. safe_syscall6(ssize_t, sendto, int, fd, const void *, buf, size_t, len,
  642. int, flags, const struct sockaddr *, addr, socklen_t, addrlen)
  643. safe_syscall6(ssize_t, recvfrom, int, fd, void *, buf, size_t, len,
  644. int, flags, struct sockaddr *, addr, socklen_t *, addrlen)
  645. safe_syscall3(ssize_t, sendmsg, int, fd, const struct msghdr *, msg, int, flags)
  646. safe_syscall3(ssize_t, recvmsg, int, fd, struct msghdr *, msg, int, flags)
  647. safe_syscall2(int, flock, int, fd, int, operation)
  648. #if defined(TARGET_NR_rt_sigtimedwait) || defined(TARGET_NR_rt_sigtimedwait_time64)
  649. safe_syscall4(int, rt_sigtimedwait, const sigset_t *, these, siginfo_t *, uinfo,
  650. const struct timespec *, uts, size_t, sigsetsize)
  651. #endif
  652. safe_syscall4(int, accept4, int, fd, struct sockaddr *, addr, socklen_t *, len,
  653. int, flags)
  654. #if defined(TARGET_NR_nanosleep)
  655. safe_syscall2(int, nanosleep, const struct timespec *, req,
  656. struct timespec *, rem)
  657. #endif
  658. #if defined(TARGET_NR_clock_nanosleep) || \
  659. defined(TARGET_NR_clock_nanosleep_time64)
  660. safe_syscall4(int, clock_nanosleep, const clockid_t, clock, int, flags,
  661. const struct timespec *, req, struct timespec *, rem)
  662. #endif
  663. #ifdef __NR_ipc
  664. #ifdef __s390x__
  665. safe_syscall5(int, ipc, int, call, long, first, long, second, long, third,
  666. void *, ptr)
  667. #else
  668. safe_syscall6(int, ipc, int, call, long, first, long, second, long, third,
  669. void *, ptr, long, fifth)
  670. #endif
  671. #endif
  672. #ifdef __NR_msgsnd
  673. safe_syscall4(int, msgsnd, int, msgid, const void *, msgp, size_t, sz,
  674. int, flags)
  675. #endif
  676. #ifdef __NR_msgrcv
  677. safe_syscall5(int, msgrcv, int, msgid, void *, msgp, size_t, sz,
  678. long, msgtype, int, flags)
  679. #endif
  680. #ifdef __NR_semtimedop
  681. safe_syscall4(int, semtimedop, int, semid, struct sembuf *, tsops,
  682. unsigned, nsops, const struct timespec *, timeout)
  683. #endif
  684. #if defined(TARGET_NR_mq_timedsend) || \
  685. defined(TARGET_NR_mq_timedsend_time64)
  686. safe_syscall5(int, mq_timedsend, int, mqdes, const char *, msg_ptr,
  687. size_t, len, unsigned, prio, const struct timespec *, timeout)
  688. #endif
  689. #if defined(TARGET_NR_mq_timedreceive) || \
  690. defined(TARGET_NR_mq_timedreceive_time64)
  691. safe_syscall5(int, mq_timedreceive, int, mqdes, char *, msg_ptr,
  692. size_t, len, unsigned *, prio, const struct timespec *, timeout)
  693. #endif
  694. #if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range)
  695. safe_syscall6(ssize_t, copy_file_range, int, infd, loff_t *, pinoff,
  696. int, outfd, loff_t *, poutoff, size_t, length,
  697. unsigned int, flags)
  698. #endif
  699. /* We do ioctl like this rather than via safe_syscall3 to preserve the
  700. * "third argument might be integer or pointer or not present" behaviour of
  701. * the libc function.
  702. */
  703. #define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
  704. /* Similarly for fcntl. Note that callers must always:
  705. * pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK
  706. * use the flock64 struct rather than unsuffixed flock
  707. * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts.
  708. */
  709. #ifdef __NR_fcntl64
  710. #define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
  711. #else
  712. #define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
  713. #endif
  714. static inline int host_to_target_sock_type(int host_type)
  715. {
  716. int target_type;
  717. switch (host_type & 0xf /* SOCK_TYPE_MASK */) {
  718. case SOCK_DGRAM:
  719. target_type = TARGET_SOCK_DGRAM;
  720. break;
  721. case SOCK_STREAM:
  722. target_type = TARGET_SOCK_STREAM;
  723. break;
  724. default:
  725. target_type = host_type & 0xf /* SOCK_TYPE_MASK */;
  726. break;
  727. }
  728. #if defined(SOCK_CLOEXEC)
  729. if (host_type & SOCK_CLOEXEC) {
  730. target_type |= TARGET_SOCK_CLOEXEC;
  731. }
  732. #endif
  733. #if defined(SOCK_NONBLOCK)
  734. if (host_type & SOCK_NONBLOCK) {
  735. target_type |= TARGET_SOCK_NONBLOCK;
  736. }
  737. #endif
  738. return target_type;
  739. }
  740. static abi_ulong target_brk, initial_target_brk;
  741. void target_set_brk(abi_ulong new_brk)
  742. {
  743. target_brk = TARGET_PAGE_ALIGN(new_brk);
  744. initial_target_brk = target_brk;
  745. }
  746. /* do_brk() must return target values and target errnos. */
  747. abi_long do_brk(abi_ulong brk_val)
  748. {
  749. abi_long mapped_addr;
  750. abi_ulong new_brk;
  751. abi_ulong old_brk;
  752. /* brk pointers are always untagged */
  753. /* do not allow to shrink below initial brk value */
  754. if (brk_val < initial_target_brk) {
  755. return target_brk;
  756. }
  757. new_brk = TARGET_PAGE_ALIGN(brk_val);
  758. old_brk = TARGET_PAGE_ALIGN(target_brk);
  759. /* new and old target_brk might be on the same page */
  760. if (new_brk == old_brk) {
  761. target_brk = brk_val;
  762. return target_brk;
  763. }
  764. /* Release heap if necessary */
  765. if (new_brk < old_brk) {
  766. target_munmap(new_brk, old_brk - new_brk);
  767. target_brk = brk_val;
  768. return target_brk;
  769. }
  770. mapped_addr = target_mmap(old_brk, new_brk - old_brk,
  771. PROT_READ | PROT_WRITE,
  772. MAP_FIXED_NOREPLACE | MAP_ANON | MAP_PRIVATE,
  773. -1, 0);
  774. if (mapped_addr == old_brk) {
  775. target_brk = brk_val;
  776. return target_brk;
  777. }
  778. #if defined(TARGET_ALPHA)
  779. /* We (partially) emulate OSF/1 on Alpha, which requires we
  780. return a proper errno, not an unchanged brk value. */
  781. return -TARGET_ENOMEM;
  782. #endif
  783. /* For everything else, return the previous break. */
  784. return target_brk;
  785. }
  786. #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
  787. defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
  788. static inline abi_long copy_from_user_fdset(fd_set *fds,
  789. abi_ulong target_fds_addr,
  790. int n)
  791. {
  792. int i, nw, j, k;
  793. abi_ulong b, *target_fds;
  794. nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
  795. if (!(target_fds = lock_user(VERIFY_READ,
  796. target_fds_addr,
  797. sizeof(abi_ulong) * nw,
  798. 1)))
  799. return -TARGET_EFAULT;
  800. FD_ZERO(fds);
  801. k = 0;
  802. for (i = 0; i < nw; i++) {
  803. /* grab the abi_ulong */
  804. __get_user(b, &target_fds[i]);
  805. for (j = 0; j < TARGET_ABI_BITS; j++) {
  806. /* check the bit inside the abi_ulong */
  807. if ((b >> j) & 1)
  808. FD_SET(k, fds);
  809. k++;
  810. }
  811. }
  812. unlock_user(target_fds, target_fds_addr, 0);
  813. return 0;
  814. }
  815. static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
  816. abi_ulong target_fds_addr,
  817. int n)
  818. {
  819. if (target_fds_addr) {
  820. if (copy_from_user_fdset(fds, target_fds_addr, n))
  821. return -TARGET_EFAULT;
  822. *fds_ptr = fds;
  823. } else {
  824. *fds_ptr = NULL;
  825. }
  826. return 0;
  827. }
  828. static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
  829. const fd_set *fds,
  830. int n)
  831. {
  832. int i, nw, j, k;
  833. abi_long v;
  834. abi_ulong *target_fds;
  835. nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
  836. if (!(target_fds = lock_user(VERIFY_WRITE,
  837. target_fds_addr,
  838. sizeof(abi_ulong) * nw,
  839. 0)))
  840. return -TARGET_EFAULT;
  841. k = 0;
  842. for (i = 0; i < nw; i++) {
  843. v = 0;
  844. for (j = 0; j < TARGET_ABI_BITS; j++) {
  845. v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
  846. k++;
  847. }
  848. __put_user(v, &target_fds[i]);
  849. }
  850. unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
  851. return 0;
  852. }
  853. #endif
  854. #if defined(__alpha__)
  855. #define HOST_HZ 1024
  856. #else
  857. #define HOST_HZ 100
  858. #endif
  859. static inline abi_long host_to_target_clock_t(long ticks)
  860. {
  861. #if HOST_HZ == TARGET_HZ
  862. return ticks;
  863. #else
  864. return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
  865. #endif
  866. }
  867. static inline abi_long host_to_target_rusage(abi_ulong target_addr,
  868. const struct rusage *rusage)
  869. {
  870. struct target_rusage *target_rusage;
  871. if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
  872. return -TARGET_EFAULT;
  873. target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
  874. target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
  875. target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
  876. target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
  877. target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
  878. target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
  879. target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
  880. target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
  881. target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
  882. target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
  883. target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
  884. target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
  885. target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
  886. target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
  887. target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
  888. target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
  889. target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
  890. target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
  891. unlock_user_struct(target_rusage, target_addr, 1);
  892. return 0;
  893. }
  894. #ifdef TARGET_NR_setrlimit
  895. static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
  896. {
  897. abi_ulong target_rlim_swap;
  898. rlim_t result;
  899. target_rlim_swap = tswapal(target_rlim);
  900. if (target_rlim_swap == TARGET_RLIM_INFINITY)
  901. return RLIM_INFINITY;
  902. result = target_rlim_swap;
  903. if (target_rlim_swap != (rlim_t)result)
  904. return RLIM_INFINITY;
  905. return result;
  906. }
  907. #endif
  908. #if defined(TARGET_NR_getrlimit) || defined(TARGET_NR_ugetrlimit)
  909. static inline abi_ulong host_to_target_rlim(rlim_t rlim)
  910. {
  911. abi_ulong target_rlim_swap;
  912. abi_ulong result;
  913. if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
  914. target_rlim_swap = TARGET_RLIM_INFINITY;
  915. else
  916. target_rlim_swap = rlim;
  917. result = tswapal(target_rlim_swap);
  918. return result;
  919. }
  920. #endif
  921. static inline int target_to_host_resource(int code)
  922. {
  923. switch (code) {
  924. case TARGET_RLIMIT_AS:
  925. return RLIMIT_AS;
  926. case TARGET_RLIMIT_CORE:
  927. return RLIMIT_CORE;
  928. case TARGET_RLIMIT_CPU:
  929. return RLIMIT_CPU;
  930. case TARGET_RLIMIT_DATA:
  931. return RLIMIT_DATA;
  932. case TARGET_RLIMIT_FSIZE:
  933. return RLIMIT_FSIZE;
  934. case TARGET_RLIMIT_LOCKS:
  935. return RLIMIT_LOCKS;
  936. case TARGET_RLIMIT_MEMLOCK:
  937. return RLIMIT_MEMLOCK;
  938. case TARGET_RLIMIT_MSGQUEUE:
  939. return RLIMIT_MSGQUEUE;
  940. case TARGET_RLIMIT_NICE:
  941. return RLIMIT_NICE;
  942. case TARGET_RLIMIT_NOFILE:
  943. return RLIMIT_NOFILE;
  944. case TARGET_RLIMIT_NPROC:
  945. return RLIMIT_NPROC;
  946. case TARGET_RLIMIT_RSS:
  947. return RLIMIT_RSS;
  948. case TARGET_RLIMIT_RTPRIO:
  949. return RLIMIT_RTPRIO;
  950. #ifdef RLIMIT_RTTIME
  951. case TARGET_RLIMIT_RTTIME:
  952. return RLIMIT_RTTIME;
  953. #endif
  954. case TARGET_RLIMIT_SIGPENDING:
  955. return RLIMIT_SIGPENDING;
  956. case TARGET_RLIMIT_STACK:
  957. return RLIMIT_STACK;
  958. default:
  959. return code;
  960. }
  961. }
  962. static inline abi_long copy_from_user_timeval(struct timeval *tv,
  963. abi_ulong target_tv_addr)
  964. {
  965. struct target_timeval *target_tv;
  966. if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) {
  967. return -TARGET_EFAULT;
  968. }
  969. __get_user(tv->tv_sec, &target_tv->tv_sec);
  970. __get_user(tv->tv_usec, &target_tv->tv_usec);
  971. unlock_user_struct(target_tv, target_tv_addr, 0);
  972. return 0;
  973. }
  974. static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
  975. const struct timeval *tv)
  976. {
  977. struct target_timeval *target_tv;
  978. if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
  979. return -TARGET_EFAULT;
  980. }
  981. __put_user(tv->tv_sec, &target_tv->tv_sec);
  982. __put_user(tv->tv_usec, &target_tv->tv_usec);
  983. unlock_user_struct(target_tv, target_tv_addr, 1);
  984. return 0;
  985. }
  986. #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
  987. static inline abi_long copy_from_user_timeval64(struct timeval *tv,
  988. abi_ulong target_tv_addr)
  989. {
  990. struct target__kernel_sock_timeval *target_tv;
  991. if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) {
  992. return -TARGET_EFAULT;
  993. }
  994. __get_user(tv->tv_sec, &target_tv->tv_sec);
  995. __get_user(tv->tv_usec, &target_tv->tv_usec);
  996. unlock_user_struct(target_tv, target_tv_addr, 0);
  997. return 0;
  998. }
  999. #endif
  1000. static inline abi_long copy_to_user_timeval64(abi_ulong target_tv_addr,
  1001. const struct timeval *tv)
  1002. {
  1003. struct target__kernel_sock_timeval *target_tv;
  1004. if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
  1005. return -TARGET_EFAULT;
  1006. }
  1007. __put_user(tv->tv_sec, &target_tv->tv_sec);
  1008. __put_user(tv->tv_usec, &target_tv->tv_usec);
  1009. unlock_user_struct(target_tv, target_tv_addr, 1);
  1010. return 0;
  1011. }
  1012. #if defined(TARGET_NR_futex) || \
  1013. defined(TARGET_NR_rt_sigtimedwait) || \
  1014. defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6) || \
  1015. defined(TARGET_NR_nanosleep) || defined(TARGET_NR_clock_settime) || \
  1016. defined(TARGET_NR_utimensat) || defined(TARGET_NR_mq_timedsend) || \
  1017. defined(TARGET_NR_mq_timedreceive) || defined(TARGET_NR_ipc) || \
  1018. defined(TARGET_NR_semop) || defined(TARGET_NR_semtimedop) || \
  1019. defined(TARGET_NR_timer_settime) || \
  1020. (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
  1021. static inline abi_long target_to_host_timespec(struct timespec *host_ts,
  1022. abi_ulong target_addr)
  1023. {
  1024. struct target_timespec *target_ts;
  1025. if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
  1026. return -TARGET_EFAULT;
  1027. }
  1028. __get_user(host_ts->tv_sec, &target_ts->tv_sec);
  1029. __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1030. unlock_user_struct(target_ts, target_addr, 0);
  1031. return 0;
  1032. }
  1033. #endif
  1034. #if defined(TARGET_NR_clock_settime64) || defined(TARGET_NR_futex_time64) || \
  1035. defined(TARGET_NR_timer_settime64) || \
  1036. defined(TARGET_NR_mq_timedsend_time64) || \
  1037. defined(TARGET_NR_mq_timedreceive_time64) || \
  1038. (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)) || \
  1039. defined(TARGET_NR_clock_nanosleep_time64) || \
  1040. defined(TARGET_NR_rt_sigtimedwait_time64) || \
  1041. defined(TARGET_NR_utimensat) || \
  1042. defined(TARGET_NR_utimensat_time64) || \
  1043. defined(TARGET_NR_semtimedop_time64) || \
  1044. defined(TARGET_NR_pselect6_time64) || defined(TARGET_NR_ppoll_time64)
  1045. static inline abi_long target_to_host_timespec64(struct timespec *host_ts,
  1046. abi_ulong target_addr)
  1047. {
  1048. struct target__kernel_timespec *target_ts;
  1049. if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
  1050. return -TARGET_EFAULT;
  1051. }
  1052. __get_user(host_ts->tv_sec, &target_ts->tv_sec);
  1053. __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1054. /* in 32bit mode, this drops the padding */
  1055. host_ts->tv_nsec = (long)(abi_long)host_ts->tv_nsec;
  1056. unlock_user_struct(target_ts, target_addr, 0);
  1057. return 0;
  1058. }
  1059. #endif
  1060. static inline abi_long host_to_target_timespec(abi_ulong target_addr,
  1061. struct timespec *host_ts)
  1062. {
  1063. struct target_timespec *target_ts;
  1064. if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
  1065. return -TARGET_EFAULT;
  1066. }
  1067. __put_user(host_ts->tv_sec, &target_ts->tv_sec);
  1068. __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1069. unlock_user_struct(target_ts, target_addr, 1);
  1070. return 0;
  1071. }
  1072. static inline abi_long host_to_target_timespec64(abi_ulong target_addr,
  1073. struct timespec *host_ts)
  1074. {
  1075. struct target__kernel_timespec *target_ts;
  1076. if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
  1077. return -TARGET_EFAULT;
  1078. }
  1079. __put_user(host_ts->tv_sec, &target_ts->tv_sec);
  1080. __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1081. unlock_user_struct(target_ts, target_addr, 1);
  1082. return 0;
  1083. }
  1084. #if defined(TARGET_NR_gettimeofday)
  1085. static inline abi_long copy_to_user_timezone(abi_ulong target_tz_addr,
  1086. struct timezone *tz)
  1087. {
  1088. struct target_timezone *target_tz;
  1089. if (!lock_user_struct(VERIFY_WRITE, target_tz, target_tz_addr, 1)) {
  1090. return -TARGET_EFAULT;
  1091. }
  1092. __put_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
  1093. __put_user(tz->tz_dsttime, &target_tz->tz_dsttime);
  1094. unlock_user_struct(target_tz, target_tz_addr, 1);
  1095. return 0;
  1096. }
  1097. #endif
  1098. #if defined(TARGET_NR_settimeofday)
  1099. static inline abi_long copy_from_user_timezone(struct timezone *tz,
  1100. abi_ulong target_tz_addr)
  1101. {
  1102. struct target_timezone *target_tz;
  1103. if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) {
  1104. return -TARGET_EFAULT;
  1105. }
  1106. __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
  1107. __get_user(tz->tz_dsttime, &target_tz->tz_dsttime);
  1108. unlock_user_struct(target_tz, target_tz_addr, 0);
  1109. return 0;
  1110. }
  1111. #endif
  1112. #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
  1113. #include <mqueue.h>
  1114. static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
  1115. abi_ulong target_mq_attr_addr)
  1116. {
  1117. struct target_mq_attr *target_mq_attr;
  1118. if (!lock_user_struct(VERIFY_READ, target_mq_attr,
  1119. target_mq_attr_addr, 1))
  1120. return -TARGET_EFAULT;
  1121. __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
  1122. __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
  1123. __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
  1124. __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
  1125. unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
  1126. return 0;
  1127. }
  1128. static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
  1129. const struct mq_attr *attr)
  1130. {
  1131. struct target_mq_attr *target_mq_attr;
  1132. if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
  1133. target_mq_attr_addr, 0))
  1134. return -TARGET_EFAULT;
  1135. __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
  1136. __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
  1137. __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
  1138. __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
  1139. unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
  1140. return 0;
  1141. }
  1142. #endif
  1143. #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
  1144. /* do_select() must return target values and target errnos. */
  1145. static abi_long do_select(int n,
  1146. abi_ulong rfd_addr, abi_ulong wfd_addr,
  1147. abi_ulong efd_addr, abi_ulong target_tv_addr)
  1148. {
  1149. fd_set rfds, wfds, efds;
  1150. fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
  1151. struct timeval tv;
  1152. struct timespec ts, *ts_ptr;
  1153. abi_long ret;
  1154. ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
  1155. if (ret) {
  1156. return ret;
  1157. }
  1158. ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
  1159. if (ret) {
  1160. return ret;
  1161. }
  1162. ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
  1163. if (ret) {
  1164. return ret;
  1165. }
  1166. if (target_tv_addr) {
  1167. if (copy_from_user_timeval(&tv, target_tv_addr))
  1168. return -TARGET_EFAULT;
  1169. ts.tv_sec = tv.tv_sec;
  1170. ts.tv_nsec = tv.tv_usec * 1000;
  1171. ts_ptr = &ts;
  1172. } else {
  1173. ts_ptr = NULL;
  1174. }
  1175. ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
  1176. ts_ptr, NULL));
  1177. if (!is_error(ret)) {
  1178. if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
  1179. return -TARGET_EFAULT;
  1180. if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
  1181. return -TARGET_EFAULT;
  1182. if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
  1183. return -TARGET_EFAULT;
  1184. if (target_tv_addr) {
  1185. tv.tv_sec = ts.tv_sec;
  1186. tv.tv_usec = ts.tv_nsec / 1000;
  1187. if (copy_to_user_timeval(target_tv_addr, &tv)) {
  1188. return -TARGET_EFAULT;
  1189. }
  1190. }
  1191. }
  1192. return ret;
  1193. }
  1194. #if defined(TARGET_WANT_OLD_SYS_SELECT)
  1195. static abi_long do_old_select(abi_ulong arg1)
  1196. {
  1197. struct target_sel_arg_struct *sel;
  1198. abi_ulong inp, outp, exp, tvp;
  1199. long nsel;
  1200. if (!lock_user_struct(VERIFY_READ, sel, arg1, 1)) {
  1201. return -TARGET_EFAULT;
  1202. }
  1203. nsel = tswapal(sel->n);
  1204. inp = tswapal(sel->inp);
  1205. outp = tswapal(sel->outp);
  1206. exp = tswapal(sel->exp);
  1207. tvp = tswapal(sel->tvp);
  1208. unlock_user_struct(sel, arg1, 0);
  1209. return do_select(nsel, inp, outp, exp, tvp);
  1210. }
  1211. #endif
  1212. #endif
  1213. #if defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
  1214. static abi_long do_pselect6(abi_long arg1, abi_long arg2, abi_long arg3,
  1215. abi_long arg4, abi_long arg5, abi_long arg6,
  1216. bool time64)
  1217. {
  1218. abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
  1219. fd_set rfds, wfds, efds;
  1220. fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
  1221. struct timespec ts, *ts_ptr;
  1222. abi_long ret;
  1223. /*
  1224. * The 6th arg is actually two args smashed together,
  1225. * so we cannot use the C library.
  1226. */
  1227. struct {
  1228. sigset_t *set;
  1229. size_t size;
  1230. } sig, *sig_ptr;
  1231. abi_ulong arg_sigset, arg_sigsize, *arg7;
  1232. n = arg1;
  1233. rfd_addr = arg2;
  1234. wfd_addr = arg3;
  1235. efd_addr = arg4;
  1236. ts_addr = arg5;
  1237. ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
  1238. if (ret) {
  1239. return ret;
  1240. }
  1241. ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
  1242. if (ret) {
  1243. return ret;
  1244. }
  1245. ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
  1246. if (ret) {
  1247. return ret;
  1248. }
  1249. /*
  1250. * This takes a timespec, and not a timeval, so we cannot
  1251. * use the do_select() helper ...
  1252. */
  1253. if (ts_addr) {
  1254. if (time64) {
  1255. if (target_to_host_timespec64(&ts, ts_addr)) {
  1256. return -TARGET_EFAULT;
  1257. }
  1258. } else {
  1259. if (target_to_host_timespec(&ts, ts_addr)) {
  1260. return -TARGET_EFAULT;
  1261. }
  1262. }
  1263. ts_ptr = &ts;
  1264. } else {
  1265. ts_ptr = NULL;
  1266. }
  1267. /* Extract the two packed args for the sigset */
  1268. sig_ptr = NULL;
  1269. if (arg6) {
  1270. arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
  1271. if (!arg7) {
  1272. return -TARGET_EFAULT;
  1273. }
  1274. arg_sigset = tswapal(arg7[0]);
  1275. arg_sigsize = tswapal(arg7[1]);
  1276. unlock_user(arg7, arg6, 0);
  1277. if (arg_sigset) {
  1278. ret = process_sigsuspend_mask(&sig.set, arg_sigset, arg_sigsize);
  1279. if (ret != 0) {
  1280. return ret;
  1281. }
  1282. sig_ptr = &sig;
  1283. sig.size = SIGSET_T_SIZE;
  1284. }
  1285. }
  1286. ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
  1287. ts_ptr, sig_ptr));
  1288. if (sig_ptr) {
  1289. finish_sigsuspend_mask(ret);
  1290. }
  1291. if (!is_error(ret)) {
  1292. if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) {
  1293. return -TARGET_EFAULT;
  1294. }
  1295. if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) {
  1296. return -TARGET_EFAULT;
  1297. }
  1298. if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) {
  1299. return -TARGET_EFAULT;
  1300. }
  1301. if (time64) {
  1302. if (ts_addr && host_to_target_timespec64(ts_addr, &ts)) {
  1303. return -TARGET_EFAULT;
  1304. }
  1305. } else {
  1306. if (ts_addr && host_to_target_timespec(ts_addr, &ts)) {
  1307. return -TARGET_EFAULT;
  1308. }
  1309. }
  1310. }
  1311. return ret;
  1312. }
  1313. #endif
  1314. #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) || \
  1315. defined(TARGET_NR_ppoll_time64)
  1316. static abi_long do_ppoll(abi_long arg1, abi_long arg2, abi_long arg3,
  1317. abi_long arg4, abi_long arg5, bool ppoll, bool time64)
  1318. {
  1319. struct target_pollfd *target_pfd;
  1320. unsigned int nfds = arg2;
  1321. struct pollfd *pfd;
  1322. unsigned int i;
  1323. abi_long ret;
  1324. pfd = NULL;
  1325. target_pfd = NULL;
  1326. if (nfds) {
  1327. if (nfds > (INT_MAX / sizeof(struct target_pollfd))) {
  1328. return -TARGET_EINVAL;
  1329. }
  1330. target_pfd = lock_user(VERIFY_WRITE, arg1,
  1331. sizeof(struct target_pollfd) * nfds, 1);
  1332. if (!target_pfd) {
  1333. return -TARGET_EFAULT;
  1334. }
  1335. pfd = alloca(sizeof(struct pollfd) * nfds);
  1336. for (i = 0; i < nfds; i++) {
  1337. pfd[i].fd = tswap32(target_pfd[i].fd);
  1338. pfd[i].events = tswap16(target_pfd[i].events);
  1339. }
  1340. }
  1341. if (ppoll) {
  1342. struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
  1343. sigset_t *set = NULL;
  1344. if (arg3) {
  1345. if (time64) {
  1346. if (target_to_host_timespec64(timeout_ts, arg3)) {
  1347. unlock_user(target_pfd, arg1, 0);
  1348. return -TARGET_EFAULT;
  1349. }
  1350. } else {
  1351. if (target_to_host_timespec(timeout_ts, arg3)) {
  1352. unlock_user(target_pfd, arg1, 0);
  1353. return -TARGET_EFAULT;
  1354. }
  1355. }
  1356. } else {
  1357. timeout_ts = NULL;
  1358. }
  1359. if (arg4) {
  1360. ret = process_sigsuspend_mask(&set, arg4, arg5);
  1361. if (ret != 0) {
  1362. unlock_user(target_pfd, arg1, 0);
  1363. return ret;
  1364. }
  1365. }
  1366. ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts,
  1367. set, SIGSET_T_SIZE));
  1368. if (set) {
  1369. finish_sigsuspend_mask(ret);
  1370. }
  1371. if (!is_error(ret) && arg3) {
  1372. if (time64) {
  1373. if (host_to_target_timespec64(arg3, timeout_ts)) {
  1374. return -TARGET_EFAULT;
  1375. }
  1376. } else {
  1377. if (host_to_target_timespec(arg3, timeout_ts)) {
  1378. return -TARGET_EFAULT;
  1379. }
  1380. }
  1381. }
  1382. } else {
  1383. struct timespec ts, *pts;
  1384. if (arg3 >= 0) {
  1385. /* Convert ms to secs, ns */
  1386. ts.tv_sec = arg3 / 1000;
  1387. ts.tv_nsec = (arg3 % 1000) * 1000000LL;
  1388. pts = &ts;
  1389. } else {
  1390. /* -ve poll() timeout means "infinite" */
  1391. pts = NULL;
  1392. }
  1393. ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0));
  1394. }
  1395. if (!is_error(ret)) {
  1396. for (i = 0; i < nfds; i++) {
  1397. target_pfd[i].revents = tswap16(pfd[i].revents);
  1398. }
  1399. }
  1400. unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
  1401. return ret;
  1402. }
  1403. #endif
  1404. static abi_long do_pipe(CPUArchState *cpu_env, abi_ulong pipedes,
  1405. int flags, int is_pipe2)
  1406. {
  1407. int host_pipe[2];
  1408. abi_long ret;
  1409. ret = pipe2(host_pipe, flags);
  1410. if (is_error(ret))
  1411. return get_errno(ret);
  1412. /* Several targets have special calling conventions for the original
  1413. pipe syscall, but didn't replicate this into the pipe2 syscall. */
  1414. if (!is_pipe2) {
  1415. #if defined(TARGET_ALPHA)
  1416. cpu_env->ir[IR_A4] = host_pipe[1];
  1417. return host_pipe[0];
  1418. #elif defined(TARGET_MIPS)
  1419. cpu_env->active_tc.gpr[3] = host_pipe[1];
  1420. return host_pipe[0];
  1421. #elif defined(TARGET_SH4)
  1422. cpu_env->gregs[1] = host_pipe[1];
  1423. return host_pipe[0];
  1424. #elif defined(TARGET_SPARC)
  1425. cpu_env->regwptr[1] = host_pipe[1];
  1426. return host_pipe[0];
  1427. #endif
  1428. }
  1429. if (put_user_s32(host_pipe[0], pipedes)
  1430. || put_user_s32(host_pipe[1], pipedes + sizeof(abi_int)))
  1431. return -TARGET_EFAULT;
  1432. return get_errno(ret);
  1433. }
  1434. static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr,
  1435. abi_ulong target_addr,
  1436. socklen_t len)
  1437. {
  1438. const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
  1439. sa_family_t sa_family;
  1440. struct target_sockaddr *target_saddr;
  1441. if (fd_trans_target_to_host_addr(fd)) {
  1442. return fd_trans_target_to_host_addr(fd)(addr, target_addr, len);
  1443. }
  1444. target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
  1445. if (!target_saddr)
  1446. return -TARGET_EFAULT;
  1447. sa_family = tswap16(target_saddr->sa_family);
  1448. /* Oops. The caller might send a incomplete sun_path; sun_path
  1449. * must be terminated by \0 (see the manual page), but
  1450. * unfortunately it is quite common to specify sockaddr_un
  1451. * length as "strlen(x->sun_path)" while it should be
  1452. * "strlen(...) + 1". We'll fix that here if needed.
  1453. * Linux kernel has a similar feature.
  1454. */
  1455. if (sa_family == AF_UNIX) {
  1456. if (len < unix_maxlen && len > 0) {
  1457. char *cp = (char*)target_saddr;
  1458. if ( cp[len-1] && !cp[len] )
  1459. len++;
  1460. }
  1461. if (len > unix_maxlen)
  1462. len = unix_maxlen;
  1463. }
  1464. memcpy(addr, target_saddr, len);
  1465. addr->sa_family = sa_family;
  1466. if (sa_family == AF_NETLINK) {
  1467. struct sockaddr_nl *nladdr;
  1468. nladdr = (struct sockaddr_nl *)addr;
  1469. nladdr->nl_pid = tswap32(nladdr->nl_pid);
  1470. nladdr->nl_groups = tswap32(nladdr->nl_groups);
  1471. } else if (sa_family == AF_PACKET) {
  1472. struct target_sockaddr_ll *lladdr;
  1473. lladdr = (struct target_sockaddr_ll *)addr;
  1474. lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex);
  1475. lladdr->sll_hatype = tswap16(lladdr->sll_hatype);
  1476. } else if (sa_family == AF_INET6) {
  1477. struct sockaddr_in6 *in6addr;
  1478. in6addr = (struct sockaddr_in6 *)addr;
  1479. in6addr->sin6_scope_id = tswap32(in6addr->sin6_scope_id);
  1480. }
  1481. unlock_user(target_saddr, target_addr, 0);
  1482. return 0;
  1483. }
  1484. static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
  1485. struct sockaddr *addr,
  1486. socklen_t len)
  1487. {
  1488. struct target_sockaddr *target_saddr;
  1489. if (len == 0) {
  1490. return 0;
  1491. }
  1492. assert(addr);
  1493. target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
  1494. if (!target_saddr)
  1495. return -TARGET_EFAULT;
  1496. memcpy(target_saddr, addr, len);
  1497. if (len >= offsetof(struct target_sockaddr, sa_family) +
  1498. sizeof(target_saddr->sa_family)) {
  1499. target_saddr->sa_family = tswap16(addr->sa_family);
  1500. }
  1501. if (addr->sa_family == AF_NETLINK &&
  1502. len >= sizeof(struct target_sockaddr_nl)) {
  1503. struct target_sockaddr_nl *target_nl =
  1504. (struct target_sockaddr_nl *)target_saddr;
  1505. target_nl->nl_pid = tswap32(target_nl->nl_pid);
  1506. target_nl->nl_groups = tswap32(target_nl->nl_groups);
  1507. } else if (addr->sa_family == AF_PACKET) {
  1508. struct sockaddr_ll *target_ll = (struct sockaddr_ll *)target_saddr;
  1509. target_ll->sll_ifindex = tswap32(target_ll->sll_ifindex);
  1510. target_ll->sll_hatype = tswap16(target_ll->sll_hatype);
  1511. } else if (addr->sa_family == AF_INET6 &&
  1512. len >= sizeof(struct target_sockaddr_in6)) {
  1513. struct target_sockaddr_in6 *target_in6 =
  1514. (struct target_sockaddr_in6 *)target_saddr;
  1515. target_in6->sin6_scope_id = tswap16(target_in6->sin6_scope_id);
  1516. }
  1517. unlock_user(target_saddr, target_addr, len);
  1518. return 0;
  1519. }
  1520. static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
  1521. struct target_msghdr *target_msgh)
  1522. {
  1523. struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
  1524. abi_long msg_controllen;
  1525. abi_ulong target_cmsg_addr;
  1526. struct target_cmsghdr *target_cmsg, *target_cmsg_start;
  1527. socklen_t space = 0;
  1528. msg_controllen = tswapal(target_msgh->msg_controllen);
  1529. if (msg_controllen < sizeof (struct target_cmsghdr))
  1530. goto the_end;
  1531. target_cmsg_addr = tswapal(target_msgh->msg_control);
  1532. target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
  1533. target_cmsg_start = target_cmsg;
  1534. if (!target_cmsg)
  1535. return -TARGET_EFAULT;
  1536. while (cmsg && target_cmsg) {
  1537. void *data = CMSG_DATA(cmsg);
  1538. void *target_data = TARGET_CMSG_DATA(target_cmsg);
  1539. int len = tswapal(target_cmsg->cmsg_len)
  1540. - sizeof(struct target_cmsghdr);
  1541. space += CMSG_SPACE(len);
  1542. if (space > msgh->msg_controllen) {
  1543. space -= CMSG_SPACE(len);
  1544. /* This is a QEMU bug, since we allocated the payload
  1545. * area ourselves (unlike overflow in host-to-target
  1546. * conversion, which is just the guest giving us a buffer
  1547. * that's too small). It can't happen for the payload types
  1548. * we currently support; if it becomes an issue in future
  1549. * we would need to improve our allocation strategy to
  1550. * something more intelligent than "twice the size of the
  1551. * target buffer we're reading from".
  1552. */
  1553. qemu_log_mask(LOG_UNIMP,
  1554. ("Unsupported ancillary data %d/%d: "
  1555. "unhandled msg size\n"),
  1556. tswap32(target_cmsg->cmsg_level),
  1557. tswap32(target_cmsg->cmsg_type));
  1558. break;
  1559. }
  1560. if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
  1561. cmsg->cmsg_level = SOL_SOCKET;
  1562. } else {
  1563. cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
  1564. }
  1565. cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
  1566. cmsg->cmsg_len = CMSG_LEN(len);
  1567. if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
  1568. int *fd = (int *)data;
  1569. int *target_fd = (int *)target_data;
  1570. int i, numfds = len / sizeof(int);
  1571. for (i = 0; i < numfds; i++) {
  1572. __get_user(fd[i], target_fd + i);
  1573. }
  1574. } else if (cmsg->cmsg_level == SOL_SOCKET
  1575. && cmsg->cmsg_type == SCM_CREDENTIALS) {
  1576. struct ucred *cred = (struct ucred *)data;
  1577. struct target_ucred *target_cred =
  1578. (struct target_ucred *)target_data;
  1579. __get_user(cred->pid, &target_cred->pid);
  1580. __get_user(cred->uid, &target_cred->uid);
  1581. __get_user(cred->gid, &target_cred->gid);
  1582. } else if (cmsg->cmsg_level == SOL_ALG) {
  1583. uint32_t *dst = (uint32_t *)data;
  1584. memcpy(dst, target_data, len);
  1585. /* fix endianness of first 32-bit word */
  1586. if (len >= sizeof(uint32_t)) {
  1587. *dst = tswap32(*dst);
  1588. }
  1589. } else {
  1590. qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n",
  1591. cmsg->cmsg_level, cmsg->cmsg_type);
  1592. memcpy(data, target_data, len);
  1593. }
  1594. cmsg = CMSG_NXTHDR(msgh, cmsg);
  1595. target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
  1596. target_cmsg_start);
  1597. }
  1598. unlock_user(target_cmsg, target_cmsg_addr, 0);
  1599. the_end:
  1600. msgh->msg_controllen = space;
  1601. return 0;
  1602. }
  1603. static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
  1604. struct msghdr *msgh)
  1605. {
  1606. struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
  1607. abi_long msg_controllen;
  1608. abi_ulong target_cmsg_addr;
  1609. struct target_cmsghdr *target_cmsg, *target_cmsg_start;
  1610. socklen_t space = 0;
  1611. msg_controllen = tswapal(target_msgh->msg_controllen);
  1612. if (msg_controllen < sizeof (struct target_cmsghdr))
  1613. goto the_end;
  1614. target_cmsg_addr = tswapal(target_msgh->msg_control);
  1615. target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
  1616. target_cmsg_start = target_cmsg;
  1617. if (!target_cmsg)
  1618. return -TARGET_EFAULT;
  1619. while (cmsg && target_cmsg) {
  1620. void *data = CMSG_DATA(cmsg);
  1621. void *target_data = TARGET_CMSG_DATA(target_cmsg);
  1622. int len = cmsg->cmsg_len - sizeof(struct cmsghdr);
  1623. int tgt_len, tgt_space;
  1624. /* We never copy a half-header but may copy half-data;
  1625. * this is Linux's behaviour in put_cmsg(). Note that
  1626. * truncation here is a guest problem (which we report
  1627. * to the guest via the CTRUNC bit), unlike truncation
  1628. * in target_to_host_cmsg, which is a QEMU bug.
  1629. */
  1630. if (msg_controllen < sizeof(struct target_cmsghdr)) {
  1631. target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
  1632. break;
  1633. }
  1634. if (cmsg->cmsg_level == SOL_SOCKET) {
  1635. target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
  1636. } else {
  1637. target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
  1638. }
  1639. target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
  1640. /* Payload types which need a different size of payload on
  1641. * the target must adjust tgt_len here.
  1642. */
  1643. tgt_len = len;
  1644. switch (cmsg->cmsg_level) {
  1645. case SOL_SOCKET:
  1646. switch (cmsg->cmsg_type) {
  1647. case SO_TIMESTAMP:
  1648. tgt_len = sizeof(struct target_timeval);
  1649. break;
  1650. default:
  1651. break;
  1652. }
  1653. break;
  1654. default:
  1655. break;
  1656. }
  1657. if (msg_controllen < TARGET_CMSG_LEN(tgt_len)) {
  1658. target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
  1659. tgt_len = msg_controllen - sizeof(struct target_cmsghdr);
  1660. }
  1661. /* We must now copy-and-convert len bytes of payload
  1662. * into tgt_len bytes of destination space. Bear in mind
  1663. * that in both source and destination we may be dealing
  1664. * with a truncated value!
  1665. */
  1666. switch (cmsg->cmsg_level) {
  1667. case SOL_SOCKET:
  1668. switch (cmsg->cmsg_type) {
  1669. case SCM_RIGHTS:
  1670. {
  1671. int *fd = (int *)data;
  1672. int *target_fd = (int *)target_data;
  1673. int i, numfds = tgt_len / sizeof(int);
  1674. for (i = 0; i < numfds; i++) {
  1675. __put_user(fd[i], target_fd + i);
  1676. }
  1677. break;
  1678. }
  1679. case SO_TIMESTAMP:
  1680. {
  1681. struct timeval *tv = (struct timeval *)data;
  1682. struct target_timeval *target_tv =
  1683. (struct target_timeval *)target_data;
  1684. if (len != sizeof(struct timeval) ||
  1685. tgt_len != sizeof(struct target_timeval)) {
  1686. goto unimplemented;
  1687. }
  1688. /* copy struct timeval to target */
  1689. __put_user(tv->tv_sec, &target_tv->tv_sec);
  1690. __put_user(tv->tv_usec, &target_tv->tv_usec);
  1691. break;
  1692. }
  1693. case SCM_CREDENTIALS:
  1694. {
  1695. struct ucred *cred = (struct ucred *)data;
  1696. struct target_ucred *target_cred =
  1697. (struct target_ucred *)target_data;
  1698. __put_user(cred->pid, &target_cred->pid);
  1699. __put_user(cred->uid, &target_cred->uid);
  1700. __put_user(cred->gid, &target_cred->gid);
  1701. break;
  1702. }
  1703. default:
  1704. goto unimplemented;
  1705. }
  1706. break;
  1707. case SOL_IP:
  1708. switch (cmsg->cmsg_type) {
  1709. case IP_TTL:
  1710. {
  1711. uint32_t *v = (uint32_t *)data;
  1712. uint32_t *t_int = (uint32_t *)target_data;
  1713. if (len != sizeof(uint32_t) ||
  1714. tgt_len != sizeof(uint32_t)) {
  1715. goto unimplemented;
  1716. }
  1717. __put_user(*v, t_int);
  1718. break;
  1719. }
  1720. case IP_RECVERR:
  1721. {
  1722. struct errhdr_t {
  1723. struct sock_extended_err ee;
  1724. struct sockaddr_in offender;
  1725. };
  1726. struct errhdr_t *errh = (struct errhdr_t *)data;
  1727. struct errhdr_t *target_errh =
  1728. (struct errhdr_t *)target_data;
  1729. if (len != sizeof(struct errhdr_t) ||
  1730. tgt_len != sizeof(struct errhdr_t)) {
  1731. goto unimplemented;
  1732. }
  1733. __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
  1734. __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
  1735. __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
  1736. __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
  1737. __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
  1738. __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
  1739. __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
  1740. host_to_target_sockaddr((unsigned long) &target_errh->offender,
  1741. (void *) &errh->offender, sizeof(errh->offender));
  1742. break;
  1743. }
  1744. default:
  1745. goto unimplemented;
  1746. }
  1747. break;
  1748. case SOL_IPV6:
  1749. switch (cmsg->cmsg_type) {
  1750. case IPV6_HOPLIMIT:
  1751. {
  1752. uint32_t *v = (uint32_t *)data;
  1753. uint32_t *t_int = (uint32_t *)target_data;
  1754. if (len != sizeof(uint32_t) ||
  1755. tgt_len != sizeof(uint32_t)) {
  1756. goto unimplemented;
  1757. }
  1758. __put_user(*v, t_int);
  1759. break;
  1760. }
  1761. case IPV6_RECVERR:
  1762. {
  1763. struct errhdr6_t {
  1764. struct sock_extended_err ee;
  1765. struct sockaddr_in6 offender;
  1766. };
  1767. struct errhdr6_t *errh = (struct errhdr6_t *)data;
  1768. struct errhdr6_t *target_errh =
  1769. (struct errhdr6_t *)target_data;
  1770. if (len != sizeof(struct errhdr6_t) ||
  1771. tgt_len != sizeof(struct errhdr6_t)) {
  1772. goto unimplemented;
  1773. }
  1774. __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
  1775. __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
  1776. __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
  1777. __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
  1778. __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
  1779. __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
  1780. __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
  1781. host_to_target_sockaddr((unsigned long) &target_errh->offender,
  1782. (void *) &errh->offender, sizeof(errh->offender));
  1783. break;
  1784. }
  1785. default:
  1786. goto unimplemented;
  1787. }
  1788. break;
  1789. default:
  1790. unimplemented:
  1791. qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n",
  1792. cmsg->cmsg_level, cmsg->cmsg_type);
  1793. memcpy(target_data, data, MIN(len, tgt_len));
  1794. if (tgt_len > len) {
  1795. memset(target_data + len, 0, tgt_len - len);
  1796. }
  1797. }
  1798. target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(tgt_len));
  1799. tgt_space = TARGET_CMSG_SPACE(tgt_len);
  1800. if (msg_controllen < tgt_space) {
  1801. tgt_space = msg_controllen;
  1802. }
  1803. msg_controllen -= tgt_space;
  1804. space += tgt_space;
  1805. cmsg = CMSG_NXTHDR(msgh, cmsg);
  1806. target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
  1807. target_cmsg_start);
  1808. }
  1809. unlock_user(target_cmsg, target_cmsg_addr, space);
  1810. the_end:
  1811. target_msgh->msg_controllen = tswapal(space);
  1812. return 0;
  1813. }
  1814. /* do_setsockopt() Must return target values and target errnos. */
  1815. static abi_long do_setsockopt(int sockfd, int level, int optname,
  1816. abi_ulong optval_addr, socklen_t optlen)
  1817. {
  1818. abi_long ret;
  1819. int val;
  1820. switch(level) {
  1821. case SOL_TCP:
  1822. case SOL_UDP:
  1823. /* TCP and UDP options all take an 'int' value. */
  1824. if (optlen < sizeof(uint32_t))
  1825. return -TARGET_EINVAL;
  1826. if (get_user_u32(val, optval_addr))
  1827. return -TARGET_EFAULT;
  1828. ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
  1829. break;
  1830. case SOL_IP:
  1831. switch(optname) {
  1832. case IP_TOS:
  1833. case IP_TTL:
  1834. case IP_HDRINCL:
  1835. case IP_ROUTER_ALERT:
  1836. case IP_RECVOPTS:
  1837. case IP_RETOPTS:
  1838. case IP_PKTINFO:
  1839. case IP_MTU_DISCOVER:
  1840. case IP_RECVERR:
  1841. case IP_RECVTTL:
  1842. case IP_RECVTOS:
  1843. #ifdef IP_FREEBIND
  1844. case IP_FREEBIND:
  1845. #endif
  1846. case IP_MULTICAST_TTL:
  1847. case IP_MULTICAST_LOOP:
  1848. val = 0;
  1849. if (optlen >= sizeof(uint32_t)) {
  1850. if (get_user_u32(val, optval_addr))
  1851. return -TARGET_EFAULT;
  1852. } else if (optlen >= 1) {
  1853. if (get_user_u8(val, optval_addr))
  1854. return -TARGET_EFAULT;
  1855. }
  1856. ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
  1857. break;
  1858. case IP_ADD_MEMBERSHIP:
  1859. case IP_DROP_MEMBERSHIP:
  1860. {
  1861. struct ip_mreqn ip_mreq;
  1862. struct target_ip_mreqn *target_smreqn;
  1863. QEMU_BUILD_BUG_ON(sizeof(struct ip_mreq) !=
  1864. sizeof(struct target_ip_mreq));
  1865. if (optlen < sizeof (struct target_ip_mreq) ||
  1866. optlen > sizeof (struct target_ip_mreqn)) {
  1867. return -TARGET_EINVAL;
  1868. }
  1869. target_smreqn = lock_user(VERIFY_READ, optval_addr, optlen, 1);
  1870. if (!target_smreqn) {
  1871. return -TARGET_EFAULT;
  1872. }
  1873. ip_mreq.imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
  1874. ip_mreq.imr_address.s_addr = target_smreqn->imr_address.s_addr;
  1875. if (optlen == sizeof(struct target_ip_mreqn)) {
  1876. ip_mreq.imr_ifindex = tswapal(target_smreqn->imr_ifindex);
  1877. optlen = sizeof(struct ip_mreqn);
  1878. }
  1879. unlock_user(target_smreqn, optval_addr, 0);
  1880. ret = get_errno(setsockopt(sockfd, level, optname, &ip_mreq, optlen));
  1881. break;
  1882. }
  1883. case IP_BLOCK_SOURCE:
  1884. case IP_UNBLOCK_SOURCE:
  1885. case IP_ADD_SOURCE_MEMBERSHIP:
  1886. case IP_DROP_SOURCE_MEMBERSHIP:
  1887. {
  1888. struct ip_mreq_source *ip_mreq_source;
  1889. if (optlen != sizeof (struct target_ip_mreq_source))
  1890. return -TARGET_EINVAL;
  1891. ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
  1892. if (!ip_mreq_source) {
  1893. return -TARGET_EFAULT;
  1894. }
  1895. ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
  1896. unlock_user (ip_mreq_source, optval_addr, 0);
  1897. break;
  1898. }
  1899. default:
  1900. goto unimplemented;
  1901. }
  1902. break;
  1903. case SOL_IPV6:
  1904. switch (optname) {
  1905. case IPV6_MTU_DISCOVER:
  1906. case IPV6_MTU:
  1907. case IPV6_V6ONLY:
  1908. case IPV6_RECVPKTINFO:
  1909. case IPV6_UNICAST_HOPS:
  1910. case IPV6_MULTICAST_HOPS:
  1911. case IPV6_MULTICAST_LOOP:
  1912. case IPV6_RECVERR:
  1913. case IPV6_RECVHOPLIMIT:
  1914. case IPV6_2292HOPLIMIT:
  1915. case IPV6_CHECKSUM:
  1916. case IPV6_ADDRFORM:
  1917. case IPV6_2292PKTINFO:
  1918. case IPV6_RECVTCLASS:
  1919. case IPV6_RECVRTHDR:
  1920. case IPV6_2292RTHDR:
  1921. case IPV6_RECVHOPOPTS:
  1922. case IPV6_2292HOPOPTS:
  1923. case IPV6_RECVDSTOPTS:
  1924. case IPV6_2292DSTOPTS:
  1925. case IPV6_TCLASS:
  1926. case IPV6_ADDR_PREFERENCES:
  1927. #ifdef IPV6_RECVPATHMTU
  1928. case IPV6_RECVPATHMTU:
  1929. #endif
  1930. #ifdef IPV6_TRANSPARENT
  1931. case IPV6_TRANSPARENT:
  1932. #endif
  1933. #ifdef IPV6_FREEBIND
  1934. case IPV6_FREEBIND:
  1935. #endif
  1936. #ifdef IPV6_RECVORIGDSTADDR
  1937. case IPV6_RECVORIGDSTADDR:
  1938. #endif
  1939. val = 0;
  1940. if (optlen < sizeof(uint32_t)) {
  1941. return -TARGET_EINVAL;
  1942. }
  1943. if (get_user_u32(val, optval_addr)) {
  1944. return -TARGET_EFAULT;
  1945. }
  1946. ret = get_errno(setsockopt(sockfd, level, optname,
  1947. &val, sizeof(val)));
  1948. break;
  1949. case IPV6_PKTINFO:
  1950. {
  1951. struct in6_pktinfo pki;
  1952. if (optlen < sizeof(pki)) {
  1953. return -TARGET_EINVAL;
  1954. }
  1955. if (copy_from_user(&pki, optval_addr, sizeof(pki))) {
  1956. return -TARGET_EFAULT;
  1957. }
  1958. pki.ipi6_ifindex = tswap32(pki.ipi6_ifindex);
  1959. ret = get_errno(setsockopt(sockfd, level, optname,
  1960. &pki, sizeof(pki)));
  1961. break;
  1962. }
  1963. case IPV6_ADD_MEMBERSHIP:
  1964. case IPV6_DROP_MEMBERSHIP:
  1965. {
  1966. struct ipv6_mreq ipv6mreq;
  1967. if (optlen < sizeof(ipv6mreq)) {
  1968. return -TARGET_EINVAL;
  1969. }
  1970. if (copy_from_user(&ipv6mreq, optval_addr, sizeof(ipv6mreq))) {
  1971. return -TARGET_EFAULT;
  1972. }
  1973. ipv6mreq.ipv6mr_interface = tswap32(ipv6mreq.ipv6mr_interface);
  1974. ret = get_errno(setsockopt(sockfd, level, optname,
  1975. &ipv6mreq, sizeof(ipv6mreq)));
  1976. break;
  1977. }
  1978. default:
  1979. goto unimplemented;
  1980. }
  1981. break;
  1982. case SOL_ICMPV6:
  1983. switch (optname) {
  1984. case ICMPV6_FILTER:
  1985. {
  1986. struct icmp6_filter icmp6f;
  1987. if (optlen > sizeof(icmp6f)) {
  1988. optlen = sizeof(icmp6f);
  1989. }
  1990. if (copy_from_user(&icmp6f, optval_addr, optlen)) {
  1991. return -TARGET_EFAULT;
  1992. }
  1993. for (val = 0; val < 8; val++) {
  1994. icmp6f.data[val] = tswap32(icmp6f.data[val]);
  1995. }
  1996. ret = get_errno(setsockopt(sockfd, level, optname,
  1997. &icmp6f, optlen));
  1998. break;
  1999. }
  2000. default:
  2001. goto unimplemented;
  2002. }
  2003. break;
  2004. case SOL_RAW:
  2005. switch (optname) {
  2006. case ICMP_FILTER:
  2007. case IPV6_CHECKSUM:
  2008. /* those take an u32 value */
  2009. if (optlen < sizeof(uint32_t)) {
  2010. return -TARGET_EINVAL;
  2011. }
  2012. if (get_user_u32(val, optval_addr)) {
  2013. return -TARGET_EFAULT;
  2014. }
  2015. ret = get_errno(setsockopt(sockfd, level, optname,
  2016. &val, sizeof(val)));
  2017. break;
  2018. default:
  2019. goto unimplemented;
  2020. }
  2021. break;
  2022. #if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE)
  2023. case SOL_ALG:
  2024. switch (optname) {
  2025. case ALG_SET_KEY:
  2026. {
  2027. char *alg_key = lock_user(VERIFY_READ, optval_addr, optlen, 1);
  2028. if (!alg_key) {
  2029. return -TARGET_EFAULT;
  2030. }
  2031. ret = get_errno(setsockopt(sockfd, level, optname,
  2032. alg_key, optlen));
  2033. unlock_user(alg_key, optval_addr, optlen);
  2034. break;
  2035. }
  2036. case ALG_SET_AEAD_AUTHSIZE:
  2037. {
  2038. ret = get_errno(setsockopt(sockfd, level, optname,
  2039. NULL, optlen));
  2040. break;
  2041. }
  2042. default:
  2043. goto unimplemented;
  2044. }
  2045. break;
  2046. #endif
  2047. case TARGET_SOL_SOCKET:
  2048. switch (optname) {
  2049. case TARGET_SO_RCVTIMEO:
  2050. case TARGET_SO_SNDTIMEO:
  2051. {
  2052. struct timeval tv;
  2053. if (optlen != sizeof(struct target_timeval)) {
  2054. return -TARGET_EINVAL;
  2055. }
  2056. if (copy_from_user_timeval(&tv, optval_addr)) {
  2057. return -TARGET_EFAULT;
  2058. }
  2059. ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
  2060. optname == TARGET_SO_RCVTIMEO ?
  2061. SO_RCVTIMEO : SO_SNDTIMEO,
  2062. &tv, sizeof(tv)));
  2063. return ret;
  2064. }
  2065. case TARGET_SO_ATTACH_FILTER:
  2066. {
  2067. struct target_sock_fprog *tfprog;
  2068. struct target_sock_filter *tfilter;
  2069. struct sock_fprog fprog;
  2070. struct sock_filter *filter;
  2071. int i;
  2072. if (optlen != sizeof(*tfprog)) {
  2073. return -TARGET_EINVAL;
  2074. }
  2075. if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
  2076. return -TARGET_EFAULT;
  2077. }
  2078. if (!lock_user_struct(VERIFY_READ, tfilter,
  2079. tswapal(tfprog->filter), 0)) {
  2080. unlock_user_struct(tfprog, optval_addr, 1);
  2081. return -TARGET_EFAULT;
  2082. }
  2083. fprog.len = tswap16(tfprog->len);
  2084. filter = g_try_new(struct sock_filter, fprog.len);
  2085. if (filter == NULL) {
  2086. unlock_user_struct(tfilter, tfprog->filter, 1);
  2087. unlock_user_struct(tfprog, optval_addr, 1);
  2088. return -TARGET_ENOMEM;
  2089. }
  2090. for (i = 0; i < fprog.len; i++) {
  2091. filter[i].code = tswap16(tfilter[i].code);
  2092. filter[i].jt = tfilter[i].jt;
  2093. filter[i].jf = tfilter[i].jf;
  2094. filter[i].k = tswap32(tfilter[i].k);
  2095. }
  2096. fprog.filter = filter;
  2097. ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
  2098. SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
  2099. g_free(filter);
  2100. unlock_user_struct(tfilter, tfprog->filter, 1);
  2101. unlock_user_struct(tfprog, optval_addr, 1);
  2102. return ret;
  2103. }
  2104. case TARGET_SO_BINDTODEVICE:
  2105. {
  2106. char *dev_ifname, *addr_ifname;
  2107. if (optlen > IFNAMSIZ - 1) {
  2108. optlen = IFNAMSIZ - 1;
  2109. }
  2110. dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1);
  2111. if (!dev_ifname) {
  2112. return -TARGET_EFAULT;
  2113. }
  2114. optname = SO_BINDTODEVICE;
  2115. addr_ifname = alloca(IFNAMSIZ);
  2116. memcpy(addr_ifname, dev_ifname, optlen);
  2117. addr_ifname[optlen] = 0;
  2118. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
  2119. addr_ifname, optlen));
  2120. unlock_user (dev_ifname, optval_addr, 0);
  2121. return ret;
  2122. }
  2123. case TARGET_SO_LINGER:
  2124. {
  2125. struct linger lg;
  2126. struct target_linger *tlg;
  2127. if (optlen != sizeof(struct target_linger)) {
  2128. return -TARGET_EINVAL;
  2129. }
  2130. if (!lock_user_struct(VERIFY_READ, tlg, optval_addr, 1)) {
  2131. return -TARGET_EFAULT;
  2132. }
  2133. __get_user(lg.l_onoff, &tlg->l_onoff);
  2134. __get_user(lg.l_linger, &tlg->l_linger);
  2135. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, SO_LINGER,
  2136. &lg, sizeof(lg)));
  2137. unlock_user_struct(tlg, optval_addr, 0);
  2138. return ret;
  2139. }
  2140. /* Options with 'int' argument. */
  2141. case TARGET_SO_DEBUG:
  2142. optname = SO_DEBUG;
  2143. break;
  2144. case TARGET_SO_REUSEADDR:
  2145. optname = SO_REUSEADDR;
  2146. break;
  2147. #ifdef SO_REUSEPORT
  2148. case TARGET_SO_REUSEPORT:
  2149. optname = SO_REUSEPORT;
  2150. break;
  2151. #endif
  2152. case TARGET_SO_TYPE:
  2153. optname = SO_TYPE;
  2154. break;
  2155. case TARGET_SO_ERROR:
  2156. optname = SO_ERROR;
  2157. break;
  2158. case TARGET_SO_DONTROUTE:
  2159. optname = SO_DONTROUTE;
  2160. break;
  2161. case TARGET_SO_BROADCAST:
  2162. optname = SO_BROADCAST;
  2163. break;
  2164. case TARGET_SO_SNDBUF:
  2165. optname = SO_SNDBUF;
  2166. break;
  2167. case TARGET_SO_SNDBUFFORCE:
  2168. optname = SO_SNDBUFFORCE;
  2169. break;
  2170. case TARGET_SO_RCVBUF:
  2171. optname = SO_RCVBUF;
  2172. break;
  2173. case TARGET_SO_RCVBUFFORCE:
  2174. optname = SO_RCVBUFFORCE;
  2175. break;
  2176. case TARGET_SO_KEEPALIVE:
  2177. optname = SO_KEEPALIVE;
  2178. break;
  2179. case TARGET_SO_OOBINLINE:
  2180. optname = SO_OOBINLINE;
  2181. break;
  2182. case TARGET_SO_NO_CHECK:
  2183. optname = SO_NO_CHECK;
  2184. break;
  2185. case TARGET_SO_PRIORITY:
  2186. optname = SO_PRIORITY;
  2187. break;
  2188. #ifdef SO_BSDCOMPAT
  2189. case TARGET_SO_BSDCOMPAT:
  2190. optname = SO_BSDCOMPAT;
  2191. break;
  2192. #endif
  2193. case TARGET_SO_PASSCRED:
  2194. optname = SO_PASSCRED;
  2195. break;
  2196. case TARGET_SO_PASSSEC:
  2197. optname = SO_PASSSEC;
  2198. break;
  2199. case TARGET_SO_TIMESTAMP:
  2200. optname = SO_TIMESTAMP;
  2201. break;
  2202. case TARGET_SO_RCVLOWAT:
  2203. optname = SO_RCVLOWAT;
  2204. break;
  2205. default:
  2206. goto unimplemented;
  2207. }
  2208. if (optlen < sizeof(uint32_t))
  2209. return -TARGET_EINVAL;
  2210. if (get_user_u32(val, optval_addr))
  2211. return -TARGET_EFAULT;
  2212. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
  2213. break;
  2214. #ifdef SOL_NETLINK
  2215. case SOL_NETLINK:
  2216. switch (optname) {
  2217. case NETLINK_PKTINFO:
  2218. case NETLINK_ADD_MEMBERSHIP:
  2219. case NETLINK_DROP_MEMBERSHIP:
  2220. case NETLINK_BROADCAST_ERROR:
  2221. case NETLINK_NO_ENOBUFS:
  2222. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
  2223. case NETLINK_LISTEN_ALL_NSID:
  2224. case NETLINK_CAP_ACK:
  2225. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
  2226. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
  2227. case NETLINK_EXT_ACK:
  2228. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2229. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
  2230. case NETLINK_GET_STRICT_CHK:
  2231. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2232. break;
  2233. default:
  2234. goto unimplemented;
  2235. }
  2236. val = 0;
  2237. if (optlen < sizeof(uint32_t)) {
  2238. return -TARGET_EINVAL;
  2239. }
  2240. if (get_user_u32(val, optval_addr)) {
  2241. return -TARGET_EFAULT;
  2242. }
  2243. ret = get_errno(setsockopt(sockfd, SOL_NETLINK, optname, &val,
  2244. sizeof(val)));
  2245. break;
  2246. #endif /* SOL_NETLINK */
  2247. default:
  2248. unimplemented:
  2249. qemu_log_mask(LOG_UNIMP, "Unsupported setsockopt level=%d optname=%d\n",
  2250. level, optname);
  2251. ret = -TARGET_ENOPROTOOPT;
  2252. }
  2253. return ret;
  2254. }
  2255. /* do_getsockopt() Must return target values and target errnos. */
  2256. static abi_long do_getsockopt(int sockfd, int level, int optname,
  2257. abi_ulong optval_addr, abi_ulong optlen)
  2258. {
  2259. abi_long ret;
  2260. int len, val;
  2261. socklen_t lv;
  2262. switch(level) {
  2263. case TARGET_SOL_SOCKET:
  2264. level = SOL_SOCKET;
  2265. switch (optname) {
  2266. /* These don't just return a single integer */
  2267. case TARGET_SO_PEERNAME:
  2268. goto unimplemented;
  2269. case TARGET_SO_RCVTIMEO: {
  2270. struct timeval tv;
  2271. socklen_t tvlen;
  2272. optname = SO_RCVTIMEO;
  2273. get_timeout:
  2274. if (get_user_u32(len, optlen)) {
  2275. return -TARGET_EFAULT;
  2276. }
  2277. if (len < 0) {
  2278. return -TARGET_EINVAL;
  2279. }
  2280. tvlen = sizeof(tv);
  2281. ret = get_errno(getsockopt(sockfd, level, optname,
  2282. &tv, &tvlen));
  2283. if (ret < 0) {
  2284. return ret;
  2285. }
  2286. if (len > sizeof(struct target_timeval)) {
  2287. len = sizeof(struct target_timeval);
  2288. }
  2289. if (copy_to_user_timeval(optval_addr, &tv)) {
  2290. return -TARGET_EFAULT;
  2291. }
  2292. if (put_user_u32(len, optlen)) {
  2293. return -TARGET_EFAULT;
  2294. }
  2295. break;
  2296. }
  2297. case TARGET_SO_SNDTIMEO:
  2298. optname = SO_SNDTIMEO;
  2299. goto get_timeout;
  2300. case TARGET_SO_PEERCRED: {
  2301. struct ucred cr;
  2302. socklen_t crlen;
  2303. struct target_ucred *tcr;
  2304. if (get_user_u32(len, optlen)) {
  2305. return -TARGET_EFAULT;
  2306. }
  2307. if (len < 0) {
  2308. return -TARGET_EINVAL;
  2309. }
  2310. crlen = sizeof(cr);
  2311. ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
  2312. &cr, &crlen));
  2313. if (ret < 0) {
  2314. return ret;
  2315. }
  2316. if (len > crlen) {
  2317. len = crlen;
  2318. }
  2319. if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
  2320. return -TARGET_EFAULT;
  2321. }
  2322. __put_user(cr.pid, &tcr->pid);
  2323. __put_user(cr.uid, &tcr->uid);
  2324. __put_user(cr.gid, &tcr->gid);
  2325. unlock_user_struct(tcr, optval_addr, 1);
  2326. if (put_user_u32(len, optlen)) {
  2327. return -TARGET_EFAULT;
  2328. }
  2329. break;
  2330. }
  2331. case TARGET_SO_PEERSEC: {
  2332. char *name;
  2333. if (get_user_u32(len, optlen)) {
  2334. return -TARGET_EFAULT;
  2335. }
  2336. if (len < 0) {
  2337. return -TARGET_EINVAL;
  2338. }
  2339. name = lock_user(VERIFY_WRITE, optval_addr, len, 0);
  2340. if (!name) {
  2341. return -TARGET_EFAULT;
  2342. }
  2343. lv = len;
  2344. ret = get_errno(getsockopt(sockfd, level, SO_PEERSEC,
  2345. name, &lv));
  2346. if (put_user_u32(lv, optlen)) {
  2347. ret = -TARGET_EFAULT;
  2348. }
  2349. unlock_user(name, optval_addr, lv);
  2350. break;
  2351. }
  2352. case TARGET_SO_LINGER:
  2353. {
  2354. struct linger lg;
  2355. socklen_t lglen;
  2356. struct target_linger *tlg;
  2357. if (get_user_u32(len, optlen)) {
  2358. return -TARGET_EFAULT;
  2359. }
  2360. if (len < 0) {
  2361. return -TARGET_EINVAL;
  2362. }
  2363. lglen = sizeof(lg);
  2364. ret = get_errno(getsockopt(sockfd, level, SO_LINGER,
  2365. &lg, &lglen));
  2366. if (ret < 0) {
  2367. return ret;
  2368. }
  2369. if (len > lglen) {
  2370. len = lglen;
  2371. }
  2372. if (!lock_user_struct(VERIFY_WRITE, tlg, optval_addr, 0)) {
  2373. return -TARGET_EFAULT;
  2374. }
  2375. __put_user(lg.l_onoff, &tlg->l_onoff);
  2376. __put_user(lg.l_linger, &tlg->l_linger);
  2377. unlock_user_struct(tlg, optval_addr, 1);
  2378. if (put_user_u32(len, optlen)) {
  2379. return -TARGET_EFAULT;
  2380. }
  2381. break;
  2382. }
  2383. /* Options with 'int' argument. */
  2384. case TARGET_SO_DEBUG:
  2385. optname = SO_DEBUG;
  2386. goto int_case;
  2387. case TARGET_SO_REUSEADDR:
  2388. optname = SO_REUSEADDR;
  2389. goto int_case;
  2390. #ifdef SO_REUSEPORT
  2391. case TARGET_SO_REUSEPORT:
  2392. optname = SO_REUSEPORT;
  2393. goto int_case;
  2394. #endif
  2395. case TARGET_SO_TYPE:
  2396. optname = SO_TYPE;
  2397. goto int_case;
  2398. case TARGET_SO_ERROR:
  2399. optname = SO_ERROR;
  2400. goto int_case;
  2401. case TARGET_SO_DONTROUTE:
  2402. optname = SO_DONTROUTE;
  2403. goto int_case;
  2404. case TARGET_SO_BROADCAST:
  2405. optname = SO_BROADCAST;
  2406. goto int_case;
  2407. case TARGET_SO_SNDBUF:
  2408. optname = SO_SNDBUF;
  2409. goto int_case;
  2410. case TARGET_SO_RCVBUF:
  2411. optname = SO_RCVBUF;
  2412. goto int_case;
  2413. case TARGET_SO_KEEPALIVE:
  2414. optname = SO_KEEPALIVE;
  2415. goto int_case;
  2416. case TARGET_SO_OOBINLINE:
  2417. optname = SO_OOBINLINE;
  2418. goto int_case;
  2419. case TARGET_SO_NO_CHECK:
  2420. optname = SO_NO_CHECK;
  2421. goto int_case;
  2422. case TARGET_SO_PRIORITY:
  2423. optname = SO_PRIORITY;
  2424. goto int_case;
  2425. #ifdef SO_BSDCOMPAT
  2426. case TARGET_SO_BSDCOMPAT:
  2427. optname = SO_BSDCOMPAT;
  2428. goto int_case;
  2429. #endif
  2430. case TARGET_SO_PASSCRED:
  2431. optname = SO_PASSCRED;
  2432. goto int_case;
  2433. case TARGET_SO_TIMESTAMP:
  2434. optname = SO_TIMESTAMP;
  2435. goto int_case;
  2436. case TARGET_SO_RCVLOWAT:
  2437. optname = SO_RCVLOWAT;
  2438. goto int_case;
  2439. case TARGET_SO_ACCEPTCONN:
  2440. optname = SO_ACCEPTCONN;
  2441. goto int_case;
  2442. case TARGET_SO_PROTOCOL:
  2443. optname = SO_PROTOCOL;
  2444. goto int_case;
  2445. case TARGET_SO_DOMAIN:
  2446. optname = SO_DOMAIN;
  2447. goto int_case;
  2448. default:
  2449. goto int_case;
  2450. }
  2451. break;
  2452. case SOL_TCP:
  2453. case SOL_UDP:
  2454. /* TCP and UDP options all take an 'int' value. */
  2455. int_case:
  2456. if (get_user_u32(len, optlen))
  2457. return -TARGET_EFAULT;
  2458. if (len < 0)
  2459. return -TARGET_EINVAL;
  2460. lv = sizeof(lv);
  2461. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2462. if (ret < 0)
  2463. return ret;
  2464. switch (optname) {
  2465. case SO_TYPE:
  2466. val = host_to_target_sock_type(val);
  2467. break;
  2468. case SO_ERROR:
  2469. val = host_to_target_errno(val);
  2470. break;
  2471. }
  2472. if (len > lv)
  2473. len = lv;
  2474. if (len == 4) {
  2475. if (put_user_u32(val, optval_addr))
  2476. return -TARGET_EFAULT;
  2477. } else {
  2478. if (put_user_u8(val, optval_addr))
  2479. return -TARGET_EFAULT;
  2480. }
  2481. if (put_user_u32(len, optlen))
  2482. return -TARGET_EFAULT;
  2483. break;
  2484. case SOL_IP:
  2485. switch(optname) {
  2486. case IP_TOS:
  2487. case IP_TTL:
  2488. case IP_HDRINCL:
  2489. case IP_ROUTER_ALERT:
  2490. case IP_RECVOPTS:
  2491. case IP_RETOPTS:
  2492. case IP_PKTINFO:
  2493. case IP_MTU_DISCOVER:
  2494. case IP_RECVERR:
  2495. case IP_RECVTOS:
  2496. #ifdef IP_FREEBIND
  2497. case IP_FREEBIND:
  2498. #endif
  2499. case IP_MULTICAST_TTL:
  2500. case IP_MULTICAST_LOOP:
  2501. if (get_user_u32(len, optlen))
  2502. return -TARGET_EFAULT;
  2503. if (len < 0)
  2504. return -TARGET_EINVAL;
  2505. lv = sizeof(lv);
  2506. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2507. if (ret < 0)
  2508. return ret;
  2509. if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
  2510. len = 1;
  2511. if (put_user_u32(len, optlen)
  2512. || put_user_u8(val, optval_addr))
  2513. return -TARGET_EFAULT;
  2514. } else {
  2515. if (len > sizeof(int))
  2516. len = sizeof(int);
  2517. if (put_user_u32(len, optlen)
  2518. || put_user_u32(val, optval_addr))
  2519. return -TARGET_EFAULT;
  2520. }
  2521. break;
  2522. default:
  2523. ret = -TARGET_ENOPROTOOPT;
  2524. break;
  2525. }
  2526. break;
  2527. case SOL_IPV6:
  2528. switch (optname) {
  2529. case IPV6_MTU_DISCOVER:
  2530. case IPV6_MTU:
  2531. case IPV6_V6ONLY:
  2532. case IPV6_RECVPKTINFO:
  2533. case IPV6_UNICAST_HOPS:
  2534. case IPV6_MULTICAST_HOPS:
  2535. case IPV6_MULTICAST_LOOP:
  2536. case IPV6_RECVERR:
  2537. case IPV6_RECVHOPLIMIT:
  2538. case IPV6_2292HOPLIMIT:
  2539. case IPV6_CHECKSUM:
  2540. case IPV6_ADDRFORM:
  2541. case IPV6_2292PKTINFO:
  2542. case IPV6_RECVTCLASS:
  2543. case IPV6_RECVRTHDR:
  2544. case IPV6_2292RTHDR:
  2545. case IPV6_RECVHOPOPTS:
  2546. case IPV6_2292HOPOPTS:
  2547. case IPV6_RECVDSTOPTS:
  2548. case IPV6_2292DSTOPTS:
  2549. case IPV6_TCLASS:
  2550. case IPV6_ADDR_PREFERENCES:
  2551. #ifdef IPV6_RECVPATHMTU
  2552. case IPV6_RECVPATHMTU:
  2553. #endif
  2554. #ifdef IPV6_TRANSPARENT
  2555. case IPV6_TRANSPARENT:
  2556. #endif
  2557. #ifdef IPV6_FREEBIND
  2558. case IPV6_FREEBIND:
  2559. #endif
  2560. #ifdef IPV6_RECVORIGDSTADDR
  2561. case IPV6_RECVORIGDSTADDR:
  2562. #endif
  2563. if (get_user_u32(len, optlen))
  2564. return -TARGET_EFAULT;
  2565. if (len < 0)
  2566. return -TARGET_EINVAL;
  2567. lv = sizeof(lv);
  2568. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2569. if (ret < 0)
  2570. return ret;
  2571. if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
  2572. len = 1;
  2573. if (put_user_u32(len, optlen)
  2574. || put_user_u8(val, optval_addr))
  2575. return -TARGET_EFAULT;
  2576. } else {
  2577. if (len > sizeof(int))
  2578. len = sizeof(int);
  2579. if (put_user_u32(len, optlen)
  2580. || put_user_u32(val, optval_addr))
  2581. return -TARGET_EFAULT;
  2582. }
  2583. break;
  2584. default:
  2585. ret = -TARGET_ENOPROTOOPT;
  2586. break;
  2587. }
  2588. break;
  2589. #ifdef SOL_NETLINK
  2590. case SOL_NETLINK:
  2591. switch (optname) {
  2592. case NETLINK_PKTINFO:
  2593. case NETLINK_BROADCAST_ERROR:
  2594. case NETLINK_NO_ENOBUFS:
  2595. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
  2596. case NETLINK_LISTEN_ALL_NSID:
  2597. case NETLINK_CAP_ACK:
  2598. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
  2599. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
  2600. case NETLINK_EXT_ACK:
  2601. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2602. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
  2603. case NETLINK_GET_STRICT_CHK:
  2604. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2605. if (get_user_u32(len, optlen)) {
  2606. return -TARGET_EFAULT;
  2607. }
  2608. if (len != sizeof(val)) {
  2609. return -TARGET_EINVAL;
  2610. }
  2611. lv = len;
  2612. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2613. if (ret < 0) {
  2614. return ret;
  2615. }
  2616. if (put_user_u32(lv, optlen)
  2617. || put_user_u32(val, optval_addr)) {
  2618. return -TARGET_EFAULT;
  2619. }
  2620. break;
  2621. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
  2622. case NETLINK_LIST_MEMBERSHIPS:
  2623. {
  2624. uint32_t *results;
  2625. int i;
  2626. if (get_user_u32(len, optlen)) {
  2627. return -TARGET_EFAULT;
  2628. }
  2629. if (len < 0) {
  2630. return -TARGET_EINVAL;
  2631. }
  2632. results = lock_user(VERIFY_WRITE, optval_addr, len, 1);
  2633. if (!results && len > 0) {
  2634. return -TARGET_EFAULT;
  2635. }
  2636. lv = len;
  2637. ret = get_errno(getsockopt(sockfd, level, optname, results, &lv));
  2638. if (ret < 0) {
  2639. unlock_user(results, optval_addr, 0);
  2640. return ret;
  2641. }
  2642. /* swap host endianness to target endianness. */
  2643. for (i = 0; i < (len / sizeof(uint32_t)); i++) {
  2644. results[i] = tswap32(results[i]);
  2645. }
  2646. if (put_user_u32(lv, optlen)) {
  2647. return -TARGET_EFAULT;
  2648. }
  2649. unlock_user(results, optval_addr, 0);
  2650. break;
  2651. }
  2652. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
  2653. default:
  2654. goto unimplemented;
  2655. }
  2656. break;
  2657. #endif /* SOL_NETLINK */
  2658. default:
  2659. unimplemented:
  2660. qemu_log_mask(LOG_UNIMP,
  2661. "getsockopt level=%d optname=%d not yet supported\n",
  2662. level, optname);
  2663. ret = -TARGET_EOPNOTSUPP;
  2664. break;
  2665. }
  2666. return ret;
  2667. }
  2668. /* Convert target low/high pair representing file offset into the host
  2669. * low/high pair. This function doesn't handle offsets bigger than 64 bits
  2670. * as the kernel doesn't handle them either.
  2671. */
  2672. static void target_to_host_low_high(abi_ulong tlow,
  2673. abi_ulong thigh,
  2674. unsigned long *hlow,
  2675. unsigned long *hhigh)
  2676. {
  2677. uint64_t off = tlow |
  2678. ((unsigned long long)thigh << TARGET_LONG_BITS / 2) <<
  2679. TARGET_LONG_BITS / 2;
  2680. *hlow = off;
  2681. *hhigh = (off >> HOST_LONG_BITS / 2) >> HOST_LONG_BITS / 2;
  2682. }
  2683. static struct iovec *lock_iovec(int type, abi_ulong target_addr,
  2684. abi_ulong count, int copy)
  2685. {
  2686. struct target_iovec *target_vec;
  2687. struct iovec *vec;
  2688. abi_ulong total_len, max_len;
  2689. int i;
  2690. int err = 0;
  2691. bool bad_address = false;
  2692. if (count == 0) {
  2693. errno = 0;
  2694. return NULL;
  2695. }
  2696. if (count > IOV_MAX) {
  2697. errno = EINVAL;
  2698. return NULL;
  2699. }
  2700. vec = g_try_new0(struct iovec, count);
  2701. if (vec == NULL) {
  2702. errno = ENOMEM;
  2703. return NULL;
  2704. }
  2705. target_vec = lock_user(VERIFY_READ, target_addr,
  2706. count * sizeof(struct target_iovec), 1);
  2707. if (target_vec == NULL) {
  2708. err = EFAULT;
  2709. goto fail2;
  2710. }
  2711. /* ??? If host page size > target page size, this will result in a
  2712. value larger than what we can actually support. */
  2713. max_len = 0x7fffffff & TARGET_PAGE_MASK;
  2714. total_len = 0;
  2715. for (i = 0; i < count; i++) {
  2716. abi_ulong base = tswapal(target_vec[i].iov_base);
  2717. abi_long len = tswapal(target_vec[i].iov_len);
  2718. if (len < 0) {
  2719. err = EINVAL;
  2720. goto fail;
  2721. } else if (len == 0) {
  2722. /* Zero length pointer is ignored. */
  2723. vec[i].iov_base = 0;
  2724. } else {
  2725. vec[i].iov_base = lock_user(type, base, len, copy);
  2726. /* If the first buffer pointer is bad, this is a fault. But
  2727. * subsequent bad buffers will result in a partial write; this
  2728. * is realized by filling the vector with null pointers and
  2729. * zero lengths. */
  2730. if (!vec[i].iov_base) {
  2731. if (i == 0) {
  2732. err = EFAULT;
  2733. goto fail;
  2734. } else {
  2735. bad_address = true;
  2736. }
  2737. }
  2738. if (bad_address) {
  2739. len = 0;
  2740. }
  2741. if (len > max_len - total_len) {
  2742. len = max_len - total_len;
  2743. }
  2744. }
  2745. vec[i].iov_len = len;
  2746. total_len += len;
  2747. }
  2748. unlock_user(target_vec, target_addr, 0);
  2749. return vec;
  2750. fail:
  2751. while (--i >= 0) {
  2752. if (tswapal(target_vec[i].iov_len) > 0) {
  2753. unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0);
  2754. }
  2755. }
  2756. unlock_user(target_vec, target_addr, 0);
  2757. fail2:
  2758. g_free(vec);
  2759. errno = err;
  2760. return NULL;
  2761. }
  2762. static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
  2763. abi_ulong count, int copy)
  2764. {
  2765. struct target_iovec *target_vec;
  2766. int i;
  2767. target_vec = lock_user(VERIFY_READ, target_addr,
  2768. count * sizeof(struct target_iovec), 1);
  2769. if (target_vec) {
  2770. for (i = 0; i < count; i++) {
  2771. abi_ulong base = tswapal(target_vec[i].iov_base);
  2772. abi_long len = tswapal(target_vec[i].iov_len);
  2773. if (len < 0) {
  2774. break;
  2775. }
  2776. unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
  2777. }
  2778. unlock_user(target_vec, target_addr, 0);
  2779. }
  2780. g_free(vec);
  2781. }
  2782. static inline int target_to_host_sock_type(int *type)
  2783. {
  2784. int host_type = 0;
  2785. int target_type = *type;
  2786. switch (target_type & TARGET_SOCK_TYPE_MASK) {
  2787. case TARGET_SOCK_DGRAM:
  2788. host_type = SOCK_DGRAM;
  2789. break;
  2790. case TARGET_SOCK_STREAM:
  2791. host_type = SOCK_STREAM;
  2792. break;
  2793. default:
  2794. host_type = target_type & TARGET_SOCK_TYPE_MASK;
  2795. break;
  2796. }
  2797. if (target_type & TARGET_SOCK_CLOEXEC) {
  2798. #if defined(SOCK_CLOEXEC)
  2799. host_type |= SOCK_CLOEXEC;
  2800. #else
  2801. return -TARGET_EINVAL;
  2802. #endif
  2803. }
  2804. if (target_type & TARGET_SOCK_NONBLOCK) {
  2805. #if defined(SOCK_NONBLOCK)
  2806. host_type |= SOCK_NONBLOCK;
  2807. #elif !defined(O_NONBLOCK)
  2808. return -TARGET_EINVAL;
  2809. #endif
  2810. }
  2811. *type = host_type;
  2812. return 0;
  2813. }
  2814. /* Try to emulate socket type flags after socket creation. */
  2815. static int sock_flags_fixup(int fd, int target_type)
  2816. {
  2817. #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
  2818. if (target_type & TARGET_SOCK_NONBLOCK) {
  2819. int flags = fcntl(fd, F_GETFL);
  2820. if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) {
  2821. close(fd);
  2822. return -TARGET_EINVAL;
  2823. }
  2824. }
  2825. #endif
  2826. return fd;
  2827. }
  2828. /* do_socket() Must return target values and target errnos. */
  2829. static abi_long do_socket(int domain, int type, int protocol)
  2830. {
  2831. int target_type = type;
  2832. int ret;
  2833. ret = target_to_host_sock_type(&type);
  2834. if (ret) {
  2835. return ret;
  2836. }
  2837. if (domain == PF_NETLINK && !(
  2838. #ifdef CONFIG_RTNETLINK
  2839. protocol == NETLINK_ROUTE ||
  2840. #endif
  2841. protocol == NETLINK_KOBJECT_UEVENT ||
  2842. protocol == NETLINK_AUDIT)) {
  2843. return -TARGET_EPROTONOSUPPORT;
  2844. }
  2845. if (domain == AF_PACKET ||
  2846. (domain == AF_INET && type == SOCK_PACKET)) {
  2847. protocol = tswap16(protocol);
  2848. }
  2849. ret = get_errno(socket(domain, type, protocol));
  2850. if (ret >= 0) {
  2851. ret = sock_flags_fixup(ret, target_type);
  2852. if (type == SOCK_PACKET) {
  2853. /* Manage an obsolete case :
  2854. * if socket type is SOCK_PACKET, bind by name
  2855. */
  2856. fd_trans_register(ret, &target_packet_trans);
  2857. } else if (domain == PF_NETLINK) {
  2858. switch (protocol) {
  2859. #ifdef CONFIG_RTNETLINK
  2860. case NETLINK_ROUTE:
  2861. fd_trans_register(ret, &target_netlink_route_trans);
  2862. break;
  2863. #endif
  2864. case NETLINK_KOBJECT_UEVENT:
  2865. /* nothing to do: messages are strings */
  2866. break;
  2867. case NETLINK_AUDIT:
  2868. fd_trans_register(ret, &target_netlink_audit_trans);
  2869. break;
  2870. default:
  2871. g_assert_not_reached();
  2872. }
  2873. }
  2874. }
  2875. return ret;
  2876. }
  2877. /* do_bind() Must return target values and target errnos. */
  2878. static abi_long do_bind(int sockfd, abi_ulong target_addr,
  2879. socklen_t addrlen)
  2880. {
  2881. void *addr;
  2882. abi_long ret;
  2883. if ((int)addrlen < 0) {
  2884. return -TARGET_EINVAL;
  2885. }
  2886. addr = alloca(addrlen+1);
  2887. ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
  2888. if (ret)
  2889. return ret;
  2890. return get_errno(bind(sockfd, addr, addrlen));
  2891. }
  2892. /* do_connect() Must return target values and target errnos. */
  2893. static abi_long do_connect(int sockfd, abi_ulong target_addr,
  2894. socklen_t addrlen)
  2895. {
  2896. void *addr;
  2897. abi_long ret;
  2898. if ((int)addrlen < 0) {
  2899. return -TARGET_EINVAL;
  2900. }
  2901. addr = alloca(addrlen+1);
  2902. ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
  2903. if (ret)
  2904. return ret;
  2905. return get_errno(safe_connect(sockfd, addr, addrlen));
  2906. }
  2907. /* do_sendrecvmsg_locked() Must return target values and target errnos. */
  2908. static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
  2909. int flags, int send)
  2910. {
  2911. abi_long ret, len;
  2912. struct msghdr msg;
  2913. abi_ulong count;
  2914. struct iovec *vec;
  2915. abi_ulong target_vec;
  2916. if (msgp->msg_name) {
  2917. msg.msg_namelen = tswap32(msgp->msg_namelen);
  2918. msg.msg_name = alloca(msg.msg_namelen+1);
  2919. ret = target_to_host_sockaddr(fd, msg.msg_name,
  2920. tswapal(msgp->msg_name),
  2921. msg.msg_namelen);
  2922. if (ret == -TARGET_EFAULT) {
  2923. /* For connected sockets msg_name and msg_namelen must
  2924. * be ignored, so returning EFAULT immediately is wrong.
  2925. * Instead, pass a bad msg_name to the host kernel, and
  2926. * let it decide whether to return EFAULT or not.
  2927. */
  2928. msg.msg_name = (void *)-1;
  2929. } else if (ret) {
  2930. goto out2;
  2931. }
  2932. } else {
  2933. msg.msg_name = NULL;
  2934. msg.msg_namelen = 0;
  2935. }
  2936. msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
  2937. msg.msg_control = alloca(msg.msg_controllen);
  2938. memset(msg.msg_control, 0, msg.msg_controllen);
  2939. msg.msg_flags = tswap32(msgp->msg_flags);
  2940. count = tswapal(msgp->msg_iovlen);
  2941. target_vec = tswapal(msgp->msg_iov);
  2942. if (count > IOV_MAX) {
  2943. /* sendrcvmsg returns a different errno for this condition than
  2944. * readv/writev, so we must catch it here before lock_iovec() does.
  2945. */
  2946. ret = -TARGET_EMSGSIZE;
  2947. goto out2;
  2948. }
  2949. vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
  2950. target_vec, count, send);
  2951. if (vec == NULL) {
  2952. ret = -host_to_target_errno(errno);
  2953. /* allow sending packet without any iov, e.g. with MSG_MORE flag */
  2954. if (!send || ret) {
  2955. goto out2;
  2956. }
  2957. }
  2958. msg.msg_iovlen = count;
  2959. msg.msg_iov = vec;
  2960. if (send) {
  2961. if (fd_trans_target_to_host_data(fd)) {
  2962. void *host_msg;
  2963. host_msg = g_malloc(msg.msg_iov->iov_len);
  2964. memcpy(host_msg, msg.msg_iov->iov_base, msg.msg_iov->iov_len);
  2965. ret = fd_trans_target_to_host_data(fd)(host_msg,
  2966. msg.msg_iov->iov_len);
  2967. if (ret >= 0) {
  2968. msg.msg_iov->iov_base = host_msg;
  2969. ret = get_errno(safe_sendmsg(fd, &msg, flags));
  2970. }
  2971. g_free(host_msg);
  2972. } else {
  2973. ret = target_to_host_cmsg(&msg, msgp);
  2974. if (ret == 0) {
  2975. ret = get_errno(safe_sendmsg(fd, &msg, flags));
  2976. }
  2977. }
  2978. } else {
  2979. ret = get_errno(safe_recvmsg(fd, &msg, flags));
  2980. if (!is_error(ret)) {
  2981. len = ret;
  2982. if (fd_trans_host_to_target_data(fd)) {
  2983. ret = fd_trans_host_to_target_data(fd)(msg.msg_iov->iov_base,
  2984. MIN(msg.msg_iov->iov_len, len));
  2985. }
  2986. if (!is_error(ret)) {
  2987. ret = host_to_target_cmsg(msgp, &msg);
  2988. }
  2989. if (!is_error(ret)) {
  2990. msgp->msg_namelen = tswap32(msg.msg_namelen);
  2991. msgp->msg_flags = tswap32(msg.msg_flags);
  2992. if (msg.msg_name != NULL && msg.msg_name != (void *)-1) {
  2993. ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
  2994. msg.msg_name, msg.msg_namelen);
  2995. if (ret) {
  2996. goto out;
  2997. }
  2998. }
  2999. ret = len;
  3000. }
  3001. }
  3002. }
  3003. out:
  3004. if (vec) {
  3005. unlock_iovec(vec, target_vec, count, !send);
  3006. }
  3007. out2:
  3008. return ret;
  3009. }
  3010. static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
  3011. int flags, int send)
  3012. {
  3013. abi_long ret;
  3014. struct target_msghdr *msgp;
  3015. if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
  3016. msgp,
  3017. target_msg,
  3018. send ? 1 : 0)) {
  3019. return -TARGET_EFAULT;
  3020. }
  3021. ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
  3022. unlock_user_struct(msgp, target_msg, send ? 0 : 1);
  3023. return ret;
  3024. }
  3025. /* We don't rely on the C library to have sendmmsg/recvmmsg support,
  3026. * so it might not have this *mmsg-specific flag either.
  3027. */
  3028. #ifndef MSG_WAITFORONE
  3029. #define MSG_WAITFORONE 0x10000
  3030. #endif
  3031. static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec,
  3032. unsigned int vlen, unsigned int flags,
  3033. int send)
  3034. {
  3035. struct target_mmsghdr *mmsgp;
  3036. abi_long ret = 0;
  3037. int i;
  3038. if (vlen > UIO_MAXIOV) {
  3039. vlen = UIO_MAXIOV;
  3040. }
  3041. mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1);
  3042. if (!mmsgp) {
  3043. return -TARGET_EFAULT;
  3044. }
  3045. for (i = 0; i < vlen; i++) {
  3046. ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send);
  3047. if (is_error(ret)) {
  3048. break;
  3049. }
  3050. mmsgp[i].msg_len = tswap32(ret);
  3051. /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
  3052. if (flags & MSG_WAITFORONE) {
  3053. flags |= MSG_DONTWAIT;
  3054. }
  3055. }
  3056. unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i);
  3057. /* Return number of datagrams sent if we sent any at all;
  3058. * otherwise return the error.
  3059. */
  3060. if (i) {
  3061. return i;
  3062. }
  3063. return ret;
  3064. }
  3065. /* do_accept4() Must return target values and target errnos. */
  3066. static abi_long do_accept4(int fd, abi_ulong target_addr,
  3067. abi_ulong target_addrlen_addr, int flags)
  3068. {
  3069. socklen_t addrlen, ret_addrlen;
  3070. void *addr;
  3071. abi_long ret;
  3072. int host_flags;
  3073. if (flags & ~(TARGET_SOCK_CLOEXEC | TARGET_SOCK_NONBLOCK)) {
  3074. return -TARGET_EINVAL;
  3075. }
  3076. host_flags = 0;
  3077. if (flags & TARGET_SOCK_NONBLOCK) {
  3078. host_flags |= SOCK_NONBLOCK;
  3079. }
  3080. if (flags & TARGET_SOCK_CLOEXEC) {
  3081. host_flags |= SOCK_CLOEXEC;
  3082. }
  3083. if (target_addr == 0) {
  3084. return get_errno(safe_accept4(fd, NULL, NULL, host_flags));
  3085. }
  3086. /* linux returns EFAULT if addrlen pointer is invalid */
  3087. if (get_user_u32(addrlen, target_addrlen_addr))
  3088. return -TARGET_EFAULT;
  3089. if ((int)addrlen < 0) {
  3090. return -TARGET_EINVAL;
  3091. }
  3092. if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
  3093. return -TARGET_EFAULT;
  3094. }
  3095. addr = alloca(addrlen);
  3096. ret_addrlen = addrlen;
  3097. ret = get_errno(safe_accept4(fd, addr, &ret_addrlen, host_flags));
  3098. if (!is_error(ret)) {
  3099. host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
  3100. if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
  3101. ret = -TARGET_EFAULT;
  3102. }
  3103. }
  3104. return ret;
  3105. }
  3106. /* do_getpeername() Must return target values and target errnos. */
  3107. static abi_long do_getpeername(int fd, abi_ulong target_addr,
  3108. abi_ulong target_addrlen_addr)
  3109. {
  3110. socklen_t addrlen, ret_addrlen;
  3111. void *addr;
  3112. abi_long ret;
  3113. if (get_user_u32(addrlen, target_addrlen_addr))
  3114. return -TARGET_EFAULT;
  3115. if ((int)addrlen < 0) {
  3116. return -TARGET_EINVAL;
  3117. }
  3118. if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
  3119. return -TARGET_EFAULT;
  3120. }
  3121. addr = alloca(addrlen);
  3122. ret_addrlen = addrlen;
  3123. ret = get_errno(getpeername(fd, addr, &ret_addrlen));
  3124. if (!is_error(ret)) {
  3125. host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
  3126. if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
  3127. ret = -TARGET_EFAULT;
  3128. }
  3129. }
  3130. return ret;
  3131. }
  3132. /* do_getsockname() Must return target values and target errnos. */
  3133. static abi_long do_getsockname(int fd, abi_ulong target_addr,
  3134. abi_ulong target_addrlen_addr)
  3135. {
  3136. socklen_t addrlen, ret_addrlen;
  3137. void *addr;
  3138. abi_long ret;
  3139. if (get_user_u32(addrlen, target_addrlen_addr))
  3140. return -TARGET_EFAULT;
  3141. if ((int)addrlen < 0) {
  3142. return -TARGET_EINVAL;
  3143. }
  3144. if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
  3145. return -TARGET_EFAULT;
  3146. }
  3147. addr = alloca(addrlen);
  3148. ret_addrlen = addrlen;
  3149. ret = get_errno(getsockname(fd, addr, &ret_addrlen));
  3150. if (!is_error(ret)) {
  3151. host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
  3152. if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
  3153. ret = -TARGET_EFAULT;
  3154. }
  3155. }
  3156. return ret;
  3157. }
  3158. /* do_socketpair() Must return target values and target errnos. */
  3159. static abi_long do_socketpair(int domain, int type, int protocol,
  3160. abi_ulong target_tab_addr)
  3161. {
  3162. int tab[2];
  3163. abi_long ret;
  3164. target_to_host_sock_type(&type);
  3165. ret = get_errno(socketpair(domain, type, protocol, tab));
  3166. if (!is_error(ret)) {
  3167. if (put_user_s32(tab[0], target_tab_addr)
  3168. || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
  3169. ret = -TARGET_EFAULT;
  3170. }
  3171. return ret;
  3172. }
  3173. /* do_sendto() Must return target values and target errnos. */
  3174. static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
  3175. abi_ulong target_addr, socklen_t addrlen)
  3176. {
  3177. void *addr;
  3178. void *host_msg;
  3179. void *copy_msg = NULL;
  3180. abi_long ret;
  3181. if ((int)addrlen < 0) {
  3182. return -TARGET_EINVAL;
  3183. }
  3184. host_msg = lock_user(VERIFY_READ, msg, len, 1);
  3185. if (!host_msg)
  3186. return -TARGET_EFAULT;
  3187. if (fd_trans_target_to_host_data(fd)) {
  3188. copy_msg = host_msg;
  3189. host_msg = g_malloc(len);
  3190. memcpy(host_msg, copy_msg, len);
  3191. ret = fd_trans_target_to_host_data(fd)(host_msg, len);
  3192. if (ret < 0) {
  3193. goto fail;
  3194. }
  3195. }
  3196. if (target_addr) {
  3197. addr = alloca(addrlen+1);
  3198. ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen);
  3199. if (ret) {
  3200. goto fail;
  3201. }
  3202. ret = get_errno(safe_sendto(fd, host_msg, len, flags, addr, addrlen));
  3203. } else {
  3204. ret = get_errno(safe_sendto(fd, host_msg, len, flags, NULL, 0));
  3205. }
  3206. fail:
  3207. if (copy_msg) {
  3208. g_free(host_msg);
  3209. host_msg = copy_msg;
  3210. }
  3211. unlock_user(host_msg, msg, 0);
  3212. return ret;
  3213. }
  3214. /* do_recvfrom() Must return target values and target errnos. */
  3215. static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
  3216. abi_ulong target_addr,
  3217. abi_ulong target_addrlen)
  3218. {
  3219. socklen_t addrlen, ret_addrlen;
  3220. void *addr;
  3221. void *host_msg;
  3222. abi_long ret;
  3223. if (!msg) {
  3224. host_msg = NULL;
  3225. } else {
  3226. host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
  3227. if (!host_msg) {
  3228. return -TARGET_EFAULT;
  3229. }
  3230. }
  3231. if (target_addr) {
  3232. if (get_user_u32(addrlen, target_addrlen)) {
  3233. ret = -TARGET_EFAULT;
  3234. goto fail;
  3235. }
  3236. if ((int)addrlen < 0) {
  3237. ret = -TARGET_EINVAL;
  3238. goto fail;
  3239. }
  3240. addr = alloca(addrlen);
  3241. ret_addrlen = addrlen;
  3242. ret = get_errno(safe_recvfrom(fd, host_msg, len, flags,
  3243. addr, &ret_addrlen));
  3244. } else {
  3245. addr = NULL; /* To keep compiler quiet. */
  3246. addrlen = 0; /* To keep compiler quiet. */
  3247. ret = get_errno(safe_recvfrom(fd, host_msg, len, flags, NULL, 0));
  3248. }
  3249. if (!is_error(ret)) {
  3250. if (fd_trans_host_to_target_data(fd)) {
  3251. abi_long trans;
  3252. trans = fd_trans_host_to_target_data(fd)(host_msg, MIN(ret, len));
  3253. if (is_error(trans)) {
  3254. ret = trans;
  3255. goto fail;
  3256. }
  3257. }
  3258. if (target_addr) {
  3259. host_to_target_sockaddr(target_addr, addr,
  3260. MIN(addrlen, ret_addrlen));
  3261. if (put_user_u32(ret_addrlen, target_addrlen)) {
  3262. ret = -TARGET_EFAULT;
  3263. goto fail;
  3264. }
  3265. }
  3266. unlock_user(host_msg, msg, len);
  3267. } else {
  3268. fail:
  3269. unlock_user(host_msg, msg, 0);
  3270. }
  3271. return ret;
  3272. }
  3273. #ifdef TARGET_NR_socketcall
  3274. /* do_socketcall() must return target values and target errnos. */
  3275. static abi_long do_socketcall(int num, abi_ulong vptr)
  3276. {
  3277. static const unsigned nargs[] = { /* number of arguments per operation */
  3278. [TARGET_SYS_SOCKET] = 3, /* domain, type, protocol */
  3279. [TARGET_SYS_BIND] = 3, /* fd, addr, addrlen */
  3280. [TARGET_SYS_CONNECT] = 3, /* fd, addr, addrlen */
  3281. [TARGET_SYS_LISTEN] = 2, /* fd, backlog */
  3282. [TARGET_SYS_ACCEPT] = 3, /* fd, addr, addrlen */
  3283. [TARGET_SYS_GETSOCKNAME] = 3, /* fd, addr, addrlen */
  3284. [TARGET_SYS_GETPEERNAME] = 3, /* fd, addr, addrlen */
  3285. [TARGET_SYS_SOCKETPAIR] = 4, /* domain, type, protocol, tab */
  3286. [TARGET_SYS_SEND] = 4, /* fd, msg, len, flags */
  3287. [TARGET_SYS_RECV] = 4, /* fd, msg, len, flags */
  3288. [TARGET_SYS_SENDTO] = 6, /* fd, msg, len, flags, addr, addrlen */
  3289. [TARGET_SYS_RECVFROM] = 6, /* fd, msg, len, flags, addr, addrlen */
  3290. [TARGET_SYS_SHUTDOWN] = 2, /* fd, how */
  3291. [TARGET_SYS_SETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
  3292. [TARGET_SYS_GETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
  3293. [TARGET_SYS_SENDMSG] = 3, /* fd, msg, flags */
  3294. [TARGET_SYS_RECVMSG] = 3, /* fd, msg, flags */
  3295. [TARGET_SYS_ACCEPT4] = 4, /* fd, addr, addrlen, flags */
  3296. [TARGET_SYS_RECVMMSG] = 4, /* fd, msgvec, vlen, flags */
  3297. [TARGET_SYS_SENDMMSG] = 4, /* fd, msgvec, vlen, flags */
  3298. };
  3299. abi_long a[6]; /* max 6 args */
  3300. unsigned i;
  3301. /* check the range of the first argument num */
  3302. /* (TARGET_SYS_SENDMMSG is the highest among TARGET_SYS_xxx) */
  3303. if (num < 1 || num > TARGET_SYS_SENDMMSG) {
  3304. return -TARGET_EINVAL;
  3305. }
  3306. /* ensure we have space for args */
  3307. if (nargs[num] > ARRAY_SIZE(a)) {
  3308. return -TARGET_EINVAL;
  3309. }
  3310. /* collect the arguments in a[] according to nargs[] */
  3311. for (i = 0; i < nargs[num]; ++i) {
  3312. if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) {
  3313. return -TARGET_EFAULT;
  3314. }
  3315. }
  3316. /* now when we have the args, invoke the appropriate underlying function */
  3317. switch (num) {
  3318. case TARGET_SYS_SOCKET: /* domain, type, protocol */
  3319. return do_socket(a[0], a[1], a[2]);
  3320. case TARGET_SYS_BIND: /* sockfd, addr, addrlen */
  3321. return do_bind(a[0], a[1], a[2]);
  3322. case TARGET_SYS_CONNECT: /* sockfd, addr, addrlen */
  3323. return do_connect(a[0], a[1], a[2]);
  3324. case TARGET_SYS_LISTEN: /* sockfd, backlog */
  3325. return get_errno(listen(a[0], a[1]));
  3326. case TARGET_SYS_ACCEPT: /* sockfd, addr, addrlen */
  3327. return do_accept4(a[0], a[1], a[2], 0);
  3328. case TARGET_SYS_GETSOCKNAME: /* sockfd, addr, addrlen */
  3329. return do_getsockname(a[0], a[1], a[2]);
  3330. case TARGET_SYS_GETPEERNAME: /* sockfd, addr, addrlen */
  3331. return do_getpeername(a[0], a[1], a[2]);
  3332. case TARGET_SYS_SOCKETPAIR: /* domain, type, protocol, tab */
  3333. return do_socketpair(a[0], a[1], a[2], a[3]);
  3334. case TARGET_SYS_SEND: /* sockfd, msg, len, flags */
  3335. return do_sendto(a[0], a[1], a[2], a[3], 0, 0);
  3336. case TARGET_SYS_RECV: /* sockfd, msg, len, flags */
  3337. return do_recvfrom(a[0], a[1], a[2], a[3], 0, 0);
  3338. case TARGET_SYS_SENDTO: /* sockfd, msg, len, flags, addr, addrlen */
  3339. return do_sendto(a[0], a[1], a[2], a[3], a[4], a[5]);
  3340. case TARGET_SYS_RECVFROM: /* sockfd, msg, len, flags, addr, addrlen */
  3341. return do_recvfrom(a[0], a[1], a[2], a[3], a[4], a[5]);
  3342. case TARGET_SYS_SHUTDOWN: /* sockfd, how */
  3343. return get_errno(shutdown(a[0], a[1]));
  3344. case TARGET_SYS_SETSOCKOPT: /* sockfd, level, optname, optval, optlen */
  3345. return do_setsockopt(a[0], a[1], a[2], a[3], a[4]);
  3346. case TARGET_SYS_GETSOCKOPT: /* sockfd, level, optname, optval, optlen */
  3347. return do_getsockopt(a[0], a[1], a[2], a[3], a[4]);
  3348. case TARGET_SYS_SENDMSG: /* sockfd, msg, flags */
  3349. return do_sendrecvmsg(a[0], a[1], a[2], 1);
  3350. case TARGET_SYS_RECVMSG: /* sockfd, msg, flags */
  3351. return do_sendrecvmsg(a[0], a[1], a[2], 0);
  3352. case TARGET_SYS_ACCEPT4: /* sockfd, addr, addrlen, flags */
  3353. return do_accept4(a[0], a[1], a[2], a[3]);
  3354. case TARGET_SYS_RECVMMSG: /* sockfd, msgvec, vlen, flags */
  3355. return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 0);
  3356. case TARGET_SYS_SENDMMSG: /* sockfd, msgvec, vlen, flags */
  3357. return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 1);
  3358. default:
  3359. qemu_log_mask(LOG_UNIMP, "Unsupported socketcall: %d\n", num);
  3360. return -TARGET_EINVAL;
  3361. }
  3362. }
  3363. #endif
  3364. #ifndef TARGET_SEMID64_DS
  3365. /* asm-generic version of this struct */
  3366. struct target_semid64_ds
  3367. {
  3368. struct target_ipc_perm sem_perm;
  3369. abi_ulong sem_otime;
  3370. #if TARGET_ABI_BITS == 32
  3371. abi_ulong __unused1;
  3372. #endif
  3373. abi_ulong sem_ctime;
  3374. #if TARGET_ABI_BITS == 32
  3375. abi_ulong __unused2;
  3376. #endif
  3377. abi_ulong sem_nsems;
  3378. abi_ulong __unused3;
  3379. abi_ulong __unused4;
  3380. };
  3381. #endif
  3382. static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
  3383. abi_ulong target_addr)
  3384. {
  3385. struct target_ipc_perm *target_ip;
  3386. struct target_semid64_ds *target_sd;
  3387. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  3388. return -TARGET_EFAULT;
  3389. target_ip = &(target_sd->sem_perm);
  3390. host_ip->__key = tswap32(target_ip->__key);
  3391. host_ip->uid = tswap32(target_ip->uid);
  3392. host_ip->gid = tswap32(target_ip->gid);
  3393. host_ip->cuid = tswap32(target_ip->cuid);
  3394. host_ip->cgid = tswap32(target_ip->cgid);
  3395. #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
  3396. host_ip->mode = tswap32(target_ip->mode);
  3397. #else
  3398. host_ip->mode = tswap16(target_ip->mode);
  3399. #endif
  3400. #if defined(TARGET_PPC)
  3401. host_ip->__seq = tswap32(target_ip->__seq);
  3402. #else
  3403. host_ip->__seq = tswap16(target_ip->__seq);
  3404. #endif
  3405. unlock_user_struct(target_sd, target_addr, 0);
  3406. return 0;
  3407. }
  3408. static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
  3409. struct ipc_perm *host_ip)
  3410. {
  3411. struct target_ipc_perm *target_ip;
  3412. struct target_semid64_ds *target_sd;
  3413. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  3414. return -TARGET_EFAULT;
  3415. target_ip = &(target_sd->sem_perm);
  3416. target_ip->__key = tswap32(host_ip->__key);
  3417. target_ip->uid = tswap32(host_ip->uid);
  3418. target_ip->gid = tswap32(host_ip->gid);
  3419. target_ip->cuid = tswap32(host_ip->cuid);
  3420. target_ip->cgid = tswap32(host_ip->cgid);
  3421. #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
  3422. target_ip->mode = tswap32(host_ip->mode);
  3423. #else
  3424. target_ip->mode = tswap16(host_ip->mode);
  3425. #endif
  3426. #if defined(TARGET_PPC)
  3427. target_ip->__seq = tswap32(host_ip->__seq);
  3428. #else
  3429. target_ip->__seq = tswap16(host_ip->__seq);
  3430. #endif
  3431. unlock_user_struct(target_sd, target_addr, 1);
  3432. return 0;
  3433. }
  3434. static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
  3435. abi_ulong target_addr)
  3436. {
  3437. struct target_semid64_ds *target_sd;
  3438. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  3439. return -TARGET_EFAULT;
  3440. if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
  3441. return -TARGET_EFAULT;
  3442. host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
  3443. host_sd->sem_otime = tswapal(target_sd->sem_otime);
  3444. host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
  3445. unlock_user_struct(target_sd, target_addr, 0);
  3446. return 0;
  3447. }
  3448. static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
  3449. struct semid_ds *host_sd)
  3450. {
  3451. struct target_semid64_ds *target_sd;
  3452. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  3453. return -TARGET_EFAULT;
  3454. if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
  3455. return -TARGET_EFAULT;
  3456. target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
  3457. target_sd->sem_otime = tswapal(host_sd->sem_otime);
  3458. target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
  3459. unlock_user_struct(target_sd, target_addr, 1);
  3460. return 0;
  3461. }
  3462. struct target_seminfo {
  3463. int semmap;
  3464. int semmni;
  3465. int semmns;
  3466. int semmnu;
  3467. int semmsl;
  3468. int semopm;
  3469. int semume;
  3470. int semusz;
  3471. int semvmx;
  3472. int semaem;
  3473. };
  3474. static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
  3475. struct seminfo *host_seminfo)
  3476. {
  3477. struct target_seminfo *target_seminfo;
  3478. if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
  3479. return -TARGET_EFAULT;
  3480. __put_user(host_seminfo->semmap, &target_seminfo->semmap);
  3481. __put_user(host_seminfo->semmni, &target_seminfo->semmni);
  3482. __put_user(host_seminfo->semmns, &target_seminfo->semmns);
  3483. __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
  3484. __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
  3485. __put_user(host_seminfo->semopm, &target_seminfo->semopm);
  3486. __put_user(host_seminfo->semume, &target_seminfo->semume);
  3487. __put_user(host_seminfo->semusz, &target_seminfo->semusz);
  3488. __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
  3489. __put_user(host_seminfo->semaem, &target_seminfo->semaem);
  3490. unlock_user_struct(target_seminfo, target_addr, 1);
  3491. return 0;
  3492. }
  3493. union semun {
  3494. int val;
  3495. struct semid_ds *buf;
  3496. unsigned short *array;
  3497. struct seminfo *__buf;
  3498. };
  3499. union target_semun {
  3500. int val;
  3501. abi_ulong buf;
  3502. abi_ulong array;
  3503. abi_ulong __buf;
  3504. };
  3505. static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
  3506. abi_ulong target_addr)
  3507. {
  3508. int nsems;
  3509. unsigned short *array;
  3510. union semun semun;
  3511. struct semid_ds semid_ds;
  3512. int i, ret;
  3513. semun.buf = &semid_ds;
  3514. ret = semctl(semid, 0, IPC_STAT, semun);
  3515. if (ret == -1)
  3516. return get_errno(ret);
  3517. nsems = semid_ds.sem_nsems;
  3518. *host_array = g_try_new(unsigned short, nsems);
  3519. if (!*host_array) {
  3520. return -TARGET_ENOMEM;
  3521. }
  3522. array = lock_user(VERIFY_READ, target_addr,
  3523. nsems*sizeof(unsigned short), 1);
  3524. if (!array) {
  3525. g_free(*host_array);
  3526. return -TARGET_EFAULT;
  3527. }
  3528. for(i=0; i<nsems; i++) {
  3529. __get_user((*host_array)[i], &array[i]);
  3530. }
  3531. unlock_user(array, target_addr, 0);
  3532. return 0;
  3533. }
  3534. static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
  3535. unsigned short **host_array)
  3536. {
  3537. int nsems;
  3538. unsigned short *array;
  3539. union semun semun;
  3540. struct semid_ds semid_ds;
  3541. int i, ret;
  3542. semun.buf = &semid_ds;
  3543. ret = semctl(semid, 0, IPC_STAT, semun);
  3544. if (ret == -1)
  3545. return get_errno(ret);
  3546. nsems = semid_ds.sem_nsems;
  3547. array = lock_user(VERIFY_WRITE, target_addr,
  3548. nsems*sizeof(unsigned short), 0);
  3549. if (!array)
  3550. return -TARGET_EFAULT;
  3551. for(i=0; i<nsems; i++) {
  3552. __put_user((*host_array)[i], &array[i]);
  3553. }
  3554. g_free(*host_array);
  3555. unlock_user(array, target_addr, 1);
  3556. return 0;
  3557. }
  3558. static inline abi_long do_semctl(int semid, int semnum, int cmd,
  3559. abi_ulong target_arg)
  3560. {
  3561. union target_semun target_su = { .buf = target_arg };
  3562. union semun arg;
  3563. struct semid_ds dsarg;
  3564. unsigned short *array = NULL;
  3565. struct seminfo seminfo;
  3566. abi_long ret = -TARGET_EINVAL;
  3567. abi_long err;
  3568. cmd &= 0xff;
  3569. switch( cmd ) {
  3570. case GETVAL:
  3571. case SETVAL:
  3572. /* In 64 bit cross-endian situations, we will erroneously pick up
  3573. * the wrong half of the union for the "val" element. To rectify
  3574. * this, the entire 8-byte structure is byteswapped, followed by
  3575. * a swap of the 4 byte val field. In other cases, the data is
  3576. * already in proper host byte order. */
  3577. if (sizeof(target_su.val) != (sizeof(target_su.buf))) {
  3578. target_su.buf = tswapal(target_su.buf);
  3579. arg.val = tswap32(target_su.val);
  3580. } else {
  3581. arg.val = target_su.val;
  3582. }
  3583. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3584. break;
  3585. case GETALL:
  3586. case SETALL:
  3587. err = target_to_host_semarray(semid, &array, target_su.array);
  3588. if (err)
  3589. return err;
  3590. arg.array = array;
  3591. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3592. err = host_to_target_semarray(semid, target_su.array, &array);
  3593. if (err)
  3594. return err;
  3595. break;
  3596. case IPC_STAT:
  3597. case IPC_SET:
  3598. case SEM_STAT:
  3599. err = target_to_host_semid_ds(&dsarg, target_su.buf);
  3600. if (err)
  3601. return err;
  3602. arg.buf = &dsarg;
  3603. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3604. err = host_to_target_semid_ds(target_su.buf, &dsarg);
  3605. if (err)
  3606. return err;
  3607. break;
  3608. case IPC_INFO:
  3609. case SEM_INFO:
  3610. arg.__buf = &seminfo;
  3611. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3612. err = host_to_target_seminfo(target_su.__buf, &seminfo);
  3613. if (err)
  3614. return err;
  3615. break;
  3616. case IPC_RMID:
  3617. case GETPID:
  3618. case GETNCNT:
  3619. case GETZCNT:
  3620. ret = get_errno(semctl(semid, semnum, cmd, NULL));
  3621. break;
  3622. }
  3623. return ret;
  3624. }
  3625. struct target_sembuf {
  3626. unsigned short sem_num;
  3627. short sem_op;
  3628. short sem_flg;
  3629. };
  3630. static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
  3631. abi_ulong target_addr,
  3632. unsigned nsops)
  3633. {
  3634. struct target_sembuf *target_sembuf;
  3635. int i;
  3636. target_sembuf = lock_user(VERIFY_READ, target_addr,
  3637. nsops*sizeof(struct target_sembuf), 1);
  3638. if (!target_sembuf)
  3639. return -TARGET_EFAULT;
  3640. for(i=0; i<nsops; i++) {
  3641. __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
  3642. __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
  3643. __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
  3644. }
  3645. unlock_user(target_sembuf, target_addr, 0);
  3646. return 0;
  3647. }
  3648. #if defined(TARGET_NR_ipc) || defined(TARGET_NR_semop) || \
  3649. defined(TARGET_NR_semtimedop) || defined(TARGET_NR_semtimedop_time64)
  3650. /*
  3651. * This macro is required to handle the s390 variants, which passes the
  3652. * arguments in a different order than default.
  3653. */
  3654. #ifdef __s390x__
  3655. #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
  3656. (__nsops), (__timeout), (__sops)
  3657. #else
  3658. #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
  3659. (__nsops), 0, (__sops), (__timeout)
  3660. #endif
  3661. static inline abi_long do_semtimedop(int semid,
  3662. abi_long ptr,
  3663. unsigned nsops,
  3664. abi_long timeout, bool time64)
  3665. {
  3666. struct sembuf *sops;
  3667. struct timespec ts, *pts = NULL;
  3668. abi_long ret;
  3669. if (timeout) {
  3670. pts = &ts;
  3671. if (time64) {
  3672. if (target_to_host_timespec64(pts, timeout)) {
  3673. return -TARGET_EFAULT;
  3674. }
  3675. } else {
  3676. if (target_to_host_timespec(pts, timeout)) {
  3677. return -TARGET_EFAULT;
  3678. }
  3679. }
  3680. }
  3681. if (nsops > TARGET_SEMOPM) {
  3682. return -TARGET_E2BIG;
  3683. }
  3684. sops = g_new(struct sembuf, nsops);
  3685. if (target_to_host_sembuf(sops, ptr, nsops)) {
  3686. g_free(sops);
  3687. return -TARGET_EFAULT;
  3688. }
  3689. ret = -TARGET_ENOSYS;
  3690. #ifdef __NR_semtimedop
  3691. ret = get_errno(safe_semtimedop(semid, sops, nsops, pts));
  3692. #endif
  3693. #ifdef __NR_ipc
  3694. if (ret == -TARGET_ENOSYS) {
  3695. ret = get_errno(safe_ipc(IPCOP_semtimedop, semid,
  3696. SEMTIMEDOP_IPC_ARGS(nsops, sops, (long)pts)));
  3697. }
  3698. #endif
  3699. g_free(sops);
  3700. return ret;
  3701. }
  3702. #endif
  3703. struct target_msqid_ds
  3704. {
  3705. struct target_ipc_perm msg_perm;
  3706. abi_ulong msg_stime;
  3707. #if TARGET_ABI_BITS == 32
  3708. abi_ulong __unused1;
  3709. #endif
  3710. abi_ulong msg_rtime;
  3711. #if TARGET_ABI_BITS == 32
  3712. abi_ulong __unused2;
  3713. #endif
  3714. abi_ulong msg_ctime;
  3715. #if TARGET_ABI_BITS == 32
  3716. abi_ulong __unused3;
  3717. #endif
  3718. abi_ulong __msg_cbytes;
  3719. abi_ulong msg_qnum;
  3720. abi_ulong msg_qbytes;
  3721. abi_ulong msg_lspid;
  3722. abi_ulong msg_lrpid;
  3723. abi_ulong __unused4;
  3724. abi_ulong __unused5;
  3725. };
  3726. static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
  3727. abi_ulong target_addr)
  3728. {
  3729. struct target_msqid_ds *target_md;
  3730. if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
  3731. return -TARGET_EFAULT;
  3732. if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
  3733. return -TARGET_EFAULT;
  3734. host_md->msg_stime = tswapal(target_md->msg_stime);
  3735. host_md->msg_rtime = tswapal(target_md->msg_rtime);
  3736. host_md->msg_ctime = tswapal(target_md->msg_ctime);
  3737. host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
  3738. host_md->msg_qnum = tswapal(target_md->msg_qnum);
  3739. host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
  3740. host_md->msg_lspid = tswapal(target_md->msg_lspid);
  3741. host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
  3742. unlock_user_struct(target_md, target_addr, 0);
  3743. return 0;
  3744. }
  3745. static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
  3746. struct msqid_ds *host_md)
  3747. {
  3748. struct target_msqid_ds *target_md;
  3749. if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
  3750. return -TARGET_EFAULT;
  3751. if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
  3752. return -TARGET_EFAULT;
  3753. target_md->msg_stime = tswapal(host_md->msg_stime);
  3754. target_md->msg_rtime = tswapal(host_md->msg_rtime);
  3755. target_md->msg_ctime = tswapal(host_md->msg_ctime);
  3756. target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
  3757. target_md->msg_qnum = tswapal(host_md->msg_qnum);
  3758. target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
  3759. target_md->msg_lspid = tswapal(host_md->msg_lspid);
  3760. target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
  3761. unlock_user_struct(target_md, target_addr, 1);
  3762. return 0;
  3763. }
  3764. struct target_msginfo {
  3765. int msgpool;
  3766. int msgmap;
  3767. int msgmax;
  3768. int msgmnb;
  3769. int msgmni;
  3770. int msgssz;
  3771. int msgtql;
  3772. unsigned short int msgseg;
  3773. };
  3774. static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
  3775. struct msginfo *host_msginfo)
  3776. {
  3777. struct target_msginfo *target_msginfo;
  3778. if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
  3779. return -TARGET_EFAULT;
  3780. __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
  3781. __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
  3782. __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
  3783. __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
  3784. __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
  3785. __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
  3786. __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
  3787. __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
  3788. unlock_user_struct(target_msginfo, target_addr, 1);
  3789. return 0;
  3790. }
  3791. static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
  3792. {
  3793. struct msqid_ds dsarg;
  3794. struct msginfo msginfo;
  3795. abi_long ret = -TARGET_EINVAL;
  3796. cmd &= 0xff;
  3797. switch (cmd) {
  3798. case IPC_STAT:
  3799. case IPC_SET:
  3800. case MSG_STAT:
  3801. if (target_to_host_msqid_ds(&dsarg,ptr))
  3802. return -TARGET_EFAULT;
  3803. ret = get_errno(msgctl(msgid, cmd, &dsarg));
  3804. if (host_to_target_msqid_ds(ptr,&dsarg))
  3805. return -TARGET_EFAULT;
  3806. break;
  3807. case IPC_RMID:
  3808. ret = get_errno(msgctl(msgid, cmd, NULL));
  3809. break;
  3810. case IPC_INFO:
  3811. case MSG_INFO:
  3812. ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
  3813. if (host_to_target_msginfo(ptr, &msginfo))
  3814. return -TARGET_EFAULT;
  3815. break;
  3816. }
  3817. return ret;
  3818. }
  3819. struct target_msgbuf {
  3820. abi_long mtype;
  3821. char mtext[1];
  3822. };
  3823. static inline abi_long do_msgsnd(int msqid, abi_long msgp,
  3824. ssize_t msgsz, int msgflg)
  3825. {
  3826. struct target_msgbuf *target_mb;
  3827. struct msgbuf *host_mb;
  3828. abi_long ret = 0;
  3829. if (msgsz < 0) {
  3830. return -TARGET_EINVAL;
  3831. }
  3832. if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
  3833. return -TARGET_EFAULT;
  3834. host_mb = g_try_malloc(msgsz + sizeof(long));
  3835. if (!host_mb) {
  3836. unlock_user_struct(target_mb, msgp, 0);
  3837. return -TARGET_ENOMEM;
  3838. }
  3839. host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
  3840. memcpy(host_mb->mtext, target_mb->mtext, msgsz);
  3841. ret = -TARGET_ENOSYS;
  3842. #ifdef __NR_msgsnd
  3843. ret = get_errno(safe_msgsnd(msqid, host_mb, msgsz, msgflg));
  3844. #endif
  3845. #ifdef __NR_ipc
  3846. if (ret == -TARGET_ENOSYS) {
  3847. #ifdef __s390x__
  3848. ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
  3849. host_mb));
  3850. #else
  3851. ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
  3852. host_mb, 0));
  3853. #endif
  3854. }
  3855. #endif
  3856. g_free(host_mb);
  3857. unlock_user_struct(target_mb, msgp, 0);
  3858. return ret;
  3859. }
  3860. #ifdef __NR_ipc
  3861. #if defined(__sparc__)
  3862. /* SPARC for msgrcv it does not use the kludge on final 2 arguments. */
  3863. #define MSGRCV_ARGS(__msgp, __msgtyp) __msgp, __msgtyp
  3864. #elif defined(__s390x__)
  3865. /* The s390 sys_ipc variant has only five parameters. */
  3866. #define MSGRCV_ARGS(__msgp, __msgtyp) \
  3867. ((long int[]){(long int)__msgp, __msgtyp})
  3868. #else
  3869. #define MSGRCV_ARGS(__msgp, __msgtyp) \
  3870. ((long int[]){(long int)__msgp, __msgtyp}), 0
  3871. #endif
  3872. #endif
  3873. static inline abi_long do_msgrcv(int msqid, abi_long msgp,
  3874. ssize_t msgsz, abi_long msgtyp,
  3875. int msgflg)
  3876. {
  3877. struct target_msgbuf *target_mb;
  3878. char *target_mtext;
  3879. struct msgbuf *host_mb;
  3880. abi_long ret = 0;
  3881. if (msgsz < 0) {
  3882. return -TARGET_EINVAL;
  3883. }
  3884. if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
  3885. return -TARGET_EFAULT;
  3886. host_mb = g_try_malloc(msgsz + sizeof(long));
  3887. if (!host_mb) {
  3888. ret = -TARGET_ENOMEM;
  3889. goto end;
  3890. }
  3891. ret = -TARGET_ENOSYS;
  3892. #ifdef __NR_msgrcv
  3893. ret = get_errno(safe_msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
  3894. #endif
  3895. #ifdef __NR_ipc
  3896. if (ret == -TARGET_ENOSYS) {
  3897. ret = get_errno(safe_ipc(IPCOP_CALL(1, IPCOP_msgrcv), msqid, msgsz,
  3898. msgflg, MSGRCV_ARGS(host_mb, msgtyp)));
  3899. }
  3900. #endif
  3901. if (ret > 0) {
  3902. abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
  3903. target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
  3904. if (!target_mtext) {
  3905. ret = -TARGET_EFAULT;
  3906. goto end;
  3907. }
  3908. memcpy(target_mb->mtext, host_mb->mtext, ret);
  3909. unlock_user(target_mtext, target_mtext_addr, ret);
  3910. }
  3911. target_mb->mtype = tswapal(host_mb->mtype);
  3912. end:
  3913. if (target_mb)
  3914. unlock_user_struct(target_mb, msgp, 1);
  3915. g_free(host_mb);
  3916. return ret;
  3917. }
  3918. static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
  3919. abi_ulong target_addr)
  3920. {
  3921. struct target_shmid_ds *target_sd;
  3922. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  3923. return -TARGET_EFAULT;
  3924. if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
  3925. return -TARGET_EFAULT;
  3926. __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
  3927. __get_user(host_sd->shm_atime, &target_sd->shm_atime);
  3928. __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
  3929. __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
  3930. __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
  3931. __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
  3932. __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
  3933. unlock_user_struct(target_sd, target_addr, 0);
  3934. return 0;
  3935. }
  3936. static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
  3937. struct shmid_ds *host_sd)
  3938. {
  3939. struct target_shmid_ds *target_sd;
  3940. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  3941. return -TARGET_EFAULT;
  3942. if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
  3943. return -TARGET_EFAULT;
  3944. __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
  3945. __put_user(host_sd->shm_atime, &target_sd->shm_atime);
  3946. __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
  3947. __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
  3948. __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
  3949. __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
  3950. __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
  3951. unlock_user_struct(target_sd, target_addr, 1);
  3952. return 0;
  3953. }
  3954. struct target_shminfo {
  3955. abi_ulong shmmax;
  3956. abi_ulong shmmin;
  3957. abi_ulong shmmni;
  3958. abi_ulong shmseg;
  3959. abi_ulong shmall;
  3960. };
  3961. static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
  3962. struct shminfo *host_shminfo)
  3963. {
  3964. struct target_shminfo *target_shminfo;
  3965. if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
  3966. return -TARGET_EFAULT;
  3967. __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
  3968. __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
  3969. __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
  3970. __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
  3971. __put_user(host_shminfo->shmall, &target_shminfo->shmall);
  3972. unlock_user_struct(target_shminfo, target_addr, 1);
  3973. return 0;
  3974. }
  3975. struct target_shm_info {
  3976. int used_ids;
  3977. abi_ulong shm_tot;
  3978. abi_ulong shm_rss;
  3979. abi_ulong shm_swp;
  3980. abi_ulong swap_attempts;
  3981. abi_ulong swap_successes;
  3982. };
  3983. static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
  3984. struct shm_info *host_shm_info)
  3985. {
  3986. struct target_shm_info *target_shm_info;
  3987. if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
  3988. return -TARGET_EFAULT;
  3989. __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
  3990. __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
  3991. __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
  3992. __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
  3993. __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
  3994. __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
  3995. unlock_user_struct(target_shm_info, target_addr, 1);
  3996. return 0;
  3997. }
  3998. static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
  3999. {
  4000. struct shmid_ds dsarg;
  4001. struct shminfo shminfo;
  4002. struct shm_info shm_info;
  4003. abi_long ret = -TARGET_EINVAL;
  4004. cmd &= 0xff;
  4005. switch(cmd) {
  4006. case IPC_STAT:
  4007. case IPC_SET:
  4008. case SHM_STAT:
  4009. if (target_to_host_shmid_ds(&dsarg, buf))
  4010. return -TARGET_EFAULT;
  4011. ret = get_errno(shmctl(shmid, cmd, &dsarg));
  4012. if (host_to_target_shmid_ds(buf, &dsarg))
  4013. return -TARGET_EFAULT;
  4014. break;
  4015. case IPC_INFO:
  4016. ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
  4017. if (host_to_target_shminfo(buf, &shminfo))
  4018. return -TARGET_EFAULT;
  4019. break;
  4020. case SHM_INFO:
  4021. ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
  4022. if (host_to_target_shm_info(buf, &shm_info))
  4023. return -TARGET_EFAULT;
  4024. break;
  4025. case IPC_RMID:
  4026. case SHM_LOCK:
  4027. case SHM_UNLOCK:
  4028. ret = get_errno(shmctl(shmid, cmd, NULL));
  4029. break;
  4030. }
  4031. return ret;
  4032. }
  4033. #ifdef TARGET_NR_ipc
  4034. /* ??? This only works with linear mappings. */
  4035. /* do_ipc() must return target values and target errnos. */
  4036. static abi_long do_ipc(CPUArchState *cpu_env,
  4037. unsigned int call, abi_long first,
  4038. abi_long second, abi_long third,
  4039. abi_long ptr, abi_long fifth)
  4040. {
  4041. int version;
  4042. abi_long ret = 0;
  4043. version = call >> 16;
  4044. call &= 0xffff;
  4045. switch (call) {
  4046. case IPCOP_semop:
  4047. ret = do_semtimedop(first, ptr, second, 0, false);
  4048. break;
  4049. case IPCOP_semtimedop:
  4050. /*
  4051. * The s390 sys_ipc variant has only five parameters instead of six
  4052. * (as for default variant) and the only difference is the handling of
  4053. * SEMTIMEDOP where on s390 the third parameter is used as a pointer
  4054. * to a struct timespec where the generic variant uses fifth parameter.
  4055. */
  4056. #if defined(TARGET_S390X)
  4057. ret = do_semtimedop(first, ptr, second, third, TARGET_ABI_BITS == 64);
  4058. #else
  4059. ret = do_semtimedop(first, ptr, second, fifth, TARGET_ABI_BITS == 64);
  4060. #endif
  4061. break;
  4062. case IPCOP_semget:
  4063. ret = get_errno(semget(first, second, third));
  4064. break;
  4065. case IPCOP_semctl: {
  4066. /* The semun argument to semctl is passed by value, so dereference the
  4067. * ptr argument. */
  4068. abi_ulong atptr;
  4069. get_user_ual(atptr, ptr);
  4070. ret = do_semctl(first, second, third, atptr);
  4071. break;
  4072. }
  4073. case IPCOP_msgget:
  4074. ret = get_errno(msgget(first, second));
  4075. break;
  4076. case IPCOP_msgsnd:
  4077. ret = do_msgsnd(first, ptr, second, third);
  4078. break;
  4079. case IPCOP_msgctl:
  4080. ret = do_msgctl(first, second, ptr);
  4081. break;
  4082. case IPCOP_msgrcv:
  4083. switch (version) {
  4084. case 0:
  4085. {
  4086. struct target_ipc_kludge {
  4087. abi_long msgp;
  4088. abi_long msgtyp;
  4089. } *tmp;
  4090. if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
  4091. ret = -TARGET_EFAULT;
  4092. break;
  4093. }
  4094. ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
  4095. unlock_user_struct(tmp, ptr, 0);
  4096. break;
  4097. }
  4098. default:
  4099. ret = do_msgrcv(first, ptr, second, fifth, third);
  4100. }
  4101. break;
  4102. case IPCOP_shmat:
  4103. switch (version) {
  4104. default:
  4105. {
  4106. abi_ulong raddr;
  4107. raddr = target_shmat(cpu_env, first, ptr, second);
  4108. if (is_error(raddr))
  4109. return get_errno(raddr);
  4110. if (put_user_ual(raddr, third))
  4111. return -TARGET_EFAULT;
  4112. break;
  4113. }
  4114. case 1:
  4115. ret = -TARGET_EINVAL;
  4116. break;
  4117. }
  4118. break;
  4119. case IPCOP_shmdt:
  4120. ret = target_shmdt(ptr);
  4121. break;
  4122. case IPCOP_shmget:
  4123. /* IPC_* flag values are the same on all linux platforms */
  4124. ret = get_errno(shmget(first, second, third));
  4125. break;
  4126. /* IPC_* and SHM_* command values are the same on all linux platforms */
  4127. case IPCOP_shmctl:
  4128. ret = do_shmctl(first, second, ptr);
  4129. break;
  4130. default:
  4131. qemu_log_mask(LOG_UNIMP, "Unsupported ipc call: %d (version %d)\n",
  4132. call, version);
  4133. ret = -TARGET_ENOSYS;
  4134. break;
  4135. }
  4136. return ret;
  4137. }
  4138. #endif
  4139. /* kernel structure types definitions */
  4140. #define STRUCT(name, ...) STRUCT_ ## name,
  4141. #define STRUCT_SPECIAL(name) STRUCT_ ## name,
  4142. enum {
  4143. #include "syscall_types.h"
  4144. STRUCT_MAX
  4145. };
  4146. #undef STRUCT
  4147. #undef STRUCT_SPECIAL
  4148. #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
  4149. #define STRUCT_SPECIAL(name)
  4150. #include "syscall_types.h"
  4151. #undef STRUCT
  4152. #undef STRUCT_SPECIAL
  4153. #define MAX_STRUCT_SIZE 4096
  4154. #ifdef CONFIG_FIEMAP
  4155. /* So fiemap access checks don't overflow on 32 bit systems.
  4156. * This is very slightly smaller than the limit imposed by
  4157. * the underlying kernel.
  4158. */
  4159. #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
  4160. / sizeof(struct fiemap_extent))
  4161. static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
  4162. int fd, int cmd, abi_long arg)
  4163. {
  4164. /* The parameter for this ioctl is a struct fiemap followed
  4165. * by an array of struct fiemap_extent whose size is set
  4166. * in fiemap->fm_extent_count. The array is filled in by the
  4167. * ioctl.
  4168. */
  4169. int target_size_in, target_size_out;
  4170. struct fiemap *fm;
  4171. const argtype *arg_type = ie->arg_type;
  4172. const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
  4173. void *argptr, *p;
  4174. abi_long ret;
  4175. int i, extent_size = thunk_type_size(extent_arg_type, 0);
  4176. uint32_t outbufsz;
  4177. int free_fm = 0;
  4178. assert(arg_type[0] == TYPE_PTR);
  4179. assert(ie->access == IOC_RW);
  4180. arg_type++;
  4181. target_size_in = thunk_type_size(arg_type, 0);
  4182. argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
  4183. if (!argptr) {
  4184. return -TARGET_EFAULT;
  4185. }
  4186. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4187. unlock_user(argptr, arg, 0);
  4188. fm = (struct fiemap *)buf_temp;
  4189. if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
  4190. return -TARGET_EINVAL;
  4191. }
  4192. outbufsz = sizeof (*fm) +
  4193. (sizeof(struct fiemap_extent) * fm->fm_extent_count);
  4194. if (outbufsz > MAX_STRUCT_SIZE) {
  4195. /* We can't fit all the extents into the fixed size buffer.
  4196. * Allocate one that is large enough and use it instead.
  4197. */
  4198. fm = g_try_malloc(outbufsz);
  4199. if (!fm) {
  4200. return -TARGET_ENOMEM;
  4201. }
  4202. memcpy(fm, buf_temp, sizeof(struct fiemap));
  4203. free_fm = 1;
  4204. }
  4205. ret = get_errno(safe_ioctl(fd, ie->host_cmd, fm));
  4206. if (!is_error(ret)) {
  4207. target_size_out = target_size_in;
  4208. /* An extent_count of 0 means we were only counting the extents
  4209. * so there are no structs to copy
  4210. */
  4211. if (fm->fm_extent_count != 0) {
  4212. target_size_out += fm->fm_mapped_extents * extent_size;
  4213. }
  4214. argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
  4215. if (!argptr) {
  4216. ret = -TARGET_EFAULT;
  4217. } else {
  4218. /* Convert the struct fiemap */
  4219. thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
  4220. if (fm->fm_extent_count != 0) {
  4221. p = argptr + target_size_in;
  4222. /* ...and then all the struct fiemap_extents */
  4223. for (i = 0; i < fm->fm_mapped_extents; i++) {
  4224. thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
  4225. THUNK_TARGET);
  4226. p += extent_size;
  4227. }
  4228. }
  4229. unlock_user(argptr, arg, target_size_out);
  4230. }
  4231. }
  4232. if (free_fm) {
  4233. g_free(fm);
  4234. }
  4235. return ret;
  4236. }
  4237. #endif
  4238. static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
  4239. int fd, int cmd, abi_long arg)
  4240. {
  4241. const argtype *arg_type = ie->arg_type;
  4242. int target_size;
  4243. void *argptr;
  4244. int ret;
  4245. struct ifconf *host_ifconf;
  4246. uint32_t outbufsz;
  4247. const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
  4248. const argtype ifreq_max_type[] = { MK_STRUCT(STRUCT_ifmap_ifreq) };
  4249. int target_ifreq_size;
  4250. int nb_ifreq;
  4251. int free_buf = 0;
  4252. int i;
  4253. int target_ifc_len;
  4254. abi_long target_ifc_buf;
  4255. int host_ifc_len;
  4256. char *host_ifc_buf;
  4257. assert(arg_type[0] == TYPE_PTR);
  4258. assert(ie->access == IOC_RW);
  4259. arg_type++;
  4260. target_size = thunk_type_size(arg_type, 0);
  4261. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4262. if (!argptr)
  4263. return -TARGET_EFAULT;
  4264. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4265. unlock_user(argptr, arg, 0);
  4266. host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
  4267. target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
  4268. target_ifreq_size = thunk_type_size(ifreq_max_type, 0);
  4269. if (target_ifc_buf != 0) {
  4270. target_ifc_len = host_ifconf->ifc_len;
  4271. nb_ifreq = target_ifc_len / target_ifreq_size;
  4272. host_ifc_len = nb_ifreq * sizeof(struct ifreq);
  4273. outbufsz = sizeof(*host_ifconf) + host_ifc_len;
  4274. if (outbufsz > MAX_STRUCT_SIZE) {
  4275. /*
  4276. * We can't fit all the extents into the fixed size buffer.
  4277. * Allocate one that is large enough and use it instead.
  4278. */
  4279. host_ifconf = g_try_malloc(outbufsz);
  4280. if (!host_ifconf) {
  4281. return -TARGET_ENOMEM;
  4282. }
  4283. memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
  4284. free_buf = 1;
  4285. }
  4286. host_ifc_buf = (char *)host_ifconf + sizeof(*host_ifconf);
  4287. host_ifconf->ifc_len = host_ifc_len;
  4288. } else {
  4289. host_ifc_buf = NULL;
  4290. }
  4291. host_ifconf->ifc_buf = host_ifc_buf;
  4292. ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_ifconf));
  4293. if (!is_error(ret)) {
  4294. /* convert host ifc_len to target ifc_len */
  4295. nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
  4296. target_ifc_len = nb_ifreq * target_ifreq_size;
  4297. host_ifconf->ifc_len = target_ifc_len;
  4298. /* restore target ifc_buf */
  4299. host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
  4300. /* copy struct ifconf to target user */
  4301. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4302. if (!argptr)
  4303. return -TARGET_EFAULT;
  4304. thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
  4305. unlock_user(argptr, arg, target_size);
  4306. if (target_ifc_buf != 0) {
  4307. /* copy ifreq[] to target user */
  4308. argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
  4309. for (i = 0; i < nb_ifreq ; i++) {
  4310. thunk_convert(argptr + i * target_ifreq_size,
  4311. host_ifc_buf + i * sizeof(struct ifreq),
  4312. ifreq_arg_type, THUNK_TARGET);
  4313. }
  4314. unlock_user(argptr, target_ifc_buf, target_ifc_len);
  4315. }
  4316. }
  4317. if (free_buf) {
  4318. g_free(host_ifconf);
  4319. }
  4320. return ret;
  4321. }
  4322. #if defined(CONFIG_USBFS)
  4323. #if HOST_LONG_BITS > 64
  4324. #error USBDEVFS thunks do not support >64 bit hosts yet.
  4325. #endif
  4326. struct live_urb {
  4327. uint64_t target_urb_adr;
  4328. uint64_t target_buf_adr;
  4329. char *target_buf_ptr;
  4330. struct usbdevfs_urb host_urb;
  4331. };
  4332. static GHashTable *usbdevfs_urb_hashtable(void)
  4333. {
  4334. static GHashTable *urb_hashtable;
  4335. if (!urb_hashtable) {
  4336. urb_hashtable = g_hash_table_new(g_int64_hash, g_int64_equal);
  4337. }
  4338. return urb_hashtable;
  4339. }
  4340. static void urb_hashtable_insert(struct live_urb *urb)
  4341. {
  4342. GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
  4343. g_hash_table_insert(urb_hashtable, urb, urb);
  4344. }
  4345. static struct live_urb *urb_hashtable_lookup(uint64_t target_urb_adr)
  4346. {
  4347. GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
  4348. return g_hash_table_lookup(urb_hashtable, &target_urb_adr);
  4349. }
  4350. static void urb_hashtable_remove(struct live_urb *urb)
  4351. {
  4352. GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
  4353. g_hash_table_remove(urb_hashtable, urb);
  4354. }
  4355. static abi_long
  4356. do_ioctl_usbdevfs_reapurb(const IOCTLEntry *ie, uint8_t *buf_temp,
  4357. int fd, int cmd, abi_long arg)
  4358. {
  4359. const argtype usbfsurb_arg_type[] = { MK_STRUCT(STRUCT_usbdevfs_urb) };
  4360. const argtype ptrvoid_arg_type[] = { TYPE_PTRVOID, 0, 0 };
  4361. struct live_urb *lurb;
  4362. void *argptr;
  4363. uint64_t hurb;
  4364. int target_size;
  4365. uintptr_t target_urb_adr;
  4366. abi_long ret;
  4367. target_size = thunk_type_size(usbfsurb_arg_type, THUNK_TARGET);
  4368. memset(buf_temp, 0, sizeof(uint64_t));
  4369. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4370. if (is_error(ret)) {
  4371. return ret;
  4372. }
  4373. memcpy(&hurb, buf_temp, sizeof(uint64_t));
  4374. lurb = (void *)((uintptr_t)hurb - offsetof(struct live_urb, host_urb));
  4375. if (!lurb->target_urb_adr) {
  4376. return -TARGET_EFAULT;
  4377. }
  4378. urb_hashtable_remove(lurb);
  4379. unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr,
  4380. lurb->host_urb.buffer_length);
  4381. lurb->target_buf_ptr = NULL;
  4382. /* restore the guest buffer pointer */
  4383. lurb->host_urb.buffer = (void *)(uintptr_t)lurb->target_buf_adr;
  4384. /* update the guest urb struct */
  4385. argptr = lock_user(VERIFY_WRITE, lurb->target_urb_adr, target_size, 0);
  4386. if (!argptr) {
  4387. g_free(lurb);
  4388. return -TARGET_EFAULT;
  4389. }
  4390. thunk_convert(argptr, &lurb->host_urb, usbfsurb_arg_type, THUNK_TARGET);
  4391. unlock_user(argptr, lurb->target_urb_adr, target_size);
  4392. target_size = thunk_type_size(ptrvoid_arg_type, THUNK_TARGET);
  4393. /* write back the urb handle */
  4394. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4395. if (!argptr) {
  4396. g_free(lurb);
  4397. return -TARGET_EFAULT;
  4398. }
  4399. /* GHashTable uses 64-bit keys but thunk_convert expects uintptr_t */
  4400. target_urb_adr = lurb->target_urb_adr;
  4401. thunk_convert(argptr, &target_urb_adr, ptrvoid_arg_type, THUNK_TARGET);
  4402. unlock_user(argptr, arg, target_size);
  4403. g_free(lurb);
  4404. return ret;
  4405. }
  4406. static abi_long
  4407. do_ioctl_usbdevfs_discardurb(const IOCTLEntry *ie,
  4408. uint8_t *buf_temp __attribute__((unused)),
  4409. int fd, int cmd, abi_long arg)
  4410. {
  4411. struct live_urb *lurb;
  4412. /* map target address back to host URB with metadata. */
  4413. lurb = urb_hashtable_lookup(arg);
  4414. if (!lurb) {
  4415. return -TARGET_EFAULT;
  4416. }
  4417. return get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb));
  4418. }
  4419. static abi_long
  4420. do_ioctl_usbdevfs_submiturb(const IOCTLEntry *ie, uint8_t *buf_temp,
  4421. int fd, int cmd, abi_long arg)
  4422. {
  4423. const argtype *arg_type = ie->arg_type;
  4424. int target_size;
  4425. abi_long ret;
  4426. void *argptr;
  4427. int rw_dir;
  4428. struct live_urb *lurb;
  4429. /*
  4430. * each submitted URB needs to map to a unique ID for the
  4431. * kernel, and that unique ID needs to be a pointer to
  4432. * host memory. hence, we need to malloc for each URB.
  4433. * isochronous transfers have a variable length struct.
  4434. */
  4435. arg_type++;
  4436. target_size = thunk_type_size(arg_type, THUNK_TARGET);
  4437. /* construct host copy of urb and metadata */
  4438. lurb = g_try_new0(struct live_urb, 1);
  4439. if (!lurb) {
  4440. return -TARGET_ENOMEM;
  4441. }
  4442. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4443. if (!argptr) {
  4444. g_free(lurb);
  4445. return -TARGET_EFAULT;
  4446. }
  4447. thunk_convert(&lurb->host_urb, argptr, arg_type, THUNK_HOST);
  4448. unlock_user(argptr, arg, 0);
  4449. lurb->target_urb_adr = arg;
  4450. lurb->target_buf_adr = (uintptr_t)lurb->host_urb.buffer;
  4451. /* buffer space used depends on endpoint type so lock the entire buffer */
  4452. /* control type urbs should check the buffer contents for true direction */
  4453. rw_dir = lurb->host_urb.endpoint & USB_DIR_IN ? VERIFY_WRITE : VERIFY_READ;
  4454. lurb->target_buf_ptr = lock_user(rw_dir, lurb->target_buf_adr,
  4455. lurb->host_urb.buffer_length, 1);
  4456. if (lurb->target_buf_ptr == NULL) {
  4457. g_free(lurb);
  4458. return -TARGET_EFAULT;
  4459. }
  4460. /* update buffer pointer in host copy */
  4461. lurb->host_urb.buffer = lurb->target_buf_ptr;
  4462. ret = get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb));
  4463. if (is_error(ret)) {
  4464. unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr, 0);
  4465. g_free(lurb);
  4466. } else {
  4467. urb_hashtable_insert(lurb);
  4468. }
  4469. return ret;
  4470. }
  4471. #endif /* CONFIG_USBFS */
  4472. static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
  4473. int cmd, abi_long arg)
  4474. {
  4475. void *argptr;
  4476. struct dm_ioctl *host_dm;
  4477. abi_long guest_data;
  4478. uint32_t guest_data_size;
  4479. int target_size;
  4480. const argtype *arg_type = ie->arg_type;
  4481. abi_long ret;
  4482. void *big_buf = NULL;
  4483. char *host_data;
  4484. arg_type++;
  4485. target_size = thunk_type_size(arg_type, 0);
  4486. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4487. if (!argptr) {
  4488. ret = -TARGET_EFAULT;
  4489. goto out;
  4490. }
  4491. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4492. unlock_user(argptr, arg, 0);
  4493. /* buf_temp is too small, so fetch things into a bigger buffer */
  4494. big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
  4495. memcpy(big_buf, buf_temp, target_size);
  4496. buf_temp = big_buf;
  4497. host_dm = big_buf;
  4498. guest_data = arg + host_dm->data_start;
  4499. if ((guest_data - arg) < 0) {
  4500. ret = -TARGET_EINVAL;
  4501. goto out;
  4502. }
  4503. guest_data_size = host_dm->data_size - host_dm->data_start;
  4504. host_data = (char*)host_dm + host_dm->data_start;
  4505. argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
  4506. if (!argptr) {
  4507. ret = -TARGET_EFAULT;
  4508. goto out;
  4509. }
  4510. switch (ie->host_cmd) {
  4511. case DM_REMOVE_ALL:
  4512. case DM_LIST_DEVICES:
  4513. case DM_DEV_CREATE:
  4514. case DM_DEV_REMOVE:
  4515. case DM_DEV_SUSPEND:
  4516. case DM_DEV_STATUS:
  4517. case DM_DEV_WAIT:
  4518. case DM_TABLE_STATUS:
  4519. case DM_TABLE_CLEAR:
  4520. case DM_TABLE_DEPS:
  4521. case DM_LIST_VERSIONS:
  4522. /* no input data */
  4523. break;
  4524. case DM_DEV_RENAME:
  4525. case DM_DEV_SET_GEOMETRY:
  4526. /* data contains only strings */
  4527. memcpy(host_data, argptr, guest_data_size);
  4528. break;
  4529. case DM_TARGET_MSG:
  4530. memcpy(host_data, argptr, guest_data_size);
  4531. *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
  4532. break;
  4533. case DM_TABLE_LOAD:
  4534. {
  4535. void *gspec = argptr;
  4536. void *cur_data = host_data;
  4537. const argtype dm_arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
  4538. int spec_size = thunk_type_size(dm_arg_type, 0);
  4539. int i;
  4540. for (i = 0; i < host_dm->target_count; i++) {
  4541. struct dm_target_spec *spec = cur_data;
  4542. uint32_t next;
  4543. int slen;
  4544. thunk_convert(spec, gspec, dm_arg_type, THUNK_HOST);
  4545. slen = strlen((char*)gspec + spec_size) + 1;
  4546. next = spec->next;
  4547. spec->next = sizeof(*spec) + slen;
  4548. strcpy((char*)&spec[1], gspec + spec_size);
  4549. gspec += next;
  4550. cur_data += spec->next;
  4551. }
  4552. break;
  4553. }
  4554. default:
  4555. ret = -TARGET_EINVAL;
  4556. unlock_user(argptr, guest_data, 0);
  4557. goto out;
  4558. }
  4559. unlock_user(argptr, guest_data, 0);
  4560. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4561. if (!is_error(ret)) {
  4562. guest_data = arg + host_dm->data_start;
  4563. guest_data_size = host_dm->data_size - host_dm->data_start;
  4564. argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
  4565. switch (ie->host_cmd) {
  4566. case DM_REMOVE_ALL:
  4567. case DM_DEV_CREATE:
  4568. case DM_DEV_REMOVE:
  4569. case DM_DEV_RENAME:
  4570. case DM_DEV_SUSPEND:
  4571. case DM_DEV_STATUS:
  4572. case DM_TABLE_LOAD:
  4573. case DM_TABLE_CLEAR:
  4574. case DM_TARGET_MSG:
  4575. case DM_DEV_SET_GEOMETRY:
  4576. /* no return data */
  4577. break;
  4578. case DM_LIST_DEVICES:
  4579. {
  4580. struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
  4581. uint32_t remaining_data = guest_data_size;
  4582. void *cur_data = argptr;
  4583. const argtype dm_arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
  4584. int nl_size = 12; /* can't use thunk_size due to alignment */
  4585. while (1) {
  4586. uint32_t next = nl->next;
  4587. if (next) {
  4588. nl->next = nl_size + (strlen(nl->name) + 1);
  4589. }
  4590. if (remaining_data < nl->next) {
  4591. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  4592. break;
  4593. }
  4594. thunk_convert(cur_data, nl, dm_arg_type, THUNK_TARGET);
  4595. strcpy(cur_data + nl_size, nl->name);
  4596. cur_data += nl->next;
  4597. remaining_data -= nl->next;
  4598. if (!next) {
  4599. break;
  4600. }
  4601. nl = (void*)nl + next;
  4602. }
  4603. break;
  4604. }
  4605. case DM_DEV_WAIT:
  4606. case DM_TABLE_STATUS:
  4607. {
  4608. struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
  4609. void *cur_data = argptr;
  4610. const argtype dm_arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
  4611. int spec_size = thunk_type_size(dm_arg_type, 0);
  4612. int i;
  4613. for (i = 0; i < host_dm->target_count; i++) {
  4614. uint32_t next = spec->next;
  4615. int slen = strlen((char*)&spec[1]) + 1;
  4616. spec->next = (cur_data - argptr) + spec_size + slen;
  4617. if (guest_data_size < spec->next) {
  4618. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  4619. break;
  4620. }
  4621. thunk_convert(cur_data, spec, dm_arg_type, THUNK_TARGET);
  4622. strcpy(cur_data + spec_size, (char*)&spec[1]);
  4623. cur_data = argptr + spec->next;
  4624. spec = (void*)host_dm + host_dm->data_start + next;
  4625. }
  4626. break;
  4627. }
  4628. case DM_TABLE_DEPS:
  4629. {
  4630. void *hdata = (void*)host_dm + host_dm->data_start;
  4631. int count = *(uint32_t*)hdata;
  4632. uint64_t *hdev = hdata + 8;
  4633. uint64_t *gdev = argptr + 8;
  4634. int i;
  4635. *(uint32_t*)argptr = tswap32(count);
  4636. for (i = 0; i < count; i++) {
  4637. *gdev = tswap64(*hdev);
  4638. gdev++;
  4639. hdev++;
  4640. }
  4641. break;
  4642. }
  4643. case DM_LIST_VERSIONS:
  4644. {
  4645. struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
  4646. uint32_t remaining_data = guest_data_size;
  4647. void *cur_data = argptr;
  4648. const argtype dm_arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
  4649. int vers_size = thunk_type_size(dm_arg_type, 0);
  4650. while (1) {
  4651. uint32_t next = vers->next;
  4652. if (next) {
  4653. vers->next = vers_size + (strlen(vers->name) + 1);
  4654. }
  4655. if (remaining_data < vers->next) {
  4656. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  4657. break;
  4658. }
  4659. thunk_convert(cur_data, vers, dm_arg_type, THUNK_TARGET);
  4660. strcpy(cur_data + vers_size, vers->name);
  4661. cur_data += vers->next;
  4662. remaining_data -= vers->next;
  4663. if (!next) {
  4664. break;
  4665. }
  4666. vers = (void*)vers + next;
  4667. }
  4668. break;
  4669. }
  4670. default:
  4671. unlock_user(argptr, guest_data, 0);
  4672. ret = -TARGET_EINVAL;
  4673. goto out;
  4674. }
  4675. unlock_user(argptr, guest_data, guest_data_size);
  4676. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4677. if (!argptr) {
  4678. ret = -TARGET_EFAULT;
  4679. goto out;
  4680. }
  4681. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  4682. unlock_user(argptr, arg, target_size);
  4683. }
  4684. out:
  4685. g_free(big_buf);
  4686. return ret;
  4687. }
  4688. static abi_long do_ioctl_blkpg(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
  4689. int cmd, abi_long arg)
  4690. {
  4691. void *argptr;
  4692. int target_size;
  4693. const argtype *arg_type = ie->arg_type;
  4694. const argtype part_arg_type[] = { MK_STRUCT(STRUCT_blkpg_partition) };
  4695. abi_long ret;
  4696. struct blkpg_ioctl_arg *host_blkpg = (void*)buf_temp;
  4697. struct blkpg_partition host_part;
  4698. /* Read and convert blkpg */
  4699. arg_type++;
  4700. target_size = thunk_type_size(arg_type, 0);
  4701. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4702. if (!argptr) {
  4703. ret = -TARGET_EFAULT;
  4704. goto out;
  4705. }
  4706. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4707. unlock_user(argptr, arg, 0);
  4708. switch (host_blkpg->op) {
  4709. case BLKPG_ADD_PARTITION:
  4710. case BLKPG_DEL_PARTITION:
  4711. /* payload is struct blkpg_partition */
  4712. break;
  4713. default:
  4714. /* Unknown opcode */
  4715. ret = -TARGET_EINVAL;
  4716. goto out;
  4717. }
  4718. /* Read and convert blkpg->data */
  4719. arg = (abi_long)(uintptr_t)host_blkpg->data;
  4720. target_size = thunk_type_size(part_arg_type, 0);
  4721. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4722. if (!argptr) {
  4723. ret = -TARGET_EFAULT;
  4724. goto out;
  4725. }
  4726. thunk_convert(&host_part, argptr, part_arg_type, THUNK_HOST);
  4727. unlock_user(argptr, arg, 0);
  4728. /* Swizzle the data pointer to our local copy and call! */
  4729. host_blkpg->data = &host_part;
  4730. ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_blkpg));
  4731. out:
  4732. return ret;
  4733. }
  4734. static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
  4735. int fd, int cmd, abi_long arg)
  4736. {
  4737. const argtype *arg_type = ie->arg_type;
  4738. const StructEntry *se;
  4739. const argtype *field_types;
  4740. const int *dst_offsets, *src_offsets;
  4741. int target_size;
  4742. void *argptr;
  4743. abi_ulong *target_rt_dev_ptr = NULL;
  4744. unsigned long *host_rt_dev_ptr = NULL;
  4745. abi_long ret;
  4746. int i;
  4747. assert(ie->access == IOC_W);
  4748. assert(*arg_type == TYPE_PTR);
  4749. arg_type++;
  4750. assert(*arg_type == TYPE_STRUCT);
  4751. target_size = thunk_type_size(arg_type, 0);
  4752. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4753. if (!argptr) {
  4754. return -TARGET_EFAULT;
  4755. }
  4756. arg_type++;
  4757. assert(*arg_type == (int)STRUCT_rtentry);
  4758. se = struct_entries + *arg_type++;
  4759. assert(se->convert[0] == NULL);
  4760. /* convert struct here to be able to catch rt_dev string */
  4761. field_types = se->field_types;
  4762. dst_offsets = se->field_offsets[THUNK_HOST];
  4763. src_offsets = se->field_offsets[THUNK_TARGET];
  4764. for (i = 0; i < se->nb_fields; i++) {
  4765. if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) {
  4766. assert(*field_types == TYPE_PTRVOID);
  4767. target_rt_dev_ptr = argptr + src_offsets[i];
  4768. host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]);
  4769. if (*target_rt_dev_ptr != 0) {
  4770. *host_rt_dev_ptr = (unsigned long)lock_user_string(
  4771. tswapal(*target_rt_dev_ptr));
  4772. if (!*host_rt_dev_ptr) {
  4773. unlock_user(argptr, arg, 0);
  4774. return -TARGET_EFAULT;
  4775. }
  4776. } else {
  4777. *host_rt_dev_ptr = 0;
  4778. }
  4779. field_types++;
  4780. continue;
  4781. }
  4782. field_types = thunk_convert(buf_temp + dst_offsets[i],
  4783. argptr + src_offsets[i],
  4784. field_types, THUNK_HOST);
  4785. }
  4786. unlock_user(argptr, arg, 0);
  4787. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4788. assert(host_rt_dev_ptr != NULL);
  4789. assert(target_rt_dev_ptr != NULL);
  4790. if (*host_rt_dev_ptr != 0) {
  4791. unlock_user((void *)*host_rt_dev_ptr,
  4792. *target_rt_dev_ptr, 0);
  4793. }
  4794. return ret;
  4795. }
  4796. static abi_long do_ioctl_kdsigaccept(const IOCTLEntry *ie, uint8_t *buf_temp,
  4797. int fd, int cmd, abi_long arg)
  4798. {
  4799. int sig = target_to_host_signal(arg);
  4800. return get_errno(safe_ioctl(fd, ie->host_cmd, sig));
  4801. }
  4802. static abi_long do_ioctl_SIOCGSTAMP(const IOCTLEntry *ie, uint8_t *buf_temp,
  4803. int fd, int cmd, abi_long arg)
  4804. {
  4805. struct timeval tv;
  4806. abi_long ret;
  4807. ret = get_errno(safe_ioctl(fd, SIOCGSTAMP, &tv));
  4808. if (is_error(ret)) {
  4809. return ret;
  4810. }
  4811. if (cmd == (int)TARGET_SIOCGSTAMP_OLD) {
  4812. if (copy_to_user_timeval(arg, &tv)) {
  4813. return -TARGET_EFAULT;
  4814. }
  4815. } else {
  4816. if (copy_to_user_timeval64(arg, &tv)) {
  4817. return -TARGET_EFAULT;
  4818. }
  4819. }
  4820. return ret;
  4821. }
  4822. static abi_long do_ioctl_SIOCGSTAMPNS(const IOCTLEntry *ie, uint8_t *buf_temp,
  4823. int fd, int cmd, abi_long arg)
  4824. {
  4825. struct timespec ts;
  4826. abi_long ret;
  4827. ret = get_errno(safe_ioctl(fd, SIOCGSTAMPNS, &ts));
  4828. if (is_error(ret)) {
  4829. return ret;
  4830. }
  4831. if (cmd == (int)TARGET_SIOCGSTAMPNS_OLD) {
  4832. if (host_to_target_timespec(arg, &ts)) {
  4833. return -TARGET_EFAULT;
  4834. }
  4835. } else{
  4836. if (host_to_target_timespec64(arg, &ts)) {
  4837. return -TARGET_EFAULT;
  4838. }
  4839. }
  4840. return ret;
  4841. }
  4842. #ifdef TIOCGPTPEER
  4843. static abi_long do_ioctl_tiocgptpeer(const IOCTLEntry *ie, uint8_t *buf_temp,
  4844. int fd, int cmd, abi_long arg)
  4845. {
  4846. int flags = target_to_host_bitmask(arg, fcntl_flags_tbl);
  4847. return get_errno(safe_ioctl(fd, ie->host_cmd, flags));
  4848. }
  4849. #endif
  4850. #ifdef HAVE_DRM_H
  4851. static void unlock_drm_version(struct drm_version *host_ver,
  4852. struct target_drm_version *target_ver,
  4853. bool copy)
  4854. {
  4855. unlock_user(host_ver->name, target_ver->name,
  4856. copy ? host_ver->name_len : 0);
  4857. unlock_user(host_ver->date, target_ver->date,
  4858. copy ? host_ver->date_len : 0);
  4859. unlock_user(host_ver->desc, target_ver->desc,
  4860. copy ? host_ver->desc_len : 0);
  4861. }
  4862. static inline abi_long target_to_host_drmversion(struct drm_version *host_ver,
  4863. struct target_drm_version *target_ver)
  4864. {
  4865. memset(host_ver, 0, sizeof(*host_ver));
  4866. __get_user(host_ver->name_len, &target_ver->name_len);
  4867. if (host_ver->name_len) {
  4868. host_ver->name = lock_user(VERIFY_WRITE, target_ver->name,
  4869. target_ver->name_len, 0);
  4870. if (!host_ver->name) {
  4871. return -EFAULT;
  4872. }
  4873. }
  4874. __get_user(host_ver->date_len, &target_ver->date_len);
  4875. if (host_ver->date_len) {
  4876. host_ver->date = lock_user(VERIFY_WRITE, target_ver->date,
  4877. target_ver->date_len, 0);
  4878. if (!host_ver->date) {
  4879. goto err;
  4880. }
  4881. }
  4882. __get_user(host_ver->desc_len, &target_ver->desc_len);
  4883. if (host_ver->desc_len) {
  4884. host_ver->desc = lock_user(VERIFY_WRITE, target_ver->desc,
  4885. target_ver->desc_len, 0);
  4886. if (!host_ver->desc) {
  4887. goto err;
  4888. }
  4889. }
  4890. return 0;
  4891. err:
  4892. unlock_drm_version(host_ver, target_ver, false);
  4893. return -EFAULT;
  4894. }
  4895. static inline void host_to_target_drmversion(
  4896. struct target_drm_version *target_ver,
  4897. struct drm_version *host_ver)
  4898. {
  4899. __put_user(host_ver->version_major, &target_ver->version_major);
  4900. __put_user(host_ver->version_minor, &target_ver->version_minor);
  4901. __put_user(host_ver->version_patchlevel, &target_ver->version_patchlevel);
  4902. __put_user(host_ver->name_len, &target_ver->name_len);
  4903. __put_user(host_ver->date_len, &target_ver->date_len);
  4904. __put_user(host_ver->desc_len, &target_ver->desc_len);
  4905. unlock_drm_version(host_ver, target_ver, true);
  4906. }
  4907. static abi_long do_ioctl_drm(const IOCTLEntry *ie, uint8_t *buf_temp,
  4908. int fd, int cmd, abi_long arg)
  4909. {
  4910. struct drm_version *ver;
  4911. struct target_drm_version *target_ver;
  4912. abi_long ret;
  4913. switch (ie->host_cmd) {
  4914. case DRM_IOCTL_VERSION:
  4915. if (!lock_user_struct(VERIFY_WRITE, target_ver, arg, 0)) {
  4916. return -TARGET_EFAULT;
  4917. }
  4918. ver = (struct drm_version *)buf_temp;
  4919. ret = target_to_host_drmversion(ver, target_ver);
  4920. if (!is_error(ret)) {
  4921. ret = get_errno(safe_ioctl(fd, ie->host_cmd, ver));
  4922. if (is_error(ret)) {
  4923. unlock_drm_version(ver, target_ver, false);
  4924. } else {
  4925. host_to_target_drmversion(target_ver, ver);
  4926. }
  4927. }
  4928. unlock_user_struct(target_ver, arg, 0);
  4929. return ret;
  4930. }
  4931. return -TARGET_ENOSYS;
  4932. }
  4933. static abi_long do_ioctl_drm_i915_getparam(const IOCTLEntry *ie,
  4934. struct drm_i915_getparam *gparam,
  4935. int fd, abi_long arg)
  4936. {
  4937. abi_long ret;
  4938. int value;
  4939. struct target_drm_i915_getparam *target_gparam;
  4940. if (!lock_user_struct(VERIFY_READ, target_gparam, arg, 0)) {
  4941. return -TARGET_EFAULT;
  4942. }
  4943. __get_user(gparam->param, &target_gparam->param);
  4944. gparam->value = &value;
  4945. ret = get_errno(safe_ioctl(fd, ie->host_cmd, gparam));
  4946. put_user_s32(value, target_gparam->value);
  4947. unlock_user_struct(target_gparam, arg, 0);
  4948. return ret;
  4949. }
  4950. static abi_long do_ioctl_drm_i915(const IOCTLEntry *ie, uint8_t *buf_temp,
  4951. int fd, int cmd, abi_long arg)
  4952. {
  4953. switch (ie->host_cmd) {
  4954. case DRM_IOCTL_I915_GETPARAM:
  4955. return do_ioctl_drm_i915_getparam(ie,
  4956. (struct drm_i915_getparam *)buf_temp,
  4957. fd, arg);
  4958. default:
  4959. return -TARGET_ENOSYS;
  4960. }
  4961. }
  4962. #endif
  4963. static abi_long do_ioctl_TUNSETTXFILTER(const IOCTLEntry *ie, uint8_t *buf_temp,
  4964. int fd, int cmd, abi_long arg)
  4965. {
  4966. struct tun_filter *filter = (struct tun_filter *)buf_temp;
  4967. struct tun_filter *target_filter;
  4968. char *target_addr;
  4969. assert(ie->access == IOC_W);
  4970. target_filter = lock_user(VERIFY_READ, arg, sizeof(*target_filter), 1);
  4971. if (!target_filter) {
  4972. return -TARGET_EFAULT;
  4973. }
  4974. filter->flags = tswap16(target_filter->flags);
  4975. filter->count = tswap16(target_filter->count);
  4976. unlock_user(target_filter, arg, 0);
  4977. if (filter->count) {
  4978. if (offsetof(struct tun_filter, addr) + filter->count * ETH_ALEN >
  4979. MAX_STRUCT_SIZE) {
  4980. return -TARGET_EFAULT;
  4981. }
  4982. target_addr = lock_user(VERIFY_READ,
  4983. arg + offsetof(struct tun_filter, addr),
  4984. filter->count * ETH_ALEN, 1);
  4985. if (!target_addr) {
  4986. return -TARGET_EFAULT;
  4987. }
  4988. memcpy(filter->addr, target_addr, filter->count * ETH_ALEN);
  4989. unlock_user(target_addr, arg + offsetof(struct tun_filter, addr), 0);
  4990. }
  4991. return get_errno(safe_ioctl(fd, ie->host_cmd, filter));
  4992. }
  4993. IOCTLEntry ioctl_entries[] = {
  4994. #define IOCTL(cmd, access, ...) \
  4995. { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
  4996. #define IOCTL_SPECIAL(cmd, access, dofn, ...) \
  4997. { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
  4998. #define IOCTL_IGNORE(cmd) \
  4999. { TARGET_ ## cmd, 0, #cmd },
  5000. #include "ioctls.h"
  5001. { 0, 0, },
  5002. };
  5003. /* ??? Implement proper locking for ioctls. */
  5004. /* do_ioctl() Must return target values and target errnos. */
  5005. static abi_long do_ioctl(int fd, int cmd, abi_long arg)
  5006. {
  5007. const IOCTLEntry *ie;
  5008. const argtype *arg_type;
  5009. abi_long ret;
  5010. uint8_t buf_temp[MAX_STRUCT_SIZE];
  5011. int target_size;
  5012. void *argptr;
  5013. ie = ioctl_entries;
  5014. for(;;) {
  5015. if (ie->target_cmd == 0) {
  5016. qemu_log_mask(
  5017. LOG_UNIMP, "Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
  5018. return -TARGET_ENOTTY;
  5019. }
  5020. if (ie->target_cmd == cmd)
  5021. break;
  5022. ie++;
  5023. }
  5024. arg_type = ie->arg_type;
  5025. if (ie->do_ioctl) {
  5026. return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
  5027. } else if (!ie->host_cmd) {
  5028. /* Some architectures define BSD ioctls in their headers
  5029. that are not implemented in Linux. */
  5030. return -TARGET_ENOTTY;
  5031. }
  5032. switch(arg_type[0]) {
  5033. case TYPE_NULL:
  5034. /* no argument */
  5035. ret = get_errno(safe_ioctl(fd, ie->host_cmd));
  5036. break;
  5037. case TYPE_PTRVOID:
  5038. case TYPE_INT:
  5039. case TYPE_LONG:
  5040. case TYPE_ULONG:
  5041. ret = get_errno(safe_ioctl(fd, ie->host_cmd, arg));
  5042. break;
  5043. case TYPE_PTR:
  5044. arg_type++;
  5045. target_size = thunk_type_size(arg_type, 0);
  5046. switch(ie->access) {
  5047. case IOC_R:
  5048. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  5049. if (!is_error(ret)) {
  5050. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  5051. if (!argptr)
  5052. return -TARGET_EFAULT;
  5053. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  5054. unlock_user(argptr, arg, target_size);
  5055. }
  5056. break;
  5057. case IOC_W:
  5058. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  5059. if (!argptr)
  5060. return -TARGET_EFAULT;
  5061. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  5062. unlock_user(argptr, arg, 0);
  5063. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  5064. break;
  5065. default:
  5066. case IOC_RW:
  5067. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  5068. if (!argptr)
  5069. return -TARGET_EFAULT;
  5070. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  5071. unlock_user(argptr, arg, 0);
  5072. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  5073. if (!is_error(ret)) {
  5074. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  5075. if (!argptr)
  5076. return -TARGET_EFAULT;
  5077. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  5078. unlock_user(argptr, arg, target_size);
  5079. }
  5080. break;
  5081. }
  5082. break;
  5083. default:
  5084. qemu_log_mask(LOG_UNIMP,
  5085. "Unsupported ioctl type: cmd=0x%04lx type=%d\n",
  5086. (long)cmd, arg_type[0]);
  5087. ret = -TARGET_ENOTTY;
  5088. break;
  5089. }
  5090. return ret;
  5091. }
  5092. static const bitmask_transtbl iflag_tbl[] = {
  5093. { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
  5094. { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
  5095. { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
  5096. { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
  5097. { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
  5098. { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
  5099. { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
  5100. { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
  5101. { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
  5102. { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
  5103. { TARGET_IXON, TARGET_IXON, IXON, IXON },
  5104. { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
  5105. { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
  5106. { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
  5107. { TARGET_IUTF8, TARGET_IUTF8, IUTF8, IUTF8},
  5108. };
  5109. static const bitmask_transtbl oflag_tbl[] = {
  5110. { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
  5111. { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
  5112. { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
  5113. { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
  5114. { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
  5115. { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
  5116. { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
  5117. { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
  5118. { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
  5119. { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
  5120. { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
  5121. { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
  5122. { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
  5123. { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
  5124. { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
  5125. { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
  5126. { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
  5127. { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
  5128. { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
  5129. { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
  5130. { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
  5131. { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
  5132. { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
  5133. { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
  5134. };
  5135. static const bitmask_transtbl cflag_tbl[] = {
  5136. { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
  5137. { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
  5138. { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
  5139. { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
  5140. { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
  5141. { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
  5142. { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
  5143. { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
  5144. { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
  5145. { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
  5146. { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
  5147. { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
  5148. { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
  5149. { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
  5150. { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
  5151. { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
  5152. { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
  5153. { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
  5154. { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
  5155. { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
  5156. { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
  5157. { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
  5158. { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
  5159. { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
  5160. { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
  5161. { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
  5162. { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
  5163. { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
  5164. { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
  5165. { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
  5166. { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
  5167. };
  5168. static const bitmask_transtbl lflag_tbl[] = {
  5169. { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
  5170. { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
  5171. { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
  5172. { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
  5173. { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
  5174. { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
  5175. { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
  5176. { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
  5177. { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
  5178. { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
  5179. { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
  5180. { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
  5181. { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
  5182. { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
  5183. { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
  5184. { TARGET_EXTPROC, TARGET_EXTPROC, EXTPROC, EXTPROC},
  5185. };
  5186. static void target_to_host_termios (void *dst, const void *src)
  5187. {
  5188. struct host_termios *host = dst;
  5189. const struct target_termios *target = src;
  5190. host->c_iflag =
  5191. target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
  5192. host->c_oflag =
  5193. target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
  5194. host->c_cflag =
  5195. target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
  5196. host->c_lflag =
  5197. target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
  5198. host->c_line = target->c_line;
  5199. memset(host->c_cc, 0, sizeof(host->c_cc));
  5200. host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
  5201. host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
  5202. host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
  5203. host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
  5204. host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
  5205. host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
  5206. host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
  5207. host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
  5208. host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
  5209. host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
  5210. host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
  5211. host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
  5212. host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
  5213. host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
  5214. host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
  5215. host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
  5216. host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
  5217. }
  5218. static void host_to_target_termios (void *dst, const void *src)
  5219. {
  5220. struct target_termios *target = dst;
  5221. const struct host_termios *host = src;
  5222. target->c_iflag =
  5223. tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
  5224. target->c_oflag =
  5225. tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
  5226. target->c_cflag =
  5227. tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
  5228. target->c_lflag =
  5229. tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
  5230. target->c_line = host->c_line;
  5231. memset(target->c_cc, 0, sizeof(target->c_cc));
  5232. target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
  5233. target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
  5234. target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
  5235. target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
  5236. target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
  5237. target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
  5238. target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
  5239. target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
  5240. target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
  5241. target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
  5242. target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
  5243. target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
  5244. target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
  5245. target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
  5246. target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
  5247. target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
  5248. target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
  5249. }
  5250. static const StructEntry struct_termios_def = {
  5251. .convert = { host_to_target_termios, target_to_host_termios },
  5252. .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
  5253. .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
  5254. .print = print_termios,
  5255. };
  5256. /* If the host does not provide these bits, they may be safely discarded. */
  5257. #ifndef MAP_SYNC
  5258. #define MAP_SYNC 0
  5259. #endif
  5260. #ifndef MAP_UNINITIALIZED
  5261. #define MAP_UNINITIALIZED 0
  5262. #endif
  5263. static const bitmask_transtbl mmap_flags_tbl[] = {
  5264. { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
  5265. { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS,
  5266. MAP_ANONYMOUS, MAP_ANONYMOUS },
  5267. { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN,
  5268. MAP_GROWSDOWN, MAP_GROWSDOWN },
  5269. { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE,
  5270. MAP_DENYWRITE, MAP_DENYWRITE },
  5271. { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE,
  5272. MAP_EXECUTABLE, MAP_EXECUTABLE },
  5273. { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
  5274. { TARGET_MAP_NORESERVE, TARGET_MAP_NORESERVE,
  5275. MAP_NORESERVE, MAP_NORESERVE },
  5276. { TARGET_MAP_HUGETLB, TARGET_MAP_HUGETLB, MAP_HUGETLB, MAP_HUGETLB },
  5277. /* MAP_STACK had been ignored by the kernel for quite some time.
  5278. Recognize it for the target insofar as we do not want to pass
  5279. it through to the host. */
  5280. { TARGET_MAP_STACK, TARGET_MAP_STACK, 0, 0 },
  5281. { TARGET_MAP_NONBLOCK, TARGET_MAP_NONBLOCK, MAP_NONBLOCK, MAP_NONBLOCK },
  5282. { TARGET_MAP_POPULATE, TARGET_MAP_POPULATE, MAP_POPULATE, MAP_POPULATE },
  5283. { TARGET_MAP_FIXED_NOREPLACE, TARGET_MAP_FIXED_NOREPLACE,
  5284. MAP_FIXED_NOREPLACE, MAP_FIXED_NOREPLACE },
  5285. { TARGET_MAP_UNINITIALIZED, TARGET_MAP_UNINITIALIZED,
  5286. MAP_UNINITIALIZED, MAP_UNINITIALIZED },
  5287. };
  5288. /*
  5289. * Arrange for legacy / undefined architecture specific flags to be
  5290. * ignored by mmap handling code.
  5291. */
  5292. #ifndef TARGET_MAP_32BIT
  5293. #define TARGET_MAP_32BIT 0
  5294. #endif
  5295. #ifndef TARGET_MAP_HUGE_2MB
  5296. #define TARGET_MAP_HUGE_2MB 0
  5297. #endif
  5298. #ifndef TARGET_MAP_HUGE_1GB
  5299. #define TARGET_MAP_HUGE_1GB 0
  5300. #endif
  5301. static abi_long do_mmap(abi_ulong addr, abi_ulong len, int prot,
  5302. int target_flags, int fd, off_t offset)
  5303. {
  5304. /*
  5305. * The historical set of flags that all mmap types implicitly support.
  5306. */
  5307. enum {
  5308. TARGET_LEGACY_MAP_MASK = TARGET_MAP_SHARED
  5309. | TARGET_MAP_PRIVATE
  5310. | TARGET_MAP_FIXED
  5311. | TARGET_MAP_ANONYMOUS
  5312. | TARGET_MAP_DENYWRITE
  5313. | TARGET_MAP_EXECUTABLE
  5314. | TARGET_MAP_UNINITIALIZED
  5315. | TARGET_MAP_GROWSDOWN
  5316. | TARGET_MAP_LOCKED
  5317. | TARGET_MAP_NORESERVE
  5318. | TARGET_MAP_POPULATE
  5319. | TARGET_MAP_NONBLOCK
  5320. | TARGET_MAP_STACK
  5321. | TARGET_MAP_HUGETLB
  5322. | TARGET_MAP_32BIT
  5323. | TARGET_MAP_HUGE_2MB
  5324. | TARGET_MAP_HUGE_1GB
  5325. };
  5326. int host_flags;
  5327. switch (target_flags & TARGET_MAP_TYPE) {
  5328. case TARGET_MAP_PRIVATE:
  5329. host_flags = MAP_PRIVATE;
  5330. break;
  5331. case TARGET_MAP_SHARED:
  5332. host_flags = MAP_SHARED;
  5333. break;
  5334. case TARGET_MAP_SHARED_VALIDATE:
  5335. /*
  5336. * MAP_SYNC is only supported for MAP_SHARED_VALIDATE, and is
  5337. * therefore omitted from mmap_flags_tbl and TARGET_LEGACY_MAP_MASK.
  5338. */
  5339. if (target_flags & ~(TARGET_LEGACY_MAP_MASK | TARGET_MAP_SYNC)) {
  5340. return -TARGET_EOPNOTSUPP;
  5341. }
  5342. host_flags = MAP_SHARED_VALIDATE;
  5343. if (target_flags & TARGET_MAP_SYNC) {
  5344. host_flags |= MAP_SYNC;
  5345. }
  5346. break;
  5347. default:
  5348. return -TARGET_EINVAL;
  5349. }
  5350. host_flags |= target_to_host_bitmask(target_flags, mmap_flags_tbl);
  5351. return get_errno(target_mmap(addr, len, prot, host_flags, fd, offset));
  5352. }
  5353. /*
  5354. * NOTE: TARGET_ABI32 is defined for TARGET_I386 (but not for TARGET_X86_64)
  5355. * TARGET_I386 is defined if TARGET_X86_64 is defined
  5356. */
  5357. #if defined(TARGET_I386)
  5358. /* NOTE: there is really one LDT for all the threads */
  5359. static uint8_t *ldt_table;
  5360. static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
  5361. {
  5362. int size;
  5363. void *p;
  5364. if (!ldt_table)
  5365. return 0;
  5366. size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
  5367. if (size > bytecount)
  5368. size = bytecount;
  5369. p = lock_user(VERIFY_WRITE, ptr, size, 0);
  5370. if (!p)
  5371. return -TARGET_EFAULT;
  5372. /* ??? Should this by byteswapped? */
  5373. memcpy(p, ldt_table, size);
  5374. unlock_user(p, ptr, size);
  5375. return size;
  5376. }
  5377. /* XXX: add locking support */
  5378. static abi_long write_ldt(CPUX86State *env,
  5379. abi_ulong ptr, unsigned long bytecount, int oldmode)
  5380. {
  5381. struct target_modify_ldt_ldt_s ldt_info;
  5382. struct target_modify_ldt_ldt_s *target_ldt_info;
  5383. int seg_32bit, contents, read_exec_only, limit_in_pages;
  5384. int seg_not_present, useable, lm;
  5385. uint32_t *lp, entry_1, entry_2;
  5386. if (bytecount != sizeof(ldt_info))
  5387. return -TARGET_EINVAL;
  5388. if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
  5389. return -TARGET_EFAULT;
  5390. ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
  5391. ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
  5392. ldt_info.limit = tswap32(target_ldt_info->limit);
  5393. ldt_info.flags = tswap32(target_ldt_info->flags);
  5394. unlock_user_struct(target_ldt_info, ptr, 0);
  5395. if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
  5396. return -TARGET_EINVAL;
  5397. seg_32bit = ldt_info.flags & 1;
  5398. contents = (ldt_info.flags >> 1) & 3;
  5399. read_exec_only = (ldt_info.flags >> 3) & 1;
  5400. limit_in_pages = (ldt_info.flags >> 4) & 1;
  5401. seg_not_present = (ldt_info.flags >> 5) & 1;
  5402. useable = (ldt_info.flags >> 6) & 1;
  5403. #ifdef TARGET_ABI32
  5404. lm = 0;
  5405. #else
  5406. lm = (ldt_info.flags >> 7) & 1;
  5407. #endif
  5408. if (contents == 3) {
  5409. if (oldmode)
  5410. return -TARGET_EINVAL;
  5411. if (seg_not_present == 0)
  5412. return -TARGET_EINVAL;
  5413. }
  5414. /* allocate the LDT */
  5415. if (!ldt_table) {
  5416. env->ldt.base = target_mmap(0,
  5417. TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
  5418. PROT_READ|PROT_WRITE,
  5419. MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
  5420. if (env->ldt.base == -1)
  5421. return -TARGET_ENOMEM;
  5422. memset(g2h_untagged(env->ldt.base), 0,
  5423. TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
  5424. env->ldt.limit = 0xffff;
  5425. ldt_table = g2h_untagged(env->ldt.base);
  5426. }
  5427. /* NOTE: same code as Linux kernel */
  5428. /* Allow LDTs to be cleared by the user. */
  5429. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  5430. if (oldmode ||
  5431. (contents == 0 &&
  5432. read_exec_only == 1 &&
  5433. seg_32bit == 0 &&
  5434. limit_in_pages == 0 &&
  5435. seg_not_present == 1 &&
  5436. useable == 0 )) {
  5437. entry_1 = 0;
  5438. entry_2 = 0;
  5439. goto install;
  5440. }
  5441. }
  5442. entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
  5443. (ldt_info.limit & 0x0ffff);
  5444. entry_2 = (ldt_info.base_addr & 0xff000000) |
  5445. ((ldt_info.base_addr & 0x00ff0000) >> 16) |
  5446. (ldt_info.limit & 0xf0000) |
  5447. ((read_exec_only ^ 1) << 9) |
  5448. (contents << 10) |
  5449. ((seg_not_present ^ 1) << 15) |
  5450. (seg_32bit << 22) |
  5451. (limit_in_pages << 23) |
  5452. (lm << 21) |
  5453. 0x7000;
  5454. if (!oldmode)
  5455. entry_2 |= (useable << 20);
  5456. /* Install the new entry ... */
  5457. install:
  5458. lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
  5459. lp[0] = tswap32(entry_1);
  5460. lp[1] = tswap32(entry_2);
  5461. return 0;
  5462. }
  5463. /* specific and weird i386 syscalls */
  5464. static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
  5465. unsigned long bytecount)
  5466. {
  5467. abi_long ret;
  5468. switch (func) {
  5469. case 0:
  5470. ret = read_ldt(ptr, bytecount);
  5471. break;
  5472. case 1:
  5473. ret = write_ldt(env, ptr, bytecount, 1);
  5474. break;
  5475. case 0x11:
  5476. ret = write_ldt(env, ptr, bytecount, 0);
  5477. break;
  5478. default:
  5479. ret = -TARGET_ENOSYS;
  5480. break;
  5481. }
  5482. return ret;
  5483. }
  5484. #if defined(TARGET_ABI32)
  5485. abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
  5486. {
  5487. uint64_t *gdt_table = g2h_untagged(env->gdt.base);
  5488. struct target_modify_ldt_ldt_s ldt_info;
  5489. struct target_modify_ldt_ldt_s *target_ldt_info;
  5490. int seg_32bit, contents, read_exec_only, limit_in_pages;
  5491. int seg_not_present, useable, lm;
  5492. uint32_t *lp, entry_1, entry_2;
  5493. int i;
  5494. lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
  5495. if (!target_ldt_info)
  5496. return -TARGET_EFAULT;
  5497. ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
  5498. ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
  5499. ldt_info.limit = tswap32(target_ldt_info->limit);
  5500. ldt_info.flags = tswap32(target_ldt_info->flags);
  5501. if (ldt_info.entry_number == -1) {
  5502. for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
  5503. if (gdt_table[i] == 0) {
  5504. ldt_info.entry_number = i;
  5505. target_ldt_info->entry_number = tswap32(i);
  5506. break;
  5507. }
  5508. }
  5509. }
  5510. unlock_user_struct(target_ldt_info, ptr, 1);
  5511. if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
  5512. ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
  5513. return -TARGET_EINVAL;
  5514. seg_32bit = ldt_info.flags & 1;
  5515. contents = (ldt_info.flags >> 1) & 3;
  5516. read_exec_only = (ldt_info.flags >> 3) & 1;
  5517. limit_in_pages = (ldt_info.flags >> 4) & 1;
  5518. seg_not_present = (ldt_info.flags >> 5) & 1;
  5519. useable = (ldt_info.flags >> 6) & 1;
  5520. #ifdef TARGET_ABI32
  5521. lm = 0;
  5522. #else
  5523. lm = (ldt_info.flags >> 7) & 1;
  5524. #endif
  5525. if (contents == 3) {
  5526. if (seg_not_present == 0)
  5527. return -TARGET_EINVAL;
  5528. }
  5529. /* NOTE: same code as Linux kernel */
  5530. /* Allow LDTs to be cleared by the user. */
  5531. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  5532. if ((contents == 0 &&
  5533. read_exec_only == 1 &&
  5534. seg_32bit == 0 &&
  5535. limit_in_pages == 0 &&
  5536. seg_not_present == 1 &&
  5537. useable == 0 )) {
  5538. entry_1 = 0;
  5539. entry_2 = 0;
  5540. goto install;
  5541. }
  5542. }
  5543. entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
  5544. (ldt_info.limit & 0x0ffff);
  5545. entry_2 = (ldt_info.base_addr & 0xff000000) |
  5546. ((ldt_info.base_addr & 0x00ff0000) >> 16) |
  5547. (ldt_info.limit & 0xf0000) |
  5548. ((read_exec_only ^ 1) << 9) |
  5549. (contents << 10) |
  5550. ((seg_not_present ^ 1) << 15) |
  5551. (seg_32bit << 22) |
  5552. (limit_in_pages << 23) |
  5553. (useable << 20) |
  5554. (lm << 21) |
  5555. 0x7000;
  5556. /* Install the new entry ... */
  5557. install:
  5558. lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
  5559. lp[0] = tswap32(entry_1);
  5560. lp[1] = tswap32(entry_2);
  5561. return 0;
  5562. }
  5563. static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
  5564. {
  5565. struct target_modify_ldt_ldt_s *target_ldt_info;
  5566. uint64_t *gdt_table = g2h_untagged(env->gdt.base);
  5567. uint32_t base_addr, limit, flags;
  5568. int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
  5569. int seg_not_present, useable, lm;
  5570. uint32_t *lp, entry_1, entry_2;
  5571. lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
  5572. if (!target_ldt_info)
  5573. return -TARGET_EFAULT;
  5574. idx = tswap32(target_ldt_info->entry_number);
  5575. if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
  5576. idx > TARGET_GDT_ENTRY_TLS_MAX) {
  5577. unlock_user_struct(target_ldt_info, ptr, 1);
  5578. return -TARGET_EINVAL;
  5579. }
  5580. lp = (uint32_t *)(gdt_table + idx);
  5581. entry_1 = tswap32(lp[0]);
  5582. entry_2 = tswap32(lp[1]);
  5583. read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
  5584. contents = (entry_2 >> 10) & 3;
  5585. seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
  5586. seg_32bit = (entry_2 >> 22) & 1;
  5587. limit_in_pages = (entry_2 >> 23) & 1;
  5588. useable = (entry_2 >> 20) & 1;
  5589. #ifdef TARGET_ABI32
  5590. lm = 0;
  5591. #else
  5592. lm = (entry_2 >> 21) & 1;
  5593. #endif
  5594. flags = (seg_32bit << 0) | (contents << 1) |
  5595. (read_exec_only << 3) | (limit_in_pages << 4) |
  5596. (seg_not_present << 5) | (useable << 6) | (lm << 7);
  5597. limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
  5598. base_addr = (entry_1 >> 16) |
  5599. (entry_2 & 0xff000000) |
  5600. ((entry_2 & 0xff) << 16);
  5601. target_ldt_info->base_addr = tswapal(base_addr);
  5602. target_ldt_info->limit = tswap32(limit);
  5603. target_ldt_info->flags = tswap32(flags);
  5604. unlock_user_struct(target_ldt_info, ptr, 1);
  5605. return 0;
  5606. }
  5607. abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
  5608. {
  5609. return -TARGET_ENOSYS;
  5610. }
  5611. #else
  5612. abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
  5613. {
  5614. abi_long ret = 0;
  5615. abi_ulong val;
  5616. int idx;
  5617. switch(code) {
  5618. case TARGET_ARCH_SET_GS:
  5619. case TARGET_ARCH_SET_FS:
  5620. if (code == TARGET_ARCH_SET_GS)
  5621. idx = R_GS;
  5622. else
  5623. idx = R_FS;
  5624. cpu_x86_load_seg(env, idx, 0);
  5625. env->segs[idx].base = addr;
  5626. break;
  5627. case TARGET_ARCH_GET_GS:
  5628. case TARGET_ARCH_GET_FS:
  5629. if (code == TARGET_ARCH_GET_GS)
  5630. idx = R_GS;
  5631. else
  5632. idx = R_FS;
  5633. val = env->segs[idx].base;
  5634. if (put_user(val, addr, abi_ulong))
  5635. ret = -TARGET_EFAULT;
  5636. break;
  5637. default:
  5638. ret = -TARGET_EINVAL;
  5639. break;
  5640. }
  5641. return ret;
  5642. }
  5643. #endif /* defined(TARGET_ABI32 */
  5644. #endif /* defined(TARGET_I386) */
  5645. /*
  5646. * These constants are generic. Supply any that are missing from the host.
  5647. */
  5648. #ifndef PR_SET_NAME
  5649. # define PR_SET_NAME 15
  5650. # define PR_GET_NAME 16
  5651. #endif
  5652. #ifndef PR_SET_FP_MODE
  5653. # define PR_SET_FP_MODE 45
  5654. # define PR_GET_FP_MODE 46
  5655. # define PR_FP_MODE_FR (1 << 0)
  5656. # define PR_FP_MODE_FRE (1 << 1)
  5657. #endif
  5658. #ifndef PR_SVE_SET_VL
  5659. # define PR_SVE_SET_VL 50
  5660. # define PR_SVE_GET_VL 51
  5661. # define PR_SVE_VL_LEN_MASK 0xffff
  5662. # define PR_SVE_VL_INHERIT (1 << 17)
  5663. #endif
  5664. #ifndef PR_PAC_RESET_KEYS
  5665. # define PR_PAC_RESET_KEYS 54
  5666. # define PR_PAC_APIAKEY (1 << 0)
  5667. # define PR_PAC_APIBKEY (1 << 1)
  5668. # define PR_PAC_APDAKEY (1 << 2)
  5669. # define PR_PAC_APDBKEY (1 << 3)
  5670. # define PR_PAC_APGAKEY (1 << 4)
  5671. #endif
  5672. #ifndef PR_SET_TAGGED_ADDR_CTRL
  5673. # define PR_SET_TAGGED_ADDR_CTRL 55
  5674. # define PR_GET_TAGGED_ADDR_CTRL 56
  5675. # define PR_TAGGED_ADDR_ENABLE (1UL << 0)
  5676. #endif
  5677. #ifndef PR_SET_IO_FLUSHER
  5678. # define PR_SET_IO_FLUSHER 57
  5679. # define PR_GET_IO_FLUSHER 58
  5680. #endif
  5681. #ifndef PR_SET_SYSCALL_USER_DISPATCH
  5682. # define PR_SET_SYSCALL_USER_DISPATCH 59
  5683. #endif
  5684. #ifndef PR_SME_SET_VL
  5685. # define PR_SME_SET_VL 63
  5686. # define PR_SME_GET_VL 64
  5687. # define PR_SME_VL_LEN_MASK 0xffff
  5688. # define PR_SME_VL_INHERIT (1 << 17)
  5689. #endif
  5690. #include "target_prctl.h"
  5691. static abi_long do_prctl_inval0(CPUArchState *env)
  5692. {
  5693. return -TARGET_EINVAL;
  5694. }
  5695. static abi_long do_prctl_inval1(CPUArchState *env, abi_long arg2)
  5696. {
  5697. return -TARGET_EINVAL;
  5698. }
  5699. #ifndef do_prctl_get_fp_mode
  5700. #define do_prctl_get_fp_mode do_prctl_inval0
  5701. #endif
  5702. #ifndef do_prctl_set_fp_mode
  5703. #define do_prctl_set_fp_mode do_prctl_inval1
  5704. #endif
  5705. #ifndef do_prctl_sve_get_vl
  5706. #define do_prctl_sve_get_vl do_prctl_inval0
  5707. #endif
  5708. #ifndef do_prctl_sve_set_vl
  5709. #define do_prctl_sve_set_vl do_prctl_inval1
  5710. #endif
  5711. #ifndef do_prctl_reset_keys
  5712. #define do_prctl_reset_keys do_prctl_inval1
  5713. #endif
  5714. #ifndef do_prctl_set_tagged_addr_ctrl
  5715. #define do_prctl_set_tagged_addr_ctrl do_prctl_inval1
  5716. #endif
  5717. #ifndef do_prctl_get_tagged_addr_ctrl
  5718. #define do_prctl_get_tagged_addr_ctrl do_prctl_inval0
  5719. #endif
  5720. #ifndef do_prctl_get_unalign
  5721. #define do_prctl_get_unalign do_prctl_inval1
  5722. #endif
  5723. #ifndef do_prctl_set_unalign
  5724. #define do_prctl_set_unalign do_prctl_inval1
  5725. #endif
  5726. #ifndef do_prctl_sme_get_vl
  5727. #define do_prctl_sme_get_vl do_prctl_inval0
  5728. #endif
  5729. #ifndef do_prctl_sme_set_vl
  5730. #define do_prctl_sme_set_vl do_prctl_inval1
  5731. #endif
  5732. static abi_long do_prctl(CPUArchState *env, abi_long option, abi_long arg2,
  5733. abi_long arg3, abi_long arg4, abi_long arg5)
  5734. {
  5735. abi_long ret;
  5736. switch (option) {
  5737. case PR_GET_PDEATHSIG:
  5738. {
  5739. int deathsig;
  5740. ret = get_errno(prctl(PR_GET_PDEATHSIG, &deathsig,
  5741. arg3, arg4, arg5));
  5742. if (!is_error(ret) &&
  5743. put_user_s32(host_to_target_signal(deathsig), arg2)) {
  5744. return -TARGET_EFAULT;
  5745. }
  5746. return ret;
  5747. }
  5748. case PR_SET_PDEATHSIG:
  5749. return get_errno(prctl(PR_SET_PDEATHSIG, target_to_host_signal(arg2),
  5750. arg3, arg4, arg5));
  5751. case PR_GET_NAME:
  5752. {
  5753. void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
  5754. if (!name) {
  5755. return -TARGET_EFAULT;
  5756. }
  5757. ret = get_errno(prctl(PR_GET_NAME, (uintptr_t)name,
  5758. arg3, arg4, arg5));
  5759. unlock_user(name, arg2, 16);
  5760. return ret;
  5761. }
  5762. case PR_SET_NAME:
  5763. {
  5764. void *name = lock_user(VERIFY_READ, arg2, 16, 1);
  5765. if (!name) {
  5766. return -TARGET_EFAULT;
  5767. }
  5768. ret = get_errno(prctl(PR_SET_NAME, (uintptr_t)name,
  5769. arg3, arg4, arg5));
  5770. unlock_user(name, arg2, 0);
  5771. return ret;
  5772. }
  5773. case PR_GET_FP_MODE:
  5774. return do_prctl_get_fp_mode(env);
  5775. case PR_SET_FP_MODE:
  5776. return do_prctl_set_fp_mode(env, arg2);
  5777. case PR_SVE_GET_VL:
  5778. return do_prctl_sve_get_vl(env);
  5779. case PR_SVE_SET_VL:
  5780. return do_prctl_sve_set_vl(env, arg2);
  5781. case PR_SME_GET_VL:
  5782. return do_prctl_sme_get_vl(env);
  5783. case PR_SME_SET_VL:
  5784. return do_prctl_sme_set_vl(env, arg2);
  5785. case PR_PAC_RESET_KEYS:
  5786. if (arg3 || arg4 || arg5) {
  5787. return -TARGET_EINVAL;
  5788. }
  5789. return do_prctl_reset_keys(env, arg2);
  5790. case PR_SET_TAGGED_ADDR_CTRL:
  5791. if (arg3 || arg4 || arg5) {
  5792. return -TARGET_EINVAL;
  5793. }
  5794. return do_prctl_set_tagged_addr_ctrl(env, arg2);
  5795. case PR_GET_TAGGED_ADDR_CTRL:
  5796. if (arg2 || arg3 || arg4 || arg5) {
  5797. return -TARGET_EINVAL;
  5798. }
  5799. return do_prctl_get_tagged_addr_ctrl(env);
  5800. case PR_GET_UNALIGN:
  5801. return do_prctl_get_unalign(env, arg2);
  5802. case PR_SET_UNALIGN:
  5803. return do_prctl_set_unalign(env, arg2);
  5804. case PR_CAP_AMBIENT:
  5805. case PR_CAPBSET_READ:
  5806. case PR_CAPBSET_DROP:
  5807. case PR_GET_DUMPABLE:
  5808. case PR_SET_DUMPABLE:
  5809. case PR_GET_KEEPCAPS:
  5810. case PR_SET_KEEPCAPS:
  5811. case PR_GET_SECUREBITS:
  5812. case PR_SET_SECUREBITS:
  5813. case PR_GET_TIMING:
  5814. case PR_SET_TIMING:
  5815. case PR_GET_TIMERSLACK:
  5816. case PR_SET_TIMERSLACK:
  5817. case PR_MCE_KILL:
  5818. case PR_MCE_KILL_GET:
  5819. case PR_GET_NO_NEW_PRIVS:
  5820. case PR_SET_NO_NEW_PRIVS:
  5821. case PR_GET_IO_FLUSHER:
  5822. case PR_SET_IO_FLUSHER:
  5823. case PR_SET_CHILD_SUBREAPER:
  5824. case PR_GET_SPECULATION_CTRL:
  5825. case PR_SET_SPECULATION_CTRL:
  5826. /* Some prctl options have no pointer arguments and we can pass on. */
  5827. return get_errno(prctl(option, arg2, arg3, arg4, arg5));
  5828. case PR_GET_CHILD_SUBREAPER:
  5829. {
  5830. int val;
  5831. ret = get_errno(prctl(PR_GET_CHILD_SUBREAPER, &val,
  5832. arg3, arg4, arg5));
  5833. if (!is_error(ret) && put_user_s32(val, arg2)) {
  5834. return -TARGET_EFAULT;
  5835. }
  5836. return ret;
  5837. }
  5838. case PR_GET_TID_ADDRESS:
  5839. {
  5840. TaskState *ts = get_task_state(env_cpu(env));
  5841. return put_user_ual(ts->child_tidptr, arg2);
  5842. }
  5843. case PR_GET_FPEXC:
  5844. case PR_SET_FPEXC:
  5845. /* Was used for SPE on PowerPC. */
  5846. return -TARGET_EINVAL;
  5847. case PR_GET_ENDIAN:
  5848. case PR_SET_ENDIAN:
  5849. case PR_GET_FPEMU:
  5850. case PR_SET_FPEMU:
  5851. case PR_SET_MM:
  5852. case PR_GET_SECCOMP:
  5853. case PR_SET_SECCOMP:
  5854. case PR_SET_SYSCALL_USER_DISPATCH:
  5855. case PR_GET_THP_DISABLE:
  5856. case PR_SET_THP_DISABLE:
  5857. case PR_GET_TSC:
  5858. case PR_SET_TSC:
  5859. /* Disable to prevent the target disabling stuff we need. */
  5860. return -TARGET_EINVAL;
  5861. default:
  5862. qemu_log_mask(LOG_UNIMP, "Unsupported prctl: " TARGET_ABI_FMT_ld "\n",
  5863. option);
  5864. return -TARGET_EINVAL;
  5865. }
  5866. }
  5867. #define NEW_STACK_SIZE 0x40000
  5868. static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
  5869. typedef struct {
  5870. CPUArchState *env;
  5871. pthread_mutex_t mutex;
  5872. pthread_cond_t cond;
  5873. pthread_t thread;
  5874. uint32_t tid;
  5875. abi_ulong child_tidptr;
  5876. abi_ulong parent_tidptr;
  5877. sigset_t sigmask;
  5878. } new_thread_info;
  5879. static void *clone_func(void *arg)
  5880. {
  5881. new_thread_info *info = arg;
  5882. CPUArchState *env;
  5883. CPUState *cpu;
  5884. TaskState *ts;
  5885. rcu_register_thread();
  5886. tcg_register_thread();
  5887. env = info->env;
  5888. cpu = env_cpu(env);
  5889. thread_cpu = cpu;
  5890. ts = get_task_state(cpu);
  5891. info->tid = sys_gettid();
  5892. task_settid(ts);
  5893. if (info->child_tidptr)
  5894. put_user_u32(info->tid, info->child_tidptr);
  5895. if (info->parent_tidptr)
  5896. put_user_u32(info->tid, info->parent_tidptr);
  5897. qemu_guest_random_seed_thread_part2(cpu->random_seed);
  5898. /* Enable signals. */
  5899. sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
  5900. /* Signal to the parent that we're ready. */
  5901. pthread_mutex_lock(&info->mutex);
  5902. pthread_cond_broadcast(&info->cond);
  5903. pthread_mutex_unlock(&info->mutex);
  5904. /* Wait until the parent has finished initializing the tls state. */
  5905. pthread_mutex_lock(&clone_lock);
  5906. pthread_mutex_unlock(&clone_lock);
  5907. cpu_loop(env);
  5908. /* never exits */
  5909. return NULL;
  5910. }
  5911. /* do_fork() Must return host values and target errnos (unlike most
  5912. do_*() functions). */
  5913. static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
  5914. abi_ulong parent_tidptr, target_ulong newtls,
  5915. abi_ulong child_tidptr)
  5916. {
  5917. CPUState *cpu = env_cpu(env);
  5918. int ret;
  5919. TaskState *ts;
  5920. CPUState *new_cpu;
  5921. CPUArchState *new_env;
  5922. sigset_t sigmask;
  5923. flags &= ~CLONE_IGNORED_FLAGS;
  5924. /* Emulate vfork() with fork() */
  5925. if (flags & CLONE_VFORK)
  5926. flags &= ~(CLONE_VFORK | CLONE_VM);
  5927. if (flags & CLONE_VM) {
  5928. TaskState *parent_ts = get_task_state(cpu);
  5929. new_thread_info info;
  5930. pthread_attr_t attr;
  5931. if (((flags & CLONE_THREAD_FLAGS) != CLONE_THREAD_FLAGS) ||
  5932. (flags & CLONE_INVALID_THREAD_FLAGS)) {
  5933. return -TARGET_EINVAL;
  5934. }
  5935. ts = g_new0(TaskState, 1);
  5936. init_task_state(ts);
  5937. /* Grab a mutex so that thread setup appears atomic. */
  5938. pthread_mutex_lock(&clone_lock);
  5939. /*
  5940. * If this is our first additional thread, we need to ensure we
  5941. * generate code for parallel execution and flush old translations.
  5942. * Do this now so that the copy gets CF_PARALLEL too.
  5943. */
  5944. if (!tcg_cflags_has(cpu, CF_PARALLEL)) {
  5945. tcg_cflags_set(cpu, CF_PARALLEL);
  5946. tb_flush(cpu);
  5947. }
  5948. /* we create a new CPU instance. */
  5949. new_env = cpu_copy(env);
  5950. /* Init regs that differ from the parent. */
  5951. cpu_clone_regs_child(new_env, newsp, flags);
  5952. cpu_clone_regs_parent(env, flags);
  5953. new_cpu = env_cpu(new_env);
  5954. new_cpu->opaque = ts;
  5955. ts->bprm = parent_ts->bprm;
  5956. ts->info = parent_ts->info;
  5957. ts->signal_mask = parent_ts->signal_mask;
  5958. if (flags & CLONE_CHILD_CLEARTID) {
  5959. ts->child_tidptr = child_tidptr;
  5960. }
  5961. if (flags & CLONE_SETTLS) {
  5962. cpu_set_tls (new_env, newtls);
  5963. }
  5964. memset(&info, 0, sizeof(info));
  5965. pthread_mutex_init(&info.mutex, NULL);
  5966. pthread_mutex_lock(&info.mutex);
  5967. pthread_cond_init(&info.cond, NULL);
  5968. info.env = new_env;
  5969. if (flags & CLONE_CHILD_SETTID) {
  5970. info.child_tidptr = child_tidptr;
  5971. }
  5972. if (flags & CLONE_PARENT_SETTID) {
  5973. info.parent_tidptr = parent_tidptr;
  5974. }
  5975. ret = pthread_attr_init(&attr);
  5976. ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
  5977. ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  5978. /* It is not safe to deliver signals until the child has finished
  5979. initializing, so temporarily block all signals. */
  5980. sigfillset(&sigmask);
  5981. sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
  5982. cpu->random_seed = qemu_guest_random_seed_thread_part1();
  5983. ret = pthread_create(&info.thread, &attr, clone_func, &info);
  5984. /* TODO: Free new CPU state if thread creation failed. */
  5985. sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
  5986. pthread_attr_destroy(&attr);
  5987. if (ret == 0) {
  5988. /* Wait for the child to initialize. */
  5989. pthread_cond_wait(&info.cond, &info.mutex);
  5990. ret = info.tid;
  5991. } else {
  5992. ret = -1;
  5993. }
  5994. pthread_mutex_unlock(&info.mutex);
  5995. pthread_cond_destroy(&info.cond);
  5996. pthread_mutex_destroy(&info.mutex);
  5997. pthread_mutex_unlock(&clone_lock);
  5998. } else {
  5999. /* if no CLONE_VM, we consider it is a fork */
  6000. if (flags & CLONE_INVALID_FORK_FLAGS) {
  6001. return -TARGET_EINVAL;
  6002. }
  6003. /* We can't support custom termination signals */
  6004. if ((flags & CSIGNAL) != TARGET_SIGCHLD) {
  6005. return -TARGET_EINVAL;
  6006. }
  6007. #if !defined(__NR_pidfd_open) || !defined(TARGET_NR_pidfd_open)
  6008. if (flags & CLONE_PIDFD) {
  6009. return -TARGET_EINVAL;
  6010. }
  6011. #endif
  6012. /* Can not allow CLONE_PIDFD with CLONE_PARENT_SETTID */
  6013. if ((flags & CLONE_PIDFD) && (flags & CLONE_PARENT_SETTID)) {
  6014. return -TARGET_EINVAL;
  6015. }
  6016. if (block_signals()) {
  6017. return -QEMU_ERESTARTSYS;
  6018. }
  6019. fork_start();
  6020. ret = fork();
  6021. if (ret == 0) {
  6022. /* Child Process. */
  6023. cpu_clone_regs_child(env, newsp, flags);
  6024. fork_end(ret);
  6025. /* There is a race condition here. The parent process could
  6026. theoretically read the TID in the child process before the child
  6027. tid is set. This would require using either ptrace
  6028. (not implemented) or having *_tidptr to point at a shared memory
  6029. mapping. We can't repeat the spinlock hack used above because
  6030. the child process gets its own copy of the lock. */
  6031. if (flags & CLONE_CHILD_SETTID)
  6032. put_user_u32(sys_gettid(), child_tidptr);
  6033. if (flags & CLONE_PARENT_SETTID)
  6034. put_user_u32(sys_gettid(), parent_tidptr);
  6035. ts = get_task_state(cpu);
  6036. if (flags & CLONE_SETTLS)
  6037. cpu_set_tls (env, newtls);
  6038. if (flags & CLONE_CHILD_CLEARTID)
  6039. ts->child_tidptr = child_tidptr;
  6040. } else {
  6041. cpu_clone_regs_parent(env, flags);
  6042. if (flags & CLONE_PIDFD) {
  6043. int pid_fd = 0;
  6044. #if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
  6045. int pid_child = ret;
  6046. pid_fd = pidfd_open(pid_child, 0);
  6047. if (pid_fd >= 0) {
  6048. fcntl(pid_fd, F_SETFD, fcntl(pid_fd, F_GETFL)
  6049. | FD_CLOEXEC);
  6050. } else {
  6051. pid_fd = 0;
  6052. }
  6053. #endif
  6054. put_user_u32(pid_fd, parent_tidptr);
  6055. }
  6056. fork_end(ret);
  6057. }
  6058. g_assert(!cpu_in_exclusive_context(cpu));
  6059. }
  6060. return ret;
  6061. }
  6062. /* warning : doesn't handle linux specific flags... */
  6063. static int target_to_host_fcntl_cmd(int cmd)
  6064. {
  6065. int ret;
  6066. switch(cmd) {
  6067. case TARGET_F_DUPFD:
  6068. case TARGET_F_GETFD:
  6069. case TARGET_F_SETFD:
  6070. case TARGET_F_GETFL:
  6071. case TARGET_F_SETFL:
  6072. case TARGET_F_OFD_GETLK:
  6073. case TARGET_F_OFD_SETLK:
  6074. case TARGET_F_OFD_SETLKW:
  6075. ret = cmd;
  6076. break;
  6077. case TARGET_F_GETLK:
  6078. ret = F_GETLK64;
  6079. break;
  6080. case TARGET_F_SETLK:
  6081. ret = F_SETLK64;
  6082. break;
  6083. case TARGET_F_SETLKW:
  6084. ret = F_SETLKW64;
  6085. break;
  6086. case TARGET_F_GETOWN:
  6087. ret = F_GETOWN;
  6088. break;
  6089. case TARGET_F_SETOWN:
  6090. ret = F_SETOWN;
  6091. break;
  6092. case TARGET_F_GETSIG:
  6093. ret = F_GETSIG;
  6094. break;
  6095. case TARGET_F_SETSIG:
  6096. ret = F_SETSIG;
  6097. break;
  6098. #if TARGET_ABI_BITS == 32
  6099. case TARGET_F_GETLK64:
  6100. ret = F_GETLK64;
  6101. break;
  6102. case TARGET_F_SETLK64:
  6103. ret = F_SETLK64;
  6104. break;
  6105. case TARGET_F_SETLKW64:
  6106. ret = F_SETLKW64;
  6107. break;
  6108. #endif
  6109. case TARGET_F_SETLEASE:
  6110. ret = F_SETLEASE;
  6111. break;
  6112. case TARGET_F_GETLEASE:
  6113. ret = F_GETLEASE;
  6114. break;
  6115. #ifdef F_DUPFD_CLOEXEC
  6116. case TARGET_F_DUPFD_CLOEXEC:
  6117. ret = F_DUPFD_CLOEXEC;
  6118. break;
  6119. #endif
  6120. case TARGET_F_NOTIFY:
  6121. ret = F_NOTIFY;
  6122. break;
  6123. #ifdef F_GETOWN_EX
  6124. case TARGET_F_GETOWN_EX:
  6125. ret = F_GETOWN_EX;
  6126. break;
  6127. #endif
  6128. #ifdef F_SETOWN_EX
  6129. case TARGET_F_SETOWN_EX:
  6130. ret = F_SETOWN_EX;
  6131. break;
  6132. #endif
  6133. #ifdef F_SETPIPE_SZ
  6134. case TARGET_F_SETPIPE_SZ:
  6135. ret = F_SETPIPE_SZ;
  6136. break;
  6137. case TARGET_F_GETPIPE_SZ:
  6138. ret = F_GETPIPE_SZ;
  6139. break;
  6140. #endif
  6141. #ifdef F_ADD_SEALS
  6142. case TARGET_F_ADD_SEALS:
  6143. ret = F_ADD_SEALS;
  6144. break;
  6145. case TARGET_F_GET_SEALS:
  6146. ret = F_GET_SEALS;
  6147. break;
  6148. #endif
  6149. default:
  6150. ret = -TARGET_EINVAL;
  6151. break;
  6152. }
  6153. #if defined(__powerpc64__)
  6154. /* On PPC64, glibc headers has the F_*LK* defined to 12, 13 and 14 and
  6155. * is not supported by kernel. The glibc fcntl call actually adjusts
  6156. * them to 5, 6 and 7 before making the syscall(). Since we make the
  6157. * syscall directly, adjust to what is supported by the kernel.
  6158. */
  6159. if (ret >= F_GETLK64 && ret <= F_SETLKW64) {
  6160. ret -= F_GETLK64 - 5;
  6161. }
  6162. #endif
  6163. return ret;
  6164. }
  6165. #define FLOCK_TRANSTBL \
  6166. switch (type) { \
  6167. TRANSTBL_CONVERT(F_RDLCK); \
  6168. TRANSTBL_CONVERT(F_WRLCK); \
  6169. TRANSTBL_CONVERT(F_UNLCK); \
  6170. }
  6171. static int target_to_host_flock(int type)
  6172. {
  6173. #define TRANSTBL_CONVERT(a) case TARGET_##a: return a
  6174. FLOCK_TRANSTBL
  6175. #undef TRANSTBL_CONVERT
  6176. return -TARGET_EINVAL;
  6177. }
  6178. static int host_to_target_flock(int type)
  6179. {
  6180. #define TRANSTBL_CONVERT(a) case a: return TARGET_##a
  6181. FLOCK_TRANSTBL
  6182. #undef TRANSTBL_CONVERT
  6183. /* if we don't know how to convert the value coming
  6184. * from the host we copy to the target field as-is
  6185. */
  6186. return type;
  6187. }
  6188. static inline abi_long copy_from_user_flock(struct flock64 *fl,
  6189. abi_ulong target_flock_addr)
  6190. {
  6191. struct target_flock *target_fl;
  6192. int l_type;
  6193. if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
  6194. return -TARGET_EFAULT;
  6195. }
  6196. __get_user(l_type, &target_fl->l_type);
  6197. l_type = target_to_host_flock(l_type);
  6198. if (l_type < 0) {
  6199. return l_type;
  6200. }
  6201. fl->l_type = l_type;
  6202. __get_user(fl->l_whence, &target_fl->l_whence);
  6203. __get_user(fl->l_start, &target_fl->l_start);
  6204. __get_user(fl->l_len, &target_fl->l_len);
  6205. __get_user(fl->l_pid, &target_fl->l_pid);
  6206. unlock_user_struct(target_fl, target_flock_addr, 0);
  6207. return 0;
  6208. }
  6209. static inline abi_long copy_to_user_flock(abi_ulong target_flock_addr,
  6210. const struct flock64 *fl)
  6211. {
  6212. struct target_flock *target_fl;
  6213. short l_type;
  6214. if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
  6215. return -TARGET_EFAULT;
  6216. }
  6217. l_type = host_to_target_flock(fl->l_type);
  6218. __put_user(l_type, &target_fl->l_type);
  6219. __put_user(fl->l_whence, &target_fl->l_whence);
  6220. __put_user(fl->l_start, &target_fl->l_start);
  6221. __put_user(fl->l_len, &target_fl->l_len);
  6222. __put_user(fl->l_pid, &target_fl->l_pid);
  6223. unlock_user_struct(target_fl, target_flock_addr, 1);
  6224. return 0;
  6225. }
  6226. typedef abi_long from_flock64_fn(struct flock64 *fl, abi_ulong target_addr);
  6227. typedef abi_long to_flock64_fn(abi_ulong target_addr, const struct flock64 *fl);
  6228. #if defined(TARGET_ARM) && TARGET_ABI_BITS == 32
  6229. struct target_oabi_flock64 {
  6230. abi_short l_type;
  6231. abi_short l_whence;
  6232. abi_llong l_start;
  6233. abi_llong l_len;
  6234. abi_int l_pid;
  6235. } QEMU_PACKED;
  6236. static inline abi_long copy_from_user_oabi_flock64(struct flock64 *fl,
  6237. abi_ulong target_flock_addr)
  6238. {
  6239. struct target_oabi_flock64 *target_fl;
  6240. int l_type;
  6241. if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
  6242. return -TARGET_EFAULT;
  6243. }
  6244. __get_user(l_type, &target_fl->l_type);
  6245. l_type = target_to_host_flock(l_type);
  6246. if (l_type < 0) {
  6247. return l_type;
  6248. }
  6249. fl->l_type = l_type;
  6250. __get_user(fl->l_whence, &target_fl->l_whence);
  6251. __get_user(fl->l_start, &target_fl->l_start);
  6252. __get_user(fl->l_len, &target_fl->l_len);
  6253. __get_user(fl->l_pid, &target_fl->l_pid);
  6254. unlock_user_struct(target_fl, target_flock_addr, 0);
  6255. return 0;
  6256. }
  6257. static inline abi_long copy_to_user_oabi_flock64(abi_ulong target_flock_addr,
  6258. const struct flock64 *fl)
  6259. {
  6260. struct target_oabi_flock64 *target_fl;
  6261. short l_type;
  6262. if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
  6263. return -TARGET_EFAULT;
  6264. }
  6265. l_type = host_to_target_flock(fl->l_type);
  6266. __put_user(l_type, &target_fl->l_type);
  6267. __put_user(fl->l_whence, &target_fl->l_whence);
  6268. __put_user(fl->l_start, &target_fl->l_start);
  6269. __put_user(fl->l_len, &target_fl->l_len);
  6270. __put_user(fl->l_pid, &target_fl->l_pid);
  6271. unlock_user_struct(target_fl, target_flock_addr, 1);
  6272. return 0;
  6273. }
  6274. #endif
  6275. static inline abi_long copy_from_user_flock64(struct flock64 *fl,
  6276. abi_ulong target_flock_addr)
  6277. {
  6278. struct target_flock64 *target_fl;
  6279. int l_type;
  6280. if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
  6281. return -TARGET_EFAULT;
  6282. }
  6283. __get_user(l_type, &target_fl->l_type);
  6284. l_type = target_to_host_flock(l_type);
  6285. if (l_type < 0) {
  6286. return l_type;
  6287. }
  6288. fl->l_type = l_type;
  6289. __get_user(fl->l_whence, &target_fl->l_whence);
  6290. __get_user(fl->l_start, &target_fl->l_start);
  6291. __get_user(fl->l_len, &target_fl->l_len);
  6292. __get_user(fl->l_pid, &target_fl->l_pid);
  6293. unlock_user_struct(target_fl, target_flock_addr, 0);
  6294. return 0;
  6295. }
  6296. static inline abi_long copy_to_user_flock64(abi_ulong target_flock_addr,
  6297. const struct flock64 *fl)
  6298. {
  6299. struct target_flock64 *target_fl;
  6300. short l_type;
  6301. if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
  6302. return -TARGET_EFAULT;
  6303. }
  6304. l_type = host_to_target_flock(fl->l_type);
  6305. __put_user(l_type, &target_fl->l_type);
  6306. __put_user(fl->l_whence, &target_fl->l_whence);
  6307. __put_user(fl->l_start, &target_fl->l_start);
  6308. __put_user(fl->l_len, &target_fl->l_len);
  6309. __put_user(fl->l_pid, &target_fl->l_pid);
  6310. unlock_user_struct(target_fl, target_flock_addr, 1);
  6311. return 0;
  6312. }
  6313. static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
  6314. {
  6315. struct flock64 fl64;
  6316. #ifdef F_GETOWN_EX
  6317. struct f_owner_ex fox;
  6318. struct target_f_owner_ex *target_fox;
  6319. #endif
  6320. abi_long ret;
  6321. int host_cmd = target_to_host_fcntl_cmd(cmd);
  6322. if (host_cmd == -TARGET_EINVAL)
  6323. return host_cmd;
  6324. switch(cmd) {
  6325. case TARGET_F_GETLK:
  6326. ret = copy_from_user_flock(&fl64, arg);
  6327. if (ret) {
  6328. return ret;
  6329. }
  6330. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  6331. if (ret == 0) {
  6332. ret = copy_to_user_flock(arg, &fl64);
  6333. }
  6334. break;
  6335. case TARGET_F_SETLK:
  6336. case TARGET_F_SETLKW:
  6337. ret = copy_from_user_flock(&fl64, arg);
  6338. if (ret) {
  6339. return ret;
  6340. }
  6341. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  6342. break;
  6343. case TARGET_F_GETLK64:
  6344. case TARGET_F_OFD_GETLK:
  6345. ret = copy_from_user_flock64(&fl64, arg);
  6346. if (ret) {
  6347. return ret;
  6348. }
  6349. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  6350. if (ret == 0) {
  6351. ret = copy_to_user_flock64(arg, &fl64);
  6352. }
  6353. break;
  6354. case TARGET_F_SETLK64:
  6355. case TARGET_F_SETLKW64:
  6356. case TARGET_F_OFD_SETLK:
  6357. case TARGET_F_OFD_SETLKW:
  6358. ret = copy_from_user_flock64(&fl64, arg);
  6359. if (ret) {
  6360. return ret;
  6361. }
  6362. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  6363. break;
  6364. case TARGET_F_GETFL:
  6365. ret = get_errno(safe_fcntl(fd, host_cmd, arg));
  6366. if (ret >= 0) {
  6367. ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
  6368. /* tell 32-bit guests it uses largefile on 64-bit hosts: */
  6369. if (O_LARGEFILE == 0 && HOST_LONG_BITS == 64) {
  6370. ret |= TARGET_O_LARGEFILE;
  6371. }
  6372. }
  6373. break;
  6374. case TARGET_F_SETFL:
  6375. ret = get_errno(safe_fcntl(fd, host_cmd,
  6376. target_to_host_bitmask(arg,
  6377. fcntl_flags_tbl)));
  6378. break;
  6379. #ifdef F_GETOWN_EX
  6380. case TARGET_F_GETOWN_EX:
  6381. ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
  6382. if (ret >= 0) {
  6383. if (!lock_user_struct(VERIFY_WRITE, target_fox, arg, 0))
  6384. return -TARGET_EFAULT;
  6385. target_fox->type = tswap32(fox.type);
  6386. target_fox->pid = tswap32(fox.pid);
  6387. unlock_user_struct(target_fox, arg, 1);
  6388. }
  6389. break;
  6390. #endif
  6391. #ifdef F_SETOWN_EX
  6392. case TARGET_F_SETOWN_EX:
  6393. if (!lock_user_struct(VERIFY_READ, target_fox, arg, 1))
  6394. return -TARGET_EFAULT;
  6395. fox.type = tswap32(target_fox->type);
  6396. fox.pid = tswap32(target_fox->pid);
  6397. unlock_user_struct(target_fox, arg, 0);
  6398. ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
  6399. break;
  6400. #endif
  6401. case TARGET_F_SETSIG:
  6402. ret = get_errno(safe_fcntl(fd, host_cmd, target_to_host_signal(arg)));
  6403. break;
  6404. case TARGET_F_GETSIG:
  6405. ret = host_to_target_signal(get_errno(safe_fcntl(fd, host_cmd, arg)));
  6406. break;
  6407. case TARGET_F_SETOWN:
  6408. case TARGET_F_GETOWN:
  6409. case TARGET_F_SETLEASE:
  6410. case TARGET_F_GETLEASE:
  6411. case TARGET_F_SETPIPE_SZ:
  6412. case TARGET_F_GETPIPE_SZ:
  6413. case TARGET_F_ADD_SEALS:
  6414. case TARGET_F_GET_SEALS:
  6415. ret = get_errno(safe_fcntl(fd, host_cmd, arg));
  6416. break;
  6417. default:
  6418. ret = get_errno(safe_fcntl(fd, cmd, arg));
  6419. break;
  6420. }
  6421. return ret;
  6422. }
  6423. #ifdef USE_UID16
  6424. static inline int high2lowuid(int uid)
  6425. {
  6426. if (uid > 65535)
  6427. return 65534;
  6428. else
  6429. return uid;
  6430. }
  6431. static inline int high2lowgid(int gid)
  6432. {
  6433. if (gid > 65535)
  6434. return 65534;
  6435. else
  6436. return gid;
  6437. }
  6438. static inline int low2highuid(int uid)
  6439. {
  6440. if ((int16_t)uid == -1)
  6441. return -1;
  6442. else
  6443. return uid;
  6444. }
  6445. static inline int low2highgid(int gid)
  6446. {
  6447. if ((int16_t)gid == -1)
  6448. return -1;
  6449. else
  6450. return gid;
  6451. }
  6452. static inline int tswapid(int id)
  6453. {
  6454. return tswap16(id);
  6455. }
  6456. #define put_user_id(x, gaddr) put_user_u16(x, gaddr)
  6457. #else /* !USE_UID16 */
  6458. static inline int high2lowuid(int uid)
  6459. {
  6460. return uid;
  6461. }
  6462. static inline int high2lowgid(int gid)
  6463. {
  6464. return gid;
  6465. }
  6466. static inline int low2highuid(int uid)
  6467. {
  6468. return uid;
  6469. }
  6470. static inline int low2highgid(int gid)
  6471. {
  6472. return gid;
  6473. }
  6474. static inline int tswapid(int id)
  6475. {
  6476. return tswap32(id);
  6477. }
  6478. #define put_user_id(x, gaddr) put_user_u32(x, gaddr)
  6479. #endif /* USE_UID16 */
  6480. /* We must do direct syscalls for setting UID/GID, because we want to
  6481. * implement the Linux system call semantics of "change only for this thread",
  6482. * not the libc/POSIX semantics of "change for all threads in process".
  6483. * (See http://ewontfix.com/17/ for more details.)
  6484. * We use the 32-bit version of the syscalls if present; if it is not
  6485. * then either the host architecture supports 32-bit UIDs natively with
  6486. * the standard syscall, or the 16-bit UID is the best we can do.
  6487. */
  6488. #ifdef __NR_setuid32
  6489. #define __NR_sys_setuid __NR_setuid32
  6490. #else
  6491. #define __NR_sys_setuid __NR_setuid
  6492. #endif
  6493. #ifdef __NR_setgid32
  6494. #define __NR_sys_setgid __NR_setgid32
  6495. #else
  6496. #define __NR_sys_setgid __NR_setgid
  6497. #endif
  6498. #ifdef __NR_setresuid32
  6499. #define __NR_sys_setresuid __NR_setresuid32
  6500. #else
  6501. #define __NR_sys_setresuid __NR_setresuid
  6502. #endif
  6503. #ifdef __NR_setresgid32
  6504. #define __NR_sys_setresgid __NR_setresgid32
  6505. #else
  6506. #define __NR_sys_setresgid __NR_setresgid
  6507. #endif
  6508. #ifdef __NR_setgroups32
  6509. #define __NR_sys_setgroups __NR_setgroups32
  6510. #else
  6511. #define __NR_sys_setgroups __NR_setgroups
  6512. #endif
  6513. #ifdef __NR_sys_setreuid32
  6514. #define __NR_sys_setreuid __NR_setreuid32
  6515. #else
  6516. #define __NR_sys_setreuid __NR_setreuid
  6517. #endif
  6518. #ifdef __NR_sys_setregid32
  6519. #define __NR_sys_setregid __NR_setregid32
  6520. #else
  6521. #define __NR_sys_setregid __NR_setregid
  6522. #endif
  6523. _syscall1(int, sys_setuid, uid_t, uid)
  6524. _syscall1(int, sys_setgid, gid_t, gid)
  6525. _syscall3(int, sys_setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
  6526. _syscall3(int, sys_setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
  6527. _syscall2(int, sys_setgroups, int, size, gid_t *, grouplist)
  6528. _syscall2(int, sys_setreuid, uid_t, ruid, uid_t, euid);
  6529. _syscall2(int, sys_setregid, gid_t, rgid, gid_t, egid);
  6530. void syscall_init(void)
  6531. {
  6532. IOCTLEntry *ie;
  6533. const argtype *arg_type;
  6534. int size;
  6535. thunk_init(STRUCT_MAX);
  6536. #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
  6537. #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
  6538. #include "syscall_types.h"
  6539. #undef STRUCT
  6540. #undef STRUCT_SPECIAL
  6541. /* we patch the ioctl size if necessary. We rely on the fact that
  6542. no ioctl has all the bits at '1' in the size field */
  6543. ie = ioctl_entries;
  6544. while (ie->target_cmd != 0) {
  6545. if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
  6546. TARGET_IOC_SIZEMASK) {
  6547. arg_type = ie->arg_type;
  6548. if (arg_type[0] != TYPE_PTR) {
  6549. fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
  6550. ie->target_cmd);
  6551. exit(1);
  6552. }
  6553. arg_type++;
  6554. size = thunk_type_size(arg_type, 0);
  6555. ie->target_cmd = (ie->target_cmd &
  6556. ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
  6557. (size << TARGET_IOC_SIZESHIFT);
  6558. }
  6559. /* automatic consistency check if same arch */
  6560. #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
  6561. (defined(__x86_64__) && defined(TARGET_X86_64))
  6562. if (unlikely(ie->target_cmd != ie->host_cmd)) {
  6563. fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
  6564. ie->name, ie->target_cmd, ie->host_cmd);
  6565. }
  6566. #endif
  6567. ie++;
  6568. }
  6569. }
  6570. #ifdef TARGET_NR_truncate64
  6571. static inline abi_long target_truncate64(CPUArchState *cpu_env, const char *arg1,
  6572. abi_long arg2,
  6573. abi_long arg3,
  6574. abi_long arg4)
  6575. {
  6576. if (regpairs_aligned(cpu_env, TARGET_NR_truncate64)) {
  6577. arg2 = arg3;
  6578. arg3 = arg4;
  6579. }
  6580. return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
  6581. }
  6582. #endif
  6583. #ifdef TARGET_NR_ftruncate64
  6584. static inline abi_long target_ftruncate64(CPUArchState *cpu_env, abi_long arg1,
  6585. abi_long arg2,
  6586. abi_long arg3,
  6587. abi_long arg4)
  6588. {
  6589. if (regpairs_aligned(cpu_env, TARGET_NR_ftruncate64)) {
  6590. arg2 = arg3;
  6591. arg3 = arg4;
  6592. }
  6593. return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
  6594. }
  6595. #endif
  6596. #if defined(TARGET_NR_timer_settime) || \
  6597. (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
  6598. static inline abi_long target_to_host_itimerspec(struct itimerspec *host_its,
  6599. abi_ulong target_addr)
  6600. {
  6601. if (target_to_host_timespec(&host_its->it_interval, target_addr +
  6602. offsetof(struct target_itimerspec,
  6603. it_interval)) ||
  6604. target_to_host_timespec(&host_its->it_value, target_addr +
  6605. offsetof(struct target_itimerspec,
  6606. it_value))) {
  6607. return -TARGET_EFAULT;
  6608. }
  6609. return 0;
  6610. }
  6611. #endif
  6612. #if defined(TARGET_NR_timer_settime64) || \
  6613. (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD))
  6614. static inline abi_long target_to_host_itimerspec64(struct itimerspec *host_its,
  6615. abi_ulong target_addr)
  6616. {
  6617. if (target_to_host_timespec64(&host_its->it_interval, target_addr +
  6618. offsetof(struct target__kernel_itimerspec,
  6619. it_interval)) ||
  6620. target_to_host_timespec64(&host_its->it_value, target_addr +
  6621. offsetof(struct target__kernel_itimerspec,
  6622. it_value))) {
  6623. return -TARGET_EFAULT;
  6624. }
  6625. return 0;
  6626. }
  6627. #endif
  6628. #if ((defined(TARGET_NR_timerfd_gettime) || \
  6629. defined(TARGET_NR_timerfd_settime)) && defined(CONFIG_TIMERFD)) || \
  6630. defined(TARGET_NR_timer_gettime) || defined(TARGET_NR_timer_settime)
  6631. static inline abi_long host_to_target_itimerspec(abi_ulong target_addr,
  6632. struct itimerspec *host_its)
  6633. {
  6634. if (host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
  6635. it_interval),
  6636. &host_its->it_interval) ||
  6637. host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
  6638. it_value),
  6639. &host_its->it_value)) {
  6640. return -TARGET_EFAULT;
  6641. }
  6642. return 0;
  6643. }
  6644. #endif
  6645. #if ((defined(TARGET_NR_timerfd_gettime64) || \
  6646. defined(TARGET_NR_timerfd_settime64)) && defined(CONFIG_TIMERFD)) || \
  6647. defined(TARGET_NR_timer_gettime64) || defined(TARGET_NR_timer_settime64)
  6648. static inline abi_long host_to_target_itimerspec64(abi_ulong target_addr,
  6649. struct itimerspec *host_its)
  6650. {
  6651. if (host_to_target_timespec64(target_addr +
  6652. offsetof(struct target__kernel_itimerspec,
  6653. it_interval),
  6654. &host_its->it_interval) ||
  6655. host_to_target_timespec64(target_addr +
  6656. offsetof(struct target__kernel_itimerspec,
  6657. it_value),
  6658. &host_its->it_value)) {
  6659. return -TARGET_EFAULT;
  6660. }
  6661. return 0;
  6662. }
  6663. #endif
  6664. #if defined(TARGET_NR_adjtimex) || \
  6665. (defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME))
  6666. static inline abi_long target_to_host_timex(struct timex *host_tx,
  6667. abi_long target_addr)
  6668. {
  6669. struct target_timex *target_tx;
  6670. if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) {
  6671. return -TARGET_EFAULT;
  6672. }
  6673. __get_user(host_tx->modes, &target_tx->modes);
  6674. __get_user(host_tx->offset, &target_tx->offset);
  6675. __get_user(host_tx->freq, &target_tx->freq);
  6676. __get_user(host_tx->maxerror, &target_tx->maxerror);
  6677. __get_user(host_tx->esterror, &target_tx->esterror);
  6678. __get_user(host_tx->status, &target_tx->status);
  6679. __get_user(host_tx->constant, &target_tx->constant);
  6680. __get_user(host_tx->precision, &target_tx->precision);
  6681. __get_user(host_tx->tolerance, &target_tx->tolerance);
  6682. __get_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
  6683. __get_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
  6684. __get_user(host_tx->tick, &target_tx->tick);
  6685. __get_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  6686. __get_user(host_tx->jitter, &target_tx->jitter);
  6687. __get_user(host_tx->shift, &target_tx->shift);
  6688. __get_user(host_tx->stabil, &target_tx->stabil);
  6689. __get_user(host_tx->jitcnt, &target_tx->jitcnt);
  6690. __get_user(host_tx->calcnt, &target_tx->calcnt);
  6691. __get_user(host_tx->errcnt, &target_tx->errcnt);
  6692. __get_user(host_tx->stbcnt, &target_tx->stbcnt);
  6693. __get_user(host_tx->tai, &target_tx->tai);
  6694. unlock_user_struct(target_tx, target_addr, 0);
  6695. return 0;
  6696. }
  6697. static inline abi_long host_to_target_timex(abi_long target_addr,
  6698. struct timex *host_tx)
  6699. {
  6700. struct target_timex *target_tx;
  6701. if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) {
  6702. return -TARGET_EFAULT;
  6703. }
  6704. __put_user(host_tx->modes, &target_tx->modes);
  6705. __put_user(host_tx->offset, &target_tx->offset);
  6706. __put_user(host_tx->freq, &target_tx->freq);
  6707. __put_user(host_tx->maxerror, &target_tx->maxerror);
  6708. __put_user(host_tx->esterror, &target_tx->esterror);
  6709. __put_user(host_tx->status, &target_tx->status);
  6710. __put_user(host_tx->constant, &target_tx->constant);
  6711. __put_user(host_tx->precision, &target_tx->precision);
  6712. __put_user(host_tx->tolerance, &target_tx->tolerance);
  6713. __put_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
  6714. __put_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
  6715. __put_user(host_tx->tick, &target_tx->tick);
  6716. __put_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  6717. __put_user(host_tx->jitter, &target_tx->jitter);
  6718. __put_user(host_tx->shift, &target_tx->shift);
  6719. __put_user(host_tx->stabil, &target_tx->stabil);
  6720. __put_user(host_tx->jitcnt, &target_tx->jitcnt);
  6721. __put_user(host_tx->calcnt, &target_tx->calcnt);
  6722. __put_user(host_tx->errcnt, &target_tx->errcnt);
  6723. __put_user(host_tx->stbcnt, &target_tx->stbcnt);
  6724. __put_user(host_tx->tai, &target_tx->tai);
  6725. unlock_user_struct(target_tx, target_addr, 1);
  6726. return 0;
  6727. }
  6728. #endif
  6729. #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
  6730. static inline abi_long target_to_host_timex64(struct timex *host_tx,
  6731. abi_long target_addr)
  6732. {
  6733. struct target__kernel_timex *target_tx;
  6734. if (copy_from_user_timeval64(&host_tx->time, target_addr +
  6735. offsetof(struct target__kernel_timex,
  6736. time))) {
  6737. return -TARGET_EFAULT;
  6738. }
  6739. if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) {
  6740. return -TARGET_EFAULT;
  6741. }
  6742. __get_user(host_tx->modes, &target_tx->modes);
  6743. __get_user(host_tx->offset, &target_tx->offset);
  6744. __get_user(host_tx->freq, &target_tx->freq);
  6745. __get_user(host_tx->maxerror, &target_tx->maxerror);
  6746. __get_user(host_tx->esterror, &target_tx->esterror);
  6747. __get_user(host_tx->status, &target_tx->status);
  6748. __get_user(host_tx->constant, &target_tx->constant);
  6749. __get_user(host_tx->precision, &target_tx->precision);
  6750. __get_user(host_tx->tolerance, &target_tx->tolerance);
  6751. __get_user(host_tx->tick, &target_tx->tick);
  6752. __get_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  6753. __get_user(host_tx->jitter, &target_tx->jitter);
  6754. __get_user(host_tx->shift, &target_tx->shift);
  6755. __get_user(host_tx->stabil, &target_tx->stabil);
  6756. __get_user(host_tx->jitcnt, &target_tx->jitcnt);
  6757. __get_user(host_tx->calcnt, &target_tx->calcnt);
  6758. __get_user(host_tx->errcnt, &target_tx->errcnt);
  6759. __get_user(host_tx->stbcnt, &target_tx->stbcnt);
  6760. __get_user(host_tx->tai, &target_tx->tai);
  6761. unlock_user_struct(target_tx, target_addr, 0);
  6762. return 0;
  6763. }
  6764. static inline abi_long host_to_target_timex64(abi_long target_addr,
  6765. struct timex *host_tx)
  6766. {
  6767. struct target__kernel_timex *target_tx;
  6768. if (copy_to_user_timeval64(target_addr +
  6769. offsetof(struct target__kernel_timex, time),
  6770. &host_tx->time)) {
  6771. return -TARGET_EFAULT;
  6772. }
  6773. if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) {
  6774. return -TARGET_EFAULT;
  6775. }
  6776. __put_user(host_tx->modes, &target_tx->modes);
  6777. __put_user(host_tx->offset, &target_tx->offset);
  6778. __put_user(host_tx->freq, &target_tx->freq);
  6779. __put_user(host_tx->maxerror, &target_tx->maxerror);
  6780. __put_user(host_tx->esterror, &target_tx->esterror);
  6781. __put_user(host_tx->status, &target_tx->status);
  6782. __put_user(host_tx->constant, &target_tx->constant);
  6783. __put_user(host_tx->precision, &target_tx->precision);
  6784. __put_user(host_tx->tolerance, &target_tx->tolerance);
  6785. __put_user(host_tx->tick, &target_tx->tick);
  6786. __put_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  6787. __put_user(host_tx->jitter, &target_tx->jitter);
  6788. __put_user(host_tx->shift, &target_tx->shift);
  6789. __put_user(host_tx->stabil, &target_tx->stabil);
  6790. __put_user(host_tx->jitcnt, &target_tx->jitcnt);
  6791. __put_user(host_tx->calcnt, &target_tx->calcnt);
  6792. __put_user(host_tx->errcnt, &target_tx->errcnt);
  6793. __put_user(host_tx->stbcnt, &target_tx->stbcnt);
  6794. __put_user(host_tx->tai, &target_tx->tai);
  6795. unlock_user_struct(target_tx, target_addr, 1);
  6796. return 0;
  6797. }
  6798. #endif
  6799. #ifndef HAVE_SIGEV_NOTIFY_THREAD_ID
  6800. #define sigev_notify_thread_id _sigev_un._tid
  6801. #endif
  6802. static inline abi_long target_to_host_sigevent(struct sigevent *host_sevp,
  6803. abi_ulong target_addr)
  6804. {
  6805. struct target_sigevent *target_sevp;
  6806. if (!lock_user_struct(VERIFY_READ, target_sevp, target_addr, 1)) {
  6807. return -TARGET_EFAULT;
  6808. }
  6809. /* This union is awkward on 64 bit systems because it has a 32 bit
  6810. * integer and a pointer in it; we follow the conversion approach
  6811. * used for handling sigval types in signal.c so the guest should get
  6812. * the correct value back even if we did a 64 bit byteswap and it's
  6813. * using the 32 bit integer.
  6814. */
  6815. host_sevp->sigev_value.sival_ptr =
  6816. (void *)(uintptr_t)tswapal(target_sevp->sigev_value.sival_ptr);
  6817. host_sevp->sigev_signo =
  6818. target_to_host_signal(tswap32(target_sevp->sigev_signo));
  6819. host_sevp->sigev_notify = tswap32(target_sevp->sigev_notify);
  6820. host_sevp->sigev_notify_thread_id = tswap32(target_sevp->_sigev_un._tid);
  6821. unlock_user_struct(target_sevp, target_addr, 1);
  6822. return 0;
  6823. }
  6824. #if defined(TARGET_NR_mlockall)
  6825. static inline int target_to_host_mlockall_arg(int arg)
  6826. {
  6827. int result = 0;
  6828. if (arg & TARGET_MCL_CURRENT) {
  6829. result |= MCL_CURRENT;
  6830. }
  6831. if (arg & TARGET_MCL_FUTURE) {
  6832. result |= MCL_FUTURE;
  6833. }
  6834. #ifdef MCL_ONFAULT
  6835. if (arg & TARGET_MCL_ONFAULT) {
  6836. result |= MCL_ONFAULT;
  6837. }
  6838. #endif
  6839. return result;
  6840. }
  6841. #endif
  6842. static inline int target_to_host_msync_arg(abi_long arg)
  6843. {
  6844. return ((arg & TARGET_MS_ASYNC) ? MS_ASYNC : 0) |
  6845. ((arg & TARGET_MS_INVALIDATE) ? MS_INVALIDATE : 0) |
  6846. ((arg & TARGET_MS_SYNC) ? MS_SYNC : 0) |
  6847. (arg & ~(TARGET_MS_ASYNC | TARGET_MS_INVALIDATE | TARGET_MS_SYNC));
  6848. }
  6849. #if (defined(TARGET_NR_stat64) || defined(TARGET_NR_lstat64) || \
  6850. defined(TARGET_NR_fstat64) || defined(TARGET_NR_fstatat64) || \
  6851. defined(TARGET_NR_newfstatat))
  6852. static inline abi_long host_to_target_stat64(CPUArchState *cpu_env,
  6853. abi_ulong target_addr,
  6854. struct stat *host_st)
  6855. {
  6856. #if defined(TARGET_ARM) && defined(TARGET_ABI32)
  6857. if (cpu_env->eabi) {
  6858. struct target_eabi_stat64 *target_st;
  6859. if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
  6860. return -TARGET_EFAULT;
  6861. memset(target_st, 0, sizeof(struct target_eabi_stat64));
  6862. __put_user(host_st->st_dev, &target_st->st_dev);
  6863. __put_user(host_st->st_ino, &target_st->st_ino);
  6864. #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
  6865. __put_user(host_st->st_ino, &target_st->__st_ino);
  6866. #endif
  6867. __put_user(host_st->st_mode, &target_st->st_mode);
  6868. __put_user(host_st->st_nlink, &target_st->st_nlink);
  6869. __put_user(host_st->st_uid, &target_st->st_uid);
  6870. __put_user(host_st->st_gid, &target_st->st_gid);
  6871. __put_user(host_st->st_rdev, &target_st->st_rdev);
  6872. __put_user(host_st->st_size, &target_st->st_size);
  6873. __put_user(host_st->st_blksize, &target_st->st_blksize);
  6874. __put_user(host_st->st_blocks, &target_st->st_blocks);
  6875. __put_user(host_st->st_atime, &target_st->target_st_atime);
  6876. __put_user(host_st->st_mtime, &target_st->target_st_mtime);
  6877. __put_user(host_st->st_ctime, &target_st->target_st_ctime);
  6878. #ifdef HAVE_STRUCT_STAT_ST_ATIM
  6879. __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
  6880. __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
  6881. __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
  6882. #endif
  6883. unlock_user_struct(target_st, target_addr, 1);
  6884. } else
  6885. #endif
  6886. {
  6887. #if defined(TARGET_HAS_STRUCT_STAT64)
  6888. struct target_stat64 *target_st;
  6889. #else
  6890. struct target_stat *target_st;
  6891. #endif
  6892. if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
  6893. return -TARGET_EFAULT;
  6894. memset(target_st, 0, sizeof(*target_st));
  6895. __put_user(host_st->st_dev, &target_st->st_dev);
  6896. __put_user(host_st->st_ino, &target_st->st_ino);
  6897. #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
  6898. __put_user(host_st->st_ino, &target_st->__st_ino);
  6899. #endif
  6900. __put_user(host_st->st_mode, &target_st->st_mode);
  6901. __put_user(host_st->st_nlink, &target_st->st_nlink);
  6902. __put_user(host_st->st_uid, &target_st->st_uid);
  6903. __put_user(host_st->st_gid, &target_st->st_gid);
  6904. __put_user(host_st->st_rdev, &target_st->st_rdev);
  6905. /* XXX: better use of kernel struct */
  6906. __put_user(host_st->st_size, &target_st->st_size);
  6907. __put_user(host_st->st_blksize, &target_st->st_blksize);
  6908. __put_user(host_st->st_blocks, &target_st->st_blocks);
  6909. __put_user(host_st->st_atime, &target_st->target_st_atime);
  6910. __put_user(host_st->st_mtime, &target_st->target_st_mtime);
  6911. __put_user(host_st->st_ctime, &target_st->target_st_ctime);
  6912. #ifdef HAVE_STRUCT_STAT_ST_ATIM
  6913. __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
  6914. __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
  6915. __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
  6916. #endif
  6917. unlock_user_struct(target_st, target_addr, 1);
  6918. }
  6919. return 0;
  6920. }
  6921. #endif
  6922. #if defined(TARGET_NR_statx) && defined(__NR_statx)
  6923. static inline abi_long host_to_target_statx(struct target_statx *host_stx,
  6924. abi_ulong target_addr)
  6925. {
  6926. struct target_statx *target_stx;
  6927. if (!lock_user_struct(VERIFY_WRITE, target_stx, target_addr, 0)) {
  6928. return -TARGET_EFAULT;
  6929. }
  6930. memset(target_stx, 0, sizeof(*target_stx));
  6931. __put_user(host_stx->stx_mask, &target_stx->stx_mask);
  6932. __put_user(host_stx->stx_blksize, &target_stx->stx_blksize);
  6933. __put_user(host_stx->stx_attributes, &target_stx->stx_attributes);
  6934. __put_user(host_stx->stx_nlink, &target_stx->stx_nlink);
  6935. __put_user(host_stx->stx_uid, &target_stx->stx_uid);
  6936. __put_user(host_stx->stx_gid, &target_stx->stx_gid);
  6937. __put_user(host_stx->stx_mode, &target_stx->stx_mode);
  6938. __put_user(host_stx->stx_ino, &target_stx->stx_ino);
  6939. __put_user(host_stx->stx_size, &target_stx->stx_size);
  6940. __put_user(host_stx->stx_blocks, &target_stx->stx_blocks);
  6941. __put_user(host_stx->stx_attributes_mask, &target_stx->stx_attributes_mask);
  6942. __put_user(host_stx->stx_atime.tv_sec, &target_stx->stx_atime.tv_sec);
  6943. __put_user(host_stx->stx_atime.tv_nsec, &target_stx->stx_atime.tv_nsec);
  6944. __put_user(host_stx->stx_btime.tv_sec, &target_stx->stx_btime.tv_sec);
  6945. __put_user(host_stx->stx_btime.tv_nsec, &target_stx->stx_btime.tv_nsec);
  6946. __put_user(host_stx->stx_ctime.tv_sec, &target_stx->stx_ctime.tv_sec);
  6947. __put_user(host_stx->stx_ctime.tv_nsec, &target_stx->stx_ctime.tv_nsec);
  6948. __put_user(host_stx->stx_mtime.tv_sec, &target_stx->stx_mtime.tv_sec);
  6949. __put_user(host_stx->stx_mtime.tv_nsec, &target_stx->stx_mtime.tv_nsec);
  6950. __put_user(host_stx->stx_rdev_major, &target_stx->stx_rdev_major);
  6951. __put_user(host_stx->stx_rdev_minor, &target_stx->stx_rdev_minor);
  6952. __put_user(host_stx->stx_dev_major, &target_stx->stx_dev_major);
  6953. __put_user(host_stx->stx_dev_minor, &target_stx->stx_dev_minor);
  6954. unlock_user_struct(target_stx, target_addr, 1);
  6955. return 0;
  6956. }
  6957. #endif
  6958. static int do_sys_futex(int *uaddr, int op, int val,
  6959. const struct timespec *timeout, int *uaddr2,
  6960. int val3)
  6961. {
  6962. #if HOST_LONG_BITS == 64
  6963. #if defined(__NR_futex)
  6964. /* always a 64-bit time_t, it doesn't define _time64 version */
  6965. return sys_futex(uaddr, op, val, timeout, uaddr2, val3);
  6966. #endif
  6967. #else /* HOST_LONG_BITS == 64 */
  6968. #if defined(__NR_futex_time64)
  6969. if (sizeof(timeout->tv_sec) == 8) {
  6970. /* _time64 function on 32bit arch */
  6971. return sys_futex_time64(uaddr, op, val, timeout, uaddr2, val3);
  6972. }
  6973. #endif
  6974. #if defined(__NR_futex)
  6975. /* old function on 32bit arch */
  6976. return sys_futex(uaddr, op, val, timeout, uaddr2, val3);
  6977. #endif
  6978. #endif /* HOST_LONG_BITS == 64 */
  6979. g_assert_not_reached();
  6980. }
  6981. static int do_safe_futex(int *uaddr, int op, int val,
  6982. const struct timespec *timeout, int *uaddr2,
  6983. int val3)
  6984. {
  6985. #if HOST_LONG_BITS == 64
  6986. #if defined(__NR_futex)
  6987. /* always a 64-bit time_t, it doesn't define _time64 version */
  6988. return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3));
  6989. #endif
  6990. #else /* HOST_LONG_BITS == 64 */
  6991. #if defined(__NR_futex_time64)
  6992. if (sizeof(timeout->tv_sec) == 8) {
  6993. /* _time64 function on 32bit arch */
  6994. return get_errno(safe_futex_time64(uaddr, op, val, timeout, uaddr2,
  6995. val3));
  6996. }
  6997. #endif
  6998. #if defined(__NR_futex)
  6999. /* old function on 32bit arch */
  7000. return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3));
  7001. #endif
  7002. #endif /* HOST_LONG_BITS == 64 */
  7003. return -TARGET_ENOSYS;
  7004. }
  7005. /* ??? Using host futex calls even when target atomic operations
  7006. are not really atomic probably breaks things. However implementing
  7007. futexes locally would make futexes shared between multiple processes
  7008. tricky. However they're probably useless because guest atomic
  7009. operations won't work either. */
  7010. #if defined(TARGET_NR_futex) || defined(TARGET_NR_futex_time64)
  7011. static int do_futex(CPUState *cpu, bool time64, target_ulong uaddr,
  7012. int op, int val, target_ulong timeout,
  7013. target_ulong uaddr2, int val3)
  7014. {
  7015. struct timespec ts, *pts = NULL;
  7016. void *haddr2 = NULL;
  7017. int base_op;
  7018. /* We assume FUTEX_* constants are the same on both host and target. */
  7019. #ifdef FUTEX_CMD_MASK
  7020. base_op = op & FUTEX_CMD_MASK;
  7021. #else
  7022. base_op = op;
  7023. #endif
  7024. switch (base_op) {
  7025. case FUTEX_WAIT:
  7026. case FUTEX_WAIT_BITSET:
  7027. val = tswap32(val);
  7028. break;
  7029. case FUTEX_WAIT_REQUEUE_PI:
  7030. val = tswap32(val);
  7031. haddr2 = g2h(cpu, uaddr2);
  7032. break;
  7033. case FUTEX_LOCK_PI:
  7034. case FUTEX_LOCK_PI2:
  7035. break;
  7036. case FUTEX_WAKE:
  7037. case FUTEX_WAKE_BITSET:
  7038. case FUTEX_TRYLOCK_PI:
  7039. case FUTEX_UNLOCK_PI:
  7040. timeout = 0;
  7041. break;
  7042. case FUTEX_FD:
  7043. val = target_to_host_signal(val);
  7044. timeout = 0;
  7045. break;
  7046. case FUTEX_CMP_REQUEUE:
  7047. case FUTEX_CMP_REQUEUE_PI:
  7048. val3 = tswap32(val3);
  7049. /* fall through */
  7050. case FUTEX_REQUEUE:
  7051. case FUTEX_WAKE_OP:
  7052. /*
  7053. * For these, the 4th argument is not TIMEOUT, but VAL2.
  7054. * But the prototype of do_safe_futex takes a pointer, so
  7055. * insert casts to satisfy the compiler. We do not need
  7056. * to tswap VAL2 since it's not compared to guest memory.
  7057. */
  7058. pts = (struct timespec *)(uintptr_t)timeout;
  7059. timeout = 0;
  7060. haddr2 = g2h(cpu, uaddr2);
  7061. break;
  7062. default:
  7063. return -TARGET_ENOSYS;
  7064. }
  7065. if (timeout) {
  7066. pts = &ts;
  7067. if (time64
  7068. ? target_to_host_timespec64(pts, timeout)
  7069. : target_to_host_timespec(pts, timeout)) {
  7070. return -TARGET_EFAULT;
  7071. }
  7072. }
  7073. return do_safe_futex(g2h(cpu, uaddr), op, val, pts, haddr2, val3);
  7074. }
  7075. #endif
  7076. #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  7077. static abi_long do_name_to_handle_at(abi_long dirfd, abi_long pathname,
  7078. abi_long handle, abi_long mount_id,
  7079. abi_long flags)
  7080. {
  7081. struct file_handle *target_fh;
  7082. struct file_handle *fh;
  7083. int mid = 0;
  7084. abi_long ret;
  7085. char *name;
  7086. unsigned int size, total_size;
  7087. if (get_user_s32(size, handle)) {
  7088. return -TARGET_EFAULT;
  7089. }
  7090. name = lock_user_string(pathname);
  7091. if (!name) {
  7092. return -TARGET_EFAULT;
  7093. }
  7094. total_size = sizeof(struct file_handle) + size;
  7095. target_fh = lock_user(VERIFY_WRITE, handle, total_size, 0);
  7096. if (!target_fh) {
  7097. unlock_user(name, pathname, 0);
  7098. return -TARGET_EFAULT;
  7099. }
  7100. fh = g_malloc0(total_size);
  7101. fh->handle_bytes = size;
  7102. ret = get_errno(name_to_handle_at(dirfd, path(name), fh, &mid, flags));
  7103. unlock_user(name, pathname, 0);
  7104. /* man name_to_handle_at(2):
  7105. * Other than the use of the handle_bytes field, the caller should treat
  7106. * the file_handle structure as an opaque data type
  7107. */
  7108. memcpy(target_fh, fh, total_size);
  7109. target_fh->handle_bytes = tswap32(fh->handle_bytes);
  7110. target_fh->handle_type = tswap32(fh->handle_type);
  7111. g_free(fh);
  7112. unlock_user(target_fh, handle, total_size);
  7113. if (put_user_s32(mid, mount_id)) {
  7114. return -TARGET_EFAULT;
  7115. }
  7116. return ret;
  7117. }
  7118. #endif
  7119. #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  7120. static abi_long do_open_by_handle_at(abi_long mount_fd, abi_long handle,
  7121. abi_long flags)
  7122. {
  7123. struct file_handle *target_fh;
  7124. struct file_handle *fh;
  7125. unsigned int size, total_size;
  7126. abi_long ret;
  7127. if (get_user_s32(size, handle)) {
  7128. return -TARGET_EFAULT;
  7129. }
  7130. total_size = sizeof(struct file_handle) + size;
  7131. target_fh = lock_user(VERIFY_READ, handle, total_size, 1);
  7132. if (!target_fh) {
  7133. return -TARGET_EFAULT;
  7134. }
  7135. fh = g_memdup(target_fh, total_size);
  7136. fh->handle_bytes = size;
  7137. fh->handle_type = tswap32(target_fh->handle_type);
  7138. ret = get_errno(open_by_handle_at(mount_fd, fh,
  7139. target_to_host_bitmask(flags, fcntl_flags_tbl)));
  7140. g_free(fh);
  7141. unlock_user(target_fh, handle, total_size);
  7142. return ret;
  7143. }
  7144. #endif
  7145. #if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4)
  7146. static abi_long do_signalfd4(int fd, abi_long mask, int flags)
  7147. {
  7148. int host_flags;
  7149. target_sigset_t *target_mask;
  7150. sigset_t host_mask;
  7151. abi_long ret;
  7152. if (flags & ~(TARGET_O_NONBLOCK_MASK | TARGET_O_CLOEXEC)) {
  7153. return -TARGET_EINVAL;
  7154. }
  7155. if (!lock_user_struct(VERIFY_READ, target_mask, mask, 1)) {
  7156. return -TARGET_EFAULT;
  7157. }
  7158. target_to_host_sigset(&host_mask, target_mask);
  7159. host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
  7160. ret = get_errno(signalfd(fd, &host_mask, host_flags));
  7161. if (ret >= 0) {
  7162. fd_trans_register(ret, &target_signalfd_trans);
  7163. }
  7164. unlock_user_struct(target_mask, mask, 0);
  7165. return ret;
  7166. }
  7167. #endif
  7168. /* Map host to target signal numbers for the wait family of syscalls.
  7169. Assume all other status bits are the same. */
  7170. int host_to_target_waitstatus(int status)
  7171. {
  7172. if (WIFSIGNALED(status)) {
  7173. return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
  7174. }
  7175. if (WIFSTOPPED(status)) {
  7176. return (host_to_target_signal(WSTOPSIG(status)) << 8)
  7177. | (status & 0xff);
  7178. }
  7179. return status;
  7180. }
  7181. static int open_self_cmdline(CPUArchState *cpu_env, int fd)
  7182. {
  7183. CPUState *cpu = env_cpu(cpu_env);
  7184. struct linux_binprm *bprm = get_task_state(cpu)->bprm;
  7185. int i;
  7186. for (i = 0; i < bprm->argc; i++) {
  7187. size_t len = strlen(bprm->argv[i]) + 1;
  7188. if (write(fd, bprm->argv[i], len) != len) {
  7189. return -1;
  7190. }
  7191. }
  7192. return 0;
  7193. }
  7194. struct open_self_maps_data {
  7195. TaskState *ts;
  7196. IntervalTreeRoot *host_maps;
  7197. int fd;
  7198. bool smaps;
  7199. };
  7200. /*
  7201. * Subroutine to output one line of /proc/self/maps,
  7202. * or one region of /proc/self/smaps.
  7203. */
  7204. #ifdef TARGET_HPPA
  7205. # define test_stack(S, E, L) (E == L)
  7206. #else
  7207. # define test_stack(S, E, L) (S == L)
  7208. #endif
  7209. static void open_self_maps_4(const struct open_self_maps_data *d,
  7210. const MapInfo *mi, abi_ptr start,
  7211. abi_ptr end, unsigned flags)
  7212. {
  7213. const struct image_info *info = d->ts->info;
  7214. const char *path = mi->path;
  7215. uint64_t offset;
  7216. int fd = d->fd;
  7217. int count;
  7218. if (test_stack(start, end, info->stack_limit)) {
  7219. path = "[stack]";
  7220. } else if (start == info->brk) {
  7221. path = "[heap]";
  7222. } else if (start == info->vdso) {
  7223. path = "[vdso]";
  7224. #ifdef TARGET_X86_64
  7225. } else if (start == TARGET_VSYSCALL_PAGE) {
  7226. path = "[vsyscall]";
  7227. #endif
  7228. }
  7229. /* Except null device (MAP_ANON), adjust offset for this fragment. */
  7230. offset = mi->offset;
  7231. if (mi->dev) {
  7232. uintptr_t hstart = (uintptr_t)g2h_untagged(start);
  7233. offset += hstart - mi->itree.start;
  7234. }
  7235. count = dprintf(fd, TARGET_ABI_FMT_ptr "-" TARGET_ABI_FMT_ptr
  7236. " %c%c%c%c %08" PRIx64 " %02x:%02x %"PRId64,
  7237. start, end,
  7238. (flags & PAGE_READ) ? 'r' : '-',
  7239. (flags & PAGE_WRITE_ORG) ? 'w' : '-',
  7240. (flags & PAGE_EXEC) ? 'x' : '-',
  7241. mi->is_priv ? 'p' : 's',
  7242. offset, major(mi->dev), minor(mi->dev),
  7243. (uint64_t)mi->inode);
  7244. if (path) {
  7245. dprintf(fd, "%*s%s\n", 73 - count, "", path);
  7246. } else {
  7247. dprintf(fd, "\n");
  7248. }
  7249. if (d->smaps) {
  7250. unsigned long size = end - start;
  7251. unsigned long page_size_kb = TARGET_PAGE_SIZE >> 10;
  7252. unsigned long size_kb = size >> 10;
  7253. dprintf(fd, "Size: %lu kB\n"
  7254. "KernelPageSize: %lu kB\n"
  7255. "MMUPageSize: %lu kB\n"
  7256. "Rss: 0 kB\n"
  7257. "Pss: 0 kB\n"
  7258. "Pss_Dirty: 0 kB\n"
  7259. "Shared_Clean: 0 kB\n"
  7260. "Shared_Dirty: 0 kB\n"
  7261. "Private_Clean: 0 kB\n"
  7262. "Private_Dirty: 0 kB\n"
  7263. "Referenced: 0 kB\n"
  7264. "Anonymous: %lu kB\n"
  7265. "LazyFree: 0 kB\n"
  7266. "AnonHugePages: 0 kB\n"
  7267. "ShmemPmdMapped: 0 kB\n"
  7268. "FilePmdMapped: 0 kB\n"
  7269. "Shared_Hugetlb: 0 kB\n"
  7270. "Private_Hugetlb: 0 kB\n"
  7271. "Swap: 0 kB\n"
  7272. "SwapPss: 0 kB\n"
  7273. "Locked: 0 kB\n"
  7274. "THPeligible: 0\n"
  7275. "VmFlags:%s%s%s%s%s%s%s%s\n",
  7276. size_kb, page_size_kb, page_size_kb,
  7277. (flags & PAGE_ANON ? size_kb : 0),
  7278. (flags & PAGE_READ) ? " rd" : "",
  7279. (flags & PAGE_WRITE_ORG) ? " wr" : "",
  7280. (flags & PAGE_EXEC) ? " ex" : "",
  7281. mi->is_priv ? "" : " sh",
  7282. (flags & PAGE_READ) ? " mr" : "",
  7283. (flags & PAGE_WRITE_ORG) ? " mw" : "",
  7284. (flags & PAGE_EXEC) ? " me" : "",
  7285. mi->is_priv ? "" : " ms");
  7286. }
  7287. }
  7288. /*
  7289. * Callback for walk_memory_regions, when read_self_maps() fails.
  7290. * Proceed without the benefit of host /proc/self/maps cross-check.
  7291. */
  7292. static int open_self_maps_3(void *opaque, target_ulong guest_start,
  7293. target_ulong guest_end, unsigned long flags)
  7294. {
  7295. static const MapInfo mi = { .is_priv = true };
  7296. open_self_maps_4(opaque, &mi, guest_start, guest_end, flags);
  7297. return 0;
  7298. }
  7299. /*
  7300. * Callback for walk_memory_regions, when read_self_maps() succeeds.
  7301. */
  7302. static int open_self_maps_2(void *opaque, target_ulong guest_start,
  7303. target_ulong guest_end, unsigned long flags)
  7304. {
  7305. const struct open_self_maps_data *d = opaque;
  7306. uintptr_t host_start = (uintptr_t)g2h_untagged(guest_start);
  7307. uintptr_t host_last = (uintptr_t)g2h_untagged(guest_end - 1);
  7308. #ifdef TARGET_X86_64
  7309. /*
  7310. * Because of the extremely high position of the page within the guest
  7311. * virtual address space, this is not backed by host memory at all.
  7312. * Therefore the loop below would fail. This is the only instance
  7313. * of not having host backing memory.
  7314. */
  7315. if (guest_start == TARGET_VSYSCALL_PAGE) {
  7316. return open_self_maps_3(opaque, guest_start, guest_end, flags);
  7317. }
  7318. #endif
  7319. while (1) {
  7320. IntervalTreeNode *n =
  7321. interval_tree_iter_first(d->host_maps, host_start, host_start);
  7322. MapInfo *mi = container_of(n, MapInfo, itree);
  7323. uintptr_t this_hlast = MIN(host_last, n->last);
  7324. target_ulong this_gend = h2g(this_hlast) + 1;
  7325. open_self_maps_4(d, mi, guest_start, this_gend, flags);
  7326. if (this_hlast == host_last) {
  7327. return 0;
  7328. }
  7329. host_start = this_hlast + 1;
  7330. guest_start = h2g(host_start);
  7331. }
  7332. }
  7333. static int open_self_maps_1(CPUArchState *env, int fd, bool smaps)
  7334. {
  7335. struct open_self_maps_data d = {
  7336. .ts = get_task_state(env_cpu(env)),
  7337. .fd = fd,
  7338. .smaps = smaps
  7339. };
  7340. mmap_lock();
  7341. d.host_maps = read_self_maps();
  7342. if (d.host_maps) {
  7343. walk_memory_regions(&d, open_self_maps_2);
  7344. free_self_maps(d.host_maps);
  7345. } else {
  7346. walk_memory_regions(&d, open_self_maps_3);
  7347. }
  7348. mmap_unlock();
  7349. return 0;
  7350. }
  7351. static int open_self_maps(CPUArchState *cpu_env, int fd)
  7352. {
  7353. return open_self_maps_1(cpu_env, fd, false);
  7354. }
  7355. static int open_self_smaps(CPUArchState *cpu_env, int fd)
  7356. {
  7357. return open_self_maps_1(cpu_env, fd, true);
  7358. }
  7359. static int open_self_stat(CPUArchState *cpu_env, int fd)
  7360. {
  7361. CPUState *cpu = env_cpu(cpu_env);
  7362. TaskState *ts = get_task_state(cpu);
  7363. g_autoptr(GString) buf = g_string_new(NULL);
  7364. int i;
  7365. for (i = 0; i < 44; i++) {
  7366. if (i == 0) {
  7367. /* pid */
  7368. g_string_printf(buf, FMT_pid " ", getpid());
  7369. } else if (i == 1) {
  7370. /* app name */
  7371. gchar *bin = g_strrstr(ts->bprm->argv[0], "/");
  7372. bin = bin ? bin + 1 : ts->bprm->argv[0];
  7373. g_string_printf(buf, "(%.15s) ", bin);
  7374. } else if (i == 2) {
  7375. /* task state */
  7376. g_string_assign(buf, "R "); /* we are running right now */
  7377. } else if (i == 3) {
  7378. /* ppid */
  7379. g_string_printf(buf, FMT_pid " ", getppid());
  7380. } else if (i == 19) {
  7381. /* num_threads */
  7382. int cpus = 0;
  7383. WITH_RCU_READ_LOCK_GUARD() {
  7384. CPUState *cpu_iter;
  7385. CPU_FOREACH(cpu_iter) {
  7386. cpus++;
  7387. }
  7388. }
  7389. g_string_printf(buf, "%d ", cpus);
  7390. } else if (i == 21) {
  7391. /* starttime */
  7392. g_string_printf(buf, "%" PRIu64 " ", ts->start_boottime);
  7393. } else if (i == 27) {
  7394. /* stack bottom */
  7395. g_string_printf(buf, TARGET_ABI_FMT_ld " ", ts->info->start_stack);
  7396. } else {
  7397. /* for the rest, there is MasterCard */
  7398. g_string_printf(buf, "0%c", i == 43 ? '\n' : ' ');
  7399. }
  7400. if (write(fd, buf->str, buf->len) != buf->len) {
  7401. return -1;
  7402. }
  7403. }
  7404. return 0;
  7405. }
  7406. static int open_self_auxv(CPUArchState *cpu_env, int fd)
  7407. {
  7408. CPUState *cpu = env_cpu(cpu_env);
  7409. TaskState *ts = get_task_state(cpu);
  7410. abi_ulong auxv = ts->info->saved_auxv;
  7411. abi_ulong len = ts->info->auxv_len;
  7412. char *ptr;
  7413. /*
  7414. * Auxiliary vector is stored in target process stack.
  7415. * read in whole auxv vector and copy it to file
  7416. */
  7417. ptr = lock_user(VERIFY_READ, auxv, len, 0);
  7418. if (ptr != NULL) {
  7419. while (len > 0) {
  7420. ssize_t r;
  7421. r = write(fd, ptr, len);
  7422. if (r <= 0) {
  7423. break;
  7424. }
  7425. len -= r;
  7426. ptr += r;
  7427. }
  7428. lseek(fd, 0, SEEK_SET);
  7429. unlock_user(ptr, auxv, len);
  7430. }
  7431. return 0;
  7432. }
  7433. static int is_proc_myself(const char *filename, const char *entry)
  7434. {
  7435. if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
  7436. filename += strlen("/proc/");
  7437. if (!strncmp(filename, "self/", strlen("self/"))) {
  7438. filename += strlen("self/");
  7439. } else if (*filename >= '1' && *filename <= '9') {
  7440. char myself[80];
  7441. snprintf(myself, sizeof(myself), "%d/", getpid());
  7442. if (!strncmp(filename, myself, strlen(myself))) {
  7443. filename += strlen(myself);
  7444. } else {
  7445. return 0;
  7446. }
  7447. } else {
  7448. return 0;
  7449. }
  7450. if (!strcmp(filename, entry)) {
  7451. return 1;
  7452. }
  7453. }
  7454. return 0;
  7455. }
  7456. static void excp_dump_file(FILE *logfile, CPUArchState *env,
  7457. const char *fmt, int code)
  7458. {
  7459. if (logfile) {
  7460. CPUState *cs = env_cpu(env);
  7461. fprintf(logfile, fmt, code);
  7462. fprintf(logfile, "Failing executable: %s\n", exec_path);
  7463. cpu_dump_state(cs, logfile, 0);
  7464. open_self_maps(env, fileno(logfile));
  7465. }
  7466. }
  7467. void target_exception_dump(CPUArchState *env, const char *fmt, int code)
  7468. {
  7469. /* dump to console */
  7470. excp_dump_file(stderr, env, fmt, code);
  7471. /* dump to log file */
  7472. if (qemu_log_separate()) {
  7473. FILE *logfile = qemu_log_trylock();
  7474. excp_dump_file(logfile, env, fmt, code);
  7475. qemu_log_unlock(logfile);
  7476. }
  7477. }
  7478. #include "target_proc.h"
  7479. #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN || \
  7480. defined(HAVE_ARCH_PROC_CPUINFO) || \
  7481. defined(HAVE_ARCH_PROC_HARDWARE)
  7482. static int is_proc(const char *filename, const char *entry)
  7483. {
  7484. return strcmp(filename, entry) == 0;
  7485. }
  7486. #endif
  7487. #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
  7488. static int open_net_route(CPUArchState *cpu_env, int fd)
  7489. {
  7490. FILE *fp;
  7491. char *line = NULL;
  7492. size_t len = 0;
  7493. ssize_t read;
  7494. fp = fopen("/proc/net/route", "r");
  7495. if (fp == NULL) {
  7496. return -1;
  7497. }
  7498. /* read header */
  7499. read = getline(&line, &len, fp);
  7500. dprintf(fd, "%s", line);
  7501. /* read routes */
  7502. while ((read = getline(&line, &len, fp)) != -1) {
  7503. char iface[16];
  7504. uint32_t dest, gw, mask;
  7505. unsigned int flags, refcnt, use, metric, mtu, window, irtt;
  7506. int fields;
  7507. fields = sscanf(line,
  7508. "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
  7509. iface, &dest, &gw, &flags, &refcnt, &use, &metric,
  7510. &mask, &mtu, &window, &irtt);
  7511. if (fields != 11) {
  7512. continue;
  7513. }
  7514. dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
  7515. iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
  7516. metric, tswap32(mask), mtu, window, irtt);
  7517. }
  7518. free(line);
  7519. fclose(fp);
  7520. return 0;
  7521. }
  7522. #endif
  7523. int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *fname,
  7524. int flags, mode_t mode, bool safe)
  7525. {
  7526. g_autofree char *proc_name = NULL;
  7527. const char *pathname;
  7528. struct fake_open {
  7529. const char *filename;
  7530. int (*fill)(CPUArchState *cpu_env, int fd);
  7531. int (*cmp)(const char *s1, const char *s2);
  7532. };
  7533. const struct fake_open *fake_open;
  7534. static const struct fake_open fakes[] = {
  7535. { "maps", open_self_maps, is_proc_myself },
  7536. { "smaps", open_self_smaps, is_proc_myself },
  7537. { "stat", open_self_stat, is_proc_myself },
  7538. { "auxv", open_self_auxv, is_proc_myself },
  7539. { "cmdline", open_self_cmdline, is_proc_myself },
  7540. #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
  7541. { "/proc/net/route", open_net_route, is_proc },
  7542. #endif
  7543. #if defined(HAVE_ARCH_PROC_CPUINFO)
  7544. { "/proc/cpuinfo", open_cpuinfo, is_proc },
  7545. #endif
  7546. #if defined(HAVE_ARCH_PROC_HARDWARE)
  7547. { "/proc/hardware", open_hardware, is_proc },
  7548. #endif
  7549. { NULL, NULL, NULL }
  7550. };
  7551. /* if this is a file from /proc/ filesystem, expand full name */
  7552. proc_name = realpath(fname, NULL);
  7553. if (proc_name && strncmp(proc_name, "/proc/", 6) == 0) {
  7554. pathname = proc_name;
  7555. } else {
  7556. pathname = fname;
  7557. }
  7558. if (is_proc_myself(pathname, "exe")) {
  7559. if (safe) {
  7560. return safe_openat(dirfd, exec_path, flags, mode);
  7561. } else {
  7562. return openat(dirfd, exec_path, flags, mode);
  7563. }
  7564. }
  7565. for (fake_open = fakes; fake_open->filename; fake_open++) {
  7566. if (fake_open->cmp(pathname, fake_open->filename)) {
  7567. break;
  7568. }
  7569. }
  7570. if (fake_open->filename) {
  7571. const char *tmpdir;
  7572. char filename[PATH_MAX];
  7573. int fd, r;
  7574. fd = memfd_create("qemu-open", 0);
  7575. if (fd < 0) {
  7576. if (errno != ENOSYS) {
  7577. return fd;
  7578. }
  7579. /* create temporary file to map stat to */
  7580. tmpdir = getenv("TMPDIR");
  7581. if (!tmpdir)
  7582. tmpdir = "/tmp";
  7583. snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
  7584. fd = mkstemp(filename);
  7585. if (fd < 0) {
  7586. return fd;
  7587. }
  7588. unlink(filename);
  7589. }
  7590. if ((r = fake_open->fill(cpu_env, fd))) {
  7591. int e = errno;
  7592. close(fd);
  7593. errno = e;
  7594. return r;
  7595. }
  7596. lseek(fd, 0, SEEK_SET);
  7597. return fd;
  7598. }
  7599. if (safe) {
  7600. return safe_openat(dirfd, path(pathname), flags, mode);
  7601. } else {
  7602. return openat(dirfd, path(pathname), flags, mode);
  7603. }
  7604. }
  7605. ssize_t do_guest_readlink(const char *pathname, char *buf, size_t bufsiz)
  7606. {
  7607. ssize_t ret;
  7608. if (!pathname || !buf) {
  7609. errno = EFAULT;
  7610. return -1;
  7611. }
  7612. if (!bufsiz) {
  7613. /* Short circuit this for the magic exe check. */
  7614. errno = EINVAL;
  7615. return -1;
  7616. }
  7617. if (is_proc_myself((const char *)pathname, "exe")) {
  7618. /*
  7619. * Don't worry about sign mismatch as earlier mapping
  7620. * logic would have thrown a bad address error.
  7621. */
  7622. ret = MIN(strlen(exec_path), bufsiz);
  7623. /* We cannot NUL terminate the string. */
  7624. memcpy(buf, exec_path, ret);
  7625. } else {
  7626. ret = readlink(path(pathname), buf, bufsiz);
  7627. }
  7628. return ret;
  7629. }
  7630. static int do_execv(CPUArchState *cpu_env, int dirfd,
  7631. abi_long pathname, abi_long guest_argp,
  7632. abi_long guest_envp, int flags, bool is_execveat)
  7633. {
  7634. int ret;
  7635. char **argp, **envp;
  7636. int argc, envc;
  7637. abi_ulong gp;
  7638. abi_ulong addr;
  7639. char **q;
  7640. void *p;
  7641. argc = 0;
  7642. for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
  7643. if (get_user_ual(addr, gp)) {
  7644. return -TARGET_EFAULT;
  7645. }
  7646. if (!addr) {
  7647. break;
  7648. }
  7649. argc++;
  7650. }
  7651. envc = 0;
  7652. for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
  7653. if (get_user_ual(addr, gp)) {
  7654. return -TARGET_EFAULT;
  7655. }
  7656. if (!addr) {
  7657. break;
  7658. }
  7659. envc++;
  7660. }
  7661. argp = g_new0(char *, argc + 1);
  7662. envp = g_new0(char *, envc + 1);
  7663. for (gp = guest_argp, q = argp; gp; gp += sizeof(abi_ulong), q++) {
  7664. if (get_user_ual(addr, gp)) {
  7665. goto execve_efault;
  7666. }
  7667. if (!addr) {
  7668. break;
  7669. }
  7670. *q = lock_user_string(addr);
  7671. if (!*q) {
  7672. goto execve_efault;
  7673. }
  7674. }
  7675. *q = NULL;
  7676. for (gp = guest_envp, q = envp; gp; gp += sizeof(abi_ulong), q++) {
  7677. if (get_user_ual(addr, gp)) {
  7678. goto execve_efault;
  7679. }
  7680. if (!addr) {
  7681. break;
  7682. }
  7683. *q = lock_user_string(addr);
  7684. if (!*q) {
  7685. goto execve_efault;
  7686. }
  7687. }
  7688. *q = NULL;
  7689. /*
  7690. * Although execve() is not an interruptible syscall it is
  7691. * a special case where we must use the safe_syscall wrapper:
  7692. * if we allow a signal to happen before we make the host
  7693. * syscall then we will 'lose' it, because at the point of
  7694. * execve the process leaves QEMU's control. So we use the
  7695. * safe syscall wrapper to ensure that we either take the
  7696. * signal as a guest signal, or else it does not happen
  7697. * before the execve completes and makes it the other
  7698. * program's problem.
  7699. */
  7700. p = lock_user_string(pathname);
  7701. if (!p) {
  7702. goto execve_efault;
  7703. }
  7704. const char *exe = p;
  7705. if (is_proc_myself(p, "exe")) {
  7706. exe = exec_path;
  7707. }
  7708. ret = is_execveat
  7709. ? safe_execveat(dirfd, exe, argp, envp, flags)
  7710. : safe_execve(exe, argp, envp);
  7711. ret = get_errno(ret);
  7712. unlock_user(p, pathname, 0);
  7713. goto execve_end;
  7714. execve_efault:
  7715. ret = -TARGET_EFAULT;
  7716. execve_end:
  7717. for (gp = guest_argp, q = argp; *q; gp += sizeof(abi_ulong), q++) {
  7718. if (get_user_ual(addr, gp) || !addr) {
  7719. break;
  7720. }
  7721. unlock_user(*q, addr, 0);
  7722. }
  7723. for (gp = guest_envp, q = envp; *q; gp += sizeof(abi_ulong), q++) {
  7724. if (get_user_ual(addr, gp) || !addr) {
  7725. break;
  7726. }
  7727. unlock_user(*q, addr, 0);
  7728. }
  7729. g_free(argp);
  7730. g_free(envp);
  7731. return ret;
  7732. }
  7733. #define TIMER_MAGIC 0x0caf0000
  7734. #define TIMER_MAGIC_MASK 0xffff0000
  7735. /* Convert QEMU provided timer ID back to internal 16bit index format */
  7736. static target_timer_t get_timer_id(abi_long arg)
  7737. {
  7738. target_timer_t timerid = arg;
  7739. if ((timerid & TIMER_MAGIC_MASK) != TIMER_MAGIC) {
  7740. return -TARGET_EINVAL;
  7741. }
  7742. timerid &= 0xffff;
  7743. if (timerid >= ARRAY_SIZE(g_posix_timers)) {
  7744. return -TARGET_EINVAL;
  7745. }
  7746. return timerid;
  7747. }
  7748. static int target_to_host_cpu_mask(unsigned long *host_mask,
  7749. size_t host_size,
  7750. abi_ulong target_addr,
  7751. size_t target_size)
  7752. {
  7753. unsigned target_bits = sizeof(abi_ulong) * 8;
  7754. unsigned host_bits = sizeof(*host_mask) * 8;
  7755. abi_ulong *target_mask;
  7756. unsigned i, j;
  7757. assert(host_size >= target_size);
  7758. target_mask = lock_user(VERIFY_READ, target_addr, target_size, 1);
  7759. if (!target_mask) {
  7760. return -TARGET_EFAULT;
  7761. }
  7762. memset(host_mask, 0, host_size);
  7763. for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
  7764. unsigned bit = i * target_bits;
  7765. abi_ulong val;
  7766. __get_user(val, &target_mask[i]);
  7767. for (j = 0; j < target_bits; j++, bit++) {
  7768. if (val & (1UL << j)) {
  7769. host_mask[bit / host_bits] |= 1UL << (bit % host_bits);
  7770. }
  7771. }
  7772. }
  7773. unlock_user(target_mask, target_addr, 0);
  7774. return 0;
  7775. }
  7776. static int host_to_target_cpu_mask(const unsigned long *host_mask,
  7777. size_t host_size,
  7778. abi_ulong target_addr,
  7779. size_t target_size)
  7780. {
  7781. unsigned target_bits = sizeof(abi_ulong) * 8;
  7782. unsigned host_bits = sizeof(*host_mask) * 8;
  7783. abi_ulong *target_mask;
  7784. unsigned i, j;
  7785. assert(host_size >= target_size);
  7786. target_mask = lock_user(VERIFY_WRITE, target_addr, target_size, 0);
  7787. if (!target_mask) {
  7788. return -TARGET_EFAULT;
  7789. }
  7790. for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
  7791. unsigned bit = i * target_bits;
  7792. abi_ulong val = 0;
  7793. for (j = 0; j < target_bits; j++, bit++) {
  7794. if (host_mask[bit / host_bits] & (1UL << (bit % host_bits))) {
  7795. val |= 1UL << j;
  7796. }
  7797. }
  7798. __put_user(val, &target_mask[i]);
  7799. }
  7800. unlock_user(target_mask, target_addr, target_size);
  7801. return 0;
  7802. }
  7803. #ifdef TARGET_NR_getdents
  7804. static int do_getdents(abi_long dirfd, abi_long arg2, abi_long count)
  7805. {
  7806. g_autofree void *hdirp = NULL;
  7807. void *tdirp;
  7808. int hlen, hoff, toff;
  7809. int hreclen, treclen;
  7810. off64_t prev_diroff = 0;
  7811. hdirp = g_try_malloc(count);
  7812. if (!hdirp) {
  7813. return -TARGET_ENOMEM;
  7814. }
  7815. #ifdef EMULATE_GETDENTS_WITH_GETDENTS
  7816. hlen = sys_getdents(dirfd, hdirp, count);
  7817. #else
  7818. hlen = sys_getdents64(dirfd, hdirp, count);
  7819. #endif
  7820. hlen = get_errno(hlen);
  7821. if (is_error(hlen)) {
  7822. return hlen;
  7823. }
  7824. tdirp = lock_user(VERIFY_WRITE, arg2, count, 0);
  7825. if (!tdirp) {
  7826. return -TARGET_EFAULT;
  7827. }
  7828. for (hoff = toff = 0; hoff < hlen; hoff += hreclen, toff += treclen) {
  7829. #ifdef EMULATE_GETDENTS_WITH_GETDENTS
  7830. struct linux_dirent *hde = hdirp + hoff;
  7831. #else
  7832. struct linux_dirent64 *hde = hdirp + hoff;
  7833. #endif
  7834. struct target_dirent *tde = tdirp + toff;
  7835. int namelen;
  7836. uint8_t type;
  7837. namelen = strlen(hde->d_name);
  7838. hreclen = hde->d_reclen;
  7839. treclen = offsetof(struct target_dirent, d_name) + namelen + 2;
  7840. treclen = QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent));
  7841. if (toff + treclen > count) {
  7842. /*
  7843. * If the host struct is smaller than the target struct, or
  7844. * requires less alignment and thus packs into less space,
  7845. * then the host can return more entries than we can pass
  7846. * on to the guest.
  7847. */
  7848. if (toff == 0) {
  7849. toff = -TARGET_EINVAL; /* result buffer is too small */
  7850. break;
  7851. }
  7852. /*
  7853. * Return what we have, resetting the file pointer to the
  7854. * location of the first record not returned.
  7855. */
  7856. lseek64(dirfd, prev_diroff, SEEK_SET);
  7857. break;
  7858. }
  7859. prev_diroff = hde->d_off;
  7860. tde->d_ino = tswapal(hde->d_ino);
  7861. tde->d_off = tswapal(hde->d_off);
  7862. tde->d_reclen = tswap16(treclen);
  7863. memcpy(tde->d_name, hde->d_name, namelen + 1);
  7864. /*
  7865. * The getdents type is in what was formerly a padding byte at the
  7866. * end of the structure.
  7867. */
  7868. #ifdef EMULATE_GETDENTS_WITH_GETDENTS
  7869. type = *((uint8_t *)hde + hreclen - 1);
  7870. #else
  7871. type = hde->d_type;
  7872. #endif
  7873. *((uint8_t *)tde + treclen - 1) = type;
  7874. }
  7875. unlock_user(tdirp, arg2, toff);
  7876. return toff;
  7877. }
  7878. #endif /* TARGET_NR_getdents */
  7879. #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
  7880. static int do_getdents64(abi_long dirfd, abi_long arg2, abi_long count)
  7881. {
  7882. g_autofree void *hdirp = NULL;
  7883. void *tdirp;
  7884. int hlen, hoff, toff;
  7885. int hreclen, treclen;
  7886. off64_t prev_diroff = 0;
  7887. hdirp = g_try_malloc(count);
  7888. if (!hdirp) {
  7889. return -TARGET_ENOMEM;
  7890. }
  7891. hlen = get_errno(sys_getdents64(dirfd, hdirp, count));
  7892. if (is_error(hlen)) {
  7893. return hlen;
  7894. }
  7895. tdirp = lock_user(VERIFY_WRITE, arg2, count, 0);
  7896. if (!tdirp) {
  7897. return -TARGET_EFAULT;
  7898. }
  7899. for (hoff = toff = 0; hoff < hlen; hoff += hreclen, toff += treclen) {
  7900. struct linux_dirent64 *hde = hdirp + hoff;
  7901. struct target_dirent64 *tde = tdirp + toff;
  7902. int namelen;
  7903. namelen = strlen(hde->d_name) + 1;
  7904. hreclen = hde->d_reclen;
  7905. treclen = offsetof(struct target_dirent64, d_name) + namelen;
  7906. treclen = QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent64));
  7907. if (toff + treclen > count) {
  7908. /*
  7909. * If the host struct is smaller than the target struct, or
  7910. * requires less alignment and thus packs into less space,
  7911. * then the host can return more entries than we can pass
  7912. * on to the guest.
  7913. */
  7914. if (toff == 0) {
  7915. toff = -TARGET_EINVAL; /* result buffer is too small */
  7916. break;
  7917. }
  7918. /*
  7919. * Return what we have, resetting the file pointer to the
  7920. * location of the first record not returned.
  7921. */
  7922. lseek64(dirfd, prev_diroff, SEEK_SET);
  7923. break;
  7924. }
  7925. prev_diroff = hde->d_off;
  7926. tde->d_ino = tswap64(hde->d_ino);
  7927. tde->d_off = tswap64(hde->d_off);
  7928. tde->d_reclen = tswap16(treclen);
  7929. tde->d_type = hde->d_type;
  7930. memcpy(tde->d_name, hde->d_name, namelen);
  7931. }
  7932. unlock_user(tdirp, arg2, toff);
  7933. return toff;
  7934. }
  7935. #endif /* TARGET_NR_getdents64 */
  7936. #if defined(TARGET_NR_riscv_hwprobe)
  7937. #define RISCV_HWPROBE_KEY_MVENDORID 0
  7938. #define RISCV_HWPROBE_KEY_MARCHID 1
  7939. #define RISCV_HWPROBE_KEY_MIMPID 2
  7940. #define RISCV_HWPROBE_KEY_BASE_BEHAVIOR 3
  7941. #define RISCV_HWPROBE_BASE_BEHAVIOR_IMA (1 << 0)
  7942. #define RISCV_HWPROBE_KEY_IMA_EXT_0 4
  7943. #define RISCV_HWPROBE_IMA_FD (1 << 0)
  7944. #define RISCV_HWPROBE_IMA_C (1 << 1)
  7945. #define RISCV_HWPROBE_IMA_V (1 << 2)
  7946. #define RISCV_HWPROBE_EXT_ZBA (1 << 3)
  7947. #define RISCV_HWPROBE_EXT_ZBB (1 << 4)
  7948. #define RISCV_HWPROBE_EXT_ZBS (1 << 5)
  7949. #define RISCV_HWPROBE_EXT_ZICBOZ (1 << 6)
  7950. #define RISCV_HWPROBE_EXT_ZBC (1 << 7)
  7951. #define RISCV_HWPROBE_EXT_ZBKB (1 << 8)
  7952. #define RISCV_HWPROBE_EXT_ZBKC (1 << 9)
  7953. #define RISCV_HWPROBE_EXT_ZBKX (1 << 10)
  7954. #define RISCV_HWPROBE_EXT_ZKND (1 << 11)
  7955. #define RISCV_HWPROBE_EXT_ZKNE (1 << 12)
  7956. #define RISCV_HWPROBE_EXT_ZKNH (1 << 13)
  7957. #define RISCV_HWPROBE_EXT_ZKSED (1 << 14)
  7958. #define RISCV_HWPROBE_EXT_ZKSH (1 << 15)
  7959. #define RISCV_HWPROBE_EXT_ZKT (1 << 16)
  7960. #define RISCV_HWPROBE_EXT_ZVBB (1 << 17)
  7961. #define RISCV_HWPROBE_EXT_ZVBC (1 << 18)
  7962. #define RISCV_HWPROBE_EXT_ZVKB (1 << 19)
  7963. #define RISCV_HWPROBE_EXT_ZVKG (1 << 20)
  7964. #define RISCV_HWPROBE_EXT_ZVKNED (1 << 21)
  7965. #define RISCV_HWPROBE_EXT_ZVKNHA (1 << 22)
  7966. #define RISCV_HWPROBE_EXT_ZVKNHB (1 << 23)
  7967. #define RISCV_HWPROBE_EXT_ZVKSED (1 << 24)
  7968. #define RISCV_HWPROBE_EXT_ZVKSH (1 << 25)
  7969. #define RISCV_HWPROBE_EXT_ZVKT (1 << 26)
  7970. #define RISCV_HWPROBE_EXT_ZFH (1 << 27)
  7971. #define RISCV_HWPROBE_EXT_ZFHMIN (1 << 28)
  7972. #define RISCV_HWPROBE_EXT_ZIHINTNTL (1 << 29)
  7973. #define RISCV_HWPROBE_EXT_ZVFH (1 << 30)
  7974. #define RISCV_HWPROBE_EXT_ZVFHMIN (1ULL << 31)
  7975. #define RISCV_HWPROBE_EXT_ZFA (1ULL << 32)
  7976. #define RISCV_HWPROBE_EXT_ZTSO (1ULL << 33)
  7977. #define RISCV_HWPROBE_EXT_ZACAS (1ULL << 34)
  7978. #define RISCV_HWPROBE_EXT_ZICOND (1ULL << 35)
  7979. #define RISCV_HWPROBE_KEY_CPUPERF_0 5
  7980. #define RISCV_HWPROBE_MISALIGNED_UNKNOWN (0 << 0)
  7981. #define RISCV_HWPROBE_MISALIGNED_EMULATED (1 << 0)
  7982. #define RISCV_HWPROBE_MISALIGNED_SLOW (2 << 0)
  7983. #define RISCV_HWPROBE_MISALIGNED_FAST (3 << 0)
  7984. #define RISCV_HWPROBE_MISALIGNED_UNSUPPORTED (4 << 0)
  7985. #define RISCV_HWPROBE_MISALIGNED_MASK (7 << 0)
  7986. #define RISCV_HWPROBE_KEY_ZICBOZ_BLOCK_SIZE 6
  7987. struct riscv_hwprobe {
  7988. abi_llong key;
  7989. abi_ullong value;
  7990. };
  7991. static void risc_hwprobe_fill_pairs(CPURISCVState *env,
  7992. struct riscv_hwprobe *pair,
  7993. size_t pair_count)
  7994. {
  7995. const RISCVCPUConfig *cfg = riscv_cpu_cfg(env);
  7996. for (; pair_count > 0; pair_count--, pair++) {
  7997. abi_llong key;
  7998. abi_ullong value;
  7999. __put_user(0, &pair->value);
  8000. __get_user(key, &pair->key);
  8001. switch (key) {
  8002. case RISCV_HWPROBE_KEY_MVENDORID:
  8003. __put_user(cfg->mvendorid, &pair->value);
  8004. break;
  8005. case RISCV_HWPROBE_KEY_MARCHID:
  8006. __put_user(cfg->marchid, &pair->value);
  8007. break;
  8008. case RISCV_HWPROBE_KEY_MIMPID:
  8009. __put_user(cfg->mimpid, &pair->value);
  8010. break;
  8011. case RISCV_HWPROBE_KEY_BASE_BEHAVIOR:
  8012. value = riscv_has_ext(env, RVI) &&
  8013. riscv_has_ext(env, RVM) &&
  8014. riscv_has_ext(env, RVA) ?
  8015. RISCV_HWPROBE_BASE_BEHAVIOR_IMA : 0;
  8016. __put_user(value, &pair->value);
  8017. break;
  8018. case RISCV_HWPROBE_KEY_IMA_EXT_0:
  8019. value = riscv_has_ext(env, RVF) &&
  8020. riscv_has_ext(env, RVD) ?
  8021. RISCV_HWPROBE_IMA_FD : 0;
  8022. value |= riscv_has_ext(env, RVC) ?
  8023. RISCV_HWPROBE_IMA_C : 0;
  8024. value |= riscv_has_ext(env, RVV) ?
  8025. RISCV_HWPROBE_IMA_V : 0;
  8026. value |= cfg->ext_zba ?
  8027. RISCV_HWPROBE_EXT_ZBA : 0;
  8028. value |= cfg->ext_zbb ?
  8029. RISCV_HWPROBE_EXT_ZBB : 0;
  8030. value |= cfg->ext_zbs ?
  8031. RISCV_HWPROBE_EXT_ZBS : 0;
  8032. value |= cfg->ext_zicboz ?
  8033. RISCV_HWPROBE_EXT_ZICBOZ : 0;
  8034. value |= cfg->ext_zbc ?
  8035. RISCV_HWPROBE_EXT_ZBC : 0;
  8036. value |= cfg->ext_zbkb ?
  8037. RISCV_HWPROBE_EXT_ZBKB : 0;
  8038. value |= cfg->ext_zbkc ?
  8039. RISCV_HWPROBE_EXT_ZBKC : 0;
  8040. value |= cfg->ext_zbkx ?
  8041. RISCV_HWPROBE_EXT_ZBKX : 0;
  8042. value |= cfg->ext_zknd ?
  8043. RISCV_HWPROBE_EXT_ZKND : 0;
  8044. value |= cfg->ext_zkne ?
  8045. RISCV_HWPROBE_EXT_ZKNE : 0;
  8046. value |= cfg->ext_zknh ?
  8047. RISCV_HWPROBE_EXT_ZKNH : 0;
  8048. value |= cfg->ext_zksed ?
  8049. RISCV_HWPROBE_EXT_ZKSED : 0;
  8050. value |= cfg->ext_zksh ?
  8051. RISCV_HWPROBE_EXT_ZKSH : 0;
  8052. value |= cfg->ext_zkt ?
  8053. RISCV_HWPROBE_EXT_ZKT : 0;
  8054. value |= cfg->ext_zvbb ?
  8055. RISCV_HWPROBE_EXT_ZVBB : 0;
  8056. value |= cfg->ext_zvbc ?
  8057. RISCV_HWPROBE_EXT_ZVBC : 0;
  8058. value |= cfg->ext_zvkb ?
  8059. RISCV_HWPROBE_EXT_ZVKB : 0;
  8060. value |= cfg->ext_zvkg ?
  8061. RISCV_HWPROBE_EXT_ZVKG : 0;
  8062. value |= cfg->ext_zvkned ?
  8063. RISCV_HWPROBE_EXT_ZVKNED : 0;
  8064. value |= cfg->ext_zvknha ?
  8065. RISCV_HWPROBE_EXT_ZVKNHA : 0;
  8066. value |= cfg->ext_zvknhb ?
  8067. RISCV_HWPROBE_EXT_ZVKNHB : 0;
  8068. value |= cfg->ext_zvksed ?
  8069. RISCV_HWPROBE_EXT_ZVKSED : 0;
  8070. value |= cfg->ext_zvksh ?
  8071. RISCV_HWPROBE_EXT_ZVKSH : 0;
  8072. value |= cfg->ext_zvkt ?
  8073. RISCV_HWPROBE_EXT_ZVKT : 0;
  8074. value |= cfg->ext_zfh ?
  8075. RISCV_HWPROBE_EXT_ZFH : 0;
  8076. value |= cfg->ext_zfhmin ?
  8077. RISCV_HWPROBE_EXT_ZFHMIN : 0;
  8078. value |= cfg->ext_zihintntl ?
  8079. RISCV_HWPROBE_EXT_ZIHINTNTL : 0;
  8080. value |= cfg->ext_zvfh ?
  8081. RISCV_HWPROBE_EXT_ZVFH : 0;
  8082. value |= cfg->ext_zvfhmin ?
  8083. RISCV_HWPROBE_EXT_ZVFHMIN : 0;
  8084. value |= cfg->ext_zfa ?
  8085. RISCV_HWPROBE_EXT_ZFA : 0;
  8086. value |= cfg->ext_ztso ?
  8087. RISCV_HWPROBE_EXT_ZTSO : 0;
  8088. value |= cfg->ext_zacas ?
  8089. RISCV_HWPROBE_EXT_ZACAS : 0;
  8090. value |= cfg->ext_zicond ?
  8091. RISCV_HWPROBE_EXT_ZICOND : 0;
  8092. __put_user(value, &pair->value);
  8093. break;
  8094. case RISCV_HWPROBE_KEY_CPUPERF_0:
  8095. __put_user(RISCV_HWPROBE_MISALIGNED_FAST, &pair->value);
  8096. break;
  8097. case RISCV_HWPROBE_KEY_ZICBOZ_BLOCK_SIZE:
  8098. value = cfg->ext_zicboz ? cfg->cboz_blocksize : 0;
  8099. __put_user(value, &pair->value);
  8100. break;
  8101. default:
  8102. __put_user(-1, &pair->key);
  8103. break;
  8104. }
  8105. }
  8106. }
  8107. static int cpu_set_valid(abi_long arg3, abi_long arg4)
  8108. {
  8109. int ret, i, tmp;
  8110. size_t host_mask_size, target_mask_size;
  8111. unsigned long *host_mask;
  8112. /*
  8113. * cpu_set_t represent CPU masks as bit masks of type unsigned long *.
  8114. * arg3 contains the cpu count.
  8115. */
  8116. tmp = (8 * sizeof(abi_ulong));
  8117. target_mask_size = ((arg3 + tmp - 1) / tmp) * sizeof(abi_ulong);
  8118. host_mask_size = (target_mask_size + (sizeof(*host_mask) - 1)) &
  8119. ~(sizeof(*host_mask) - 1);
  8120. host_mask = alloca(host_mask_size);
  8121. ret = target_to_host_cpu_mask(host_mask, host_mask_size,
  8122. arg4, target_mask_size);
  8123. if (ret != 0) {
  8124. return ret;
  8125. }
  8126. for (i = 0 ; i < host_mask_size / sizeof(*host_mask); i++) {
  8127. if (host_mask[i] != 0) {
  8128. return 0;
  8129. }
  8130. }
  8131. return -TARGET_EINVAL;
  8132. }
  8133. static abi_long do_riscv_hwprobe(CPUArchState *cpu_env, abi_long arg1,
  8134. abi_long arg2, abi_long arg3,
  8135. abi_long arg4, abi_long arg5)
  8136. {
  8137. int ret;
  8138. struct riscv_hwprobe *host_pairs;
  8139. /* flags must be 0 */
  8140. if (arg5 != 0) {
  8141. return -TARGET_EINVAL;
  8142. }
  8143. /* check cpu_set */
  8144. if (arg3 != 0) {
  8145. ret = cpu_set_valid(arg3, arg4);
  8146. if (ret != 0) {
  8147. return ret;
  8148. }
  8149. } else if (arg4 != 0) {
  8150. return -TARGET_EINVAL;
  8151. }
  8152. /* no pairs */
  8153. if (arg2 == 0) {
  8154. return 0;
  8155. }
  8156. host_pairs = lock_user(VERIFY_WRITE, arg1,
  8157. sizeof(*host_pairs) * (size_t)arg2, 0);
  8158. if (host_pairs == NULL) {
  8159. return -TARGET_EFAULT;
  8160. }
  8161. risc_hwprobe_fill_pairs(cpu_env, host_pairs, arg2);
  8162. unlock_user(host_pairs, arg1, sizeof(*host_pairs) * (size_t)arg2);
  8163. return 0;
  8164. }
  8165. #endif /* TARGET_NR_riscv_hwprobe */
  8166. #if defined(TARGET_NR_pivot_root) && defined(__NR_pivot_root)
  8167. _syscall2(int, pivot_root, const char *, new_root, const char *, put_old)
  8168. #endif
  8169. #if defined(TARGET_NR_open_tree) && defined(__NR_open_tree)
  8170. #define __NR_sys_open_tree __NR_open_tree
  8171. _syscall3(int, sys_open_tree, int, __dfd, const char *, __filename,
  8172. unsigned int, __flags)
  8173. #endif
  8174. #if defined(TARGET_NR_move_mount) && defined(__NR_move_mount)
  8175. #define __NR_sys_move_mount __NR_move_mount
  8176. _syscall5(int, sys_move_mount, int, __from_dfd, const char *, __from_pathname,
  8177. int, __to_dfd, const char *, __to_pathname, unsigned int, flag)
  8178. #endif
  8179. /* This is an internal helper for do_syscall so that it is easier
  8180. * to have a single return point, so that actions, such as logging
  8181. * of syscall results, can be performed.
  8182. * All errnos that do_syscall() returns must be -TARGET_<errcode>.
  8183. */
  8184. static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
  8185. abi_long arg2, abi_long arg3, abi_long arg4,
  8186. abi_long arg5, abi_long arg6, abi_long arg7,
  8187. abi_long arg8)
  8188. {
  8189. CPUState *cpu = env_cpu(cpu_env);
  8190. abi_long ret;
  8191. #if defined(TARGET_NR_stat) || defined(TARGET_NR_stat64) \
  8192. || defined(TARGET_NR_lstat) || defined(TARGET_NR_lstat64) \
  8193. || defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64) \
  8194. || defined(TARGET_NR_statx)
  8195. struct stat st;
  8196. #endif
  8197. #if defined(TARGET_NR_statfs) || defined(TARGET_NR_statfs64) \
  8198. || defined(TARGET_NR_fstatfs)
  8199. struct statfs stfs;
  8200. #endif
  8201. void *p;
  8202. switch(num) {
  8203. case TARGET_NR_exit:
  8204. /* In old applications this may be used to implement _exit(2).
  8205. However in threaded applications it is used for thread termination,
  8206. and _exit_group is used for application termination.
  8207. Do thread termination if we have more then one thread. */
  8208. if (block_signals()) {
  8209. return -QEMU_ERESTARTSYS;
  8210. }
  8211. pthread_mutex_lock(&clone_lock);
  8212. if (CPU_NEXT(first_cpu)) {
  8213. TaskState *ts = get_task_state(cpu);
  8214. if (ts->child_tidptr) {
  8215. put_user_u32(0, ts->child_tidptr);
  8216. do_sys_futex(g2h(cpu, ts->child_tidptr),
  8217. FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
  8218. }
  8219. object_unparent(OBJECT(cpu));
  8220. object_unref(OBJECT(cpu));
  8221. /*
  8222. * At this point the CPU should be unrealized and removed
  8223. * from cpu lists. We can clean-up the rest of the thread
  8224. * data without the lock held.
  8225. */
  8226. pthread_mutex_unlock(&clone_lock);
  8227. thread_cpu = NULL;
  8228. g_free(ts);
  8229. rcu_unregister_thread();
  8230. pthread_exit(NULL);
  8231. }
  8232. pthread_mutex_unlock(&clone_lock);
  8233. preexit_cleanup(cpu_env, arg1);
  8234. _exit(arg1);
  8235. return 0; /* avoid warning */
  8236. case TARGET_NR_read:
  8237. if (arg2 == 0 && arg3 == 0) {
  8238. return get_errno(safe_read(arg1, 0, 0));
  8239. } else {
  8240. if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
  8241. return -TARGET_EFAULT;
  8242. ret = get_errno(safe_read(arg1, p, arg3));
  8243. if (ret >= 0 &&
  8244. fd_trans_host_to_target_data(arg1)) {
  8245. ret = fd_trans_host_to_target_data(arg1)(p, ret);
  8246. }
  8247. unlock_user(p, arg2, ret);
  8248. }
  8249. return ret;
  8250. case TARGET_NR_write:
  8251. if (arg2 == 0 && arg3 == 0) {
  8252. return get_errno(safe_write(arg1, 0, 0));
  8253. }
  8254. if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
  8255. return -TARGET_EFAULT;
  8256. if (fd_trans_target_to_host_data(arg1)) {
  8257. void *copy = g_malloc(arg3);
  8258. memcpy(copy, p, arg3);
  8259. ret = fd_trans_target_to_host_data(arg1)(copy, arg3);
  8260. if (ret >= 0) {
  8261. ret = get_errno(safe_write(arg1, copy, ret));
  8262. }
  8263. g_free(copy);
  8264. } else {
  8265. ret = get_errno(safe_write(arg1, p, arg3));
  8266. }
  8267. unlock_user(p, arg2, 0);
  8268. return ret;
  8269. #ifdef TARGET_NR_open
  8270. case TARGET_NR_open:
  8271. if (!(p = lock_user_string(arg1)))
  8272. return -TARGET_EFAULT;
  8273. ret = get_errno(do_guest_openat(cpu_env, AT_FDCWD, p,
  8274. target_to_host_bitmask(arg2, fcntl_flags_tbl),
  8275. arg3, true));
  8276. fd_trans_unregister(ret);
  8277. unlock_user(p, arg1, 0);
  8278. return ret;
  8279. #endif
  8280. case TARGET_NR_openat:
  8281. if (!(p = lock_user_string(arg2)))
  8282. return -TARGET_EFAULT;
  8283. ret = get_errno(do_guest_openat(cpu_env, arg1, p,
  8284. target_to_host_bitmask(arg3, fcntl_flags_tbl),
  8285. arg4, true));
  8286. fd_trans_unregister(ret);
  8287. unlock_user(p, arg2, 0);
  8288. return ret;
  8289. #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  8290. case TARGET_NR_name_to_handle_at:
  8291. ret = do_name_to_handle_at(arg1, arg2, arg3, arg4, arg5);
  8292. return ret;
  8293. #endif
  8294. #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  8295. case TARGET_NR_open_by_handle_at:
  8296. ret = do_open_by_handle_at(arg1, arg2, arg3);
  8297. fd_trans_unregister(ret);
  8298. return ret;
  8299. #endif
  8300. #if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
  8301. case TARGET_NR_pidfd_open:
  8302. return get_errno(pidfd_open(arg1, arg2));
  8303. #endif
  8304. #if defined(__NR_pidfd_send_signal) && defined(TARGET_NR_pidfd_send_signal)
  8305. case TARGET_NR_pidfd_send_signal:
  8306. {
  8307. siginfo_t uinfo, *puinfo;
  8308. if (arg3) {
  8309. p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1);
  8310. if (!p) {
  8311. return -TARGET_EFAULT;
  8312. }
  8313. target_to_host_siginfo(&uinfo, p);
  8314. unlock_user(p, arg3, 0);
  8315. puinfo = &uinfo;
  8316. } else {
  8317. puinfo = NULL;
  8318. }
  8319. ret = get_errno(pidfd_send_signal(arg1, target_to_host_signal(arg2),
  8320. puinfo, arg4));
  8321. }
  8322. return ret;
  8323. #endif
  8324. #if defined(__NR_pidfd_getfd) && defined(TARGET_NR_pidfd_getfd)
  8325. case TARGET_NR_pidfd_getfd:
  8326. return get_errno(pidfd_getfd(arg1, arg2, arg3));
  8327. #endif
  8328. case TARGET_NR_close:
  8329. fd_trans_unregister(arg1);
  8330. return get_errno(close(arg1));
  8331. #if defined(__NR_close_range) && defined(TARGET_NR_close_range)
  8332. case TARGET_NR_close_range:
  8333. ret = get_errno(sys_close_range(arg1, arg2, arg3));
  8334. if (ret == 0 && !(arg3 & CLOSE_RANGE_CLOEXEC)) {
  8335. abi_long fd, maxfd;
  8336. maxfd = MIN(arg2, target_fd_max);
  8337. for (fd = arg1; fd < maxfd; fd++) {
  8338. fd_trans_unregister(fd);
  8339. }
  8340. }
  8341. return ret;
  8342. #endif
  8343. case TARGET_NR_brk:
  8344. return do_brk(arg1);
  8345. #ifdef TARGET_NR_fork
  8346. case TARGET_NR_fork:
  8347. return get_errno(do_fork(cpu_env, TARGET_SIGCHLD, 0, 0, 0, 0));
  8348. #endif
  8349. #ifdef TARGET_NR_waitpid
  8350. case TARGET_NR_waitpid:
  8351. {
  8352. int status;
  8353. ret = get_errno(safe_wait4(arg1, &status, arg3, 0));
  8354. if (!is_error(ret) && arg2 && ret
  8355. && put_user_s32(host_to_target_waitstatus(status), arg2))
  8356. return -TARGET_EFAULT;
  8357. }
  8358. return ret;
  8359. #endif
  8360. #ifdef TARGET_NR_waitid
  8361. case TARGET_NR_waitid:
  8362. {
  8363. struct rusage ru;
  8364. siginfo_t info;
  8365. ret = get_errno(safe_waitid(arg1, arg2, (arg3 ? &info : NULL),
  8366. arg4, (arg5 ? &ru : NULL)));
  8367. if (!is_error(ret)) {
  8368. if (arg3) {
  8369. p = lock_user(VERIFY_WRITE, arg3,
  8370. sizeof(target_siginfo_t), 0);
  8371. if (!p) {
  8372. return -TARGET_EFAULT;
  8373. }
  8374. host_to_target_siginfo(p, &info);
  8375. unlock_user(p, arg3, sizeof(target_siginfo_t));
  8376. }
  8377. if (arg5 && host_to_target_rusage(arg5, &ru)) {
  8378. return -TARGET_EFAULT;
  8379. }
  8380. }
  8381. }
  8382. return ret;
  8383. #endif
  8384. #ifdef TARGET_NR_creat /* not on alpha */
  8385. case TARGET_NR_creat:
  8386. if (!(p = lock_user_string(arg1)))
  8387. return -TARGET_EFAULT;
  8388. ret = get_errno(creat(p, arg2));
  8389. fd_trans_unregister(ret);
  8390. unlock_user(p, arg1, 0);
  8391. return ret;
  8392. #endif
  8393. #ifdef TARGET_NR_link
  8394. case TARGET_NR_link:
  8395. {
  8396. void * p2;
  8397. p = lock_user_string(arg1);
  8398. p2 = lock_user_string(arg2);
  8399. if (!p || !p2)
  8400. ret = -TARGET_EFAULT;
  8401. else
  8402. ret = get_errno(link(p, p2));
  8403. unlock_user(p2, arg2, 0);
  8404. unlock_user(p, arg1, 0);
  8405. }
  8406. return ret;
  8407. #endif
  8408. #if defined(TARGET_NR_linkat)
  8409. case TARGET_NR_linkat:
  8410. {
  8411. void * p2 = NULL;
  8412. if (!arg2 || !arg4)
  8413. return -TARGET_EFAULT;
  8414. p = lock_user_string(arg2);
  8415. p2 = lock_user_string(arg4);
  8416. if (!p || !p2)
  8417. ret = -TARGET_EFAULT;
  8418. else
  8419. ret = get_errno(linkat(arg1, p, arg3, p2, arg5));
  8420. unlock_user(p, arg2, 0);
  8421. unlock_user(p2, arg4, 0);
  8422. }
  8423. return ret;
  8424. #endif
  8425. #ifdef TARGET_NR_unlink
  8426. case TARGET_NR_unlink:
  8427. if (!(p = lock_user_string(arg1)))
  8428. return -TARGET_EFAULT;
  8429. ret = get_errno(unlink(p));
  8430. unlock_user(p, arg1, 0);
  8431. return ret;
  8432. #endif
  8433. #if defined(TARGET_NR_unlinkat)
  8434. case TARGET_NR_unlinkat:
  8435. if (!(p = lock_user_string(arg2)))
  8436. return -TARGET_EFAULT;
  8437. ret = get_errno(unlinkat(arg1, p, arg3));
  8438. unlock_user(p, arg2, 0);
  8439. return ret;
  8440. #endif
  8441. case TARGET_NR_execveat:
  8442. return do_execv(cpu_env, arg1, arg2, arg3, arg4, arg5, true);
  8443. case TARGET_NR_execve:
  8444. return do_execv(cpu_env, AT_FDCWD, arg1, arg2, arg3, 0, false);
  8445. case TARGET_NR_chdir:
  8446. if (!(p = lock_user_string(arg1)))
  8447. return -TARGET_EFAULT;
  8448. ret = get_errno(chdir(p));
  8449. unlock_user(p, arg1, 0);
  8450. return ret;
  8451. #ifdef TARGET_NR_time
  8452. case TARGET_NR_time:
  8453. {
  8454. time_t host_time;
  8455. ret = get_errno(time(&host_time));
  8456. if (!is_error(ret)
  8457. && arg1
  8458. && put_user_sal(host_time, arg1))
  8459. return -TARGET_EFAULT;
  8460. }
  8461. return ret;
  8462. #endif
  8463. #ifdef TARGET_NR_mknod
  8464. case TARGET_NR_mknod:
  8465. if (!(p = lock_user_string(arg1)))
  8466. return -TARGET_EFAULT;
  8467. ret = get_errno(mknod(p, arg2, arg3));
  8468. unlock_user(p, arg1, 0);
  8469. return ret;
  8470. #endif
  8471. #if defined(TARGET_NR_mknodat)
  8472. case TARGET_NR_mknodat:
  8473. if (!(p = lock_user_string(arg2)))
  8474. return -TARGET_EFAULT;
  8475. ret = get_errno(mknodat(arg1, p, arg3, arg4));
  8476. unlock_user(p, arg2, 0);
  8477. return ret;
  8478. #endif
  8479. #ifdef TARGET_NR_chmod
  8480. case TARGET_NR_chmod:
  8481. if (!(p = lock_user_string(arg1)))
  8482. return -TARGET_EFAULT;
  8483. ret = get_errno(chmod(p, arg2));
  8484. unlock_user(p, arg1, 0);
  8485. return ret;
  8486. #endif
  8487. #ifdef TARGET_NR_lseek
  8488. case TARGET_NR_lseek:
  8489. return get_errno(lseek(arg1, arg2, arg3));
  8490. #endif
  8491. #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
  8492. /* Alpha specific */
  8493. case TARGET_NR_getxpid:
  8494. cpu_env->ir[IR_A4] = getppid();
  8495. return get_errno(getpid());
  8496. #endif
  8497. #ifdef TARGET_NR_getpid
  8498. case TARGET_NR_getpid:
  8499. return get_errno(getpid());
  8500. #endif
  8501. case TARGET_NR_mount:
  8502. {
  8503. /* need to look at the data field */
  8504. void *p2, *p3;
  8505. if (arg1) {
  8506. p = lock_user_string(arg1);
  8507. if (!p) {
  8508. return -TARGET_EFAULT;
  8509. }
  8510. } else {
  8511. p = NULL;
  8512. }
  8513. p2 = lock_user_string(arg2);
  8514. if (!p2) {
  8515. if (arg1) {
  8516. unlock_user(p, arg1, 0);
  8517. }
  8518. return -TARGET_EFAULT;
  8519. }
  8520. if (arg3) {
  8521. p3 = lock_user_string(arg3);
  8522. if (!p3) {
  8523. if (arg1) {
  8524. unlock_user(p, arg1, 0);
  8525. }
  8526. unlock_user(p2, arg2, 0);
  8527. return -TARGET_EFAULT;
  8528. }
  8529. } else {
  8530. p3 = NULL;
  8531. }
  8532. /* FIXME - arg5 should be locked, but it isn't clear how to
  8533. * do that since it's not guaranteed to be a NULL-terminated
  8534. * string.
  8535. */
  8536. if (!arg5) {
  8537. ret = mount(p, p2, p3, (unsigned long)arg4, NULL);
  8538. } else {
  8539. ret = mount(p, p2, p3, (unsigned long)arg4, g2h(cpu, arg5));
  8540. }
  8541. ret = get_errno(ret);
  8542. if (arg1) {
  8543. unlock_user(p, arg1, 0);
  8544. }
  8545. unlock_user(p2, arg2, 0);
  8546. if (arg3) {
  8547. unlock_user(p3, arg3, 0);
  8548. }
  8549. }
  8550. return ret;
  8551. #if defined(TARGET_NR_umount) || defined(TARGET_NR_oldumount)
  8552. #if defined(TARGET_NR_umount)
  8553. case TARGET_NR_umount:
  8554. #endif
  8555. #if defined(TARGET_NR_oldumount)
  8556. case TARGET_NR_oldumount:
  8557. #endif
  8558. if (!(p = lock_user_string(arg1)))
  8559. return -TARGET_EFAULT;
  8560. ret = get_errno(umount(p));
  8561. unlock_user(p, arg1, 0);
  8562. return ret;
  8563. #endif
  8564. #if defined(TARGET_NR_move_mount) && defined(__NR_move_mount)
  8565. case TARGET_NR_move_mount:
  8566. {
  8567. void *p2, *p4;
  8568. if (!arg2 || !arg4) {
  8569. return -TARGET_EFAULT;
  8570. }
  8571. p2 = lock_user_string(arg2);
  8572. if (!p2) {
  8573. return -TARGET_EFAULT;
  8574. }
  8575. p4 = lock_user_string(arg4);
  8576. if (!p4) {
  8577. unlock_user(p2, arg2, 0);
  8578. return -TARGET_EFAULT;
  8579. }
  8580. ret = get_errno(sys_move_mount(arg1, p2, arg3, p4, arg5));
  8581. unlock_user(p2, arg2, 0);
  8582. unlock_user(p4, arg4, 0);
  8583. return ret;
  8584. }
  8585. #endif
  8586. #if defined(TARGET_NR_open_tree) && defined(__NR_open_tree)
  8587. case TARGET_NR_open_tree:
  8588. {
  8589. void *p2;
  8590. int host_flags;
  8591. if (!arg2) {
  8592. return -TARGET_EFAULT;
  8593. }
  8594. p2 = lock_user_string(arg2);
  8595. if (!p2) {
  8596. return -TARGET_EFAULT;
  8597. }
  8598. host_flags = arg3 & ~TARGET_O_CLOEXEC;
  8599. if (arg3 & TARGET_O_CLOEXEC) {
  8600. host_flags |= O_CLOEXEC;
  8601. }
  8602. ret = get_errno(sys_open_tree(arg1, p2, host_flags));
  8603. unlock_user(p2, arg2, 0);
  8604. return ret;
  8605. }
  8606. #endif
  8607. #ifdef TARGET_NR_stime /* not on alpha */
  8608. case TARGET_NR_stime:
  8609. {
  8610. struct timespec ts;
  8611. ts.tv_nsec = 0;
  8612. if (get_user_sal(ts.tv_sec, arg1)) {
  8613. return -TARGET_EFAULT;
  8614. }
  8615. return get_errno(clock_settime(CLOCK_REALTIME, &ts));
  8616. }
  8617. #endif
  8618. #ifdef TARGET_NR_alarm /* not on alpha */
  8619. case TARGET_NR_alarm:
  8620. return alarm(arg1);
  8621. #endif
  8622. #ifdef TARGET_NR_pause /* not on alpha */
  8623. case TARGET_NR_pause:
  8624. if (!block_signals()) {
  8625. sigsuspend(&get_task_state(cpu)->signal_mask);
  8626. }
  8627. return -TARGET_EINTR;
  8628. #endif
  8629. #ifdef TARGET_NR_utime
  8630. case TARGET_NR_utime:
  8631. {
  8632. struct utimbuf tbuf, *host_tbuf;
  8633. struct target_utimbuf *target_tbuf;
  8634. if (arg2) {
  8635. if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
  8636. return -TARGET_EFAULT;
  8637. tbuf.actime = tswapal(target_tbuf->actime);
  8638. tbuf.modtime = tswapal(target_tbuf->modtime);
  8639. unlock_user_struct(target_tbuf, arg2, 0);
  8640. host_tbuf = &tbuf;
  8641. } else {
  8642. host_tbuf = NULL;
  8643. }
  8644. if (!(p = lock_user_string(arg1)))
  8645. return -TARGET_EFAULT;
  8646. ret = get_errno(utime(p, host_tbuf));
  8647. unlock_user(p, arg1, 0);
  8648. }
  8649. return ret;
  8650. #endif
  8651. #ifdef TARGET_NR_utimes
  8652. case TARGET_NR_utimes:
  8653. {
  8654. struct timeval *tvp, tv[2];
  8655. if (arg2) {
  8656. if (copy_from_user_timeval(&tv[0], arg2)
  8657. || copy_from_user_timeval(&tv[1],
  8658. arg2 + sizeof(struct target_timeval)))
  8659. return -TARGET_EFAULT;
  8660. tvp = tv;
  8661. } else {
  8662. tvp = NULL;
  8663. }
  8664. if (!(p = lock_user_string(arg1)))
  8665. return -TARGET_EFAULT;
  8666. ret = get_errno(utimes(p, tvp));
  8667. unlock_user(p, arg1, 0);
  8668. }
  8669. return ret;
  8670. #endif
  8671. #if defined(TARGET_NR_futimesat)
  8672. case TARGET_NR_futimesat:
  8673. {
  8674. struct timeval *tvp, tv[2];
  8675. if (arg3) {
  8676. if (copy_from_user_timeval(&tv[0], arg3)
  8677. || copy_from_user_timeval(&tv[1],
  8678. arg3 + sizeof(struct target_timeval)))
  8679. return -TARGET_EFAULT;
  8680. tvp = tv;
  8681. } else {
  8682. tvp = NULL;
  8683. }
  8684. if (!(p = lock_user_string(arg2))) {
  8685. return -TARGET_EFAULT;
  8686. }
  8687. ret = get_errno(futimesat(arg1, path(p), tvp));
  8688. unlock_user(p, arg2, 0);
  8689. }
  8690. return ret;
  8691. #endif
  8692. #ifdef TARGET_NR_access
  8693. case TARGET_NR_access:
  8694. if (!(p = lock_user_string(arg1))) {
  8695. return -TARGET_EFAULT;
  8696. }
  8697. ret = get_errno(access(path(p), arg2));
  8698. unlock_user(p, arg1, 0);
  8699. return ret;
  8700. #endif
  8701. #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
  8702. case TARGET_NR_faccessat:
  8703. if (!(p = lock_user_string(arg2))) {
  8704. return -TARGET_EFAULT;
  8705. }
  8706. ret = get_errno(faccessat(arg1, p, arg3, 0));
  8707. unlock_user(p, arg2, 0);
  8708. return ret;
  8709. #endif
  8710. #if defined(TARGET_NR_faccessat2)
  8711. case TARGET_NR_faccessat2:
  8712. if (!(p = lock_user_string(arg2))) {
  8713. return -TARGET_EFAULT;
  8714. }
  8715. ret = get_errno(faccessat(arg1, p, arg3, arg4));
  8716. unlock_user(p, arg2, 0);
  8717. return ret;
  8718. #endif
  8719. #ifdef TARGET_NR_nice /* not on alpha */
  8720. case TARGET_NR_nice:
  8721. return get_errno(nice(arg1));
  8722. #endif
  8723. case TARGET_NR_sync:
  8724. sync();
  8725. return 0;
  8726. #if defined(TARGET_NR_syncfs) && defined(CONFIG_SYNCFS)
  8727. case TARGET_NR_syncfs:
  8728. return get_errno(syncfs(arg1));
  8729. #endif
  8730. case TARGET_NR_kill:
  8731. return get_errno(safe_kill(arg1, target_to_host_signal(arg2)));
  8732. #ifdef TARGET_NR_rename
  8733. case TARGET_NR_rename:
  8734. {
  8735. void *p2;
  8736. p = lock_user_string(arg1);
  8737. p2 = lock_user_string(arg2);
  8738. if (!p || !p2)
  8739. ret = -TARGET_EFAULT;
  8740. else
  8741. ret = get_errno(rename(p, p2));
  8742. unlock_user(p2, arg2, 0);
  8743. unlock_user(p, arg1, 0);
  8744. }
  8745. return ret;
  8746. #endif
  8747. #if defined(TARGET_NR_renameat)
  8748. case TARGET_NR_renameat:
  8749. {
  8750. void *p2;
  8751. p = lock_user_string(arg2);
  8752. p2 = lock_user_string(arg4);
  8753. if (!p || !p2)
  8754. ret = -TARGET_EFAULT;
  8755. else
  8756. ret = get_errno(renameat(arg1, p, arg3, p2));
  8757. unlock_user(p2, arg4, 0);
  8758. unlock_user(p, arg2, 0);
  8759. }
  8760. return ret;
  8761. #endif
  8762. #if defined(TARGET_NR_renameat2)
  8763. case TARGET_NR_renameat2:
  8764. {
  8765. void *p2;
  8766. p = lock_user_string(arg2);
  8767. p2 = lock_user_string(arg4);
  8768. if (!p || !p2) {
  8769. ret = -TARGET_EFAULT;
  8770. } else {
  8771. ret = get_errno(sys_renameat2(arg1, p, arg3, p2, arg5));
  8772. }
  8773. unlock_user(p2, arg4, 0);
  8774. unlock_user(p, arg2, 0);
  8775. }
  8776. return ret;
  8777. #endif
  8778. #ifdef TARGET_NR_mkdir
  8779. case TARGET_NR_mkdir:
  8780. if (!(p = lock_user_string(arg1)))
  8781. return -TARGET_EFAULT;
  8782. ret = get_errno(mkdir(p, arg2));
  8783. unlock_user(p, arg1, 0);
  8784. return ret;
  8785. #endif
  8786. #if defined(TARGET_NR_mkdirat)
  8787. case TARGET_NR_mkdirat:
  8788. if (!(p = lock_user_string(arg2)))
  8789. return -TARGET_EFAULT;
  8790. ret = get_errno(mkdirat(arg1, p, arg3));
  8791. unlock_user(p, arg2, 0);
  8792. return ret;
  8793. #endif
  8794. #ifdef TARGET_NR_rmdir
  8795. case TARGET_NR_rmdir:
  8796. if (!(p = lock_user_string(arg1)))
  8797. return -TARGET_EFAULT;
  8798. ret = get_errno(rmdir(p));
  8799. unlock_user(p, arg1, 0);
  8800. return ret;
  8801. #endif
  8802. case TARGET_NR_dup:
  8803. ret = get_errno(dup(arg1));
  8804. if (ret >= 0) {
  8805. fd_trans_dup(arg1, ret);
  8806. }
  8807. return ret;
  8808. #ifdef TARGET_NR_pipe
  8809. case TARGET_NR_pipe:
  8810. return do_pipe(cpu_env, arg1, 0, 0);
  8811. #endif
  8812. #ifdef TARGET_NR_pipe2
  8813. case TARGET_NR_pipe2:
  8814. return do_pipe(cpu_env, arg1,
  8815. target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
  8816. #endif
  8817. case TARGET_NR_times:
  8818. {
  8819. struct target_tms *tmsp;
  8820. struct tms tms;
  8821. ret = get_errno(times(&tms));
  8822. if (arg1) {
  8823. tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
  8824. if (!tmsp)
  8825. return -TARGET_EFAULT;
  8826. tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
  8827. tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
  8828. tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
  8829. tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
  8830. }
  8831. if (!is_error(ret))
  8832. ret = host_to_target_clock_t(ret);
  8833. }
  8834. return ret;
  8835. case TARGET_NR_acct:
  8836. if (arg1 == 0) {
  8837. ret = get_errno(acct(NULL));
  8838. } else {
  8839. if (!(p = lock_user_string(arg1))) {
  8840. return -TARGET_EFAULT;
  8841. }
  8842. ret = get_errno(acct(path(p)));
  8843. unlock_user(p, arg1, 0);
  8844. }
  8845. return ret;
  8846. #ifdef TARGET_NR_umount2
  8847. case TARGET_NR_umount2:
  8848. if (!(p = lock_user_string(arg1)))
  8849. return -TARGET_EFAULT;
  8850. ret = get_errno(umount2(p, arg2));
  8851. unlock_user(p, arg1, 0);
  8852. return ret;
  8853. #endif
  8854. case TARGET_NR_ioctl:
  8855. return do_ioctl(arg1, arg2, arg3);
  8856. #ifdef TARGET_NR_fcntl
  8857. case TARGET_NR_fcntl:
  8858. return do_fcntl(arg1, arg2, arg3);
  8859. #endif
  8860. case TARGET_NR_setpgid:
  8861. return get_errno(setpgid(arg1, arg2));
  8862. case TARGET_NR_umask:
  8863. return get_errno(umask(arg1));
  8864. case TARGET_NR_chroot:
  8865. if (!(p = lock_user_string(arg1)))
  8866. return -TARGET_EFAULT;
  8867. ret = get_errno(chroot(p));
  8868. unlock_user(p, arg1, 0);
  8869. return ret;
  8870. #ifdef TARGET_NR_dup2
  8871. case TARGET_NR_dup2:
  8872. ret = get_errno(dup2(arg1, arg2));
  8873. if (ret >= 0) {
  8874. fd_trans_dup(arg1, arg2);
  8875. }
  8876. return ret;
  8877. #endif
  8878. #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
  8879. case TARGET_NR_dup3:
  8880. {
  8881. int host_flags;
  8882. if ((arg3 & ~TARGET_O_CLOEXEC) != 0) {
  8883. return -EINVAL;
  8884. }
  8885. host_flags = target_to_host_bitmask(arg3, fcntl_flags_tbl);
  8886. ret = get_errno(dup3(arg1, arg2, host_flags));
  8887. if (ret >= 0) {
  8888. fd_trans_dup(arg1, arg2);
  8889. }
  8890. return ret;
  8891. }
  8892. #endif
  8893. #ifdef TARGET_NR_getppid /* not on alpha */
  8894. case TARGET_NR_getppid:
  8895. return get_errno(getppid());
  8896. #endif
  8897. #ifdef TARGET_NR_getpgrp
  8898. case TARGET_NR_getpgrp:
  8899. return get_errno(getpgrp());
  8900. #endif
  8901. case TARGET_NR_setsid:
  8902. return get_errno(setsid());
  8903. #ifdef TARGET_NR_sigaction
  8904. case TARGET_NR_sigaction:
  8905. {
  8906. #if defined(TARGET_MIPS)
  8907. struct target_sigaction act, oact, *pact, *old_act;
  8908. if (arg2) {
  8909. if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
  8910. return -TARGET_EFAULT;
  8911. act._sa_handler = old_act->_sa_handler;
  8912. target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
  8913. act.sa_flags = old_act->sa_flags;
  8914. unlock_user_struct(old_act, arg2, 0);
  8915. pact = &act;
  8916. } else {
  8917. pact = NULL;
  8918. }
  8919. ret = get_errno(do_sigaction(arg1, pact, &oact, 0));
  8920. if (!is_error(ret) && arg3) {
  8921. if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
  8922. return -TARGET_EFAULT;
  8923. old_act->_sa_handler = oact._sa_handler;
  8924. old_act->sa_flags = oact.sa_flags;
  8925. old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
  8926. old_act->sa_mask.sig[1] = 0;
  8927. old_act->sa_mask.sig[2] = 0;
  8928. old_act->sa_mask.sig[3] = 0;
  8929. unlock_user_struct(old_act, arg3, 1);
  8930. }
  8931. #else
  8932. struct target_old_sigaction *old_act;
  8933. struct target_sigaction act, oact, *pact;
  8934. if (arg2) {
  8935. if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
  8936. return -TARGET_EFAULT;
  8937. act._sa_handler = old_act->_sa_handler;
  8938. target_siginitset(&act.sa_mask, old_act->sa_mask);
  8939. act.sa_flags = old_act->sa_flags;
  8940. #ifdef TARGET_ARCH_HAS_SA_RESTORER
  8941. act.sa_restorer = old_act->sa_restorer;
  8942. #endif
  8943. unlock_user_struct(old_act, arg2, 0);
  8944. pact = &act;
  8945. } else {
  8946. pact = NULL;
  8947. }
  8948. ret = get_errno(do_sigaction(arg1, pact, &oact, 0));
  8949. if (!is_error(ret) && arg3) {
  8950. if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
  8951. return -TARGET_EFAULT;
  8952. old_act->_sa_handler = oact._sa_handler;
  8953. old_act->sa_mask = oact.sa_mask.sig[0];
  8954. old_act->sa_flags = oact.sa_flags;
  8955. #ifdef TARGET_ARCH_HAS_SA_RESTORER
  8956. old_act->sa_restorer = oact.sa_restorer;
  8957. #endif
  8958. unlock_user_struct(old_act, arg3, 1);
  8959. }
  8960. #endif
  8961. }
  8962. return ret;
  8963. #endif
  8964. case TARGET_NR_rt_sigaction:
  8965. {
  8966. /*
  8967. * For Alpha and SPARC this is a 5 argument syscall, with
  8968. * a 'restorer' parameter which must be copied into the
  8969. * sa_restorer field of the sigaction struct.
  8970. * For Alpha that 'restorer' is arg5; for SPARC it is arg4,
  8971. * and arg5 is the sigsetsize.
  8972. */
  8973. #if defined(TARGET_ALPHA)
  8974. target_ulong sigsetsize = arg4;
  8975. target_ulong restorer = arg5;
  8976. #elif defined(TARGET_SPARC)
  8977. target_ulong restorer = arg4;
  8978. target_ulong sigsetsize = arg5;
  8979. #else
  8980. target_ulong sigsetsize = arg4;
  8981. target_ulong restorer = 0;
  8982. #endif
  8983. struct target_sigaction *act = NULL;
  8984. struct target_sigaction *oact = NULL;
  8985. if (sigsetsize != sizeof(target_sigset_t)) {
  8986. return -TARGET_EINVAL;
  8987. }
  8988. if (arg2 && !lock_user_struct(VERIFY_READ, act, arg2, 1)) {
  8989. return -TARGET_EFAULT;
  8990. }
  8991. if (arg3 && !lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
  8992. ret = -TARGET_EFAULT;
  8993. } else {
  8994. ret = get_errno(do_sigaction(arg1, act, oact, restorer));
  8995. if (oact) {
  8996. unlock_user_struct(oact, arg3, 1);
  8997. }
  8998. }
  8999. if (act) {
  9000. unlock_user_struct(act, arg2, 0);
  9001. }
  9002. }
  9003. return ret;
  9004. #ifdef TARGET_NR_sgetmask /* not on alpha */
  9005. case TARGET_NR_sgetmask:
  9006. {
  9007. sigset_t cur_set;
  9008. abi_ulong target_set;
  9009. ret = do_sigprocmask(0, NULL, &cur_set);
  9010. if (!ret) {
  9011. host_to_target_old_sigset(&target_set, &cur_set);
  9012. ret = target_set;
  9013. }
  9014. }
  9015. return ret;
  9016. #endif
  9017. #ifdef TARGET_NR_ssetmask /* not on alpha */
  9018. case TARGET_NR_ssetmask:
  9019. {
  9020. sigset_t set, oset;
  9021. abi_ulong target_set = arg1;
  9022. target_to_host_old_sigset(&set, &target_set);
  9023. ret = do_sigprocmask(SIG_SETMASK, &set, &oset);
  9024. if (!ret) {
  9025. host_to_target_old_sigset(&target_set, &oset);
  9026. ret = target_set;
  9027. }
  9028. }
  9029. return ret;
  9030. #endif
  9031. #ifdef TARGET_NR_sigprocmask
  9032. case TARGET_NR_sigprocmask:
  9033. {
  9034. #if defined(TARGET_ALPHA)
  9035. sigset_t set, oldset;
  9036. abi_ulong mask;
  9037. int how;
  9038. switch (arg1) {
  9039. case TARGET_SIG_BLOCK:
  9040. how = SIG_BLOCK;
  9041. break;
  9042. case TARGET_SIG_UNBLOCK:
  9043. how = SIG_UNBLOCK;
  9044. break;
  9045. case TARGET_SIG_SETMASK:
  9046. how = SIG_SETMASK;
  9047. break;
  9048. default:
  9049. return -TARGET_EINVAL;
  9050. }
  9051. mask = arg2;
  9052. target_to_host_old_sigset(&set, &mask);
  9053. ret = do_sigprocmask(how, &set, &oldset);
  9054. if (!is_error(ret)) {
  9055. host_to_target_old_sigset(&mask, &oldset);
  9056. ret = mask;
  9057. cpu_env->ir[IR_V0] = 0; /* force no error */
  9058. }
  9059. #else
  9060. sigset_t set, oldset, *set_ptr;
  9061. int how;
  9062. if (arg2) {
  9063. p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1);
  9064. if (!p) {
  9065. return -TARGET_EFAULT;
  9066. }
  9067. target_to_host_old_sigset(&set, p);
  9068. unlock_user(p, arg2, 0);
  9069. set_ptr = &set;
  9070. switch (arg1) {
  9071. case TARGET_SIG_BLOCK:
  9072. how = SIG_BLOCK;
  9073. break;
  9074. case TARGET_SIG_UNBLOCK:
  9075. how = SIG_UNBLOCK;
  9076. break;
  9077. case TARGET_SIG_SETMASK:
  9078. how = SIG_SETMASK;
  9079. break;
  9080. default:
  9081. return -TARGET_EINVAL;
  9082. }
  9083. } else {
  9084. how = 0;
  9085. set_ptr = NULL;
  9086. }
  9087. ret = do_sigprocmask(how, set_ptr, &oldset);
  9088. if (!is_error(ret) && arg3) {
  9089. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
  9090. return -TARGET_EFAULT;
  9091. host_to_target_old_sigset(p, &oldset);
  9092. unlock_user(p, arg3, sizeof(target_sigset_t));
  9093. }
  9094. #endif
  9095. }
  9096. return ret;
  9097. #endif
  9098. case TARGET_NR_rt_sigprocmask:
  9099. {
  9100. int how = arg1;
  9101. sigset_t set, oldset, *set_ptr;
  9102. if (arg4 != sizeof(target_sigset_t)) {
  9103. return -TARGET_EINVAL;
  9104. }
  9105. if (arg2) {
  9106. p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1);
  9107. if (!p) {
  9108. return -TARGET_EFAULT;
  9109. }
  9110. target_to_host_sigset(&set, p);
  9111. unlock_user(p, arg2, 0);
  9112. set_ptr = &set;
  9113. switch(how) {
  9114. case TARGET_SIG_BLOCK:
  9115. how = SIG_BLOCK;
  9116. break;
  9117. case TARGET_SIG_UNBLOCK:
  9118. how = SIG_UNBLOCK;
  9119. break;
  9120. case TARGET_SIG_SETMASK:
  9121. how = SIG_SETMASK;
  9122. break;
  9123. default:
  9124. return -TARGET_EINVAL;
  9125. }
  9126. } else {
  9127. how = 0;
  9128. set_ptr = NULL;
  9129. }
  9130. ret = do_sigprocmask(how, set_ptr, &oldset);
  9131. if (!is_error(ret) && arg3) {
  9132. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
  9133. return -TARGET_EFAULT;
  9134. host_to_target_sigset(p, &oldset);
  9135. unlock_user(p, arg3, sizeof(target_sigset_t));
  9136. }
  9137. }
  9138. return ret;
  9139. #ifdef TARGET_NR_sigpending
  9140. case TARGET_NR_sigpending:
  9141. {
  9142. sigset_t set;
  9143. ret = get_errno(sigpending(&set));
  9144. if (!is_error(ret)) {
  9145. if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
  9146. return -TARGET_EFAULT;
  9147. host_to_target_old_sigset(p, &set);
  9148. unlock_user(p, arg1, sizeof(target_sigset_t));
  9149. }
  9150. }
  9151. return ret;
  9152. #endif
  9153. case TARGET_NR_rt_sigpending:
  9154. {
  9155. sigset_t set;
  9156. /* Yes, this check is >, not != like most. We follow the kernel's
  9157. * logic and it does it like this because it implements
  9158. * NR_sigpending through the same code path, and in that case
  9159. * the old_sigset_t is smaller in size.
  9160. */
  9161. if (arg2 > sizeof(target_sigset_t)) {
  9162. return -TARGET_EINVAL;
  9163. }
  9164. ret = get_errno(sigpending(&set));
  9165. if (!is_error(ret)) {
  9166. if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
  9167. return -TARGET_EFAULT;
  9168. host_to_target_sigset(p, &set);
  9169. unlock_user(p, arg1, sizeof(target_sigset_t));
  9170. }
  9171. }
  9172. return ret;
  9173. #ifdef TARGET_NR_sigsuspend
  9174. case TARGET_NR_sigsuspend:
  9175. {
  9176. sigset_t *set;
  9177. #if defined(TARGET_ALPHA)
  9178. TaskState *ts = get_task_state(cpu);
  9179. /* target_to_host_old_sigset will bswap back */
  9180. abi_ulong mask = tswapal(arg1);
  9181. set = &ts->sigsuspend_mask;
  9182. target_to_host_old_sigset(set, &mask);
  9183. #else
  9184. ret = process_sigsuspend_mask(&set, arg1, sizeof(target_sigset_t));
  9185. if (ret != 0) {
  9186. return ret;
  9187. }
  9188. #endif
  9189. ret = get_errno(safe_rt_sigsuspend(set, SIGSET_T_SIZE));
  9190. finish_sigsuspend_mask(ret);
  9191. }
  9192. return ret;
  9193. #endif
  9194. case TARGET_NR_rt_sigsuspend:
  9195. {
  9196. sigset_t *set;
  9197. ret = process_sigsuspend_mask(&set, arg1, arg2);
  9198. if (ret != 0) {
  9199. return ret;
  9200. }
  9201. ret = get_errno(safe_rt_sigsuspend(set, SIGSET_T_SIZE));
  9202. finish_sigsuspend_mask(ret);
  9203. }
  9204. return ret;
  9205. #ifdef TARGET_NR_rt_sigtimedwait
  9206. case TARGET_NR_rt_sigtimedwait:
  9207. {
  9208. sigset_t set;
  9209. struct timespec uts, *puts;
  9210. siginfo_t uinfo;
  9211. if (arg4 != sizeof(target_sigset_t)) {
  9212. return -TARGET_EINVAL;
  9213. }
  9214. if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
  9215. return -TARGET_EFAULT;
  9216. target_to_host_sigset(&set, p);
  9217. unlock_user(p, arg1, 0);
  9218. if (arg3) {
  9219. puts = &uts;
  9220. if (target_to_host_timespec(puts, arg3)) {
  9221. return -TARGET_EFAULT;
  9222. }
  9223. } else {
  9224. puts = NULL;
  9225. }
  9226. ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts,
  9227. SIGSET_T_SIZE));
  9228. if (!is_error(ret)) {
  9229. if (arg2) {
  9230. p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t),
  9231. 0);
  9232. if (!p) {
  9233. return -TARGET_EFAULT;
  9234. }
  9235. host_to_target_siginfo(p, &uinfo);
  9236. unlock_user(p, arg2, sizeof(target_siginfo_t));
  9237. }
  9238. ret = host_to_target_signal(ret);
  9239. }
  9240. }
  9241. return ret;
  9242. #endif
  9243. #ifdef TARGET_NR_rt_sigtimedwait_time64
  9244. case TARGET_NR_rt_sigtimedwait_time64:
  9245. {
  9246. sigset_t set;
  9247. struct timespec uts, *puts;
  9248. siginfo_t uinfo;
  9249. if (arg4 != sizeof(target_sigset_t)) {
  9250. return -TARGET_EINVAL;
  9251. }
  9252. p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1);
  9253. if (!p) {
  9254. return -TARGET_EFAULT;
  9255. }
  9256. target_to_host_sigset(&set, p);
  9257. unlock_user(p, arg1, 0);
  9258. if (arg3) {
  9259. puts = &uts;
  9260. if (target_to_host_timespec64(puts, arg3)) {
  9261. return -TARGET_EFAULT;
  9262. }
  9263. } else {
  9264. puts = NULL;
  9265. }
  9266. ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts,
  9267. SIGSET_T_SIZE));
  9268. if (!is_error(ret)) {
  9269. if (arg2) {
  9270. p = lock_user(VERIFY_WRITE, arg2,
  9271. sizeof(target_siginfo_t), 0);
  9272. if (!p) {
  9273. return -TARGET_EFAULT;
  9274. }
  9275. host_to_target_siginfo(p, &uinfo);
  9276. unlock_user(p, arg2, sizeof(target_siginfo_t));
  9277. }
  9278. ret = host_to_target_signal(ret);
  9279. }
  9280. }
  9281. return ret;
  9282. #endif
  9283. case TARGET_NR_rt_sigqueueinfo:
  9284. {
  9285. siginfo_t uinfo;
  9286. p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1);
  9287. if (!p) {
  9288. return -TARGET_EFAULT;
  9289. }
  9290. target_to_host_siginfo(&uinfo, p);
  9291. unlock_user(p, arg3, 0);
  9292. ret = get_errno(sys_rt_sigqueueinfo(arg1, target_to_host_signal(arg2), &uinfo));
  9293. }
  9294. return ret;
  9295. case TARGET_NR_rt_tgsigqueueinfo:
  9296. {
  9297. siginfo_t uinfo;
  9298. p = lock_user(VERIFY_READ, arg4, sizeof(target_siginfo_t), 1);
  9299. if (!p) {
  9300. return -TARGET_EFAULT;
  9301. }
  9302. target_to_host_siginfo(&uinfo, p);
  9303. unlock_user(p, arg4, 0);
  9304. ret = get_errno(sys_rt_tgsigqueueinfo(arg1, arg2, target_to_host_signal(arg3), &uinfo));
  9305. }
  9306. return ret;
  9307. #ifdef TARGET_NR_sigreturn
  9308. case TARGET_NR_sigreturn:
  9309. if (block_signals()) {
  9310. return -QEMU_ERESTARTSYS;
  9311. }
  9312. return do_sigreturn(cpu_env);
  9313. #endif
  9314. case TARGET_NR_rt_sigreturn:
  9315. if (block_signals()) {
  9316. return -QEMU_ERESTARTSYS;
  9317. }
  9318. return do_rt_sigreturn(cpu_env);
  9319. case TARGET_NR_sethostname:
  9320. if (!(p = lock_user_string(arg1)))
  9321. return -TARGET_EFAULT;
  9322. ret = get_errno(sethostname(p, arg2));
  9323. unlock_user(p, arg1, 0);
  9324. return ret;
  9325. #ifdef TARGET_NR_setrlimit
  9326. case TARGET_NR_setrlimit:
  9327. {
  9328. int resource = target_to_host_resource(arg1);
  9329. struct target_rlimit *target_rlim;
  9330. struct rlimit rlim;
  9331. if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
  9332. return -TARGET_EFAULT;
  9333. rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
  9334. rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
  9335. unlock_user_struct(target_rlim, arg2, 0);
  9336. /*
  9337. * If we just passed through resource limit settings for memory then
  9338. * they would also apply to QEMU's own allocations, and QEMU will
  9339. * crash or hang or die if its allocations fail. Ideally we would
  9340. * track the guest allocations in QEMU and apply the limits ourselves.
  9341. * For now, just tell the guest the call succeeded but don't actually
  9342. * limit anything.
  9343. */
  9344. if (resource != RLIMIT_AS &&
  9345. resource != RLIMIT_DATA &&
  9346. resource != RLIMIT_STACK) {
  9347. return get_errno(setrlimit(resource, &rlim));
  9348. } else {
  9349. return 0;
  9350. }
  9351. }
  9352. #endif
  9353. #ifdef TARGET_NR_getrlimit
  9354. case TARGET_NR_getrlimit:
  9355. {
  9356. int resource = target_to_host_resource(arg1);
  9357. struct target_rlimit *target_rlim;
  9358. struct rlimit rlim;
  9359. ret = get_errno(getrlimit(resource, &rlim));
  9360. if (!is_error(ret)) {
  9361. if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
  9362. return -TARGET_EFAULT;
  9363. target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
  9364. target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
  9365. unlock_user_struct(target_rlim, arg2, 1);
  9366. }
  9367. }
  9368. return ret;
  9369. #endif
  9370. case TARGET_NR_getrusage:
  9371. {
  9372. struct rusage rusage;
  9373. ret = get_errno(getrusage(arg1, &rusage));
  9374. if (!is_error(ret)) {
  9375. ret = host_to_target_rusage(arg2, &rusage);
  9376. }
  9377. }
  9378. return ret;
  9379. #if defined(TARGET_NR_gettimeofday)
  9380. case TARGET_NR_gettimeofday:
  9381. {
  9382. struct timeval tv;
  9383. struct timezone tz;
  9384. ret = get_errno(gettimeofday(&tv, &tz));
  9385. if (!is_error(ret)) {
  9386. if (arg1 && copy_to_user_timeval(arg1, &tv)) {
  9387. return -TARGET_EFAULT;
  9388. }
  9389. if (arg2 && copy_to_user_timezone(arg2, &tz)) {
  9390. return -TARGET_EFAULT;
  9391. }
  9392. }
  9393. }
  9394. return ret;
  9395. #endif
  9396. #if defined(TARGET_NR_settimeofday)
  9397. case TARGET_NR_settimeofday:
  9398. {
  9399. struct timeval tv, *ptv = NULL;
  9400. struct timezone tz, *ptz = NULL;
  9401. if (arg1) {
  9402. if (copy_from_user_timeval(&tv, arg1)) {
  9403. return -TARGET_EFAULT;
  9404. }
  9405. ptv = &tv;
  9406. }
  9407. if (arg2) {
  9408. if (copy_from_user_timezone(&tz, arg2)) {
  9409. return -TARGET_EFAULT;
  9410. }
  9411. ptz = &tz;
  9412. }
  9413. return get_errno(settimeofday(ptv, ptz));
  9414. }
  9415. #endif
  9416. #if defined(TARGET_NR_select)
  9417. case TARGET_NR_select:
  9418. #if defined(TARGET_WANT_NI_OLD_SELECT)
  9419. /* some architectures used to have old_select here
  9420. * but now ENOSYS it.
  9421. */
  9422. ret = -TARGET_ENOSYS;
  9423. #elif defined(TARGET_WANT_OLD_SYS_SELECT)
  9424. ret = do_old_select(arg1);
  9425. #else
  9426. ret = do_select(arg1, arg2, arg3, arg4, arg5);
  9427. #endif
  9428. return ret;
  9429. #endif
  9430. #ifdef TARGET_NR_pselect6
  9431. case TARGET_NR_pselect6:
  9432. return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, false);
  9433. #endif
  9434. #ifdef TARGET_NR_pselect6_time64
  9435. case TARGET_NR_pselect6_time64:
  9436. return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, true);
  9437. #endif
  9438. #ifdef TARGET_NR_symlink
  9439. case TARGET_NR_symlink:
  9440. {
  9441. void *p2;
  9442. p = lock_user_string(arg1);
  9443. p2 = lock_user_string(arg2);
  9444. if (!p || !p2)
  9445. ret = -TARGET_EFAULT;
  9446. else
  9447. ret = get_errno(symlink(p, p2));
  9448. unlock_user(p2, arg2, 0);
  9449. unlock_user(p, arg1, 0);
  9450. }
  9451. return ret;
  9452. #endif
  9453. #if defined(TARGET_NR_symlinkat)
  9454. case TARGET_NR_symlinkat:
  9455. {
  9456. void *p2;
  9457. p = lock_user_string(arg1);
  9458. p2 = lock_user_string(arg3);
  9459. if (!p || !p2)
  9460. ret = -TARGET_EFAULT;
  9461. else
  9462. ret = get_errno(symlinkat(p, arg2, p2));
  9463. unlock_user(p2, arg3, 0);
  9464. unlock_user(p, arg1, 0);
  9465. }
  9466. return ret;
  9467. #endif
  9468. #ifdef TARGET_NR_readlink
  9469. case TARGET_NR_readlink:
  9470. {
  9471. void *p2;
  9472. p = lock_user_string(arg1);
  9473. p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  9474. ret = get_errno(do_guest_readlink(p, p2, arg3));
  9475. unlock_user(p2, arg2, ret);
  9476. unlock_user(p, arg1, 0);
  9477. }
  9478. return ret;
  9479. #endif
  9480. #if defined(TARGET_NR_readlinkat)
  9481. case TARGET_NR_readlinkat:
  9482. {
  9483. void *p2;
  9484. p = lock_user_string(arg2);
  9485. p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  9486. if (!p || !p2) {
  9487. ret = -TARGET_EFAULT;
  9488. } else if (!arg4) {
  9489. /* Short circuit this for the magic exe check. */
  9490. ret = -TARGET_EINVAL;
  9491. } else if (is_proc_myself((const char *)p, "exe")) {
  9492. /*
  9493. * Don't worry about sign mismatch as earlier mapping
  9494. * logic would have thrown a bad address error.
  9495. */
  9496. ret = MIN(strlen(exec_path), arg4);
  9497. /* We cannot NUL terminate the string. */
  9498. memcpy(p2, exec_path, ret);
  9499. } else {
  9500. ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
  9501. }
  9502. unlock_user(p2, arg3, ret);
  9503. unlock_user(p, arg2, 0);
  9504. }
  9505. return ret;
  9506. #endif
  9507. #ifdef TARGET_NR_swapon
  9508. case TARGET_NR_swapon:
  9509. if (!(p = lock_user_string(arg1)))
  9510. return -TARGET_EFAULT;
  9511. ret = get_errno(swapon(p, arg2));
  9512. unlock_user(p, arg1, 0);
  9513. return ret;
  9514. #endif
  9515. case TARGET_NR_reboot:
  9516. if (arg3 == LINUX_REBOOT_CMD_RESTART2) {
  9517. /* arg4 must be ignored in all other cases */
  9518. p = lock_user_string(arg4);
  9519. if (!p) {
  9520. return -TARGET_EFAULT;
  9521. }
  9522. ret = get_errno(reboot(arg1, arg2, arg3, p));
  9523. unlock_user(p, arg4, 0);
  9524. } else {
  9525. ret = get_errno(reboot(arg1, arg2, arg3, NULL));
  9526. }
  9527. return ret;
  9528. #ifdef TARGET_NR_mmap
  9529. case TARGET_NR_mmap:
  9530. #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
  9531. (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
  9532. defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
  9533. || defined(TARGET_S390X)
  9534. {
  9535. abi_ulong *v;
  9536. abi_ulong v1, v2, v3, v4, v5, v6;
  9537. if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
  9538. return -TARGET_EFAULT;
  9539. v1 = tswapal(v[0]);
  9540. v2 = tswapal(v[1]);
  9541. v3 = tswapal(v[2]);
  9542. v4 = tswapal(v[3]);
  9543. v5 = tswapal(v[4]);
  9544. v6 = tswapal(v[5]);
  9545. unlock_user(v, arg1, 0);
  9546. return do_mmap(v1, v2, v3, v4, v5, v6);
  9547. }
  9548. #else
  9549. /* mmap pointers are always untagged */
  9550. return do_mmap(arg1, arg2, arg3, arg4, arg5, arg6);
  9551. #endif
  9552. #endif
  9553. #ifdef TARGET_NR_mmap2
  9554. case TARGET_NR_mmap2:
  9555. #ifndef MMAP_SHIFT
  9556. #define MMAP_SHIFT 12
  9557. #endif
  9558. return do_mmap(arg1, arg2, arg3, arg4, arg5,
  9559. (off_t)(abi_ulong)arg6 << MMAP_SHIFT);
  9560. #endif
  9561. case TARGET_NR_munmap:
  9562. arg1 = cpu_untagged_addr(cpu, arg1);
  9563. return get_errno(target_munmap(arg1, arg2));
  9564. case TARGET_NR_mprotect:
  9565. arg1 = cpu_untagged_addr(cpu, arg1);
  9566. {
  9567. TaskState *ts = get_task_state(cpu);
  9568. /* Special hack to detect libc making the stack executable. */
  9569. if ((arg3 & PROT_GROWSDOWN)
  9570. && arg1 >= ts->info->stack_limit
  9571. && arg1 <= ts->info->start_stack) {
  9572. arg3 &= ~PROT_GROWSDOWN;
  9573. arg2 = arg2 + arg1 - ts->info->stack_limit;
  9574. arg1 = ts->info->stack_limit;
  9575. }
  9576. }
  9577. return get_errno(target_mprotect(arg1, arg2, arg3));
  9578. #ifdef TARGET_NR_mremap
  9579. case TARGET_NR_mremap:
  9580. arg1 = cpu_untagged_addr(cpu, arg1);
  9581. /* mremap new_addr (arg5) is always untagged */
  9582. return get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
  9583. #endif
  9584. /* ??? msync/mlock/munlock are broken for softmmu. */
  9585. #ifdef TARGET_NR_msync
  9586. case TARGET_NR_msync:
  9587. return get_errno(msync(g2h(cpu, arg1), arg2,
  9588. target_to_host_msync_arg(arg3)));
  9589. #endif
  9590. #ifdef TARGET_NR_mlock
  9591. case TARGET_NR_mlock:
  9592. return get_errno(mlock(g2h(cpu, arg1), arg2));
  9593. #endif
  9594. #ifdef TARGET_NR_munlock
  9595. case TARGET_NR_munlock:
  9596. return get_errno(munlock(g2h(cpu, arg1), arg2));
  9597. #endif
  9598. #ifdef TARGET_NR_mlockall
  9599. case TARGET_NR_mlockall:
  9600. return get_errno(mlockall(target_to_host_mlockall_arg(arg1)));
  9601. #endif
  9602. #ifdef TARGET_NR_munlockall
  9603. case TARGET_NR_munlockall:
  9604. return get_errno(munlockall());
  9605. #endif
  9606. #ifdef TARGET_NR_truncate
  9607. case TARGET_NR_truncate:
  9608. if (!(p = lock_user_string(arg1)))
  9609. return -TARGET_EFAULT;
  9610. ret = get_errno(truncate(p, arg2));
  9611. unlock_user(p, arg1, 0);
  9612. return ret;
  9613. #endif
  9614. #ifdef TARGET_NR_ftruncate
  9615. case TARGET_NR_ftruncate:
  9616. return get_errno(ftruncate(arg1, arg2));
  9617. #endif
  9618. case TARGET_NR_fchmod:
  9619. return get_errno(fchmod(arg1, arg2));
  9620. #if defined(TARGET_NR_fchmodat)
  9621. case TARGET_NR_fchmodat:
  9622. if (!(p = lock_user_string(arg2)))
  9623. return -TARGET_EFAULT;
  9624. ret = get_errno(fchmodat(arg1, p, arg3, 0));
  9625. unlock_user(p, arg2, 0);
  9626. return ret;
  9627. #endif
  9628. case TARGET_NR_getpriority:
  9629. /* Note that negative values are valid for getpriority, so we must
  9630. differentiate based on errno settings. */
  9631. errno = 0;
  9632. ret = getpriority(arg1, arg2);
  9633. if (ret == -1 && errno != 0) {
  9634. return -host_to_target_errno(errno);
  9635. }
  9636. #ifdef TARGET_ALPHA
  9637. /* Return value is the unbiased priority. Signal no error. */
  9638. cpu_env->ir[IR_V0] = 0;
  9639. #else
  9640. /* Return value is a biased priority to avoid negative numbers. */
  9641. ret = 20 - ret;
  9642. #endif
  9643. return ret;
  9644. case TARGET_NR_setpriority:
  9645. return get_errno(setpriority(arg1, arg2, arg3));
  9646. #ifdef TARGET_NR_statfs
  9647. case TARGET_NR_statfs:
  9648. if (!(p = lock_user_string(arg1))) {
  9649. return -TARGET_EFAULT;
  9650. }
  9651. ret = get_errno(statfs(path(p), &stfs));
  9652. unlock_user(p, arg1, 0);
  9653. convert_statfs:
  9654. if (!is_error(ret)) {
  9655. struct target_statfs *target_stfs;
  9656. if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
  9657. return -TARGET_EFAULT;
  9658. __put_user(stfs.f_type, &target_stfs->f_type);
  9659. __put_user(stfs.f_bsize, &target_stfs->f_bsize);
  9660. __put_user(stfs.f_blocks, &target_stfs->f_blocks);
  9661. __put_user(stfs.f_bfree, &target_stfs->f_bfree);
  9662. __put_user(stfs.f_bavail, &target_stfs->f_bavail);
  9663. __put_user(stfs.f_files, &target_stfs->f_files);
  9664. __put_user(stfs.f_ffree, &target_stfs->f_ffree);
  9665. __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
  9666. __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
  9667. __put_user(stfs.f_namelen, &target_stfs->f_namelen);
  9668. __put_user(stfs.f_frsize, &target_stfs->f_frsize);
  9669. #ifdef _STATFS_F_FLAGS
  9670. __put_user(stfs.f_flags, &target_stfs->f_flags);
  9671. #else
  9672. __put_user(0, &target_stfs->f_flags);
  9673. #endif
  9674. memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
  9675. unlock_user_struct(target_stfs, arg2, 1);
  9676. }
  9677. return ret;
  9678. #endif
  9679. #ifdef TARGET_NR_fstatfs
  9680. case TARGET_NR_fstatfs:
  9681. ret = get_errno(fstatfs(arg1, &stfs));
  9682. goto convert_statfs;
  9683. #endif
  9684. #ifdef TARGET_NR_statfs64
  9685. case TARGET_NR_statfs64:
  9686. if (!(p = lock_user_string(arg1))) {
  9687. return -TARGET_EFAULT;
  9688. }
  9689. ret = get_errno(statfs(path(p), &stfs));
  9690. unlock_user(p, arg1, 0);
  9691. convert_statfs64:
  9692. if (!is_error(ret)) {
  9693. struct target_statfs64 *target_stfs;
  9694. if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
  9695. return -TARGET_EFAULT;
  9696. __put_user(stfs.f_type, &target_stfs->f_type);
  9697. __put_user(stfs.f_bsize, &target_stfs->f_bsize);
  9698. __put_user(stfs.f_blocks, &target_stfs->f_blocks);
  9699. __put_user(stfs.f_bfree, &target_stfs->f_bfree);
  9700. __put_user(stfs.f_bavail, &target_stfs->f_bavail);
  9701. __put_user(stfs.f_files, &target_stfs->f_files);
  9702. __put_user(stfs.f_ffree, &target_stfs->f_ffree);
  9703. __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
  9704. __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
  9705. __put_user(stfs.f_namelen, &target_stfs->f_namelen);
  9706. __put_user(stfs.f_frsize, &target_stfs->f_frsize);
  9707. #ifdef _STATFS_F_FLAGS
  9708. __put_user(stfs.f_flags, &target_stfs->f_flags);
  9709. #else
  9710. __put_user(0, &target_stfs->f_flags);
  9711. #endif
  9712. memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
  9713. unlock_user_struct(target_stfs, arg3, 1);
  9714. }
  9715. return ret;
  9716. case TARGET_NR_fstatfs64:
  9717. ret = get_errno(fstatfs(arg1, &stfs));
  9718. goto convert_statfs64;
  9719. #endif
  9720. #ifdef TARGET_NR_socketcall
  9721. case TARGET_NR_socketcall:
  9722. return do_socketcall(arg1, arg2);
  9723. #endif
  9724. #ifdef TARGET_NR_accept
  9725. case TARGET_NR_accept:
  9726. return do_accept4(arg1, arg2, arg3, 0);
  9727. #endif
  9728. #ifdef TARGET_NR_accept4
  9729. case TARGET_NR_accept4:
  9730. return do_accept4(arg1, arg2, arg3, arg4);
  9731. #endif
  9732. #ifdef TARGET_NR_bind
  9733. case TARGET_NR_bind:
  9734. return do_bind(arg1, arg2, arg3);
  9735. #endif
  9736. #ifdef TARGET_NR_connect
  9737. case TARGET_NR_connect:
  9738. return do_connect(arg1, arg2, arg3);
  9739. #endif
  9740. #ifdef TARGET_NR_getpeername
  9741. case TARGET_NR_getpeername:
  9742. return do_getpeername(arg1, arg2, arg3);
  9743. #endif
  9744. #ifdef TARGET_NR_getsockname
  9745. case TARGET_NR_getsockname:
  9746. return do_getsockname(arg1, arg2, arg3);
  9747. #endif
  9748. #ifdef TARGET_NR_getsockopt
  9749. case TARGET_NR_getsockopt:
  9750. return do_getsockopt(arg1, arg2, arg3, arg4, arg5);
  9751. #endif
  9752. #ifdef TARGET_NR_listen
  9753. case TARGET_NR_listen:
  9754. return get_errno(listen(arg1, arg2));
  9755. #endif
  9756. #ifdef TARGET_NR_recv
  9757. case TARGET_NR_recv:
  9758. return do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
  9759. #endif
  9760. #ifdef TARGET_NR_recvfrom
  9761. case TARGET_NR_recvfrom:
  9762. return do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
  9763. #endif
  9764. #ifdef TARGET_NR_recvmsg
  9765. case TARGET_NR_recvmsg:
  9766. return do_sendrecvmsg(arg1, arg2, arg3, 0);
  9767. #endif
  9768. #ifdef TARGET_NR_send
  9769. case TARGET_NR_send:
  9770. return do_sendto(arg1, arg2, arg3, arg4, 0, 0);
  9771. #endif
  9772. #ifdef TARGET_NR_sendmsg
  9773. case TARGET_NR_sendmsg:
  9774. return do_sendrecvmsg(arg1, arg2, arg3, 1);
  9775. #endif
  9776. #ifdef TARGET_NR_sendmmsg
  9777. case TARGET_NR_sendmmsg:
  9778. return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 1);
  9779. #endif
  9780. #ifdef TARGET_NR_recvmmsg
  9781. case TARGET_NR_recvmmsg:
  9782. return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 0);
  9783. #endif
  9784. #ifdef TARGET_NR_sendto
  9785. case TARGET_NR_sendto:
  9786. return do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
  9787. #endif
  9788. #ifdef TARGET_NR_shutdown
  9789. case TARGET_NR_shutdown:
  9790. return get_errno(shutdown(arg1, arg2));
  9791. #endif
  9792. #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
  9793. case TARGET_NR_getrandom:
  9794. p = lock_user(VERIFY_WRITE, arg1, arg2, 0);
  9795. if (!p) {
  9796. return -TARGET_EFAULT;
  9797. }
  9798. ret = get_errno(getrandom(p, arg2, arg3));
  9799. unlock_user(p, arg1, ret);
  9800. return ret;
  9801. #endif
  9802. #ifdef TARGET_NR_socket
  9803. case TARGET_NR_socket:
  9804. return do_socket(arg1, arg2, arg3);
  9805. #endif
  9806. #ifdef TARGET_NR_socketpair
  9807. case TARGET_NR_socketpair:
  9808. return do_socketpair(arg1, arg2, arg3, arg4);
  9809. #endif
  9810. #ifdef TARGET_NR_setsockopt
  9811. case TARGET_NR_setsockopt:
  9812. return do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
  9813. #endif
  9814. #if defined(TARGET_NR_syslog)
  9815. case TARGET_NR_syslog:
  9816. {
  9817. int len = arg2;
  9818. switch (arg1) {
  9819. case TARGET_SYSLOG_ACTION_CLOSE: /* Close log */
  9820. case TARGET_SYSLOG_ACTION_OPEN: /* Open log */
  9821. case TARGET_SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
  9822. case TARGET_SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging */
  9823. case TARGET_SYSLOG_ACTION_CONSOLE_ON: /* Enable logging */
  9824. case TARGET_SYSLOG_ACTION_CONSOLE_LEVEL: /* Set messages level */
  9825. case TARGET_SYSLOG_ACTION_SIZE_UNREAD: /* Number of chars */
  9826. case TARGET_SYSLOG_ACTION_SIZE_BUFFER: /* Size of the buffer */
  9827. return get_errno(sys_syslog((int)arg1, NULL, (int)arg3));
  9828. case TARGET_SYSLOG_ACTION_READ: /* Read from log */
  9829. case TARGET_SYSLOG_ACTION_READ_CLEAR: /* Read/clear msgs */
  9830. case TARGET_SYSLOG_ACTION_READ_ALL: /* Read last messages */
  9831. {
  9832. if (len < 0) {
  9833. return -TARGET_EINVAL;
  9834. }
  9835. if (len == 0) {
  9836. return 0;
  9837. }
  9838. p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  9839. if (!p) {
  9840. return -TARGET_EFAULT;
  9841. }
  9842. ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
  9843. unlock_user(p, arg2, arg3);
  9844. }
  9845. return ret;
  9846. default:
  9847. return -TARGET_EINVAL;
  9848. }
  9849. }
  9850. break;
  9851. #endif
  9852. case TARGET_NR_setitimer:
  9853. {
  9854. struct itimerval value, ovalue, *pvalue;
  9855. if (arg2) {
  9856. pvalue = &value;
  9857. if (copy_from_user_timeval(&pvalue->it_interval, arg2)
  9858. || copy_from_user_timeval(&pvalue->it_value,
  9859. arg2 + sizeof(struct target_timeval)))
  9860. return -TARGET_EFAULT;
  9861. } else {
  9862. pvalue = NULL;
  9863. }
  9864. ret = get_errno(setitimer(arg1, pvalue, &ovalue));
  9865. if (!is_error(ret) && arg3) {
  9866. if (copy_to_user_timeval(arg3,
  9867. &ovalue.it_interval)
  9868. || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
  9869. &ovalue.it_value))
  9870. return -TARGET_EFAULT;
  9871. }
  9872. }
  9873. return ret;
  9874. case TARGET_NR_getitimer:
  9875. {
  9876. struct itimerval value;
  9877. ret = get_errno(getitimer(arg1, &value));
  9878. if (!is_error(ret) && arg2) {
  9879. if (copy_to_user_timeval(arg2,
  9880. &value.it_interval)
  9881. || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
  9882. &value.it_value))
  9883. return -TARGET_EFAULT;
  9884. }
  9885. }
  9886. return ret;
  9887. #ifdef TARGET_NR_stat
  9888. case TARGET_NR_stat:
  9889. if (!(p = lock_user_string(arg1))) {
  9890. return -TARGET_EFAULT;
  9891. }
  9892. ret = get_errno(stat(path(p), &st));
  9893. unlock_user(p, arg1, 0);
  9894. goto do_stat;
  9895. #endif
  9896. #ifdef TARGET_NR_lstat
  9897. case TARGET_NR_lstat:
  9898. if (!(p = lock_user_string(arg1))) {
  9899. return -TARGET_EFAULT;
  9900. }
  9901. ret = get_errno(lstat(path(p), &st));
  9902. unlock_user(p, arg1, 0);
  9903. goto do_stat;
  9904. #endif
  9905. #ifdef TARGET_NR_fstat
  9906. case TARGET_NR_fstat:
  9907. {
  9908. ret = get_errno(fstat(arg1, &st));
  9909. #if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat)
  9910. do_stat:
  9911. #endif
  9912. if (!is_error(ret)) {
  9913. struct target_stat *target_st;
  9914. if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
  9915. return -TARGET_EFAULT;
  9916. memset(target_st, 0, sizeof(*target_st));
  9917. __put_user(st.st_dev, &target_st->st_dev);
  9918. __put_user(st.st_ino, &target_st->st_ino);
  9919. __put_user(st.st_mode, &target_st->st_mode);
  9920. __put_user(st.st_uid, &target_st->st_uid);
  9921. __put_user(st.st_gid, &target_st->st_gid);
  9922. __put_user(st.st_nlink, &target_st->st_nlink);
  9923. __put_user(st.st_rdev, &target_st->st_rdev);
  9924. __put_user(st.st_size, &target_st->st_size);
  9925. __put_user(st.st_blksize, &target_st->st_blksize);
  9926. __put_user(st.st_blocks, &target_st->st_blocks);
  9927. __put_user(st.st_atime, &target_st->target_st_atime);
  9928. __put_user(st.st_mtime, &target_st->target_st_mtime);
  9929. __put_user(st.st_ctime, &target_st->target_st_ctime);
  9930. #if defined(HAVE_STRUCT_STAT_ST_ATIM) && defined(TARGET_STAT_HAVE_NSEC)
  9931. __put_user(st.st_atim.tv_nsec,
  9932. &target_st->target_st_atime_nsec);
  9933. __put_user(st.st_mtim.tv_nsec,
  9934. &target_st->target_st_mtime_nsec);
  9935. __put_user(st.st_ctim.tv_nsec,
  9936. &target_st->target_st_ctime_nsec);
  9937. #endif
  9938. unlock_user_struct(target_st, arg2, 1);
  9939. }
  9940. }
  9941. return ret;
  9942. #endif
  9943. case TARGET_NR_vhangup:
  9944. return get_errno(vhangup());
  9945. #ifdef TARGET_NR_syscall
  9946. case TARGET_NR_syscall:
  9947. return do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
  9948. arg6, arg7, arg8, 0);
  9949. #endif
  9950. #if defined(TARGET_NR_wait4)
  9951. case TARGET_NR_wait4:
  9952. {
  9953. int status;
  9954. abi_long status_ptr = arg2;
  9955. struct rusage rusage, *rusage_ptr;
  9956. abi_ulong target_rusage = arg4;
  9957. abi_long rusage_err;
  9958. if (target_rusage)
  9959. rusage_ptr = &rusage;
  9960. else
  9961. rusage_ptr = NULL;
  9962. ret = get_errno(safe_wait4(arg1, &status, arg3, rusage_ptr));
  9963. if (!is_error(ret)) {
  9964. if (status_ptr && ret) {
  9965. status = host_to_target_waitstatus(status);
  9966. if (put_user_s32(status, status_ptr))
  9967. return -TARGET_EFAULT;
  9968. }
  9969. if (target_rusage) {
  9970. rusage_err = host_to_target_rusage(target_rusage, &rusage);
  9971. if (rusage_err) {
  9972. ret = rusage_err;
  9973. }
  9974. }
  9975. }
  9976. }
  9977. return ret;
  9978. #endif
  9979. #ifdef TARGET_NR_swapoff
  9980. case TARGET_NR_swapoff:
  9981. if (!(p = lock_user_string(arg1)))
  9982. return -TARGET_EFAULT;
  9983. ret = get_errno(swapoff(p));
  9984. unlock_user(p, arg1, 0);
  9985. return ret;
  9986. #endif
  9987. case TARGET_NR_sysinfo:
  9988. {
  9989. struct target_sysinfo *target_value;
  9990. struct sysinfo value;
  9991. ret = get_errno(sysinfo(&value));
  9992. if (!is_error(ret) && arg1)
  9993. {
  9994. if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
  9995. return -TARGET_EFAULT;
  9996. __put_user(value.uptime, &target_value->uptime);
  9997. __put_user(value.loads[0], &target_value->loads[0]);
  9998. __put_user(value.loads[1], &target_value->loads[1]);
  9999. __put_user(value.loads[2], &target_value->loads[2]);
  10000. __put_user(value.totalram, &target_value->totalram);
  10001. __put_user(value.freeram, &target_value->freeram);
  10002. __put_user(value.sharedram, &target_value->sharedram);
  10003. __put_user(value.bufferram, &target_value->bufferram);
  10004. __put_user(value.totalswap, &target_value->totalswap);
  10005. __put_user(value.freeswap, &target_value->freeswap);
  10006. __put_user(value.procs, &target_value->procs);
  10007. __put_user(value.totalhigh, &target_value->totalhigh);
  10008. __put_user(value.freehigh, &target_value->freehigh);
  10009. __put_user(value.mem_unit, &target_value->mem_unit);
  10010. unlock_user_struct(target_value, arg1, 1);
  10011. }
  10012. }
  10013. return ret;
  10014. #ifdef TARGET_NR_ipc
  10015. case TARGET_NR_ipc:
  10016. return do_ipc(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6);
  10017. #endif
  10018. #ifdef TARGET_NR_semget
  10019. case TARGET_NR_semget:
  10020. return get_errno(semget(arg1, arg2, arg3));
  10021. #endif
  10022. #ifdef TARGET_NR_semop
  10023. case TARGET_NR_semop:
  10024. return do_semtimedop(arg1, arg2, arg3, 0, false);
  10025. #endif
  10026. #ifdef TARGET_NR_semtimedop
  10027. case TARGET_NR_semtimedop:
  10028. return do_semtimedop(arg1, arg2, arg3, arg4, false);
  10029. #endif
  10030. #ifdef TARGET_NR_semtimedop_time64
  10031. case TARGET_NR_semtimedop_time64:
  10032. return do_semtimedop(arg1, arg2, arg3, arg4, true);
  10033. #endif
  10034. #ifdef TARGET_NR_semctl
  10035. case TARGET_NR_semctl:
  10036. return do_semctl(arg1, arg2, arg3, arg4);
  10037. #endif
  10038. #ifdef TARGET_NR_msgctl
  10039. case TARGET_NR_msgctl:
  10040. return do_msgctl(arg1, arg2, arg3);
  10041. #endif
  10042. #ifdef TARGET_NR_msgget
  10043. case TARGET_NR_msgget:
  10044. return get_errno(msgget(arg1, arg2));
  10045. #endif
  10046. #ifdef TARGET_NR_msgrcv
  10047. case TARGET_NR_msgrcv:
  10048. return do_msgrcv(arg1, arg2, arg3, arg4, arg5);
  10049. #endif
  10050. #ifdef TARGET_NR_msgsnd
  10051. case TARGET_NR_msgsnd:
  10052. return do_msgsnd(arg1, arg2, arg3, arg4);
  10053. #endif
  10054. #ifdef TARGET_NR_shmget
  10055. case TARGET_NR_shmget:
  10056. return get_errno(shmget(arg1, arg2, arg3));
  10057. #endif
  10058. #ifdef TARGET_NR_shmctl
  10059. case TARGET_NR_shmctl:
  10060. return do_shmctl(arg1, arg2, arg3);
  10061. #endif
  10062. #ifdef TARGET_NR_shmat
  10063. case TARGET_NR_shmat:
  10064. return target_shmat(cpu_env, arg1, arg2, arg3);
  10065. #endif
  10066. #ifdef TARGET_NR_shmdt
  10067. case TARGET_NR_shmdt:
  10068. return target_shmdt(arg1);
  10069. #endif
  10070. case TARGET_NR_fsync:
  10071. return get_errno(fsync(arg1));
  10072. case TARGET_NR_clone:
  10073. /* Linux manages to have three different orderings for its
  10074. * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
  10075. * match the kernel's CONFIG_CLONE_* settings.
  10076. * Microblaze is further special in that it uses a sixth
  10077. * implicit argument to clone for the TLS pointer.
  10078. */
  10079. #if defined(TARGET_MICROBLAZE)
  10080. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5));
  10081. #elif defined(TARGET_CLONE_BACKWARDS)
  10082. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
  10083. #elif defined(TARGET_CLONE_BACKWARDS2)
  10084. ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
  10085. #else
  10086. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
  10087. #endif
  10088. return ret;
  10089. #ifdef __NR_exit_group
  10090. /* new thread calls */
  10091. case TARGET_NR_exit_group:
  10092. preexit_cleanup(cpu_env, arg1);
  10093. return get_errno(exit_group(arg1));
  10094. #endif
  10095. case TARGET_NR_setdomainname:
  10096. if (!(p = lock_user_string(arg1)))
  10097. return -TARGET_EFAULT;
  10098. ret = get_errno(setdomainname(p, arg2));
  10099. unlock_user(p, arg1, 0);
  10100. return ret;
  10101. case TARGET_NR_uname:
  10102. /* no need to transcode because we use the linux syscall */
  10103. {
  10104. struct new_utsname * buf;
  10105. if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
  10106. return -TARGET_EFAULT;
  10107. ret = get_errno(sys_uname(buf));
  10108. if (!is_error(ret)) {
  10109. /* Overwrite the native machine name with whatever is being
  10110. emulated. */
  10111. g_strlcpy(buf->machine, cpu_to_uname_machine(cpu_env),
  10112. sizeof(buf->machine));
  10113. /* Allow the user to override the reported release. */
  10114. if (qemu_uname_release && *qemu_uname_release) {
  10115. g_strlcpy(buf->release, qemu_uname_release,
  10116. sizeof(buf->release));
  10117. }
  10118. }
  10119. unlock_user_struct(buf, arg1, 1);
  10120. }
  10121. return ret;
  10122. #ifdef TARGET_I386
  10123. case TARGET_NR_modify_ldt:
  10124. return do_modify_ldt(cpu_env, arg1, arg2, arg3);
  10125. #if !defined(TARGET_X86_64)
  10126. case TARGET_NR_vm86:
  10127. return do_vm86(cpu_env, arg1, arg2);
  10128. #endif
  10129. #endif
  10130. #if defined(TARGET_NR_adjtimex)
  10131. case TARGET_NR_adjtimex:
  10132. {
  10133. struct timex host_buf;
  10134. if (target_to_host_timex(&host_buf, arg1) != 0) {
  10135. return -TARGET_EFAULT;
  10136. }
  10137. ret = get_errno(adjtimex(&host_buf));
  10138. if (!is_error(ret)) {
  10139. if (host_to_target_timex(arg1, &host_buf) != 0) {
  10140. return -TARGET_EFAULT;
  10141. }
  10142. }
  10143. }
  10144. return ret;
  10145. #endif
  10146. #if defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME)
  10147. case TARGET_NR_clock_adjtime:
  10148. {
  10149. struct timex htx;
  10150. if (target_to_host_timex(&htx, arg2) != 0) {
  10151. return -TARGET_EFAULT;
  10152. }
  10153. ret = get_errno(clock_adjtime(arg1, &htx));
  10154. if (!is_error(ret) && host_to_target_timex(arg2, &htx)) {
  10155. return -TARGET_EFAULT;
  10156. }
  10157. }
  10158. return ret;
  10159. #endif
  10160. #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
  10161. case TARGET_NR_clock_adjtime64:
  10162. {
  10163. struct timex htx;
  10164. if (target_to_host_timex64(&htx, arg2) != 0) {
  10165. return -TARGET_EFAULT;
  10166. }
  10167. ret = get_errno(clock_adjtime(arg1, &htx));
  10168. if (!is_error(ret) && host_to_target_timex64(arg2, &htx)) {
  10169. return -TARGET_EFAULT;
  10170. }
  10171. }
  10172. return ret;
  10173. #endif
  10174. case TARGET_NR_getpgid:
  10175. return get_errno(getpgid(arg1));
  10176. case TARGET_NR_fchdir:
  10177. return get_errno(fchdir(arg1));
  10178. case TARGET_NR_personality:
  10179. return get_errno(personality(arg1));
  10180. #ifdef TARGET_NR__llseek /* Not on alpha */
  10181. case TARGET_NR__llseek:
  10182. {
  10183. int64_t res;
  10184. #if !defined(__NR_llseek)
  10185. res = lseek(arg1, ((uint64_t)arg2 << 32) | (abi_ulong)arg3, arg5);
  10186. if (res == -1) {
  10187. ret = get_errno(res);
  10188. } else {
  10189. ret = 0;
  10190. }
  10191. #else
  10192. ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
  10193. #endif
  10194. if ((ret == 0) && put_user_s64(res, arg4)) {
  10195. return -TARGET_EFAULT;
  10196. }
  10197. }
  10198. return ret;
  10199. #endif
  10200. #ifdef TARGET_NR_getdents
  10201. case TARGET_NR_getdents:
  10202. return do_getdents(arg1, arg2, arg3);
  10203. #endif /* TARGET_NR_getdents */
  10204. #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
  10205. case TARGET_NR_getdents64:
  10206. return do_getdents64(arg1, arg2, arg3);
  10207. #endif /* TARGET_NR_getdents64 */
  10208. #if defined(TARGET_NR__newselect)
  10209. case TARGET_NR__newselect:
  10210. return do_select(arg1, arg2, arg3, arg4, arg5);
  10211. #endif
  10212. #ifdef TARGET_NR_poll
  10213. case TARGET_NR_poll:
  10214. return do_ppoll(arg1, arg2, arg3, arg4, arg5, false, false);
  10215. #endif
  10216. #ifdef TARGET_NR_ppoll
  10217. case TARGET_NR_ppoll:
  10218. return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, false);
  10219. #endif
  10220. #ifdef TARGET_NR_ppoll_time64
  10221. case TARGET_NR_ppoll_time64:
  10222. return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, true);
  10223. #endif
  10224. case TARGET_NR_flock:
  10225. /* NOTE: the flock constant seems to be the same for every
  10226. Linux platform */
  10227. return get_errno(safe_flock(arg1, arg2));
  10228. case TARGET_NR_readv:
  10229. {
  10230. struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
  10231. if (vec != NULL) {
  10232. ret = get_errno(safe_readv(arg1, vec, arg3));
  10233. unlock_iovec(vec, arg2, arg3, 1);
  10234. } else {
  10235. ret = -host_to_target_errno(errno);
  10236. }
  10237. }
  10238. return ret;
  10239. case TARGET_NR_writev:
  10240. {
  10241. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  10242. if (vec != NULL) {
  10243. ret = get_errno(safe_writev(arg1, vec, arg3));
  10244. unlock_iovec(vec, arg2, arg3, 0);
  10245. } else {
  10246. ret = -host_to_target_errno(errno);
  10247. }
  10248. }
  10249. return ret;
  10250. #if defined(TARGET_NR_preadv)
  10251. case TARGET_NR_preadv:
  10252. {
  10253. struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
  10254. if (vec != NULL) {
  10255. unsigned long low, high;
  10256. target_to_host_low_high(arg4, arg5, &low, &high);
  10257. ret = get_errno(safe_preadv(arg1, vec, arg3, low, high));
  10258. unlock_iovec(vec, arg2, arg3, 1);
  10259. } else {
  10260. ret = -host_to_target_errno(errno);
  10261. }
  10262. }
  10263. return ret;
  10264. #endif
  10265. #if defined(TARGET_NR_pwritev)
  10266. case TARGET_NR_pwritev:
  10267. {
  10268. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  10269. if (vec != NULL) {
  10270. unsigned long low, high;
  10271. target_to_host_low_high(arg4, arg5, &low, &high);
  10272. ret = get_errno(safe_pwritev(arg1, vec, arg3, low, high));
  10273. unlock_iovec(vec, arg2, arg3, 0);
  10274. } else {
  10275. ret = -host_to_target_errno(errno);
  10276. }
  10277. }
  10278. return ret;
  10279. #endif
  10280. case TARGET_NR_getsid:
  10281. return get_errno(getsid(arg1));
  10282. #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
  10283. case TARGET_NR_fdatasync:
  10284. return get_errno(fdatasync(arg1));
  10285. #endif
  10286. case TARGET_NR_sched_getaffinity:
  10287. {
  10288. unsigned int mask_size;
  10289. unsigned long *mask;
  10290. /*
  10291. * sched_getaffinity needs multiples of ulong, so need to take
  10292. * care of mismatches between target ulong and host ulong sizes.
  10293. */
  10294. if (arg2 & (sizeof(abi_ulong) - 1)) {
  10295. return -TARGET_EINVAL;
  10296. }
  10297. mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
  10298. mask = alloca(mask_size);
  10299. memset(mask, 0, mask_size);
  10300. ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
  10301. if (!is_error(ret)) {
  10302. if (ret > arg2) {
  10303. /* More data returned than the caller's buffer will fit.
  10304. * This only happens if sizeof(abi_long) < sizeof(long)
  10305. * and the caller passed us a buffer holding an odd number
  10306. * of abi_longs. If the host kernel is actually using the
  10307. * extra 4 bytes then fail EINVAL; otherwise we can just
  10308. * ignore them and only copy the interesting part.
  10309. */
  10310. int numcpus = sysconf(_SC_NPROCESSORS_CONF);
  10311. if (numcpus > arg2 * 8) {
  10312. return -TARGET_EINVAL;
  10313. }
  10314. ret = arg2;
  10315. }
  10316. if (host_to_target_cpu_mask(mask, mask_size, arg3, ret)) {
  10317. return -TARGET_EFAULT;
  10318. }
  10319. }
  10320. }
  10321. return ret;
  10322. case TARGET_NR_sched_setaffinity:
  10323. {
  10324. unsigned int mask_size;
  10325. unsigned long *mask;
  10326. /*
  10327. * sched_setaffinity needs multiples of ulong, so need to take
  10328. * care of mismatches between target ulong and host ulong sizes.
  10329. */
  10330. if (arg2 & (sizeof(abi_ulong) - 1)) {
  10331. return -TARGET_EINVAL;
  10332. }
  10333. mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
  10334. mask = alloca(mask_size);
  10335. ret = target_to_host_cpu_mask(mask, mask_size, arg3, arg2);
  10336. if (ret) {
  10337. return ret;
  10338. }
  10339. return get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
  10340. }
  10341. case TARGET_NR_getcpu:
  10342. {
  10343. unsigned cpuid, node;
  10344. ret = get_errno(sys_getcpu(arg1 ? &cpuid : NULL,
  10345. arg2 ? &node : NULL,
  10346. NULL));
  10347. if (is_error(ret)) {
  10348. return ret;
  10349. }
  10350. if (arg1 && put_user_u32(cpuid, arg1)) {
  10351. return -TARGET_EFAULT;
  10352. }
  10353. if (arg2 && put_user_u32(node, arg2)) {
  10354. return -TARGET_EFAULT;
  10355. }
  10356. }
  10357. return ret;
  10358. case TARGET_NR_sched_setparam:
  10359. {
  10360. struct target_sched_param *target_schp;
  10361. struct sched_param schp;
  10362. if (arg2 == 0) {
  10363. return -TARGET_EINVAL;
  10364. }
  10365. if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1)) {
  10366. return -TARGET_EFAULT;
  10367. }
  10368. schp.sched_priority = tswap32(target_schp->sched_priority);
  10369. unlock_user_struct(target_schp, arg2, 0);
  10370. return get_errno(sys_sched_setparam(arg1, &schp));
  10371. }
  10372. case TARGET_NR_sched_getparam:
  10373. {
  10374. struct target_sched_param *target_schp;
  10375. struct sched_param schp;
  10376. if (arg2 == 0) {
  10377. return -TARGET_EINVAL;
  10378. }
  10379. ret = get_errno(sys_sched_getparam(arg1, &schp));
  10380. if (!is_error(ret)) {
  10381. if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0)) {
  10382. return -TARGET_EFAULT;
  10383. }
  10384. target_schp->sched_priority = tswap32(schp.sched_priority);
  10385. unlock_user_struct(target_schp, arg2, 1);
  10386. }
  10387. }
  10388. return ret;
  10389. case TARGET_NR_sched_setscheduler:
  10390. {
  10391. struct target_sched_param *target_schp;
  10392. struct sched_param schp;
  10393. if (arg3 == 0) {
  10394. return -TARGET_EINVAL;
  10395. }
  10396. if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1)) {
  10397. return -TARGET_EFAULT;
  10398. }
  10399. schp.sched_priority = tswap32(target_schp->sched_priority);
  10400. unlock_user_struct(target_schp, arg3, 0);
  10401. return get_errno(sys_sched_setscheduler(arg1, arg2, &schp));
  10402. }
  10403. case TARGET_NR_sched_getscheduler:
  10404. return get_errno(sys_sched_getscheduler(arg1));
  10405. case TARGET_NR_sched_getattr:
  10406. {
  10407. struct target_sched_attr *target_scha;
  10408. struct sched_attr scha;
  10409. if (arg2 == 0) {
  10410. return -TARGET_EINVAL;
  10411. }
  10412. if (arg3 > sizeof(scha)) {
  10413. arg3 = sizeof(scha);
  10414. }
  10415. ret = get_errno(sys_sched_getattr(arg1, &scha, arg3, arg4));
  10416. if (!is_error(ret)) {
  10417. target_scha = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  10418. if (!target_scha) {
  10419. return -TARGET_EFAULT;
  10420. }
  10421. target_scha->size = tswap32(scha.size);
  10422. target_scha->sched_policy = tswap32(scha.sched_policy);
  10423. target_scha->sched_flags = tswap64(scha.sched_flags);
  10424. target_scha->sched_nice = tswap32(scha.sched_nice);
  10425. target_scha->sched_priority = tswap32(scha.sched_priority);
  10426. target_scha->sched_runtime = tswap64(scha.sched_runtime);
  10427. target_scha->sched_deadline = tswap64(scha.sched_deadline);
  10428. target_scha->sched_period = tswap64(scha.sched_period);
  10429. if (scha.size > offsetof(struct sched_attr, sched_util_min)) {
  10430. target_scha->sched_util_min = tswap32(scha.sched_util_min);
  10431. target_scha->sched_util_max = tswap32(scha.sched_util_max);
  10432. }
  10433. unlock_user(target_scha, arg2, arg3);
  10434. }
  10435. return ret;
  10436. }
  10437. case TARGET_NR_sched_setattr:
  10438. {
  10439. struct target_sched_attr *target_scha;
  10440. struct sched_attr scha;
  10441. uint32_t size;
  10442. int zeroed;
  10443. if (arg2 == 0) {
  10444. return -TARGET_EINVAL;
  10445. }
  10446. if (get_user_u32(size, arg2)) {
  10447. return -TARGET_EFAULT;
  10448. }
  10449. if (!size) {
  10450. size = offsetof(struct target_sched_attr, sched_util_min);
  10451. }
  10452. if (size < offsetof(struct target_sched_attr, sched_util_min)) {
  10453. if (put_user_u32(sizeof(struct target_sched_attr), arg2)) {
  10454. return -TARGET_EFAULT;
  10455. }
  10456. return -TARGET_E2BIG;
  10457. }
  10458. zeroed = check_zeroed_user(arg2, sizeof(struct target_sched_attr), size);
  10459. if (zeroed < 0) {
  10460. return zeroed;
  10461. } else if (zeroed == 0) {
  10462. if (put_user_u32(sizeof(struct target_sched_attr), arg2)) {
  10463. return -TARGET_EFAULT;
  10464. }
  10465. return -TARGET_E2BIG;
  10466. }
  10467. if (size > sizeof(struct target_sched_attr)) {
  10468. size = sizeof(struct target_sched_attr);
  10469. }
  10470. target_scha = lock_user(VERIFY_READ, arg2, size, 1);
  10471. if (!target_scha) {
  10472. return -TARGET_EFAULT;
  10473. }
  10474. scha.size = size;
  10475. scha.sched_policy = tswap32(target_scha->sched_policy);
  10476. scha.sched_flags = tswap64(target_scha->sched_flags);
  10477. scha.sched_nice = tswap32(target_scha->sched_nice);
  10478. scha.sched_priority = tswap32(target_scha->sched_priority);
  10479. scha.sched_runtime = tswap64(target_scha->sched_runtime);
  10480. scha.sched_deadline = tswap64(target_scha->sched_deadline);
  10481. scha.sched_period = tswap64(target_scha->sched_period);
  10482. if (size > offsetof(struct target_sched_attr, sched_util_min)) {
  10483. scha.sched_util_min = tswap32(target_scha->sched_util_min);
  10484. scha.sched_util_max = tswap32(target_scha->sched_util_max);
  10485. }
  10486. unlock_user(target_scha, arg2, 0);
  10487. return get_errno(sys_sched_setattr(arg1, &scha, arg3));
  10488. }
  10489. case TARGET_NR_sched_yield:
  10490. return get_errno(sched_yield());
  10491. case TARGET_NR_sched_get_priority_max:
  10492. return get_errno(sched_get_priority_max(arg1));
  10493. case TARGET_NR_sched_get_priority_min:
  10494. return get_errno(sched_get_priority_min(arg1));
  10495. #ifdef TARGET_NR_sched_rr_get_interval
  10496. case TARGET_NR_sched_rr_get_interval:
  10497. {
  10498. struct timespec ts;
  10499. ret = get_errno(sched_rr_get_interval(arg1, &ts));
  10500. if (!is_error(ret)) {
  10501. ret = host_to_target_timespec(arg2, &ts);
  10502. }
  10503. }
  10504. return ret;
  10505. #endif
  10506. #ifdef TARGET_NR_sched_rr_get_interval_time64
  10507. case TARGET_NR_sched_rr_get_interval_time64:
  10508. {
  10509. struct timespec ts;
  10510. ret = get_errno(sched_rr_get_interval(arg1, &ts));
  10511. if (!is_error(ret)) {
  10512. ret = host_to_target_timespec64(arg2, &ts);
  10513. }
  10514. }
  10515. return ret;
  10516. #endif
  10517. #if defined(TARGET_NR_nanosleep)
  10518. case TARGET_NR_nanosleep:
  10519. {
  10520. struct timespec req, rem;
  10521. target_to_host_timespec(&req, arg1);
  10522. ret = get_errno(safe_nanosleep(&req, &rem));
  10523. if (is_error(ret) && arg2) {
  10524. host_to_target_timespec(arg2, &rem);
  10525. }
  10526. }
  10527. return ret;
  10528. #endif
  10529. case TARGET_NR_prctl:
  10530. return do_prctl(cpu_env, arg1, arg2, arg3, arg4, arg5);
  10531. break;
  10532. #ifdef TARGET_NR_arch_prctl
  10533. case TARGET_NR_arch_prctl:
  10534. return do_arch_prctl(cpu_env, arg1, arg2);
  10535. #endif
  10536. #ifdef TARGET_NR_pread64
  10537. case TARGET_NR_pread64:
  10538. if (regpairs_aligned(cpu_env, num)) {
  10539. arg4 = arg5;
  10540. arg5 = arg6;
  10541. }
  10542. if (arg2 == 0 && arg3 == 0) {
  10543. /* Special-case NULL buffer and zero length, which should succeed */
  10544. p = 0;
  10545. } else {
  10546. p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  10547. if (!p) {
  10548. return -TARGET_EFAULT;
  10549. }
  10550. }
  10551. ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
  10552. unlock_user(p, arg2, ret);
  10553. return ret;
  10554. case TARGET_NR_pwrite64:
  10555. if (regpairs_aligned(cpu_env, num)) {
  10556. arg4 = arg5;
  10557. arg5 = arg6;
  10558. }
  10559. if (arg2 == 0 && arg3 == 0) {
  10560. /* Special-case NULL buffer and zero length, which should succeed */
  10561. p = 0;
  10562. } else {
  10563. p = lock_user(VERIFY_READ, arg2, arg3, 1);
  10564. if (!p) {
  10565. return -TARGET_EFAULT;
  10566. }
  10567. }
  10568. ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
  10569. unlock_user(p, arg2, 0);
  10570. return ret;
  10571. #endif
  10572. case TARGET_NR_getcwd:
  10573. if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
  10574. return -TARGET_EFAULT;
  10575. ret = get_errno(sys_getcwd1(p, arg2));
  10576. unlock_user(p, arg1, ret);
  10577. return ret;
  10578. case TARGET_NR_capget:
  10579. case TARGET_NR_capset:
  10580. {
  10581. struct target_user_cap_header *target_header;
  10582. struct target_user_cap_data *target_data = NULL;
  10583. struct __user_cap_header_struct header;
  10584. struct __user_cap_data_struct data[2];
  10585. struct __user_cap_data_struct *dataptr = NULL;
  10586. int i, target_datalen;
  10587. int data_items = 1;
  10588. if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) {
  10589. return -TARGET_EFAULT;
  10590. }
  10591. header.version = tswap32(target_header->version);
  10592. header.pid = tswap32(target_header->pid);
  10593. if (header.version != _LINUX_CAPABILITY_VERSION) {
  10594. /* Version 2 and up takes pointer to two user_data structs */
  10595. data_items = 2;
  10596. }
  10597. target_datalen = sizeof(*target_data) * data_items;
  10598. if (arg2) {
  10599. if (num == TARGET_NR_capget) {
  10600. target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0);
  10601. } else {
  10602. target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1);
  10603. }
  10604. if (!target_data) {
  10605. unlock_user_struct(target_header, arg1, 0);
  10606. return -TARGET_EFAULT;
  10607. }
  10608. if (num == TARGET_NR_capset) {
  10609. for (i = 0; i < data_items; i++) {
  10610. data[i].effective = tswap32(target_data[i].effective);
  10611. data[i].permitted = tswap32(target_data[i].permitted);
  10612. data[i].inheritable = tswap32(target_data[i].inheritable);
  10613. }
  10614. }
  10615. dataptr = data;
  10616. }
  10617. if (num == TARGET_NR_capget) {
  10618. ret = get_errno(capget(&header, dataptr));
  10619. } else {
  10620. ret = get_errno(capset(&header, dataptr));
  10621. }
  10622. /* The kernel always updates version for both capget and capset */
  10623. target_header->version = tswap32(header.version);
  10624. unlock_user_struct(target_header, arg1, 1);
  10625. if (arg2) {
  10626. if (num == TARGET_NR_capget) {
  10627. for (i = 0; i < data_items; i++) {
  10628. target_data[i].effective = tswap32(data[i].effective);
  10629. target_data[i].permitted = tswap32(data[i].permitted);
  10630. target_data[i].inheritable = tswap32(data[i].inheritable);
  10631. }
  10632. unlock_user(target_data, arg2, target_datalen);
  10633. } else {
  10634. unlock_user(target_data, arg2, 0);
  10635. }
  10636. }
  10637. return ret;
  10638. }
  10639. case TARGET_NR_sigaltstack:
  10640. return do_sigaltstack(arg1, arg2, cpu_env);
  10641. #ifdef CONFIG_SENDFILE
  10642. #ifdef TARGET_NR_sendfile
  10643. case TARGET_NR_sendfile:
  10644. {
  10645. off_t *offp = NULL;
  10646. off_t off;
  10647. if (arg3) {
  10648. ret = get_user_sal(off, arg3);
  10649. if (is_error(ret)) {
  10650. return ret;
  10651. }
  10652. offp = &off;
  10653. }
  10654. ret = get_errno(sendfile(arg1, arg2, offp, arg4));
  10655. if (!is_error(ret) && arg3) {
  10656. abi_long ret2 = put_user_sal(off, arg3);
  10657. if (is_error(ret2)) {
  10658. ret = ret2;
  10659. }
  10660. }
  10661. return ret;
  10662. }
  10663. #endif
  10664. #ifdef TARGET_NR_sendfile64
  10665. case TARGET_NR_sendfile64:
  10666. {
  10667. off_t *offp = NULL;
  10668. off_t off;
  10669. if (arg3) {
  10670. ret = get_user_s64(off, arg3);
  10671. if (is_error(ret)) {
  10672. return ret;
  10673. }
  10674. offp = &off;
  10675. }
  10676. ret = get_errno(sendfile(arg1, arg2, offp, arg4));
  10677. if (!is_error(ret) && arg3) {
  10678. abi_long ret2 = put_user_s64(off, arg3);
  10679. if (is_error(ret2)) {
  10680. ret = ret2;
  10681. }
  10682. }
  10683. return ret;
  10684. }
  10685. #endif
  10686. #endif
  10687. #ifdef TARGET_NR_vfork
  10688. case TARGET_NR_vfork:
  10689. return get_errno(do_fork(cpu_env,
  10690. CLONE_VFORK | CLONE_VM | TARGET_SIGCHLD,
  10691. 0, 0, 0, 0));
  10692. #endif
  10693. #ifdef TARGET_NR_ugetrlimit
  10694. case TARGET_NR_ugetrlimit:
  10695. {
  10696. struct rlimit rlim;
  10697. int resource = target_to_host_resource(arg1);
  10698. ret = get_errno(getrlimit(resource, &rlim));
  10699. if (!is_error(ret)) {
  10700. struct target_rlimit *target_rlim;
  10701. if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
  10702. return -TARGET_EFAULT;
  10703. target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
  10704. target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
  10705. unlock_user_struct(target_rlim, arg2, 1);
  10706. }
  10707. return ret;
  10708. }
  10709. #endif
  10710. #ifdef TARGET_NR_truncate64
  10711. case TARGET_NR_truncate64:
  10712. if (!(p = lock_user_string(arg1)))
  10713. return -TARGET_EFAULT;
  10714. ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
  10715. unlock_user(p, arg1, 0);
  10716. return ret;
  10717. #endif
  10718. #ifdef TARGET_NR_ftruncate64
  10719. case TARGET_NR_ftruncate64:
  10720. return target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
  10721. #endif
  10722. #ifdef TARGET_NR_stat64
  10723. case TARGET_NR_stat64:
  10724. if (!(p = lock_user_string(arg1))) {
  10725. return -TARGET_EFAULT;
  10726. }
  10727. ret = get_errno(stat(path(p), &st));
  10728. unlock_user(p, arg1, 0);
  10729. if (!is_error(ret))
  10730. ret = host_to_target_stat64(cpu_env, arg2, &st);
  10731. return ret;
  10732. #endif
  10733. #ifdef TARGET_NR_lstat64
  10734. case TARGET_NR_lstat64:
  10735. if (!(p = lock_user_string(arg1))) {
  10736. return -TARGET_EFAULT;
  10737. }
  10738. ret = get_errno(lstat(path(p), &st));
  10739. unlock_user(p, arg1, 0);
  10740. if (!is_error(ret))
  10741. ret = host_to_target_stat64(cpu_env, arg2, &st);
  10742. return ret;
  10743. #endif
  10744. #ifdef TARGET_NR_fstat64
  10745. case TARGET_NR_fstat64:
  10746. ret = get_errno(fstat(arg1, &st));
  10747. if (!is_error(ret))
  10748. ret = host_to_target_stat64(cpu_env, arg2, &st);
  10749. return ret;
  10750. #endif
  10751. #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
  10752. #ifdef TARGET_NR_fstatat64
  10753. case TARGET_NR_fstatat64:
  10754. #endif
  10755. #ifdef TARGET_NR_newfstatat
  10756. case TARGET_NR_newfstatat:
  10757. #endif
  10758. if (!(p = lock_user_string(arg2))) {
  10759. return -TARGET_EFAULT;
  10760. }
  10761. ret = get_errno(fstatat(arg1, path(p), &st, arg4));
  10762. unlock_user(p, arg2, 0);
  10763. if (!is_error(ret))
  10764. ret = host_to_target_stat64(cpu_env, arg3, &st);
  10765. return ret;
  10766. #endif
  10767. #if defined(TARGET_NR_statx)
  10768. case TARGET_NR_statx:
  10769. {
  10770. struct target_statx *target_stx;
  10771. int dirfd = arg1;
  10772. int flags = arg3;
  10773. p = lock_user_string(arg2);
  10774. if (p == NULL) {
  10775. return -TARGET_EFAULT;
  10776. }
  10777. #if defined(__NR_statx)
  10778. {
  10779. /*
  10780. * It is assumed that struct statx is architecture independent.
  10781. */
  10782. struct target_statx host_stx;
  10783. int mask = arg4;
  10784. ret = get_errno(sys_statx(dirfd, p, flags, mask, &host_stx));
  10785. if (!is_error(ret)) {
  10786. if (host_to_target_statx(&host_stx, arg5) != 0) {
  10787. unlock_user(p, arg2, 0);
  10788. return -TARGET_EFAULT;
  10789. }
  10790. }
  10791. if (ret != -TARGET_ENOSYS) {
  10792. unlock_user(p, arg2, 0);
  10793. return ret;
  10794. }
  10795. }
  10796. #endif
  10797. ret = get_errno(fstatat(dirfd, path(p), &st, flags));
  10798. unlock_user(p, arg2, 0);
  10799. if (!is_error(ret)) {
  10800. if (!lock_user_struct(VERIFY_WRITE, target_stx, arg5, 0)) {
  10801. return -TARGET_EFAULT;
  10802. }
  10803. memset(target_stx, 0, sizeof(*target_stx));
  10804. __put_user(major(st.st_dev), &target_stx->stx_dev_major);
  10805. __put_user(minor(st.st_dev), &target_stx->stx_dev_minor);
  10806. __put_user(st.st_ino, &target_stx->stx_ino);
  10807. __put_user(st.st_mode, &target_stx->stx_mode);
  10808. __put_user(st.st_uid, &target_stx->stx_uid);
  10809. __put_user(st.st_gid, &target_stx->stx_gid);
  10810. __put_user(st.st_nlink, &target_stx->stx_nlink);
  10811. __put_user(major(st.st_rdev), &target_stx->stx_rdev_major);
  10812. __put_user(minor(st.st_rdev), &target_stx->stx_rdev_minor);
  10813. __put_user(st.st_size, &target_stx->stx_size);
  10814. __put_user(st.st_blksize, &target_stx->stx_blksize);
  10815. __put_user(st.st_blocks, &target_stx->stx_blocks);
  10816. __put_user(st.st_atime, &target_stx->stx_atime.tv_sec);
  10817. __put_user(st.st_mtime, &target_stx->stx_mtime.tv_sec);
  10818. __put_user(st.st_ctime, &target_stx->stx_ctime.tv_sec);
  10819. unlock_user_struct(target_stx, arg5, 1);
  10820. }
  10821. }
  10822. return ret;
  10823. #endif
  10824. #ifdef TARGET_NR_lchown
  10825. case TARGET_NR_lchown:
  10826. if (!(p = lock_user_string(arg1)))
  10827. return -TARGET_EFAULT;
  10828. ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
  10829. unlock_user(p, arg1, 0);
  10830. return ret;
  10831. #endif
  10832. #ifdef TARGET_NR_getuid
  10833. case TARGET_NR_getuid:
  10834. return get_errno(high2lowuid(getuid()));
  10835. #endif
  10836. #ifdef TARGET_NR_getgid
  10837. case TARGET_NR_getgid:
  10838. return get_errno(high2lowgid(getgid()));
  10839. #endif
  10840. #ifdef TARGET_NR_geteuid
  10841. case TARGET_NR_geteuid:
  10842. return get_errno(high2lowuid(geteuid()));
  10843. #endif
  10844. #ifdef TARGET_NR_getegid
  10845. case TARGET_NR_getegid:
  10846. return get_errno(high2lowgid(getegid()));
  10847. #endif
  10848. case TARGET_NR_setreuid:
  10849. return get_errno(sys_setreuid(low2highuid(arg1), low2highuid(arg2)));
  10850. case TARGET_NR_setregid:
  10851. return get_errno(sys_setregid(low2highgid(arg1), low2highgid(arg2)));
  10852. case TARGET_NR_getgroups:
  10853. { /* the same code as for TARGET_NR_getgroups32 */
  10854. int gidsetsize = arg1;
  10855. target_id *target_grouplist;
  10856. g_autofree gid_t *grouplist = NULL;
  10857. int i;
  10858. if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) {
  10859. return -TARGET_EINVAL;
  10860. }
  10861. if (gidsetsize > 0) {
  10862. grouplist = g_try_new(gid_t, gidsetsize);
  10863. if (!grouplist) {
  10864. return -TARGET_ENOMEM;
  10865. }
  10866. }
  10867. ret = get_errno(getgroups(gidsetsize, grouplist));
  10868. if (!is_error(ret) && gidsetsize > 0) {
  10869. target_grouplist = lock_user(VERIFY_WRITE, arg2,
  10870. gidsetsize * sizeof(target_id), 0);
  10871. if (!target_grouplist) {
  10872. return -TARGET_EFAULT;
  10873. }
  10874. for (i = 0; i < ret; i++) {
  10875. target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
  10876. }
  10877. unlock_user(target_grouplist, arg2,
  10878. gidsetsize * sizeof(target_id));
  10879. }
  10880. return ret;
  10881. }
  10882. case TARGET_NR_setgroups:
  10883. { /* the same code as for TARGET_NR_setgroups32 */
  10884. int gidsetsize = arg1;
  10885. target_id *target_grouplist;
  10886. g_autofree gid_t *grouplist = NULL;
  10887. int i;
  10888. if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) {
  10889. return -TARGET_EINVAL;
  10890. }
  10891. if (gidsetsize > 0) {
  10892. grouplist = g_try_new(gid_t, gidsetsize);
  10893. if (!grouplist) {
  10894. return -TARGET_ENOMEM;
  10895. }
  10896. target_grouplist = lock_user(VERIFY_READ, arg2,
  10897. gidsetsize * sizeof(target_id), 1);
  10898. if (!target_grouplist) {
  10899. return -TARGET_EFAULT;
  10900. }
  10901. for (i = 0; i < gidsetsize; i++) {
  10902. grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
  10903. }
  10904. unlock_user(target_grouplist, arg2,
  10905. gidsetsize * sizeof(target_id));
  10906. }
  10907. return get_errno(sys_setgroups(gidsetsize, grouplist));
  10908. }
  10909. case TARGET_NR_fchown:
  10910. return get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
  10911. #if defined(TARGET_NR_fchownat)
  10912. case TARGET_NR_fchownat:
  10913. if (!(p = lock_user_string(arg2)))
  10914. return -TARGET_EFAULT;
  10915. ret = get_errno(fchownat(arg1, p, low2highuid(arg3),
  10916. low2highgid(arg4), arg5));
  10917. unlock_user(p, arg2, 0);
  10918. return ret;
  10919. #endif
  10920. #ifdef TARGET_NR_setresuid
  10921. case TARGET_NR_setresuid:
  10922. return get_errno(sys_setresuid(low2highuid(arg1),
  10923. low2highuid(arg2),
  10924. low2highuid(arg3)));
  10925. #endif
  10926. #ifdef TARGET_NR_getresuid
  10927. case TARGET_NR_getresuid:
  10928. {
  10929. uid_t ruid, euid, suid;
  10930. ret = get_errno(getresuid(&ruid, &euid, &suid));
  10931. if (!is_error(ret)) {
  10932. if (put_user_id(high2lowuid(ruid), arg1)
  10933. || put_user_id(high2lowuid(euid), arg2)
  10934. || put_user_id(high2lowuid(suid), arg3))
  10935. return -TARGET_EFAULT;
  10936. }
  10937. }
  10938. return ret;
  10939. #endif
  10940. #ifdef TARGET_NR_getresgid
  10941. case TARGET_NR_setresgid:
  10942. return get_errno(sys_setresgid(low2highgid(arg1),
  10943. low2highgid(arg2),
  10944. low2highgid(arg3)));
  10945. #endif
  10946. #ifdef TARGET_NR_getresgid
  10947. case TARGET_NR_getresgid:
  10948. {
  10949. gid_t rgid, egid, sgid;
  10950. ret = get_errno(getresgid(&rgid, &egid, &sgid));
  10951. if (!is_error(ret)) {
  10952. if (put_user_id(high2lowgid(rgid), arg1)
  10953. || put_user_id(high2lowgid(egid), arg2)
  10954. || put_user_id(high2lowgid(sgid), arg3))
  10955. return -TARGET_EFAULT;
  10956. }
  10957. }
  10958. return ret;
  10959. #endif
  10960. #ifdef TARGET_NR_chown
  10961. case TARGET_NR_chown:
  10962. if (!(p = lock_user_string(arg1)))
  10963. return -TARGET_EFAULT;
  10964. ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
  10965. unlock_user(p, arg1, 0);
  10966. return ret;
  10967. #endif
  10968. case TARGET_NR_setuid:
  10969. return get_errno(sys_setuid(low2highuid(arg1)));
  10970. case TARGET_NR_setgid:
  10971. return get_errno(sys_setgid(low2highgid(arg1)));
  10972. case TARGET_NR_setfsuid:
  10973. return get_errno(setfsuid(arg1));
  10974. case TARGET_NR_setfsgid:
  10975. return get_errno(setfsgid(arg1));
  10976. #ifdef TARGET_NR_lchown32
  10977. case TARGET_NR_lchown32:
  10978. if (!(p = lock_user_string(arg1)))
  10979. return -TARGET_EFAULT;
  10980. ret = get_errno(lchown(p, arg2, arg3));
  10981. unlock_user(p, arg1, 0);
  10982. return ret;
  10983. #endif
  10984. #ifdef TARGET_NR_getuid32
  10985. case TARGET_NR_getuid32:
  10986. return get_errno(getuid());
  10987. #endif
  10988. #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
  10989. /* Alpha specific */
  10990. case TARGET_NR_getxuid:
  10991. {
  10992. uid_t euid;
  10993. euid=geteuid();
  10994. cpu_env->ir[IR_A4]=euid;
  10995. }
  10996. return get_errno(getuid());
  10997. #endif
  10998. #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
  10999. /* Alpha specific */
  11000. case TARGET_NR_getxgid:
  11001. {
  11002. uid_t egid;
  11003. egid=getegid();
  11004. cpu_env->ir[IR_A4]=egid;
  11005. }
  11006. return get_errno(getgid());
  11007. #endif
  11008. #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
  11009. /* Alpha specific */
  11010. case TARGET_NR_osf_getsysinfo:
  11011. ret = -TARGET_EOPNOTSUPP;
  11012. switch (arg1) {
  11013. case TARGET_GSI_IEEE_FP_CONTROL:
  11014. {
  11015. uint64_t fpcr = cpu_alpha_load_fpcr(cpu_env);
  11016. uint64_t swcr = cpu_env->swcr;
  11017. swcr &= ~SWCR_STATUS_MASK;
  11018. swcr |= (fpcr >> 35) & SWCR_STATUS_MASK;
  11019. if (put_user_u64 (swcr, arg2))
  11020. return -TARGET_EFAULT;
  11021. ret = 0;
  11022. }
  11023. break;
  11024. /* case GSI_IEEE_STATE_AT_SIGNAL:
  11025. -- Not implemented in linux kernel.
  11026. case GSI_UACPROC:
  11027. -- Retrieves current unaligned access state; not much used.
  11028. case GSI_PROC_TYPE:
  11029. -- Retrieves implver information; surely not used.
  11030. case GSI_GET_HWRPB:
  11031. -- Grabs a copy of the HWRPB; surely not used.
  11032. */
  11033. }
  11034. return ret;
  11035. #endif
  11036. #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
  11037. /* Alpha specific */
  11038. case TARGET_NR_osf_setsysinfo:
  11039. ret = -TARGET_EOPNOTSUPP;
  11040. switch (arg1) {
  11041. case TARGET_SSI_IEEE_FP_CONTROL:
  11042. {
  11043. uint64_t swcr, fpcr;
  11044. if (get_user_u64 (swcr, arg2)) {
  11045. return -TARGET_EFAULT;
  11046. }
  11047. /*
  11048. * The kernel calls swcr_update_status to update the
  11049. * status bits from the fpcr at every point that it
  11050. * could be queried. Therefore, we store the status
  11051. * bits only in FPCR.
  11052. */
  11053. cpu_env->swcr = swcr & (SWCR_TRAP_ENABLE_MASK | SWCR_MAP_MASK);
  11054. fpcr = cpu_alpha_load_fpcr(cpu_env);
  11055. fpcr &= ((uint64_t)FPCR_DYN_MASK << 32);
  11056. fpcr |= alpha_ieee_swcr_to_fpcr(swcr);
  11057. cpu_alpha_store_fpcr(cpu_env, fpcr);
  11058. ret = 0;
  11059. }
  11060. break;
  11061. case TARGET_SSI_IEEE_RAISE_EXCEPTION:
  11062. {
  11063. uint64_t exc, fpcr, fex;
  11064. if (get_user_u64(exc, arg2)) {
  11065. return -TARGET_EFAULT;
  11066. }
  11067. exc &= SWCR_STATUS_MASK;
  11068. fpcr = cpu_alpha_load_fpcr(cpu_env);
  11069. /* Old exceptions are not signaled. */
  11070. fex = alpha_ieee_fpcr_to_swcr(fpcr);
  11071. fex = exc & ~fex;
  11072. fex >>= SWCR_STATUS_TO_EXCSUM_SHIFT;
  11073. fex &= (cpu_env)->swcr;
  11074. /* Update the hardware fpcr. */
  11075. fpcr |= alpha_ieee_swcr_to_fpcr(exc);
  11076. cpu_alpha_store_fpcr(cpu_env, fpcr);
  11077. if (fex) {
  11078. int si_code = TARGET_FPE_FLTUNK;
  11079. target_siginfo_t info;
  11080. if (fex & SWCR_TRAP_ENABLE_DNO) {
  11081. si_code = TARGET_FPE_FLTUND;
  11082. }
  11083. if (fex & SWCR_TRAP_ENABLE_INE) {
  11084. si_code = TARGET_FPE_FLTRES;
  11085. }
  11086. if (fex & SWCR_TRAP_ENABLE_UNF) {
  11087. si_code = TARGET_FPE_FLTUND;
  11088. }
  11089. if (fex & SWCR_TRAP_ENABLE_OVF) {
  11090. si_code = TARGET_FPE_FLTOVF;
  11091. }
  11092. if (fex & SWCR_TRAP_ENABLE_DZE) {
  11093. si_code = TARGET_FPE_FLTDIV;
  11094. }
  11095. if (fex & SWCR_TRAP_ENABLE_INV) {
  11096. si_code = TARGET_FPE_FLTINV;
  11097. }
  11098. info.si_signo = SIGFPE;
  11099. info.si_errno = 0;
  11100. info.si_code = si_code;
  11101. info._sifields._sigfault._addr = (cpu_env)->pc;
  11102. queue_signal(cpu_env, info.si_signo,
  11103. QEMU_SI_FAULT, &info);
  11104. }
  11105. ret = 0;
  11106. }
  11107. break;
  11108. /* case SSI_NVPAIRS:
  11109. -- Used with SSIN_UACPROC to enable unaligned accesses.
  11110. case SSI_IEEE_STATE_AT_SIGNAL:
  11111. case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
  11112. -- Not implemented in linux kernel
  11113. */
  11114. }
  11115. return ret;
  11116. #endif
  11117. #ifdef TARGET_NR_osf_sigprocmask
  11118. /* Alpha specific. */
  11119. case TARGET_NR_osf_sigprocmask:
  11120. {
  11121. abi_ulong mask;
  11122. int how;
  11123. sigset_t set, oldset;
  11124. switch(arg1) {
  11125. case TARGET_SIG_BLOCK:
  11126. how = SIG_BLOCK;
  11127. break;
  11128. case TARGET_SIG_UNBLOCK:
  11129. how = SIG_UNBLOCK;
  11130. break;
  11131. case TARGET_SIG_SETMASK:
  11132. how = SIG_SETMASK;
  11133. break;
  11134. default:
  11135. return -TARGET_EINVAL;
  11136. }
  11137. mask = arg2;
  11138. target_to_host_old_sigset(&set, &mask);
  11139. ret = do_sigprocmask(how, &set, &oldset);
  11140. if (!ret) {
  11141. host_to_target_old_sigset(&mask, &oldset);
  11142. ret = mask;
  11143. }
  11144. }
  11145. return ret;
  11146. #endif
  11147. #ifdef TARGET_NR_getgid32
  11148. case TARGET_NR_getgid32:
  11149. return get_errno(getgid());
  11150. #endif
  11151. #ifdef TARGET_NR_geteuid32
  11152. case TARGET_NR_geteuid32:
  11153. return get_errno(geteuid());
  11154. #endif
  11155. #ifdef TARGET_NR_getegid32
  11156. case TARGET_NR_getegid32:
  11157. return get_errno(getegid());
  11158. #endif
  11159. #ifdef TARGET_NR_setreuid32
  11160. case TARGET_NR_setreuid32:
  11161. return get_errno(sys_setreuid(arg1, arg2));
  11162. #endif
  11163. #ifdef TARGET_NR_setregid32
  11164. case TARGET_NR_setregid32:
  11165. return get_errno(sys_setregid(arg1, arg2));
  11166. #endif
  11167. #ifdef TARGET_NR_getgroups32
  11168. case TARGET_NR_getgroups32:
  11169. { /* the same code as for TARGET_NR_getgroups */
  11170. int gidsetsize = arg1;
  11171. uint32_t *target_grouplist;
  11172. g_autofree gid_t *grouplist = NULL;
  11173. int i;
  11174. if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) {
  11175. return -TARGET_EINVAL;
  11176. }
  11177. if (gidsetsize > 0) {
  11178. grouplist = g_try_new(gid_t, gidsetsize);
  11179. if (!grouplist) {
  11180. return -TARGET_ENOMEM;
  11181. }
  11182. }
  11183. ret = get_errno(getgroups(gidsetsize, grouplist));
  11184. if (!is_error(ret) && gidsetsize > 0) {
  11185. target_grouplist = lock_user(VERIFY_WRITE, arg2,
  11186. gidsetsize * 4, 0);
  11187. if (!target_grouplist) {
  11188. return -TARGET_EFAULT;
  11189. }
  11190. for (i = 0; i < ret; i++) {
  11191. target_grouplist[i] = tswap32(grouplist[i]);
  11192. }
  11193. unlock_user(target_grouplist, arg2, gidsetsize * 4);
  11194. }
  11195. return ret;
  11196. }
  11197. #endif
  11198. #ifdef TARGET_NR_setgroups32
  11199. case TARGET_NR_setgroups32:
  11200. { /* the same code as for TARGET_NR_setgroups */
  11201. int gidsetsize = arg1;
  11202. uint32_t *target_grouplist;
  11203. g_autofree gid_t *grouplist = NULL;
  11204. int i;
  11205. if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) {
  11206. return -TARGET_EINVAL;
  11207. }
  11208. if (gidsetsize > 0) {
  11209. grouplist = g_try_new(gid_t, gidsetsize);
  11210. if (!grouplist) {
  11211. return -TARGET_ENOMEM;
  11212. }
  11213. target_grouplist = lock_user(VERIFY_READ, arg2,
  11214. gidsetsize * 4, 1);
  11215. if (!target_grouplist) {
  11216. return -TARGET_EFAULT;
  11217. }
  11218. for (i = 0; i < gidsetsize; i++) {
  11219. grouplist[i] = tswap32(target_grouplist[i]);
  11220. }
  11221. unlock_user(target_grouplist, arg2, 0);
  11222. }
  11223. return get_errno(sys_setgroups(gidsetsize, grouplist));
  11224. }
  11225. #endif
  11226. #ifdef TARGET_NR_fchown32
  11227. case TARGET_NR_fchown32:
  11228. return get_errno(fchown(arg1, arg2, arg3));
  11229. #endif
  11230. #ifdef TARGET_NR_setresuid32
  11231. case TARGET_NR_setresuid32:
  11232. return get_errno(sys_setresuid(arg1, arg2, arg3));
  11233. #endif
  11234. #ifdef TARGET_NR_getresuid32
  11235. case TARGET_NR_getresuid32:
  11236. {
  11237. uid_t ruid, euid, suid;
  11238. ret = get_errno(getresuid(&ruid, &euid, &suid));
  11239. if (!is_error(ret)) {
  11240. if (put_user_u32(ruid, arg1)
  11241. || put_user_u32(euid, arg2)
  11242. || put_user_u32(suid, arg3))
  11243. return -TARGET_EFAULT;
  11244. }
  11245. }
  11246. return ret;
  11247. #endif
  11248. #ifdef TARGET_NR_setresgid32
  11249. case TARGET_NR_setresgid32:
  11250. return get_errno(sys_setresgid(arg1, arg2, arg3));
  11251. #endif
  11252. #ifdef TARGET_NR_getresgid32
  11253. case TARGET_NR_getresgid32:
  11254. {
  11255. gid_t rgid, egid, sgid;
  11256. ret = get_errno(getresgid(&rgid, &egid, &sgid));
  11257. if (!is_error(ret)) {
  11258. if (put_user_u32(rgid, arg1)
  11259. || put_user_u32(egid, arg2)
  11260. || put_user_u32(sgid, arg3))
  11261. return -TARGET_EFAULT;
  11262. }
  11263. }
  11264. return ret;
  11265. #endif
  11266. #ifdef TARGET_NR_chown32
  11267. case TARGET_NR_chown32:
  11268. if (!(p = lock_user_string(arg1)))
  11269. return -TARGET_EFAULT;
  11270. ret = get_errno(chown(p, arg2, arg3));
  11271. unlock_user(p, arg1, 0);
  11272. return ret;
  11273. #endif
  11274. #ifdef TARGET_NR_setuid32
  11275. case TARGET_NR_setuid32:
  11276. return get_errno(sys_setuid(arg1));
  11277. #endif
  11278. #ifdef TARGET_NR_setgid32
  11279. case TARGET_NR_setgid32:
  11280. return get_errno(sys_setgid(arg1));
  11281. #endif
  11282. #ifdef TARGET_NR_setfsuid32
  11283. case TARGET_NR_setfsuid32:
  11284. return get_errno(setfsuid(arg1));
  11285. #endif
  11286. #ifdef TARGET_NR_setfsgid32
  11287. case TARGET_NR_setfsgid32:
  11288. return get_errno(setfsgid(arg1));
  11289. #endif
  11290. #ifdef TARGET_NR_mincore
  11291. case TARGET_NR_mincore:
  11292. {
  11293. void *a = lock_user(VERIFY_NONE, arg1, arg2, 0);
  11294. if (!a) {
  11295. return -TARGET_ENOMEM;
  11296. }
  11297. p = lock_user_string(arg3);
  11298. if (!p) {
  11299. ret = -TARGET_EFAULT;
  11300. } else {
  11301. ret = get_errno(mincore(a, arg2, p));
  11302. unlock_user(p, arg3, ret);
  11303. }
  11304. unlock_user(a, arg1, 0);
  11305. }
  11306. return ret;
  11307. #endif
  11308. #ifdef TARGET_NR_arm_fadvise64_64
  11309. case TARGET_NR_arm_fadvise64_64:
  11310. /* arm_fadvise64_64 looks like fadvise64_64 but
  11311. * with different argument order: fd, advice, offset, len
  11312. * rather than the usual fd, offset, len, advice.
  11313. * Note that offset and len are both 64-bit so appear as
  11314. * pairs of 32-bit registers.
  11315. */
  11316. ret = posix_fadvise(arg1, target_offset64(arg3, arg4),
  11317. target_offset64(arg5, arg6), arg2);
  11318. return -host_to_target_errno(ret);
  11319. #endif
  11320. #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
  11321. #ifdef TARGET_NR_fadvise64_64
  11322. case TARGET_NR_fadvise64_64:
  11323. #if defined(TARGET_PPC) || defined(TARGET_XTENSA)
  11324. /* 6 args: fd, advice, offset (high, low), len (high, low) */
  11325. ret = arg2;
  11326. arg2 = arg3;
  11327. arg3 = arg4;
  11328. arg4 = arg5;
  11329. arg5 = arg6;
  11330. arg6 = ret;
  11331. #else
  11332. /* 6 args: fd, offset (high, low), len (high, low), advice */
  11333. if (regpairs_aligned(cpu_env, num)) {
  11334. /* offset is in (3,4), len in (5,6) and advice in 7 */
  11335. arg2 = arg3;
  11336. arg3 = arg4;
  11337. arg4 = arg5;
  11338. arg5 = arg6;
  11339. arg6 = arg7;
  11340. }
  11341. #endif
  11342. ret = posix_fadvise(arg1, target_offset64(arg2, arg3),
  11343. target_offset64(arg4, arg5), arg6);
  11344. return -host_to_target_errno(ret);
  11345. #endif
  11346. #ifdef TARGET_NR_fadvise64
  11347. case TARGET_NR_fadvise64:
  11348. /* 5 args: fd, offset (high, low), len, advice */
  11349. if (regpairs_aligned(cpu_env, num)) {
  11350. /* offset is in (3,4), len in 5 and advice in 6 */
  11351. arg2 = arg3;
  11352. arg3 = arg4;
  11353. arg4 = arg5;
  11354. arg5 = arg6;
  11355. }
  11356. ret = posix_fadvise(arg1, target_offset64(arg2, arg3), arg4, arg5);
  11357. return -host_to_target_errno(ret);
  11358. #endif
  11359. #else /* not a 32-bit ABI */
  11360. #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_fadvise64)
  11361. #ifdef TARGET_NR_fadvise64_64
  11362. case TARGET_NR_fadvise64_64:
  11363. #endif
  11364. #ifdef TARGET_NR_fadvise64
  11365. case TARGET_NR_fadvise64:
  11366. #endif
  11367. #ifdef TARGET_S390X
  11368. switch (arg4) {
  11369. case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
  11370. case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
  11371. case 6: arg4 = POSIX_FADV_DONTNEED; break;
  11372. case 7: arg4 = POSIX_FADV_NOREUSE; break;
  11373. default: break;
  11374. }
  11375. #endif
  11376. return -host_to_target_errno(posix_fadvise(arg1, arg2, arg3, arg4));
  11377. #endif
  11378. #endif /* end of 64-bit ABI fadvise handling */
  11379. #ifdef TARGET_NR_madvise
  11380. case TARGET_NR_madvise:
  11381. return target_madvise(arg1, arg2, arg3);
  11382. #endif
  11383. #ifdef TARGET_NR_fcntl64
  11384. case TARGET_NR_fcntl64:
  11385. {
  11386. int cmd;
  11387. struct flock64 fl;
  11388. from_flock64_fn *copyfrom = copy_from_user_flock64;
  11389. to_flock64_fn *copyto = copy_to_user_flock64;
  11390. #ifdef TARGET_ARM
  11391. if (!cpu_env->eabi) {
  11392. copyfrom = copy_from_user_oabi_flock64;
  11393. copyto = copy_to_user_oabi_flock64;
  11394. }
  11395. #endif
  11396. cmd = target_to_host_fcntl_cmd(arg2);
  11397. if (cmd == -TARGET_EINVAL) {
  11398. return cmd;
  11399. }
  11400. switch(arg2) {
  11401. case TARGET_F_GETLK64:
  11402. ret = copyfrom(&fl, arg3);
  11403. if (ret) {
  11404. break;
  11405. }
  11406. ret = get_errno(safe_fcntl(arg1, cmd, &fl));
  11407. if (ret == 0) {
  11408. ret = copyto(arg3, &fl);
  11409. }
  11410. break;
  11411. case TARGET_F_SETLK64:
  11412. case TARGET_F_SETLKW64:
  11413. ret = copyfrom(&fl, arg3);
  11414. if (ret) {
  11415. break;
  11416. }
  11417. ret = get_errno(safe_fcntl(arg1, cmd, &fl));
  11418. break;
  11419. default:
  11420. ret = do_fcntl(arg1, arg2, arg3);
  11421. break;
  11422. }
  11423. return ret;
  11424. }
  11425. #endif
  11426. #ifdef TARGET_NR_cacheflush
  11427. case TARGET_NR_cacheflush:
  11428. /* self-modifying code is handled automatically, so nothing needed */
  11429. return 0;
  11430. #endif
  11431. #ifdef TARGET_NR_getpagesize
  11432. case TARGET_NR_getpagesize:
  11433. return TARGET_PAGE_SIZE;
  11434. #endif
  11435. case TARGET_NR_gettid:
  11436. return get_errno(sys_gettid());
  11437. #ifdef TARGET_NR_readahead
  11438. case TARGET_NR_readahead:
  11439. #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
  11440. if (regpairs_aligned(cpu_env, num)) {
  11441. arg2 = arg3;
  11442. arg3 = arg4;
  11443. arg4 = arg5;
  11444. }
  11445. ret = get_errno(readahead(arg1, target_offset64(arg2, arg3) , arg4));
  11446. #else
  11447. ret = get_errno(readahead(arg1, arg2, arg3));
  11448. #endif
  11449. return ret;
  11450. #endif
  11451. #ifdef CONFIG_ATTR
  11452. #ifdef TARGET_NR_setxattr
  11453. case TARGET_NR_listxattr:
  11454. case TARGET_NR_llistxattr:
  11455. {
  11456. void *b = 0;
  11457. if (arg2) {
  11458. b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  11459. if (!b) {
  11460. return -TARGET_EFAULT;
  11461. }
  11462. }
  11463. p = lock_user_string(arg1);
  11464. if (p) {
  11465. if (num == TARGET_NR_listxattr) {
  11466. ret = get_errno(listxattr(p, b, arg3));
  11467. } else {
  11468. ret = get_errno(llistxattr(p, b, arg3));
  11469. }
  11470. } else {
  11471. ret = -TARGET_EFAULT;
  11472. }
  11473. unlock_user(p, arg1, 0);
  11474. unlock_user(b, arg2, arg3);
  11475. return ret;
  11476. }
  11477. case TARGET_NR_flistxattr:
  11478. {
  11479. void *b = 0;
  11480. if (arg2) {
  11481. b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  11482. if (!b) {
  11483. return -TARGET_EFAULT;
  11484. }
  11485. }
  11486. ret = get_errno(flistxattr(arg1, b, arg3));
  11487. unlock_user(b, arg2, arg3);
  11488. return ret;
  11489. }
  11490. case TARGET_NR_setxattr:
  11491. case TARGET_NR_lsetxattr:
  11492. {
  11493. void *n, *v = 0;
  11494. if (arg3) {
  11495. v = lock_user(VERIFY_READ, arg3, arg4, 1);
  11496. if (!v) {
  11497. return -TARGET_EFAULT;
  11498. }
  11499. }
  11500. p = lock_user_string(arg1);
  11501. n = lock_user_string(arg2);
  11502. if (p && n) {
  11503. if (num == TARGET_NR_setxattr) {
  11504. ret = get_errno(setxattr(p, n, v, arg4, arg5));
  11505. } else {
  11506. ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
  11507. }
  11508. } else {
  11509. ret = -TARGET_EFAULT;
  11510. }
  11511. unlock_user(p, arg1, 0);
  11512. unlock_user(n, arg2, 0);
  11513. unlock_user(v, arg3, 0);
  11514. }
  11515. return ret;
  11516. case TARGET_NR_fsetxattr:
  11517. {
  11518. void *n, *v = 0;
  11519. if (arg3) {
  11520. v = lock_user(VERIFY_READ, arg3, arg4, 1);
  11521. if (!v) {
  11522. return -TARGET_EFAULT;
  11523. }
  11524. }
  11525. n = lock_user_string(arg2);
  11526. if (n) {
  11527. ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
  11528. } else {
  11529. ret = -TARGET_EFAULT;
  11530. }
  11531. unlock_user(n, arg2, 0);
  11532. unlock_user(v, arg3, 0);
  11533. }
  11534. return ret;
  11535. case TARGET_NR_getxattr:
  11536. case TARGET_NR_lgetxattr:
  11537. {
  11538. void *n, *v = 0;
  11539. if (arg3) {
  11540. v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  11541. if (!v) {
  11542. return -TARGET_EFAULT;
  11543. }
  11544. }
  11545. p = lock_user_string(arg1);
  11546. n = lock_user_string(arg2);
  11547. if (p && n) {
  11548. if (num == TARGET_NR_getxattr) {
  11549. ret = get_errno(getxattr(p, n, v, arg4));
  11550. } else {
  11551. ret = get_errno(lgetxattr(p, n, v, arg4));
  11552. }
  11553. } else {
  11554. ret = -TARGET_EFAULT;
  11555. }
  11556. unlock_user(p, arg1, 0);
  11557. unlock_user(n, arg2, 0);
  11558. unlock_user(v, arg3, arg4);
  11559. }
  11560. return ret;
  11561. case TARGET_NR_fgetxattr:
  11562. {
  11563. void *n, *v = 0;
  11564. if (arg3) {
  11565. v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  11566. if (!v) {
  11567. return -TARGET_EFAULT;
  11568. }
  11569. }
  11570. n = lock_user_string(arg2);
  11571. if (n) {
  11572. ret = get_errno(fgetxattr(arg1, n, v, arg4));
  11573. } else {
  11574. ret = -TARGET_EFAULT;
  11575. }
  11576. unlock_user(n, arg2, 0);
  11577. unlock_user(v, arg3, arg4);
  11578. }
  11579. return ret;
  11580. case TARGET_NR_removexattr:
  11581. case TARGET_NR_lremovexattr:
  11582. {
  11583. void *n;
  11584. p = lock_user_string(arg1);
  11585. n = lock_user_string(arg2);
  11586. if (p && n) {
  11587. if (num == TARGET_NR_removexattr) {
  11588. ret = get_errno(removexattr(p, n));
  11589. } else {
  11590. ret = get_errno(lremovexattr(p, n));
  11591. }
  11592. } else {
  11593. ret = -TARGET_EFAULT;
  11594. }
  11595. unlock_user(p, arg1, 0);
  11596. unlock_user(n, arg2, 0);
  11597. }
  11598. return ret;
  11599. case TARGET_NR_fremovexattr:
  11600. {
  11601. void *n;
  11602. n = lock_user_string(arg2);
  11603. if (n) {
  11604. ret = get_errno(fremovexattr(arg1, n));
  11605. } else {
  11606. ret = -TARGET_EFAULT;
  11607. }
  11608. unlock_user(n, arg2, 0);
  11609. }
  11610. return ret;
  11611. #endif
  11612. #endif /* CONFIG_ATTR */
  11613. #ifdef TARGET_NR_set_thread_area
  11614. case TARGET_NR_set_thread_area:
  11615. #if defined(TARGET_MIPS)
  11616. cpu_env->active_tc.CP0_UserLocal = arg1;
  11617. return 0;
  11618. #elif defined(TARGET_CRIS)
  11619. if (arg1 & 0xff)
  11620. ret = -TARGET_EINVAL;
  11621. else {
  11622. cpu_env->pregs[PR_PID] = arg1;
  11623. ret = 0;
  11624. }
  11625. return ret;
  11626. #elif defined(TARGET_I386) && defined(TARGET_ABI32)
  11627. return do_set_thread_area(cpu_env, arg1);
  11628. #elif defined(TARGET_M68K)
  11629. {
  11630. TaskState *ts = get_task_state(cpu);
  11631. ts->tp_value = arg1;
  11632. return 0;
  11633. }
  11634. #else
  11635. return -TARGET_ENOSYS;
  11636. #endif
  11637. #endif
  11638. #ifdef TARGET_NR_get_thread_area
  11639. case TARGET_NR_get_thread_area:
  11640. #if defined(TARGET_I386) && defined(TARGET_ABI32)
  11641. return do_get_thread_area(cpu_env, arg1);
  11642. #elif defined(TARGET_M68K)
  11643. {
  11644. TaskState *ts = get_task_state(cpu);
  11645. return ts->tp_value;
  11646. }
  11647. #else
  11648. return -TARGET_ENOSYS;
  11649. #endif
  11650. #endif
  11651. #ifdef TARGET_NR_getdomainname
  11652. case TARGET_NR_getdomainname:
  11653. return -TARGET_ENOSYS;
  11654. #endif
  11655. #ifdef TARGET_NR_clock_settime
  11656. case TARGET_NR_clock_settime:
  11657. {
  11658. struct timespec ts;
  11659. ret = target_to_host_timespec(&ts, arg2);
  11660. if (!is_error(ret)) {
  11661. ret = get_errno(clock_settime(arg1, &ts));
  11662. }
  11663. return ret;
  11664. }
  11665. #endif
  11666. #ifdef TARGET_NR_clock_settime64
  11667. case TARGET_NR_clock_settime64:
  11668. {
  11669. struct timespec ts;
  11670. ret = target_to_host_timespec64(&ts, arg2);
  11671. if (!is_error(ret)) {
  11672. ret = get_errno(clock_settime(arg1, &ts));
  11673. }
  11674. return ret;
  11675. }
  11676. #endif
  11677. #ifdef TARGET_NR_clock_gettime
  11678. case TARGET_NR_clock_gettime:
  11679. {
  11680. struct timespec ts;
  11681. ret = get_errno(clock_gettime(arg1, &ts));
  11682. if (!is_error(ret)) {
  11683. ret = host_to_target_timespec(arg2, &ts);
  11684. }
  11685. return ret;
  11686. }
  11687. #endif
  11688. #ifdef TARGET_NR_clock_gettime64
  11689. case TARGET_NR_clock_gettime64:
  11690. {
  11691. struct timespec ts;
  11692. ret = get_errno(clock_gettime(arg1, &ts));
  11693. if (!is_error(ret)) {
  11694. ret = host_to_target_timespec64(arg2, &ts);
  11695. }
  11696. return ret;
  11697. }
  11698. #endif
  11699. #ifdef TARGET_NR_clock_getres
  11700. case TARGET_NR_clock_getres:
  11701. {
  11702. struct timespec ts;
  11703. ret = get_errno(clock_getres(arg1, &ts));
  11704. if (!is_error(ret)) {
  11705. host_to_target_timespec(arg2, &ts);
  11706. }
  11707. return ret;
  11708. }
  11709. #endif
  11710. #ifdef TARGET_NR_clock_getres_time64
  11711. case TARGET_NR_clock_getres_time64:
  11712. {
  11713. struct timespec ts;
  11714. ret = get_errno(clock_getres(arg1, &ts));
  11715. if (!is_error(ret)) {
  11716. host_to_target_timespec64(arg2, &ts);
  11717. }
  11718. return ret;
  11719. }
  11720. #endif
  11721. #ifdef TARGET_NR_clock_nanosleep
  11722. case TARGET_NR_clock_nanosleep:
  11723. {
  11724. struct timespec ts;
  11725. if (target_to_host_timespec(&ts, arg3)) {
  11726. return -TARGET_EFAULT;
  11727. }
  11728. ret = get_errno(safe_clock_nanosleep(arg1, arg2,
  11729. &ts, arg4 ? &ts : NULL));
  11730. /*
  11731. * if the call is interrupted by a signal handler, it fails
  11732. * with error -TARGET_EINTR and if arg4 is not NULL and arg2 is not
  11733. * TIMER_ABSTIME, it returns the remaining unslept time in arg4.
  11734. */
  11735. if (ret == -TARGET_EINTR && arg4 && arg2 != TIMER_ABSTIME &&
  11736. host_to_target_timespec(arg4, &ts)) {
  11737. return -TARGET_EFAULT;
  11738. }
  11739. return ret;
  11740. }
  11741. #endif
  11742. #ifdef TARGET_NR_clock_nanosleep_time64
  11743. case TARGET_NR_clock_nanosleep_time64:
  11744. {
  11745. struct timespec ts;
  11746. if (target_to_host_timespec64(&ts, arg3)) {
  11747. return -TARGET_EFAULT;
  11748. }
  11749. ret = get_errno(safe_clock_nanosleep(arg1, arg2,
  11750. &ts, arg4 ? &ts : NULL));
  11751. if (ret == -TARGET_EINTR && arg4 && arg2 != TIMER_ABSTIME &&
  11752. host_to_target_timespec64(arg4, &ts)) {
  11753. return -TARGET_EFAULT;
  11754. }
  11755. return ret;
  11756. }
  11757. #endif
  11758. #if defined(TARGET_NR_set_tid_address)
  11759. case TARGET_NR_set_tid_address:
  11760. {
  11761. TaskState *ts = get_task_state(cpu);
  11762. ts->child_tidptr = arg1;
  11763. /* do not call host set_tid_address() syscall, instead return tid() */
  11764. return get_errno(sys_gettid());
  11765. }
  11766. #endif
  11767. case TARGET_NR_tkill:
  11768. return get_errno(safe_tkill((int)arg1, target_to_host_signal(arg2)));
  11769. case TARGET_NR_tgkill:
  11770. return get_errno(safe_tgkill((int)arg1, (int)arg2,
  11771. target_to_host_signal(arg3)));
  11772. #ifdef TARGET_NR_set_robust_list
  11773. case TARGET_NR_set_robust_list:
  11774. case TARGET_NR_get_robust_list:
  11775. /* The ABI for supporting robust futexes has userspace pass
  11776. * the kernel a pointer to a linked list which is updated by
  11777. * userspace after the syscall; the list is walked by the kernel
  11778. * when the thread exits. Since the linked list in QEMU guest
  11779. * memory isn't a valid linked list for the host and we have
  11780. * no way to reliably intercept the thread-death event, we can't
  11781. * support these. Silently return ENOSYS so that guest userspace
  11782. * falls back to a non-robust futex implementation (which should
  11783. * be OK except in the corner case of the guest crashing while
  11784. * holding a mutex that is shared with another process via
  11785. * shared memory).
  11786. */
  11787. return -TARGET_ENOSYS;
  11788. #endif
  11789. #if defined(TARGET_NR_utimensat)
  11790. case TARGET_NR_utimensat:
  11791. {
  11792. struct timespec *tsp, ts[2];
  11793. if (!arg3) {
  11794. tsp = NULL;
  11795. } else {
  11796. if (target_to_host_timespec(ts, arg3)) {
  11797. return -TARGET_EFAULT;
  11798. }
  11799. if (target_to_host_timespec(ts + 1, arg3 +
  11800. sizeof(struct target_timespec))) {
  11801. return -TARGET_EFAULT;
  11802. }
  11803. tsp = ts;
  11804. }
  11805. if (!arg2)
  11806. ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
  11807. else {
  11808. if (!(p = lock_user_string(arg2))) {
  11809. return -TARGET_EFAULT;
  11810. }
  11811. ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
  11812. unlock_user(p, arg2, 0);
  11813. }
  11814. }
  11815. return ret;
  11816. #endif
  11817. #ifdef TARGET_NR_utimensat_time64
  11818. case TARGET_NR_utimensat_time64:
  11819. {
  11820. struct timespec *tsp, ts[2];
  11821. if (!arg3) {
  11822. tsp = NULL;
  11823. } else {
  11824. if (target_to_host_timespec64(ts, arg3)) {
  11825. return -TARGET_EFAULT;
  11826. }
  11827. if (target_to_host_timespec64(ts + 1, arg3 +
  11828. sizeof(struct target__kernel_timespec))) {
  11829. return -TARGET_EFAULT;
  11830. }
  11831. tsp = ts;
  11832. }
  11833. if (!arg2)
  11834. ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
  11835. else {
  11836. p = lock_user_string(arg2);
  11837. if (!p) {
  11838. return -TARGET_EFAULT;
  11839. }
  11840. ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
  11841. unlock_user(p, arg2, 0);
  11842. }
  11843. }
  11844. return ret;
  11845. #endif
  11846. #ifdef TARGET_NR_futex
  11847. case TARGET_NR_futex:
  11848. return do_futex(cpu, false, arg1, arg2, arg3, arg4, arg5, arg6);
  11849. #endif
  11850. #ifdef TARGET_NR_futex_time64
  11851. case TARGET_NR_futex_time64:
  11852. return do_futex(cpu, true, arg1, arg2, arg3, arg4, arg5, arg6);
  11853. #endif
  11854. #ifdef CONFIG_INOTIFY
  11855. #if defined(TARGET_NR_inotify_init)
  11856. case TARGET_NR_inotify_init:
  11857. ret = get_errno(inotify_init());
  11858. if (ret >= 0) {
  11859. fd_trans_register(ret, &target_inotify_trans);
  11860. }
  11861. return ret;
  11862. #endif
  11863. #if defined(TARGET_NR_inotify_init1) && defined(CONFIG_INOTIFY1)
  11864. case TARGET_NR_inotify_init1:
  11865. ret = get_errno(inotify_init1(target_to_host_bitmask(arg1,
  11866. fcntl_flags_tbl)));
  11867. if (ret >= 0) {
  11868. fd_trans_register(ret, &target_inotify_trans);
  11869. }
  11870. return ret;
  11871. #endif
  11872. #if defined(TARGET_NR_inotify_add_watch)
  11873. case TARGET_NR_inotify_add_watch:
  11874. p = lock_user_string(arg2);
  11875. ret = get_errno(inotify_add_watch(arg1, path(p), arg3));
  11876. unlock_user(p, arg2, 0);
  11877. return ret;
  11878. #endif
  11879. #if defined(TARGET_NR_inotify_rm_watch)
  11880. case TARGET_NR_inotify_rm_watch:
  11881. return get_errno(inotify_rm_watch(arg1, arg2));
  11882. #endif
  11883. #endif
  11884. #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
  11885. case TARGET_NR_mq_open:
  11886. {
  11887. struct mq_attr posix_mq_attr;
  11888. struct mq_attr *pposix_mq_attr;
  11889. int host_flags;
  11890. host_flags = target_to_host_bitmask(arg2, fcntl_flags_tbl);
  11891. pposix_mq_attr = NULL;
  11892. if (arg4) {
  11893. if (copy_from_user_mq_attr(&posix_mq_attr, arg4) != 0) {
  11894. return -TARGET_EFAULT;
  11895. }
  11896. pposix_mq_attr = &posix_mq_attr;
  11897. }
  11898. p = lock_user_string(arg1 - 1);
  11899. if (!p) {
  11900. return -TARGET_EFAULT;
  11901. }
  11902. ret = get_errno(mq_open(p, host_flags, arg3, pposix_mq_attr));
  11903. unlock_user (p, arg1, 0);
  11904. }
  11905. return ret;
  11906. case TARGET_NR_mq_unlink:
  11907. p = lock_user_string(arg1 - 1);
  11908. if (!p) {
  11909. return -TARGET_EFAULT;
  11910. }
  11911. ret = get_errno(mq_unlink(p));
  11912. unlock_user (p, arg1, 0);
  11913. return ret;
  11914. #ifdef TARGET_NR_mq_timedsend
  11915. case TARGET_NR_mq_timedsend:
  11916. {
  11917. struct timespec ts;
  11918. p = lock_user (VERIFY_READ, arg2, arg3, 1);
  11919. if (arg5 != 0) {
  11920. if (target_to_host_timespec(&ts, arg5)) {
  11921. return -TARGET_EFAULT;
  11922. }
  11923. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts));
  11924. if (!is_error(ret) && host_to_target_timespec(arg5, &ts)) {
  11925. return -TARGET_EFAULT;
  11926. }
  11927. } else {
  11928. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL));
  11929. }
  11930. unlock_user (p, arg2, arg3);
  11931. }
  11932. return ret;
  11933. #endif
  11934. #ifdef TARGET_NR_mq_timedsend_time64
  11935. case TARGET_NR_mq_timedsend_time64:
  11936. {
  11937. struct timespec ts;
  11938. p = lock_user(VERIFY_READ, arg2, arg3, 1);
  11939. if (arg5 != 0) {
  11940. if (target_to_host_timespec64(&ts, arg5)) {
  11941. return -TARGET_EFAULT;
  11942. }
  11943. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts));
  11944. if (!is_error(ret) && host_to_target_timespec64(arg5, &ts)) {
  11945. return -TARGET_EFAULT;
  11946. }
  11947. } else {
  11948. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL));
  11949. }
  11950. unlock_user(p, arg2, arg3);
  11951. }
  11952. return ret;
  11953. #endif
  11954. #ifdef TARGET_NR_mq_timedreceive
  11955. case TARGET_NR_mq_timedreceive:
  11956. {
  11957. struct timespec ts;
  11958. unsigned int prio;
  11959. p = lock_user (VERIFY_READ, arg2, arg3, 1);
  11960. if (arg5 != 0) {
  11961. if (target_to_host_timespec(&ts, arg5)) {
  11962. return -TARGET_EFAULT;
  11963. }
  11964. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  11965. &prio, &ts));
  11966. if (!is_error(ret) && host_to_target_timespec(arg5, &ts)) {
  11967. return -TARGET_EFAULT;
  11968. }
  11969. } else {
  11970. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  11971. &prio, NULL));
  11972. }
  11973. unlock_user (p, arg2, arg3);
  11974. if (arg4 != 0)
  11975. put_user_u32(prio, arg4);
  11976. }
  11977. return ret;
  11978. #endif
  11979. #ifdef TARGET_NR_mq_timedreceive_time64
  11980. case TARGET_NR_mq_timedreceive_time64:
  11981. {
  11982. struct timespec ts;
  11983. unsigned int prio;
  11984. p = lock_user(VERIFY_READ, arg2, arg3, 1);
  11985. if (arg5 != 0) {
  11986. if (target_to_host_timespec64(&ts, arg5)) {
  11987. return -TARGET_EFAULT;
  11988. }
  11989. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  11990. &prio, &ts));
  11991. if (!is_error(ret) && host_to_target_timespec64(arg5, &ts)) {
  11992. return -TARGET_EFAULT;
  11993. }
  11994. } else {
  11995. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  11996. &prio, NULL));
  11997. }
  11998. unlock_user(p, arg2, arg3);
  11999. if (arg4 != 0) {
  12000. put_user_u32(prio, arg4);
  12001. }
  12002. }
  12003. return ret;
  12004. #endif
  12005. /* Not implemented for now... */
  12006. /* case TARGET_NR_mq_notify: */
  12007. /* break; */
  12008. case TARGET_NR_mq_getsetattr:
  12009. {
  12010. struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
  12011. ret = 0;
  12012. if (arg2 != 0) {
  12013. copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
  12014. ret = get_errno(mq_setattr(arg1, &posix_mq_attr_in,
  12015. &posix_mq_attr_out));
  12016. } else if (arg3 != 0) {
  12017. ret = get_errno(mq_getattr(arg1, &posix_mq_attr_out));
  12018. }
  12019. if (ret == 0 && arg3 != 0) {
  12020. copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
  12021. }
  12022. }
  12023. return ret;
  12024. #endif
  12025. #ifdef CONFIG_SPLICE
  12026. #ifdef TARGET_NR_tee
  12027. case TARGET_NR_tee:
  12028. {
  12029. ret = get_errno(tee(arg1,arg2,arg3,arg4));
  12030. }
  12031. return ret;
  12032. #endif
  12033. #ifdef TARGET_NR_splice
  12034. case TARGET_NR_splice:
  12035. {
  12036. loff_t loff_in, loff_out;
  12037. loff_t *ploff_in = NULL, *ploff_out = NULL;
  12038. if (arg2) {
  12039. if (get_user_u64(loff_in, arg2)) {
  12040. return -TARGET_EFAULT;
  12041. }
  12042. ploff_in = &loff_in;
  12043. }
  12044. if (arg4) {
  12045. if (get_user_u64(loff_out, arg4)) {
  12046. return -TARGET_EFAULT;
  12047. }
  12048. ploff_out = &loff_out;
  12049. }
  12050. ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
  12051. if (arg2) {
  12052. if (put_user_u64(loff_in, arg2)) {
  12053. return -TARGET_EFAULT;
  12054. }
  12055. }
  12056. if (arg4) {
  12057. if (put_user_u64(loff_out, arg4)) {
  12058. return -TARGET_EFAULT;
  12059. }
  12060. }
  12061. }
  12062. return ret;
  12063. #endif
  12064. #ifdef TARGET_NR_vmsplice
  12065. case TARGET_NR_vmsplice:
  12066. {
  12067. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  12068. if (vec != NULL) {
  12069. ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
  12070. unlock_iovec(vec, arg2, arg3, 0);
  12071. } else {
  12072. ret = -host_to_target_errno(errno);
  12073. }
  12074. }
  12075. return ret;
  12076. #endif
  12077. #endif /* CONFIG_SPLICE */
  12078. #ifdef CONFIG_EVENTFD
  12079. #if defined(TARGET_NR_eventfd)
  12080. case TARGET_NR_eventfd:
  12081. ret = get_errno(eventfd(arg1, 0));
  12082. if (ret >= 0) {
  12083. fd_trans_register(ret, &target_eventfd_trans);
  12084. }
  12085. return ret;
  12086. #endif
  12087. #if defined(TARGET_NR_eventfd2)
  12088. case TARGET_NR_eventfd2:
  12089. {
  12090. int host_flags = arg2 & (~(TARGET_O_NONBLOCK_MASK | TARGET_O_CLOEXEC));
  12091. if (arg2 & TARGET_O_NONBLOCK) {
  12092. host_flags |= O_NONBLOCK;
  12093. }
  12094. if (arg2 & TARGET_O_CLOEXEC) {
  12095. host_flags |= O_CLOEXEC;
  12096. }
  12097. ret = get_errno(eventfd(arg1, host_flags));
  12098. if (ret >= 0) {
  12099. fd_trans_register(ret, &target_eventfd_trans);
  12100. }
  12101. return ret;
  12102. }
  12103. #endif
  12104. #endif /* CONFIG_EVENTFD */
  12105. #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
  12106. case TARGET_NR_fallocate:
  12107. #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
  12108. ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
  12109. target_offset64(arg5, arg6)));
  12110. #else
  12111. ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
  12112. #endif
  12113. return ret;
  12114. #endif
  12115. #if defined(CONFIG_SYNC_FILE_RANGE)
  12116. #if defined(TARGET_NR_sync_file_range)
  12117. case TARGET_NR_sync_file_range:
  12118. #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
  12119. #if defined(TARGET_MIPS)
  12120. ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
  12121. target_offset64(arg5, arg6), arg7));
  12122. #else
  12123. ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
  12124. target_offset64(arg4, arg5), arg6));
  12125. #endif /* !TARGET_MIPS */
  12126. #else
  12127. ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
  12128. #endif
  12129. return ret;
  12130. #endif
  12131. #if defined(TARGET_NR_sync_file_range2) || \
  12132. defined(TARGET_NR_arm_sync_file_range)
  12133. #if defined(TARGET_NR_sync_file_range2)
  12134. case TARGET_NR_sync_file_range2:
  12135. #endif
  12136. #if defined(TARGET_NR_arm_sync_file_range)
  12137. case TARGET_NR_arm_sync_file_range:
  12138. #endif
  12139. /* This is like sync_file_range but the arguments are reordered */
  12140. #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
  12141. ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
  12142. target_offset64(arg5, arg6), arg2));
  12143. #else
  12144. ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
  12145. #endif
  12146. return ret;
  12147. #endif
  12148. #endif
  12149. #if defined(TARGET_NR_signalfd4)
  12150. case TARGET_NR_signalfd4:
  12151. return do_signalfd4(arg1, arg2, arg4);
  12152. #endif
  12153. #if defined(TARGET_NR_signalfd)
  12154. case TARGET_NR_signalfd:
  12155. return do_signalfd4(arg1, arg2, 0);
  12156. #endif
  12157. #if defined(CONFIG_EPOLL)
  12158. #if defined(TARGET_NR_epoll_create)
  12159. case TARGET_NR_epoll_create:
  12160. return get_errno(epoll_create(arg1));
  12161. #endif
  12162. #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
  12163. case TARGET_NR_epoll_create1:
  12164. return get_errno(epoll_create1(target_to_host_bitmask(arg1, fcntl_flags_tbl)));
  12165. #endif
  12166. #if defined(TARGET_NR_epoll_ctl)
  12167. case TARGET_NR_epoll_ctl:
  12168. {
  12169. struct epoll_event ep;
  12170. struct epoll_event *epp = 0;
  12171. if (arg4) {
  12172. if (arg2 != EPOLL_CTL_DEL) {
  12173. struct target_epoll_event *target_ep;
  12174. if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
  12175. return -TARGET_EFAULT;
  12176. }
  12177. ep.events = tswap32(target_ep->events);
  12178. /*
  12179. * The epoll_data_t union is just opaque data to the kernel,
  12180. * so we transfer all 64 bits across and need not worry what
  12181. * actual data type it is.
  12182. */
  12183. ep.data.u64 = tswap64(target_ep->data.u64);
  12184. unlock_user_struct(target_ep, arg4, 0);
  12185. }
  12186. /*
  12187. * before kernel 2.6.9, EPOLL_CTL_DEL operation required a
  12188. * non-null pointer, even though this argument is ignored.
  12189. *
  12190. */
  12191. epp = &ep;
  12192. }
  12193. return get_errno(epoll_ctl(arg1, arg2, arg3, epp));
  12194. }
  12195. #endif
  12196. #if defined(TARGET_NR_epoll_wait) || defined(TARGET_NR_epoll_pwait)
  12197. #if defined(TARGET_NR_epoll_wait)
  12198. case TARGET_NR_epoll_wait:
  12199. #endif
  12200. #if defined(TARGET_NR_epoll_pwait)
  12201. case TARGET_NR_epoll_pwait:
  12202. #endif
  12203. {
  12204. struct target_epoll_event *target_ep;
  12205. struct epoll_event *ep;
  12206. int epfd = arg1;
  12207. int maxevents = arg3;
  12208. int timeout = arg4;
  12209. if (maxevents <= 0 || maxevents > TARGET_EP_MAX_EVENTS) {
  12210. return -TARGET_EINVAL;
  12211. }
  12212. target_ep = lock_user(VERIFY_WRITE, arg2,
  12213. maxevents * sizeof(struct target_epoll_event), 1);
  12214. if (!target_ep) {
  12215. return -TARGET_EFAULT;
  12216. }
  12217. ep = g_try_new(struct epoll_event, maxevents);
  12218. if (!ep) {
  12219. unlock_user(target_ep, arg2, 0);
  12220. return -TARGET_ENOMEM;
  12221. }
  12222. switch (num) {
  12223. #if defined(TARGET_NR_epoll_pwait)
  12224. case TARGET_NR_epoll_pwait:
  12225. {
  12226. sigset_t *set = NULL;
  12227. if (arg5) {
  12228. ret = process_sigsuspend_mask(&set, arg5, arg6);
  12229. if (ret != 0) {
  12230. break;
  12231. }
  12232. }
  12233. ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
  12234. set, SIGSET_T_SIZE));
  12235. if (set) {
  12236. finish_sigsuspend_mask(ret);
  12237. }
  12238. break;
  12239. }
  12240. #endif
  12241. #if defined(TARGET_NR_epoll_wait)
  12242. case TARGET_NR_epoll_wait:
  12243. ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
  12244. NULL, 0));
  12245. break;
  12246. #endif
  12247. default:
  12248. ret = -TARGET_ENOSYS;
  12249. }
  12250. if (!is_error(ret)) {
  12251. int i;
  12252. for (i = 0; i < ret; i++) {
  12253. target_ep[i].events = tswap32(ep[i].events);
  12254. target_ep[i].data.u64 = tswap64(ep[i].data.u64);
  12255. }
  12256. unlock_user(target_ep, arg2,
  12257. ret * sizeof(struct target_epoll_event));
  12258. } else {
  12259. unlock_user(target_ep, arg2, 0);
  12260. }
  12261. g_free(ep);
  12262. return ret;
  12263. }
  12264. #endif
  12265. #endif
  12266. #ifdef TARGET_NR_prlimit64
  12267. case TARGET_NR_prlimit64:
  12268. {
  12269. /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
  12270. struct target_rlimit64 *target_rnew, *target_rold;
  12271. struct host_rlimit64 rnew, rold, *rnewp = 0;
  12272. int resource = target_to_host_resource(arg2);
  12273. if (arg3 && (resource != RLIMIT_AS &&
  12274. resource != RLIMIT_DATA &&
  12275. resource != RLIMIT_STACK)) {
  12276. if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
  12277. return -TARGET_EFAULT;
  12278. }
  12279. __get_user(rnew.rlim_cur, &target_rnew->rlim_cur);
  12280. __get_user(rnew.rlim_max, &target_rnew->rlim_max);
  12281. unlock_user_struct(target_rnew, arg3, 0);
  12282. rnewp = &rnew;
  12283. }
  12284. ret = get_errno(sys_prlimit64(arg1, resource, rnewp, arg4 ? &rold : 0));
  12285. if (!is_error(ret) && arg4) {
  12286. if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
  12287. return -TARGET_EFAULT;
  12288. }
  12289. __put_user(rold.rlim_cur, &target_rold->rlim_cur);
  12290. __put_user(rold.rlim_max, &target_rold->rlim_max);
  12291. unlock_user_struct(target_rold, arg4, 1);
  12292. }
  12293. return ret;
  12294. }
  12295. #endif
  12296. #ifdef TARGET_NR_gethostname
  12297. case TARGET_NR_gethostname:
  12298. {
  12299. char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
  12300. if (name) {
  12301. ret = get_errno(gethostname(name, arg2));
  12302. unlock_user(name, arg1, arg2);
  12303. } else {
  12304. ret = -TARGET_EFAULT;
  12305. }
  12306. return ret;
  12307. }
  12308. #endif
  12309. #ifdef TARGET_NR_atomic_cmpxchg_32
  12310. case TARGET_NR_atomic_cmpxchg_32:
  12311. {
  12312. /* should use start_exclusive from main.c */
  12313. abi_ulong mem_value;
  12314. if (get_user_u32(mem_value, arg6)) {
  12315. target_siginfo_t info;
  12316. info.si_signo = SIGSEGV;
  12317. info.si_errno = 0;
  12318. info.si_code = TARGET_SEGV_MAPERR;
  12319. info._sifields._sigfault._addr = arg6;
  12320. queue_signal(cpu_env, info.si_signo, QEMU_SI_FAULT, &info);
  12321. ret = 0xdeadbeef;
  12322. }
  12323. if (mem_value == arg2)
  12324. put_user_u32(arg1, arg6);
  12325. return mem_value;
  12326. }
  12327. #endif
  12328. #ifdef TARGET_NR_atomic_barrier
  12329. case TARGET_NR_atomic_barrier:
  12330. /* Like the kernel implementation and the
  12331. qemu arm barrier, no-op this? */
  12332. return 0;
  12333. #endif
  12334. #ifdef TARGET_NR_timer_create
  12335. case TARGET_NR_timer_create:
  12336. {
  12337. /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
  12338. struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL;
  12339. int clkid = arg1;
  12340. int timer_index = next_free_host_timer();
  12341. if (timer_index < 0) {
  12342. ret = -TARGET_EAGAIN;
  12343. } else {
  12344. timer_t *phtimer = g_posix_timers + timer_index;
  12345. if (arg2) {
  12346. phost_sevp = &host_sevp;
  12347. ret = target_to_host_sigevent(phost_sevp, arg2);
  12348. if (ret != 0) {
  12349. free_host_timer_slot(timer_index);
  12350. return ret;
  12351. }
  12352. }
  12353. ret = get_errno(timer_create(clkid, phost_sevp, phtimer));
  12354. if (ret) {
  12355. free_host_timer_slot(timer_index);
  12356. } else {
  12357. if (put_user(TIMER_MAGIC | timer_index, arg3, target_timer_t)) {
  12358. timer_delete(*phtimer);
  12359. free_host_timer_slot(timer_index);
  12360. return -TARGET_EFAULT;
  12361. }
  12362. }
  12363. }
  12364. return ret;
  12365. }
  12366. #endif
  12367. #ifdef TARGET_NR_timer_settime
  12368. case TARGET_NR_timer_settime:
  12369. {
  12370. /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
  12371. * struct itimerspec * old_value */
  12372. target_timer_t timerid = get_timer_id(arg1);
  12373. if (timerid < 0) {
  12374. ret = timerid;
  12375. } else if (arg3 == 0) {
  12376. ret = -TARGET_EINVAL;
  12377. } else {
  12378. timer_t htimer = g_posix_timers[timerid];
  12379. struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
  12380. if (target_to_host_itimerspec(&hspec_new, arg3)) {
  12381. return -TARGET_EFAULT;
  12382. }
  12383. ret = get_errno(
  12384. timer_settime(htimer, arg2, &hspec_new, &hspec_old));
  12385. if (arg4 && host_to_target_itimerspec(arg4, &hspec_old)) {
  12386. return -TARGET_EFAULT;
  12387. }
  12388. }
  12389. return ret;
  12390. }
  12391. #endif
  12392. #ifdef TARGET_NR_timer_settime64
  12393. case TARGET_NR_timer_settime64:
  12394. {
  12395. target_timer_t timerid = get_timer_id(arg1);
  12396. if (timerid < 0) {
  12397. ret = timerid;
  12398. } else if (arg3 == 0) {
  12399. ret = -TARGET_EINVAL;
  12400. } else {
  12401. timer_t htimer = g_posix_timers[timerid];
  12402. struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
  12403. if (target_to_host_itimerspec64(&hspec_new, arg3)) {
  12404. return -TARGET_EFAULT;
  12405. }
  12406. ret = get_errno(
  12407. timer_settime(htimer, arg2, &hspec_new, &hspec_old));
  12408. if (arg4 && host_to_target_itimerspec64(arg4, &hspec_old)) {
  12409. return -TARGET_EFAULT;
  12410. }
  12411. }
  12412. return ret;
  12413. }
  12414. #endif
  12415. #ifdef TARGET_NR_timer_gettime
  12416. case TARGET_NR_timer_gettime:
  12417. {
  12418. /* args: timer_t timerid, struct itimerspec *curr_value */
  12419. target_timer_t timerid = get_timer_id(arg1);
  12420. if (timerid < 0) {
  12421. ret = timerid;
  12422. } else if (!arg2) {
  12423. ret = -TARGET_EFAULT;
  12424. } else {
  12425. timer_t htimer = g_posix_timers[timerid];
  12426. struct itimerspec hspec;
  12427. ret = get_errno(timer_gettime(htimer, &hspec));
  12428. if (host_to_target_itimerspec(arg2, &hspec)) {
  12429. ret = -TARGET_EFAULT;
  12430. }
  12431. }
  12432. return ret;
  12433. }
  12434. #endif
  12435. #ifdef TARGET_NR_timer_gettime64
  12436. case TARGET_NR_timer_gettime64:
  12437. {
  12438. /* args: timer_t timerid, struct itimerspec64 *curr_value */
  12439. target_timer_t timerid = get_timer_id(arg1);
  12440. if (timerid < 0) {
  12441. ret = timerid;
  12442. } else if (!arg2) {
  12443. ret = -TARGET_EFAULT;
  12444. } else {
  12445. timer_t htimer = g_posix_timers[timerid];
  12446. struct itimerspec hspec;
  12447. ret = get_errno(timer_gettime(htimer, &hspec));
  12448. if (host_to_target_itimerspec64(arg2, &hspec)) {
  12449. ret = -TARGET_EFAULT;
  12450. }
  12451. }
  12452. return ret;
  12453. }
  12454. #endif
  12455. #ifdef TARGET_NR_timer_getoverrun
  12456. case TARGET_NR_timer_getoverrun:
  12457. {
  12458. /* args: timer_t timerid */
  12459. target_timer_t timerid = get_timer_id(arg1);
  12460. if (timerid < 0) {
  12461. ret = timerid;
  12462. } else {
  12463. timer_t htimer = g_posix_timers[timerid];
  12464. ret = get_errno(timer_getoverrun(htimer));
  12465. }
  12466. return ret;
  12467. }
  12468. #endif
  12469. #ifdef TARGET_NR_timer_delete
  12470. case TARGET_NR_timer_delete:
  12471. {
  12472. /* args: timer_t timerid */
  12473. target_timer_t timerid = get_timer_id(arg1);
  12474. if (timerid < 0) {
  12475. ret = timerid;
  12476. } else {
  12477. timer_t htimer = g_posix_timers[timerid];
  12478. ret = get_errno(timer_delete(htimer));
  12479. free_host_timer_slot(timerid);
  12480. }
  12481. return ret;
  12482. }
  12483. #endif
  12484. #if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
  12485. case TARGET_NR_timerfd_create:
  12486. ret = get_errno(timerfd_create(arg1,
  12487. target_to_host_bitmask(arg2, fcntl_flags_tbl)));
  12488. if (ret >= 0) {
  12489. fd_trans_register(ret, &target_timerfd_trans);
  12490. }
  12491. return ret;
  12492. #endif
  12493. #if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
  12494. case TARGET_NR_timerfd_gettime:
  12495. {
  12496. struct itimerspec its_curr;
  12497. ret = get_errno(timerfd_gettime(arg1, &its_curr));
  12498. if (arg2 && host_to_target_itimerspec(arg2, &its_curr)) {
  12499. return -TARGET_EFAULT;
  12500. }
  12501. }
  12502. return ret;
  12503. #endif
  12504. #if defined(TARGET_NR_timerfd_gettime64) && defined(CONFIG_TIMERFD)
  12505. case TARGET_NR_timerfd_gettime64:
  12506. {
  12507. struct itimerspec its_curr;
  12508. ret = get_errno(timerfd_gettime(arg1, &its_curr));
  12509. if (arg2 && host_to_target_itimerspec64(arg2, &its_curr)) {
  12510. return -TARGET_EFAULT;
  12511. }
  12512. }
  12513. return ret;
  12514. #endif
  12515. #if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
  12516. case TARGET_NR_timerfd_settime:
  12517. {
  12518. struct itimerspec its_new, its_old, *p_new;
  12519. if (arg3) {
  12520. if (target_to_host_itimerspec(&its_new, arg3)) {
  12521. return -TARGET_EFAULT;
  12522. }
  12523. p_new = &its_new;
  12524. } else {
  12525. p_new = NULL;
  12526. }
  12527. ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
  12528. if (arg4 && host_to_target_itimerspec(arg4, &its_old)) {
  12529. return -TARGET_EFAULT;
  12530. }
  12531. }
  12532. return ret;
  12533. #endif
  12534. #if defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)
  12535. case TARGET_NR_timerfd_settime64:
  12536. {
  12537. struct itimerspec its_new, its_old, *p_new;
  12538. if (arg3) {
  12539. if (target_to_host_itimerspec64(&its_new, arg3)) {
  12540. return -TARGET_EFAULT;
  12541. }
  12542. p_new = &its_new;
  12543. } else {
  12544. p_new = NULL;
  12545. }
  12546. ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
  12547. if (arg4 && host_to_target_itimerspec64(arg4, &its_old)) {
  12548. return -TARGET_EFAULT;
  12549. }
  12550. }
  12551. return ret;
  12552. #endif
  12553. #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
  12554. case TARGET_NR_ioprio_get:
  12555. return get_errno(ioprio_get(arg1, arg2));
  12556. #endif
  12557. #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
  12558. case TARGET_NR_ioprio_set:
  12559. return get_errno(ioprio_set(arg1, arg2, arg3));
  12560. #endif
  12561. #if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
  12562. case TARGET_NR_setns:
  12563. return get_errno(setns(arg1, arg2));
  12564. #endif
  12565. #if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
  12566. case TARGET_NR_unshare:
  12567. return get_errno(unshare(arg1));
  12568. #endif
  12569. #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
  12570. case TARGET_NR_kcmp:
  12571. return get_errno(kcmp(arg1, arg2, arg3, arg4, arg5));
  12572. #endif
  12573. #ifdef TARGET_NR_swapcontext
  12574. case TARGET_NR_swapcontext:
  12575. /* PowerPC specific. */
  12576. return do_swapcontext(cpu_env, arg1, arg2, arg3);
  12577. #endif
  12578. #ifdef TARGET_NR_memfd_create
  12579. case TARGET_NR_memfd_create:
  12580. p = lock_user_string(arg1);
  12581. if (!p) {
  12582. return -TARGET_EFAULT;
  12583. }
  12584. ret = get_errno(memfd_create(p, arg2));
  12585. fd_trans_unregister(ret);
  12586. unlock_user(p, arg1, 0);
  12587. return ret;
  12588. #endif
  12589. #if defined TARGET_NR_membarrier && defined __NR_membarrier
  12590. case TARGET_NR_membarrier:
  12591. return get_errno(membarrier(arg1, arg2));
  12592. #endif
  12593. #if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range)
  12594. case TARGET_NR_copy_file_range:
  12595. {
  12596. loff_t inoff, outoff;
  12597. loff_t *pinoff = NULL, *poutoff = NULL;
  12598. if (arg2) {
  12599. if (get_user_u64(inoff, arg2)) {
  12600. return -TARGET_EFAULT;
  12601. }
  12602. pinoff = &inoff;
  12603. }
  12604. if (arg4) {
  12605. if (get_user_u64(outoff, arg4)) {
  12606. return -TARGET_EFAULT;
  12607. }
  12608. poutoff = &outoff;
  12609. }
  12610. /* Do not sign-extend the count parameter. */
  12611. ret = get_errno(safe_copy_file_range(arg1, pinoff, arg3, poutoff,
  12612. (abi_ulong)arg5, arg6));
  12613. if (!is_error(ret) && ret > 0) {
  12614. if (arg2) {
  12615. if (put_user_u64(inoff, arg2)) {
  12616. return -TARGET_EFAULT;
  12617. }
  12618. }
  12619. if (arg4) {
  12620. if (put_user_u64(outoff, arg4)) {
  12621. return -TARGET_EFAULT;
  12622. }
  12623. }
  12624. }
  12625. }
  12626. return ret;
  12627. #endif
  12628. #if defined(TARGET_NR_pivot_root)
  12629. case TARGET_NR_pivot_root:
  12630. {
  12631. void *p2;
  12632. p = lock_user_string(arg1); /* new_root */
  12633. p2 = lock_user_string(arg2); /* put_old */
  12634. if (!p || !p2) {
  12635. ret = -TARGET_EFAULT;
  12636. } else {
  12637. ret = get_errno(pivot_root(p, p2));
  12638. }
  12639. unlock_user(p2, arg2, 0);
  12640. unlock_user(p, arg1, 0);
  12641. }
  12642. return ret;
  12643. #endif
  12644. #if defined(TARGET_NR_riscv_hwprobe)
  12645. case TARGET_NR_riscv_hwprobe:
  12646. return do_riscv_hwprobe(cpu_env, arg1, arg2, arg3, arg4, arg5);
  12647. #endif
  12648. default:
  12649. qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num);
  12650. return -TARGET_ENOSYS;
  12651. }
  12652. return ret;
  12653. }
  12654. abi_long do_syscall(CPUArchState *cpu_env, int num, abi_long arg1,
  12655. abi_long arg2, abi_long arg3, abi_long arg4,
  12656. abi_long arg5, abi_long arg6, abi_long arg7,
  12657. abi_long arg8)
  12658. {
  12659. CPUState *cpu = env_cpu(cpu_env);
  12660. abi_long ret;
  12661. #ifdef DEBUG_ERESTARTSYS
  12662. /* Debug-only code for exercising the syscall-restart code paths
  12663. * in the per-architecture cpu main loops: restart every syscall
  12664. * the guest makes once before letting it through.
  12665. */
  12666. {
  12667. static bool flag;
  12668. flag = !flag;
  12669. if (flag) {
  12670. return -QEMU_ERESTARTSYS;
  12671. }
  12672. }
  12673. #endif
  12674. record_syscall_start(cpu, num, arg1,
  12675. arg2, arg3, arg4, arg5, arg6, arg7, arg8);
  12676. if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
  12677. print_syscall(cpu_env, num, arg1, arg2, arg3, arg4, arg5, arg6);
  12678. }
  12679. ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4,
  12680. arg5, arg6, arg7, arg8);
  12681. if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
  12682. print_syscall_ret(cpu_env, num, ret, arg1, arg2,
  12683. arg3, arg4, arg5, arg6);
  12684. }
  12685. record_syscall_return(cpu, num, ret);
  12686. return ret;
  12687. }