syscall.c 437 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904
  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 "target_mman.h"
  27. #include <elf.h>
  28. #include <endian.h>
  29. #include <grp.h>
  30. #include <sys/ipc.h>
  31. #include <sys/msg.h>
  32. #include <sys/wait.h>
  33. #include <sys/mount.h>
  34. #include <sys/file.h>
  35. #include <sys/fsuid.h>
  36. #include <sys/personality.h>
  37. #include <sys/prctl.h>
  38. #include <sys/resource.h>
  39. #include <sys/swap.h>
  40. #include <linux/capability.h>
  41. #include <sched.h>
  42. #include <sys/timex.h>
  43. #include <sys/socket.h>
  44. #include <linux/sockios.h>
  45. #include <sys/un.h>
  46. #include <sys/uio.h>
  47. #include <poll.h>
  48. #include <sys/times.h>
  49. #include <sys/shm.h>
  50. #include <sys/sem.h>
  51. #include <sys/statfs.h>
  52. #include <utime.h>
  53. #include <sys/sysinfo.h>
  54. #include <sys/signalfd.h>
  55. //#include <sys/user.h>
  56. #include <netinet/in.h>
  57. #include <netinet/ip.h>
  58. #include <netinet/tcp.h>
  59. #include <netinet/udp.h>
  60. #include <linux/wireless.h>
  61. #include <linux/icmp.h>
  62. #include <linux/icmpv6.h>
  63. #include <linux/if_tun.h>
  64. #include <linux/in6.h>
  65. #include <linux/errqueue.h>
  66. #include <linux/random.h>
  67. #ifdef CONFIG_TIMERFD
  68. #include <sys/timerfd.h>
  69. #endif
  70. #ifdef CONFIG_EVENTFD
  71. #include <sys/eventfd.h>
  72. #endif
  73. #ifdef CONFIG_EPOLL
  74. #include <sys/epoll.h>
  75. #endif
  76. #ifdef CONFIG_ATTR
  77. #include "qemu/xattr.h"
  78. #endif
  79. #ifdef CONFIG_SENDFILE
  80. #include <sys/sendfile.h>
  81. #endif
  82. #ifdef HAVE_SYS_KCOV_H
  83. #include <sys/kcov.h>
  84. #endif
  85. #define termios host_termios
  86. #define winsize host_winsize
  87. #define termio host_termio
  88. #define sgttyb host_sgttyb /* same as target */
  89. #define tchars host_tchars /* same as target */
  90. #define ltchars host_ltchars /* same as target */
  91. #include <linux/termios.h>
  92. #include <linux/unistd.h>
  93. #include <linux/cdrom.h>
  94. #include <linux/hdreg.h>
  95. #include <linux/soundcard.h>
  96. #include <linux/kd.h>
  97. #include <linux/mtio.h>
  98. #include <linux/fs.h>
  99. #include <linux/fd.h>
  100. #if defined(CONFIG_FIEMAP)
  101. #include <linux/fiemap.h>
  102. #endif
  103. #include <linux/fb.h>
  104. #if defined(CONFIG_USBFS)
  105. #include <linux/usbdevice_fs.h>
  106. #include <linux/usb/ch9.h>
  107. #endif
  108. #include <linux/vt.h>
  109. #include <linux/dm-ioctl.h>
  110. #include <linux/reboot.h>
  111. #include <linux/route.h>
  112. #include <linux/filter.h>
  113. #include <linux/blkpg.h>
  114. #include <netpacket/packet.h>
  115. #include <linux/netlink.h>
  116. #include <linux/if_alg.h>
  117. #include <linux/rtc.h>
  118. #include <sound/asound.h>
  119. #ifdef HAVE_BTRFS_H
  120. #include <linux/btrfs.h>
  121. #endif
  122. #ifdef HAVE_DRM_H
  123. #include <libdrm/drm.h>
  124. #include <libdrm/i915_drm.h>
  125. #endif
  126. #include "linux_loop.h"
  127. #include "uname.h"
  128. #include "qemu.h"
  129. #include "user-internals.h"
  130. #include "strace.h"
  131. #include "signal-common.h"
  132. #include "loader.h"
  133. #include "user-mmap.h"
  134. #include "user/safe-syscall.h"
  135. #include "qemu/guest-random.h"
  136. #include "qemu/selfmap.h"
  137. #include "user/syscall-trace.h"
  138. #include "special-errno.h"
  139. #include "qapi/error.h"
  140. #include "fd-trans.h"
  141. #include "tcg/tcg.h"
  142. #include "cpu_loop-common.h"
  143. #ifndef CLONE_IO
  144. #define CLONE_IO 0x80000000 /* Clone io context */
  145. #endif
  146. /* We can't directly call the host clone syscall, because this will
  147. * badly confuse libc (breaking mutexes, for example). So we must
  148. * divide clone flags into:
  149. * * flag combinations that look like pthread_create()
  150. * * flag combinations that look like fork()
  151. * * flags we can implement within QEMU itself
  152. * * flags we can't support and will return an error for
  153. */
  154. /* For thread creation, all these flags must be present; for
  155. * fork, none must be present.
  156. */
  157. #define CLONE_THREAD_FLAGS \
  158. (CLONE_VM | CLONE_FS | CLONE_FILES | \
  159. CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM)
  160. /* These flags are ignored:
  161. * CLONE_DETACHED is now ignored by the kernel;
  162. * CLONE_IO is just an optimisation hint to the I/O scheduler
  163. */
  164. #define CLONE_IGNORED_FLAGS \
  165. (CLONE_DETACHED | CLONE_IO)
  166. #ifndef CLONE_PIDFD
  167. # define CLONE_PIDFD 0x00001000
  168. #endif
  169. /* Flags for fork which we can implement within QEMU itself */
  170. #define CLONE_OPTIONAL_FORK_FLAGS \
  171. (CLONE_SETTLS | CLONE_PARENT_SETTID | CLONE_PIDFD | \
  172. CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID)
  173. /* Flags for thread creation which we can implement within QEMU itself */
  174. #define CLONE_OPTIONAL_THREAD_FLAGS \
  175. (CLONE_SETTLS | CLONE_PARENT_SETTID | \
  176. CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | CLONE_PARENT)
  177. #define CLONE_INVALID_FORK_FLAGS \
  178. (~(CSIGNAL | CLONE_OPTIONAL_FORK_FLAGS | CLONE_IGNORED_FLAGS))
  179. #define CLONE_INVALID_THREAD_FLAGS \
  180. (~(CSIGNAL | CLONE_THREAD_FLAGS | CLONE_OPTIONAL_THREAD_FLAGS | \
  181. CLONE_IGNORED_FLAGS))
  182. /* CLONE_VFORK is special cased early in do_fork(). The other flag bits
  183. * have almost all been allocated. We cannot support any of
  184. * CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC,
  185. * CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, CLONE_PTRACE, CLONE_UNTRACED.
  186. * The checks against the invalid thread masks above will catch these.
  187. * (The one remaining unallocated bit is 0x1000 which used to be CLONE_PID.)
  188. */
  189. /* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
  190. * once. This exercises the codepaths for restart.
  191. */
  192. //#define DEBUG_ERESTARTSYS
  193. //#include <linux/msdos_fs.h>
  194. #define VFAT_IOCTL_READDIR_BOTH \
  195. _IOC(_IOC_READ, 'r', 1, (sizeof(struct linux_dirent) + 256) * 2)
  196. #define VFAT_IOCTL_READDIR_SHORT \
  197. _IOC(_IOC_READ, 'r', 2, (sizeof(struct linux_dirent) + 256) * 2)
  198. #undef _syscall0
  199. #undef _syscall1
  200. #undef _syscall2
  201. #undef _syscall3
  202. #undef _syscall4
  203. #undef _syscall5
  204. #undef _syscall6
  205. #define _syscall0(type,name) \
  206. static type name (void) \
  207. { \
  208. return syscall(__NR_##name); \
  209. }
  210. #define _syscall1(type,name,type1,arg1) \
  211. static type name (type1 arg1) \
  212. { \
  213. return syscall(__NR_##name, arg1); \
  214. }
  215. #define _syscall2(type,name,type1,arg1,type2,arg2) \
  216. static type name (type1 arg1,type2 arg2) \
  217. { \
  218. return syscall(__NR_##name, arg1, arg2); \
  219. }
  220. #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
  221. static type name (type1 arg1,type2 arg2,type3 arg3) \
  222. { \
  223. return syscall(__NR_##name, arg1, arg2, arg3); \
  224. }
  225. #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
  226. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
  227. { \
  228. return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
  229. }
  230. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  231. type5,arg5) \
  232. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
  233. { \
  234. return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
  235. }
  236. #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  237. type5,arg5,type6,arg6) \
  238. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
  239. type6 arg6) \
  240. { \
  241. return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
  242. }
  243. #define __NR_sys_uname __NR_uname
  244. #define __NR_sys_getcwd1 __NR_getcwd
  245. #define __NR_sys_getdents __NR_getdents
  246. #define __NR_sys_getdents64 __NR_getdents64
  247. #define __NR_sys_getpriority __NR_getpriority
  248. #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
  249. #define __NR_sys_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo
  250. #define __NR_sys_syslog __NR_syslog
  251. #if defined(__NR_futex)
  252. # define __NR_sys_futex __NR_futex
  253. #endif
  254. #if defined(__NR_futex_time64)
  255. # define __NR_sys_futex_time64 __NR_futex_time64
  256. #endif
  257. #define __NR_sys_statx __NR_statx
  258. #if defined(__alpha__) || defined(__x86_64__) || defined(__s390x__)
  259. #define __NR__llseek __NR_lseek
  260. #endif
  261. /* Newer kernel ports have llseek() instead of _llseek() */
  262. #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
  263. #define TARGET_NR__llseek TARGET_NR_llseek
  264. #endif
  265. /* some platforms need to mask more bits than just TARGET_O_NONBLOCK */
  266. #ifndef TARGET_O_NONBLOCK_MASK
  267. #define TARGET_O_NONBLOCK_MASK TARGET_O_NONBLOCK
  268. #endif
  269. #define __NR_sys_gettid __NR_gettid
  270. _syscall0(int, sys_gettid)
  271. /* For the 64-bit guest on 32-bit host case we must emulate
  272. * getdents using getdents64, because otherwise the host
  273. * might hand us back more dirent records than we can fit
  274. * into the guest buffer after structure format conversion.
  275. * Otherwise we emulate getdents with getdents if the host has it.
  276. */
  277. #if defined(__NR_getdents) && HOST_LONG_BITS >= TARGET_ABI_BITS
  278. #define EMULATE_GETDENTS_WITH_GETDENTS
  279. #endif
  280. #if defined(TARGET_NR_getdents) && defined(EMULATE_GETDENTS_WITH_GETDENTS)
  281. _syscall3(int, sys_getdents, unsigned int, fd, struct linux_dirent *, dirp, unsigned int, count);
  282. #endif
  283. #if (defined(TARGET_NR_getdents) && \
  284. !defined(EMULATE_GETDENTS_WITH_GETDENTS)) || \
  285. (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
  286. _syscall3(int, sys_getdents64, unsigned int, fd, struct linux_dirent64 *, dirp, unsigned int, count);
  287. #endif
  288. #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
  289. _syscall5(int, _llseek, unsigned int, fd, unsigned long, hi, unsigned long, lo,
  290. loff_t *, res, unsigned int, wh);
  291. #endif
  292. _syscall3(int, sys_rt_sigqueueinfo, pid_t, pid, int, sig, siginfo_t *, uinfo)
  293. _syscall4(int, sys_rt_tgsigqueueinfo, pid_t, pid, pid_t, tid, int, sig,
  294. siginfo_t *, uinfo)
  295. _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
  296. #ifdef __NR_exit_group
  297. _syscall1(int,exit_group,int,error_code)
  298. #endif
  299. #if defined(__NR_close_range) && defined(TARGET_NR_close_range)
  300. #define __NR_sys_close_range __NR_close_range
  301. _syscall3(int,sys_close_range,int,first,int,last,int,flags)
  302. #ifndef CLOSE_RANGE_CLOEXEC
  303. #define CLOSE_RANGE_CLOEXEC (1U << 2)
  304. #endif
  305. #endif
  306. #if defined(__NR_futex)
  307. _syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
  308. const struct timespec *,timeout,int *,uaddr2,int,val3)
  309. #endif
  310. #if defined(__NR_futex_time64)
  311. _syscall6(int,sys_futex_time64,int *,uaddr,int,op,int,val,
  312. const struct timespec *,timeout,int *,uaddr2,int,val3)
  313. #endif
  314. #if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
  315. _syscall2(int, pidfd_open, pid_t, pid, unsigned int, flags);
  316. #endif
  317. #if defined(__NR_pidfd_send_signal) && defined(TARGET_NR_pidfd_send_signal)
  318. _syscall4(int, pidfd_send_signal, int, pidfd, int, sig, siginfo_t *, info,
  319. unsigned int, flags);
  320. #endif
  321. #if defined(__NR_pidfd_getfd) && defined(TARGET_NR_pidfd_getfd)
  322. _syscall3(int, pidfd_getfd, int, pidfd, int, targetfd, unsigned int, flags);
  323. #endif
  324. #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
  325. _syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
  326. unsigned long *, user_mask_ptr);
  327. #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
  328. _syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
  329. unsigned long *, user_mask_ptr);
  330. /* sched_attr is not defined in glibc */
  331. struct sched_attr {
  332. uint32_t size;
  333. uint32_t sched_policy;
  334. uint64_t sched_flags;
  335. int32_t sched_nice;
  336. uint32_t sched_priority;
  337. uint64_t sched_runtime;
  338. uint64_t sched_deadline;
  339. uint64_t sched_period;
  340. uint32_t sched_util_min;
  341. uint32_t sched_util_max;
  342. };
  343. #define __NR_sys_sched_getattr __NR_sched_getattr
  344. _syscall4(int, sys_sched_getattr, pid_t, pid, struct sched_attr *, attr,
  345. unsigned int, size, unsigned int, flags);
  346. #define __NR_sys_sched_setattr __NR_sched_setattr
  347. _syscall3(int, sys_sched_setattr, pid_t, pid, struct sched_attr *, attr,
  348. unsigned int, flags);
  349. #define __NR_sys_sched_getscheduler __NR_sched_getscheduler
  350. _syscall1(int, sys_sched_getscheduler, pid_t, pid);
  351. #define __NR_sys_sched_setscheduler __NR_sched_setscheduler
  352. _syscall3(int, sys_sched_setscheduler, pid_t, pid, int, policy,
  353. const struct sched_param *, param);
  354. #define __NR_sys_sched_getparam __NR_sched_getparam
  355. _syscall2(int, sys_sched_getparam, pid_t, pid,
  356. struct sched_param *, param);
  357. #define __NR_sys_sched_setparam __NR_sched_setparam
  358. _syscall2(int, sys_sched_setparam, pid_t, pid,
  359. const struct sched_param *, param);
  360. #define __NR_sys_getcpu __NR_getcpu
  361. _syscall3(int, sys_getcpu, unsigned *, cpu, unsigned *, node, void *, tcache);
  362. _syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
  363. void *, arg);
  364. _syscall2(int, capget, struct __user_cap_header_struct *, header,
  365. struct __user_cap_data_struct *, data);
  366. _syscall2(int, capset, struct __user_cap_header_struct *, header,
  367. struct __user_cap_data_struct *, data);
  368. #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
  369. _syscall2(int, ioprio_get, int, which, int, who)
  370. #endif
  371. #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
  372. _syscall3(int, ioprio_set, int, which, int, who, int, ioprio)
  373. #endif
  374. #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
  375. _syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags)
  376. #endif
  377. #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
  378. _syscall5(int, kcmp, pid_t, pid1, pid_t, pid2, int, type,
  379. unsigned long, idx1, unsigned long, idx2)
  380. #endif
  381. /*
  382. * It is assumed that struct statx is architecture independent.
  383. */
  384. #if defined(TARGET_NR_statx) && defined(__NR_statx)
  385. _syscall5(int, sys_statx, int, dirfd, const char *, pathname, int, flags,
  386. unsigned int, mask, struct target_statx *, statxbuf)
  387. #endif
  388. #if defined(TARGET_NR_membarrier) && defined(__NR_membarrier)
  389. _syscall2(int, membarrier, int, cmd, int, flags)
  390. #endif
  391. static const bitmask_transtbl fcntl_flags_tbl[] = {
  392. { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
  393. { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
  394. { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
  395. { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
  396. { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
  397. { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
  398. { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
  399. { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
  400. { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
  401. { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
  402. { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
  403. { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
  404. { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
  405. #if defined(O_DIRECT)
  406. { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
  407. #endif
  408. #if defined(O_NOATIME)
  409. { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
  410. #endif
  411. #if defined(O_CLOEXEC)
  412. { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
  413. #endif
  414. #if defined(O_PATH)
  415. { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
  416. #endif
  417. #if defined(O_TMPFILE)
  418. { TARGET_O_TMPFILE, TARGET_O_TMPFILE, O_TMPFILE, O_TMPFILE },
  419. #endif
  420. /* Don't terminate the list prematurely on 64-bit host+guest. */
  421. #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
  422. { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
  423. #endif
  424. };
  425. _syscall2(int, sys_getcwd1, char *, buf, size_t, size)
  426. #if defined(TARGET_NR_utimensat) || defined(TARGET_NR_utimensat_time64)
  427. #if defined(__NR_utimensat)
  428. #define __NR_sys_utimensat __NR_utimensat
  429. _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
  430. const struct timespec *,tsp,int,flags)
  431. #else
  432. static int sys_utimensat(int dirfd, const char *pathname,
  433. const struct timespec times[2], int flags)
  434. {
  435. errno = ENOSYS;
  436. return -1;
  437. }
  438. #endif
  439. #endif /* TARGET_NR_utimensat */
  440. #ifdef TARGET_NR_renameat2
  441. #if defined(__NR_renameat2)
  442. #define __NR_sys_renameat2 __NR_renameat2
  443. _syscall5(int, sys_renameat2, int, oldfd, const char *, old, int, newfd,
  444. const char *, new, unsigned int, flags)
  445. #else
  446. static int sys_renameat2(int oldfd, const char *old,
  447. int newfd, const char *new, int flags)
  448. {
  449. if (flags == 0) {
  450. return renameat(oldfd, old, newfd, new);
  451. }
  452. errno = ENOSYS;
  453. return -1;
  454. }
  455. #endif
  456. #endif /* TARGET_NR_renameat2 */
  457. #ifdef CONFIG_INOTIFY
  458. #include <sys/inotify.h>
  459. #else
  460. /* Userspace can usually survive runtime without inotify */
  461. #undef TARGET_NR_inotify_init
  462. #undef TARGET_NR_inotify_init1
  463. #undef TARGET_NR_inotify_add_watch
  464. #undef TARGET_NR_inotify_rm_watch
  465. #endif /* CONFIG_INOTIFY */
  466. #if defined(TARGET_NR_prlimit64)
  467. #ifndef __NR_prlimit64
  468. # define __NR_prlimit64 -1
  469. #endif
  470. #define __NR_sys_prlimit64 __NR_prlimit64
  471. /* The glibc rlimit structure may not be that used by the underlying syscall */
  472. struct host_rlimit64 {
  473. uint64_t rlim_cur;
  474. uint64_t rlim_max;
  475. };
  476. _syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
  477. const struct host_rlimit64 *, new_limit,
  478. struct host_rlimit64 *, old_limit)
  479. #endif
  480. #if defined(TARGET_NR_timer_create)
  481. /* Maximum of 32 active POSIX timers allowed at any one time. */
  482. #define GUEST_TIMER_MAX 32
  483. static timer_t g_posix_timers[GUEST_TIMER_MAX];
  484. static int g_posix_timer_allocated[GUEST_TIMER_MAX];
  485. static inline int next_free_host_timer(void)
  486. {
  487. int k;
  488. for (k = 0; k < ARRAY_SIZE(g_posix_timer_allocated); k++) {
  489. if (qatomic_xchg(g_posix_timer_allocated + k, 1) == 0) {
  490. return k;
  491. }
  492. }
  493. return -1;
  494. }
  495. static inline void free_host_timer_slot(int id)
  496. {
  497. qatomic_store_release(g_posix_timer_allocated + id, 0);
  498. }
  499. #endif
  500. static inline int host_to_target_errno(int host_errno)
  501. {
  502. switch (host_errno) {
  503. #define E(X) case X: return TARGET_##X;
  504. #include "errnos.c.inc"
  505. #undef E
  506. default:
  507. return host_errno;
  508. }
  509. }
  510. static inline int target_to_host_errno(int target_errno)
  511. {
  512. switch (target_errno) {
  513. #define E(X) case TARGET_##X: return X;
  514. #include "errnos.c.inc"
  515. #undef E
  516. default:
  517. return target_errno;
  518. }
  519. }
  520. abi_long get_errno(abi_long ret)
  521. {
  522. if (ret == -1)
  523. return -host_to_target_errno(errno);
  524. else
  525. return ret;
  526. }
  527. const char *target_strerror(int err)
  528. {
  529. if (err == QEMU_ERESTARTSYS) {
  530. return "To be restarted";
  531. }
  532. if (err == QEMU_ESIGRETURN) {
  533. return "Successful exit from sigreturn";
  534. }
  535. return strerror(target_to_host_errno(err));
  536. }
  537. static int check_zeroed_user(abi_long addr, size_t ksize, size_t usize)
  538. {
  539. int i;
  540. uint8_t b;
  541. if (usize <= ksize) {
  542. return 1;
  543. }
  544. for (i = ksize; i < usize; i++) {
  545. if (get_user_u8(b, addr + i)) {
  546. return -TARGET_EFAULT;
  547. }
  548. if (b != 0) {
  549. return 0;
  550. }
  551. }
  552. return 1;
  553. }
  554. #define safe_syscall0(type, name) \
  555. static type safe_##name(void) \
  556. { \
  557. return safe_syscall(__NR_##name); \
  558. }
  559. #define safe_syscall1(type, name, type1, arg1) \
  560. static type safe_##name(type1 arg1) \
  561. { \
  562. return safe_syscall(__NR_##name, arg1); \
  563. }
  564. #define safe_syscall2(type, name, type1, arg1, type2, arg2) \
  565. static type safe_##name(type1 arg1, type2 arg2) \
  566. { \
  567. return safe_syscall(__NR_##name, arg1, arg2); \
  568. }
  569. #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
  570. static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
  571. { \
  572. return safe_syscall(__NR_##name, arg1, arg2, arg3); \
  573. }
  574. #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
  575. type4, arg4) \
  576. static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
  577. { \
  578. return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
  579. }
  580. #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
  581. type4, arg4, type5, arg5) \
  582. static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  583. type5 arg5) \
  584. { \
  585. return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
  586. }
  587. #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
  588. type4, arg4, type5, arg5, type6, arg6) \
  589. static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  590. type5 arg5, type6 arg6) \
  591. { \
  592. return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
  593. }
  594. safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count)
  595. safe_syscall3(ssize_t, write, int, fd, const void *, buff, size_t, count)
  596. safe_syscall4(int, openat, int, dirfd, const char *, pathname, \
  597. int, flags, mode_t, mode)
  598. #if defined(TARGET_NR_wait4) || defined(TARGET_NR_waitpid)
  599. safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \
  600. struct rusage *, rusage)
  601. #endif
  602. safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \
  603. int, options, struct rusage *, rusage)
  604. safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp)
  605. safe_syscall5(int, execveat, int, dirfd, const char *, filename,
  606. char **, argv, char **, envp, int, flags)
  607. #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
  608. defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
  609. safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \
  610. fd_set *, exceptfds, struct timespec *, timeout, void *, sig)
  611. #endif
  612. #if defined(TARGET_NR_ppoll) || defined(TARGET_NR_ppoll_time64)
  613. safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds,
  614. struct timespec *, tsp, const sigset_t *, sigmask,
  615. size_t, sigsetsize)
  616. #endif
  617. safe_syscall6(int, epoll_pwait, int, epfd, struct epoll_event *, events,
  618. int, maxevents, int, timeout, const sigset_t *, sigmask,
  619. size_t, sigsetsize)
  620. #if defined(__NR_futex)
  621. safe_syscall6(int,futex,int *,uaddr,int,op,int,val, \
  622. const struct timespec *,timeout,int *,uaddr2,int,val3)
  623. #endif
  624. #if defined(__NR_futex_time64)
  625. safe_syscall6(int,futex_time64,int *,uaddr,int,op,int,val, \
  626. const struct timespec *,timeout,int *,uaddr2,int,val3)
  627. #endif
  628. safe_syscall2(int, rt_sigsuspend, sigset_t *, newset, size_t, sigsetsize)
  629. safe_syscall2(int, kill, pid_t, pid, int, sig)
  630. safe_syscall2(int, tkill, int, tid, int, sig)
  631. safe_syscall3(int, tgkill, int, tgid, int, pid, int, sig)
  632. safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt)
  633. safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt)
  634. safe_syscall5(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt,
  635. unsigned long, pos_l, unsigned long, pos_h)
  636. safe_syscall5(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, iovcnt,
  637. unsigned long, pos_l, unsigned long, pos_h)
  638. safe_syscall3(int, connect, int, fd, const struct sockaddr *, addr,
  639. socklen_t, addrlen)
  640. safe_syscall6(ssize_t, sendto, int, fd, const void *, buf, size_t, len,
  641. int, flags, const struct sockaddr *, addr, socklen_t, addrlen)
  642. safe_syscall6(ssize_t, recvfrom, int, fd, void *, buf, size_t, len,
  643. int, flags, struct sockaddr *, addr, socklen_t *, addrlen)
  644. safe_syscall3(ssize_t, sendmsg, int, fd, const struct msghdr *, msg, int, flags)
  645. safe_syscall3(ssize_t, recvmsg, int, fd, struct msghdr *, msg, int, flags)
  646. safe_syscall2(int, flock, int, fd, int, operation)
  647. #if defined(TARGET_NR_rt_sigtimedwait) || defined(TARGET_NR_rt_sigtimedwait_time64)
  648. safe_syscall4(int, rt_sigtimedwait, const sigset_t *, these, siginfo_t *, uinfo,
  649. const struct timespec *, uts, size_t, sigsetsize)
  650. #endif
  651. safe_syscall4(int, accept4, int, fd, struct sockaddr *, addr, socklen_t *, len,
  652. int, flags)
  653. #if defined(TARGET_NR_nanosleep)
  654. safe_syscall2(int, nanosleep, const struct timespec *, req,
  655. struct timespec *, rem)
  656. #endif
  657. #if defined(TARGET_NR_clock_nanosleep) || \
  658. defined(TARGET_NR_clock_nanosleep_time64)
  659. safe_syscall4(int, clock_nanosleep, const clockid_t, clock, int, flags,
  660. const struct timespec *, req, struct timespec *, rem)
  661. #endif
  662. #ifdef __NR_ipc
  663. #ifdef __s390x__
  664. safe_syscall5(int, ipc, int, call, long, first, long, second, long, third,
  665. void *, ptr)
  666. #else
  667. safe_syscall6(int, ipc, int, call, long, first, long, second, long, third,
  668. void *, ptr, long, fifth)
  669. #endif
  670. #endif
  671. #ifdef __NR_msgsnd
  672. safe_syscall4(int, msgsnd, int, msgid, const void *, msgp, size_t, sz,
  673. int, flags)
  674. #endif
  675. #ifdef __NR_msgrcv
  676. safe_syscall5(int, msgrcv, int, msgid, void *, msgp, size_t, sz,
  677. long, msgtype, int, flags)
  678. #endif
  679. #ifdef __NR_semtimedop
  680. safe_syscall4(int, semtimedop, int, semid, struct sembuf *, tsops,
  681. unsigned, nsops, const struct timespec *, timeout)
  682. #endif
  683. #if defined(TARGET_NR_mq_timedsend) || \
  684. defined(TARGET_NR_mq_timedsend_time64)
  685. safe_syscall5(int, mq_timedsend, int, mqdes, const char *, msg_ptr,
  686. size_t, len, unsigned, prio, const struct timespec *, timeout)
  687. #endif
  688. #if defined(TARGET_NR_mq_timedreceive) || \
  689. defined(TARGET_NR_mq_timedreceive_time64)
  690. safe_syscall5(int, mq_timedreceive, int, mqdes, char *, msg_ptr,
  691. size_t, len, unsigned *, prio, const struct timespec *, timeout)
  692. #endif
  693. #if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range)
  694. safe_syscall6(ssize_t, copy_file_range, int, infd, loff_t *, pinoff,
  695. int, outfd, loff_t *, poutoff, size_t, length,
  696. unsigned int, flags)
  697. #endif
  698. /* We do ioctl like this rather than via safe_syscall3 to preserve the
  699. * "third argument might be integer or pointer or not present" behaviour of
  700. * the libc function.
  701. */
  702. #define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
  703. /* Similarly for fcntl. Note that callers must always:
  704. * pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK
  705. * use the flock64 struct rather than unsuffixed flock
  706. * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts.
  707. */
  708. #ifdef __NR_fcntl64
  709. #define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
  710. #else
  711. #define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
  712. #endif
  713. static inline int host_to_target_sock_type(int host_type)
  714. {
  715. int target_type;
  716. switch (host_type & 0xf /* SOCK_TYPE_MASK */) {
  717. case SOCK_DGRAM:
  718. target_type = TARGET_SOCK_DGRAM;
  719. break;
  720. case SOCK_STREAM:
  721. target_type = TARGET_SOCK_STREAM;
  722. break;
  723. default:
  724. target_type = host_type & 0xf /* SOCK_TYPE_MASK */;
  725. break;
  726. }
  727. #if defined(SOCK_CLOEXEC)
  728. if (host_type & SOCK_CLOEXEC) {
  729. target_type |= TARGET_SOCK_CLOEXEC;
  730. }
  731. #endif
  732. #if defined(SOCK_NONBLOCK)
  733. if (host_type & SOCK_NONBLOCK) {
  734. target_type |= TARGET_SOCK_NONBLOCK;
  735. }
  736. #endif
  737. return target_type;
  738. }
  739. static abi_ulong target_brk, initial_target_brk;
  740. void target_set_brk(abi_ulong new_brk)
  741. {
  742. target_brk = TARGET_PAGE_ALIGN(new_brk);
  743. initial_target_brk = target_brk;
  744. }
  745. /* do_brk() must return target values and target errnos. */
  746. abi_long do_brk(abi_ulong brk_val)
  747. {
  748. abi_long mapped_addr;
  749. abi_ulong new_brk;
  750. abi_ulong old_brk;
  751. /* brk pointers are always untagged */
  752. /* do not allow to shrink below initial brk value */
  753. if (brk_val < initial_target_brk) {
  754. return target_brk;
  755. }
  756. new_brk = TARGET_PAGE_ALIGN(brk_val);
  757. old_brk = TARGET_PAGE_ALIGN(target_brk);
  758. /* new and old target_brk might be on the same page */
  759. if (new_brk == old_brk) {
  760. target_brk = brk_val;
  761. return target_brk;
  762. }
  763. /* Release heap if necesary */
  764. if (new_brk < old_brk) {
  765. target_munmap(new_brk, old_brk - new_brk);
  766. target_brk = brk_val;
  767. return target_brk;
  768. }
  769. mapped_addr = target_mmap(old_brk, new_brk - old_brk,
  770. PROT_READ | PROT_WRITE,
  771. MAP_FIXED_NOREPLACE | MAP_ANON | MAP_PRIVATE,
  772. -1, 0);
  773. if (mapped_addr == old_brk) {
  774. target_brk = brk_val;
  775. return target_brk;
  776. }
  777. #if defined(TARGET_ALPHA)
  778. /* We (partially) emulate OSF/1 on Alpha, which requires we
  779. return a proper errno, not an unchanged brk value. */
  780. return -TARGET_ENOMEM;
  781. #endif
  782. /* For everything else, return the previous break. */
  783. return target_brk;
  784. }
  785. #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
  786. defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
  787. static inline abi_long copy_from_user_fdset(fd_set *fds,
  788. abi_ulong target_fds_addr,
  789. int n)
  790. {
  791. int i, nw, j, k;
  792. abi_ulong b, *target_fds;
  793. nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
  794. if (!(target_fds = lock_user(VERIFY_READ,
  795. target_fds_addr,
  796. sizeof(abi_ulong) * nw,
  797. 1)))
  798. return -TARGET_EFAULT;
  799. FD_ZERO(fds);
  800. k = 0;
  801. for (i = 0; i < nw; i++) {
  802. /* grab the abi_ulong */
  803. __get_user(b, &target_fds[i]);
  804. for (j = 0; j < TARGET_ABI_BITS; j++) {
  805. /* check the bit inside the abi_ulong */
  806. if ((b >> j) & 1)
  807. FD_SET(k, fds);
  808. k++;
  809. }
  810. }
  811. unlock_user(target_fds, target_fds_addr, 0);
  812. return 0;
  813. }
  814. static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
  815. abi_ulong target_fds_addr,
  816. int n)
  817. {
  818. if (target_fds_addr) {
  819. if (copy_from_user_fdset(fds, target_fds_addr, n))
  820. return -TARGET_EFAULT;
  821. *fds_ptr = fds;
  822. } else {
  823. *fds_ptr = NULL;
  824. }
  825. return 0;
  826. }
  827. static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
  828. const fd_set *fds,
  829. int n)
  830. {
  831. int i, nw, j, k;
  832. abi_long v;
  833. abi_ulong *target_fds;
  834. nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
  835. if (!(target_fds = lock_user(VERIFY_WRITE,
  836. target_fds_addr,
  837. sizeof(abi_ulong) * nw,
  838. 0)))
  839. return -TARGET_EFAULT;
  840. k = 0;
  841. for (i = 0; i < nw; i++) {
  842. v = 0;
  843. for (j = 0; j < TARGET_ABI_BITS; j++) {
  844. v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
  845. k++;
  846. }
  847. __put_user(v, &target_fds[i]);
  848. }
  849. unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
  850. return 0;
  851. }
  852. #endif
  853. #if defined(__alpha__)
  854. #define HOST_HZ 1024
  855. #else
  856. #define HOST_HZ 100
  857. #endif
  858. static inline abi_long host_to_target_clock_t(long ticks)
  859. {
  860. #if HOST_HZ == TARGET_HZ
  861. return ticks;
  862. #else
  863. return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
  864. #endif
  865. }
  866. static inline abi_long host_to_target_rusage(abi_ulong target_addr,
  867. const struct rusage *rusage)
  868. {
  869. struct target_rusage *target_rusage;
  870. if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
  871. return -TARGET_EFAULT;
  872. target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
  873. target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
  874. target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
  875. target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
  876. target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
  877. target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
  878. target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
  879. target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
  880. target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
  881. target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
  882. target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
  883. target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
  884. target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
  885. target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
  886. target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
  887. target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
  888. target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
  889. target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
  890. unlock_user_struct(target_rusage, target_addr, 1);
  891. return 0;
  892. }
  893. #ifdef TARGET_NR_setrlimit
  894. static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
  895. {
  896. abi_ulong target_rlim_swap;
  897. rlim_t result;
  898. target_rlim_swap = tswapal(target_rlim);
  899. if (target_rlim_swap == TARGET_RLIM_INFINITY)
  900. return RLIM_INFINITY;
  901. result = target_rlim_swap;
  902. if (target_rlim_swap != (rlim_t)result)
  903. return RLIM_INFINITY;
  904. return result;
  905. }
  906. #endif
  907. #if defined(TARGET_NR_getrlimit) || defined(TARGET_NR_ugetrlimit)
  908. static inline abi_ulong host_to_target_rlim(rlim_t rlim)
  909. {
  910. abi_ulong target_rlim_swap;
  911. abi_ulong result;
  912. if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
  913. target_rlim_swap = TARGET_RLIM_INFINITY;
  914. else
  915. target_rlim_swap = rlim;
  916. result = tswapal(target_rlim_swap);
  917. return result;
  918. }
  919. #endif
  920. static inline int target_to_host_resource(int code)
  921. {
  922. switch (code) {
  923. case TARGET_RLIMIT_AS:
  924. return RLIMIT_AS;
  925. case TARGET_RLIMIT_CORE:
  926. return RLIMIT_CORE;
  927. case TARGET_RLIMIT_CPU:
  928. return RLIMIT_CPU;
  929. case TARGET_RLIMIT_DATA:
  930. return RLIMIT_DATA;
  931. case TARGET_RLIMIT_FSIZE:
  932. return RLIMIT_FSIZE;
  933. case TARGET_RLIMIT_LOCKS:
  934. return RLIMIT_LOCKS;
  935. case TARGET_RLIMIT_MEMLOCK:
  936. return RLIMIT_MEMLOCK;
  937. case TARGET_RLIMIT_MSGQUEUE:
  938. return RLIMIT_MSGQUEUE;
  939. case TARGET_RLIMIT_NICE:
  940. return RLIMIT_NICE;
  941. case TARGET_RLIMIT_NOFILE:
  942. return RLIMIT_NOFILE;
  943. case TARGET_RLIMIT_NPROC:
  944. return RLIMIT_NPROC;
  945. case TARGET_RLIMIT_RSS:
  946. return RLIMIT_RSS;
  947. case TARGET_RLIMIT_RTPRIO:
  948. return RLIMIT_RTPRIO;
  949. #ifdef RLIMIT_RTTIME
  950. case TARGET_RLIMIT_RTTIME:
  951. return RLIMIT_RTTIME;
  952. #endif
  953. case TARGET_RLIMIT_SIGPENDING:
  954. return RLIMIT_SIGPENDING;
  955. case TARGET_RLIMIT_STACK:
  956. return RLIMIT_STACK;
  957. default:
  958. return code;
  959. }
  960. }
  961. static inline abi_long copy_from_user_timeval(struct timeval *tv,
  962. abi_ulong target_tv_addr)
  963. {
  964. struct target_timeval *target_tv;
  965. if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) {
  966. return -TARGET_EFAULT;
  967. }
  968. __get_user(tv->tv_sec, &target_tv->tv_sec);
  969. __get_user(tv->tv_usec, &target_tv->tv_usec);
  970. unlock_user_struct(target_tv, target_tv_addr, 0);
  971. return 0;
  972. }
  973. static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
  974. const struct timeval *tv)
  975. {
  976. struct target_timeval *target_tv;
  977. if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
  978. return -TARGET_EFAULT;
  979. }
  980. __put_user(tv->tv_sec, &target_tv->tv_sec);
  981. __put_user(tv->tv_usec, &target_tv->tv_usec);
  982. unlock_user_struct(target_tv, target_tv_addr, 1);
  983. return 0;
  984. }
  985. #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
  986. static inline abi_long copy_from_user_timeval64(struct timeval *tv,
  987. abi_ulong target_tv_addr)
  988. {
  989. struct target__kernel_sock_timeval *target_tv;
  990. if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) {
  991. return -TARGET_EFAULT;
  992. }
  993. __get_user(tv->tv_sec, &target_tv->tv_sec);
  994. __get_user(tv->tv_usec, &target_tv->tv_usec);
  995. unlock_user_struct(target_tv, target_tv_addr, 0);
  996. return 0;
  997. }
  998. #endif
  999. static inline abi_long copy_to_user_timeval64(abi_ulong target_tv_addr,
  1000. const struct timeval *tv)
  1001. {
  1002. struct target__kernel_sock_timeval *target_tv;
  1003. if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
  1004. return -TARGET_EFAULT;
  1005. }
  1006. __put_user(tv->tv_sec, &target_tv->tv_sec);
  1007. __put_user(tv->tv_usec, &target_tv->tv_usec);
  1008. unlock_user_struct(target_tv, target_tv_addr, 1);
  1009. return 0;
  1010. }
  1011. #if defined(TARGET_NR_futex) || \
  1012. defined(TARGET_NR_rt_sigtimedwait) || \
  1013. defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6) || \
  1014. defined(TARGET_NR_nanosleep) || defined(TARGET_NR_clock_settime) || \
  1015. defined(TARGET_NR_utimensat) || defined(TARGET_NR_mq_timedsend) || \
  1016. defined(TARGET_NR_mq_timedreceive) || defined(TARGET_NR_ipc) || \
  1017. defined(TARGET_NR_semop) || defined(TARGET_NR_semtimedop) || \
  1018. defined(TARGET_NR_timer_settime) || \
  1019. (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
  1020. static inline abi_long target_to_host_timespec(struct timespec *host_ts,
  1021. abi_ulong target_addr)
  1022. {
  1023. struct target_timespec *target_ts;
  1024. if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
  1025. return -TARGET_EFAULT;
  1026. }
  1027. __get_user(host_ts->tv_sec, &target_ts->tv_sec);
  1028. __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1029. unlock_user_struct(target_ts, target_addr, 0);
  1030. return 0;
  1031. }
  1032. #endif
  1033. #if defined(TARGET_NR_clock_settime64) || defined(TARGET_NR_futex_time64) || \
  1034. defined(TARGET_NR_timer_settime64) || \
  1035. defined(TARGET_NR_mq_timedsend_time64) || \
  1036. defined(TARGET_NR_mq_timedreceive_time64) || \
  1037. (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)) || \
  1038. defined(TARGET_NR_clock_nanosleep_time64) || \
  1039. defined(TARGET_NR_rt_sigtimedwait_time64) || \
  1040. defined(TARGET_NR_utimensat) || \
  1041. defined(TARGET_NR_utimensat_time64) || \
  1042. defined(TARGET_NR_semtimedop_time64) || \
  1043. defined(TARGET_NR_pselect6_time64) || defined(TARGET_NR_ppoll_time64)
  1044. static inline abi_long target_to_host_timespec64(struct timespec *host_ts,
  1045. abi_ulong target_addr)
  1046. {
  1047. struct target__kernel_timespec *target_ts;
  1048. if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
  1049. return -TARGET_EFAULT;
  1050. }
  1051. __get_user(host_ts->tv_sec, &target_ts->tv_sec);
  1052. __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1053. /* in 32bit mode, this drops the padding */
  1054. host_ts->tv_nsec = (long)(abi_long)host_ts->tv_nsec;
  1055. unlock_user_struct(target_ts, target_addr, 0);
  1056. return 0;
  1057. }
  1058. #endif
  1059. static inline abi_long host_to_target_timespec(abi_ulong target_addr,
  1060. struct timespec *host_ts)
  1061. {
  1062. struct target_timespec *target_ts;
  1063. if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
  1064. return -TARGET_EFAULT;
  1065. }
  1066. __put_user(host_ts->tv_sec, &target_ts->tv_sec);
  1067. __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1068. unlock_user_struct(target_ts, target_addr, 1);
  1069. return 0;
  1070. }
  1071. static inline abi_long host_to_target_timespec64(abi_ulong target_addr,
  1072. struct timespec *host_ts)
  1073. {
  1074. struct target__kernel_timespec *target_ts;
  1075. if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
  1076. return -TARGET_EFAULT;
  1077. }
  1078. __put_user(host_ts->tv_sec, &target_ts->tv_sec);
  1079. __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1080. unlock_user_struct(target_ts, target_addr, 1);
  1081. return 0;
  1082. }
  1083. #if defined(TARGET_NR_gettimeofday)
  1084. static inline abi_long copy_to_user_timezone(abi_ulong target_tz_addr,
  1085. struct timezone *tz)
  1086. {
  1087. struct target_timezone *target_tz;
  1088. if (!lock_user_struct(VERIFY_WRITE, target_tz, target_tz_addr, 1)) {
  1089. return -TARGET_EFAULT;
  1090. }
  1091. __put_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
  1092. __put_user(tz->tz_dsttime, &target_tz->tz_dsttime);
  1093. unlock_user_struct(target_tz, target_tz_addr, 1);
  1094. return 0;
  1095. }
  1096. #endif
  1097. #if defined(TARGET_NR_settimeofday)
  1098. static inline abi_long copy_from_user_timezone(struct timezone *tz,
  1099. abi_ulong target_tz_addr)
  1100. {
  1101. struct target_timezone *target_tz;
  1102. if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) {
  1103. return -TARGET_EFAULT;
  1104. }
  1105. __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
  1106. __get_user(tz->tz_dsttime, &target_tz->tz_dsttime);
  1107. unlock_user_struct(target_tz, target_tz_addr, 0);
  1108. return 0;
  1109. }
  1110. #endif
  1111. #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
  1112. #include <mqueue.h>
  1113. static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
  1114. abi_ulong target_mq_attr_addr)
  1115. {
  1116. struct target_mq_attr *target_mq_attr;
  1117. if (!lock_user_struct(VERIFY_READ, target_mq_attr,
  1118. target_mq_attr_addr, 1))
  1119. return -TARGET_EFAULT;
  1120. __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
  1121. __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
  1122. __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
  1123. __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
  1124. unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
  1125. return 0;
  1126. }
  1127. static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
  1128. const struct mq_attr *attr)
  1129. {
  1130. struct target_mq_attr *target_mq_attr;
  1131. if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
  1132. target_mq_attr_addr, 0))
  1133. return -TARGET_EFAULT;
  1134. __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
  1135. __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
  1136. __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
  1137. __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
  1138. unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
  1139. return 0;
  1140. }
  1141. #endif
  1142. #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
  1143. /* do_select() must return target values and target errnos. */
  1144. static abi_long do_select(int n,
  1145. abi_ulong rfd_addr, abi_ulong wfd_addr,
  1146. abi_ulong efd_addr, abi_ulong target_tv_addr)
  1147. {
  1148. fd_set rfds, wfds, efds;
  1149. fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
  1150. struct timeval tv;
  1151. struct timespec ts, *ts_ptr;
  1152. abi_long ret;
  1153. ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
  1154. if (ret) {
  1155. return ret;
  1156. }
  1157. ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
  1158. if (ret) {
  1159. return ret;
  1160. }
  1161. ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
  1162. if (ret) {
  1163. return ret;
  1164. }
  1165. if (target_tv_addr) {
  1166. if (copy_from_user_timeval(&tv, target_tv_addr))
  1167. return -TARGET_EFAULT;
  1168. ts.tv_sec = tv.tv_sec;
  1169. ts.tv_nsec = tv.tv_usec * 1000;
  1170. ts_ptr = &ts;
  1171. } else {
  1172. ts_ptr = NULL;
  1173. }
  1174. ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
  1175. ts_ptr, NULL));
  1176. if (!is_error(ret)) {
  1177. if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
  1178. return -TARGET_EFAULT;
  1179. if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
  1180. return -TARGET_EFAULT;
  1181. if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
  1182. return -TARGET_EFAULT;
  1183. if (target_tv_addr) {
  1184. tv.tv_sec = ts.tv_sec;
  1185. tv.tv_usec = ts.tv_nsec / 1000;
  1186. if (copy_to_user_timeval(target_tv_addr, &tv)) {
  1187. return -TARGET_EFAULT;
  1188. }
  1189. }
  1190. }
  1191. return ret;
  1192. }
  1193. #if defined(TARGET_WANT_OLD_SYS_SELECT)
  1194. static abi_long do_old_select(abi_ulong arg1)
  1195. {
  1196. struct target_sel_arg_struct *sel;
  1197. abi_ulong inp, outp, exp, tvp;
  1198. long nsel;
  1199. if (!lock_user_struct(VERIFY_READ, sel, arg1, 1)) {
  1200. return -TARGET_EFAULT;
  1201. }
  1202. nsel = tswapal(sel->n);
  1203. inp = tswapal(sel->inp);
  1204. outp = tswapal(sel->outp);
  1205. exp = tswapal(sel->exp);
  1206. tvp = tswapal(sel->tvp);
  1207. unlock_user_struct(sel, arg1, 0);
  1208. return do_select(nsel, inp, outp, exp, tvp);
  1209. }
  1210. #endif
  1211. #endif
  1212. #if defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
  1213. static abi_long do_pselect6(abi_long arg1, abi_long arg2, abi_long arg3,
  1214. abi_long arg4, abi_long arg5, abi_long arg6,
  1215. bool time64)
  1216. {
  1217. abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
  1218. fd_set rfds, wfds, efds;
  1219. fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
  1220. struct timespec ts, *ts_ptr;
  1221. abi_long ret;
  1222. /*
  1223. * The 6th arg is actually two args smashed together,
  1224. * so we cannot use the C library.
  1225. */
  1226. struct {
  1227. sigset_t *set;
  1228. size_t size;
  1229. } sig, *sig_ptr;
  1230. abi_ulong arg_sigset, arg_sigsize, *arg7;
  1231. n = arg1;
  1232. rfd_addr = arg2;
  1233. wfd_addr = arg3;
  1234. efd_addr = arg4;
  1235. ts_addr = arg5;
  1236. ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
  1237. if (ret) {
  1238. return ret;
  1239. }
  1240. ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
  1241. if (ret) {
  1242. return ret;
  1243. }
  1244. ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
  1245. if (ret) {
  1246. return ret;
  1247. }
  1248. /*
  1249. * This takes a timespec, and not a timeval, so we cannot
  1250. * use the do_select() helper ...
  1251. */
  1252. if (ts_addr) {
  1253. if (time64) {
  1254. if (target_to_host_timespec64(&ts, ts_addr)) {
  1255. return -TARGET_EFAULT;
  1256. }
  1257. } else {
  1258. if (target_to_host_timespec(&ts, ts_addr)) {
  1259. return -TARGET_EFAULT;
  1260. }
  1261. }
  1262. ts_ptr = &ts;
  1263. } else {
  1264. ts_ptr = NULL;
  1265. }
  1266. /* Extract the two packed args for the sigset */
  1267. sig_ptr = NULL;
  1268. if (arg6) {
  1269. arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
  1270. if (!arg7) {
  1271. return -TARGET_EFAULT;
  1272. }
  1273. arg_sigset = tswapal(arg7[0]);
  1274. arg_sigsize = tswapal(arg7[1]);
  1275. unlock_user(arg7, arg6, 0);
  1276. if (arg_sigset) {
  1277. ret = process_sigsuspend_mask(&sig.set, arg_sigset, arg_sigsize);
  1278. if (ret != 0) {
  1279. return ret;
  1280. }
  1281. sig_ptr = &sig;
  1282. sig.size = SIGSET_T_SIZE;
  1283. }
  1284. }
  1285. ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
  1286. ts_ptr, sig_ptr));
  1287. if (sig_ptr) {
  1288. finish_sigsuspend_mask(ret);
  1289. }
  1290. if (!is_error(ret)) {
  1291. if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) {
  1292. return -TARGET_EFAULT;
  1293. }
  1294. if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) {
  1295. return -TARGET_EFAULT;
  1296. }
  1297. if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) {
  1298. return -TARGET_EFAULT;
  1299. }
  1300. if (time64) {
  1301. if (ts_addr && host_to_target_timespec64(ts_addr, &ts)) {
  1302. return -TARGET_EFAULT;
  1303. }
  1304. } else {
  1305. if (ts_addr && host_to_target_timespec(ts_addr, &ts)) {
  1306. return -TARGET_EFAULT;
  1307. }
  1308. }
  1309. }
  1310. return ret;
  1311. }
  1312. #endif
  1313. #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) || \
  1314. defined(TARGET_NR_ppoll_time64)
  1315. static abi_long do_ppoll(abi_long arg1, abi_long arg2, abi_long arg3,
  1316. abi_long arg4, abi_long arg5, bool ppoll, bool time64)
  1317. {
  1318. struct target_pollfd *target_pfd;
  1319. unsigned int nfds = arg2;
  1320. struct pollfd *pfd;
  1321. unsigned int i;
  1322. abi_long ret;
  1323. pfd = NULL;
  1324. target_pfd = NULL;
  1325. if (nfds) {
  1326. if (nfds > (INT_MAX / sizeof(struct target_pollfd))) {
  1327. return -TARGET_EINVAL;
  1328. }
  1329. target_pfd = lock_user(VERIFY_WRITE, arg1,
  1330. sizeof(struct target_pollfd) * nfds, 1);
  1331. if (!target_pfd) {
  1332. return -TARGET_EFAULT;
  1333. }
  1334. pfd = alloca(sizeof(struct pollfd) * nfds);
  1335. for (i = 0; i < nfds; i++) {
  1336. pfd[i].fd = tswap32(target_pfd[i].fd);
  1337. pfd[i].events = tswap16(target_pfd[i].events);
  1338. }
  1339. }
  1340. if (ppoll) {
  1341. struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
  1342. sigset_t *set = NULL;
  1343. if (arg3) {
  1344. if (time64) {
  1345. if (target_to_host_timespec64(timeout_ts, arg3)) {
  1346. unlock_user(target_pfd, arg1, 0);
  1347. return -TARGET_EFAULT;
  1348. }
  1349. } else {
  1350. if (target_to_host_timespec(timeout_ts, arg3)) {
  1351. unlock_user(target_pfd, arg1, 0);
  1352. return -TARGET_EFAULT;
  1353. }
  1354. }
  1355. } else {
  1356. timeout_ts = NULL;
  1357. }
  1358. if (arg4) {
  1359. ret = process_sigsuspend_mask(&set, arg4, arg5);
  1360. if (ret != 0) {
  1361. unlock_user(target_pfd, arg1, 0);
  1362. return ret;
  1363. }
  1364. }
  1365. ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts,
  1366. set, SIGSET_T_SIZE));
  1367. if (set) {
  1368. finish_sigsuspend_mask(ret);
  1369. }
  1370. if (!is_error(ret) && arg3) {
  1371. if (time64) {
  1372. if (host_to_target_timespec64(arg3, timeout_ts)) {
  1373. return -TARGET_EFAULT;
  1374. }
  1375. } else {
  1376. if (host_to_target_timespec(arg3, timeout_ts)) {
  1377. return -TARGET_EFAULT;
  1378. }
  1379. }
  1380. }
  1381. } else {
  1382. struct timespec ts, *pts;
  1383. if (arg3 >= 0) {
  1384. /* Convert ms to secs, ns */
  1385. ts.tv_sec = arg3 / 1000;
  1386. ts.tv_nsec = (arg3 % 1000) * 1000000LL;
  1387. pts = &ts;
  1388. } else {
  1389. /* -ve poll() timeout means "infinite" */
  1390. pts = NULL;
  1391. }
  1392. ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0));
  1393. }
  1394. if (!is_error(ret)) {
  1395. for (i = 0; i < nfds; i++) {
  1396. target_pfd[i].revents = tswap16(pfd[i].revents);
  1397. }
  1398. }
  1399. unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
  1400. return ret;
  1401. }
  1402. #endif
  1403. static abi_long do_pipe(CPUArchState *cpu_env, abi_ulong pipedes,
  1404. int flags, int is_pipe2)
  1405. {
  1406. int host_pipe[2];
  1407. abi_long ret;
  1408. ret = pipe2(host_pipe, flags);
  1409. if (is_error(ret))
  1410. return get_errno(ret);
  1411. /* Several targets have special calling conventions for the original
  1412. pipe syscall, but didn't replicate this into the pipe2 syscall. */
  1413. if (!is_pipe2) {
  1414. #if defined(TARGET_ALPHA)
  1415. cpu_env->ir[IR_A4] = host_pipe[1];
  1416. return host_pipe[0];
  1417. #elif defined(TARGET_MIPS)
  1418. cpu_env->active_tc.gpr[3] = host_pipe[1];
  1419. return host_pipe[0];
  1420. #elif defined(TARGET_SH4)
  1421. cpu_env->gregs[1] = host_pipe[1];
  1422. return host_pipe[0];
  1423. #elif defined(TARGET_SPARC)
  1424. cpu_env->regwptr[1] = host_pipe[1];
  1425. return host_pipe[0];
  1426. #endif
  1427. }
  1428. if (put_user_s32(host_pipe[0], pipedes)
  1429. || put_user_s32(host_pipe[1], pipedes + sizeof(abi_int)))
  1430. return -TARGET_EFAULT;
  1431. return get_errno(ret);
  1432. }
  1433. static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
  1434. abi_ulong target_addr,
  1435. socklen_t len)
  1436. {
  1437. struct target_ip_mreqn *target_smreqn;
  1438. target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
  1439. if (!target_smreqn)
  1440. return -TARGET_EFAULT;
  1441. mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
  1442. mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
  1443. if (len == sizeof(struct target_ip_mreqn))
  1444. mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
  1445. unlock_user(target_smreqn, target_addr, 0);
  1446. return 0;
  1447. }
  1448. static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr,
  1449. abi_ulong target_addr,
  1450. socklen_t len)
  1451. {
  1452. const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
  1453. sa_family_t sa_family;
  1454. struct target_sockaddr *target_saddr;
  1455. if (fd_trans_target_to_host_addr(fd)) {
  1456. return fd_trans_target_to_host_addr(fd)(addr, target_addr, len);
  1457. }
  1458. target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
  1459. if (!target_saddr)
  1460. return -TARGET_EFAULT;
  1461. sa_family = tswap16(target_saddr->sa_family);
  1462. /* Oops. The caller might send a incomplete sun_path; sun_path
  1463. * must be terminated by \0 (see the manual page), but
  1464. * unfortunately it is quite common to specify sockaddr_un
  1465. * length as "strlen(x->sun_path)" while it should be
  1466. * "strlen(...) + 1". We'll fix that here if needed.
  1467. * Linux kernel has a similar feature.
  1468. */
  1469. if (sa_family == AF_UNIX) {
  1470. if (len < unix_maxlen && len > 0) {
  1471. char *cp = (char*)target_saddr;
  1472. if ( cp[len-1] && !cp[len] )
  1473. len++;
  1474. }
  1475. if (len > unix_maxlen)
  1476. len = unix_maxlen;
  1477. }
  1478. memcpy(addr, target_saddr, len);
  1479. addr->sa_family = sa_family;
  1480. if (sa_family == AF_NETLINK) {
  1481. struct sockaddr_nl *nladdr;
  1482. nladdr = (struct sockaddr_nl *)addr;
  1483. nladdr->nl_pid = tswap32(nladdr->nl_pid);
  1484. nladdr->nl_groups = tswap32(nladdr->nl_groups);
  1485. } else if (sa_family == AF_PACKET) {
  1486. struct target_sockaddr_ll *lladdr;
  1487. lladdr = (struct target_sockaddr_ll *)addr;
  1488. lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex);
  1489. lladdr->sll_hatype = tswap16(lladdr->sll_hatype);
  1490. } else if (sa_family == AF_INET6) {
  1491. struct sockaddr_in6 *in6addr;
  1492. in6addr = (struct sockaddr_in6 *)addr;
  1493. in6addr->sin6_scope_id = tswap32(in6addr->sin6_scope_id);
  1494. }
  1495. unlock_user(target_saddr, target_addr, 0);
  1496. return 0;
  1497. }
  1498. static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
  1499. struct sockaddr *addr,
  1500. socklen_t len)
  1501. {
  1502. struct target_sockaddr *target_saddr;
  1503. if (len == 0) {
  1504. return 0;
  1505. }
  1506. assert(addr);
  1507. target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
  1508. if (!target_saddr)
  1509. return -TARGET_EFAULT;
  1510. memcpy(target_saddr, addr, len);
  1511. if (len >= offsetof(struct target_sockaddr, sa_family) +
  1512. sizeof(target_saddr->sa_family)) {
  1513. target_saddr->sa_family = tswap16(addr->sa_family);
  1514. }
  1515. if (addr->sa_family == AF_NETLINK &&
  1516. len >= sizeof(struct target_sockaddr_nl)) {
  1517. struct target_sockaddr_nl *target_nl =
  1518. (struct target_sockaddr_nl *)target_saddr;
  1519. target_nl->nl_pid = tswap32(target_nl->nl_pid);
  1520. target_nl->nl_groups = tswap32(target_nl->nl_groups);
  1521. } else if (addr->sa_family == AF_PACKET) {
  1522. struct sockaddr_ll *target_ll = (struct sockaddr_ll *)target_saddr;
  1523. target_ll->sll_ifindex = tswap32(target_ll->sll_ifindex);
  1524. target_ll->sll_hatype = tswap16(target_ll->sll_hatype);
  1525. } else if (addr->sa_family == AF_INET6 &&
  1526. len >= sizeof(struct target_sockaddr_in6)) {
  1527. struct target_sockaddr_in6 *target_in6 =
  1528. (struct target_sockaddr_in6 *)target_saddr;
  1529. target_in6->sin6_scope_id = tswap16(target_in6->sin6_scope_id);
  1530. }
  1531. unlock_user(target_saddr, target_addr, len);
  1532. return 0;
  1533. }
  1534. static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
  1535. struct target_msghdr *target_msgh)
  1536. {
  1537. struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
  1538. abi_long msg_controllen;
  1539. abi_ulong target_cmsg_addr;
  1540. struct target_cmsghdr *target_cmsg, *target_cmsg_start;
  1541. socklen_t space = 0;
  1542. msg_controllen = tswapal(target_msgh->msg_controllen);
  1543. if (msg_controllen < sizeof (struct target_cmsghdr))
  1544. goto the_end;
  1545. target_cmsg_addr = tswapal(target_msgh->msg_control);
  1546. target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
  1547. target_cmsg_start = target_cmsg;
  1548. if (!target_cmsg)
  1549. return -TARGET_EFAULT;
  1550. while (cmsg && target_cmsg) {
  1551. void *data = CMSG_DATA(cmsg);
  1552. void *target_data = TARGET_CMSG_DATA(target_cmsg);
  1553. int len = tswapal(target_cmsg->cmsg_len)
  1554. - sizeof(struct target_cmsghdr);
  1555. space += CMSG_SPACE(len);
  1556. if (space > msgh->msg_controllen) {
  1557. space -= CMSG_SPACE(len);
  1558. /* This is a QEMU bug, since we allocated the payload
  1559. * area ourselves (unlike overflow in host-to-target
  1560. * conversion, which is just the guest giving us a buffer
  1561. * that's too small). It can't happen for the payload types
  1562. * we currently support; if it becomes an issue in future
  1563. * we would need to improve our allocation strategy to
  1564. * something more intelligent than "twice the size of the
  1565. * target buffer we're reading from".
  1566. */
  1567. qemu_log_mask(LOG_UNIMP,
  1568. ("Unsupported ancillary data %d/%d: "
  1569. "unhandled msg size\n"),
  1570. tswap32(target_cmsg->cmsg_level),
  1571. tswap32(target_cmsg->cmsg_type));
  1572. break;
  1573. }
  1574. if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
  1575. cmsg->cmsg_level = SOL_SOCKET;
  1576. } else {
  1577. cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
  1578. }
  1579. cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
  1580. cmsg->cmsg_len = CMSG_LEN(len);
  1581. if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
  1582. int *fd = (int *)data;
  1583. int *target_fd = (int *)target_data;
  1584. int i, numfds = len / sizeof(int);
  1585. for (i = 0; i < numfds; i++) {
  1586. __get_user(fd[i], target_fd + i);
  1587. }
  1588. } else if (cmsg->cmsg_level == SOL_SOCKET
  1589. && cmsg->cmsg_type == SCM_CREDENTIALS) {
  1590. struct ucred *cred = (struct ucred *)data;
  1591. struct target_ucred *target_cred =
  1592. (struct target_ucred *)target_data;
  1593. __get_user(cred->pid, &target_cred->pid);
  1594. __get_user(cred->uid, &target_cred->uid);
  1595. __get_user(cred->gid, &target_cred->gid);
  1596. } else if (cmsg->cmsg_level == SOL_ALG) {
  1597. uint32_t *dst = (uint32_t *)data;
  1598. memcpy(dst, target_data, len);
  1599. /* fix endianness of first 32-bit word */
  1600. if (len >= sizeof(uint32_t)) {
  1601. *dst = tswap32(*dst);
  1602. }
  1603. } else {
  1604. qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n",
  1605. cmsg->cmsg_level, cmsg->cmsg_type);
  1606. memcpy(data, target_data, len);
  1607. }
  1608. cmsg = CMSG_NXTHDR(msgh, cmsg);
  1609. target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
  1610. target_cmsg_start);
  1611. }
  1612. unlock_user(target_cmsg, target_cmsg_addr, 0);
  1613. the_end:
  1614. msgh->msg_controllen = space;
  1615. return 0;
  1616. }
  1617. static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
  1618. struct msghdr *msgh)
  1619. {
  1620. struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
  1621. abi_long msg_controllen;
  1622. abi_ulong target_cmsg_addr;
  1623. struct target_cmsghdr *target_cmsg, *target_cmsg_start;
  1624. socklen_t space = 0;
  1625. msg_controllen = tswapal(target_msgh->msg_controllen);
  1626. if (msg_controllen < sizeof (struct target_cmsghdr))
  1627. goto the_end;
  1628. target_cmsg_addr = tswapal(target_msgh->msg_control);
  1629. target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
  1630. target_cmsg_start = target_cmsg;
  1631. if (!target_cmsg)
  1632. return -TARGET_EFAULT;
  1633. while (cmsg && target_cmsg) {
  1634. void *data = CMSG_DATA(cmsg);
  1635. void *target_data = TARGET_CMSG_DATA(target_cmsg);
  1636. int len = cmsg->cmsg_len - sizeof(struct cmsghdr);
  1637. int tgt_len, tgt_space;
  1638. /* We never copy a half-header but may copy half-data;
  1639. * this is Linux's behaviour in put_cmsg(). Note that
  1640. * truncation here is a guest problem (which we report
  1641. * to the guest via the CTRUNC bit), unlike truncation
  1642. * in target_to_host_cmsg, which is a QEMU bug.
  1643. */
  1644. if (msg_controllen < sizeof(struct target_cmsghdr)) {
  1645. target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
  1646. break;
  1647. }
  1648. if (cmsg->cmsg_level == SOL_SOCKET) {
  1649. target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
  1650. } else {
  1651. target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
  1652. }
  1653. target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
  1654. /* Payload types which need a different size of payload on
  1655. * the target must adjust tgt_len here.
  1656. */
  1657. tgt_len = len;
  1658. switch (cmsg->cmsg_level) {
  1659. case SOL_SOCKET:
  1660. switch (cmsg->cmsg_type) {
  1661. case SO_TIMESTAMP:
  1662. tgt_len = sizeof(struct target_timeval);
  1663. break;
  1664. default:
  1665. break;
  1666. }
  1667. break;
  1668. default:
  1669. break;
  1670. }
  1671. if (msg_controllen < TARGET_CMSG_LEN(tgt_len)) {
  1672. target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
  1673. tgt_len = msg_controllen - sizeof(struct target_cmsghdr);
  1674. }
  1675. /* We must now copy-and-convert len bytes of payload
  1676. * into tgt_len bytes of destination space. Bear in mind
  1677. * that in both source and destination we may be dealing
  1678. * with a truncated value!
  1679. */
  1680. switch (cmsg->cmsg_level) {
  1681. case SOL_SOCKET:
  1682. switch (cmsg->cmsg_type) {
  1683. case SCM_RIGHTS:
  1684. {
  1685. int *fd = (int *)data;
  1686. int *target_fd = (int *)target_data;
  1687. int i, numfds = tgt_len / sizeof(int);
  1688. for (i = 0; i < numfds; i++) {
  1689. __put_user(fd[i], target_fd + i);
  1690. }
  1691. break;
  1692. }
  1693. case SO_TIMESTAMP:
  1694. {
  1695. struct timeval *tv = (struct timeval *)data;
  1696. struct target_timeval *target_tv =
  1697. (struct target_timeval *)target_data;
  1698. if (len != sizeof(struct timeval) ||
  1699. tgt_len != sizeof(struct target_timeval)) {
  1700. goto unimplemented;
  1701. }
  1702. /* copy struct timeval to target */
  1703. __put_user(tv->tv_sec, &target_tv->tv_sec);
  1704. __put_user(tv->tv_usec, &target_tv->tv_usec);
  1705. break;
  1706. }
  1707. case SCM_CREDENTIALS:
  1708. {
  1709. struct ucred *cred = (struct ucred *)data;
  1710. struct target_ucred *target_cred =
  1711. (struct target_ucred *)target_data;
  1712. __put_user(cred->pid, &target_cred->pid);
  1713. __put_user(cred->uid, &target_cred->uid);
  1714. __put_user(cred->gid, &target_cred->gid);
  1715. break;
  1716. }
  1717. default:
  1718. goto unimplemented;
  1719. }
  1720. break;
  1721. case SOL_IP:
  1722. switch (cmsg->cmsg_type) {
  1723. case IP_TTL:
  1724. {
  1725. uint32_t *v = (uint32_t *)data;
  1726. uint32_t *t_int = (uint32_t *)target_data;
  1727. if (len != sizeof(uint32_t) ||
  1728. tgt_len != sizeof(uint32_t)) {
  1729. goto unimplemented;
  1730. }
  1731. __put_user(*v, t_int);
  1732. break;
  1733. }
  1734. case IP_RECVERR:
  1735. {
  1736. struct errhdr_t {
  1737. struct sock_extended_err ee;
  1738. struct sockaddr_in offender;
  1739. };
  1740. struct errhdr_t *errh = (struct errhdr_t *)data;
  1741. struct errhdr_t *target_errh =
  1742. (struct errhdr_t *)target_data;
  1743. if (len != sizeof(struct errhdr_t) ||
  1744. tgt_len != sizeof(struct errhdr_t)) {
  1745. goto unimplemented;
  1746. }
  1747. __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
  1748. __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
  1749. __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
  1750. __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
  1751. __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
  1752. __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
  1753. __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
  1754. host_to_target_sockaddr((unsigned long) &target_errh->offender,
  1755. (void *) &errh->offender, sizeof(errh->offender));
  1756. break;
  1757. }
  1758. default:
  1759. goto unimplemented;
  1760. }
  1761. break;
  1762. case SOL_IPV6:
  1763. switch (cmsg->cmsg_type) {
  1764. case IPV6_HOPLIMIT:
  1765. {
  1766. uint32_t *v = (uint32_t *)data;
  1767. uint32_t *t_int = (uint32_t *)target_data;
  1768. if (len != sizeof(uint32_t) ||
  1769. tgt_len != sizeof(uint32_t)) {
  1770. goto unimplemented;
  1771. }
  1772. __put_user(*v, t_int);
  1773. break;
  1774. }
  1775. case IPV6_RECVERR:
  1776. {
  1777. struct errhdr6_t {
  1778. struct sock_extended_err ee;
  1779. struct sockaddr_in6 offender;
  1780. };
  1781. struct errhdr6_t *errh = (struct errhdr6_t *)data;
  1782. struct errhdr6_t *target_errh =
  1783. (struct errhdr6_t *)target_data;
  1784. if (len != sizeof(struct errhdr6_t) ||
  1785. tgt_len != sizeof(struct errhdr6_t)) {
  1786. goto unimplemented;
  1787. }
  1788. __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
  1789. __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
  1790. __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
  1791. __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
  1792. __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
  1793. __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
  1794. __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
  1795. host_to_target_sockaddr((unsigned long) &target_errh->offender,
  1796. (void *) &errh->offender, sizeof(errh->offender));
  1797. break;
  1798. }
  1799. default:
  1800. goto unimplemented;
  1801. }
  1802. break;
  1803. default:
  1804. unimplemented:
  1805. qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n",
  1806. cmsg->cmsg_level, cmsg->cmsg_type);
  1807. memcpy(target_data, data, MIN(len, tgt_len));
  1808. if (tgt_len > len) {
  1809. memset(target_data + len, 0, tgt_len - len);
  1810. }
  1811. }
  1812. target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(tgt_len));
  1813. tgt_space = TARGET_CMSG_SPACE(tgt_len);
  1814. if (msg_controllen < tgt_space) {
  1815. tgt_space = msg_controllen;
  1816. }
  1817. msg_controllen -= tgt_space;
  1818. space += tgt_space;
  1819. cmsg = CMSG_NXTHDR(msgh, cmsg);
  1820. target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
  1821. target_cmsg_start);
  1822. }
  1823. unlock_user(target_cmsg, target_cmsg_addr, space);
  1824. the_end:
  1825. target_msgh->msg_controllen = tswapal(space);
  1826. return 0;
  1827. }
  1828. /* do_setsockopt() Must return target values and target errnos. */
  1829. static abi_long do_setsockopt(int sockfd, int level, int optname,
  1830. abi_ulong optval_addr, socklen_t optlen)
  1831. {
  1832. abi_long ret;
  1833. int val;
  1834. struct ip_mreqn *ip_mreq;
  1835. struct ip_mreq_source *ip_mreq_source;
  1836. switch(level) {
  1837. case SOL_TCP:
  1838. case SOL_UDP:
  1839. /* TCP and UDP options all take an 'int' value. */
  1840. if (optlen < sizeof(uint32_t))
  1841. return -TARGET_EINVAL;
  1842. if (get_user_u32(val, optval_addr))
  1843. return -TARGET_EFAULT;
  1844. ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
  1845. break;
  1846. case SOL_IP:
  1847. switch(optname) {
  1848. case IP_TOS:
  1849. case IP_TTL:
  1850. case IP_HDRINCL:
  1851. case IP_ROUTER_ALERT:
  1852. case IP_RECVOPTS:
  1853. case IP_RETOPTS:
  1854. case IP_PKTINFO:
  1855. case IP_MTU_DISCOVER:
  1856. case IP_RECVERR:
  1857. case IP_RECVTTL:
  1858. case IP_RECVTOS:
  1859. #ifdef IP_FREEBIND
  1860. case IP_FREEBIND:
  1861. #endif
  1862. case IP_MULTICAST_TTL:
  1863. case IP_MULTICAST_LOOP:
  1864. val = 0;
  1865. if (optlen >= sizeof(uint32_t)) {
  1866. if (get_user_u32(val, optval_addr))
  1867. return -TARGET_EFAULT;
  1868. } else if (optlen >= 1) {
  1869. if (get_user_u8(val, optval_addr))
  1870. return -TARGET_EFAULT;
  1871. }
  1872. ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
  1873. break;
  1874. case IP_ADD_MEMBERSHIP:
  1875. case IP_DROP_MEMBERSHIP:
  1876. if (optlen < sizeof (struct target_ip_mreq) ||
  1877. optlen > sizeof (struct target_ip_mreqn))
  1878. return -TARGET_EINVAL;
  1879. ip_mreq = (struct ip_mreqn *) alloca(optlen);
  1880. target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
  1881. ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
  1882. break;
  1883. case IP_BLOCK_SOURCE:
  1884. case IP_UNBLOCK_SOURCE:
  1885. case IP_ADD_SOURCE_MEMBERSHIP:
  1886. case IP_DROP_SOURCE_MEMBERSHIP:
  1887. if (optlen != sizeof (struct target_ip_mreq_source))
  1888. return -TARGET_EINVAL;
  1889. ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
  1890. if (!ip_mreq_source) {
  1891. return -TARGET_EFAULT;
  1892. }
  1893. ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
  1894. unlock_user (ip_mreq_source, optval_addr, 0);
  1895. break;
  1896. default:
  1897. goto unimplemented;
  1898. }
  1899. break;
  1900. case SOL_IPV6:
  1901. switch (optname) {
  1902. case IPV6_MTU_DISCOVER:
  1903. case IPV6_MTU:
  1904. case IPV6_V6ONLY:
  1905. case IPV6_RECVPKTINFO:
  1906. case IPV6_UNICAST_HOPS:
  1907. case IPV6_MULTICAST_HOPS:
  1908. case IPV6_MULTICAST_LOOP:
  1909. case IPV6_RECVERR:
  1910. case IPV6_RECVHOPLIMIT:
  1911. case IPV6_2292HOPLIMIT:
  1912. case IPV6_CHECKSUM:
  1913. case IPV6_ADDRFORM:
  1914. case IPV6_2292PKTINFO:
  1915. case IPV6_RECVTCLASS:
  1916. case IPV6_RECVRTHDR:
  1917. case IPV6_2292RTHDR:
  1918. case IPV6_RECVHOPOPTS:
  1919. case IPV6_2292HOPOPTS:
  1920. case IPV6_RECVDSTOPTS:
  1921. case IPV6_2292DSTOPTS:
  1922. case IPV6_TCLASS:
  1923. case IPV6_ADDR_PREFERENCES:
  1924. #ifdef IPV6_RECVPATHMTU
  1925. case IPV6_RECVPATHMTU:
  1926. #endif
  1927. #ifdef IPV6_TRANSPARENT
  1928. case IPV6_TRANSPARENT:
  1929. #endif
  1930. #ifdef IPV6_FREEBIND
  1931. case IPV6_FREEBIND:
  1932. #endif
  1933. #ifdef IPV6_RECVORIGDSTADDR
  1934. case IPV6_RECVORIGDSTADDR:
  1935. #endif
  1936. val = 0;
  1937. if (optlen < sizeof(uint32_t)) {
  1938. return -TARGET_EINVAL;
  1939. }
  1940. if (get_user_u32(val, optval_addr)) {
  1941. return -TARGET_EFAULT;
  1942. }
  1943. ret = get_errno(setsockopt(sockfd, level, optname,
  1944. &val, sizeof(val)));
  1945. break;
  1946. case IPV6_PKTINFO:
  1947. {
  1948. struct in6_pktinfo pki;
  1949. if (optlen < sizeof(pki)) {
  1950. return -TARGET_EINVAL;
  1951. }
  1952. if (copy_from_user(&pki, optval_addr, sizeof(pki))) {
  1953. return -TARGET_EFAULT;
  1954. }
  1955. pki.ipi6_ifindex = tswap32(pki.ipi6_ifindex);
  1956. ret = get_errno(setsockopt(sockfd, level, optname,
  1957. &pki, sizeof(pki)));
  1958. break;
  1959. }
  1960. case IPV6_ADD_MEMBERSHIP:
  1961. case IPV6_DROP_MEMBERSHIP:
  1962. {
  1963. struct ipv6_mreq ipv6mreq;
  1964. if (optlen < sizeof(ipv6mreq)) {
  1965. return -TARGET_EINVAL;
  1966. }
  1967. if (copy_from_user(&ipv6mreq, optval_addr, sizeof(ipv6mreq))) {
  1968. return -TARGET_EFAULT;
  1969. }
  1970. ipv6mreq.ipv6mr_interface = tswap32(ipv6mreq.ipv6mr_interface);
  1971. ret = get_errno(setsockopt(sockfd, level, optname,
  1972. &ipv6mreq, sizeof(ipv6mreq)));
  1973. break;
  1974. }
  1975. default:
  1976. goto unimplemented;
  1977. }
  1978. break;
  1979. case SOL_ICMPV6:
  1980. switch (optname) {
  1981. case ICMPV6_FILTER:
  1982. {
  1983. struct icmp6_filter icmp6f;
  1984. if (optlen > sizeof(icmp6f)) {
  1985. optlen = sizeof(icmp6f);
  1986. }
  1987. if (copy_from_user(&icmp6f, optval_addr, optlen)) {
  1988. return -TARGET_EFAULT;
  1989. }
  1990. for (val = 0; val < 8; val++) {
  1991. icmp6f.data[val] = tswap32(icmp6f.data[val]);
  1992. }
  1993. ret = get_errno(setsockopt(sockfd, level, optname,
  1994. &icmp6f, optlen));
  1995. break;
  1996. }
  1997. default:
  1998. goto unimplemented;
  1999. }
  2000. break;
  2001. case SOL_RAW:
  2002. switch (optname) {
  2003. case ICMP_FILTER:
  2004. case IPV6_CHECKSUM:
  2005. /* those take an u32 value */
  2006. if (optlen < sizeof(uint32_t)) {
  2007. return -TARGET_EINVAL;
  2008. }
  2009. if (get_user_u32(val, optval_addr)) {
  2010. return -TARGET_EFAULT;
  2011. }
  2012. ret = get_errno(setsockopt(sockfd, level, optname,
  2013. &val, sizeof(val)));
  2014. break;
  2015. default:
  2016. goto unimplemented;
  2017. }
  2018. break;
  2019. #if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE)
  2020. case SOL_ALG:
  2021. switch (optname) {
  2022. case ALG_SET_KEY:
  2023. {
  2024. char *alg_key = g_malloc(optlen);
  2025. if (!alg_key) {
  2026. return -TARGET_ENOMEM;
  2027. }
  2028. if (copy_from_user(alg_key, optval_addr, optlen)) {
  2029. g_free(alg_key);
  2030. return -TARGET_EFAULT;
  2031. }
  2032. ret = get_errno(setsockopt(sockfd, level, optname,
  2033. alg_key, optlen));
  2034. g_free(alg_key);
  2035. break;
  2036. }
  2037. case ALG_SET_AEAD_AUTHSIZE:
  2038. {
  2039. ret = get_errno(setsockopt(sockfd, level, optname,
  2040. NULL, optlen));
  2041. break;
  2042. }
  2043. default:
  2044. goto unimplemented;
  2045. }
  2046. break;
  2047. #endif
  2048. case TARGET_SOL_SOCKET:
  2049. switch (optname) {
  2050. case TARGET_SO_RCVTIMEO:
  2051. {
  2052. struct timeval tv;
  2053. optname = SO_RCVTIMEO;
  2054. set_timeout:
  2055. if (optlen != sizeof(struct target_timeval)) {
  2056. return -TARGET_EINVAL;
  2057. }
  2058. if (copy_from_user_timeval(&tv, optval_addr)) {
  2059. return -TARGET_EFAULT;
  2060. }
  2061. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
  2062. &tv, sizeof(tv)));
  2063. return ret;
  2064. }
  2065. case TARGET_SO_SNDTIMEO:
  2066. optname = SO_SNDTIMEO;
  2067. goto set_timeout;
  2068. case TARGET_SO_ATTACH_FILTER:
  2069. {
  2070. struct target_sock_fprog *tfprog;
  2071. struct target_sock_filter *tfilter;
  2072. struct sock_fprog fprog;
  2073. struct sock_filter *filter;
  2074. int i;
  2075. if (optlen != sizeof(*tfprog)) {
  2076. return -TARGET_EINVAL;
  2077. }
  2078. if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
  2079. return -TARGET_EFAULT;
  2080. }
  2081. if (!lock_user_struct(VERIFY_READ, tfilter,
  2082. tswapal(tfprog->filter), 0)) {
  2083. unlock_user_struct(tfprog, optval_addr, 1);
  2084. return -TARGET_EFAULT;
  2085. }
  2086. fprog.len = tswap16(tfprog->len);
  2087. filter = g_try_new(struct sock_filter, fprog.len);
  2088. if (filter == NULL) {
  2089. unlock_user_struct(tfilter, tfprog->filter, 1);
  2090. unlock_user_struct(tfprog, optval_addr, 1);
  2091. return -TARGET_ENOMEM;
  2092. }
  2093. for (i = 0; i < fprog.len; i++) {
  2094. filter[i].code = tswap16(tfilter[i].code);
  2095. filter[i].jt = tfilter[i].jt;
  2096. filter[i].jf = tfilter[i].jf;
  2097. filter[i].k = tswap32(tfilter[i].k);
  2098. }
  2099. fprog.filter = filter;
  2100. ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
  2101. SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
  2102. g_free(filter);
  2103. unlock_user_struct(tfilter, tfprog->filter, 1);
  2104. unlock_user_struct(tfprog, optval_addr, 1);
  2105. return ret;
  2106. }
  2107. case TARGET_SO_BINDTODEVICE:
  2108. {
  2109. char *dev_ifname, *addr_ifname;
  2110. if (optlen > IFNAMSIZ - 1) {
  2111. optlen = IFNAMSIZ - 1;
  2112. }
  2113. dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1);
  2114. if (!dev_ifname) {
  2115. return -TARGET_EFAULT;
  2116. }
  2117. optname = SO_BINDTODEVICE;
  2118. addr_ifname = alloca(IFNAMSIZ);
  2119. memcpy(addr_ifname, dev_ifname, optlen);
  2120. addr_ifname[optlen] = 0;
  2121. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
  2122. addr_ifname, optlen));
  2123. unlock_user (dev_ifname, optval_addr, 0);
  2124. return ret;
  2125. }
  2126. case TARGET_SO_LINGER:
  2127. {
  2128. struct linger lg;
  2129. struct target_linger *tlg;
  2130. if (optlen != sizeof(struct target_linger)) {
  2131. return -TARGET_EINVAL;
  2132. }
  2133. if (!lock_user_struct(VERIFY_READ, tlg, optval_addr, 1)) {
  2134. return -TARGET_EFAULT;
  2135. }
  2136. __get_user(lg.l_onoff, &tlg->l_onoff);
  2137. __get_user(lg.l_linger, &tlg->l_linger);
  2138. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, SO_LINGER,
  2139. &lg, sizeof(lg)));
  2140. unlock_user_struct(tlg, optval_addr, 0);
  2141. return ret;
  2142. }
  2143. /* Options with 'int' argument. */
  2144. case TARGET_SO_DEBUG:
  2145. optname = SO_DEBUG;
  2146. break;
  2147. case TARGET_SO_REUSEADDR:
  2148. optname = SO_REUSEADDR;
  2149. break;
  2150. #ifdef SO_REUSEPORT
  2151. case TARGET_SO_REUSEPORT:
  2152. optname = SO_REUSEPORT;
  2153. break;
  2154. #endif
  2155. case TARGET_SO_TYPE:
  2156. optname = SO_TYPE;
  2157. break;
  2158. case TARGET_SO_ERROR:
  2159. optname = SO_ERROR;
  2160. break;
  2161. case TARGET_SO_DONTROUTE:
  2162. optname = SO_DONTROUTE;
  2163. break;
  2164. case TARGET_SO_BROADCAST:
  2165. optname = SO_BROADCAST;
  2166. break;
  2167. case TARGET_SO_SNDBUF:
  2168. optname = SO_SNDBUF;
  2169. break;
  2170. case TARGET_SO_SNDBUFFORCE:
  2171. optname = SO_SNDBUFFORCE;
  2172. break;
  2173. case TARGET_SO_RCVBUF:
  2174. optname = SO_RCVBUF;
  2175. break;
  2176. case TARGET_SO_RCVBUFFORCE:
  2177. optname = SO_RCVBUFFORCE;
  2178. break;
  2179. case TARGET_SO_KEEPALIVE:
  2180. optname = SO_KEEPALIVE;
  2181. break;
  2182. case TARGET_SO_OOBINLINE:
  2183. optname = SO_OOBINLINE;
  2184. break;
  2185. case TARGET_SO_NO_CHECK:
  2186. optname = SO_NO_CHECK;
  2187. break;
  2188. case TARGET_SO_PRIORITY:
  2189. optname = SO_PRIORITY;
  2190. break;
  2191. #ifdef SO_BSDCOMPAT
  2192. case TARGET_SO_BSDCOMPAT:
  2193. optname = SO_BSDCOMPAT;
  2194. break;
  2195. #endif
  2196. case TARGET_SO_PASSCRED:
  2197. optname = SO_PASSCRED;
  2198. break;
  2199. case TARGET_SO_PASSSEC:
  2200. optname = SO_PASSSEC;
  2201. break;
  2202. case TARGET_SO_TIMESTAMP:
  2203. optname = SO_TIMESTAMP;
  2204. break;
  2205. case TARGET_SO_RCVLOWAT:
  2206. optname = SO_RCVLOWAT;
  2207. break;
  2208. default:
  2209. goto unimplemented;
  2210. }
  2211. if (optlen < sizeof(uint32_t))
  2212. return -TARGET_EINVAL;
  2213. if (get_user_u32(val, optval_addr))
  2214. return -TARGET_EFAULT;
  2215. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
  2216. break;
  2217. #ifdef SOL_NETLINK
  2218. case SOL_NETLINK:
  2219. switch (optname) {
  2220. case NETLINK_PKTINFO:
  2221. case NETLINK_ADD_MEMBERSHIP:
  2222. case NETLINK_DROP_MEMBERSHIP:
  2223. case NETLINK_BROADCAST_ERROR:
  2224. case NETLINK_NO_ENOBUFS:
  2225. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
  2226. case NETLINK_LISTEN_ALL_NSID:
  2227. case NETLINK_CAP_ACK:
  2228. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
  2229. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
  2230. case NETLINK_EXT_ACK:
  2231. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2232. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
  2233. case NETLINK_GET_STRICT_CHK:
  2234. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2235. break;
  2236. default:
  2237. goto unimplemented;
  2238. }
  2239. val = 0;
  2240. if (optlen < sizeof(uint32_t)) {
  2241. return -TARGET_EINVAL;
  2242. }
  2243. if (get_user_u32(val, optval_addr)) {
  2244. return -TARGET_EFAULT;
  2245. }
  2246. ret = get_errno(setsockopt(sockfd, SOL_NETLINK, optname, &val,
  2247. sizeof(val)));
  2248. break;
  2249. #endif /* SOL_NETLINK */
  2250. default:
  2251. unimplemented:
  2252. qemu_log_mask(LOG_UNIMP, "Unsupported setsockopt level=%d optname=%d\n",
  2253. level, optname);
  2254. ret = -TARGET_ENOPROTOOPT;
  2255. }
  2256. return ret;
  2257. }
  2258. /* do_getsockopt() Must return target values and target errnos. */
  2259. static abi_long do_getsockopt(int sockfd, int level, int optname,
  2260. abi_ulong optval_addr, abi_ulong optlen)
  2261. {
  2262. abi_long ret;
  2263. int len, val;
  2264. socklen_t lv;
  2265. switch(level) {
  2266. case TARGET_SOL_SOCKET:
  2267. level = SOL_SOCKET;
  2268. switch (optname) {
  2269. /* These don't just return a single integer */
  2270. case TARGET_SO_PEERNAME:
  2271. goto unimplemented;
  2272. case TARGET_SO_RCVTIMEO: {
  2273. struct timeval tv;
  2274. socklen_t tvlen;
  2275. optname = SO_RCVTIMEO;
  2276. get_timeout:
  2277. if (get_user_u32(len, optlen)) {
  2278. return -TARGET_EFAULT;
  2279. }
  2280. if (len < 0) {
  2281. return -TARGET_EINVAL;
  2282. }
  2283. tvlen = sizeof(tv);
  2284. ret = get_errno(getsockopt(sockfd, level, optname,
  2285. &tv, &tvlen));
  2286. if (ret < 0) {
  2287. return ret;
  2288. }
  2289. if (len > sizeof(struct target_timeval)) {
  2290. len = sizeof(struct target_timeval);
  2291. }
  2292. if (copy_to_user_timeval(optval_addr, &tv)) {
  2293. return -TARGET_EFAULT;
  2294. }
  2295. if (put_user_u32(len, optlen)) {
  2296. return -TARGET_EFAULT;
  2297. }
  2298. break;
  2299. }
  2300. case TARGET_SO_SNDTIMEO:
  2301. optname = SO_SNDTIMEO;
  2302. goto get_timeout;
  2303. case TARGET_SO_PEERCRED: {
  2304. struct ucred cr;
  2305. socklen_t crlen;
  2306. struct target_ucred *tcr;
  2307. if (get_user_u32(len, optlen)) {
  2308. return -TARGET_EFAULT;
  2309. }
  2310. if (len < 0) {
  2311. return -TARGET_EINVAL;
  2312. }
  2313. crlen = sizeof(cr);
  2314. ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
  2315. &cr, &crlen));
  2316. if (ret < 0) {
  2317. return ret;
  2318. }
  2319. if (len > crlen) {
  2320. len = crlen;
  2321. }
  2322. if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
  2323. return -TARGET_EFAULT;
  2324. }
  2325. __put_user(cr.pid, &tcr->pid);
  2326. __put_user(cr.uid, &tcr->uid);
  2327. __put_user(cr.gid, &tcr->gid);
  2328. unlock_user_struct(tcr, optval_addr, 1);
  2329. if (put_user_u32(len, optlen)) {
  2330. return -TARGET_EFAULT;
  2331. }
  2332. break;
  2333. }
  2334. case TARGET_SO_PEERSEC: {
  2335. char *name;
  2336. if (get_user_u32(len, optlen)) {
  2337. return -TARGET_EFAULT;
  2338. }
  2339. if (len < 0) {
  2340. return -TARGET_EINVAL;
  2341. }
  2342. name = lock_user(VERIFY_WRITE, optval_addr, len, 0);
  2343. if (!name) {
  2344. return -TARGET_EFAULT;
  2345. }
  2346. lv = len;
  2347. ret = get_errno(getsockopt(sockfd, level, SO_PEERSEC,
  2348. name, &lv));
  2349. if (put_user_u32(lv, optlen)) {
  2350. ret = -TARGET_EFAULT;
  2351. }
  2352. unlock_user(name, optval_addr, lv);
  2353. break;
  2354. }
  2355. case TARGET_SO_LINGER:
  2356. {
  2357. struct linger lg;
  2358. socklen_t lglen;
  2359. struct target_linger *tlg;
  2360. if (get_user_u32(len, optlen)) {
  2361. return -TARGET_EFAULT;
  2362. }
  2363. if (len < 0) {
  2364. return -TARGET_EINVAL;
  2365. }
  2366. lglen = sizeof(lg);
  2367. ret = get_errno(getsockopt(sockfd, level, SO_LINGER,
  2368. &lg, &lglen));
  2369. if (ret < 0) {
  2370. return ret;
  2371. }
  2372. if (len > lglen) {
  2373. len = lglen;
  2374. }
  2375. if (!lock_user_struct(VERIFY_WRITE, tlg, optval_addr, 0)) {
  2376. return -TARGET_EFAULT;
  2377. }
  2378. __put_user(lg.l_onoff, &tlg->l_onoff);
  2379. __put_user(lg.l_linger, &tlg->l_linger);
  2380. unlock_user_struct(tlg, optval_addr, 1);
  2381. if (put_user_u32(len, optlen)) {
  2382. return -TARGET_EFAULT;
  2383. }
  2384. break;
  2385. }
  2386. /* Options with 'int' argument. */
  2387. case TARGET_SO_DEBUG:
  2388. optname = SO_DEBUG;
  2389. goto int_case;
  2390. case TARGET_SO_REUSEADDR:
  2391. optname = SO_REUSEADDR;
  2392. goto int_case;
  2393. #ifdef SO_REUSEPORT
  2394. case TARGET_SO_REUSEPORT:
  2395. optname = SO_REUSEPORT;
  2396. goto int_case;
  2397. #endif
  2398. case TARGET_SO_TYPE:
  2399. optname = SO_TYPE;
  2400. goto int_case;
  2401. case TARGET_SO_ERROR:
  2402. optname = SO_ERROR;
  2403. goto int_case;
  2404. case TARGET_SO_DONTROUTE:
  2405. optname = SO_DONTROUTE;
  2406. goto int_case;
  2407. case TARGET_SO_BROADCAST:
  2408. optname = SO_BROADCAST;
  2409. goto int_case;
  2410. case TARGET_SO_SNDBUF:
  2411. optname = SO_SNDBUF;
  2412. goto int_case;
  2413. case TARGET_SO_RCVBUF:
  2414. optname = SO_RCVBUF;
  2415. goto int_case;
  2416. case TARGET_SO_KEEPALIVE:
  2417. optname = SO_KEEPALIVE;
  2418. goto int_case;
  2419. case TARGET_SO_OOBINLINE:
  2420. optname = SO_OOBINLINE;
  2421. goto int_case;
  2422. case TARGET_SO_NO_CHECK:
  2423. optname = SO_NO_CHECK;
  2424. goto int_case;
  2425. case TARGET_SO_PRIORITY:
  2426. optname = SO_PRIORITY;
  2427. goto int_case;
  2428. #ifdef SO_BSDCOMPAT
  2429. case TARGET_SO_BSDCOMPAT:
  2430. optname = SO_BSDCOMPAT;
  2431. goto int_case;
  2432. #endif
  2433. case TARGET_SO_PASSCRED:
  2434. optname = SO_PASSCRED;
  2435. goto int_case;
  2436. case TARGET_SO_TIMESTAMP:
  2437. optname = SO_TIMESTAMP;
  2438. goto int_case;
  2439. case TARGET_SO_RCVLOWAT:
  2440. optname = SO_RCVLOWAT;
  2441. goto int_case;
  2442. case TARGET_SO_ACCEPTCONN:
  2443. optname = SO_ACCEPTCONN;
  2444. goto int_case;
  2445. case TARGET_SO_PROTOCOL:
  2446. optname = SO_PROTOCOL;
  2447. goto int_case;
  2448. case TARGET_SO_DOMAIN:
  2449. optname = SO_DOMAIN;
  2450. goto int_case;
  2451. default:
  2452. goto int_case;
  2453. }
  2454. break;
  2455. case SOL_TCP:
  2456. case SOL_UDP:
  2457. /* TCP and UDP options all take an 'int' value. */
  2458. int_case:
  2459. if (get_user_u32(len, optlen))
  2460. return -TARGET_EFAULT;
  2461. if (len < 0)
  2462. return -TARGET_EINVAL;
  2463. lv = sizeof(lv);
  2464. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2465. if (ret < 0)
  2466. return ret;
  2467. switch (optname) {
  2468. case SO_TYPE:
  2469. val = host_to_target_sock_type(val);
  2470. break;
  2471. case SO_ERROR:
  2472. val = host_to_target_errno(val);
  2473. break;
  2474. }
  2475. if (len > lv)
  2476. len = lv;
  2477. if (len == 4) {
  2478. if (put_user_u32(val, optval_addr))
  2479. return -TARGET_EFAULT;
  2480. } else {
  2481. if (put_user_u8(val, optval_addr))
  2482. return -TARGET_EFAULT;
  2483. }
  2484. if (put_user_u32(len, optlen))
  2485. return -TARGET_EFAULT;
  2486. break;
  2487. case SOL_IP:
  2488. switch(optname) {
  2489. case IP_TOS:
  2490. case IP_TTL:
  2491. case IP_HDRINCL:
  2492. case IP_ROUTER_ALERT:
  2493. case IP_RECVOPTS:
  2494. case IP_RETOPTS:
  2495. case IP_PKTINFO:
  2496. case IP_MTU_DISCOVER:
  2497. case IP_RECVERR:
  2498. case IP_RECVTOS:
  2499. #ifdef IP_FREEBIND
  2500. case IP_FREEBIND:
  2501. #endif
  2502. case IP_MULTICAST_TTL:
  2503. case IP_MULTICAST_LOOP:
  2504. if (get_user_u32(len, optlen))
  2505. return -TARGET_EFAULT;
  2506. if (len < 0)
  2507. return -TARGET_EINVAL;
  2508. lv = sizeof(lv);
  2509. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2510. if (ret < 0)
  2511. return ret;
  2512. if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
  2513. len = 1;
  2514. if (put_user_u32(len, optlen)
  2515. || put_user_u8(val, optval_addr))
  2516. return -TARGET_EFAULT;
  2517. } else {
  2518. if (len > sizeof(int))
  2519. len = sizeof(int);
  2520. if (put_user_u32(len, optlen)
  2521. || put_user_u32(val, optval_addr))
  2522. return -TARGET_EFAULT;
  2523. }
  2524. break;
  2525. default:
  2526. ret = -TARGET_ENOPROTOOPT;
  2527. break;
  2528. }
  2529. break;
  2530. case SOL_IPV6:
  2531. switch (optname) {
  2532. case IPV6_MTU_DISCOVER:
  2533. case IPV6_MTU:
  2534. case IPV6_V6ONLY:
  2535. case IPV6_RECVPKTINFO:
  2536. case IPV6_UNICAST_HOPS:
  2537. case IPV6_MULTICAST_HOPS:
  2538. case IPV6_MULTICAST_LOOP:
  2539. case IPV6_RECVERR:
  2540. case IPV6_RECVHOPLIMIT:
  2541. case IPV6_2292HOPLIMIT:
  2542. case IPV6_CHECKSUM:
  2543. case IPV6_ADDRFORM:
  2544. case IPV6_2292PKTINFO:
  2545. case IPV6_RECVTCLASS:
  2546. case IPV6_RECVRTHDR:
  2547. case IPV6_2292RTHDR:
  2548. case IPV6_RECVHOPOPTS:
  2549. case IPV6_2292HOPOPTS:
  2550. case IPV6_RECVDSTOPTS:
  2551. case IPV6_2292DSTOPTS:
  2552. case IPV6_TCLASS:
  2553. case IPV6_ADDR_PREFERENCES:
  2554. #ifdef IPV6_RECVPATHMTU
  2555. case IPV6_RECVPATHMTU:
  2556. #endif
  2557. #ifdef IPV6_TRANSPARENT
  2558. case IPV6_TRANSPARENT:
  2559. #endif
  2560. #ifdef IPV6_FREEBIND
  2561. case IPV6_FREEBIND:
  2562. #endif
  2563. #ifdef IPV6_RECVORIGDSTADDR
  2564. case IPV6_RECVORIGDSTADDR:
  2565. #endif
  2566. if (get_user_u32(len, optlen))
  2567. return -TARGET_EFAULT;
  2568. if (len < 0)
  2569. return -TARGET_EINVAL;
  2570. lv = sizeof(lv);
  2571. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2572. if (ret < 0)
  2573. return ret;
  2574. if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
  2575. len = 1;
  2576. if (put_user_u32(len, optlen)
  2577. || put_user_u8(val, optval_addr))
  2578. return -TARGET_EFAULT;
  2579. } else {
  2580. if (len > sizeof(int))
  2581. len = sizeof(int);
  2582. if (put_user_u32(len, optlen)
  2583. || put_user_u32(val, optval_addr))
  2584. return -TARGET_EFAULT;
  2585. }
  2586. break;
  2587. default:
  2588. ret = -TARGET_ENOPROTOOPT;
  2589. break;
  2590. }
  2591. break;
  2592. #ifdef SOL_NETLINK
  2593. case SOL_NETLINK:
  2594. switch (optname) {
  2595. case NETLINK_PKTINFO:
  2596. case NETLINK_BROADCAST_ERROR:
  2597. case NETLINK_NO_ENOBUFS:
  2598. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
  2599. case NETLINK_LISTEN_ALL_NSID:
  2600. case NETLINK_CAP_ACK:
  2601. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
  2602. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
  2603. case NETLINK_EXT_ACK:
  2604. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2605. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
  2606. case NETLINK_GET_STRICT_CHK:
  2607. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2608. if (get_user_u32(len, optlen)) {
  2609. return -TARGET_EFAULT;
  2610. }
  2611. if (len != sizeof(val)) {
  2612. return -TARGET_EINVAL;
  2613. }
  2614. lv = len;
  2615. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2616. if (ret < 0) {
  2617. return ret;
  2618. }
  2619. if (put_user_u32(lv, optlen)
  2620. || put_user_u32(val, optval_addr)) {
  2621. return -TARGET_EFAULT;
  2622. }
  2623. break;
  2624. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
  2625. case NETLINK_LIST_MEMBERSHIPS:
  2626. {
  2627. uint32_t *results;
  2628. int i;
  2629. if (get_user_u32(len, optlen)) {
  2630. return -TARGET_EFAULT;
  2631. }
  2632. if (len < 0) {
  2633. return -TARGET_EINVAL;
  2634. }
  2635. results = lock_user(VERIFY_WRITE, optval_addr, len, 1);
  2636. if (!results && len > 0) {
  2637. return -TARGET_EFAULT;
  2638. }
  2639. lv = len;
  2640. ret = get_errno(getsockopt(sockfd, level, optname, results, &lv));
  2641. if (ret < 0) {
  2642. unlock_user(results, optval_addr, 0);
  2643. return ret;
  2644. }
  2645. /* swap host endianness to target endianness. */
  2646. for (i = 0; i < (len / sizeof(uint32_t)); i++) {
  2647. results[i] = tswap32(results[i]);
  2648. }
  2649. if (put_user_u32(lv, optlen)) {
  2650. return -TARGET_EFAULT;
  2651. }
  2652. unlock_user(results, optval_addr, 0);
  2653. break;
  2654. }
  2655. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
  2656. default:
  2657. goto unimplemented;
  2658. }
  2659. break;
  2660. #endif /* SOL_NETLINK */
  2661. default:
  2662. unimplemented:
  2663. qemu_log_mask(LOG_UNIMP,
  2664. "getsockopt level=%d optname=%d not yet supported\n",
  2665. level, optname);
  2666. ret = -TARGET_EOPNOTSUPP;
  2667. break;
  2668. }
  2669. return ret;
  2670. }
  2671. /* Convert target low/high pair representing file offset into the host
  2672. * low/high pair. This function doesn't handle offsets bigger than 64 bits
  2673. * as the kernel doesn't handle them either.
  2674. */
  2675. static void target_to_host_low_high(abi_ulong tlow,
  2676. abi_ulong thigh,
  2677. unsigned long *hlow,
  2678. unsigned long *hhigh)
  2679. {
  2680. uint64_t off = tlow |
  2681. ((unsigned long long)thigh << TARGET_LONG_BITS / 2) <<
  2682. TARGET_LONG_BITS / 2;
  2683. *hlow = off;
  2684. *hhigh = (off >> HOST_LONG_BITS / 2) >> HOST_LONG_BITS / 2;
  2685. }
  2686. static struct iovec *lock_iovec(int type, abi_ulong target_addr,
  2687. abi_ulong count, int copy)
  2688. {
  2689. struct target_iovec *target_vec;
  2690. struct iovec *vec;
  2691. abi_ulong total_len, max_len;
  2692. int i;
  2693. int err = 0;
  2694. bool bad_address = false;
  2695. if (count == 0) {
  2696. errno = 0;
  2697. return NULL;
  2698. }
  2699. if (count > IOV_MAX) {
  2700. errno = EINVAL;
  2701. return NULL;
  2702. }
  2703. vec = g_try_new0(struct iovec, count);
  2704. if (vec == NULL) {
  2705. errno = ENOMEM;
  2706. return NULL;
  2707. }
  2708. target_vec = lock_user(VERIFY_READ, target_addr,
  2709. count * sizeof(struct target_iovec), 1);
  2710. if (target_vec == NULL) {
  2711. err = EFAULT;
  2712. goto fail2;
  2713. }
  2714. /* ??? If host page size > target page size, this will result in a
  2715. value larger than what we can actually support. */
  2716. max_len = 0x7fffffff & TARGET_PAGE_MASK;
  2717. total_len = 0;
  2718. for (i = 0; i < count; i++) {
  2719. abi_ulong base = tswapal(target_vec[i].iov_base);
  2720. abi_long len = tswapal(target_vec[i].iov_len);
  2721. if (len < 0) {
  2722. err = EINVAL;
  2723. goto fail;
  2724. } else if (len == 0) {
  2725. /* Zero length pointer is ignored. */
  2726. vec[i].iov_base = 0;
  2727. } else {
  2728. vec[i].iov_base = lock_user(type, base, len, copy);
  2729. /* If the first buffer pointer is bad, this is a fault. But
  2730. * subsequent bad buffers will result in a partial write; this
  2731. * is realized by filling the vector with null pointers and
  2732. * zero lengths. */
  2733. if (!vec[i].iov_base) {
  2734. if (i == 0) {
  2735. err = EFAULT;
  2736. goto fail;
  2737. } else {
  2738. bad_address = true;
  2739. }
  2740. }
  2741. if (bad_address) {
  2742. len = 0;
  2743. }
  2744. if (len > max_len - total_len) {
  2745. len = max_len - total_len;
  2746. }
  2747. }
  2748. vec[i].iov_len = len;
  2749. total_len += len;
  2750. }
  2751. unlock_user(target_vec, target_addr, 0);
  2752. return vec;
  2753. fail:
  2754. while (--i >= 0) {
  2755. if (tswapal(target_vec[i].iov_len) > 0) {
  2756. unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0);
  2757. }
  2758. }
  2759. unlock_user(target_vec, target_addr, 0);
  2760. fail2:
  2761. g_free(vec);
  2762. errno = err;
  2763. return NULL;
  2764. }
  2765. static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
  2766. abi_ulong count, int copy)
  2767. {
  2768. struct target_iovec *target_vec;
  2769. int i;
  2770. target_vec = lock_user(VERIFY_READ, target_addr,
  2771. count * sizeof(struct target_iovec), 1);
  2772. if (target_vec) {
  2773. for (i = 0; i < count; i++) {
  2774. abi_ulong base = tswapal(target_vec[i].iov_base);
  2775. abi_long len = tswapal(target_vec[i].iov_len);
  2776. if (len < 0) {
  2777. break;
  2778. }
  2779. unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
  2780. }
  2781. unlock_user(target_vec, target_addr, 0);
  2782. }
  2783. g_free(vec);
  2784. }
  2785. static inline int target_to_host_sock_type(int *type)
  2786. {
  2787. int host_type = 0;
  2788. int target_type = *type;
  2789. switch (target_type & TARGET_SOCK_TYPE_MASK) {
  2790. case TARGET_SOCK_DGRAM:
  2791. host_type = SOCK_DGRAM;
  2792. break;
  2793. case TARGET_SOCK_STREAM:
  2794. host_type = SOCK_STREAM;
  2795. break;
  2796. default:
  2797. host_type = target_type & TARGET_SOCK_TYPE_MASK;
  2798. break;
  2799. }
  2800. if (target_type & TARGET_SOCK_CLOEXEC) {
  2801. #if defined(SOCK_CLOEXEC)
  2802. host_type |= SOCK_CLOEXEC;
  2803. #else
  2804. return -TARGET_EINVAL;
  2805. #endif
  2806. }
  2807. if (target_type & TARGET_SOCK_NONBLOCK) {
  2808. #if defined(SOCK_NONBLOCK)
  2809. host_type |= SOCK_NONBLOCK;
  2810. #elif !defined(O_NONBLOCK)
  2811. return -TARGET_EINVAL;
  2812. #endif
  2813. }
  2814. *type = host_type;
  2815. return 0;
  2816. }
  2817. /* Try to emulate socket type flags after socket creation. */
  2818. static int sock_flags_fixup(int fd, int target_type)
  2819. {
  2820. #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
  2821. if (target_type & TARGET_SOCK_NONBLOCK) {
  2822. int flags = fcntl(fd, F_GETFL);
  2823. if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) {
  2824. close(fd);
  2825. return -TARGET_EINVAL;
  2826. }
  2827. }
  2828. #endif
  2829. return fd;
  2830. }
  2831. /* do_socket() Must return target values and target errnos. */
  2832. static abi_long do_socket(int domain, int type, int protocol)
  2833. {
  2834. int target_type = type;
  2835. int ret;
  2836. ret = target_to_host_sock_type(&type);
  2837. if (ret) {
  2838. return ret;
  2839. }
  2840. if (domain == PF_NETLINK && !(
  2841. #ifdef CONFIG_RTNETLINK
  2842. protocol == NETLINK_ROUTE ||
  2843. #endif
  2844. protocol == NETLINK_KOBJECT_UEVENT ||
  2845. protocol == NETLINK_AUDIT)) {
  2846. return -TARGET_EPROTONOSUPPORT;
  2847. }
  2848. if (domain == AF_PACKET ||
  2849. (domain == AF_INET && type == SOCK_PACKET)) {
  2850. protocol = tswap16(protocol);
  2851. }
  2852. ret = get_errno(socket(domain, type, protocol));
  2853. if (ret >= 0) {
  2854. ret = sock_flags_fixup(ret, target_type);
  2855. if (type == SOCK_PACKET) {
  2856. /* Manage an obsolete case :
  2857. * if socket type is SOCK_PACKET, bind by name
  2858. */
  2859. fd_trans_register(ret, &target_packet_trans);
  2860. } else if (domain == PF_NETLINK) {
  2861. switch (protocol) {
  2862. #ifdef CONFIG_RTNETLINK
  2863. case NETLINK_ROUTE:
  2864. fd_trans_register(ret, &target_netlink_route_trans);
  2865. break;
  2866. #endif
  2867. case NETLINK_KOBJECT_UEVENT:
  2868. /* nothing to do: messages are strings */
  2869. break;
  2870. case NETLINK_AUDIT:
  2871. fd_trans_register(ret, &target_netlink_audit_trans);
  2872. break;
  2873. default:
  2874. g_assert_not_reached();
  2875. }
  2876. }
  2877. }
  2878. return ret;
  2879. }
  2880. /* do_bind() Must return target values and target errnos. */
  2881. static abi_long do_bind(int sockfd, abi_ulong target_addr,
  2882. socklen_t addrlen)
  2883. {
  2884. void *addr;
  2885. abi_long ret;
  2886. if ((int)addrlen < 0) {
  2887. return -TARGET_EINVAL;
  2888. }
  2889. addr = alloca(addrlen+1);
  2890. ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
  2891. if (ret)
  2892. return ret;
  2893. return get_errno(bind(sockfd, addr, addrlen));
  2894. }
  2895. /* do_connect() Must return target values and target errnos. */
  2896. static abi_long do_connect(int sockfd, abi_ulong target_addr,
  2897. socklen_t addrlen)
  2898. {
  2899. void *addr;
  2900. abi_long ret;
  2901. if ((int)addrlen < 0) {
  2902. return -TARGET_EINVAL;
  2903. }
  2904. addr = alloca(addrlen+1);
  2905. ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
  2906. if (ret)
  2907. return ret;
  2908. return get_errno(safe_connect(sockfd, addr, addrlen));
  2909. }
  2910. /* do_sendrecvmsg_locked() Must return target values and target errnos. */
  2911. static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
  2912. int flags, int send)
  2913. {
  2914. abi_long ret, len;
  2915. struct msghdr msg;
  2916. abi_ulong count;
  2917. struct iovec *vec;
  2918. abi_ulong target_vec;
  2919. if (msgp->msg_name) {
  2920. msg.msg_namelen = tswap32(msgp->msg_namelen);
  2921. msg.msg_name = alloca(msg.msg_namelen+1);
  2922. ret = target_to_host_sockaddr(fd, msg.msg_name,
  2923. tswapal(msgp->msg_name),
  2924. msg.msg_namelen);
  2925. if (ret == -TARGET_EFAULT) {
  2926. /* For connected sockets msg_name and msg_namelen must
  2927. * be ignored, so returning EFAULT immediately is wrong.
  2928. * Instead, pass a bad msg_name to the host kernel, and
  2929. * let it decide whether to return EFAULT or not.
  2930. */
  2931. msg.msg_name = (void *)-1;
  2932. } else if (ret) {
  2933. goto out2;
  2934. }
  2935. } else {
  2936. msg.msg_name = NULL;
  2937. msg.msg_namelen = 0;
  2938. }
  2939. msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
  2940. msg.msg_control = alloca(msg.msg_controllen);
  2941. memset(msg.msg_control, 0, msg.msg_controllen);
  2942. msg.msg_flags = tswap32(msgp->msg_flags);
  2943. count = tswapal(msgp->msg_iovlen);
  2944. target_vec = tswapal(msgp->msg_iov);
  2945. if (count > IOV_MAX) {
  2946. /* sendrcvmsg returns a different errno for this condition than
  2947. * readv/writev, so we must catch it here before lock_iovec() does.
  2948. */
  2949. ret = -TARGET_EMSGSIZE;
  2950. goto out2;
  2951. }
  2952. vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
  2953. target_vec, count, send);
  2954. if (vec == NULL) {
  2955. ret = -host_to_target_errno(errno);
  2956. /* allow sending packet without any iov, e.g. with MSG_MORE flag */
  2957. if (!send || ret) {
  2958. goto out2;
  2959. }
  2960. }
  2961. msg.msg_iovlen = count;
  2962. msg.msg_iov = vec;
  2963. if (send) {
  2964. if (fd_trans_target_to_host_data(fd)) {
  2965. void *host_msg;
  2966. host_msg = g_malloc(msg.msg_iov->iov_len);
  2967. memcpy(host_msg, msg.msg_iov->iov_base, msg.msg_iov->iov_len);
  2968. ret = fd_trans_target_to_host_data(fd)(host_msg,
  2969. msg.msg_iov->iov_len);
  2970. if (ret >= 0) {
  2971. msg.msg_iov->iov_base = host_msg;
  2972. ret = get_errno(safe_sendmsg(fd, &msg, flags));
  2973. }
  2974. g_free(host_msg);
  2975. } else {
  2976. ret = target_to_host_cmsg(&msg, msgp);
  2977. if (ret == 0) {
  2978. ret = get_errno(safe_sendmsg(fd, &msg, flags));
  2979. }
  2980. }
  2981. } else {
  2982. ret = get_errno(safe_recvmsg(fd, &msg, flags));
  2983. if (!is_error(ret)) {
  2984. len = ret;
  2985. if (fd_trans_host_to_target_data(fd)) {
  2986. ret = fd_trans_host_to_target_data(fd)(msg.msg_iov->iov_base,
  2987. MIN(msg.msg_iov->iov_len, len));
  2988. }
  2989. if (!is_error(ret)) {
  2990. ret = host_to_target_cmsg(msgp, &msg);
  2991. }
  2992. if (!is_error(ret)) {
  2993. msgp->msg_namelen = tswap32(msg.msg_namelen);
  2994. msgp->msg_flags = tswap32(msg.msg_flags);
  2995. if (msg.msg_name != NULL && msg.msg_name != (void *)-1) {
  2996. ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
  2997. msg.msg_name, msg.msg_namelen);
  2998. if (ret) {
  2999. goto out;
  3000. }
  3001. }
  3002. ret = len;
  3003. }
  3004. }
  3005. }
  3006. out:
  3007. if (vec) {
  3008. unlock_iovec(vec, target_vec, count, !send);
  3009. }
  3010. out2:
  3011. return ret;
  3012. }
  3013. static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
  3014. int flags, int send)
  3015. {
  3016. abi_long ret;
  3017. struct target_msghdr *msgp;
  3018. if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
  3019. msgp,
  3020. target_msg,
  3021. send ? 1 : 0)) {
  3022. return -TARGET_EFAULT;
  3023. }
  3024. ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
  3025. unlock_user_struct(msgp, target_msg, send ? 0 : 1);
  3026. return ret;
  3027. }
  3028. /* We don't rely on the C library to have sendmmsg/recvmmsg support,
  3029. * so it might not have this *mmsg-specific flag either.
  3030. */
  3031. #ifndef MSG_WAITFORONE
  3032. #define MSG_WAITFORONE 0x10000
  3033. #endif
  3034. static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec,
  3035. unsigned int vlen, unsigned int flags,
  3036. int send)
  3037. {
  3038. struct target_mmsghdr *mmsgp;
  3039. abi_long ret = 0;
  3040. int i;
  3041. if (vlen > UIO_MAXIOV) {
  3042. vlen = UIO_MAXIOV;
  3043. }
  3044. mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1);
  3045. if (!mmsgp) {
  3046. return -TARGET_EFAULT;
  3047. }
  3048. for (i = 0; i < vlen; i++) {
  3049. ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send);
  3050. if (is_error(ret)) {
  3051. break;
  3052. }
  3053. mmsgp[i].msg_len = tswap32(ret);
  3054. /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
  3055. if (flags & MSG_WAITFORONE) {
  3056. flags |= MSG_DONTWAIT;
  3057. }
  3058. }
  3059. unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i);
  3060. /* Return number of datagrams sent if we sent any at all;
  3061. * otherwise return the error.
  3062. */
  3063. if (i) {
  3064. return i;
  3065. }
  3066. return ret;
  3067. }
  3068. /* do_accept4() Must return target values and target errnos. */
  3069. static abi_long do_accept4(int fd, abi_ulong target_addr,
  3070. abi_ulong target_addrlen_addr, int flags)
  3071. {
  3072. socklen_t addrlen, ret_addrlen;
  3073. void *addr;
  3074. abi_long ret;
  3075. int host_flags;
  3076. if (flags & ~(TARGET_SOCK_CLOEXEC | TARGET_SOCK_NONBLOCK)) {
  3077. return -TARGET_EINVAL;
  3078. }
  3079. host_flags = 0;
  3080. if (flags & TARGET_SOCK_NONBLOCK) {
  3081. host_flags |= SOCK_NONBLOCK;
  3082. }
  3083. if (flags & TARGET_SOCK_CLOEXEC) {
  3084. host_flags |= SOCK_CLOEXEC;
  3085. }
  3086. if (target_addr == 0) {
  3087. return get_errno(safe_accept4(fd, NULL, NULL, host_flags));
  3088. }
  3089. /* linux returns EFAULT if addrlen pointer is invalid */
  3090. if (get_user_u32(addrlen, target_addrlen_addr))
  3091. return -TARGET_EFAULT;
  3092. if ((int)addrlen < 0) {
  3093. return -TARGET_EINVAL;
  3094. }
  3095. if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
  3096. return -TARGET_EFAULT;
  3097. }
  3098. addr = alloca(addrlen);
  3099. ret_addrlen = addrlen;
  3100. ret = get_errno(safe_accept4(fd, addr, &ret_addrlen, host_flags));
  3101. if (!is_error(ret)) {
  3102. host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
  3103. if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
  3104. ret = -TARGET_EFAULT;
  3105. }
  3106. }
  3107. return ret;
  3108. }
  3109. /* do_getpeername() Must return target values and target errnos. */
  3110. static abi_long do_getpeername(int fd, abi_ulong target_addr,
  3111. abi_ulong target_addrlen_addr)
  3112. {
  3113. socklen_t addrlen, ret_addrlen;
  3114. void *addr;
  3115. abi_long ret;
  3116. if (get_user_u32(addrlen, target_addrlen_addr))
  3117. return -TARGET_EFAULT;
  3118. if ((int)addrlen < 0) {
  3119. return -TARGET_EINVAL;
  3120. }
  3121. if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
  3122. return -TARGET_EFAULT;
  3123. }
  3124. addr = alloca(addrlen);
  3125. ret_addrlen = addrlen;
  3126. ret = get_errno(getpeername(fd, addr, &ret_addrlen));
  3127. if (!is_error(ret)) {
  3128. host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
  3129. if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
  3130. ret = -TARGET_EFAULT;
  3131. }
  3132. }
  3133. return ret;
  3134. }
  3135. /* do_getsockname() Must return target values and target errnos. */
  3136. static abi_long do_getsockname(int fd, abi_ulong target_addr,
  3137. abi_ulong target_addrlen_addr)
  3138. {
  3139. socklen_t addrlen, ret_addrlen;
  3140. void *addr;
  3141. abi_long ret;
  3142. if (get_user_u32(addrlen, target_addrlen_addr))
  3143. return -TARGET_EFAULT;
  3144. if ((int)addrlen < 0) {
  3145. return -TARGET_EINVAL;
  3146. }
  3147. if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
  3148. return -TARGET_EFAULT;
  3149. }
  3150. addr = alloca(addrlen);
  3151. ret_addrlen = addrlen;
  3152. ret = get_errno(getsockname(fd, addr, &ret_addrlen));
  3153. if (!is_error(ret)) {
  3154. host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
  3155. if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
  3156. ret = -TARGET_EFAULT;
  3157. }
  3158. }
  3159. return ret;
  3160. }
  3161. /* do_socketpair() Must return target values and target errnos. */
  3162. static abi_long do_socketpair(int domain, int type, int protocol,
  3163. abi_ulong target_tab_addr)
  3164. {
  3165. int tab[2];
  3166. abi_long ret;
  3167. target_to_host_sock_type(&type);
  3168. ret = get_errno(socketpair(domain, type, protocol, tab));
  3169. if (!is_error(ret)) {
  3170. if (put_user_s32(tab[0], target_tab_addr)
  3171. || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
  3172. ret = -TARGET_EFAULT;
  3173. }
  3174. return ret;
  3175. }
  3176. /* do_sendto() Must return target values and target errnos. */
  3177. static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
  3178. abi_ulong target_addr, socklen_t addrlen)
  3179. {
  3180. void *addr;
  3181. void *host_msg;
  3182. void *copy_msg = NULL;
  3183. abi_long ret;
  3184. if ((int)addrlen < 0) {
  3185. return -TARGET_EINVAL;
  3186. }
  3187. host_msg = lock_user(VERIFY_READ, msg, len, 1);
  3188. if (!host_msg)
  3189. return -TARGET_EFAULT;
  3190. if (fd_trans_target_to_host_data(fd)) {
  3191. copy_msg = host_msg;
  3192. host_msg = g_malloc(len);
  3193. memcpy(host_msg, copy_msg, len);
  3194. ret = fd_trans_target_to_host_data(fd)(host_msg, len);
  3195. if (ret < 0) {
  3196. goto fail;
  3197. }
  3198. }
  3199. if (target_addr) {
  3200. addr = alloca(addrlen+1);
  3201. ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen);
  3202. if (ret) {
  3203. goto fail;
  3204. }
  3205. ret = get_errno(safe_sendto(fd, host_msg, len, flags, addr, addrlen));
  3206. } else {
  3207. ret = get_errno(safe_sendto(fd, host_msg, len, flags, NULL, 0));
  3208. }
  3209. fail:
  3210. if (copy_msg) {
  3211. g_free(host_msg);
  3212. host_msg = copy_msg;
  3213. }
  3214. unlock_user(host_msg, msg, 0);
  3215. return ret;
  3216. }
  3217. /* do_recvfrom() Must return target values and target errnos. */
  3218. static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
  3219. abi_ulong target_addr,
  3220. abi_ulong target_addrlen)
  3221. {
  3222. socklen_t addrlen, ret_addrlen;
  3223. void *addr;
  3224. void *host_msg;
  3225. abi_long ret;
  3226. if (!msg) {
  3227. host_msg = NULL;
  3228. } else {
  3229. host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
  3230. if (!host_msg) {
  3231. return -TARGET_EFAULT;
  3232. }
  3233. }
  3234. if (target_addr) {
  3235. if (get_user_u32(addrlen, target_addrlen)) {
  3236. ret = -TARGET_EFAULT;
  3237. goto fail;
  3238. }
  3239. if ((int)addrlen < 0) {
  3240. ret = -TARGET_EINVAL;
  3241. goto fail;
  3242. }
  3243. addr = alloca(addrlen);
  3244. ret_addrlen = addrlen;
  3245. ret = get_errno(safe_recvfrom(fd, host_msg, len, flags,
  3246. addr, &ret_addrlen));
  3247. } else {
  3248. addr = NULL; /* To keep compiler quiet. */
  3249. addrlen = 0; /* To keep compiler quiet. */
  3250. ret = get_errno(safe_recvfrom(fd, host_msg, len, flags, NULL, 0));
  3251. }
  3252. if (!is_error(ret)) {
  3253. if (fd_trans_host_to_target_data(fd)) {
  3254. abi_long trans;
  3255. trans = fd_trans_host_to_target_data(fd)(host_msg, MIN(ret, len));
  3256. if (is_error(trans)) {
  3257. ret = trans;
  3258. goto fail;
  3259. }
  3260. }
  3261. if (target_addr) {
  3262. host_to_target_sockaddr(target_addr, addr,
  3263. MIN(addrlen, ret_addrlen));
  3264. if (put_user_u32(ret_addrlen, target_addrlen)) {
  3265. ret = -TARGET_EFAULT;
  3266. goto fail;
  3267. }
  3268. }
  3269. unlock_user(host_msg, msg, len);
  3270. } else {
  3271. fail:
  3272. unlock_user(host_msg, msg, 0);
  3273. }
  3274. return ret;
  3275. }
  3276. #ifdef TARGET_NR_socketcall
  3277. /* do_socketcall() must return target values and target errnos. */
  3278. static abi_long do_socketcall(int num, abi_ulong vptr)
  3279. {
  3280. static const unsigned nargs[] = { /* number of arguments per operation */
  3281. [TARGET_SYS_SOCKET] = 3, /* domain, type, protocol */
  3282. [TARGET_SYS_BIND] = 3, /* fd, addr, addrlen */
  3283. [TARGET_SYS_CONNECT] = 3, /* fd, addr, addrlen */
  3284. [TARGET_SYS_LISTEN] = 2, /* fd, backlog */
  3285. [TARGET_SYS_ACCEPT] = 3, /* fd, addr, addrlen */
  3286. [TARGET_SYS_GETSOCKNAME] = 3, /* fd, addr, addrlen */
  3287. [TARGET_SYS_GETPEERNAME] = 3, /* fd, addr, addrlen */
  3288. [TARGET_SYS_SOCKETPAIR] = 4, /* domain, type, protocol, tab */
  3289. [TARGET_SYS_SEND] = 4, /* fd, msg, len, flags */
  3290. [TARGET_SYS_RECV] = 4, /* fd, msg, len, flags */
  3291. [TARGET_SYS_SENDTO] = 6, /* fd, msg, len, flags, addr, addrlen */
  3292. [TARGET_SYS_RECVFROM] = 6, /* fd, msg, len, flags, addr, addrlen */
  3293. [TARGET_SYS_SHUTDOWN] = 2, /* fd, how */
  3294. [TARGET_SYS_SETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
  3295. [TARGET_SYS_GETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
  3296. [TARGET_SYS_SENDMSG] = 3, /* fd, msg, flags */
  3297. [TARGET_SYS_RECVMSG] = 3, /* fd, msg, flags */
  3298. [TARGET_SYS_ACCEPT4] = 4, /* fd, addr, addrlen, flags */
  3299. [TARGET_SYS_RECVMMSG] = 4, /* fd, msgvec, vlen, flags */
  3300. [TARGET_SYS_SENDMMSG] = 4, /* fd, msgvec, vlen, flags */
  3301. };
  3302. abi_long a[6]; /* max 6 args */
  3303. unsigned i;
  3304. /* check the range of the first argument num */
  3305. /* (TARGET_SYS_SENDMMSG is the highest among TARGET_SYS_xxx) */
  3306. if (num < 1 || num > TARGET_SYS_SENDMMSG) {
  3307. return -TARGET_EINVAL;
  3308. }
  3309. /* ensure we have space for args */
  3310. if (nargs[num] > ARRAY_SIZE(a)) {
  3311. return -TARGET_EINVAL;
  3312. }
  3313. /* collect the arguments in a[] according to nargs[] */
  3314. for (i = 0; i < nargs[num]; ++i) {
  3315. if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) {
  3316. return -TARGET_EFAULT;
  3317. }
  3318. }
  3319. /* now when we have the args, invoke the appropriate underlying function */
  3320. switch (num) {
  3321. case TARGET_SYS_SOCKET: /* domain, type, protocol */
  3322. return do_socket(a[0], a[1], a[2]);
  3323. case TARGET_SYS_BIND: /* sockfd, addr, addrlen */
  3324. return do_bind(a[0], a[1], a[2]);
  3325. case TARGET_SYS_CONNECT: /* sockfd, addr, addrlen */
  3326. return do_connect(a[0], a[1], a[2]);
  3327. case TARGET_SYS_LISTEN: /* sockfd, backlog */
  3328. return get_errno(listen(a[0], a[1]));
  3329. case TARGET_SYS_ACCEPT: /* sockfd, addr, addrlen */
  3330. return do_accept4(a[0], a[1], a[2], 0);
  3331. case TARGET_SYS_GETSOCKNAME: /* sockfd, addr, addrlen */
  3332. return do_getsockname(a[0], a[1], a[2]);
  3333. case TARGET_SYS_GETPEERNAME: /* sockfd, addr, addrlen */
  3334. return do_getpeername(a[0], a[1], a[2]);
  3335. case TARGET_SYS_SOCKETPAIR: /* domain, type, protocol, tab */
  3336. return do_socketpair(a[0], a[1], a[2], a[3]);
  3337. case TARGET_SYS_SEND: /* sockfd, msg, len, flags */
  3338. return do_sendto(a[0], a[1], a[2], a[3], 0, 0);
  3339. case TARGET_SYS_RECV: /* sockfd, msg, len, flags */
  3340. return do_recvfrom(a[0], a[1], a[2], a[3], 0, 0);
  3341. case TARGET_SYS_SENDTO: /* sockfd, msg, len, flags, addr, addrlen */
  3342. return do_sendto(a[0], a[1], a[2], a[3], a[4], a[5]);
  3343. case TARGET_SYS_RECVFROM: /* sockfd, msg, len, flags, addr, addrlen */
  3344. return do_recvfrom(a[0], a[1], a[2], a[3], a[4], a[5]);
  3345. case TARGET_SYS_SHUTDOWN: /* sockfd, how */
  3346. return get_errno(shutdown(a[0], a[1]));
  3347. case TARGET_SYS_SETSOCKOPT: /* sockfd, level, optname, optval, optlen */
  3348. return do_setsockopt(a[0], a[1], a[2], a[3], a[4]);
  3349. case TARGET_SYS_GETSOCKOPT: /* sockfd, level, optname, optval, optlen */
  3350. return do_getsockopt(a[0], a[1], a[2], a[3], a[4]);
  3351. case TARGET_SYS_SENDMSG: /* sockfd, msg, flags */
  3352. return do_sendrecvmsg(a[0], a[1], a[2], 1);
  3353. case TARGET_SYS_RECVMSG: /* sockfd, msg, flags */
  3354. return do_sendrecvmsg(a[0], a[1], a[2], 0);
  3355. case TARGET_SYS_ACCEPT4: /* sockfd, addr, addrlen, flags */
  3356. return do_accept4(a[0], a[1], a[2], a[3]);
  3357. case TARGET_SYS_RECVMMSG: /* sockfd, msgvec, vlen, flags */
  3358. return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 0);
  3359. case TARGET_SYS_SENDMMSG: /* sockfd, msgvec, vlen, flags */
  3360. return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 1);
  3361. default:
  3362. qemu_log_mask(LOG_UNIMP, "Unsupported socketcall: %d\n", num);
  3363. return -TARGET_EINVAL;
  3364. }
  3365. }
  3366. #endif
  3367. #define N_SHM_REGIONS 32
  3368. static struct shm_region {
  3369. abi_ulong start;
  3370. abi_ulong size;
  3371. bool in_use;
  3372. } shm_regions[N_SHM_REGIONS];
  3373. #ifndef TARGET_SEMID64_DS
  3374. /* asm-generic version of this struct */
  3375. struct target_semid64_ds
  3376. {
  3377. struct target_ipc_perm sem_perm;
  3378. abi_ulong sem_otime;
  3379. #if TARGET_ABI_BITS == 32
  3380. abi_ulong __unused1;
  3381. #endif
  3382. abi_ulong sem_ctime;
  3383. #if TARGET_ABI_BITS == 32
  3384. abi_ulong __unused2;
  3385. #endif
  3386. abi_ulong sem_nsems;
  3387. abi_ulong __unused3;
  3388. abi_ulong __unused4;
  3389. };
  3390. #endif
  3391. static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
  3392. abi_ulong target_addr)
  3393. {
  3394. struct target_ipc_perm *target_ip;
  3395. struct target_semid64_ds *target_sd;
  3396. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  3397. return -TARGET_EFAULT;
  3398. target_ip = &(target_sd->sem_perm);
  3399. host_ip->__key = tswap32(target_ip->__key);
  3400. host_ip->uid = tswap32(target_ip->uid);
  3401. host_ip->gid = tswap32(target_ip->gid);
  3402. host_ip->cuid = tswap32(target_ip->cuid);
  3403. host_ip->cgid = tswap32(target_ip->cgid);
  3404. #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
  3405. host_ip->mode = tswap32(target_ip->mode);
  3406. #else
  3407. host_ip->mode = tswap16(target_ip->mode);
  3408. #endif
  3409. #if defined(TARGET_PPC)
  3410. host_ip->__seq = tswap32(target_ip->__seq);
  3411. #else
  3412. host_ip->__seq = tswap16(target_ip->__seq);
  3413. #endif
  3414. unlock_user_struct(target_sd, target_addr, 0);
  3415. return 0;
  3416. }
  3417. static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
  3418. struct ipc_perm *host_ip)
  3419. {
  3420. struct target_ipc_perm *target_ip;
  3421. struct target_semid64_ds *target_sd;
  3422. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  3423. return -TARGET_EFAULT;
  3424. target_ip = &(target_sd->sem_perm);
  3425. target_ip->__key = tswap32(host_ip->__key);
  3426. target_ip->uid = tswap32(host_ip->uid);
  3427. target_ip->gid = tswap32(host_ip->gid);
  3428. target_ip->cuid = tswap32(host_ip->cuid);
  3429. target_ip->cgid = tswap32(host_ip->cgid);
  3430. #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
  3431. target_ip->mode = tswap32(host_ip->mode);
  3432. #else
  3433. target_ip->mode = tswap16(host_ip->mode);
  3434. #endif
  3435. #if defined(TARGET_PPC)
  3436. target_ip->__seq = tswap32(host_ip->__seq);
  3437. #else
  3438. target_ip->__seq = tswap16(host_ip->__seq);
  3439. #endif
  3440. unlock_user_struct(target_sd, target_addr, 1);
  3441. return 0;
  3442. }
  3443. static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
  3444. abi_ulong target_addr)
  3445. {
  3446. struct target_semid64_ds *target_sd;
  3447. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  3448. return -TARGET_EFAULT;
  3449. if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
  3450. return -TARGET_EFAULT;
  3451. host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
  3452. host_sd->sem_otime = tswapal(target_sd->sem_otime);
  3453. host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
  3454. unlock_user_struct(target_sd, target_addr, 0);
  3455. return 0;
  3456. }
  3457. static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
  3458. struct semid_ds *host_sd)
  3459. {
  3460. struct target_semid64_ds *target_sd;
  3461. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  3462. return -TARGET_EFAULT;
  3463. if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
  3464. return -TARGET_EFAULT;
  3465. target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
  3466. target_sd->sem_otime = tswapal(host_sd->sem_otime);
  3467. target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
  3468. unlock_user_struct(target_sd, target_addr, 1);
  3469. return 0;
  3470. }
  3471. struct target_seminfo {
  3472. int semmap;
  3473. int semmni;
  3474. int semmns;
  3475. int semmnu;
  3476. int semmsl;
  3477. int semopm;
  3478. int semume;
  3479. int semusz;
  3480. int semvmx;
  3481. int semaem;
  3482. };
  3483. static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
  3484. struct seminfo *host_seminfo)
  3485. {
  3486. struct target_seminfo *target_seminfo;
  3487. if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
  3488. return -TARGET_EFAULT;
  3489. __put_user(host_seminfo->semmap, &target_seminfo->semmap);
  3490. __put_user(host_seminfo->semmni, &target_seminfo->semmni);
  3491. __put_user(host_seminfo->semmns, &target_seminfo->semmns);
  3492. __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
  3493. __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
  3494. __put_user(host_seminfo->semopm, &target_seminfo->semopm);
  3495. __put_user(host_seminfo->semume, &target_seminfo->semume);
  3496. __put_user(host_seminfo->semusz, &target_seminfo->semusz);
  3497. __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
  3498. __put_user(host_seminfo->semaem, &target_seminfo->semaem);
  3499. unlock_user_struct(target_seminfo, target_addr, 1);
  3500. return 0;
  3501. }
  3502. union semun {
  3503. int val;
  3504. struct semid_ds *buf;
  3505. unsigned short *array;
  3506. struct seminfo *__buf;
  3507. };
  3508. union target_semun {
  3509. int val;
  3510. abi_ulong buf;
  3511. abi_ulong array;
  3512. abi_ulong __buf;
  3513. };
  3514. static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
  3515. abi_ulong target_addr)
  3516. {
  3517. int nsems;
  3518. unsigned short *array;
  3519. union semun semun;
  3520. struct semid_ds semid_ds;
  3521. int i, ret;
  3522. semun.buf = &semid_ds;
  3523. ret = semctl(semid, 0, IPC_STAT, semun);
  3524. if (ret == -1)
  3525. return get_errno(ret);
  3526. nsems = semid_ds.sem_nsems;
  3527. *host_array = g_try_new(unsigned short, nsems);
  3528. if (!*host_array) {
  3529. return -TARGET_ENOMEM;
  3530. }
  3531. array = lock_user(VERIFY_READ, target_addr,
  3532. nsems*sizeof(unsigned short), 1);
  3533. if (!array) {
  3534. g_free(*host_array);
  3535. return -TARGET_EFAULT;
  3536. }
  3537. for(i=0; i<nsems; i++) {
  3538. __get_user((*host_array)[i], &array[i]);
  3539. }
  3540. unlock_user(array, target_addr, 0);
  3541. return 0;
  3542. }
  3543. static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
  3544. unsigned short **host_array)
  3545. {
  3546. int nsems;
  3547. unsigned short *array;
  3548. union semun semun;
  3549. struct semid_ds semid_ds;
  3550. int i, ret;
  3551. semun.buf = &semid_ds;
  3552. ret = semctl(semid, 0, IPC_STAT, semun);
  3553. if (ret == -1)
  3554. return get_errno(ret);
  3555. nsems = semid_ds.sem_nsems;
  3556. array = lock_user(VERIFY_WRITE, target_addr,
  3557. nsems*sizeof(unsigned short), 0);
  3558. if (!array)
  3559. return -TARGET_EFAULT;
  3560. for(i=0; i<nsems; i++) {
  3561. __put_user((*host_array)[i], &array[i]);
  3562. }
  3563. g_free(*host_array);
  3564. unlock_user(array, target_addr, 1);
  3565. return 0;
  3566. }
  3567. static inline abi_long do_semctl(int semid, int semnum, int cmd,
  3568. abi_ulong target_arg)
  3569. {
  3570. union target_semun target_su = { .buf = target_arg };
  3571. union semun arg;
  3572. struct semid_ds dsarg;
  3573. unsigned short *array = NULL;
  3574. struct seminfo seminfo;
  3575. abi_long ret = -TARGET_EINVAL;
  3576. abi_long err;
  3577. cmd &= 0xff;
  3578. switch( cmd ) {
  3579. case GETVAL:
  3580. case SETVAL:
  3581. /* In 64 bit cross-endian situations, we will erroneously pick up
  3582. * the wrong half of the union for the "val" element. To rectify
  3583. * this, the entire 8-byte structure is byteswapped, followed by
  3584. * a swap of the 4 byte val field. In other cases, the data is
  3585. * already in proper host byte order. */
  3586. if (sizeof(target_su.val) != (sizeof(target_su.buf))) {
  3587. target_su.buf = tswapal(target_su.buf);
  3588. arg.val = tswap32(target_su.val);
  3589. } else {
  3590. arg.val = target_su.val;
  3591. }
  3592. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3593. break;
  3594. case GETALL:
  3595. case SETALL:
  3596. err = target_to_host_semarray(semid, &array, target_su.array);
  3597. if (err)
  3598. return err;
  3599. arg.array = array;
  3600. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3601. err = host_to_target_semarray(semid, target_su.array, &array);
  3602. if (err)
  3603. return err;
  3604. break;
  3605. case IPC_STAT:
  3606. case IPC_SET:
  3607. case SEM_STAT:
  3608. err = target_to_host_semid_ds(&dsarg, target_su.buf);
  3609. if (err)
  3610. return err;
  3611. arg.buf = &dsarg;
  3612. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3613. err = host_to_target_semid_ds(target_su.buf, &dsarg);
  3614. if (err)
  3615. return err;
  3616. break;
  3617. case IPC_INFO:
  3618. case SEM_INFO:
  3619. arg.__buf = &seminfo;
  3620. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3621. err = host_to_target_seminfo(target_su.__buf, &seminfo);
  3622. if (err)
  3623. return err;
  3624. break;
  3625. case IPC_RMID:
  3626. case GETPID:
  3627. case GETNCNT:
  3628. case GETZCNT:
  3629. ret = get_errno(semctl(semid, semnum, cmd, NULL));
  3630. break;
  3631. }
  3632. return ret;
  3633. }
  3634. struct target_sembuf {
  3635. unsigned short sem_num;
  3636. short sem_op;
  3637. short sem_flg;
  3638. };
  3639. static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
  3640. abi_ulong target_addr,
  3641. unsigned nsops)
  3642. {
  3643. struct target_sembuf *target_sembuf;
  3644. int i;
  3645. target_sembuf = lock_user(VERIFY_READ, target_addr,
  3646. nsops*sizeof(struct target_sembuf), 1);
  3647. if (!target_sembuf)
  3648. return -TARGET_EFAULT;
  3649. for(i=0; i<nsops; i++) {
  3650. __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
  3651. __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
  3652. __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
  3653. }
  3654. unlock_user(target_sembuf, target_addr, 0);
  3655. return 0;
  3656. }
  3657. #if defined(TARGET_NR_ipc) || defined(TARGET_NR_semop) || \
  3658. defined(TARGET_NR_semtimedop) || defined(TARGET_NR_semtimedop_time64)
  3659. /*
  3660. * This macro is required to handle the s390 variants, which passes the
  3661. * arguments in a different order than default.
  3662. */
  3663. #ifdef __s390x__
  3664. #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
  3665. (__nsops), (__timeout), (__sops)
  3666. #else
  3667. #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
  3668. (__nsops), 0, (__sops), (__timeout)
  3669. #endif
  3670. static inline abi_long do_semtimedop(int semid,
  3671. abi_long ptr,
  3672. unsigned nsops,
  3673. abi_long timeout, bool time64)
  3674. {
  3675. struct sembuf *sops;
  3676. struct timespec ts, *pts = NULL;
  3677. abi_long ret;
  3678. if (timeout) {
  3679. pts = &ts;
  3680. if (time64) {
  3681. if (target_to_host_timespec64(pts, timeout)) {
  3682. return -TARGET_EFAULT;
  3683. }
  3684. } else {
  3685. if (target_to_host_timespec(pts, timeout)) {
  3686. return -TARGET_EFAULT;
  3687. }
  3688. }
  3689. }
  3690. if (nsops > TARGET_SEMOPM) {
  3691. return -TARGET_E2BIG;
  3692. }
  3693. sops = g_new(struct sembuf, nsops);
  3694. if (target_to_host_sembuf(sops, ptr, nsops)) {
  3695. g_free(sops);
  3696. return -TARGET_EFAULT;
  3697. }
  3698. ret = -TARGET_ENOSYS;
  3699. #ifdef __NR_semtimedop
  3700. ret = get_errno(safe_semtimedop(semid, sops, nsops, pts));
  3701. #endif
  3702. #ifdef __NR_ipc
  3703. if (ret == -TARGET_ENOSYS) {
  3704. ret = get_errno(safe_ipc(IPCOP_semtimedop, semid,
  3705. SEMTIMEDOP_IPC_ARGS(nsops, sops, (long)pts)));
  3706. }
  3707. #endif
  3708. g_free(sops);
  3709. return ret;
  3710. }
  3711. #endif
  3712. struct target_msqid_ds
  3713. {
  3714. struct target_ipc_perm msg_perm;
  3715. abi_ulong msg_stime;
  3716. #if TARGET_ABI_BITS == 32
  3717. abi_ulong __unused1;
  3718. #endif
  3719. abi_ulong msg_rtime;
  3720. #if TARGET_ABI_BITS == 32
  3721. abi_ulong __unused2;
  3722. #endif
  3723. abi_ulong msg_ctime;
  3724. #if TARGET_ABI_BITS == 32
  3725. abi_ulong __unused3;
  3726. #endif
  3727. abi_ulong __msg_cbytes;
  3728. abi_ulong msg_qnum;
  3729. abi_ulong msg_qbytes;
  3730. abi_ulong msg_lspid;
  3731. abi_ulong msg_lrpid;
  3732. abi_ulong __unused4;
  3733. abi_ulong __unused5;
  3734. };
  3735. static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
  3736. abi_ulong target_addr)
  3737. {
  3738. struct target_msqid_ds *target_md;
  3739. if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
  3740. return -TARGET_EFAULT;
  3741. if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
  3742. return -TARGET_EFAULT;
  3743. host_md->msg_stime = tswapal(target_md->msg_stime);
  3744. host_md->msg_rtime = tswapal(target_md->msg_rtime);
  3745. host_md->msg_ctime = tswapal(target_md->msg_ctime);
  3746. host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
  3747. host_md->msg_qnum = tswapal(target_md->msg_qnum);
  3748. host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
  3749. host_md->msg_lspid = tswapal(target_md->msg_lspid);
  3750. host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
  3751. unlock_user_struct(target_md, target_addr, 0);
  3752. return 0;
  3753. }
  3754. static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
  3755. struct msqid_ds *host_md)
  3756. {
  3757. struct target_msqid_ds *target_md;
  3758. if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
  3759. return -TARGET_EFAULT;
  3760. if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
  3761. return -TARGET_EFAULT;
  3762. target_md->msg_stime = tswapal(host_md->msg_stime);
  3763. target_md->msg_rtime = tswapal(host_md->msg_rtime);
  3764. target_md->msg_ctime = tswapal(host_md->msg_ctime);
  3765. target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
  3766. target_md->msg_qnum = tswapal(host_md->msg_qnum);
  3767. target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
  3768. target_md->msg_lspid = tswapal(host_md->msg_lspid);
  3769. target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
  3770. unlock_user_struct(target_md, target_addr, 1);
  3771. return 0;
  3772. }
  3773. struct target_msginfo {
  3774. int msgpool;
  3775. int msgmap;
  3776. int msgmax;
  3777. int msgmnb;
  3778. int msgmni;
  3779. int msgssz;
  3780. int msgtql;
  3781. unsigned short int msgseg;
  3782. };
  3783. static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
  3784. struct msginfo *host_msginfo)
  3785. {
  3786. struct target_msginfo *target_msginfo;
  3787. if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
  3788. return -TARGET_EFAULT;
  3789. __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
  3790. __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
  3791. __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
  3792. __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
  3793. __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
  3794. __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
  3795. __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
  3796. __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
  3797. unlock_user_struct(target_msginfo, target_addr, 1);
  3798. return 0;
  3799. }
  3800. static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
  3801. {
  3802. struct msqid_ds dsarg;
  3803. struct msginfo msginfo;
  3804. abi_long ret = -TARGET_EINVAL;
  3805. cmd &= 0xff;
  3806. switch (cmd) {
  3807. case IPC_STAT:
  3808. case IPC_SET:
  3809. case MSG_STAT:
  3810. if (target_to_host_msqid_ds(&dsarg,ptr))
  3811. return -TARGET_EFAULT;
  3812. ret = get_errno(msgctl(msgid, cmd, &dsarg));
  3813. if (host_to_target_msqid_ds(ptr,&dsarg))
  3814. return -TARGET_EFAULT;
  3815. break;
  3816. case IPC_RMID:
  3817. ret = get_errno(msgctl(msgid, cmd, NULL));
  3818. break;
  3819. case IPC_INFO:
  3820. case MSG_INFO:
  3821. ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
  3822. if (host_to_target_msginfo(ptr, &msginfo))
  3823. return -TARGET_EFAULT;
  3824. break;
  3825. }
  3826. return ret;
  3827. }
  3828. struct target_msgbuf {
  3829. abi_long mtype;
  3830. char mtext[1];
  3831. };
  3832. static inline abi_long do_msgsnd(int msqid, abi_long msgp,
  3833. ssize_t msgsz, int msgflg)
  3834. {
  3835. struct target_msgbuf *target_mb;
  3836. struct msgbuf *host_mb;
  3837. abi_long ret = 0;
  3838. if (msgsz < 0) {
  3839. return -TARGET_EINVAL;
  3840. }
  3841. if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
  3842. return -TARGET_EFAULT;
  3843. host_mb = g_try_malloc(msgsz + sizeof(long));
  3844. if (!host_mb) {
  3845. unlock_user_struct(target_mb, msgp, 0);
  3846. return -TARGET_ENOMEM;
  3847. }
  3848. host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
  3849. memcpy(host_mb->mtext, target_mb->mtext, msgsz);
  3850. ret = -TARGET_ENOSYS;
  3851. #ifdef __NR_msgsnd
  3852. ret = get_errno(safe_msgsnd(msqid, host_mb, msgsz, msgflg));
  3853. #endif
  3854. #ifdef __NR_ipc
  3855. if (ret == -TARGET_ENOSYS) {
  3856. #ifdef __s390x__
  3857. ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
  3858. host_mb));
  3859. #else
  3860. ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
  3861. host_mb, 0));
  3862. #endif
  3863. }
  3864. #endif
  3865. g_free(host_mb);
  3866. unlock_user_struct(target_mb, msgp, 0);
  3867. return ret;
  3868. }
  3869. #ifdef __NR_ipc
  3870. #if defined(__sparc__)
  3871. /* SPARC for msgrcv it does not use the kludge on final 2 arguments. */
  3872. #define MSGRCV_ARGS(__msgp, __msgtyp) __msgp, __msgtyp
  3873. #elif defined(__s390x__)
  3874. /* The s390 sys_ipc variant has only five parameters. */
  3875. #define MSGRCV_ARGS(__msgp, __msgtyp) \
  3876. ((long int[]){(long int)__msgp, __msgtyp})
  3877. #else
  3878. #define MSGRCV_ARGS(__msgp, __msgtyp) \
  3879. ((long int[]){(long int)__msgp, __msgtyp}), 0
  3880. #endif
  3881. #endif
  3882. static inline abi_long do_msgrcv(int msqid, abi_long msgp,
  3883. ssize_t msgsz, abi_long msgtyp,
  3884. int msgflg)
  3885. {
  3886. struct target_msgbuf *target_mb;
  3887. char *target_mtext;
  3888. struct msgbuf *host_mb;
  3889. abi_long ret = 0;
  3890. if (msgsz < 0) {
  3891. return -TARGET_EINVAL;
  3892. }
  3893. if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
  3894. return -TARGET_EFAULT;
  3895. host_mb = g_try_malloc(msgsz + sizeof(long));
  3896. if (!host_mb) {
  3897. ret = -TARGET_ENOMEM;
  3898. goto end;
  3899. }
  3900. ret = -TARGET_ENOSYS;
  3901. #ifdef __NR_msgrcv
  3902. ret = get_errno(safe_msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
  3903. #endif
  3904. #ifdef __NR_ipc
  3905. if (ret == -TARGET_ENOSYS) {
  3906. ret = get_errno(safe_ipc(IPCOP_CALL(1, IPCOP_msgrcv), msqid, msgsz,
  3907. msgflg, MSGRCV_ARGS(host_mb, msgtyp)));
  3908. }
  3909. #endif
  3910. if (ret > 0) {
  3911. abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
  3912. target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
  3913. if (!target_mtext) {
  3914. ret = -TARGET_EFAULT;
  3915. goto end;
  3916. }
  3917. memcpy(target_mb->mtext, host_mb->mtext, ret);
  3918. unlock_user(target_mtext, target_mtext_addr, ret);
  3919. }
  3920. target_mb->mtype = tswapal(host_mb->mtype);
  3921. end:
  3922. if (target_mb)
  3923. unlock_user_struct(target_mb, msgp, 1);
  3924. g_free(host_mb);
  3925. return ret;
  3926. }
  3927. static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
  3928. abi_ulong target_addr)
  3929. {
  3930. struct target_shmid_ds *target_sd;
  3931. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  3932. return -TARGET_EFAULT;
  3933. if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
  3934. return -TARGET_EFAULT;
  3935. __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
  3936. __get_user(host_sd->shm_atime, &target_sd->shm_atime);
  3937. __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
  3938. __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
  3939. __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
  3940. __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
  3941. __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
  3942. unlock_user_struct(target_sd, target_addr, 0);
  3943. return 0;
  3944. }
  3945. static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
  3946. struct shmid_ds *host_sd)
  3947. {
  3948. struct target_shmid_ds *target_sd;
  3949. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  3950. return -TARGET_EFAULT;
  3951. if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
  3952. return -TARGET_EFAULT;
  3953. __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
  3954. __put_user(host_sd->shm_atime, &target_sd->shm_atime);
  3955. __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
  3956. __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
  3957. __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
  3958. __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
  3959. __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
  3960. unlock_user_struct(target_sd, target_addr, 1);
  3961. return 0;
  3962. }
  3963. struct target_shminfo {
  3964. abi_ulong shmmax;
  3965. abi_ulong shmmin;
  3966. abi_ulong shmmni;
  3967. abi_ulong shmseg;
  3968. abi_ulong shmall;
  3969. };
  3970. static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
  3971. struct shminfo *host_shminfo)
  3972. {
  3973. struct target_shminfo *target_shminfo;
  3974. if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
  3975. return -TARGET_EFAULT;
  3976. __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
  3977. __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
  3978. __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
  3979. __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
  3980. __put_user(host_shminfo->shmall, &target_shminfo->shmall);
  3981. unlock_user_struct(target_shminfo, target_addr, 1);
  3982. return 0;
  3983. }
  3984. struct target_shm_info {
  3985. int used_ids;
  3986. abi_ulong shm_tot;
  3987. abi_ulong shm_rss;
  3988. abi_ulong shm_swp;
  3989. abi_ulong swap_attempts;
  3990. abi_ulong swap_successes;
  3991. };
  3992. static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
  3993. struct shm_info *host_shm_info)
  3994. {
  3995. struct target_shm_info *target_shm_info;
  3996. if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
  3997. return -TARGET_EFAULT;
  3998. __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
  3999. __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
  4000. __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
  4001. __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
  4002. __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
  4003. __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
  4004. unlock_user_struct(target_shm_info, target_addr, 1);
  4005. return 0;
  4006. }
  4007. static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
  4008. {
  4009. struct shmid_ds dsarg;
  4010. struct shminfo shminfo;
  4011. struct shm_info shm_info;
  4012. abi_long ret = -TARGET_EINVAL;
  4013. cmd &= 0xff;
  4014. switch(cmd) {
  4015. case IPC_STAT:
  4016. case IPC_SET:
  4017. case SHM_STAT:
  4018. if (target_to_host_shmid_ds(&dsarg, buf))
  4019. return -TARGET_EFAULT;
  4020. ret = get_errno(shmctl(shmid, cmd, &dsarg));
  4021. if (host_to_target_shmid_ds(buf, &dsarg))
  4022. return -TARGET_EFAULT;
  4023. break;
  4024. case IPC_INFO:
  4025. ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
  4026. if (host_to_target_shminfo(buf, &shminfo))
  4027. return -TARGET_EFAULT;
  4028. break;
  4029. case SHM_INFO:
  4030. ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
  4031. if (host_to_target_shm_info(buf, &shm_info))
  4032. return -TARGET_EFAULT;
  4033. break;
  4034. case IPC_RMID:
  4035. case SHM_LOCK:
  4036. case SHM_UNLOCK:
  4037. ret = get_errno(shmctl(shmid, cmd, NULL));
  4038. break;
  4039. }
  4040. return ret;
  4041. }
  4042. #ifndef TARGET_FORCE_SHMLBA
  4043. /* For most architectures, SHMLBA is the same as the page size;
  4044. * some architectures have larger values, in which case they should
  4045. * define TARGET_FORCE_SHMLBA and provide a target_shmlba() function.
  4046. * This corresponds to the kernel arch code defining __ARCH_FORCE_SHMLBA
  4047. * and defining its own value for SHMLBA.
  4048. *
  4049. * The kernel also permits SHMLBA to be set by the architecture to a
  4050. * value larger than the page size without setting __ARCH_FORCE_SHMLBA;
  4051. * this means that addresses are rounded to the large size if
  4052. * SHM_RND is set but addresses not aligned to that size are not rejected
  4053. * as long as they are at least page-aligned. Since the only architecture
  4054. * which uses this is ia64 this code doesn't provide for that oddity.
  4055. */
  4056. static inline abi_ulong target_shmlba(CPUArchState *cpu_env)
  4057. {
  4058. return TARGET_PAGE_SIZE;
  4059. }
  4060. #endif
  4061. static abi_ulong do_shmat(CPUArchState *cpu_env, int shmid,
  4062. abi_ulong shmaddr, int shmflg)
  4063. {
  4064. CPUState *cpu = env_cpu(cpu_env);
  4065. abi_ulong raddr;
  4066. void *host_raddr;
  4067. struct shmid_ds shm_info;
  4068. int i, ret;
  4069. abi_ulong shmlba;
  4070. /* shmat pointers are always untagged */
  4071. /* find out the length of the shared memory segment */
  4072. ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
  4073. if (is_error(ret)) {
  4074. /* can't get length, bail out */
  4075. return ret;
  4076. }
  4077. shmlba = target_shmlba(cpu_env);
  4078. if (shmaddr & (shmlba - 1)) {
  4079. if (shmflg & SHM_RND) {
  4080. shmaddr &= ~(shmlba - 1);
  4081. } else {
  4082. return -TARGET_EINVAL;
  4083. }
  4084. }
  4085. if (!guest_range_valid_untagged(shmaddr, shm_info.shm_segsz)) {
  4086. return -TARGET_EINVAL;
  4087. }
  4088. mmap_lock();
  4089. /*
  4090. * We're mapping shared memory, so ensure we generate code for parallel
  4091. * execution and flush old translations. This will work up to the level
  4092. * supported by the host -- anything that requires EXCP_ATOMIC will not
  4093. * be atomic with respect to an external process.
  4094. */
  4095. if (!(cpu->tcg_cflags & CF_PARALLEL)) {
  4096. cpu->tcg_cflags |= CF_PARALLEL;
  4097. tb_flush(cpu);
  4098. }
  4099. if (shmaddr)
  4100. host_raddr = shmat(shmid, (void *)g2h_untagged(shmaddr), shmflg);
  4101. else {
  4102. abi_ulong mmap_start;
  4103. /* In order to use the host shmat, we need to honor host SHMLBA. */
  4104. mmap_start = mmap_find_vma(0, shm_info.shm_segsz, MAX(SHMLBA, shmlba));
  4105. if (mmap_start == -1) {
  4106. errno = ENOMEM;
  4107. host_raddr = (void *)-1;
  4108. } else
  4109. host_raddr = shmat(shmid, g2h_untagged(mmap_start),
  4110. shmflg | SHM_REMAP);
  4111. }
  4112. if (host_raddr == (void *)-1) {
  4113. mmap_unlock();
  4114. return get_errno((intptr_t)host_raddr);
  4115. }
  4116. raddr = h2g((uintptr_t)host_raddr);
  4117. page_set_flags(raddr, raddr + shm_info.shm_segsz - 1,
  4118. PAGE_VALID | PAGE_RESET | PAGE_READ |
  4119. (shmflg & SHM_RDONLY ? 0 : PAGE_WRITE));
  4120. for (i = 0; i < N_SHM_REGIONS; i++) {
  4121. if (!shm_regions[i].in_use) {
  4122. shm_regions[i].in_use = true;
  4123. shm_regions[i].start = raddr;
  4124. shm_regions[i].size = shm_info.shm_segsz;
  4125. break;
  4126. }
  4127. }
  4128. mmap_unlock();
  4129. return raddr;
  4130. }
  4131. static inline abi_long do_shmdt(abi_ulong shmaddr)
  4132. {
  4133. int i;
  4134. abi_long rv;
  4135. /* shmdt pointers are always untagged */
  4136. mmap_lock();
  4137. for (i = 0; i < N_SHM_REGIONS; ++i) {
  4138. if (shm_regions[i].in_use && shm_regions[i].start == shmaddr) {
  4139. shm_regions[i].in_use = false;
  4140. page_set_flags(shmaddr, shmaddr + shm_regions[i].size - 1, 0);
  4141. break;
  4142. }
  4143. }
  4144. rv = get_errno(shmdt(g2h_untagged(shmaddr)));
  4145. mmap_unlock();
  4146. return rv;
  4147. }
  4148. #ifdef TARGET_NR_ipc
  4149. /* ??? This only works with linear mappings. */
  4150. /* do_ipc() must return target values and target errnos. */
  4151. static abi_long do_ipc(CPUArchState *cpu_env,
  4152. unsigned int call, abi_long first,
  4153. abi_long second, abi_long third,
  4154. abi_long ptr, abi_long fifth)
  4155. {
  4156. int version;
  4157. abi_long ret = 0;
  4158. version = call >> 16;
  4159. call &= 0xffff;
  4160. switch (call) {
  4161. case IPCOP_semop:
  4162. ret = do_semtimedop(first, ptr, second, 0, false);
  4163. break;
  4164. case IPCOP_semtimedop:
  4165. /*
  4166. * The s390 sys_ipc variant has only five parameters instead of six
  4167. * (as for default variant) and the only difference is the handling of
  4168. * SEMTIMEDOP where on s390 the third parameter is used as a pointer
  4169. * to a struct timespec where the generic variant uses fifth parameter.
  4170. */
  4171. #if defined(TARGET_S390X)
  4172. ret = do_semtimedop(first, ptr, second, third, TARGET_ABI_BITS == 64);
  4173. #else
  4174. ret = do_semtimedop(first, ptr, second, fifth, TARGET_ABI_BITS == 64);
  4175. #endif
  4176. break;
  4177. case IPCOP_semget:
  4178. ret = get_errno(semget(first, second, third));
  4179. break;
  4180. case IPCOP_semctl: {
  4181. /* The semun argument to semctl is passed by value, so dereference the
  4182. * ptr argument. */
  4183. abi_ulong atptr;
  4184. get_user_ual(atptr, ptr);
  4185. ret = do_semctl(first, second, third, atptr);
  4186. break;
  4187. }
  4188. case IPCOP_msgget:
  4189. ret = get_errno(msgget(first, second));
  4190. break;
  4191. case IPCOP_msgsnd:
  4192. ret = do_msgsnd(first, ptr, second, third);
  4193. break;
  4194. case IPCOP_msgctl:
  4195. ret = do_msgctl(first, second, ptr);
  4196. break;
  4197. case IPCOP_msgrcv:
  4198. switch (version) {
  4199. case 0:
  4200. {
  4201. struct target_ipc_kludge {
  4202. abi_long msgp;
  4203. abi_long msgtyp;
  4204. } *tmp;
  4205. if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
  4206. ret = -TARGET_EFAULT;
  4207. break;
  4208. }
  4209. ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
  4210. unlock_user_struct(tmp, ptr, 0);
  4211. break;
  4212. }
  4213. default:
  4214. ret = do_msgrcv(first, ptr, second, fifth, third);
  4215. }
  4216. break;
  4217. case IPCOP_shmat:
  4218. switch (version) {
  4219. default:
  4220. {
  4221. abi_ulong raddr;
  4222. raddr = do_shmat(cpu_env, first, ptr, second);
  4223. if (is_error(raddr))
  4224. return get_errno(raddr);
  4225. if (put_user_ual(raddr, third))
  4226. return -TARGET_EFAULT;
  4227. break;
  4228. }
  4229. case 1:
  4230. ret = -TARGET_EINVAL;
  4231. break;
  4232. }
  4233. break;
  4234. case IPCOP_shmdt:
  4235. ret = do_shmdt(ptr);
  4236. break;
  4237. case IPCOP_shmget:
  4238. /* IPC_* flag values are the same on all linux platforms */
  4239. ret = get_errno(shmget(first, second, third));
  4240. break;
  4241. /* IPC_* and SHM_* command values are the same on all linux platforms */
  4242. case IPCOP_shmctl:
  4243. ret = do_shmctl(first, second, ptr);
  4244. break;
  4245. default:
  4246. qemu_log_mask(LOG_UNIMP, "Unsupported ipc call: %d (version %d)\n",
  4247. call, version);
  4248. ret = -TARGET_ENOSYS;
  4249. break;
  4250. }
  4251. return ret;
  4252. }
  4253. #endif
  4254. /* kernel structure types definitions */
  4255. #define STRUCT(name, ...) STRUCT_ ## name,
  4256. #define STRUCT_SPECIAL(name) STRUCT_ ## name,
  4257. enum {
  4258. #include "syscall_types.h"
  4259. STRUCT_MAX
  4260. };
  4261. #undef STRUCT
  4262. #undef STRUCT_SPECIAL
  4263. #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
  4264. #define STRUCT_SPECIAL(name)
  4265. #include "syscall_types.h"
  4266. #undef STRUCT
  4267. #undef STRUCT_SPECIAL
  4268. #define MAX_STRUCT_SIZE 4096
  4269. #ifdef CONFIG_FIEMAP
  4270. /* So fiemap access checks don't overflow on 32 bit systems.
  4271. * This is very slightly smaller than the limit imposed by
  4272. * the underlying kernel.
  4273. */
  4274. #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
  4275. / sizeof(struct fiemap_extent))
  4276. static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
  4277. int fd, int cmd, abi_long arg)
  4278. {
  4279. /* The parameter for this ioctl is a struct fiemap followed
  4280. * by an array of struct fiemap_extent whose size is set
  4281. * in fiemap->fm_extent_count. The array is filled in by the
  4282. * ioctl.
  4283. */
  4284. int target_size_in, target_size_out;
  4285. struct fiemap *fm;
  4286. const argtype *arg_type = ie->arg_type;
  4287. const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
  4288. void *argptr, *p;
  4289. abi_long ret;
  4290. int i, extent_size = thunk_type_size(extent_arg_type, 0);
  4291. uint32_t outbufsz;
  4292. int free_fm = 0;
  4293. assert(arg_type[0] == TYPE_PTR);
  4294. assert(ie->access == IOC_RW);
  4295. arg_type++;
  4296. target_size_in = thunk_type_size(arg_type, 0);
  4297. argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
  4298. if (!argptr) {
  4299. return -TARGET_EFAULT;
  4300. }
  4301. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4302. unlock_user(argptr, arg, 0);
  4303. fm = (struct fiemap *)buf_temp;
  4304. if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
  4305. return -TARGET_EINVAL;
  4306. }
  4307. outbufsz = sizeof (*fm) +
  4308. (sizeof(struct fiemap_extent) * fm->fm_extent_count);
  4309. if (outbufsz > MAX_STRUCT_SIZE) {
  4310. /* We can't fit all the extents into the fixed size buffer.
  4311. * Allocate one that is large enough and use it instead.
  4312. */
  4313. fm = g_try_malloc(outbufsz);
  4314. if (!fm) {
  4315. return -TARGET_ENOMEM;
  4316. }
  4317. memcpy(fm, buf_temp, sizeof(struct fiemap));
  4318. free_fm = 1;
  4319. }
  4320. ret = get_errno(safe_ioctl(fd, ie->host_cmd, fm));
  4321. if (!is_error(ret)) {
  4322. target_size_out = target_size_in;
  4323. /* An extent_count of 0 means we were only counting the extents
  4324. * so there are no structs to copy
  4325. */
  4326. if (fm->fm_extent_count != 0) {
  4327. target_size_out += fm->fm_mapped_extents * extent_size;
  4328. }
  4329. argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
  4330. if (!argptr) {
  4331. ret = -TARGET_EFAULT;
  4332. } else {
  4333. /* Convert the struct fiemap */
  4334. thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
  4335. if (fm->fm_extent_count != 0) {
  4336. p = argptr + target_size_in;
  4337. /* ...and then all the struct fiemap_extents */
  4338. for (i = 0; i < fm->fm_mapped_extents; i++) {
  4339. thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
  4340. THUNK_TARGET);
  4341. p += extent_size;
  4342. }
  4343. }
  4344. unlock_user(argptr, arg, target_size_out);
  4345. }
  4346. }
  4347. if (free_fm) {
  4348. g_free(fm);
  4349. }
  4350. return ret;
  4351. }
  4352. #endif
  4353. static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
  4354. int fd, int cmd, abi_long arg)
  4355. {
  4356. const argtype *arg_type = ie->arg_type;
  4357. int target_size;
  4358. void *argptr;
  4359. int ret;
  4360. struct ifconf *host_ifconf;
  4361. uint32_t outbufsz;
  4362. const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
  4363. const argtype ifreq_max_type[] = { MK_STRUCT(STRUCT_ifmap_ifreq) };
  4364. int target_ifreq_size;
  4365. int nb_ifreq;
  4366. int free_buf = 0;
  4367. int i;
  4368. int target_ifc_len;
  4369. abi_long target_ifc_buf;
  4370. int host_ifc_len;
  4371. char *host_ifc_buf;
  4372. assert(arg_type[0] == TYPE_PTR);
  4373. assert(ie->access == IOC_RW);
  4374. arg_type++;
  4375. target_size = thunk_type_size(arg_type, 0);
  4376. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4377. if (!argptr)
  4378. return -TARGET_EFAULT;
  4379. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4380. unlock_user(argptr, arg, 0);
  4381. host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
  4382. target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
  4383. target_ifreq_size = thunk_type_size(ifreq_max_type, 0);
  4384. if (target_ifc_buf != 0) {
  4385. target_ifc_len = host_ifconf->ifc_len;
  4386. nb_ifreq = target_ifc_len / target_ifreq_size;
  4387. host_ifc_len = nb_ifreq * sizeof(struct ifreq);
  4388. outbufsz = sizeof(*host_ifconf) + host_ifc_len;
  4389. if (outbufsz > MAX_STRUCT_SIZE) {
  4390. /*
  4391. * We can't fit all the extents into the fixed size buffer.
  4392. * Allocate one that is large enough and use it instead.
  4393. */
  4394. host_ifconf = g_try_malloc(outbufsz);
  4395. if (!host_ifconf) {
  4396. return -TARGET_ENOMEM;
  4397. }
  4398. memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
  4399. free_buf = 1;
  4400. }
  4401. host_ifc_buf = (char *)host_ifconf + sizeof(*host_ifconf);
  4402. host_ifconf->ifc_len = host_ifc_len;
  4403. } else {
  4404. host_ifc_buf = NULL;
  4405. }
  4406. host_ifconf->ifc_buf = host_ifc_buf;
  4407. ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_ifconf));
  4408. if (!is_error(ret)) {
  4409. /* convert host ifc_len to target ifc_len */
  4410. nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
  4411. target_ifc_len = nb_ifreq * target_ifreq_size;
  4412. host_ifconf->ifc_len = target_ifc_len;
  4413. /* restore target ifc_buf */
  4414. host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
  4415. /* copy struct ifconf to target user */
  4416. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4417. if (!argptr)
  4418. return -TARGET_EFAULT;
  4419. thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
  4420. unlock_user(argptr, arg, target_size);
  4421. if (target_ifc_buf != 0) {
  4422. /* copy ifreq[] to target user */
  4423. argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
  4424. for (i = 0; i < nb_ifreq ; i++) {
  4425. thunk_convert(argptr + i * target_ifreq_size,
  4426. host_ifc_buf + i * sizeof(struct ifreq),
  4427. ifreq_arg_type, THUNK_TARGET);
  4428. }
  4429. unlock_user(argptr, target_ifc_buf, target_ifc_len);
  4430. }
  4431. }
  4432. if (free_buf) {
  4433. g_free(host_ifconf);
  4434. }
  4435. return ret;
  4436. }
  4437. #if defined(CONFIG_USBFS)
  4438. #if HOST_LONG_BITS > 64
  4439. #error USBDEVFS thunks do not support >64 bit hosts yet.
  4440. #endif
  4441. struct live_urb {
  4442. uint64_t target_urb_adr;
  4443. uint64_t target_buf_adr;
  4444. char *target_buf_ptr;
  4445. struct usbdevfs_urb host_urb;
  4446. };
  4447. static GHashTable *usbdevfs_urb_hashtable(void)
  4448. {
  4449. static GHashTable *urb_hashtable;
  4450. if (!urb_hashtable) {
  4451. urb_hashtable = g_hash_table_new(g_int64_hash, g_int64_equal);
  4452. }
  4453. return urb_hashtable;
  4454. }
  4455. static void urb_hashtable_insert(struct live_urb *urb)
  4456. {
  4457. GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
  4458. g_hash_table_insert(urb_hashtable, urb, urb);
  4459. }
  4460. static struct live_urb *urb_hashtable_lookup(uint64_t target_urb_adr)
  4461. {
  4462. GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
  4463. return g_hash_table_lookup(urb_hashtable, &target_urb_adr);
  4464. }
  4465. static void urb_hashtable_remove(struct live_urb *urb)
  4466. {
  4467. GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
  4468. g_hash_table_remove(urb_hashtable, urb);
  4469. }
  4470. static abi_long
  4471. do_ioctl_usbdevfs_reapurb(const IOCTLEntry *ie, uint8_t *buf_temp,
  4472. int fd, int cmd, abi_long arg)
  4473. {
  4474. const argtype usbfsurb_arg_type[] = { MK_STRUCT(STRUCT_usbdevfs_urb) };
  4475. const argtype ptrvoid_arg_type[] = { TYPE_PTRVOID, 0, 0 };
  4476. struct live_urb *lurb;
  4477. void *argptr;
  4478. uint64_t hurb;
  4479. int target_size;
  4480. uintptr_t target_urb_adr;
  4481. abi_long ret;
  4482. target_size = thunk_type_size(usbfsurb_arg_type, THUNK_TARGET);
  4483. memset(buf_temp, 0, sizeof(uint64_t));
  4484. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4485. if (is_error(ret)) {
  4486. return ret;
  4487. }
  4488. memcpy(&hurb, buf_temp, sizeof(uint64_t));
  4489. lurb = (void *)((uintptr_t)hurb - offsetof(struct live_urb, host_urb));
  4490. if (!lurb->target_urb_adr) {
  4491. return -TARGET_EFAULT;
  4492. }
  4493. urb_hashtable_remove(lurb);
  4494. unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr,
  4495. lurb->host_urb.buffer_length);
  4496. lurb->target_buf_ptr = NULL;
  4497. /* restore the guest buffer pointer */
  4498. lurb->host_urb.buffer = (void *)(uintptr_t)lurb->target_buf_adr;
  4499. /* update the guest urb struct */
  4500. argptr = lock_user(VERIFY_WRITE, lurb->target_urb_adr, target_size, 0);
  4501. if (!argptr) {
  4502. g_free(lurb);
  4503. return -TARGET_EFAULT;
  4504. }
  4505. thunk_convert(argptr, &lurb->host_urb, usbfsurb_arg_type, THUNK_TARGET);
  4506. unlock_user(argptr, lurb->target_urb_adr, target_size);
  4507. target_size = thunk_type_size(ptrvoid_arg_type, THUNK_TARGET);
  4508. /* write back the urb handle */
  4509. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4510. if (!argptr) {
  4511. g_free(lurb);
  4512. return -TARGET_EFAULT;
  4513. }
  4514. /* GHashTable uses 64-bit keys but thunk_convert expects uintptr_t */
  4515. target_urb_adr = lurb->target_urb_adr;
  4516. thunk_convert(argptr, &target_urb_adr, ptrvoid_arg_type, THUNK_TARGET);
  4517. unlock_user(argptr, arg, target_size);
  4518. g_free(lurb);
  4519. return ret;
  4520. }
  4521. static abi_long
  4522. do_ioctl_usbdevfs_discardurb(const IOCTLEntry *ie,
  4523. uint8_t *buf_temp __attribute__((unused)),
  4524. int fd, int cmd, abi_long arg)
  4525. {
  4526. struct live_urb *lurb;
  4527. /* map target address back to host URB with metadata. */
  4528. lurb = urb_hashtable_lookup(arg);
  4529. if (!lurb) {
  4530. return -TARGET_EFAULT;
  4531. }
  4532. return get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb));
  4533. }
  4534. static abi_long
  4535. do_ioctl_usbdevfs_submiturb(const IOCTLEntry *ie, uint8_t *buf_temp,
  4536. int fd, int cmd, abi_long arg)
  4537. {
  4538. const argtype *arg_type = ie->arg_type;
  4539. int target_size;
  4540. abi_long ret;
  4541. void *argptr;
  4542. int rw_dir;
  4543. struct live_urb *lurb;
  4544. /*
  4545. * each submitted URB needs to map to a unique ID for the
  4546. * kernel, and that unique ID needs to be a pointer to
  4547. * host memory. hence, we need to malloc for each URB.
  4548. * isochronous transfers have a variable length struct.
  4549. */
  4550. arg_type++;
  4551. target_size = thunk_type_size(arg_type, THUNK_TARGET);
  4552. /* construct host copy of urb and metadata */
  4553. lurb = g_try_new0(struct live_urb, 1);
  4554. if (!lurb) {
  4555. return -TARGET_ENOMEM;
  4556. }
  4557. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4558. if (!argptr) {
  4559. g_free(lurb);
  4560. return -TARGET_EFAULT;
  4561. }
  4562. thunk_convert(&lurb->host_urb, argptr, arg_type, THUNK_HOST);
  4563. unlock_user(argptr, arg, 0);
  4564. lurb->target_urb_adr = arg;
  4565. lurb->target_buf_adr = (uintptr_t)lurb->host_urb.buffer;
  4566. /* buffer space used depends on endpoint type so lock the entire buffer */
  4567. /* control type urbs should check the buffer contents for true direction */
  4568. rw_dir = lurb->host_urb.endpoint & USB_DIR_IN ? VERIFY_WRITE : VERIFY_READ;
  4569. lurb->target_buf_ptr = lock_user(rw_dir, lurb->target_buf_adr,
  4570. lurb->host_urb.buffer_length, 1);
  4571. if (lurb->target_buf_ptr == NULL) {
  4572. g_free(lurb);
  4573. return -TARGET_EFAULT;
  4574. }
  4575. /* update buffer pointer in host copy */
  4576. lurb->host_urb.buffer = lurb->target_buf_ptr;
  4577. ret = get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb));
  4578. if (is_error(ret)) {
  4579. unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr, 0);
  4580. g_free(lurb);
  4581. } else {
  4582. urb_hashtable_insert(lurb);
  4583. }
  4584. return ret;
  4585. }
  4586. #endif /* CONFIG_USBFS */
  4587. static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
  4588. int cmd, abi_long arg)
  4589. {
  4590. void *argptr;
  4591. struct dm_ioctl *host_dm;
  4592. abi_long guest_data;
  4593. uint32_t guest_data_size;
  4594. int target_size;
  4595. const argtype *arg_type = ie->arg_type;
  4596. abi_long ret;
  4597. void *big_buf = NULL;
  4598. char *host_data;
  4599. arg_type++;
  4600. target_size = thunk_type_size(arg_type, 0);
  4601. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4602. if (!argptr) {
  4603. ret = -TARGET_EFAULT;
  4604. goto out;
  4605. }
  4606. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4607. unlock_user(argptr, arg, 0);
  4608. /* buf_temp is too small, so fetch things into a bigger buffer */
  4609. big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
  4610. memcpy(big_buf, buf_temp, target_size);
  4611. buf_temp = big_buf;
  4612. host_dm = big_buf;
  4613. guest_data = arg + host_dm->data_start;
  4614. if ((guest_data - arg) < 0) {
  4615. ret = -TARGET_EINVAL;
  4616. goto out;
  4617. }
  4618. guest_data_size = host_dm->data_size - host_dm->data_start;
  4619. host_data = (char*)host_dm + host_dm->data_start;
  4620. argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
  4621. if (!argptr) {
  4622. ret = -TARGET_EFAULT;
  4623. goto out;
  4624. }
  4625. switch (ie->host_cmd) {
  4626. case DM_REMOVE_ALL:
  4627. case DM_LIST_DEVICES:
  4628. case DM_DEV_CREATE:
  4629. case DM_DEV_REMOVE:
  4630. case DM_DEV_SUSPEND:
  4631. case DM_DEV_STATUS:
  4632. case DM_DEV_WAIT:
  4633. case DM_TABLE_STATUS:
  4634. case DM_TABLE_CLEAR:
  4635. case DM_TABLE_DEPS:
  4636. case DM_LIST_VERSIONS:
  4637. /* no input data */
  4638. break;
  4639. case DM_DEV_RENAME:
  4640. case DM_DEV_SET_GEOMETRY:
  4641. /* data contains only strings */
  4642. memcpy(host_data, argptr, guest_data_size);
  4643. break;
  4644. case DM_TARGET_MSG:
  4645. memcpy(host_data, argptr, guest_data_size);
  4646. *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
  4647. break;
  4648. case DM_TABLE_LOAD:
  4649. {
  4650. void *gspec = argptr;
  4651. void *cur_data = host_data;
  4652. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
  4653. int spec_size = thunk_type_size(arg_type, 0);
  4654. int i;
  4655. for (i = 0; i < host_dm->target_count; i++) {
  4656. struct dm_target_spec *spec = cur_data;
  4657. uint32_t next;
  4658. int slen;
  4659. thunk_convert(spec, gspec, arg_type, THUNK_HOST);
  4660. slen = strlen((char*)gspec + spec_size) + 1;
  4661. next = spec->next;
  4662. spec->next = sizeof(*spec) + slen;
  4663. strcpy((char*)&spec[1], gspec + spec_size);
  4664. gspec += next;
  4665. cur_data += spec->next;
  4666. }
  4667. break;
  4668. }
  4669. default:
  4670. ret = -TARGET_EINVAL;
  4671. unlock_user(argptr, guest_data, 0);
  4672. goto out;
  4673. }
  4674. unlock_user(argptr, guest_data, 0);
  4675. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4676. if (!is_error(ret)) {
  4677. guest_data = arg + host_dm->data_start;
  4678. guest_data_size = host_dm->data_size - host_dm->data_start;
  4679. argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
  4680. switch (ie->host_cmd) {
  4681. case DM_REMOVE_ALL:
  4682. case DM_DEV_CREATE:
  4683. case DM_DEV_REMOVE:
  4684. case DM_DEV_RENAME:
  4685. case DM_DEV_SUSPEND:
  4686. case DM_DEV_STATUS:
  4687. case DM_TABLE_LOAD:
  4688. case DM_TABLE_CLEAR:
  4689. case DM_TARGET_MSG:
  4690. case DM_DEV_SET_GEOMETRY:
  4691. /* no return data */
  4692. break;
  4693. case DM_LIST_DEVICES:
  4694. {
  4695. struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
  4696. uint32_t remaining_data = guest_data_size;
  4697. void *cur_data = argptr;
  4698. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
  4699. int nl_size = 12; /* can't use thunk_size due to alignment */
  4700. while (1) {
  4701. uint32_t next = nl->next;
  4702. if (next) {
  4703. nl->next = nl_size + (strlen(nl->name) + 1);
  4704. }
  4705. if (remaining_data < nl->next) {
  4706. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  4707. break;
  4708. }
  4709. thunk_convert(cur_data, nl, arg_type, THUNK_TARGET);
  4710. strcpy(cur_data + nl_size, nl->name);
  4711. cur_data += nl->next;
  4712. remaining_data -= nl->next;
  4713. if (!next) {
  4714. break;
  4715. }
  4716. nl = (void*)nl + next;
  4717. }
  4718. break;
  4719. }
  4720. case DM_DEV_WAIT:
  4721. case DM_TABLE_STATUS:
  4722. {
  4723. struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
  4724. void *cur_data = argptr;
  4725. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
  4726. int spec_size = thunk_type_size(arg_type, 0);
  4727. int i;
  4728. for (i = 0; i < host_dm->target_count; i++) {
  4729. uint32_t next = spec->next;
  4730. int slen = strlen((char*)&spec[1]) + 1;
  4731. spec->next = (cur_data - argptr) + spec_size + slen;
  4732. if (guest_data_size < spec->next) {
  4733. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  4734. break;
  4735. }
  4736. thunk_convert(cur_data, spec, arg_type, THUNK_TARGET);
  4737. strcpy(cur_data + spec_size, (char*)&spec[1]);
  4738. cur_data = argptr + spec->next;
  4739. spec = (void*)host_dm + host_dm->data_start + next;
  4740. }
  4741. break;
  4742. }
  4743. case DM_TABLE_DEPS:
  4744. {
  4745. void *hdata = (void*)host_dm + host_dm->data_start;
  4746. int count = *(uint32_t*)hdata;
  4747. uint64_t *hdev = hdata + 8;
  4748. uint64_t *gdev = argptr + 8;
  4749. int i;
  4750. *(uint32_t*)argptr = tswap32(count);
  4751. for (i = 0; i < count; i++) {
  4752. *gdev = tswap64(*hdev);
  4753. gdev++;
  4754. hdev++;
  4755. }
  4756. break;
  4757. }
  4758. case DM_LIST_VERSIONS:
  4759. {
  4760. struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
  4761. uint32_t remaining_data = guest_data_size;
  4762. void *cur_data = argptr;
  4763. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
  4764. int vers_size = thunk_type_size(arg_type, 0);
  4765. while (1) {
  4766. uint32_t next = vers->next;
  4767. if (next) {
  4768. vers->next = vers_size + (strlen(vers->name) + 1);
  4769. }
  4770. if (remaining_data < vers->next) {
  4771. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  4772. break;
  4773. }
  4774. thunk_convert(cur_data, vers, arg_type, THUNK_TARGET);
  4775. strcpy(cur_data + vers_size, vers->name);
  4776. cur_data += vers->next;
  4777. remaining_data -= vers->next;
  4778. if (!next) {
  4779. break;
  4780. }
  4781. vers = (void*)vers + next;
  4782. }
  4783. break;
  4784. }
  4785. default:
  4786. unlock_user(argptr, guest_data, 0);
  4787. ret = -TARGET_EINVAL;
  4788. goto out;
  4789. }
  4790. unlock_user(argptr, guest_data, guest_data_size);
  4791. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4792. if (!argptr) {
  4793. ret = -TARGET_EFAULT;
  4794. goto out;
  4795. }
  4796. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  4797. unlock_user(argptr, arg, target_size);
  4798. }
  4799. out:
  4800. g_free(big_buf);
  4801. return ret;
  4802. }
  4803. static abi_long do_ioctl_blkpg(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
  4804. int cmd, abi_long arg)
  4805. {
  4806. void *argptr;
  4807. int target_size;
  4808. const argtype *arg_type = ie->arg_type;
  4809. const argtype part_arg_type[] = { MK_STRUCT(STRUCT_blkpg_partition) };
  4810. abi_long ret;
  4811. struct blkpg_ioctl_arg *host_blkpg = (void*)buf_temp;
  4812. struct blkpg_partition host_part;
  4813. /* Read and convert blkpg */
  4814. arg_type++;
  4815. target_size = thunk_type_size(arg_type, 0);
  4816. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4817. if (!argptr) {
  4818. ret = -TARGET_EFAULT;
  4819. goto out;
  4820. }
  4821. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4822. unlock_user(argptr, arg, 0);
  4823. switch (host_blkpg->op) {
  4824. case BLKPG_ADD_PARTITION:
  4825. case BLKPG_DEL_PARTITION:
  4826. /* payload is struct blkpg_partition */
  4827. break;
  4828. default:
  4829. /* Unknown opcode */
  4830. ret = -TARGET_EINVAL;
  4831. goto out;
  4832. }
  4833. /* Read and convert blkpg->data */
  4834. arg = (abi_long)(uintptr_t)host_blkpg->data;
  4835. target_size = thunk_type_size(part_arg_type, 0);
  4836. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4837. if (!argptr) {
  4838. ret = -TARGET_EFAULT;
  4839. goto out;
  4840. }
  4841. thunk_convert(&host_part, argptr, part_arg_type, THUNK_HOST);
  4842. unlock_user(argptr, arg, 0);
  4843. /* Swizzle the data pointer to our local copy and call! */
  4844. host_blkpg->data = &host_part;
  4845. ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_blkpg));
  4846. out:
  4847. return ret;
  4848. }
  4849. static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
  4850. int fd, int cmd, abi_long arg)
  4851. {
  4852. const argtype *arg_type = ie->arg_type;
  4853. const StructEntry *se;
  4854. const argtype *field_types;
  4855. const int *dst_offsets, *src_offsets;
  4856. int target_size;
  4857. void *argptr;
  4858. abi_ulong *target_rt_dev_ptr = NULL;
  4859. unsigned long *host_rt_dev_ptr = NULL;
  4860. abi_long ret;
  4861. int i;
  4862. assert(ie->access == IOC_W);
  4863. assert(*arg_type == TYPE_PTR);
  4864. arg_type++;
  4865. assert(*arg_type == TYPE_STRUCT);
  4866. target_size = thunk_type_size(arg_type, 0);
  4867. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4868. if (!argptr) {
  4869. return -TARGET_EFAULT;
  4870. }
  4871. arg_type++;
  4872. assert(*arg_type == (int)STRUCT_rtentry);
  4873. se = struct_entries + *arg_type++;
  4874. assert(se->convert[0] == NULL);
  4875. /* convert struct here to be able to catch rt_dev string */
  4876. field_types = se->field_types;
  4877. dst_offsets = se->field_offsets[THUNK_HOST];
  4878. src_offsets = se->field_offsets[THUNK_TARGET];
  4879. for (i = 0; i < se->nb_fields; i++) {
  4880. if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) {
  4881. assert(*field_types == TYPE_PTRVOID);
  4882. target_rt_dev_ptr = argptr + src_offsets[i];
  4883. host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]);
  4884. if (*target_rt_dev_ptr != 0) {
  4885. *host_rt_dev_ptr = (unsigned long)lock_user_string(
  4886. tswapal(*target_rt_dev_ptr));
  4887. if (!*host_rt_dev_ptr) {
  4888. unlock_user(argptr, arg, 0);
  4889. return -TARGET_EFAULT;
  4890. }
  4891. } else {
  4892. *host_rt_dev_ptr = 0;
  4893. }
  4894. field_types++;
  4895. continue;
  4896. }
  4897. field_types = thunk_convert(buf_temp + dst_offsets[i],
  4898. argptr + src_offsets[i],
  4899. field_types, THUNK_HOST);
  4900. }
  4901. unlock_user(argptr, arg, 0);
  4902. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4903. assert(host_rt_dev_ptr != NULL);
  4904. assert(target_rt_dev_ptr != NULL);
  4905. if (*host_rt_dev_ptr != 0) {
  4906. unlock_user((void *)*host_rt_dev_ptr,
  4907. *target_rt_dev_ptr, 0);
  4908. }
  4909. return ret;
  4910. }
  4911. static abi_long do_ioctl_kdsigaccept(const IOCTLEntry *ie, uint8_t *buf_temp,
  4912. int fd, int cmd, abi_long arg)
  4913. {
  4914. int sig = target_to_host_signal(arg);
  4915. return get_errno(safe_ioctl(fd, ie->host_cmd, sig));
  4916. }
  4917. static abi_long do_ioctl_SIOCGSTAMP(const IOCTLEntry *ie, uint8_t *buf_temp,
  4918. int fd, int cmd, abi_long arg)
  4919. {
  4920. struct timeval tv;
  4921. abi_long ret;
  4922. ret = get_errno(safe_ioctl(fd, SIOCGSTAMP, &tv));
  4923. if (is_error(ret)) {
  4924. return ret;
  4925. }
  4926. if (cmd == (int)TARGET_SIOCGSTAMP_OLD) {
  4927. if (copy_to_user_timeval(arg, &tv)) {
  4928. return -TARGET_EFAULT;
  4929. }
  4930. } else {
  4931. if (copy_to_user_timeval64(arg, &tv)) {
  4932. return -TARGET_EFAULT;
  4933. }
  4934. }
  4935. return ret;
  4936. }
  4937. static abi_long do_ioctl_SIOCGSTAMPNS(const IOCTLEntry *ie, uint8_t *buf_temp,
  4938. int fd, int cmd, abi_long arg)
  4939. {
  4940. struct timespec ts;
  4941. abi_long ret;
  4942. ret = get_errno(safe_ioctl(fd, SIOCGSTAMPNS, &ts));
  4943. if (is_error(ret)) {
  4944. return ret;
  4945. }
  4946. if (cmd == (int)TARGET_SIOCGSTAMPNS_OLD) {
  4947. if (host_to_target_timespec(arg, &ts)) {
  4948. return -TARGET_EFAULT;
  4949. }
  4950. } else{
  4951. if (host_to_target_timespec64(arg, &ts)) {
  4952. return -TARGET_EFAULT;
  4953. }
  4954. }
  4955. return ret;
  4956. }
  4957. #ifdef TIOCGPTPEER
  4958. static abi_long do_ioctl_tiocgptpeer(const IOCTLEntry *ie, uint8_t *buf_temp,
  4959. int fd, int cmd, abi_long arg)
  4960. {
  4961. int flags = target_to_host_bitmask(arg, fcntl_flags_tbl);
  4962. return get_errno(safe_ioctl(fd, ie->host_cmd, flags));
  4963. }
  4964. #endif
  4965. #ifdef HAVE_DRM_H
  4966. static void unlock_drm_version(struct drm_version *host_ver,
  4967. struct target_drm_version *target_ver,
  4968. bool copy)
  4969. {
  4970. unlock_user(host_ver->name, target_ver->name,
  4971. copy ? host_ver->name_len : 0);
  4972. unlock_user(host_ver->date, target_ver->date,
  4973. copy ? host_ver->date_len : 0);
  4974. unlock_user(host_ver->desc, target_ver->desc,
  4975. copy ? host_ver->desc_len : 0);
  4976. }
  4977. static inline abi_long target_to_host_drmversion(struct drm_version *host_ver,
  4978. struct target_drm_version *target_ver)
  4979. {
  4980. memset(host_ver, 0, sizeof(*host_ver));
  4981. __get_user(host_ver->name_len, &target_ver->name_len);
  4982. if (host_ver->name_len) {
  4983. host_ver->name = lock_user(VERIFY_WRITE, target_ver->name,
  4984. target_ver->name_len, 0);
  4985. if (!host_ver->name) {
  4986. return -EFAULT;
  4987. }
  4988. }
  4989. __get_user(host_ver->date_len, &target_ver->date_len);
  4990. if (host_ver->date_len) {
  4991. host_ver->date = lock_user(VERIFY_WRITE, target_ver->date,
  4992. target_ver->date_len, 0);
  4993. if (!host_ver->date) {
  4994. goto err;
  4995. }
  4996. }
  4997. __get_user(host_ver->desc_len, &target_ver->desc_len);
  4998. if (host_ver->desc_len) {
  4999. host_ver->desc = lock_user(VERIFY_WRITE, target_ver->desc,
  5000. target_ver->desc_len, 0);
  5001. if (!host_ver->desc) {
  5002. goto err;
  5003. }
  5004. }
  5005. return 0;
  5006. err:
  5007. unlock_drm_version(host_ver, target_ver, false);
  5008. return -EFAULT;
  5009. }
  5010. static inline void host_to_target_drmversion(
  5011. struct target_drm_version *target_ver,
  5012. struct drm_version *host_ver)
  5013. {
  5014. __put_user(host_ver->version_major, &target_ver->version_major);
  5015. __put_user(host_ver->version_minor, &target_ver->version_minor);
  5016. __put_user(host_ver->version_patchlevel, &target_ver->version_patchlevel);
  5017. __put_user(host_ver->name_len, &target_ver->name_len);
  5018. __put_user(host_ver->date_len, &target_ver->date_len);
  5019. __put_user(host_ver->desc_len, &target_ver->desc_len);
  5020. unlock_drm_version(host_ver, target_ver, true);
  5021. }
  5022. static abi_long do_ioctl_drm(const IOCTLEntry *ie, uint8_t *buf_temp,
  5023. int fd, int cmd, abi_long arg)
  5024. {
  5025. struct drm_version *ver;
  5026. struct target_drm_version *target_ver;
  5027. abi_long ret;
  5028. switch (ie->host_cmd) {
  5029. case DRM_IOCTL_VERSION:
  5030. if (!lock_user_struct(VERIFY_WRITE, target_ver, arg, 0)) {
  5031. return -TARGET_EFAULT;
  5032. }
  5033. ver = (struct drm_version *)buf_temp;
  5034. ret = target_to_host_drmversion(ver, target_ver);
  5035. if (!is_error(ret)) {
  5036. ret = get_errno(safe_ioctl(fd, ie->host_cmd, ver));
  5037. if (is_error(ret)) {
  5038. unlock_drm_version(ver, target_ver, false);
  5039. } else {
  5040. host_to_target_drmversion(target_ver, ver);
  5041. }
  5042. }
  5043. unlock_user_struct(target_ver, arg, 0);
  5044. return ret;
  5045. }
  5046. return -TARGET_ENOSYS;
  5047. }
  5048. static abi_long do_ioctl_drm_i915_getparam(const IOCTLEntry *ie,
  5049. struct drm_i915_getparam *gparam,
  5050. int fd, abi_long arg)
  5051. {
  5052. abi_long ret;
  5053. int value;
  5054. struct target_drm_i915_getparam *target_gparam;
  5055. if (!lock_user_struct(VERIFY_READ, target_gparam, arg, 0)) {
  5056. return -TARGET_EFAULT;
  5057. }
  5058. __get_user(gparam->param, &target_gparam->param);
  5059. gparam->value = &value;
  5060. ret = get_errno(safe_ioctl(fd, ie->host_cmd, gparam));
  5061. put_user_s32(value, target_gparam->value);
  5062. unlock_user_struct(target_gparam, arg, 0);
  5063. return ret;
  5064. }
  5065. static abi_long do_ioctl_drm_i915(const IOCTLEntry *ie, uint8_t *buf_temp,
  5066. int fd, int cmd, abi_long arg)
  5067. {
  5068. switch (ie->host_cmd) {
  5069. case DRM_IOCTL_I915_GETPARAM:
  5070. return do_ioctl_drm_i915_getparam(ie,
  5071. (struct drm_i915_getparam *)buf_temp,
  5072. fd, arg);
  5073. default:
  5074. return -TARGET_ENOSYS;
  5075. }
  5076. }
  5077. #endif
  5078. static abi_long do_ioctl_TUNSETTXFILTER(const IOCTLEntry *ie, uint8_t *buf_temp,
  5079. int fd, int cmd, abi_long arg)
  5080. {
  5081. struct tun_filter *filter = (struct tun_filter *)buf_temp;
  5082. struct tun_filter *target_filter;
  5083. char *target_addr;
  5084. assert(ie->access == IOC_W);
  5085. target_filter = lock_user(VERIFY_READ, arg, sizeof(*target_filter), 1);
  5086. if (!target_filter) {
  5087. return -TARGET_EFAULT;
  5088. }
  5089. filter->flags = tswap16(target_filter->flags);
  5090. filter->count = tswap16(target_filter->count);
  5091. unlock_user(target_filter, arg, 0);
  5092. if (filter->count) {
  5093. if (offsetof(struct tun_filter, addr) + filter->count * ETH_ALEN >
  5094. MAX_STRUCT_SIZE) {
  5095. return -TARGET_EFAULT;
  5096. }
  5097. target_addr = lock_user(VERIFY_READ,
  5098. arg + offsetof(struct tun_filter, addr),
  5099. filter->count * ETH_ALEN, 1);
  5100. if (!target_addr) {
  5101. return -TARGET_EFAULT;
  5102. }
  5103. memcpy(filter->addr, target_addr, filter->count * ETH_ALEN);
  5104. unlock_user(target_addr, arg + offsetof(struct tun_filter, addr), 0);
  5105. }
  5106. return get_errno(safe_ioctl(fd, ie->host_cmd, filter));
  5107. }
  5108. IOCTLEntry ioctl_entries[] = {
  5109. #define IOCTL(cmd, access, ...) \
  5110. { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
  5111. #define IOCTL_SPECIAL(cmd, access, dofn, ...) \
  5112. { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
  5113. #define IOCTL_IGNORE(cmd) \
  5114. { TARGET_ ## cmd, 0, #cmd },
  5115. #include "ioctls.h"
  5116. { 0, 0, },
  5117. };
  5118. /* ??? Implement proper locking for ioctls. */
  5119. /* do_ioctl() Must return target values and target errnos. */
  5120. static abi_long do_ioctl(int fd, int cmd, abi_long arg)
  5121. {
  5122. const IOCTLEntry *ie;
  5123. const argtype *arg_type;
  5124. abi_long ret;
  5125. uint8_t buf_temp[MAX_STRUCT_SIZE];
  5126. int target_size;
  5127. void *argptr;
  5128. ie = ioctl_entries;
  5129. for(;;) {
  5130. if (ie->target_cmd == 0) {
  5131. qemu_log_mask(
  5132. LOG_UNIMP, "Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
  5133. return -TARGET_ENOTTY;
  5134. }
  5135. if (ie->target_cmd == cmd)
  5136. break;
  5137. ie++;
  5138. }
  5139. arg_type = ie->arg_type;
  5140. if (ie->do_ioctl) {
  5141. return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
  5142. } else if (!ie->host_cmd) {
  5143. /* Some architectures define BSD ioctls in their headers
  5144. that are not implemented in Linux. */
  5145. return -TARGET_ENOTTY;
  5146. }
  5147. switch(arg_type[0]) {
  5148. case TYPE_NULL:
  5149. /* no argument */
  5150. ret = get_errno(safe_ioctl(fd, ie->host_cmd));
  5151. break;
  5152. case TYPE_PTRVOID:
  5153. case TYPE_INT:
  5154. case TYPE_LONG:
  5155. case TYPE_ULONG:
  5156. ret = get_errno(safe_ioctl(fd, ie->host_cmd, arg));
  5157. break;
  5158. case TYPE_PTR:
  5159. arg_type++;
  5160. target_size = thunk_type_size(arg_type, 0);
  5161. switch(ie->access) {
  5162. case IOC_R:
  5163. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  5164. if (!is_error(ret)) {
  5165. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  5166. if (!argptr)
  5167. return -TARGET_EFAULT;
  5168. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  5169. unlock_user(argptr, arg, target_size);
  5170. }
  5171. break;
  5172. case IOC_W:
  5173. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  5174. if (!argptr)
  5175. return -TARGET_EFAULT;
  5176. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  5177. unlock_user(argptr, arg, 0);
  5178. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  5179. break;
  5180. default:
  5181. case IOC_RW:
  5182. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  5183. if (!argptr)
  5184. return -TARGET_EFAULT;
  5185. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  5186. unlock_user(argptr, arg, 0);
  5187. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  5188. if (!is_error(ret)) {
  5189. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  5190. if (!argptr)
  5191. return -TARGET_EFAULT;
  5192. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  5193. unlock_user(argptr, arg, target_size);
  5194. }
  5195. break;
  5196. }
  5197. break;
  5198. default:
  5199. qemu_log_mask(LOG_UNIMP,
  5200. "Unsupported ioctl type: cmd=0x%04lx type=%d\n",
  5201. (long)cmd, arg_type[0]);
  5202. ret = -TARGET_ENOTTY;
  5203. break;
  5204. }
  5205. return ret;
  5206. }
  5207. static const bitmask_transtbl iflag_tbl[] = {
  5208. { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
  5209. { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
  5210. { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
  5211. { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
  5212. { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
  5213. { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
  5214. { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
  5215. { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
  5216. { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
  5217. { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
  5218. { TARGET_IXON, TARGET_IXON, IXON, IXON },
  5219. { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
  5220. { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
  5221. { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
  5222. { TARGET_IUTF8, TARGET_IUTF8, IUTF8, IUTF8},
  5223. };
  5224. static const bitmask_transtbl oflag_tbl[] = {
  5225. { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
  5226. { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
  5227. { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
  5228. { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
  5229. { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
  5230. { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
  5231. { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
  5232. { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
  5233. { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
  5234. { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
  5235. { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
  5236. { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
  5237. { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
  5238. { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
  5239. { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
  5240. { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
  5241. { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
  5242. { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
  5243. { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
  5244. { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
  5245. { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
  5246. { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
  5247. { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
  5248. { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
  5249. };
  5250. static const bitmask_transtbl cflag_tbl[] = {
  5251. { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
  5252. { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
  5253. { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
  5254. { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
  5255. { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
  5256. { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
  5257. { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
  5258. { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
  5259. { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
  5260. { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
  5261. { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
  5262. { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
  5263. { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
  5264. { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
  5265. { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
  5266. { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
  5267. { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
  5268. { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
  5269. { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
  5270. { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
  5271. { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
  5272. { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
  5273. { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
  5274. { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
  5275. { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
  5276. { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
  5277. { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
  5278. { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
  5279. { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
  5280. { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
  5281. { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
  5282. };
  5283. static const bitmask_transtbl lflag_tbl[] = {
  5284. { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
  5285. { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
  5286. { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
  5287. { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
  5288. { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
  5289. { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
  5290. { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
  5291. { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
  5292. { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
  5293. { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
  5294. { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
  5295. { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
  5296. { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
  5297. { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
  5298. { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
  5299. { TARGET_EXTPROC, TARGET_EXTPROC, EXTPROC, EXTPROC},
  5300. };
  5301. static void target_to_host_termios (void *dst, const void *src)
  5302. {
  5303. struct host_termios *host = dst;
  5304. const struct target_termios *target = src;
  5305. host->c_iflag =
  5306. target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
  5307. host->c_oflag =
  5308. target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
  5309. host->c_cflag =
  5310. target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
  5311. host->c_lflag =
  5312. target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
  5313. host->c_line = target->c_line;
  5314. memset(host->c_cc, 0, sizeof(host->c_cc));
  5315. host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
  5316. host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
  5317. host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
  5318. host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
  5319. host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
  5320. host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
  5321. host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
  5322. host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
  5323. host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
  5324. host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
  5325. host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
  5326. host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
  5327. host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
  5328. host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
  5329. host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
  5330. host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
  5331. host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
  5332. }
  5333. static void host_to_target_termios (void *dst, const void *src)
  5334. {
  5335. struct target_termios *target = dst;
  5336. const struct host_termios *host = src;
  5337. target->c_iflag =
  5338. tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
  5339. target->c_oflag =
  5340. tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
  5341. target->c_cflag =
  5342. tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
  5343. target->c_lflag =
  5344. tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
  5345. target->c_line = host->c_line;
  5346. memset(target->c_cc, 0, sizeof(target->c_cc));
  5347. target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
  5348. target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
  5349. target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
  5350. target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
  5351. target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
  5352. target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
  5353. target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
  5354. target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
  5355. target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
  5356. target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
  5357. target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
  5358. target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
  5359. target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
  5360. target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
  5361. target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
  5362. target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
  5363. target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
  5364. }
  5365. static const StructEntry struct_termios_def = {
  5366. .convert = { host_to_target_termios, target_to_host_termios },
  5367. .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
  5368. .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
  5369. .print = print_termios,
  5370. };
  5371. /* If the host does not provide these bits, they may be safely discarded. */
  5372. #ifndef MAP_SYNC
  5373. #define MAP_SYNC 0
  5374. #endif
  5375. #ifndef MAP_UNINITIALIZED
  5376. #define MAP_UNINITIALIZED 0
  5377. #endif
  5378. static const bitmask_transtbl mmap_flags_tbl[] = {
  5379. { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
  5380. { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS,
  5381. MAP_ANONYMOUS, MAP_ANONYMOUS },
  5382. { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN,
  5383. MAP_GROWSDOWN, MAP_GROWSDOWN },
  5384. { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE,
  5385. MAP_DENYWRITE, MAP_DENYWRITE },
  5386. { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE,
  5387. MAP_EXECUTABLE, MAP_EXECUTABLE },
  5388. { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
  5389. { TARGET_MAP_NORESERVE, TARGET_MAP_NORESERVE,
  5390. MAP_NORESERVE, MAP_NORESERVE },
  5391. { TARGET_MAP_HUGETLB, TARGET_MAP_HUGETLB, MAP_HUGETLB, MAP_HUGETLB },
  5392. /* MAP_STACK had been ignored by the kernel for quite some time.
  5393. Recognize it for the target insofar as we do not want to pass
  5394. it through to the host. */
  5395. { TARGET_MAP_STACK, TARGET_MAP_STACK, 0, 0 },
  5396. { TARGET_MAP_NONBLOCK, TARGET_MAP_NONBLOCK, MAP_NONBLOCK, MAP_NONBLOCK },
  5397. { TARGET_MAP_POPULATE, TARGET_MAP_POPULATE, MAP_POPULATE, MAP_POPULATE },
  5398. { TARGET_MAP_FIXED_NOREPLACE, TARGET_MAP_FIXED_NOREPLACE,
  5399. MAP_FIXED_NOREPLACE, MAP_FIXED_NOREPLACE },
  5400. { TARGET_MAP_UNINITIALIZED, TARGET_MAP_UNINITIALIZED,
  5401. MAP_UNINITIALIZED, MAP_UNINITIALIZED },
  5402. };
  5403. /*
  5404. * Arrange for legacy / undefined architecture specific flags to be
  5405. * ignored by mmap handling code.
  5406. */
  5407. #ifndef TARGET_MAP_32BIT
  5408. #define TARGET_MAP_32BIT 0
  5409. #endif
  5410. #ifndef TARGET_MAP_HUGE_2MB
  5411. #define TARGET_MAP_HUGE_2MB 0
  5412. #endif
  5413. #ifndef TARGET_MAP_HUGE_1GB
  5414. #define TARGET_MAP_HUGE_1GB 0
  5415. #endif
  5416. static abi_long do_mmap(abi_ulong addr, abi_ulong len, int prot,
  5417. int target_flags, int fd, off_t offset)
  5418. {
  5419. /*
  5420. * The historical set of flags that all mmap types implicitly support.
  5421. */
  5422. enum {
  5423. TARGET_LEGACY_MAP_MASK = TARGET_MAP_SHARED
  5424. | TARGET_MAP_PRIVATE
  5425. | TARGET_MAP_FIXED
  5426. | TARGET_MAP_ANONYMOUS
  5427. | TARGET_MAP_DENYWRITE
  5428. | TARGET_MAP_EXECUTABLE
  5429. | TARGET_MAP_UNINITIALIZED
  5430. | TARGET_MAP_GROWSDOWN
  5431. | TARGET_MAP_LOCKED
  5432. | TARGET_MAP_NORESERVE
  5433. | TARGET_MAP_POPULATE
  5434. | TARGET_MAP_NONBLOCK
  5435. | TARGET_MAP_STACK
  5436. | TARGET_MAP_HUGETLB
  5437. | TARGET_MAP_32BIT
  5438. | TARGET_MAP_HUGE_2MB
  5439. | TARGET_MAP_HUGE_1GB
  5440. };
  5441. int host_flags;
  5442. switch (target_flags & TARGET_MAP_TYPE) {
  5443. case TARGET_MAP_PRIVATE:
  5444. host_flags = MAP_PRIVATE;
  5445. break;
  5446. case TARGET_MAP_SHARED:
  5447. host_flags = MAP_SHARED;
  5448. break;
  5449. case TARGET_MAP_SHARED_VALIDATE:
  5450. /*
  5451. * MAP_SYNC is only supported for MAP_SHARED_VALIDATE, and is
  5452. * therefore omitted from mmap_flags_tbl and TARGET_LEGACY_MAP_MASK.
  5453. */
  5454. if (target_flags & ~(TARGET_LEGACY_MAP_MASK | TARGET_MAP_SYNC)) {
  5455. return -TARGET_EOPNOTSUPP;
  5456. }
  5457. host_flags = MAP_SHARED_VALIDATE;
  5458. if (target_flags & TARGET_MAP_SYNC) {
  5459. host_flags |= MAP_SYNC;
  5460. }
  5461. break;
  5462. default:
  5463. return -TARGET_EINVAL;
  5464. }
  5465. host_flags |= target_to_host_bitmask(target_flags, mmap_flags_tbl);
  5466. return get_errno(target_mmap(addr, len, prot, host_flags, fd, offset));
  5467. }
  5468. /*
  5469. * NOTE: TARGET_ABI32 is defined for TARGET_I386 (but not for TARGET_X86_64)
  5470. * TARGET_I386 is defined if TARGET_X86_64 is defined
  5471. */
  5472. #if defined(TARGET_I386)
  5473. /* NOTE: there is really one LDT for all the threads */
  5474. static uint8_t *ldt_table;
  5475. static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
  5476. {
  5477. int size;
  5478. void *p;
  5479. if (!ldt_table)
  5480. return 0;
  5481. size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
  5482. if (size > bytecount)
  5483. size = bytecount;
  5484. p = lock_user(VERIFY_WRITE, ptr, size, 0);
  5485. if (!p)
  5486. return -TARGET_EFAULT;
  5487. /* ??? Should this by byteswapped? */
  5488. memcpy(p, ldt_table, size);
  5489. unlock_user(p, ptr, size);
  5490. return size;
  5491. }
  5492. /* XXX: add locking support */
  5493. static abi_long write_ldt(CPUX86State *env,
  5494. abi_ulong ptr, unsigned long bytecount, int oldmode)
  5495. {
  5496. struct target_modify_ldt_ldt_s ldt_info;
  5497. struct target_modify_ldt_ldt_s *target_ldt_info;
  5498. int seg_32bit, contents, read_exec_only, limit_in_pages;
  5499. int seg_not_present, useable, lm;
  5500. uint32_t *lp, entry_1, entry_2;
  5501. if (bytecount != sizeof(ldt_info))
  5502. return -TARGET_EINVAL;
  5503. if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
  5504. return -TARGET_EFAULT;
  5505. ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
  5506. ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
  5507. ldt_info.limit = tswap32(target_ldt_info->limit);
  5508. ldt_info.flags = tswap32(target_ldt_info->flags);
  5509. unlock_user_struct(target_ldt_info, ptr, 0);
  5510. if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
  5511. return -TARGET_EINVAL;
  5512. seg_32bit = ldt_info.flags & 1;
  5513. contents = (ldt_info.flags >> 1) & 3;
  5514. read_exec_only = (ldt_info.flags >> 3) & 1;
  5515. limit_in_pages = (ldt_info.flags >> 4) & 1;
  5516. seg_not_present = (ldt_info.flags >> 5) & 1;
  5517. useable = (ldt_info.flags >> 6) & 1;
  5518. #ifdef TARGET_ABI32
  5519. lm = 0;
  5520. #else
  5521. lm = (ldt_info.flags >> 7) & 1;
  5522. #endif
  5523. if (contents == 3) {
  5524. if (oldmode)
  5525. return -TARGET_EINVAL;
  5526. if (seg_not_present == 0)
  5527. return -TARGET_EINVAL;
  5528. }
  5529. /* allocate the LDT */
  5530. if (!ldt_table) {
  5531. env->ldt.base = target_mmap(0,
  5532. TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
  5533. PROT_READ|PROT_WRITE,
  5534. MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
  5535. if (env->ldt.base == -1)
  5536. return -TARGET_ENOMEM;
  5537. memset(g2h_untagged(env->ldt.base), 0,
  5538. TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
  5539. env->ldt.limit = 0xffff;
  5540. ldt_table = g2h_untagged(env->ldt.base);
  5541. }
  5542. /* NOTE: same code as Linux kernel */
  5543. /* Allow LDTs to be cleared by the user. */
  5544. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  5545. if (oldmode ||
  5546. (contents == 0 &&
  5547. read_exec_only == 1 &&
  5548. seg_32bit == 0 &&
  5549. limit_in_pages == 0 &&
  5550. seg_not_present == 1 &&
  5551. useable == 0 )) {
  5552. entry_1 = 0;
  5553. entry_2 = 0;
  5554. goto install;
  5555. }
  5556. }
  5557. entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
  5558. (ldt_info.limit & 0x0ffff);
  5559. entry_2 = (ldt_info.base_addr & 0xff000000) |
  5560. ((ldt_info.base_addr & 0x00ff0000) >> 16) |
  5561. (ldt_info.limit & 0xf0000) |
  5562. ((read_exec_only ^ 1) << 9) |
  5563. (contents << 10) |
  5564. ((seg_not_present ^ 1) << 15) |
  5565. (seg_32bit << 22) |
  5566. (limit_in_pages << 23) |
  5567. (lm << 21) |
  5568. 0x7000;
  5569. if (!oldmode)
  5570. entry_2 |= (useable << 20);
  5571. /* Install the new entry ... */
  5572. install:
  5573. lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
  5574. lp[0] = tswap32(entry_1);
  5575. lp[1] = tswap32(entry_2);
  5576. return 0;
  5577. }
  5578. /* specific and weird i386 syscalls */
  5579. static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
  5580. unsigned long bytecount)
  5581. {
  5582. abi_long ret;
  5583. switch (func) {
  5584. case 0:
  5585. ret = read_ldt(ptr, bytecount);
  5586. break;
  5587. case 1:
  5588. ret = write_ldt(env, ptr, bytecount, 1);
  5589. break;
  5590. case 0x11:
  5591. ret = write_ldt(env, ptr, bytecount, 0);
  5592. break;
  5593. default:
  5594. ret = -TARGET_ENOSYS;
  5595. break;
  5596. }
  5597. return ret;
  5598. }
  5599. #if defined(TARGET_ABI32)
  5600. abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
  5601. {
  5602. uint64_t *gdt_table = g2h_untagged(env->gdt.base);
  5603. struct target_modify_ldt_ldt_s ldt_info;
  5604. struct target_modify_ldt_ldt_s *target_ldt_info;
  5605. int seg_32bit, contents, read_exec_only, limit_in_pages;
  5606. int seg_not_present, useable, lm;
  5607. uint32_t *lp, entry_1, entry_2;
  5608. int i;
  5609. lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
  5610. if (!target_ldt_info)
  5611. return -TARGET_EFAULT;
  5612. ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
  5613. ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
  5614. ldt_info.limit = tswap32(target_ldt_info->limit);
  5615. ldt_info.flags = tswap32(target_ldt_info->flags);
  5616. if (ldt_info.entry_number == -1) {
  5617. for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
  5618. if (gdt_table[i] == 0) {
  5619. ldt_info.entry_number = i;
  5620. target_ldt_info->entry_number = tswap32(i);
  5621. break;
  5622. }
  5623. }
  5624. }
  5625. unlock_user_struct(target_ldt_info, ptr, 1);
  5626. if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
  5627. ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
  5628. return -TARGET_EINVAL;
  5629. seg_32bit = ldt_info.flags & 1;
  5630. contents = (ldt_info.flags >> 1) & 3;
  5631. read_exec_only = (ldt_info.flags >> 3) & 1;
  5632. limit_in_pages = (ldt_info.flags >> 4) & 1;
  5633. seg_not_present = (ldt_info.flags >> 5) & 1;
  5634. useable = (ldt_info.flags >> 6) & 1;
  5635. #ifdef TARGET_ABI32
  5636. lm = 0;
  5637. #else
  5638. lm = (ldt_info.flags >> 7) & 1;
  5639. #endif
  5640. if (contents == 3) {
  5641. if (seg_not_present == 0)
  5642. return -TARGET_EINVAL;
  5643. }
  5644. /* NOTE: same code as Linux kernel */
  5645. /* Allow LDTs to be cleared by the user. */
  5646. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  5647. if ((contents == 0 &&
  5648. read_exec_only == 1 &&
  5649. seg_32bit == 0 &&
  5650. limit_in_pages == 0 &&
  5651. seg_not_present == 1 &&
  5652. useable == 0 )) {
  5653. entry_1 = 0;
  5654. entry_2 = 0;
  5655. goto install;
  5656. }
  5657. }
  5658. entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
  5659. (ldt_info.limit & 0x0ffff);
  5660. entry_2 = (ldt_info.base_addr & 0xff000000) |
  5661. ((ldt_info.base_addr & 0x00ff0000) >> 16) |
  5662. (ldt_info.limit & 0xf0000) |
  5663. ((read_exec_only ^ 1) << 9) |
  5664. (contents << 10) |
  5665. ((seg_not_present ^ 1) << 15) |
  5666. (seg_32bit << 22) |
  5667. (limit_in_pages << 23) |
  5668. (useable << 20) |
  5669. (lm << 21) |
  5670. 0x7000;
  5671. /* Install the new entry ... */
  5672. install:
  5673. lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
  5674. lp[0] = tswap32(entry_1);
  5675. lp[1] = tswap32(entry_2);
  5676. return 0;
  5677. }
  5678. static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
  5679. {
  5680. struct target_modify_ldt_ldt_s *target_ldt_info;
  5681. uint64_t *gdt_table = g2h_untagged(env->gdt.base);
  5682. uint32_t base_addr, limit, flags;
  5683. int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
  5684. int seg_not_present, useable, lm;
  5685. uint32_t *lp, entry_1, entry_2;
  5686. lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
  5687. if (!target_ldt_info)
  5688. return -TARGET_EFAULT;
  5689. idx = tswap32(target_ldt_info->entry_number);
  5690. if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
  5691. idx > TARGET_GDT_ENTRY_TLS_MAX) {
  5692. unlock_user_struct(target_ldt_info, ptr, 1);
  5693. return -TARGET_EINVAL;
  5694. }
  5695. lp = (uint32_t *)(gdt_table + idx);
  5696. entry_1 = tswap32(lp[0]);
  5697. entry_2 = tswap32(lp[1]);
  5698. read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
  5699. contents = (entry_2 >> 10) & 3;
  5700. seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
  5701. seg_32bit = (entry_2 >> 22) & 1;
  5702. limit_in_pages = (entry_2 >> 23) & 1;
  5703. useable = (entry_2 >> 20) & 1;
  5704. #ifdef TARGET_ABI32
  5705. lm = 0;
  5706. #else
  5707. lm = (entry_2 >> 21) & 1;
  5708. #endif
  5709. flags = (seg_32bit << 0) | (contents << 1) |
  5710. (read_exec_only << 3) | (limit_in_pages << 4) |
  5711. (seg_not_present << 5) | (useable << 6) | (lm << 7);
  5712. limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
  5713. base_addr = (entry_1 >> 16) |
  5714. (entry_2 & 0xff000000) |
  5715. ((entry_2 & 0xff) << 16);
  5716. target_ldt_info->base_addr = tswapal(base_addr);
  5717. target_ldt_info->limit = tswap32(limit);
  5718. target_ldt_info->flags = tswap32(flags);
  5719. unlock_user_struct(target_ldt_info, ptr, 1);
  5720. return 0;
  5721. }
  5722. abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
  5723. {
  5724. return -TARGET_ENOSYS;
  5725. }
  5726. #else
  5727. abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
  5728. {
  5729. abi_long ret = 0;
  5730. abi_ulong val;
  5731. int idx;
  5732. switch(code) {
  5733. case TARGET_ARCH_SET_GS:
  5734. case TARGET_ARCH_SET_FS:
  5735. if (code == TARGET_ARCH_SET_GS)
  5736. idx = R_GS;
  5737. else
  5738. idx = R_FS;
  5739. cpu_x86_load_seg(env, idx, 0);
  5740. env->segs[idx].base = addr;
  5741. break;
  5742. case TARGET_ARCH_GET_GS:
  5743. case TARGET_ARCH_GET_FS:
  5744. if (code == TARGET_ARCH_GET_GS)
  5745. idx = R_GS;
  5746. else
  5747. idx = R_FS;
  5748. val = env->segs[idx].base;
  5749. if (put_user(val, addr, abi_ulong))
  5750. ret = -TARGET_EFAULT;
  5751. break;
  5752. default:
  5753. ret = -TARGET_EINVAL;
  5754. break;
  5755. }
  5756. return ret;
  5757. }
  5758. #endif /* defined(TARGET_ABI32 */
  5759. #endif /* defined(TARGET_I386) */
  5760. /*
  5761. * These constants are generic. Supply any that are missing from the host.
  5762. */
  5763. #ifndef PR_SET_NAME
  5764. # define PR_SET_NAME 15
  5765. # define PR_GET_NAME 16
  5766. #endif
  5767. #ifndef PR_SET_FP_MODE
  5768. # define PR_SET_FP_MODE 45
  5769. # define PR_GET_FP_MODE 46
  5770. # define PR_FP_MODE_FR (1 << 0)
  5771. # define PR_FP_MODE_FRE (1 << 1)
  5772. #endif
  5773. #ifndef PR_SVE_SET_VL
  5774. # define PR_SVE_SET_VL 50
  5775. # define PR_SVE_GET_VL 51
  5776. # define PR_SVE_VL_LEN_MASK 0xffff
  5777. # define PR_SVE_VL_INHERIT (1 << 17)
  5778. #endif
  5779. #ifndef PR_PAC_RESET_KEYS
  5780. # define PR_PAC_RESET_KEYS 54
  5781. # define PR_PAC_APIAKEY (1 << 0)
  5782. # define PR_PAC_APIBKEY (1 << 1)
  5783. # define PR_PAC_APDAKEY (1 << 2)
  5784. # define PR_PAC_APDBKEY (1 << 3)
  5785. # define PR_PAC_APGAKEY (1 << 4)
  5786. #endif
  5787. #ifndef PR_SET_TAGGED_ADDR_CTRL
  5788. # define PR_SET_TAGGED_ADDR_CTRL 55
  5789. # define PR_GET_TAGGED_ADDR_CTRL 56
  5790. # define PR_TAGGED_ADDR_ENABLE (1UL << 0)
  5791. #endif
  5792. #ifndef PR_MTE_TCF_SHIFT
  5793. # define PR_MTE_TCF_SHIFT 1
  5794. # define PR_MTE_TCF_NONE (0UL << PR_MTE_TCF_SHIFT)
  5795. # define PR_MTE_TCF_SYNC (1UL << PR_MTE_TCF_SHIFT)
  5796. # define PR_MTE_TCF_ASYNC (2UL << PR_MTE_TCF_SHIFT)
  5797. # define PR_MTE_TCF_MASK (3UL << PR_MTE_TCF_SHIFT)
  5798. # define PR_MTE_TAG_SHIFT 3
  5799. # define PR_MTE_TAG_MASK (0xffffUL << PR_MTE_TAG_SHIFT)
  5800. #endif
  5801. #ifndef PR_SET_IO_FLUSHER
  5802. # define PR_SET_IO_FLUSHER 57
  5803. # define PR_GET_IO_FLUSHER 58
  5804. #endif
  5805. #ifndef PR_SET_SYSCALL_USER_DISPATCH
  5806. # define PR_SET_SYSCALL_USER_DISPATCH 59
  5807. #endif
  5808. #ifndef PR_SME_SET_VL
  5809. # define PR_SME_SET_VL 63
  5810. # define PR_SME_GET_VL 64
  5811. # define PR_SME_VL_LEN_MASK 0xffff
  5812. # define PR_SME_VL_INHERIT (1 << 17)
  5813. #endif
  5814. #include "target_prctl.h"
  5815. static abi_long do_prctl_inval0(CPUArchState *env)
  5816. {
  5817. return -TARGET_EINVAL;
  5818. }
  5819. static abi_long do_prctl_inval1(CPUArchState *env, abi_long arg2)
  5820. {
  5821. return -TARGET_EINVAL;
  5822. }
  5823. #ifndef do_prctl_get_fp_mode
  5824. #define do_prctl_get_fp_mode do_prctl_inval0
  5825. #endif
  5826. #ifndef do_prctl_set_fp_mode
  5827. #define do_prctl_set_fp_mode do_prctl_inval1
  5828. #endif
  5829. #ifndef do_prctl_sve_get_vl
  5830. #define do_prctl_sve_get_vl do_prctl_inval0
  5831. #endif
  5832. #ifndef do_prctl_sve_set_vl
  5833. #define do_prctl_sve_set_vl do_prctl_inval1
  5834. #endif
  5835. #ifndef do_prctl_reset_keys
  5836. #define do_prctl_reset_keys do_prctl_inval1
  5837. #endif
  5838. #ifndef do_prctl_set_tagged_addr_ctrl
  5839. #define do_prctl_set_tagged_addr_ctrl do_prctl_inval1
  5840. #endif
  5841. #ifndef do_prctl_get_tagged_addr_ctrl
  5842. #define do_prctl_get_tagged_addr_ctrl do_prctl_inval0
  5843. #endif
  5844. #ifndef do_prctl_get_unalign
  5845. #define do_prctl_get_unalign do_prctl_inval1
  5846. #endif
  5847. #ifndef do_prctl_set_unalign
  5848. #define do_prctl_set_unalign do_prctl_inval1
  5849. #endif
  5850. #ifndef do_prctl_sme_get_vl
  5851. #define do_prctl_sme_get_vl do_prctl_inval0
  5852. #endif
  5853. #ifndef do_prctl_sme_set_vl
  5854. #define do_prctl_sme_set_vl do_prctl_inval1
  5855. #endif
  5856. static abi_long do_prctl(CPUArchState *env, abi_long option, abi_long arg2,
  5857. abi_long arg3, abi_long arg4, abi_long arg5)
  5858. {
  5859. abi_long ret;
  5860. switch (option) {
  5861. case PR_GET_PDEATHSIG:
  5862. {
  5863. int deathsig;
  5864. ret = get_errno(prctl(PR_GET_PDEATHSIG, &deathsig,
  5865. arg3, arg4, arg5));
  5866. if (!is_error(ret) &&
  5867. put_user_s32(host_to_target_signal(deathsig), arg2)) {
  5868. return -TARGET_EFAULT;
  5869. }
  5870. return ret;
  5871. }
  5872. case PR_SET_PDEATHSIG:
  5873. return get_errno(prctl(PR_SET_PDEATHSIG, target_to_host_signal(arg2),
  5874. arg3, arg4, arg5));
  5875. case PR_GET_NAME:
  5876. {
  5877. void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
  5878. if (!name) {
  5879. return -TARGET_EFAULT;
  5880. }
  5881. ret = get_errno(prctl(PR_GET_NAME, (uintptr_t)name,
  5882. arg3, arg4, arg5));
  5883. unlock_user(name, arg2, 16);
  5884. return ret;
  5885. }
  5886. case PR_SET_NAME:
  5887. {
  5888. void *name = lock_user(VERIFY_READ, arg2, 16, 1);
  5889. if (!name) {
  5890. return -TARGET_EFAULT;
  5891. }
  5892. ret = get_errno(prctl(PR_SET_NAME, (uintptr_t)name,
  5893. arg3, arg4, arg5));
  5894. unlock_user(name, arg2, 0);
  5895. return ret;
  5896. }
  5897. case PR_GET_FP_MODE:
  5898. return do_prctl_get_fp_mode(env);
  5899. case PR_SET_FP_MODE:
  5900. return do_prctl_set_fp_mode(env, arg2);
  5901. case PR_SVE_GET_VL:
  5902. return do_prctl_sve_get_vl(env);
  5903. case PR_SVE_SET_VL:
  5904. return do_prctl_sve_set_vl(env, arg2);
  5905. case PR_SME_GET_VL:
  5906. return do_prctl_sme_get_vl(env);
  5907. case PR_SME_SET_VL:
  5908. return do_prctl_sme_set_vl(env, arg2);
  5909. case PR_PAC_RESET_KEYS:
  5910. if (arg3 || arg4 || arg5) {
  5911. return -TARGET_EINVAL;
  5912. }
  5913. return do_prctl_reset_keys(env, arg2);
  5914. case PR_SET_TAGGED_ADDR_CTRL:
  5915. if (arg3 || arg4 || arg5) {
  5916. return -TARGET_EINVAL;
  5917. }
  5918. return do_prctl_set_tagged_addr_ctrl(env, arg2);
  5919. case PR_GET_TAGGED_ADDR_CTRL:
  5920. if (arg2 || arg3 || arg4 || arg5) {
  5921. return -TARGET_EINVAL;
  5922. }
  5923. return do_prctl_get_tagged_addr_ctrl(env);
  5924. case PR_GET_UNALIGN:
  5925. return do_prctl_get_unalign(env, arg2);
  5926. case PR_SET_UNALIGN:
  5927. return do_prctl_set_unalign(env, arg2);
  5928. case PR_CAP_AMBIENT:
  5929. case PR_CAPBSET_READ:
  5930. case PR_CAPBSET_DROP:
  5931. case PR_GET_DUMPABLE:
  5932. case PR_SET_DUMPABLE:
  5933. case PR_GET_KEEPCAPS:
  5934. case PR_SET_KEEPCAPS:
  5935. case PR_GET_SECUREBITS:
  5936. case PR_SET_SECUREBITS:
  5937. case PR_GET_TIMING:
  5938. case PR_SET_TIMING:
  5939. case PR_GET_TIMERSLACK:
  5940. case PR_SET_TIMERSLACK:
  5941. case PR_MCE_KILL:
  5942. case PR_MCE_KILL_GET:
  5943. case PR_GET_NO_NEW_PRIVS:
  5944. case PR_SET_NO_NEW_PRIVS:
  5945. case PR_GET_IO_FLUSHER:
  5946. case PR_SET_IO_FLUSHER:
  5947. /* Some prctl options have no pointer arguments and we can pass on. */
  5948. return get_errno(prctl(option, arg2, arg3, arg4, arg5));
  5949. case PR_GET_CHILD_SUBREAPER:
  5950. case PR_SET_CHILD_SUBREAPER:
  5951. case PR_GET_SPECULATION_CTRL:
  5952. case PR_SET_SPECULATION_CTRL:
  5953. case PR_GET_TID_ADDRESS:
  5954. /* TODO */
  5955. return -TARGET_EINVAL;
  5956. case PR_GET_FPEXC:
  5957. case PR_SET_FPEXC:
  5958. /* Was used for SPE on PowerPC. */
  5959. return -TARGET_EINVAL;
  5960. case PR_GET_ENDIAN:
  5961. case PR_SET_ENDIAN:
  5962. case PR_GET_FPEMU:
  5963. case PR_SET_FPEMU:
  5964. case PR_SET_MM:
  5965. case PR_GET_SECCOMP:
  5966. case PR_SET_SECCOMP:
  5967. case PR_SET_SYSCALL_USER_DISPATCH:
  5968. case PR_GET_THP_DISABLE:
  5969. case PR_SET_THP_DISABLE:
  5970. case PR_GET_TSC:
  5971. case PR_SET_TSC:
  5972. /* Disable to prevent the target disabling stuff we need. */
  5973. return -TARGET_EINVAL;
  5974. default:
  5975. qemu_log_mask(LOG_UNIMP, "Unsupported prctl: " TARGET_ABI_FMT_ld "\n",
  5976. option);
  5977. return -TARGET_EINVAL;
  5978. }
  5979. }
  5980. #define NEW_STACK_SIZE 0x40000
  5981. static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
  5982. typedef struct {
  5983. CPUArchState *env;
  5984. pthread_mutex_t mutex;
  5985. pthread_cond_t cond;
  5986. pthread_t thread;
  5987. uint32_t tid;
  5988. abi_ulong child_tidptr;
  5989. abi_ulong parent_tidptr;
  5990. sigset_t sigmask;
  5991. } new_thread_info;
  5992. static void *clone_func(void *arg)
  5993. {
  5994. new_thread_info *info = arg;
  5995. CPUArchState *env;
  5996. CPUState *cpu;
  5997. TaskState *ts;
  5998. rcu_register_thread();
  5999. tcg_register_thread();
  6000. env = info->env;
  6001. cpu = env_cpu(env);
  6002. thread_cpu = cpu;
  6003. ts = (TaskState *)cpu->opaque;
  6004. info->tid = sys_gettid();
  6005. task_settid(ts);
  6006. if (info->child_tidptr)
  6007. put_user_u32(info->tid, info->child_tidptr);
  6008. if (info->parent_tidptr)
  6009. put_user_u32(info->tid, info->parent_tidptr);
  6010. qemu_guest_random_seed_thread_part2(cpu->random_seed);
  6011. /* Enable signals. */
  6012. sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
  6013. /* Signal to the parent that we're ready. */
  6014. pthread_mutex_lock(&info->mutex);
  6015. pthread_cond_broadcast(&info->cond);
  6016. pthread_mutex_unlock(&info->mutex);
  6017. /* Wait until the parent has finished initializing the tls state. */
  6018. pthread_mutex_lock(&clone_lock);
  6019. pthread_mutex_unlock(&clone_lock);
  6020. cpu_loop(env);
  6021. /* never exits */
  6022. return NULL;
  6023. }
  6024. /* do_fork() Must return host values and target errnos (unlike most
  6025. do_*() functions). */
  6026. static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
  6027. abi_ulong parent_tidptr, target_ulong newtls,
  6028. abi_ulong child_tidptr)
  6029. {
  6030. CPUState *cpu = env_cpu(env);
  6031. int ret;
  6032. TaskState *ts;
  6033. CPUState *new_cpu;
  6034. CPUArchState *new_env;
  6035. sigset_t sigmask;
  6036. flags &= ~CLONE_IGNORED_FLAGS;
  6037. /* Emulate vfork() with fork() */
  6038. if (flags & CLONE_VFORK)
  6039. flags &= ~(CLONE_VFORK | CLONE_VM);
  6040. if (flags & CLONE_VM) {
  6041. TaskState *parent_ts = (TaskState *)cpu->opaque;
  6042. new_thread_info info;
  6043. pthread_attr_t attr;
  6044. if (((flags & CLONE_THREAD_FLAGS) != CLONE_THREAD_FLAGS) ||
  6045. (flags & CLONE_INVALID_THREAD_FLAGS)) {
  6046. return -TARGET_EINVAL;
  6047. }
  6048. ts = g_new0(TaskState, 1);
  6049. init_task_state(ts);
  6050. /* Grab a mutex so that thread setup appears atomic. */
  6051. pthread_mutex_lock(&clone_lock);
  6052. /*
  6053. * If this is our first additional thread, we need to ensure we
  6054. * generate code for parallel execution and flush old translations.
  6055. * Do this now so that the copy gets CF_PARALLEL too.
  6056. */
  6057. if (!(cpu->tcg_cflags & CF_PARALLEL)) {
  6058. cpu->tcg_cflags |= CF_PARALLEL;
  6059. tb_flush(cpu);
  6060. }
  6061. /* we create a new CPU instance. */
  6062. new_env = cpu_copy(env);
  6063. /* Init regs that differ from the parent. */
  6064. cpu_clone_regs_child(new_env, newsp, flags);
  6065. cpu_clone_regs_parent(env, flags);
  6066. new_cpu = env_cpu(new_env);
  6067. new_cpu->opaque = ts;
  6068. ts->bprm = parent_ts->bprm;
  6069. ts->info = parent_ts->info;
  6070. ts->signal_mask = parent_ts->signal_mask;
  6071. if (flags & CLONE_CHILD_CLEARTID) {
  6072. ts->child_tidptr = child_tidptr;
  6073. }
  6074. if (flags & CLONE_SETTLS) {
  6075. cpu_set_tls (new_env, newtls);
  6076. }
  6077. memset(&info, 0, sizeof(info));
  6078. pthread_mutex_init(&info.mutex, NULL);
  6079. pthread_mutex_lock(&info.mutex);
  6080. pthread_cond_init(&info.cond, NULL);
  6081. info.env = new_env;
  6082. if (flags & CLONE_CHILD_SETTID) {
  6083. info.child_tidptr = child_tidptr;
  6084. }
  6085. if (flags & CLONE_PARENT_SETTID) {
  6086. info.parent_tidptr = parent_tidptr;
  6087. }
  6088. ret = pthread_attr_init(&attr);
  6089. ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
  6090. ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  6091. /* It is not safe to deliver signals until the child has finished
  6092. initializing, so temporarily block all signals. */
  6093. sigfillset(&sigmask);
  6094. sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
  6095. cpu->random_seed = qemu_guest_random_seed_thread_part1();
  6096. ret = pthread_create(&info.thread, &attr, clone_func, &info);
  6097. /* TODO: Free new CPU state if thread creation failed. */
  6098. sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
  6099. pthread_attr_destroy(&attr);
  6100. if (ret == 0) {
  6101. /* Wait for the child to initialize. */
  6102. pthread_cond_wait(&info.cond, &info.mutex);
  6103. ret = info.tid;
  6104. } else {
  6105. ret = -1;
  6106. }
  6107. pthread_mutex_unlock(&info.mutex);
  6108. pthread_cond_destroy(&info.cond);
  6109. pthread_mutex_destroy(&info.mutex);
  6110. pthread_mutex_unlock(&clone_lock);
  6111. } else {
  6112. /* if no CLONE_VM, we consider it is a fork */
  6113. if (flags & CLONE_INVALID_FORK_FLAGS) {
  6114. return -TARGET_EINVAL;
  6115. }
  6116. /* We can't support custom termination signals */
  6117. if ((flags & CSIGNAL) != TARGET_SIGCHLD) {
  6118. return -TARGET_EINVAL;
  6119. }
  6120. #if !defined(__NR_pidfd_open) || !defined(TARGET_NR_pidfd_open)
  6121. if (flags & CLONE_PIDFD) {
  6122. return -TARGET_EINVAL;
  6123. }
  6124. #endif
  6125. /* Can not allow CLONE_PIDFD with CLONE_PARENT_SETTID */
  6126. if ((flags & CLONE_PIDFD) && (flags & CLONE_PARENT_SETTID)) {
  6127. return -TARGET_EINVAL;
  6128. }
  6129. if (block_signals()) {
  6130. return -QEMU_ERESTARTSYS;
  6131. }
  6132. fork_start();
  6133. ret = fork();
  6134. if (ret == 0) {
  6135. /* Child Process. */
  6136. cpu_clone_regs_child(env, newsp, flags);
  6137. fork_end(1);
  6138. /* There is a race condition here. The parent process could
  6139. theoretically read the TID in the child process before the child
  6140. tid is set. This would require using either ptrace
  6141. (not implemented) or having *_tidptr to point at a shared memory
  6142. mapping. We can't repeat the spinlock hack used above because
  6143. the child process gets its own copy of the lock. */
  6144. if (flags & CLONE_CHILD_SETTID)
  6145. put_user_u32(sys_gettid(), child_tidptr);
  6146. if (flags & CLONE_PARENT_SETTID)
  6147. put_user_u32(sys_gettid(), parent_tidptr);
  6148. ts = (TaskState *)cpu->opaque;
  6149. if (flags & CLONE_SETTLS)
  6150. cpu_set_tls (env, newtls);
  6151. if (flags & CLONE_CHILD_CLEARTID)
  6152. ts->child_tidptr = child_tidptr;
  6153. } else {
  6154. cpu_clone_regs_parent(env, flags);
  6155. if (flags & CLONE_PIDFD) {
  6156. int pid_fd = 0;
  6157. #if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
  6158. int pid_child = ret;
  6159. pid_fd = pidfd_open(pid_child, 0);
  6160. if (pid_fd >= 0) {
  6161. fcntl(pid_fd, F_SETFD, fcntl(pid_fd, F_GETFL)
  6162. | FD_CLOEXEC);
  6163. } else {
  6164. pid_fd = 0;
  6165. }
  6166. #endif
  6167. put_user_u32(pid_fd, parent_tidptr);
  6168. }
  6169. fork_end(0);
  6170. }
  6171. g_assert(!cpu_in_exclusive_context(cpu));
  6172. }
  6173. return ret;
  6174. }
  6175. /* warning : doesn't handle linux specific flags... */
  6176. static int target_to_host_fcntl_cmd(int cmd)
  6177. {
  6178. int ret;
  6179. switch(cmd) {
  6180. case TARGET_F_DUPFD:
  6181. case TARGET_F_GETFD:
  6182. case TARGET_F_SETFD:
  6183. case TARGET_F_GETFL:
  6184. case TARGET_F_SETFL:
  6185. case TARGET_F_OFD_GETLK:
  6186. case TARGET_F_OFD_SETLK:
  6187. case TARGET_F_OFD_SETLKW:
  6188. ret = cmd;
  6189. break;
  6190. case TARGET_F_GETLK:
  6191. ret = F_GETLK64;
  6192. break;
  6193. case TARGET_F_SETLK:
  6194. ret = F_SETLK64;
  6195. break;
  6196. case TARGET_F_SETLKW:
  6197. ret = F_SETLKW64;
  6198. break;
  6199. case TARGET_F_GETOWN:
  6200. ret = F_GETOWN;
  6201. break;
  6202. case TARGET_F_SETOWN:
  6203. ret = F_SETOWN;
  6204. break;
  6205. case TARGET_F_GETSIG:
  6206. ret = F_GETSIG;
  6207. break;
  6208. case TARGET_F_SETSIG:
  6209. ret = F_SETSIG;
  6210. break;
  6211. #if TARGET_ABI_BITS == 32
  6212. case TARGET_F_GETLK64:
  6213. ret = F_GETLK64;
  6214. break;
  6215. case TARGET_F_SETLK64:
  6216. ret = F_SETLK64;
  6217. break;
  6218. case TARGET_F_SETLKW64:
  6219. ret = F_SETLKW64;
  6220. break;
  6221. #endif
  6222. case TARGET_F_SETLEASE:
  6223. ret = F_SETLEASE;
  6224. break;
  6225. case TARGET_F_GETLEASE:
  6226. ret = F_GETLEASE;
  6227. break;
  6228. #ifdef F_DUPFD_CLOEXEC
  6229. case TARGET_F_DUPFD_CLOEXEC:
  6230. ret = F_DUPFD_CLOEXEC;
  6231. break;
  6232. #endif
  6233. case TARGET_F_NOTIFY:
  6234. ret = F_NOTIFY;
  6235. break;
  6236. #ifdef F_GETOWN_EX
  6237. case TARGET_F_GETOWN_EX:
  6238. ret = F_GETOWN_EX;
  6239. break;
  6240. #endif
  6241. #ifdef F_SETOWN_EX
  6242. case TARGET_F_SETOWN_EX:
  6243. ret = F_SETOWN_EX;
  6244. break;
  6245. #endif
  6246. #ifdef F_SETPIPE_SZ
  6247. case TARGET_F_SETPIPE_SZ:
  6248. ret = F_SETPIPE_SZ;
  6249. break;
  6250. case TARGET_F_GETPIPE_SZ:
  6251. ret = F_GETPIPE_SZ;
  6252. break;
  6253. #endif
  6254. #ifdef F_ADD_SEALS
  6255. case TARGET_F_ADD_SEALS:
  6256. ret = F_ADD_SEALS;
  6257. break;
  6258. case TARGET_F_GET_SEALS:
  6259. ret = F_GET_SEALS;
  6260. break;
  6261. #endif
  6262. default:
  6263. ret = -TARGET_EINVAL;
  6264. break;
  6265. }
  6266. #if defined(__powerpc64__)
  6267. /* On PPC64, glibc headers has the F_*LK* defined to 12, 13 and 14 and
  6268. * is not supported by kernel. The glibc fcntl call actually adjusts
  6269. * them to 5, 6 and 7 before making the syscall(). Since we make the
  6270. * syscall directly, adjust to what is supported by the kernel.
  6271. */
  6272. if (ret >= F_GETLK64 && ret <= F_SETLKW64) {
  6273. ret -= F_GETLK64 - 5;
  6274. }
  6275. #endif
  6276. return ret;
  6277. }
  6278. #define FLOCK_TRANSTBL \
  6279. switch (type) { \
  6280. TRANSTBL_CONVERT(F_RDLCK); \
  6281. TRANSTBL_CONVERT(F_WRLCK); \
  6282. TRANSTBL_CONVERT(F_UNLCK); \
  6283. }
  6284. static int target_to_host_flock(int type)
  6285. {
  6286. #define TRANSTBL_CONVERT(a) case TARGET_##a: return a
  6287. FLOCK_TRANSTBL
  6288. #undef TRANSTBL_CONVERT
  6289. return -TARGET_EINVAL;
  6290. }
  6291. static int host_to_target_flock(int type)
  6292. {
  6293. #define TRANSTBL_CONVERT(a) case a: return TARGET_##a
  6294. FLOCK_TRANSTBL
  6295. #undef TRANSTBL_CONVERT
  6296. /* if we don't know how to convert the value coming
  6297. * from the host we copy to the target field as-is
  6298. */
  6299. return type;
  6300. }
  6301. static inline abi_long copy_from_user_flock(struct flock64 *fl,
  6302. abi_ulong target_flock_addr)
  6303. {
  6304. struct target_flock *target_fl;
  6305. int l_type;
  6306. if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
  6307. return -TARGET_EFAULT;
  6308. }
  6309. __get_user(l_type, &target_fl->l_type);
  6310. l_type = target_to_host_flock(l_type);
  6311. if (l_type < 0) {
  6312. return l_type;
  6313. }
  6314. fl->l_type = l_type;
  6315. __get_user(fl->l_whence, &target_fl->l_whence);
  6316. __get_user(fl->l_start, &target_fl->l_start);
  6317. __get_user(fl->l_len, &target_fl->l_len);
  6318. __get_user(fl->l_pid, &target_fl->l_pid);
  6319. unlock_user_struct(target_fl, target_flock_addr, 0);
  6320. return 0;
  6321. }
  6322. static inline abi_long copy_to_user_flock(abi_ulong target_flock_addr,
  6323. const struct flock64 *fl)
  6324. {
  6325. struct target_flock *target_fl;
  6326. short l_type;
  6327. if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
  6328. return -TARGET_EFAULT;
  6329. }
  6330. l_type = host_to_target_flock(fl->l_type);
  6331. __put_user(l_type, &target_fl->l_type);
  6332. __put_user(fl->l_whence, &target_fl->l_whence);
  6333. __put_user(fl->l_start, &target_fl->l_start);
  6334. __put_user(fl->l_len, &target_fl->l_len);
  6335. __put_user(fl->l_pid, &target_fl->l_pid);
  6336. unlock_user_struct(target_fl, target_flock_addr, 1);
  6337. return 0;
  6338. }
  6339. typedef abi_long from_flock64_fn(struct flock64 *fl, abi_ulong target_addr);
  6340. typedef abi_long to_flock64_fn(abi_ulong target_addr, const struct flock64 *fl);
  6341. #if defined(TARGET_ARM) && TARGET_ABI_BITS == 32
  6342. struct target_oabi_flock64 {
  6343. abi_short l_type;
  6344. abi_short l_whence;
  6345. abi_llong l_start;
  6346. abi_llong l_len;
  6347. abi_int l_pid;
  6348. } QEMU_PACKED;
  6349. static inline abi_long copy_from_user_oabi_flock64(struct flock64 *fl,
  6350. abi_ulong target_flock_addr)
  6351. {
  6352. struct target_oabi_flock64 *target_fl;
  6353. int l_type;
  6354. if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
  6355. return -TARGET_EFAULT;
  6356. }
  6357. __get_user(l_type, &target_fl->l_type);
  6358. l_type = target_to_host_flock(l_type);
  6359. if (l_type < 0) {
  6360. return l_type;
  6361. }
  6362. fl->l_type = l_type;
  6363. __get_user(fl->l_whence, &target_fl->l_whence);
  6364. __get_user(fl->l_start, &target_fl->l_start);
  6365. __get_user(fl->l_len, &target_fl->l_len);
  6366. __get_user(fl->l_pid, &target_fl->l_pid);
  6367. unlock_user_struct(target_fl, target_flock_addr, 0);
  6368. return 0;
  6369. }
  6370. static inline abi_long copy_to_user_oabi_flock64(abi_ulong target_flock_addr,
  6371. const struct flock64 *fl)
  6372. {
  6373. struct target_oabi_flock64 *target_fl;
  6374. short l_type;
  6375. if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
  6376. return -TARGET_EFAULT;
  6377. }
  6378. l_type = host_to_target_flock(fl->l_type);
  6379. __put_user(l_type, &target_fl->l_type);
  6380. __put_user(fl->l_whence, &target_fl->l_whence);
  6381. __put_user(fl->l_start, &target_fl->l_start);
  6382. __put_user(fl->l_len, &target_fl->l_len);
  6383. __put_user(fl->l_pid, &target_fl->l_pid);
  6384. unlock_user_struct(target_fl, target_flock_addr, 1);
  6385. return 0;
  6386. }
  6387. #endif
  6388. static inline abi_long copy_from_user_flock64(struct flock64 *fl,
  6389. abi_ulong target_flock_addr)
  6390. {
  6391. struct target_flock64 *target_fl;
  6392. int l_type;
  6393. if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
  6394. return -TARGET_EFAULT;
  6395. }
  6396. __get_user(l_type, &target_fl->l_type);
  6397. l_type = target_to_host_flock(l_type);
  6398. if (l_type < 0) {
  6399. return l_type;
  6400. }
  6401. fl->l_type = l_type;
  6402. __get_user(fl->l_whence, &target_fl->l_whence);
  6403. __get_user(fl->l_start, &target_fl->l_start);
  6404. __get_user(fl->l_len, &target_fl->l_len);
  6405. __get_user(fl->l_pid, &target_fl->l_pid);
  6406. unlock_user_struct(target_fl, target_flock_addr, 0);
  6407. return 0;
  6408. }
  6409. static inline abi_long copy_to_user_flock64(abi_ulong target_flock_addr,
  6410. const struct flock64 *fl)
  6411. {
  6412. struct target_flock64 *target_fl;
  6413. short l_type;
  6414. if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
  6415. return -TARGET_EFAULT;
  6416. }
  6417. l_type = host_to_target_flock(fl->l_type);
  6418. __put_user(l_type, &target_fl->l_type);
  6419. __put_user(fl->l_whence, &target_fl->l_whence);
  6420. __put_user(fl->l_start, &target_fl->l_start);
  6421. __put_user(fl->l_len, &target_fl->l_len);
  6422. __put_user(fl->l_pid, &target_fl->l_pid);
  6423. unlock_user_struct(target_fl, target_flock_addr, 1);
  6424. return 0;
  6425. }
  6426. static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
  6427. {
  6428. struct flock64 fl64;
  6429. #ifdef F_GETOWN_EX
  6430. struct f_owner_ex fox;
  6431. struct target_f_owner_ex *target_fox;
  6432. #endif
  6433. abi_long ret;
  6434. int host_cmd = target_to_host_fcntl_cmd(cmd);
  6435. if (host_cmd == -TARGET_EINVAL)
  6436. return host_cmd;
  6437. switch(cmd) {
  6438. case TARGET_F_GETLK:
  6439. ret = copy_from_user_flock(&fl64, arg);
  6440. if (ret) {
  6441. return ret;
  6442. }
  6443. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  6444. if (ret == 0) {
  6445. ret = copy_to_user_flock(arg, &fl64);
  6446. }
  6447. break;
  6448. case TARGET_F_SETLK:
  6449. case TARGET_F_SETLKW:
  6450. ret = copy_from_user_flock(&fl64, arg);
  6451. if (ret) {
  6452. return ret;
  6453. }
  6454. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  6455. break;
  6456. case TARGET_F_GETLK64:
  6457. case TARGET_F_OFD_GETLK:
  6458. ret = copy_from_user_flock64(&fl64, arg);
  6459. if (ret) {
  6460. return ret;
  6461. }
  6462. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  6463. if (ret == 0) {
  6464. ret = copy_to_user_flock64(arg, &fl64);
  6465. }
  6466. break;
  6467. case TARGET_F_SETLK64:
  6468. case TARGET_F_SETLKW64:
  6469. case TARGET_F_OFD_SETLK:
  6470. case TARGET_F_OFD_SETLKW:
  6471. ret = copy_from_user_flock64(&fl64, arg);
  6472. if (ret) {
  6473. return ret;
  6474. }
  6475. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  6476. break;
  6477. case TARGET_F_GETFL:
  6478. ret = get_errno(safe_fcntl(fd, host_cmd, arg));
  6479. if (ret >= 0) {
  6480. ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
  6481. /* tell 32-bit guests it uses largefile on 64-bit hosts: */
  6482. if (O_LARGEFILE == 0 && HOST_LONG_BITS == 64) {
  6483. ret |= TARGET_O_LARGEFILE;
  6484. }
  6485. }
  6486. break;
  6487. case TARGET_F_SETFL:
  6488. ret = get_errno(safe_fcntl(fd, host_cmd,
  6489. target_to_host_bitmask(arg,
  6490. fcntl_flags_tbl)));
  6491. break;
  6492. #ifdef F_GETOWN_EX
  6493. case TARGET_F_GETOWN_EX:
  6494. ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
  6495. if (ret >= 0) {
  6496. if (!lock_user_struct(VERIFY_WRITE, target_fox, arg, 0))
  6497. return -TARGET_EFAULT;
  6498. target_fox->type = tswap32(fox.type);
  6499. target_fox->pid = tswap32(fox.pid);
  6500. unlock_user_struct(target_fox, arg, 1);
  6501. }
  6502. break;
  6503. #endif
  6504. #ifdef F_SETOWN_EX
  6505. case TARGET_F_SETOWN_EX:
  6506. if (!lock_user_struct(VERIFY_READ, target_fox, arg, 1))
  6507. return -TARGET_EFAULT;
  6508. fox.type = tswap32(target_fox->type);
  6509. fox.pid = tswap32(target_fox->pid);
  6510. unlock_user_struct(target_fox, arg, 0);
  6511. ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
  6512. break;
  6513. #endif
  6514. case TARGET_F_SETSIG:
  6515. ret = get_errno(safe_fcntl(fd, host_cmd, target_to_host_signal(arg)));
  6516. break;
  6517. case TARGET_F_GETSIG:
  6518. ret = host_to_target_signal(get_errno(safe_fcntl(fd, host_cmd, arg)));
  6519. break;
  6520. case TARGET_F_SETOWN:
  6521. case TARGET_F_GETOWN:
  6522. case TARGET_F_SETLEASE:
  6523. case TARGET_F_GETLEASE:
  6524. case TARGET_F_SETPIPE_SZ:
  6525. case TARGET_F_GETPIPE_SZ:
  6526. case TARGET_F_ADD_SEALS:
  6527. case TARGET_F_GET_SEALS:
  6528. ret = get_errno(safe_fcntl(fd, host_cmd, arg));
  6529. break;
  6530. default:
  6531. ret = get_errno(safe_fcntl(fd, cmd, arg));
  6532. break;
  6533. }
  6534. return ret;
  6535. }
  6536. #ifdef USE_UID16
  6537. static inline int high2lowuid(int uid)
  6538. {
  6539. if (uid > 65535)
  6540. return 65534;
  6541. else
  6542. return uid;
  6543. }
  6544. static inline int high2lowgid(int gid)
  6545. {
  6546. if (gid > 65535)
  6547. return 65534;
  6548. else
  6549. return gid;
  6550. }
  6551. static inline int low2highuid(int uid)
  6552. {
  6553. if ((int16_t)uid == -1)
  6554. return -1;
  6555. else
  6556. return uid;
  6557. }
  6558. static inline int low2highgid(int gid)
  6559. {
  6560. if ((int16_t)gid == -1)
  6561. return -1;
  6562. else
  6563. return gid;
  6564. }
  6565. static inline int tswapid(int id)
  6566. {
  6567. return tswap16(id);
  6568. }
  6569. #define put_user_id(x, gaddr) put_user_u16(x, gaddr)
  6570. #else /* !USE_UID16 */
  6571. static inline int high2lowuid(int uid)
  6572. {
  6573. return uid;
  6574. }
  6575. static inline int high2lowgid(int gid)
  6576. {
  6577. return gid;
  6578. }
  6579. static inline int low2highuid(int uid)
  6580. {
  6581. return uid;
  6582. }
  6583. static inline int low2highgid(int gid)
  6584. {
  6585. return gid;
  6586. }
  6587. static inline int tswapid(int id)
  6588. {
  6589. return tswap32(id);
  6590. }
  6591. #define put_user_id(x, gaddr) put_user_u32(x, gaddr)
  6592. #endif /* USE_UID16 */
  6593. /* We must do direct syscalls for setting UID/GID, because we want to
  6594. * implement the Linux system call semantics of "change only for this thread",
  6595. * not the libc/POSIX semantics of "change for all threads in process".
  6596. * (See http://ewontfix.com/17/ for more details.)
  6597. * We use the 32-bit version of the syscalls if present; if it is not
  6598. * then either the host architecture supports 32-bit UIDs natively with
  6599. * the standard syscall, or the 16-bit UID is the best we can do.
  6600. */
  6601. #ifdef __NR_setuid32
  6602. #define __NR_sys_setuid __NR_setuid32
  6603. #else
  6604. #define __NR_sys_setuid __NR_setuid
  6605. #endif
  6606. #ifdef __NR_setgid32
  6607. #define __NR_sys_setgid __NR_setgid32
  6608. #else
  6609. #define __NR_sys_setgid __NR_setgid
  6610. #endif
  6611. #ifdef __NR_setresuid32
  6612. #define __NR_sys_setresuid __NR_setresuid32
  6613. #else
  6614. #define __NR_sys_setresuid __NR_setresuid
  6615. #endif
  6616. #ifdef __NR_setresgid32
  6617. #define __NR_sys_setresgid __NR_setresgid32
  6618. #else
  6619. #define __NR_sys_setresgid __NR_setresgid
  6620. #endif
  6621. _syscall1(int, sys_setuid, uid_t, uid)
  6622. _syscall1(int, sys_setgid, gid_t, gid)
  6623. _syscall3(int, sys_setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
  6624. _syscall3(int, sys_setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
  6625. void syscall_init(void)
  6626. {
  6627. IOCTLEntry *ie;
  6628. const argtype *arg_type;
  6629. int size;
  6630. thunk_init(STRUCT_MAX);
  6631. #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
  6632. #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
  6633. #include "syscall_types.h"
  6634. #undef STRUCT
  6635. #undef STRUCT_SPECIAL
  6636. /* we patch the ioctl size if necessary. We rely on the fact that
  6637. no ioctl has all the bits at '1' in the size field */
  6638. ie = ioctl_entries;
  6639. while (ie->target_cmd != 0) {
  6640. if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
  6641. TARGET_IOC_SIZEMASK) {
  6642. arg_type = ie->arg_type;
  6643. if (arg_type[0] != TYPE_PTR) {
  6644. fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
  6645. ie->target_cmd);
  6646. exit(1);
  6647. }
  6648. arg_type++;
  6649. size = thunk_type_size(arg_type, 0);
  6650. ie->target_cmd = (ie->target_cmd &
  6651. ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
  6652. (size << TARGET_IOC_SIZESHIFT);
  6653. }
  6654. /* automatic consistency check if same arch */
  6655. #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
  6656. (defined(__x86_64__) && defined(TARGET_X86_64))
  6657. if (unlikely(ie->target_cmd != ie->host_cmd)) {
  6658. fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
  6659. ie->name, ie->target_cmd, ie->host_cmd);
  6660. }
  6661. #endif
  6662. ie++;
  6663. }
  6664. }
  6665. #ifdef TARGET_NR_truncate64
  6666. static inline abi_long target_truncate64(CPUArchState *cpu_env, const char *arg1,
  6667. abi_long arg2,
  6668. abi_long arg3,
  6669. abi_long arg4)
  6670. {
  6671. if (regpairs_aligned(cpu_env, TARGET_NR_truncate64)) {
  6672. arg2 = arg3;
  6673. arg3 = arg4;
  6674. }
  6675. return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
  6676. }
  6677. #endif
  6678. #ifdef TARGET_NR_ftruncate64
  6679. static inline abi_long target_ftruncate64(CPUArchState *cpu_env, abi_long arg1,
  6680. abi_long arg2,
  6681. abi_long arg3,
  6682. abi_long arg4)
  6683. {
  6684. if (regpairs_aligned(cpu_env, TARGET_NR_ftruncate64)) {
  6685. arg2 = arg3;
  6686. arg3 = arg4;
  6687. }
  6688. return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
  6689. }
  6690. #endif
  6691. #if defined(TARGET_NR_timer_settime) || \
  6692. (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
  6693. static inline abi_long target_to_host_itimerspec(struct itimerspec *host_its,
  6694. abi_ulong target_addr)
  6695. {
  6696. if (target_to_host_timespec(&host_its->it_interval, target_addr +
  6697. offsetof(struct target_itimerspec,
  6698. it_interval)) ||
  6699. target_to_host_timespec(&host_its->it_value, target_addr +
  6700. offsetof(struct target_itimerspec,
  6701. it_value))) {
  6702. return -TARGET_EFAULT;
  6703. }
  6704. return 0;
  6705. }
  6706. #endif
  6707. #if defined(TARGET_NR_timer_settime64) || \
  6708. (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD))
  6709. static inline abi_long target_to_host_itimerspec64(struct itimerspec *host_its,
  6710. abi_ulong target_addr)
  6711. {
  6712. if (target_to_host_timespec64(&host_its->it_interval, target_addr +
  6713. offsetof(struct target__kernel_itimerspec,
  6714. it_interval)) ||
  6715. target_to_host_timespec64(&host_its->it_value, target_addr +
  6716. offsetof(struct target__kernel_itimerspec,
  6717. it_value))) {
  6718. return -TARGET_EFAULT;
  6719. }
  6720. return 0;
  6721. }
  6722. #endif
  6723. #if ((defined(TARGET_NR_timerfd_gettime) || \
  6724. defined(TARGET_NR_timerfd_settime)) && defined(CONFIG_TIMERFD)) || \
  6725. defined(TARGET_NR_timer_gettime) || defined(TARGET_NR_timer_settime)
  6726. static inline abi_long host_to_target_itimerspec(abi_ulong target_addr,
  6727. struct itimerspec *host_its)
  6728. {
  6729. if (host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
  6730. it_interval),
  6731. &host_its->it_interval) ||
  6732. host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
  6733. it_value),
  6734. &host_its->it_value)) {
  6735. return -TARGET_EFAULT;
  6736. }
  6737. return 0;
  6738. }
  6739. #endif
  6740. #if ((defined(TARGET_NR_timerfd_gettime64) || \
  6741. defined(TARGET_NR_timerfd_settime64)) && defined(CONFIG_TIMERFD)) || \
  6742. defined(TARGET_NR_timer_gettime64) || defined(TARGET_NR_timer_settime64)
  6743. static inline abi_long host_to_target_itimerspec64(abi_ulong target_addr,
  6744. struct itimerspec *host_its)
  6745. {
  6746. if (host_to_target_timespec64(target_addr +
  6747. offsetof(struct target__kernel_itimerspec,
  6748. it_interval),
  6749. &host_its->it_interval) ||
  6750. host_to_target_timespec64(target_addr +
  6751. offsetof(struct target__kernel_itimerspec,
  6752. it_value),
  6753. &host_its->it_value)) {
  6754. return -TARGET_EFAULT;
  6755. }
  6756. return 0;
  6757. }
  6758. #endif
  6759. #if defined(TARGET_NR_adjtimex) || \
  6760. (defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME))
  6761. static inline abi_long target_to_host_timex(struct timex *host_tx,
  6762. abi_long target_addr)
  6763. {
  6764. struct target_timex *target_tx;
  6765. if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) {
  6766. return -TARGET_EFAULT;
  6767. }
  6768. __get_user(host_tx->modes, &target_tx->modes);
  6769. __get_user(host_tx->offset, &target_tx->offset);
  6770. __get_user(host_tx->freq, &target_tx->freq);
  6771. __get_user(host_tx->maxerror, &target_tx->maxerror);
  6772. __get_user(host_tx->esterror, &target_tx->esterror);
  6773. __get_user(host_tx->status, &target_tx->status);
  6774. __get_user(host_tx->constant, &target_tx->constant);
  6775. __get_user(host_tx->precision, &target_tx->precision);
  6776. __get_user(host_tx->tolerance, &target_tx->tolerance);
  6777. __get_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
  6778. __get_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
  6779. __get_user(host_tx->tick, &target_tx->tick);
  6780. __get_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  6781. __get_user(host_tx->jitter, &target_tx->jitter);
  6782. __get_user(host_tx->shift, &target_tx->shift);
  6783. __get_user(host_tx->stabil, &target_tx->stabil);
  6784. __get_user(host_tx->jitcnt, &target_tx->jitcnt);
  6785. __get_user(host_tx->calcnt, &target_tx->calcnt);
  6786. __get_user(host_tx->errcnt, &target_tx->errcnt);
  6787. __get_user(host_tx->stbcnt, &target_tx->stbcnt);
  6788. __get_user(host_tx->tai, &target_tx->tai);
  6789. unlock_user_struct(target_tx, target_addr, 0);
  6790. return 0;
  6791. }
  6792. static inline abi_long host_to_target_timex(abi_long target_addr,
  6793. struct timex *host_tx)
  6794. {
  6795. struct target_timex *target_tx;
  6796. if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) {
  6797. return -TARGET_EFAULT;
  6798. }
  6799. __put_user(host_tx->modes, &target_tx->modes);
  6800. __put_user(host_tx->offset, &target_tx->offset);
  6801. __put_user(host_tx->freq, &target_tx->freq);
  6802. __put_user(host_tx->maxerror, &target_tx->maxerror);
  6803. __put_user(host_tx->esterror, &target_tx->esterror);
  6804. __put_user(host_tx->status, &target_tx->status);
  6805. __put_user(host_tx->constant, &target_tx->constant);
  6806. __put_user(host_tx->precision, &target_tx->precision);
  6807. __put_user(host_tx->tolerance, &target_tx->tolerance);
  6808. __put_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
  6809. __put_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
  6810. __put_user(host_tx->tick, &target_tx->tick);
  6811. __put_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  6812. __put_user(host_tx->jitter, &target_tx->jitter);
  6813. __put_user(host_tx->shift, &target_tx->shift);
  6814. __put_user(host_tx->stabil, &target_tx->stabil);
  6815. __put_user(host_tx->jitcnt, &target_tx->jitcnt);
  6816. __put_user(host_tx->calcnt, &target_tx->calcnt);
  6817. __put_user(host_tx->errcnt, &target_tx->errcnt);
  6818. __put_user(host_tx->stbcnt, &target_tx->stbcnt);
  6819. __put_user(host_tx->tai, &target_tx->tai);
  6820. unlock_user_struct(target_tx, target_addr, 1);
  6821. return 0;
  6822. }
  6823. #endif
  6824. #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
  6825. static inline abi_long target_to_host_timex64(struct timex *host_tx,
  6826. abi_long target_addr)
  6827. {
  6828. struct target__kernel_timex *target_tx;
  6829. if (copy_from_user_timeval64(&host_tx->time, target_addr +
  6830. offsetof(struct target__kernel_timex,
  6831. time))) {
  6832. return -TARGET_EFAULT;
  6833. }
  6834. if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) {
  6835. return -TARGET_EFAULT;
  6836. }
  6837. __get_user(host_tx->modes, &target_tx->modes);
  6838. __get_user(host_tx->offset, &target_tx->offset);
  6839. __get_user(host_tx->freq, &target_tx->freq);
  6840. __get_user(host_tx->maxerror, &target_tx->maxerror);
  6841. __get_user(host_tx->esterror, &target_tx->esterror);
  6842. __get_user(host_tx->status, &target_tx->status);
  6843. __get_user(host_tx->constant, &target_tx->constant);
  6844. __get_user(host_tx->precision, &target_tx->precision);
  6845. __get_user(host_tx->tolerance, &target_tx->tolerance);
  6846. __get_user(host_tx->tick, &target_tx->tick);
  6847. __get_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  6848. __get_user(host_tx->jitter, &target_tx->jitter);
  6849. __get_user(host_tx->shift, &target_tx->shift);
  6850. __get_user(host_tx->stabil, &target_tx->stabil);
  6851. __get_user(host_tx->jitcnt, &target_tx->jitcnt);
  6852. __get_user(host_tx->calcnt, &target_tx->calcnt);
  6853. __get_user(host_tx->errcnt, &target_tx->errcnt);
  6854. __get_user(host_tx->stbcnt, &target_tx->stbcnt);
  6855. __get_user(host_tx->tai, &target_tx->tai);
  6856. unlock_user_struct(target_tx, target_addr, 0);
  6857. return 0;
  6858. }
  6859. static inline abi_long host_to_target_timex64(abi_long target_addr,
  6860. struct timex *host_tx)
  6861. {
  6862. struct target__kernel_timex *target_tx;
  6863. if (copy_to_user_timeval64(target_addr +
  6864. offsetof(struct target__kernel_timex, time),
  6865. &host_tx->time)) {
  6866. return -TARGET_EFAULT;
  6867. }
  6868. if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) {
  6869. return -TARGET_EFAULT;
  6870. }
  6871. __put_user(host_tx->modes, &target_tx->modes);
  6872. __put_user(host_tx->offset, &target_tx->offset);
  6873. __put_user(host_tx->freq, &target_tx->freq);
  6874. __put_user(host_tx->maxerror, &target_tx->maxerror);
  6875. __put_user(host_tx->esterror, &target_tx->esterror);
  6876. __put_user(host_tx->status, &target_tx->status);
  6877. __put_user(host_tx->constant, &target_tx->constant);
  6878. __put_user(host_tx->precision, &target_tx->precision);
  6879. __put_user(host_tx->tolerance, &target_tx->tolerance);
  6880. __put_user(host_tx->tick, &target_tx->tick);
  6881. __put_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  6882. __put_user(host_tx->jitter, &target_tx->jitter);
  6883. __put_user(host_tx->shift, &target_tx->shift);
  6884. __put_user(host_tx->stabil, &target_tx->stabil);
  6885. __put_user(host_tx->jitcnt, &target_tx->jitcnt);
  6886. __put_user(host_tx->calcnt, &target_tx->calcnt);
  6887. __put_user(host_tx->errcnt, &target_tx->errcnt);
  6888. __put_user(host_tx->stbcnt, &target_tx->stbcnt);
  6889. __put_user(host_tx->tai, &target_tx->tai);
  6890. unlock_user_struct(target_tx, target_addr, 1);
  6891. return 0;
  6892. }
  6893. #endif
  6894. #ifndef HAVE_SIGEV_NOTIFY_THREAD_ID
  6895. #define sigev_notify_thread_id _sigev_un._tid
  6896. #endif
  6897. static inline abi_long target_to_host_sigevent(struct sigevent *host_sevp,
  6898. abi_ulong target_addr)
  6899. {
  6900. struct target_sigevent *target_sevp;
  6901. if (!lock_user_struct(VERIFY_READ, target_sevp, target_addr, 1)) {
  6902. return -TARGET_EFAULT;
  6903. }
  6904. /* This union is awkward on 64 bit systems because it has a 32 bit
  6905. * integer and a pointer in it; we follow the conversion approach
  6906. * used for handling sigval types in signal.c so the guest should get
  6907. * the correct value back even if we did a 64 bit byteswap and it's
  6908. * using the 32 bit integer.
  6909. */
  6910. host_sevp->sigev_value.sival_ptr =
  6911. (void *)(uintptr_t)tswapal(target_sevp->sigev_value.sival_ptr);
  6912. host_sevp->sigev_signo =
  6913. target_to_host_signal(tswap32(target_sevp->sigev_signo));
  6914. host_sevp->sigev_notify = tswap32(target_sevp->sigev_notify);
  6915. host_sevp->sigev_notify_thread_id = tswap32(target_sevp->_sigev_un._tid);
  6916. unlock_user_struct(target_sevp, target_addr, 1);
  6917. return 0;
  6918. }
  6919. #if defined(TARGET_NR_mlockall)
  6920. static inline int target_to_host_mlockall_arg(int arg)
  6921. {
  6922. int result = 0;
  6923. if (arg & TARGET_MCL_CURRENT) {
  6924. result |= MCL_CURRENT;
  6925. }
  6926. if (arg & TARGET_MCL_FUTURE) {
  6927. result |= MCL_FUTURE;
  6928. }
  6929. #ifdef MCL_ONFAULT
  6930. if (arg & TARGET_MCL_ONFAULT) {
  6931. result |= MCL_ONFAULT;
  6932. }
  6933. #endif
  6934. return result;
  6935. }
  6936. #endif
  6937. static inline int target_to_host_msync_arg(abi_long arg)
  6938. {
  6939. return ((arg & TARGET_MS_ASYNC) ? MS_ASYNC : 0) |
  6940. ((arg & TARGET_MS_INVALIDATE) ? MS_INVALIDATE : 0) |
  6941. ((arg & TARGET_MS_SYNC) ? MS_SYNC : 0) |
  6942. (arg & ~(TARGET_MS_ASYNC | TARGET_MS_INVALIDATE | TARGET_MS_SYNC));
  6943. }
  6944. #if (defined(TARGET_NR_stat64) || defined(TARGET_NR_lstat64) || \
  6945. defined(TARGET_NR_fstat64) || defined(TARGET_NR_fstatat64) || \
  6946. defined(TARGET_NR_newfstatat))
  6947. static inline abi_long host_to_target_stat64(CPUArchState *cpu_env,
  6948. abi_ulong target_addr,
  6949. struct stat *host_st)
  6950. {
  6951. #if defined(TARGET_ARM) && defined(TARGET_ABI32)
  6952. if (cpu_env->eabi) {
  6953. struct target_eabi_stat64 *target_st;
  6954. if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
  6955. return -TARGET_EFAULT;
  6956. memset(target_st, 0, sizeof(struct target_eabi_stat64));
  6957. __put_user(host_st->st_dev, &target_st->st_dev);
  6958. __put_user(host_st->st_ino, &target_st->st_ino);
  6959. #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
  6960. __put_user(host_st->st_ino, &target_st->__st_ino);
  6961. #endif
  6962. __put_user(host_st->st_mode, &target_st->st_mode);
  6963. __put_user(host_st->st_nlink, &target_st->st_nlink);
  6964. __put_user(host_st->st_uid, &target_st->st_uid);
  6965. __put_user(host_st->st_gid, &target_st->st_gid);
  6966. __put_user(host_st->st_rdev, &target_st->st_rdev);
  6967. __put_user(host_st->st_size, &target_st->st_size);
  6968. __put_user(host_st->st_blksize, &target_st->st_blksize);
  6969. __put_user(host_st->st_blocks, &target_st->st_blocks);
  6970. __put_user(host_st->st_atime, &target_st->target_st_atime);
  6971. __put_user(host_st->st_mtime, &target_st->target_st_mtime);
  6972. __put_user(host_st->st_ctime, &target_st->target_st_ctime);
  6973. #ifdef HAVE_STRUCT_STAT_ST_ATIM
  6974. __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
  6975. __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
  6976. __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
  6977. #endif
  6978. unlock_user_struct(target_st, target_addr, 1);
  6979. } else
  6980. #endif
  6981. {
  6982. #if defined(TARGET_HAS_STRUCT_STAT64)
  6983. struct target_stat64 *target_st;
  6984. #else
  6985. struct target_stat *target_st;
  6986. #endif
  6987. if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
  6988. return -TARGET_EFAULT;
  6989. memset(target_st, 0, sizeof(*target_st));
  6990. __put_user(host_st->st_dev, &target_st->st_dev);
  6991. __put_user(host_st->st_ino, &target_st->st_ino);
  6992. #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
  6993. __put_user(host_st->st_ino, &target_st->__st_ino);
  6994. #endif
  6995. __put_user(host_st->st_mode, &target_st->st_mode);
  6996. __put_user(host_st->st_nlink, &target_st->st_nlink);
  6997. __put_user(host_st->st_uid, &target_st->st_uid);
  6998. __put_user(host_st->st_gid, &target_st->st_gid);
  6999. __put_user(host_st->st_rdev, &target_st->st_rdev);
  7000. /* XXX: better use of kernel struct */
  7001. __put_user(host_st->st_size, &target_st->st_size);
  7002. __put_user(host_st->st_blksize, &target_st->st_blksize);
  7003. __put_user(host_st->st_blocks, &target_st->st_blocks);
  7004. __put_user(host_st->st_atime, &target_st->target_st_atime);
  7005. __put_user(host_st->st_mtime, &target_st->target_st_mtime);
  7006. __put_user(host_st->st_ctime, &target_st->target_st_ctime);
  7007. #ifdef HAVE_STRUCT_STAT_ST_ATIM
  7008. __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
  7009. __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
  7010. __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
  7011. #endif
  7012. unlock_user_struct(target_st, target_addr, 1);
  7013. }
  7014. return 0;
  7015. }
  7016. #endif
  7017. #if defined(TARGET_NR_statx) && defined(__NR_statx)
  7018. static inline abi_long host_to_target_statx(struct target_statx *host_stx,
  7019. abi_ulong target_addr)
  7020. {
  7021. struct target_statx *target_stx;
  7022. if (!lock_user_struct(VERIFY_WRITE, target_stx, target_addr, 0)) {
  7023. return -TARGET_EFAULT;
  7024. }
  7025. memset(target_stx, 0, sizeof(*target_stx));
  7026. __put_user(host_stx->stx_mask, &target_stx->stx_mask);
  7027. __put_user(host_stx->stx_blksize, &target_stx->stx_blksize);
  7028. __put_user(host_stx->stx_attributes, &target_stx->stx_attributes);
  7029. __put_user(host_stx->stx_nlink, &target_stx->stx_nlink);
  7030. __put_user(host_stx->stx_uid, &target_stx->stx_uid);
  7031. __put_user(host_stx->stx_gid, &target_stx->stx_gid);
  7032. __put_user(host_stx->stx_mode, &target_stx->stx_mode);
  7033. __put_user(host_stx->stx_ino, &target_stx->stx_ino);
  7034. __put_user(host_stx->stx_size, &target_stx->stx_size);
  7035. __put_user(host_stx->stx_blocks, &target_stx->stx_blocks);
  7036. __put_user(host_stx->stx_attributes_mask, &target_stx->stx_attributes_mask);
  7037. __put_user(host_stx->stx_atime.tv_sec, &target_stx->stx_atime.tv_sec);
  7038. __put_user(host_stx->stx_atime.tv_nsec, &target_stx->stx_atime.tv_nsec);
  7039. __put_user(host_stx->stx_btime.tv_sec, &target_stx->stx_btime.tv_sec);
  7040. __put_user(host_stx->stx_btime.tv_nsec, &target_stx->stx_btime.tv_nsec);
  7041. __put_user(host_stx->stx_ctime.tv_sec, &target_stx->stx_ctime.tv_sec);
  7042. __put_user(host_stx->stx_ctime.tv_nsec, &target_stx->stx_ctime.tv_nsec);
  7043. __put_user(host_stx->stx_mtime.tv_sec, &target_stx->stx_mtime.tv_sec);
  7044. __put_user(host_stx->stx_mtime.tv_nsec, &target_stx->stx_mtime.tv_nsec);
  7045. __put_user(host_stx->stx_rdev_major, &target_stx->stx_rdev_major);
  7046. __put_user(host_stx->stx_rdev_minor, &target_stx->stx_rdev_minor);
  7047. __put_user(host_stx->stx_dev_major, &target_stx->stx_dev_major);
  7048. __put_user(host_stx->stx_dev_minor, &target_stx->stx_dev_minor);
  7049. unlock_user_struct(target_stx, target_addr, 1);
  7050. return 0;
  7051. }
  7052. #endif
  7053. static int do_sys_futex(int *uaddr, int op, int val,
  7054. const struct timespec *timeout, int *uaddr2,
  7055. int val3)
  7056. {
  7057. #if HOST_LONG_BITS == 64
  7058. #if defined(__NR_futex)
  7059. /* always a 64-bit time_t, it doesn't define _time64 version */
  7060. return sys_futex(uaddr, op, val, timeout, uaddr2, val3);
  7061. #endif
  7062. #else /* HOST_LONG_BITS == 64 */
  7063. #if defined(__NR_futex_time64)
  7064. if (sizeof(timeout->tv_sec) == 8) {
  7065. /* _time64 function on 32bit arch */
  7066. return sys_futex_time64(uaddr, op, val, timeout, uaddr2, val3);
  7067. }
  7068. #endif
  7069. #if defined(__NR_futex)
  7070. /* old function on 32bit arch */
  7071. return sys_futex(uaddr, op, val, timeout, uaddr2, val3);
  7072. #endif
  7073. #endif /* HOST_LONG_BITS == 64 */
  7074. g_assert_not_reached();
  7075. }
  7076. static int do_safe_futex(int *uaddr, int op, int val,
  7077. const struct timespec *timeout, int *uaddr2,
  7078. int val3)
  7079. {
  7080. #if HOST_LONG_BITS == 64
  7081. #if defined(__NR_futex)
  7082. /* always a 64-bit time_t, it doesn't define _time64 version */
  7083. return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3));
  7084. #endif
  7085. #else /* HOST_LONG_BITS == 64 */
  7086. #if defined(__NR_futex_time64)
  7087. if (sizeof(timeout->tv_sec) == 8) {
  7088. /* _time64 function on 32bit arch */
  7089. return get_errno(safe_futex_time64(uaddr, op, val, timeout, uaddr2,
  7090. val3));
  7091. }
  7092. #endif
  7093. #if defined(__NR_futex)
  7094. /* old function on 32bit arch */
  7095. return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3));
  7096. #endif
  7097. #endif /* HOST_LONG_BITS == 64 */
  7098. return -TARGET_ENOSYS;
  7099. }
  7100. /* ??? Using host futex calls even when target atomic operations
  7101. are not really atomic probably breaks things. However implementing
  7102. futexes locally would make futexes shared between multiple processes
  7103. tricky. However they're probably useless because guest atomic
  7104. operations won't work either. */
  7105. #if defined(TARGET_NR_futex) || defined(TARGET_NR_futex_time64)
  7106. static int do_futex(CPUState *cpu, bool time64, target_ulong uaddr,
  7107. int op, int val, target_ulong timeout,
  7108. target_ulong uaddr2, int val3)
  7109. {
  7110. struct timespec ts, *pts = NULL;
  7111. void *haddr2 = NULL;
  7112. int base_op;
  7113. /* We assume FUTEX_* constants are the same on both host and target. */
  7114. #ifdef FUTEX_CMD_MASK
  7115. base_op = op & FUTEX_CMD_MASK;
  7116. #else
  7117. base_op = op;
  7118. #endif
  7119. switch (base_op) {
  7120. case FUTEX_WAIT:
  7121. case FUTEX_WAIT_BITSET:
  7122. val = tswap32(val);
  7123. break;
  7124. case FUTEX_WAIT_REQUEUE_PI:
  7125. val = tswap32(val);
  7126. haddr2 = g2h(cpu, uaddr2);
  7127. break;
  7128. case FUTEX_LOCK_PI:
  7129. case FUTEX_LOCK_PI2:
  7130. break;
  7131. case FUTEX_WAKE:
  7132. case FUTEX_WAKE_BITSET:
  7133. case FUTEX_TRYLOCK_PI:
  7134. case FUTEX_UNLOCK_PI:
  7135. timeout = 0;
  7136. break;
  7137. case FUTEX_FD:
  7138. val = target_to_host_signal(val);
  7139. timeout = 0;
  7140. break;
  7141. case FUTEX_CMP_REQUEUE:
  7142. case FUTEX_CMP_REQUEUE_PI:
  7143. val3 = tswap32(val3);
  7144. /* fall through */
  7145. case FUTEX_REQUEUE:
  7146. case FUTEX_WAKE_OP:
  7147. /*
  7148. * For these, the 4th argument is not TIMEOUT, but VAL2.
  7149. * But the prototype of do_safe_futex takes a pointer, so
  7150. * insert casts to satisfy the compiler. We do not need
  7151. * to tswap VAL2 since it's not compared to guest memory.
  7152. */
  7153. pts = (struct timespec *)(uintptr_t)timeout;
  7154. timeout = 0;
  7155. haddr2 = g2h(cpu, uaddr2);
  7156. break;
  7157. default:
  7158. return -TARGET_ENOSYS;
  7159. }
  7160. if (timeout) {
  7161. pts = &ts;
  7162. if (time64
  7163. ? target_to_host_timespec64(pts, timeout)
  7164. : target_to_host_timespec(pts, timeout)) {
  7165. return -TARGET_EFAULT;
  7166. }
  7167. }
  7168. return do_safe_futex(g2h(cpu, uaddr), op, val, pts, haddr2, val3);
  7169. }
  7170. #endif
  7171. #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  7172. static abi_long do_name_to_handle_at(abi_long dirfd, abi_long pathname,
  7173. abi_long handle, abi_long mount_id,
  7174. abi_long flags)
  7175. {
  7176. struct file_handle *target_fh;
  7177. struct file_handle *fh;
  7178. int mid = 0;
  7179. abi_long ret;
  7180. char *name;
  7181. unsigned int size, total_size;
  7182. if (get_user_s32(size, handle)) {
  7183. return -TARGET_EFAULT;
  7184. }
  7185. name = lock_user_string(pathname);
  7186. if (!name) {
  7187. return -TARGET_EFAULT;
  7188. }
  7189. total_size = sizeof(struct file_handle) + size;
  7190. target_fh = lock_user(VERIFY_WRITE, handle, total_size, 0);
  7191. if (!target_fh) {
  7192. unlock_user(name, pathname, 0);
  7193. return -TARGET_EFAULT;
  7194. }
  7195. fh = g_malloc0(total_size);
  7196. fh->handle_bytes = size;
  7197. ret = get_errno(name_to_handle_at(dirfd, path(name), fh, &mid, flags));
  7198. unlock_user(name, pathname, 0);
  7199. /* man name_to_handle_at(2):
  7200. * Other than the use of the handle_bytes field, the caller should treat
  7201. * the file_handle structure as an opaque data type
  7202. */
  7203. memcpy(target_fh, fh, total_size);
  7204. target_fh->handle_bytes = tswap32(fh->handle_bytes);
  7205. target_fh->handle_type = tswap32(fh->handle_type);
  7206. g_free(fh);
  7207. unlock_user(target_fh, handle, total_size);
  7208. if (put_user_s32(mid, mount_id)) {
  7209. return -TARGET_EFAULT;
  7210. }
  7211. return ret;
  7212. }
  7213. #endif
  7214. #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  7215. static abi_long do_open_by_handle_at(abi_long mount_fd, abi_long handle,
  7216. abi_long flags)
  7217. {
  7218. struct file_handle *target_fh;
  7219. struct file_handle *fh;
  7220. unsigned int size, total_size;
  7221. abi_long ret;
  7222. if (get_user_s32(size, handle)) {
  7223. return -TARGET_EFAULT;
  7224. }
  7225. total_size = sizeof(struct file_handle) + size;
  7226. target_fh = lock_user(VERIFY_READ, handle, total_size, 1);
  7227. if (!target_fh) {
  7228. return -TARGET_EFAULT;
  7229. }
  7230. fh = g_memdup(target_fh, total_size);
  7231. fh->handle_bytes = size;
  7232. fh->handle_type = tswap32(target_fh->handle_type);
  7233. ret = get_errno(open_by_handle_at(mount_fd, fh,
  7234. target_to_host_bitmask(flags, fcntl_flags_tbl)));
  7235. g_free(fh);
  7236. unlock_user(target_fh, handle, total_size);
  7237. return ret;
  7238. }
  7239. #endif
  7240. #if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4)
  7241. static abi_long do_signalfd4(int fd, abi_long mask, int flags)
  7242. {
  7243. int host_flags;
  7244. target_sigset_t *target_mask;
  7245. sigset_t host_mask;
  7246. abi_long ret;
  7247. if (flags & ~(TARGET_O_NONBLOCK_MASK | TARGET_O_CLOEXEC)) {
  7248. return -TARGET_EINVAL;
  7249. }
  7250. if (!lock_user_struct(VERIFY_READ, target_mask, mask, 1)) {
  7251. return -TARGET_EFAULT;
  7252. }
  7253. target_to_host_sigset(&host_mask, target_mask);
  7254. host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
  7255. ret = get_errno(signalfd(fd, &host_mask, host_flags));
  7256. if (ret >= 0) {
  7257. fd_trans_register(ret, &target_signalfd_trans);
  7258. }
  7259. unlock_user_struct(target_mask, mask, 0);
  7260. return ret;
  7261. }
  7262. #endif
  7263. /* Map host to target signal numbers for the wait family of syscalls.
  7264. Assume all other status bits are the same. */
  7265. int host_to_target_waitstatus(int status)
  7266. {
  7267. if (WIFSIGNALED(status)) {
  7268. return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
  7269. }
  7270. if (WIFSTOPPED(status)) {
  7271. return (host_to_target_signal(WSTOPSIG(status)) << 8)
  7272. | (status & 0xff);
  7273. }
  7274. return status;
  7275. }
  7276. static int open_self_cmdline(CPUArchState *cpu_env, int fd)
  7277. {
  7278. CPUState *cpu = env_cpu(cpu_env);
  7279. struct linux_binprm *bprm = ((TaskState *)cpu->opaque)->bprm;
  7280. int i;
  7281. for (i = 0; i < bprm->argc; i++) {
  7282. size_t len = strlen(bprm->argv[i]) + 1;
  7283. if (write(fd, bprm->argv[i], len) != len) {
  7284. return -1;
  7285. }
  7286. }
  7287. return 0;
  7288. }
  7289. static void show_smaps(int fd, unsigned long size)
  7290. {
  7291. unsigned long page_size_kb = TARGET_PAGE_SIZE >> 10;
  7292. unsigned long size_kb = size >> 10;
  7293. dprintf(fd, "Size: %lu kB\n"
  7294. "KernelPageSize: %lu kB\n"
  7295. "MMUPageSize: %lu kB\n"
  7296. "Rss: 0 kB\n"
  7297. "Pss: 0 kB\n"
  7298. "Pss_Dirty: 0 kB\n"
  7299. "Shared_Clean: 0 kB\n"
  7300. "Shared_Dirty: 0 kB\n"
  7301. "Private_Clean: 0 kB\n"
  7302. "Private_Dirty: 0 kB\n"
  7303. "Referenced: 0 kB\n"
  7304. "Anonymous: 0 kB\n"
  7305. "LazyFree: 0 kB\n"
  7306. "AnonHugePages: 0 kB\n"
  7307. "ShmemPmdMapped: 0 kB\n"
  7308. "FilePmdMapped: 0 kB\n"
  7309. "Shared_Hugetlb: 0 kB\n"
  7310. "Private_Hugetlb: 0 kB\n"
  7311. "Swap: 0 kB\n"
  7312. "SwapPss: 0 kB\n"
  7313. "Locked: 0 kB\n"
  7314. "THPeligible: 0\n", size_kb, page_size_kb, page_size_kb);
  7315. }
  7316. static int open_self_maps_1(CPUArchState *cpu_env, int fd, bool smaps)
  7317. {
  7318. CPUState *cpu = env_cpu(cpu_env);
  7319. TaskState *ts = cpu->opaque;
  7320. IntervalTreeRoot *map_info = read_self_maps();
  7321. IntervalTreeNode *s;
  7322. int count;
  7323. for (s = interval_tree_iter_first(map_info, 0, -1); s;
  7324. s = interval_tree_iter_next(s, 0, -1)) {
  7325. MapInfo *e = container_of(s, MapInfo, itree);
  7326. if (h2g_valid(e->itree.start)) {
  7327. unsigned long min = e->itree.start;
  7328. unsigned long max = e->itree.last + 1;
  7329. int flags = page_get_flags(h2g(min));
  7330. const char *path;
  7331. max = h2g_valid(max - 1) ?
  7332. max : (uintptr_t) g2h_untagged(GUEST_ADDR_MAX) + 1;
  7333. if (!page_check_range(h2g(min), max - min, flags)) {
  7334. continue;
  7335. }
  7336. #ifdef TARGET_HPPA
  7337. if (h2g(max) == ts->info->stack_limit) {
  7338. #else
  7339. if (h2g(min) == ts->info->stack_limit) {
  7340. #endif
  7341. path = "[stack]";
  7342. } else {
  7343. path = e->path;
  7344. }
  7345. count = dprintf(fd, TARGET_ABI_FMT_ptr "-" TARGET_ABI_FMT_ptr
  7346. " %c%c%c%c %08" PRIx64 " %s %"PRId64,
  7347. h2g(min), h2g(max - 1) + 1,
  7348. (flags & PAGE_READ) ? 'r' : '-',
  7349. (flags & PAGE_WRITE_ORG) ? 'w' : '-',
  7350. (flags & PAGE_EXEC) ? 'x' : '-',
  7351. e->is_priv ? 'p' : 's',
  7352. (uint64_t) e->offset, e->dev, e->inode);
  7353. if (path) {
  7354. dprintf(fd, "%*s%s\n", 73 - count, "", path);
  7355. } else {
  7356. dprintf(fd, "\n");
  7357. }
  7358. if (smaps) {
  7359. show_smaps(fd, max - min);
  7360. dprintf(fd, "VmFlags:%s%s%s%s%s%s%s%s\n",
  7361. (flags & PAGE_READ) ? " rd" : "",
  7362. (flags & PAGE_WRITE_ORG) ? " wr" : "",
  7363. (flags & PAGE_EXEC) ? " ex" : "",
  7364. e->is_priv ? "" : " sh",
  7365. (flags & PAGE_READ) ? " mr" : "",
  7366. (flags & PAGE_WRITE_ORG) ? " mw" : "",
  7367. (flags & PAGE_EXEC) ? " me" : "",
  7368. e->is_priv ? "" : " ms");
  7369. }
  7370. }
  7371. }
  7372. free_self_maps(map_info);
  7373. #ifdef TARGET_VSYSCALL_PAGE
  7374. /*
  7375. * We only support execution from the vsyscall page.
  7376. * This is as if CONFIG_LEGACY_VSYSCALL_XONLY=y from v5.3.
  7377. */
  7378. count = dprintf(fd, TARGET_FMT_lx "-" TARGET_FMT_lx
  7379. " --xp 00000000 00:00 0",
  7380. TARGET_VSYSCALL_PAGE, TARGET_VSYSCALL_PAGE + TARGET_PAGE_SIZE);
  7381. dprintf(fd, "%*s%s\n", 73 - count, "", "[vsyscall]");
  7382. if (smaps) {
  7383. show_smaps(fd, TARGET_PAGE_SIZE);
  7384. dprintf(fd, "VmFlags: ex\n");
  7385. }
  7386. #endif
  7387. return 0;
  7388. }
  7389. static int open_self_maps(CPUArchState *cpu_env, int fd)
  7390. {
  7391. return open_self_maps_1(cpu_env, fd, false);
  7392. }
  7393. static int open_self_smaps(CPUArchState *cpu_env, int fd)
  7394. {
  7395. return open_self_maps_1(cpu_env, fd, true);
  7396. }
  7397. static int open_self_stat(CPUArchState *cpu_env, int fd)
  7398. {
  7399. CPUState *cpu = env_cpu(cpu_env);
  7400. TaskState *ts = cpu->opaque;
  7401. g_autoptr(GString) buf = g_string_new(NULL);
  7402. int i;
  7403. for (i = 0; i < 44; i++) {
  7404. if (i == 0) {
  7405. /* pid */
  7406. g_string_printf(buf, FMT_pid " ", getpid());
  7407. } else if (i == 1) {
  7408. /* app name */
  7409. gchar *bin = g_strrstr(ts->bprm->argv[0], "/");
  7410. bin = bin ? bin + 1 : ts->bprm->argv[0];
  7411. g_string_printf(buf, "(%.15s) ", bin);
  7412. } else if (i == 2) {
  7413. /* task state */
  7414. g_string_assign(buf, "R "); /* we are running right now */
  7415. } else if (i == 3) {
  7416. /* ppid */
  7417. g_string_printf(buf, FMT_pid " ", getppid());
  7418. } else if (i == 21) {
  7419. /* starttime */
  7420. g_string_printf(buf, "%" PRIu64 " ", ts->start_boottime);
  7421. } else if (i == 27) {
  7422. /* stack bottom */
  7423. g_string_printf(buf, TARGET_ABI_FMT_ld " ", ts->info->start_stack);
  7424. } else {
  7425. /* for the rest, there is MasterCard */
  7426. g_string_printf(buf, "0%c", i == 43 ? '\n' : ' ');
  7427. }
  7428. if (write(fd, buf->str, buf->len) != buf->len) {
  7429. return -1;
  7430. }
  7431. }
  7432. return 0;
  7433. }
  7434. static int open_self_auxv(CPUArchState *cpu_env, int fd)
  7435. {
  7436. CPUState *cpu = env_cpu(cpu_env);
  7437. TaskState *ts = cpu->opaque;
  7438. abi_ulong auxv = ts->info->saved_auxv;
  7439. abi_ulong len = ts->info->auxv_len;
  7440. char *ptr;
  7441. /*
  7442. * Auxiliary vector is stored in target process stack.
  7443. * read in whole auxv vector and copy it to file
  7444. */
  7445. ptr = lock_user(VERIFY_READ, auxv, len, 0);
  7446. if (ptr != NULL) {
  7447. while (len > 0) {
  7448. ssize_t r;
  7449. r = write(fd, ptr, len);
  7450. if (r <= 0) {
  7451. break;
  7452. }
  7453. len -= r;
  7454. ptr += r;
  7455. }
  7456. lseek(fd, 0, SEEK_SET);
  7457. unlock_user(ptr, auxv, len);
  7458. }
  7459. return 0;
  7460. }
  7461. static int is_proc_myself(const char *filename, const char *entry)
  7462. {
  7463. if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
  7464. filename += strlen("/proc/");
  7465. if (!strncmp(filename, "self/", strlen("self/"))) {
  7466. filename += strlen("self/");
  7467. } else if (*filename >= '1' && *filename <= '9') {
  7468. char myself[80];
  7469. snprintf(myself, sizeof(myself), "%d/", getpid());
  7470. if (!strncmp(filename, myself, strlen(myself))) {
  7471. filename += strlen(myself);
  7472. } else {
  7473. return 0;
  7474. }
  7475. } else {
  7476. return 0;
  7477. }
  7478. if (!strcmp(filename, entry)) {
  7479. return 1;
  7480. }
  7481. }
  7482. return 0;
  7483. }
  7484. static void excp_dump_file(FILE *logfile, CPUArchState *env,
  7485. const char *fmt, int code)
  7486. {
  7487. if (logfile) {
  7488. CPUState *cs = env_cpu(env);
  7489. fprintf(logfile, fmt, code);
  7490. fprintf(logfile, "Failing executable: %s\n", exec_path);
  7491. cpu_dump_state(cs, logfile, 0);
  7492. open_self_maps(env, fileno(logfile));
  7493. }
  7494. }
  7495. void target_exception_dump(CPUArchState *env, const char *fmt, int code)
  7496. {
  7497. /* dump to console */
  7498. excp_dump_file(stderr, env, fmt, code);
  7499. /* dump to log file */
  7500. if (qemu_log_separate()) {
  7501. FILE *logfile = qemu_log_trylock();
  7502. excp_dump_file(logfile, env, fmt, code);
  7503. qemu_log_unlock(logfile);
  7504. }
  7505. }
  7506. #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN || \
  7507. defined(TARGET_SPARC) || defined(TARGET_M68K) || defined(TARGET_HPPA) || \
  7508. defined(TARGET_RISCV) || defined(TARGET_S390X)
  7509. static int is_proc(const char *filename, const char *entry)
  7510. {
  7511. return strcmp(filename, entry) == 0;
  7512. }
  7513. #endif
  7514. #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
  7515. static int open_net_route(CPUArchState *cpu_env, int fd)
  7516. {
  7517. FILE *fp;
  7518. char *line = NULL;
  7519. size_t len = 0;
  7520. ssize_t read;
  7521. fp = fopen("/proc/net/route", "r");
  7522. if (fp == NULL) {
  7523. return -1;
  7524. }
  7525. /* read header */
  7526. read = getline(&line, &len, fp);
  7527. dprintf(fd, "%s", line);
  7528. /* read routes */
  7529. while ((read = getline(&line, &len, fp)) != -1) {
  7530. char iface[16];
  7531. uint32_t dest, gw, mask;
  7532. unsigned int flags, refcnt, use, metric, mtu, window, irtt;
  7533. int fields;
  7534. fields = sscanf(line,
  7535. "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
  7536. iface, &dest, &gw, &flags, &refcnt, &use, &metric,
  7537. &mask, &mtu, &window, &irtt);
  7538. if (fields != 11) {
  7539. continue;
  7540. }
  7541. dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
  7542. iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
  7543. metric, tswap32(mask), mtu, window, irtt);
  7544. }
  7545. free(line);
  7546. fclose(fp);
  7547. return 0;
  7548. }
  7549. #endif
  7550. #if defined(TARGET_SPARC)
  7551. static int open_cpuinfo(CPUArchState *cpu_env, int fd)
  7552. {
  7553. dprintf(fd, "type\t\t: sun4u\n");
  7554. return 0;
  7555. }
  7556. #endif
  7557. #if defined(TARGET_HPPA)
  7558. static int open_cpuinfo(CPUArchState *cpu_env, int fd)
  7559. {
  7560. int i, num_cpus;
  7561. num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
  7562. for (i = 0; i < num_cpus; i++) {
  7563. dprintf(fd, "processor\t: %d\n", i);
  7564. dprintf(fd, "cpu family\t: PA-RISC 1.1e\n");
  7565. dprintf(fd, "cpu\t\t: PA7300LC (PCX-L2)\n");
  7566. dprintf(fd, "capabilities\t: os32\n");
  7567. dprintf(fd, "model\t\t: 9000/778/B160L - "
  7568. "Merlin L2 160 QEMU (9000/778/B160L)\n\n");
  7569. }
  7570. return 0;
  7571. }
  7572. #endif
  7573. #if defined(TARGET_RISCV)
  7574. static int open_cpuinfo(CPUArchState *cpu_env, int fd)
  7575. {
  7576. int i;
  7577. int num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
  7578. RISCVCPU *cpu = env_archcpu(cpu_env);
  7579. const RISCVCPUConfig *cfg = riscv_cpu_cfg((CPURISCVState *) cpu_env);
  7580. char *isa_string = riscv_isa_string(cpu);
  7581. const char *mmu;
  7582. if (cfg->mmu) {
  7583. mmu = (cpu_env->xl == MXL_RV32) ? "sv32" : "sv48";
  7584. } else {
  7585. mmu = "none";
  7586. }
  7587. for (i = 0; i < num_cpus; i++) {
  7588. dprintf(fd, "processor\t: %d\n", i);
  7589. dprintf(fd, "hart\t\t: %d\n", i);
  7590. dprintf(fd, "isa\t\t: %s\n", isa_string);
  7591. dprintf(fd, "mmu\t\t: %s\n", mmu);
  7592. dprintf(fd, "uarch\t\t: qemu\n\n");
  7593. }
  7594. g_free(isa_string);
  7595. return 0;
  7596. }
  7597. #endif
  7598. #if defined(TARGET_S390X)
  7599. /*
  7600. * Emulate what a Linux kernel running in qemu-system-s390x -M accel=tcg would
  7601. * show in /proc/cpuinfo.
  7602. *
  7603. * Skip the following in order to match the missing support in op_ecag():
  7604. * - show_cacheinfo().
  7605. * - show_cpu_topology().
  7606. * - show_cpu_mhz().
  7607. *
  7608. * Use fixed values for certain fields:
  7609. * - bogomips per cpu - from a qemu-system-s390x run.
  7610. * - max thread id = 0, since SMT / SIGP_SET_MULTI_THREADING is not supported.
  7611. *
  7612. * Keep the code structure close to arch/s390/kernel/processor.c.
  7613. */
  7614. static void show_facilities(int fd)
  7615. {
  7616. size_t sizeof_stfl_bytes = 2048;
  7617. g_autofree uint8_t *stfl_bytes = g_new0(uint8_t, sizeof_stfl_bytes);
  7618. unsigned int bit;
  7619. dprintf(fd, "facilities :");
  7620. s390_get_feat_block(S390_FEAT_TYPE_STFL, stfl_bytes);
  7621. for (bit = 0; bit < sizeof_stfl_bytes * 8; bit++) {
  7622. if (test_be_bit(bit, stfl_bytes)) {
  7623. dprintf(fd, " %d", bit);
  7624. }
  7625. }
  7626. dprintf(fd, "\n");
  7627. }
  7628. static int cpu_ident(unsigned long n)
  7629. {
  7630. return deposit32(0, CPU_ID_BITS - CPU_PHYS_ADDR_BITS, CPU_PHYS_ADDR_BITS,
  7631. n);
  7632. }
  7633. static void show_cpu_summary(CPUArchState *cpu_env, int fd)
  7634. {
  7635. S390CPUModel *model = env_archcpu(cpu_env)->model;
  7636. int num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
  7637. uint32_t elf_hwcap = get_elf_hwcap();
  7638. const char *hwcap_str;
  7639. int i;
  7640. dprintf(fd, "vendor_id : IBM/S390\n"
  7641. "# processors : %i\n"
  7642. "bogomips per cpu: 13370.00\n",
  7643. num_cpus);
  7644. dprintf(fd, "max thread id : 0\n");
  7645. dprintf(fd, "features\t: ");
  7646. for (i = 0; i < sizeof(elf_hwcap) * 8; i++) {
  7647. if (!(elf_hwcap & (1 << i))) {
  7648. continue;
  7649. }
  7650. hwcap_str = elf_hwcap_str(i);
  7651. if (hwcap_str) {
  7652. dprintf(fd, "%s ", hwcap_str);
  7653. }
  7654. }
  7655. dprintf(fd, "\n");
  7656. show_facilities(fd);
  7657. for (i = 0; i < num_cpus; i++) {
  7658. dprintf(fd, "processor %d: "
  7659. "version = %02X, "
  7660. "identification = %06X, "
  7661. "machine = %04X\n",
  7662. i, model->cpu_ver, cpu_ident(i), model->def->type);
  7663. }
  7664. }
  7665. static void show_cpu_ids(CPUArchState *cpu_env, int fd, unsigned long n)
  7666. {
  7667. S390CPUModel *model = env_archcpu(cpu_env)->model;
  7668. dprintf(fd, "version : %02X\n", model->cpu_ver);
  7669. dprintf(fd, "identification : %06X\n", cpu_ident(n));
  7670. dprintf(fd, "machine : %04X\n", model->def->type);
  7671. }
  7672. static void show_cpuinfo(CPUArchState *cpu_env, int fd, unsigned long n)
  7673. {
  7674. dprintf(fd, "\ncpu number : %ld\n", n);
  7675. show_cpu_ids(cpu_env, fd, n);
  7676. }
  7677. static int open_cpuinfo(CPUArchState *cpu_env, int fd)
  7678. {
  7679. int num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
  7680. int i;
  7681. show_cpu_summary(cpu_env, fd);
  7682. for (i = 0; i < num_cpus; i++) {
  7683. show_cpuinfo(cpu_env, fd, i);
  7684. }
  7685. return 0;
  7686. }
  7687. #endif
  7688. #if defined(TARGET_M68K)
  7689. static int open_hardware(CPUArchState *cpu_env, int fd)
  7690. {
  7691. dprintf(fd, "Model:\t\tqemu-m68k\n");
  7692. return 0;
  7693. }
  7694. #endif
  7695. int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *fname,
  7696. int flags, mode_t mode, bool safe)
  7697. {
  7698. g_autofree char *proc_name = NULL;
  7699. const char *pathname;
  7700. struct fake_open {
  7701. const char *filename;
  7702. int (*fill)(CPUArchState *cpu_env, int fd);
  7703. int (*cmp)(const char *s1, const char *s2);
  7704. };
  7705. const struct fake_open *fake_open;
  7706. static const struct fake_open fakes[] = {
  7707. { "maps", open_self_maps, is_proc_myself },
  7708. { "smaps", open_self_smaps, is_proc_myself },
  7709. { "stat", open_self_stat, is_proc_myself },
  7710. { "auxv", open_self_auxv, is_proc_myself },
  7711. { "cmdline", open_self_cmdline, is_proc_myself },
  7712. #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
  7713. { "/proc/net/route", open_net_route, is_proc },
  7714. #endif
  7715. #if defined(TARGET_SPARC) || defined(TARGET_HPPA) || \
  7716. defined(TARGET_RISCV) || defined(TARGET_S390X)
  7717. { "/proc/cpuinfo", open_cpuinfo, is_proc },
  7718. #endif
  7719. #if defined(TARGET_M68K)
  7720. { "/proc/hardware", open_hardware, is_proc },
  7721. #endif
  7722. { NULL, NULL, NULL }
  7723. };
  7724. /* if this is a file from /proc/ filesystem, expand full name */
  7725. proc_name = realpath(fname, NULL);
  7726. if (proc_name && strncmp(proc_name, "/proc/", 6) == 0) {
  7727. pathname = proc_name;
  7728. } else {
  7729. pathname = fname;
  7730. }
  7731. if (is_proc_myself(pathname, "exe")) {
  7732. if (safe) {
  7733. return safe_openat(dirfd, exec_path, flags, mode);
  7734. } else {
  7735. return openat(dirfd, exec_path, flags, mode);
  7736. }
  7737. }
  7738. for (fake_open = fakes; fake_open->filename; fake_open++) {
  7739. if (fake_open->cmp(pathname, fake_open->filename)) {
  7740. break;
  7741. }
  7742. }
  7743. if (fake_open->filename) {
  7744. const char *tmpdir;
  7745. char filename[PATH_MAX];
  7746. int fd, r;
  7747. fd = memfd_create("qemu-open", 0);
  7748. if (fd < 0) {
  7749. if (errno != ENOSYS) {
  7750. return fd;
  7751. }
  7752. /* create temporary file to map stat to */
  7753. tmpdir = getenv("TMPDIR");
  7754. if (!tmpdir)
  7755. tmpdir = "/tmp";
  7756. snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
  7757. fd = mkstemp(filename);
  7758. if (fd < 0) {
  7759. return fd;
  7760. }
  7761. unlink(filename);
  7762. }
  7763. if ((r = fake_open->fill(cpu_env, fd))) {
  7764. int e = errno;
  7765. close(fd);
  7766. errno = e;
  7767. return r;
  7768. }
  7769. lseek(fd, 0, SEEK_SET);
  7770. return fd;
  7771. }
  7772. if (safe) {
  7773. return safe_openat(dirfd, path(pathname), flags, mode);
  7774. } else {
  7775. return openat(dirfd, path(pathname), flags, mode);
  7776. }
  7777. }
  7778. ssize_t do_guest_readlink(const char *pathname, char *buf, size_t bufsiz)
  7779. {
  7780. ssize_t ret;
  7781. if (!pathname || !buf) {
  7782. errno = EFAULT;
  7783. return -1;
  7784. }
  7785. if (!bufsiz) {
  7786. /* Short circuit this for the magic exe check. */
  7787. errno = EINVAL;
  7788. return -1;
  7789. }
  7790. if (is_proc_myself((const char *)pathname, "exe")) {
  7791. /*
  7792. * Don't worry about sign mismatch as earlier mapping
  7793. * logic would have thrown a bad address error.
  7794. */
  7795. ret = MIN(strlen(exec_path), bufsiz);
  7796. /* We cannot NUL terminate the string. */
  7797. memcpy(buf, exec_path, ret);
  7798. } else {
  7799. ret = readlink(path(pathname), buf, bufsiz);
  7800. }
  7801. return ret;
  7802. }
  7803. static int do_execv(CPUArchState *cpu_env, int dirfd,
  7804. abi_long pathname, abi_long guest_argp,
  7805. abi_long guest_envp, int flags, bool is_execveat)
  7806. {
  7807. int ret;
  7808. char **argp, **envp;
  7809. int argc, envc;
  7810. abi_ulong gp;
  7811. abi_ulong addr;
  7812. char **q;
  7813. void *p;
  7814. argc = 0;
  7815. for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
  7816. if (get_user_ual(addr, gp)) {
  7817. return -TARGET_EFAULT;
  7818. }
  7819. if (!addr) {
  7820. break;
  7821. }
  7822. argc++;
  7823. }
  7824. envc = 0;
  7825. for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
  7826. if (get_user_ual(addr, gp)) {
  7827. return -TARGET_EFAULT;
  7828. }
  7829. if (!addr) {
  7830. break;
  7831. }
  7832. envc++;
  7833. }
  7834. argp = g_new0(char *, argc + 1);
  7835. envp = g_new0(char *, envc + 1);
  7836. for (gp = guest_argp, q = argp; gp; gp += sizeof(abi_ulong), q++) {
  7837. if (get_user_ual(addr, gp)) {
  7838. goto execve_efault;
  7839. }
  7840. if (!addr) {
  7841. break;
  7842. }
  7843. *q = lock_user_string(addr);
  7844. if (!*q) {
  7845. goto execve_efault;
  7846. }
  7847. }
  7848. *q = NULL;
  7849. for (gp = guest_envp, q = envp; gp; gp += sizeof(abi_ulong), q++) {
  7850. if (get_user_ual(addr, gp)) {
  7851. goto execve_efault;
  7852. }
  7853. if (!addr) {
  7854. break;
  7855. }
  7856. *q = lock_user_string(addr);
  7857. if (!*q) {
  7858. goto execve_efault;
  7859. }
  7860. }
  7861. *q = NULL;
  7862. /*
  7863. * Although execve() is not an interruptible syscall it is
  7864. * a special case where we must use the safe_syscall wrapper:
  7865. * if we allow a signal to happen before we make the host
  7866. * syscall then we will 'lose' it, because at the point of
  7867. * execve the process leaves QEMU's control. So we use the
  7868. * safe syscall wrapper to ensure that we either take the
  7869. * signal as a guest signal, or else it does not happen
  7870. * before the execve completes and makes it the other
  7871. * program's problem.
  7872. */
  7873. p = lock_user_string(pathname);
  7874. if (!p) {
  7875. goto execve_efault;
  7876. }
  7877. const char *exe = p;
  7878. if (is_proc_myself(p, "exe")) {
  7879. exe = exec_path;
  7880. }
  7881. ret = is_execveat
  7882. ? safe_execveat(dirfd, exe, argp, envp, flags)
  7883. : safe_execve(exe, argp, envp);
  7884. ret = get_errno(ret);
  7885. unlock_user(p, pathname, 0);
  7886. goto execve_end;
  7887. execve_efault:
  7888. ret = -TARGET_EFAULT;
  7889. execve_end:
  7890. for (gp = guest_argp, q = argp; *q; gp += sizeof(abi_ulong), q++) {
  7891. if (get_user_ual(addr, gp) || !addr) {
  7892. break;
  7893. }
  7894. unlock_user(*q, addr, 0);
  7895. }
  7896. for (gp = guest_envp, q = envp; *q; gp += sizeof(abi_ulong), q++) {
  7897. if (get_user_ual(addr, gp) || !addr) {
  7898. break;
  7899. }
  7900. unlock_user(*q, addr, 0);
  7901. }
  7902. g_free(argp);
  7903. g_free(envp);
  7904. return ret;
  7905. }
  7906. #define TIMER_MAGIC 0x0caf0000
  7907. #define TIMER_MAGIC_MASK 0xffff0000
  7908. /* Convert QEMU provided timer ID back to internal 16bit index format */
  7909. static target_timer_t get_timer_id(abi_long arg)
  7910. {
  7911. target_timer_t timerid = arg;
  7912. if ((timerid & TIMER_MAGIC_MASK) != TIMER_MAGIC) {
  7913. return -TARGET_EINVAL;
  7914. }
  7915. timerid &= 0xffff;
  7916. if (timerid >= ARRAY_SIZE(g_posix_timers)) {
  7917. return -TARGET_EINVAL;
  7918. }
  7919. return timerid;
  7920. }
  7921. static int target_to_host_cpu_mask(unsigned long *host_mask,
  7922. size_t host_size,
  7923. abi_ulong target_addr,
  7924. size_t target_size)
  7925. {
  7926. unsigned target_bits = sizeof(abi_ulong) * 8;
  7927. unsigned host_bits = sizeof(*host_mask) * 8;
  7928. abi_ulong *target_mask;
  7929. unsigned i, j;
  7930. assert(host_size >= target_size);
  7931. target_mask = lock_user(VERIFY_READ, target_addr, target_size, 1);
  7932. if (!target_mask) {
  7933. return -TARGET_EFAULT;
  7934. }
  7935. memset(host_mask, 0, host_size);
  7936. for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
  7937. unsigned bit = i * target_bits;
  7938. abi_ulong val;
  7939. __get_user(val, &target_mask[i]);
  7940. for (j = 0; j < target_bits; j++, bit++) {
  7941. if (val & (1UL << j)) {
  7942. host_mask[bit / host_bits] |= 1UL << (bit % host_bits);
  7943. }
  7944. }
  7945. }
  7946. unlock_user(target_mask, target_addr, 0);
  7947. return 0;
  7948. }
  7949. static int host_to_target_cpu_mask(const unsigned long *host_mask,
  7950. size_t host_size,
  7951. abi_ulong target_addr,
  7952. size_t target_size)
  7953. {
  7954. unsigned target_bits = sizeof(abi_ulong) * 8;
  7955. unsigned host_bits = sizeof(*host_mask) * 8;
  7956. abi_ulong *target_mask;
  7957. unsigned i, j;
  7958. assert(host_size >= target_size);
  7959. target_mask = lock_user(VERIFY_WRITE, target_addr, target_size, 0);
  7960. if (!target_mask) {
  7961. return -TARGET_EFAULT;
  7962. }
  7963. for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
  7964. unsigned bit = i * target_bits;
  7965. abi_ulong val = 0;
  7966. for (j = 0; j < target_bits; j++, bit++) {
  7967. if (host_mask[bit / host_bits] & (1UL << (bit % host_bits))) {
  7968. val |= 1UL << j;
  7969. }
  7970. }
  7971. __put_user(val, &target_mask[i]);
  7972. }
  7973. unlock_user(target_mask, target_addr, target_size);
  7974. return 0;
  7975. }
  7976. #ifdef TARGET_NR_getdents
  7977. static int do_getdents(abi_long dirfd, abi_long arg2, abi_long count)
  7978. {
  7979. g_autofree void *hdirp = NULL;
  7980. void *tdirp;
  7981. int hlen, hoff, toff;
  7982. int hreclen, treclen;
  7983. off64_t prev_diroff = 0;
  7984. hdirp = g_try_malloc(count);
  7985. if (!hdirp) {
  7986. return -TARGET_ENOMEM;
  7987. }
  7988. #ifdef EMULATE_GETDENTS_WITH_GETDENTS
  7989. hlen = sys_getdents(dirfd, hdirp, count);
  7990. #else
  7991. hlen = sys_getdents64(dirfd, hdirp, count);
  7992. #endif
  7993. hlen = get_errno(hlen);
  7994. if (is_error(hlen)) {
  7995. return hlen;
  7996. }
  7997. tdirp = lock_user(VERIFY_WRITE, arg2, count, 0);
  7998. if (!tdirp) {
  7999. return -TARGET_EFAULT;
  8000. }
  8001. for (hoff = toff = 0; hoff < hlen; hoff += hreclen, toff += treclen) {
  8002. #ifdef EMULATE_GETDENTS_WITH_GETDENTS
  8003. struct linux_dirent *hde = hdirp + hoff;
  8004. #else
  8005. struct linux_dirent64 *hde = hdirp + hoff;
  8006. #endif
  8007. struct target_dirent *tde = tdirp + toff;
  8008. int namelen;
  8009. uint8_t type;
  8010. namelen = strlen(hde->d_name);
  8011. hreclen = hde->d_reclen;
  8012. treclen = offsetof(struct target_dirent, d_name) + namelen + 2;
  8013. treclen = QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent));
  8014. if (toff + treclen > count) {
  8015. /*
  8016. * If the host struct is smaller than the target struct, or
  8017. * requires less alignment and thus packs into less space,
  8018. * then the host can return more entries than we can pass
  8019. * on to the guest.
  8020. */
  8021. if (toff == 0) {
  8022. toff = -TARGET_EINVAL; /* result buffer is too small */
  8023. break;
  8024. }
  8025. /*
  8026. * Return what we have, resetting the file pointer to the
  8027. * location of the first record not returned.
  8028. */
  8029. lseek64(dirfd, prev_diroff, SEEK_SET);
  8030. break;
  8031. }
  8032. prev_diroff = hde->d_off;
  8033. tde->d_ino = tswapal(hde->d_ino);
  8034. tde->d_off = tswapal(hde->d_off);
  8035. tde->d_reclen = tswap16(treclen);
  8036. memcpy(tde->d_name, hde->d_name, namelen + 1);
  8037. /*
  8038. * The getdents type is in what was formerly a padding byte at the
  8039. * end of the structure.
  8040. */
  8041. #ifdef EMULATE_GETDENTS_WITH_GETDENTS
  8042. type = *((uint8_t *)hde + hreclen - 1);
  8043. #else
  8044. type = hde->d_type;
  8045. #endif
  8046. *((uint8_t *)tde + treclen - 1) = type;
  8047. }
  8048. unlock_user(tdirp, arg2, toff);
  8049. return toff;
  8050. }
  8051. #endif /* TARGET_NR_getdents */
  8052. #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
  8053. static int do_getdents64(abi_long dirfd, abi_long arg2, abi_long count)
  8054. {
  8055. g_autofree void *hdirp = NULL;
  8056. void *tdirp;
  8057. int hlen, hoff, toff;
  8058. int hreclen, treclen;
  8059. off64_t prev_diroff = 0;
  8060. hdirp = g_try_malloc(count);
  8061. if (!hdirp) {
  8062. return -TARGET_ENOMEM;
  8063. }
  8064. hlen = get_errno(sys_getdents64(dirfd, hdirp, count));
  8065. if (is_error(hlen)) {
  8066. return hlen;
  8067. }
  8068. tdirp = lock_user(VERIFY_WRITE, arg2, count, 0);
  8069. if (!tdirp) {
  8070. return -TARGET_EFAULT;
  8071. }
  8072. for (hoff = toff = 0; hoff < hlen; hoff += hreclen, toff += treclen) {
  8073. struct linux_dirent64 *hde = hdirp + hoff;
  8074. struct target_dirent64 *tde = tdirp + toff;
  8075. int namelen;
  8076. namelen = strlen(hde->d_name) + 1;
  8077. hreclen = hde->d_reclen;
  8078. treclen = offsetof(struct target_dirent64, d_name) + namelen;
  8079. treclen = QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent64));
  8080. if (toff + treclen > count) {
  8081. /*
  8082. * If the host struct is smaller than the target struct, or
  8083. * requires less alignment and thus packs into less space,
  8084. * then the host can return more entries than we can pass
  8085. * on to the guest.
  8086. */
  8087. if (toff == 0) {
  8088. toff = -TARGET_EINVAL; /* result buffer is too small */
  8089. break;
  8090. }
  8091. /*
  8092. * Return what we have, resetting the file pointer to the
  8093. * location of the first record not returned.
  8094. */
  8095. lseek64(dirfd, prev_diroff, SEEK_SET);
  8096. break;
  8097. }
  8098. prev_diroff = hde->d_off;
  8099. tde->d_ino = tswap64(hde->d_ino);
  8100. tde->d_off = tswap64(hde->d_off);
  8101. tde->d_reclen = tswap16(treclen);
  8102. tde->d_type = hde->d_type;
  8103. memcpy(tde->d_name, hde->d_name, namelen);
  8104. }
  8105. unlock_user(tdirp, arg2, toff);
  8106. return toff;
  8107. }
  8108. #endif /* TARGET_NR_getdents64 */
  8109. #if defined(TARGET_NR_riscv_hwprobe)
  8110. #define RISCV_HWPROBE_KEY_MVENDORID 0
  8111. #define RISCV_HWPROBE_KEY_MARCHID 1
  8112. #define RISCV_HWPROBE_KEY_MIMPID 2
  8113. #define RISCV_HWPROBE_KEY_BASE_BEHAVIOR 3
  8114. #define RISCV_HWPROBE_BASE_BEHAVIOR_IMA (1 << 0)
  8115. #define RISCV_HWPROBE_KEY_IMA_EXT_0 4
  8116. #define RISCV_HWPROBE_IMA_FD (1 << 0)
  8117. #define RISCV_HWPROBE_IMA_C (1 << 1)
  8118. #define RISCV_HWPROBE_KEY_CPUPERF_0 5
  8119. #define RISCV_HWPROBE_MISALIGNED_UNKNOWN (0 << 0)
  8120. #define RISCV_HWPROBE_MISALIGNED_EMULATED (1 << 0)
  8121. #define RISCV_HWPROBE_MISALIGNED_SLOW (2 << 0)
  8122. #define RISCV_HWPROBE_MISALIGNED_FAST (3 << 0)
  8123. #define RISCV_HWPROBE_MISALIGNED_UNSUPPORTED (4 << 0)
  8124. #define RISCV_HWPROBE_MISALIGNED_MASK (7 << 0)
  8125. struct riscv_hwprobe {
  8126. abi_llong key;
  8127. abi_ullong value;
  8128. };
  8129. static void risc_hwprobe_fill_pairs(CPURISCVState *env,
  8130. struct riscv_hwprobe *pair,
  8131. size_t pair_count)
  8132. {
  8133. const RISCVCPUConfig *cfg = riscv_cpu_cfg(env);
  8134. for (; pair_count > 0; pair_count--, pair++) {
  8135. abi_llong key;
  8136. abi_ullong value;
  8137. __put_user(0, &pair->value);
  8138. __get_user(key, &pair->key);
  8139. switch (key) {
  8140. case RISCV_HWPROBE_KEY_MVENDORID:
  8141. __put_user(cfg->mvendorid, &pair->value);
  8142. break;
  8143. case RISCV_HWPROBE_KEY_MARCHID:
  8144. __put_user(cfg->marchid, &pair->value);
  8145. break;
  8146. case RISCV_HWPROBE_KEY_MIMPID:
  8147. __put_user(cfg->mimpid, &pair->value);
  8148. break;
  8149. case RISCV_HWPROBE_KEY_BASE_BEHAVIOR:
  8150. value = riscv_has_ext(env, RVI) &&
  8151. riscv_has_ext(env, RVM) &&
  8152. riscv_has_ext(env, RVA) ?
  8153. RISCV_HWPROBE_BASE_BEHAVIOR_IMA : 0;
  8154. __put_user(value, &pair->value);
  8155. break;
  8156. case RISCV_HWPROBE_KEY_IMA_EXT_0:
  8157. value = riscv_has_ext(env, RVF) &&
  8158. riscv_has_ext(env, RVD) ?
  8159. RISCV_HWPROBE_IMA_FD : 0;
  8160. value |= riscv_has_ext(env, RVC) ?
  8161. RISCV_HWPROBE_IMA_C : pair->value;
  8162. __put_user(value, &pair->value);
  8163. break;
  8164. case RISCV_HWPROBE_KEY_CPUPERF_0:
  8165. __put_user(RISCV_HWPROBE_MISALIGNED_FAST, &pair->value);
  8166. break;
  8167. default:
  8168. __put_user(-1, &pair->key);
  8169. break;
  8170. }
  8171. }
  8172. }
  8173. static int cpu_set_valid(abi_long arg3, abi_long arg4)
  8174. {
  8175. int ret, i, tmp;
  8176. size_t host_mask_size, target_mask_size;
  8177. unsigned long *host_mask;
  8178. /*
  8179. * cpu_set_t represent CPU masks as bit masks of type unsigned long *.
  8180. * arg3 contains the cpu count.
  8181. */
  8182. tmp = (8 * sizeof(abi_ulong));
  8183. target_mask_size = ((arg3 + tmp - 1) / tmp) * sizeof(abi_ulong);
  8184. host_mask_size = (target_mask_size + (sizeof(*host_mask) - 1)) &
  8185. ~(sizeof(*host_mask) - 1);
  8186. host_mask = alloca(host_mask_size);
  8187. ret = target_to_host_cpu_mask(host_mask, host_mask_size,
  8188. arg4, target_mask_size);
  8189. if (ret != 0) {
  8190. return ret;
  8191. }
  8192. for (i = 0 ; i < host_mask_size / sizeof(*host_mask); i++) {
  8193. if (host_mask[i] != 0) {
  8194. return 0;
  8195. }
  8196. }
  8197. return -TARGET_EINVAL;
  8198. }
  8199. static abi_long do_riscv_hwprobe(CPUArchState *cpu_env, abi_long arg1,
  8200. abi_long arg2, abi_long arg3,
  8201. abi_long arg4, abi_long arg5)
  8202. {
  8203. int ret;
  8204. struct riscv_hwprobe *host_pairs;
  8205. /* flags must be 0 */
  8206. if (arg5 != 0) {
  8207. return -TARGET_EINVAL;
  8208. }
  8209. /* check cpu_set */
  8210. if (arg3 != 0) {
  8211. ret = cpu_set_valid(arg3, arg4);
  8212. if (ret != 0) {
  8213. return ret;
  8214. }
  8215. } else if (arg4 != 0) {
  8216. return -TARGET_EINVAL;
  8217. }
  8218. /* no pairs */
  8219. if (arg2 == 0) {
  8220. return 0;
  8221. }
  8222. host_pairs = lock_user(VERIFY_WRITE, arg1,
  8223. sizeof(*host_pairs) * (size_t)arg2, 0);
  8224. if (host_pairs == NULL) {
  8225. return -TARGET_EFAULT;
  8226. }
  8227. risc_hwprobe_fill_pairs(cpu_env, host_pairs, arg2);
  8228. unlock_user(host_pairs, arg1, sizeof(*host_pairs) * (size_t)arg2);
  8229. return 0;
  8230. }
  8231. #endif /* TARGET_NR_riscv_hwprobe */
  8232. #if defined(TARGET_NR_pivot_root) && defined(__NR_pivot_root)
  8233. _syscall2(int, pivot_root, const char *, new_root, const char *, put_old)
  8234. #endif
  8235. #if defined(TARGET_NR_open_tree) && defined(__NR_open_tree)
  8236. #define __NR_sys_open_tree __NR_open_tree
  8237. _syscall3(int, sys_open_tree, int, __dfd, const char *, __filename,
  8238. unsigned int, __flags)
  8239. #endif
  8240. #if defined(TARGET_NR_move_mount) && defined(__NR_move_mount)
  8241. #define __NR_sys_move_mount __NR_move_mount
  8242. _syscall5(int, sys_move_mount, int, __from_dfd, const char *, __from_pathname,
  8243. int, __to_dfd, const char *, __to_pathname, unsigned int, flag)
  8244. #endif
  8245. /* This is an internal helper for do_syscall so that it is easier
  8246. * to have a single return point, so that actions, such as logging
  8247. * of syscall results, can be performed.
  8248. * All errnos that do_syscall() returns must be -TARGET_<errcode>.
  8249. */
  8250. static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
  8251. abi_long arg2, abi_long arg3, abi_long arg4,
  8252. abi_long arg5, abi_long arg6, abi_long arg7,
  8253. abi_long arg8)
  8254. {
  8255. CPUState *cpu = env_cpu(cpu_env);
  8256. abi_long ret;
  8257. #if defined(TARGET_NR_stat) || defined(TARGET_NR_stat64) \
  8258. || defined(TARGET_NR_lstat) || defined(TARGET_NR_lstat64) \
  8259. || defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64) \
  8260. || defined(TARGET_NR_statx)
  8261. struct stat st;
  8262. #endif
  8263. #if defined(TARGET_NR_statfs) || defined(TARGET_NR_statfs64) \
  8264. || defined(TARGET_NR_fstatfs)
  8265. struct statfs stfs;
  8266. #endif
  8267. void *p;
  8268. switch(num) {
  8269. case TARGET_NR_exit:
  8270. /* In old applications this may be used to implement _exit(2).
  8271. However in threaded applications it is used for thread termination,
  8272. and _exit_group is used for application termination.
  8273. Do thread termination if we have more then one thread. */
  8274. if (block_signals()) {
  8275. return -QEMU_ERESTARTSYS;
  8276. }
  8277. pthread_mutex_lock(&clone_lock);
  8278. if (CPU_NEXT(first_cpu)) {
  8279. TaskState *ts = cpu->opaque;
  8280. if (ts->child_tidptr) {
  8281. put_user_u32(0, ts->child_tidptr);
  8282. do_sys_futex(g2h(cpu, ts->child_tidptr),
  8283. FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
  8284. }
  8285. object_unparent(OBJECT(cpu));
  8286. object_unref(OBJECT(cpu));
  8287. /*
  8288. * At this point the CPU should be unrealized and removed
  8289. * from cpu lists. We can clean-up the rest of the thread
  8290. * data without the lock held.
  8291. */
  8292. pthread_mutex_unlock(&clone_lock);
  8293. thread_cpu = NULL;
  8294. g_free(ts);
  8295. rcu_unregister_thread();
  8296. pthread_exit(NULL);
  8297. }
  8298. pthread_mutex_unlock(&clone_lock);
  8299. preexit_cleanup(cpu_env, arg1);
  8300. _exit(arg1);
  8301. return 0; /* avoid warning */
  8302. case TARGET_NR_read:
  8303. if (arg2 == 0 && arg3 == 0) {
  8304. return get_errno(safe_read(arg1, 0, 0));
  8305. } else {
  8306. if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
  8307. return -TARGET_EFAULT;
  8308. ret = get_errno(safe_read(arg1, p, arg3));
  8309. if (ret >= 0 &&
  8310. fd_trans_host_to_target_data(arg1)) {
  8311. ret = fd_trans_host_to_target_data(arg1)(p, ret);
  8312. }
  8313. unlock_user(p, arg2, ret);
  8314. }
  8315. return ret;
  8316. case TARGET_NR_write:
  8317. if (arg2 == 0 && arg3 == 0) {
  8318. return get_errno(safe_write(arg1, 0, 0));
  8319. }
  8320. if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
  8321. return -TARGET_EFAULT;
  8322. if (fd_trans_target_to_host_data(arg1)) {
  8323. void *copy = g_malloc(arg3);
  8324. memcpy(copy, p, arg3);
  8325. ret = fd_trans_target_to_host_data(arg1)(copy, arg3);
  8326. if (ret >= 0) {
  8327. ret = get_errno(safe_write(arg1, copy, ret));
  8328. }
  8329. g_free(copy);
  8330. } else {
  8331. ret = get_errno(safe_write(arg1, p, arg3));
  8332. }
  8333. unlock_user(p, arg2, 0);
  8334. return ret;
  8335. #ifdef TARGET_NR_open
  8336. case TARGET_NR_open:
  8337. if (!(p = lock_user_string(arg1)))
  8338. return -TARGET_EFAULT;
  8339. ret = get_errno(do_guest_openat(cpu_env, AT_FDCWD, p,
  8340. target_to_host_bitmask(arg2, fcntl_flags_tbl),
  8341. arg3, true));
  8342. fd_trans_unregister(ret);
  8343. unlock_user(p, arg1, 0);
  8344. return ret;
  8345. #endif
  8346. case TARGET_NR_openat:
  8347. if (!(p = lock_user_string(arg2)))
  8348. return -TARGET_EFAULT;
  8349. ret = get_errno(do_guest_openat(cpu_env, arg1, p,
  8350. target_to_host_bitmask(arg3, fcntl_flags_tbl),
  8351. arg4, true));
  8352. fd_trans_unregister(ret);
  8353. unlock_user(p, arg2, 0);
  8354. return ret;
  8355. #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  8356. case TARGET_NR_name_to_handle_at:
  8357. ret = do_name_to_handle_at(arg1, arg2, arg3, arg4, arg5);
  8358. return ret;
  8359. #endif
  8360. #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  8361. case TARGET_NR_open_by_handle_at:
  8362. ret = do_open_by_handle_at(arg1, arg2, arg3);
  8363. fd_trans_unregister(ret);
  8364. return ret;
  8365. #endif
  8366. #if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
  8367. case TARGET_NR_pidfd_open:
  8368. return get_errno(pidfd_open(arg1, arg2));
  8369. #endif
  8370. #if defined(__NR_pidfd_send_signal) && defined(TARGET_NR_pidfd_send_signal)
  8371. case TARGET_NR_pidfd_send_signal:
  8372. {
  8373. siginfo_t uinfo, *puinfo;
  8374. if (arg3) {
  8375. p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1);
  8376. if (!p) {
  8377. return -TARGET_EFAULT;
  8378. }
  8379. target_to_host_siginfo(&uinfo, p);
  8380. unlock_user(p, arg3, 0);
  8381. puinfo = &uinfo;
  8382. } else {
  8383. puinfo = NULL;
  8384. }
  8385. ret = get_errno(pidfd_send_signal(arg1, target_to_host_signal(arg2),
  8386. puinfo, arg4));
  8387. }
  8388. return ret;
  8389. #endif
  8390. #if defined(__NR_pidfd_getfd) && defined(TARGET_NR_pidfd_getfd)
  8391. case TARGET_NR_pidfd_getfd:
  8392. return get_errno(pidfd_getfd(arg1, arg2, arg3));
  8393. #endif
  8394. case TARGET_NR_close:
  8395. fd_trans_unregister(arg1);
  8396. return get_errno(close(arg1));
  8397. #if defined(__NR_close_range) && defined(TARGET_NR_close_range)
  8398. case TARGET_NR_close_range:
  8399. ret = get_errno(sys_close_range(arg1, arg2, arg3));
  8400. if (ret == 0 && !(arg3 & CLOSE_RANGE_CLOEXEC)) {
  8401. abi_long fd, maxfd;
  8402. maxfd = MIN(arg2, target_fd_max);
  8403. for (fd = arg1; fd < maxfd; fd++) {
  8404. fd_trans_unregister(fd);
  8405. }
  8406. }
  8407. return ret;
  8408. #endif
  8409. case TARGET_NR_brk:
  8410. return do_brk(arg1);
  8411. #ifdef TARGET_NR_fork
  8412. case TARGET_NR_fork:
  8413. return get_errno(do_fork(cpu_env, TARGET_SIGCHLD, 0, 0, 0, 0));
  8414. #endif
  8415. #ifdef TARGET_NR_waitpid
  8416. case TARGET_NR_waitpid:
  8417. {
  8418. int status;
  8419. ret = get_errno(safe_wait4(arg1, &status, arg3, 0));
  8420. if (!is_error(ret) && arg2 && ret
  8421. && put_user_s32(host_to_target_waitstatus(status), arg2))
  8422. return -TARGET_EFAULT;
  8423. }
  8424. return ret;
  8425. #endif
  8426. #ifdef TARGET_NR_waitid
  8427. case TARGET_NR_waitid:
  8428. {
  8429. siginfo_t info;
  8430. info.si_pid = 0;
  8431. ret = get_errno(safe_waitid(arg1, arg2, &info, arg4, NULL));
  8432. if (!is_error(ret) && arg3 && info.si_pid != 0) {
  8433. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
  8434. return -TARGET_EFAULT;
  8435. host_to_target_siginfo(p, &info);
  8436. unlock_user(p, arg3, sizeof(target_siginfo_t));
  8437. }
  8438. }
  8439. return ret;
  8440. #endif
  8441. #ifdef TARGET_NR_creat /* not on alpha */
  8442. case TARGET_NR_creat:
  8443. if (!(p = lock_user_string(arg1)))
  8444. return -TARGET_EFAULT;
  8445. ret = get_errno(creat(p, arg2));
  8446. fd_trans_unregister(ret);
  8447. unlock_user(p, arg1, 0);
  8448. return ret;
  8449. #endif
  8450. #ifdef TARGET_NR_link
  8451. case TARGET_NR_link:
  8452. {
  8453. void * p2;
  8454. p = lock_user_string(arg1);
  8455. p2 = lock_user_string(arg2);
  8456. if (!p || !p2)
  8457. ret = -TARGET_EFAULT;
  8458. else
  8459. ret = get_errno(link(p, p2));
  8460. unlock_user(p2, arg2, 0);
  8461. unlock_user(p, arg1, 0);
  8462. }
  8463. return ret;
  8464. #endif
  8465. #if defined(TARGET_NR_linkat)
  8466. case TARGET_NR_linkat:
  8467. {
  8468. void * p2 = NULL;
  8469. if (!arg2 || !arg4)
  8470. return -TARGET_EFAULT;
  8471. p = lock_user_string(arg2);
  8472. p2 = lock_user_string(arg4);
  8473. if (!p || !p2)
  8474. ret = -TARGET_EFAULT;
  8475. else
  8476. ret = get_errno(linkat(arg1, p, arg3, p2, arg5));
  8477. unlock_user(p, arg2, 0);
  8478. unlock_user(p2, arg4, 0);
  8479. }
  8480. return ret;
  8481. #endif
  8482. #ifdef TARGET_NR_unlink
  8483. case TARGET_NR_unlink:
  8484. if (!(p = lock_user_string(arg1)))
  8485. return -TARGET_EFAULT;
  8486. ret = get_errno(unlink(p));
  8487. unlock_user(p, arg1, 0);
  8488. return ret;
  8489. #endif
  8490. #if defined(TARGET_NR_unlinkat)
  8491. case TARGET_NR_unlinkat:
  8492. if (!(p = lock_user_string(arg2)))
  8493. return -TARGET_EFAULT;
  8494. ret = get_errno(unlinkat(arg1, p, arg3));
  8495. unlock_user(p, arg2, 0);
  8496. return ret;
  8497. #endif
  8498. case TARGET_NR_execveat:
  8499. return do_execv(cpu_env, arg1, arg2, arg3, arg4, arg5, true);
  8500. case TARGET_NR_execve:
  8501. return do_execv(cpu_env, AT_FDCWD, arg1, arg2, arg3, 0, false);
  8502. case TARGET_NR_chdir:
  8503. if (!(p = lock_user_string(arg1)))
  8504. return -TARGET_EFAULT;
  8505. ret = get_errno(chdir(p));
  8506. unlock_user(p, arg1, 0);
  8507. return ret;
  8508. #ifdef TARGET_NR_time
  8509. case TARGET_NR_time:
  8510. {
  8511. time_t host_time;
  8512. ret = get_errno(time(&host_time));
  8513. if (!is_error(ret)
  8514. && arg1
  8515. && put_user_sal(host_time, arg1))
  8516. return -TARGET_EFAULT;
  8517. }
  8518. return ret;
  8519. #endif
  8520. #ifdef TARGET_NR_mknod
  8521. case TARGET_NR_mknod:
  8522. if (!(p = lock_user_string(arg1)))
  8523. return -TARGET_EFAULT;
  8524. ret = get_errno(mknod(p, arg2, arg3));
  8525. unlock_user(p, arg1, 0);
  8526. return ret;
  8527. #endif
  8528. #if defined(TARGET_NR_mknodat)
  8529. case TARGET_NR_mknodat:
  8530. if (!(p = lock_user_string(arg2)))
  8531. return -TARGET_EFAULT;
  8532. ret = get_errno(mknodat(arg1, p, arg3, arg4));
  8533. unlock_user(p, arg2, 0);
  8534. return ret;
  8535. #endif
  8536. #ifdef TARGET_NR_chmod
  8537. case TARGET_NR_chmod:
  8538. if (!(p = lock_user_string(arg1)))
  8539. return -TARGET_EFAULT;
  8540. ret = get_errno(chmod(p, arg2));
  8541. unlock_user(p, arg1, 0);
  8542. return ret;
  8543. #endif
  8544. #ifdef TARGET_NR_lseek
  8545. case TARGET_NR_lseek:
  8546. return get_errno(lseek(arg1, arg2, arg3));
  8547. #endif
  8548. #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
  8549. /* Alpha specific */
  8550. case TARGET_NR_getxpid:
  8551. cpu_env->ir[IR_A4] = getppid();
  8552. return get_errno(getpid());
  8553. #endif
  8554. #ifdef TARGET_NR_getpid
  8555. case TARGET_NR_getpid:
  8556. return get_errno(getpid());
  8557. #endif
  8558. case TARGET_NR_mount:
  8559. {
  8560. /* need to look at the data field */
  8561. void *p2, *p3;
  8562. if (arg1) {
  8563. p = lock_user_string(arg1);
  8564. if (!p) {
  8565. return -TARGET_EFAULT;
  8566. }
  8567. } else {
  8568. p = NULL;
  8569. }
  8570. p2 = lock_user_string(arg2);
  8571. if (!p2) {
  8572. if (arg1) {
  8573. unlock_user(p, arg1, 0);
  8574. }
  8575. return -TARGET_EFAULT;
  8576. }
  8577. if (arg3) {
  8578. p3 = lock_user_string(arg3);
  8579. if (!p3) {
  8580. if (arg1) {
  8581. unlock_user(p, arg1, 0);
  8582. }
  8583. unlock_user(p2, arg2, 0);
  8584. return -TARGET_EFAULT;
  8585. }
  8586. } else {
  8587. p3 = NULL;
  8588. }
  8589. /* FIXME - arg5 should be locked, but it isn't clear how to
  8590. * do that since it's not guaranteed to be a NULL-terminated
  8591. * string.
  8592. */
  8593. if (!arg5) {
  8594. ret = mount(p, p2, p3, (unsigned long)arg4, NULL);
  8595. } else {
  8596. ret = mount(p, p2, p3, (unsigned long)arg4, g2h(cpu, arg5));
  8597. }
  8598. ret = get_errno(ret);
  8599. if (arg1) {
  8600. unlock_user(p, arg1, 0);
  8601. }
  8602. unlock_user(p2, arg2, 0);
  8603. if (arg3) {
  8604. unlock_user(p3, arg3, 0);
  8605. }
  8606. }
  8607. return ret;
  8608. #if defined(TARGET_NR_umount) || defined(TARGET_NR_oldumount)
  8609. #if defined(TARGET_NR_umount)
  8610. case TARGET_NR_umount:
  8611. #endif
  8612. #if defined(TARGET_NR_oldumount)
  8613. case TARGET_NR_oldumount:
  8614. #endif
  8615. if (!(p = lock_user_string(arg1)))
  8616. return -TARGET_EFAULT;
  8617. ret = get_errno(umount(p));
  8618. unlock_user(p, arg1, 0);
  8619. return ret;
  8620. #endif
  8621. #if defined(TARGET_NR_move_mount) && defined(__NR_move_mount)
  8622. case TARGET_NR_move_mount:
  8623. {
  8624. void *p2, *p4;
  8625. if (!arg2 || !arg4) {
  8626. return -TARGET_EFAULT;
  8627. }
  8628. p2 = lock_user_string(arg2);
  8629. if (!p2) {
  8630. return -TARGET_EFAULT;
  8631. }
  8632. p4 = lock_user_string(arg4);
  8633. if (!p4) {
  8634. unlock_user(p2, arg2, 0);
  8635. return -TARGET_EFAULT;
  8636. }
  8637. ret = get_errno(sys_move_mount(arg1, p2, arg3, p4, arg5));
  8638. unlock_user(p2, arg2, 0);
  8639. unlock_user(p4, arg4, 0);
  8640. return ret;
  8641. }
  8642. #endif
  8643. #if defined(TARGET_NR_open_tree) && defined(__NR_open_tree)
  8644. case TARGET_NR_open_tree:
  8645. {
  8646. void *p2;
  8647. int host_flags;
  8648. if (!arg2) {
  8649. return -TARGET_EFAULT;
  8650. }
  8651. p2 = lock_user_string(arg2);
  8652. if (!p2) {
  8653. return -TARGET_EFAULT;
  8654. }
  8655. host_flags = arg3 & ~TARGET_O_CLOEXEC;
  8656. if (arg3 & TARGET_O_CLOEXEC) {
  8657. host_flags |= O_CLOEXEC;
  8658. }
  8659. ret = get_errno(sys_open_tree(arg1, p2, host_flags));
  8660. unlock_user(p2, arg2, 0);
  8661. return ret;
  8662. }
  8663. #endif
  8664. #ifdef TARGET_NR_stime /* not on alpha */
  8665. case TARGET_NR_stime:
  8666. {
  8667. struct timespec ts;
  8668. ts.tv_nsec = 0;
  8669. if (get_user_sal(ts.tv_sec, arg1)) {
  8670. return -TARGET_EFAULT;
  8671. }
  8672. return get_errno(clock_settime(CLOCK_REALTIME, &ts));
  8673. }
  8674. #endif
  8675. #ifdef TARGET_NR_alarm /* not on alpha */
  8676. case TARGET_NR_alarm:
  8677. return alarm(arg1);
  8678. #endif
  8679. #ifdef TARGET_NR_pause /* not on alpha */
  8680. case TARGET_NR_pause:
  8681. if (!block_signals()) {
  8682. sigsuspend(&((TaskState *)cpu->opaque)->signal_mask);
  8683. }
  8684. return -TARGET_EINTR;
  8685. #endif
  8686. #ifdef TARGET_NR_utime
  8687. case TARGET_NR_utime:
  8688. {
  8689. struct utimbuf tbuf, *host_tbuf;
  8690. struct target_utimbuf *target_tbuf;
  8691. if (arg2) {
  8692. if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
  8693. return -TARGET_EFAULT;
  8694. tbuf.actime = tswapal(target_tbuf->actime);
  8695. tbuf.modtime = tswapal(target_tbuf->modtime);
  8696. unlock_user_struct(target_tbuf, arg2, 0);
  8697. host_tbuf = &tbuf;
  8698. } else {
  8699. host_tbuf = NULL;
  8700. }
  8701. if (!(p = lock_user_string(arg1)))
  8702. return -TARGET_EFAULT;
  8703. ret = get_errno(utime(p, host_tbuf));
  8704. unlock_user(p, arg1, 0);
  8705. }
  8706. return ret;
  8707. #endif
  8708. #ifdef TARGET_NR_utimes
  8709. case TARGET_NR_utimes:
  8710. {
  8711. struct timeval *tvp, tv[2];
  8712. if (arg2) {
  8713. if (copy_from_user_timeval(&tv[0], arg2)
  8714. || copy_from_user_timeval(&tv[1],
  8715. arg2 + sizeof(struct target_timeval)))
  8716. return -TARGET_EFAULT;
  8717. tvp = tv;
  8718. } else {
  8719. tvp = NULL;
  8720. }
  8721. if (!(p = lock_user_string(arg1)))
  8722. return -TARGET_EFAULT;
  8723. ret = get_errno(utimes(p, tvp));
  8724. unlock_user(p, arg1, 0);
  8725. }
  8726. return ret;
  8727. #endif
  8728. #if defined(TARGET_NR_futimesat)
  8729. case TARGET_NR_futimesat:
  8730. {
  8731. struct timeval *tvp, tv[2];
  8732. if (arg3) {
  8733. if (copy_from_user_timeval(&tv[0], arg3)
  8734. || copy_from_user_timeval(&tv[1],
  8735. arg3 + sizeof(struct target_timeval)))
  8736. return -TARGET_EFAULT;
  8737. tvp = tv;
  8738. } else {
  8739. tvp = NULL;
  8740. }
  8741. if (!(p = lock_user_string(arg2))) {
  8742. return -TARGET_EFAULT;
  8743. }
  8744. ret = get_errno(futimesat(arg1, path(p), tvp));
  8745. unlock_user(p, arg2, 0);
  8746. }
  8747. return ret;
  8748. #endif
  8749. #ifdef TARGET_NR_access
  8750. case TARGET_NR_access:
  8751. if (!(p = lock_user_string(arg1))) {
  8752. return -TARGET_EFAULT;
  8753. }
  8754. ret = get_errno(access(path(p), arg2));
  8755. unlock_user(p, arg1, 0);
  8756. return ret;
  8757. #endif
  8758. #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
  8759. case TARGET_NR_faccessat:
  8760. if (!(p = lock_user_string(arg2))) {
  8761. return -TARGET_EFAULT;
  8762. }
  8763. ret = get_errno(faccessat(arg1, p, arg3, 0));
  8764. unlock_user(p, arg2, 0);
  8765. return ret;
  8766. #endif
  8767. #if defined(TARGET_NR_faccessat2)
  8768. case TARGET_NR_faccessat2:
  8769. if (!(p = lock_user_string(arg2))) {
  8770. return -TARGET_EFAULT;
  8771. }
  8772. ret = get_errno(faccessat(arg1, p, arg3, arg4));
  8773. unlock_user(p, arg2, 0);
  8774. return ret;
  8775. #endif
  8776. #ifdef TARGET_NR_nice /* not on alpha */
  8777. case TARGET_NR_nice:
  8778. return get_errno(nice(arg1));
  8779. #endif
  8780. case TARGET_NR_sync:
  8781. sync();
  8782. return 0;
  8783. #if defined(TARGET_NR_syncfs) && defined(CONFIG_SYNCFS)
  8784. case TARGET_NR_syncfs:
  8785. return get_errno(syncfs(arg1));
  8786. #endif
  8787. case TARGET_NR_kill:
  8788. return get_errno(safe_kill(arg1, target_to_host_signal(arg2)));
  8789. #ifdef TARGET_NR_rename
  8790. case TARGET_NR_rename:
  8791. {
  8792. void *p2;
  8793. p = lock_user_string(arg1);
  8794. p2 = lock_user_string(arg2);
  8795. if (!p || !p2)
  8796. ret = -TARGET_EFAULT;
  8797. else
  8798. ret = get_errno(rename(p, p2));
  8799. unlock_user(p2, arg2, 0);
  8800. unlock_user(p, arg1, 0);
  8801. }
  8802. return ret;
  8803. #endif
  8804. #if defined(TARGET_NR_renameat)
  8805. case TARGET_NR_renameat:
  8806. {
  8807. void *p2;
  8808. p = lock_user_string(arg2);
  8809. p2 = lock_user_string(arg4);
  8810. if (!p || !p2)
  8811. ret = -TARGET_EFAULT;
  8812. else
  8813. ret = get_errno(renameat(arg1, p, arg3, p2));
  8814. unlock_user(p2, arg4, 0);
  8815. unlock_user(p, arg2, 0);
  8816. }
  8817. return ret;
  8818. #endif
  8819. #if defined(TARGET_NR_renameat2)
  8820. case TARGET_NR_renameat2:
  8821. {
  8822. void *p2;
  8823. p = lock_user_string(arg2);
  8824. p2 = lock_user_string(arg4);
  8825. if (!p || !p2) {
  8826. ret = -TARGET_EFAULT;
  8827. } else {
  8828. ret = get_errno(sys_renameat2(arg1, p, arg3, p2, arg5));
  8829. }
  8830. unlock_user(p2, arg4, 0);
  8831. unlock_user(p, arg2, 0);
  8832. }
  8833. return ret;
  8834. #endif
  8835. #ifdef TARGET_NR_mkdir
  8836. case TARGET_NR_mkdir:
  8837. if (!(p = lock_user_string(arg1)))
  8838. return -TARGET_EFAULT;
  8839. ret = get_errno(mkdir(p, arg2));
  8840. unlock_user(p, arg1, 0);
  8841. return ret;
  8842. #endif
  8843. #if defined(TARGET_NR_mkdirat)
  8844. case TARGET_NR_mkdirat:
  8845. if (!(p = lock_user_string(arg2)))
  8846. return -TARGET_EFAULT;
  8847. ret = get_errno(mkdirat(arg1, p, arg3));
  8848. unlock_user(p, arg2, 0);
  8849. return ret;
  8850. #endif
  8851. #ifdef TARGET_NR_rmdir
  8852. case TARGET_NR_rmdir:
  8853. if (!(p = lock_user_string(arg1)))
  8854. return -TARGET_EFAULT;
  8855. ret = get_errno(rmdir(p));
  8856. unlock_user(p, arg1, 0);
  8857. return ret;
  8858. #endif
  8859. case TARGET_NR_dup:
  8860. ret = get_errno(dup(arg1));
  8861. if (ret >= 0) {
  8862. fd_trans_dup(arg1, ret);
  8863. }
  8864. return ret;
  8865. #ifdef TARGET_NR_pipe
  8866. case TARGET_NR_pipe:
  8867. return do_pipe(cpu_env, arg1, 0, 0);
  8868. #endif
  8869. #ifdef TARGET_NR_pipe2
  8870. case TARGET_NR_pipe2:
  8871. return do_pipe(cpu_env, arg1,
  8872. target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
  8873. #endif
  8874. case TARGET_NR_times:
  8875. {
  8876. struct target_tms *tmsp;
  8877. struct tms tms;
  8878. ret = get_errno(times(&tms));
  8879. if (arg1) {
  8880. tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
  8881. if (!tmsp)
  8882. return -TARGET_EFAULT;
  8883. tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
  8884. tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
  8885. tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
  8886. tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
  8887. }
  8888. if (!is_error(ret))
  8889. ret = host_to_target_clock_t(ret);
  8890. }
  8891. return ret;
  8892. case TARGET_NR_acct:
  8893. if (arg1 == 0) {
  8894. ret = get_errno(acct(NULL));
  8895. } else {
  8896. if (!(p = lock_user_string(arg1))) {
  8897. return -TARGET_EFAULT;
  8898. }
  8899. ret = get_errno(acct(path(p)));
  8900. unlock_user(p, arg1, 0);
  8901. }
  8902. return ret;
  8903. #ifdef TARGET_NR_umount2
  8904. case TARGET_NR_umount2:
  8905. if (!(p = lock_user_string(arg1)))
  8906. return -TARGET_EFAULT;
  8907. ret = get_errno(umount2(p, arg2));
  8908. unlock_user(p, arg1, 0);
  8909. return ret;
  8910. #endif
  8911. case TARGET_NR_ioctl:
  8912. return do_ioctl(arg1, arg2, arg3);
  8913. #ifdef TARGET_NR_fcntl
  8914. case TARGET_NR_fcntl:
  8915. return do_fcntl(arg1, arg2, arg3);
  8916. #endif
  8917. case TARGET_NR_setpgid:
  8918. return get_errno(setpgid(arg1, arg2));
  8919. case TARGET_NR_umask:
  8920. return get_errno(umask(arg1));
  8921. case TARGET_NR_chroot:
  8922. if (!(p = lock_user_string(arg1)))
  8923. return -TARGET_EFAULT;
  8924. ret = get_errno(chroot(p));
  8925. unlock_user(p, arg1, 0);
  8926. return ret;
  8927. #ifdef TARGET_NR_dup2
  8928. case TARGET_NR_dup2:
  8929. ret = get_errno(dup2(arg1, arg2));
  8930. if (ret >= 0) {
  8931. fd_trans_dup(arg1, arg2);
  8932. }
  8933. return ret;
  8934. #endif
  8935. #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
  8936. case TARGET_NR_dup3:
  8937. {
  8938. int host_flags;
  8939. if ((arg3 & ~TARGET_O_CLOEXEC) != 0) {
  8940. return -EINVAL;
  8941. }
  8942. host_flags = target_to_host_bitmask(arg3, fcntl_flags_tbl);
  8943. ret = get_errno(dup3(arg1, arg2, host_flags));
  8944. if (ret >= 0) {
  8945. fd_trans_dup(arg1, arg2);
  8946. }
  8947. return ret;
  8948. }
  8949. #endif
  8950. #ifdef TARGET_NR_getppid /* not on alpha */
  8951. case TARGET_NR_getppid:
  8952. return get_errno(getppid());
  8953. #endif
  8954. #ifdef TARGET_NR_getpgrp
  8955. case TARGET_NR_getpgrp:
  8956. return get_errno(getpgrp());
  8957. #endif
  8958. case TARGET_NR_setsid:
  8959. return get_errno(setsid());
  8960. #ifdef TARGET_NR_sigaction
  8961. case TARGET_NR_sigaction:
  8962. {
  8963. #if defined(TARGET_MIPS)
  8964. struct target_sigaction act, oact, *pact, *old_act;
  8965. if (arg2) {
  8966. if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
  8967. return -TARGET_EFAULT;
  8968. act._sa_handler = old_act->_sa_handler;
  8969. target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
  8970. act.sa_flags = old_act->sa_flags;
  8971. unlock_user_struct(old_act, arg2, 0);
  8972. pact = &act;
  8973. } else {
  8974. pact = NULL;
  8975. }
  8976. ret = get_errno(do_sigaction(arg1, pact, &oact, 0));
  8977. if (!is_error(ret) && arg3) {
  8978. if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
  8979. return -TARGET_EFAULT;
  8980. old_act->_sa_handler = oact._sa_handler;
  8981. old_act->sa_flags = oact.sa_flags;
  8982. old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
  8983. old_act->sa_mask.sig[1] = 0;
  8984. old_act->sa_mask.sig[2] = 0;
  8985. old_act->sa_mask.sig[3] = 0;
  8986. unlock_user_struct(old_act, arg3, 1);
  8987. }
  8988. #else
  8989. struct target_old_sigaction *old_act;
  8990. struct target_sigaction act, oact, *pact;
  8991. if (arg2) {
  8992. if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
  8993. return -TARGET_EFAULT;
  8994. act._sa_handler = old_act->_sa_handler;
  8995. target_siginitset(&act.sa_mask, old_act->sa_mask);
  8996. act.sa_flags = old_act->sa_flags;
  8997. #ifdef TARGET_ARCH_HAS_SA_RESTORER
  8998. act.sa_restorer = old_act->sa_restorer;
  8999. #endif
  9000. unlock_user_struct(old_act, arg2, 0);
  9001. pact = &act;
  9002. } else {
  9003. pact = NULL;
  9004. }
  9005. ret = get_errno(do_sigaction(arg1, pact, &oact, 0));
  9006. if (!is_error(ret) && arg3) {
  9007. if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
  9008. return -TARGET_EFAULT;
  9009. old_act->_sa_handler = oact._sa_handler;
  9010. old_act->sa_mask = oact.sa_mask.sig[0];
  9011. old_act->sa_flags = oact.sa_flags;
  9012. #ifdef TARGET_ARCH_HAS_SA_RESTORER
  9013. old_act->sa_restorer = oact.sa_restorer;
  9014. #endif
  9015. unlock_user_struct(old_act, arg3, 1);
  9016. }
  9017. #endif
  9018. }
  9019. return ret;
  9020. #endif
  9021. case TARGET_NR_rt_sigaction:
  9022. {
  9023. /*
  9024. * For Alpha and SPARC this is a 5 argument syscall, with
  9025. * a 'restorer' parameter which must be copied into the
  9026. * sa_restorer field of the sigaction struct.
  9027. * For Alpha that 'restorer' is arg5; for SPARC it is arg4,
  9028. * and arg5 is the sigsetsize.
  9029. */
  9030. #if defined(TARGET_ALPHA)
  9031. target_ulong sigsetsize = arg4;
  9032. target_ulong restorer = arg5;
  9033. #elif defined(TARGET_SPARC)
  9034. target_ulong restorer = arg4;
  9035. target_ulong sigsetsize = arg5;
  9036. #else
  9037. target_ulong sigsetsize = arg4;
  9038. target_ulong restorer = 0;
  9039. #endif
  9040. struct target_sigaction *act = NULL;
  9041. struct target_sigaction *oact = NULL;
  9042. if (sigsetsize != sizeof(target_sigset_t)) {
  9043. return -TARGET_EINVAL;
  9044. }
  9045. if (arg2 && !lock_user_struct(VERIFY_READ, act, arg2, 1)) {
  9046. return -TARGET_EFAULT;
  9047. }
  9048. if (arg3 && !lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
  9049. ret = -TARGET_EFAULT;
  9050. } else {
  9051. ret = get_errno(do_sigaction(arg1, act, oact, restorer));
  9052. if (oact) {
  9053. unlock_user_struct(oact, arg3, 1);
  9054. }
  9055. }
  9056. if (act) {
  9057. unlock_user_struct(act, arg2, 0);
  9058. }
  9059. }
  9060. return ret;
  9061. #ifdef TARGET_NR_sgetmask /* not on alpha */
  9062. case TARGET_NR_sgetmask:
  9063. {
  9064. sigset_t cur_set;
  9065. abi_ulong target_set;
  9066. ret = do_sigprocmask(0, NULL, &cur_set);
  9067. if (!ret) {
  9068. host_to_target_old_sigset(&target_set, &cur_set);
  9069. ret = target_set;
  9070. }
  9071. }
  9072. return ret;
  9073. #endif
  9074. #ifdef TARGET_NR_ssetmask /* not on alpha */
  9075. case TARGET_NR_ssetmask:
  9076. {
  9077. sigset_t set, oset;
  9078. abi_ulong target_set = arg1;
  9079. target_to_host_old_sigset(&set, &target_set);
  9080. ret = do_sigprocmask(SIG_SETMASK, &set, &oset);
  9081. if (!ret) {
  9082. host_to_target_old_sigset(&target_set, &oset);
  9083. ret = target_set;
  9084. }
  9085. }
  9086. return ret;
  9087. #endif
  9088. #ifdef TARGET_NR_sigprocmask
  9089. case TARGET_NR_sigprocmask:
  9090. {
  9091. #if defined(TARGET_ALPHA)
  9092. sigset_t set, oldset;
  9093. abi_ulong mask;
  9094. int how;
  9095. switch (arg1) {
  9096. case TARGET_SIG_BLOCK:
  9097. how = SIG_BLOCK;
  9098. break;
  9099. case TARGET_SIG_UNBLOCK:
  9100. how = SIG_UNBLOCK;
  9101. break;
  9102. case TARGET_SIG_SETMASK:
  9103. how = SIG_SETMASK;
  9104. break;
  9105. default:
  9106. return -TARGET_EINVAL;
  9107. }
  9108. mask = arg2;
  9109. target_to_host_old_sigset(&set, &mask);
  9110. ret = do_sigprocmask(how, &set, &oldset);
  9111. if (!is_error(ret)) {
  9112. host_to_target_old_sigset(&mask, &oldset);
  9113. ret = mask;
  9114. cpu_env->ir[IR_V0] = 0; /* force no error */
  9115. }
  9116. #else
  9117. sigset_t set, oldset, *set_ptr;
  9118. int how;
  9119. if (arg2) {
  9120. p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1);
  9121. if (!p) {
  9122. return -TARGET_EFAULT;
  9123. }
  9124. target_to_host_old_sigset(&set, p);
  9125. unlock_user(p, arg2, 0);
  9126. set_ptr = &set;
  9127. switch (arg1) {
  9128. case TARGET_SIG_BLOCK:
  9129. how = SIG_BLOCK;
  9130. break;
  9131. case TARGET_SIG_UNBLOCK:
  9132. how = SIG_UNBLOCK;
  9133. break;
  9134. case TARGET_SIG_SETMASK:
  9135. how = SIG_SETMASK;
  9136. break;
  9137. default:
  9138. return -TARGET_EINVAL;
  9139. }
  9140. } else {
  9141. how = 0;
  9142. set_ptr = NULL;
  9143. }
  9144. ret = do_sigprocmask(how, set_ptr, &oldset);
  9145. if (!is_error(ret) && arg3) {
  9146. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
  9147. return -TARGET_EFAULT;
  9148. host_to_target_old_sigset(p, &oldset);
  9149. unlock_user(p, arg3, sizeof(target_sigset_t));
  9150. }
  9151. #endif
  9152. }
  9153. return ret;
  9154. #endif
  9155. case TARGET_NR_rt_sigprocmask:
  9156. {
  9157. int how = arg1;
  9158. sigset_t set, oldset, *set_ptr;
  9159. if (arg4 != sizeof(target_sigset_t)) {
  9160. return -TARGET_EINVAL;
  9161. }
  9162. if (arg2) {
  9163. p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1);
  9164. if (!p) {
  9165. return -TARGET_EFAULT;
  9166. }
  9167. target_to_host_sigset(&set, p);
  9168. unlock_user(p, arg2, 0);
  9169. set_ptr = &set;
  9170. switch(how) {
  9171. case TARGET_SIG_BLOCK:
  9172. how = SIG_BLOCK;
  9173. break;
  9174. case TARGET_SIG_UNBLOCK:
  9175. how = SIG_UNBLOCK;
  9176. break;
  9177. case TARGET_SIG_SETMASK:
  9178. how = SIG_SETMASK;
  9179. break;
  9180. default:
  9181. return -TARGET_EINVAL;
  9182. }
  9183. } else {
  9184. how = 0;
  9185. set_ptr = NULL;
  9186. }
  9187. ret = do_sigprocmask(how, set_ptr, &oldset);
  9188. if (!is_error(ret) && arg3) {
  9189. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
  9190. return -TARGET_EFAULT;
  9191. host_to_target_sigset(p, &oldset);
  9192. unlock_user(p, arg3, sizeof(target_sigset_t));
  9193. }
  9194. }
  9195. return ret;
  9196. #ifdef TARGET_NR_sigpending
  9197. case TARGET_NR_sigpending:
  9198. {
  9199. sigset_t set;
  9200. ret = get_errno(sigpending(&set));
  9201. if (!is_error(ret)) {
  9202. if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
  9203. return -TARGET_EFAULT;
  9204. host_to_target_old_sigset(p, &set);
  9205. unlock_user(p, arg1, sizeof(target_sigset_t));
  9206. }
  9207. }
  9208. return ret;
  9209. #endif
  9210. case TARGET_NR_rt_sigpending:
  9211. {
  9212. sigset_t set;
  9213. /* Yes, this check is >, not != like most. We follow the kernel's
  9214. * logic and it does it like this because it implements
  9215. * NR_sigpending through the same code path, and in that case
  9216. * the old_sigset_t is smaller in size.
  9217. */
  9218. if (arg2 > sizeof(target_sigset_t)) {
  9219. return -TARGET_EINVAL;
  9220. }
  9221. ret = get_errno(sigpending(&set));
  9222. if (!is_error(ret)) {
  9223. if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
  9224. return -TARGET_EFAULT;
  9225. host_to_target_sigset(p, &set);
  9226. unlock_user(p, arg1, sizeof(target_sigset_t));
  9227. }
  9228. }
  9229. return ret;
  9230. #ifdef TARGET_NR_sigsuspend
  9231. case TARGET_NR_sigsuspend:
  9232. {
  9233. sigset_t *set;
  9234. #if defined(TARGET_ALPHA)
  9235. TaskState *ts = cpu->opaque;
  9236. /* target_to_host_old_sigset will bswap back */
  9237. abi_ulong mask = tswapal(arg1);
  9238. set = &ts->sigsuspend_mask;
  9239. target_to_host_old_sigset(set, &mask);
  9240. #else
  9241. ret = process_sigsuspend_mask(&set, arg1, sizeof(target_sigset_t));
  9242. if (ret != 0) {
  9243. return ret;
  9244. }
  9245. #endif
  9246. ret = get_errno(safe_rt_sigsuspend(set, SIGSET_T_SIZE));
  9247. finish_sigsuspend_mask(ret);
  9248. }
  9249. return ret;
  9250. #endif
  9251. case TARGET_NR_rt_sigsuspend:
  9252. {
  9253. sigset_t *set;
  9254. ret = process_sigsuspend_mask(&set, arg1, arg2);
  9255. if (ret != 0) {
  9256. return ret;
  9257. }
  9258. ret = get_errno(safe_rt_sigsuspend(set, SIGSET_T_SIZE));
  9259. finish_sigsuspend_mask(ret);
  9260. }
  9261. return ret;
  9262. #ifdef TARGET_NR_rt_sigtimedwait
  9263. case TARGET_NR_rt_sigtimedwait:
  9264. {
  9265. sigset_t set;
  9266. struct timespec uts, *puts;
  9267. siginfo_t uinfo;
  9268. if (arg4 != sizeof(target_sigset_t)) {
  9269. return -TARGET_EINVAL;
  9270. }
  9271. if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
  9272. return -TARGET_EFAULT;
  9273. target_to_host_sigset(&set, p);
  9274. unlock_user(p, arg1, 0);
  9275. if (arg3) {
  9276. puts = &uts;
  9277. if (target_to_host_timespec(puts, arg3)) {
  9278. return -TARGET_EFAULT;
  9279. }
  9280. } else {
  9281. puts = NULL;
  9282. }
  9283. ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts,
  9284. SIGSET_T_SIZE));
  9285. if (!is_error(ret)) {
  9286. if (arg2) {
  9287. p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t),
  9288. 0);
  9289. if (!p) {
  9290. return -TARGET_EFAULT;
  9291. }
  9292. host_to_target_siginfo(p, &uinfo);
  9293. unlock_user(p, arg2, sizeof(target_siginfo_t));
  9294. }
  9295. ret = host_to_target_signal(ret);
  9296. }
  9297. }
  9298. return ret;
  9299. #endif
  9300. #ifdef TARGET_NR_rt_sigtimedwait_time64
  9301. case TARGET_NR_rt_sigtimedwait_time64:
  9302. {
  9303. sigset_t set;
  9304. struct timespec uts, *puts;
  9305. siginfo_t uinfo;
  9306. if (arg4 != sizeof(target_sigset_t)) {
  9307. return -TARGET_EINVAL;
  9308. }
  9309. p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1);
  9310. if (!p) {
  9311. return -TARGET_EFAULT;
  9312. }
  9313. target_to_host_sigset(&set, p);
  9314. unlock_user(p, arg1, 0);
  9315. if (arg3) {
  9316. puts = &uts;
  9317. if (target_to_host_timespec64(puts, arg3)) {
  9318. return -TARGET_EFAULT;
  9319. }
  9320. } else {
  9321. puts = NULL;
  9322. }
  9323. ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts,
  9324. SIGSET_T_SIZE));
  9325. if (!is_error(ret)) {
  9326. if (arg2) {
  9327. p = lock_user(VERIFY_WRITE, arg2,
  9328. sizeof(target_siginfo_t), 0);
  9329. if (!p) {
  9330. return -TARGET_EFAULT;
  9331. }
  9332. host_to_target_siginfo(p, &uinfo);
  9333. unlock_user(p, arg2, sizeof(target_siginfo_t));
  9334. }
  9335. ret = host_to_target_signal(ret);
  9336. }
  9337. }
  9338. return ret;
  9339. #endif
  9340. case TARGET_NR_rt_sigqueueinfo:
  9341. {
  9342. siginfo_t uinfo;
  9343. p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1);
  9344. if (!p) {
  9345. return -TARGET_EFAULT;
  9346. }
  9347. target_to_host_siginfo(&uinfo, p);
  9348. unlock_user(p, arg3, 0);
  9349. ret = get_errno(sys_rt_sigqueueinfo(arg1, target_to_host_signal(arg2), &uinfo));
  9350. }
  9351. return ret;
  9352. case TARGET_NR_rt_tgsigqueueinfo:
  9353. {
  9354. siginfo_t uinfo;
  9355. p = lock_user(VERIFY_READ, arg4, sizeof(target_siginfo_t), 1);
  9356. if (!p) {
  9357. return -TARGET_EFAULT;
  9358. }
  9359. target_to_host_siginfo(&uinfo, p);
  9360. unlock_user(p, arg4, 0);
  9361. ret = get_errno(sys_rt_tgsigqueueinfo(arg1, arg2, target_to_host_signal(arg3), &uinfo));
  9362. }
  9363. return ret;
  9364. #ifdef TARGET_NR_sigreturn
  9365. case TARGET_NR_sigreturn:
  9366. if (block_signals()) {
  9367. return -QEMU_ERESTARTSYS;
  9368. }
  9369. return do_sigreturn(cpu_env);
  9370. #endif
  9371. case TARGET_NR_rt_sigreturn:
  9372. if (block_signals()) {
  9373. return -QEMU_ERESTARTSYS;
  9374. }
  9375. return do_rt_sigreturn(cpu_env);
  9376. case TARGET_NR_sethostname:
  9377. if (!(p = lock_user_string(arg1)))
  9378. return -TARGET_EFAULT;
  9379. ret = get_errno(sethostname(p, arg2));
  9380. unlock_user(p, arg1, 0);
  9381. return ret;
  9382. #ifdef TARGET_NR_setrlimit
  9383. case TARGET_NR_setrlimit:
  9384. {
  9385. int resource = target_to_host_resource(arg1);
  9386. struct target_rlimit *target_rlim;
  9387. struct rlimit rlim;
  9388. if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
  9389. return -TARGET_EFAULT;
  9390. rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
  9391. rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
  9392. unlock_user_struct(target_rlim, arg2, 0);
  9393. /*
  9394. * If we just passed through resource limit settings for memory then
  9395. * they would also apply to QEMU's own allocations, and QEMU will
  9396. * crash or hang or die if its allocations fail. Ideally we would
  9397. * track the guest allocations in QEMU and apply the limits ourselves.
  9398. * For now, just tell the guest the call succeeded but don't actually
  9399. * limit anything.
  9400. */
  9401. if (resource != RLIMIT_AS &&
  9402. resource != RLIMIT_DATA &&
  9403. resource != RLIMIT_STACK) {
  9404. return get_errno(setrlimit(resource, &rlim));
  9405. } else {
  9406. return 0;
  9407. }
  9408. }
  9409. #endif
  9410. #ifdef TARGET_NR_getrlimit
  9411. case TARGET_NR_getrlimit:
  9412. {
  9413. int resource = target_to_host_resource(arg1);
  9414. struct target_rlimit *target_rlim;
  9415. struct rlimit rlim;
  9416. ret = get_errno(getrlimit(resource, &rlim));
  9417. if (!is_error(ret)) {
  9418. if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
  9419. return -TARGET_EFAULT;
  9420. target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
  9421. target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
  9422. unlock_user_struct(target_rlim, arg2, 1);
  9423. }
  9424. }
  9425. return ret;
  9426. #endif
  9427. case TARGET_NR_getrusage:
  9428. {
  9429. struct rusage rusage;
  9430. ret = get_errno(getrusage(arg1, &rusage));
  9431. if (!is_error(ret)) {
  9432. ret = host_to_target_rusage(arg2, &rusage);
  9433. }
  9434. }
  9435. return ret;
  9436. #if defined(TARGET_NR_gettimeofday)
  9437. case TARGET_NR_gettimeofday:
  9438. {
  9439. struct timeval tv;
  9440. struct timezone tz;
  9441. ret = get_errno(gettimeofday(&tv, &tz));
  9442. if (!is_error(ret)) {
  9443. if (arg1 && copy_to_user_timeval(arg1, &tv)) {
  9444. return -TARGET_EFAULT;
  9445. }
  9446. if (arg2 && copy_to_user_timezone(arg2, &tz)) {
  9447. return -TARGET_EFAULT;
  9448. }
  9449. }
  9450. }
  9451. return ret;
  9452. #endif
  9453. #if defined(TARGET_NR_settimeofday)
  9454. case TARGET_NR_settimeofday:
  9455. {
  9456. struct timeval tv, *ptv = NULL;
  9457. struct timezone tz, *ptz = NULL;
  9458. if (arg1) {
  9459. if (copy_from_user_timeval(&tv, arg1)) {
  9460. return -TARGET_EFAULT;
  9461. }
  9462. ptv = &tv;
  9463. }
  9464. if (arg2) {
  9465. if (copy_from_user_timezone(&tz, arg2)) {
  9466. return -TARGET_EFAULT;
  9467. }
  9468. ptz = &tz;
  9469. }
  9470. return get_errno(settimeofday(ptv, ptz));
  9471. }
  9472. #endif
  9473. #if defined(TARGET_NR_select)
  9474. case TARGET_NR_select:
  9475. #if defined(TARGET_WANT_NI_OLD_SELECT)
  9476. /* some architectures used to have old_select here
  9477. * but now ENOSYS it.
  9478. */
  9479. ret = -TARGET_ENOSYS;
  9480. #elif defined(TARGET_WANT_OLD_SYS_SELECT)
  9481. ret = do_old_select(arg1);
  9482. #else
  9483. ret = do_select(arg1, arg2, arg3, arg4, arg5);
  9484. #endif
  9485. return ret;
  9486. #endif
  9487. #ifdef TARGET_NR_pselect6
  9488. case TARGET_NR_pselect6:
  9489. return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, false);
  9490. #endif
  9491. #ifdef TARGET_NR_pselect6_time64
  9492. case TARGET_NR_pselect6_time64:
  9493. return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, true);
  9494. #endif
  9495. #ifdef TARGET_NR_symlink
  9496. case TARGET_NR_symlink:
  9497. {
  9498. void *p2;
  9499. p = lock_user_string(arg1);
  9500. p2 = lock_user_string(arg2);
  9501. if (!p || !p2)
  9502. ret = -TARGET_EFAULT;
  9503. else
  9504. ret = get_errno(symlink(p, p2));
  9505. unlock_user(p2, arg2, 0);
  9506. unlock_user(p, arg1, 0);
  9507. }
  9508. return ret;
  9509. #endif
  9510. #if defined(TARGET_NR_symlinkat)
  9511. case TARGET_NR_symlinkat:
  9512. {
  9513. void *p2;
  9514. p = lock_user_string(arg1);
  9515. p2 = lock_user_string(arg3);
  9516. if (!p || !p2)
  9517. ret = -TARGET_EFAULT;
  9518. else
  9519. ret = get_errno(symlinkat(p, arg2, p2));
  9520. unlock_user(p2, arg3, 0);
  9521. unlock_user(p, arg1, 0);
  9522. }
  9523. return ret;
  9524. #endif
  9525. #ifdef TARGET_NR_readlink
  9526. case TARGET_NR_readlink:
  9527. {
  9528. void *p2;
  9529. p = lock_user_string(arg1);
  9530. p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  9531. ret = get_errno(do_guest_readlink(p, p2, arg3));
  9532. unlock_user(p2, arg2, ret);
  9533. unlock_user(p, arg1, 0);
  9534. }
  9535. return ret;
  9536. #endif
  9537. #if defined(TARGET_NR_readlinkat)
  9538. case TARGET_NR_readlinkat:
  9539. {
  9540. void *p2;
  9541. p = lock_user_string(arg2);
  9542. p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  9543. if (!p || !p2) {
  9544. ret = -TARGET_EFAULT;
  9545. } else if (!arg4) {
  9546. /* Short circuit this for the magic exe check. */
  9547. ret = -TARGET_EINVAL;
  9548. } else if (is_proc_myself((const char *)p, "exe")) {
  9549. /*
  9550. * Don't worry about sign mismatch as earlier mapping
  9551. * logic would have thrown a bad address error.
  9552. */
  9553. ret = MIN(strlen(exec_path), arg4);
  9554. /* We cannot NUL terminate the string. */
  9555. memcpy(p2, exec_path, ret);
  9556. } else {
  9557. ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
  9558. }
  9559. unlock_user(p2, arg3, ret);
  9560. unlock_user(p, arg2, 0);
  9561. }
  9562. return ret;
  9563. #endif
  9564. #ifdef TARGET_NR_swapon
  9565. case TARGET_NR_swapon:
  9566. if (!(p = lock_user_string(arg1)))
  9567. return -TARGET_EFAULT;
  9568. ret = get_errno(swapon(p, arg2));
  9569. unlock_user(p, arg1, 0);
  9570. return ret;
  9571. #endif
  9572. case TARGET_NR_reboot:
  9573. if (arg3 == LINUX_REBOOT_CMD_RESTART2) {
  9574. /* arg4 must be ignored in all other cases */
  9575. p = lock_user_string(arg4);
  9576. if (!p) {
  9577. return -TARGET_EFAULT;
  9578. }
  9579. ret = get_errno(reboot(arg1, arg2, arg3, p));
  9580. unlock_user(p, arg4, 0);
  9581. } else {
  9582. ret = get_errno(reboot(arg1, arg2, arg3, NULL));
  9583. }
  9584. return ret;
  9585. #ifdef TARGET_NR_mmap
  9586. case TARGET_NR_mmap:
  9587. #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
  9588. (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
  9589. defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
  9590. || defined(TARGET_S390X)
  9591. {
  9592. abi_ulong *v;
  9593. abi_ulong v1, v2, v3, v4, v5, v6;
  9594. if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
  9595. return -TARGET_EFAULT;
  9596. v1 = tswapal(v[0]);
  9597. v2 = tswapal(v[1]);
  9598. v3 = tswapal(v[2]);
  9599. v4 = tswapal(v[3]);
  9600. v5 = tswapal(v[4]);
  9601. v6 = tswapal(v[5]);
  9602. unlock_user(v, arg1, 0);
  9603. return do_mmap(v1, v2, v3, v4, v5, v6);
  9604. }
  9605. #else
  9606. /* mmap pointers are always untagged */
  9607. return do_mmap(arg1, arg2, arg3, arg4, arg5, arg6);
  9608. #endif
  9609. #endif
  9610. #ifdef TARGET_NR_mmap2
  9611. case TARGET_NR_mmap2:
  9612. #ifndef MMAP_SHIFT
  9613. #define MMAP_SHIFT 12
  9614. #endif
  9615. return do_mmap(arg1, arg2, arg3, arg4, arg5,
  9616. (off_t)(abi_ulong)arg6 << MMAP_SHIFT);
  9617. #endif
  9618. case TARGET_NR_munmap:
  9619. arg1 = cpu_untagged_addr(cpu, arg1);
  9620. return get_errno(target_munmap(arg1, arg2));
  9621. case TARGET_NR_mprotect:
  9622. arg1 = cpu_untagged_addr(cpu, arg1);
  9623. {
  9624. TaskState *ts = cpu->opaque;
  9625. /* Special hack to detect libc making the stack executable. */
  9626. if ((arg3 & PROT_GROWSDOWN)
  9627. && arg1 >= ts->info->stack_limit
  9628. && arg1 <= ts->info->start_stack) {
  9629. arg3 &= ~PROT_GROWSDOWN;
  9630. arg2 = arg2 + arg1 - ts->info->stack_limit;
  9631. arg1 = ts->info->stack_limit;
  9632. }
  9633. }
  9634. return get_errno(target_mprotect(arg1, arg2, arg3));
  9635. #ifdef TARGET_NR_mremap
  9636. case TARGET_NR_mremap:
  9637. arg1 = cpu_untagged_addr(cpu, arg1);
  9638. /* mremap new_addr (arg5) is always untagged */
  9639. return get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
  9640. #endif
  9641. /* ??? msync/mlock/munlock are broken for softmmu. */
  9642. #ifdef TARGET_NR_msync
  9643. case TARGET_NR_msync:
  9644. return get_errno(msync(g2h(cpu, arg1), arg2,
  9645. target_to_host_msync_arg(arg3)));
  9646. #endif
  9647. #ifdef TARGET_NR_mlock
  9648. case TARGET_NR_mlock:
  9649. return get_errno(mlock(g2h(cpu, arg1), arg2));
  9650. #endif
  9651. #ifdef TARGET_NR_munlock
  9652. case TARGET_NR_munlock:
  9653. return get_errno(munlock(g2h(cpu, arg1), arg2));
  9654. #endif
  9655. #ifdef TARGET_NR_mlockall
  9656. case TARGET_NR_mlockall:
  9657. return get_errno(mlockall(target_to_host_mlockall_arg(arg1)));
  9658. #endif
  9659. #ifdef TARGET_NR_munlockall
  9660. case TARGET_NR_munlockall:
  9661. return get_errno(munlockall());
  9662. #endif
  9663. #ifdef TARGET_NR_truncate
  9664. case TARGET_NR_truncate:
  9665. if (!(p = lock_user_string(arg1)))
  9666. return -TARGET_EFAULT;
  9667. ret = get_errno(truncate(p, arg2));
  9668. unlock_user(p, arg1, 0);
  9669. return ret;
  9670. #endif
  9671. #ifdef TARGET_NR_ftruncate
  9672. case TARGET_NR_ftruncate:
  9673. return get_errno(ftruncate(arg1, arg2));
  9674. #endif
  9675. case TARGET_NR_fchmod:
  9676. return get_errno(fchmod(arg1, arg2));
  9677. #if defined(TARGET_NR_fchmodat)
  9678. case TARGET_NR_fchmodat:
  9679. if (!(p = lock_user_string(arg2)))
  9680. return -TARGET_EFAULT;
  9681. ret = get_errno(fchmodat(arg1, p, arg3, 0));
  9682. unlock_user(p, arg2, 0);
  9683. return ret;
  9684. #endif
  9685. case TARGET_NR_getpriority:
  9686. /* Note that negative values are valid for getpriority, so we must
  9687. differentiate based on errno settings. */
  9688. errno = 0;
  9689. ret = getpriority(arg1, arg2);
  9690. if (ret == -1 && errno != 0) {
  9691. return -host_to_target_errno(errno);
  9692. }
  9693. #ifdef TARGET_ALPHA
  9694. /* Return value is the unbiased priority. Signal no error. */
  9695. cpu_env->ir[IR_V0] = 0;
  9696. #else
  9697. /* Return value is a biased priority to avoid negative numbers. */
  9698. ret = 20 - ret;
  9699. #endif
  9700. return ret;
  9701. case TARGET_NR_setpriority:
  9702. return get_errno(setpriority(arg1, arg2, arg3));
  9703. #ifdef TARGET_NR_statfs
  9704. case TARGET_NR_statfs:
  9705. if (!(p = lock_user_string(arg1))) {
  9706. return -TARGET_EFAULT;
  9707. }
  9708. ret = get_errno(statfs(path(p), &stfs));
  9709. unlock_user(p, arg1, 0);
  9710. convert_statfs:
  9711. if (!is_error(ret)) {
  9712. struct target_statfs *target_stfs;
  9713. if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
  9714. return -TARGET_EFAULT;
  9715. __put_user(stfs.f_type, &target_stfs->f_type);
  9716. __put_user(stfs.f_bsize, &target_stfs->f_bsize);
  9717. __put_user(stfs.f_blocks, &target_stfs->f_blocks);
  9718. __put_user(stfs.f_bfree, &target_stfs->f_bfree);
  9719. __put_user(stfs.f_bavail, &target_stfs->f_bavail);
  9720. __put_user(stfs.f_files, &target_stfs->f_files);
  9721. __put_user(stfs.f_ffree, &target_stfs->f_ffree);
  9722. __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
  9723. __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
  9724. __put_user(stfs.f_namelen, &target_stfs->f_namelen);
  9725. __put_user(stfs.f_frsize, &target_stfs->f_frsize);
  9726. #ifdef _STATFS_F_FLAGS
  9727. __put_user(stfs.f_flags, &target_stfs->f_flags);
  9728. #else
  9729. __put_user(0, &target_stfs->f_flags);
  9730. #endif
  9731. memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
  9732. unlock_user_struct(target_stfs, arg2, 1);
  9733. }
  9734. return ret;
  9735. #endif
  9736. #ifdef TARGET_NR_fstatfs
  9737. case TARGET_NR_fstatfs:
  9738. ret = get_errno(fstatfs(arg1, &stfs));
  9739. goto convert_statfs;
  9740. #endif
  9741. #ifdef TARGET_NR_statfs64
  9742. case TARGET_NR_statfs64:
  9743. if (!(p = lock_user_string(arg1))) {
  9744. return -TARGET_EFAULT;
  9745. }
  9746. ret = get_errno(statfs(path(p), &stfs));
  9747. unlock_user(p, arg1, 0);
  9748. convert_statfs64:
  9749. if (!is_error(ret)) {
  9750. struct target_statfs64 *target_stfs;
  9751. if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
  9752. return -TARGET_EFAULT;
  9753. __put_user(stfs.f_type, &target_stfs->f_type);
  9754. __put_user(stfs.f_bsize, &target_stfs->f_bsize);
  9755. __put_user(stfs.f_blocks, &target_stfs->f_blocks);
  9756. __put_user(stfs.f_bfree, &target_stfs->f_bfree);
  9757. __put_user(stfs.f_bavail, &target_stfs->f_bavail);
  9758. __put_user(stfs.f_files, &target_stfs->f_files);
  9759. __put_user(stfs.f_ffree, &target_stfs->f_ffree);
  9760. __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
  9761. __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
  9762. __put_user(stfs.f_namelen, &target_stfs->f_namelen);
  9763. __put_user(stfs.f_frsize, &target_stfs->f_frsize);
  9764. #ifdef _STATFS_F_FLAGS
  9765. __put_user(stfs.f_flags, &target_stfs->f_flags);
  9766. #else
  9767. __put_user(0, &target_stfs->f_flags);
  9768. #endif
  9769. memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
  9770. unlock_user_struct(target_stfs, arg3, 1);
  9771. }
  9772. return ret;
  9773. case TARGET_NR_fstatfs64:
  9774. ret = get_errno(fstatfs(arg1, &stfs));
  9775. goto convert_statfs64;
  9776. #endif
  9777. #ifdef TARGET_NR_socketcall
  9778. case TARGET_NR_socketcall:
  9779. return do_socketcall(arg1, arg2);
  9780. #endif
  9781. #ifdef TARGET_NR_accept
  9782. case TARGET_NR_accept:
  9783. return do_accept4(arg1, arg2, arg3, 0);
  9784. #endif
  9785. #ifdef TARGET_NR_accept4
  9786. case TARGET_NR_accept4:
  9787. return do_accept4(arg1, arg2, arg3, arg4);
  9788. #endif
  9789. #ifdef TARGET_NR_bind
  9790. case TARGET_NR_bind:
  9791. return do_bind(arg1, arg2, arg3);
  9792. #endif
  9793. #ifdef TARGET_NR_connect
  9794. case TARGET_NR_connect:
  9795. return do_connect(arg1, arg2, arg3);
  9796. #endif
  9797. #ifdef TARGET_NR_getpeername
  9798. case TARGET_NR_getpeername:
  9799. return do_getpeername(arg1, arg2, arg3);
  9800. #endif
  9801. #ifdef TARGET_NR_getsockname
  9802. case TARGET_NR_getsockname:
  9803. return do_getsockname(arg1, arg2, arg3);
  9804. #endif
  9805. #ifdef TARGET_NR_getsockopt
  9806. case TARGET_NR_getsockopt:
  9807. return do_getsockopt(arg1, arg2, arg3, arg4, arg5);
  9808. #endif
  9809. #ifdef TARGET_NR_listen
  9810. case TARGET_NR_listen:
  9811. return get_errno(listen(arg1, arg2));
  9812. #endif
  9813. #ifdef TARGET_NR_recv
  9814. case TARGET_NR_recv:
  9815. return do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
  9816. #endif
  9817. #ifdef TARGET_NR_recvfrom
  9818. case TARGET_NR_recvfrom:
  9819. return do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
  9820. #endif
  9821. #ifdef TARGET_NR_recvmsg
  9822. case TARGET_NR_recvmsg:
  9823. return do_sendrecvmsg(arg1, arg2, arg3, 0);
  9824. #endif
  9825. #ifdef TARGET_NR_send
  9826. case TARGET_NR_send:
  9827. return do_sendto(arg1, arg2, arg3, arg4, 0, 0);
  9828. #endif
  9829. #ifdef TARGET_NR_sendmsg
  9830. case TARGET_NR_sendmsg:
  9831. return do_sendrecvmsg(arg1, arg2, arg3, 1);
  9832. #endif
  9833. #ifdef TARGET_NR_sendmmsg
  9834. case TARGET_NR_sendmmsg:
  9835. return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 1);
  9836. #endif
  9837. #ifdef TARGET_NR_recvmmsg
  9838. case TARGET_NR_recvmmsg:
  9839. return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 0);
  9840. #endif
  9841. #ifdef TARGET_NR_sendto
  9842. case TARGET_NR_sendto:
  9843. return do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
  9844. #endif
  9845. #ifdef TARGET_NR_shutdown
  9846. case TARGET_NR_shutdown:
  9847. return get_errno(shutdown(arg1, arg2));
  9848. #endif
  9849. #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
  9850. case TARGET_NR_getrandom:
  9851. p = lock_user(VERIFY_WRITE, arg1, arg2, 0);
  9852. if (!p) {
  9853. return -TARGET_EFAULT;
  9854. }
  9855. ret = get_errno(getrandom(p, arg2, arg3));
  9856. unlock_user(p, arg1, ret);
  9857. return ret;
  9858. #endif
  9859. #ifdef TARGET_NR_socket
  9860. case TARGET_NR_socket:
  9861. return do_socket(arg1, arg2, arg3);
  9862. #endif
  9863. #ifdef TARGET_NR_socketpair
  9864. case TARGET_NR_socketpair:
  9865. return do_socketpair(arg1, arg2, arg3, arg4);
  9866. #endif
  9867. #ifdef TARGET_NR_setsockopt
  9868. case TARGET_NR_setsockopt:
  9869. return do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
  9870. #endif
  9871. #if defined(TARGET_NR_syslog)
  9872. case TARGET_NR_syslog:
  9873. {
  9874. int len = arg2;
  9875. switch (arg1) {
  9876. case TARGET_SYSLOG_ACTION_CLOSE: /* Close log */
  9877. case TARGET_SYSLOG_ACTION_OPEN: /* Open log */
  9878. case TARGET_SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
  9879. case TARGET_SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging */
  9880. case TARGET_SYSLOG_ACTION_CONSOLE_ON: /* Enable logging */
  9881. case TARGET_SYSLOG_ACTION_CONSOLE_LEVEL: /* Set messages level */
  9882. case TARGET_SYSLOG_ACTION_SIZE_UNREAD: /* Number of chars */
  9883. case TARGET_SYSLOG_ACTION_SIZE_BUFFER: /* Size of the buffer */
  9884. return get_errno(sys_syslog((int)arg1, NULL, (int)arg3));
  9885. case TARGET_SYSLOG_ACTION_READ: /* Read from log */
  9886. case TARGET_SYSLOG_ACTION_READ_CLEAR: /* Read/clear msgs */
  9887. case TARGET_SYSLOG_ACTION_READ_ALL: /* Read last messages */
  9888. {
  9889. if (len < 0) {
  9890. return -TARGET_EINVAL;
  9891. }
  9892. if (len == 0) {
  9893. return 0;
  9894. }
  9895. p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  9896. if (!p) {
  9897. return -TARGET_EFAULT;
  9898. }
  9899. ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
  9900. unlock_user(p, arg2, arg3);
  9901. }
  9902. return ret;
  9903. default:
  9904. return -TARGET_EINVAL;
  9905. }
  9906. }
  9907. break;
  9908. #endif
  9909. case TARGET_NR_setitimer:
  9910. {
  9911. struct itimerval value, ovalue, *pvalue;
  9912. if (arg2) {
  9913. pvalue = &value;
  9914. if (copy_from_user_timeval(&pvalue->it_interval, arg2)
  9915. || copy_from_user_timeval(&pvalue->it_value,
  9916. arg2 + sizeof(struct target_timeval)))
  9917. return -TARGET_EFAULT;
  9918. } else {
  9919. pvalue = NULL;
  9920. }
  9921. ret = get_errno(setitimer(arg1, pvalue, &ovalue));
  9922. if (!is_error(ret) && arg3) {
  9923. if (copy_to_user_timeval(arg3,
  9924. &ovalue.it_interval)
  9925. || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
  9926. &ovalue.it_value))
  9927. return -TARGET_EFAULT;
  9928. }
  9929. }
  9930. return ret;
  9931. case TARGET_NR_getitimer:
  9932. {
  9933. struct itimerval value;
  9934. ret = get_errno(getitimer(arg1, &value));
  9935. if (!is_error(ret) && arg2) {
  9936. if (copy_to_user_timeval(arg2,
  9937. &value.it_interval)
  9938. || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
  9939. &value.it_value))
  9940. return -TARGET_EFAULT;
  9941. }
  9942. }
  9943. return ret;
  9944. #ifdef TARGET_NR_stat
  9945. case TARGET_NR_stat:
  9946. if (!(p = lock_user_string(arg1))) {
  9947. return -TARGET_EFAULT;
  9948. }
  9949. ret = get_errno(stat(path(p), &st));
  9950. unlock_user(p, arg1, 0);
  9951. goto do_stat;
  9952. #endif
  9953. #ifdef TARGET_NR_lstat
  9954. case TARGET_NR_lstat:
  9955. if (!(p = lock_user_string(arg1))) {
  9956. return -TARGET_EFAULT;
  9957. }
  9958. ret = get_errno(lstat(path(p), &st));
  9959. unlock_user(p, arg1, 0);
  9960. goto do_stat;
  9961. #endif
  9962. #ifdef TARGET_NR_fstat
  9963. case TARGET_NR_fstat:
  9964. {
  9965. ret = get_errno(fstat(arg1, &st));
  9966. #if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat)
  9967. do_stat:
  9968. #endif
  9969. if (!is_error(ret)) {
  9970. struct target_stat *target_st;
  9971. if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
  9972. return -TARGET_EFAULT;
  9973. memset(target_st, 0, sizeof(*target_st));
  9974. __put_user(st.st_dev, &target_st->st_dev);
  9975. __put_user(st.st_ino, &target_st->st_ino);
  9976. __put_user(st.st_mode, &target_st->st_mode);
  9977. __put_user(st.st_uid, &target_st->st_uid);
  9978. __put_user(st.st_gid, &target_st->st_gid);
  9979. __put_user(st.st_nlink, &target_st->st_nlink);
  9980. __put_user(st.st_rdev, &target_st->st_rdev);
  9981. __put_user(st.st_size, &target_st->st_size);
  9982. __put_user(st.st_blksize, &target_st->st_blksize);
  9983. __put_user(st.st_blocks, &target_st->st_blocks);
  9984. __put_user(st.st_atime, &target_st->target_st_atime);
  9985. __put_user(st.st_mtime, &target_st->target_st_mtime);
  9986. __put_user(st.st_ctime, &target_st->target_st_ctime);
  9987. #if defined(HAVE_STRUCT_STAT_ST_ATIM) && defined(TARGET_STAT_HAVE_NSEC)
  9988. __put_user(st.st_atim.tv_nsec,
  9989. &target_st->target_st_atime_nsec);
  9990. __put_user(st.st_mtim.tv_nsec,
  9991. &target_st->target_st_mtime_nsec);
  9992. __put_user(st.st_ctim.tv_nsec,
  9993. &target_st->target_st_ctime_nsec);
  9994. #endif
  9995. unlock_user_struct(target_st, arg2, 1);
  9996. }
  9997. }
  9998. return ret;
  9999. #endif
  10000. case TARGET_NR_vhangup:
  10001. return get_errno(vhangup());
  10002. #ifdef TARGET_NR_syscall
  10003. case TARGET_NR_syscall:
  10004. return do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
  10005. arg6, arg7, arg8, 0);
  10006. #endif
  10007. #if defined(TARGET_NR_wait4)
  10008. case TARGET_NR_wait4:
  10009. {
  10010. int status;
  10011. abi_long status_ptr = arg2;
  10012. struct rusage rusage, *rusage_ptr;
  10013. abi_ulong target_rusage = arg4;
  10014. abi_long rusage_err;
  10015. if (target_rusage)
  10016. rusage_ptr = &rusage;
  10017. else
  10018. rusage_ptr = NULL;
  10019. ret = get_errno(safe_wait4(arg1, &status, arg3, rusage_ptr));
  10020. if (!is_error(ret)) {
  10021. if (status_ptr && ret) {
  10022. status = host_to_target_waitstatus(status);
  10023. if (put_user_s32(status, status_ptr))
  10024. return -TARGET_EFAULT;
  10025. }
  10026. if (target_rusage) {
  10027. rusage_err = host_to_target_rusage(target_rusage, &rusage);
  10028. if (rusage_err) {
  10029. ret = rusage_err;
  10030. }
  10031. }
  10032. }
  10033. }
  10034. return ret;
  10035. #endif
  10036. #ifdef TARGET_NR_swapoff
  10037. case TARGET_NR_swapoff:
  10038. if (!(p = lock_user_string(arg1)))
  10039. return -TARGET_EFAULT;
  10040. ret = get_errno(swapoff(p));
  10041. unlock_user(p, arg1, 0);
  10042. return ret;
  10043. #endif
  10044. case TARGET_NR_sysinfo:
  10045. {
  10046. struct target_sysinfo *target_value;
  10047. struct sysinfo value;
  10048. ret = get_errno(sysinfo(&value));
  10049. if (!is_error(ret) && arg1)
  10050. {
  10051. if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
  10052. return -TARGET_EFAULT;
  10053. __put_user(value.uptime, &target_value->uptime);
  10054. __put_user(value.loads[0], &target_value->loads[0]);
  10055. __put_user(value.loads[1], &target_value->loads[1]);
  10056. __put_user(value.loads[2], &target_value->loads[2]);
  10057. __put_user(value.totalram, &target_value->totalram);
  10058. __put_user(value.freeram, &target_value->freeram);
  10059. __put_user(value.sharedram, &target_value->sharedram);
  10060. __put_user(value.bufferram, &target_value->bufferram);
  10061. __put_user(value.totalswap, &target_value->totalswap);
  10062. __put_user(value.freeswap, &target_value->freeswap);
  10063. __put_user(value.procs, &target_value->procs);
  10064. __put_user(value.totalhigh, &target_value->totalhigh);
  10065. __put_user(value.freehigh, &target_value->freehigh);
  10066. __put_user(value.mem_unit, &target_value->mem_unit);
  10067. unlock_user_struct(target_value, arg1, 1);
  10068. }
  10069. }
  10070. return ret;
  10071. #ifdef TARGET_NR_ipc
  10072. case TARGET_NR_ipc:
  10073. return do_ipc(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6);
  10074. #endif
  10075. #ifdef TARGET_NR_semget
  10076. case TARGET_NR_semget:
  10077. return get_errno(semget(arg1, arg2, arg3));
  10078. #endif
  10079. #ifdef TARGET_NR_semop
  10080. case TARGET_NR_semop:
  10081. return do_semtimedop(arg1, arg2, arg3, 0, false);
  10082. #endif
  10083. #ifdef TARGET_NR_semtimedop
  10084. case TARGET_NR_semtimedop:
  10085. return do_semtimedop(arg1, arg2, arg3, arg4, false);
  10086. #endif
  10087. #ifdef TARGET_NR_semtimedop_time64
  10088. case TARGET_NR_semtimedop_time64:
  10089. return do_semtimedop(arg1, arg2, arg3, arg4, true);
  10090. #endif
  10091. #ifdef TARGET_NR_semctl
  10092. case TARGET_NR_semctl:
  10093. return do_semctl(arg1, arg2, arg3, arg4);
  10094. #endif
  10095. #ifdef TARGET_NR_msgctl
  10096. case TARGET_NR_msgctl:
  10097. return do_msgctl(arg1, arg2, arg3);
  10098. #endif
  10099. #ifdef TARGET_NR_msgget
  10100. case TARGET_NR_msgget:
  10101. return get_errno(msgget(arg1, arg2));
  10102. #endif
  10103. #ifdef TARGET_NR_msgrcv
  10104. case TARGET_NR_msgrcv:
  10105. return do_msgrcv(arg1, arg2, arg3, arg4, arg5);
  10106. #endif
  10107. #ifdef TARGET_NR_msgsnd
  10108. case TARGET_NR_msgsnd:
  10109. return do_msgsnd(arg1, arg2, arg3, arg4);
  10110. #endif
  10111. #ifdef TARGET_NR_shmget
  10112. case TARGET_NR_shmget:
  10113. return get_errno(shmget(arg1, arg2, arg3));
  10114. #endif
  10115. #ifdef TARGET_NR_shmctl
  10116. case TARGET_NR_shmctl:
  10117. return do_shmctl(arg1, arg2, arg3);
  10118. #endif
  10119. #ifdef TARGET_NR_shmat
  10120. case TARGET_NR_shmat:
  10121. return do_shmat(cpu_env, arg1, arg2, arg3);
  10122. #endif
  10123. #ifdef TARGET_NR_shmdt
  10124. case TARGET_NR_shmdt:
  10125. return do_shmdt(arg1);
  10126. #endif
  10127. case TARGET_NR_fsync:
  10128. return get_errno(fsync(arg1));
  10129. case TARGET_NR_clone:
  10130. /* Linux manages to have three different orderings for its
  10131. * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
  10132. * match the kernel's CONFIG_CLONE_* settings.
  10133. * Microblaze is further special in that it uses a sixth
  10134. * implicit argument to clone for the TLS pointer.
  10135. */
  10136. #if defined(TARGET_MICROBLAZE)
  10137. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5));
  10138. #elif defined(TARGET_CLONE_BACKWARDS)
  10139. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
  10140. #elif defined(TARGET_CLONE_BACKWARDS2)
  10141. ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
  10142. #else
  10143. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
  10144. #endif
  10145. return ret;
  10146. #ifdef __NR_exit_group
  10147. /* new thread calls */
  10148. case TARGET_NR_exit_group:
  10149. preexit_cleanup(cpu_env, arg1);
  10150. return get_errno(exit_group(arg1));
  10151. #endif
  10152. case TARGET_NR_setdomainname:
  10153. if (!(p = lock_user_string(arg1)))
  10154. return -TARGET_EFAULT;
  10155. ret = get_errno(setdomainname(p, arg2));
  10156. unlock_user(p, arg1, 0);
  10157. return ret;
  10158. case TARGET_NR_uname:
  10159. /* no need to transcode because we use the linux syscall */
  10160. {
  10161. struct new_utsname * buf;
  10162. if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
  10163. return -TARGET_EFAULT;
  10164. ret = get_errno(sys_uname(buf));
  10165. if (!is_error(ret)) {
  10166. /* Overwrite the native machine name with whatever is being
  10167. emulated. */
  10168. g_strlcpy(buf->machine, cpu_to_uname_machine(cpu_env),
  10169. sizeof(buf->machine));
  10170. /* Allow the user to override the reported release. */
  10171. if (qemu_uname_release && *qemu_uname_release) {
  10172. g_strlcpy(buf->release, qemu_uname_release,
  10173. sizeof(buf->release));
  10174. }
  10175. }
  10176. unlock_user_struct(buf, arg1, 1);
  10177. }
  10178. return ret;
  10179. #ifdef TARGET_I386
  10180. case TARGET_NR_modify_ldt:
  10181. return do_modify_ldt(cpu_env, arg1, arg2, arg3);
  10182. #if !defined(TARGET_X86_64)
  10183. case TARGET_NR_vm86:
  10184. return do_vm86(cpu_env, arg1, arg2);
  10185. #endif
  10186. #endif
  10187. #if defined(TARGET_NR_adjtimex)
  10188. case TARGET_NR_adjtimex:
  10189. {
  10190. struct timex host_buf;
  10191. if (target_to_host_timex(&host_buf, arg1) != 0) {
  10192. return -TARGET_EFAULT;
  10193. }
  10194. ret = get_errno(adjtimex(&host_buf));
  10195. if (!is_error(ret)) {
  10196. if (host_to_target_timex(arg1, &host_buf) != 0) {
  10197. return -TARGET_EFAULT;
  10198. }
  10199. }
  10200. }
  10201. return ret;
  10202. #endif
  10203. #if defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME)
  10204. case TARGET_NR_clock_adjtime:
  10205. {
  10206. struct timex htx;
  10207. if (target_to_host_timex(&htx, arg2) != 0) {
  10208. return -TARGET_EFAULT;
  10209. }
  10210. ret = get_errno(clock_adjtime(arg1, &htx));
  10211. if (!is_error(ret) && host_to_target_timex(arg2, &htx)) {
  10212. return -TARGET_EFAULT;
  10213. }
  10214. }
  10215. return ret;
  10216. #endif
  10217. #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
  10218. case TARGET_NR_clock_adjtime64:
  10219. {
  10220. struct timex htx;
  10221. if (target_to_host_timex64(&htx, arg2) != 0) {
  10222. return -TARGET_EFAULT;
  10223. }
  10224. ret = get_errno(clock_adjtime(arg1, &htx));
  10225. if (!is_error(ret) && host_to_target_timex64(arg2, &htx)) {
  10226. return -TARGET_EFAULT;
  10227. }
  10228. }
  10229. return ret;
  10230. #endif
  10231. case TARGET_NR_getpgid:
  10232. return get_errno(getpgid(arg1));
  10233. case TARGET_NR_fchdir:
  10234. return get_errno(fchdir(arg1));
  10235. case TARGET_NR_personality:
  10236. return get_errno(personality(arg1));
  10237. #ifdef TARGET_NR__llseek /* Not on alpha */
  10238. case TARGET_NR__llseek:
  10239. {
  10240. int64_t res;
  10241. #if !defined(__NR_llseek)
  10242. res = lseek(arg1, ((uint64_t)arg2 << 32) | (abi_ulong)arg3, arg5);
  10243. if (res == -1) {
  10244. ret = get_errno(res);
  10245. } else {
  10246. ret = 0;
  10247. }
  10248. #else
  10249. ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
  10250. #endif
  10251. if ((ret == 0) && put_user_s64(res, arg4)) {
  10252. return -TARGET_EFAULT;
  10253. }
  10254. }
  10255. return ret;
  10256. #endif
  10257. #ifdef TARGET_NR_getdents
  10258. case TARGET_NR_getdents:
  10259. return do_getdents(arg1, arg2, arg3);
  10260. #endif /* TARGET_NR_getdents */
  10261. #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
  10262. case TARGET_NR_getdents64:
  10263. return do_getdents64(arg1, arg2, arg3);
  10264. #endif /* TARGET_NR_getdents64 */
  10265. #if defined(TARGET_NR__newselect)
  10266. case TARGET_NR__newselect:
  10267. return do_select(arg1, arg2, arg3, arg4, arg5);
  10268. #endif
  10269. #ifdef TARGET_NR_poll
  10270. case TARGET_NR_poll:
  10271. return do_ppoll(arg1, arg2, arg3, arg4, arg5, false, false);
  10272. #endif
  10273. #ifdef TARGET_NR_ppoll
  10274. case TARGET_NR_ppoll:
  10275. return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, false);
  10276. #endif
  10277. #ifdef TARGET_NR_ppoll_time64
  10278. case TARGET_NR_ppoll_time64:
  10279. return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, true);
  10280. #endif
  10281. case TARGET_NR_flock:
  10282. /* NOTE: the flock constant seems to be the same for every
  10283. Linux platform */
  10284. return get_errno(safe_flock(arg1, arg2));
  10285. case TARGET_NR_readv:
  10286. {
  10287. struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
  10288. if (vec != NULL) {
  10289. ret = get_errno(safe_readv(arg1, vec, arg3));
  10290. unlock_iovec(vec, arg2, arg3, 1);
  10291. } else {
  10292. ret = -host_to_target_errno(errno);
  10293. }
  10294. }
  10295. return ret;
  10296. case TARGET_NR_writev:
  10297. {
  10298. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  10299. if (vec != NULL) {
  10300. ret = get_errno(safe_writev(arg1, vec, arg3));
  10301. unlock_iovec(vec, arg2, arg3, 0);
  10302. } else {
  10303. ret = -host_to_target_errno(errno);
  10304. }
  10305. }
  10306. return ret;
  10307. #if defined(TARGET_NR_preadv)
  10308. case TARGET_NR_preadv:
  10309. {
  10310. struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
  10311. if (vec != NULL) {
  10312. unsigned long low, high;
  10313. target_to_host_low_high(arg4, arg5, &low, &high);
  10314. ret = get_errno(safe_preadv(arg1, vec, arg3, low, high));
  10315. unlock_iovec(vec, arg2, arg3, 1);
  10316. } else {
  10317. ret = -host_to_target_errno(errno);
  10318. }
  10319. }
  10320. return ret;
  10321. #endif
  10322. #if defined(TARGET_NR_pwritev)
  10323. case TARGET_NR_pwritev:
  10324. {
  10325. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  10326. if (vec != NULL) {
  10327. unsigned long low, high;
  10328. target_to_host_low_high(arg4, arg5, &low, &high);
  10329. ret = get_errno(safe_pwritev(arg1, vec, arg3, low, high));
  10330. unlock_iovec(vec, arg2, arg3, 0);
  10331. } else {
  10332. ret = -host_to_target_errno(errno);
  10333. }
  10334. }
  10335. return ret;
  10336. #endif
  10337. case TARGET_NR_getsid:
  10338. return get_errno(getsid(arg1));
  10339. #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
  10340. case TARGET_NR_fdatasync:
  10341. return get_errno(fdatasync(arg1));
  10342. #endif
  10343. case TARGET_NR_sched_getaffinity:
  10344. {
  10345. unsigned int mask_size;
  10346. unsigned long *mask;
  10347. /*
  10348. * sched_getaffinity needs multiples of ulong, so need to take
  10349. * care of mismatches between target ulong and host ulong sizes.
  10350. */
  10351. if (arg2 & (sizeof(abi_ulong) - 1)) {
  10352. return -TARGET_EINVAL;
  10353. }
  10354. mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
  10355. mask = alloca(mask_size);
  10356. memset(mask, 0, mask_size);
  10357. ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
  10358. if (!is_error(ret)) {
  10359. if (ret > arg2) {
  10360. /* More data returned than the caller's buffer will fit.
  10361. * This only happens if sizeof(abi_long) < sizeof(long)
  10362. * and the caller passed us a buffer holding an odd number
  10363. * of abi_longs. If the host kernel is actually using the
  10364. * extra 4 bytes then fail EINVAL; otherwise we can just
  10365. * ignore them and only copy the interesting part.
  10366. */
  10367. int numcpus = sysconf(_SC_NPROCESSORS_CONF);
  10368. if (numcpus > arg2 * 8) {
  10369. return -TARGET_EINVAL;
  10370. }
  10371. ret = arg2;
  10372. }
  10373. if (host_to_target_cpu_mask(mask, mask_size, arg3, ret)) {
  10374. return -TARGET_EFAULT;
  10375. }
  10376. }
  10377. }
  10378. return ret;
  10379. case TARGET_NR_sched_setaffinity:
  10380. {
  10381. unsigned int mask_size;
  10382. unsigned long *mask;
  10383. /*
  10384. * sched_setaffinity needs multiples of ulong, so need to take
  10385. * care of mismatches between target ulong and host ulong sizes.
  10386. */
  10387. if (arg2 & (sizeof(abi_ulong) - 1)) {
  10388. return -TARGET_EINVAL;
  10389. }
  10390. mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
  10391. mask = alloca(mask_size);
  10392. ret = target_to_host_cpu_mask(mask, mask_size, arg3, arg2);
  10393. if (ret) {
  10394. return ret;
  10395. }
  10396. return get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
  10397. }
  10398. case TARGET_NR_getcpu:
  10399. {
  10400. unsigned cpu, node;
  10401. ret = get_errno(sys_getcpu(arg1 ? &cpu : NULL,
  10402. arg2 ? &node : NULL,
  10403. NULL));
  10404. if (is_error(ret)) {
  10405. return ret;
  10406. }
  10407. if (arg1 && put_user_u32(cpu, arg1)) {
  10408. return -TARGET_EFAULT;
  10409. }
  10410. if (arg2 && put_user_u32(node, arg2)) {
  10411. return -TARGET_EFAULT;
  10412. }
  10413. }
  10414. return ret;
  10415. case TARGET_NR_sched_setparam:
  10416. {
  10417. struct target_sched_param *target_schp;
  10418. struct sched_param schp;
  10419. if (arg2 == 0) {
  10420. return -TARGET_EINVAL;
  10421. }
  10422. if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1)) {
  10423. return -TARGET_EFAULT;
  10424. }
  10425. schp.sched_priority = tswap32(target_schp->sched_priority);
  10426. unlock_user_struct(target_schp, arg2, 0);
  10427. return get_errno(sys_sched_setparam(arg1, &schp));
  10428. }
  10429. case TARGET_NR_sched_getparam:
  10430. {
  10431. struct target_sched_param *target_schp;
  10432. struct sched_param schp;
  10433. if (arg2 == 0) {
  10434. return -TARGET_EINVAL;
  10435. }
  10436. ret = get_errno(sys_sched_getparam(arg1, &schp));
  10437. if (!is_error(ret)) {
  10438. if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0)) {
  10439. return -TARGET_EFAULT;
  10440. }
  10441. target_schp->sched_priority = tswap32(schp.sched_priority);
  10442. unlock_user_struct(target_schp, arg2, 1);
  10443. }
  10444. }
  10445. return ret;
  10446. case TARGET_NR_sched_setscheduler:
  10447. {
  10448. struct target_sched_param *target_schp;
  10449. struct sched_param schp;
  10450. if (arg3 == 0) {
  10451. return -TARGET_EINVAL;
  10452. }
  10453. if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1)) {
  10454. return -TARGET_EFAULT;
  10455. }
  10456. schp.sched_priority = tswap32(target_schp->sched_priority);
  10457. unlock_user_struct(target_schp, arg3, 0);
  10458. return get_errno(sys_sched_setscheduler(arg1, arg2, &schp));
  10459. }
  10460. case TARGET_NR_sched_getscheduler:
  10461. return get_errno(sys_sched_getscheduler(arg1));
  10462. case TARGET_NR_sched_getattr:
  10463. {
  10464. struct target_sched_attr *target_scha;
  10465. struct sched_attr scha;
  10466. if (arg2 == 0) {
  10467. return -TARGET_EINVAL;
  10468. }
  10469. if (arg3 > sizeof(scha)) {
  10470. arg3 = sizeof(scha);
  10471. }
  10472. ret = get_errno(sys_sched_getattr(arg1, &scha, arg3, arg4));
  10473. if (!is_error(ret)) {
  10474. target_scha = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  10475. if (!target_scha) {
  10476. return -TARGET_EFAULT;
  10477. }
  10478. target_scha->size = tswap32(scha.size);
  10479. target_scha->sched_policy = tswap32(scha.sched_policy);
  10480. target_scha->sched_flags = tswap64(scha.sched_flags);
  10481. target_scha->sched_nice = tswap32(scha.sched_nice);
  10482. target_scha->sched_priority = tswap32(scha.sched_priority);
  10483. target_scha->sched_runtime = tswap64(scha.sched_runtime);
  10484. target_scha->sched_deadline = tswap64(scha.sched_deadline);
  10485. target_scha->sched_period = tswap64(scha.sched_period);
  10486. if (scha.size > offsetof(struct sched_attr, sched_util_min)) {
  10487. target_scha->sched_util_min = tswap32(scha.sched_util_min);
  10488. target_scha->sched_util_max = tswap32(scha.sched_util_max);
  10489. }
  10490. unlock_user(target_scha, arg2, arg3);
  10491. }
  10492. return ret;
  10493. }
  10494. case TARGET_NR_sched_setattr:
  10495. {
  10496. struct target_sched_attr *target_scha;
  10497. struct sched_attr scha;
  10498. uint32_t size;
  10499. int zeroed;
  10500. if (arg2 == 0) {
  10501. return -TARGET_EINVAL;
  10502. }
  10503. if (get_user_u32(size, arg2)) {
  10504. return -TARGET_EFAULT;
  10505. }
  10506. if (!size) {
  10507. size = offsetof(struct target_sched_attr, sched_util_min);
  10508. }
  10509. if (size < offsetof(struct target_sched_attr, sched_util_min)) {
  10510. if (put_user_u32(sizeof(struct target_sched_attr), arg2)) {
  10511. return -TARGET_EFAULT;
  10512. }
  10513. return -TARGET_E2BIG;
  10514. }
  10515. zeroed = check_zeroed_user(arg2, sizeof(struct target_sched_attr), size);
  10516. if (zeroed < 0) {
  10517. return zeroed;
  10518. } else if (zeroed == 0) {
  10519. if (put_user_u32(sizeof(struct target_sched_attr), arg2)) {
  10520. return -TARGET_EFAULT;
  10521. }
  10522. return -TARGET_E2BIG;
  10523. }
  10524. if (size > sizeof(struct target_sched_attr)) {
  10525. size = sizeof(struct target_sched_attr);
  10526. }
  10527. target_scha = lock_user(VERIFY_READ, arg2, size, 1);
  10528. if (!target_scha) {
  10529. return -TARGET_EFAULT;
  10530. }
  10531. scha.size = size;
  10532. scha.sched_policy = tswap32(target_scha->sched_policy);
  10533. scha.sched_flags = tswap64(target_scha->sched_flags);
  10534. scha.sched_nice = tswap32(target_scha->sched_nice);
  10535. scha.sched_priority = tswap32(target_scha->sched_priority);
  10536. scha.sched_runtime = tswap64(target_scha->sched_runtime);
  10537. scha.sched_deadline = tswap64(target_scha->sched_deadline);
  10538. scha.sched_period = tswap64(target_scha->sched_period);
  10539. if (size > offsetof(struct target_sched_attr, sched_util_min)) {
  10540. scha.sched_util_min = tswap32(target_scha->sched_util_min);
  10541. scha.sched_util_max = tswap32(target_scha->sched_util_max);
  10542. }
  10543. unlock_user(target_scha, arg2, 0);
  10544. return get_errno(sys_sched_setattr(arg1, &scha, arg3));
  10545. }
  10546. case TARGET_NR_sched_yield:
  10547. return get_errno(sched_yield());
  10548. case TARGET_NR_sched_get_priority_max:
  10549. return get_errno(sched_get_priority_max(arg1));
  10550. case TARGET_NR_sched_get_priority_min:
  10551. return get_errno(sched_get_priority_min(arg1));
  10552. #ifdef TARGET_NR_sched_rr_get_interval
  10553. case TARGET_NR_sched_rr_get_interval:
  10554. {
  10555. struct timespec ts;
  10556. ret = get_errno(sched_rr_get_interval(arg1, &ts));
  10557. if (!is_error(ret)) {
  10558. ret = host_to_target_timespec(arg2, &ts);
  10559. }
  10560. }
  10561. return ret;
  10562. #endif
  10563. #ifdef TARGET_NR_sched_rr_get_interval_time64
  10564. case TARGET_NR_sched_rr_get_interval_time64:
  10565. {
  10566. struct timespec ts;
  10567. ret = get_errno(sched_rr_get_interval(arg1, &ts));
  10568. if (!is_error(ret)) {
  10569. ret = host_to_target_timespec64(arg2, &ts);
  10570. }
  10571. }
  10572. return ret;
  10573. #endif
  10574. #if defined(TARGET_NR_nanosleep)
  10575. case TARGET_NR_nanosleep:
  10576. {
  10577. struct timespec req, rem;
  10578. target_to_host_timespec(&req, arg1);
  10579. ret = get_errno(safe_nanosleep(&req, &rem));
  10580. if (is_error(ret) && arg2) {
  10581. host_to_target_timespec(arg2, &rem);
  10582. }
  10583. }
  10584. return ret;
  10585. #endif
  10586. case TARGET_NR_prctl:
  10587. return do_prctl(cpu_env, arg1, arg2, arg3, arg4, arg5);
  10588. break;
  10589. #ifdef TARGET_NR_arch_prctl
  10590. case TARGET_NR_arch_prctl:
  10591. return do_arch_prctl(cpu_env, arg1, arg2);
  10592. #endif
  10593. #ifdef TARGET_NR_pread64
  10594. case TARGET_NR_pread64:
  10595. if (regpairs_aligned(cpu_env, num)) {
  10596. arg4 = arg5;
  10597. arg5 = arg6;
  10598. }
  10599. if (arg2 == 0 && arg3 == 0) {
  10600. /* Special-case NULL buffer and zero length, which should succeed */
  10601. p = 0;
  10602. } else {
  10603. p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  10604. if (!p) {
  10605. return -TARGET_EFAULT;
  10606. }
  10607. }
  10608. ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
  10609. unlock_user(p, arg2, ret);
  10610. return ret;
  10611. case TARGET_NR_pwrite64:
  10612. if (regpairs_aligned(cpu_env, num)) {
  10613. arg4 = arg5;
  10614. arg5 = arg6;
  10615. }
  10616. if (arg2 == 0 && arg3 == 0) {
  10617. /* Special-case NULL buffer and zero length, which should succeed */
  10618. p = 0;
  10619. } else {
  10620. p = lock_user(VERIFY_READ, arg2, arg3, 1);
  10621. if (!p) {
  10622. return -TARGET_EFAULT;
  10623. }
  10624. }
  10625. ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
  10626. unlock_user(p, arg2, 0);
  10627. return ret;
  10628. #endif
  10629. case TARGET_NR_getcwd:
  10630. if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
  10631. return -TARGET_EFAULT;
  10632. ret = get_errno(sys_getcwd1(p, arg2));
  10633. unlock_user(p, arg1, ret);
  10634. return ret;
  10635. case TARGET_NR_capget:
  10636. case TARGET_NR_capset:
  10637. {
  10638. struct target_user_cap_header *target_header;
  10639. struct target_user_cap_data *target_data = NULL;
  10640. struct __user_cap_header_struct header;
  10641. struct __user_cap_data_struct data[2];
  10642. struct __user_cap_data_struct *dataptr = NULL;
  10643. int i, target_datalen;
  10644. int data_items = 1;
  10645. if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) {
  10646. return -TARGET_EFAULT;
  10647. }
  10648. header.version = tswap32(target_header->version);
  10649. header.pid = tswap32(target_header->pid);
  10650. if (header.version != _LINUX_CAPABILITY_VERSION) {
  10651. /* Version 2 and up takes pointer to two user_data structs */
  10652. data_items = 2;
  10653. }
  10654. target_datalen = sizeof(*target_data) * data_items;
  10655. if (arg2) {
  10656. if (num == TARGET_NR_capget) {
  10657. target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0);
  10658. } else {
  10659. target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1);
  10660. }
  10661. if (!target_data) {
  10662. unlock_user_struct(target_header, arg1, 0);
  10663. return -TARGET_EFAULT;
  10664. }
  10665. if (num == TARGET_NR_capset) {
  10666. for (i = 0; i < data_items; i++) {
  10667. data[i].effective = tswap32(target_data[i].effective);
  10668. data[i].permitted = tswap32(target_data[i].permitted);
  10669. data[i].inheritable = tswap32(target_data[i].inheritable);
  10670. }
  10671. }
  10672. dataptr = data;
  10673. }
  10674. if (num == TARGET_NR_capget) {
  10675. ret = get_errno(capget(&header, dataptr));
  10676. } else {
  10677. ret = get_errno(capset(&header, dataptr));
  10678. }
  10679. /* The kernel always updates version for both capget and capset */
  10680. target_header->version = tswap32(header.version);
  10681. unlock_user_struct(target_header, arg1, 1);
  10682. if (arg2) {
  10683. if (num == TARGET_NR_capget) {
  10684. for (i = 0; i < data_items; i++) {
  10685. target_data[i].effective = tswap32(data[i].effective);
  10686. target_data[i].permitted = tswap32(data[i].permitted);
  10687. target_data[i].inheritable = tswap32(data[i].inheritable);
  10688. }
  10689. unlock_user(target_data, arg2, target_datalen);
  10690. } else {
  10691. unlock_user(target_data, arg2, 0);
  10692. }
  10693. }
  10694. return ret;
  10695. }
  10696. case TARGET_NR_sigaltstack:
  10697. return do_sigaltstack(arg1, arg2, cpu_env);
  10698. #ifdef CONFIG_SENDFILE
  10699. #ifdef TARGET_NR_sendfile
  10700. case TARGET_NR_sendfile:
  10701. {
  10702. off_t *offp = NULL;
  10703. off_t off;
  10704. if (arg3) {
  10705. ret = get_user_sal(off, arg3);
  10706. if (is_error(ret)) {
  10707. return ret;
  10708. }
  10709. offp = &off;
  10710. }
  10711. ret = get_errno(sendfile(arg1, arg2, offp, arg4));
  10712. if (!is_error(ret) && arg3) {
  10713. abi_long ret2 = put_user_sal(off, arg3);
  10714. if (is_error(ret2)) {
  10715. ret = ret2;
  10716. }
  10717. }
  10718. return ret;
  10719. }
  10720. #endif
  10721. #ifdef TARGET_NR_sendfile64
  10722. case TARGET_NR_sendfile64:
  10723. {
  10724. off_t *offp = NULL;
  10725. off_t off;
  10726. if (arg3) {
  10727. ret = get_user_s64(off, arg3);
  10728. if (is_error(ret)) {
  10729. return ret;
  10730. }
  10731. offp = &off;
  10732. }
  10733. ret = get_errno(sendfile(arg1, arg2, offp, arg4));
  10734. if (!is_error(ret) && arg3) {
  10735. abi_long ret2 = put_user_s64(off, arg3);
  10736. if (is_error(ret2)) {
  10737. ret = ret2;
  10738. }
  10739. }
  10740. return ret;
  10741. }
  10742. #endif
  10743. #endif
  10744. #ifdef TARGET_NR_vfork
  10745. case TARGET_NR_vfork:
  10746. return get_errno(do_fork(cpu_env,
  10747. CLONE_VFORK | CLONE_VM | TARGET_SIGCHLD,
  10748. 0, 0, 0, 0));
  10749. #endif
  10750. #ifdef TARGET_NR_ugetrlimit
  10751. case TARGET_NR_ugetrlimit:
  10752. {
  10753. struct rlimit rlim;
  10754. int resource = target_to_host_resource(arg1);
  10755. ret = get_errno(getrlimit(resource, &rlim));
  10756. if (!is_error(ret)) {
  10757. struct target_rlimit *target_rlim;
  10758. if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
  10759. return -TARGET_EFAULT;
  10760. target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
  10761. target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
  10762. unlock_user_struct(target_rlim, arg2, 1);
  10763. }
  10764. return ret;
  10765. }
  10766. #endif
  10767. #ifdef TARGET_NR_truncate64
  10768. case TARGET_NR_truncate64:
  10769. if (!(p = lock_user_string(arg1)))
  10770. return -TARGET_EFAULT;
  10771. ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
  10772. unlock_user(p, arg1, 0);
  10773. return ret;
  10774. #endif
  10775. #ifdef TARGET_NR_ftruncate64
  10776. case TARGET_NR_ftruncate64:
  10777. return target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
  10778. #endif
  10779. #ifdef TARGET_NR_stat64
  10780. case TARGET_NR_stat64:
  10781. if (!(p = lock_user_string(arg1))) {
  10782. return -TARGET_EFAULT;
  10783. }
  10784. ret = get_errno(stat(path(p), &st));
  10785. unlock_user(p, arg1, 0);
  10786. if (!is_error(ret))
  10787. ret = host_to_target_stat64(cpu_env, arg2, &st);
  10788. return ret;
  10789. #endif
  10790. #ifdef TARGET_NR_lstat64
  10791. case TARGET_NR_lstat64:
  10792. if (!(p = lock_user_string(arg1))) {
  10793. return -TARGET_EFAULT;
  10794. }
  10795. ret = get_errno(lstat(path(p), &st));
  10796. unlock_user(p, arg1, 0);
  10797. if (!is_error(ret))
  10798. ret = host_to_target_stat64(cpu_env, arg2, &st);
  10799. return ret;
  10800. #endif
  10801. #ifdef TARGET_NR_fstat64
  10802. case TARGET_NR_fstat64:
  10803. ret = get_errno(fstat(arg1, &st));
  10804. if (!is_error(ret))
  10805. ret = host_to_target_stat64(cpu_env, arg2, &st);
  10806. return ret;
  10807. #endif
  10808. #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
  10809. #ifdef TARGET_NR_fstatat64
  10810. case TARGET_NR_fstatat64:
  10811. #endif
  10812. #ifdef TARGET_NR_newfstatat
  10813. case TARGET_NR_newfstatat:
  10814. #endif
  10815. if (!(p = lock_user_string(arg2))) {
  10816. return -TARGET_EFAULT;
  10817. }
  10818. ret = get_errno(fstatat(arg1, path(p), &st, arg4));
  10819. unlock_user(p, arg2, 0);
  10820. if (!is_error(ret))
  10821. ret = host_to_target_stat64(cpu_env, arg3, &st);
  10822. return ret;
  10823. #endif
  10824. #if defined(TARGET_NR_statx)
  10825. case TARGET_NR_statx:
  10826. {
  10827. struct target_statx *target_stx;
  10828. int dirfd = arg1;
  10829. int flags = arg3;
  10830. p = lock_user_string(arg2);
  10831. if (p == NULL) {
  10832. return -TARGET_EFAULT;
  10833. }
  10834. #if defined(__NR_statx)
  10835. {
  10836. /*
  10837. * It is assumed that struct statx is architecture independent.
  10838. */
  10839. struct target_statx host_stx;
  10840. int mask = arg4;
  10841. ret = get_errno(sys_statx(dirfd, p, flags, mask, &host_stx));
  10842. if (!is_error(ret)) {
  10843. if (host_to_target_statx(&host_stx, arg5) != 0) {
  10844. unlock_user(p, arg2, 0);
  10845. return -TARGET_EFAULT;
  10846. }
  10847. }
  10848. if (ret != -TARGET_ENOSYS) {
  10849. unlock_user(p, arg2, 0);
  10850. return ret;
  10851. }
  10852. }
  10853. #endif
  10854. ret = get_errno(fstatat(dirfd, path(p), &st, flags));
  10855. unlock_user(p, arg2, 0);
  10856. if (!is_error(ret)) {
  10857. if (!lock_user_struct(VERIFY_WRITE, target_stx, arg5, 0)) {
  10858. return -TARGET_EFAULT;
  10859. }
  10860. memset(target_stx, 0, sizeof(*target_stx));
  10861. __put_user(major(st.st_dev), &target_stx->stx_dev_major);
  10862. __put_user(minor(st.st_dev), &target_stx->stx_dev_minor);
  10863. __put_user(st.st_ino, &target_stx->stx_ino);
  10864. __put_user(st.st_mode, &target_stx->stx_mode);
  10865. __put_user(st.st_uid, &target_stx->stx_uid);
  10866. __put_user(st.st_gid, &target_stx->stx_gid);
  10867. __put_user(st.st_nlink, &target_stx->stx_nlink);
  10868. __put_user(major(st.st_rdev), &target_stx->stx_rdev_major);
  10869. __put_user(minor(st.st_rdev), &target_stx->stx_rdev_minor);
  10870. __put_user(st.st_size, &target_stx->stx_size);
  10871. __put_user(st.st_blksize, &target_stx->stx_blksize);
  10872. __put_user(st.st_blocks, &target_stx->stx_blocks);
  10873. __put_user(st.st_atime, &target_stx->stx_atime.tv_sec);
  10874. __put_user(st.st_mtime, &target_stx->stx_mtime.tv_sec);
  10875. __put_user(st.st_ctime, &target_stx->stx_ctime.tv_sec);
  10876. unlock_user_struct(target_stx, arg5, 1);
  10877. }
  10878. }
  10879. return ret;
  10880. #endif
  10881. #ifdef TARGET_NR_lchown
  10882. case TARGET_NR_lchown:
  10883. if (!(p = lock_user_string(arg1)))
  10884. return -TARGET_EFAULT;
  10885. ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
  10886. unlock_user(p, arg1, 0);
  10887. return ret;
  10888. #endif
  10889. #ifdef TARGET_NR_getuid
  10890. case TARGET_NR_getuid:
  10891. return get_errno(high2lowuid(getuid()));
  10892. #endif
  10893. #ifdef TARGET_NR_getgid
  10894. case TARGET_NR_getgid:
  10895. return get_errno(high2lowgid(getgid()));
  10896. #endif
  10897. #ifdef TARGET_NR_geteuid
  10898. case TARGET_NR_geteuid:
  10899. return get_errno(high2lowuid(geteuid()));
  10900. #endif
  10901. #ifdef TARGET_NR_getegid
  10902. case TARGET_NR_getegid:
  10903. return get_errno(high2lowgid(getegid()));
  10904. #endif
  10905. case TARGET_NR_setreuid:
  10906. return get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
  10907. case TARGET_NR_setregid:
  10908. return get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
  10909. case TARGET_NR_getgroups:
  10910. { /* the same code as for TARGET_NR_getgroups32 */
  10911. int gidsetsize = arg1;
  10912. target_id *target_grouplist;
  10913. g_autofree gid_t *grouplist = NULL;
  10914. int i;
  10915. if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) {
  10916. return -TARGET_EINVAL;
  10917. }
  10918. if (gidsetsize > 0) {
  10919. grouplist = g_try_new(gid_t, gidsetsize);
  10920. if (!grouplist) {
  10921. return -TARGET_ENOMEM;
  10922. }
  10923. }
  10924. ret = get_errno(getgroups(gidsetsize, grouplist));
  10925. if (!is_error(ret) && gidsetsize > 0) {
  10926. target_grouplist = lock_user(VERIFY_WRITE, arg2,
  10927. gidsetsize * sizeof(target_id), 0);
  10928. if (!target_grouplist) {
  10929. return -TARGET_EFAULT;
  10930. }
  10931. for (i = 0; i < ret; i++) {
  10932. target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
  10933. }
  10934. unlock_user(target_grouplist, arg2,
  10935. gidsetsize * sizeof(target_id));
  10936. }
  10937. return ret;
  10938. }
  10939. case TARGET_NR_setgroups:
  10940. { /* the same code as for TARGET_NR_setgroups32 */
  10941. int gidsetsize = arg1;
  10942. target_id *target_grouplist;
  10943. g_autofree gid_t *grouplist = NULL;
  10944. int i;
  10945. if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) {
  10946. return -TARGET_EINVAL;
  10947. }
  10948. if (gidsetsize > 0) {
  10949. grouplist = g_try_new(gid_t, gidsetsize);
  10950. if (!grouplist) {
  10951. return -TARGET_ENOMEM;
  10952. }
  10953. target_grouplist = lock_user(VERIFY_READ, arg2,
  10954. gidsetsize * sizeof(target_id), 1);
  10955. if (!target_grouplist) {
  10956. return -TARGET_EFAULT;
  10957. }
  10958. for (i = 0; i < gidsetsize; i++) {
  10959. grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
  10960. }
  10961. unlock_user(target_grouplist, arg2,
  10962. gidsetsize * sizeof(target_id));
  10963. }
  10964. return get_errno(setgroups(gidsetsize, grouplist));
  10965. }
  10966. case TARGET_NR_fchown:
  10967. return get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
  10968. #if defined(TARGET_NR_fchownat)
  10969. case TARGET_NR_fchownat:
  10970. if (!(p = lock_user_string(arg2)))
  10971. return -TARGET_EFAULT;
  10972. ret = get_errno(fchownat(arg1, p, low2highuid(arg3),
  10973. low2highgid(arg4), arg5));
  10974. unlock_user(p, arg2, 0);
  10975. return ret;
  10976. #endif
  10977. #ifdef TARGET_NR_setresuid
  10978. case TARGET_NR_setresuid:
  10979. return get_errno(sys_setresuid(low2highuid(arg1),
  10980. low2highuid(arg2),
  10981. low2highuid(arg3)));
  10982. #endif
  10983. #ifdef TARGET_NR_getresuid
  10984. case TARGET_NR_getresuid:
  10985. {
  10986. uid_t ruid, euid, suid;
  10987. ret = get_errno(getresuid(&ruid, &euid, &suid));
  10988. if (!is_error(ret)) {
  10989. if (put_user_id(high2lowuid(ruid), arg1)
  10990. || put_user_id(high2lowuid(euid), arg2)
  10991. || put_user_id(high2lowuid(suid), arg3))
  10992. return -TARGET_EFAULT;
  10993. }
  10994. }
  10995. return ret;
  10996. #endif
  10997. #ifdef TARGET_NR_getresgid
  10998. case TARGET_NR_setresgid:
  10999. return get_errno(sys_setresgid(low2highgid(arg1),
  11000. low2highgid(arg2),
  11001. low2highgid(arg3)));
  11002. #endif
  11003. #ifdef TARGET_NR_getresgid
  11004. case TARGET_NR_getresgid:
  11005. {
  11006. gid_t rgid, egid, sgid;
  11007. ret = get_errno(getresgid(&rgid, &egid, &sgid));
  11008. if (!is_error(ret)) {
  11009. if (put_user_id(high2lowgid(rgid), arg1)
  11010. || put_user_id(high2lowgid(egid), arg2)
  11011. || put_user_id(high2lowgid(sgid), arg3))
  11012. return -TARGET_EFAULT;
  11013. }
  11014. }
  11015. return ret;
  11016. #endif
  11017. #ifdef TARGET_NR_chown
  11018. case TARGET_NR_chown:
  11019. if (!(p = lock_user_string(arg1)))
  11020. return -TARGET_EFAULT;
  11021. ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
  11022. unlock_user(p, arg1, 0);
  11023. return ret;
  11024. #endif
  11025. case TARGET_NR_setuid:
  11026. return get_errno(sys_setuid(low2highuid(arg1)));
  11027. case TARGET_NR_setgid:
  11028. return get_errno(sys_setgid(low2highgid(arg1)));
  11029. case TARGET_NR_setfsuid:
  11030. return get_errno(setfsuid(arg1));
  11031. case TARGET_NR_setfsgid:
  11032. return get_errno(setfsgid(arg1));
  11033. #ifdef TARGET_NR_lchown32
  11034. case TARGET_NR_lchown32:
  11035. if (!(p = lock_user_string(arg1)))
  11036. return -TARGET_EFAULT;
  11037. ret = get_errno(lchown(p, arg2, arg3));
  11038. unlock_user(p, arg1, 0);
  11039. return ret;
  11040. #endif
  11041. #ifdef TARGET_NR_getuid32
  11042. case TARGET_NR_getuid32:
  11043. return get_errno(getuid());
  11044. #endif
  11045. #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
  11046. /* Alpha specific */
  11047. case TARGET_NR_getxuid:
  11048. {
  11049. uid_t euid;
  11050. euid=geteuid();
  11051. cpu_env->ir[IR_A4]=euid;
  11052. }
  11053. return get_errno(getuid());
  11054. #endif
  11055. #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
  11056. /* Alpha specific */
  11057. case TARGET_NR_getxgid:
  11058. {
  11059. uid_t egid;
  11060. egid=getegid();
  11061. cpu_env->ir[IR_A4]=egid;
  11062. }
  11063. return get_errno(getgid());
  11064. #endif
  11065. #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
  11066. /* Alpha specific */
  11067. case TARGET_NR_osf_getsysinfo:
  11068. ret = -TARGET_EOPNOTSUPP;
  11069. switch (arg1) {
  11070. case TARGET_GSI_IEEE_FP_CONTROL:
  11071. {
  11072. uint64_t fpcr = cpu_alpha_load_fpcr(cpu_env);
  11073. uint64_t swcr = cpu_env->swcr;
  11074. swcr &= ~SWCR_STATUS_MASK;
  11075. swcr |= (fpcr >> 35) & SWCR_STATUS_MASK;
  11076. if (put_user_u64 (swcr, arg2))
  11077. return -TARGET_EFAULT;
  11078. ret = 0;
  11079. }
  11080. break;
  11081. /* case GSI_IEEE_STATE_AT_SIGNAL:
  11082. -- Not implemented in linux kernel.
  11083. case GSI_UACPROC:
  11084. -- Retrieves current unaligned access state; not much used.
  11085. case GSI_PROC_TYPE:
  11086. -- Retrieves implver information; surely not used.
  11087. case GSI_GET_HWRPB:
  11088. -- Grabs a copy of the HWRPB; surely not used.
  11089. */
  11090. }
  11091. return ret;
  11092. #endif
  11093. #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
  11094. /* Alpha specific */
  11095. case TARGET_NR_osf_setsysinfo:
  11096. ret = -TARGET_EOPNOTSUPP;
  11097. switch (arg1) {
  11098. case TARGET_SSI_IEEE_FP_CONTROL:
  11099. {
  11100. uint64_t swcr, fpcr;
  11101. if (get_user_u64 (swcr, arg2)) {
  11102. return -TARGET_EFAULT;
  11103. }
  11104. /*
  11105. * The kernel calls swcr_update_status to update the
  11106. * status bits from the fpcr at every point that it
  11107. * could be queried. Therefore, we store the status
  11108. * bits only in FPCR.
  11109. */
  11110. cpu_env->swcr = swcr & (SWCR_TRAP_ENABLE_MASK | SWCR_MAP_MASK);
  11111. fpcr = cpu_alpha_load_fpcr(cpu_env);
  11112. fpcr &= ((uint64_t)FPCR_DYN_MASK << 32);
  11113. fpcr |= alpha_ieee_swcr_to_fpcr(swcr);
  11114. cpu_alpha_store_fpcr(cpu_env, fpcr);
  11115. ret = 0;
  11116. }
  11117. break;
  11118. case TARGET_SSI_IEEE_RAISE_EXCEPTION:
  11119. {
  11120. uint64_t exc, fpcr, fex;
  11121. if (get_user_u64(exc, arg2)) {
  11122. return -TARGET_EFAULT;
  11123. }
  11124. exc &= SWCR_STATUS_MASK;
  11125. fpcr = cpu_alpha_load_fpcr(cpu_env);
  11126. /* Old exceptions are not signaled. */
  11127. fex = alpha_ieee_fpcr_to_swcr(fpcr);
  11128. fex = exc & ~fex;
  11129. fex >>= SWCR_STATUS_TO_EXCSUM_SHIFT;
  11130. fex &= (cpu_env)->swcr;
  11131. /* Update the hardware fpcr. */
  11132. fpcr |= alpha_ieee_swcr_to_fpcr(exc);
  11133. cpu_alpha_store_fpcr(cpu_env, fpcr);
  11134. if (fex) {
  11135. int si_code = TARGET_FPE_FLTUNK;
  11136. target_siginfo_t info;
  11137. if (fex & SWCR_TRAP_ENABLE_DNO) {
  11138. si_code = TARGET_FPE_FLTUND;
  11139. }
  11140. if (fex & SWCR_TRAP_ENABLE_INE) {
  11141. si_code = TARGET_FPE_FLTRES;
  11142. }
  11143. if (fex & SWCR_TRAP_ENABLE_UNF) {
  11144. si_code = TARGET_FPE_FLTUND;
  11145. }
  11146. if (fex & SWCR_TRAP_ENABLE_OVF) {
  11147. si_code = TARGET_FPE_FLTOVF;
  11148. }
  11149. if (fex & SWCR_TRAP_ENABLE_DZE) {
  11150. si_code = TARGET_FPE_FLTDIV;
  11151. }
  11152. if (fex & SWCR_TRAP_ENABLE_INV) {
  11153. si_code = TARGET_FPE_FLTINV;
  11154. }
  11155. info.si_signo = SIGFPE;
  11156. info.si_errno = 0;
  11157. info.si_code = si_code;
  11158. info._sifields._sigfault._addr = (cpu_env)->pc;
  11159. queue_signal(cpu_env, info.si_signo,
  11160. QEMU_SI_FAULT, &info);
  11161. }
  11162. ret = 0;
  11163. }
  11164. break;
  11165. /* case SSI_NVPAIRS:
  11166. -- Used with SSIN_UACPROC to enable unaligned accesses.
  11167. case SSI_IEEE_STATE_AT_SIGNAL:
  11168. case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
  11169. -- Not implemented in linux kernel
  11170. */
  11171. }
  11172. return ret;
  11173. #endif
  11174. #ifdef TARGET_NR_osf_sigprocmask
  11175. /* Alpha specific. */
  11176. case TARGET_NR_osf_sigprocmask:
  11177. {
  11178. abi_ulong mask;
  11179. int how;
  11180. sigset_t set, oldset;
  11181. switch(arg1) {
  11182. case TARGET_SIG_BLOCK:
  11183. how = SIG_BLOCK;
  11184. break;
  11185. case TARGET_SIG_UNBLOCK:
  11186. how = SIG_UNBLOCK;
  11187. break;
  11188. case TARGET_SIG_SETMASK:
  11189. how = SIG_SETMASK;
  11190. break;
  11191. default:
  11192. return -TARGET_EINVAL;
  11193. }
  11194. mask = arg2;
  11195. target_to_host_old_sigset(&set, &mask);
  11196. ret = do_sigprocmask(how, &set, &oldset);
  11197. if (!ret) {
  11198. host_to_target_old_sigset(&mask, &oldset);
  11199. ret = mask;
  11200. }
  11201. }
  11202. return ret;
  11203. #endif
  11204. #ifdef TARGET_NR_getgid32
  11205. case TARGET_NR_getgid32:
  11206. return get_errno(getgid());
  11207. #endif
  11208. #ifdef TARGET_NR_geteuid32
  11209. case TARGET_NR_geteuid32:
  11210. return get_errno(geteuid());
  11211. #endif
  11212. #ifdef TARGET_NR_getegid32
  11213. case TARGET_NR_getegid32:
  11214. return get_errno(getegid());
  11215. #endif
  11216. #ifdef TARGET_NR_setreuid32
  11217. case TARGET_NR_setreuid32:
  11218. return get_errno(setreuid(arg1, arg2));
  11219. #endif
  11220. #ifdef TARGET_NR_setregid32
  11221. case TARGET_NR_setregid32:
  11222. return get_errno(setregid(arg1, arg2));
  11223. #endif
  11224. #ifdef TARGET_NR_getgroups32
  11225. case TARGET_NR_getgroups32:
  11226. { /* the same code as for TARGET_NR_getgroups */
  11227. int gidsetsize = arg1;
  11228. uint32_t *target_grouplist;
  11229. g_autofree gid_t *grouplist = NULL;
  11230. int i;
  11231. if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) {
  11232. return -TARGET_EINVAL;
  11233. }
  11234. if (gidsetsize > 0) {
  11235. grouplist = g_try_new(gid_t, gidsetsize);
  11236. if (!grouplist) {
  11237. return -TARGET_ENOMEM;
  11238. }
  11239. }
  11240. ret = get_errno(getgroups(gidsetsize, grouplist));
  11241. if (!is_error(ret) && gidsetsize > 0) {
  11242. target_grouplist = lock_user(VERIFY_WRITE, arg2,
  11243. gidsetsize * 4, 0);
  11244. if (!target_grouplist) {
  11245. return -TARGET_EFAULT;
  11246. }
  11247. for (i = 0; i < ret; i++) {
  11248. target_grouplist[i] = tswap32(grouplist[i]);
  11249. }
  11250. unlock_user(target_grouplist, arg2, gidsetsize * 4);
  11251. }
  11252. return ret;
  11253. }
  11254. #endif
  11255. #ifdef TARGET_NR_setgroups32
  11256. case TARGET_NR_setgroups32:
  11257. { /* the same code as for TARGET_NR_setgroups */
  11258. int gidsetsize = arg1;
  11259. uint32_t *target_grouplist;
  11260. g_autofree gid_t *grouplist = NULL;
  11261. int i;
  11262. if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) {
  11263. return -TARGET_EINVAL;
  11264. }
  11265. if (gidsetsize > 0) {
  11266. grouplist = g_try_new(gid_t, gidsetsize);
  11267. if (!grouplist) {
  11268. return -TARGET_ENOMEM;
  11269. }
  11270. target_grouplist = lock_user(VERIFY_READ, arg2,
  11271. gidsetsize * 4, 1);
  11272. if (!target_grouplist) {
  11273. return -TARGET_EFAULT;
  11274. }
  11275. for (i = 0; i < gidsetsize; i++) {
  11276. grouplist[i] = tswap32(target_grouplist[i]);
  11277. }
  11278. unlock_user(target_grouplist, arg2, 0);
  11279. }
  11280. return get_errno(setgroups(gidsetsize, grouplist));
  11281. }
  11282. #endif
  11283. #ifdef TARGET_NR_fchown32
  11284. case TARGET_NR_fchown32:
  11285. return get_errno(fchown(arg1, arg2, arg3));
  11286. #endif
  11287. #ifdef TARGET_NR_setresuid32
  11288. case TARGET_NR_setresuid32:
  11289. return get_errno(sys_setresuid(arg1, arg2, arg3));
  11290. #endif
  11291. #ifdef TARGET_NR_getresuid32
  11292. case TARGET_NR_getresuid32:
  11293. {
  11294. uid_t ruid, euid, suid;
  11295. ret = get_errno(getresuid(&ruid, &euid, &suid));
  11296. if (!is_error(ret)) {
  11297. if (put_user_u32(ruid, arg1)
  11298. || put_user_u32(euid, arg2)
  11299. || put_user_u32(suid, arg3))
  11300. return -TARGET_EFAULT;
  11301. }
  11302. }
  11303. return ret;
  11304. #endif
  11305. #ifdef TARGET_NR_setresgid32
  11306. case TARGET_NR_setresgid32:
  11307. return get_errno(sys_setresgid(arg1, arg2, arg3));
  11308. #endif
  11309. #ifdef TARGET_NR_getresgid32
  11310. case TARGET_NR_getresgid32:
  11311. {
  11312. gid_t rgid, egid, sgid;
  11313. ret = get_errno(getresgid(&rgid, &egid, &sgid));
  11314. if (!is_error(ret)) {
  11315. if (put_user_u32(rgid, arg1)
  11316. || put_user_u32(egid, arg2)
  11317. || put_user_u32(sgid, arg3))
  11318. return -TARGET_EFAULT;
  11319. }
  11320. }
  11321. return ret;
  11322. #endif
  11323. #ifdef TARGET_NR_chown32
  11324. case TARGET_NR_chown32:
  11325. if (!(p = lock_user_string(arg1)))
  11326. return -TARGET_EFAULT;
  11327. ret = get_errno(chown(p, arg2, arg3));
  11328. unlock_user(p, arg1, 0);
  11329. return ret;
  11330. #endif
  11331. #ifdef TARGET_NR_setuid32
  11332. case TARGET_NR_setuid32:
  11333. return get_errno(sys_setuid(arg1));
  11334. #endif
  11335. #ifdef TARGET_NR_setgid32
  11336. case TARGET_NR_setgid32:
  11337. return get_errno(sys_setgid(arg1));
  11338. #endif
  11339. #ifdef TARGET_NR_setfsuid32
  11340. case TARGET_NR_setfsuid32:
  11341. return get_errno(setfsuid(arg1));
  11342. #endif
  11343. #ifdef TARGET_NR_setfsgid32
  11344. case TARGET_NR_setfsgid32:
  11345. return get_errno(setfsgid(arg1));
  11346. #endif
  11347. #ifdef TARGET_NR_mincore
  11348. case TARGET_NR_mincore:
  11349. {
  11350. void *a = lock_user(VERIFY_NONE, arg1, arg2, 0);
  11351. if (!a) {
  11352. return -TARGET_ENOMEM;
  11353. }
  11354. p = lock_user_string(arg3);
  11355. if (!p) {
  11356. ret = -TARGET_EFAULT;
  11357. } else {
  11358. ret = get_errno(mincore(a, arg2, p));
  11359. unlock_user(p, arg3, ret);
  11360. }
  11361. unlock_user(a, arg1, 0);
  11362. }
  11363. return ret;
  11364. #endif
  11365. #ifdef TARGET_NR_arm_fadvise64_64
  11366. case TARGET_NR_arm_fadvise64_64:
  11367. /* arm_fadvise64_64 looks like fadvise64_64 but
  11368. * with different argument order: fd, advice, offset, len
  11369. * rather than the usual fd, offset, len, advice.
  11370. * Note that offset and len are both 64-bit so appear as
  11371. * pairs of 32-bit registers.
  11372. */
  11373. ret = posix_fadvise(arg1, target_offset64(arg3, arg4),
  11374. target_offset64(arg5, arg6), arg2);
  11375. return -host_to_target_errno(ret);
  11376. #endif
  11377. #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
  11378. #ifdef TARGET_NR_fadvise64_64
  11379. case TARGET_NR_fadvise64_64:
  11380. #if defined(TARGET_PPC) || defined(TARGET_XTENSA)
  11381. /* 6 args: fd, advice, offset (high, low), len (high, low) */
  11382. ret = arg2;
  11383. arg2 = arg3;
  11384. arg3 = arg4;
  11385. arg4 = arg5;
  11386. arg5 = arg6;
  11387. arg6 = ret;
  11388. #else
  11389. /* 6 args: fd, offset (high, low), len (high, low), advice */
  11390. if (regpairs_aligned(cpu_env, num)) {
  11391. /* offset is in (3,4), len in (5,6) and advice in 7 */
  11392. arg2 = arg3;
  11393. arg3 = arg4;
  11394. arg4 = arg5;
  11395. arg5 = arg6;
  11396. arg6 = arg7;
  11397. }
  11398. #endif
  11399. ret = posix_fadvise(arg1, target_offset64(arg2, arg3),
  11400. target_offset64(arg4, arg5), arg6);
  11401. return -host_to_target_errno(ret);
  11402. #endif
  11403. #ifdef TARGET_NR_fadvise64
  11404. case TARGET_NR_fadvise64:
  11405. /* 5 args: fd, offset (high, low), len, advice */
  11406. if (regpairs_aligned(cpu_env, num)) {
  11407. /* offset is in (3,4), len in 5 and advice in 6 */
  11408. arg2 = arg3;
  11409. arg3 = arg4;
  11410. arg4 = arg5;
  11411. arg5 = arg6;
  11412. }
  11413. ret = posix_fadvise(arg1, target_offset64(arg2, arg3), arg4, arg5);
  11414. return -host_to_target_errno(ret);
  11415. #endif
  11416. #else /* not a 32-bit ABI */
  11417. #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_fadvise64)
  11418. #ifdef TARGET_NR_fadvise64_64
  11419. case TARGET_NR_fadvise64_64:
  11420. #endif
  11421. #ifdef TARGET_NR_fadvise64
  11422. case TARGET_NR_fadvise64:
  11423. #endif
  11424. #ifdef TARGET_S390X
  11425. switch (arg4) {
  11426. case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
  11427. case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
  11428. case 6: arg4 = POSIX_FADV_DONTNEED; break;
  11429. case 7: arg4 = POSIX_FADV_NOREUSE; break;
  11430. default: break;
  11431. }
  11432. #endif
  11433. return -host_to_target_errno(posix_fadvise(arg1, arg2, arg3, arg4));
  11434. #endif
  11435. #endif /* end of 64-bit ABI fadvise handling */
  11436. #ifdef TARGET_NR_madvise
  11437. case TARGET_NR_madvise:
  11438. return target_madvise(arg1, arg2, arg3);
  11439. #endif
  11440. #ifdef TARGET_NR_fcntl64
  11441. case TARGET_NR_fcntl64:
  11442. {
  11443. int cmd;
  11444. struct flock64 fl;
  11445. from_flock64_fn *copyfrom = copy_from_user_flock64;
  11446. to_flock64_fn *copyto = copy_to_user_flock64;
  11447. #ifdef TARGET_ARM
  11448. if (!cpu_env->eabi) {
  11449. copyfrom = copy_from_user_oabi_flock64;
  11450. copyto = copy_to_user_oabi_flock64;
  11451. }
  11452. #endif
  11453. cmd = target_to_host_fcntl_cmd(arg2);
  11454. if (cmd == -TARGET_EINVAL) {
  11455. return cmd;
  11456. }
  11457. switch(arg2) {
  11458. case TARGET_F_GETLK64:
  11459. ret = copyfrom(&fl, arg3);
  11460. if (ret) {
  11461. break;
  11462. }
  11463. ret = get_errno(safe_fcntl(arg1, cmd, &fl));
  11464. if (ret == 0) {
  11465. ret = copyto(arg3, &fl);
  11466. }
  11467. break;
  11468. case TARGET_F_SETLK64:
  11469. case TARGET_F_SETLKW64:
  11470. ret = copyfrom(&fl, arg3);
  11471. if (ret) {
  11472. break;
  11473. }
  11474. ret = get_errno(safe_fcntl(arg1, cmd, &fl));
  11475. break;
  11476. default:
  11477. ret = do_fcntl(arg1, arg2, arg3);
  11478. break;
  11479. }
  11480. return ret;
  11481. }
  11482. #endif
  11483. #ifdef TARGET_NR_cacheflush
  11484. case TARGET_NR_cacheflush:
  11485. /* self-modifying code is handled automatically, so nothing needed */
  11486. return 0;
  11487. #endif
  11488. #ifdef TARGET_NR_getpagesize
  11489. case TARGET_NR_getpagesize:
  11490. return TARGET_PAGE_SIZE;
  11491. #endif
  11492. case TARGET_NR_gettid:
  11493. return get_errno(sys_gettid());
  11494. #ifdef TARGET_NR_readahead
  11495. case TARGET_NR_readahead:
  11496. #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
  11497. if (regpairs_aligned(cpu_env, num)) {
  11498. arg2 = arg3;
  11499. arg3 = arg4;
  11500. arg4 = arg5;
  11501. }
  11502. ret = get_errno(readahead(arg1, target_offset64(arg2, arg3) , arg4));
  11503. #else
  11504. ret = get_errno(readahead(arg1, arg2, arg3));
  11505. #endif
  11506. return ret;
  11507. #endif
  11508. #ifdef CONFIG_ATTR
  11509. #ifdef TARGET_NR_setxattr
  11510. case TARGET_NR_listxattr:
  11511. case TARGET_NR_llistxattr:
  11512. {
  11513. void *p, *b = 0;
  11514. if (arg2) {
  11515. b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  11516. if (!b) {
  11517. return -TARGET_EFAULT;
  11518. }
  11519. }
  11520. p = lock_user_string(arg1);
  11521. if (p) {
  11522. if (num == TARGET_NR_listxattr) {
  11523. ret = get_errno(listxattr(p, b, arg3));
  11524. } else {
  11525. ret = get_errno(llistxattr(p, b, arg3));
  11526. }
  11527. } else {
  11528. ret = -TARGET_EFAULT;
  11529. }
  11530. unlock_user(p, arg1, 0);
  11531. unlock_user(b, arg2, arg3);
  11532. return ret;
  11533. }
  11534. case TARGET_NR_flistxattr:
  11535. {
  11536. void *b = 0;
  11537. if (arg2) {
  11538. b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  11539. if (!b) {
  11540. return -TARGET_EFAULT;
  11541. }
  11542. }
  11543. ret = get_errno(flistxattr(arg1, b, arg3));
  11544. unlock_user(b, arg2, arg3);
  11545. return ret;
  11546. }
  11547. case TARGET_NR_setxattr:
  11548. case TARGET_NR_lsetxattr:
  11549. {
  11550. void *p, *n, *v = 0;
  11551. if (arg3) {
  11552. v = lock_user(VERIFY_READ, arg3, arg4, 1);
  11553. if (!v) {
  11554. return -TARGET_EFAULT;
  11555. }
  11556. }
  11557. p = lock_user_string(arg1);
  11558. n = lock_user_string(arg2);
  11559. if (p && n) {
  11560. if (num == TARGET_NR_setxattr) {
  11561. ret = get_errno(setxattr(p, n, v, arg4, arg5));
  11562. } else {
  11563. ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
  11564. }
  11565. } else {
  11566. ret = -TARGET_EFAULT;
  11567. }
  11568. unlock_user(p, arg1, 0);
  11569. unlock_user(n, arg2, 0);
  11570. unlock_user(v, arg3, 0);
  11571. }
  11572. return ret;
  11573. case TARGET_NR_fsetxattr:
  11574. {
  11575. void *n, *v = 0;
  11576. if (arg3) {
  11577. v = lock_user(VERIFY_READ, arg3, arg4, 1);
  11578. if (!v) {
  11579. return -TARGET_EFAULT;
  11580. }
  11581. }
  11582. n = lock_user_string(arg2);
  11583. if (n) {
  11584. ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
  11585. } else {
  11586. ret = -TARGET_EFAULT;
  11587. }
  11588. unlock_user(n, arg2, 0);
  11589. unlock_user(v, arg3, 0);
  11590. }
  11591. return ret;
  11592. case TARGET_NR_getxattr:
  11593. case TARGET_NR_lgetxattr:
  11594. {
  11595. void *p, *n, *v = 0;
  11596. if (arg3) {
  11597. v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  11598. if (!v) {
  11599. return -TARGET_EFAULT;
  11600. }
  11601. }
  11602. p = lock_user_string(arg1);
  11603. n = lock_user_string(arg2);
  11604. if (p && n) {
  11605. if (num == TARGET_NR_getxattr) {
  11606. ret = get_errno(getxattr(p, n, v, arg4));
  11607. } else {
  11608. ret = get_errno(lgetxattr(p, n, v, arg4));
  11609. }
  11610. } else {
  11611. ret = -TARGET_EFAULT;
  11612. }
  11613. unlock_user(p, arg1, 0);
  11614. unlock_user(n, arg2, 0);
  11615. unlock_user(v, arg3, arg4);
  11616. }
  11617. return ret;
  11618. case TARGET_NR_fgetxattr:
  11619. {
  11620. void *n, *v = 0;
  11621. if (arg3) {
  11622. v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  11623. if (!v) {
  11624. return -TARGET_EFAULT;
  11625. }
  11626. }
  11627. n = lock_user_string(arg2);
  11628. if (n) {
  11629. ret = get_errno(fgetxattr(arg1, n, v, arg4));
  11630. } else {
  11631. ret = -TARGET_EFAULT;
  11632. }
  11633. unlock_user(n, arg2, 0);
  11634. unlock_user(v, arg3, arg4);
  11635. }
  11636. return ret;
  11637. case TARGET_NR_removexattr:
  11638. case TARGET_NR_lremovexattr:
  11639. {
  11640. void *p, *n;
  11641. p = lock_user_string(arg1);
  11642. n = lock_user_string(arg2);
  11643. if (p && n) {
  11644. if (num == TARGET_NR_removexattr) {
  11645. ret = get_errno(removexattr(p, n));
  11646. } else {
  11647. ret = get_errno(lremovexattr(p, n));
  11648. }
  11649. } else {
  11650. ret = -TARGET_EFAULT;
  11651. }
  11652. unlock_user(p, arg1, 0);
  11653. unlock_user(n, arg2, 0);
  11654. }
  11655. return ret;
  11656. case TARGET_NR_fremovexattr:
  11657. {
  11658. void *n;
  11659. n = lock_user_string(arg2);
  11660. if (n) {
  11661. ret = get_errno(fremovexattr(arg1, n));
  11662. } else {
  11663. ret = -TARGET_EFAULT;
  11664. }
  11665. unlock_user(n, arg2, 0);
  11666. }
  11667. return ret;
  11668. #endif
  11669. #endif /* CONFIG_ATTR */
  11670. #ifdef TARGET_NR_set_thread_area
  11671. case TARGET_NR_set_thread_area:
  11672. #if defined(TARGET_MIPS)
  11673. cpu_env->active_tc.CP0_UserLocal = arg1;
  11674. return 0;
  11675. #elif defined(TARGET_CRIS)
  11676. if (arg1 & 0xff)
  11677. ret = -TARGET_EINVAL;
  11678. else {
  11679. cpu_env->pregs[PR_PID] = arg1;
  11680. ret = 0;
  11681. }
  11682. return ret;
  11683. #elif defined(TARGET_I386) && defined(TARGET_ABI32)
  11684. return do_set_thread_area(cpu_env, arg1);
  11685. #elif defined(TARGET_M68K)
  11686. {
  11687. TaskState *ts = cpu->opaque;
  11688. ts->tp_value = arg1;
  11689. return 0;
  11690. }
  11691. #else
  11692. return -TARGET_ENOSYS;
  11693. #endif
  11694. #endif
  11695. #ifdef TARGET_NR_get_thread_area
  11696. case TARGET_NR_get_thread_area:
  11697. #if defined(TARGET_I386) && defined(TARGET_ABI32)
  11698. return do_get_thread_area(cpu_env, arg1);
  11699. #elif defined(TARGET_M68K)
  11700. {
  11701. TaskState *ts = cpu->opaque;
  11702. return ts->tp_value;
  11703. }
  11704. #else
  11705. return -TARGET_ENOSYS;
  11706. #endif
  11707. #endif
  11708. #ifdef TARGET_NR_getdomainname
  11709. case TARGET_NR_getdomainname:
  11710. return -TARGET_ENOSYS;
  11711. #endif
  11712. #ifdef TARGET_NR_clock_settime
  11713. case TARGET_NR_clock_settime:
  11714. {
  11715. struct timespec ts;
  11716. ret = target_to_host_timespec(&ts, arg2);
  11717. if (!is_error(ret)) {
  11718. ret = get_errno(clock_settime(arg1, &ts));
  11719. }
  11720. return ret;
  11721. }
  11722. #endif
  11723. #ifdef TARGET_NR_clock_settime64
  11724. case TARGET_NR_clock_settime64:
  11725. {
  11726. struct timespec ts;
  11727. ret = target_to_host_timespec64(&ts, arg2);
  11728. if (!is_error(ret)) {
  11729. ret = get_errno(clock_settime(arg1, &ts));
  11730. }
  11731. return ret;
  11732. }
  11733. #endif
  11734. #ifdef TARGET_NR_clock_gettime
  11735. case TARGET_NR_clock_gettime:
  11736. {
  11737. struct timespec ts;
  11738. ret = get_errno(clock_gettime(arg1, &ts));
  11739. if (!is_error(ret)) {
  11740. ret = host_to_target_timespec(arg2, &ts);
  11741. }
  11742. return ret;
  11743. }
  11744. #endif
  11745. #ifdef TARGET_NR_clock_gettime64
  11746. case TARGET_NR_clock_gettime64:
  11747. {
  11748. struct timespec ts;
  11749. ret = get_errno(clock_gettime(arg1, &ts));
  11750. if (!is_error(ret)) {
  11751. ret = host_to_target_timespec64(arg2, &ts);
  11752. }
  11753. return ret;
  11754. }
  11755. #endif
  11756. #ifdef TARGET_NR_clock_getres
  11757. case TARGET_NR_clock_getres:
  11758. {
  11759. struct timespec ts;
  11760. ret = get_errno(clock_getres(arg1, &ts));
  11761. if (!is_error(ret)) {
  11762. host_to_target_timespec(arg2, &ts);
  11763. }
  11764. return ret;
  11765. }
  11766. #endif
  11767. #ifdef TARGET_NR_clock_getres_time64
  11768. case TARGET_NR_clock_getres_time64:
  11769. {
  11770. struct timespec ts;
  11771. ret = get_errno(clock_getres(arg1, &ts));
  11772. if (!is_error(ret)) {
  11773. host_to_target_timespec64(arg2, &ts);
  11774. }
  11775. return ret;
  11776. }
  11777. #endif
  11778. #ifdef TARGET_NR_clock_nanosleep
  11779. case TARGET_NR_clock_nanosleep:
  11780. {
  11781. struct timespec ts;
  11782. if (target_to_host_timespec(&ts, arg3)) {
  11783. return -TARGET_EFAULT;
  11784. }
  11785. ret = get_errno(safe_clock_nanosleep(arg1, arg2,
  11786. &ts, arg4 ? &ts : NULL));
  11787. /*
  11788. * if the call is interrupted by a signal handler, it fails
  11789. * with error -TARGET_EINTR and if arg4 is not NULL and arg2 is not
  11790. * TIMER_ABSTIME, it returns the remaining unslept time in arg4.
  11791. */
  11792. if (ret == -TARGET_EINTR && arg4 && arg2 != TIMER_ABSTIME &&
  11793. host_to_target_timespec(arg4, &ts)) {
  11794. return -TARGET_EFAULT;
  11795. }
  11796. return ret;
  11797. }
  11798. #endif
  11799. #ifdef TARGET_NR_clock_nanosleep_time64
  11800. case TARGET_NR_clock_nanosleep_time64:
  11801. {
  11802. struct timespec ts;
  11803. if (target_to_host_timespec64(&ts, arg3)) {
  11804. return -TARGET_EFAULT;
  11805. }
  11806. ret = get_errno(safe_clock_nanosleep(arg1, arg2,
  11807. &ts, arg4 ? &ts : NULL));
  11808. if (ret == -TARGET_EINTR && arg4 && arg2 != TIMER_ABSTIME &&
  11809. host_to_target_timespec64(arg4, &ts)) {
  11810. return -TARGET_EFAULT;
  11811. }
  11812. return ret;
  11813. }
  11814. #endif
  11815. #if defined(TARGET_NR_set_tid_address)
  11816. case TARGET_NR_set_tid_address:
  11817. {
  11818. TaskState *ts = cpu->opaque;
  11819. ts->child_tidptr = arg1;
  11820. /* do not call host set_tid_address() syscall, instead return tid() */
  11821. return get_errno(sys_gettid());
  11822. }
  11823. #endif
  11824. case TARGET_NR_tkill:
  11825. return get_errno(safe_tkill((int)arg1, target_to_host_signal(arg2)));
  11826. case TARGET_NR_tgkill:
  11827. return get_errno(safe_tgkill((int)arg1, (int)arg2,
  11828. target_to_host_signal(arg3)));
  11829. #ifdef TARGET_NR_set_robust_list
  11830. case TARGET_NR_set_robust_list:
  11831. case TARGET_NR_get_robust_list:
  11832. /* The ABI for supporting robust futexes has userspace pass
  11833. * the kernel a pointer to a linked list which is updated by
  11834. * userspace after the syscall; the list is walked by the kernel
  11835. * when the thread exits. Since the linked list in QEMU guest
  11836. * memory isn't a valid linked list for the host and we have
  11837. * no way to reliably intercept the thread-death event, we can't
  11838. * support these. Silently return ENOSYS so that guest userspace
  11839. * falls back to a non-robust futex implementation (which should
  11840. * be OK except in the corner case of the guest crashing while
  11841. * holding a mutex that is shared with another process via
  11842. * shared memory).
  11843. */
  11844. return -TARGET_ENOSYS;
  11845. #endif
  11846. #if defined(TARGET_NR_utimensat)
  11847. case TARGET_NR_utimensat:
  11848. {
  11849. struct timespec *tsp, ts[2];
  11850. if (!arg3) {
  11851. tsp = NULL;
  11852. } else {
  11853. if (target_to_host_timespec(ts, arg3)) {
  11854. return -TARGET_EFAULT;
  11855. }
  11856. if (target_to_host_timespec(ts + 1, arg3 +
  11857. sizeof(struct target_timespec))) {
  11858. return -TARGET_EFAULT;
  11859. }
  11860. tsp = ts;
  11861. }
  11862. if (!arg2)
  11863. ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
  11864. else {
  11865. if (!(p = lock_user_string(arg2))) {
  11866. return -TARGET_EFAULT;
  11867. }
  11868. ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
  11869. unlock_user(p, arg2, 0);
  11870. }
  11871. }
  11872. return ret;
  11873. #endif
  11874. #ifdef TARGET_NR_utimensat_time64
  11875. case TARGET_NR_utimensat_time64:
  11876. {
  11877. struct timespec *tsp, ts[2];
  11878. if (!arg3) {
  11879. tsp = NULL;
  11880. } else {
  11881. if (target_to_host_timespec64(ts, arg3)) {
  11882. return -TARGET_EFAULT;
  11883. }
  11884. if (target_to_host_timespec64(ts + 1, arg3 +
  11885. sizeof(struct target__kernel_timespec))) {
  11886. return -TARGET_EFAULT;
  11887. }
  11888. tsp = ts;
  11889. }
  11890. if (!arg2)
  11891. ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
  11892. else {
  11893. p = lock_user_string(arg2);
  11894. if (!p) {
  11895. return -TARGET_EFAULT;
  11896. }
  11897. ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
  11898. unlock_user(p, arg2, 0);
  11899. }
  11900. }
  11901. return ret;
  11902. #endif
  11903. #ifdef TARGET_NR_futex
  11904. case TARGET_NR_futex:
  11905. return do_futex(cpu, false, arg1, arg2, arg3, arg4, arg5, arg6);
  11906. #endif
  11907. #ifdef TARGET_NR_futex_time64
  11908. case TARGET_NR_futex_time64:
  11909. return do_futex(cpu, true, arg1, arg2, arg3, arg4, arg5, arg6);
  11910. #endif
  11911. #ifdef CONFIG_INOTIFY
  11912. #if defined(TARGET_NR_inotify_init)
  11913. case TARGET_NR_inotify_init:
  11914. ret = get_errno(inotify_init());
  11915. if (ret >= 0) {
  11916. fd_trans_register(ret, &target_inotify_trans);
  11917. }
  11918. return ret;
  11919. #endif
  11920. #if defined(TARGET_NR_inotify_init1) && defined(CONFIG_INOTIFY1)
  11921. case TARGET_NR_inotify_init1:
  11922. ret = get_errno(inotify_init1(target_to_host_bitmask(arg1,
  11923. fcntl_flags_tbl)));
  11924. if (ret >= 0) {
  11925. fd_trans_register(ret, &target_inotify_trans);
  11926. }
  11927. return ret;
  11928. #endif
  11929. #if defined(TARGET_NR_inotify_add_watch)
  11930. case TARGET_NR_inotify_add_watch:
  11931. p = lock_user_string(arg2);
  11932. ret = get_errno(inotify_add_watch(arg1, path(p), arg3));
  11933. unlock_user(p, arg2, 0);
  11934. return ret;
  11935. #endif
  11936. #if defined(TARGET_NR_inotify_rm_watch)
  11937. case TARGET_NR_inotify_rm_watch:
  11938. return get_errno(inotify_rm_watch(arg1, arg2));
  11939. #endif
  11940. #endif
  11941. #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
  11942. case TARGET_NR_mq_open:
  11943. {
  11944. struct mq_attr posix_mq_attr;
  11945. struct mq_attr *pposix_mq_attr;
  11946. int host_flags;
  11947. host_flags = target_to_host_bitmask(arg2, fcntl_flags_tbl);
  11948. pposix_mq_attr = NULL;
  11949. if (arg4) {
  11950. if (copy_from_user_mq_attr(&posix_mq_attr, arg4) != 0) {
  11951. return -TARGET_EFAULT;
  11952. }
  11953. pposix_mq_attr = &posix_mq_attr;
  11954. }
  11955. p = lock_user_string(arg1 - 1);
  11956. if (!p) {
  11957. return -TARGET_EFAULT;
  11958. }
  11959. ret = get_errno(mq_open(p, host_flags, arg3, pposix_mq_attr));
  11960. unlock_user (p, arg1, 0);
  11961. }
  11962. return ret;
  11963. case TARGET_NR_mq_unlink:
  11964. p = lock_user_string(arg1 - 1);
  11965. if (!p) {
  11966. return -TARGET_EFAULT;
  11967. }
  11968. ret = get_errno(mq_unlink(p));
  11969. unlock_user (p, arg1, 0);
  11970. return ret;
  11971. #ifdef TARGET_NR_mq_timedsend
  11972. case TARGET_NR_mq_timedsend:
  11973. {
  11974. struct timespec ts;
  11975. p = lock_user (VERIFY_READ, arg2, arg3, 1);
  11976. if (arg5 != 0) {
  11977. if (target_to_host_timespec(&ts, arg5)) {
  11978. return -TARGET_EFAULT;
  11979. }
  11980. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts));
  11981. if (!is_error(ret) && host_to_target_timespec(arg5, &ts)) {
  11982. return -TARGET_EFAULT;
  11983. }
  11984. } else {
  11985. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL));
  11986. }
  11987. unlock_user (p, arg2, arg3);
  11988. }
  11989. return ret;
  11990. #endif
  11991. #ifdef TARGET_NR_mq_timedsend_time64
  11992. case TARGET_NR_mq_timedsend_time64:
  11993. {
  11994. struct timespec ts;
  11995. p = lock_user(VERIFY_READ, arg2, arg3, 1);
  11996. if (arg5 != 0) {
  11997. if (target_to_host_timespec64(&ts, arg5)) {
  11998. return -TARGET_EFAULT;
  11999. }
  12000. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts));
  12001. if (!is_error(ret) && host_to_target_timespec64(arg5, &ts)) {
  12002. return -TARGET_EFAULT;
  12003. }
  12004. } else {
  12005. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL));
  12006. }
  12007. unlock_user(p, arg2, arg3);
  12008. }
  12009. return ret;
  12010. #endif
  12011. #ifdef TARGET_NR_mq_timedreceive
  12012. case TARGET_NR_mq_timedreceive:
  12013. {
  12014. struct timespec ts;
  12015. unsigned int prio;
  12016. p = lock_user (VERIFY_READ, arg2, arg3, 1);
  12017. if (arg5 != 0) {
  12018. if (target_to_host_timespec(&ts, arg5)) {
  12019. return -TARGET_EFAULT;
  12020. }
  12021. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  12022. &prio, &ts));
  12023. if (!is_error(ret) && host_to_target_timespec(arg5, &ts)) {
  12024. return -TARGET_EFAULT;
  12025. }
  12026. } else {
  12027. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  12028. &prio, NULL));
  12029. }
  12030. unlock_user (p, arg2, arg3);
  12031. if (arg4 != 0)
  12032. put_user_u32(prio, arg4);
  12033. }
  12034. return ret;
  12035. #endif
  12036. #ifdef TARGET_NR_mq_timedreceive_time64
  12037. case TARGET_NR_mq_timedreceive_time64:
  12038. {
  12039. struct timespec ts;
  12040. unsigned int prio;
  12041. p = lock_user(VERIFY_READ, arg2, arg3, 1);
  12042. if (arg5 != 0) {
  12043. if (target_to_host_timespec64(&ts, arg5)) {
  12044. return -TARGET_EFAULT;
  12045. }
  12046. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  12047. &prio, &ts));
  12048. if (!is_error(ret) && host_to_target_timespec64(arg5, &ts)) {
  12049. return -TARGET_EFAULT;
  12050. }
  12051. } else {
  12052. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  12053. &prio, NULL));
  12054. }
  12055. unlock_user(p, arg2, arg3);
  12056. if (arg4 != 0) {
  12057. put_user_u32(prio, arg4);
  12058. }
  12059. }
  12060. return ret;
  12061. #endif
  12062. /* Not implemented for now... */
  12063. /* case TARGET_NR_mq_notify: */
  12064. /* break; */
  12065. case TARGET_NR_mq_getsetattr:
  12066. {
  12067. struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
  12068. ret = 0;
  12069. if (arg2 != 0) {
  12070. copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
  12071. ret = get_errno(mq_setattr(arg1, &posix_mq_attr_in,
  12072. &posix_mq_attr_out));
  12073. } else if (arg3 != 0) {
  12074. ret = get_errno(mq_getattr(arg1, &posix_mq_attr_out));
  12075. }
  12076. if (ret == 0 && arg3 != 0) {
  12077. copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
  12078. }
  12079. }
  12080. return ret;
  12081. #endif
  12082. #ifdef CONFIG_SPLICE
  12083. #ifdef TARGET_NR_tee
  12084. case TARGET_NR_tee:
  12085. {
  12086. ret = get_errno(tee(arg1,arg2,arg3,arg4));
  12087. }
  12088. return ret;
  12089. #endif
  12090. #ifdef TARGET_NR_splice
  12091. case TARGET_NR_splice:
  12092. {
  12093. loff_t loff_in, loff_out;
  12094. loff_t *ploff_in = NULL, *ploff_out = NULL;
  12095. if (arg2) {
  12096. if (get_user_u64(loff_in, arg2)) {
  12097. return -TARGET_EFAULT;
  12098. }
  12099. ploff_in = &loff_in;
  12100. }
  12101. if (arg4) {
  12102. if (get_user_u64(loff_out, arg4)) {
  12103. return -TARGET_EFAULT;
  12104. }
  12105. ploff_out = &loff_out;
  12106. }
  12107. ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
  12108. if (arg2) {
  12109. if (put_user_u64(loff_in, arg2)) {
  12110. return -TARGET_EFAULT;
  12111. }
  12112. }
  12113. if (arg4) {
  12114. if (put_user_u64(loff_out, arg4)) {
  12115. return -TARGET_EFAULT;
  12116. }
  12117. }
  12118. }
  12119. return ret;
  12120. #endif
  12121. #ifdef TARGET_NR_vmsplice
  12122. case TARGET_NR_vmsplice:
  12123. {
  12124. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  12125. if (vec != NULL) {
  12126. ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
  12127. unlock_iovec(vec, arg2, arg3, 0);
  12128. } else {
  12129. ret = -host_to_target_errno(errno);
  12130. }
  12131. }
  12132. return ret;
  12133. #endif
  12134. #endif /* CONFIG_SPLICE */
  12135. #ifdef CONFIG_EVENTFD
  12136. #if defined(TARGET_NR_eventfd)
  12137. case TARGET_NR_eventfd:
  12138. ret = get_errno(eventfd(arg1, 0));
  12139. if (ret >= 0) {
  12140. fd_trans_register(ret, &target_eventfd_trans);
  12141. }
  12142. return ret;
  12143. #endif
  12144. #if defined(TARGET_NR_eventfd2)
  12145. case TARGET_NR_eventfd2:
  12146. {
  12147. int host_flags = arg2 & (~(TARGET_O_NONBLOCK_MASK | TARGET_O_CLOEXEC));
  12148. if (arg2 & TARGET_O_NONBLOCK) {
  12149. host_flags |= O_NONBLOCK;
  12150. }
  12151. if (arg2 & TARGET_O_CLOEXEC) {
  12152. host_flags |= O_CLOEXEC;
  12153. }
  12154. ret = get_errno(eventfd(arg1, host_flags));
  12155. if (ret >= 0) {
  12156. fd_trans_register(ret, &target_eventfd_trans);
  12157. }
  12158. return ret;
  12159. }
  12160. #endif
  12161. #endif /* CONFIG_EVENTFD */
  12162. #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
  12163. case TARGET_NR_fallocate:
  12164. #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
  12165. ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
  12166. target_offset64(arg5, arg6)));
  12167. #else
  12168. ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
  12169. #endif
  12170. return ret;
  12171. #endif
  12172. #if defined(CONFIG_SYNC_FILE_RANGE)
  12173. #if defined(TARGET_NR_sync_file_range)
  12174. case TARGET_NR_sync_file_range:
  12175. #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
  12176. #if defined(TARGET_MIPS)
  12177. ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
  12178. target_offset64(arg5, arg6), arg7));
  12179. #else
  12180. ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
  12181. target_offset64(arg4, arg5), arg6));
  12182. #endif /* !TARGET_MIPS */
  12183. #else
  12184. ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
  12185. #endif
  12186. return ret;
  12187. #endif
  12188. #if defined(TARGET_NR_sync_file_range2) || \
  12189. defined(TARGET_NR_arm_sync_file_range)
  12190. #if defined(TARGET_NR_sync_file_range2)
  12191. case TARGET_NR_sync_file_range2:
  12192. #endif
  12193. #if defined(TARGET_NR_arm_sync_file_range)
  12194. case TARGET_NR_arm_sync_file_range:
  12195. #endif
  12196. /* This is like sync_file_range but the arguments are reordered */
  12197. #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
  12198. ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
  12199. target_offset64(arg5, arg6), arg2));
  12200. #else
  12201. ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
  12202. #endif
  12203. return ret;
  12204. #endif
  12205. #endif
  12206. #if defined(TARGET_NR_signalfd4)
  12207. case TARGET_NR_signalfd4:
  12208. return do_signalfd4(arg1, arg2, arg4);
  12209. #endif
  12210. #if defined(TARGET_NR_signalfd)
  12211. case TARGET_NR_signalfd:
  12212. return do_signalfd4(arg1, arg2, 0);
  12213. #endif
  12214. #if defined(CONFIG_EPOLL)
  12215. #if defined(TARGET_NR_epoll_create)
  12216. case TARGET_NR_epoll_create:
  12217. return get_errno(epoll_create(arg1));
  12218. #endif
  12219. #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
  12220. case TARGET_NR_epoll_create1:
  12221. return get_errno(epoll_create1(target_to_host_bitmask(arg1, fcntl_flags_tbl)));
  12222. #endif
  12223. #if defined(TARGET_NR_epoll_ctl)
  12224. case TARGET_NR_epoll_ctl:
  12225. {
  12226. struct epoll_event ep;
  12227. struct epoll_event *epp = 0;
  12228. if (arg4) {
  12229. if (arg2 != EPOLL_CTL_DEL) {
  12230. struct target_epoll_event *target_ep;
  12231. if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
  12232. return -TARGET_EFAULT;
  12233. }
  12234. ep.events = tswap32(target_ep->events);
  12235. /*
  12236. * The epoll_data_t union is just opaque data to the kernel,
  12237. * so we transfer all 64 bits across and need not worry what
  12238. * actual data type it is.
  12239. */
  12240. ep.data.u64 = tswap64(target_ep->data.u64);
  12241. unlock_user_struct(target_ep, arg4, 0);
  12242. }
  12243. /*
  12244. * before kernel 2.6.9, EPOLL_CTL_DEL operation required a
  12245. * non-null pointer, even though this argument is ignored.
  12246. *
  12247. */
  12248. epp = &ep;
  12249. }
  12250. return get_errno(epoll_ctl(arg1, arg2, arg3, epp));
  12251. }
  12252. #endif
  12253. #if defined(TARGET_NR_epoll_wait) || defined(TARGET_NR_epoll_pwait)
  12254. #if defined(TARGET_NR_epoll_wait)
  12255. case TARGET_NR_epoll_wait:
  12256. #endif
  12257. #if defined(TARGET_NR_epoll_pwait)
  12258. case TARGET_NR_epoll_pwait:
  12259. #endif
  12260. {
  12261. struct target_epoll_event *target_ep;
  12262. struct epoll_event *ep;
  12263. int epfd = arg1;
  12264. int maxevents = arg3;
  12265. int timeout = arg4;
  12266. if (maxevents <= 0 || maxevents > TARGET_EP_MAX_EVENTS) {
  12267. return -TARGET_EINVAL;
  12268. }
  12269. target_ep = lock_user(VERIFY_WRITE, arg2,
  12270. maxevents * sizeof(struct target_epoll_event), 1);
  12271. if (!target_ep) {
  12272. return -TARGET_EFAULT;
  12273. }
  12274. ep = g_try_new(struct epoll_event, maxevents);
  12275. if (!ep) {
  12276. unlock_user(target_ep, arg2, 0);
  12277. return -TARGET_ENOMEM;
  12278. }
  12279. switch (num) {
  12280. #if defined(TARGET_NR_epoll_pwait)
  12281. case TARGET_NR_epoll_pwait:
  12282. {
  12283. sigset_t *set = NULL;
  12284. if (arg5) {
  12285. ret = process_sigsuspend_mask(&set, arg5, arg6);
  12286. if (ret != 0) {
  12287. break;
  12288. }
  12289. }
  12290. ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
  12291. set, SIGSET_T_SIZE));
  12292. if (set) {
  12293. finish_sigsuspend_mask(ret);
  12294. }
  12295. break;
  12296. }
  12297. #endif
  12298. #if defined(TARGET_NR_epoll_wait)
  12299. case TARGET_NR_epoll_wait:
  12300. ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
  12301. NULL, 0));
  12302. break;
  12303. #endif
  12304. default:
  12305. ret = -TARGET_ENOSYS;
  12306. }
  12307. if (!is_error(ret)) {
  12308. int i;
  12309. for (i = 0; i < ret; i++) {
  12310. target_ep[i].events = tswap32(ep[i].events);
  12311. target_ep[i].data.u64 = tswap64(ep[i].data.u64);
  12312. }
  12313. unlock_user(target_ep, arg2,
  12314. ret * sizeof(struct target_epoll_event));
  12315. } else {
  12316. unlock_user(target_ep, arg2, 0);
  12317. }
  12318. g_free(ep);
  12319. return ret;
  12320. }
  12321. #endif
  12322. #endif
  12323. #ifdef TARGET_NR_prlimit64
  12324. case TARGET_NR_prlimit64:
  12325. {
  12326. /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
  12327. struct target_rlimit64 *target_rnew, *target_rold;
  12328. struct host_rlimit64 rnew, rold, *rnewp = 0;
  12329. int resource = target_to_host_resource(arg2);
  12330. if (arg3 && (resource != RLIMIT_AS &&
  12331. resource != RLIMIT_DATA &&
  12332. resource != RLIMIT_STACK)) {
  12333. if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
  12334. return -TARGET_EFAULT;
  12335. }
  12336. __get_user(rnew.rlim_cur, &target_rnew->rlim_cur);
  12337. __get_user(rnew.rlim_max, &target_rnew->rlim_max);
  12338. unlock_user_struct(target_rnew, arg3, 0);
  12339. rnewp = &rnew;
  12340. }
  12341. ret = get_errno(sys_prlimit64(arg1, resource, rnewp, arg4 ? &rold : 0));
  12342. if (!is_error(ret) && arg4) {
  12343. if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
  12344. return -TARGET_EFAULT;
  12345. }
  12346. __put_user(rold.rlim_cur, &target_rold->rlim_cur);
  12347. __put_user(rold.rlim_max, &target_rold->rlim_max);
  12348. unlock_user_struct(target_rold, arg4, 1);
  12349. }
  12350. return ret;
  12351. }
  12352. #endif
  12353. #ifdef TARGET_NR_gethostname
  12354. case TARGET_NR_gethostname:
  12355. {
  12356. char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
  12357. if (name) {
  12358. ret = get_errno(gethostname(name, arg2));
  12359. unlock_user(name, arg1, arg2);
  12360. } else {
  12361. ret = -TARGET_EFAULT;
  12362. }
  12363. return ret;
  12364. }
  12365. #endif
  12366. #ifdef TARGET_NR_atomic_cmpxchg_32
  12367. case TARGET_NR_atomic_cmpxchg_32:
  12368. {
  12369. /* should use start_exclusive from main.c */
  12370. abi_ulong mem_value;
  12371. if (get_user_u32(mem_value, arg6)) {
  12372. target_siginfo_t info;
  12373. info.si_signo = SIGSEGV;
  12374. info.si_errno = 0;
  12375. info.si_code = TARGET_SEGV_MAPERR;
  12376. info._sifields._sigfault._addr = arg6;
  12377. queue_signal(cpu_env, info.si_signo, QEMU_SI_FAULT, &info);
  12378. ret = 0xdeadbeef;
  12379. }
  12380. if (mem_value == arg2)
  12381. put_user_u32(arg1, arg6);
  12382. return mem_value;
  12383. }
  12384. #endif
  12385. #ifdef TARGET_NR_atomic_barrier
  12386. case TARGET_NR_atomic_barrier:
  12387. /* Like the kernel implementation and the
  12388. qemu arm barrier, no-op this? */
  12389. return 0;
  12390. #endif
  12391. #ifdef TARGET_NR_timer_create
  12392. case TARGET_NR_timer_create:
  12393. {
  12394. /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
  12395. struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL;
  12396. int clkid = arg1;
  12397. int timer_index = next_free_host_timer();
  12398. if (timer_index < 0) {
  12399. ret = -TARGET_EAGAIN;
  12400. } else {
  12401. timer_t *phtimer = g_posix_timers + timer_index;
  12402. if (arg2) {
  12403. phost_sevp = &host_sevp;
  12404. ret = target_to_host_sigevent(phost_sevp, arg2);
  12405. if (ret != 0) {
  12406. free_host_timer_slot(timer_index);
  12407. return ret;
  12408. }
  12409. }
  12410. ret = get_errno(timer_create(clkid, phost_sevp, phtimer));
  12411. if (ret) {
  12412. free_host_timer_slot(timer_index);
  12413. } else {
  12414. if (put_user(TIMER_MAGIC | timer_index, arg3, target_timer_t)) {
  12415. timer_delete(*phtimer);
  12416. free_host_timer_slot(timer_index);
  12417. return -TARGET_EFAULT;
  12418. }
  12419. }
  12420. }
  12421. return ret;
  12422. }
  12423. #endif
  12424. #ifdef TARGET_NR_timer_settime
  12425. case TARGET_NR_timer_settime:
  12426. {
  12427. /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
  12428. * struct itimerspec * old_value */
  12429. target_timer_t timerid = get_timer_id(arg1);
  12430. if (timerid < 0) {
  12431. ret = timerid;
  12432. } else if (arg3 == 0) {
  12433. ret = -TARGET_EINVAL;
  12434. } else {
  12435. timer_t htimer = g_posix_timers[timerid];
  12436. struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
  12437. if (target_to_host_itimerspec(&hspec_new, arg3)) {
  12438. return -TARGET_EFAULT;
  12439. }
  12440. ret = get_errno(
  12441. timer_settime(htimer, arg2, &hspec_new, &hspec_old));
  12442. if (arg4 && host_to_target_itimerspec(arg4, &hspec_old)) {
  12443. return -TARGET_EFAULT;
  12444. }
  12445. }
  12446. return ret;
  12447. }
  12448. #endif
  12449. #ifdef TARGET_NR_timer_settime64
  12450. case TARGET_NR_timer_settime64:
  12451. {
  12452. target_timer_t timerid = get_timer_id(arg1);
  12453. if (timerid < 0) {
  12454. ret = timerid;
  12455. } else if (arg3 == 0) {
  12456. ret = -TARGET_EINVAL;
  12457. } else {
  12458. timer_t htimer = g_posix_timers[timerid];
  12459. struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
  12460. if (target_to_host_itimerspec64(&hspec_new, arg3)) {
  12461. return -TARGET_EFAULT;
  12462. }
  12463. ret = get_errno(
  12464. timer_settime(htimer, arg2, &hspec_new, &hspec_old));
  12465. if (arg4 && host_to_target_itimerspec64(arg4, &hspec_old)) {
  12466. return -TARGET_EFAULT;
  12467. }
  12468. }
  12469. return ret;
  12470. }
  12471. #endif
  12472. #ifdef TARGET_NR_timer_gettime
  12473. case TARGET_NR_timer_gettime:
  12474. {
  12475. /* args: timer_t timerid, struct itimerspec *curr_value */
  12476. target_timer_t timerid = get_timer_id(arg1);
  12477. if (timerid < 0) {
  12478. ret = timerid;
  12479. } else if (!arg2) {
  12480. ret = -TARGET_EFAULT;
  12481. } else {
  12482. timer_t htimer = g_posix_timers[timerid];
  12483. struct itimerspec hspec;
  12484. ret = get_errno(timer_gettime(htimer, &hspec));
  12485. if (host_to_target_itimerspec(arg2, &hspec)) {
  12486. ret = -TARGET_EFAULT;
  12487. }
  12488. }
  12489. return ret;
  12490. }
  12491. #endif
  12492. #ifdef TARGET_NR_timer_gettime64
  12493. case TARGET_NR_timer_gettime64:
  12494. {
  12495. /* args: timer_t timerid, struct itimerspec64 *curr_value */
  12496. target_timer_t timerid = get_timer_id(arg1);
  12497. if (timerid < 0) {
  12498. ret = timerid;
  12499. } else if (!arg2) {
  12500. ret = -TARGET_EFAULT;
  12501. } else {
  12502. timer_t htimer = g_posix_timers[timerid];
  12503. struct itimerspec hspec;
  12504. ret = get_errno(timer_gettime(htimer, &hspec));
  12505. if (host_to_target_itimerspec64(arg2, &hspec)) {
  12506. ret = -TARGET_EFAULT;
  12507. }
  12508. }
  12509. return ret;
  12510. }
  12511. #endif
  12512. #ifdef TARGET_NR_timer_getoverrun
  12513. case TARGET_NR_timer_getoverrun:
  12514. {
  12515. /* args: timer_t timerid */
  12516. target_timer_t timerid = get_timer_id(arg1);
  12517. if (timerid < 0) {
  12518. ret = timerid;
  12519. } else {
  12520. timer_t htimer = g_posix_timers[timerid];
  12521. ret = get_errno(timer_getoverrun(htimer));
  12522. }
  12523. return ret;
  12524. }
  12525. #endif
  12526. #ifdef TARGET_NR_timer_delete
  12527. case TARGET_NR_timer_delete:
  12528. {
  12529. /* args: timer_t timerid */
  12530. target_timer_t timerid = get_timer_id(arg1);
  12531. if (timerid < 0) {
  12532. ret = timerid;
  12533. } else {
  12534. timer_t htimer = g_posix_timers[timerid];
  12535. ret = get_errno(timer_delete(htimer));
  12536. free_host_timer_slot(timerid);
  12537. }
  12538. return ret;
  12539. }
  12540. #endif
  12541. #if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
  12542. case TARGET_NR_timerfd_create:
  12543. ret = get_errno(timerfd_create(arg1,
  12544. target_to_host_bitmask(arg2, fcntl_flags_tbl)));
  12545. if (ret >= 0) {
  12546. fd_trans_register(ret, &target_timerfd_trans);
  12547. }
  12548. return ret;
  12549. #endif
  12550. #if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
  12551. case TARGET_NR_timerfd_gettime:
  12552. {
  12553. struct itimerspec its_curr;
  12554. ret = get_errno(timerfd_gettime(arg1, &its_curr));
  12555. if (arg2 && host_to_target_itimerspec(arg2, &its_curr)) {
  12556. return -TARGET_EFAULT;
  12557. }
  12558. }
  12559. return ret;
  12560. #endif
  12561. #if defined(TARGET_NR_timerfd_gettime64) && defined(CONFIG_TIMERFD)
  12562. case TARGET_NR_timerfd_gettime64:
  12563. {
  12564. struct itimerspec its_curr;
  12565. ret = get_errno(timerfd_gettime(arg1, &its_curr));
  12566. if (arg2 && host_to_target_itimerspec64(arg2, &its_curr)) {
  12567. return -TARGET_EFAULT;
  12568. }
  12569. }
  12570. return ret;
  12571. #endif
  12572. #if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
  12573. case TARGET_NR_timerfd_settime:
  12574. {
  12575. struct itimerspec its_new, its_old, *p_new;
  12576. if (arg3) {
  12577. if (target_to_host_itimerspec(&its_new, arg3)) {
  12578. return -TARGET_EFAULT;
  12579. }
  12580. p_new = &its_new;
  12581. } else {
  12582. p_new = NULL;
  12583. }
  12584. ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
  12585. if (arg4 && host_to_target_itimerspec(arg4, &its_old)) {
  12586. return -TARGET_EFAULT;
  12587. }
  12588. }
  12589. return ret;
  12590. #endif
  12591. #if defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)
  12592. case TARGET_NR_timerfd_settime64:
  12593. {
  12594. struct itimerspec its_new, its_old, *p_new;
  12595. if (arg3) {
  12596. if (target_to_host_itimerspec64(&its_new, arg3)) {
  12597. return -TARGET_EFAULT;
  12598. }
  12599. p_new = &its_new;
  12600. } else {
  12601. p_new = NULL;
  12602. }
  12603. ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
  12604. if (arg4 && host_to_target_itimerspec64(arg4, &its_old)) {
  12605. return -TARGET_EFAULT;
  12606. }
  12607. }
  12608. return ret;
  12609. #endif
  12610. #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
  12611. case TARGET_NR_ioprio_get:
  12612. return get_errno(ioprio_get(arg1, arg2));
  12613. #endif
  12614. #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
  12615. case TARGET_NR_ioprio_set:
  12616. return get_errno(ioprio_set(arg1, arg2, arg3));
  12617. #endif
  12618. #if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
  12619. case TARGET_NR_setns:
  12620. return get_errno(setns(arg1, arg2));
  12621. #endif
  12622. #if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
  12623. case TARGET_NR_unshare:
  12624. return get_errno(unshare(arg1));
  12625. #endif
  12626. #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
  12627. case TARGET_NR_kcmp:
  12628. return get_errno(kcmp(arg1, arg2, arg3, arg4, arg5));
  12629. #endif
  12630. #ifdef TARGET_NR_swapcontext
  12631. case TARGET_NR_swapcontext:
  12632. /* PowerPC specific. */
  12633. return do_swapcontext(cpu_env, arg1, arg2, arg3);
  12634. #endif
  12635. #ifdef TARGET_NR_memfd_create
  12636. case TARGET_NR_memfd_create:
  12637. p = lock_user_string(arg1);
  12638. if (!p) {
  12639. return -TARGET_EFAULT;
  12640. }
  12641. ret = get_errno(memfd_create(p, arg2));
  12642. fd_trans_unregister(ret);
  12643. unlock_user(p, arg1, 0);
  12644. return ret;
  12645. #endif
  12646. #if defined TARGET_NR_membarrier && defined __NR_membarrier
  12647. case TARGET_NR_membarrier:
  12648. return get_errno(membarrier(arg1, arg2));
  12649. #endif
  12650. #if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range)
  12651. case TARGET_NR_copy_file_range:
  12652. {
  12653. loff_t inoff, outoff;
  12654. loff_t *pinoff = NULL, *poutoff = NULL;
  12655. if (arg2) {
  12656. if (get_user_u64(inoff, arg2)) {
  12657. return -TARGET_EFAULT;
  12658. }
  12659. pinoff = &inoff;
  12660. }
  12661. if (arg4) {
  12662. if (get_user_u64(outoff, arg4)) {
  12663. return -TARGET_EFAULT;
  12664. }
  12665. poutoff = &outoff;
  12666. }
  12667. /* Do not sign-extend the count parameter. */
  12668. ret = get_errno(safe_copy_file_range(arg1, pinoff, arg3, poutoff,
  12669. (abi_ulong)arg5, arg6));
  12670. if (!is_error(ret) && ret > 0) {
  12671. if (arg2) {
  12672. if (put_user_u64(inoff, arg2)) {
  12673. return -TARGET_EFAULT;
  12674. }
  12675. }
  12676. if (arg4) {
  12677. if (put_user_u64(outoff, arg4)) {
  12678. return -TARGET_EFAULT;
  12679. }
  12680. }
  12681. }
  12682. }
  12683. return ret;
  12684. #endif
  12685. #if defined(TARGET_NR_pivot_root)
  12686. case TARGET_NR_pivot_root:
  12687. {
  12688. void *p2;
  12689. p = lock_user_string(arg1); /* new_root */
  12690. p2 = lock_user_string(arg2); /* put_old */
  12691. if (!p || !p2) {
  12692. ret = -TARGET_EFAULT;
  12693. } else {
  12694. ret = get_errno(pivot_root(p, p2));
  12695. }
  12696. unlock_user(p2, arg2, 0);
  12697. unlock_user(p, arg1, 0);
  12698. }
  12699. return ret;
  12700. #endif
  12701. #if defined(TARGET_NR_riscv_hwprobe)
  12702. case TARGET_NR_riscv_hwprobe:
  12703. return do_riscv_hwprobe(cpu_env, arg1, arg2, arg3, arg4, arg5);
  12704. #endif
  12705. default:
  12706. qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num);
  12707. return -TARGET_ENOSYS;
  12708. }
  12709. return ret;
  12710. }
  12711. abi_long do_syscall(CPUArchState *cpu_env, int num, abi_long arg1,
  12712. abi_long arg2, abi_long arg3, abi_long arg4,
  12713. abi_long arg5, abi_long arg6, abi_long arg7,
  12714. abi_long arg8)
  12715. {
  12716. CPUState *cpu = env_cpu(cpu_env);
  12717. abi_long ret;
  12718. #ifdef DEBUG_ERESTARTSYS
  12719. /* Debug-only code for exercising the syscall-restart code paths
  12720. * in the per-architecture cpu main loops: restart every syscall
  12721. * the guest makes once before letting it through.
  12722. */
  12723. {
  12724. static bool flag;
  12725. flag = !flag;
  12726. if (flag) {
  12727. return -QEMU_ERESTARTSYS;
  12728. }
  12729. }
  12730. #endif
  12731. record_syscall_start(cpu, num, arg1,
  12732. arg2, arg3, arg4, arg5, arg6, arg7, arg8);
  12733. if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
  12734. print_syscall(cpu_env, num, arg1, arg2, arg3, arg4, arg5, arg6);
  12735. }
  12736. ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4,
  12737. arg5, arg6, arg7, arg8);
  12738. if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
  12739. print_syscall_ret(cpu_env, num, ret, arg1, arg2,
  12740. arg3, arg4, arg5, arg6);
  12741. }
  12742. record_syscall_return(cpu, num, ret);
  12743. return ret;
  12744. }