syscall.c 396 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449
  1. /*
  2. * Linux syscalls
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #define _ATFILE_SOURCE
  20. #include "qemu/osdep.h"
  21. #include "qemu/cutils.h"
  22. #include "qemu/path.h"
  23. #include "qemu/memfd.h"
  24. #include "qemu/queue.h"
  25. #include <elf.h>
  26. #include <endian.h>
  27. #include <grp.h>
  28. #include <sys/ipc.h>
  29. #include <sys/msg.h>
  30. #include <sys/wait.h>
  31. #include <sys/mount.h>
  32. #include <sys/file.h>
  33. #include <sys/fsuid.h>
  34. #include <sys/personality.h>
  35. #include <sys/prctl.h>
  36. #include <sys/resource.h>
  37. #include <sys/swap.h>
  38. #include <linux/capability.h>
  39. #include <sched.h>
  40. #include <sys/timex.h>
  41. #include <sys/socket.h>
  42. #include <linux/sockios.h>
  43. #include <sys/un.h>
  44. #include <sys/uio.h>
  45. #include <poll.h>
  46. #include <sys/times.h>
  47. #include <sys/shm.h>
  48. #include <sys/sem.h>
  49. #include <sys/statfs.h>
  50. #include <utime.h>
  51. #include <sys/sysinfo.h>
  52. #include <sys/signalfd.h>
  53. //#include <sys/user.h>
  54. #include <netinet/ip.h>
  55. #include <netinet/tcp.h>
  56. #include <linux/wireless.h>
  57. #include <linux/icmp.h>
  58. #include <linux/icmpv6.h>
  59. #include <linux/errqueue.h>
  60. #include <linux/random.h>
  61. #ifdef CONFIG_TIMERFD
  62. #include <sys/timerfd.h>
  63. #endif
  64. #ifdef CONFIG_EVENTFD
  65. #include <sys/eventfd.h>
  66. #endif
  67. #ifdef CONFIG_EPOLL
  68. #include <sys/epoll.h>
  69. #endif
  70. #ifdef CONFIG_ATTR
  71. #include "qemu/xattr.h"
  72. #endif
  73. #ifdef CONFIG_SENDFILE
  74. #include <sys/sendfile.h>
  75. #endif
  76. #ifdef CONFIG_KCOV
  77. #include <sys/kcov.h>
  78. #endif
  79. #define termios host_termios
  80. #define winsize host_winsize
  81. #define termio host_termio
  82. #define sgttyb host_sgttyb /* same as target */
  83. #define tchars host_tchars /* same as target */
  84. #define ltchars host_ltchars /* same as target */
  85. #include <linux/termios.h>
  86. #include <linux/unistd.h>
  87. #include <linux/cdrom.h>
  88. #include <linux/hdreg.h>
  89. #include <linux/soundcard.h>
  90. #include <linux/kd.h>
  91. #include <linux/mtio.h>
  92. #include <linux/fs.h>
  93. #include <linux/fd.h>
  94. #if defined(CONFIG_FIEMAP)
  95. #include <linux/fiemap.h>
  96. #endif
  97. #include <linux/fb.h>
  98. #if defined(CONFIG_USBFS)
  99. #include <linux/usbdevice_fs.h>
  100. #include <linux/usb/ch9.h>
  101. #endif
  102. #include <linux/vt.h>
  103. #include <linux/dm-ioctl.h>
  104. #include <linux/reboot.h>
  105. #include <linux/route.h>
  106. #include <linux/filter.h>
  107. #include <linux/blkpg.h>
  108. #include <netpacket/packet.h>
  109. #include <linux/netlink.h>
  110. #include <linux/if_alg.h>
  111. #include <linux/rtc.h>
  112. #include <sound/asound.h>
  113. #include "linux_loop.h"
  114. #include "uname.h"
  115. #include "qemu.h"
  116. #include "qemu/guest-random.h"
  117. #include "qemu/selfmap.h"
  118. #include "user/syscall-trace.h"
  119. #include "qapi/error.h"
  120. #include "fd-trans.h"
  121. #include "tcg/tcg.h"
  122. #ifndef CLONE_IO
  123. #define CLONE_IO 0x80000000 /* Clone io context */
  124. #endif
  125. /* We can't directly call the host clone syscall, because this will
  126. * badly confuse libc (breaking mutexes, for example). So we must
  127. * divide clone flags into:
  128. * * flag combinations that look like pthread_create()
  129. * * flag combinations that look like fork()
  130. * * flags we can implement within QEMU itself
  131. * * flags we can't support and will return an error for
  132. */
  133. /* For thread creation, all these flags must be present; for
  134. * fork, none must be present.
  135. */
  136. #define CLONE_THREAD_FLAGS \
  137. (CLONE_VM | CLONE_FS | CLONE_FILES | \
  138. CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM)
  139. /* These flags are ignored:
  140. * CLONE_DETACHED is now ignored by the kernel;
  141. * CLONE_IO is just an optimisation hint to the I/O scheduler
  142. */
  143. #define CLONE_IGNORED_FLAGS \
  144. (CLONE_DETACHED | CLONE_IO)
  145. /* Flags for fork which we can implement within QEMU itself */
  146. #define CLONE_OPTIONAL_FORK_FLAGS \
  147. (CLONE_SETTLS | CLONE_PARENT_SETTID | \
  148. CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID)
  149. /* Flags for thread creation which we can implement within QEMU itself */
  150. #define CLONE_OPTIONAL_THREAD_FLAGS \
  151. (CLONE_SETTLS | CLONE_PARENT_SETTID | \
  152. CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | CLONE_PARENT)
  153. #define CLONE_INVALID_FORK_FLAGS \
  154. (~(CSIGNAL | CLONE_OPTIONAL_FORK_FLAGS | CLONE_IGNORED_FLAGS))
  155. #define CLONE_INVALID_THREAD_FLAGS \
  156. (~(CSIGNAL | CLONE_THREAD_FLAGS | CLONE_OPTIONAL_THREAD_FLAGS | \
  157. CLONE_IGNORED_FLAGS))
  158. /* CLONE_VFORK is special cased early in do_fork(). The other flag bits
  159. * have almost all been allocated. We cannot support any of
  160. * CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC,
  161. * CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, CLONE_PTRACE, CLONE_UNTRACED.
  162. * The checks against the invalid thread masks above will catch these.
  163. * (The one remaining unallocated bit is 0x1000 which used to be CLONE_PID.)
  164. */
  165. /* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
  166. * once. This exercises the codepaths for restart.
  167. */
  168. //#define DEBUG_ERESTARTSYS
  169. //#include <linux/msdos_fs.h>
  170. #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
  171. #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
  172. #undef _syscall0
  173. #undef _syscall1
  174. #undef _syscall2
  175. #undef _syscall3
  176. #undef _syscall4
  177. #undef _syscall5
  178. #undef _syscall6
  179. #define _syscall0(type,name) \
  180. static type name (void) \
  181. { \
  182. return syscall(__NR_##name); \
  183. }
  184. #define _syscall1(type,name,type1,arg1) \
  185. static type name (type1 arg1) \
  186. { \
  187. return syscall(__NR_##name, arg1); \
  188. }
  189. #define _syscall2(type,name,type1,arg1,type2,arg2) \
  190. static type name (type1 arg1,type2 arg2) \
  191. { \
  192. return syscall(__NR_##name, arg1, arg2); \
  193. }
  194. #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
  195. static type name (type1 arg1,type2 arg2,type3 arg3) \
  196. { \
  197. return syscall(__NR_##name, arg1, arg2, arg3); \
  198. }
  199. #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
  200. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
  201. { \
  202. return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
  203. }
  204. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  205. type5,arg5) \
  206. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
  207. { \
  208. return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
  209. }
  210. #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  211. type5,arg5,type6,arg6) \
  212. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
  213. type6 arg6) \
  214. { \
  215. return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
  216. }
  217. #define __NR_sys_uname __NR_uname
  218. #define __NR_sys_getcwd1 __NR_getcwd
  219. #define __NR_sys_getdents __NR_getdents
  220. #define __NR_sys_getdents64 __NR_getdents64
  221. #define __NR_sys_getpriority __NR_getpriority
  222. #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
  223. #define __NR_sys_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo
  224. #define __NR_sys_syslog __NR_syslog
  225. #if defined(__NR_futex)
  226. # define __NR_sys_futex __NR_futex
  227. #endif
  228. #if defined(__NR_futex_time64)
  229. # define __NR_sys_futex_time64 __NR_futex_time64
  230. #endif
  231. #define __NR_sys_inotify_init __NR_inotify_init
  232. #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
  233. #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
  234. #define __NR_sys_statx __NR_statx
  235. #if defined(__alpha__) || defined(__x86_64__) || defined(__s390x__)
  236. #define __NR__llseek __NR_lseek
  237. #endif
  238. /* Newer kernel ports have llseek() instead of _llseek() */
  239. #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
  240. #define TARGET_NR__llseek TARGET_NR_llseek
  241. #endif
  242. #define __NR_sys_gettid __NR_gettid
  243. _syscall0(int, sys_gettid)
  244. /* For the 64-bit guest on 32-bit host case we must emulate
  245. * getdents using getdents64, because otherwise the host
  246. * might hand us back more dirent records than we can fit
  247. * into the guest buffer after structure format conversion.
  248. * Otherwise we emulate getdents with getdents if the host has it.
  249. */
  250. #if defined(__NR_getdents) && HOST_LONG_BITS >= TARGET_ABI_BITS
  251. #define EMULATE_GETDENTS_WITH_GETDENTS
  252. #endif
  253. #if defined(TARGET_NR_getdents) && defined(EMULATE_GETDENTS_WITH_GETDENTS)
  254. _syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
  255. #endif
  256. #if (defined(TARGET_NR_getdents) && \
  257. !defined(EMULATE_GETDENTS_WITH_GETDENTS)) || \
  258. (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
  259. _syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
  260. #endif
  261. #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
  262. _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
  263. loff_t *, res, uint, wh);
  264. #endif
  265. _syscall3(int, sys_rt_sigqueueinfo, pid_t, pid, int, sig, siginfo_t *, uinfo)
  266. _syscall4(int, sys_rt_tgsigqueueinfo, pid_t, pid, pid_t, tid, int, sig,
  267. siginfo_t *, uinfo)
  268. _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
  269. #ifdef __NR_exit_group
  270. _syscall1(int,exit_group,int,error_code)
  271. #endif
  272. #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
  273. _syscall1(int,set_tid_address,int *,tidptr)
  274. #endif
  275. #if defined(__NR_futex)
  276. _syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
  277. const struct timespec *,timeout,int *,uaddr2,int,val3)
  278. #endif
  279. #if defined(__NR_futex_time64)
  280. _syscall6(int,sys_futex_time64,int *,uaddr,int,op,int,val,
  281. const struct timespec *,timeout,int *,uaddr2,int,val3)
  282. #endif
  283. #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
  284. _syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
  285. unsigned long *, user_mask_ptr);
  286. #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
  287. _syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
  288. unsigned long *, user_mask_ptr);
  289. #define __NR_sys_getcpu __NR_getcpu
  290. _syscall3(int, sys_getcpu, unsigned *, cpu, unsigned *, node, void *, tcache);
  291. _syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
  292. void *, arg);
  293. _syscall2(int, capget, struct __user_cap_header_struct *, header,
  294. struct __user_cap_data_struct *, data);
  295. _syscall2(int, capset, struct __user_cap_header_struct *, header,
  296. struct __user_cap_data_struct *, data);
  297. #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
  298. _syscall2(int, ioprio_get, int, which, int, who)
  299. #endif
  300. #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
  301. _syscall3(int, ioprio_set, int, which, int, who, int, ioprio)
  302. #endif
  303. #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
  304. _syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags)
  305. #endif
  306. #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
  307. _syscall5(int, kcmp, pid_t, pid1, pid_t, pid2, int, type,
  308. unsigned long, idx1, unsigned long, idx2)
  309. #endif
  310. /*
  311. * It is assumed that struct statx is architecture independent.
  312. */
  313. #if defined(TARGET_NR_statx) && defined(__NR_statx)
  314. _syscall5(int, sys_statx, int, dirfd, const char *, pathname, int, flags,
  315. unsigned int, mask, struct target_statx *, statxbuf)
  316. #endif
  317. #if defined(TARGET_NR_membarrier) && defined(__NR_membarrier)
  318. _syscall2(int, membarrier, int, cmd, int, flags)
  319. #endif
  320. static bitmask_transtbl fcntl_flags_tbl[] = {
  321. { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
  322. { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
  323. { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
  324. { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
  325. { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
  326. { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
  327. { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
  328. { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
  329. { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
  330. { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
  331. { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
  332. { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
  333. { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
  334. #if defined(O_DIRECT)
  335. { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
  336. #endif
  337. #if defined(O_NOATIME)
  338. { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
  339. #endif
  340. #if defined(O_CLOEXEC)
  341. { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
  342. #endif
  343. #if defined(O_PATH)
  344. { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
  345. #endif
  346. #if defined(O_TMPFILE)
  347. { TARGET_O_TMPFILE, TARGET_O_TMPFILE, O_TMPFILE, O_TMPFILE },
  348. #endif
  349. /* Don't terminate the list prematurely on 64-bit host+guest. */
  350. #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
  351. { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
  352. #endif
  353. { 0, 0, 0, 0 }
  354. };
  355. static int sys_getcwd1(char *buf, size_t size)
  356. {
  357. if (getcwd(buf, size) == NULL) {
  358. /* getcwd() sets errno */
  359. return (-1);
  360. }
  361. return strlen(buf)+1;
  362. }
  363. #ifdef TARGET_NR_utimensat
  364. #if defined(__NR_utimensat)
  365. #define __NR_sys_utimensat __NR_utimensat
  366. _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
  367. const struct timespec *,tsp,int,flags)
  368. #else
  369. static int sys_utimensat(int dirfd, const char *pathname,
  370. const struct timespec times[2], int flags)
  371. {
  372. errno = ENOSYS;
  373. return -1;
  374. }
  375. #endif
  376. #endif /* TARGET_NR_utimensat */
  377. #ifdef TARGET_NR_renameat2
  378. #if defined(__NR_renameat2)
  379. #define __NR_sys_renameat2 __NR_renameat2
  380. _syscall5(int, sys_renameat2, int, oldfd, const char *, old, int, newfd,
  381. const char *, new, unsigned int, flags)
  382. #else
  383. static int sys_renameat2(int oldfd, const char *old,
  384. int newfd, const char *new, int flags)
  385. {
  386. if (flags == 0) {
  387. return renameat(oldfd, old, newfd, new);
  388. }
  389. errno = ENOSYS;
  390. return -1;
  391. }
  392. #endif
  393. #endif /* TARGET_NR_renameat2 */
  394. #ifdef CONFIG_INOTIFY
  395. #include <sys/inotify.h>
  396. #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
  397. static int sys_inotify_init(void)
  398. {
  399. return (inotify_init());
  400. }
  401. #endif
  402. #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
  403. static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
  404. {
  405. return (inotify_add_watch(fd, pathname, mask));
  406. }
  407. #endif
  408. #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
  409. static int sys_inotify_rm_watch(int fd, int32_t wd)
  410. {
  411. return (inotify_rm_watch(fd, wd));
  412. }
  413. #endif
  414. #ifdef CONFIG_INOTIFY1
  415. #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
  416. static int sys_inotify_init1(int flags)
  417. {
  418. return (inotify_init1(flags));
  419. }
  420. #endif
  421. #endif
  422. #else
  423. /* Userspace can usually survive runtime without inotify */
  424. #undef TARGET_NR_inotify_init
  425. #undef TARGET_NR_inotify_init1
  426. #undef TARGET_NR_inotify_add_watch
  427. #undef TARGET_NR_inotify_rm_watch
  428. #endif /* CONFIG_INOTIFY */
  429. #if defined(TARGET_NR_prlimit64)
  430. #ifndef __NR_prlimit64
  431. # define __NR_prlimit64 -1
  432. #endif
  433. #define __NR_sys_prlimit64 __NR_prlimit64
  434. /* The glibc rlimit structure may not be that used by the underlying syscall */
  435. struct host_rlimit64 {
  436. uint64_t rlim_cur;
  437. uint64_t rlim_max;
  438. };
  439. _syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
  440. const struct host_rlimit64 *, new_limit,
  441. struct host_rlimit64 *, old_limit)
  442. #endif
  443. #if defined(TARGET_NR_timer_create)
  444. /* Maxiumum of 32 active POSIX timers allowed at any one time. */
  445. static timer_t g_posix_timers[32] = { 0, } ;
  446. static inline int next_free_host_timer(void)
  447. {
  448. int k ;
  449. /* FIXME: Does finding the next free slot require a lock? */
  450. for (k = 0; k < ARRAY_SIZE(g_posix_timers); k++) {
  451. if (g_posix_timers[k] == 0) {
  452. g_posix_timers[k] = (timer_t) 1;
  453. return k;
  454. }
  455. }
  456. return -1;
  457. }
  458. #endif
  459. /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
  460. #ifdef TARGET_ARM
  461. static inline int regpairs_aligned(void *cpu_env, int num)
  462. {
  463. return ((((CPUARMState *)cpu_env)->eabi) == 1) ;
  464. }
  465. #elif defined(TARGET_MIPS) && (TARGET_ABI_BITS == 32)
  466. static inline int regpairs_aligned(void *cpu_env, int num) { return 1; }
  467. #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
  468. /* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
  469. * of registers which translates to the same as ARM/MIPS, because we start with
  470. * r3 as arg1 */
  471. static inline int regpairs_aligned(void *cpu_env, int num) { return 1; }
  472. #elif defined(TARGET_SH4)
  473. /* SH4 doesn't align register pairs, except for p{read,write}64 */
  474. static inline int regpairs_aligned(void *cpu_env, int num)
  475. {
  476. switch (num) {
  477. case TARGET_NR_pread64:
  478. case TARGET_NR_pwrite64:
  479. return 1;
  480. default:
  481. return 0;
  482. }
  483. }
  484. #elif defined(TARGET_XTENSA)
  485. static inline int regpairs_aligned(void *cpu_env, int num) { return 1; }
  486. #else
  487. static inline int regpairs_aligned(void *cpu_env, int num) { return 0; }
  488. #endif
  489. #define ERRNO_TABLE_SIZE 1200
  490. /* target_to_host_errno_table[] is initialized from
  491. * host_to_target_errno_table[] in syscall_init(). */
  492. static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
  493. };
  494. /*
  495. * This list is the union of errno values overridden in asm-<arch>/errno.h
  496. * minus the errnos that are not actually generic to all archs.
  497. */
  498. static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
  499. [EAGAIN] = TARGET_EAGAIN,
  500. [EIDRM] = TARGET_EIDRM,
  501. [ECHRNG] = TARGET_ECHRNG,
  502. [EL2NSYNC] = TARGET_EL2NSYNC,
  503. [EL3HLT] = TARGET_EL3HLT,
  504. [EL3RST] = TARGET_EL3RST,
  505. [ELNRNG] = TARGET_ELNRNG,
  506. [EUNATCH] = TARGET_EUNATCH,
  507. [ENOCSI] = TARGET_ENOCSI,
  508. [EL2HLT] = TARGET_EL2HLT,
  509. [EDEADLK] = TARGET_EDEADLK,
  510. [ENOLCK] = TARGET_ENOLCK,
  511. [EBADE] = TARGET_EBADE,
  512. [EBADR] = TARGET_EBADR,
  513. [EXFULL] = TARGET_EXFULL,
  514. [ENOANO] = TARGET_ENOANO,
  515. [EBADRQC] = TARGET_EBADRQC,
  516. [EBADSLT] = TARGET_EBADSLT,
  517. [EBFONT] = TARGET_EBFONT,
  518. [ENOSTR] = TARGET_ENOSTR,
  519. [ENODATA] = TARGET_ENODATA,
  520. [ETIME] = TARGET_ETIME,
  521. [ENOSR] = TARGET_ENOSR,
  522. [ENONET] = TARGET_ENONET,
  523. [ENOPKG] = TARGET_ENOPKG,
  524. [EREMOTE] = TARGET_EREMOTE,
  525. [ENOLINK] = TARGET_ENOLINK,
  526. [EADV] = TARGET_EADV,
  527. [ESRMNT] = TARGET_ESRMNT,
  528. [ECOMM] = TARGET_ECOMM,
  529. [EPROTO] = TARGET_EPROTO,
  530. [EDOTDOT] = TARGET_EDOTDOT,
  531. [EMULTIHOP] = TARGET_EMULTIHOP,
  532. [EBADMSG] = TARGET_EBADMSG,
  533. [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
  534. [EOVERFLOW] = TARGET_EOVERFLOW,
  535. [ENOTUNIQ] = TARGET_ENOTUNIQ,
  536. [EBADFD] = TARGET_EBADFD,
  537. [EREMCHG] = TARGET_EREMCHG,
  538. [ELIBACC] = TARGET_ELIBACC,
  539. [ELIBBAD] = TARGET_ELIBBAD,
  540. [ELIBSCN] = TARGET_ELIBSCN,
  541. [ELIBMAX] = TARGET_ELIBMAX,
  542. [ELIBEXEC] = TARGET_ELIBEXEC,
  543. [EILSEQ] = TARGET_EILSEQ,
  544. [ENOSYS] = TARGET_ENOSYS,
  545. [ELOOP] = TARGET_ELOOP,
  546. [ERESTART] = TARGET_ERESTART,
  547. [ESTRPIPE] = TARGET_ESTRPIPE,
  548. [ENOTEMPTY] = TARGET_ENOTEMPTY,
  549. [EUSERS] = TARGET_EUSERS,
  550. [ENOTSOCK] = TARGET_ENOTSOCK,
  551. [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
  552. [EMSGSIZE] = TARGET_EMSGSIZE,
  553. [EPROTOTYPE] = TARGET_EPROTOTYPE,
  554. [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
  555. [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
  556. [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
  557. [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
  558. [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
  559. [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
  560. [EADDRINUSE] = TARGET_EADDRINUSE,
  561. [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
  562. [ENETDOWN] = TARGET_ENETDOWN,
  563. [ENETUNREACH] = TARGET_ENETUNREACH,
  564. [ENETRESET] = TARGET_ENETRESET,
  565. [ECONNABORTED] = TARGET_ECONNABORTED,
  566. [ECONNRESET] = TARGET_ECONNRESET,
  567. [ENOBUFS] = TARGET_ENOBUFS,
  568. [EISCONN] = TARGET_EISCONN,
  569. [ENOTCONN] = TARGET_ENOTCONN,
  570. [EUCLEAN] = TARGET_EUCLEAN,
  571. [ENOTNAM] = TARGET_ENOTNAM,
  572. [ENAVAIL] = TARGET_ENAVAIL,
  573. [EISNAM] = TARGET_EISNAM,
  574. [EREMOTEIO] = TARGET_EREMOTEIO,
  575. [EDQUOT] = TARGET_EDQUOT,
  576. [ESHUTDOWN] = TARGET_ESHUTDOWN,
  577. [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
  578. [ETIMEDOUT] = TARGET_ETIMEDOUT,
  579. [ECONNREFUSED] = TARGET_ECONNREFUSED,
  580. [EHOSTDOWN] = TARGET_EHOSTDOWN,
  581. [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
  582. [EALREADY] = TARGET_EALREADY,
  583. [EINPROGRESS] = TARGET_EINPROGRESS,
  584. [ESTALE] = TARGET_ESTALE,
  585. [ECANCELED] = TARGET_ECANCELED,
  586. [ENOMEDIUM] = TARGET_ENOMEDIUM,
  587. [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
  588. #ifdef ENOKEY
  589. [ENOKEY] = TARGET_ENOKEY,
  590. #endif
  591. #ifdef EKEYEXPIRED
  592. [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
  593. #endif
  594. #ifdef EKEYREVOKED
  595. [EKEYREVOKED] = TARGET_EKEYREVOKED,
  596. #endif
  597. #ifdef EKEYREJECTED
  598. [EKEYREJECTED] = TARGET_EKEYREJECTED,
  599. #endif
  600. #ifdef EOWNERDEAD
  601. [EOWNERDEAD] = TARGET_EOWNERDEAD,
  602. #endif
  603. #ifdef ENOTRECOVERABLE
  604. [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
  605. #endif
  606. #ifdef ENOMSG
  607. [ENOMSG] = TARGET_ENOMSG,
  608. #endif
  609. #ifdef ERKFILL
  610. [ERFKILL] = TARGET_ERFKILL,
  611. #endif
  612. #ifdef EHWPOISON
  613. [EHWPOISON] = TARGET_EHWPOISON,
  614. #endif
  615. };
  616. static inline int host_to_target_errno(int err)
  617. {
  618. if (err >= 0 && err < ERRNO_TABLE_SIZE &&
  619. host_to_target_errno_table[err]) {
  620. return host_to_target_errno_table[err];
  621. }
  622. return err;
  623. }
  624. static inline int target_to_host_errno(int err)
  625. {
  626. if (err >= 0 && err < ERRNO_TABLE_SIZE &&
  627. target_to_host_errno_table[err]) {
  628. return target_to_host_errno_table[err];
  629. }
  630. return err;
  631. }
  632. static inline abi_long get_errno(abi_long ret)
  633. {
  634. if (ret == -1)
  635. return -host_to_target_errno(errno);
  636. else
  637. return ret;
  638. }
  639. const char *target_strerror(int err)
  640. {
  641. if (err == TARGET_ERESTARTSYS) {
  642. return "To be restarted";
  643. }
  644. if (err == TARGET_QEMU_ESIGRETURN) {
  645. return "Successful exit from sigreturn";
  646. }
  647. if ((err >= ERRNO_TABLE_SIZE) || (err < 0)) {
  648. return NULL;
  649. }
  650. return strerror(target_to_host_errno(err));
  651. }
  652. #define safe_syscall0(type, name) \
  653. static type safe_##name(void) \
  654. { \
  655. return safe_syscall(__NR_##name); \
  656. }
  657. #define safe_syscall1(type, name, type1, arg1) \
  658. static type safe_##name(type1 arg1) \
  659. { \
  660. return safe_syscall(__NR_##name, arg1); \
  661. }
  662. #define safe_syscall2(type, name, type1, arg1, type2, arg2) \
  663. static type safe_##name(type1 arg1, type2 arg2) \
  664. { \
  665. return safe_syscall(__NR_##name, arg1, arg2); \
  666. }
  667. #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
  668. static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
  669. { \
  670. return safe_syscall(__NR_##name, arg1, arg2, arg3); \
  671. }
  672. #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
  673. type4, arg4) \
  674. static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
  675. { \
  676. return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
  677. }
  678. #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
  679. type4, arg4, type5, arg5) \
  680. static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  681. type5 arg5) \
  682. { \
  683. return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
  684. }
  685. #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
  686. type4, arg4, type5, arg5, type6, arg6) \
  687. static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  688. type5 arg5, type6 arg6) \
  689. { \
  690. return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
  691. }
  692. safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count)
  693. safe_syscall3(ssize_t, write, int, fd, const void *, buff, size_t, count)
  694. safe_syscall4(int, openat, int, dirfd, const char *, pathname, \
  695. int, flags, mode_t, mode)
  696. #if defined(TARGET_NR_wait4) || defined(TARGET_NR_waitpid)
  697. safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \
  698. struct rusage *, rusage)
  699. #endif
  700. safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \
  701. int, options, struct rusage *, rusage)
  702. safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp)
  703. #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
  704. defined(TARGET_NR_pselect6)
  705. safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \
  706. fd_set *, exceptfds, struct timespec *, timeout, void *, sig)
  707. #endif
  708. #if defined(TARGET_NR_ppoll) || defined(TARGET_NR_poll)
  709. safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds,
  710. struct timespec *, tsp, const sigset_t *, sigmask,
  711. size_t, sigsetsize)
  712. #endif
  713. safe_syscall6(int, epoll_pwait, int, epfd, struct epoll_event *, events,
  714. int, maxevents, int, timeout, const sigset_t *, sigmask,
  715. size_t, sigsetsize)
  716. #if defined(__NR_futex)
  717. safe_syscall6(int,futex,int *,uaddr,int,op,int,val, \
  718. const struct timespec *,timeout,int *,uaddr2,int,val3)
  719. #endif
  720. #if defined(__NR_futex_time64)
  721. safe_syscall6(int,futex_time64,int *,uaddr,int,op,int,val, \
  722. const struct timespec *,timeout,int *,uaddr2,int,val3)
  723. #endif
  724. safe_syscall2(int, rt_sigsuspend, sigset_t *, newset, size_t, sigsetsize)
  725. safe_syscall2(int, kill, pid_t, pid, int, sig)
  726. safe_syscall2(int, tkill, int, tid, int, sig)
  727. safe_syscall3(int, tgkill, int, tgid, int, pid, int, sig)
  728. safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt)
  729. safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt)
  730. safe_syscall5(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt,
  731. unsigned long, pos_l, unsigned long, pos_h)
  732. safe_syscall5(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, iovcnt,
  733. unsigned long, pos_l, unsigned long, pos_h)
  734. safe_syscall3(int, connect, int, fd, const struct sockaddr *, addr,
  735. socklen_t, addrlen)
  736. safe_syscall6(ssize_t, sendto, int, fd, const void *, buf, size_t, len,
  737. int, flags, const struct sockaddr *, addr, socklen_t, addrlen)
  738. safe_syscall6(ssize_t, recvfrom, int, fd, void *, buf, size_t, len,
  739. int, flags, struct sockaddr *, addr, socklen_t *, addrlen)
  740. safe_syscall3(ssize_t, sendmsg, int, fd, const struct msghdr *, msg, int, flags)
  741. safe_syscall3(ssize_t, recvmsg, int, fd, struct msghdr *, msg, int, flags)
  742. safe_syscall2(int, flock, int, fd, int, operation)
  743. #ifdef TARGET_NR_rt_sigtimedwait
  744. safe_syscall4(int, rt_sigtimedwait, const sigset_t *, these, siginfo_t *, uinfo,
  745. const struct timespec *, uts, size_t, sigsetsize)
  746. #endif
  747. safe_syscall4(int, accept4, int, fd, struct sockaddr *, addr, socklen_t *, len,
  748. int, flags)
  749. #if defined(TARGET_NR_nanosleep)
  750. safe_syscall2(int, nanosleep, const struct timespec *, req,
  751. struct timespec *, rem)
  752. #endif
  753. #ifdef TARGET_NR_clock_nanosleep
  754. safe_syscall4(int, clock_nanosleep, const clockid_t, clock, int, flags,
  755. const struct timespec *, req, struct timespec *, rem)
  756. #endif
  757. #ifdef __NR_ipc
  758. safe_syscall6(int, ipc, int, call, long, first, long, second, long, third,
  759. void *, ptr, long, fifth)
  760. #endif
  761. #ifdef __NR_msgsnd
  762. safe_syscall4(int, msgsnd, int, msgid, const void *, msgp, size_t, sz,
  763. int, flags)
  764. #endif
  765. #ifdef __NR_msgrcv
  766. safe_syscall5(int, msgrcv, int, msgid, void *, msgp, size_t, sz,
  767. long, msgtype, int, flags)
  768. #endif
  769. #ifdef __NR_semtimedop
  770. safe_syscall4(int, semtimedop, int, semid, struct sembuf *, tsops,
  771. unsigned, nsops, const struct timespec *, timeout)
  772. #endif
  773. #ifdef TARGET_NR_mq_timedsend
  774. safe_syscall5(int, mq_timedsend, int, mqdes, const char *, msg_ptr,
  775. size_t, len, unsigned, prio, const struct timespec *, timeout)
  776. #endif
  777. #ifdef TARGET_NR_mq_timedreceive
  778. safe_syscall5(int, mq_timedreceive, int, mqdes, char *, msg_ptr,
  779. size_t, len, unsigned *, prio, const struct timespec *, timeout)
  780. #endif
  781. /* We do ioctl like this rather than via safe_syscall3 to preserve the
  782. * "third argument might be integer or pointer or not present" behaviour of
  783. * the libc function.
  784. */
  785. #define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
  786. /* Similarly for fcntl. Note that callers must always:
  787. * pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK
  788. * use the flock64 struct rather than unsuffixed flock
  789. * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts.
  790. */
  791. #ifdef __NR_fcntl64
  792. #define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
  793. #else
  794. #define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
  795. #endif
  796. static inline int host_to_target_sock_type(int host_type)
  797. {
  798. int target_type;
  799. switch (host_type & 0xf /* SOCK_TYPE_MASK */) {
  800. case SOCK_DGRAM:
  801. target_type = TARGET_SOCK_DGRAM;
  802. break;
  803. case SOCK_STREAM:
  804. target_type = TARGET_SOCK_STREAM;
  805. break;
  806. default:
  807. target_type = host_type & 0xf /* SOCK_TYPE_MASK */;
  808. break;
  809. }
  810. #if defined(SOCK_CLOEXEC)
  811. if (host_type & SOCK_CLOEXEC) {
  812. target_type |= TARGET_SOCK_CLOEXEC;
  813. }
  814. #endif
  815. #if defined(SOCK_NONBLOCK)
  816. if (host_type & SOCK_NONBLOCK) {
  817. target_type |= TARGET_SOCK_NONBLOCK;
  818. }
  819. #endif
  820. return target_type;
  821. }
  822. static abi_ulong target_brk;
  823. static abi_ulong target_original_brk;
  824. static abi_ulong brk_page;
  825. void target_set_brk(abi_ulong new_brk)
  826. {
  827. target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
  828. brk_page = HOST_PAGE_ALIGN(target_brk);
  829. }
  830. //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
  831. #define DEBUGF_BRK(message, args...)
  832. /* do_brk() must return target values and target errnos. */
  833. abi_long do_brk(abi_ulong new_brk)
  834. {
  835. abi_long mapped_addr;
  836. abi_ulong new_alloc_size;
  837. DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
  838. if (!new_brk) {
  839. DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
  840. return target_brk;
  841. }
  842. if (new_brk < target_original_brk) {
  843. DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
  844. target_brk);
  845. return target_brk;
  846. }
  847. /* If the new brk is less than the highest page reserved to the
  848. * target heap allocation, set it and we're almost done... */
  849. if (new_brk <= brk_page) {
  850. /* Heap contents are initialized to zero, as for anonymous
  851. * mapped pages. */
  852. if (new_brk > target_brk) {
  853. memset(g2h(target_brk), 0, new_brk - target_brk);
  854. }
  855. target_brk = new_brk;
  856. DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
  857. return target_brk;
  858. }
  859. /* We need to allocate more memory after the brk... Note that
  860. * we don't use MAP_FIXED because that will map over the top of
  861. * any existing mapping (like the one with the host libc or qemu
  862. * itself); instead we treat "mapped but at wrong address" as
  863. * a failure and unmap again.
  864. */
  865. new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
  866. mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
  867. PROT_READ|PROT_WRITE,
  868. MAP_ANON|MAP_PRIVATE, 0, 0));
  869. if (mapped_addr == brk_page) {
  870. /* Heap contents are initialized to zero, as for anonymous
  871. * mapped pages. Technically the new pages are already
  872. * initialized to zero since they *are* anonymous mapped
  873. * pages, however we have to take care with the contents that
  874. * come from the remaining part of the previous page: it may
  875. * contains garbage data due to a previous heap usage (grown
  876. * then shrunken). */
  877. memset(g2h(target_brk), 0, brk_page - target_brk);
  878. target_brk = new_brk;
  879. brk_page = HOST_PAGE_ALIGN(target_brk);
  880. DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
  881. target_brk);
  882. return target_brk;
  883. } else if (mapped_addr != -1) {
  884. /* Mapped but at wrong address, meaning there wasn't actually
  885. * enough space for this brk.
  886. */
  887. target_munmap(mapped_addr, new_alloc_size);
  888. mapped_addr = -1;
  889. DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
  890. }
  891. else {
  892. DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
  893. }
  894. #if defined(TARGET_ALPHA)
  895. /* We (partially) emulate OSF/1 on Alpha, which requires we
  896. return a proper errno, not an unchanged brk value. */
  897. return -TARGET_ENOMEM;
  898. #endif
  899. /* For everything else, return the previous break. */
  900. return target_brk;
  901. }
  902. #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
  903. defined(TARGET_NR_pselect6)
  904. static inline abi_long copy_from_user_fdset(fd_set *fds,
  905. abi_ulong target_fds_addr,
  906. int n)
  907. {
  908. int i, nw, j, k;
  909. abi_ulong b, *target_fds;
  910. nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
  911. if (!(target_fds = lock_user(VERIFY_READ,
  912. target_fds_addr,
  913. sizeof(abi_ulong) * nw,
  914. 1)))
  915. return -TARGET_EFAULT;
  916. FD_ZERO(fds);
  917. k = 0;
  918. for (i = 0; i < nw; i++) {
  919. /* grab the abi_ulong */
  920. __get_user(b, &target_fds[i]);
  921. for (j = 0; j < TARGET_ABI_BITS; j++) {
  922. /* check the bit inside the abi_ulong */
  923. if ((b >> j) & 1)
  924. FD_SET(k, fds);
  925. k++;
  926. }
  927. }
  928. unlock_user(target_fds, target_fds_addr, 0);
  929. return 0;
  930. }
  931. static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
  932. abi_ulong target_fds_addr,
  933. int n)
  934. {
  935. if (target_fds_addr) {
  936. if (copy_from_user_fdset(fds, target_fds_addr, n))
  937. return -TARGET_EFAULT;
  938. *fds_ptr = fds;
  939. } else {
  940. *fds_ptr = NULL;
  941. }
  942. return 0;
  943. }
  944. static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
  945. const fd_set *fds,
  946. int n)
  947. {
  948. int i, nw, j, k;
  949. abi_long v;
  950. abi_ulong *target_fds;
  951. nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
  952. if (!(target_fds = lock_user(VERIFY_WRITE,
  953. target_fds_addr,
  954. sizeof(abi_ulong) * nw,
  955. 0)))
  956. return -TARGET_EFAULT;
  957. k = 0;
  958. for (i = 0; i < nw; i++) {
  959. v = 0;
  960. for (j = 0; j < TARGET_ABI_BITS; j++) {
  961. v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
  962. k++;
  963. }
  964. __put_user(v, &target_fds[i]);
  965. }
  966. unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
  967. return 0;
  968. }
  969. #endif
  970. #if defined(__alpha__)
  971. #define HOST_HZ 1024
  972. #else
  973. #define HOST_HZ 100
  974. #endif
  975. static inline abi_long host_to_target_clock_t(long ticks)
  976. {
  977. #if HOST_HZ == TARGET_HZ
  978. return ticks;
  979. #else
  980. return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
  981. #endif
  982. }
  983. static inline abi_long host_to_target_rusage(abi_ulong target_addr,
  984. const struct rusage *rusage)
  985. {
  986. struct target_rusage *target_rusage;
  987. if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
  988. return -TARGET_EFAULT;
  989. target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
  990. target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
  991. target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
  992. target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
  993. target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
  994. target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
  995. target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
  996. target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
  997. target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
  998. target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
  999. target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
  1000. target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
  1001. target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
  1002. target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
  1003. target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
  1004. target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
  1005. target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
  1006. target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
  1007. unlock_user_struct(target_rusage, target_addr, 1);
  1008. return 0;
  1009. }
  1010. #ifdef TARGET_NR_setrlimit
  1011. static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
  1012. {
  1013. abi_ulong target_rlim_swap;
  1014. rlim_t result;
  1015. target_rlim_swap = tswapal(target_rlim);
  1016. if (target_rlim_swap == TARGET_RLIM_INFINITY)
  1017. return RLIM_INFINITY;
  1018. result = target_rlim_swap;
  1019. if (target_rlim_swap != (rlim_t)result)
  1020. return RLIM_INFINITY;
  1021. return result;
  1022. }
  1023. #endif
  1024. #if defined(TARGET_NR_getrlimit) || defined(TARGET_NR_ugetrlimit)
  1025. static inline abi_ulong host_to_target_rlim(rlim_t rlim)
  1026. {
  1027. abi_ulong target_rlim_swap;
  1028. abi_ulong result;
  1029. if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
  1030. target_rlim_swap = TARGET_RLIM_INFINITY;
  1031. else
  1032. target_rlim_swap = rlim;
  1033. result = tswapal(target_rlim_swap);
  1034. return result;
  1035. }
  1036. #endif
  1037. static inline int target_to_host_resource(int code)
  1038. {
  1039. switch (code) {
  1040. case TARGET_RLIMIT_AS:
  1041. return RLIMIT_AS;
  1042. case TARGET_RLIMIT_CORE:
  1043. return RLIMIT_CORE;
  1044. case TARGET_RLIMIT_CPU:
  1045. return RLIMIT_CPU;
  1046. case TARGET_RLIMIT_DATA:
  1047. return RLIMIT_DATA;
  1048. case TARGET_RLIMIT_FSIZE:
  1049. return RLIMIT_FSIZE;
  1050. case TARGET_RLIMIT_LOCKS:
  1051. return RLIMIT_LOCKS;
  1052. case TARGET_RLIMIT_MEMLOCK:
  1053. return RLIMIT_MEMLOCK;
  1054. case TARGET_RLIMIT_MSGQUEUE:
  1055. return RLIMIT_MSGQUEUE;
  1056. case TARGET_RLIMIT_NICE:
  1057. return RLIMIT_NICE;
  1058. case TARGET_RLIMIT_NOFILE:
  1059. return RLIMIT_NOFILE;
  1060. case TARGET_RLIMIT_NPROC:
  1061. return RLIMIT_NPROC;
  1062. case TARGET_RLIMIT_RSS:
  1063. return RLIMIT_RSS;
  1064. case TARGET_RLIMIT_RTPRIO:
  1065. return RLIMIT_RTPRIO;
  1066. case TARGET_RLIMIT_SIGPENDING:
  1067. return RLIMIT_SIGPENDING;
  1068. case TARGET_RLIMIT_STACK:
  1069. return RLIMIT_STACK;
  1070. default:
  1071. return code;
  1072. }
  1073. }
  1074. static inline abi_long copy_from_user_timeval(struct timeval *tv,
  1075. abi_ulong target_tv_addr)
  1076. {
  1077. struct target_timeval *target_tv;
  1078. if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) {
  1079. return -TARGET_EFAULT;
  1080. }
  1081. __get_user(tv->tv_sec, &target_tv->tv_sec);
  1082. __get_user(tv->tv_usec, &target_tv->tv_usec);
  1083. unlock_user_struct(target_tv, target_tv_addr, 0);
  1084. return 0;
  1085. }
  1086. static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
  1087. const struct timeval *tv)
  1088. {
  1089. struct target_timeval *target_tv;
  1090. if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
  1091. return -TARGET_EFAULT;
  1092. }
  1093. __put_user(tv->tv_sec, &target_tv->tv_sec);
  1094. __put_user(tv->tv_usec, &target_tv->tv_usec);
  1095. unlock_user_struct(target_tv, target_tv_addr, 1);
  1096. return 0;
  1097. }
  1098. static inline abi_long copy_to_user_timeval64(abi_ulong target_tv_addr,
  1099. const struct timeval *tv)
  1100. {
  1101. struct target__kernel_sock_timeval *target_tv;
  1102. if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
  1103. return -TARGET_EFAULT;
  1104. }
  1105. __put_user(tv->tv_sec, &target_tv->tv_sec);
  1106. __put_user(tv->tv_usec, &target_tv->tv_usec);
  1107. unlock_user_struct(target_tv, target_tv_addr, 1);
  1108. return 0;
  1109. }
  1110. #if defined(TARGET_NR_futex) || \
  1111. defined(TARGET_NR_rt_sigtimedwait) || \
  1112. defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6) || \
  1113. defined(TARGET_NR_nanosleep) || defined(TARGET_NR_clock_settime) || \
  1114. defined(TARGET_NR_utimensat) || defined(TARGET_NR_mq_timedsend) || \
  1115. defined(TARGET_NR_mq_timedreceive)
  1116. static inline abi_long target_to_host_timespec(struct timespec *host_ts,
  1117. abi_ulong target_addr)
  1118. {
  1119. struct target_timespec *target_ts;
  1120. if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
  1121. return -TARGET_EFAULT;
  1122. }
  1123. __get_user(host_ts->tv_sec, &target_ts->tv_sec);
  1124. __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1125. unlock_user_struct(target_ts, target_addr, 0);
  1126. return 0;
  1127. }
  1128. #endif
  1129. #if defined(TARGET_NR_clock_settime64) || defined(TARGET_NR_futex_time64)
  1130. static inline abi_long target_to_host_timespec64(struct timespec *host_ts,
  1131. abi_ulong target_addr)
  1132. {
  1133. struct target__kernel_timespec *target_ts;
  1134. if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
  1135. return -TARGET_EFAULT;
  1136. }
  1137. __get_user(host_ts->tv_sec, &target_ts->tv_sec);
  1138. __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1139. unlock_user_struct(target_ts, target_addr, 0);
  1140. return 0;
  1141. }
  1142. #endif
  1143. static inline abi_long host_to_target_timespec(abi_ulong target_addr,
  1144. struct timespec *host_ts)
  1145. {
  1146. struct target_timespec *target_ts;
  1147. if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
  1148. return -TARGET_EFAULT;
  1149. }
  1150. __put_user(host_ts->tv_sec, &target_ts->tv_sec);
  1151. __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1152. unlock_user_struct(target_ts, target_addr, 1);
  1153. return 0;
  1154. }
  1155. static inline abi_long host_to_target_timespec64(abi_ulong target_addr,
  1156. struct timespec *host_ts)
  1157. {
  1158. struct target__kernel_timespec *target_ts;
  1159. if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
  1160. return -TARGET_EFAULT;
  1161. }
  1162. __put_user(host_ts->tv_sec, &target_ts->tv_sec);
  1163. __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
  1164. unlock_user_struct(target_ts, target_addr, 1);
  1165. return 0;
  1166. }
  1167. #if defined(TARGET_NR_gettimeofday)
  1168. static inline abi_long copy_to_user_timezone(abi_ulong target_tz_addr,
  1169. struct timezone *tz)
  1170. {
  1171. struct target_timezone *target_tz;
  1172. if (!lock_user_struct(VERIFY_WRITE, target_tz, target_tz_addr, 1)) {
  1173. return -TARGET_EFAULT;
  1174. }
  1175. __put_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
  1176. __put_user(tz->tz_dsttime, &target_tz->tz_dsttime);
  1177. unlock_user_struct(target_tz, target_tz_addr, 1);
  1178. return 0;
  1179. }
  1180. #endif
  1181. #if defined(TARGET_NR_settimeofday)
  1182. static inline abi_long copy_from_user_timezone(struct timezone *tz,
  1183. abi_ulong target_tz_addr)
  1184. {
  1185. struct target_timezone *target_tz;
  1186. if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) {
  1187. return -TARGET_EFAULT;
  1188. }
  1189. __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
  1190. __get_user(tz->tz_dsttime, &target_tz->tz_dsttime);
  1191. unlock_user_struct(target_tz, target_tz_addr, 0);
  1192. return 0;
  1193. }
  1194. #endif
  1195. #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
  1196. #include <mqueue.h>
  1197. static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
  1198. abi_ulong target_mq_attr_addr)
  1199. {
  1200. struct target_mq_attr *target_mq_attr;
  1201. if (!lock_user_struct(VERIFY_READ, target_mq_attr,
  1202. target_mq_attr_addr, 1))
  1203. return -TARGET_EFAULT;
  1204. __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
  1205. __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
  1206. __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
  1207. __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
  1208. unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
  1209. return 0;
  1210. }
  1211. static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
  1212. const struct mq_attr *attr)
  1213. {
  1214. struct target_mq_attr *target_mq_attr;
  1215. if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
  1216. target_mq_attr_addr, 0))
  1217. return -TARGET_EFAULT;
  1218. __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
  1219. __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
  1220. __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
  1221. __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
  1222. unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
  1223. return 0;
  1224. }
  1225. #endif
  1226. #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
  1227. /* do_select() must return target values and target errnos. */
  1228. static abi_long do_select(int n,
  1229. abi_ulong rfd_addr, abi_ulong wfd_addr,
  1230. abi_ulong efd_addr, abi_ulong target_tv_addr)
  1231. {
  1232. fd_set rfds, wfds, efds;
  1233. fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
  1234. struct timeval tv;
  1235. struct timespec ts, *ts_ptr;
  1236. abi_long ret;
  1237. ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
  1238. if (ret) {
  1239. return ret;
  1240. }
  1241. ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
  1242. if (ret) {
  1243. return ret;
  1244. }
  1245. ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
  1246. if (ret) {
  1247. return ret;
  1248. }
  1249. if (target_tv_addr) {
  1250. if (copy_from_user_timeval(&tv, target_tv_addr))
  1251. return -TARGET_EFAULT;
  1252. ts.tv_sec = tv.tv_sec;
  1253. ts.tv_nsec = tv.tv_usec * 1000;
  1254. ts_ptr = &ts;
  1255. } else {
  1256. ts_ptr = NULL;
  1257. }
  1258. ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
  1259. ts_ptr, NULL));
  1260. if (!is_error(ret)) {
  1261. if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
  1262. return -TARGET_EFAULT;
  1263. if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
  1264. return -TARGET_EFAULT;
  1265. if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
  1266. return -TARGET_EFAULT;
  1267. if (target_tv_addr) {
  1268. tv.tv_sec = ts.tv_sec;
  1269. tv.tv_usec = ts.tv_nsec / 1000;
  1270. if (copy_to_user_timeval(target_tv_addr, &tv)) {
  1271. return -TARGET_EFAULT;
  1272. }
  1273. }
  1274. }
  1275. return ret;
  1276. }
  1277. #if defined(TARGET_WANT_OLD_SYS_SELECT)
  1278. static abi_long do_old_select(abi_ulong arg1)
  1279. {
  1280. struct target_sel_arg_struct *sel;
  1281. abi_ulong inp, outp, exp, tvp;
  1282. long nsel;
  1283. if (!lock_user_struct(VERIFY_READ, sel, arg1, 1)) {
  1284. return -TARGET_EFAULT;
  1285. }
  1286. nsel = tswapal(sel->n);
  1287. inp = tswapal(sel->inp);
  1288. outp = tswapal(sel->outp);
  1289. exp = tswapal(sel->exp);
  1290. tvp = tswapal(sel->tvp);
  1291. unlock_user_struct(sel, arg1, 0);
  1292. return do_select(nsel, inp, outp, exp, tvp);
  1293. }
  1294. #endif
  1295. #endif
  1296. static abi_long do_pipe2(int host_pipe[], int flags)
  1297. {
  1298. #ifdef CONFIG_PIPE2
  1299. return pipe2(host_pipe, flags);
  1300. #else
  1301. return -ENOSYS;
  1302. #endif
  1303. }
  1304. static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
  1305. int flags, int is_pipe2)
  1306. {
  1307. int host_pipe[2];
  1308. abi_long ret;
  1309. ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
  1310. if (is_error(ret))
  1311. return get_errno(ret);
  1312. /* Several targets have special calling conventions for the original
  1313. pipe syscall, but didn't replicate this into the pipe2 syscall. */
  1314. if (!is_pipe2) {
  1315. #if defined(TARGET_ALPHA)
  1316. ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
  1317. return host_pipe[0];
  1318. #elif defined(TARGET_MIPS)
  1319. ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
  1320. return host_pipe[0];
  1321. #elif defined(TARGET_SH4)
  1322. ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
  1323. return host_pipe[0];
  1324. #elif defined(TARGET_SPARC)
  1325. ((CPUSPARCState*)cpu_env)->regwptr[1] = host_pipe[1];
  1326. return host_pipe[0];
  1327. #endif
  1328. }
  1329. if (put_user_s32(host_pipe[0], pipedes)
  1330. || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
  1331. return -TARGET_EFAULT;
  1332. return get_errno(ret);
  1333. }
  1334. static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
  1335. abi_ulong target_addr,
  1336. socklen_t len)
  1337. {
  1338. struct target_ip_mreqn *target_smreqn;
  1339. target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
  1340. if (!target_smreqn)
  1341. return -TARGET_EFAULT;
  1342. mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
  1343. mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
  1344. if (len == sizeof(struct target_ip_mreqn))
  1345. mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
  1346. unlock_user(target_smreqn, target_addr, 0);
  1347. return 0;
  1348. }
  1349. static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr,
  1350. abi_ulong target_addr,
  1351. socklen_t len)
  1352. {
  1353. const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
  1354. sa_family_t sa_family;
  1355. struct target_sockaddr *target_saddr;
  1356. if (fd_trans_target_to_host_addr(fd)) {
  1357. return fd_trans_target_to_host_addr(fd)(addr, target_addr, len);
  1358. }
  1359. target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
  1360. if (!target_saddr)
  1361. return -TARGET_EFAULT;
  1362. sa_family = tswap16(target_saddr->sa_family);
  1363. /* Oops. The caller might send a incomplete sun_path; sun_path
  1364. * must be terminated by \0 (see the manual page), but
  1365. * unfortunately it is quite common to specify sockaddr_un
  1366. * length as "strlen(x->sun_path)" while it should be
  1367. * "strlen(...) + 1". We'll fix that here if needed.
  1368. * Linux kernel has a similar feature.
  1369. */
  1370. if (sa_family == AF_UNIX) {
  1371. if (len < unix_maxlen && len > 0) {
  1372. char *cp = (char*)target_saddr;
  1373. if ( cp[len-1] && !cp[len] )
  1374. len++;
  1375. }
  1376. if (len > unix_maxlen)
  1377. len = unix_maxlen;
  1378. }
  1379. memcpy(addr, target_saddr, len);
  1380. addr->sa_family = sa_family;
  1381. if (sa_family == AF_NETLINK) {
  1382. struct sockaddr_nl *nladdr;
  1383. nladdr = (struct sockaddr_nl *)addr;
  1384. nladdr->nl_pid = tswap32(nladdr->nl_pid);
  1385. nladdr->nl_groups = tswap32(nladdr->nl_groups);
  1386. } else if (sa_family == AF_PACKET) {
  1387. struct target_sockaddr_ll *lladdr;
  1388. lladdr = (struct target_sockaddr_ll *)addr;
  1389. lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex);
  1390. lladdr->sll_hatype = tswap16(lladdr->sll_hatype);
  1391. }
  1392. unlock_user(target_saddr, target_addr, 0);
  1393. return 0;
  1394. }
  1395. static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
  1396. struct sockaddr *addr,
  1397. socklen_t len)
  1398. {
  1399. struct target_sockaddr *target_saddr;
  1400. if (len == 0) {
  1401. return 0;
  1402. }
  1403. assert(addr);
  1404. target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
  1405. if (!target_saddr)
  1406. return -TARGET_EFAULT;
  1407. memcpy(target_saddr, addr, len);
  1408. if (len >= offsetof(struct target_sockaddr, sa_family) +
  1409. sizeof(target_saddr->sa_family)) {
  1410. target_saddr->sa_family = tswap16(addr->sa_family);
  1411. }
  1412. if (addr->sa_family == AF_NETLINK &&
  1413. len >= sizeof(struct target_sockaddr_nl)) {
  1414. struct target_sockaddr_nl *target_nl =
  1415. (struct target_sockaddr_nl *)target_saddr;
  1416. target_nl->nl_pid = tswap32(target_nl->nl_pid);
  1417. target_nl->nl_groups = tswap32(target_nl->nl_groups);
  1418. } else if (addr->sa_family == AF_PACKET) {
  1419. struct sockaddr_ll *target_ll = (struct sockaddr_ll *)target_saddr;
  1420. target_ll->sll_ifindex = tswap32(target_ll->sll_ifindex);
  1421. target_ll->sll_hatype = tswap16(target_ll->sll_hatype);
  1422. } else if (addr->sa_family == AF_INET6 &&
  1423. len >= sizeof(struct target_sockaddr_in6)) {
  1424. struct target_sockaddr_in6 *target_in6 =
  1425. (struct target_sockaddr_in6 *)target_saddr;
  1426. target_in6->sin6_scope_id = tswap16(target_in6->sin6_scope_id);
  1427. }
  1428. unlock_user(target_saddr, target_addr, len);
  1429. return 0;
  1430. }
  1431. static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
  1432. struct target_msghdr *target_msgh)
  1433. {
  1434. struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
  1435. abi_long msg_controllen;
  1436. abi_ulong target_cmsg_addr;
  1437. struct target_cmsghdr *target_cmsg, *target_cmsg_start;
  1438. socklen_t space = 0;
  1439. msg_controllen = tswapal(target_msgh->msg_controllen);
  1440. if (msg_controllen < sizeof (struct target_cmsghdr))
  1441. goto the_end;
  1442. target_cmsg_addr = tswapal(target_msgh->msg_control);
  1443. target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
  1444. target_cmsg_start = target_cmsg;
  1445. if (!target_cmsg)
  1446. return -TARGET_EFAULT;
  1447. while (cmsg && target_cmsg) {
  1448. void *data = CMSG_DATA(cmsg);
  1449. void *target_data = TARGET_CMSG_DATA(target_cmsg);
  1450. int len = tswapal(target_cmsg->cmsg_len)
  1451. - sizeof(struct target_cmsghdr);
  1452. space += CMSG_SPACE(len);
  1453. if (space > msgh->msg_controllen) {
  1454. space -= CMSG_SPACE(len);
  1455. /* This is a QEMU bug, since we allocated the payload
  1456. * area ourselves (unlike overflow in host-to-target
  1457. * conversion, which is just the guest giving us a buffer
  1458. * that's too small). It can't happen for the payload types
  1459. * we currently support; if it becomes an issue in future
  1460. * we would need to improve our allocation strategy to
  1461. * something more intelligent than "twice the size of the
  1462. * target buffer we're reading from".
  1463. */
  1464. qemu_log_mask(LOG_UNIMP,
  1465. ("Unsupported ancillary data %d/%d: "
  1466. "unhandled msg size\n"),
  1467. tswap32(target_cmsg->cmsg_level),
  1468. tswap32(target_cmsg->cmsg_type));
  1469. break;
  1470. }
  1471. if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
  1472. cmsg->cmsg_level = SOL_SOCKET;
  1473. } else {
  1474. cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
  1475. }
  1476. cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
  1477. cmsg->cmsg_len = CMSG_LEN(len);
  1478. if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
  1479. int *fd = (int *)data;
  1480. int *target_fd = (int *)target_data;
  1481. int i, numfds = len / sizeof(int);
  1482. for (i = 0; i < numfds; i++) {
  1483. __get_user(fd[i], target_fd + i);
  1484. }
  1485. } else if (cmsg->cmsg_level == SOL_SOCKET
  1486. && cmsg->cmsg_type == SCM_CREDENTIALS) {
  1487. struct ucred *cred = (struct ucred *)data;
  1488. struct target_ucred *target_cred =
  1489. (struct target_ucred *)target_data;
  1490. __get_user(cred->pid, &target_cred->pid);
  1491. __get_user(cred->uid, &target_cred->uid);
  1492. __get_user(cred->gid, &target_cred->gid);
  1493. } else {
  1494. qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n",
  1495. cmsg->cmsg_level, cmsg->cmsg_type);
  1496. memcpy(data, target_data, len);
  1497. }
  1498. cmsg = CMSG_NXTHDR(msgh, cmsg);
  1499. target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
  1500. target_cmsg_start);
  1501. }
  1502. unlock_user(target_cmsg, target_cmsg_addr, 0);
  1503. the_end:
  1504. msgh->msg_controllen = space;
  1505. return 0;
  1506. }
  1507. static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
  1508. struct msghdr *msgh)
  1509. {
  1510. struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
  1511. abi_long msg_controllen;
  1512. abi_ulong target_cmsg_addr;
  1513. struct target_cmsghdr *target_cmsg, *target_cmsg_start;
  1514. socklen_t space = 0;
  1515. msg_controllen = tswapal(target_msgh->msg_controllen);
  1516. if (msg_controllen < sizeof (struct target_cmsghdr))
  1517. goto the_end;
  1518. target_cmsg_addr = tswapal(target_msgh->msg_control);
  1519. target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
  1520. target_cmsg_start = target_cmsg;
  1521. if (!target_cmsg)
  1522. return -TARGET_EFAULT;
  1523. while (cmsg && target_cmsg) {
  1524. void *data = CMSG_DATA(cmsg);
  1525. void *target_data = TARGET_CMSG_DATA(target_cmsg);
  1526. int len = cmsg->cmsg_len - sizeof(struct cmsghdr);
  1527. int tgt_len, tgt_space;
  1528. /* We never copy a half-header but may copy half-data;
  1529. * this is Linux's behaviour in put_cmsg(). Note that
  1530. * truncation here is a guest problem (which we report
  1531. * to the guest via the CTRUNC bit), unlike truncation
  1532. * in target_to_host_cmsg, which is a QEMU bug.
  1533. */
  1534. if (msg_controllen < sizeof(struct target_cmsghdr)) {
  1535. target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
  1536. break;
  1537. }
  1538. if (cmsg->cmsg_level == SOL_SOCKET) {
  1539. target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
  1540. } else {
  1541. target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
  1542. }
  1543. target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
  1544. /* Payload types which need a different size of payload on
  1545. * the target must adjust tgt_len here.
  1546. */
  1547. tgt_len = len;
  1548. switch (cmsg->cmsg_level) {
  1549. case SOL_SOCKET:
  1550. switch (cmsg->cmsg_type) {
  1551. case SO_TIMESTAMP:
  1552. tgt_len = sizeof(struct target_timeval);
  1553. break;
  1554. default:
  1555. break;
  1556. }
  1557. break;
  1558. default:
  1559. break;
  1560. }
  1561. if (msg_controllen < TARGET_CMSG_LEN(tgt_len)) {
  1562. target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
  1563. tgt_len = msg_controllen - sizeof(struct target_cmsghdr);
  1564. }
  1565. /* We must now copy-and-convert len bytes of payload
  1566. * into tgt_len bytes of destination space. Bear in mind
  1567. * that in both source and destination we may be dealing
  1568. * with a truncated value!
  1569. */
  1570. switch (cmsg->cmsg_level) {
  1571. case SOL_SOCKET:
  1572. switch (cmsg->cmsg_type) {
  1573. case SCM_RIGHTS:
  1574. {
  1575. int *fd = (int *)data;
  1576. int *target_fd = (int *)target_data;
  1577. int i, numfds = tgt_len / sizeof(int);
  1578. for (i = 0; i < numfds; i++) {
  1579. __put_user(fd[i], target_fd + i);
  1580. }
  1581. break;
  1582. }
  1583. case SO_TIMESTAMP:
  1584. {
  1585. struct timeval *tv = (struct timeval *)data;
  1586. struct target_timeval *target_tv =
  1587. (struct target_timeval *)target_data;
  1588. if (len != sizeof(struct timeval) ||
  1589. tgt_len != sizeof(struct target_timeval)) {
  1590. goto unimplemented;
  1591. }
  1592. /* copy struct timeval to target */
  1593. __put_user(tv->tv_sec, &target_tv->tv_sec);
  1594. __put_user(tv->tv_usec, &target_tv->tv_usec);
  1595. break;
  1596. }
  1597. case SCM_CREDENTIALS:
  1598. {
  1599. struct ucred *cred = (struct ucred *)data;
  1600. struct target_ucred *target_cred =
  1601. (struct target_ucred *)target_data;
  1602. __put_user(cred->pid, &target_cred->pid);
  1603. __put_user(cred->uid, &target_cred->uid);
  1604. __put_user(cred->gid, &target_cred->gid);
  1605. break;
  1606. }
  1607. default:
  1608. goto unimplemented;
  1609. }
  1610. break;
  1611. case SOL_IP:
  1612. switch (cmsg->cmsg_type) {
  1613. case IP_TTL:
  1614. {
  1615. uint32_t *v = (uint32_t *)data;
  1616. uint32_t *t_int = (uint32_t *)target_data;
  1617. if (len != sizeof(uint32_t) ||
  1618. tgt_len != sizeof(uint32_t)) {
  1619. goto unimplemented;
  1620. }
  1621. __put_user(*v, t_int);
  1622. break;
  1623. }
  1624. case IP_RECVERR:
  1625. {
  1626. struct errhdr_t {
  1627. struct sock_extended_err ee;
  1628. struct sockaddr_in offender;
  1629. };
  1630. struct errhdr_t *errh = (struct errhdr_t *)data;
  1631. struct errhdr_t *target_errh =
  1632. (struct errhdr_t *)target_data;
  1633. if (len != sizeof(struct errhdr_t) ||
  1634. tgt_len != sizeof(struct errhdr_t)) {
  1635. goto unimplemented;
  1636. }
  1637. __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
  1638. __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
  1639. __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
  1640. __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
  1641. __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
  1642. __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
  1643. __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
  1644. host_to_target_sockaddr((unsigned long) &target_errh->offender,
  1645. (void *) &errh->offender, sizeof(errh->offender));
  1646. break;
  1647. }
  1648. default:
  1649. goto unimplemented;
  1650. }
  1651. break;
  1652. case SOL_IPV6:
  1653. switch (cmsg->cmsg_type) {
  1654. case IPV6_HOPLIMIT:
  1655. {
  1656. uint32_t *v = (uint32_t *)data;
  1657. uint32_t *t_int = (uint32_t *)target_data;
  1658. if (len != sizeof(uint32_t) ||
  1659. tgt_len != sizeof(uint32_t)) {
  1660. goto unimplemented;
  1661. }
  1662. __put_user(*v, t_int);
  1663. break;
  1664. }
  1665. case IPV6_RECVERR:
  1666. {
  1667. struct errhdr6_t {
  1668. struct sock_extended_err ee;
  1669. struct sockaddr_in6 offender;
  1670. };
  1671. struct errhdr6_t *errh = (struct errhdr6_t *)data;
  1672. struct errhdr6_t *target_errh =
  1673. (struct errhdr6_t *)target_data;
  1674. if (len != sizeof(struct errhdr6_t) ||
  1675. tgt_len != sizeof(struct errhdr6_t)) {
  1676. goto unimplemented;
  1677. }
  1678. __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
  1679. __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
  1680. __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
  1681. __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
  1682. __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
  1683. __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
  1684. __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
  1685. host_to_target_sockaddr((unsigned long) &target_errh->offender,
  1686. (void *) &errh->offender, sizeof(errh->offender));
  1687. break;
  1688. }
  1689. default:
  1690. goto unimplemented;
  1691. }
  1692. break;
  1693. default:
  1694. unimplemented:
  1695. qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n",
  1696. cmsg->cmsg_level, cmsg->cmsg_type);
  1697. memcpy(target_data, data, MIN(len, tgt_len));
  1698. if (tgt_len > len) {
  1699. memset(target_data + len, 0, tgt_len - len);
  1700. }
  1701. }
  1702. target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(tgt_len));
  1703. tgt_space = TARGET_CMSG_SPACE(tgt_len);
  1704. if (msg_controllen < tgt_space) {
  1705. tgt_space = msg_controllen;
  1706. }
  1707. msg_controllen -= tgt_space;
  1708. space += tgt_space;
  1709. cmsg = CMSG_NXTHDR(msgh, cmsg);
  1710. target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
  1711. target_cmsg_start);
  1712. }
  1713. unlock_user(target_cmsg, target_cmsg_addr, space);
  1714. the_end:
  1715. target_msgh->msg_controllen = tswapal(space);
  1716. return 0;
  1717. }
  1718. /* do_setsockopt() Must return target values and target errnos. */
  1719. static abi_long do_setsockopt(int sockfd, int level, int optname,
  1720. abi_ulong optval_addr, socklen_t optlen)
  1721. {
  1722. abi_long ret;
  1723. int val;
  1724. struct ip_mreqn *ip_mreq;
  1725. struct ip_mreq_source *ip_mreq_source;
  1726. switch(level) {
  1727. case SOL_TCP:
  1728. /* TCP options all take an 'int' value. */
  1729. if (optlen < sizeof(uint32_t))
  1730. return -TARGET_EINVAL;
  1731. if (get_user_u32(val, optval_addr))
  1732. return -TARGET_EFAULT;
  1733. ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
  1734. break;
  1735. case SOL_IP:
  1736. switch(optname) {
  1737. case IP_TOS:
  1738. case IP_TTL:
  1739. case IP_HDRINCL:
  1740. case IP_ROUTER_ALERT:
  1741. case IP_RECVOPTS:
  1742. case IP_RETOPTS:
  1743. case IP_PKTINFO:
  1744. case IP_MTU_DISCOVER:
  1745. case IP_RECVERR:
  1746. case IP_RECVTTL:
  1747. case IP_RECVTOS:
  1748. #ifdef IP_FREEBIND
  1749. case IP_FREEBIND:
  1750. #endif
  1751. case IP_MULTICAST_TTL:
  1752. case IP_MULTICAST_LOOP:
  1753. val = 0;
  1754. if (optlen >= sizeof(uint32_t)) {
  1755. if (get_user_u32(val, optval_addr))
  1756. return -TARGET_EFAULT;
  1757. } else if (optlen >= 1) {
  1758. if (get_user_u8(val, optval_addr))
  1759. return -TARGET_EFAULT;
  1760. }
  1761. ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
  1762. break;
  1763. case IP_ADD_MEMBERSHIP:
  1764. case IP_DROP_MEMBERSHIP:
  1765. if (optlen < sizeof (struct target_ip_mreq) ||
  1766. optlen > sizeof (struct target_ip_mreqn))
  1767. return -TARGET_EINVAL;
  1768. ip_mreq = (struct ip_mreqn *) alloca(optlen);
  1769. target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
  1770. ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
  1771. break;
  1772. case IP_BLOCK_SOURCE:
  1773. case IP_UNBLOCK_SOURCE:
  1774. case IP_ADD_SOURCE_MEMBERSHIP:
  1775. case IP_DROP_SOURCE_MEMBERSHIP:
  1776. if (optlen != sizeof (struct target_ip_mreq_source))
  1777. return -TARGET_EINVAL;
  1778. ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
  1779. ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
  1780. unlock_user (ip_mreq_source, optval_addr, 0);
  1781. break;
  1782. default:
  1783. goto unimplemented;
  1784. }
  1785. break;
  1786. case SOL_IPV6:
  1787. switch (optname) {
  1788. case IPV6_MTU_DISCOVER:
  1789. case IPV6_MTU:
  1790. case IPV6_V6ONLY:
  1791. case IPV6_RECVPKTINFO:
  1792. case IPV6_UNICAST_HOPS:
  1793. case IPV6_MULTICAST_HOPS:
  1794. case IPV6_MULTICAST_LOOP:
  1795. case IPV6_RECVERR:
  1796. case IPV6_RECVHOPLIMIT:
  1797. case IPV6_2292HOPLIMIT:
  1798. case IPV6_CHECKSUM:
  1799. case IPV6_ADDRFORM:
  1800. case IPV6_2292PKTINFO:
  1801. case IPV6_RECVTCLASS:
  1802. case IPV6_RECVRTHDR:
  1803. case IPV6_2292RTHDR:
  1804. case IPV6_RECVHOPOPTS:
  1805. case IPV6_2292HOPOPTS:
  1806. case IPV6_RECVDSTOPTS:
  1807. case IPV6_2292DSTOPTS:
  1808. case IPV6_TCLASS:
  1809. #ifdef IPV6_RECVPATHMTU
  1810. case IPV6_RECVPATHMTU:
  1811. #endif
  1812. #ifdef IPV6_TRANSPARENT
  1813. case IPV6_TRANSPARENT:
  1814. #endif
  1815. #ifdef IPV6_FREEBIND
  1816. case IPV6_FREEBIND:
  1817. #endif
  1818. #ifdef IPV6_RECVORIGDSTADDR
  1819. case IPV6_RECVORIGDSTADDR:
  1820. #endif
  1821. val = 0;
  1822. if (optlen < sizeof(uint32_t)) {
  1823. return -TARGET_EINVAL;
  1824. }
  1825. if (get_user_u32(val, optval_addr)) {
  1826. return -TARGET_EFAULT;
  1827. }
  1828. ret = get_errno(setsockopt(sockfd, level, optname,
  1829. &val, sizeof(val)));
  1830. break;
  1831. case IPV6_PKTINFO:
  1832. {
  1833. struct in6_pktinfo pki;
  1834. if (optlen < sizeof(pki)) {
  1835. return -TARGET_EINVAL;
  1836. }
  1837. if (copy_from_user(&pki, optval_addr, sizeof(pki))) {
  1838. return -TARGET_EFAULT;
  1839. }
  1840. pki.ipi6_ifindex = tswap32(pki.ipi6_ifindex);
  1841. ret = get_errno(setsockopt(sockfd, level, optname,
  1842. &pki, sizeof(pki)));
  1843. break;
  1844. }
  1845. case IPV6_ADD_MEMBERSHIP:
  1846. case IPV6_DROP_MEMBERSHIP:
  1847. {
  1848. struct ipv6_mreq ipv6mreq;
  1849. if (optlen < sizeof(ipv6mreq)) {
  1850. return -TARGET_EINVAL;
  1851. }
  1852. if (copy_from_user(&ipv6mreq, optval_addr, sizeof(ipv6mreq))) {
  1853. return -TARGET_EFAULT;
  1854. }
  1855. ipv6mreq.ipv6mr_interface = tswap32(ipv6mreq.ipv6mr_interface);
  1856. ret = get_errno(setsockopt(sockfd, level, optname,
  1857. &ipv6mreq, sizeof(ipv6mreq)));
  1858. break;
  1859. }
  1860. default:
  1861. goto unimplemented;
  1862. }
  1863. break;
  1864. case SOL_ICMPV6:
  1865. switch (optname) {
  1866. case ICMPV6_FILTER:
  1867. {
  1868. struct icmp6_filter icmp6f;
  1869. if (optlen > sizeof(icmp6f)) {
  1870. optlen = sizeof(icmp6f);
  1871. }
  1872. if (copy_from_user(&icmp6f, optval_addr, optlen)) {
  1873. return -TARGET_EFAULT;
  1874. }
  1875. for (val = 0; val < 8; val++) {
  1876. icmp6f.data[val] = tswap32(icmp6f.data[val]);
  1877. }
  1878. ret = get_errno(setsockopt(sockfd, level, optname,
  1879. &icmp6f, optlen));
  1880. break;
  1881. }
  1882. default:
  1883. goto unimplemented;
  1884. }
  1885. break;
  1886. case SOL_RAW:
  1887. switch (optname) {
  1888. case ICMP_FILTER:
  1889. case IPV6_CHECKSUM:
  1890. /* those take an u32 value */
  1891. if (optlen < sizeof(uint32_t)) {
  1892. return -TARGET_EINVAL;
  1893. }
  1894. if (get_user_u32(val, optval_addr)) {
  1895. return -TARGET_EFAULT;
  1896. }
  1897. ret = get_errno(setsockopt(sockfd, level, optname,
  1898. &val, sizeof(val)));
  1899. break;
  1900. default:
  1901. goto unimplemented;
  1902. }
  1903. break;
  1904. #if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE)
  1905. case SOL_ALG:
  1906. switch (optname) {
  1907. case ALG_SET_KEY:
  1908. {
  1909. char *alg_key = g_malloc(optlen);
  1910. if (!alg_key) {
  1911. return -TARGET_ENOMEM;
  1912. }
  1913. if (copy_from_user(alg_key, optval_addr, optlen)) {
  1914. g_free(alg_key);
  1915. return -TARGET_EFAULT;
  1916. }
  1917. ret = get_errno(setsockopt(sockfd, level, optname,
  1918. alg_key, optlen));
  1919. g_free(alg_key);
  1920. break;
  1921. }
  1922. case ALG_SET_AEAD_AUTHSIZE:
  1923. {
  1924. ret = get_errno(setsockopt(sockfd, level, optname,
  1925. NULL, optlen));
  1926. break;
  1927. }
  1928. default:
  1929. goto unimplemented;
  1930. }
  1931. break;
  1932. #endif
  1933. case TARGET_SOL_SOCKET:
  1934. switch (optname) {
  1935. case TARGET_SO_RCVTIMEO:
  1936. {
  1937. struct timeval tv;
  1938. optname = SO_RCVTIMEO;
  1939. set_timeout:
  1940. if (optlen != sizeof(struct target_timeval)) {
  1941. return -TARGET_EINVAL;
  1942. }
  1943. if (copy_from_user_timeval(&tv, optval_addr)) {
  1944. return -TARGET_EFAULT;
  1945. }
  1946. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
  1947. &tv, sizeof(tv)));
  1948. return ret;
  1949. }
  1950. case TARGET_SO_SNDTIMEO:
  1951. optname = SO_SNDTIMEO;
  1952. goto set_timeout;
  1953. case TARGET_SO_ATTACH_FILTER:
  1954. {
  1955. struct target_sock_fprog *tfprog;
  1956. struct target_sock_filter *tfilter;
  1957. struct sock_fprog fprog;
  1958. struct sock_filter *filter;
  1959. int i;
  1960. if (optlen != sizeof(*tfprog)) {
  1961. return -TARGET_EINVAL;
  1962. }
  1963. if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
  1964. return -TARGET_EFAULT;
  1965. }
  1966. if (!lock_user_struct(VERIFY_READ, tfilter,
  1967. tswapal(tfprog->filter), 0)) {
  1968. unlock_user_struct(tfprog, optval_addr, 1);
  1969. return -TARGET_EFAULT;
  1970. }
  1971. fprog.len = tswap16(tfprog->len);
  1972. filter = g_try_new(struct sock_filter, fprog.len);
  1973. if (filter == NULL) {
  1974. unlock_user_struct(tfilter, tfprog->filter, 1);
  1975. unlock_user_struct(tfprog, optval_addr, 1);
  1976. return -TARGET_ENOMEM;
  1977. }
  1978. for (i = 0; i < fprog.len; i++) {
  1979. filter[i].code = tswap16(tfilter[i].code);
  1980. filter[i].jt = tfilter[i].jt;
  1981. filter[i].jf = tfilter[i].jf;
  1982. filter[i].k = tswap32(tfilter[i].k);
  1983. }
  1984. fprog.filter = filter;
  1985. ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
  1986. SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
  1987. g_free(filter);
  1988. unlock_user_struct(tfilter, tfprog->filter, 1);
  1989. unlock_user_struct(tfprog, optval_addr, 1);
  1990. return ret;
  1991. }
  1992. case TARGET_SO_BINDTODEVICE:
  1993. {
  1994. char *dev_ifname, *addr_ifname;
  1995. if (optlen > IFNAMSIZ - 1) {
  1996. optlen = IFNAMSIZ - 1;
  1997. }
  1998. dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1);
  1999. if (!dev_ifname) {
  2000. return -TARGET_EFAULT;
  2001. }
  2002. optname = SO_BINDTODEVICE;
  2003. addr_ifname = alloca(IFNAMSIZ);
  2004. memcpy(addr_ifname, dev_ifname, optlen);
  2005. addr_ifname[optlen] = 0;
  2006. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
  2007. addr_ifname, optlen));
  2008. unlock_user (dev_ifname, optval_addr, 0);
  2009. return ret;
  2010. }
  2011. case TARGET_SO_LINGER:
  2012. {
  2013. struct linger lg;
  2014. struct target_linger *tlg;
  2015. if (optlen != sizeof(struct target_linger)) {
  2016. return -TARGET_EINVAL;
  2017. }
  2018. if (!lock_user_struct(VERIFY_READ, tlg, optval_addr, 1)) {
  2019. return -TARGET_EFAULT;
  2020. }
  2021. __get_user(lg.l_onoff, &tlg->l_onoff);
  2022. __get_user(lg.l_linger, &tlg->l_linger);
  2023. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, SO_LINGER,
  2024. &lg, sizeof(lg)));
  2025. unlock_user_struct(tlg, optval_addr, 0);
  2026. return ret;
  2027. }
  2028. /* Options with 'int' argument. */
  2029. case TARGET_SO_DEBUG:
  2030. optname = SO_DEBUG;
  2031. break;
  2032. case TARGET_SO_REUSEADDR:
  2033. optname = SO_REUSEADDR;
  2034. break;
  2035. #ifdef SO_REUSEPORT
  2036. case TARGET_SO_REUSEPORT:
  2037. optname = SO_REUSEPORT;
  2038. break;
  2039. #endif
  2040. case TARGET_SO_TYPE:
  2041. optname = SO_TYPE;
  2042. break;
  2043. case TARGET_SO_ERROR:
  2044. optname = SO_ERROR;
  2045. break;
  2046. case TARGET_SO_DONTROUTE:
  2047. optname = SO_DONTROUTE;
  2048. break;
  2049. case TARGET_SO_BROADCAST:
  2050. optname = SO_BROADCAST;
  2051. break;
  2052. case TARGET_SO_SNDBUF:
  2053. optname = SO_SNDBUF;
  2054. break;
  2055. case TARGET_SO_SNDBUFFORCE:
  2056. optname = SO_SNDBUFFORCE;
  2057. break;
  2058. case TARGET_SO_RCVBUF:
  2059. optname = SO_RCVBUF;
  2060. break;
  2061. case TARGET_SO_RCVBUFFORCE:
  2062. optname = SO_RCVBUFFORCE;
  2063. break;
  2064. case TARGET_SO_KEEPALIVE:
  2065. optname = SO_KEEPALIVE;
  2066. break;
  2067. case TARGET_SO_OOBINLINE:
  2068. optname = SO_OOBINLINE;
  2069. break;
  2070. case TARGET_SO_NO_CHECK:
  2071. optname = SO_NO_CHECK;
  2072. break;
  2073. case TARGET_SO_PRIORITY:
  2074. optname = SO_PRIORITY;
  2075. break;
  2076. #ifdef SO_BSDCOMPAT
  2077. case TARGET_SO_BSDCOMPAT:
  2078. optname = SO_BSDCOMPAT;
  2079. break;
  2080. #endif
  2081. case TARGET_SO_PASSCRED:
  2082. optname = SO_PASSCRED;
  2083. break;
  2084. case TARGET_SO_PASSSEC:
  2085. optname = SO_PASSSEC;
  2086. break;
  2087. case TARGET_SO_TIMESTAMP:
  2088. optname = SO_TIMESTAMP;
  2089. break;
  2090. case TARGET_SO_RCVLOWAT:
  2091. optname = SO_RCVLOWAT;
  2092. break;
  2093. default:
  2094. goto unimplemented;
  2095. }
  2096. if (optlen < sizeof(uint32_t))
  2097. return -TARGET_EINVAL;
  2098. if (get_user_u32(val, optval_addr))
  2099. return -TARGET_EFAULT;
  2100. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
  2101. break;
  2102. #ifdef SOL_NETLINK
  2103. case SOL_NETLINK:
  2104. switch (optname) {
  2105. case NETLINK_PKTINFO:
  2106. case NETLINK_ADD_MEMBERSHIP:
  2107. case NETLINK_DROP_MEMBERSHIP:
  2108. case NETLINK_BROADCAST_ERROR:
  2109. case NETLINK_NO_ENOBUFS:
  2110. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
  2111. case NETLINK_LISTEN_ALL_NSID:
  2112. case NETLINK_CAP_ACK:
  2113. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
  2114. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
  2115. case NETLINK_EXT_ACK:
  2116. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2117. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
  2118. case NETLINK_GET_STRICT_CHK:
  2119. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2120. break;
  2121. default:
  2122. goto unimplemented;
  2123. }
  2124. val = 0;
  2125. if (optlen < sizeof(uint32_t)) {
  2126. return -TARGET_EINVAL;
  2127. }
  2128. if (get_user_u32(val, optval_addr)) {
  2129. return -TARGET_EFAULT;
  2130. }
  2131. ret = get_errno(setsockopt(sockfd, SOL_NETLINK, optname, &val,
  2132. sizeof(val)));
  2133. break;
  2134. #endif /* SOL_NETLINK */
  2135. default:
  2136. unimplemented:
  2137. qemu_log_mask(LOG_UNIMP, "Unsupported setsockopt level=%d optname=%d\n",
  2138. level, optname);
  2139. ret = -TARGET_ENOPROTOOPT;
  2140. }
  2141. return ret;
  2142. }
  2143. /* do_getsockopt() Must return target values and target errnos. */
  2144. static abi_long do_getsockopt(int sockfd, int level, int optname,
  2145. abi_ulong optval_addr, abi_ulong optlen)
  2146. {
  2147. abi_long ret;
  2148. int len, val;
  2149. socklen_t lv;
  2150. switch(level) {
  2151. case TARGET_SOL_SOCKET:
  2152. level = SOL_SOCKET;
  2153. switch (optname) {
  2154. /* These don't just return a single integer */
  2155. case TARGET_SO_PEERNAME:
  2156. goto unimplemented;
  2157. case TARGET_SO_RCVTIMEO: {
  2158. struct timeval tv;
  2159. socklen_t tvlen;
  2160. optname = SO_RCVTIMEO;
  2161. get_timeout:
  2162. if (get_user_u32(len, optlen)) {
  2163. return -TARGET_EFAULT;
  2164. }
  2165. if (len < 0) {
  2166. return -TARGET_EINVAL;
  2167. }
  2168. tvlen = sizeof(tv);
  2169. ret = get_errno(getsockopt(sockfd, level, optname,
  2170. &tv, &tvlen));
  2171. if (ret < 0) {
  2172. return ret;
  2173. }
  2174. if (len > sizeof(struct target_timeval)) {
  2175. len = sizeof(struct target_timeval);
  2176. }
  2177. if (copy_to_user_timeval(optval_addr, &tv)) {
  2178. return -TARGET_EFAULT;
  2179. }
  2180. if (put_user_u32(len, optlen)) {
  2181. return -TARGET_EFAULT;
  2182. }
  2183. break;
  2184. }
  2185. case TARGET_SO_SNDTIMEO:
  2186. optname = SO_SNDTIMEO;
  2187. goto get_timeout;
  2188. case TARGET_SO_PEERCRED: {
  2189. struct ucred cr;
  2190. socklen_t crlen;
  2191. struct target_ucred *tcr;
  2192. if (get_user_u32(len, optlen)) {
  2193. return -TARGET_EFAULT;
  2194. }
  2195. if (len < 0) {
  2196. return -TARGET_EINVAL;
  2197. }
  2198. crlen = sizeof(cr);
  2199. ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
  2200. &cr, &crlen));
  2201. if (ret < 0) {
  2202. return ret;
  2203. }
  2204. if (len > crlen) {
  2205. len = crlen;
  2206. }
  2207. if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
  2208. return -TARGET_EFAULT;
  2209. }
  2210. __put_user(cr.pid, &tcr->pid);
  2211. __put_user(cr.uid, &tcr->uid);
  2212. __put_user(cr.gid, &tcr->gid);
  2213. unlock_user_struct(tcr, optval_addr, 1);
  2214. if (put_user_u32(len, optlen)) {
  2215. return -TARGET_EFAULT;
  2216. }
  2217. break;
  2218. }
  2219. case TARGET_SO_PEERSEC: {
  2220. char *name;
  2221. if (get_user_u32(len, optlen)) {
  2222. return -TARGET_EFAULT;
  2223. }
  2224. if (len < 0) {
  2225. return -TARGET_EINVAL;
  2226. }
  2227. name = lock_user(VERIFY_WRITE, optval_addr, len, 0);
  2228. if (!name) {
  2229. return -TARGET_EFAULT;
  2230. }
  2231. lv = len;
  2232. ret = get_errno(getsockopt(sockfd, level, SO_PEERSEC,
  2233. name, &lv));
  2234. if (put_user_u32(lv, optlen)) {
  2235. ret = -TARGET_EFAULT;
  2236. }
  2237. unlock_user(name, optval_addr, lv);
  2238. break;
  2239. }
  2240. case TARGET_SO_LINGER:
  2241. {
  2242. struct linger lg;
  2243. socklen_t lglen;
  2244. struct target_linger *tlg;
  2245. if (get_user_u32(len, optlen)) {
  2246. return -TARGET_EFAULT;
  2247. }
  2248. if (len < 0) {
  2249. return -TARGET_EINVAL;
  2250. }
  2251. lglen = sizeof(lg);
  2252. ret = get_errno(getsockopt(sockfd, level, SO_LINGER,
  2253. &lg, &lglen));
  2254. if (ret < 0) {
  2255. return ret;
  2256. }
  2257. if (len > lglen) {
  2258. len = lglen;
  2259. }
  2260. if (!lock_user_struct(VERIFY_WRITE, tlg, optval_addr, 0)) {
  2261. return -TARGET_EFAULT;
  2262. }
  2263. __put_user(lg.l_onoff, &tlg->l_onoff);
  2264. __put_user(lg.l_linger, &tlg->l_linger);
  2265. unlock_user_struct(tlg, optval_addr, 1);
  2266. if (put_user_u32(len, optlen)) {
  2267. return -TARGET_EFAULT;
  2268. }
  2269. break;
  2270. }
  2271. /* Options with 'int' argument. */
  2272. case TARGET_SO_DEBUG:
  2273. optname = SO_DEBUG;
  2274. goto int_case;
  2275. case TARGET_SO_REUSEADDR:
  2276. optname = SO_REUSEADDR;
  2277. goto int_case;
  2278. #ifdef SO_REUSEPORT
  2279. case TARGET_SO_REUSEPORT:
  2280. optname = SO_REUSEPORT;
  2281. goto int_case;
  2282. #endif
  2283. case TARGET_SO_TYPE:
  2284. optname = SO_TYPE;
  2285. goto int_case;
  2286. case TARGET_SO_ERROR:
  2287. optname = SO_ERROR;
  2288. goto int_case;
  2289. case TARGET_SO_DONTROUTE:
  2290. optname = SO_DONTROUTE;
  2291. goto int_case;
  2292. case TARGET_SO_BROADCAST:
  2293. optname = SO_BROADCAST;
  2294. goto int_case;
  2295. case TARGET_SO_SNDBUF:
  2296. optname = SO_SNDBUF;
  2297. goto int_case;
  2298. case TARGET_SO_RCVBUF:
  2299. optname = SO_RCVBUF;
  2300. goto int_case;
  2301. case TARGET_SO_KEEPALIVE:
  2302. optname = SO_KEEPALIVE;
  2303. goto int_case;
  2304. case TARGET_SO_OOBINLINE:
  2305. optname = SO_OOBINLINE;
  2306. goto int_case;
  2307. case TARGET_SO_NO_CHECK:
  2308. optname = SO_NO_CHECK;
  2309. goto int_case;
  2310. case TARGET_SO_PRIORITY:
  2311. optname = SO_PRIORITY;
  2312. goto int_case;
  2313. #ifdef SO_BSDCOMPAT
  2314. case TARGET_SO_BSDCOMPAT:
  2315. optname = SO_BSDCOMPAT;
  2316. goto int_case;
  2317. #endif
  2318. case TARGET_SO_PASSCRED:
  2319. optname = SO_PASSCRED;
  2320. goto int_case;
  2321. case TARGET_SO_TIMESTAMP:
  2322. optname = SO_TIMESTAMP;
  2323. goto int_case;
  2324. case TARGET_SO_RCVLOWAT:
  2325. optname = SO_RCVLOWAT;
  2326. goto int_case;
  2327. case TARGET_SO_ACCEPTCONN:
  2328. optname = SO_ACCEPTCONN;
  2329. goto int_case;
  2330. default:
  2331. goto int_case;
  2332. }
  2333. break;
  2334. case SOL_TCP:
  2335. /* TCP options all take an 'int' value. */
  2336. int_case:
  2337. if (get_user_u32(len, optlen))
  2338. return -TARGET_EFAULT;
  2339. if (len < 0)
  2340. return -TARGET_EINVAL;
  2341. lv = sizeof(lv);
  2342. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2343. if (ret < 0)
  2344. return ret;
  2345. if (optname == SO_TYPE) {
  2346. val = host_to_target_sock_type(val);
  2347. }
  2348. if (len > lv)
  2349. len = lv;
  2350. if (len == 4) {
  2351. if (put_user_u32(val, optval_addr))
  2352. return -TARGET_EFAULT;
  2353. } else {
  2354. if (put_user_u8(val, optval_addr))
  2355. return -TARGET_EFAULT;
  2356. }
  2357. if (put_user_u32(len, optlen))
  2358. return -TARGET_EFAULT;
  2359. break;
  2360. case SOL_IP:
  2361. switch(optname) {
  2362. case IP_TOS:
  2363. case IP_TTL:
  2364. case IP_HDRINCL:
  2365. case IP_ROUTER_ALERT:
  2366. case IP_RECVOPTS:
  2367. case IP_RETOPTS:
  2368. case IP_PKTINFO:
  2369. case IP_MTU_DISCOVER:
  2370. case IP_RECVERR:
  2371. case IP_RECVTOS:
  2372. #ifdef IP_FREEBIND
  2373. case IP_FREEBIND:
  2374. #endif
  2375. case IP_MULTICAST_TTL:
  2376. case IP_MULTICAST_LOOP:
  2377. if (get_user_u32(len, optlen))
  2378. return -TARGET_EFAULT;
  2379. if (len < 0)
  2380. return -TARGET_EINVAL;
  2381. lv = sizeof(lv);
  2382. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2383. if (ret < 0)
  2384. return ret;
  2385. if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
  2386. len = 1;
  2387. if (put_user_u32(len, optlen)
  2388. || put_user_u8(val, optval_addr))
  2389. return -TARGET_EFAULT;
  2390. } else {
  2391. if (len > sizeof(int))
  2392. len = sizeof(int);
  2393. if (put_user_u32(len, optlen)
  2394. || put_user_u32(val, optval_addr))
  2395. return -TARGET_EFAULT;
  2396. }
  2397. break;
  2398. default:
  2399. ret = -TARGET_ENOPROTOOPT;
  2400. break;
  2401. }
  2402. break;
  2403. case SOL_IPV6:
  2404. switch (optname) {
  2405. case IPV6_MTU_DISCOVER:
  2406. case IPV6_MTU:
  2407. case IPV6_V6ONLY:
  2408. case IPV6_RECVPKTINFO:
  2409. case IPV6_UNICAST_HOPS:
  2410. case IPV6_MULTICAST_HOPS:
  2411. case IPV6_MULTICAST_LOOP:
  2412. case IPV6_RECVERR:
  2413. case IPV6_RECVHOPLIMIT:
  2414. case IPV6_2292HOPLIMIT:
  2415. case IPV6_CHECKSUM:
  2416. case IPV6_ADDRFORM:
  2417. case IPV6_2292PKTINFO:
  2418. case IPV6_RECVTCLASS:
  2419. case IPV6_RECVRTHDR:
  2420. case IPV6_2292RTHDR:
  2421. case IPV6_RECVHOPOPTS:
  2422. case IPV6_2292HOPOPTS:
  2423. case IPV6_RECVDSTOPTS:
  2424. case IPV6_2292DSTOPTS:
  2425. case IPV6_TCLASS:
  2426. #ifdef IPV6_RECVPATHMTU
  2427. case IPV6_RECVPATHMTU:
  2428. #endif
  2429. #ifdef IPV6_TRANSPARENT
  2430. case IPV6_TRANSPARENT:
  2431. #endif
  2432. #ifdef IPV6_FREEBIND
  2433. case IPV6_FREEBIND:
  2434. #endif
  2435. #ifdef IPV6_RECVORIGDSTADDR
  2436. case IPV6_RECVORIGDSTADDR:
  2437. #endif
  2438. if (get_user_u32(len, optlen))
  2439. return -TARGET_EFAULT;
  2440. if (len < 0)
  2441. return -TARGET_EINVAL;
  2442. lv = sizeof(lv);
  2443. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2444. if (ret < 0)
  2445. return ret;
  2446. if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
  2447. len = 1;
  2448. if (put_user_u32(len, optlen)
  2449. || put_user_u8(val, optval_addr))
  2450. return -TARGET_EFAULT;
  2451. } else {
  2452. if (len > sizeof(int))
  2453. len = sizeof(int);
  2454. if (put_user_u32(len, optlen)
  2455. || put_user_u32(val, optval_addr))
  2456. return -TARGET_EFAULT;
  2457. }
  2458. break;
  2459. default:
  2460. ret = -TARGET_ENOPROTOOPT;
  2461. break;
  2462. }
  2463. break;
  2464. #ifdef SOL_NETLINK
  2465. case SOL_NETLINK:
  2466. switch (optname) {
  2467. case NETLINK_PKTINFO:
  2468. case NETLINK_BROADCAST_ERROR:
  2469. case NETLINK_NO_ENOBUFS:
  2470. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
  2471. case NETLINK_LISTEN_ALL_NSID:
  2472. case NETLINK_CAP_ACK:
  2473. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
  2474. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
  2475. case NETLINK_EXT_ACK:
  2476. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2477. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
  2478. case NETLINK_GET_STRICT_CHK:
  2479. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
  2480. if (get_user_u32(len, optlen)) {
  2481. return -TARGET_EFAULT;
  2482. }
  2483. if (len != sizeof(val)) {
  2484. return -TARGET_EINVAL;
  2485. }
  2486. lv = len;
  2487. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  2488. if (ret < 0) {
  2489. return ret;
  2490. }
  2491. if (put_user_u32(lv, optlen)
  2492. || put_user_u32(val, optval_addr)) {
  2493. return -TARGET_EFAULT;
  2494. }
  2495. break;
  2496. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
  2497. case NETLINK_LIST_MEMBERSHIPS:
  2498. {
  2499. uint32_t *results;
  2500. int i;
  2501. if (get_user_u32(len, optlen)) {
  2502. return -TARGET_EFAULT;
  2503. }
  2504. if (len < 0) {
  2505. return -TARGET_EINVAL;
  2506. }
  2507. results = lock_user(VERIFY_WRITE, optval_addr, len, 1);
  2508. if (!results) {
  2509. return -TARGET_EFAULT;
  2510. }
  2511. lv = len;
  2512. ret = get_errno(getsockopt(sockfd, level, optname, results, &lv));
  2513. if (ret < 0) {
  2514. unlock_user(results, optval_addr, 0);
  2515. return ret;
  2516. }
  2517. /* swap host endianess to target endianess. */
  2518. for (i = 0; i < (len / sizeof(uint32_t)); i++) {
  2519. results[i] = tswap32(results[i]);
  2520. }
  2521. if (put_user_u32(lv, optlen)) {
  2522. return -TARGET_EFAULT;
  2523. }
  2524. unlock_user(results, optval_addr, 0);
  2525. break;
  2526. }
  2527. #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
  2528. default:
  2529. goto unimplemented;
  2530. }
  2531. break;
  2532. #endif /* SOL_NETLINK */
  2533. default:
  2534. unimplemented:
  2535. qemu_log_mask(LOG_UNIMP,
  2536. "getsockopt level=%d optname=%d not yet supported\n",
  2537. level, optname);
  2538. ret = -TARGET_EOPNOTSUPP;
  2539. break;
  2540. }
  2541. return ret;
  2542. }
  2543. /* Convert target low/high pair representing file offset into the host
  2544. * low/high pair. This function doesn't handle offsets bigger than 64 bits
  2545. * as the kernel doesn't handle them either.
  2546. */
  2547. static void target_to_host_low_high(abi_ulong tlow,
  2548. abi_ulong thigh,
  2549. unsigned long *hlow,
  2550. unsigned long *hhigh)
  2551. {
  2552. uint64_t off = tlow |
  2553. ((unsigned long long)thigh << TARGET_LONG_BITS / 2) <<
  2554. TARGET_LONG_BITS / 2;
  2555. *hlow = off;
  2556. *hhigh = (off >> HOST_LONG_BITS / 2) >> HOST_LONG_BITS / 2;
  2557. }
  2558. static struct iovec *lock_iovec(int type, abi_ulong target_addr,
  2559. abi_ulong count, int copy)
  2560. {
  2561. struct target_iovec *target_vec;
  2562. struct iovec *vec;
  2563. abi_ulong total_len, max_len;
  2564. int i;
  2565. int err = 0;
  2566. bool bad_address = false;
  2567. if (count == 0) {
  2568. errno = 0;
  2569. return NULL;
  2570. }
  2571. if (count > IOV_MAX) {
  2572. errno = EINVAL;
  2573. return NULL;
  2574. }
  2575. vec = g_try_new0(struct iovec, count);
  2576. if (vec == NULL) {
  2577. errno = ENOMEM;
  2578. return NULL;
  2579. }
  2580. target_vec = lock_user(VERIFY_READ, target_addr,
  2581. count * sizeof(struct target_iovec), 1);
  2582. if (target_vec == NULL) {
  2583. err = EFAULT;
  2584. goto fail2;
  2585. }
  2586. /* ??? If host page size > target page size, this will result in a
  2587. value larger than what we can actually support. */
  2588. max_len = 0x7fffffff & TARGET_PAGE_MASK;
  2589. total_len = 0;
  2590. for (i = 0; i < count; i++) {
  2591. abi_ulong base = tswapal(target_vec[i].iov_base);
  2592. abi_long len = tswapal(target_vec[i].iov_len);
  2593. if (len < 0) {
  2594. err = EINVAL;
  2595. goto fail;
  2596. } else if (len == 0) {
  2597. /* Zero length pointer is ignored. */
  2598. vec[i].iov_base = 0;
  2599. } else {
  2600. vec[i].iov_base = lock_user(type, base, len, copy);
  2601. /* If the first buffer pointer is bad, this is a fault. But
  2602. * subsequent bad buffers will result in a partial write; this
  2603. * is realized by filling the vector with null pointers and
  2604. * zero lengths. */
  2605. if (!vec[i].iov_base) {
  2606. if (i == 0) {
  2607. err = EFAULT;
  2608. goto fail;
  2609. } else {
  2610. bad_address = true;
  2611. }
  2612. }
  2613. if (bad_address) {
  2614. len = 0;
  2615. }
  2616. if (len > max_len - total_len) {
  2617. len = max_len - total_len;
  2618. }
  2619. }
  2620. vec[i].iov_len = len;
  2621. total_len += len;
  2622. }
  2623. unlock_user(target_vec, target_addr, 0);
  2624. return vec;
  2625. fail:
  2626. while (--i >= 0) {
  2627. if (tswapal(target_vec[i].iov_len) > 0) {
  2628. unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0);
  2629. }
  2630. }
  2631. unlock_user(target_vec, target_addr, 0);
  2632. fail2:
  2633. g_free(vec);
  2634. errno = err;
  2635. return NULL;
  2636. }
  2637. static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
  2638. abi_ulong count, int copy)
  2639. {
  2640. struct target_iovec *target_vec;
  2641. int i;
  2642. target_vec = lock_user(VERIFY_READ, target_addr,
  2643. count * sizeof(struct target_iovec), 1);
  2644. if (target_vec) {
  2645. for (i = 0; i < count; i++) {
  2646. abi_ulong base = tswapal(target_vec[i].iov_base);
  2647. abi_long len = tswapal(target_vec[i].iov_len);
  2648. if (len < 0) {
  2649. break;
  2650. }
  2651. unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
  2652. }
  2653. unlock_user(target_vec, target_addr, 0);
  2654. }
  2655. g_free(vec);
  2656. }
  2657. static inline int target_to_host_sock_type(int *type)
  2658. {
  2659. int host_type = 0;
  2660. int target_type = *type;
  2661. switch (target_type & TARGET_SOCK_TYPE_MASK) {
  2662. case TARGET_SOCK_DGRAM:
  2663. host_type = SOCK_DGRAM;
  2664. break;
  2665. case TARGET_SOCK_STREAM:
  2666. host_type = SOCK_STREAM;
  2667. break;
  2668. default:
  2669. host_type = target_type & TARGET_SOCK_TYPE_MASK;
  2670. break;
  2671. }
  2672. if (target_type & TARGET_SOCK_CLOEXEC) {
  2673. #if defined(SOCK_CLOEXEC)
  2674. host_type |= SOCK_CLOEXEC;
  2675. #else
  2676. return -TARGET_EINVAL;
  2677. #endif
  2678. }
  2679. if (target_type & TARGET_SOCK_NONBLOCK) {
  2680. #if defined(SOCK_NONBLOCK)
  2681. host_type |= SOCK_NONBLOCK;
  2682. #elif !defined(O_NONBLOCK)
  2683. return -TARGET_EINVAL;
  2684. #endif
  2685. }
  2686. *type = host_type;
  2687. return 0;
  2688. }
  2689. /* Try to emulate socket type flags after socket creation. */
  2690. static int sock_flags_fixup(int fd, int target_type)
  2691. {
  2692. #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
  2693. if (target_type & TARGET_SOCK_NONBLOCK) {
  2694. int flags = fcntl(fd, F_GETFL);
  2695. if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) {
  2696. close(fd);
  2697. return -TARGET_EINVAL;
  2698. }
  2699. }
  2700. #endif
  2701. return fd;
  2702. }
  2703. /* do_socket() Must return target values and target errnos. */
  2704. static abi_long do_socket(int domain, int type, int protocol)
  2705. {
  2706. int target_type = type;
  2707. int ret;
  2708. ret = target_to_host_sock_type(&type);
  2709. if (ret) {
  2710. return ret;
  2711. }
  2712. if (domain == PF_NETLINK && !(
  2713. #ifdef CONFIG_RTNETLINK
  2714. protocol == NETLINK_ROUTE ||
  2715. #endif
  2716. protocol == NETLINK_KOBJECT_UEVENT ||
  2717. protocol == NETLINK_AUDIT)) {
  2718. return -EPFNOSUPPORT;
  2719. }
  2720. if (domain == AF_PACKET ||
  2721. (domain == AF_INET && type == SOCK_PACKET)) {
  2722. protocol = tswap16(protocol);
  2723. }
  2724. ret = get_errno(socket(domain, type, protocol));
  2725. if (ret >= 0) {
  2726. ret = sock_flags_fixup(ret, target_type);
  2727. if (type == SOCK_PACKET) {
  2728. /* Manage an obsolete case :
  2729. * if socket type is SOCK_PACKET, bind by name
  2730. */
  2731. fd_trans_register(ret, &target_packet_trans);
  2732. } else if (domain == PF_NETLINK) {
  2733. switch (protocol) {
  2734. #ifdef CONFIG_RTNETLINK
  2735. case NETLINK_ROUTE:
  2736. fd_trans_register(ret, &target_netlink_route_trans);
  2737. break;
  2738. #endif
  2739. case NETLINK_KOBJECT_UEVENT:
  2740. /* nothing to do: messages are strings */
  2741. break;
  2742. case NETLINK_AUDIT:
  2743. fd_trans_register(ret, &target_netlink_audit_trans);
  2744. break;
  2745. default:
  2746. g_assert_not_reached();
  2747. }
  2748. }
  2749. }
  2750. return ret;
  2751. }
  2752. /* do_bind() Must return target values and target errnos. */
  2753. static abi_long do_bind(int sockfd, abi_ulong target_addr,
  2754. socklen_t addrlen)
  2755. {
  2756. void *addr;
  2757. abi_long ret;
  2758. if ((int)addrlen < 0) {
  2759. return -TARGET_EINVAL;
  2760. }
  2761. addr = alloca(addrlen+1);
  2762. ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
  2763. if (ret)
  2764. return ret;
  2765. return get_errno(bind(sockfd, addr, addrlen));
  2766. }
  2767. /* do_connect() Must return target values and target errnos. */
  2768. static abi_long do_connect(int sockfd, abi_ulong target_addr,
  2769. socklen_t addrlen)
  2770. {
  2771. void *addr;
  2772. abi_long ret;
  2773. if ((int)addrlen < 0) {
  2774. return -TARGET_EINVAL;
  2775. }
  2776. addr = alloca(addrlen+1);
  2777. ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
  2778. if (ret)
  2779. return ret;
  2780. return get_errno(safe_connect(sockfd, addr, addrlen));
  2781. }
  2782. /* do_sendrecvmsg_locked() Must return target values and target errnos. */
  2783. static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
  2784. int flags, int send)
  2785. {
  2786. abi_long ret, len;
  2787. struct msghdr msg;
  2788. abi_ulong count;
  2789. struct iovec *vec;
  2790. abi_ulong target_vec;
  2791. if (msgp->msg_name) {
  2792. msg.msg_namelen = tswap32(msgp->msg_namelen);
  2793. msg.msg_name = alloca(msg.msg_namelen+1);
  2794. ret = target_to_host_sockaddr(fd, msg.msg_name,
  2795. tswapal(msgp->msg_name),
  2796. msg.msg_namelen);
  2797. if (ret == -TARGET_EFAULT) {
  2798. /* For connected sockets msg_name and msg_namelen must
  2799. * be ignored, so returning EFAULT immediately is wrong.
  2800. * Instead, pass a bad msg_name to the host kernel, and
  2801. * let it decide whether to return EFAULT or not.
  2802. */
  2803. msg.msg_name = (void *)-1;
  2804. } else if (ret) {
  2805. goto out2;
  2806. }
  2807. } else {
  2808. msg.msg_name = NULL;
  2809. msg.msg_namelen = 0;
  2810. }
  2811. msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
  2812. msg.msg_control = alloca(msg.msg_controllen);
  2813. memset(msg.msg_control, 0, msg.msg_controllen);
  2814. msg.msg_flags = tswap32(msgp->msg_flags);
  2815. count = tswapal(msgp->msg_iovlen);
  2816. target_vec = tswapal(msgp->msg_iov);
  2817. if (count > IOV_MAX) {
  2818. /* sendrcvmsg returns a different errno for this condition than
  2819. * readv/writev, so we must catch it here before lock_iovec() does.
  2820. */
  2821. ret = -TARGET_EMSGSIZE;
  2822. goto out2;
  2823. }
  2824. vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
  2825. target_vec, count, send);
  2826. if (vec == NULL) {
  2827. ret = -host_to_target_errno(errno);
  2828. goto out2;
  2829. }
  2830. msg.msg_iovlen = count;
  2831. msg.msg_iov = vec;
  2832. if (send) {
  2833. if (fd_trans_target_to_host_data(fd)) {
  2834. void *host_msg;
  2835. host_msg = g_malloc(msg.msg_iov->iov_len);
  2836. memcpy(host_msg, msg.msg_iov->iov_base, msg.msg_iov->iov_len);
  2837. ret = fd_trans_target_to_host_data(fd)(host_msg,
  2838. msg.msg_iov->iov_len);
  2839. if (ret >= 0) {
  2840. msg.msg_iov->iov_base = host_msg;
  2841. ret = get_errno(safe_sendmsg(fd, &msg, flags));
  2842. }
  2843. g_free(host_msg);
  2844. } else {
  2845. ret = target_to_host_cmsg(&msg, msgp);
  2846. if (ret == 0) {
  2847. ret = get_errno(safe_sendmsg(fd, &msg, flags));
  2848. }
  2849. }
  2850. } else {
  2851. ret = get_errno(safe_recvmsg(fd, &msg, flags));
  2852. if (!is_error(ret)) {
  2853. len = ret;
  2854. if (fd_trans_host_to_target_data(fd)) {
  2855. ret = fd_trans_host_to_target_data(fd)(msg.msg_iov->iov_base,
  2856. MIN(msg.msg_iov->iov_len, len));
  2857. } else {
  2858. ret = host_to_target_cmsg(msgp, &msg);
  2859. }
  2860. if (!is_error(ret)) {
  2861. msgp->msg_namelen = tswap32(msg.msg_namelen);
  2862. msgp->msg_flags = tswap32(msg.msg_flags);
  2863. if (msg.msg_name != NULL && msg.msg_name != (void *)-1) {
  2864. ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
  2865. msg.msg_name, msg.msg_namelen);
  2866. if (ret) {
  2867. goto out;
  2868. }
  2869. }
  2870. ret = len;
  2871. }
  2872. }
  2873. }
  2874. out:
  2875. unlock_iovec(vec, target_vec, count, !send);
  2876. out2:
  2877. return ret;
  2878. }
  2879. static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
  2880. int flags, int send)
  2881. {
  2882. abi_long ret;
  2883. struct target_msghdr *msgp;
  2884. if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
  2885. msgp,
  2886. target_msg,
  2887. send ? 1 : 0)) {
  2888. return -TARGET_EFAULT;
  2889. }
  2890. ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
  2891. unlock_user_struct(msgp, target_msg, send ? 0 : 1);
  2892. return ret;
  2893. }
  2894. /* We don't rely on the C library to have sendmmsg/recvmmsg support,
  2895. * so it might not have this *mmsg-specific flag either.
  2896. */
  2897. #ifndef MSG_WAITFORONE
  2898. #define MSG_WAITFORONE 0x10000
  2899. #endif
  2900. static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec,
  2901. unsigned int vlen, unsigned int flags,
  2902. int send)
  2903. {
  2904. struct target_mmsghdr *mmsgp;
  2905. abi_long ret = 0;
  2906. int i;
  2907. if (vlen > UIO_MAXIOV) {
  2908. vlen = UIO_MAXIOV;
  2909. }
  2910. mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1);
  2911. if (!mmsgp) {
  2912. return -TARGET_EFAULT;
  2913. }
  2914. for (i = 0; i < vlen; i++) {
  2915. ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send);
  2916. if (is_error(ret)) {
  2917. break;
  2918. }
  2919. mmsgp[i].msg_len = tswap32(ret);
  2920. /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
  2921. if (flags & MSG_WAITFORONE) {
  2922. flags |= MSG_DONTWAIT;
  2923. }
  2924. }
  2925. unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i);
  2926. /* Return number of datagrams sent if we sent any at all;
  2927. * otherwise return the error.
  2928. */
  2929. if (i) {
  2930. return i;
  2931. }
  2932. return ret;
  2933. }
  2934. /* do_accept4() Must return target values and target errnos. */
  2935. static abi_long do_accept4(int fd, abi_ulong target_addr,
  2936. abi_ulong target_addrlen_addr, int flags)
  2937. {
  2938. socklen_t addrlen, ret_addrlen;
  2939. void *addr;
  2940. abi_long ret;
  2941. int host_flags;
  2942. host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
  2943. if (target_addr == 0) {
  2944. return get_errno(safe_accept4(fd, NULL, NULL, host_flags));
  2945. }
  2946. /* linux returns EINVAL if addrlen pointer is invalid */
  2947. if (get_user_u32(addrlen, target_addrlen_addr))
  2948. return -TARGET_EINVAL;
  2949. if ((int)addrlen < 0) {
  2950. return -TARGET_EINVAL;
  2951. }
  2952. if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
  2953. return -TARGET_EINVAL;
  2954. addr = alloca(addrlen);
  2955. ret_addrlen = addrlen;
  2956. ret = get_errno(safe_accept4(fd, addr, &ret_addrlen, host_flags));
  2957. if (!is_error(ret)) {
  2958. host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
  2959. if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
  2960. ret = -TARGET_EFAULT;
  2961. }
  2962. }
  2963. return ret;
  2964. }
  2965. /* do_getpeername() Must return target values and target errnos. */
  2966. static abi_long do_getpeername(int fd, abi_ulong target_addr,
  2967. abi_ulong target_addrlen_addr)
  2968. {
  2969. socklen_t addrlen, ret_addrlen;
  2970. void *addr;
  2971. abi_long ret;
  2972. if (get_user_u32(addrlen, target_addrlen_addr))
  2973. return -TARGET_EFAULT;
  2974. if ((int)addrlen < 0) {
  2975. return -TARGET_EINVAL;
  2976. }
  2977. if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
  2978. return -TARGET_EFAULT;
  2979. addr = alloca(addrlen);
  2980. ret_addrlen = addrlen;
  2981. ret = get_errno(getpeername(fd, addr, &ret_addrlen));
  2982. if (!is_error(ret)) {
  2983. host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
  2984. if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
  2985. ret = -TARGET_EFAULT;
  2986. }
  2987. }
  2988. return ret;
  2989. }
  2990. /* do_getsockname() Must return target values and target errnos. */
  2991. static abi_long do_getsockname(int fd, abi_ulong target_addr,
  2992. abi_ulong target_addrlen_addr)
  2993. {
  2994. socklen_t addrlen, ret_addrlen;
  2995. void *addr;
  2996. abi_long ret;
  2997. if (get_user_u32(addrlen, target_addrlen_addr))
  2998. return -TARGET_EFAULT;
  2999. if ((int)addrlen < 0) {
  3000. return -TARGET_EINVAL;
  3001. }
  3002. if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
  3003. return -TARGET_EFAULT;
  3004. addr = alloca(addrlen);
  3005. ret_addrlen = addrlen;
  3006. ret = get_errno(getsockname(fd, addr, &ret_addrlen));
  3007. if (!is_error(ret)) {
  3008. host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
  3009. if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
  3010. ret = -TARGET_EFAULT;
  3011. }
  3012. }
  3013. return ret;
  3014. }
  3015. /* do_socketpair() Must return target values and target errnos. */
  3016. static abi_long do_socketpair(int domain, int type, int protocol,
  3017. abi_ulong target_tab_addr)
  3018. {
  3019. int tab[2];
  3020. abi_long ret;
  3021. target_to_host_sock_type(&type);
  3022. ret = get_errno(socketpair(domain, type, protocol, tab));
  3023. if (!is_error(ret)) {
  3024. if (put_user_s32(tab[0], target_tab_addr)
  3025. || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
  3026. ret = -TARGET_EFAULT;
  3027. }
  3028. return ret;
  3029. }
  3030. /* do_sendto() Must return target values and target errnos. */
  3031. static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
  3032. abi_ulong target_addr, socklen_t addrlen)
  3033. {
  3034. void *addr;
  3035. void *host_msg;
  3036. void *copy_msg = NULL;
  3037. abi_long ret;
  3038. if ((int)addrlen < 0) {
  3039. return -TARGET_EINVAL;
  3040. }
  3041. host_msg = lock_user(VERIFY_READ, msg, len, 1);
  3042. if (!host_msg)
  3043. return -TARGET_EFAULT;
  3044. if (fd_trans_target_to_host_data(fd)) {
  3045. copy_msg = host_msg;
  3046. host_msg = g_malloc(len);
  3047. memcpy(host_msg, copy_msg, len);
  3048. ret = fd_trans_target_to_host_data(fd)(host_msg, len);
  3049. if (ret < 0) {
  3050. goto fail;
  3051. }
  3052. }
  3053. if (target_addr) {
  3054. addr = alloca(addrlen+1);
  3055. ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen);
  3056. if (ret) {
  3057. goto fail;
  3058. }
  3059. ret = get_errno(safe_sendto(fd, host_msg, len, flags, addr, addrlen));
  3060. } else {
  3061. ret = get_errno(safe_sendto(fd, host_msg, len, flags, NULL, 0));
  3062. }
  3063. fail:
  3064. if (copy_msg) {
  3065. g_free(host_msg);
  3066. host_msg = copy_msg;
  3067. }
  3068. unlock_user(host_msg, msg, 0);
  3069. return ret;
  3070. }
  3071. /* do_recvfrom() Must return target values and target errnos. */
  3072. static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
  3073. abi_ulong target_addr,
  3074. abi_ulong target_addrlen)
  3075. {
  3076. socklen_t addrlen, ret_addrlen;
  3077. void *addr;
  3078. void *host_msg;
  3079. abi_long ret;
  3080. host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
  3081. if (!host_msg)
  3082. return -TARGET_EFAULT;
  3083. if (target_addr) {
  3084. if (get_user_u32(addrlen, target_addrlen)) {
  3085. ret = -TARGET_EFAULT;
  3086. goto fail;
  3087. }
  3088. if ((int)addrlen < 0) {
  3089. ret = -TARGET_EINVAL;
  3090. goto fail;
  3091. }
  3092. addr = alloca(addrlen);
  3093. ret_addrlen = addrlen;
  3094. ret = get_errno(safe_recvfrom(fd, host_msg, len, flags,
  3095. addr, &ret_addrlen));
  3096. } else {
  3097. addr = NULL; /* To keep compiler quiet. */
  3098. addrlen = 0; /* To keep compiler quiet. */
  3099. ret = get_errno(safe_recvfrom(fd, host_msg, len, flags, NULL, 0));
  3100. }
  3101. if (!is_error(ret)) {
  3102. if (fd_trans_host_to_target_data(fd)) {
  3103. abi_long trans;
  3104. trans = fd_trans_host_to_target_data(fd)(host_msg, MIN(ret, len));
  3105. if (is_error(trans)) {
  3106. ret = trans;
  3107. goto fail;
  3108. }
  3109. }
  3110. if (target_addr) {
  3111. host_to_target_sockaddr(target_addr, addr,
  3112. MIN(addrlen, ret_addrlen));
  3113. if (put_user_u32(ret_addrlen, target_addrlen)) {
  3114. ret = -TARGET_EFAULT;
  3115. goto fail;
  3116. }
  3117. }
  3118. unlock_user(host_msg, msg, len);
  3119. } else {
  3120. fail:
  3121. unlock_user(host_msg, msg, 0);
  3122. }
  3123. return ret;
  3124. }
  3125. #ifdef TARGET_NR_socketcall
  3126. /* do_socketcall() must return target values and target errnos. */
  3127. static abi_long do_socketcall(int num, abi_ulong vptr)
  3128. {
  3129. static const unsigned nargs[] = { /* number of arguments per operation */
  3130. [TARGET_SYS_SOCKET] = 3, /* domain, type, protocol */
  3131. [TARGET_SYS_BIND] = 3, /* fd, addr, addrlen */
  3132. [TARGET_SYS_CONNECT] = 3, /* fd, addr, addrlen */
  3133. [TARGET_SYS_LISTEN] = 2, /* fd, backlog */
  3134. [TARGET_SYS_ACCEPT] = 3, /* fd, addr, addrlen */
  3135. [TARGET_SYS_GETSOCKNAME] = 3, /* fd, addr, addrlen */
  3136. [TARGET_SYS_GETPEERNAME] = 3, /* fd, addr, addrlen */
  3137. [TARGET_SYS_SOCKETPAIR] = 4, /* domain, type, protocol, tab */
  3138. [TARGET_SYS_SEND] = 4, /* fd, msg, len, flags */
  3139. [TARGET_SYS_RECV] = 4, /* fd, msg, len, flags */
  3140. [TARGET_SYS_SENDTO] = 6, /* fd, msg, len, flags, addr, addrlen */
  3141. [TARGET_SYS_RECVFROM] = 6, /* fd, msg, len, flags, addr, addrlen */
  3142. [TARGET_SYS_SHUTDOWN] = 2, /* fd, how */
  3143. [TARGET_SYS_SETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
  3144. [TARGET_SYS_GETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
  3145. [TARGET_SYS_SENDMSG] = 3, /* fd, msg, flags */
  3146. [TARGET_SYS_RECVMSG] = 3, /* fd, msg, flags */
  3147. [TARGET_SYS_ACCEPT4] = 4, /* fd, addr, addrlen, flags */
  3148. [TARGET_SYS_RECVMMSG] = 4, /* fd, msgvec, vlen, flags */
  3149. [TARGET_SYS_SENDMMSG] = 4, /* fd, msgvec, vlen, flags */
  3150. };
  3151. abi_long a[6]; /* max 6 args */
  3152. unsigned i;
  3153. /* check the range of the first argument num */
  3154. /* (TARGET_SYS_SENDMMSG is the highest among TARGET_SYS_xxx) */
  3155. if (num < 1 || num > TARGET_SYS_SENDMMSG) {
  3156. return -TARGET_EINVAL;
  3157. }
  3158. /* ensure we have space for args */
  3159. if (nargs[num] > ARRAY_SIZE(a)) {
  3160. return -TARGET_EINVAL;
  3161. }
  3162. /* collect the arguments in a[] according to nargs[] */
  3163. for (i = 0; i < nargs[num]; ++i) {
  3164. if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) {
  3165. return -TARGET_EFAULT;
  3166. }
  3167. }
  3168. /* now when we have the args, invoke the appropriate underlying function */
  3169. switch (num) {
  3170. case TARGET_SYS_SOCKET: /* domain, type, protocol */
  3171. return do_socket(a[0], a[1], a[2]);
  3172. case TARGET_SYS_BIND: /* sockfd, addr, addrlen */
  3173. return do_bind(a[0], a[1], a[2]);
  3174. case TARGET_SYS_CONNECT: /* sockfd, addr, addrlen */
  3175. return do_connect(a[0], a[1], a[2]);
  3176. case TARGET_SYS_LISTEN: /* sockfd, backlog */
  3177. return get_errno(listen(a[0], a[1]));
  3178. case TARGET_SYS_ACCEPT: /* sockfd, addr, addrlen */
  3179. return do_accept4(a[0], a[1], a[2], 0);
  3180. case TARGET_SYS_GETSOCKNAME: /* sockfd, addr, addrlen */
  3181. return do_getsockname(a[0], a[1], a[2]);
  3182. case TARGET_SYS_GETPEERNAME: /* sockfd, addr, addrlen */
  3183. return do_getpeername(a[0], a[1], a[2]);
  3184. case TARGET_SYS_SOCKETPAIR: /* domain, type, protocol, tab */
  3185. return do_socketpair(a[0], a[1], a[2], a[3]);
  3186. case TARGET_SYS_SEND: /* sockfd, msg, len, flags */
  3187. return do_sendto(a[0], a[1], a[2], a[3], 0, 0);
  3188. case TARGET_SYS_RECV: /* sockfd, msg, len, flags */
  3189. return do_recvfrom(a[0], a[1], a[2], a[3], 0, 0);
  3190. case TARGET_SYS_SENDTO: /* sockfd, msg, len, flags, addr, addrlen */
  3191. return do_sendto(a[0], a[1], a[2], a[3], a[4], a[5]);
  3192. case TARGET_SYS_RECVFROM: /* sockfd, msg, len, flags, addr, addrlen */
  3193. return do_recvfrom(a[0], a[1], a[2], a[3], a[4], a[5]);
  3194. case TARGET_SYS_SHUTDOWN: /* sockfd, how */
  3195. return get_errno(shutdown(a[0], a[1]));
  3196. case TARGET_SYS_SETSOCKOPT: /* sockfd, level, optname, optval, optlen */
  3197. return do_setsockopt(a[0], a[1], a[2], a[3], a[4]);
  3198. case TARGET_SYS_GETSOCKOPT: /* sockfd, level, optname, optval, optlen */
  3199. return do_getsockopt(a[0], a[1], a[2], a[3], a[4]);
  3200. case TARGET_SYS_SENDMSG: /* sockfd, msg, flags */
  3201. return do_sendrecvmsg(a[0], a[1], a[2], 1);
  3202. case TARGET_SYS_RECVMSG: /* sockfd, msg, flags */
  3203. return do_sendrecvmsg(a[0], a[1], a[2], 0);
  3204. case TARGET_SYS_ACCEPT4: /* sockfd, addr, addrlen, flags */
  3205. return do_accept4(a[0], a[1], a[2], a[3]);
  3206. case TARGET_SYS_RECVMMSG: /* sockfd, msgvec, vlen, flags */
  3207. return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 0);
  3208. case TARGET_SYS_SENDMMSG: /* sockfd, msgvec, vlen, flags */
  3209. return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 1);
  3210. default:
  3211. qemu_log_mask(LOG_UNIMP, "Unsupported socketcall: %d\n", num);
  3212. return -TARGET_EINVAL;
  3213. }
  3214. }
  3215. #endif
  3216. #define N_SHM_REGIONS 32
  3217. static struct shm_region {
  3218. abi_ulong start;
  3219. abi_ulong size;
  3220. bool in_use;
  3221. } shm_regions[N_SHM_REGIONS];
  3222. #ifndef TARGET_SEMID64_DS
  3223. /* asm-generic version of this struct */
  3224. struct target_semid64_ds
  3225. {
  3226. struct target_ipc_perm sem_perm;
  3227. abi_ulong sem_otime;
  3228. #if TARGET_ABI_BITS == 32
  3229. abi_ulong __unused1;
  3230. #endif
  3231. abi_ulong sem_ctime;
  3232. #if TARGET_ABI_BITS == 32
  3233. abi_ulong __unused2;
  3234. #endif
  3235. abi_ulong sem_nsems;
  3236. abi_ulong __unused3;
  3237. abi_ulong __unused4;
  3238. };
  3239. #endif
  3240. static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
  3241. abi_ulong target_addr)
  3242. {
  3243. struct target_ipc_perm *target_ip;
  3244. struct target_semid64_ds *target_sd;
  3245. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  3246. return -TARGET_EFAULT;
  3247. target_ip = &(target_sd->sem_perm);
  3248. host_ip->__key = tswap32(target_ip->__key);
  3249. host_ip->uid = tswap32(target_ip->uid);
  3250. host_ip->gid = tswap32(target_ip->gid);
  3251. host_ip->cuid = tswap32(target_ip->cuid);
  3252. host_ip->cgid = tswap32(target_ip->cgid);
  3253. #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
  3254. host_ip->mode = tswap32(target_ip->mode);
  3255. #else
  3256. host_ip->mode = tswap16(target_ip->mode);
  3257. #endif
  3258. #if defined(TARGET_PPC)
  3259. host_ip->__seq = tswap32(target_ip->__seq);
  3260. #else
  3261. host_ip->__seq = tswap16(target_ip->__seq);
  3262. #endif
  3263. unlock_user_struct(target_sd, target_addr, 0);
  3264. return 0;
  3265. }
  3266. static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
  3267. struct ipc_perm *host_ip)
  3268. {
  3269. struct target_ipc_perm *target_ip;
  3270. struct target_semid64_ds *target_sd;
  3271. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  3272. return -TARGET_EFAULT;
  3273. target_ip = &(target_sd->sem_perm);
  3274. target_ip->__key = tswap32(host_ip->__key);
  3275. target_ip->uid = tswap32(host_ip->uid);
  3276. target_ip->gid = tswap32(host_ip->gid);
  3277. target_ip->cuid = tswap32(host_ip->cuid);
  3278. target_ip->cgid = tswap32(host_ip->cgid);
  3279. #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
  3280. target_ip->mode = tswap32(host_ip->mode);
  3281. #else
  3282. target_ip->mode = tswap16(host_ip->mode);
  3283. #endif
  3284. #if defined(TARGET_PPC)
  3285. target_ip->__seq = tswap32(host_ip->__seq);
  3286. #else
  3287. target_ip->__seq = tswap16(host_ip->__seq);
  3288. #endif
  3289. unlock_user_struct(target_sd, target_addr, 1);
  3290. return 0;
  3291. }
  3292. static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
  3293. abi_ulong target_addr)
  3294. {
  3295. struct target_semid64_ds *target_sd;
  3296. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  3297. return -TARGET_EFAULT;
  3298. if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
  3299. return -TARGET_EFAULT;
  3300. host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
  3301. host_sd->sem_otime = tswapal(target_sd->sem_otime);
  3302. host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
  3303. unlock_user_struct(target_sd, target_addr, 0);
  3304. return 0;
  3305. }
  3306. static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
  3307. struct semid_ds *host_sd)
  3308. {
  3309. struct target_semid64_ds *target_sd;
  3310. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  3311. return -TARGET_EFAULT;
  3312. if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
  3313. return -TARGET_EFAULT;
  3314. target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
  3315. target_sd->sem_otime = tswapal(host_sd->sem_otime);
  3316. target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
  3317. unlock_user_struct(target_sd, target_addr, 1);
  3318. return 0;
  3319. }
  3320. struct target_seminfo {
  3321. int semmap;
  3322. int semmni;
  3323. int semmns;
  3324. int semmnu;
  3325. int semmsl;
  3326. int semopm;
  3327. int semume;
  3328. int semusz;
  3329. int semvmx;
  3330. int semaem;
  3331. };
  3332. static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
  3333. struct seminfo *host_seminfo)
  3334. {
  3335. struct target_seminfo *target_seminfo;
  3336. if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
  3337. return -TARGET_EFAULT;
  3338. __put_user(host_seminfo->semmap, &target_seminfo->semmap);
  3339. __put_user(host_seminfo->semmni, &target_seminfo->semmni);
  3340. __put_user(host_seminfo->semmns, &target_seminfo->semmns);
  3341. __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
  3342. __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
  3343. __put_user(host_seminfo->semopm, &target_seminfo->semopm);
  3344. __put_user(host_seminfo->semume, &target_seminfo->semume);
  3345. __put_user(host_seminfo->semusz, &target_seminfo->semusz);
  3346. __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
  3347. __put_user(host_seminfo->semaem, &target_seminfo->semaem);
  3348. unlock_user_struct(target_seminfo, target_addr, 1);
  3349. return 0;
  3350. }
  3351. union semun {
  3352. int val;
  3353. struct semid_ds *buf;
  3354. unsigned short *array;
  3355. struct seminfo *__buf;
  3356. };
  3357. union target_semun {
  3358. int val;
  3359. abi_ulong buf;
  3360. abi_ulong array;
  3361. abi_ulong __buf;
  3362. };
  3363. static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
  3364. abi_ulong target_addr)
  3365. {
  3366. int nsems;
  3367. unsigned short *array;
  3368. union semun semun;
  3369. struct semid_ds semid_ds;
  3370. int i, ret;
  3371. semun.buf = &semid_ds;
  3372. ret = semctl(semid, 0, IPC_STAT, semun);
  3373. if (ret == -1)
  3374. return get_errno(ret);
  3375. nsems = semid_ds.sem_nsems;
  3376. *host_array = g_try_new(unsigned short, nsems);
  3377. if (!*host_array) {
  3378. return -TARGET_ENOMEM;
  3379. }
  3380. array = lock_user(VERIFY_READ, target_addr,
  3381. nsems*sizeof(unsigned short), 1);
  3382. if (!array) {
  3383. g_free(*host_array);
  3384. return -TARGET_EFAULT;
  3385. }
  3386. for(i=0; i<nsems; i++) {
  3387. __get_user((*host_array)[i], &array[i]);
  3388. }
  3389. unlock_user(array, target_addr, 0);
  3390. return 0;
  3391. }
  3392. static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
  3393. unsigned short **host_array)
  3394. {
  3395. int nsems;
  3396. unsigned short *array;
  3397. union semun semun;
  3398. struct semid_ds semid_ds;
  3399. int i, ret;
  3400. semun.buf = &semid_ds;
  3401. ret = semctl(semid, 0, IPC_STAT, semun);
  3402. if (ret == -1)
  3403. return get_errno(ret);
  3404. nsems = semid_ds.sem_nsems;
  3405. array = lock_user(VERIFY_WRITE, target_addr,
  3406. nsems*sizeof(unsigned short), 0);
  3407. if (!array)
  3408. return -TARGET_EFAULT;
  3409. for(i=0; i<nsems; i++) {
  3410. __put_user((*host_array)[i], &array[i]);
  3411. }
  3412. g_free(*host_array);
  3413. unlock_user(array, target_addr, 1);
  3414. return 0;
  3415. }
  3416. static inline abi_long do_semctl(int semid, int semnum, int cmd,
  3417. abi_ulong target_arg)
  3418. {
  3419. union target_semun target_su = { .buf = target_arg };
  3420. union semun arg;
  3421. struct semid_ds dsarg;
  3422. unsigned short *array = NULL;
  3423. struct seminfo seminfo;
  3424. abi_long ret = -TARGET_EINVAL;
  3425. abi_long err;
  3426. cmd &= 0xff;
  3427. switch( cmd ) {
  3428. case GETVAL:
  3429. case SETVAL:
  3430. /* In 64 bit cross-endian situations, we will erroneously pick up
  3431. * the wrong half of the union for the "val" element. To rectify
  3432. * this, the entire 8-byte structure is byteswapped, followed by
  3433. * a swap of the 4 byte val field. In other cases, the data is
  3434. * already in proper host byte order. */
  3435. if (sizeof(target_su.val) != (sizeof(target_su.buf))) {
  3436. target_su.buf = tswapal(target_su.buf);
  3437. arg.val = tswap32(target_su.val);
  3438. } else {
  3439. arg.val = target_su.val;
  3440. }
  3441. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3442. break;
  3443. case GETALL:
  3444. case SETALL:
  3445. err = target_to_host_semarray(semid, &array, target_su.array);
  3446. if (err)
  3447. return err;
  3448. arg.array = array;
  3449. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3450. err = host_to_target_semarray(semid, target_su.array, &array);
  3451. if (err)
  3452. return err;
  3453. break;
  3454. case IPC_STAT:
  3455. case IPC_SET:
  3456. case SEM_STAT:
  3457. err = target_to_host_semid_ds(&dsarg, target_su.buf);
  3458. if (err)
  3459. return err;
  3460. arg.buf = &dsarg;
  3461. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3462. err = host_to_target_semid_ds(target_su.buf, &dsarg);
  3463. if (err)
  3464. return err;
  3465. break;
  3466. case IPC_INFO:
  3467. case SEM_INFO:
  3468. arg.__buf = &seminfo;
  3469. ret = get_errno(semctl(semid, semnum, cmd, arg));
  3470. err = host_to_target_seminfo(target_su.__buf, &seminfo);
  3471. if (err)
  3472. return err;
  3473. break;
  3474. case IPC_RMID:
  3475. case GETPID:
  3476. case GETNCNT:
  3477. case GETZCNT:
  3478. ret = get_errno(semctl(semid, semnum, cmd, NULL));
  3479. break;
  3480. }
  3481. return ret;
  3482. }
  3483. struct target_sembuf {
  3484. unsigned short sem_num;
  3485. short sem_op;
  3486. short sem_flg;
  3487. };
  3488. static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
  3489. abi_ulong target_addr,
  3490. unsigned nsops)
  3491. {
  3492. struct target_sembuf *target_sembuf;
  3493. int i;
  3494. target_sembuf = lock_user(VERIFY_READ, target_addr,
  3495. nsops*sizeof(struct target_sembuf), 1);
  3496. if (!target_sembuf)
  3497. return -TARGET_EFAULT;
  3498. for(i=0; i<nsops; i++) {
  3499. __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
  3500. __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
  3501. __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
  3502. }
  3503. unlock_user(target_sembuf, target_addr, 0);
  3504. return 0;
  3505. }
  3506. static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
  3507. {
  3508. struct sembuf sops[nsops];
  3509. abi_long ret;
  3510. if (target_to_host_sembuf(sops, ptr, nsops))
  3511. return -TARGET_EFAULT;
  3512. ret = -TARGET_ENOSYS;
  3513. #ifdef __NR_semtimedop
  3514. ret = get_errno(safe_semtimedop(semid, sops, nsops, NULL));
  3515. #endif
  3516. #ifdef __NR_ipc
  3517. if (ret == -TARGET_ENOSYS) {
  3518. ret = get_errno(safe_ipc(IPCOP_semtimedop, semid, nsops, 0, sops, 0));
  3519. }
  3520. #endif
  3521. return ret;
  3522. }
  3523. struct target_msqid_ds
  3524. {
  3525. struct target_ipc_perm msg_perm;
  3526. abi_ulong msg_stime;
  3527. #if TARGET_ABI_BITS == 32
  3528. abi_ulong __unused1;
  3529. #endif
  3530. abi_ulong msg_rtime;
  3531. #if TARGET_ABI_BITS == 32
  3532. abi_ulong __unused2;
  3533. #endif
  3534. abi_ulong msg_ctime;
  3535. #if TARGET_ABI_BITS == 32
  3536. abi_ulong __unused3;
  3537. #endif
  3538. abi_ulong __msg_cbytes;
  3539. abi_ulong msg_qnum;
  3540. abi_ulong msg_qbytes;
  3541. abi_ulong msg_lspid;
  3542. abi_ulong msg_lrpid;
  3543. abi_ulong __unused4;
  3544. abi_ulong __unused5;
  3545. };
  3546. static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
  3547. abi_ulong target_addr)
  3548. {
  3549. struct target_msqid_ds *target_md;
  3550. if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
  3551. return -TARGET_EFAULT;
  3552. if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
  3553. return -TARGET_EFAULT;
  3554. host_md->msg_stime = tswapal(target_md->msg_stime);
  3555. host_md->msg_rtime = tswapal(target_md->msg_rtime);
  3556. host_md->msg_ctime = tswapal(target_md->msg_ctime);
  3557. host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
  3558. host_md->msg_qnum = tswapal(target_md->msg_qnum);
  3559. host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
  3560. host_md->msg_lspid = tswapal(target_md->msg_lspid);
  3561. host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
  3562. unlock_user_struct(target_md, target_addr, 0);
  3563. return 0;
  3564. }
  3565. static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
  3566. struct msqid_ds *host_md)
  3567. {
  3568. struct target_msqid_ds *target_md;
  3569. if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
  3570. return -TARGET_EFAULT;
  3571. if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
  3572. return -TARGET_EFAULT;
  3573. target_md->msg_stime = tswapal(host_md->msg_stime);
  3574. target_md->msg_rtime = tswapal(host_md->msg_rtime);
  3575. target_md->msg_ctime = tswapal(host_md->msg_ctime);
  3576. target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
  3577. target_md->msg_qnum = tswapal(host_md->msg_qnum);
  3578. target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
  3579. target_md->msg_lspid = tswapal(host_md->msg_lspid);
  3580. target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
  3581. unlock_user_struct(target_md, target_addr, 1);
  3582. return 0;
  3583. }
  3584. struct target_msginfo {
  3585. int msgpool;
  3586. int msgmap;
  3587. int msgmax;
  3588. int msgmnb;
  3589. int msgmni;
  3590. int msgssz;
  3591. int msgtql;
  3592. unsigned short int msgseg;
  3593. };
  3594. static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
  3595. struct msginfo *host_msginfo)
  3596. {
  3597. struct target_msginfo *target_msginfo;
  3598. if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
  3599. return -TARGET_EFAULT;
  3600. __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
  3601. __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
  3602. __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
  3603. __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
  3604. __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
  3605. __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
  3606. __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
  3607. __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
  3608. unlock_user_struct(target_msginfo, target_addr, 1);
  3609. return 0;
  3610. }
  3611. static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
  3612. {
  3613. struct msqid_ds dsarg;
  3614. struct msginfo msginfo;
  3615. abi_long ret = -TARGET_EINVAL;
  3616. cmd &= 0xff;
  3617. switch (cmd) {
  3618. case IPC_STAT:
  3619. case IPC_SET:
  3620. case MSG_STAT:
  3621. if (target_to_host_msqid_ds(&dsarg,ptr))
  3622. return -TARGET_EFAULT;
  3623. ret = get_errno(msgctl(msgid, cmd, &dsarg));
  3624. if (host_to_target_msqid_ds(ptr,&dsarg))
  3625. return -TARGET_EFAULT;
  3626. break;
  3627. case IPC_RMID:
  3628. ret = get_errno(msgctl(msgid, cmd, NULL));
  3629. break;
  3630. case IPC_INFO:
  3631. case MSG_INFO:
  3632. ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
  3633. if (host_to_target_msginfo(ptr, &msginfo))
  3634. return -TARGET_EFAULT;
  3635. break;
  3636. }
  3637. return ret;
  3638. }
  3639. struct target_msgbuf {
  3640. abi_long mtype;
  3641. char mtext[1];
  3642. };
  3643. static inline abi_long do_msgsnd(int msqid, abi_long msgp,
  3644. ssize_t msgsz, int msgflg)
  3645. {
  3646. struct target_msgbuf *target_mb;
  3647. struct msgbuf *host_mb;
  3648. abi_long ret = 0;
  3649. if (msgsz < 0) {
  3650. return -TARGET_EINVAL;
  3651. }
  3652. if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
  3653. return -TARGET_EFAULT;
  3654. host_mb = g_try_malloc(msgsz + sizeof(long));
  3655. if (!host_mb) {
  3656. unlock_user_struct(target_mb, msgp, 0);
  3657. return -TARGET_ENOMEM;
  3658. }
  3659. host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
  3660. memcpy(host_mb->mtext, target_mb->mtext, msgsz);
  3661. ret = -TARGET_ENOSYS;
  3662. #ifdef __NR_msgsnd
  3663. ret = get_errno(safe_msgsnd(msqid, host_mb, msgsz, msgflg));
  3664. #endif
  3665. #ifdef __NR_ipc
  3666. if (ret == -TARGET_ENOSYS) {
  3667. ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
  3668. host_mb, 0));
  3669. }
  3670. #endif
  3671. g_free(host_mb);
  3672. unlock_user_struct(target_mb, msgp, 0);
  3673. return ret;
  3674. }
  3675. static inline abi_long do_msgrcv(int msqid, abi_long msgp,
  3676. ssize_t msgsz, abi_long msgtyp,
  3677. int msgflg)
  3678. {
  3679. struct target_msgbuf *target_mb;
  3680. char *target_mtext;
  3681. struct msgbuf *host_mb;
  3682. abi_long ret = 0;
  3683. if (msgsz < 0) {
  3684. return -TARGET_EINVAL;
  3685. }
  3686. if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
  3687. return -TARGET_EFAULT;
  3688. host_mb = g_try_malloc(msgsz + sizeof(long));
  3689. if (!host_mb) {
  3690. ret = -TARGET_ENOMEM;
  3691. goto end;
  3692. }
  3693. ret = -TARGET_ENOSYS;
  3694. #ifdef __NR_msgrcv
  3695. ret = get_errno(safe_msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
  3696. #endif
  3697. #ifdef __NR_ipc
  3698. if (ret == -TARGET_ENOSYS) {
  3699. ret = get_errno(safe_ipc(IPCOP_CALL(1, IPCOP_msgrcv), msqid, msgsz,
  3700. msgflg, host_mb, msgtyp));
  3701. }
  3702. #endif
  3703. if (ret > 0) {
  3704. abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
  3705. target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
  3706. if (!target_mtext) {
  3707. ret = -TARGET_EFAULT;
  3708. goto end;
  3709. }
  3710. memcpy(target_mb->mtext, host_mb->mtext, ret);
  3711. unlock_user(target_mtext, target_mtext_addr, ret);
  3712. }
  3713. target_mb->mtype = tswapal(host_mb->mtype);
  3714. end:
  3715. if (target_mb)
  3716. unlock_user_struct(target_mb, msgp, 1);
  3717. g_free(host_mb);
  3718. return ret;
  3719. }
  3720. static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
  3721. abi_ulong target_addr)
  3722. {
  3723. struct target_shmid_ds *target_sd;
  3724. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  3725. return -TARGET_EFAULT;
  3726. if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
  3727. return -TARGET_EFAULT;
  3728. __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
  3729. __get_user(host_sd->shm_atime, &target_sd->shm_atime);
  3730. __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
  3731. __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
  3732. __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
  3733. __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
  3734. __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
  3735. unlock_user_struct(target_sd, target_addr, 0);
  3736. return 0;
  3737. }
  3738. static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
  3739. struct shmid_ds *host_sd)
  3740. {
  3741. struct target_shmid_ds *target_sd;
  3742. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  3743. return -TARGET_EFAULT;
  3744. if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
  3745. return -TARGET_EFAULT;
  3746. __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
  3747. __put_user(host_sd->shm_atime, &target_sd->shm_atime);
  3748. __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
  3749. __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
  3750. __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
  3751. __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
  3752. __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
  3753. unlock_user_struct(target_sd, target_addr, 1);
  3754. return 0;
  3755. }
  3756. struct target_shminfo {
  3757. abi_ulong shmmax;
  3758. abi_ulong shmmin;
  3759. abi_ulong shmmni;
  3760. abi_ulong shmseg;
  3761. abi_ulong shmall;
  3762. };
  3763. static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
  3764. struct shminfo *host_shminfo)
  3765. {
  3766. struct target_shminfo *target_shminfo;
  3767. if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
  3768. return -TARGET_EFAULT;
  3769. __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
  3770. __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
  3771. __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
  3772. __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
  3773. __put_user(host_shminfo->shmall, &target_shminfo->shmall);
  3774. unlock_user_struct(target_shminfo, target_addr, 1);
  3775. return 0;
  3776. }
  3777. struct target_shm_info {
  3778. int used_ids;
  3779. abi_ulong shm_tot;
  3780. abi_ulong shm_rss;
  3781. abi_ulong shm_swp;
  3782. abi_ulong swap_attempts;
  3783. abi_ulong swap_successes;
  3784. };
  3785. static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
  3786. struct shm_info *host_shm_info)
  3787. {
  3788. struct target_shm_info *target_shm_info;
  3789. if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
  3790. return -TARGET_EFAULT;
  3791. __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
  3792. __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
  3793. __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
  3794. __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
  3795. __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
  3796. __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
  3797. unlock_user_struct(target_shm_info, target_addr, 1);
  3798. return 0;
  3799. }
  3800. static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
  3801. {
  3802. struct shmid_ds dsarg;
  3803. struct shminfo shminfo;
  3804. struct shm_info shm_info;
  3805. abi_long ret = -TARGET_EINVAL;
  3806. cmd &= 0xff;
  3807. switch(cmd) {
  3808. case IPC_STAT:
  3809. case IPC_SET:
  3810. case SHM_STAT:
  3811. if (target_to_host_shmid_ds(&dsarg, buf))
  3812. return -TARGET_EFAULT;
  3813. ret = get_errno(shmctl(shmid, cmd, &dsarg));
  3814. if (host_to_target_shmid_ds(buf, &dsarg))
  3815. return -TARGET_EFAULT;
  3816. break;
  3817. case IPC_INFO:
  3818. ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
  3819. if (host_to_target_shminfo(buf, &shminfo))
  3820. return -TARGET_EFAULT;
  3821. break;
  3822. case SHM_INFO:
  3823. ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
  3824. if (host_to_target_shm_info(buf, &shm_info))
  3825. return -TARGET_EFAULT;
  3826. break;
  3827. case IPC_RMID:
  3828. case SHM_LOCK:
  3829. case SHM_UNLOCK:
  3830. ret = get_errno(shmctl(shmid, cmd, NULL));
  3831. break;
  3832. }
  3833. return ret;
  3834. }
  3835. #ifndef TARGET_FORCE_SHMLBA
  3836. /* For most architectures, SHMLBA is the same as the page size;
  3837. * some architectures have larger values, in which case they should
  3838. * define TARGET_FORCE_SHMLBA and provide a target_shmlba() function.
  3839. * This corresponds to the kernel arch code defining __ARCH_FORCE_SHMLBA
  3840. * and defining its own value for SHMLBA.
  3841. *
  3842. * The kernel also permits SHMLBA to be set by the architecture to a
  3843. * value larger than the page size without setting __ARCH_FORCE_SHMLBA;
  3844. * this means that addresses are rounded to the large size if
  3845. * SHM_RND is set but addresses not aligned to that size are not rejected
  3846. * as long as they are at least page-aligned. Since the only architecture
  3847. * which uses this is ia64 this code doesn't provide for that oddity.
  3848. */
  3849. static inline abi_ulong target_shmlba(CPUArchState *cpu_env)
  3850. {
  3851. return TARGET_PAGE_SIZE;
  3852. }
  3853. #endif
  3854. static inline abi_ulong do_shmat(CPUArchState *cpu_env,
  3855. int shmid, abi_ulong shmaddr, int shmflg)
  3856. {
  3857. abi_long raddr;
  3858. void *host_raddr;
  3859. struct shmid_ds shm_info;
  3860. int i,ret;
  3861. abi_ulong shmlba;
  3862. /* find out the length of the shared memory segment */
  3863. ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
  3864. if (is_error(ret)) {
  3865. /* can't get length, bail out */
  3866. return ret;
  3867. }
  3868. shmlba = target_shmlba(cpu_env);
  3869. if (shmaddr & (shmlba - 1)) {
  3870. if (shmflg & SHM_RND) {
  3871. shmaddr &= ~(shmlba - 1);
  3872. } else {
  3873. return -TARGET_EINVAL;
  3874. }
  3875. }
  3876. if (!guest_range_valid(shmaddr, shm_info.shm_segsz)) {
  3877. return -TARGET_EINVAL;
  3878. }
  3879. mmap_lock();
  3880. if (shmaddr)
  3881. host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
  3882. else {
  3883. abi_ulong mmap_start;
  3884. /* In order to use the host shmat, we need to honor host SHMLBA. */
  3885. mmap_start = mmap_find_vma(0, shm_info.shm_segsz, MAX(SHMLBA, shmlba));
  3886. if (mmap_start == -1) {
  3887. errno = ENOMEM;
  3888. host_raddr = (void *)-1;
  3889. } else
  3890. host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
  3891. }
  3892. if (host_raddr == (void *)-1) {
  3893. mmap_unlock();
  3894. return get_errno((long)host_raddr);
  3895. }
  3896. raddr=h2g((unsigned long)host_raddr);
  3897. page_set_flags(raddr, raddr + shm_info.shm_segsz,
  3898. PAGE_VALID | PAGE_READ |
  3899. ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
  3900. for (i = 0; i < N_SHM_REGIONS; i++) {
  3901. if (!shm_regions[i].in_use) {
  3902. shm_regions[i].in_use = true;
  3903. shm_regions[i].start = raddr;
  3904. shm_regions[i].size = shm_info.shm_segsz;
  3905. break;
  3906. }
  3907. }
  3908. mmap_unlock();
  3909. return raddr;
  3910. }
  3911. static inline abi_long do_shmdt(abi_ulong shmaddr)
  3912. {
  3913. int i;
  3914. abi_long rv;
  3915. mmap_lock();
  3916. for (i = 0; i < N_SHM_REGIONS; ++i) {
  3917. if (shm_regions[i].in_use && shm_regions[i].start == shmaddr) {
  3918. shm_regions[i].in_use = false;
  3919. page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
  3920. break;
  3921. }
  3922. }
  3923. rv = get_errno(shmdt(g2h(shmaddr)));
  3924. mmap_unlock();
  3925. return rv;
  3926. }
  3927. #ifdef TARGET_NR_ipc
  3928. /* ??? This only works with linear mappings. */
  3929. /* do_ipc() must return target values and target errnos. */
  3930. static abi_long do_ipc(CPUArchState *cpu_env,
  3931. unsigned int call, abi_long first,
  3932. abi_long second, abi_long third,
  3933. abi_long ptr, abi_long fifth)
  3934. {
  3935. int version;
  3936. abi_long ret = 0;
  3937. version = call >> 16;
  3938. call &= 0xffff;
  3939. switch (call) {
  3940. case IPCOP_semop:
  3941. ret = do_semop(first, ptr, second);
  3942. break;
  3943. case IPCOP_semget:
  3944. ret = get_errno(semget(first, second, third));
  3945. break;
  3946. case IPCOP_semctl: {
  3947. /* The semun argument to semctl is passed by value, so dereference the
  3948. * ptr argument. */
  3949. abi_ulong atptr;
  3950. get_user_ual(atptr, ptr);
  3951. ret = do_semctl(first, second, third, atptr);
  3952. break;
  3953. }
  3954. case IPCOP_msgget:
  3955. ret = get_errno(msgget(first, second));
  3956. break;
  3957. case IPCOP_msgsnd:
  3958. ret = do_msgsnd(first, ptr, second, third);
  3959. break;
  3960. case IPCOP_msgctl:
  3961. ret = do_msgctl(first, second, ptr);
  3962. break;
  3963. case IPCOP_msgrcv:
  3964. switch (version) {
  3965. case 0:
  3966. {
  3967. struct target_ipc_kludge {
  3968. abi_long msgp;
  3969. abi_long msgtyp;
  3970. } *tmp;
  3971. if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
  3972. ret = -TARGET_EFAULT;
  3973. break;
  3974. }
  3975. ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
  3976. unlock_user_struct(tmp, ptr, 0);
  3977. break;
  3978. }
  3979. default:
  3980. ret = do_msgrcv(first, ptr, second, fifth, third);
  3981. }
  3982. break;
  3983. case IPCOP_shmat:
  3984. switch (version) {
  3985. default:
  3986. {
  3987. abi_ulong raddr;
  3988. raddr = do_shmat(cpu_env, first, ptr, second);
  3989. if (is_error(raddr))
  3990. return get_errno(raddr);
  3991. if (put_user_ual(raddr, third))
  3992. return -TARGET_EFAULT;
  3993. break;
  3994. }
  3995. case 1:
  3996. ret = -TARGET_EINVAL;
  3997. break;
  3998. }
  3999. break;
  4000. case IPCOP_shmdt:
  4001. ret = do_shmdt(ptr);
  4002. break;
  4003. case IPCOP_shmget:
  4004. /* IPC_* flag values are the same on all linux platforms */
  4005. ret = get_errno(shmget(first, second, third));
  4006. break;
  4007. /* IPC_* and SHM_* command values are the same on all linux platforms */
  4008. case IPCOP_shmctl:
  4009. ret = do_shmctl(first, second, ptr);
  4010. break;
  4011. default:
  4012. qemu_log_mask(LOG_UNIMP, "Unsupported ipc call: %d (version %d)\n",
  4013. call, version);
  4014. ret = -TARGET_ENOSYS;
  4015. break;
  4016. }
  4017. return ret;
  4018. }
  4019. #endif
  4020. /* kernel structure types definitions */
  4021. #define STRUCT(name, ...) STRUCT_ ## name,
  4022. #define STRUCT_SPECIAL(name) STRUCT_ ## name,
  4023. enum {
  4024. #include "syscall_types.h"
  4025. STRUCT_MAX
  4026. };
  4027. #undef STRUCT
  4028. #undef STRUCT_SPECIAL
  4029. #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
  4030. #define STRUCT_SPECIAL(name)
  4031. #include "syscall_types.h"
  4032. #undef STRUCT
  4033. #undef STRUCT_SPECIAL
  4034. typedef struct IOCTLEntry IOCTLEntry;
  4035. typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
  4036. int fd, int cmd, abi_long arg);
  4037. struct IOCTLEntry {
  4038. int target_cmd;
  4039. unsigned int host_cmd;
  4040. const char *name;
  4041. int access;
  4042. do_ioctl_fn *do_ioctl;
  4043. const argtype arg_type[5];
  4044. };
  4045. #define IOC_R 0x0001
  4046. #define IOC_W 0x0002
  4047. #define IOC_RW (IOC_R | IOC_W)
  4048. #define MAX_STRUCT_SIZE 4096
  4049. #ifdef CONFIG_FIEMAP
  4050. /* So fiemap access checks don't overflow on 32 bit systems.
  4051. * This is very slightly smaller than the limit imposed by
  4052. * the underlying kernel.
  4053. */
  4054. #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
  4055. / sizeof(struct fiemap_extent))
  4056. static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
  4057. int fd, int cmd, abi_long arg)
  4058. {
  4059. /* The parameter for this ioctl is a struct fiemap followed
  4060. * by an array of struct fiemap_extent whose size is set
  4061. * in fiemap->fm_extent_count. The array is filled in by the
  4062. * ioctl.
  4063. */
  4064. int target_size_in, target_size_out;
  4065. struct fiemap *fm;
  4066. const argtype *arg_type = ie->arg_type;
  4067. const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
  4068. void *argptr, *p;
  4069. abi_long ret;
  4070. int i, extent_size = thunk_type_size(extent_arg_type, 0);
  4071. uint32_t outbufsz;
  4072. int free_fm = 0;
  4073. assert(arg_type[0] == TYPE_PTR);
  4074. assert(ie->access == IOC_RW);
  4075. arg_type++;
  4076. target_size_in = thunk_type_size(arg_type, 0);
  4077. argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
  4078. if (!argptr) {
  4079. return -TARGET_EFAULT;
  4080. }
  4081. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4082. unlock_user(argptr, arg, 0);
  4083. fm = (struct fiemap *)buf_temp;
  4084. if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
  4085. return -TARGET_EINVAL;
  4086. }
  4087. outbufsz = sizeof (*fm) +
  4088. (sizeof(struct fiemap_extent) * fm->fm_extent_count);
  4089. if (outbufsz > MAX_STRUCT_SIZE) {
  4090. /* We can't fit all the extents into the fixed size buffer.
  4091. * Allocate one that is large enough and use it instead.
  4092. */
  4093. fm = g_try_malloc(outbufsz);
  4094. if (!fm) {
  4095. return -TARGET_ENOMEM;
  4096. }
  4097. memcpy(fm, buf_temp, sizeof(struct fiemap));
  4098. free_fm = 1;
  4099. }
  4100. ret = get_errno(safe_ioctl(fd, ie->host_cmd, fm));
  4101. if (!is_error(ret)) {
  4102. target_size_out = target_size_in;
  4103. /* An extent_count of 0 means we were only counting the extents
  4104. * so there are no structs to copy
  4105. */
  4106. if (fm->fm_extent_count != 0) {
  4107. target_size_out += fm->fm_mapped_extents * extent_size;
  4108. }
  4109. argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
  4110. if (!argptr) {
  4111. ret = -TARGET_EFAULT;
  4112. } else {
  4113. /* Convert the struct fiemap */
  4114. thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
  4115. if (fm->fm_extent_count != 0) {
  4116. p = argptr + target_size_in;
  4117. /* ...and then all the struct fiemap_extents */
  4118. for (i = 0; i < fm->fm_mapped_extents; i++) {
  4119. thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
  4120. THUNK_TARGET);
  4121. p += extent_size;
  4122. }
  4123. }
  4124. unlock_user(argptr, arg, target_size_out);
  4125. }
  4126. }
  4127. if (free_fm) {
  4128. g_free(fm);
  4129. }
  4130. return ret;
  4131. }
  4132. #endif
  4133. static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
  4134. int fd, int cmd, abi_long arg)
  4135. {
  4136. const argtype *arg_type = ie->arg_type;
  4137. int target_size;
  4138. void *argptr;
  4139. int ret;
  4140. struct ifconf *host_ifconf;
  4141. uint32_t outbufsz;
  4142. const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
  4143. int target_ifreq_size;
  4144. int nb_ifreq;
  4145. int free_buf = 0;
  4146. int i;
  4147. int target_ifc_len;
  4148. abi_long target_ifc_buf;
  4149. int host_ifc_len;
  4150. char *host_ifc_buf;
  4151. assert(arg_type[0] == TYPE_PTR);
  4152. assert(ie->access == IOC_RW);
  4153. arg_type++;
  4154. target_size = thunk_type_size(arg_type, 0);
  4155. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4156. if (!argptr)
  4157. return -TARGET_EFAULT;
  4158. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4159. unlock_user(argptr, arg, 0);
  4160. host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
  4161. target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
  4162. target_ifreq_size = thunk_type_size(ifreq_arg_type, 0);
  4163. if (target_ifc_buf != 0) {
  4164. target_ifc_len = host_ifconf->ifc_len;
  4165. nb_ifreq = target_ifc_len / target_ifreq_size;
  4166. host_ifc_len = nb_ifreq * sizeof(struct ifreq);
  4167. outbufsz = sizeof(*host_ifconf) + host_ifc_len;
  4168. if (outbufsz > MAX_STRUCT_SIZE) {
  4169. /*
  4170. * We can't fit all the extents into the fixed size buffer.
  4171. * Allocate one that is large enough and use it instead.
  4172. */
  4173. host_ifconf = malloc(outbufsz);
  4174. if (!host_ifconf) {
  4175. return -TARGET_ENOMEM;
  4176. }
  4177. memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
  4178. free_buf = 1;
  4179. }
  4180. host_ifc_buf = (char *)host_ifconf + sizeof(*host_ifconf);
  4181. host_ifconf->ifc_len = host_ifc_len;
  4182. } else {
  4183. host_ifc_buf = NULL;
  4184. }
  4185. host_ifconf->ifc_buf = host_ifc_buf;
  4186. ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_ifconf));
  4187. if (!is_error(ret)) {
  4188. /* convert host ifc_len to target ifc_len */
  4189. nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
  4190. target_ifc_len = nb_ifreq * target_ifreq_size;
  4191. host_ifconf->ifc_len = target_ifc_len;
  4192. /* restore target ifc_buf */
  4193. host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
  4194. /* copy struct ifconf to target user */
  4195. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4196. if (!argptr)
  4197. return -TARGET_EFAULT;
  4198. thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
  4199. unlock_user(argptr, arg, target_size);
  4200. if (target_ifc_buf != 0) {
  4201. /* copy ifreq[] to target user */
  4202. argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
  4203. for (i = 0; i < nb_ifreq ; i++) {
  4204. thunk_convert(argptr + i * target_ifreq_size,
  4205. host_ifc_buf + i * sizeof(struct ifreq),
  4206. ifreq_arg_type, THUNK_TARGET);
  4207. }
  4208. unlock_user(argptr, target_ifc_buf, target_ifc_len);
  4209. }
  4210. }
  4211. if (free_buf) {
  4212. free(host_ifconf);
  4213. }
  4214. return ret;
  4215. }
  4216. #if defined(CONFIG_USBFS)
  4217. #if HOST_LONG_BITS > 64
  4218. #error USBDEVFS thunks do not support >64 bit hosts yet.
  4219. #endif
  4220. struct live_urb {
  4221. uint64_t target_urb_adr;
  4222. uint64_t target_buf_adr;
  4223. char *target_buf_ptr;
  4224. struct usbdevfs_urb host_urb;
  4225. };
  4226. static GHashTable *usbdevfs_urb_hashtable(void)
  4227. {
  4228. static GHashTable *urb_hashtable;
  4229. if (!urb_hashtable) {
  4230. urb_hashtable = g_hash_table_new(g_int64_hash, g_int64_equal);
  4231. }
  4232. return urb_hashtable;
  4233. }
  4234. static void urb_hashtable_insert(struct live_urb *urb)
  4235. {
  4236. GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
  4237. g_hash_table_insert(urb_hashtable, urb, urb);
  4238. }
  4239. static struct live_urb *urb_hashtable_lookup(uint64_t target_urb_adr)
  4240. {
  4241. GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
  4242. return g_hash_table_lookup(urb_hashtable, &target_urb_adr);
  4243. }
  4244. static void urb_hashtable_remove(struct live_urb *urb)
  4245. {
  4246. GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
  4247. g_hash_table_remove(urb_hashtable, urb);
  4248. }
  4249. static abi_long
  4250. do_ioctl_usbdevfs_reapurb(const IOCTLEntry *ie, uint8_t *buf_temp,
  4251. int fd, int cmd, abi_long arg)
  4252. {
  4253. const argtype usbfsurb_arg_type[] = { MK_STRUCT(STRUCT_usbdevfs_urb) };
  4254. const argtype ptrvoid_arg_type[] = { TYPE_PTRVOID, 0, 0 };
  4255. struct live_urb *lurb;
  4256. void *argptr;
  4257. uint64_t hurb;
  4258. int target_size;
  4259. uintptr_t target_urb_adr;
  4260. abi_long ret;
  4261. target_size = thunk_type_size(usbfsurb_arg_type, THUNK_TARGET);
  4262. memset(buf_temp, 0, sizeof(uint64_t));
  4263. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4264. if (is_error(ret)) {
  4265. return ret;
  4266. }
  4267. memcpy(&hurb, buf_temp, sizeof(uint64_t));
  4268. lurb = (void *)((uintptr_t)hurb - offsetof(struct live_urb, host_urb));
  4269. if (!lurb->target_urb_adr) {
  4270. return -TARGET_EFAULT;
  4271. }
  4272. urb_hashtable_remove(lurb);
  4273. unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr,
  4274. lurb->host_urb.buffer_length);
  4275. lurb->target_buf_ptr = NULL;
  4276. /* restore the guest buffer pointer */
  4277. lurb->host_urb.buffer = (void *)(uintptr_t)lurb->target_buf_adr;
  4278. /* update the guest urb struct */
  4279. argptr = lock_user(VERIFY_WRITE, lurb->target_urb_adr, target_size, 0);
  4280. if (!argptr) {
  4281. g_free(lurb);
  4282. return -TARGET_EFAULT;
  4283. }
  4284. thunk_convert(argptr, &lurb->host_urb, usbfsurb_arg_type, THUNK_TARGET);
  4285. unlock_user(argptr, lurb->target_urb_adr, target_size);
  4286. target_size = thunk_type_size(ptrvoid_arg_type, THUNK_TARGET);
  4287. /* write back the urb handle */
  4288. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4289. if (!argptr) {
  4290. g_free(lurb);
  4291. return -TARGET_EFAULT;
  4292. }
  4293. /* GHashTable uses 64-bit keys but thunk_convert expects uintptr_t */
  4294. target_urb_adr = lurb->target_urb_adr;
  4295. thunk_convert(argptr, &target_urb_adr, ptrvoid_arg_type, THUNK_TARGET);
  4296. unlock_user(argptr, arg, target_size);
  4297. g_free(lurb);
  4298. return ret;
  4299. }
  4300. static abi_long
  4301. do_ioctl_usbdevfs_discardurb(const IOCTLEntry *ie,
  4302. uint8_t *buf_temp __attribute__((unused)),
  4303. int fd, int cmd, abi_long arg)
  4304. {
  4305. struct live_urb *lurb;
  4306. /* map target address back to host URB with metadata. */
  4307. lurb = urb_hashtable_lookup(arg);
  4308. if (!lurb) {
  4309. return -TARGET_EFAULT;
  4310. }
  4311. return get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb));
  4312. }
  4313. static abi_long
  4314. do_ioctl_usbdevfs_submiturb(const IOCTLEntry *ie, uint8_t *buf_temp,
  4315. int fd, int cmd, abi_long arg)
  4316. {
  4317. const argtype *arg_type = ie->arg_type;
  4318. int target_size;
  4319. abi_long ret;
  4320. void *argptr;
  4321. int rw_dir;
  4322. struct live_urb *lurb;
  4323. /*
  4324. * each submitted URB needs to map to a unique ID for the
  4325. * kernel, and that unique ID needs to be a pointer to
  4326. * host memory. hence, we need to malloc for each URB.
  4327. * isochronous transfers have a variable length struct.
  4328. */
  4329. arg_type++;
  4330. target_size = thunk_type_size(arg_type, THUNK_TARGET);
  4331. /* construct host copy of urb and metadata */
  4332. lurb = g_try_malloc0(sizeof(struct live_urb));
  4333. if (!lurb) {
  4334. return -TARGET_ENOMEM;
  4335. }
  4336. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4337. if (!argptr) {
  4338. g_free(lurb);
  4339. return -TARGET_EFAULT;
  4340. }
  4341. thunk_convert(&lurb->host_urb, argptr, arg_type, THUNK_HOST);
  4342. unlock_user(argptr, arg, 0);
  4343. lurb->target_urb_adr = arg;
  4344. lurb->target_buf_adr = (uintptr_t)lurb->host_urb.buffer;
  4345. /* buffer space used depends on endpoint type so lock the entire buffer */
  4346. /* control type urbs should check the buffer contents for true direction */
  4347. rw_dir = lurb->host_urb.endpoint & USB_DIR_IN ? VERIFY_WRITE : VERIFY_READ;
  4348. lurb->target_buf_ptr = lock_user(rw_dir, lurb->target_buf_adr,
  4349. lurb->host_urb.buffer_length, 1);
  4350. if (lurb->target_buf_ptr == NULL) {
  4351. g_free(lurb);
  4352. return -TARGET_EFAULT;
  4353. }
  4354. /* update buffer pointer in host copy */
  4355. lurb->host_urb.buffer = lurb->target_buf_ptr;
  4356. ret = get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb));
  4357. if (is_error(ret)) {
  4358. unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr, 0);
  4359. g_free(lurb);
  4360. } else {
  4361. urb_hashtable_insert(lurb);
  4362. }
  4363. return ret;
  4364. }
  4365. #endif /* CONFIG_USBFS */
  4366. static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
  4367. int cmd, abi_long arg)
  4368. {
  4369. void *argptr;
  4370. struct dm_ioctl *host_dm;
  4371. abi_long guest_data;
  4372. uint32_t guest_data_size;
  4373. int target_size;
  4374. const argtype *arg_type = ie->arg_type;
  4375. abi_long ret;
  4376. void *big_buf = NULL;
  4377. char *host_data;
  4378. arg_type++;
  4379. target_size = thunk_type_size(arg_type, 0);
  4380. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4381. if (!argptr) {
  4382. ret = -TARGET_EFAULT;
  4383. goto out;
  4384. }
  4385. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4386. unlock_user(argptr, arg, 0);
  4387. /* buf_temp is too small, so fetch things into a bigger buffer */
  4388. big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
  4389. memcpy(big_buf, buf_temp, target_size);
  4390. buf_temp = big_buf;
  4391. host_dm = big_buf;
  4392. guest_data = arg + host_dm->data_start;
  4393. if ((guest_data - arg) < 0) {
  4394. ret = -TARGET_EINVAL;
  4395. goto out;
  4396. }
  4397. guest_data_size = host_dm->data_size - host_dm->data_start;
  4398. host_data = (char*)host_dm + host_dm->data_start;
  4399. argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
  4400. if (!argptr) {
  4401. ret = -TARGET_EFAULT;
  4402. goto out;
  4403. }
  4404. switch (ie->host_cmd) {
  4405. case DM_REMOVE_ALL:
  4406. case DM_LIST_DEVICES:
  4407. case DM_DEV_CREATE:
  4408. case DM_DEV_REMOVE:
  4409. case DM_DEV_SUSPEND:
  4410. case DM_DEV_STATUS:
  4411. case DM_DEV_WAIT:
  4412. case DM_TABLE_STATUS:
  4413. case DM_TABLE_CLEAR:
  4414. case DM_TABLE_DEPS:
  4415. case DM_LIST_VERSIONS:
  4416. /* no input data */
  4417. break;
  4418. case DM_DEV_RENAME:
  4419. case DM_DEV_SET_GEOMETRY:
  4420. /* data contains only strings */
  4421. memcpy(host_data, argptr, guest_data_size);
  4422. break;
  4423. case DM_TARGET_MSG:
  4424. memcpy(host_data, argptr, guest_data_size);
  4425. *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
  4426. break;
  4427. case DM_TABLE_LOAD:
  4428. {
  4429. void *gspec = argptr;
  4430. void *cur_data = host_data;
  4431. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
  4432. int spec_size = thunk_type_size(arg_type, 0);
  4433. int i;
  4434. for (i = 0; i < host_dm->target_count; i++) {
  4435. struct dm_target_spec *spec = cur_data;
  4436. uint32_t next;
  4437. int slen;
  4438. thunk_convert(spec, gspec, arg_type, THUNK_HOST);
  4439. slen = strlen((char*)gspec + spec_size) + 1;
  4440. next = spec->next;
  4441. spec->next = sizeof(*spec) + slen;
  4442. strcpy((char*)&spec[1], gspec + spec_size);
  4443. gspec += next;
  4444. cur_data += spec->next;
  4445. }
  4446. break;
  4447. }
  4448. default:
  4449. ret = -TARGET_EINVAL;
  4450. unlock_user(argptr, guest_data, 0);
  4451. goto out;
  4452. }
  4453. unlock_user(argptr, guest_data, 0);
  4454. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4455. if (!is_error(ret)) {
  4456. guest_data = arg + host_dm->data_start;
  4457. guest_data_size = host_dm->data_size - host_dm->data_start;
  4458. argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
  4459. switch (ie->host_cmd) {
  4460. case DM_REMOVE_ALL:
  4461. case DM_DEV_CREATE:
  4462. case DM_DEV_REMOVE:
  4463. case DM_DEV_RENAME:
  4464. case DM_DEV_SUSPEND:
  4465. case DM_DEV_STATUS:
  4466. case DM_TABLE_LOAD:
  4467. case DM_TABLE_CLEAR:
  4468. case DM_TARGET_MSG:
  4469. case DM_DEV_SET_GEOMETRY:
  4470. /* no return data */
  4471. break;
  4472. case DM_LIST_DEVICES:
  4473. {
  4474. struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
  4475. uint32_t remaining_data = guest_data_size;
  4476. void *cur_data = argptr;
  4477. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
  4478. int nl_size = 12; /* can't use thunk_size due to alignment */
  4479. while (1) {
  4480. uint32_t next = nl->next;
  4481. if (next) {
  4482. nl->next = nl_size + (strlen(nl->name) + 1);
  4483. }
  4484. if (remaining_data < nl->next) {
  4485. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  4486. break;
  4487. }
  4488. thunk_convert(cur_data, nl, arg_type, THUNK_TARGET);
  4489. strcpy(cur_data + nl_size, nl->name);
  4490. cur_data += nl->next;
  4491. remaining_data -= nl->next;
  4492. if (!next) {
  4493. break;
  4494. }
  4495. nl = (void*)nl + next;
  4496. }
  4497. break;
  4498. }
  4499. case DM_DEV_WAIT:
  4500. case DM_TABLE_STATUS:
  4501. {
  4502. struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
  4503. void *cur_data = argptr;
  4504. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
  4505. int spec_size = thunk_type_size(arg_type, 0);
  4506. int i;
  4507. for (i = 0; i < host_dm->target_count; i++) {
  4508. uint32_t next = spec->next;
  4509. int slen = strlen((char*)&spec[1]) + 1;
  4510. spec->next = (cur_data - argptr) + spec_size + slen;
  4511. if (guest_data_size < spec->next) {
  4512. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  4513. break;
  4514. }
  4515. thunk_convert(cur_data, spec, arg_type, THUNK_TARGET);
  4516. strcpy(cur_data + spec_size, (char*)&spec[1]);
  4517. cur_data = argptr + spec->next;
  4518. spec = (void*)host_dm + host_dm->data_start + next;
  4519. }
  4520. break;
  4521. }
  4522. case DM_TABLE_DEPS:
  4523. {
  4524. void *hdata = (void*)host_dm + host_dm->data_start;
  4525. int count = *(uint32_t*)hdata;
  4526. uint64_t *hdev = hdata + 8;
  4527. uint64_t *gdev = argptr + 8;
  4528. int i;
  4529. *(uint32_t*)argptr = tswap32(count);
  4530. for (i = 0; i < count; i++) {
  4531. *gdev = tswap64(*hdev);
  4532. gdev++;
  4533. hdev++;
  4534. }
  4535. break;
  4536. }
  4537. case DM_LIST_VERSIONS:
  4538. {
  4539. struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
  4540. uint32_t remaining_data = guest_data_size;
  4541. void *cur_data = argptr;
  4542. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
  4543. int vers_size = thunk_type_size(arg_type, 0);
  4544. while (1) {
  4545. uint32_t next = vers->next;
  4546. if (next) {
  4547. vers->next = vers_size + (strlen(vers->name) + 1);
  4548. }
  4549. if (remaining_data < vers->next) {
  4550. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  4551. break;
  4552. }
  4553. thunk_convert(cur_data, vers, arg_type, THUNK_TARGET);
  4554. strcpy(cur_data + vers_size, vers->name);
  4555. cur_data += vers->next;
  4556. remaining_data -= vers->next;
  4557. if (!next) {
  4558. break;
  4559. }
  4560. vers = (void*)vers + next;
  4561. }
  4562. break;
  4563. }
  4564. default:
  4565. unlock_user(argptr, guest_data, 0);
  4566. ret = -TARGET_EINVAL;
  4567. goto out;
  4568. }
  4569. unlock_user(argptr, guest_data, guest_data_size);
  4570. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4571. if (!argptr) {
  4572. ret = -TARGET_EFAULT;
  4573. goto out;
  4574. }
  4575. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  4576. unlock_user(argptr, arg, target_size);
  4577. }
  4578. out:
  4579. g_free(big_buf);
  4580. return ret;
  4581. }
  4582. static abi_long do_ioctl_blkpg(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
  4583. int cmd, abi_long arg)
  4584. {
  4585. void *argptr;
  4586. int target_size;
  4587. const argtype *arg_type = ie->arg_type;
  4588. const argtype part_arg_type[] = { MK_STRUCT(STRUCT_blkpg_partition) };
  4589. abi_long ret;
  4590. struct blkpg_ioctl_arg *host_blkpg = (void*)buf_temp;
  4591. struct blkpg_partition host_part;
  4592. /* Read and convert blkpg */
  4593. arg_type++;
  4594. target_size = thunk_type_size(arg_type, 0);
  4595. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4596. if (!argptr) {
  4597. ret = -TARGET_EFAULT;
  4598. goto out;
  4599. }
  4600. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4601. unlock_user(argptr, arg, 0);
  4602. switch (host_blkpg->op) {
  4603. case BLKPG_ADD_PARTITION:
  4604. case BLKPG_DEL_PARTITION:
  4605. /* payload is struct blkpg_partition */
  4606. break;
  4607. default:
  4608. /* Unknown opcode */
  4609. ret = -TARGET_EINVAL;
  4610. goto out;
  4611. }
  4612. /* Read and convert blkpg->data */
  4613. arg = (abi_long)(uintptr_t)host_blkpg->data;
  4614. target_size = thunk_type_size(part_arg_type, 0);
  4615. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4616. if (!argptr) {
  4617. ret = -TARGET_EFAULT;
  4618. goto out;
  4619. }
  4620. thunk_convert(&host_part, argptr, part_arg_type, THUNK_HOST);
  4621. unlock_user(argptr, arg, 0);
  4622. /* Swizzle the data pointer to our local copy and call! */
  4623. host_blkpg->data = &host_part;
  4624. ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_blkpg));
  4625. out:
  4626. return ret;
  4627. }
  4628. static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
  4629. int fd, int cmd, abi_long arg)
  4630. {
  4631. const argtype *arg_type = ie->arg_type;
  4632. const StructEntry *se;
  4633. const argtype *field_types;
  4634. const int *dst_offsets, *src_offsets;
  4635. int target_size;
  4636. void *argptr;
  4637. abi_ulong *target_rt_dev_ptr = NULL;
  4638. unsigned long *host_rt_dev_ptr = NULL;
  4639. abi_long ret;
  4640. int i;
  4641. assert(ie->access == IOC_W);
  4642. assert(*arg_type == TYPE_PTR);
  4643. arg_type++;
  4644. assert(*arg_type == TYPE_STRUCT);
  4645. target_size = thunk_type_size(arg_type, 0);
  4646. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4647. if (!argptr) {
  4648. return -TARGET_EFAULT;
  4649. }
  4650. arg_type++;
  4651. assert(*arg_type == (int)STRUCT_rtentry);
  4652. se = struct_entries + *arg_type++;
  4653. assert(se->convert[0] == NULL);
  4654. /* convert struct here to be able to catch rt_dev string */
  4655. field_types = se->field_types;
  4656. dst_offsets = se->field_offsets[THUNK_HOST];
  4657. src_offsets = se->field_offsets[THUNK_TARGET];
  4658. for (i = 0; i < se->nb_fields; i++) {
  4659. if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) {
  4660. assert(*field_types == TYPE_PTRVOID);
  4661. target_rt_dev_ptr = (abi_ulong *)(argptr + src_offsets[i]);
  4662. host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]);
  4663. if (*target_rt_dev_ptr != 0) {
  4664. *host_rt_dev_ptr = (unsigned long)lock_user_string(
  4665. tswapal(*target_rt_dev_ptr));
  4666. if (!*host_rt_dev_ptr) {
  4667. unlock_user(argptr, arg, 0);
  4668. return -TARGET_EFAULT;
  4669. }
  4670. } else {
  4671. *host_rt_dev_ptr = 0;
  4672. }
  4673. field_types++;
  4674. continue;
  4675. }
  4676. field_types = thunk_convert(buf_temp + dst_offsets[i],
  4677. argptr + src_offsets[i],
  4678. field_types, THUNK_HOST);
  4679. }
  4680. unlock_user(argptr, arg, 0);
  4681. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4682. assert(host_rt_dev_ptr != NULL);
  4683. assert(target_rt_dev_ptr != NULL);
  4684. if (*host_rt_dev_ptr != 0) {
  4685. unlock_user((void *)*host_rt_dev_ptr,
  4686. *target_rt_dev_ptr, 0);
  4687. }
  4688. return ret;
  4689. }
  4690. static abi_long do_ioctl_kdsigaccept(const IOCTLEntry *ie, uint8_t *buf_temp,
  4691. int fd, int cmd, abi_long arg)
  4692. {
  4693. int sig = target_to_host_signal(arg);
  4694. return get_errno(safe_ioctl(fd, ie->host_cmd, sig));
  4695. }
  4696. static abi_long do_ioctl_SIOCGSTAMP(const IOCTLEntry *ie, uint8_t *buf_temp,
  4697. int fd, int cmd, abi_long arg)
  4698. {
  4699. struct timeval tv;
  4700. abi_long ret;
  4701. ret = get_errno(safe_ioctl(fd, SIOCGSTAMP, &tv));
  4702. if (is_error(ret)) {
  4703. return ret;
  4704. }
  4705. if (cmd == (int)TARGET_SIOCGSTAMP_OLD) {
  4706. if (copy_to_user_timeval(arg, &tv)) {
  4707. return -TARGET_EFAULT;
  4708. }
  4709. } else {
  4710. if (copy_to_user_timeval64(arg, &tv)) {
  4711. return -TARGET_EFAULT;
  4712. }
  4713. }
  4714. return ret;
  4715. }
  4716. static abi_long do_ioctl_SIOCGSTAMPNS(const IOCTLEntry *ie, uint8_t *buf_temp,
  4717. int fd, int cmd, abi_long arg)
  4718. {
  4719. struct timespec ts;
  4720. abi_long ret;
  4721. ret = get_errno(safe_ioctl(fd, SIOCGSTAMPNS, &ts));
  4722. if (is_error(ret)) {
  4723. return ret;
  4724. }
  4725. if (cmd == (int)TARGET_SIOCGSTAMPNS_OLD) {
  4726. if (host_to_target_timespec(arg, &ts)) {
  4727. return -TARGET_EFAULT;
  4728. }
  4729. } else{
  4730. if (host_to_target_timespec64(arg, &ts)) {
  4731. return -TARGET_EFAULT;
  4732. }
  4733. }
  4734. return ret;
  4735. }
  4736. #ifdef TIOCGPTPEER
  4737. static abi_long do_ioctl_tiocgptpeer(const IOCTLEntry *ie, uint8_t *buf_temp,
  4738. int fd, int cmd, abi_long arg)
  4739. {
  4740. int flags = target_to_host_bitmask(arg, fcntl_flags_tbl);
  4741. return get_errno(safe_ioctl(fd, ie->host_cmd, flags));
  4742. }
  4743. #endif
  4744. static IOCTLEntry ioctl_entries[] = {
  4745. #define IOCTL(cmd, access, ...) \
  4746. { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
  4747. #define IOCTL_SPECIAL(cmd, access, dofn, ...) \
  4748. { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
  4749. #define IOCTL_IGNORE(cmd) \
  4750. { TARGET_ ## cmd, 0, #cmd },
  4751. #include "ioctls.h"
  4752. { 0, 0, },
  4753. };
  4754. /* ??? Implement proper locking for ioctls. */
  4755. /* do_ioctl() Must return target values and target errnos. */
  4756. static abi_long do_ioctl(int fd, int cmd, abi_long arg)
  4757. {
  4758. const IOCTLEntry *ie;
  4759. const argtype *arg_type;
  4760. abi_long ret;
  4761. uint8_t buf_temp[MAX_STRUCT_SIZE];
  4762. int target_size;
  4763. void *argptr;
  4764. ie = ioctl_entries;
  4765. for(;;) {
  4766. if (ie->target_cmd == 0) {
  4767. qemu_log_mask(
  4768. LOG_UNIMP, "Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
  4769. return -TARGET_ENOSYS;
  4770. }
  4771. if (ie->target_cmd == cmd)
  4772. break;
  4773. ie++;
  4774. }
  4775. arg_type = ie->arg_type;
  4776. if (ie->do_ioctl) {
  4777. return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
  4778. } else if (!ie->host_cmd) {
  4779. /* Some architectures define BSD ioctls in their headers
  4780. that are not implemented in Linux. */
  4781. return -TARGET_ENOSYS;
  4782. }
  4783. switch(arg_type[0]) {
  4784. case TYPE_NULL:
  4785. /* no argument */
  4786. ret = get_errno(safe_ioctl(fd, ie->host_cmd));
  4787. break;
  4788. case TYPE_PTRVOID:
  4789. case TYPE_INT:
  4790. case TYPE_LONG:
  4791. case TYPE_ULONG:
  4792. ret = get_errno(safe_ioctl(fd, ie->host_cmd, arg));
  4793. break;
  4794. case TYPE_PTR:
  4795. arg_type++;
  4796. target_size = thunk_type_size(arg_type, 0);
  4797. switch(ie->access) {
  4798. case IOC_R:
  4799. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4800. if (!is_error(ret)) {
  4801. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4802. if (!argptr)
  4803. return -TARGET_EFAULT;
  4804. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  4805. unlock_user(argptr, arg, target_size);
  4806. }
  4807. break;
  4808. case IOC_W:
  4809. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4810. if (!argptr)
  4811. return -TARGET_EFAULT;
  4812. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4813. unlock_user(argptr, arg, 0);
  4814. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4815. break;
  4816. default:
  4817. case IOC_RW:
  4818. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  4819. if (!argptr)
  4820. return -TARGET_EFAULT;
  4821. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  4822. unlock_user(argptr, arg, 0);
  4823. ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
  4824. if (!is_error(ret)) {
  4825. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  4826. if (!argptr)
  4827. return -TARGET_EFAULT;
  4828. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  4829. unlock_user(argptr, arg, target_size);
  4830. }
  4831. break;
  4832. }
  4833. break;
  4834. default:
  4835. qemu_log_mask(LOG_UNIMP,
  4836. "Unsupported ioctl type: cmd=0x%04lx type=%d\n",
  4837. (long)cmd, arg_type[0]);
  4838. ret = -TARGET_ENOSYS;
  4839. break;
  4840. }
  4841. return ret;
  4842. }
  4843. static const bitmask_transtbl iflag_tbl[] = {
  4844. { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
  4845. { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
  4846. { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
  4847. { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
  4848. { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
  4849. { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
  4850. { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
  4851. { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
  4852. { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
  4853. { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
  4854. { TARGET_IXON, TARGET_IXON, IXON, IXON },
  4855. { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
  4856. { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
  4857. { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
  4858. { 0, 0, 0, 0 }
  4859. };
  4860. static const bitmask_transtbl oflag_tbl[] = {
  4861. { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
  4862. { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
  4863. { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
  4864. { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
  4865. { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
  4866. { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
  4867. { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
  4868. { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
  4869. { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
  4870. { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
  4871. { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
  4872. { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
  4873. { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
  4874. { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
  4875. { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
  4876. { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
  4877. { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
  4878. { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
  4879. { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
  4880. { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
  4881. { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
  4882. { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
  4883. { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
  4884. { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
  4885. { 0, 0, 0, 0 }
  4886. };
  4887. static const bitmask_transtbl cflag_tbl[] = {
  4888. { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
  4889. { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
  4890. { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
  4891. { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
  4892. { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
  4893. { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
  4894. { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
  4895. { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
  4896. { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
  4897. { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
  4898. { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
  4899. { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
  4900. { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
  4901. { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
  4902. { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
  4903. { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
  4904. { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
  4905. { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
  4906. { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
  4907. { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
  4908. { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
  4909. { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
  4910. { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
  4911. { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
  4912. { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
  4913. { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
  4914. { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
  4915. { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
  4916. { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
  4917. { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
  4918. { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
  4919. { 0, 0, 0, 0 }
  4920. };
  4921. static const bitmask_transtbl lflag_tbl[] = {
  4922. { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
  4923. { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
  4924. { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
  4925. { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
  4926. { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
  4927. { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
  4928. { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
  4929. { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
  4930. { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
  4931. { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
  4932. { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
  4933. { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
  4934. { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
  4935. { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
  4936. { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
  4937. { 0, 0, 0, 0 }
  4938. };
  4939. static void target_to_host_termios (void *dst, const void *src)
  4940. {
  4941. struct host_termios *host = dst;
  4942. const struct target_termios *target = src;
  4943. host->c_iflag =
  4944. target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
  4945. host->c_oflag =
  4946. target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
  4947. host->c_cflag =
  4948. target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
  4949. host->c_lflag =
  4950. target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
  4951. host->c_line = target->c_line;
  4952. memset(host->c_cc, 0, sizeof(host->c_cc));
  4953. host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
  4954. host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
  4955. host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
  4956. host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
  4957. host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
  4958. host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
  4959. host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
  4960. host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
  4961. host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
  4962. host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
  4963. host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
  4964. host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
  4965. host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
  4966. host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
  4967. host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
  4968. host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
  4969. host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
  4970. }
  4971. static void host_to_target_termios (void *dst, const void *src)
  4972. {
  4973. struct target_termios *target = dst;
  4974. const struct host_termios *host = src;
  4975. target->c_iflag =
  4976. tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
  4977. target->c_oflag =
  4978. tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
  4979. target->c_cflag =
  4980. tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
  4981. target->c_lflag =
  4982. tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
  4983. target->c_line = host->c_line;
  4984. memset(target->c_cc, 0, sizeof(target->c_cc));
  4985. target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
  4986. target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
  4987. target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
  4988. target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
  4989. target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
  4990. target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
  4991. target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
  4992. target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
  4993. target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
  4994. target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
  4995. target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
  4996. target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
  4997. target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
  4998. target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
  4999. target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
  5000. target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
  5001. target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
  5002. }
  5003. static const StructEntry struct_termios_def = {
  5004. .convert = { host_to_target_termios, target_to_host_termios },
  5005. .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
  5006. .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
  5007. };
  5008. static bitmask_transtbl mmap_flags_tbl[] = {
  5009. { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
  5010. { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
  5011. { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
  5012. { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS,
  5013. MAP_ANONYMOUS, MAP_ANONYMOUS },
  5014. { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN,
  5015. MAP_GROWSDOWN, MAP_GROWSDOWN },
  5016. { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE,
  5017. MAP_DENYWRITE, MAP_DENYWRITE },
  5018. { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE,
  5019. MAP_EXECUTABLE, MAP_EXECUTABLE },
  5020. { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
  5021. { TARGET_MAP_NORESERVE, TARGET_MAP_NORESERVE,
  5022. MAP_NORESERVE, MAP_NORESERVE },
  5023. { TARGET_MAP_HUGETLB, TARGET_MAP_HUGETLB, MAP_HUGETLB, MAP_HUGETLB },
  5024. /* MAP_STACK had been ignored by the kernel for quite some time.
  5025. Recognize it for the target insofar as we do not want to pass
  5026. it through to the host. */
  5027. { TARGET_MAP_STACK, TARGET_MAP_STACK, 0, 0 },
  5028. { 0, 0, 0, 0 }
  5029. };
  5030. /*
  5031. * NOTE: TARGET_ABI32 is defined for TARGET_I386 (but not for TARGET_X86_64)
  5032. * TARGET_I386 is defined if TARGET_X86_64 is defined
  5033. */
  5034. #if defined(TARGET_I386)
  5035. /* NOTE: there is really one LDT for all the threads */
  5036. static uint8_t *ldt_table;
  5037. static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
  5038. {
  5039. int size;
  5040. void *p;
  5041. if (!ldt_table)
  5042. return 0;
  5043. size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
  5044. if (size > bytecount)
  5045. size = bytecount;
  5046. p = lock_user(VERIFY_WRITE, ptr, size, 0);
  5047. if (!p)
  5048. return -TARGET_EFAULT;
  5049. /* ??? Should this by byteswapped? */
  5050. memcpy(p, ldt_table, size);
  5051. unlock_user(p, ptr, size);
  5052. return size;
  5053. }
  5054. /* XXX: add locking support */
  5055. static abi_long write_ldt(CPUX86State *env,
  5056. abi_ulong ptr, unsigned long bytecount, int oldmode)
  5057. {
  5058. struct target_modify_ldt_ldt_s ldt_info;
  5059. struct target_modify_ldt_ldt_s *target_ldt_info;
  5060. int seg_32bit, contents, read_exec_only, limit_in_pages;
  5061. int seg_not_present, useable, lm;
  5062. uint32_t *lp, entry_1, entry_2;
  5063. if (bytecount != sizeof(ldt_info))
  5064. return -TARGET_EINVAL;
  5065. if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
  5066. return -TARGET_EFAULT;
  5067. ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
  5068. ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
  5069. ldt_info.limit = tswap32(target_ldt_info->limit);
  5070. ldt_info.flags = tswap32(target_ldt_info->flags);
  5071. unlock_user_struct(target_ldt_info, ptr, 0);
  5072. if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
  5073. return -TARGET_EINVAL;
  5074. seg_32bit = ldt_info.flags & 1;
  5075. contents = (ldt_info.flags >> 1) & 3;
  5076. read_exec_only = (ldt_info.flags >> 3) & 1;
  5077. limit_in_pages = (ldt_info.flags >> 4) & 1;
  5078. seg_not_present = (ldt_info.flags >> 5) & 1;
  5079. useable = (ldt_info.flags >> 6) & 1;
  5080. #ifdef TARGET_ABI32
  5081. lm = 0;
  5082. #else
  5083. lm = (ldt_info.flags >> 7) & 1;
  5084. #endif
  5085. if (contents == 3) {
  5086. if (oldmode)
  5087. return -TARGET_EINVAL;
  5088. if (seg_not_present == 0)
  5089. return -TARGET_EINVAL;
  5090. }
  5091. /* allocate the LDT */
  5092. if (!ldt_table) {
  5093. env->ldt.base = target_mmap(0,
  5094. TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
  5095. PROT_READ|PROT_WRITE,
  5096. MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
  5097. if (env->ldt.base == -1)
  5098. return -TARGET_ENOMEM;
  5099. memset(g2h(env->ldt.base), 0,
  5100. TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
  5101. env->ldt.limit = 0xffff;
  5102. ldt_table = g2h(env->ldt.base);
  5103. }
  5104. /* NOTE: same code as Linux kernel */
  5105. /* Allow LDTs to be cleared by the user. */
  5106. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  5107. if (oldmode ||
  5108. (contents == 0 &&
  5109. read_exec_only == 1 &&
  5110. seg_32bit == 0 &&
  5111. limit_in_pages == 0 &&
  5112. seg_not_present == 1 &&
  5113. useable == 0 )) {
  5114. entry_1 = 0;
  5115. entry_2 = 0;
  5116. goto install;
  5117. }
  5118. }
  5119. entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
  5120. (ldt_info.limit & 0x0ffff);
  5121. entry_2 = (ldt_info.base_addr & 0xff000000) |
  5122. ((ldt_info.base_addr & 0x00ff0000) >> 16) |
  5123. (ldt_info.limit & 0xf0000) |
  5124. ((read_exec_only ^ 1) << 9) |
  5125. (contents << 10) |
  5126. ((seg_not_present ^ 1) << 15) |
  5127. (seg_32bit << 22) |
  5128. (limit_in_pages << 23) |
  5129. (lm << 21) |
  5130. 0x7000;
  5131. if (!oldmode)
  5132. entry_2 |= (useable << 20);
  5133. /* Install the new entry ... */
  5134. install:
  5135. lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
  5136. lp[0] = tswap32(entry_1);
  5137. lp[1] = tswap32(entry_2);
  5138. return 0;
  5139. }
  5140. /* specific and weird i386 syscalls */
  5141. static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
  5142. unsigned long bytecount)
  5143. {
  5144. abi_long ret;
  5145. switch (func) {
  5146. case 0:
  5147. ret = read_ldt(ptr, bytecount);
  5148. break;
  5149. case 1:
  5150. ret = write_ldt(env, ptr, bytecount, 1);
  5151. break;
  5152. case 0x11:
  5153. ret = write_ldt(env, ptr, bytecount, 0);
  5154. break;
  5155. default:
  5156. ret = -TARGET_ENOSYS;
  5157. break;
  5158. }
  5159. return ret;
  5160. }
  5161. #if defined(TARGET_ABI32)
  5162. abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
  5163. {
  5164. uint64_t *gdt_table = g2h(env->gdt.base);
  5165. struct target_modify_ldt_ldt_s ldt_info;
  5166. struct target_modify_ldt_ldt_s *target_ldt_info;
  5167. int seg_32bit, contents, read_exec_only, limit_in_pages;
  5168. int seg_not_present, useable, lm;
  5169. uint32_t *lp, entry_1, entry_2;
  5170. int i;
  5171. lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
  5172. if (!target_ldt_info)
  5173. return -TARGET_EFAULT;
  5174. ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
  5175. ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
  5176. ldt_info.limit = tswap32(target_ldt_info->limit);
  5177. ldt_info.flags = tswap32(target_ldt_info->flags);
  5178. if (ldt_info.entry_number == -1) {
  5179. for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
  5180. if (gdt_table[i] == 0) {
  5181. ldt_info.entry_number = i;
  5182. target_ldt_info->entry_number = tswap32(i);
  5183. break;
  5184. }
  5185. }
  5186. }
  5187. unlock_user_struct(target_ldt_info, ptr, 1);
  5188. if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
  5189. ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
  5190. return -TARGET_EINVAL;
  5191. seg_32bit = ldt_info.flags & 1;
  5192. contents = (ldt_info.flags >> 1) & 3;
  5193. read_exec_only = (ldt_info.flags >> 3) & 1;
  5194. limit_in_pages = (ldt_info.flags >> 4) & 1;
  5195. seg_not_present = (ldt_info.flags >> 5) & 1;
  5196. useable = (ldt_info.flags >> 6) & 1;
  5197. #ifdef TARGET_ABI32
  5198. lm = 0;
  5199. #else
  5200. lm = (ldt_info.flags >> 7) & 1;
  5201. #endif
  5202. if (contents == 3) {
  5203. if (seg_not_present == 0)
  5204. return -TARGET_EINVAL;
  5205. }
  5206. /* NOTE: same code as Linux kernel */
  5207. /* Allow LDTs to be cleared by the user. */
  5208. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  5209. if ((contents == 0 &&
  5210. read_exec_only == 1 &&
  5211. seg_32bit == 0 &&
  5212. limit_in_pages == 0 &&
  5213. seg_not_present == 1 &&
  5214. useable == 0 )) {
  5215. entry_1 = 0;
  5216. entry_2 = 0;
  5217. goto install;
  5218. }
  5219. }
  5220. entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
  5221. (ldt_info.limit & 0x0ffff);
  5222. entry_2 = (ldt_info.base_addr & 0xff000000) |
  5223. ((ldt_info.base_addr & 0x00ff0000) >> 16) |
  5224. (ldt_info.limit & 0xf0000) |
  5225. ((read_exec_only ^ 1) << 9) |
  5226. (contents << 10) |
  5227. ((seg_not_present ^ 1) << 15) |
  5228. (seg_32bit << 22) |
  5229. (limit_in_pages << 23) |
  5230. (useable << 20) |
  5231. (lm << 21) |
  5232. 0x7000;
  5233. /* Install the new entry ... */
  5234. install:
  5235. lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
  5236. lp[0] = tswap32(entry_1);
  5237. lp[1] = tswap32(entry_2);
  5238. return 0;
  5239. }
  5240. static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
  5241. {
  5242. struct target_modify_ldt_ldt_s *target_ldt_info;
  5243. uint64_t *gdt_table = g2h(env->gdt.base);
  5244. uint32_t base_addr, limit, flags;
  5245. int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
  5246. int seg_not_present, useable, lm;
  5247. uint32_t *lp, entry_1, entry_2;
  5248. lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
  5249. if (!target_ldt_info)
  5250. return -TARGET_EFAULT;
  5251. idx = tswap32(target_ldt_info->entry_number);
  5252. if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
  5253. idx > TARGET_GDT_ENTRY_TLS_MAX) {
  5254. unlock_user_struct(target_ldt_info, ptr, 1);
  5255. return -TARGET_EINVAL;
  5256. }
  5257. lp = (uint32_t *)(gdt_table + idx);
  5258. entry_1 = tswap32(lp[0]);
  5259. entry_2 = tswap32(lp[1]);
  5260. read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
  5261. contents = (entry_2 >> 10) & 3;
  5262. seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
  5263. seg_32bit = (entry_2 >> 22) & 1;
  5264. limit_in_pages = (entry_2 >> 23) & 1;
  5265. useable = (entry_2 >> 20) & 1;
  5266. #ifdef TARGET_ABI32
  5267. lm = 0;
  5268. #else
  5269. lm = (entry_2 >> 21) & 1;
  5270. #endif
  5271. flags = (seg_32bit << 0) | (contents << 1) |
  5272. (read_exec_only << 3) | (limit_in_pages << 4) |
  5273. (seg_not_present << 5) | (useable << 6) | (lm << 7);
  5274. limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
  5275. base_addr = (entry_1 >> 16) |
  5276. (entry_2 & 0xff000000) |
  5277. ((entry_2 & 0xff) << 16);
  5278. target_ldt_info->base_addr = tswapal(base_addr);
  5279. target_ldt_info->limit = tswap32(limit);
  5280. target_ldt_info->flags = tswap32(flags);
  5281. unlock_user_struct(target_ldt_info, ptr, 1);
  5282. return 0;
  5283. }
  5284. abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
  5285. {
  5286. return -ENOSYS;
  5287. }
  5288. #else
  5289. abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
  5290. {
  5291. abi_long ret = 0;
  5292. abi_ulong val;
  5293. int idx;
  5294. switch(code) {
  5295. case TARGET_ARCH_SET_GS:
  5296. case TARGET_ARCH_SET_FS:
  5297. if (code == TARGET_ARCH_SET_GS)
  5298. idx = R_GS;
  5299. else
  5300. idx = R_FS;
  5301. cpu_x86_load_seg(env, idx, 0);
  5302. env->segs[idx].base = addr;
  5303. break;
  5304. case TARGET_ARCH_GET_GS:
  5305. case TARGET_ARCH_GET_FS:
  5306. if (code == TARGET_ARCH_GET_GS)
  5307. idx = R_GS;
  5308. else
  5309. idx = R_FS;
  5310. val = env->segs[idx].base;
  5311. if (put_user(val, addr, abi_ulong))
  5312. ret = -TARGET_EFAULT;
  5313. break;
  5314. default:
  5315. ret = -TARGET_EINVAL;
  5316. break;
  5317. }
  5318. return ret;
  5319. }
  5320. #endif /* defined(TARGET_ABI32 */
  5321. #endif /* defined(TARGET_I386) */
  5322. #define NEW_STACK_SIZE 0x40000
  5323. static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
  5324. typedef struct {
  5325. CPUArchState *env;
  5326. pthread_mutex_t mutex;
  5327. pthread_cond_t cond;
  5328. pthread_t thread;
  5329. uint32_t tid;
  5330. abi_ulong child_tidptr;
  5331. abi_ulong parent_tidptr;
  5332. sigset_t sigmask;
  5333. } new_thread_info;
  5334. static void *clone_func(void *arg)
  5335. {
  5336. new_thread_info *info = arg;
  5337. CPUArchState *env;
  5338. CPUState *cpu;
  5339. TaskState *ts;
  5340. rcu_register_thread();
  5341. tcg_register_thread();
  5342. env = info->env;
  5343. cpu = env_cpu(env);
  5344. thread_cpu = cpu;
  5345. ts = (TaskState *)cpu->opaque;
  5346. info->tid = sys_gettid();
  5347. task_settid(ts);
  5348. if (info->child_tidptr)
  5349. put_user_u32(info->tid, info->child_tidptr);
  5350. if (info->parent_tidptr)
  5351. put_user_u32(info->tid, info->parent_tidptr);
  5352. qemu_guest_random_seed_thread_part2(cpu->random_seed);
  5353. /* Enable signals. */
  5354. sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
  5355. /* Signal to the parent that we're ready. */
  5356. pthread_mutex_lock(&info->mutex);
  5357. pthread_cond_broadcast(&info->cond);
  5358. pthread_mutex_unlock(&info->mutex);
  5359. /* Wait until the parent has finished initializing the tls state. */
  5360. pthread_mutex_lock(&clone_lock);
  5361. pthread_mutex_unlock(&clone_lock);
  5362. cpu_loop(env);
  5363. /* never exits */
  5364. return NULL;
  5365. }
  5366. /* do_fork() Must return host values and target errnos (unlike most
  5367. do_*() functions). */
  5368. static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
  5369. abi_ulong parent_tidptr, target_ulong newtls,
  5370. abi_ulong child_tidptr)
  5371. {
  5372. CPUState *cpu = env_cpu(env);
  5373. int ret;
  5374. TaskState *ts;
  5375. CPUState *new_cpu;
  5376. CPUArchState *new_env;
  5377. sigset_t sigmask;
  5378. flags &= ~CLONE_IGNORED_FLAGS;
  5379. /* Emulate vfork() with fork() */
  5380. if (flags & CLONE_VFORK)
  5381. flags &= ~(CLONE_VFORK | CLONE_VM);
  5382. if (flags & CLONE_VM) {
  5383. TaskState *parent_ts = (TaskState *)cpu->opaque;
  5384. new_thread_info info;
  5385. pthread_attr_t attr;
  5386. if (((flags & CLONE_THREAD_FLAGS) != CLONE_THREAD_FLAGS) ||
  5387. (flags & CLONE_INVALID_THREAD_FLAGS)) {
  5388. return -TARGET_EINVAL;
  5389. }
  5390. ts = g_new0(TaskState, 1);
  5391. init_task_state(ts);
  5392. /* Grab a mutex so that thread setup appears atomic. */
  5393. pthread_mutex_lock(&clone_lock);
  5394. /* we create a new CPU instance. */
  5395. new_env = cpu_copy(env);
  5396. /* Init regs that differ from the parent. */
  5397. cpu_clone_regs_child(new_env, newsp, flags);
  5398. cpu_clone_regs_parent(env, flags);
  5399. new_cpu = env_cpu(new_env);
  5400. new_cpu->opaque = ts;
  5401. ts->bprm = parent_ts->bprm;
  5402. ts->info = parent_ts->info;
  5403. ts->signal_mask = parent_ts->signal_mask;
  5404. if (flags & CLONE_CHILD_CLEARTID) {
  5405. ts->child_tidptr = child_tidptr;
  5406. }
  5407. if (flags & CLONE_SETTLS) {
  5408. cpu_set_tls (new_env, newtls);
  5409. }
  5410. memset(&info, 0, sizeof(info));
  5411. pthread_mutex_init(&info.mutex, NULL);
  5412. pthread_mutex_lock(&info.mutex);
  5413. pthread_cond_init(&info.cond, NULL);
  5414. info.env = new_env;
  5415. if (flags & CLONE_CHILD_SETTID) {
  5416. info.child_tidptr = child_tidptr;
  5417. }
  5418. if (flags & CLONE_PARENT_SETTID) {
  5419. info.parent_tidptr = parent_tidptr;
  5420. }
  5421. ret = pthread_attr_init(&attr);
  5422. ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
  5423. ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  5424. /* It is not safe to deliver signals until the child has finished
  5425. initializing, so temporarily block all signals. */
  5426. sigfillset(&sigmask);
  5427. sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
  5428. cpu->random_seed = qemu_guest_random_seed_thread_part1();
  5429. /* If this is our first additional thread, we need to ensure we
  5430. * generate code for parallel execution and flush old translations.
  5431. */
  5432. if (!parallel_cpus) {
  5433. parallel_cpus = true;
  5434. tb_flush(cpu);
  5435. }
  5436. ret = pthread_create(&info.thread, &attr, clone_func, &info);
  5437. /* TODO: Free new CPU state if thread creation failed. */
  5438. sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
  5439. pthread_attr_destroy(&attr);
  5440. if (ret == 0) {
  5441. /* Wait for the child to initialize. */
  5442. pthread_cond_wait(&info.cond, &info.mutex);
  5443. ret = info.tid;
  5444. } else {
  5445. ret = -1;
  5446. }
  5447. pthread_mutex_unlock(&info.mutex);
  5448. pthread_cond_destroy(&info.cond);
  5449. pthread_mutex_destroy(&info.mutex);
  5450. pthread_mutex_unlock(&clone_lock);
  5451. } else {
  5452. /* if no CLONE_VM, we consider it is a fork */
  5453. if (flags & CLONE_INVALID_FORK_FLAGS) {
  5454. return -TARGET_EINVAL;
  5455. }
  5456. /* We can't support custom termination signals */
  5457. if ((flags & CSIGNAL) != TARGET_SIGCHLD) {
  5458. return -TARGET_EINVAL;
  5459. }
  5460. if (block_signals()) {
  5461. return -TARGET_ERESTARTSYS;
  5462. }
  5463. fork_start();
  5464. ret = fork();
  5465. if (ret == 0) {
  5466. /* Child Process. */
  5467. cpu_clone_regs_child(env, newsp, flags);
  5468. fork_end(1);
  5469. /* There is a race condition here. The parent process could
  5470. theoretically read the TID in the child process before the child
  5471. tid is set. This would require using either ptrace
  5472. (not implemented) or having *_tidptr to point at a shared memory
  5473. mapping. We can't repeat the spinlock hack used above because
  5474. the child process gets its own copy of the lock. */
  5475. if (flags & CLONE_CHILD_SETTID)
  5476. put_user_u32(sys_gettid(), child_tidptr);
  5477. if (flags & CLONE_PARENT_SETTID)
  5478. put_user_u32(sys_gettid(), parent_tidptr);
  5479. ts = (TaskState *)cpu->opaque;
  5480. if (flags & CLONE_SETTLS)
  5481. cpu_set_tls (env, newtls);
  5482. if (flags & CLONE_CHILD_CLEARTID)
  5483. ts->child_tidptr = child_tidptr;
  5484. } else {
  5485. cpu_clone_regs_parent(env, flags);
  5486. fork_end(0);
  5487. }
  5488. }
  5489. return ret;
  5490. }
  5491. /* warning : doesn't handle linux specific flags... */
  5492. static int target_to_host_fcntl_cmd(int cmd)
  5493. {
  5494. int ret;
  5495. switch(cmd) {
  5496. case TARGET_F_DUPFD:
  5497. case TARGET_F_GETFD:
  5498. case TARGET_F_SETFD:
  5499. case TARGET_F_GETFL:
  5500. case TARGET_F_SETFL:
  5501. ret = cmd;
  5502. break;
  5503. case TARGET_F_GETLK:
  5504. ret = F_GETLK64;
  5505. break;
  5506. case TARGET_F_SETLK:
  5507. ret = F_SETLK64;
  5508. break;
  5509. case TARGET_F_SETLKW:
  5510. ret = F_SETLKW64;
  5511. break;
  5512. case TARGET_F_GETOWN:
  5513. ret = F_GETOWN;
  5514. break;
  5515. case TARGET_F_SETOWN:
  5516. ret = F_SETOWN;
  5517. break;
  5518. case TARGET_F_GETSIG:
  5519. ret = F_GETSIG;
  5520. break;
  5521. case TARGET_F_SETSIG:
  5522. ret = F_SETSIG;
  5523. break;
  5524. #if TARGET_ABI_BITS == 32
  5525. case TARGET_F_GETLK64:
  5526. ret = F_GETLK64;
  5527. break;
  5528. case TARGET_F_SETLK64:
  5529. ret = F_SETLK64;
  5530. break;
  5531. case TARGET_F_SETLKW64:
  5532. ret = F_SETLKW64;
  5533. break;
  5534. #endif
  5535. case TARGET_F_SETLEASE:
  5536. ret = F_SETLEASE;
  5537. break;
  5538. case TARGET_F_GETLEASE:
  5539. ret = F_GETLEASE;
  5540. break;
  5541. #ifdef F_DUPFD_CLOEXEC
  5542. case TARGET_F_DUPFD_CLOEXEC:
  5543. ret = F_DUPFD_CLOEXEC;
  5544. break;
  5545. #endif
  5546. case TARGET_F_NOTIFY:
  5547. ret = F_NOTIFY;
  5548. break;
  5549. #ifdef F_GETOWN_EX
  5550. case TARGET_F_GETOWN_EX:
  5551. ret = F_GETOWN_EX;
  5552. break;
  5553. #endif
  5554. #ifdef F_SETOWN_EX
  5555. case TARGET_F_SETOWN_EX:
  5556. ret = F_SETOWN_EX;
  5557. break;
  5558. #endif
  5559. #ifdef F_SETPIPE_SZ
  5560. case TARGET_F_SETPIPE_SZ:
  5561. ret = F_SETPIPE_SZ;
  5562. break;
  5563. case TARGET_F_GETPIPE_SZ:
  5564. ret = F_GETPIPE_SZ;
  5565. break;
  5566. #endif
  5567. default:
  5568. ret = -TARGET_EINVAL;
  5569. break;
  5570. }
  5571. #if defined(__powerpc64__)
  5572. /* On PPC64, glibc headers has the F_*LK* defined to 12, 13 and 14 and
  5573. * is not supported by kernel. The glibc fcntl call actually adjusts
  5574. * them to 5, 6 and 7 before making the syscall(). Since we make the
  5575. * syscall directly, adjust to what is supported by the kernel.
  5576. */
  5577. if (ret >= F_GETLK64 && ret <= F_SETLKW64) {
  5578. ret -= F_GETLK64 - 5;
  5579. }
  5580. #endif
  5581. return ret;
  5582. }
  5583. #define FLOCK_TRANSTBL \
  5584. switch (type) { \
  5585. TRANSTBL_CONVERT(F_RDLCK); \
  5586. TRANSTBL_CONVERT(F_WRLCK); \
  5587. TRANSTBL_CONVERT(F_UNLCK); \
  5588. TRANSTBL_CONVERT(F_EXLCK); \
  5589. TRANSTBL_CONVERT(F_SHLCK); \
  5590. }
  5591. static int target_to_host_flock(int type)
  5592. {
  5593. #define TRANSTBL_CONVERT(a) case TARGET_##a: return a
  5594. FLOCK_TRANSTBL
  5595. #undef TRANSTBL_CONVERT
  5596. return -TARGET_EINVAL;
  5597. }
  5598. static int host_to_target_flock(int type)
  5599. {
  5600. #define TRANSTBL_CONVERT(a) case a: return TARGET_##a
  5601. FLOCK_TRANSTBL
  5602. #undef TRANSTBL_CONVERT
  5603. /* if we don't know how to convert the value coming
  5604. * from the host we copy to the target field as-is
  5605. */
  5606. return type;
  5607. }
  5608. static inline abi_long copy_from_user_flock(struct flock64 *fl,
  5609. abi_ulong target_flock_addr)
  5610. {
  5611. struct target_flock *target_fl;
  5612. int l_type;
  5613. if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
  5614. return -TARGET_EFAULT;
  5615. }
  5616. __get_user(l_type, &target_fl->l_type);
  5617. l_type = target_to_host_flock(l_type);
  5618. if (l_type < 0) {
  5619. return l_type;
  5620. }
  5621. fl->l_type = l_type;
  5622. __get_user(fl->l_whence, &target_fl->l_whence);
  5623. __get_user(fl->l_start, &target_fl->l_start);
  5624. __get_user(fl->l_len, &target_fl->l_len);
  5625. __get_user(fl->l_pid, &target_fl->l_pid);
  5626. unlock_user_struct(target_fl, target_flock_addr, 0);
  5627. return 0;
  5628. }
  5629. static inline abi_long copy_to_user_flock(abi_ulong target_flock_addr,
  5630. const struct flock64 *fl)
  5631. {
  5632. struct target_flock *target_fl;
  5633. short l_type;
  5634. if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
  5635. return -TARGET_EFAULT;
  5636. }
  5637. l_type = host_to_target_flock(fl->l_type);
  5638. __put_user(l_type, &target_fl->l_type);
  5639. __put_user(fl->l_whence, &target_fl->l_whence);
  5640. __put_user(fl->l_start, &target_fl->l_start);
  5641. __put_user(fl->l_len, &target_fl->l_len);
  5642. __put_user(fl->l_pid, &target_fl->l_pid);
  5643. unlock_user_struct(target_fl, target_flock_addr, 1);
  5644. return 0;
  5645. }
  5646. typedef abi_long from_flock64_fn(struct flock64 *fl, abi_ulong target_addr);
  5647. typedef abi_long to_flock64_fn(abi_ulong target_addr, const struct flock64 *fl);
  5648. #if defined(TARGET_ARM) && TARGET_ABI_BITS == 32
  5649. static inline abi_long copy_from_user_oabi_flock64(struct flock64 *fl,
  5650. abi_ulong target_flock_addr)
  5651. {
  5652. struct target_oabi_flock64 *target_fl;
  5653. int l_type;
  5654. if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
  5655. return -TARGET_EFAULT;
  5656. }
  5657. __get_user(l_type, &target_fl->l_type);
  5658. l_type = target_to_host_flock(l_type);
  5659. if (l_type < 0) {
  5660. return l_type;
  5661. }
  5662. fl->l_type = l_type;
  5663. __get_user(fl->l_whence, &target_fl->l_whence);
  5664. __get_user(fl->l_start, &target_fl->l_start);
  5665. __get_user(fl->l_len, &target_fl->l_len);
  5666. __get_user(fl->l_pid, &target_fl->l_pid);
  5667. unlock_user_struct(target_fl, target_flock_addr, 0);
  5668. return 0;
  5669. }
  5670. static inline abi_long copy_to_user_oabi_flock64(abi_ulong target_flock_addr,
  5671. const struct flock64 *fl)
  5672. {
  5673. struct target_oabi_flock64 *target_fl;
  5674. short l_type;
  5675. if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
  5676. return -TARGET_EFAULT;
  5677. }
  5678. l_type = host_to_target_flock(fl->l_type);
  5679. __put_user(l_type, &target_fl->l_type);
  5680. __put_user(fl->l_whence, &target_fl->l_whence);
  5681. __put_user(fl->l_start, &target_fl->l_start);
  5682. __put_user(fl->l_len, &target_fl->l_len);
  5683. __put_user(fl->l_pid, &target_fl->l_pid);
  5684. unlock_user_struct(target_fl, target_flock_addr, 1);
  5685. return 0;
  5686. }
  5687. #endif
  5688. static inline abi_long copy_from_user_flock64(struct flock64 *fl,
  5689. abi_ulong target_flock_addr)
  5690. {
  5691. struct target_flock64 *target_fl;
  5692. int l_type;
  5693. if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
  5694. return -TARGET_EFAULT;
  5695. }
  5696. __get_user(l_type, &target_fl->l_type);
  5697. l_type = target_to_host_flock(l_type);
  5698. if (l_type < 0) {
  5699. return l_type;
  5700. }
  5701. fl->l_type = l_type;
  5702. __get_user(fl->l_whence, &target_fl->l_whence);
  5703. __get_user(fl->l_start, &target_fl->l_start);
  5704. __get_user(fl->l_len, &target_fl->l_len);
  5705. __get_user(fl->l_pid, &target_fl->l_pid);
  5706. unlock_user_struct(target_fl, target_flock_addr, 0);
  5707. return 0;
  5708. }
  5709. static inline abi_long copy_to_user_flock64(abi_ulong target_flock_addr,
  5710. const struct flock64 *fl)
  5711. {
  5712. struct target_flock64 *target_fl;
  5713. short l_type;
  5714. if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
  5715. return -TARGET_EFAULT;
  5716. }
  5717. l_type = host_to_target_flock(fl->l_type);
  5718. __put_user(l_type, &target_fl->l_type);
  5719. __put_user(fl->l_whence, &target_fl->l_whence);
  5720. __put_user(fl->l_start, &target_fl->l_start);
  5721. __put_user(fl->l_len, &target_fl->l_len);
  5722. __put_user(fl->l_pid, &target_fl->l_pid);
  5723. unlock_user_struct(target_fl, target_flock_addr, 1);
  5724. return 0;
  5725. }
  5726. static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
  5727. {
  5728. struct flock64 fl64;
  5729. #ifdef F_GETOWN_EX
  5730. struct f_owner_ex fox;
  5731. struct target_f_owner_ex *target_fox;
  5732. #endif
  5733. abi_long ret;
  5734. int host_cmd = target_to_host_fcntl_cmd(cmd);
  5735. if (host_cmd == -TARGET_EINVAL)
  5736. return host_cmd;
  5737. switch(cmd) {
  5738. case TARGET_F_GETLK:
  5739. ret = copy_from_user_flock(&fl64, arg);
  5740. if (ret) {
  5741. return ret;
  5742. }
  5743. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  5744. if (ret == 0) {
  5745. ret = copy_to_user_flock(arg, &fl64);
  5746. }
  5747. break;
  5748. case TARGET_F_SETLK:
  5749. case TARGET_F_SETLKW:
  5750. ret = copy_from_user_flock(&fl64, arg);
  5751. if (ret) {
  5752. return ret;
  5753. }
  5754. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  5755. break;
  5756. case TARGET_F_GETLK64:
  5757. ret = copy_from_user_flock64(&fl64, arg);
  5758. if (ret) {
  5759. return ret;
  5760. }
  5761. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  5762. if (ret == 0) {
  5763. ret = copy_to_user_flock64(arg, &fl64);
  5764. }
  5765. break;
  5766. case TARGET_F_SETLK64:
  5767. case TARGET_F_SETLKW64:
  5768. ret = copy_from_user_flock64(&fl64, arg);
  5769. if (ret) {
  5770. return ret;
  5771. }
  5772. ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
  5773. break;
  5774. case TARGET_F_GETFL:
  5775. ret = get_errno(safe_fcntl(fd, host_cmd, arg));
  5776. if (ret >= 0) {
  5777. ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
  5778. }
  5779. break;
  5780. case TARGET_F_SETFL:
  5781. ret = get_errno(safe_fcntl(fd, host_cmd,
  5782. target_to_host_bitmask(arg,
  5783. fcntl_flags_tbl)));
  5784. break;
  5785. #ifdef F_GETOWN_EX
  5786. case TARGET_F_GETOWN_EX:
  5787. ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
  5788. if (ret >= 0) {
  5789. if (!lock_user_struct(VERIFY_WRITE, target_fox, arg, 0))
  5790. return -TARGET_EFAULT;
  5791. target_fox->type = tswap32(fox.type);
  5792. target_fox->pid = tswap32(fox.pid);
  5793. unlock_user_struct(target_fox, arg, 1);
  5794. }
  5795. break;
  5796. #endif
  5797. #ifdef F_SETOWN_EX
  5798. case TARGET_F_SETOWN_EX:
  5799. if (!lock_user_struct(VERIFY_READ, target_fox, arg, 1))
  5800. return -TARGET_EFAULT;
  5801. fox.type = tswap32(target_fox->type);
  5802. fox.pid = tswap32(target_fox->pid);
  5803. unlock_user_struct(target_fox, arg, 0);
  5804. ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
  5805. break;
  5806. #endif
  5807. case TARGET_F_SETOWN:
  5808. case TARGET_F_GETOWN:
  5809. case TARGET_F_SETSIG:
  5810. case TARGET_F_GETSIG:
  5811. case TARGET_F_SETLEASE:
  5812. case TARGET_F_GETLEASE:
  5813. case TARGET_F_SETPIPE_SZ:
  5814. case TARGET_F_GETPIPE_SZ:
  5815. ret = get_errno(safe_fcntl(fd, host_cmd, arg));
  5816. break;
  5817. default:
  5818. ret = get_errno(safe_fcntl(fd, cmd, arg));
  5819. break;
  5820. }
  5821. return ret;
  5822. }
  5823. #ifdef USE_UID16
  5824. static inline int high2lowuid(int uid)
  5825. {
  5826. if (uid > 65535)
  5827. return 65534;
  5828. else
  5829. return uid;
  5830. }
  5831. static inline int high2lowgid(int gid)
  5832. {
  5833. if (gid > 65535)
  5834. return 65534;
  5835. else
  5836. return gid;
  5837. }
  5838. static inline int low2highuid(int uid)
  5839. {
  5840. if ((int16_t)uid == -1)
  5841. return -1;
  5842. else
  5843. return uid;
  5844. }
  5845. static inline int low2highgid(int gid)
  5846. {
  5847. if ((int16_t)gid == -1)
  5848. return -1;
  5849. else
  5850. return gid;
  5851. }
  5852. static inline int tswapid(int id)
  5853. {
  5854. return tswap16(id);
  5855. }
  5856. #define put_user_id(x, gaddr) put_user_u16(x, gaddr)
  5857. #else /* !USE_UID16 */
  5858. static inline int high2lowuid(int uid)
  5859. {
  5860. return uid;
  5861. }
  5862. static inline int high2lowgid(int gid)
  5863. {
  5864. return gid;
  5865. }
  5866. static inline int low2highuid(int uid)
  5867. {
  5868. return uid;
  5869. }
  5870. static inline int low2highgid(int gid)
  5871. {
  5872. return gid;
  5873. }
  5874. static inline int tswapid(int id)
  5875. {
  5876. return tswap32(id);
  5877. }
  5878. #define put_user_id(x, gaddr) put_user_u32(x, gaddr)
  5879. #endif /* USE_UID16 */
  5880. /* We must do direct syscalls for setting UID/GID, because we want to
  5881. * implement the Linux system call semantics of "change only for this thread",
  5882. * not the libc/POSIX semantics of "change for all threads in process".
  5883. * (See http://ewontfix.com/17/ for more details.)
  5884. * We use the 32-bit version of the syscalls if present; if it is not
  5885. * then either the host architecture supports 32-bit UIDs natively with
  5886. * the standard syscall, or the 16-bit UID is the best we can do.
  5887. */
  5888. #ifdef __NR_setuid32
  5889. #define __NR_sys_setuid __NR_setuid32
  5890. #else
  5891. #define __NR_sys_setuid __NR_setuid
  5892. #endif
  5893. #ifdef __NR_setgid32
  5894. #define __NR_sys_setgid __NR_setgid32
  5895. #else
  5896. #define __NR_sys_setgid __NR_setgid
  5897. #endif
  5898. #ifdef __NR_setresuid32
  5899. #define __NR_sys_setresuid __NR_setresuid32
  5900. #else
  5901. #define __NR_sys_setresuid __NR_setresuid
  5902. #endif
  5903. #ifdef __NR_setresgid32
  5904. #define __NR_sys_setresgid __NR_setresgid32
  5905. #else
  5906. #define __NR_sys_setresgid __NR_setresgid
  5907. #endif
  5908. _syscall1(int, sys_setuid, uid_t, uid)
  5909. _syscall1(int, sys_setgid, gid_t, gid)
  5910. _syscall3(int, sys_setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
  5911. _syscall3(int, sys_setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
  5912. void syscall_init(void)
  5913. {
  5914. IOCTLEntry *ie;
  5915. const argtype *arg_type;
  5916. int size;
  5917. int i;
  5918. thunk_init(STRUCT_MAX);
  5919. #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
  5920. #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
  5921. #include "syscall_types.h"
  5922. #undef STRUCT
  5923. #undef STRUCT_SPECIAL
  5924. /* Build target_to_host_errno_table[] table from
  5925. * host_to_target_errno_table[]. */
  5926. for (i = 0; i < ERRNO_TABLE_SIZE; i++) {
  5927. target_to_host_errno_table[host_to_target_errno_table[i]] = i;
  5928. }
  5929. /* we patch the ioctl size if necessary. We rely on the fact that
  5930. no ioctl has all the bits at '1' in the size field */
  5931. ie = ioctl_entries;
  5932. while (ie->target_cmd != 0) {
  5933. if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
  5934. TARGET_IOC_SIZEMASK) {
  5935. arg_type = ie->arg_type;
  5936. if (arg_type[0] != TYPE_PTR) {
  5937. fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
  5938. ie->target_cmd);
  5939. exit(1);
  5940. }
  5941. arg_type++;
  5942. size = thunk_type_size(arg_type, 0);
  5943. ie->target_cmd = (ie->target_cmd &
  5944. ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
  5945. (size << TARGET_IOC_SIZESHIFT);
  5946. }
  5947. /* automatic consistency check if same arch */
  5948. #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
  5949. (defined(__x86_64__) && defined(TARGET_X86_64))
  5950. if (unlikely(ie->target_cmd != ie->host_cmd)) {
  5951. fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
  5952. ie->name, ie->target_cmd, ie->host_cmd);
  5953. }
  5954. #endif
  5955. ie++;
  5956. }
  5957. }
  5958. #if TARGET_ABI_BITS == 32
  5959. static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
  5960. {
  5961. #ifdef TARGET_WORDS_BIGENDIAN
  5962. return ((uint64_t)word0 << 32) | word1;
  5963. #else
  5964. return ((uint64_t)word1 << 32) | word0;
  5965. #endif
  5966. }
  5967. #else /* TARGET_ABI_BITS == 32 */
  5968. static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
  5969. {
  5970. return word0;
  5971. }
  5972. #endif /* TARGET_ABI_BITS != 32 */
  5973. #ifdef TARGET_NR_truncate64
  5974. static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
  5975. abi_long arg2,
  5976. abi_long arg3,
  5977. abi_long arg4)
  5978. {
  5979. if (regpairs_aligned(cpu_env, TARGET_NR_truncate64)) {
  5980. arg2 = arg3;
  5981. arg3 = arg4;
  5982. }
  5983. return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
  5984. }
  5985. #endif
  5986. #ifdef TARGET_NR_ftruncate64
  5987. static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
  5988. abi_long arg2,
  5989. abi_long arg3,
  5990. abi_long arg4)
  5991. {
  5992. if (regpairs_aligned(cpu_env, TARGET_NR_ftruncate64)) {
  5993. arg2 = arg3;
  5994. arg3 = arg4;
  5995. }
  5996. return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
  5997. }
  5998. #endif
  5999. #if defined(TARGET_NR_timer_settime) || \
  6000. (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
  6001. static inline abi_long target_to_host_itimerspec(struct itimerspec *host_itspec,
  6002. abi_ulong target_addr)
  6003. {
  6004. struct target_itimerspec *target_itspec;
  6005. if (!lock_user_struct(VERIFY_READ, target_itspec, target_addr, 1)) {
  6006. return -TARGET_EFAULT;
  6007. }
  6008. host_itspec->it_interval.tv_sec =
  6009. tswapal(target_itspec->it_interval.tv_sec);
  6010. host_itspec->it_interval.tv_nsec =
  6011. tswapal(target_itspec->it_interval.tv_nsec);
  6012. host_itspec->it_value.tv_sec = tswapal(target_itspec->it_value.tv_sec);
  6013. host_itspec->it_value.tv_nsec = tswapal(target_itspec->it_value.tv_nsec);
  6014. unlock_user_struct(target_itspec, target_addr, 1);
  6015. return 0;
  6016. }
  6017. #endif
  6018. #if ((defined(TARGET_NR_timerfd_gettime) || \
  6019. defined(TARGET_NR_timerfd_settime)) && defined(CONFIG_TIMERFD)) || \
  6020. defined(TARGET_NR_timer_gettime) || defined(TARGET_NR_timer_settime)
  6021. static inline abi_long host_to_target_itimerspec(abi_ulong target_addr,
  6022. struct itimerspec *host_its)
  6023. {
  6024. struct target_itimerspec *target_itspec;
  6025. if (!lock_user_struct(VERIFY_WRITE, target_itspec, target_addr, 0)) {
  6026. return -TARGET_EFAULT;
  6027. }
  6028. target_itspec->it_interval.tv_sec = tswapal(host_its->it_interval.tv_sec);
  6029. target_itspec->it_interval.tv_nsec = tswapal(host_its->it_interval.tv_nsec);
  6030. target_itspec->it_value.tv_sec = tswapal(host_its->it_value.tv_sec);
  6031. target_itspec->it_value.tv_nsec = tswapal(host_its->it_value.tv_nsec);
  6032. unlock_user_struct(target_itspec, target_addr, 0);
  6033. return 0;
  6034. }
  6035. #endif
  6036. #if defined(TARGET_NR_adjtimex) || \
  6037. (defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME))
  6038. static inline abi_long target_to_host_timex(struct timex *host_tx,
  6039. abi_long target_addr)
  6040. {
  6041. struct target_timex *target_tx;
  6042. if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) {
  6043. return -TARGET_EFAULT;
  6044. }
  6045. __get_user(host_tx->modes, &target_tx->modes);
  6046. __get_user(host_tx->offset, &target_tx->offset);
  6047. __get_user(host_tx->freq, &target_tx->freq);
  6048. __get_user(host_tx->maxerror, &target_tx->maxerror);
  6049. __get_user(host_tx->esterror, &target_tx->esterror);
  6050. __get_user(host_tx->status, &target_tx->status);
  6051. __get_user(host_tx->constant, &target_tx->constant);
  6052. __get_user(host_tx->precision, &target_tx->precision);
  6053. __get_user(host_tx->tolerance, &target_tx->tolerance);
  6054. __get_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
  6055. __get_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
  6056. __get_user(host_tx->tick, &target_tx->tick);
  6057. __get_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  6058. __get_user(host_tx->jitter, &target_tx->jitter);
  6059. __get_user(host_tx->shift, &target_tx->shift);
  6060. __get_user(host_tx->stabil, &target_tx->stabil);
  6061. __get_user(host_tx->jitcnt, &target_tx->jitcnt);
  6062. __get_user(host_tx->calcnt, &target_tx->calcnt);
  6063. __get_user(host_tx->errcnt, &target_tx->errcnt);
  6064. __get_user(host_tx->stbcnt, &target_tx->stbcnt);
  6065. __get_user(host_tx->tai, &target_tx->tai);
  6066. unlock_user_struct(target_tx, target_addr, 0);
  6067. return 0;
  6068. }
  6069. static inline abi_long host_to_target_timex(abi_long target_addr,
  6070. struct timex *host_tx)
  6071. {
  6072. struct target_timex *target_tx;
  6073. if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) {
  6074. return -TARGET_EFAULT;
  6075. }
  6076. __put_user(host_tx->modes, &target_tx->modes);
  6077. __put_user(host_tx->offset, &target_tx->offset);
  6078. __put_user(host_tx->freq, &target_tx->freq);
  6079. __put_user(host_tx->maxerror, &target_tx->maxerror);
  6080. __put_user(host_tx->esterror, &target_tx->esterror);
  6081. __put_user(host_tx->status, &target_tx->status);
  6082. __put_user(host_tx->constant, &target_tx->constant);
  6083. __put_user(host_tx->precision, &target_tx->precision);
  6084. __put_user(host_tx->tolerance, &target_tx->tolerance);
  6085. __put_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
  6086. __put_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
  6087. __put_user(host_tx->tick, &target_tx->tick);
  6088. __put_user(host_tx->ppsfreq, &target_tx->ppsfreq);
  6089. __put_user(host_tx->jitter, &target_tx->jitter);
  6090. __put_user(host_tx->shift, &target_tx->shift);
  6091. __put_user(host_tx->stabil, &target_tx->stabil);
  6092. __put_user(host_tx->jitcnt, &target_tx->jitcnt);
  6093. __put_user(host_tx->calcnt, &target_tx->calcnt);
  6094. __put_user(host_tx->errcnt, &target_tx->errcnt);
  6095. __put_user(host_tx->stbcnt, &target_tx->stbcnt);
  6096. __put_user(host_tx->tai, &target_tx->tai);
  6097. unlock_user_struct(target_tx, target_addr, 1);
  6098. return 0;
  6099. }
  6100. #endif
  6101. static inline abi_long target_to_host_sigevent(struct sigevent *host_sevp,
  6102. abi_ulong target_addr)
  6103. {
  6104. struct target_sigevent *target_sevp;
  6105. if (!lock_user_struct(VERIFY_READ, target_sevp, target_addr, 1)) {
  6106. return -TARGET_EFAULT;
  6107. }
  6108. /* This union is awkward on 64 bit systems because it has a 32 bit
  6109. * integer and a pointer in it; we follow the conversion approach
  6110. * used for handling sigval types in signal.c so the guest should get
  6111. * the correct value back even if we did a 64 bit byteswap and it's
  6112. * using the 32 bit integer.
  6113. */
  6114. host_sevp->sigev_value.sival_ptr =
  6115. (void *)(uintptr_t)tswapal(target_sevp->sigev_value.sival_ptr);
  6116. host_sevp->sigev_signo =
  6117. target_to_host_signal(tswap32(target_sevp->sigev_signo));
  6118. host_sevp->sigev_notify = tswap32(target_sevp->sigev_notify);
  6119. host_sevp->_sigev_un._tid = tswap32(target_sevp->_sigev_un._tid);
  6120. unlock_user_struct(target_sevp, target_addr, 1);
  6121. return 0;
  6122. }
  6123. #if defined(TARGET_NR_mlockall)
  6124. static inline int target_to_host_mlockall_arg(int arg)
  6125. {
  6126. int result = 0;
  6127. if (arg & TARGET_MLOCKALL_MCL_CURRENT) {
  6128. result |= MCL_CURRENT;
  6129. }
  6130. if (arg & TARGET_MLOCKALL_MCL_FUTURE) {
  6131. result |= MCL_FUTURE;
  6132. }
  6133. return result;
  6134. }
  6135. #endif
  6136. #if (defined(TARGET_NR_stat64) || defined(TARGET_NR_lstat64) || \
  6137. defined(TARGET_NR_fstat64) || defined(TARGET_NR_fstatat64) || \
  6138. defined(TARGET_NR_newfstatat))
  6139. static inline abi_long host_to_target_stat64(void *cpu_env,
  6140. abi_ulong target_addr,
  6141. struct stat *host_st)
  6142. {
  6143. #if defined(TARGET_ARM) && defined(TARGET_ABI32)
  6144. if (((CPUARMState *)cpu_env)->eabi) {
  6145. struct target_eabi_stat64 *target_st;
  6146. if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
  6147. return -TARGET_EFAULT;
  6148. memset(target_st, 0, sizeof(struct target_eabi_stat64));
  6149. __put_user(host_st->st_dev, &target_st->st_dev);
  6150. __put_user(host_st->st_ino, &target_st->st_ino);
  6151. #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
  6152. __put_user(host_st->st_ino, &target_st->__st_ino);
  6153. #endif
  6154. __put_user(host_st->st_mode, &target_st->st_mode);
  6155. __put_user(host_st->st_nlink, &target_st->st_nlink);
  6156. __put_user(host_st->st_uid, &target_st->st_uid);
  6157. __put_user(host_st->st_gid, &target_st->st_gid);
  6158. __put_user(host_st->st_rdev, &target_st->st_rdev);
  6159. __put_user(host_st->st_size, &target_st->st_size);
  6160. __put_user(host_st->st_blksize, &target_st->st_blksize);
  6161. __put_user(host_st->st_blocks, &target_st->st_blocks);
  6162. __put_user(host_st->st_atime, &target_st->target_st_atime);
  6163. __put_user(host_st->st_mtime, &target_st->target_st_mtime);
  6164. __put_user(host_st->st_ctime, &target_st->target_st_ctime);
  6165. #if _POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700
  6166. __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
  6167. __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
  6168. __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
  6169. #endif
  6170. unlock_user_struct(target_st, target_addr, 1);
  6171. } else
  6172. #endif
  6173. {
  6174. #if defined(TARGET_HAS_STRUCT_STAT64)
  6175. struct target_stat64 *target_st;
  6176. #else
  6177. struct target_stat *target_st;
  6178. #endif
  6179. if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
  6180. return -TARGET_EFAULT;
  6181. memset(target_st, 0, sizeof(*target_st));
  6182. __put_user(host_st->st_dev, &target_st->st_dev);
  6183. __put_user(host_st->st_ino, &target_st->st_ino);
  6184. #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
  6185. __put_user(host_st->st_ino, &target_st->__st_ino);
  6186. #endif
  6187. __put_user(host_st->st_mode, &target_st->st_mode);
  6188. __put_user(host_st->st_nlink, &target_st->st_nlink);
  6189. __put_user(host_st->st_uid, &target_st->st_uid);
  6190. __put_user(host_st->st_gid, &target_st->st_gid);
  6191. __put_user(host_st->st_rdev, &target_st->st_rdev);
  6192. /* XXX: better use of kernel struct */
  6193. __put_user(host_st->st_size, &target_st->st_size);
  6194. __put_user(host_st->st_blksize, &target_st->st_blksize);
  6195. __put_user(host_st->st_blocks, &target_st->st_blocks);
  6196. __put_user(host_st->st_atime, &target_st->target_st_atime);
  6197. __put_user(host_st->st_mtime, &target_st->target_st_mtime);
  6198. __put_user(host_st->st_ctime, &target_st->target_st_ctime);
  6199. #if _POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700
  6200. __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
  6201. __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
  6202. __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
  6203. #endif
  6204. unlock_user_struct(target_st, target_addr, 1);
  6205. }
  6206. return 0;
  6207. }
  6208. #endif
  6209. #if defined(TARGET_NR_statx) && defined(__NR_statx)
  6210. static inline abi_long host_to_target_statx(struct target_statx *host_stx,
  6211. abi_ulong target_addr)
  6212. {
  6213. struct target_statx *target_stx;
  6214. if (!lock_user_struct(VERIFY_WRITE, target_stx, target_addr, 0)) {
  6215. return -TARGET_EFAULT;
  6216. }
  6217. memset(target_stx, 0, sizeof(*target_stx));
  6218. __put_user(host_stx->stx_mask, &target_stx->stx_mask);
  6219. __put_user(host_stx->stx_blksize, &target_stx->stx_blksize);
  6220. __put_user(host_stx->stx_attributes, &target_stx->stx_attributes);
  6221. __put_user(host_stx->stx_nlink, &target_stx->stx_nlink);
  6222. __put_user(host_stx->stx_uid, &target_stx->stx_uid);
  6223. __put_user(host_stx->stx_gid, &target_stx->stx_gid);
  6224. __put_user(host_stx->stx_mode, &target_stx->stx_mode);
  6225. __put_user(host_stx->stx_ino, &target_stx->stx_ino);
  6226. __put_user(host_stx->stx_size, &target_stx->stx_size);
  6227. __put_user(host_stx->stx_blocks, &target_stx->stx_blocks);
  6228. __put_user(host_stx->stx_attributes_mask, &target_stx->stx_attributes_mask);
  6229. __put_user(host_stx->stx_atime.tv_sec, &target_stx->stx_atime.tv_sec);
  6230. __put_user(host_stx->stx_atime.tv_nsec, &target_stx->stx_atime.tv_nsec);
  6231. __put_user(host_stx->stx_btime.tv_sec, &target_stx->stx_btime.tv_sec);
  6232. __put_user(host_stx->stx_btime.tv_nsec, &target_stx->stx_btime.tv_nsec);
  6233. __put_user(host_stx->stx_ctime.tv_sec, &target_stx->stx_ctime.tv_sec);
  6234. __put_user(host_stx->stx_ctime.tv_nsec, &target_stx->stx_ctime.tv_nsec);
  6235. __put_user(host_stx->stx_mtime.tv_sec, &target_stx->stx_mtime.tv_sec);
  6236. __put_user(host_stx->stx_mtime.tv_nsec, &target_stx->stx_mtime.tv_nsec);
  6237. __put_user(host_stx->stx_rdev_major, &target_stx->stx_rdev_major);
  6238. __put_user(host_stx->stx_rdev_minor, &target_stx->stx_rdev_minor);
  6239. __put_user(host_stx->stx_dev_major, &target_stx->stx_dev_major);
  6240. __put_user(host_stx->stx_dev_minor, &target_stx->stx_dev_minor);
  6241. unlock_user_struct(target_stx, target_addr, 1);
  6242. return 0;
  6243. }
  6244. #endif
  6245. static int do_sys_futex(int *uaddr, int op, int val,
  6246. const struct timespec *timeout, int *uaddr2,
  6247. int val3)
  6248. {
  6249. #if HOST_LONG_BITS == 64
  6250. #if defined(__NR_futex)
  6251. /* always a 64-bit time_t, it doesn't define _time64 version */
  6252. return sys_futex(uaddr, op, val, timeout, uaddr2, val3);
  6253. #endif
  6254. #else /* HOST_LONG_BITS == 64 */
  6255. #if defined(__NR_futex_time64)
  6256. if (sizeof(timeout->tv_sec) == 8) {
  6257. /* _time64 function on 32bit arch */
  6258. return sys_futex_time64(uaddr, op, val, timeout, uaddr2, val3);
  6259. }
  6260. #endif
  6261. #if defined(__NR_futex)
  6262. /* old function on 32bit arch */
  6263. return sys_futex(uaddr, op, val, timeout, uaddr2, val3);
  6264. #endif
  6265. #endif /* HOST_LONG_BITS == 64 */
  6266. g_assert_not_reached();
  6267. }
  6268. static int do_safe_futex(int *uaddr, int op, int val,
  6269. const struct timespec *timeout, int *uaddr2,
  6270. int val3)
  6271. {
  6272. #if HOST_LONG_BITS == 64
  6273. #if defined(__NR_futex)
  6274. /* always a 64-bit time_t, it doesn't define _time64 version */
  6275. return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3));
  6276. #endif
  6277. #else /* HOST_LONG_BITS == 64 */
  6278. #if defined(__NR_futex_time64)
  6279. if (sizeof(timeout->tv_sec) == 8) {
  6280. /* _time64 function on 32bit arch */
  6281. return get_errno(safe_futex_time64(uaddr, op, val, timeout, uaddr2,
  6282. val3));
  6283. }
  6284. #endif
  6285. #if defined(__NR_futex)
  6286. /* old function on 32bit arch */
  6287. return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3));
  6288. #endif
  6289. #endif /* HOST_LONG_BITS == 64 */
  6290. return -TARGET_ENOSYS;
  6291. }
  6292. /* ??? Using host futex calls even when target atomic operations
  6293. are not really atomic probably breaks things. However implementing
  6294. futexes locally would make futexes shared between multiple processes
  6295. tricky. However they're probably useless because guest atomic
  6296. operations won't work either. */
  6297. #if defined(TARGET_NR_futex)
  6298. static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
  6299. target_ulong uaddr2, int val3)
  6300. {
  6301. struct timespec ts, *pts;
  6302. int base_op;
  6303. /* ??? We assume FUTEX_* constants are the same on both host
  6304. and target. */
  6305. #ifdef FUTEX_CMD_MASK
  6306. base_op = op & FUTEX_CMD_MASK;
  6307. #else
  6308. base_op = op;
  6309. #endif
  6310. switch (base_op) {
  6311. case FUTEX_WAIT:
  6312. case FUTEX_WAIT_BITSET:
  6313. if (timeout) {
  6314. pts = &ts;
  6315. target_to_host_timespec(pts, timeout);
  6316. } else {
  6317. pts = NULL;
  6318. }
  6319. return do_safe_futex(g2h(uaddr), op, tswap32(val), pts, NULL, val3);
  6320. case FUTEX_WAKE:
  6321. return do_safe_futex(g2h(uaddr), op, val, NULL, NULL, 0);
  6322. case FUTEX_FD:
  6323. return do_safe_futex(g2h(uaddr), op, val, NULL, NULL, 0);
  6324. case FUTEX_REQUEUE:
  6325. case FUTEX_CMP_REQUEUE:
  6326. case FUTEX_WAKE_OP:
  6327. /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
  6328. TIMEOUT parameter is interpreted as a uint32_t by the kernel.
  6329. But the prototype takes a `struct timespec *'; insert casts
  6330. to satisfy the compiler. We do not need to tswap TIMEOUT
  6331. since it's not compared to guest memory. */
  6332. pts = (struct timespec *)(uintptr_t) timeout;
  6333. return do_safe_futex(g2h(uaddr), op, val, pts, g2h(uaddr2),
  6334. (base_op == FUTEX_CMP_REQUEUE
  6335. ? tswap32(val3)
  6336. : val3));
  6337. default:
  6338. return -TARGET_ENOSYS;
  6339. }
  6340. }
  6341. #endif
  6342. #if defined(TARGET_NR_futex_time64)
  6343. static int do_futex_time64(target_ulong uaddr, int op, int val, target_ulong timeout,
  6344. target_ulong uaddr2, int val3)
  6345. {
  6346. struct timespec ts, *pts;
  6347. int base_op;
  6348. /* ??? We assume FUTEX_* constants are the same on both host
  6349. and target. */
  6350. #ifdef FUTEX_CMD_MASK
  6351. base_op = op & FUTEX_CMD_MASK;
  6352. #else
  6353. base_op = op;
  6354. #endif
  6355. switch (base_op) {
  6356. case FUTEX_WAIT:
  6357. case FUTEX_WAIT_BITSET:
  6358. if (timeout) {
  6359. pts = &ts;
  6360. target_to_host_timespec64(pts, timeout);
  6361. } else {
  6362. pts = NULL;
  6363. }
  6364. return do_safe_futex(g2h(uaddr), op, tswap32(val), pts, NULL, val3);
  6365. case FUTEX_WAKE:
  6366. return do_safe_futex(g2h(uaddr), op, val, NULL, NULL, 0);
  6367. case FUTEX_FD:
  6368. return do_safe_futex(g2h(uaddr), op, val, NULL, NULL, 0);
  6369. case FUTEX_REQUEUE:
  6370. case FUTEX_CMP_REQUEUE:
  6371. case FUTEX_WAKE_OP:
  6372. /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
  6373. TIMEOUT parameter is interpreted as a uint32_t by the kernel.
  6374. But the prototype takes a `struct timespec *'; insert casts
  6375. to satisfy the compiler. We do not need to tswap TIMEOUT
  6376. since it's not compared to guest memory. */
  6377. pts = (struct timespec *)(uintptr_t) timeout;
  6378. return do_safe_futex(g2h(uaddr), op, val, pts, g2h(uaddr2),
  6379. (base_op == FUTEX_CMP_REQUEUE
  6380. ? tswap32(val3)
  6381. : val3));
  6382. default:
  6383. return -TARGET_ENOSYS;
  6384. }
  6385. }
  6386. #endif
  6387. #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  6388. static abi_long do_name_to_handle_at(abi_long dirfd, abi_long pathname,
  6389. abi_long handle, abi_long mount_id,
  6390. abi_long flags)
  6391. {
  6392. struct file_handle *target_fh;
  6393. struct file_handle *fh;
  6394. int mid = 0;
  6395. abi_long ret;
  6396. char *name;
  6397. unsigned int size, total_size;
  6398. if (get_user_s32(size, handle)) {
  6399. return -TARGET_EFAULT;
  6400. }
  6401. name = lock_user_string(pathname);
  6402. if (!name) {
  6403. return -TARGET_EFAULT;
  6404. }
  6405. total_size = sizeof(struct file_handle) + size;
  6406. target_fh = lock_user(VERIFY_WRITE, handle, total_size, 0);
  6407. if (!target_fh) {
  6408. unlock_user(name, pathname, 0);
  6409. return -TARGET_EFAULT;
  6410. }
  6411. fh = g_malloc0(total_size);
  6412. fh->handle_bytes = size;
  6413. ret = get_errno(name_to_handle_at(dirfd, path(name), fh, &mid, flags));
  6414. unlock_user(name, pathname, 0);
  6415. /* man name_to_handle_at(2):
  6416. * Other than the use of the handle_bytes field, the caller should treat
  6417. * the file_handle structure as an opaque data type
  6418. */
  6419. memcpy(target_fh, fh, total_size);
  6420. target_fh->handle_bytes = tswap32(fh->handle_bytes);
  6421. target_fh->handle_type = tswap32(fh->handle_type);
  6422. g_free(fh);
  6423. unlock_user(target_fh, handle, total_size);
  6424. if (put_user_s32(mid, mount_id)) {
  6425. return -TARGET_EFAULT;
  6426. }
  6427. return ret;
  6428. }
  6429. #endif
  6430. #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  6431. static abi_long do_open_by_handle_at(abi_long mount_fd, abi_long handle,
  6432. abi_long flags)
  6433. {
  6434. struct file_handle *target_fh;
  6435. struct file_handle *fh;
  6436. unsigned int size, total_size;
  6437. abi_long ret;
  6438. if (get_user_s32(size, handle)) {
  6439. return -TARGET_EFAULT;
  6440. }
  6441. total_size = sizeof(struct file_handle) + size;
  6442. target_fh = lock_user(VERIFY_READ, handle, total_size, 1);
  6443. if (!target_fh) {
  6444. return -TARGET_EFAULT;
  6445. }
  6446. fh = g_memdup(target_fh, total_size);
  6447. fh->handle_bytes = size;
  6448. fh->handle_type = tswap32(target_fh->handle_type);
  6449. ret = get_errno(open_by_handle_at(mount_fd, fh,
  6450. target_to_host_bitmask(flags, fcntl_flags_tbl)));
  6451. g_free(fh);
  6452. unlock_user(target_fh, handle, total_size);
  6453. return ret;
  6454. }
  6455. #endif
  6456. #if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4)
  6457. static abi_long do_signalfd4(int fd, abi_long mask, int flags)
  6458. {
  6459. int host_flags;
  6460. target_sigset_t *target_mask;
  6461. sigset_t host_mask;
  6462. abi_long ret;
  6463. if (flags & ~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC)) {
  6464. return -TARGET_EINVAL;
  6465. }
  6466. if (!lock_user_struct(VERIFY_READ, target_mask, mask, 1)) {
  6467. return -TARGET_EFAULT;
  6468. }
  6469. target_to_host_sigset(&host_mask, target_mask);
  6470. host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
  6471. ret = get_errno(signalfd(fd, &host_mask, host_flags));
  6472. if (ret >= 0) {
  6473. fd_trans_register(ret, &target_signalfd_trans);
  6474. }
  6475. unlock_user_struct(target_mask, mask, 0);
  6476. return ret;
  6477. }
  6478. #endif
  6479. /* Map host to target signal numbers for the wait family of syscalls.
  6480. Assume all other status bits are the same. */
  6481. int host_to_target_waitstatus(int status)
  6482. {
  6483. if (WIFSIGNALED(status)) {
  6484. return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
  6485. }
  6486. if (WIFSTOPPED(status)) {
  6487. return (host_to_target_signal(WSTOPSIG(status)) << 8)
  6488. | (status & 0xff);
  6489. }
  6490. return status;
  6491. }
  6492. static int open_self_cmdline(void *cpu_env, int fd)
  6493. {
  6494. CPUState *cpu = env_cpu((CPUArchState *)cpu_env);
  6495. struct linux_binprm *bprm = ((TaskState *)cpu->opaque)->bprm;
  6496. int i;
  6497. for (i = 0; i < bprm->argc; i++) {
  6498. size_t len = strlen(bprm->argv[i]) + 1;
  6499. if (write(fd, bprm->argv[i], len) != len) {
  6500. return -1;
  6501. }
  6502. }
  6503. return 0;
  6504. }
  6505. static int open_self_maps(void *cpu_env, int fd)
  6506. {
  6507. CPUState *cpu = env_cpu((CPUArchState *)cpu_env);
  6508. TaskState *ts = cpu->opaque;
  6509. GSList *map_info = read_self_maps();
  6510. GSList *s;
  6511. int count;
  6512. for (s = map_info; s; s = g_slist_next(s)) {
  6513. MapInfo *e = (MapInfo *) s->data;
  6514. if (h2g_valid(e->start)) {
  6515. unsigned long min = e->start;
  6516. unsigned long max = e->end;
  6517. int flags = page_get_flags(h2g(min));
  6518. const char *path;
  6519. max = h2g_valid(max - 1) ?
  6520. max : (uintptr_t) g2h(GUEST_ADDR_MAX) + 1;
  6521. if (page_check_range(h2g(min), max - min, flags) == -1) {
  6522. continue;
  6523. }
  6524. if (h2g(min) == ts->info->stack_limit) {
  6525. path = "[stack]";
  6526. } else {
  6527. path = e->path;
  6528. }
  6529. count = dprintf(fd, TARGET_ABI_FMT_ptr "-" TARGET_ABI_FMT_ptr
  6530. " %c%c%c%c %08" PRIx64 " %s %"PRId64,
  6531. h2g(min), h2g(max - 1) + 1,
  6532. e->is_read ? 'r' : '-',
  6533. e->is_write ? 'w' : '-',
  6534. e->is_exec ? 'x' : '-',
  6535. e->is_priv ? 'p' : '-',
  6536. (uint64_t) e->offset, e->dev, e->inode);
  6537. if (path) {
  6538. dprintf(fd, "%*s%s\n", 73 - count, "", path);
  6539. } else {
  6540. dprintf(fd, "\n");
  6541. }
  6542. }
  6543. }
  6544. free_self_maps(map_info);
  6545. #ifdef TARGET_VSYSCALL_PAGE
  6546. /*
  6547. * We only support execution from the vsyscall page.
  6548. * This is as if CONFIG_LEGACY_VSYSCALL_XONLY=y from v5.3.
  6549. */
  6550. count = dprintf(fd, TARGET_FMT_lx "-" TARGET_FMT_lx
  6551. " --xp 00000000 00:00 0",
  6552. TARGET_VSYSCALL_PAGE, TARGET_VSYSCALL_PAGE + TARGET_PAGE_SIZE);
  6553. dprintf(fd, "%*s%s\n", 73 - count, "", "[vsyscall]");
  6554. #endif
  6555. return 0;
  6556. }
  6557. static int open_self_stat(void *cpu_env, int fd)
  6558. {
  6559. CPUState *cpu = env_cpu((CPUArchState *)cpu_env);
  6560. TaskState *ts = cpu->opaque;
  6561. g_autoptr(GString) buf = g_string_new(NULL);
  6562. int i;
  6563. for (i = 0; i < 44; i++) {
  6564. if (i == 0) {
  6565. /* pid */
  6566. g_string_printf(buf, FMT_pid " ", getpid());
  6567. } else if (i == 1) {
  6568. /* app name */
  6569. gchar *bin = g_strrstr(ts->bprm->argv[0], "/");
  6570. bin = bin ? bin + 1 : ts->bprm->argv[0];
  6571. g_string_printf(buf, "(%.15s) ", bin);
  6572. } else if (i == 27) {
  6573. /* stack bottom */
  6574. g_string_printf(buf, TARGET_ABI_FMT_ld " ", ts->info->start_stack);
  6575. } else {
  6576. /* for the rest, there is MasterCard */
  6577. g_string_printf(buf, "0%c", i == 43 ? '\n' : ' ');
  6578. }
  6579. if (write(fd, buf->str, buf->len) != buf->len) {
  6580. return -1;
  6581. }
  6582. }
  6583. return 0;
  6584. }
  6585. static int open_self_auxv(void *cpu_env, int fd)
  6586. {
  6587. CPUState *cpu = env_cpu((CPUArchState *)cpu_env);
  6588. TaskState *ts = cpu->opaque;
  6589. abi_ulong auxv = ts->info->saved_auxv;
  6590. abi_ulong len = ts->info->auxv_len;
  6591. char *ptr;
  6592. /*
  6593. * Auxiliary vector is stored in target process stack.
  6594. * read in whole auxv vector and copy it to file
  6595. */
  6596. ptr = lock_user(VERIFY_READ, auxv, len, 0);
  6597. if (ptr != NULL) {
  6598. while (len > 0) {
  6599. ssize_t r;
  6600. r = write(fd, ptr, len);
  6601. if (r <= 0) {
  6602. break;
  6603. }
  6604. len -= r;
  6605. ptr += r;
  6606. }
  6607. lseek(fd, 0, SEEK_SET);
  6608. unlock_user(ptr, auxv, len);
  6609. }
  6610. return 0;
  6611. }
  6612. static int is_proc_myself(const char *filename, const char *entry)
  6613. {
  6614. if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
  6615. filename += strlen("/proc/");
  6616. if (!strncmp(filename, "self/", strlen("self/"))) {
  6617. filename += strlen("self/");
  6618. } else if (*filename >= '1' && *filename <= '9') {
  6619. char myself[80];
  6620. snprintf(myself, sizeof(myself), "%d/", getpid());
  6621. if (!strncmp(filename, myself, strlen(myself))) {
  6622. filename += strlen(myself);
  6623. } else {
  6624. return 0;
  6625. }
  6626. } else {
  6627. return 0;
  6628. }
  6629. if (!strcmp(filename, entry)) {
  6630. return 1;
  6631. }
  6632. }
  6633. return 0;
  6634. }
  6635. #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN) || \
  6636. defined(TARGET_SPARC) || defined(TARGET_M68K)
  6637. static int is_proc(const char *filename, const char *entry)
  6638. {
  6639. return strcmp(filename, entry) == 0;
  6640. }
  6641. #endif
  6642. #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
  6643. static int open_net_route(void *cpu_env, int fd)
  6644. {
  6645. FILE *fp;
  6646. char *line = NULL;
  6647. size_t len = 0;
  6648. ssize_t read;
  6649. fp = fopen("/proc/net/route", "r");
  6650. if (fp == NULL) {
  6651. return -1;
  6652. }
  6653. /* read header */
  6654. read = getline(&line, &len, fp);
  6655. dprintf(fd, "%s", line);
  6656. /* read routes */
  6657. while ((read = getline(&line, &len, fp)) != -1) {
  6658. char iface[16];
  6659. uint32_t dest, gw, mask;
  6660. unsigned int flags, refcnt, use, metric, mtu, window, irtt;
  6661. int fields;
  6662. fields = sscanf(line,
  6663. "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
  6664. iface, &dest, &gw, &flags, &refcnt, &use, &metric,
  6665. &mask, &mtu, &window, &irtt);
  6666. if (fields != 11) {
  6667. continue;
  6668. }
  6669. dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
  6670. iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
  6671. metric, tswap32(mask), mtu, window, irtt);
  6672. }
  6673. free(line);
  6674. fclose(fp);
  6675. return 0;
  6676. }
  6677. #endif
  6678. #if defined(TARGET_SPARC)
  6679. static int open_cpuinfo(void *cpu_env, int fd)
  6680. {
  6681. dprintf(fd, "type\t\t: sun4u\n");
  6682. return 0;
  6683. }
  6684. #endif
  6685. #if defined(TARGET_M68K)
  6686. static int open_hardware(void *cpu_env, int fd)
  6687. {
  6688. dprintf(fd, "Model:\t\tqemu-m68k\n");
  6689. return 0;
  6690. }
  6691. #endif
  6692. static int do_openat(void *cpu_env, int dirfd, const char *pathname, int flags, mode_t mode)
  6693. {
  6694. struct fake_open {
  6695. const char *filename;
  6696. int (*fill)(void *cpu_env, int fd);
  6697. int (*cmp)(const char *s1, const char *s2);
  6698. };
  6699. const struct fake_open *fake_open;
  6700. static const struct fake_open fakes[] = {
  6701. { "maps", open_self_maps, is_proc_myself },
  6702. { "stat", open_self_stat, is_proc_myself },
  6703. { "auxv", open_self_auxv, is_proc_myself },
  6704. { "cmdline", open_self_cmdline, is_proc_myself },
  6705. #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
  6706. { "/proc/net/route", open_net_route, is_proc },
  6707. #endif
  6708. #if defined(TARGET_SPARC)
  6709. { "/proc/cpuinfo", open_cpuinfo, is_proc },
  6710. #endif
  6711. #if defined(TARGET_M68K)
  6712. { "/proc/hardware", open_hardware, is_proc },
  6713. #endif
  6714. { NULL, NULL, NULL }
  6715. };
  6716. if (is_proc_myself(pathname, "exe")) {
  6717. int execfd = qemu_getauxval(AT_EXECFD);
  6718. return execfd ? execfd : safe_openat(dirfd, exec_path, flags, mode);
  6719. }
  6720. for (fake_open = fakes; fake_open->filename; fake_open++) {
  6721. if (fake_open->cmp(pathname, fake_open->filename)) {
  6722. break;
  6723. }
  6724. }
  6725. if (fake_open->filename) {
  6726. const char *tmpdir;
  6727. char filename[PATH_MAX];
  6728. int fd, r;
  6729. /* create temporary file to map stat to */
  6730. tmpdir = getenv("TMPDIR");
  6731. if (!tmpdir)
  6732. tmpdir = "/tmp";
  6733. snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
  6734. fd = mkstemp(filename);
  6735. if (fd < 0) {
  6736. return fd;
  6737. }
  6738. unlink(filename);
  6739. if ((r = fake_open->fill(cpu_env, fd))) {
  6740. int e = errno;
  6741. close(fd);
  6742. errno = e;
  6743. return r;
  6744. }
  6745. lseek(fd, 0, SEEK_SET);
  6746. return fd;
  6747. }
  6748. return safe_openat(dirfd, path(pathname), flags, mode);
  6749. }
  6750. #define TIMER_MAGIC 0x0caf0000
  6751. #define TIMER_MAGIC_MASK 0xffff0000
  6752. /* Convert QEMU provided timer ID back to internal 16bit index format */
  6753. static target_timer_t get_timer_id(abi_long arg)
  6754. {
  6755. target_timer_t timerid = arg;
  6756. if ((timerid & TIMER_MAGIC_MASK) != TIMER_MAGIC) {
  6757. return -TARGET_EINVAL;
  6758. }
  6759. timerid &= 0xffff;
  6760. if (timerid >= ARRAY_SIZE(g_posix_timers)) {
  6761. return -TARGET_EINVAL;
  6762. }
  6763. return timerid;
  6764. }
  6765. static int target_to_host_cpu_mask(unsigned long *host_mask,
  6766. size_t host_size,
  6767. abi_ulong target_addr,
  6768. size_t target_size)
  6769. {
  6770. unsigned target_bits = sizeof(abi_ulong) * 8;
  6771. unsigned host_bits = sizeof(*host_mask) * 8;
  6772. abi_ulong *target_mask;
  6773. unsigned i, j;
  6774. assert(host_size >= target_size);
  6775. target_mask = lock_user(VERIFY_READ, target_addr, target_size, 1);
  6776. if (!target_mask) {
  6777. return -TARGET_EFAULT;
  6778. }
  6779. memset(host_mask, 0, host_size);
  6780. for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
  6781. unsigned bit = i * target_bits;
  6782. abi_ulong val;
  6783. __get_user(val, &target_mask[i]);
  6784. for (j = 0; j < target_bits; j++, bit++) {
  6785. if (val & (1UL << j)) {
  6786. host_mask[bit / host_bits] |= 1UL << (bit % host_bits);
  6787. }
  6788. }
  6789. }
  6790. unlock_user(target_mask, target_addr, 0);
  6791. return 0;
  6792. }
  6793. static int host_to_target_cpu_mask(const unsigned long *host_mask,
  6794. size_t host_size,
  6795. abi_ulong target_addr,
  6796. size_t target_size)
  6797. {
  6798. unsigned target_bits = sizeof(abi_ulong) * 8;
  6799. unsigned host_bits = sizeof(*host_mask) * 8;
  6800. abi_ulong *target_mask;
  6801. unsigned i, j;
  6802. assert(host_size >= target_size);
  6803. target_mask = lock_user(VERIFY_WRITE, target_addr, target_size, 0);
  6804. if (!target_mask) {
  6805. return -TARGET_EFAULT;
  6806. }
  6807. for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
  6808. unsigned bit = i * target_bits;
  6809. abi_ulong val = 0;
  6810. for (j = 0; j < target_bits; j++, bit++) {
  6811. if (host_mask[bit / host_bits] & (1UL << (bit % host_bits))) {
  6812. val |= 1UL << j;
  6813. }
  6814. }
  6815. __put_user(val, &target_mask[i]);
  6816. }
  6817. unlock_user(target_mask, target_addr, target_size);
  6818. return 0;
  6819. }
  6820. /* This is an internal helper for do_syscall so that it is easier
  6821. * to have a single return point, so that actions, such as logging
  6822. * of syscall results, can be performed.
  6823. * All errnos that do_syscall() returns must be -TARGET_<errcode>.
  6824. */
  6825. static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
  6826. abi_long arg2, abi_long arg3, abi_long arg4,
  6827. abi_long arg5, abi_long arg6, abi_long arg7,
  6828. abi_long arg8)
  6829. {
  6830. CPUState *cpu = env_cpu(cpu_env);
  6831. abi_long ret;
  6832. #if defined(TARGET_NR_stat) || defined(TARGET_NR_stat64) \
  6833. || defined(TARGET_NR_lstat) || defined(TARGET_NR_lstat64) \
  6834. || defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64) \
  6835. || defined(TARGET_NR_statx)
  6836. struct stat st;
  6837. #endif
  6838. #if defined(TARGET_NR_statfs) || defined(TARGET_NR_statfs64) \
  6839. || defined(TARGET_NR_fstatfs)
  6840. struct statfs stfs;
  6841. #endif
  6842. void *p;
  6843. switch(num) {
  6844. case TARGET_NR_exit:
  6845. /* In old applications this may be used to implement _exit(2).
  6846. However in threaded applictions it is used for thread termination,
  6847. and _exit_group is used for application termination.
  6848. Do thread termination if we have more then one thread. */
  6849. if (block_signals()) {
  6850. return -TARGET_ERESTARTSYS;
  6851. }
  6852. cpu_list_lock();
  6853. if (CPU_NEXT(first_cpu)) {
  6854. TaskState *ts;
  6855. /* Remove the CPU from the list. */
  6856. QTAILQ_REMOVE_RCU(&cpus, cpu, node);
  6857. cpu_list_unlock();
  6858. ts = cpu->opaque;
  6859. if (ts->child_tidptr) {
  6860. put_user_u32(0, ts->child_tidptr);
  6861. do_sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
  6862. NULL, NULL, 0);
  6863. }
  6864. thread_cpu = NULL;
  6865. object_unref(OBJECT(cpu));
  6866. g_free(ts);
  6867. rcu_unregister_thread();
  6868. pthread_exit(NULL);
  6869. }
  6870. cpu_list_unlock();
  6871. preexit_cleanup(cpu_env, arg1);
  6872. _exit(arg1);
  6873. return 0; /* avoid warning */
  6874. case TARGET_NR_read:
  6875. if (arg2 == 0 && arg3 == 0) {
  6876. return get_errno(safe_read(arg1, 0, 0));
  6877. } else {
  6878. if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
  6879. return -TARGET_EFAULT;
  6880. ret = get_errno(safe_read(arg1, p, arg3));
  6881. if (ret >= 0 &&
  6882. fd_trans_host_to_target_data(arg1)) {
  6883. ret = fd_trans_host_to_target_data(arg1)(p, ret);
  6884. }
  6885. unlock_user(p, arg2, ret);
  6886. }
  6887. return ret;
  6888. case TARGET_NR_write:
  6889. if (arg2 == 0 && arg3 == 0) {
  6890. return get_errno(safe_write(arg1, 0, 0));
  6891. }
  6892. if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
  6893. return -TARGET_EFAULT;
  6894. if (fd_trans_target_to_host_data(arg1)) {
  6895. void *copy = g_malloc(arg3);
  6896. memcpy(copy, p, arg3);
  6897. ret = fd_trans_target_to_host_data(arg1)(copy, arg3);
  6898. if (ret >= 0) {
  6899. ret = get_errno(safe_write(arg1, copy, ret));
  6900. }
  6901. g_free(copy);
  6902. } else {
  6903. ret = get_errno(safe_write(arg1, p, arg3));
  6904. }
  6905. unlock_user(p, arg2, 0);
  6906. return ret;
  6907. #ifdef TARGET_NR_open
  6908. case TARGET_NR_open:
  6909. if (!(p = lock_user_string(arg1)))
  6910. return -TARGET_EFAULT;
  6911. ret = get_errno(do_openat(cpu_env, AT_FDCWD, p,
  6912. target_to_host_bitmask(arg2, fcntl_flags_tbl),
  6913. arg3));
  6914. fd_trans_unregister(ret);
  6915. unlock_user(p, arg1, 0);
  6916. return ret;
  6917. #endif
  6918. case TARGET_NR_openat:
  6919. if (!(p = lock_user_string(arg2)))
  6920. return -TARGET_EFAULT;
  6921. ret = get_errno(do_openat(cpu_env, arg1, p,
  6922. target_to_host_bitmask(arg3, fcntl_flags_tbl),
  6923. arg4));
  6924. fd_trans_unregister(ret);
  6925. unlock_user(p, arg2, 0);
  6926. return ret;
  6927. #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  6928. case TARGET_NR_name_to_handle_at:
  6929. ret = do_name_to_handle_at(arg1, arg2, arg3, arg4, arg5);
  6930. return ret;
  6931. #endif
  6932. #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
  6933. case TARGET_NR_open_by_handle_at:
  6934. ret = do_open_by_handle_at(arg1, arg2, arg3);
  6935. fd_trans_unregister(ret);
  6936. return ret;
  6937. #endif
  6938. case TARGET_NR_close:
  6939. fd_trans_unregister(arg1);
  6940. return get_errno(close(arg1));
  6941. case TARGET_NR_brk:
  6942. return do_brk(arg1);
  6943. #ifdef TARGET_NR_fork
  6944. case TARGET_NR_fork:
  6945. return get_errno(do_fork(cpu_env, TARGET_SIGCHLD, 0, 0, 0, 0));
  6946. #endif
  6947. #ifdef TARGET_NR_waitpid
  6948. case TARGET_NR_waitpid:
  6949. {
  6950. int status;
  6951. ret = get_errno(safe_wait4(arg1, &status, arg3, 0));
  6952. if (!is_error(ret) && arg2 && ret
  6953. && put_user_s32(host_to_target_waitstatus(status), arg2))
  6954. return -TARGET_EFAULT;
  6955. }
  6956. return ret;
  6957. #endif
  6958. #ifdef TARGET_NR_waitid
  6959. case TARGET_NR_waitid:
  6960. {
  6961. siginfo_t info;
  6962. info.si_pid = 0;
  6963. ret = get_errno(safe_waitid(arg1, arg2, &info, arg4, NULL));
  6964. if (!is_error(ret) && arg3 && info.si_pid != 0) {
  6965. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
  6966. return -TARGET_EFAULT;
  6967. host_to_target_siginfo(p, &info);
  6968. unlock_user(p, arg3, sizeof(target_siginfo_t));
  6969. }
  6970. }
  6971. return ret;
  6972. #endif
  6973. #ifdef TARGET_NR_creat /* not on alpha */
  6974. case TARGET_NR_creat:
  6975. if (!(p = lock_user_string(arg1)))
  6976. return -TARGET_EFAULT;
  6977. ret = get_errno(creat(p, arg2));
  6978. fd_trans_unregister(ret);
  6979. unlock_user(p, arg1, 0);
  6980. return ret;
  6981. #endif
  6982. #ifdef TARGET_NR_link
  6983. case TARGET_NR_link:
  6984. {
  6985. void * p2;
  6986. p = lock_user_string(arg1);
  6987. p2 = lock_user_string(arg2);
  6988. if (!p || !p2)
  6989. ret = -TARGET_EFAULT;
  6990. else
  6991. ret = get_errno(link(p, p2));
  6992. unlock_user(p2, arg2, 0);
  6993. unlock_user(p, arg1, 0);
  6994. }
  6995. return ret;
  6996. #endif
  6997. #if defined(TARGET_NR_linkat)
  6998. case TARGET_NR_linkat:
  6999. {
  7000. void * p2 = NULL;
  7001. if (!arg2 || !arg4)
  7002. return -TARGET_EFAULT;
  7003. p = lock_user_string(arg2);
  7004. p2 = lock_user_string(arg4);
  7005. if (!p || !p2)
  7006. ret = -TARGET_EFAULT;
  7007. else
  7008. ret = get_errno(linkat(arg1, p, arg3, p2, arg5));
  7009. unlock_user(p, arg2, 0);
  7010. unlock_user(p2, arg4, 0);
  7011. }
  7012. return ret;
  7013. #endif
  7014. #ifdef TARGET_NR_unlink
  7015. case TARGET_NR_unlink:
  7016. if (!(p = lock_user_string(arg1)))
  7017. return -TARGET_EFAULT;
  7018. ret = get_errno(unlink(p));
  7019. unlock_user(p, arg1, 0);
  7020. return ret;
  7021. #endif
  7022. #if defined(TARGET_NR_unlinkat)
  7023. case TARGET_NR_unlinkat:
  7024. if (!(p = lock_user_string(arg2)))
  7025. return -TARGET_EFAULT;
  7026. ret = get_errno(unlinkat(arg1, p, arg3));
  7027. unlock_user(p, arg2, 0);
  7028. return ret;
  7029. #endif
  7030. case TARGET_NR_execve:
  7031. {
  7032. char **argp, **envp;
  7033. int argc, envc;
  7034. abi_ulong gp;
  7035. abi_ulong guest_argp;
  7036. abi_ulong guest_envp;
  7037. abi_ulong addr;
  7038. char **q;
  7039. int total_size = 0;
  7040. argc = 0;
  7041. guest_argp = arg2;
  7042. for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
  7043. if (get_user_ual(addr, gp))
  7044. return -TARGET_EFAULT;
  7045. if (!addr)
  7046. break;
  7047. argc++;
  7048. }
  7049. envc = 0;
  7050. guest_envp = arg3;
  7051. for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
  7052. if (get_user_ual(addr, gp))
  7053. return -TARGET_EFAULT;
  7054. if (!addr)
  7055. break;
  7056. envc++;
  7057. }
  7058. argp = g_new0(char *, argc + 1);
  7059. envp = g_new0(char *, envc + 1);
  7060. for (gp = guest_argp, q = argp; gp;
  7061. gp += sizeof(abi_ulong), q++) {
  7062. if (get_user_ual(addr, gp))
  7063. goto execve_efault;
  7064. if (!addr)
  7065. break;
  7066. if (!(*q = lock_user_string(addr)))
  7067. goto execve_efault;
  7068. total_size += strlen(*q) + 1;
  7069. }
  7070. *q = NULL;
  7071. for (gp = guest_envp, q = envp; gp;
  7072. gp += sizeof(abi_ulong), q++) {
  7073. if (get_user_ual(addr, gp))
  7074. goto execve_efault;
  7075. if (!addr)
  7076. break;
  7077. if (!(*q = lock_user_string(addr)))
  7078. goto execve_efault;
  7079. total_size += strlen(*q) + 1;
  7080. }
  7081. *q = NULL;
  7082. if (!(p = lock_user_string(arg1)))
  7083. goto execve_efault;
  7084. /* Although execve() is not an interruptible syscall it is
  7085. * a special case where we must use the safe_syscall wrapper:
  7086. * if we allow a signal to happen before we make the host
  7087. * syscall then we will 'lose' it, because at the point of
  7088. * execve the process leaves QEMU's control. So we use the
  7089. * safe syscall wrapper to ensure that we either take the
  7090. * signal as a guest signal, or else it does not happen
  7091. * before the execve completes and makes it the other
  7092. * program's problem.
  7093. */
  7094. ret = get_errno(safe_execve(p, argp, envp));
  7095. unlock_user(p, arg1, 0);
  7096. goto execve_end;
  7097. execve_efault:
  7098. ret = -TARGET_EFAULT;
  7099. execve_end:
  7100. for (gp = guest_argp, q = argp; *q;
  7101. gp += sizeof(abi_ulong), q++) {
  7102. if (get_user_ual(addr, gp)
  7103. || !addr)
  7104. break;
  7105. unlock_user(*q, addr, 0);
  7106. }
  7107. for (gp = guest_envp, q = envp; *q;
  7108. gp += sizeof(abi_ulong), q++) {
  7109. if (get_user_ual(addr, gp)
  7110. || !addr)
  7111. break;
  7112. unlock_user(*q, addr, 0);
  7113. }
  7114. g_free(argp);
  7115. g_free(envp);
  7116. }
  7117. return ret;
  7118. case TARGET_NR_chdir:
  7119. if (!(p = lock_user_string(arg1)))
  7120. return -TARGET_EFAULT;
  7121. ret = get_errno(chdir(p));
  7122. unlock_user(p, arg1, 0);
  7123. return ret;
  7124. #ifdef TARGET_NR_time
  7125. case TARGET_NR_time:
  7126. {
  7127. time_t host_time;
  7128. ret = get_errno(time(&host_time));
  7129. if (!is_error(ret)
  7130. && arg1
  7131. && put_user_sal(host_time, arg1))
  7132. return -TARGET_EFAULT;
  7133. }
  7134. return ret;
  7135. #endif
  7136. #ifdef TARGET_NR_mknod
  7137. case TARGET_NR_mknod:
  7138. if (!(p = lock_user_string(arg1)))
  7139. return -TARGET_EFAULT;
  7140. ret = get_errno(mknod(p, arg2, arg3));
  7141. unlock_user(p, arg1, 0);
  7142. return ret;
  7143. #endif
  7144. #if defined(TARGET_NR_mknodat)
  7145. case TARGET_NR_mknodat:
  7146. if (!(p = lock_user_string(arg2)))
  7147. return -TARGET_EFAULT;
  7148. ret = get_errno(mknodat(arg1, p, arg3, arg4));
  7149. unlock_user(p, arg2, 0);
  7150. return ret;
  7151. #endif
  7152. #ifdef TARGET_NR_chmod
  7153. case TARGET_NR_chmod:
  7154. if (!(p = lock_user_string(arg1)))
  7155. return -TARGET_EFAULT;
  7156. ret = get_errno(chmod(p, arg2));
  7157. unlock_user(p, arg1, 0);
  7158. return ret;
  7159. #endif
  7160. #ifdef TARGET_NR_lseek
  7161. case TARGET_NR_lseek:
  7162. return get_errno(lseek(arg1, arg2, arg3));
  7163. #endif
  7164. #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
  7165. /* Alpha specific */
  7166. case TARGET_NR_getxpid:
  7167. ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
  7168. return get_errno(getpid());
  7169. #endif
  7170. #ifdef TARGET_NR_getpid
  7171. case TARGET_NR_getpid:
  7172. return get_errno(getpid());
  7173. #endif
  7174. case TARGET_NR_mount:
  7175. {
  7176. /* need to look at the data field */
  7177. void *p2, *p3;
  7178. if (arg1) {
  7179. p = lock_user_string(arg1);
  7180. if (!p) {
  7181. return -TARGET_EFAULT;
  7182. }
  7183. } else {
  7184. p = NULL;
  7185. }
  7186. p2 = lock_user_string(arg2);
  7187. if (!p2) {
  7188. if (arg1) {
  7189. unlock_user(p, arg1, 0);
  7190. }
  7191. return -TARGET_EFAULT;
  7192. }
  7193. if (arg3) {
  7194. p3 = lock_user_string(arg3);
  7195. if (!p3) {
  7196. if (arg1) {
  7197. unlock_user(p, arg1, 0);
  7198. }
  7199. unlock_user(p2, arg2, 0);
  7200. return -TARGET_EFAULT;
  7201. }
  7202. } else {
  7203. p3 = NULL;
  7204. }
  7205. /* FIXME - arg5 should be locked, but it isn't clear how to
  7206. * do that since it's not guaranteed to be a NULL-terminated
  7207. * string.
  7208. */
  7209. if (!arg5) {
  7210. ret = mount(p, p2, p3, (unsigned long)arg4, NULL);
  7211. } else {
  7212. ret = mount(p, p2, p3, (unsigned long)arg4, g2h(arg5));
  7213. }
  7214. ret = get_errno(ret);
  7215. if (arg1) {
  7216. unlock_user(p, arg1, 0);
  7217. }
  7218. unlock_user(p2, arg2, 0);
  7219. if (arg3) {
  7220. unlock_user(p3, arg3, 0);
  7221. }
  7222. }
  7223. return ret;
  7224. #ifdef TARGET_NR_umount
  7225. case TARGET_NR_umount:
  7226. if (!(p = lock_user_string(arg1)))
  7227. return -TARGET_EFAULT;
  7228. ret = get_errno(umount(p));
  7229. unlock_user(p, arg1, 0);
  7230. return ret;
  7231. #endif
  7232. #ifdef TARGET_NR_stime /* not on alpha */
  7233. case TARGET_NR_stime:
  7234. {
  7235. struct timespec ts;
  7236. ts.tv_nsec = 0;
  7237. if (get_user_sal(ts.tv_sec, arg1)) {
  7238. return -TARGET_EFAULT;
  7239. }
  7240. return get_errno(clock_settime(CLOCK_REALTIME, &ts));
  7241. }
  7242. #endif
  7243. #ifdef TARGET_NR_alarm /* not on alpha */
  7244. case TARGET_NR_alarm:
  7245. return alarm(arg1);
  7246. #endif
  7247. #ifdef TARGET_NR_pause /* not on alpha */
  7248. case TARGET_NR_pause:
  7249. if (!block_signals()) {
  7250. sigsuspend(&((TaskState *)cpu->opaque)->signal_mask);
  7251. }
  7252. return -TARGET_EINTR;
  7253. #endif
  7254. #ifdef TARGET_NR_utime
  7255. case TARGET_NR_utime:
  7256. {
  7257. struct utimbuf tbuf, *host_tbuf;
  7258. struct target_utimbuf *target_tbuf;
  7259. if (arg2) {
  7260. if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
  7261. return -TARGET_EFAULT;
  7262. tbuf.actime = tswapal(target_tbuf->actime);
  7263. tbuf.modtime = tswapal(target_tbuf->modtime);
  7264. unlock_user_struct(target_tbuf, arg2, 0);
  7265. host_tbuf = &tbuf;
  7266. } else {
  7267. host_tbuf = NULL;
  7268. }
  7269. if (!(p = lock_user_string(arg1)))
  7270. return -TARGET_EFAULT;
  7271. ret = get_errno(utime(p, host_tbuf));
  7272. unlock_user(p, arg1, 0);
  7273. }
  7274. return ret;
  7275. #endif
  7276. #ifdef TARGET_NR_utimes
  7277. case TARGET_NR_utimes:
  7278. {
  7279. struct timeval *tvp, tv[2];
  7280. if (arg2) {
  7281. if (copy_from_user_timeval(&tv[0], arg2)
  7282. || copy_from_user_timeval(&tv[1],
  7283. arg2 + sizeof(struct target_timeval)))
  7284. return -TARGET_EFAULT;
  7285. tvp = tv;
  7286. } else {
  7287. tvp = NULL;
  7288. }
  7289. if (!(p = lock_user_string(arg1)))
  7290. return -TARGET_EFAULT;
  7291. ret = get_errno(utimes(p, tvp));
  7292. unlock_user(p, arg1, 0);
  7293. }
  7294. return ret;
  7295. #endif
  7296. #if defined(TARGET_NR_futimesat)
  7297. case TARGET_NR_futimesat:
  7298. {
  7299. struct timeval *tvp, tv[2];
  7300. if (arg3) {
  7301. if (copy_from_user_timeval(&tv[0], arg3)
  7302. || copy_from_user_timeval(&tv[1],
  7303. arg3 + sizeof(struct target_timeval)))
  7304. return -TARGET_EFAULT;
  7305. tvp = tv;
  7306. } else {
  7307. tvp = NULL;
  7308. }
  7309. if (!(p = lock_user_string(arg2))) {
  7310. return -TARGET_EFAULT;
  7311. }
  7312. ret = get_errno(futimesat(arg1, path(p), tvp));
  7313. unlock_user(p, arg2, 0);
  7314. }
  7315. return ret;
  7316. #endif
  7317. #ifdef TARGET_NR_access
  7318. case TARGET_NR_access:
  7319. if (!(p = lock_user_string(arg1))) {
  7320. return -TARGET_EFAULT;
  7321. }
  7322. ret = get_errno(access(path(p), arg2));
  7323. unlock_user(p, arg1, 0);
  7324. return ret;
  7325. #endif
  7326. #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
  7327. case TARGET_NR_faccessat:
  7328. if (!(p = lock_user_string(arg2))) {
  7329. return -TARGET_EFAULT;
  7330. }
  7331. ret = get_errno(faccessat(arg1, p, arg3, 0));
  7332. unlock_user(p, arg2, 0);
  7333. return ret;
  7334. #endif
  7335. #ifdef TARGET_NR_nice /* not on alpha */
  7336. case TARGET_NR_nice:
  7337. return get_errno(nice(arg1));
  7338. #endif
  7339. case TARGET_NR_sync:
  7340. sync();
  7341. return 0;
  7342. #if defined(TARGET_NR_syncfs) && defined(CONFIG_SYNCFS)
  7343. case TARGET_NR_syncfs:
  7344. return get_errno(syncfs(arg1));
  7345. #endif
  7346. case TARGET_NR_kill:
  7347. return get_errno(safe_kill(arg1, target_to_host_signal(arg2)));
  7348. #ifdef TARGET_NR_rename
  7349. case TARGET_NR_rename:
  7350. {
  7351. void *p2;
  7352. p = lock_user_string(arg1);
  7353. p2 = lock_user_string(arg2);
  7354. if (!p || !p2)
  7355. ret = -TARGET_EFAULT;
  7356. else
  7357. ret = get_errno(rename(p, p2));
  7358. unlock_user(p2, arg2, 0);
  7359. unlock_user(p, arg1, 0);
  7360. }
  7361. return ret;
  7362. #endif
  7363. #if defined(TARGET_NR_renameat)
  7364. case TARGET_NR_renameat:
  7365. {
  7366. void *p2;
  7367. p = lock_user_string(arg2);
  7368. p2 = lock_user_string(arg4);
  7369. if (!p || !p2)
  7370. ret = -TARGET_EFAULT;
  7371. else
  7372. ret = get_errno(renameat(arg1, p, arg3, p2));
  7373. unlock_user(p2, arg4, 0);
  7374. unlock_user(p, arg2, 0);
  7375. }
  7376. return ret;
  7377. #endif
  7378. #if defined(TARGET_NR_renameat2)
  7379. case TARGET_NR_renameat2:
  7380. {
  7381. void *p2;
  7382. p = lock_user_string(arg2);
  7383. p2 = lock_user_string(arg4);
  7384. if (!p || !p2) {
  7385. ret = -TARGET_EFAULT;
  7386. } else {
  7387. ret = get_errno(sys_renameat2(arg1, p, arg3, p2, arg5));
  7388. }
  7389. unlock_user(p2, arg4, 0);
  7390. unlock_user(p, arg2, 0);
  7391. }
  7392. return ret;
  7393. #endif
  7394. #ifdef TARGET_NR_mkdir
  7395. case TARGET_NR_mkdir:
  7396. if (!(p = lock_user_string(arg1)))
  7397. return -TARGET_EFAULT;
  7398. ret = get_errno(mkdir(p, arg2));
  7399. unlock_user(p, arg1, 0);
  7400. return ret;
  7401. #endif
  7402. #if defined(TARGET_NR_mkdirat)
  7403. case TARGET_NR_mkdirat:
  7404. if (!(p = lock_user_string(arg2)))
  7405. return -TARGET_EFAULT;
  7406. ret = get_errno(mkdirat(arg1, p, arg3));
  7407. unlock_user(p, arg2, 0);
  7408. return ret;
  7409. #endif
  7410. #ifdef TARGET_NR_rmdir
  7411. case TARGET_NR_rmdir:
  7412. if (!(p = lock_user_string(arg1)))
  7413. return -TARGET_EFAULT;
  7414. ret = get_errno(rmdir(p));
  7415. unlock_user(p, arg1, 0);
  7416. return ret;
  7417. #endif
  7418. case TARGET_NR_dup:
  7419. ret = get_errno(dup(arg1));
  7420. if (ret >= 0) {
  7421. fd_trans_dup(arg1, ret);
  7422. }
  7423. return ret;
  7424. #ifdef TARGET_NR_pipe
  7425. case TARGET_NR_pipe:
  7426. return do_pipe(cpu_env, arg1, 0, 0);
  7427. #endif
  7428. #ifdef TARGET_NR_pipe2
  7429. case TARGET_NR_pipe2:
  7430. return do_pipe(cpu_env, arg1,
  7431. target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
  7432. #endif
  7433. case TARGET_NR_times:
  7434. {
  7435. struct target_tms *tmsp;
  7436. struct tms tms;
  7437. ret = get_errno(times(&tms));
  7438. if (arg1) {
  7439. tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
  7440. if (!tmsp)
  7441. return -TARGET_EFAULT;
  7442. tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
  7443. tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
  7444. tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
  7445. tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
  7446. }
  7447. if (!is_error(ret))
  7448. ret = host_to_target_clock_t(ret);
  7449. }
  7450. return ret;
  7451. case TARGET_NR_acct:
  7452. if (arg1 == 0) {
  7453. ret = get_errno(acct(NULL));
  7454. } else {
  7455. if (!(p = lock_user_string(arg1))) {
  7456. return -TARGET_EFAULT;
  7457. }
  7458. ret = get_errno(acct(path(p)));
  7459. unlock_user(p, arg1, 0);
  7460. }
  7461. return ret;
  7462. #ifdef TARGET_NR_umount2
  7463. case TARGET_NR_umount2:
  7464. if (!(p = lock_user_string(arg1)))
  7465. return -TARGET_EFAULT;
  7466. ret = get_errno(umount2(p, arg2));
  7467. unlock_user(p, arg1, 0);
  7468. return ret;
  7469. #endif
  7470. case TARGET_NR_ioctl:
  7471. return do_ioctl(arg1, arg2, arg3);
  7472. #ifdef TARGET_NR_fcntl
  7473. case TARGET_NR_fcntl:
  7474. return do_fcntl(arg1, arg2, arg3);
  7475. #endif
  7476. case TARGET_NR_setpgid:
  7477. return get_errno(setpgid(arg1, arg2));
  7478. case TARGET_NR_umask:
  7479. return get_errno(umask(arg1));
  7480. case TARGET_NR_chroot:
  7481. if (!(p = lock_user_string(arg1)))
  7482. return -TARGET_EFAULT;
  7483. ret = get_errno(chroot(p));
  7484. unlock_user(p, arg1, 0);
  7485. return ret;
  7486. #ifdef TARGET_NR_dup2
  7487. case TARGET_NR_dup2:
  7488. ret = get_errno(dup2(arg1, arg2));
  7489. if (ret >= 0) {
  7490. fd_trans_dup(arg1, arg2);
  7491. }
  7492. return ret;
  7493. #endif
  7494. #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
  7495. case TARGET_NR_dup3:
  7496. {
  7497. int host_flags;
  7498. if ((arg3 & ~TARGET_O_CLOEXEC) != 0) {
  7499. return -EINVAL;
  7500. }
  7501. host_flags = target_to_host_bitmask(arg3, fcntl_flags_tbl);
  7502. ret = get_errno(dup3(arg1, arg2, host_flags));
  7503. if (ret >= 0) {
  7504. fd_trans_dup(arg1, arg2);
  7505. }
  7506. return ret;
  7507. }
  7508. #endif
  7509. #ifdef TARGET_NR_getppid /* not on alpha */
  7510. case TARGET_NR_getppid:
  7511. return get_errno(getppid());
  7512. #endif
  7513. #ifdef TARGET_NR_getpgrp
  7514. case TARGET_NR_getpgrp:
  7515. return get_errno(getpgrp());
  7516. #endif
  7517. case TARGET_NR_setsid:
  7518. return get_errno(setsid());
  7519. #ifdef TARGET_NR_sigaction
  7520. case TARGET_NR_sigaction:
  7521. {
  7522. #if defined(TARGET_ALPHA)
  7523. struct target_sigaction act, oact, *pact = 0;
  7524. struct target_old_sigaction *old_act;
  7525. if (arg2) {
  7526. if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
  7527. return -TARGET_EFAULT;
  7528. act._sa_handler = old_act->_sa_handler;
  7529. target_siginitset(&act.sa_mask, old_act->sa_mask);
  7530. act.sa_flags = old_act->sa_flags;
  7531. act.sa_restorer = 0;
  7532. unlock_user_struct(old_act, arg2, 0);
  7533. pact = &act;
  7534. }
  7535. ret = get_errno(do_sigaction(arg1, pact, &oact));
  7536. if (!is_error(ret) && arg3) {
  7537. if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
  7538. return -TARGET_EFAULT;
  7539. old_act->_sa_handler = oact._sa_handler;
  7540. old_act->sa_mask = oact.sa_mask.sig[0];
  7541. old_act->sa_flags = oact.sa_flags;
  7542. unlock_user_struct(old_act, arg3, 1);
  7543. }
  7544. #elif defined(TARGET_MIPS)
  7545. struct target_sigaction act, oact, *pact, *old_act;
  7546. if (arg2) {
  7547. if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
  7548. return -TARGET_EFAULT;
  7549. act._sa_handler = old_act->_sa_handler;
  7550. target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
  7551. act.sa_flags = old_act->sa_flags;
  7552. unlock_user_struct(old_act, arg2, 0);
  7553. pact = &act;
  7554. } else {
  7555. pact = NULL;
  7556. }
  7557. ret = get_errno(do_sigaction(arg1, pact, &oact));
  7558. if (!is_error(ret) && arg3) {
  7559. if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
  7560. return -TARGET_EFAULT;
  7561. old_act->_sa_handler = oact._sa_handler;
  7562. old_act->sa_flags = oact.sa_flags;
  7563. old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
  7564. old_act->sa_mask.sig[1] = 0;
  7565. old_act->sa_mask.sig[2] = 0;
  7566. old_act->sa_mask.sig[3] = 0;
  7567. unlock_user_struct(old_act, arg3, 1);
  7568. }
  7569. #else
  7570. struct target_old_sigaction *old_act;
  7571. struct target_sigaction act, oact, *pact;
  7572. if (arg2) {
  7573. if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
  7574. return -TARGET_EFAULT;
  7575. act._sa_handler = old_act->_sa_handler;
  7576. target_siginitset(&act.sa_mask, old_act->sa_mask);
  7577. act.sa_flags = old_act->sa_flags;
  7578. act.sa_restorer = old_act->sa_restorer;
  7579. #ifdef TARGET_ARCH_HAS_KA_RESTORER
  7580. act.ka_restorer = 0;
  7581. #endif
  7582. unlock_user_struct(old_act, arg2, 0);
  7583. pact = &act;
  7584. } else {
  7585. pact = NULL;
  7586. }
  7587. ret = get_errno(do_sigaction(arg1, pact, &oact));
  7588. if (!is_error(ret) && arg3) {
  7589. if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
  7590. return -TARGET_EFAULT;
  7591. old_act->_sa_handler = oact._sa_handler;
  7592. old_act->sa_mask = oact.sa_mask.sig[0];
  7593. old_act->sa_flags = oact.sa_flags;
  7594. old_act->sa_restorer = oact.sa_restorer;
  7595. unlock_user_struct(old_act, arg3, 1);
  7596. }
  7597. #endif
  7598. }
  7599. return ret;
  7600. #endif
  7601. case TARGET_NR_rt_sigaction:
  7602. {
  7603. #if defined(TARGET_ALPHA)
  7604. /* For Alpha and SPARC this is a 5 argument syscall, with
  7605. * a 'restorer' parameter which must be copied into the
  7606. * sa_restorer field of the sigaction struct.
  7607. * For Alpha that 'restorer' is arg5; for SPARC it is arg4,
  7608. * and arg5 is the sigsetsize.
  7609. * Alpha also has a separate rt_sigaction struct that it uses
  7610. * here; SPARC uses the usual sigaction struct.
  7611. */
  7612. struct target_rt_sigaction *rt_act;
  7613. struct target_sigaction act, oact, *pact = 0;
  7614. if (arg4 != sizeof(target_sigset_t)) {
  7615. return -TARGET_EINVAL;
  7616. }
  7617. if (arg2) {
  7618. if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
  7619. return -TARGET_EFAULT;
  7620. act._sa_handler = rt_act->_sa_handler;
  7621. act.sa_mask = rt_act->sa_mask;
  7622. act.sa_flags = rt_act->sa_flags;
  7623. act.sa_restorer = arg5;
  7624. unlock_user_struct(rt_act, arg2, 0);
  7625. pact = &act;
  7626. }
  7627. ret = get_errno(do_sigaction(arg1, pact, &oact));
  7628. if (!is_error(ret) && arg3) {
  7629. if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
  7630. return -TARGET_EFAULT;
  7631. rt_act->_sa_handler = oact._sa_handler;
  7632. rt_act->sa_mask = oact.sa_mask;
  7633. rt_act->sa_flags = oact.sa_flags;
  7634. unlock_user_struct(rt_act, arg3, 1);
  7635. }
  7636. #else
  7637. #ifdef TARGET_SPARC
  7638. target_ulong restorer = arg4;
  7639. target_ulong sigsetsize = arg5;
  7640. #else
  7641. target_ulong sigsetsize = arg4;
  7642. #endif
  7643. struct target_sigaction *act;
  7644. struct target_sigaction *oact;
  7645. if (sigsetsize != sizeof(target_sigset_t)) {
  7646. return -TARGET_EINVAL;
  7647. }
  7648. if (arg2) {
  7649. if (!lock_user_struct(VERIFY_READ, act, arg2, 1)) {
  7650. return -TARGET_EFAULT;
  7651. }
  7652. #ifdef TARGET_ARCH_HAS_KA_RESTORER
  7653. act->ka_restorer = restorer;
  7654. #endif
  7655. } else {
  7656. act = NULL;
  7657. }
  7658. if (arg3) {
  7659. if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
  7660. ret = -TARGET_EFAULT;
  7661. goto rt_sigaction_fail;
  7662. }
  7663. } else
  7664. oact = NULL;
  7665. ret = get_errno(do_sigaction(arg1, act, oact));
  7666. rt_sigaction_fail:
  7667. if (act)
  7668. unlock_user_struct(act, arg2, 0);
  7669. if (oact)
  7670. unlock_user_struct(oact, arg3, 1);
  7671. #endif
  7672. }
  7673. return ret;
  7674. #ifdef TARGET_NR_sgetmask /* not on alpha */
  7675. case TARGET_NR_sgetmask:
  7676. {
  7677. sigset_t cur_set;
  7678. abi_ulong target_set;
  7679. ret = do_sigprocmask(0, NULL, &cur_set);
  7680. if (!ret) {
  7681. host_to_target_old_sigset(&target_set, &cur_set);
  7682. ret = target_set;
  7683. }
  7684. }
  7685. return ret;
  7686. #endif
  7687. #ifdef TARGET_NR_ssetmask /* not on alpha */
  7688. case TARGET_NR_ssetmask:
  7689. {
  7690. sigset_t set, oset;
  7691. abi_ulong target_set = arg1;
  7692. target_to_host_old_sigset(&set, &target_set);
  7693. ret = do_sigprocmask(SIG_SETMASK, &set, &oset);
  7694. if (!ret) {
  7695. host_to_target_old_sigset(&target_set, &oset);
  7696. ret = target_set;
  7697. }
  7698. }
  7699. return ret;
  7700. #endif
  7701. #ifdef TARGET_NR_sigprocmask
  7702. case TARGET_NR_sigprocmask:
  7703. {
  7704. #if defined(TARGET_ALPHA)
  7705. sigset_t set, oldset;
  7706. abi_ulong mask;
  7707. int how;
  7708. switch (arg1) {
  7709. case TARGET_SIG_BLOCK:
  7710. how = SIG_BLOCK;
  7711. break;
  7712. case TARGET_SIG_UNBLOCK:
  7713. how = SIG_UNBLOCK;
  7714. break;
  7715. case TARGET_SIG_SETMASK:
  7716. how = SIG_SETMASK;
  7717. break;
  7718. default:
  7719. return -TARGET_EINVAL;
  7720. }
  7721. mask = arg2;
  7722. target_to_host_old_sigset(&set, &mask);
  7723. ret = do_sigprocmask(how, &set, &oldset);
  7724. if (!is_error(ret)) {
  7725. host_to_target_old_sigset(&mask, &oldset);
  7726. ret = mask;
  7727. ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0; /* force no error */
  7728. }
  7729. #else
  7730. sigset_t set, oldset, *set_ptr;
  7731. int how;
  7732. if (arg2) {
  7733. switch (arg1) {
  7734. case TARGET_SIG_BLOCK:
  7735. how = SIG_BLOCK;
  7736. break;
  7737. case TARGET_SIG_UNBLOCK:
  7738. how = SIG_UNBLOCK;
  7739. break;
  7740. case TARGET_SIG_SETMASK:
  7741. how = SIG_SETMASK;
  7742. break;
  7743. default:
  7744. return -TARGET_EINVAL;
  7745. }
  7746. if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
  7747. return -TARGET_EFAULT;
  7748. target_to_host_old_sigset(&set, p);
  7749. unlock_user(p, arg2, 0);
  7750. set_ptr = &set;
  7751. } else {
  7752. how = 0;
  7753. set_ptr = NULL;
  7754. }
  7755. ret = do_sigprocmask(how, set_ptr, &oldset);
  7756. if (!is_error(ret) && arg3) {
  7757. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
  7758. return -TARGET_EFAULT;
  7759. host_to_target_old_sigset(p, &oldset);
  7760. unlock_user(p, arg3, sizeof(target_sigset_t));
  7761. }
  7762. #endif
  7763. }
  7764. return ret;
  7765. #endif
  7766. case TARGET_NR_rt_sigprocmask:
  7767. {
  7768. int how = arg1;
  7769. sigset_t set, oldset, *set_ptr;
  7770. if (arg4 != sizeof(target_sigset_t)) {
  7771. return -TARGET_EINVAL;
  7772. }
  7773. if (arg2) {
  7774. switch(how) {
  7775. case TARGET_SIG_BLOCK:
  7776. how = SIG_BLOCK;
  7777. break;
  7778. case TARGET_SIG_UNBLOCK:
  7779. how = SIG_UNBLOCK;
  7780. break;
  7781. case TARGET_SIG_SETMASK:
  7782. how = SIG_SETMASK;
  7783. break;
  7784. default:
  7785. return -TARGET_EINVAL;
  7786. }
  7787. if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
  7788. return -TARGET_EFAULT;
  7789. target_to_host_sigset(&set, p);
  7790. unlock_user(p, arg2, 0);
  7791. set_ptr = &set;
  7792. } else {
  7793. how = 0;
  7794. set_ptr = NULL;
  7795. }
  7796. ret = do_sigprocmask(how, set_ptr, &oldset);
  7797. if (!is_error(ret) && arg3) {
  7798. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
  7799. return -TARGET_EFAULT;
  7800. host_to_target_sigset(p, &oldset);
  7801. unlock_user(p, arg3, sizeof(target_sigset_t));
  7802. }
  7803. }
  7804. return ret;
  7805. #ifdef TARGET_NR_sigpending
  7806. case TARGET_NR_sigpending:
  7807. {
  7808. sigset_t set;
  7809. ret = get_errno(sigpending(&set));
  7810. if (!is_error(ret)) {
  7811. if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
  7812. return -TARGET_EFAULT;
  7813. host_to_target_old_sigset(p, &set);
  7814. unlock_user(p, arg1, sizeof(target_sigset_t));
  7815. }
  7816. }
  7817. return ret;
  7818. #endif
  7819. case TARGET_NR_rt_sigpending:
  7820. {
  7821. sigset_t set;
  7822. /* Yes, this check is >, not != like most. We follow the kernel's
  7823. * logic and it does it like this because it implements
  7824. * NR_sigpending through the same code path, and in that case
  7825. * the old_sigset_t is smaller in size.
  7826. */
  7827. if (arg2 > sizeof(target_sigset_t)) {
  7828. return -TARGET_EINVAL;
  7829. }
  7830. ret = get_errno(sigpending(&set));
  7831. if (!is_error(ret)) {
  7832. if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
  7833. return -TARGET_EFAULT;
  7834. host_to_target_sigset(p, &set);
  7835. unlock_user(p, arg1, sizeof(target_sigset_t));
  7836. }
  7837. }
  7838. return ret;
  7839. #ifdef TARGET_NR_sigsuspend
  7840. case TARGET_NR_sigsuspend:
  7841. {
  7842. TaskState *ts = cpu->opaque;
  7843. #if defined(TARGET_ALPHA)
  7844. abi_ulong mask = arg1;
  7845. target_to_host_old_sigset(&ts->sigsuspend_mask, &mask);
  7846. #else
  7847. if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
  7848. return -TARGET_EFAULT;
  7849. target_to_host_old_sigset(&ts->sigsuspend_mask, p);
  7850. unlock_user(p, arg1, 0);
  7851. #endif
  7852. ret = get_errno(safe_rt_sigsuspend(&ts->sigsuspend_mask,
  7853. SIGSET_T_SIZE));
  7854. if (ret != -TARGET_ERESTARTSYS) {
  7855. ts->in_sigsuspend = 1;
  7856. }
  7857. }
  7858. return ret;
  7859. #endif
  7860. case TARGET_NR_rt_sigsuspend:
  7861. {
  7862. TaskState *ts = cpu->opaque;
  7863. if (arg2 != sizeof(target_sigset_t)) {
  7864. return -TARGET_EINVAL;
  7865. }
  7866. if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
  7867. return -TARGET_EFAULT;
  7868. target_to_host_sigset(&ts->sigsuspend_mask, p);
  7869. unlock_user(p, arg1, 0);
  7870. ret = get_errno(safe_rt_sigsuspend(&ts->sigsuspend_mask,
  7871. SIGSET_T_SIZE));
  7872. if (ret != -TARGET_ERESTARTSYS) {
  7873. ts->in_sigsuspend = 1;
  7874. }
  7875. }
  7876. return ret;
  7877. #ifdef TARGET_NR_rt_sigtimedwait
  7878. case TARGET_NR_rt_sigtimedwait:
  7879. {
  7880. sigset_t set;
  7881. struct timespec uts, *puts;
  7882. siginfo_t uinfo;
  7883. if (arg4 != sizeof(target_sigset_t)) {
  7884. return -TARGET_EINVAL;
  7885. }
  7886. if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
  7887. return -TARGET_EFAULT;
  7888. target_to_host_sigset(&set, p);
  7889. unlock_user(p, arg1, 0);
  7890. if (arg3) {
  7891. puts = &uts;
  7892. target_to_host_timespec(puts, arg3);
  7893. } else {
  7894. puts = NULL;
  7895. }
  7896. ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts,
  7897. SIGSET_T_SIZE));
  7898. if (!is_error(ret)) {
  7899. if (arg2) {
  7900. p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t),
  7901. 0);
  7902. if (!p) {
  7903. return -TARGET_EFAULT;
  7904. }
  7905. host_to_target_siginfo(p, &uinfo);
  7906. unlock_user(p, arg2, sizeof(target_siginfo_t));
  7907. }
  7908. ret = host_to_target_signal(ret);
  7909. }
  7910. }
  7911. return ret;
  7912. #endif
  7913. case TARGET_NR_rt_sigqueueinfo:
  7914. {
  7915. siginfo_t uinfo;
  7916. p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1);
  7917. if (!p) {
  7918. return -TARGET_EFAULT;
  7919. }
  7920. target_to_host_siginfo(&uinfo, p);
  7921. unlock_user(p, arg3, 0);
  7922. ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
  7923. }
  7924. return ret;
  7925. case TARGET_NR_rt_tgsigqueueinfo:
  7926. {
  7927. siginfo_t uinfo;
  7928. p = lock_user(VERIFY_READ, arg4, sizeof(target_siginfo_t), 1);
  7929. if (!p) {
  7930. return -TARGET_EFAULT;
  7931. }
  7932. target_to_host_siginfo(&uinfo, p);
  7933. unlock_user(p, arg4, 0);
  7934. ret = get_errno(sys_rt_tgsigqueueinfo(arg1, arg2, arg3, &uinfo));
  7935. }
  7936. return ret;
  7937. #ifdef TARGET_NR_sigreturn
  7938. case TARGET_NR_sigreturn:
  7939. if (block_signals()) {
  7940. return -TARGET_ERESTARTSYS;
  7941. }
  7942. return do_sigreturn(cpu_env);
  7943. #endif
  7944. case TARGET_NR_rt_sigreturn:
  7945. if (block_signals()) {
  7946. return -TARGET_ERESTARTSYS;
  7947. }
  7948. return do_rt_sigreturn(cpu_env);
  7949. case TARGET_NR_sethostname:
  7950. if (!(p = lock_user_string(arg1)))
  7951. return -TARGET_EFAULT;
  7952. ret = get_errno(sethostname(p, arg2));
  7953. unlock_user(p, arg1, 0);
  7954. return ret;
  7955. #ifdef TARGET_NR_setrlimit
  7956. case TARGET_NR_setrlimit:
  7957. {
  7958. int resource = target_to_host_resource(arg1);
  7959. struct target_rlimit *target_rlim;
  7960. struct rlimit rlim;
  7961. if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
  7962. return -TARGET_EFAULT;
  7963. rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
  7964. rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
  7965. unlock_user_struct(target_rlim, arg2, 0);
  7966. /*
  7967. * If we just passed through resource limit settings for memory then
  7968. * they would also apply to QEMU's own allocations, and QEMU will
  7969. * crash or hang or die if its allocations fail. Ideally we would
  7970. * track the guest allocations in QEMU and apply the limits ourselves.
  7971. * For now, just tell the guest the call succeeded but don't actually
  7972. * limit anything.
  7973. */
  7974. if (resource != RLIMIT_AS &&
  7975. resource != RLIMIT_DATA &&
  7976. resource != RLIMIT_STACK) {
  7977. return get_errno(setrlimit(resource, &rlim));
  7978. } else {
  7979. return 0;
  7980. }
  7981. }
  7982. #endif
  7983. #ifdef TARGET_NR_getrlimit
  7984. case TARGET_NR_getrlimit:
  7985. {
  7986. int resource = target_to_host_resource(arg1);
  7987. struct target_rlimit *target_rlim;
  7988. struct rlimit rlim;
  7989. ret = get_errno(getrlimit(resource, &rlim));
  7990. if (!is_error(ret)) {
  7991. if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
  7992. return -TARGET_EFAULT;
  7993. target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
  7994. target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
  7995. unlock_user_struct(target_rlim, arg2, 1);
  7996. }
  7997. }
  7998. return ret;
  7999. #endif
  8000. case TARGET_NR_getrusage:
  8001. {
  8002. struct rusage rusage;
  8003. ret = get_errno(getrusage(arg1, &rusage));
  8004. if (!is_error(ret)) {
  8005. ret = host_to_target_rusage(arg2, &rusage);
  8006. }
  8007. }
  8008. return ret;
  8009. #if defined(TARGET_NR_gettimeofday)
  8010. case TARGET_NR_gettimeofday:
  8011. {
  8012. struct timeval tv;
  8013. struct timezone tz;
  8014. ret = get_errno(gettimeofday(&tv, &tz));
  8015. if (!is_error(ret)) {
  8016. if (arg1 && copy_to_user_timeval(arg1, &tv)) {
  8017. return -TARGET_EFAULT;
  8018. }
  8019. if (arg2 && copy_to_user_timezone(arg2, &tz)) {
  8020. return -TARGET_EFAULT;
  8021. }
  8022. }
  8023. }
  8024. return ret;
  8025. #endif
  8026. #if defined(TARGET_NR_settimeofday)
  8027. case TARGET_NR_settimeofday:
  8028. {
  8029. struct timeval tv, *ptv = NULL;
  8030. struct timezone tz, *ptz = NULL;
  8031. if (arg1) {
  8032. if (copy_from_user_timeval(&tv, arg1)) {
  8033. return -TARGET_EFAULT;
  8034. }
  8035. ptv = &tv;
  8036. }
  8037. if (arg2) {
  8038. if (copy_from_user_timezone(&tz, arg2)) {
  8039. return -TARGET_EFAULT;
  8040. }
  8041. ptz = &tz;
  8042. }
  8043. return get_errno(settimeofday(ptv, ptz));
  8044. }
  8045. #endif
  8046. #if defined(TARGET_NR_select)
  8047. case TARGET_NR_select:
  8048. #if defined(TARGET_WANT_NI_OLD_SELECT)
  8049. /* some architectures used to have old_select here
  8050. * but now ENOSYS it.
  8051. */
  8052. ret = -TARGET_ENOSYS;
  8053. #elif defined(TARGET_WANT_OLD_SYS_SELECT)
  8054. ret = do_old_select(arg1);
  8055. #else
  8056. ret = do_select(arg1, arg2, arg3, arg4, arg5);
  8057. #endif
  8058. return ret;
  8059. #endif
  8060. #ifdef TARGET_NR_pselect6
  8061. case TARGET_NR_pselect6:
  8062. {
  8063. abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
  8064. fd_set rfds, wfds, efds;
  8065. fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
  8066. struct timespec ts, *ts_ptr;
  8067. /*
  8068. * The 6th arg is actually two args smashed together,
  8069. * so we cannot use the C library.
  8070. */
  8071. sigset_t set;
  8072. struct {
  8073. sigset_t *set;
  8074. size_t size;
  8075. } sig, *sig_ptr;
  8076. abi_ulong arg_sigset, arg_sigsize, *arg7;
  8077. target_sigset_t *target_sigset;
  8078. n = arg1;
  8079. rfd_addr = arg2;
  8080. wfd_addr = arg3;
  8081. efd_addr = arg4;
  8082. ts_addr = arg5;
  8083. ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
  8084. if (ret) {
  8085. return ret;
  8086. }
  8087. ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
  8088. if (ret) {
  8089. return ret;
  8090. }
  8091. ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
  8092. if (ret) {
  8093. return ret;
  8094. }
  8095. /*
  8096. * This takes a timespec, and not a timeval, so we cannot
  8097. * use the do_select() helper ...
  8098. */
  8099. if (ts_addr) {
  8100. if (target_to_host_timespec(&ts, ts_addr)) {
  8101. return -TARGET_EFAULT;
  8102. }
  8103. ts_ptr = &ts;
  8104. } else {
  8105. ts_ptr = NULL;
  8106. }
  8107. /* Extract the two packed args for the sigset */
  8108. if (arg6) {
  8109. sig_ptr = &sig;
  8110. sig.size = SIGSET_T_SIZE;
  8111. arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
  8112. if (!arg7) {
  8113. return -TARGET_EFAULT;
  8114. }
  8115. arg_sigset = tswapal(arg7[0]);
  8116. arg_sigsize = tswapal(arg7[1]);
  8117. unlock_user(arg7, arg6, 0);
  8118. if (arg_sigset) {
  8119. sig.set = &set;
  8120. if (arg_sigsize != sizeof(*target_sigset)) {
  8121. /* Like the kernel, we enforce correct size sigsets */
  8122. return -TARGET_EINVAL;
  8123. }
  8124. target_sigset = lock_user(VERIFY_READ, arg_sigset,
  8125. sizeof(*target_sigset), 1);
  8126. if (!target_sigset) {
  8127. return -TARGET_EFAULT;
  8128. }
  8129. target_to_host_sigset(&set, target_sigset);
  8130. unlock_user(target_sigset, arg_sigset, 0);
  8131. } else {
  8132. sig.set = NULL;
  8133. }
  8134. } else {
  8135. sig_ptr = NULL;
  8136. }
  8137. ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
  8138. ts_ptr, sig_ptr));
  8139. if (!is_error(ret)) {
  8140. if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
  8141. return -TARGET_EFAULT;
  8142. if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
  8143. return -TARGET_EFAULT;
  8144. if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
  8145. return -TARGET_EFAULT;
  8146. if (ts_addr && host_to_target_timespec(ts_addr, &ts))
  8147. return -TARGET_EFAULT;
  8148. }
  8149. }
  8150. return ret;
  8151. #endif
  8152. #ifdef TARGET_NR_symlink
  8153. case TARGET_NR_symlink:
  8154. {
  8155. void *p2;
  8156. p = lock_user_string(arg1);
  8157. p2 = lock_user_string(arg2);
  8158. if (!p || !p2)
  8159. ret = -TARGET_EFAULT;
  8160. else
  8161. ret = get_errno(symlink(p, p2));
  8162. unlock_user(p2, arg2, 0);
  8163. unlock_user(p, arg1, 0);
  8164. }
  8165. return ret;
  8166. #endif
  8167. #if defined(TARGET_NR_symlinkat)
  8168. case TARGET_NR_symlinkat:
  8169. {
  8170. void *p2;
  8171. p = lock_user_string(arg1);
  8172. p2 = lock_user_string(arg3);
  8173. if (!p || !p2)
  8174. ret = -TARGET_EFAULT;
  8175. else
  8176. ret = get_errno(symlinkat(p, arg2, p2));
  8177. unlock_user(p2, arg3, 0);
  8178. unlock_user(p, arg1, 0);
  8179. }
  8180. return ret;
  8181. #endif
  8182. #ifdef TARGET_NR_readlink
  8183. case TARGET_NR_readlink:
  8184. {
  8185. void *p2;
  8186. p = lock_user_string(arg1);
  8187. p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  8188. if (!p || !p2) {
  8189. ret = -TARGET_EFAULT;
  8190. } else if (!arg3) {
  8191. /* Short circuit this for the magic exe check. */
  8192. ret = -TARGET_EINVAL;
  8193. } else if (is_proc_myself((const char *)p, "exe")) {
  8194. char real[PATH_MAX], *temp;
  8195. temp = realpath(exec_path, real);
  8196. /* Return value is # of bytes that we wrote to the buffer. */
  8197. if (temp == NULL) {
  8198. ret = get_errno(-1);
  8199. } else {
  8200. /* Don't worry about sign mismatch as earlier mapping
  8201. * logic would have thrown a bad address error. */
  8202. ret = MIN(strlen(real), arg3);
  8203. /* We cannot NUL terminate the string. */
  8204. memcpy(p2, real, ret);
  8205. }
  8206. } else {
  8207. ret = get_errno(readlink(path(p), p2, arg3));
  8208. }
  8209. unlock_user(p2, arg2, ret);
  8210. unlock_user(p, arg1, 0);
  8211. }
  8212. return ret;
  8213. #endif
  8214. #if defined(TARGET_NR_readlinkat)
  8215. case TARGET_NR_readlinkat:
  8216. {
  8217. void *p2;
  8218. p = lock_user_string(arg2);
  8219. p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  8220. if (!p || !p2) {
  8221. ret = -TARGET_EFAULT;
  8222. } else if (is_proc_myself((const char *)p, "exe")) {
  8223. char real[PATH_MAX], *temp;
  8224. temp = realpath(exec_path, real);
  8225. ret = temp == NULL ? get_errno(-1) : strlen(real) ;
  8226. snprintf((char *)p2, arg4, "%s", real);
  8227. } else {
  8228. ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
  8229. }
  8230. unlock_user(p2, arg3, ret);
  8231. unlock_user(p, arg2, 0);
  8232. }
  8233. return ret;
  8234. #endif
  8235. #ifdef TARGET_NR_swapon
  8236. case TARGET_NR_swapon:
  8237. if (!(p = lock_user_string(arg1)))
  8238. return -TARGET_EFAULT;
  8239. ret = get_errno(swapon(p, arg2));
  8240. unlock_user(p, arg1, 0);
  8241. return ret;
  8242. #endif
  8243. case TARGET_NR_reboot:
  8244. if (arg3 == LINUX_REBOOT_CMD_RESTART2) {
  8245. /* arg4 must be ignored in all other cases */
  8246. p = lock_user_string(arg4);
  8247. if (!p) {
  8248. return -TARGET_EFAULT;
  8249. }
  8250. ret = get_errno(reboot(arg1, arg2, arg3, p));
  8251. unlock_user(p, arg4, 0);
  8252. } else {
  8253. ret = get_errno(reboot(arg1, arg2, arg3, NULL));
  8254. }
  8255. return ret;
  8256. #ifdef TARGET_NR_mmap
  8257. case TARGET_NR_mmap:
  8258. #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
  8259. (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
  8260. defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
  8261. || defined(TARGET_S390X)
  8262. {
  8263. abi_ulong *v;
  8264. abi_ulong v1, v2, v3, v4, v5, v6;
  8265. if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
  8266. return -TARGET_EFAULT;
  8267. v1 = tswapal(v[0]);
  8268. v2 = tswapal(v[1]);
  8269. v3 = tswapal(v[2]);
  8270. v4 = tswapal(v[3]);
  8271. v5 = tswapal(v[4]);
  8272. v6 = tswapal(v[5]);
  8273. unlock_user(v, arg1, 0);
  8274. ret = get_errno(target_mmap(v1, v2, v3,
  8275. target_to_host_bitmask(v4, mmap_flags_tbl),
  8276. v5, v6));
  8277. }
  8278. #else
  8279. ret = get_errno(target_mmap(arg1, arg2, arg3,
  8280. target_to_host_bitmask(arg4, mmap_flags_tbl),
  8281. arg5,
  8282. arg6));
  8283. #endif
  8284. return ret;
  8285. #endif
  8286. #ifdef TARGET_NR_mmap2
  8287. case TARGET_NR_mmap2:
  8288. #ifndef MMAP_SHIFT
  8289. #define MMAP_SHIFT 12
  8290. #endif
  8291. ret = target_mmap(arg1, arg2, arg3,
  8292. target_to_host_bitmask(arg4, mmap_flags_tbl),
  8293. arg5, arg6 << MMAP_SHIFT);
  8294. return get_errno(ret);
  8295. #endif
  8296. case TARGET_NR_munmap:
  8297. return get_errno(target_munmap(arg1, arg2));
  8298. case TARGET_NR_mprotect:
  8299. {
  8300. TaskState *ts = cpu->opaque;
  8301. /* Special hack to detect libc making the stack executable. */
  8302. if ((arg3 & PROT_GROWSDOWN)
  8303. && arg1 >= ts->info->stack_limit
  8304. && arg1 <= ts->info->start_stack) {
  8305. arg3 &= ~PROT_GROWSDOWN;
  8306. arg2 = arg2 + arg1 - ts->info->stack_limit;
  8307. arg1 = ts->info->stack_limit;
  8308. }
  8309. }
  8310. return get_errno(target_mprotect(arg1, arg2, arg3));
  8311. #ifdef TARGET_NR_mremap
  8312. case TARGET_NR_mremap:
  8313. return get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
  8314. #endif
  8315. /* ??? msync/mlock/munlock are broken for softmmu. */
  8316. #ifdef TARGET_NR_msync
  8317. case TARGET_NR_msync:
  8318. return get_errno(msync(g2h(arg1), arg2, arg3));
  8319. #endif
  8320. #ifdef TARGET_NR_mlock
  8321. case TARGET_NR_mlock:
  8322. return get_errno(mlock(g2h(arg1), arg2));
  8323. #endif
  8324. #ifdef TARGET_NR_munlock
  8325. case TARGET_NR_munlock:
  8326. return get_errno(munlock(g2h(arg1), arg2));
  8327. #endif
  8328. #ifdef TARGET_NR_mlockall
  8329. case TARGET_NR_mlockall:
  8330. return get_errno(mlockall(target_to_host_mlockall_arg(arg1)));
  8331. #endif
  8332. #ifdef TARGET_NR_munlockall
  8333. case TARGET_NR_munlockall:
  8334. return get_errno(munlockall());
  8335. #endif
  8336. #ifdef TARGET_NR_truncate
  8337. case TARGET_NR_truncate:
  8338. if (!(p = lock_user_string(arg1)))
  8339. return -TARGET_EFAULT;
  8340. ret = get_errno(truncate(p, arg2));
  8341. unlock_user(p, arg1, 0);
  8342. return ret;
  8343. #endif
  8344. #ifdef TARGET_NR_ftruncate
  8345. case TARGET_NR_ftruncate:
  8346. return get_errno(ftruncate(arg1, arg2));
  8347. #endif
  8348. case TARGET_NR_fchmod:
  8349. return get_errno(fchmod(arg1, arg2));
  8350. #if defined(TARGET_NR_fchmodat)
  8351. case TARGET_NR_fchmodat:
  8352. if (!(p = lock_user_string(arg2)))
  8353. return -TARGET_EFAULT;
  8354. ret = get_errno(fchmodat(arg1, p, arg3, 0));
  8355. unlock_user(p, arg2, 0);
  8356. return ret;
  8357. #endif
  8358. case TARGET_NR_getpriority:
  8359. /* Note that negative values are valid for getpriority, so we must
  8360. differentiate based on errno settings. */
  8361. errno = 0;
  8362. ret = getpriority(arg1, arg2);
  8363. if (ret == -1 && errno != 0) {
  8364. return -host_to_target_errno(errno);
  8365. }
  8366. #ifdef TARGET_ALPHA
  8367. /* Return value is the unbiased priority. Signal no error. */
  8368. ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0;
  8369. #else
  8370. /* Return value is a biased priority to avoid negative numbers. */
  8371. ret = 20 - ret;
  8372. #endif
  8373. return ret;
  8374. case TARGET_NR_setpriority:
  8375. return get_errno(setpriority(arg1, arg2, arg3));
  8376. #ifdef TARGET_NR_statfs
  8377. case TARGET_NR_statfs:
  8378. if (!(p = lock_user_string(arg1))) {
  8379. return -TARGET_EFAULT;
  8380. }
  8381. ret = get_errno(statfs(path(p), &stfs));
  8382. unlock_user(p, arg1, 0);
  8383. convert_statfs:
  8384. if (!is_error(ret)) {
  8385. struct target_statfs *target_stfs;
  8386. if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
  8387. return -TARGET_EFAULT;
  8388. __put_user(stfs.f_type, &target_stfs->f_type);
  8389. __put_user(stfs.f_bsize, &target_stfs->f_bsize);
  8390. __put_user(stfs.f_blocks, &target_stfs->f_blocks);
  8391. __put_user(stfs.f_bfree, &target_stfs->f_bfree);
  8392. __put_user(stfs.f_bavail, &target_stfs->f_bavail);
  8393. __put_user(stfs.f_files, &target_stfs->f_files);
  8394. __put_user(stfs.f_ffree, &target_stfs->f_ffree);
  8395. __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
  8396. __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
  8397. __put_user(stfs.f_namelen, &target_stfs->f_namelen);
  8398. __put_user(stfs.f_frsize, &target_stfs->f_frsize);
  8399. #ifdef _STATFS_F_FLAGS
  8400. __put_user(stfs.f_flags, &target_stfs->f_flags);
  8401. #else
  8402. __put_user(0, &target_stfs->f_flags);
  8403. #endif
  8404. memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
  8405. unlock_user_struct(target_stfs, arg2, 1);
  8406. }
  8407. return ret;
  8408. #endif
  8409. #ifdef TARGET_NR_fstatfs
  8410. case TARGET_NR_fstatfs:
  8411. ret = get_errno(fstatfs(arg1, &stfs));
  8412. goto convert_statfs;
  8413. #endif
  8414. #ifdef TARGET_NR_statfs64
  8415. case TARGET_NR_statfs64:
  8416. if (!(p = lock_user_string(arg1))) {
  8417. return -TARGET_EFAULT;
  8418. }
  8419. ret = get_errno(statfs(path(p), &stfs));
  8420. unlock_user(p, arg1, 0);
  8421. convert_statfs64:
  8422. if (!is_error(ret)) {
  8423. struct target_statfs64 *target_stfs;
  8424. if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
  8425. return -TARGET_EFAULT;
  8426. __put_user(stfs.f_type, &target_stfs->f_type);
  8427. __put_user(stfs.f_bsize, &target_stfs->f_bsize);
  8428. __put_user(stfs.f_blocks, &target_stfs->f_blocks);
  8429. __put_user(stfs.f_bfree, &target_stfs->f_bfree);
  8430. __put_user(stfs.f_bavail, &target_stfs->f_bavail);
  8431. __put_user(stfs.f_files, &target_stfs->f_files);
  8432. __put_user(stfs.f_ffree, &target_stfs->f_ffree);
  8433. __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
  8434. __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
  8435. __put_user(stfs.f_namelen, &target_stfs->f_namelen);
  8436. __put_user(stfs.f_frsize, &target_stfs->f_frsize);
  8437. memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
  8438. unlock_user_struct(target_stfs, arg3, 1);
  8439. }
  8440. return ret;
  8441. case TARGET_NR_fstatfs64:
  8442. ret = get_errno(fstatfs(arg1, &stfs));
  8443. goto convert_statfs64;
  8444. #endif
  8445. #ifdef TARGET_NR_socketcall
  8446. case TARGET_NR_socketcall:
  8447. return do_socketcall(arg1, arg2);
  8448. #endif
  8449. #ifdef TARGET_NR_accept
  8450. case TARGET_NR_accept:
  8451. return do_accept4(arg1, arg2, arg3, 0);
  8452. #endif
  8453. #ifdef TARGET_NR_accept4
  8454. case TARGET_NR_accept4:
  8455. return do_accept4(arg1, arg2, arg3, arg4);
  8456. #endif
  8457. #ifdef TARGET_NR_bind
  8458. case TARGET_NR_bind:
  8459. return do_bind(arg1, arg2, arg3);
  8460. #endif
  8461. #ifdef TARGET_NR_connect
  8462. case TARGET_NR_connect:
  8463. return do_connect(arg1, arg2, arg3);
  8464. #endif
  8465. #ifdef TARGET_NR_getpeername
  8466. case TARGET_NR_getpeername:
  8467. return do_getpeername(arg1, arg2, arg3);
  8468. #endif
  8469. #ifdef TARGET_NR_getsockname
  8470. case TARGET_NR_getsockname:
  8471. return do_getsockname(arg1, arg2, arg3);
  8472. #endif
  8473. #ifdef TARGET_NR_getsockopt
  8474. case TARGET_NR_getsockopt:
  8475. return do_getsockopt(arg1, arg2, arg3, arg4, arg5);
  8476. #endif
  8477. #ifdef TARGET_NR_listen
  8478. case TARGET_NR_listen:
  8479. return get_errno(listen(arg1, arg2));
  8480. #endif
  8481. #ifdef TARGET_NR_recv
  8482. case TARGET_NR_recv:
  8483. return do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
  8484. #endif
  8485. #ifdef TARGET_NR_recvfrom
  8486. case TARGET_NR_recvfrom:
  8487. return do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
  8488. #endif
  8489. #ifdef TARGET_NR_recvmsg
  8490. case TARGET_NR_recvmsg:
  8491. return do_sendrecvmsg(arg1, arg2, arg3, 0);
  8492. #endif
  8493. #ifdef TARGET_NR_send
  8494. case TARGET_NR_send:
  8495. return do_sendto(arg1, arg2, arg3, arg4, 0, 0);
  8496. #endif
  8497. #ifdef TARGET_NR_sendmsg
  8498. case TARGET_NR_sendmsg:
  8499. return do_sendrecvmsg(arg1, arg2, arg3, 1);
  8500. #endif
  8501. #ifdef TARGET_NR_sendmmsg
  8502. case TARGET_NR_sendmmsg:
  8503. return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 1);
  8504. #endif
  8505. #ifdef TARGET_NR_recvmmsg
  8506. case TARGET_NR_recvmmsg:
  8507. return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 0);
  8508. #endif
  8509. #ifdef TARGET_NR_sendto
  8510. case TARGET_NR_sendto:
  8511. return do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
  8512. #endif
  8513. #ifdef TARGET_NR_shutdown
  8514. case TARGET_NR_shutdown:
  8515. return get_errno(shutdown(arg1, arg2));
  8516. #endif
  8517. #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
  8518. case TARGET_NR_getrandom:
  8519. p = lock_user(VERIFY_WRITE, arg1, arg2, 0);
  8520. if (!p) {
  8521. return -TARGET_EFAULT;
  8522. }
  8523. ret = get_errno(getrandom(p, arg2, arg3));
  8524. unlock_user(p, arg1, ret);
  8525. return ret;
  8526. #endif
  8527. #ifdef TARGET_NR_socket
  8528. case TARGET_NR_socket:
  8529. return do_socket(arg1, arg2, arg3);
  8530. #endif
  8531. #ifdef TARGET_NR_socketpair
  8532. case TARGET_NR_socketpair:
  8533. return do_socketpair(arg1, arg2, arg3, arg4);
  8534. #endif
  8535. #ifdef TARGET_NR_setsockopt
  8536. case TARGET_NR_setsockopt:
  8537. return do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
  8538. #endif
  8539. #if defined(TARGET_NR_syslog)
  8540. case TARGET_NR_syslog:
  8541. {
  8542. int len = arg2;
  8543. switch (arg1) {
  8544. case TARGET_SYSLOG_ACTION_CLOSE: /* Close log */
  8545. case TARGET_SYSLOG_ACTION_OPEN: /* Open log */
  8546. case TARGET_SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
  8547. case TARGET_SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging */
  8548. case TARGET_SYSLOG_ACTION_CONSOLE_ON: /* Enable logging */
  8549. case TARGET_SYSLOG_ACTION_CONSOLE_LEVEL: /* Set messages level */
  8550. case TARGET_SYSLOG_ACTION_SIZE_UNREAD: /* Number of chars */
  8551. case TARGET_SYSLOG_ACTION_SIZE_BUFFER: /* Size of the buffer */
  8552. return get_errno(sys_syslog((int)arg1, NULL, (int)arg3));
  8553. case TARGET_SYSLOG_ACTION_READ: /* Read from log */
  8554. case TARGET_SYSLOG_ACTION_READ_CLEAR: /* Read/clear msgs */
  8555. case TARGET_SYSLOG_ACTION_READ_ALL: /* Read last messages */
  8556. {
  8557. if (len < 0) {
  8558. return -TARGET_EINVAL;
  8559. }
  8560. if (len == 0) {
  8561. return 0;
  8562. }
  8563. p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  8564. if (!p) {
  8565. return -TARGET_EFAULT;
  8566. }
  8567. ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
  8568. unlock_user(p, arg2, arg3);
  8569. }
  8570. return ret;
  8571. default:
  8572. return -TARGET_EINVAL;
  8573. }
  8574. }
  8575. break;
  8576. #endif
  8577. case TARGET_NR_setitimer:
  8578. {
  8579. struct itimerval value, ovalue, *pvalue;
  8580. if (arg2) {
  8581. pvalue = &value;
  8582. if (copy_from_user_timeval(&pvalue->it_interval, arg2)
  8583. || copy_from_user_timeval(&pvalue->it_value,
  8584. arg2 + sizeof(struct target_timeval)))
  8585. return -TARGET_EFAULT;
  8586. } else {
  8587. pvalue = NULL;
  8588. }
  8589. ret = get_errno(setitimer(arg1, pvalue, &ovalue));
  8590. if (!is_error(ret) && arg3) {
  8591. if (copy_to_user_timeval(arg3,
  8592. &ovalue.it_interval)
  8593. || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
  8594. &ovalue.it_value))
  8595. return -TARGET_EFAULT;
  8596. }
  8597. }
  8598. return ret;
  8599. case TARGET_NR_getitimer:
  8600. {
  8601. struct itimerval value;
  8602. ret = get_errno(getitimer(arg1, &value));
  8603. if (!is_error(ret) && arg2) {
  8604. if (copy_to_user_timeval(arg2,
  8605. &value.it_interval)
  8606. || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
  8607. &value.it_value))
  8608. return -TARGET_EFAULT;
  8609. }
  8610. }
  8611. return ret;
  8612. #ifdef TARGET_NR_stat
  8613. case TARGET_NR_stat:
  8614. if (!(p = lock_user_string(arg1))) {
  8615. return -TARGET_EFAULT;
  8616. }
  8617. ret = get_errno(stat(path(p), &st));
  8618. unlock_user(p, arg1, 0);
  8619. goto do_stat;
  8620. #endif
  8621. #ifdef TARGET_NR_lstat
  8622. case TARGET_NR_lstat:
  8623. if (!(p = lock_user_string(arg1))) {
  8624. return -TARGET_EFAULT;
  8625. }
  8626. ret = get_errno(lstat(path(p), &st));
  8627. unlock_user(p, arg1, 0);
  8628. goto do_stat;
  8629. #endif
  8630. #ifdef TARGET_NR_fstat
  8631. case TARGET_NR_fstat:
  8632. {
  8633. ret = get_errno(fstat(arg1, &st));
  8634. #if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat)
  8635. do_stat:
  8636. #endif
  8637. if (!is_error(ret)) {
  8638. struct target_stat *target_st;
  8639. if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
  8640. return -TARGET_EFAULT;
  8641. memset(target_st, 0, sizeof(*target_st));
  8642. __put_user(st.st_dev, &target_st->st_dev);
  8643. __put_user(st.st_ino, &target_st->st_ino);
  8644. __put_user(st.st_mode, &target_st->st_mode);
  8645. __put_user(st.st_uid, &target_st->st_uid);
  8646. __put_user(st.st_gid, &target_st->st_gid);
  8647. __put_user(st.st_nlink, &target_st->st_nlink);
  8648. __put_user(st.st_rdev, &target_st->st_rdev);
  8649. __put_user(st.st_size, &target_st->st_size);
  8650. __put_user(st.st_blksize, &target_st->st_blksize);
  8651. __put_user(st.st_blocks, &target_st->st_blocks);
  8652. __put_user(st.st_atime, &target_st->target_st_atime);
  8653. __put_user(st.st_mtime, &target_st->target_st_mtime);
  8654. __put_user(st.st_ctime, &target_st->target_st_ctime);
  8655. #if (_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700) && \
  8656. defined(TARGET_STAT_HAVE_NSEC)
  8657. __put_user(st.st_atim.tv_nsec,
  8658. &target_st->target_st_atime_nsec);
  8659. __put_user(st.st_mtim.tv_nsec,
  8660. &target_st->target_st_mtime_nsec);
  8661. __put_user(st.st_ctim.tv_nsec,
  8662. &target_st->target_st_ctime_nsec);
  8663. #endif
  8664. unlock_user_struct(target_st, arg2, 1);
  8665. }
  8666. }
  8667. return ret;
  8668. #endif
  8669. case TARGET_NR_vhangup:
  8670. return get_errno(vhangup());
  8671. #ifdef TARGET_NR_syscall
  8672. case TARGET_NR_syscall:
  8673. return do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
  8674. arg6, arg7, arg8, 0);
  8675. #endif
  8676. #if defined(TARGET_NR_wait4)
  8677. case TARGET_NR_wait4:
  8678. {
  8679. int status;
  8680. abi_long status_ptr = arg2;
  8681. struct rusage rusage, *rusage_ptr;
  8682. abi_ulong target_rusage = arg4;
  8683. abi_long rusage_err;
  8684. if (target_rusage)
  8685. rusage_ptr = &rusage;
  8686. else
  8687. rusage_ptr = NULL;
  8688. ret = get_errno(safe_wait4(arg1, &status, arg3, rusage_ptr));
  8689. if (!is_error(ret)) {
  8690. if (status_ptr && ret) {
  8691. status = host_to_target_waitstatus(status);
  8692. if (put_user_s32(status, status_ptr))
  8693. return -TARGET_EFAULT;
  8694. }
  8695. if (target_rusage) {
  8696. rusage_err = host_to_target_rusage(target_rusage, &rusage);
  8697. if (rusage_err) {
  8698. ret = rusage_err;
  8699. }
  8700. }
  8701. }
  8702. }
  8703. return ret;
  8704. #endif
  8705. #ifdef TARGET_NR_swapoff
  8706. case TARGET_NR_swapoff:
  8707. if (!(p = lock_user_string(arg1)))
  8708. return -TARGET_EFAULT;
  8709. ret = get_errno(swapoff(p));
  8710. unlock_user(p, arg1, 0);
  8711. return ret;
  8712. #endif
  8713. case TARGET_NR_sysinfo:
  8714. {
  8715. struct target_sysinfo *target_value;
  8716. struct sysinfo value;
  8717. ret = get_errno(sysinfo(&value));
  8718. if (!is_error(ret) && arg1)
  8719. {
  8720. if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
  8721. return -TARGET_EFAULT;
  8722. __put_user(value.uptime, &target_value->uptime);
  8723. __put_user(value.loads[0], &target_value->loads[0]);
  8724. __put_user(value.loads[1], &target_value->loads[1]);
  8725. __put_user(value.loads[2], &target_value->loads[2]);
  8726. __put_user(value.totalram, &target_value->totalram);
  8727. __put_user(value.freeram, &target_value->freeram);
  8728. __put_user(value.sharedram, &target_value->sharedram);
  8729. __put_user(value.bufferram, &target_value->bufferram);
  8730. __put_user(value.totalswap, &target_value->totalswap);
  8731. __put_user(value.freeswap, &target_value->freeswap);
  8732. __put_user(value.procs, &target_value->procs);
  8733. __put_user(value.totalhigh, &target_value->totalhigh);
  8734. __put_user(value.freehigh, &target_value->freehigh);
  8735. __put_user(value.mem_unit, &target_value->mem_unit);
  8736. unlock_user_struct(target_value, arg1, 1);
  8737. }
  8738. }
  8739. return ret;
  8740. #ifdef TARGET_NR_ipc
  8741. case TARGET_NR_ipc:
  8742. return do_ipc(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6);
  8743. #endif
  8744. #ifdef TARGET_NR_semget
  8745. case TARGET_NR_semget:
  8746. return get_errno(semget(arg1, arg2, arg3));
  8747. #endif
  8748. #ifdef TARGET_NR_semop
  8749. case TARGET_NR_semop:
  8750. return do_semop(arg1, arg2, arg3);
  8751. #endif
  8752. #ifdef TARGET_NR_semctl
  8753. case TARGET_NR_semctl:
  8754. return do_semctl(arg1, arg2, arg3, arg4);
  8755. #endif
  8756. #ifdef TARGET_NR_msgctl
  8757. case TARGET_NR_msgctl:
  8758. return do_msgctl(arg1, arg2, arg3);
  8759. #endif
  8760. #ifdef TARGET_NR_msgget
  8761. case TARGET_NR_msgget:
  8762. return get_errno(msgget(arg1, arg2));
  8763. #endif
  8764. #ifdef TARGET_NR_msgrcv
  8765. case TARGET_NR_msgrcv:
  8766. return do_msgrcv(arg1, arg2, arg3, arg4, arg5);
  8767. #endif
  8768. #ifdef TARGET_NR_msgsnd
  8769. case TARGET_NR_msgsnd:
  8770. return do_msgsnd(arg1, arg2, arg3, arg4);
  8771. #endif
  8772. #ifdef TARGET_NR_shmget
  8773. case TARGET_NR_shmget:
  8774. return get_errno(shmget(arg1, arg2, arg3));
  8775. #endif
  8776. #ifdef TARGET_NR_shmctl
  8777. case TARGET_NR_shmctl:
  8778. return do_shmctl(arg1, arg2, arg3);
  8779. #endif
  8780. #ifdef TARGET_NR_shmat
  8781. case TARGET_NR_shmat:
  8782. return do_shmat(cpu_env, arg1, arg2, arg3);
  8783. #endif
  8784. #ifdef TARGET_NR_shmdt
  8785. case TARGET_NR_shmdt:
  8786. return do_shmdt(arg1);
  8787. #endif
  8788. case TARGET_NR_fsync:
  8789. return get_errno(fsync(arg1));
  8790. case TARGET_NR_clone:
  8791. /* Linux manages to have three different orderings for its
  8792. * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
  8793. * match the kernel's CONFIG_CLONE_* settings.
  8794. * Microblaze is further special in that it uses a sixth
  8795. * implicit argument to clone for the TLS pointer.
  8796. */
  8797. #if defined(TARGET_MICROBLAZE)
  8798. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5));
  8799. #elif defined(TARGET_CLONE_BACKWARDS)
  8800. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
  8801. #elif defined(TARGET_CLONE_BACKWARDS2)
  8802. ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
  8803. #else
  8804. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
  8805. #endif
  8806. return ret;
  8807. #ifdef __NR_exit_group
  8808. /* new thread calls */
  8809. case TARGET_NR_exit_group:
  8810. preexit_cleanup(cpu_env, arg1);
  8811. return get_errno(exit_group(arg1));
  8812. #endif
  8813. case TARGET_NR_setdomainname:
  8814. if (!(p = lock_user_string(arg1)))
  8815. return -TARGET_EFAULT;
  8816. ret = get_errno(setdomainname(p, arg2));
  8817. unlock_user(p, arg1, 0);
  8818. return ret;
  8819. case TARGET_NR_uname:
  8820. /* no need to transcode because we use the linux syscall */
  8821. {
  8822. struct new_utsname * buf;
  8823. if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
  8824. return -TARGET_EFAULT;
  8825. ret = get_errno(sys_uname(buf));
  8826. if (!is_error(ret)) {
  8827. /* Overwrite the native machine name with whatever is being
  8828. emulated. */
  8829. g_strlcpy(buf->machine, cpu_to_uname_machine(cpu_env),
  8830. sizeof(buf->machine));
  8831. /* Allow the user to override the reported release. */
  8832. if (qemu_uname_release && *qemu_uname_release) {
  8833. g_strlcpy(buf->release, qemu_uname_release,
  8834. sizeof(buf->release));
  8835. }
  8836. }
  8837. unlock_user_struct(buf, arg1, 1);
  8838. }
  8839. return ret;
  8840. #ifdef TARGET_I386
  8841. case TARGET_NR_modify_ldt:
  8842. return do_modify_ldt(cpu_env, arg1, arg2, arg3);
  8843. #if !defined(TARGET_X86_64)
  8844. case TARGET_NR_vm86:
  8845. return do_vm86(cpu_env, arg1, arg2);
  8846. #endif
  8847. #endif
  8848. #if defined(TARGET_NR_adjtimex)
  8849. case TARGET_NR_adjtimex:
  8850. {
  8851. struct timex host_buf;
  8852. if (target_to_host_timex(&host_buf, arg1) != 0) {
  8853. return -TARGET_EFAULT;
  8854. }
  8855. ret = get_errno(adjtimex(&host_buf));
  8856. if (!is_error(ret)) {
  8857. if (host_to_target_timex(arg1, &host_buf) != 0) {
  8858. return -TARGET_EFAULT;
  8859. }
  8860. }
  8861. }
  8862. return ret;
  8863. #endif
  8864. #if defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME)
  8865. case TARGET_NR_clock_adjtime:
  8866. {
  8867. struct timex htx, *phtx = &htx;
  8868. if (target_to_host_timex(phtx, arg2) != 0) {
  8869. return -TARGET_EFAULT;
  8870. }
  8871. ret = get_errno(clock_adjtime(arg1, phtx));
  8872. if (!is_error(ret) && phtx) {
  8873. if (host_to_target_timex(arg2, phtx) != 0) {
  8874. return -TARGET_EFAULT;
  8875. }
  8876. }
  8877. }
  8878. return ret;
  8879. #endif
  8880. case TARGET_NR_getpgid:
  8881. return get_errno(getpgid(arg1));
  8882. case TARGET_NR_fchdir:
  8883. return get_errno(fchdir(arg1));
  8884. case TARGET_NR_personality:
  8885. return get_errno(personality(arg1));
  8886. #ifdef TARGET_NR__llseek /* Not on alpha */
  8887. case TARGET_NR__llseek:
  8888. {
  8889. int64_t res;
  8890. #if !defined(__NR_llseek)
  8891. res = lseek(arg1, ((uint64_t)arg2 << 32) | (abi_ulong)arg3, arg5);
  8892. if (res == -1) {
  8893. ret = get_errno(res);
  8894. } else {
  8895. ret = 0;
  8896. }
  8897. #else
  8898. ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
  8899. #endif
  8900. if ((ret == 0) && put_user_s64(res, arg4)) {
  8901. return -TARGET_EFAULT;
  8902. }
  8903. }
  8904. return ret;
  8905. #endif
  8906. #ifdef TARGET_NR_getdents
  8907. case TARGET_NR_getdents:
  8908. #ifdef EMULATE_GETDENTS_WITH_GETDENTS
  8909. #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
  8910. {
  8911. struct target_dirent *target_dirp;
  8912. struct linux_dirent *dirp;
  8913. abi_long count = arg3;
  8914. dirp = g_try_malloc(count);
  8915. if (!dirp) {
  8916. return -TARGET_ENOMEM;
  8917. }
  8918. ret = get_errno(sys_getdents(arg1, dirp, count));
  8919. if (!is_error(ret)) {
  8920. struct linux_dirent *de;
  8921. struct target_dirent *tde;
  8922. int len = ret;
  8923. int reclen, treclen;
  8924. int count1, tnamelen;
  8925. count1 = 0;
  8926. de = dirp;
  8927. if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
  8928. return -TARGET_EFAULT;
  8929. tde = target_dirp;
  8930. while (len > 0) {
  8931. reclen = de->d_reclen;
  8932. tnamelen = reclen - offsetof(struct linux_dirent, d_name);
  8933. assert(tnamelen >= 0);
  8934. treclen = tnamelen + offsetof(struct target_dirent, d_name);
  8935. assert(count1 + treclen <= count);
  8936. tde->d_reclen = tswap16(treclen);
  8937. tde->d_ino = tswapal(de->d_ino);
  8938. tde->d_off = tswapal(de->d_off);
  8939. memcpy(tde->d_name, de->d_name, tnamelen);
  8940. de = (struct linux_dirent *)((char *)de + reclen);
  8941. len -= reclen;
  8942. tde = (struct target_dirent *)((char *)tde + treclen);
  8943. count1 += treclen;
  8944. }
  8945. ret = count1;
  8946. unlock_user(target_dirp, arg2, ret);
  8947. }
  8948. g_free(dirp);
  8949. }
  8950. #else
  8951. {
  8952. struct linux_dirent *dirp;
  8953. abi_long count = arg3;
  8954. if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
  8955. return -TARGET_EFAULT;
  8956. ret = get_errno(sys_getdents(arg1, dirp, count));
  8957. if (!is_error(ret)) {
  8958. struct linux_dirent *de;
  8959. int len = ret;
  8960. int reclen;
  8961. de = dirp;
  8962. while (len > 0) {
  8963. reclen = de->d_reclen;
  8964. if (reclen > len)
  8965. break;
  8966. de->d_reclen = tswap16(reclen);
  8967. tswapls(&de->d_ino);
  8968. tswapls(&de->d_off);
  8969. de = (struct linux_dirent *)((char *)de + reclen);
  8970. len -= reclen;
  8971. }
  8972. }
  8973. unlock_user(dirp, arg2, ret);
  8974. }
  8975. #endif
  8976. #else
  8977. /* Implement getdents in terms of getdents64 */
  8978. {
  8979. struct linux_dirent64 *dirp;
  8980. abi_long count = arg3;
  8981. dirp = lock_user(VERIFY_WRITE, arg2, count, 0);
  8982. if (!dirp) {
  8983. return -TARGET_EFAULT;
  8984. }
  8985. ret = get_errno(sys_getdents64(arg1, dirp, count));
  8986. if (!is_error(ret)) {
  8987. /* Convert the dirent64 structs to target dirent. We do this
  8988. * in-place, since we can guarantee that a target_dirent is no
  8989. * larger than a dirent64; however this means we have to be
  8990. * careful to read everything before writing in the new format.
  8991. */
  8992. struct linux_dirent64 *de;
  8993. struct target_dirent *tde;
  8994. int len = ret;
  8995. int tlen = 0;
  8996. de = dirp;
  8997. tde = (struct target_dirent *)dirp;
  8998. while (len > 0) {
  8999. int namelen, treclen;
  9000. int reclen = de->d_reclen;
  9001. uint64_t ino = de->d_ino;
  9002. int64_t off = de->d_off;
  9003. uint8_t type = de->d_type;
  9004. namelen = strlen(de->d_name);
  9005. treclen = offsetof(struct target_dirent, d_name)
  9006. + namelen + 2;
  9007. treclen = QEMU_ALIGN_UP(treclen, sizeof(abi_long));
  9008. memmove(tde->d_name, de->d_name, namelen + 1);
  9009. tde->d_ino = tswapal(ino);
  9010. tde->d_off = tswapal(off);
  9011. tde->d_reclen = tswap16(treclen);
  9012. /* The target_dirent type is in what was formerly a padding
  9013. * byte at the end of the structure:
  9014. */
  9015. *(((char *)tde) + treclen - 1) = type;
  9016. de = (struct linux_dirent64 *)((char *)de + reclen);
  9017. tde = (struct target_dirent *)((char *)tde + treclen);
  9018. len -= reclen;
  9019. tlen += treclen;
  9020. }
  9021. ret = tlen;
  9022. }
  9023. unlock_user(dirp, arg2, ret);
  9024. }
  9025. #endif
  9026. return ret;
  9027. #endif /* TARGET_NR_getdents */
  9028. #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
  9029. case TARGET_NR_getdents64:
  9030. {
  9031. struct linux_dirent64 *dirp;
  9032. abi_long count = arg3;
  9033. if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
  9034. return -TARGET_EFAULT;
  9035. ret = get_errno(sys_getdents64(arg1, dirp, count));
  9036. if (!is_error(ret)) {
  9037. struct linux_dirent64 *de;
  9038. int len = ret;
  9039. int reclen;
  9040. de = dirp;
  9041. while (len > 0) {
  9042. reclen = de->d_reclen;
  9043. if (reclen > len)
  9044. break;
  9045. de->d_reclen = tswap16(reclen);
  9046. tswap64s((uint64_t *)&de->d_ino);
  9047. tswap64s((uint64_t *)&de->d_off);
  9048. de = (struct linux_dirent64 *)((char *)de + reclen);
  9049. len -= reclen;
  9050. }
  9051. }
  9052. unlock_user(dirp, arg2, ret);
  9053. }
  9054. return ret;
  9055. #endif /* TARGET_NR_getdents64 */
  9056. #if defined(TARGET_NR__newselect)
  9057. case TARGET_NR__newselect:
  9058. return do_select(arg1, arg2, arg3, arg4, arg5);
  9059. #endif
  9060. #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
  9061. # ifdef TARGET_NR_poll
  9062. case TARGET_NR_poll:
  9063. # endif
  9064. # ifdef TARGET_NR_ppoll
  9065. case TARGET_NR_ppoll:
  9066. # endif
  9067. {
  9068. struct target_pollfd *target_pfd;
  9069. unsigned int nfds = arg2;
  9070. struct pollfd *pfd;
  9071. unsigned int i;
  9072. pfd = NULL;
  9073. target_pfd = NULL;
  9074. if (nfds) {
  9075. if (nfds > (INT_MAX / sizeof(struct target_pollfd))) {
  9076. return -TARGET_EINVAL;
  9077. }
  9078. target_pfd = lock_user(VERIFY_WRITE, arg1,
  9079. sizeof(struct target_pollfd) * nfds, 1);
  9080. if (!target_pfd) {
  9081. return -TARGET_EFAULT;
  9082. }
  9083. pfd = alloca(sizeof(struct pollfd) * nfds);
  9084. for (i = 0; i < nfds; i++) {
  9085. pfd[i].fd = tswap32(target_pfd[i].fd);
  9086. pfd[i].events = tswap16(target_pfd[i].events);
  9087. }
  9088. }
  9089. switch (num) {
  9090. # ifdef TARGET_NR_ppoll
  9091. case TARGET_NR_ppoll:
  9092. {
  9093. struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
  9094. target_sigset_t *target_set;
  9095. sigset_t _set, *set = &_set;
  9096. if (arg3) {
  9097. if (target_to_host_timespec(timeout_ts, arg3)) {
  9098. unlock_user(target_pfd, arg1, 0);
  9099. return -TARGET_EFAULT;
  9100. }
  9101. } else {
  9102. timeout_ts = NULL;
  9103. }
  9104. if (arg4) {
  9105. if (arg5 != sizeof(target_sigset_t)) {
  9106. unlock_user(target_pfd, arg1, 0);
  9107. return -TARGET_EINVAL;
  9108. }
  9109. target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1);
  9110. if (!target_set) {
  9111. unlock_user(target_pfd, arg1, 0);
  9112. return -TARGET_EFAULT;
  9113. }
  9114. target_to_host_sigset(set, target_set);
  9115. } else {
  9116. set = NULL;
  9117. }
  9118. ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts,
  9119. set, SIGSET_T_SIZE));
  9120. if (!is_error(ret) && arg3) {
  9121. host_to_target_timespec(arg3, timeout_ts);
  9122. }
  9123. if (arg4) {
  9124. unlock_user(target_set, arg4, 0);
  9125. }
  9126. break;
  9127. }
  9128. # endif
  9129. # ifdef TARGET_NR_poll
  9130. case TARGET_NR_poll:
  9131. {
  9132. struct timespec ts, *pts;
  9133. if (arg3 >= 0) {
  9134. /* Convert ms to secs, ns */
  9135. ts.tv_sec = arg3 / 1000;
  9136. ts.tv_nsec = (arg3 % 1000) * 1000000LL;
  9137. pts = &ts;
  9138. } else {
  9139. /* -ve poll() timeout means "infinite" */
  9140. pts = NULL;
  9141. }
  9142. ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0));
  9143. break;
  9144. }
  9145. # endif
  9146. default:
  9147. g_assert_not_reached();
  9148. }
  9149. if (!is_error(ret)) {
  9150. for(i = 0; i < nfds; i++) {
  9151. target_pfd[i].revents = tswap16(pfd[i].revents);
  9152. }
  9153. }
  9154. unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
  9155. }
  9156. return ret;
  9157. #endif
  9158. case TARGET_NR_flock:
  9159. /* NOTE: the flock constant seems to be the same for every
  9160. Linux platform */
  9161. return get_errno(safe_flock(arg1, arg2));
  9162. case TARGET_NR_readv:
  9163. {
  9164. struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
  9165. if (vec != NULL) {
  9166. ret = get_errno(safe_readv(arg1, vec, arg3));
  9167. unlock_iovec(vec, arg2, arg3, 1);
  9168. } else {
  9169. ret = -host_to_target_errno(errno);
  9170. }
  9171. }
  9172. return ret;
  9173. case TARGET_NR_writev:
  9174. {
  9175. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  9176. if (vec != NULL) {
  9177. ret = get_errno(safe_writev(arg1, vec, arg3));
  9178. unlock_iovec(vec, arg2, arg3, 0);
  9179. } else {
  9180. ret = -host_to_target_errno(errno);
  9181. }
  9182. }
  9183. return ret;
  9184. #if defined(TARGET_NR_preadv)
  9185. case TARGET_NR_preadv:
  9186. {
  9187. struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
  9188. if (vec != NULL) {
  9189. unsigned long low, high;
  9190. target_to_host_low_high(arg4, arg5, &low, &high);
  9191. ret = get_errno(safe_preadv(arg1, vec, arg3, low, high));
  9192. unlock_iovec(vec, arg2, arg3, 1);
  9193. } else {
  9194. ret = -host_to_target_errno(errno);
  9195. }
  9196. }
  9197. return ret;
  9198. #endif
  9199. #if defined(TARGET_NR_pwritev)
  9200. case TARGET_NR_pwritev:
  9201. {
  9202. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  9203. if (vec != NULL) {
  9204. unsigned long low, high;
  9205. target_to_host_low_high(arg4, arg5, &low, &high);
  9206. ret = get_errno(safe_pwritev(arg1, vec, arg3, low, high));
  9207. unlock_iovec(vec, arg2, arg3, 0);
  9208. } else {
  9209. ret = -host_to_target_errno(errno);
  9210. }
  9211. }
  9212. return ret;
  9213. #endif
  9214. case TARGET_NR_getsid:
  9215. return get_errno(getsid(arg1));
  9216. #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
  9217. case TARGET_NR_fdatasync:
  9218. return get_errno(fdatasync(arg1));
  9219. #endif
  9220. #ifdef TARGET_NR__sysctl
  9221. case TARGET_NR__sysctl:
  9222. /* We don't implement this, but ENOTDIR is always a safe
  9223. return value. */
  9224. return -TARGET_ENOTDIR;
  9225. #endif
  9226. case TARGET_NR_sched_getaffinity:
  9227. {
  9228. unsigned int mask_size;
  9229. unsigned long *mask;
  9230. /*
  9231. * sched_getaffinity needs multiples of ulong, so need to take
  9232. * care of mismatches between target ulong and host ulong sizes.
  9233. */
  9234. if (arg2 & (sizeof(abi_ulong) - 1)) {
  9235. return -TARGET_EINVAL;
  9236. }
  9237. mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
  9238. mask = alloca(mask_size);
  9239. memset(mask, 0, mask_size);
  9240. ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
  9241. if (!is_error(ret)) {
  9242. if (ret > arg2) {
  9243. /* More data returned than the caller's buffer will fit.
  9244. * This only happens if sizeof(abi_long) < sizeof(long)
  9245. * and the caller passed us a buffer holding an odd number
  9246. * of abi_longs. If the host kernel is actually using the
  9247. * extra 4 bytes then fail EINVAL; otherwise we can just
  9248. * ignore them and only copy the interesting part.
  9249. */
  9250. int numcpus = sysconf(_SC_NPROCESSORS_CONF);
  9251. if (numcpus > arg2 * 8) {
  9252. return -TARGET_EINVAL;
  9253. }
  9254. ret = arg2;
  9255. }
  9256. if (host_to_target_cpu_mask(mask, mask_size, arg3, ret)) {
  9257. return -TARGET_EFAULT;
  9258. }
  9259. }
  9260. }
  9261. return ret;
  9262. case TARGET_NR_sched_setaffinity:
  9263. {
  9264. unsigned int mask_size;
  9265. unsigned long *mask;
  9266. /*
  9267. * sched_setaffinity needs multiples of ulong, so need to take
  9268. * care of mismatches between target ulong and host ulong sizes.
  9269. */
  9270. if (arg2 & (sizeof(abi_ulong) - 1)) {
  9271. return -TARGET_EINVAL;
  9272. }
  9273. mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
  9274. mask = alloca(mask_size);
  9275. ret = target_to_host_cpu_mask(mask, mask_size, arg3, arg2);
  9276. if (ret) {
  9277. return ret;
  9278. }
  9279. return get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
  9280. }
  9281. case TARGET_NR_getcpu:
  9282. {
  9283. unsigned cpu, node;
  9284. ret = get_errno(sys_getcpu(arg1 ? &cpu : NULL,
  9285. arg2 ? &node : NULL,
  9286. NULL));
  9287. if (is_error(ret)) {
  9288. return ret;
  9289. }
  9290. if (arg1 && put_user_u32(cpu, arg1)) {
  9291. return -TARGET_EFAULT;
  9292. }
  9293. if (arg2 && put_user_u32(node, arg2)) {
  9294. return -TARGET_EFAULT;
  9295. }
  9296. }
  9297. return ret;
  9298. case TARGET_NR_sched_setparam:
  9299. {
  9300. struct sched_param *target_schp;
  9301. struct sched_param schp;
  9302. if (arg2 == 0) {
  9303. return -TARGET_EINVAL;
  9304. }
  9305. if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
  9306. return -TARGET_EFAULT;
  9307. schp.sched_priority = tswap32(target_schp->sched_priority);
  9308. unlock_user_struct(target_schp, arg2, 0);
  9309. return get_errno(sched_setparam(arg1, &schp));
  9310. }
  9311. case TARGET_NR_sched_getparam:
  9312. {
  9313. struct sched_param *target_schp;
  9314. struct sched_param schp;
  9315. if (arg2 == 0) {
  9316. return -TARGET_EINVAL;
  9317. }
  9318. ret = get_errno(sched_getparam(arg1, &schp));
  9319. if (!is_error(ret)) {
  9320. if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
  9321. return -TARGET_EFAULT;
  9322. target_schp->sched_priority = tswap32(schp.sched_priority);
  9323. unlock_user_struct(target_schp, arg2, 1);
  9324. }
  9325. }
  9326. return ret;
  9327. case TARGET_NR_sched_setscheduler:
  9328. {
  9329. struct sched_param *target_schp;
  9330. struct sched_param schp;
  9331. if (arg3 == 0) {
  9332. return -TARGET_EINVAL;
  9333. }
  9334. if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
  9335. return -TARGET_EFAULT;
  9336. schp.sched_priority = tswap32(target_schp->sched_priority);
  9337. unlock_user_struct(target_schp, arg3, 0);
  9338. return get_errno(sched_setscheduler(arg1, arg2, &schp));
  9339. }
  9340. case TARGET_NR_sched_getscheduler:
  9341. return get_errno(sched_getscheduler(arg1));
  9342. case TARGET_NR_sched_yield:
  9343. return get_errno(sched_yield());
  9344. case TARGET_NR_sched_get_priority_max:
  9345. return get_errno(sched_get_priority_max(arg1));
  9346. case TARGET_NR_sched_get_priority_min:
  9347. return get_errno(sched_get_priority_min(arg1));
  9348. #ifdef TARGET_NR_sched_rr_get_interval
  9349. case TARGET_NR_sched_rr_get_interval:
  9350. {
  9351. struct timespec ts;
  9352. ret = get_errno(sched_rr_get_interval(arg1, &ts));
  9353. if (!is_error(ret)) {
  9354. ret = host_to_target_timespec(arg2, &ts);
  9355. }
  9356. }
  9357. return ret;
  9358. #endif
  9359. #if defined(TARGET_NR_nanosleep)
  9360. case TARGET_NR_nanosleep:
  9361. {
  9362. struct timespec req, rem;
  9363. target_to_host_timespec(&req, arg1);
  9364. ret = get_errno(safe_nanosleep(&req, &rem));
  9365. if (is_error(ret) && arg2) {
  9366. host_to_target_timespec(arg2, &rem);
  9367. }
  9368. }
  9369. return ret;
  9370. #endif
  9371. case TARGET_NR_prctl:
  9372. switch (arg1) {
  9373. case PR_GET_PDEATHSIG:
  9374. {
  9375. int deathsig;
  9376. ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
  9377. if (!is_error(ret) && arg2
  9378. && put_user_ual(deathsig, arg2)) {
  9379. return -TARGET_EFAULT;
  9380. }
  9381. return ret;
  9382. }
  9383. #ifdef PR_GET_NAME
  9384. case PR_GET_NAME:
  9385. {
  9386. void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
  9387. if (!name) {
  9388. return -TARGET_EFAULT;
  9389. }
  9390. ret = get_errno(prctl(arg1, (unsigned long)name,
  9391. arg3, arg4, arg5));
  9392. unlock_user(name, arg2, 16);
  9393. return ret;
  9394. }
  9395. case PR_SET_NAME:
  9396. {
  9397. void *name = lock_user(VERIFY_READ, arg2, 16, 1);
  9398. if (!name) {
  9399. return -TARGET_EFAULT;
  9400. }
  9401. ret = get_errno(prctl(arg1, (unsigned long)name,
  9402. arg3, arg4, arg5));
  9403. unlock_user(name, arg2, 0);
  9404. return ret;
  9405. }
  9406. #endif
  9407. #ifdef TARGET_MIPS
  9408. case TARGET_PR_GET_FP_MODE:
  9409. {
  9410. CPUMIPSState *env = ((CPUMIPSState *)cpu_env);
  9411. ret = 0;
  9412. if (env->CP0_Status & (1 << CP0St_FR)) {
  9413. ret |= TARGET_PR_FP_MODE_FR;
  9414. }
  9415. if (env->CP0_Config5 & (1 << CP0C5_FRE)) {
  9416. ret |= TARGET_PR_FP_MODE_FRE;
  9417. }
  9418. return ret;
  9419. }
  9420. case TARGET_PR_SET_FP_MODE:
  9421. {
  9422. CPUMIPSState *env = ((CPUMIPSState *)cpu_env);
  9423. bool old_fr = env->CP0_Status & (1 << CP0St_FR);
  9424. bool old_fre = env->CP0_Config5 & (1 << CP0C5_FRE);
  9425. bool new_fr = arg2 & TARGET_PR_FP_MODE_FR;
  9426. bool new_fre = arg2 & TARGET_PR_FP_MODE_FRE;
  9427. const unsigned int known_bits = TARGET_PR_FP_MODE_FR |
  9428. TARGET_PR_FP_MODE_FRE;
  9429. /* If nothing to change, return right away, successfully. */
  9430. if (old_fr == new_fr && old_fre == new_fre) {
  9431. return 0;
  9432. }
  9433. /* Check the value is valid */
  9434. if (arg2 & ~known_bits) {
  9435. return -TARGET_EOPNOTSUPP;
  9436. }
  9437. /* Setting FRE without FR is not supported. */
  9438. if (new_fre && !new_fr) {
  9439. return -TARGET_EOPNOTSUPP;
  9440. }
  9441. if (new_fr && !(env->active_fpu.fcr0 & (1 << FCR0_F64))) {
  9442. /* FR1 is not supported */
  9443. return -TARGET_EOPNOTSUPP;
  9444. }
  9445. if (!new_fr && (env->active_fpu.fcr0 & (1 << FCR0_F64))
  9446. && !(env->CP0_Status_rw_bitmask & (1 << CP0St_FR))) {
  9447. /* cannot set FR=0 */
  9448. return -TARGET_EOPNOTSUPP;
  9449. }
  9450. if (new_fre && !(env->active_fpu.fcr0 & (1 << FCR0_FREP))) {
  9451. /* Cannot set FRE=1 */
  9452. return -TARGET_EOPNOTSUPP;
  9453. }
  9454. int i;
  9455. fpr_t *fpr = env->active_fpu.fpr;
  9456. for (i = 0; i < 32 ; i += 2) {
  9457. if (!old_fr && new_fr) {
  9458. fpr[i].w[!FP_ENDIAN_IDX] = fpr[i + 1].w[FP_ENDIAN_IDX];
  9459. } else if (old_fr && !new_fr) {
  9460. fpr[i + 1].w[FP_ENDIAN_IDX] = fpr[i].w[!FP_ENDIAN_IDX];
  9461. }
  9462. }
  9463. if (new_fr) {
  9464. env->CP0_Status |= (1 << CP0St_FR);
  9465. env->hflags |= MIPS_HFLAG_F64;
  9466. } else {
  9467. env->CP0_Status &= ~(1 << CP0St_FR);
  9468. env->hflags &= ~MIPS_HFLAG_F64;
  9469. }
  9470. if (new_fre) {
  9471. env->CP0_Config5 |= (1 << CP0C5_FRE);
  9472. if (env->active_fpu.fcr0 & (1 << FCR0_FREP)) {
  9473. env->hflags |= MIPS_HFLAG_FRE;
  9474. }
  9475. } else {
  9476. env->CP0_Config5 &= ~(1 << CP0C5_FRE);
  9477. env->hflags &= ~MIPS_HFLAG_FRE;
  9478. }
  9479. return 0;
  9480. }
  9481. #endif /* MIPS */
  9482. #ifdef TARGET_AARCH64
  9483. case TARGET_PR_SVE_SET_VL:
  9484. /*
  9485. * We cannot support either PR_SVE_SET_VL_ONEXEC or
  9486. * PR_SVE_VL_INHERIT. Note the kernel definition
  9487. * of sve_vl_valid allows for VQ=512, i.e. VL=8192,
  9488. * even though the current architectural maximum is VQ=16.
  9489. */
  9490. ret = -TARGET_EINVAL;
  9491. if (cpu_isar_feature(aa64_sve, env_archcpu(cpu_env))
  9492. && arg2 >= 0 && arg2 <= 512 * 16 && !(arg2 & 15)) {
  9493. CPUARMState *env = cpu_env;
  9494. ARMCPU *cpu = env_archcpu(env);
  9495. uint32_t vq, old_vq;
  9496. old_vq = (env->vfp.zcr_el[1] & 0xf) + 1;
  9497. vq = MAX(arg2 / 16, 1);
  9498. vq = MIN(vq, cpu->sve_max_vq);
  9499. if (vq < old_vq) {
  9500. aarch64_sve_narrow_vq(env, vq);
  9501. }
  9502. env->vfp.zcr_el[1] = vq - 1;
  9503. arm_rebuild_hflags(env);
  9504. ret = vq * 16;
  9505. }
  9506. return ret;
  9507. case TARGET_PR_SVE_GET_VL:
  9508. ret = -TARGET_EINVAL;
  9509. {
  9510. ARMCPU *cpu = env_archcpu(cpu_env);
  9511. if (cpu_isar_feature(aa64_sve, cpu)) {
  9512. ret = ((cpu->env.vfp.zcr_el[1] & 0xf) + 1) * 16;
  9513. }
  9514. }
  9515. return ret;
  9516. case TARGET_PR_PAC_RESET_KEYS:
  9517. {
  9518. CPUARMState *env = cpu_env;
  9519. ARMCPU *cpu = env_archcpu(env);
  9520. if (arg3 || arg4 || arg5) {
  9521. return -TARGET_EINVAL;
  9522. }
  9523. if (cpu_isar_feature(aa64_pauth, cpu)) {
  9524. int all = (TARGET_PR_PAC_APIAKEY | TARGET_PR_PAC_APIBKEY |
  9525. TARGET_PR_PAC_APDAKEY | TARGET_PR_PAC_APDBKEY |
  9526. TARGET_PR_PAC_APGAKEY);
  9527. int ret = 0;
  9528. Error *err = NULL;
  9529. if (arg2 == 0) {
  9530. arg2 = all;
  9531. } else if (arg2 & ~all) {
  9532. return -TARGET_EINVAL;
  9533. }
  9534. if (arg2 & TARGET_PR_PAC_APIAKEY) {
  9535. ret |= qemu_guest_getrandom(&env->keys.apia,
  9536. sizeof(ARMPACKey), &err);
  9537. }
  9538. if (arg2 & TARGET_PR_PAC_APIBKEY) {
  9539. ret |= qemu_guest_getrandom(&env->keys.apib,
  9540. sizeof(ARMPACKey), &err);
  9541. }
  9542. if (arg2 & TARGET_PR_PAC_APDAKEY) {
  9543. ret |= qemu_guest_getrandom(&env->keys.apda,
  9544. sizeof(ARMPACKey), &err);
  9545. }
  9546. if (arg2 & TARGET_PR_PAC_APDBKEY) {
  9547. ret |= qemu_guest_getrandom(&env->keys.apdb,
  9548. sizeof(ARMPACKey), &err);
  9549. }
  9550. if (arg2 & TARGET_PR_PAC_APGAKEY) {
  9551. ret |= qemu_guest_getrandom(&env->keys.apga,
  9552. sizeof(ARMPACKey), &err);
  9553. }
  9554. if (ret != 0) {
  9555. /*
  9556. * Some unknown failure in the crypto. The best
  9557. * we can do is log it and fail the syscall.
  9558. * The real syscall cannot fail this way.
  9559. */
  9560. qemu_log_mask(LOG_UNIMP,
  9561. "PR_PAC_RESET_KEYS: Crypto failure: %s",
  9562. error_get_pretty(err));
  9563. error_free(err);
  9564. return -TARGET_EIO;
  9565. }
  9566. return 0;
  9567. }
  9568. }
  9569. return -TARGET_EINVAL;
  9570. #endif /* AARCH64 */
  9571. case PR_GET_SECCOMP:
  9572. case PR_SET_SECCOMP:
  9573. /* Disable seccomp to prevent the target disabling syscalls we
  9574. * need. */
  9575. return -TARGET_EINVAL;
  9576. default:
  9577. /* Most prctl options have no pointer arguments */
  9578. return get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
  9579. }
  9580. break;
  9581. #ifdef TARGET_NR_arch_prctl
  9582. case TARGET_NR_arch_prctl:
  9583. return do_arch_prctl(cpu_env, arg1, arg2);
  9584. #endif
  9585. #ifdef TARGET_NR_pread64
  9586. case TARGET_NR_pread64:
  9587. if (regpairs_aligned(cpu_env, num)) {
  9588. arg4 = arg5;
  9589. arg5 = arg6;
  9590. }
  9591. if (arg2 == 0 && arg3 == 0) {
  9592. /* Special-case NULL buffer and zero length, which should succeed */
  9593. p = 0;
  9594. } else {
  9595. p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  9596. if (!p) {
  9597. return -TARGET_EFAULT;
  9598. }
  9599. }
  9600. ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
  9601. unlock_user(p, arg2, ret);
  9602. return ret;
  9603. case TARGET_NR_pwrite64:
  9604. if (regpairs_aligned(cpu_env, num)) {
  9605. arg4 = arg5;
  9606. arg5 = arg6;
  9607. }
  9608. if (arg2 == 0 && arg3 == 0) {
  9609. /* Special-case NULL buffer and zero length, which should succeed */
  9610. p = 0;
  9611. } else {
  9612. p = lock_user(VERIFY_READ, arg2, arg3, 1);
  9613. if (!p) {
  9614. return -TARGET_EFAULT;
  9615. }
  9616. }
  9617. ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
  9618. unlock_user(p, arg2, 0);
  9619. return ret;
  9620. #endif
  9621. case TARGET_NR_getcwd:
  9622. if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
  9623. return -TARGET_EFAULT;
  9624. ret = get_errno(sys_getcwd1(p, arg2));
  9625. unlock_user(p, arg1, ret);
  9626. return ret;
  9627. case TARGET_NR_capget:
  9628. case TARGET_NR_capset:
  9629. {
  9630. struct target_user_cap_header *target_header;
  9631. struct target_user_cap_data *target_data = NULL;
  9632. struct __user_cap_header_struct header;
  9633. struct __user_cap_data_struct data[2];
  9634. struct __user_cap_data_struct *dataptr = NULL;
  9635. int i, target_datalen;
  9636. int data_items = 1;
  9637. if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) {
  9638. return -TARGET_EFAULT;
  9639. }
  9640. header.version = tswap32(target_header->version);
  9641. header.pid = tswap32(target_header->pid);
  9642. if (header.version != _LINUX_CAPABILITY_VERSION) {
  9643. /* Version 2 and up takes pointer to two user_data structs */
  9644. data_items = 2;
  9645. }
  9646. target_datalen = sizeof(*target_data) * data_items;
  9647. if (arg2) {
  9648. if (num == TARGET_NR_capget) {
  9649. target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0);
  9650. } else {
  9651. target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1);
  9652. }
  9653. if (!target_data) {
  9654. unlock_user_struct(target_header, arg1, 0);
  9655. return -TARGET_EFAULT;
  9656. }
  9657. if (num == TARGET_NR_capset) {
  9658. for (i = 0; i < data_items; i++) {
  9659. data[i].effective = tswap32(target_data[i].effective);
  9660. data[i].permitted = tswap32(target_data[i].permitted);
  9661. data[i].inheritable = tswap32(target_data[i].inheritable);
  9662. }
  9663. }
  9664. dataptr = data;
  9665. }
  9666. if (num == TARGET_NR_capget) {
  9667. ret = get_errno(capget(&header, dataptr));
  9668. } else {
  9669. ret = get_errno(capset(&header, dataptr));
  9670. }
  9671. /* The kernel always updates version for both capget and capset */
  9672. target_header->version = tswap32(header.version);
  9673. unlock_user_struct(target_header, arg1, 1);
  9674. if (arg2) {
  9675. if (num == TARGET_NR_capget) {
  9676. for (i = 0; i < data_items; i++) {
  9677. target_data[i].effective = tswap32(data[i].effective);
  9678. target_data[i].permitted = tswap32(data[i].permitted);
  9679. target_data[i].inheritable = tswap32(data[i].inheritable);
  9680. }
  9681. unlock_user(target_data, arg2, target_datalen);
  9682. } else {
  9683. unlock_user(target_data, arg2, 0);
  9684. }
  9685. }
  9686. return ret;
  9687. }
  9688. case TARGET_NR_sigaltstack:
  9689. return do_sigaltstack(arg1, arg2,
  9690. get_sp_from_cpustate((CPUArchState *)cpu_env));
  9691. #ifdef CONFIG_SENDFILE
  9692. #ifdef TARGET_NR_sendfile
  9693. case TARGET_NR_sendfile:
  9694. {
  9695. off_t *offp = NULL;
  9696. off_t off;
  9697. if (arg3) {
  9698. ret = get_user_sal(off, arg3);
  9699. if (is_error(ret)) {
  9700. return ret;
  9701. }
  9702. offp = &off;
  9703. }
  9704. ret = get_errno(sendfile(arg1, arg2, offp, arg4));
  9705. if (!is_error(ret) && arg3) {
  9706. abi_long ret2 = put_user_sal(off, arg3);
  9707. if (is_error(ret2)) {
  9708. ret = ret2;
  9709. }
  9710. }
  9711. return ret;
  9712. }
  9713. #endif
  9714. #ifdef TARGET_NR_sendfile64
  9715. case TARGET_NR_sendfile64:
  9716. {
  9717. off_t *offp = NULL;
  9718. off_t off;
  9719. if (arg3) {
  9720. ret = get_user_s64(off, arg3);
  9721. if (is_error(ret)) {
  9722. return ret;
  9723. }
  9724. offp = &off;
  9725. }
  9726. ret = get_errno(sendfile(arg1, arg2, offp, arg4));
  9727. if (!is_error(ret) && arg3) {
  9728. abi_long ret2 = put_user_s64(off, arg3);
  9729. if (is_error(ret2)) {
  9730. ret = ret2;
  9731. }
  9732. }
  9733. return ret;
  9734. }
  9735. #endif
  9736. #endif
  9737. #ifdef TARGET_NR_vfork
  9738. case TARGET_NR_vfork:
  9739. return get_errno(do_fork(cpu_env,
  9740. CLONE_VFORK | CLONE_VM | TARGET_SIGCHLD,
  9741. 0, 0, 0, 0));
  9742. #endif
  9743. #ifdef TARGET_NR_ugetrlimit
  9744. case TARGET_NR_ugetrlimit:
  9745. {
  9746. struct rlimit rlim;
  9747. int resource = target_to_host_resource(arg1);
  9748. ret = get_errno(getrlimit(resource, &rlim));
  9749. if (!is_error(ret)) {
  9750. struct target_rlimit *target_rlim;
  9751. if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
  9752. return -TARGET_EFAULT;
  9753. target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
  9754. target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
  9755. unlock_user_struct(target_rlim, arg2, 1);
  9756. }
  9757. return ret;
  9758. }
  9759. #endif
  9760. #ifdef TARGET_NR_truncate64
  9761. case TARGET_NR_truncate64:
  9762. if (!(p = lock_user_string(arg1)))
  9763. return -TARGET_EFAULT;
  9764. ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
  9765. unlock_user(p, arg1, 0);
  9766. return ret;
  9767. #endif
  9768. #ifdef TARGET_NR_ftruncate64
  9769. case TARGET_NR_ftruncate64:
  9770. return target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
  9771. #endif
  9772. #ifdef TARGET_NR_stat64
  9773. case TARGET_NR_stat64:
  9774. if (!(p = lock_user_string(arg1))) {
  9775. return -TARGET_EFAULT;
  9776. }
  9777. ret = get_errno(stat(path(p), &st));
  9778. unlock_user(p, arg1, 0);
  9779. if (!is_error(ret))
  9780. ret = host_to_target_stat64(cpu_env, arg2, &st);
  9781. return ret;
  9782. #endif
  9783. #ifdef TARGET_NR_lstat64
  9784. case TARGET_NR_lstat64:
  9785. if (!(p = lock_user_string(arg1))) {
  9786. return -TARGET_EFAULT;
  9787. }
  9788. ret = get_errno(lstat(path(p), &st));
  9789. unlock_user(p, arg1, 0);
  9790. if (!is_error(ret))
  9791. ret = host_to_target_stat64(cpu_env, arg2, &st);
  9792. return ret;
  9793. #endif
  9794. #ifdef TARGET_NR_fstat64
  9795. case TARGET_NR_fstat64:
  9796. ret = get_errno(fstat(arg1, &st));
  9797. if (!is_error(ret))
  9798. ret = host_to_target_stat64(cpu_env, arg2, &st);
  9799. return ret;
  9800. #endif
  9801. #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
  9802. #ifdef TARGET_NR_fstatat64
  9803. case TARGET_NR_fstatat64:
  9804. #endif
  9805. #ifdef TARGET_NR_newfstatat
  9806. case TARGET_NR_newfstatat:
  9807. #endif
  9808. if (!(p = lock_user_string(arg2))) {
  9809. return -TARGET_EFAULT;
  9810. }
  9811. ret = get_errno(fstatat(arg1, path(p), &st, arg4));
  9812. unlock_user(p, arg2, 0);
  9813. if (!is_error(ret))
  9814. ret = host_to_target_stat64(cpu_env, arg3, &st);
  9815. return ret;
  9816. #endif
  9817. #if defined(TARGET_NR_statx)
  9818. case TARGET_NR_statx:
  9819. {
  9820. struct target_statx *target_stx;
  9821. int dirfd = arg1;
  9822. int flags = arg3;
  9823. p = lock_user_string(arg2);
  9824. if (p == NULL) {
  9825. return -TARGET_EFAULT;
  9826. }
  9827. #if defined(__NR_statx)
  9828. {
  9829. /*
  9830. * It is assumed that struct statx is architecture independent.
  9831. */
  9832. struct target_statx host_stx;
  9833. int mask = arg4;
  9834. ret = get_errno(sys_statx(dirfd, p, flags, mask, &host_stx));
  9835. if (!is_error(ret)) {
  9836. if (host_to_target_statx(&host_stx, arg5) != 0) {
  9837. unlock_user(p, arg2, 0);
  9838. return -TARGET_EFAULT;
  9839. }
  9840. }
  9841. if (ret != -TARGET_ENOSYS) {
  9842. unlock_user(p, arg2, 0);
  9843. return ret;
  9844. }
  9845. }
  9846. #endif
  9847. ret = get_errno(fstatat(dirfd, path(p), &st, flags));
  9848. unlock_user(p, arg2, 0);
  9849. if (!is_error(ret)) {
  9850. if (!lock_user_struct(VERIFY_WRITE, target_stx, arg5, 0)) {
  9851. return -TARGET_EFAULT;
  9852. }
  9853. memset(target_stx, 0, sizeof(*target_stx));
  9854. __put_user(major(st.st_dev), &target_stx->stx_dev_major);
  9855. __put_user(minor(st.st_dev), &target_stx->stx_dev_minor);
  9856. __put_user(st.st_ino, &target_stx->stx_ino);
  9857. __put_user(st.st_mode, &target_stx->stx_mode);
  9858. __put_user(st.st_uid, &target_stx->stx_uid);
  9859. __put_user(st.st_gid, &target_stx->stx_gid);
  9860. __put_user(st.st_nlink, &target_stx->stx_nlink);
  9861. __put_user(major(st.st_rdev), &target_stx->stx_rdev_major);
  9862. __put_user(minor(st.st_rdev), &target_stx->stx_rdev_minor);
  9863. __put_user(st.st_size, &target_stx->stx_size);
  9864. __put_user(st.st_blksize, &target_stx->stx_blksize);
  9865. __put_user(st.st_blocks, &target_stx->stx_blocks);
  9866. __put_user(st.st_atime, &target_stx->stx_atime.tv_sec);
  9867. __put_user(st.st_mtime, &target_stx->stx_mtime.tv_sec);
  9868. __put_user(st.st_ctime, &target_stx->stx_ctime.tv_sec);
  9869. unlock_user_struct(target_stx, arg5, 1);
  9870. }
  9871. }
  9872. return ret;
  9873. #endif
  9874. #ifdef TARGET_NR_lchown
  9875. case TARGET_NR_lchown:
  9876. if (!(p = lock_user_string(arg1)))
  9877. return -TARGET_EFAULT;
  9878. ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
  9879. unlock_user(p, arg1, 0);
  9880. return ret;
  9881. #endif
  9882. #ifdef TARGET_NR_getuid
  9883. case TARGET_NR_getuid:
  9884. return get_errno(high2lowuid(getuid()));
  9885. #endif
  9886. #ifdef TARGET_NR_getgid
  9887. case TARGET_NR_getgid:
  9888. return get_errno(high2lowgid(getgid()));
  9889. #endif
  9890. #ifdef TARGET_NR_geteuid
  9891. case TARGET_NR_geteuid:
  9892. return get_errno(high2lowuid(geteuid()));
  9893. #endif
  9894. #ifdef TARGET_NR_getegid
  9895. case TARGET_NR_getegid:
  9896. return get_errno(high2lowgid(getegid()));
  9897. #endif
  9898. case TARGET_NR_setreuid:
  9899. return get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
  9900. case TARGET_NR_setregid:
  9901. return get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
  9902. case TARGET_NR_getgroups:
  9903. {
  9904. int gidsetsize = arg1;
  9905. target_id *target_grouplist;
  9906. gid_t *grouplist;
  9907. int i;
  9908. grouplist = alloca(gidsetsize * sizeof(gid_t));
  9909. ret = get_errno(getgroups(gidsetsize, grouplist));
  9910. if (gidsetsize == 0)
  9911. return ret;
  9912. if (!is_error(ret)) {
  9913. target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * sizeof(target_id), 0);
  9914. if (!target_grouplist)
  9915. return -TARGET_EFAULT;
  9916. for(i = 0;i < ret; i++)
  9917. target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
  9918. unlock_user(target_grouplist, arg2, gidsetsize * sizeof(target_id));
  9919. }
  9920. }
  9921. return ret;
  9922. case TARGET_NR_setgroups:
  9923. {
  9924. int gidsetsize = arg1;
  9925. target_id *target_grouplist;
  9926. gid_t *grouplist = NULL;
  9927. int i;
  9928. if (gidsetsize) {
  9929. grouplist = alloca(gidsetsize * sizeof(gid_t));
  9930. target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * sizeof(target_id), 1);
  9931. if (!target_grouplist) {
  9932. return -TARGET_EFAULT;
  9933. }
  9934. for (i = 0; i < gidsetsize; i++) {
  9935. grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
  9936. }
  9937. unlock_user(target_grouplist, arg2, 0);
  9938. }
  9939. return get_errno(setgroups(gidsetsize, grouplist));
  9940. }
  9941. case TARGET_NR_fchown:
  9942. return get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
  9943. #if defined(TARGET_NR_fchownat)
  9944. case TARGET_NR_fchownat:
  9945. if (!(p = lock_user_string(arg2)))
  9946. return -TARGET_EFAULT;
  9947. ret = get_errno(fchownat(arg1, p, low2highuid(arg3),
  9948. low2highgid(arg4), arg5));
  9949. unlock_user(p, arg2, 0);
  9950. return ret;
  9951. #endif
  9952. #ifdef TARGET_NR_setresuid
  9953. case TARGET_NR_setresuid:
  9954. return get_errno(sys_setresuid(low2highuid(arg1),
  9955. low2highuid(arg2),
  9956. low2highuid(arg3)));
  9957. #endif
  9958. #ifdef TARGET_NR_getresuid
  9959. case TARGET_NR_getresuid:
  9960. {
  9961. uid_t ruid, euid, suid;
  9962. ret = get_errno(getresuid(&ruid, &euid, &suid));
  9963. if (!is_error(ret)) {
  9964. if (put_user_id(high2lowuid(ruid), arg1)
  9965. || put_user_id(high2lowuid(euid), arg2)
  9966. || put_user_id(high2lowuid(suid), arg3))
  9967. return -TARGET_EFAULT;
  9968. }
  9969. }
  9970. return ret;
  9971. #endif
  9972. #ifdef TARGET_NR_getresgid
  9973. case TARGET_NR_setresgid:
  9974. return get_errno(sys_setresgid(low2highgid(arg1),
  9975. low2highgid(arg2),
  9976. low2highgid(arg3)));
  9977. #endif
  9978. #ifdef TARGET_NR_getresgid
  9979. case TARGET_NR_getresgid:
  9980. {
  9981. gid_t rgid, egid, sgid;
  9982. ret = get_errno(getresgid(&rgid, &egid, &sgid));
  9983. if (!is_error(ret)) {
  9984. if (put_user_id(high2lowgid(rgid), arg1)
  9985. || put_user_id(high2lowgid(egid), arg2)
  9986. || put_user_id(high2lowgid(sgid), arg3))
  9987. return -TARGET_EFAULT;
  9988. }
  9989. }
  9990. return ret;
  9991. #endif
  9992. #ifdef TARGET_NR_chown
  9993. case TARGET_NR_chown:
  9994. if (!(p = lock_user_string(arg1)))
  9995. return -TARGET_EFAULT;
  9996. ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
  9997. unlock_user(p, arg1, 0);
  9998. return ret;
  9999. #endif
  10000. case TARGET_NR_setuid:
  10001. return get_errno(sys_setuid(low2highuid(arg1)));
  10002. case TARGET_NR_setgid:
  10003. return get_errno(sys_setgid(low2highgid(arg1)));
  10004. case TARGET_NR_setfsuid:
  10005. return get_errno(setfsuid(arg1));
  10006. case TARGET_NR_setfsgid:
  10007. return get_errno(setfsgid(arg1));
  10008. #ifdef TARGET_NR_lchown32
  10009. case TARGET_NR_lchown32:
  10010. if (!(p = lock_user_string(arg1)))
  10011. return -TARGET_EFAULT;
  10012. ret = get_errno(lchown(p, arg2, arg3));
  10013. unlock_user(p, arg1, 0);
  10014. return ret;
  10015. #endif
  10016. #ifdef TARGET_NR_getuid32
  10017. case TARGET_NR_getuid32:
  10018. return get_errno(getuid());
  10019. #endif
  10020. #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
  10021. /* Alpha specific */
  10022. case TARGET_NR_getxuid:
  10023. {
  10024. uid_t euid;
  10025. euid=geteuid();
  10026. ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
  10027. }
  10028. return get_errno(getuid());
  10029. #endif
  10030. #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
  10031. /* Alpha specific */
  10032. case TARGET_NR_getxgid:
  10033. {
  10034. uid_t egid;
  10035. egid=getegid();
  10036. ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
  10037. }
  10038. return get_errno(getgid());
  10039. #endif
  10040. #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
  10041. /* Alpha specific */
  10042. case TARGET_NR_osf_getsysinfo:
  10043. ret = -TARGET_EOPNOTSUPP;
  10044. switch (arg1) {
  10045. case TARGET_GSI_IEEE_FP_CONTROL:
  10046. {
  10047. uint64_t fpcr = cpu_alpha_load_fpcr(cpu_env);
  10048. uint64_t swcr = ((CPUAlphaState *)cpu_env)->swcr;
  10049. swcr &= ~SWCR_STATUS_MASK;
  10050. swcr |= (fpcr >> 35) & SWCR_STATUS_MASK;
  10051. if (put_user_u64 (swcr, arg2))
  10052. return -TARGET_EFAULT;
  10053. ret = 0;
  10054. }
  10055. break;
  10056. /* case GSI_IEEE_STATE_AT_SIGNAL:
  10057. -- Not implemented in linux kernel.
  10058. case GSI_UACPROC:
  10059. -- Retrieves current unaligned access state; not much used.
  10060. case GSI_PROC_TYPE:
  10061. -- Retrieves implver information; surely not used.
  10062. case GSI_GET_HWRPB:
  10063. -- Grabs a copy of the HWRPB; surely not used.
  10064. */
  10065. }
  10066. return ret;
  10067. #endif
  10068. #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
  10069. /* Alpha specific */
  10070. case TARGET_NR_osf_setsysinfo:
  10071. ret = -TARGET_EOPNOTSUPP;
  10072. switch (arg1) {
  10073. case TARGET_SSI_IEEE_FP_CONTROL:
  10074. {
  10075. uint64_t swcr, fpcr;
  10076. if (get_user_u64 (swcr, arg2)) {
  10077. return -TARGET_EFAULT;
  10078. }
  10079. /*
  10080. * The kernel calls swcr_update_status to update the
  10081. * status bits from the fpcr at every point that it
  10082. * could be queried. Therefore, we store the status
  10083. * bits only in FPCR.
  10084. */
  10085. ((CPUAlphaState *)cpu_env)->swcr
  10086. = swcr & (SWCR_TRAP_ENABLE_MASK | SWCR_MAP_MASK);
  10087. fpcr = cpu_alpha_load_fpcr(cpu_env);
  10088. fpcr &= ((uint64_t)FPCR_DYN_MASK << 32);
  10089. fpcr |= alpha_ieee_swcr_to_fpcr(swcr);
  10090. cpu_alpha_store_fpcr(cpu_env, fpcr);
  10091. ret = 0;
  10092. }
  10093. break;
  10094. case TARGET_SSI_IEEE_RAISE_EXCEPTION:
  10095. {
  10096. uint64_t exc, fpcr, fex;
  10097. if (get_user_u64(exc, arg2)) {
  10098. return -TARGET_EFAULT;
  10099. }
  10100. exc &= SWCR_STATUS_MASK;
  10101. fpcr = cpu_alpha_load_fpcr(cpu_env);
  10102. /* Old exceptions are not signaled. */
  10103. fex = alpha_ieee_fpcr_to_swcr(fpcr);
  10104. fex = exc & ~fex;
  10105. fex >>= SWCR_STATUS_TO_EXCSUM_SHIFT;
  10106. fex &= ((CPUArchState *)cpu_env)->swcr;
  10107. /* Update the hardware fpcr. */
  10108. fpcr |= alpha_ieee_swcr_to_fpcr(exc);
  10109. cpu_alpha_store_fpcr(cpu_env, fpcr);
  10110. if (fex) {
  10111. int si_code = TARGET_FPE_FLTUNK;
  10112. target_siginfo_t info;
  10113. if (fex & SWCR_TRAP_ENABLE_DNO) {
  10114. si_code = TARGET_FPE_FLTUND;
  10115. }
  10116. if (fex & SWCR_TRAP_ENABLE_INE) {
  10117. si_code = TARGET_FPE_FLTRES;
  10118. }
  10119. if (fex & SWCR_TRAP_ENABLE_UNF) {
  10120. si_code = TARGET_FPE_FLTUND;
  10121. }
  10122. if (fex & SWCR_TRAP_ENABLE_OVF) {
  10123. si_code = TARGET_FPE_FLTOVF;
  10124. }
  10125. if (fex & SWCR_TRAP_ENABLE_DZE) {
  10126. si_code = TARGET_FPE_FLTDIV;
  10127. }
  10128. if (fex & SWCR_TRAP_ENABLE_INV) {
  10129. si_code = TARGET_FPE_FLTINV;
  10130. }
  10131. info.si_signo = SIGFPE;
  10132. info.si_errno = 0;
  10133. info.si_code = si_code;
  10134. info._sifields._sigfault._addr
  10135. = ((CPUArchState *)cpu_env)->pc;
  10136. queue_signal((CPUArchState *)cpu_env, info.si_signo,
  10137. QEMU_SI_FAULT, &info);
  10138. }
  10139. ret = 0;
  10140. }
  10141. break;
  10142. /* case SSI_NVPAIRS:
  10143. -- Used with SSIN_UACPROC to enable unaligned accesses.
  10144. case SSI_IEEE_STATE_AT_SIGNAL:
  10145. case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
  10146. -- Not implemented in linux kernel
  10147. */
  10148. }
  10149. return ret;
  10150. #endif
  10151. #ifdef TARGET_NR_osf_sigprocmask
  10152. /* Alpha specific. */
  10153. case TARGET_NR_osf_sigprocmask:
  10154. {
  10155. abi_ulong mask;
  10156. int how;
  10157. sigset_t set, oldset;
  10158. switch(arg1) {
  10159. case TARGET_SIG_BLOCK:
  10160. how = SIG_BLOCK;
  10161. break;
  10162. case TARGET_SIG_UNBLOCK:
  10163. how = SIG_UNBLOCK;
  10164. break;
  10165. case TARGET_SIG_SETMASK:
  10166. how = SIG_SETMASK;
  10167. break;
  10168. default:
  10169. return -TARGET_EINVAL;
  10170. }
  10171. mask = arg2;
  10172. target_to_host_old_sigset(&set, &mask);
  10173. ret = do_sigprocmask(how, &set, &oldset);
  10174. if (!ret) {
  10175. host_to_target_old_sigset(&mask, &oldset);
  10176. ret = mask;
  10177. }
  10178. }
  10179. return ret;
  10180. #endif
  10181. #ifdef TARGET_NR_getgid32
  10182. case TARGET_NR_getgid32:
  10183. return get_errno(getgid());
  10184. #endif
  10185. #ifdef TARGET_NR_geteuid32
  10186. case TARGET_NR_geteuid32:
  10187. return get_errno(geteuid());
  10188. #endif
  10189. #ifdef TARGET_NR_getegid32
  10190. case TARGET_NR_getegid32:
  10191. return get_errno(getegid());
  10192. #endif
  10193. #ifdef TARGET_NR_setreuid32
  10194. case TARGET_NR_setreuid32:
  10195. return get_errno(setreuid(arg1, arg2));
  10196. #endif
  10197. #ifdef TARGET_NR_setregid32
  10198. case TARGET_NR_setregid32:
  10199. return get_errno(setregid(arg1, arg2));
  10200. #endif
  10201. #ifdef TARGET_NR_getgroups32
  10202. case TARGET_NR_getgroups32:
  10203. {
  10204. int gidsetsize = arg1;
  10205. uint32_t *target_grouplist;
  10206. gid_t *grouplist;
  10207. int i;
  10208. grouplist = alloca(gidsetsize * sizeof(gid_t));
  10209. ret = get_errno(getgroups(gidsetsize, grouplist));
  10210. if (gidsetsize == 0)
  10211. return ret;
  10212. if (!is_error(ret)) {
  10213. target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
  10214. if (!target_grouplist) {
  10215. return -TARGET_EFAULT;
  10216. }
  10217. for(i = 0;i < ret; i++)
  10218. target_grouplist[i] = tswap32(grouplist[i]);
  10219. unlock_user(target_grouplist, arg2, gidsetsize * 4);
  10220. }
  10221. }
  10222. return ret;
  10223. #endif
  10224. #ifdef TARGET_NR_setgroups32
  10225. case TARGET_NR_setgroups32:
  10226. {
  10227. int gidsetsize = arg1;
  10228. uint32_t *target_grouplist;
  10229. gid_t *grouplist;
  10230. int i;
  10231. grouplist = alloca(gidsetsize * sizeof(gid_t));
  10232. target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
  10233. if (!target_grouplist) {
  10234. return -TARGET_EFAULT;
  10235. }
  10236. for(i = 0;i < gidsetsize; i++)
  10237. grouplist[i] = tswap32(target_grouplist[i]);
  10238. unlock_user(target_grouplist, arg2, 0);
  10239. return get_errno(setgroups(gidsetsize, grouplist));
  10240. }
  10241. #endif
  10242. #ifdef TARGET_NR_fchown32
  10243. case TARGET_NR_fchown32:
  10244. return get_errno(fchown(arg1, arg2, arg3));
  10245. #endif
  10246. #ifdef TARGET_NR_setresuid32
  10247. case TARGET_NR_setresuid32:
  10248. return get_errno(sys_setresuid(arg1, arg2, arg3));
  10249. #endif
  10250. #ifdef TARGET_NR_getresuid32
  10251. case TARGET_NR_getresuid32:
  10252. {
  10253. uid_t ruid, euid, suid;
  10254. ret = get_errno(getresuid(&ruid, &euid, &suid));
  10255. if (!is_error(ret)) {
  10256. if (put_user_u32(ruid, arg1)
  10257. || put_user_u32(euid, arg2)
  10258. || put_user_u32(suid, arg3))
  10259. return -TARGET_EFAULT;
  10260. }
  10261. }
  10262. return ret;
  10263. #endif
  10264. #ifdef TARGET_NR_setresgid32
  10265. case TARGET_NR_setresgid32:
  10266. return get_errno(sys_setresgid(arg1, arg2, arg3));
  10267. #endif
  10268. #ifdef TARGET_NR_getresgid32
  10269. case TARGET_NR_getresgid32:
  10270. {
  10271. gid_t rgid, egid, sgid;
  10272. ret = get_errno(getresgid(&rgid, &egid, &sgid));
  10273. if (!is_error(ret)) {
  10274. if (put_user_u32(rgid, arg1)
  10275. || put_user_u32(egid, arg2)
  10276. || put_user_u32(sgid, arg3))
  10277. return -TARGET_EFAULT;
  10278. }
  10279. }
  10280. return ret;
  10281. #endif
  10282. #ifdef TARGET_NR_chown32
  10283. case TARGET_NR_chown32:
  10284. if (!(p = lock_user_string(arg1)))
  10285. return -TARGET_EFAULT;
  10286. ret = get_errno(chown(p, arg2, arg3));
  10287. unlock_user(p, arg1, 0);
  10288. return ret;
  10289. #endif
  10290. #ifdef TARGET_NR_setuid32
  10291. case TARGET_NR_setuid32:
  10292. return get_errno(sys_setuid(arg1));
  10293. #endif
  10294. #ifdef TARGET_NR_setgid32
  10295. case TARGET_NR_setgid32:
  10296. return get_errno(sys_setgid(arg1));
  10297. #endif
  10298. #ifdef TARGET_NR_setfsuid32
  10299. case TARGET_NR_setfsuid32:
  10300. return get_errno(setfsuid(arg1));
  10301. #endif
  10302. #ifdef TARGET_NR_setfsgid32
  10303. case TARGET_NR_setfsgid32:
  10304. return get_errno(setfsgid(arg1));
  10305. #endif
  10306. #ifdef TARGET_NR_mincore
  10307. case TARGET_NR_mincore:
  10308. {
  10309. void *a = lock_user(VERIFY_READ, arg1, arg2, 0);
  10310. if (!a) {
  10311. return -TARGET_ENOMEM;
  10312. }
  10313. p = lock_user_string(arg3);
  10314. if (!p) {
  10315. ret = -TARGET_EFAULT;
  10316. } else {
  10317. ret = get_errno(mincore(a, arg2, p));
  10318. unlock_user(p, arg3, ret);
  10319. }
  10320. unlock_user(a, arg1, 0);
  10321. }
  10322. return ret;
  10323. #endif
  10324. #ifdef TARGET_NR_arm_fadvise64_64
  10325. case TARGET_NR_arm_fadvise64_64:
  10326. /* arm_fadvise64_64 looks like fadvise64_64 but
  10327. * with different argument order: fd, advice, offset, len
  10328. * rather than the usual fd, offset, len, advice.
  10329. * Note that offset and len are both 64-bit so appear as
  10330. * pairs of 32-bit registers.
  10331. */
  10332. ret = posix_fadvise(arg1, target_offset64(arg3, arg4),
  10333. target_offset64(arg5, arg6), arg2);
  10334. return -host_to_target_errno(ret);
  10335. #endif
  10336. #if TARGET_ABI_BITS == 32
  10337. #ifdef TARGET_NR_fadvise64_64
  10338. case TARGET_NR_fadvise64_64:
  10339. #if defined(TARGET_PPC) || defined(TARGET_XTENSA)
  10340. /* 6 args: fd, advice, offset (high, low), len (high, low) */
  10341. ret = arg2;
  10342. arg2 = arg3;
  10343. arg3 = arg4;
  10344. arg4 = arg5;
  10345. arg5 = arg6;
  10346. arg6 = ret;
  10347. #else
  10348. /* 6 args: fd, offset (high, low), len (high, low), advice */
  10349. if (regpairs_aligned(cpu_env, num)) {
  10350. /* offset is in (3,4), len in (5,6) and advice in 7 */
  10351. arg2 = arg3;
  10352. arg3 = arg4;
  10353. arg4 = arg5;
  10354. arg5 = arg6;
  10355. arg6 = arg7;
  10356. }
  10357. #endif
  10358. ret = posix_fadvise(arg1, target_offset64(arg2, arg3),
  10359. target_offset64(arg4, arg5), arg6);
  10360. return -host_to_target_errno(ret);
  10361. #endif
  10362. #ifdef TARGET_NR_fadvise64
  10363. case TARGET_NR_fadvise64:
  10364. /* 5 args: fd, offset (high, low), len, advice */
  10365. if (regpairs_aligned(cpu_env, num)) {
  10366. /* offset is in (3,4), len in 5 and advice in 6 */
  10367. arg2 = arg3;
  10368. arg3 = arg4;
  10369. arg4 = arg5;
  10370. arg5 = arg6;
  10371. }
  10372. ret = posix_fadvise(arg1, target_offset64(arg2, arg3), arg4, arg5);
  10373. return -host_to_target_errno(ret);
  10374. #endif
  10375. #else /* not a 32-bit ABI */
  10376. #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_fadvise64)
  10377. #ifdef TARGET_NR_fadvise64_64
  10378. case TARGET_NR_fadvise64_64:
  10379. #endif
  10380. #ifdef TARGET_NR_fadvise64
  10381. case TARGET_NR_fadvise64:
  10382. #endif
  10383. #ifdef TARGET_S390X
  10384. switch (arg4) {
  10385. case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
  10386. case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
  10387. case 6: arg4 = POSIX_FADV_DONTNEED; break;
  10388. case 7: arg4 = POSIX_FADV_NOREUSE; break;
  10389. default: break;
  10390. }
  10391. #endif
  10392. return -host_to_target_errno(posix_fadvise(arg1, arg2, arg3, arg4));
  10393. #endif
  10394. #endif /* end of 64-bit ABI fadvise handling */
  10395. #ifdef TARGET_NR_madvise
  10396. case TARGET_NR_madvise:
  10397. /* A straight passthrough may not be safe because qemu sometimes
  10398. turns private file-backed mappings into anonymous mappings.
  10399. This will break MADV_DONTNEED.
  10400. This is a hint, so ignoring and returning success is ok. */
  10401. return 0;
  10402. #endif
  10403. #ifdef TARGET_NR_fcntl64
  10404. case TARGET_NR_fcntl64:
  10405. {
  10406. int cmd;
  10407. struct flock64 fl;
  10408. from_flock64_fn *copyfrom = copy_from_user_flock64;
  10409. to_flock64_fn *copyto = copy_to_user_flock64;
  10410. #ifdef TARGET_ARM
  10411. if (!((CPUARMState *)cpu_env)->eabi) {
  10412. copyfrom = copy_from_user_oabi_flock64;
  10413. copyto = copy_to_user_oabi_flock64;
  10414. }
  10415. #endif
  10416. cmd = target_to_host_fcntl_cmd(arg2);
  10417. if (cmd == -TARGET_EINVAL) {
  10418. return cmd;
  10419. }
  10420. switch(arg2) {
  10421. case TARGET_F_GETLK64:
  10422. ret = copyfrom(&fl, arg3);
  10423. if (ret) {
  10424. break;
  10425. }
  10426. ret = get_errno(safe_fcntl(arg1, cmd, &fl));
  10427. if (ret == 0) {
  10428. ret = copyto(arg3, &fl);
  10429. }
  10430. break;
  10431. case TARGET_F_SETLK64:
  10432. case TARGET_F_SETLKW64:
  10433. ret = copyfrom(&fl, arg3);
  10434. if (ret) {
  10435. break;
  10436. }
  10437. ret = get_errno(safe_fcntl(arg1, cmd, &fl));
  10438. break;
  10439. default:
  10440. ret = do_fcntl(arg1, arg2, arg3);
  10441. break;
  10442. }
  10443. return ret;
  10444. }
  10445. #endif
  10446. #ifdef TARGET_NR_cacheflush
  10447. case TARGET_NR_cacheflush:
  10448. /* self-modifying code is handled automatically, so nothing needed */
  10449. return 0;
  10450. #endif
  10451. #ifdef TARGET_NR_getpagesize
  10452. case TARGET_NR_getpagesize:
  10453. return TARGET_PAGE_SIZE;
  10454. #endif
  10455. case TARGET_NR_gettid:
  10456. return get_errno(sys_gettid());
  10457. #ifdef TARGET_NR_readahead
  10458. case TARGET_NR_readahead:
  10459. #if TARGET_ABI_BITS == 32
  10460. if (regpairs_aligned(cpu_env, num)) {
  10461. arg2 = arg3;
  10462. arg3 = arg4;
  10463. arg4 = arg5;
  10464. }
  10465. ret = get_errno(readahead(arg1, target_offset64(arg2, arg3) , arg4));
  10466. #else
  10467. ret = get_errno(readahead(arg1, arg2, arg3));
  10468. #endif
  10469. return ret;
  10470. #endif
  10471. #ifdef CONFIG_ATTR
  10472. #ifdef TARGET_NR_setxattr
  10473. case TARGET_NR_listxattr:
  10474. case TARGET_NR_llistxattr:
  10475. {
  10476. void *p, *b = 0;
  10477. if (arg2) {
  10478. b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  10479. if (!b) {
  10480. return -TARGET_EFAULT;
  10481. }
  10482. }
  10483. p = lock_user_string(arg1);
  10484. if (p) {
  10485. if (num == TARGET_NR_listxattr) {
  10486. ret = get_errno(listxattr(p, b, arg3));
  10487. } else {
  10488. ret = get_errno(llistxattr(p, b, arg3));
  10489. }
  10490. } else {
  10491. ret = -TARGET_EFAULT;
  10492. }
  10493. unlock_user(p, arg1, 0);
  10494. unlock_user(b, arg2, arg3);
  10495. return ret;
  10496. }
  10497. case TARGET_NR_flistxattr:
  10498. {
  10499. void *b = 0;
  10500. if (arg2) {
  10501. b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  10502. if (!b) {
  10503. return -TARGET_EFAULT;
  10504. }
  10505. }
  10506. ret = get_errno(flistxattr(arg1, b, arg3));
  10507. unlock_user(b, arg2, arg3);
  10508. return ret;
  10509. }
  10510. case TARGET_NR_setxattr:
  10511. case TARGET_NR_lsetxattr:
  10512. {
  10513. void *p, *n, *v = 0;
  10514. if (arg3) {
  10515. v = lock_user(VERIFY_READ, arg3, arg4, 1);
  10516. if (!v) {
  10517. return -TARGET_EFAULT;
  10518. }
  10519. }
  10520. p = lock_user_string(arg1);
  10521. n = lock_user_string(arg2);
  10522. if (p && n) {
  10523. if (num == TARGET_NR_setxattr) {
  10524. ret = get_errno(setxattr(p, n, v, arg4, arg5));
  10525. } else {
  10526. ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
  10527. }
  10528. } else {
  10529. ret = -TARGET_EFAULT;
  10530. }
  10531. unlock_user(p, arg1, 0);
  10532. unlock_user(n, arg2, 0);
  10533. unlock_user(v, arg3, 0);
  10534. }
  10535. return ret;
  10536. case TARGET_NR_fsetxattr:
  10537. {
  10538. void *n, *v = 0;
  10539. if (arg3) {
  10540. v = lock_user(VERIFY_READ, arg3, arg4, 1);
  10541. if (!v) {
  10542. return -TARGET_EFAULT;
  10543. }
  10544. }
  10545. n = lock_user_string(arg2);
  10546. if (n) {
  10547. ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
  10548. } else {
  10549. ret = -TARGET_EFAULT;
  10550. }
  10551. unlock_user(n, arg2, 0);
  10552. unlock_user(v, arg3, 0);
  10553. }
  10554. return ret;
  10555. case TARGET_NR_getxattr:
  10556. case TARGET_NR_lgetxattr:
  10557. {
  10558. void *p, *n, *v = 0;
  10559. if (arg3) {
  10560. v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  10561. if (!v) {
  10562. return -TARGET_EFAULT;
  10563. }
  10564. }
  10565. p = lock_user_string(arg1);
  10566. n = lock_user_string(arg2);
  10567. if (p && n) {
  10568. if (num == TARGET_NR_getxattr) {
  10569. ret = get_errno(getxattr(p, n, v, arg4));
  10570. } else {
  10571. ret = get_errno(lgetxattr(p, n, v, arg4));
  10572. }
  10573. } else {
  10574. ret = -TARGET_EFAULT;
  10575. }
  10576. unlock_user(p, arg1, 0);
  10577. unlock_user(n, arg2, 0);
  10578. unlock_user(v, arg3, arg4);
  10579. }
  10580. return ret;
  10581. case TARGET_NR_fgetxattr:
  10582. {
  10583. void *n, *v = 0;
  10584. if (arg3) {
  10585. v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  10586. if (!v) {
  10587. return -TARGET_EFAULT;
  10588. }
  10589. }
  10590. n = lock_user_string(arg2);
  10591. if (n) {
  10592. ret = get_errno(fgetxattr(arg1, n, v, arg4));
  10593. } else {
  10594. ret = -TARGET_EFAULT;
  10595. }
  10596. unlock_user(n, arg2, 0);
  10597. unlock_user(v, arg3, arg4);
  10598. }
  10599. return ret;
  10600. case TARGET_NR_removexattr:
  10601. case TARGET_NR_lremovexattr:
  10602. {
  10603. void *p, *n;
  10604. p = lock_user_string(arg1);
  10605. n = lock_user_string(arg2);
  10606. if (p && n) {
  10607. if (num == TARGET_NR_removexattr) {
  10608. ret = get_errno(removexattr(p, n));
  10609. } else {
  10610. ret = get_errno(lremovexattr(p, n));
  10611. }
  10612. } else {
  10613. ret = -TARGET_EFAULT;
  10614. }
  10615. unlock_user(p, arg1, 0);
  10616. unlock_user(n, arg2, 0);
  10617. }
  10618. return ret;
  10619. case TARGET_NR_fremovexattr:
  10620. {
  10621. void *n;
  10622. n = lock_user_string(arg2);
  10623. if (n) {
  10624. ret = get_errno(fremovexattr(arg1, n));
  10625. } else {
  10626. ret = -TARGET_EFAULT;
  10627. }
  10628. unlock_user(n, arg2, 0);
  10629. }
  10630. return ret;
  10631. #endif
  10632. #endif /* CONFIG_ATTR */
  10633. #ifdef TARGET_NR_set_thread_area
  10634. case TARGET_NR_set_thread_area:
  10635. #if defined(TARGET_MIPS)
  10636. ((CPUMIPSState *) cpu_env)->active_tc.CP0_UserLocal = arg1;
  10637. return 0;
  10638. #elif defined(TARGET_CRIS)
  10639. if (arg1 & 0xff)
  10640. ret = -TARGET_EINVAL;
  10641. else {
  10642. ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
  10643. ret = 0;
  10644. }
  10645. return ret;
  10646. #elif defined(TARGET_I386) && defined(TARGET_ABI32)
  10647. return do_set_thread_area(cpu_env, arg1);
  10648. #elif defined(TARGET_M68K)
  10649. {
  10650. TaskState *ts = cpu->opaque;
  10651. ts->tp_value = arg1;
  10652. return 0;
  10653. }
  10654. #else
  10655. return -TARGET_ENOSYS;
  10656. #endif
  10657. #endif
  10658. #ifdef TARGET_NR_get_thread_area
  10659. case TARGET_NR_get_thread_area:
  10660. #if defined(TARGET_I386) && defined(TARGET_ABI32)
  10661. return do_get_thread_area(cpu_env, arg1);
  10662. #elif defined(TARGET_M68K)
  10663. {
  10664. TaskState *ts = cpu->opaque;
  10665. return ts->tp_value;
  10666. }
  10667. #else
  10668. return -TARGET_ENOSYS;
  10669. #endif
  10670. #endif
  10671. #ifdef TARGET_NR_getdomainname
  10672. case TARGET_NR_getdomainname:
  10673. return -TARGET_ENOSYS;
  10674. #endif
  10675. #ifdef TARGET_NR_clock_settime
  10676. case TARGET_NR_clock_settime:
  10677. {
  10678. struct timespec ts;
  10679. ret = target_to_host_timespec(&ts, arg2);
  10680. if (!is_error(ret)) {
  10681. ret = get_errno(clock_settime(arg1, &ts));
  10682. }
  10683. return ret;
  10684. }
  10685. #endif
  10686. #ifdef TARGET_NR_clock_settime64
  10687. case TARGET_NR_clock_settime64:
  10688. {
  10689. struct timespec ts;
  10690. ret = target_to_host_timespec64(&ts, arg2);
  10691. if (!is_error(ret)) {
  10692. ret = get_errno(clock_settime(arg1, &ts));
  10693. }
  10694. return ret;
  10695. }
  10696. #endif
  10697. #ifdef TARGET_NR_clock_gettime
  10698. case TARGET_NR_clock_gettime:
  10699. {
  10700. struct timespec ts;
  10701. ret = get_errno(clock_gettime(arg1, &ts));
  10702. if (!is_error(ret)) {
  10703. ret = host_to_target_timespec(arg2, &ts);
  10704. }
  10705. return ret;
  10706. }
  10707. #endif
  10708. #ifdef TARGET_NR_clock_gettime64
  10709. case TARGET_NR_clock_gettime64:
  10710. {
  10711. struct timespec ts;
  10712. ret = get_errno(clock_gettime(arg1, &ts));
  10713. if (!is_error(ret)) {
  10714. ret = host_to_target_timespec64(arg2, &ts);
  10715. }
  10716. return ret;
  10717. }
  10718. #endif
  10719. #ifdef TARGET_NR_clock_getres
  10720. case TARGET_NR_clock_getres:
  10721. {
  10722. struct timespec ts;
  10723. ret = get_errno(clock_getres(arg1, &ts));
  10724. if (!is_error(ret)) {
  10725. host_to_target_timespec(arg2, &ts);
  10726. }
  10727. return ret;
  10728. }
  10729. #endif
  10730. #ifdef TARGET_NR_clock_nanosleep
  10731. case TARGET_NR_clock_nanosleep:
  10732. {
  10733. struct timespec ts;
  10734. target_to_host_timespec(&ts, arg3);
  10735. ret = get_errno(safe_clock_nanosleep(arg1, arg2,
  10736. &ts, arg4 ? &ts : NULL));
  10737. if (arg4)
  10738. host_to_target_timespec(arg4, &ts);
  10739. #if defined(TARGET_PPC)
  10740. /* clock_nanosleep is odd in that it returns positive errno values.
  10741. * On PPC, CR0 bit 3 should be set in such a situation. */
  10742. if (ret && ret != -TARGET_ERESTARTSYS) {
  10743. ((CPUPPCState *)cpu_env)->crf[0] |= 1;
  10744. }
  10745. #endif
  10746. return ret;
  10747. }
  10748. #endif
  10749. #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
  10750. case TARGET_NR_set_tid_address:
  10751. return get_errno(set_tid_address((int *)g2h(arg1)));
  10752. #endif
  10753. case TARGET_NR_tkill:
  10754. return get_errno(safe_tkill((int)arg1, target_to_host_signal(arg2)));
  10755. case TARGET_NR_tgkill:
  10756. return get_errno(safe_tgkill((int)arg1, (int)arg2,
  10757. target_to_host_signal(arg3)));
  10758. #ifdef TARGET_NR_set_robust_list
  10759. case TARGET_NR_set_robust_list:
  10760. case TARGET_NR_get_robust_list:
  10761. /* The ABI for supporting robust futexes has userspace pass
  10762. * the kernel a pointer to a linked list which is updated by
  10763. * userspace after the syscall; the list is walked by the kernel
  10764. * when the thread exits. Since the linked list in QEMU guest
  10765. * memory isn't a valid linked list for the host and we have
  10766. * no way to reliably intercept the thread-death event, we can't
  10767. * support these. Silently return ENOSYS so that guest userspace
  10768. * falls back to a non-robust futex implementation (which should
  10769. * be OK except in the corner case of the guest crashing while
  10770. * holding a mutex that is shared with another process via
  10771. * shared memory).
  10772. */
  10773. return -TARGET_ENOSYS;
  10774. #endif
  10775. #if defined(TARGET_NR_utimensat)
  10776. case TARGET_NR_utimensat:
  10777. {
  10778. struct timespec *tsp, ts[2];
  10779. if (!arg3) {
  10780. tsp = NULL;
  10781. } else {
  10782. target_to_host_timespec(ts, arg3);
  10783. target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
  10784. tsp = ts;
  10785. }
  10786. if (!arg2)
  10787. ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
  10788. else {
  10789. if (!(p = lock_user_string(arg2))) {
  10790. return -TARGET_EFAULT;
  10791. }
  10792. ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
  10793. unlock_user(p, arg2, 0);
  10794. }
  10795. }
  10796. return ret;
  10797. #endif
  10798. #ifdef TARGET_NR_futex
  10799. case TARGET_NR_futex:
  10800. return do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
  10801. #endif
  10802. #ifdef TARGET_NR_futex_time64
  10803. case TARGET_NR_futex_time64:
  10804. return do_futex_time64(arg1, arg2, arg3, arg4, arg5, arg6);
  10805. #endif
  10806. #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
  10807. case TARGET_NR_inotify_init:
  10808. ret = get_errno(sys_inotify_init());
  10809. if (ret >= 0) {
  10810. fd_trans_register(ret, &target_inotify_trans);
  10811. }
  10812. return ret;
  10813. #endif
  10814. #ifdef CONFIG_INOTIFY1
  10815. #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
  10816. case TARGET_NR_inotify_init1:
  10817. ret = get_errno(sys_inotify_init1(target_to_host_bitmask(arg1,
  10818. fcntl_flags_tbl)));
  10819. if (ret >= 0) {
  10820. fd_trans_register(ret, &target_inotify_trans);
  10821. }
  10822. return ret;
  10823. #endif
  10824. #endif
  10825. #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
  10826. case TARGET_NR_inotify_add_watch:
  10827. p = lock_user_string(arg2);
  10828. ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
  10829. unlock_user(p, arg2, 0);
  10830. return ret;
  10831. #endif
  10832. #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
  10833. case TARGET_NR_inotify_rm_watch:
  10834. return get_errno(sys_inotify_rm_watch(arg1, arg2));
  10835. #endif
  10836. #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
  10837. case TARGET_NR_mq_open:
  10838. {
  10839. struct mq_attr posix_mq_attr;
  10840. struct mq_attr *pposix_mq_attr;
  10841. int host_flags;
  10842. host_flags = target_to_host_bitmask(arg2, fcntl_flags_tbl);
  10843. pposix_mq_attr = NULL;
  10844. if (arg4) {
  10845. if (copy_from_user_mq_attr(&posix_mq_attr, arg4) != 0) {
  10846. return -TARGET_EFAULT;
  10847. }
  10848. pposix_mq_attr = &posix_mq_attr;
  10849. }
  10850. p = lock_user_string(arg1 - 1);
  10851. if (!p) {
  10852. return -TARGET_EFAULT;
  10853. }
  10854. ret = get_errno(mq_open(p, host_flags, arg3, pposix_mq_attr));
  10855. unlock_user (p, arg1, 0);
  10856. }
  10857. return ret;
  10858. case TARGET_NR_mq_unlink:
  10859. p = lock_user_string(arg1 - 1);
  10860. if (!p) {
  10861. return -TARGET_EFAULT;
  10862. }
  10863. ret = get_errno(mq_unlink(p));
  10864. unlock_user (p, arg1, 0);
  10865. return ret;
  10866. #ifdef TARGET_NR_mq_timedsend
  10867. case TARGET_NR_mq_timedsend:
  10868. {
  10869. struct timespec ts;
  10870. p = lock_user (VERIFY_READ, arg2, arg3, 1);
  10871. if (arg5 != 0) {
  10872. target_to_host_timespec(&ts, arg5);
  10873. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts));
  10874. host_to_target_timespec(arg5, &ts);
  10875. } else {
  10876. ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL));
  10877. }
  10878. unlock_user (p, arg2, arg3);
  10879. }
  10880. return ret;
  10881. #endif
  10882. #ifdef TARGET_NR_mq_timedreceive
  10883. case TARGET_NR_mq_timedreceive:
  10884. {
  10885. struct timespec ts;
  10886. unsigned int prio;
  10887. p = lock_user (VERIFY_READ, arg2, arg3, 1);
  10888. if (arg5 != 0) {
  10889. target_to_host_timespec(&ts, arg5);
  10890. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  10891. &prio, &ts));
  10892. host_to_target_timespec(arg5, &ts);
  10893. } else {
  10894. ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
  10895. &prio, NULL));
  10896. }
  10897. unlock_user (p, arg2, arg3);
  10898. if (arg4 != 0)
  10899. put_user_u32(prio, arg4);
  10900. }
  10901. return ret;
  10902. #endif
  10903. /* Not implemented for now... */
  10904. /* case TARGET_NR_mq_notify: */
  10905. /* break; */
  10906. case TARGET_NR_mq_getsetattr:
  10907. {
  10908. struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
  10909. ret = 0;
  10910. if (arg2 != 0) {
  10911. copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
  10912. ret = get_errno(mq_setattr(arg1, &posix_mq_attr_in,
  10913. &posix_mq_attr_out));
  10914. } else if (arg3 != 0) {
  10915. ret = get_errno(mq_getattr(arg1, &posix_mq_attr_out));
  10916. }
  10917. if (ret == 0 && arg3 != 0) {
  10918. copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
  10919. }
  10920. }
  10921. return ret;
  10922. #endif
  10923. #ifdef CONFIG_SPLICE
  10924. #ifdef TARGET_NR_tee
  10925. case TARGET_NR_tee:
  10926. {
  10927. ret = get_errno(tee(arg1,arg2,arg3,arg4));
  10928. }
  10929. return ret;
  10930. #endif
  10931. #ifdef TARGET_NR_splice
  10932. case TARGET_NR_splice:
  10933. {
  10934. loff_t loff_in, loff_out;
  10935. loff_t *ploff_in = NULL, *ploff_out = NULL;
  10936. if (arg2) {
  10937. if (get_user_u64(loff_in, arg2)) {
  10938. return -TARGET_EFAULT;
  10939. }
  10940. ploff_in = &loff_in;
  10941. }
  10942. if (arg4) {
  10943. if (get_user_u64(loff_out, arg4)) {
  10944. return -TARGET_EFAULT;
  10945. }
  10946. ploff_out = &loff_out;
  10947. }
  10948. ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
  10949. if (arg2) {
  10950. if (put_user_u64(loff_in, arg2)) {
  10951. return -TARGET_EFAULT;
  10952. }
  10953. }
  10954. if (arg4) {
  10955. if (put_user_u64(loff_out, arg4)) {
  10956. return -TARGET_EFAULT;
  10957. }
  10958. }
  10959. }
  10960. return ret;
  10961. #endif
  10962. #ifdef TARGET_NR_vmsplice
  10963. case TARGET_NR_vmsplice:
  10964. {
  10965. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  10966. if (vec != NULL) {
  10967. ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
  10968. unlock_iovec(vec, arg2, arg3, 0);
  10969. } else {
  10970. ret = -host_to_target_errno(errno);
  10971. }
  10972. }
  10973. return ret;
  10974. #endif
  10975. #endif /* CONFIG_SPLICE */
  10976. #ifdef CONFIG_EVENTFD
  10977. #if defined(TARGET_NR_eventfd)
  10978. case TARGET_NR_eventfd:
  10979. ret = get_errno(eventfd(arg1, 0));
  10980. if (ret >= 0) {
  10981. fd_trans_register(ret, &target_eventfd_trans);
  10982. }
  10983. return ret;
  10984. #endif
  10985. #if defined(TARGET_NR_eventfd2)
  10986. case TARGET_NR_eventfd2:
  10987. {
  10988. int host_flags = arg2 & (~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC));
  10989. if (arg2 & TARGET_O_NONBLOCK) {
  10990. host_flags |= O_NONBLOCK;
  10991. }
  10992. if (arg2 & TARGET_O_CLOEXEC) {
  10993. host_flags |= O_CLOEXEC;
  10994. }
  10995. ret = get_errno(eventfd(arg1, host_flags));
  10996. if (ret >= 0) {
  10997. fd_trans_register(ret, &target_eventfd_trans);
  10998. }
  10999. return ret;
  11000. }
  11001. #endif
  11002. #endif /* CONFIG_EVENTFD */
  11003. #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
  11004. case TARGET_NR_fallocate:
  11005. #if TARGET_ABI_BITS == 32
  11006. ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
  11007. target_offset64(arg5, arg6)));
  11008. #else
  11009. ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
  11010. #endif
  11011. return ret;
  11012. #endif
  11013. #if defined(CONFIG_SYNC_FILE_RANGE)
  11014. #if defined(TARGET_NR_sync_file_range)
  11015. case TARGET_NR_sync_file_range:
  11016. #if TARGET_ABI_BITS == 32
  11017. #if defined(TARGET_MIPS)
  11018. ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
  11019. target_offset64(arg5, arg6), arg7));
  11020. #else
  11021. ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
  11022. target_offset64(arg4, arg5), arg6));
  11023. #endif /* !TARGET_MIPS */
  11024. #else
  11025. ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
  11026. #endif
  11027. return ret;
  11028. #endif
  11029. #if defined(TARGET_NR_sync_file_range2) || \
  11030. defined(TARGET_NR_arm_sync_file_range)
  11031. #if defined(TARGET_NR_sync_file_range2)
  11032. case TARGET_NR_sync_file_range2:
  11033. #endif
  11034. #if defined(TARGET_NR_arm_sync_file_range)
  11035. case TARGET_NR_arm_sync_file_range:
  11036. #endif
  11037. /* This is like sync_file_range but the arguments are reordered */
  11038. #if TARGET_ABI_BITS == 32
  11039. ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
  11040. target_offset64(arg5, arg6), arg2));
  11041. #else
  11042. ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
  11043. #endif
  11044. return ret;
  11045. #endif
  11046. #endif
  11047. #if defined(TARGET_NR_signalfd4)
  11048. case TARGET_NR_signalfd4:
  11049. return do_signalfd4(arg1, arg2, arg4);
  11050. #endif
  11051. #if defined(TARGET_NR_signalfd)
  11052. case TARGET_NR_signalfd:
  11053. return do_signalfd4(arg1, arg2, 0);
  11054. #endif
  11055. #if defined(CONFIG_EPOLL)
  11056. #if defined(TARGET_NR_epoll_create)
  11057. case TARGET_NR_epoll_create:
  11058. return get_errno(epoll_create(arg1));
  11059. #endif
  11060. #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
  11061. case TARGET_NR_epoll_create1:
  11062. return get_errno(epoll_create1(target_to_host_bitmask(arg1, fcntl_flags_tbl)));
  11063. #endif
  11064. #if defined(TARGET_NR_epoll_ctl)
  11065. case TARGET_NR_epoll_ctl:
  11066. {
  11067. struct epoll_event ep;
  11068. struct epoll_event *epp = 0;
  11069. if (arg4) {
  11070. struct target_epoll_event *target_ep;
  11071. if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
  11072. return -TARGET_EFAULT;
  11073. }
  11074. ep.events = tswap32(target_ep->events);
  11075. /* The epoll_data_t union is just opaque data to the kernel,
  11076. * so we transfer all 64 bits across and need not worry what
  11077. * actual data type it is.
  11078. */
  11079. ep.data.u64 = tswap64(target_ep->data.u64);
  11080. unlock_user_struct(target_ep, arg4, 0);
  11081. epp = &ep;
  11082. }
  11083. return get_errno(epoll_ctl(arg1, arg2, arg3, epp));
  11084. }
  11085. #endif
  11086. #if defined(TARGET_NR_epoll_wait) || defined(TARGET_NR_epoll_pwait)
  11087. #if defined(TARGET_NR_epoll_wait)
  11088. case TARGET_NR_epoll_wait:
  11089. #endif
  11090. #if defined(TARGET_NR_epoll_pwait)
  11091. case TARGET_NR_epoll_pwait:
  11092. #endif
  11093. {
  11094. struct target_epoll_event *target_ep;
  11095. struct epoll_event *ep;
  11096. int epfd = arg1;
  11097. int maxevents = arg3;
  11098. int timeout = arg4;
  11099. if (maxevents <= 0 || maxevents > TARGET_EP_MAX_EVENTS) {
  11100. return -TARGET_EINVAL;
  11101. }
  11102. target_ep = lock_user(VERIFY_WRITE, arg2,
  11103. maxevents * sizeof(struct target_epoll_event), 1);
  11104. if (!target_ep) {
  11105. return -TARGET_EFAULT;
  11106. }
  11107. ep = g_try_new(struct epoll_event, maxevents);
  11108. if (!ep) {
  11109. unlock_user(target_ep, arg2, 0);
  11110. return -TARGET_ENOMEM;
  11111. }
  11112. switch (num) {
  11113. #if defined(TARGET_NR_epoll_pwait)
  11114. case TARGET_NR_epoll_pwait:
  11115. {
  11116. target_sigset_t *target_set;
  11117. sigset_t _set, *set = &_set;
  11118. if (arg5) {
  11119. if (arg6 != sizeof(target_sigset_t)) {
  11120. ret = -TARGET_EINVAL;
  11121. break;
  11122. }
  11123. target_set = lock_user(VERIFY_READ, arg5,
  11124. sizeof(target_sigset_t), 1);
  11125. if (!target_set) {
  11126. ret = -TARGET_EFAULT;
  11127. break;
  11128. }
  11129. target_to_host_sigset(set, target_set);
  11130. unlock_user(target_set, arg5, 0);
  11131. } else {
  11132. set = NULL;
  11133. }
  11134. ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
  11135. set, SIGSET_T_SIZE));
  11136. break;
  11137. }
  11138. #endif
  11139. #if defined(TARGET_NR_epoll_wait)
  11140. case TARGET_NR_epoll_wait:
  11141. ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
  11142. NULL, 0));
  11143. break;
  11144. #endif
  11145. default:
  11146. ret = -TARGET_ENOSYS;
  11147. }
  11148. if (!is_error(ret)) {
  11149. int i;
  11150. for (i = 0; i < ret; i++) {
  11151. target_ep[i].events = tswap32(ep[i].events);
  11152. target_ep[i].data.u64 = tswap64(ep[i].data.u64);
  11153. }
  11154. unlock_user(target_ep, arg2,
  11155. ret * sizeof(struct target_epoll_event));
  11156. } else {
  11157. unlock_user(target_ep, arg2, 0);
  11158. }
  11159. g_free(ep);
  11160. return ret;
  11161. }
  11162. #endif
  11163. #endif
  11164. #ifdef TARGET_NR_prlimit64
  11165. case TARGET_NR_prlimit64:
  11166. {
  11167. /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
  11168. struct target_rlimit64 *target_rnew, *target_rold;
  11169. struct host_rlimit64 rnew, rold, *rnewp = 0;
  11170. int resource = target_to_host_resource(arg2);
  11171. if (arg3 && (resource != RLIMIT_AS &&
  11172. resource != RLIMIT_DATA &&
  11173. resource != RLIMIT_STACK)) {
  11174. if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
  11175. return -TARGET_EFAULT;
  11176. }
  11177. rnew.rlim_cur = tswap64(target_rnew->rlim_cur);
  11178. rnew.rlim_max = tswap64(target_rnew->rlim_max);
  11179. unlock_user_struct(target_rnew, arg3, 0);
  11180. rnewp = &rnew;
  11181. }
  11182. ret = get_errno(sys_prlimit64(arg1, resource, rnewp, arg4 ? &rold : 0));
  11183. if (!is_error(ret) && arg4) {
  11184. if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
  11185. return -TARGET_EFAULT;
  11186. }
  11187. target_rold->rlim_cur = tswap64(rold.rlim_cur);
  11188. target_rold->rlim_max = tswap64(rold.rlim_max);
  11189. unlock_user_struct(target_rold, arg4, 1);
  11190. }
  11191. return ret;
  11192. }
  11193. #endif
  11194. #ifdef TARGET_NR_gethostname
  11195. case TARGET_NR_gethostname:
  11196. {
  11197. char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
  11198. if (name) {
  11199. ret = get_errno(gethostname(name, arg2));
  11200. unlock_user(name, arg1, arg2);
  11201. } else {
  11202. ret = -TARGET_EFAULT;
  11203. }
  11204. return ret;
  11205. }
  11206. #endif
  11207. #ifdef TARGET_NR_atomic_cmpxchg_32
  11208. case TARGET_NR_atomic_cmpxchg_32:
  11209. {
  11210. /* should use start_exclusive from main.c */
  11211. abi_ulong mem_value;
  11212. if (get_user_u32(mem_value, arg6)) {
  11213. target_siginfo_t info;
  11214. info.si_signo = SIGSEGV;
  11215. info.si_errno = 0;
  11216. info.si_code = TARGET_SEGV_MAPERR;
  11217. info._sifields._sigfault._addr = arg6;
  11218. queue_signal((CPUArchState *)cpu_env, info.si_signo,
  11219. QEMU_SI_FAULT, &info);
  11220. ret = 0xdeadbeef;
  11221. }
  11222. if (mem_value == arg2)
  11223. put_user_u32(arg1, arg6);
  11224. return mem_value;
  11225. }
  11226. #endif
  11227. #ifdef TARGET_NR_atomic_barrier
  11228. case TARGET_NR_atomic_barrier:
  11229. /* Like the kernel implementation and the
  11230. qemu arm barrier, no-op this? */
  11231. return 0;
  11232. #endif
  11233. #ifdef TARGET_NR_timer_create
  11234. case TARGET_NR_timer_create:
  11235. {
  11236. /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
  11237. struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL;
  11238. int clkid = arg1;
  11239. int timer_index = next_free_host_timer();
  11240. if (timer_index < 0) {
  11241. ret = -TARGET_EAGAIN;
  11242. } else {
  11243. timer_t *phtimer = g_posix_timers + timer_index;
  11244. if (arg2) {
  11245. phost_sevp = &host_sevp;
  11246. ret = target_to_host_sigevent(phost_sevp, arg2);
  11247. if (ret != 0) {
  11248. return ret;
  11249. }
  11250. }
  11251. ret = get_errno(timer_create(clkid, phost_sevp, phtimer));
  11252. if (ret) {
  11253. phtimer = NULL;
  11254. } else {
  11255. if (put_user(TIMER_MAGIC | timer_index, arg3, target_timer_t)) {
  11256. return -TARGET_EFAULT;
  11257. }
  11258. }
  11259. }
  11260. return ret;
  11261. }
  11262. #endif
  11263. #ifdef TARGET_NR_timer_settime
  11264. case TARGET_NR_timer_settime:
  11265. {
  11266. /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
  11267. * struct itimerspec * old_value */
  11268. target_timer_t timerid = get_timer_id(arg1);
  11269. if (timerid < 0) {
  11270. ret = timerid;
  11271. } else if (arg3 == 0) {
  11272. ret = -TARGET_EINVAL;
  11273. } else {
  11274. timer_t htimer = g_posix_timers[timerid];
  11275. struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
  11276. if (target_to_host_itimerspec(&hspec_new, arg3)) {
  11277. return -TARGET_EFAULT;
  11278. }
  11279. ret = get_errno(
  11280. timer_settime(htimer, arg2, &hspec_new, &hspec_old));
  11281. if (arg4 && host_to_target_itimerspec(arg4, &hspec_old)) {
  11282. return -TARGET_EFAULT;
  11283. }
  11284. }
  11285. return ret;
  11286. }
  11287. #endif
  11288. #ifdef TARGET_NR_timer_gettime
  11289. case TARGET_NR_timer_gettime:
  11290. {
  11291. /* args: timer_t timerid, struct itimerspec *curr_value */
  11292. target_timer_t timerid = get_timer_id(arg1);
  11293. if (timerid < 0) {
  11294. ret = timerid;
  11295. } else if (!arg2) {
  11296. ret = -TARGET_EFAULT;
  11297. } else {
  11298. timer_t htimer = g_posix_timers[timerid];
  11299. struct itimerspec hspec;
  11300. ret = get_errno(timer_gettime(htimer, &hspec));
  11301. if (host_to_target_itimerspec(arg2, &hspec)) {
  11302. ret = -TARGET_EFAULT;
  11303. }
  11304. }
  11305. return ret;
  11306. }
  11307. #endif
  11308. #ifdef TARGET_NR_timer_getoverrun
  11309. case TARGET_NR_timer_getoverrun:
  11310. {
  11311. /* args: timer_t timerid */
  11312. target_timer_t timerid = get_timer_id(arg1);
  11313. if (timerid < 0) {
  11314. ret = timerid;
  11315. } else {
  11316. timer_t htimer = g_posix_timers[timerid];
  11317. ret = get_errno(timer_getoverrun(htimer));
  11318. }
  11319. return ret;
  11320. }
  11321. #endif
  11322. #ifdef TARGET_NR_timer_delete
  11323. case TARGET_NR_timer_delete:
  11324. {
  11325. /* args: timer_t timerid */
  11326. target_timer_t timerid = get_timer_id(arg1);
  11327. if (timerid < 0) {
  11328. ret = timerid;
  11329. } else {
  11330. timer_t htimer = g_posix_timers[timerid];
  11331. ret = get_errno(timer_delete(htimer));
  11332. g_posix_timers[timerid] = 0;
  11333. }
  11334. return ret;
  11335. }
  11336. #endif
  11337. #if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
  11338. case TARGET_NR_timerfd_create:
  11339. return get_errno(timerfd_create(arg1,
  11340. target_to_host_bitmask(arg2, fcntl_flags_tbl)));
  11341. #endif
  11342. #if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
  11343. case TARGET_NR_timerfd_gettime:
  11344. {
  11345. struct itimerspec its_curr;
  11346. ret = get_errno(timerfd_gettime(arg1, &its_curr));
  11347. if (arg2 && host_to_target_itimerspec(arg2, &its_curr)) {
  11348. return -TARGET_EFAULT;
  11349. }
  11350. }
  11351. return ret;
  11352. #endif
  11353. #if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
  11354. case TARGET_NR_timerfd_settime:
  11355. {
  11356. struct itimerspec its_new, its_old, *p_new;
  11357. if (arg3) {
  11358. if (target_to_host_itimerspec(&its_new, arg3)) {
  11359. return -TARGET_EFAULT;
  11360. }
  11361. p_new = &its_new;
  11362. } else {
  11363. p_new = NULL;
  11364. }
  11365. ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
  11366. if (arg4 && host_to_target_itimerspec(arg4, &its_old)) {
  11367. return -TARGET_EFAULT;
  11368. }
  11369. }
  11370. return ret;
  11371. #endif
  11372. #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
  11373. case TARGET_NR_ioprio_get:
  11374. return get_errno(ioprio_get(arg1, arg2));
  11375. #endif
  11376. #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
  11377. case TARGET_NR_ioprio_set:
  11378. return get_errno(ioprio_set(arg1, arg2, arg3));
  11379. #endif
  11380. #if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
  11381. case TARGET_NR_setns:
  11382. return get_errno(setns(arg1, arg2));
  11383. #endif
  11384. #if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
  11385. case TARGET_NR_unshare:
  11386. return get_errno(unshare(arg1));
  11387. #endif
  11388. #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
  11389. case TARGET_NR_kcmp:
  11390. return get_errno(kcmp(arg1, arg2, arg3, arg4, arg5));
  11391. #endif
  11392. #ifdef TARGET_NR_swapcontext
  11393. case TARGET_NR_swapcontext:
  11394. /* PowerPC specific. */
  11395. return do_swapcontext(cpu_env, arg1, arg2, arg3);
  11396. #endif
  11397. #ifdef TARGET_NR_memfd_create
  11398. case TARGET_NR_memfd_create:
  11399. p = lock_user_string(arg1);
  11400. if (!p) {
  11401. return -TARGET_EFAULT;
  11402. }
  11403. ret = get_errno(memfd_create(p, arg2));
  11404. fd_trans_unregister(ret);
  11405. unlock_user(p, arg1, 0);
  11406. return ret;
  11407. #endif
  11408. #if defined TARGET_NR_membarrier && defined __NR_membarrier
  11409. case TARGET_NR_membarrier:
  11410. return get_errno(membarrier(arg1, arg2));
  11411. #endif
  11412. default:
  11413. qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num);
  11414. return -TARGET_ENOSYS;
  11415. }
  11416. return ret;
  11417. }
  11418. abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
  11419. abi_long arg2, abi_long arg3, abi_long arg4,
  11420. abi_long arg5, abi_long arg6, abi_long arg7,
  11421. abi_long arg8)
  11422. {
  11423. CPUState *cpu = env_cpu(cpu_env);
  11424. abi_long ret;
  11425. #ifdef DEBUG_ERESTARTSYS
  11426. /* Debug-only code for exercising the syscall-restart code paths
  11427. * in the per-architecture cpu main loops: restart every syscall
  11428. * the guest makes once before letting it through.
  11429. */
  11430. {
  11431. static bool flag;
  11432. flag = !flag;
  11433. if (flag) {
  11434. return -TARGET_ERESTARTSYS;
  11435. }
  11436. }
  11437. #endif
  11438. record_syscall_start(cpu, num, arg1,
  11439. arg2, arg3, arg4, arg5, arg6, arg7, arg8);
  11440. if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
  11441. print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
  11442. }
  11443. ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4,
  11444. arg5, arg6, arg7, arg8);
  11445. if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
  11446. print_syscall_ret(num, ret);
  11447. }
  11448. record_syscall_return(cpu, num, ret);
  11449. return ret;
  11450. }