2
0

syscall.c 293 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418
  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 <stdlib.h>
  21. #include <stdio.h>
  22. #include <stdarg.h>
  23. #include <string.h>
  24. #include <elf.h>
  25. #include <endian.h>
  26. #include <errno.h>
  27. #include <unistd.h>
  28. #include <fcntl.h>
  29. #include <time.h>
  30. #include <limits.h>
  31. #include <grp.h>
  32. #include <sys/types.h>
  33. #include <sys/ipc.h>
  34. #include <sys/msg.h>
  35. #include <sys/wait.h>
  36. #include <sys/time.h>
  37. #include <sys/stat.h>
  38. #include <sys/mount.h>
  39. #include <sys/file.h>
  40. #include <sys/fsuid.h>
  41. #include <sys/personality.h>
  42. #include <sys/prctl.h>
  43. #include <sys/resource.h>
  44. #include <sys/mman.h>
  45. #include <sys/swap.h>
  46. #include <linux/capability.h>
  47. #include <signal.h>
  48. #include <sched.h>
  49. #ifdef __ia64__
  50. int __clone2(int (*fn)(void *), void *child_stack_base,
  51. size_t stack_size, int flags, void *arg, ...);
  52. #endif
  53. #include <sys/socket.h>
  54. #include <sys/un.h>
  55. #include <sys/uio.h>
  56. #include <sys/poll.h>
  57. #include <sys/times.h>
  58. #include <sys/shm.h>
  59. #include <sys/sem.h>
  60. #include <sys/statfs.h>
  61. #include <utime.h>
  62. #include <sys/sysinfo.h>
  63. #include <sys/utsname.h>
  64. //#include <sys/user.h>
  65. #include <netinet/ip.h>
  66. #include <netinet/tcp.h>
  67. #include <linux/wireless.h>
  68. #include <linux/icmp.h>
  69. #include "qemu-common.h"
  70. #ifdef TARGET_GPROF
  71. #include <sys/gmon.h>
  72. #endif
  73. #ifdef CONFIG_EVENTFD
  74. #include <sys/eventfd.h>
  75. #endif
  76. #ifdef CONFIG_EPOLL
  77. #include <sys/epoll.h>
  78. #endif
  79. #ifdef CONFIG_ATTR
  80. #include "qemu/xattr.h"
  81. #endif
  82. #ifdef CONFIG_SENDFILE
  83. #include <sys/sendfile.h>
  84. #endif
  85. #define termios host_termios
  86. #define winsize host_winsize
  87. #define termio host_termio
  88. #define sgttyb host_sgttyb /* same as target */
  89. #define tchars host_tchars /* same as target */
  90. #define ltchars host_ltchars /* same as target */
  91. #include <linux/termios.h>
  92. #include <linux/unistd.h>
  93. #include <linux/utsname.h>
  94. #include <linux/cdrom.h>
  95. #include <linux/hdreg.h>
  96. #include <linux/soundcard.h>
  97. #include <linux/kd.h>
  98. #include <linux/mtio.h>
  99. #include <linux/fs.h>
  100. #if defined(CONFIG_FIEMAP)
  101. #include <linux/fiemap.h>
  102. #endif
  103. #include <linux/fb.h>
  104. #include <linux/vt.h>
  105. #include <linux/dm-ioctl.h>
  106. #include <linux/reboot.h>
  107. #include <linux/route.h>
  108. #include <linux/filter.h>
  109. #include <linux/blkpg.h>
  110. #include "linux_loop.h"
  111. #include "cpu-uname.h"
  112. #include "qemu.h"
  113. #define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
  114. CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
  115. //#define DEBUG
  116. //#include <linux/msdos_fs.h>
  117. #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
  118. #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
  119. #undef _syscall0
  120. #undef _syscall1
  121. #undef _syscall2
  122. #undef _syscall3
  123. #undef _syscall4
  124. #undef _syscall5
  125. #undef _syscall6
  126. #define _syscall0(type,name) \
  127. static type name (void) \
  128. { \
  129. return syscall(__NR_##name); \
  130. }
  131. #define _syscall1(type,name,type1,arg1) \
  132. static type name (type1 arg1) \
  133. { \
  134. return syscall(__NR_##name, arg1); \
  135. }
  136. #define _syscall2(type,name,type1,arg1,type2,arg2) \
  137. static type name (type1 arg1,type2 arg2) \
  138. { \
  139. return syscall(__NR_##name, arg1, arg2); \
  140. }
  141. #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
  142. static type name (type1 arg1,type2 arg2,type3 arg3) \
  143. { \
  144. return syscall(__NR_##name, arg1, arg2, arg3); \
  145. }
  146. #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
  147. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
  148. { \
  149. return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
  150. }
  151. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  152. type5,arg5) \
  153. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
  154. { \
  155. return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
  156. }
  157. #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  158. type5,arg5,type6,arg6) \
  159. static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
  160. type6 arg6) \
  161. { \
  162. return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
  163. }
  164. #define __NR_sys_uname __NR_uname
  165. #define __NR_sys_getcwd1 __NR_getcwd
  166. #define __NR_sys_getdents __NR_getdents
  167. #define __NR_sys_getdents64 __NR_getdents64
  168. #define __NR_sys_getpriority __NR_getpriority
  169. #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
  170. #define __NR_sys_syslog __NR_syslog
  171. #define __NR_sys_tgkill __NR_tgkill
  172. #define __NR_sys_tkill __NR_tkill
  173. #define __NR_sys_futex __NR_futex
  174. #define __NR_sys_inotify_init __NR_inotify_init
  175. #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
  176. #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
  177. #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) || \
  178. defined(__s390x__)
  179. #define __NR__llseek __NR_lseek
  180. #endif
  181. #ifdef __NR_gettid
  182. _syscall0(int, gettid)
  183. #else
  184. /* This is a replacement for the host gettid() and must return a host
  185. errno. */
  186. static int gettid(void) {
  187. return -ENOSYS;
  188. }
  189. #endif
  190. #ifdef __NR_getdents
  191. _syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
  192. #endif
  193. #if !defined(__NR_getdents) || \
  194. (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
  195. _syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
  196. #endif
  197. #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
  198. _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
  199. loff_t *, res, uint, wh);
  200. #endif
  201. _syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
  202. _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
  203. #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
  204. _syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
  205. #endif
  206. #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
  207. _syscall2(int,sys_tkill,int,tid,int,sig)
  208. #endif
  209. #ifdef __NR_exit_group
  210. _syscall1(int,exit_group,int,error_code)
  211. #endif
  212. #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
  213. _syscall1(int,set_tid_address,int *,tidptr)
  214. #endif
  215. #if defined(TARGET_NR_futex) && defined(__NR_futex)
  216. _syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
  217. const struct timespec *,timeout,int *,uaddr2,int,val3)
  218. #endif
  219. #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
  220. _syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
  221. unsigned long *, user_mask_ptr);
  222. #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
  223. _syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
  224. unsigned long *, user_mask_ptr);
  225. _syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
  226. void *, arg);
  227. _syscall2(int, capget, struct __user_cap_header_struct *, header,
  228. struct __user_cap_data_struct *, data);
  229. _syscall2(int, capset, struct __user_cap_header_struct *, header,
  230. struct __user_cap_data_struct *, data);
  231. static bitmask_transtbl fcntl_flags_tbl[] = {
  232. { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
  233. { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
  234. { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
  235. { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
  236. { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
  237. { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
  238. { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
  239. { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
  240. { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
  241. { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
  242. { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
  243. { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
  244. { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
  245. #if defined(O_DIRECT)
  246. { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
  247. #endif
  248. #if defined(O_NOATIME)
  249. { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
  250. #endif
  251. #if defined(O_CLOEXEC)
  252. { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
  253. #endif
  254. #if defined(O_PATH)
  255. { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
  256. #endif
  257. /* Don't terminate the list prematurely on 64-bit host+guest. */
  258. #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
  259. { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
  260. #endif
  261. { 0, 0, 0, 0 }
  262. };
  263. #define COPY_UTSNAME_FIELD(dest, src) \
  264. do { \
  265. /* __NEW_UTS_LEN doesn't include terminating null */ \
  266. (void) strncpy((dest), (src), __NEW_UTS_LEN); \
  267. (dest)[__NEW_UTS_LEN] = '\0'; \
  268. } while (0)
  269. static int sys_uname(struct new_utsname *buf)
  270. {
  271. struct utsname uts_buf;
  272. if (uname(&uts_buf) < 0)
  273. return (-1);
  274. /*
  275. * Just in case these have some differences, we
  276. * translate utsname to new_utsname (which is the
  277. * struct linux kernel uses).
  278. */
  279. memset(buf, 0, sizeof(*buf));
  280. COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
  281. COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
  282. COPY_UTSNAME_FIELD(buf->release, uts_buf.release);
  283. COPY_UTSNAME_FIELD(buf->version, uts_buf.version);
  284. COPY_UTSNAME_FIELD(buf->machine, uts_buf.machine);
  285. #ifdef _GNU_SOURCE
  286. COPY_UTSNAME_FIELD(buf->domainname, uts_buf.domainname);
  287. #endif
  288. return (0);
  289. #undef COPY_UTSNAME_FIELD
  290. }
  291. static int sys_getcwd1(char *buf, size_t size)
  292. {
  293. if (getcwd(buf, size) == NULL) {
  294. /* getcwd() sets errno */
  295. return (-1);
  296. }
  297. return strlen(buf)+1;
  298. }
  299. #ifdef TARGET_NR_openat
  300. static int sys_openat(int dirfd, const char *pathname, int flags, mode_t mode)
  301. {
  302. /*
  303. * open(2) has extra parameter 'mode' when called with
  304. * flag O_CREAT.
  305. */
  306. if ((flags & O_CREAT) != 0) {
  307. return (openat(dirfd, pathname, flags, mode));
  308. }
  309. return (openat(dirfd, pathname, flags));
  310. }
  311. #endif
  312. #ifdef TARGET_NR_utimensat
  313. #ifdef CONFIG_UTIMENSAT
  314. static int sys_utimensat(int dirfd, const char *pathname,
  315. const struct timespec times[2], int flags)
  316. {
  317. if (pathname == NULL)
  318. return futimens(dirfd, times);
  319. else
  320. return utimensat(dirfd, pathname, times, flags);
  321. }
  322. #elif defined(__NR_utimensat)
  323. #define __NR_sys_utimensat __NR_utimensat
  324. _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
  325. const struct timespec *,tsp,int,flags)
  326. #else
  327. static int sys_utimensat(int dirfd, const char *pathname,
  328. const struct timespec times[2], int flags)
  329. {
  330. errno = ENOSYS;
  331. return -1;
  332. }
  333. #endif
  334. #endif /* TARGET_NR_utimensat */
  335. #ifdef CONFIG_INOTIFY
  336. #include <sys/inotify.h>
  337. #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
  338. static int sys_inotify_init(void)
  339. {
  340. return (inotify_init());
  341. }
  342. #endif
  343. #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
  344. static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
  345. {
  346. return (inotify_add_watch(fd, pathname, mask));
  347. }
  348. #endif
  349. #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
  350. static int sys_inotify_rm_watch(int fd, int32_t wd)
  351. {
  352. return (inotify_rm_watch(fd, wd));
  353. }
  354. #endif
  355. #ifdef CONFIG_INOTIFY1
  356. #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
  357. static int sys_inotify_init1(int flags)
  358. {
  359. return (inotify_init1(flags));
  360. }
  361. #endif
  362. #endif
  363. #else
  364. /* Userspace can usually survive runtime without inotify */
  365. #undef TARGET_NR_inotify_init
  366. #undef TARGET_NR_inotify_init1
  367. #undef TARGET_NR_inotify_add_watch
  368. #undef TARGET_NR_inotify_rm_watch
  369. #endif /* CONFIG_INOTIFY */
  370. #if defined(TARGET_NR_ppoll)
  371. #ifndef __NR_ppoll
  372. # define __NR_ppoll -1
  373. #endif
  374. #define __NR_sys_ppoll __NR_ppoll
  375. _syscall5(int, sys_ppoll, struct pollfd *, fds, nfds_t, nfds,
  376. struct timespec *, timeout, const __sigset_t *, sigmask,
  377. size_t, sigsetsize)
  378. #endif
  379. #if defined(TARGET_NR_pselect6)
  380. #ifndef __NR_pselect6
  381. # define __NR_pselect6 -1
  382. #endif
  383. #define __NR_sys_pselect6 __NR_pselect6
  384. _syscall6(int, sys_pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds,
  385. fd_set *, exceptfds, struct timespec *, timeout, void *, sig);
  386. #endif
  387. #if defined(TARGET_NR_prlimit64)
  388. #ifndef __NR_prlimit64
  389. # define __NR_prlimit64 -1
  390. #endif
  391. #define __NR_sys_prlimit64 __NR_prlimit64
  392. /* The glibc rlimit structure may not be that used by the underlying syscall */
  393. struct host_rlimit64 {
  394. uint64_t rlim_cur;
  395. uint64_t rlim_max;
  396. };
  397. _syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
  398. const struct host_rlimit64 *, new_limit,
  399. struct host_rlimit64 *, old_limit)
  400. #endif
  401. #if defined(TARGET_NR_timer_create)
  402. /* Maxiumum of 32 active POSIX timers allowed at any one time. */
  403. static timer_t g_posix_timers[32] = { 0, } ;
  404. static inline int next_free_host_timer(void)
  405. {
  406. int k ;
  407. /* FIXME: Does finding the next free slot require a lock? */
  408. for (k = 0; k < ARRAY_SIZE(g_posix_timers); k++) {
  409. if (g_posix_timers[k] == 0) {
  410. g_posix_timers[k] = (timer_t) 1;
  411. return k;
  412. }
  413. }
  414. return -1;
  415. }
  416. #endif
  417. /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
  418. #ifdef TARGET_ARM
  419. static inline int regpairs_aligned(void *cpu_env) {
  420. return ((((CPUARMState *)cpu_env)->eabi) == 1) ;
  421. }
  422. #elif defined(TARGET_MIPS)
  423. static inline int regpairs_aligned(void *cpu_env) { return 1; }
  424. #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
  425. /* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
  426. * of registers which translates to the same as ARM/MIPS, because we start with
  427. * r3 as arg1 */
  428. static inline int regpairs_aligned(void *cpu_env) { return 1; }
  429. #else
  430. static inline int regpairs_aligned(void *cpu_env) { return 0; }
  431. #endif
  432. #define ERRNO_TABLE_SIZE 1200
  433. /* target_to_host_errno_table[] is initialized from
  434. * host_to_target_errno_table[] in syscall_init(). */
  435. static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
  436. };
  437. /*
  438. * This list is the union of errno values overridden in asm-<arch>/errno.h
  439. * minus the errnos that are not actually generic to all archs.
  440. */
  441. static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
  442. [EIDRM] = TARGET_EIDRM,
  443. [ECHRNG] = TARGET_ECHRNG,
  444. [EL2NSYNC] = TARGET_EL2NSYNC,
  445. [EL3HLT] = TARGET_EL3HLT,
  446. [EL3RST] = TARGET_EL3RST,
  447. [ELNRNG] = TARGET_ELNRNG,
  448. [EUNATCH] = TARGET_EUNATCH,
  449. [ENOCSI] = TARGET_ENOCSI,
  450. [EL2HLT] = TARGET_EL2HLT,
  451. [EDEADLK] = TARGET_EDEADLK,
  452. [ENOLCK] = TARGET_ENOLCK,
  453. [EBADE] = TARGET_EBADE,
  454. [EBADR] = TARGET_EBADR,
  455. [EXFULL] = TARGET_EXFULL,
  456. [ENOANO] = TARGET_ENOANO,
  457. [EBADRQC] = TARGET_EBADRQC,
  458. [EBADSLT] = TARGET_EBADSLT,
  459. [EBFONT] = TARGET_EBFONT,
  460. [ENOSTR] = TARGET_ENOSTR,
  461. [ENODATA] = TARGET_ENODATA,
  462. [ETIME] = TARGET_ETIME,
  463. [ENOSR] = TARGET_ENOSR,
  464. [ENONET] = TARGET_ENONET,
  465. [ENOPKG] = TARGET_ENOPKG,
  466. [EREMOTE] = TARGET_EREMOTE,
  467. [ENOLINK] = TARGET_ENOLINK,
  468. [EADV] = TARGET_EADV,
  469. [ESRMNT] = TARGET_ESRMNT,
  470. [ECOMM] = TARGET_ECOMM,
  471. [EPROTO] = TARGET_EPROTO,
  472. [EDOTDOT] = TARGET_EDOTDOT,
  473. [EMULTIHOP] = TARGET_EMULTIHOP,
  474. [EBADMSG] = TARGET_EBADMSG,
  475. [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
  476. [EOVERFLOW] = TARGET_EOVERFLOW,
  477. [ENOTUNIQ] = TARGET_ENOTUNIQ,
  478. [EBADFD] = TARGET_EBADFD,
  479. [EREMCHG] = TARGET_EREMCHG,
  480. [ELIBACC] = TARGET_ELIBACC,
  481. [ELIBBAD] = TARGET_ELIBBAD,
  482. [ELIBSCN] = TARGET_ELIBSCN,
  483. [ELIBMAX] = TARGET_ELIBMAX,
  484. [ELIBEXEC] = TARGET_ELIBEXEC,
  485. [EILSEQ] = TARGET_EILSEQ,
  486. [ENOSYS] = TARGET_ENOSYS,
  487. [ELOOP] = TARGET_ELOOP,
  488. [ERESTART] = TARGET_ERESTART,
  489. [ESTRPIPE] = TARGET_ESTRPIPE,
  490. [ENOTEMPTY] = TARGET_ENOTEMPTY,
  491. [EUSERS] = TARGET_EUSERS,
  492. [ENOTSOCK] = TARGET_ENOTSOCK,
  493. [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
  494. [EMSGSIZE] = TARGET_EMSGSIZE,
  495. [EPROTOTYPE] = TARGET_EPROTOTYPE,
  496. [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
  497. [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
  498. [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
  499. [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
  500. [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
  501. [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
  502. [EADDRINUSE] = TARGET_EADDRINUSE,
  503. [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
  504. [ENETDOWN] = TARGET_ENETDOWN,
  505. [ENETUNREACH] = TARGET_ENETUNREACH,
  506. [ENETRESET] = TARGET_ENETRESET,
  507. [ECONNABORTED] = TARGET_ECONNABORTED,
  508. [ECONNRESET] = TARGET_ECONNRESET,
  509. [ENOBUFS] = TARGET_ENOBUFS,
  510. [EISCONN] = TARGET_EISCONN,
  511. [ENOTCONN] = TARGET_ENOTCONN,
  512. [EUCLEAN] = TARGET_EUCLEAN,
  513. [ENOTNAM] = TARGET_ENOTNAM,
  514. [ENAVAIL] = TARGET_ENAVAIL,
  515. [EISNAM] = TARGET_EISNAM,
  516. [EREMOTEIO] = TARGET_EREMOTEIO,
  517. [ESHUTDOWN] = TARGET_ESHUTDOWN,
  518. [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
  519. [ETIMEDOUT] = TARGET_ETIMEDOUT,
  520. [ECONNREFUSED] = TARGET_ECONNREFUSED,
  521. [EHOSTDOWN] = TARGET_EHOSTDOWN,
  522. [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
  523. [EALREADY] = TARGET_EALREADY,
  524. [EINPROGRESS] = TARGET_EINPROGRESS,
  525. [ESTALE] = TARGET_ESTALE,
  526. [ECANCELED] = TARGET_ECANCELED,
  527. [ENOMEDIUM] = TARGET_ENOMEDIUM,
  528. [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
  529. #ifdef ENOKEY
  530. [ENOKEY] = TARGET_ENOKEY,
  531. #endif
  532. #ifdef EKEYEXPIRED
  533. [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
  534. #endif
  535. #ifdef EKEYREVOKED
  536. [EKEYREVOKED] = TARGET_EKEYREVOKED,
  537. #endif
  538. #ifdef EKEYREJECTED
  539. [EKEYREJECTED] = TARGET_EKEYREJECTED,
  540. #endif
  541. #ifdef EOWNERDEAD
  542. [EOWNERDEAD] = TARGET_EOWNERDEAD,
  543. #endif
  544. #ifdef ENOTRECOVERABLE
  545. [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
  546. #endif
  547. };
  548. static inline int host_to_target_errno(int err)
  549. {
  550. if(host_to_target_errno_table[err])
  551. return host_to_target_errno_table[err];
  552. return err;
  553. }
  554. static inline int target_to_host_errno(int err)
  555. {
  556. if (target_to_host_errno_table[err])
  557. return target_to_host_errno_table[err];
  558. return err;
  559. }
  560. static inline abi_long get_errno(abi_long ret)
  561. {
  562. if (ret == -1)
  563. return -host_to_target_errno(errno);
  564. else
  565. return ret;
  566. }
  567. static inline int is_error(abi_long ret)
  568. {
  569. return (abi_ulong)ret >= (abi_ulong)(-4096);
  570. }
  571. char *target_strerror(int err)
  572. {
  573. if ((err >= ERRNO_TABLE_SIZE) || (err < 0)) {
  574. return NULL;
  575. }
  576. return strerror(target_to_host_errno(err));
  577. }
  578. static abi_ulong target_brk;
  579. static abi_ulong target_original_brk;
  580. static abi_ulong brk_page;
  581. void target_set_brk(abi_ulong new_brk)
  582. {
  583. target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
  584. brk_page = HOST_PAGE_ALIGN(target_brk);
  585. }
  586. //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
  587. #define DEBUGF_BRK(message, args...)
  588. /* do_brk() must return target values and target errnos. */
  589. abi_long do_brk(abi_ulong new_brk)
  590. {
  591. abi_long mapped_addr;
  592. int new_alloc_size;
  593. DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
  594. if (!new_brk) {
  595. DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
  596. return target_brk;
  597. }
  598. if (new_brk < target_original_brk) {
  599. DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
  600. target_brk);
  601. return target_brk;
  602. }
  603. /* If the new brk is less than the highest page reserved to the
  604. * target heap allocation, set it and we're almost done... */
  605. if (new_brk <= brk_page) {
  606. /* Heap contents are initialized to zero, as for anonymous
  607. * mapped pages. */
  608. if (new_brk > target_brk) {
  609. memset(g2h(target_brk), 0, new_brk - target_brk);
  610. }
  611. target_brk = new_brk;
  612. DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
  613. return target_brk;
  614. }
  615. /* We need to allocate more memory after the brk... Note that
  616. * we don't use MAP_FIXED because that will map over the top of
  617. * any existing mapping (like the one with the host libc or qemu
  618. * itself); instead we treat "mapped but at wrong address" as
  619. * a failure and unmap again.
  620. */
  621. new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
  622. mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
  623. PROT_READ|PROT_WRITE,
  624. MAP_ANON|MAP_PRIVATE, 0, 0));
  625. if (mapped_addr == brk_page) {
  626. /* Heap contents are initialized to zero, as for anonymous
  627. * mapped pages. Technically the new pages are already
  628. * initialized to zero since they *are* anonymous mapped
  629. * pages, however we have to take care with the contents that
  630. * come from the remaining part of the previous page: it may
  631. * contains garbage data due to a previous heap usage (grown
  632. * then shrunken). */
  633. memset(g2h(target_brk), 0, brk_page - target_brk);
  634. target_brk = new_brk;
  635. brk_page = HOST_PAGE_ALIGN(target_brk);
  636. DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
  637. target_brk);
  638. return target_brk;
  639. } else if (mapped_addr != -1) {
  640. /* Mapped but at wrong address, meaning there wasn't actually
  641. * enough space for this brk.
  642. */
  643. target_munmap(mapped_addr, new_alloc_size);
  644. mapped_addr = -1;
  645. DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
  646. }
  647. else {
  648. DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
  649. }
  650. #if defined(TARGET_ALPHA)
  651. /* We (partially) emulate OSF/1 on Alpha, which requires we
  652. return a proper errno, not an unchanged brk value. */
  653. return -TARGET_ENOMEM;
  654. #endif
  655. /* For everything else, return the previous break. */
  656. return target_brk;
  657. }
  658. static inline abi_long copy_from_user_fdset(fd_set *fds,
  659. abi_ulong target_fds_addr,
  660. int n)
  661. {
  662. int i, nw, j, k;
  663. abi_ulong b, *target_fds;
  664. nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
  665. if (!(target_fds = lock_user(VERIFY_READ,
  666. target_fds_addr,
  667. sizeof(abi_ulong) * nw,
  668. 1)))
  669. return -TARGET_EFAULT;
  670. FD_ZERO(fds);
  671. k = 0;
  672. for (i = 0; i < nw; i++) {
  673. /* grab the abi_ulong */
  674. __get_user(b, &target_fds[i]);
  675. for (j = 0; j < TARGET_ABI_BITS; j++) {
  676. /* check the bit inside the abi_ulong */
  677. if ((b >> j) & 1)
  678. FD_SET(k, fds);
  679. k++;
  680. }
  681. }
  682. unlock_user(target_fds, target_fds_addr, 0);
  683. return 0;
  684. }
  685. static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
  686. abi_ulong target_fds_addr,
  687. int n)
  688. {
  689. if (target_fds_addr) {
  690. if (copy_from_user_fdset(fds, target_fds_addr, n))
  691. return -TARGET_EFAULT;
  692. *fds_ptr = fds;
  693. } else {
  694. *fds_ptr = NULL;
  695. }
  696. return 0;
  697. }
  698. static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
  699. const fd_set *fds,
  700. int n)
  701. {
  702. int i, nw, j, k;
  703. abi_long v;
  704. abi_ulong *target_fds;
  705. nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
  706. if (!(target_fds = lock_user(VERIFY_WRITE,
  707. target_fds_addr,
  708. sizeof(abi_ulong) * nw,
  709. 0)))
  710. return -TARGET_EFAULT;
  711. k = 0;
  712. for (i = 0; i < nw; i++) {
  713. v = 0;
  714. for (j = 0; j < TARGET_ABI_BITS; j++) {
  715. v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
  716. k++;
  717. }
  718. __put_user(v, &target_fds[i]);
  719. }
  720. unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
  721. return 0;
  722. }
  723. #if defined(__alpha__)
  724. #define HOST_HZ 1024
  725. #else
  726. #define HOST_HZ 100
  727. #endif
  728. static inline abi_long host_to_target_clock_t(long ticks)
  729. {
  730. #if HOST_HZ == TARGET_HZ
  731. return ticks;
  732. #else
  733. return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
  734. #endif
  735. }
  736. static inline abi_long host_to_target_rusage(abi_ulong target_addr,
  737. const struct rusage *rusage)
  738. {
  739. struct target_rusage *target_rusage;
  740. if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
  741. return -TARGET_EFAULT;
  742. target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
  743. target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
  744. target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
  745. target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
  746. target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
  747. target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
  748. target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
  749. target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
  750. target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
  751. target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
  752. target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
  753. target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
  754. target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
  755. target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
  756. target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
  757. target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
  758. target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
  759. target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
  760. unlock_user_struct(target_rusage, target_addr, 1);
  761. return 0;
  762. }
  763. static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
  764. {
  765. abi_ulong target_rlim_swap;
  766. rlim_t result;
  767. target_rlim_swap = tswapal(target_rlim);
  768. if (target_rlim_swap == TARGET_RLIM_INFINITY)
  769. return RLIM_INFINITY;
  770. result = target_rlim_swap;
  771. if (target_rlim_swap != (rlim_t)result)
  772. return RLIM_INFINITY;
  773. return result;
  774. }
  775. static inline abi_ulong host_to_target_rlim(rlim_t rlim)
  776. {
  777. abi_ulong target_rlim_swap;
  778. abi_ulong result;
  779. if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
  780. target_rlim_swap = TARGET_RLIM_INFINITY;
  781. else
  782. target_rlim_swap = rlim;
  783. result = tswapal(target_rlim_swap);
  784. return result;
  785. }
  786. static inline int target_to_host_resource(int code)
  787. {
  788. switch (code) {
  789. case TARGET_RLIMIT_AS:
  790. return RLIMIT_AS;
  791. case TARGET_RLIMIT_CORE:
  792. return RLIMIT_CORE;
  793. case TARGET_RLIMIT_CPU:
  794. return RLIMIT_CPU;
  795. case TARGET_RLIMIT_DATA:
  796. return RLIMIT_DATA;
  797. case TARGET_RLIMIT_FSIZE:
  798. return RLIMIT_FSIZE;
  799. case TARGET_RLIMIT_LOCKS:
  800. return RLIMIT_LOCKS;
  801. case TARGET_RLIMIT_MEMLOCK:
  802. return RLIMIT_MEMLOCK;
  803. case TARGET_RLIMIT_MSGQUEUE:
  804. return RLIMIT_MSGQUEUE;
  805. case TARGET_RLIMIT_NICE:
  806. return RLIMIT_NICE;
  807. case TARGET_RLIMIT_NOFILE:
  808. return RLIMIT_NOFILE;
  809. case TARGET_RLIMIT_NPROC:
  810. return RLIMIT_NPROC;
  811. case TARGET_RLIMIT_RSS:
  812. return RLIMIT_RSS;
  813. case TARGET_RLIMIT_RTPRIO:
  814. return RLIMIT_RTPRIO;
  815. case TARGET_RLIMIT_SIGPENDING:
  816. return RLIMIT_SIGPENDING;
  817. case TARGET_RLIMIT_STACK:
  818. return RLIMIT_STACK;
  819. default:
  820. return code;
  821. }
  822. }
  823. static inline abi_long copy_from_user_timeval(struct timeval *tv,
  824. abi_ulong target_tv_addr)
  825. {
  826. struct target_timeval *target_tv;
  827. if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
  828. return -TARGET_EFAULT;
  829. __get_user(tv->tv_sec, &target_tv->tv_sec);
  830. __get_user(tv->tv_usec, &target_tv->tv_usec);
  831. unlock_user_struct(target_tv, target_tv_addr, 0);
  832. return 0;
  833. }
  834. static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
  835. const struct timeval *tv)
  836. {
  837. struct target_timeval *target_tv;
  838. if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
  839. return -TARGET_EFAULT;
  840. __put_user(tv->tv_sec, &target_tv->tv_sec);
  841. __put_user(tv->tv_usec, &target_tv->tv_usec);
  842. unlock_user_struct(target_tv, target_tv_addr, 1);
  843. return 0;
  844. }
  845. #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
  846. #include <mqueue.h>
  847. static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
  848. abi_ulong target_mq_attr_addr)
  849. {
  850. struct target_mq_attr *target_mq_attr;
  851. if (!lock_user_struct(VERIFY_READ, target_mq_attr,
  852. target_mq_attr_addr, 1))
  853. return -TARGET_EFAULT;
  854. __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
  855. __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
  856. __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
  857. __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
  858. unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
  859. return 0;
  860. }
  861. static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
  862. const struct mq_attr *attr)
  863. {
  864. struct target_mq_attr *target_mq_attr;
  865. if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
  866. target_mq_attr_addr, 0))
  867. return -TARGET_EFAULT;
  868. __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
  869. __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
  870. __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
  871. __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
  872. unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
  873. return 0;
  874. }
  875. #endif
  876. #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
  877. /* do_select() must return target values and target errnos. */
  878. static abi_long do_select(int n,
  879. abi_ulong rfd_addr, abi_ulong wfd_addr,
  880. abi_ulong efd_addr, abi_ulong target_tv_addr)
  881. {
  882. fd_set rfds, wfds, efds;
  883. fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
  884. struct timeval tv, *tv_ptr;
  885. abi_long ret;
  886. ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
  887. if (ret) {
  888. return ret;
  889. }
  890. ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
  891. if (ret) {
  892. return ret;
  893. }
  894. ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
  895. if (ret) {
  896. return ret;
  897. }
  898. if (target_tv_addr) {
  899. if (copy_from_user_timeval(&tv, target_tv_addr))
  900. return -TARGET_EFAULT;
  901. tv_ptr = &tv;
  902. } else {
  903. tv_ptr = NULL;
  904. }
  905. ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
  906. if (!is_error(ret)) {
  907. if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
  908. return -TARGET_EFAULT;
  909. if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
  910. return -TARGET_EFAULT;
  911. if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
  912. return -TARGET_EFAULT;
  913. if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
  914. return -TARGET_EFAULT;
  915. }
  916. return ret;
  917. }
  918. #endif
  919. static abi_long do_pipe2(int host_pipe[], int flags)
  920. {
  921. #ifdef CONFIG_PIPE2
  922. return pipe2(host_pipe, flags);
  923. #else
  924. return -ENOSYS;
  925. #endif
  926. }
  927. static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
  928. int flags, int is_pipe2)
  929. {
  930. int host_pipe[2];
  931. abi_long ret;
  932. ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
  933. if (is_error(ret))
  934. return get_errno(ret);
  935. /* Several targets have special calling conventions for the original
  936. pipe syscall, but didn't replicate this into the pipe2 syscall. */
  937. if (!is_pipe2) {
  938. #if defined(TARGET_ALPHA)
  939. ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
  940. return host_pipe[0];
  941. #elif defined(TARGET_MIPS)
  942. ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
  943. return host_pipe[0];
  944. #elif defined(TARGET_SH4)
  945. ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
  946. return host_pipe[0];
  947. #elif defined(TARGET_SPARC)
  948. ((CPUSPARCState*)cpu_env)->regwptr[1] = host_pipe[1];
  949. return host_pipe[0];
  950. #endif
  951. }
  952. if (put_user_s32(host_pipe[0], pipedes)
  953. || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
  954. return -TARGET_EFAULT;
  955. return get_errno(ret);
  956. }
  957. static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
  958. abi_ulong target_addr,
  959. socklen_t len)
  960. {
  961. struct target_ip_mreqn *target_smreqn;
  962. target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
  963. if (!target_smreqn)
  964. return -TARGET_EFAULT;
  965. mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
  966. mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
  967. if (len == sizeof(struct target_ip_mreqn))
  968. mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
  969. unlock_user(target_smreqn, target_addr, 0);
  970. return 0;
  971. }
  972. static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
  973. abi_ulong target_addr,
  974. socklen_t len)
  975. {
  976. const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
  977. sa_family_t sa_family;
  978. struct target_sockaddr *target_saddr;
  979. target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
  980. if (!target_saddr)
  981. return -TARGET_EFAULT;
  982. sa_family = tswap16(target_saddr->sa_family);
  983. /* Oops. The caller might send a incomplete sun_path; sun_path
  984. * must be terminated by \0 (see the manual page), but
  985. * unfortunately it is quite common to specify sockaddr_un
  986. * length as "strlen(x->sun_path)" while it should be
  987. * "strlen(...) + 1". We'll fix that here if needed.
  988. * Linux kernel has a similar feature.
  989. */
  990. if (sa_family == AF_UNIX) {
  991. if (len < unix_maxlen && len > 0) {
  992. char *cp = (char*)target_saddr;
  993. if ( cp[len-1] && !cp[len] )
  994. len++;
  995. }
  996. if (len > unix_maxlen)
  997. len = unix_maxlen;
  998. }
  999. memcpy(addr, target_saddr, len);
  1000. addr->sa_family = sa_family;
  1001. unlock_user(target_saddr, target_addr, 0);
  1002. return 0;
  1003. }
  1004. static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
  1005. struct sockaddr *addr,
  1006. socklen_t len)
  1007. {
  1008. struct target_sockaddr *target_saddr;
  1009. target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
  1010. if (!target_saddr)
  1011. return -TARGET_EFAULT;
  1012. memcpy(target_saddr, addr, len);
  1013. target_saddr->sa_family = tswap16(addr->sa_family);
  1014. unlock_user(target_saddr, target_addr, len);
  1015. return 0;
  1016. }
  1017. static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
  1018. struct target_msghdr *target_msgh)
  1019. {
  1020. struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
  1021. abi_long msg_controllen;
  1022. abi_ulong target_cmsg_addr;
  1023. struct target_cmsghdr *target_cmsg;
  1024. socklen_t space = 0;
  1025. msg_controllen = tswapal(target_msgh->msg_controllen);
  1026. if (msg_controllen < sizeof (struct target_cmsghdr))
  1027. goto the_end;
  1028. target_cmsg_addr = tswapal(target_msgh->msg_control);
  1029. target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
  1030. if (!target_cmsg)
  1031. return -TARGET_EFAULT;
  1032. while (cmsg && target_cmsg) {
  1033. void *data = CMSG_DATA(cmsg);
  1034. void *target_data = TARGET_CMSG_DATA(target_cmsg);
  1035. int len = tswapal(target_cmsg->cmsg_len)
  1036. - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
  1037. space += CMSG_SPACE(len);
  1038. if (space > msgh->msg_controllen) {
  1039. space -= CMSG_SPACE(len);
  1040. gemu_log("Host cmsg overflow\n");
  1041. break;
  1042. }
  1043. if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
  1044. cmsg->cmsg_level = SOL_SOCKET;
  1045. } else {
  1046. cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
  1047. }
  1048. cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
  1049. cmsg->cmsg_len = CMSG_LEN(len);
  1050. if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
  1051. gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
  1052. memcpy(data, target_data, len);
  1053. } else {
  1054. int *fd = (int *)data;
  1055. int *target_fd = (int *)target_data;
  1056. int i, numfds = len / sizeof(int);
  1057. for (i = 0; i < numfds; i++)
  1058. fd[i] = tswap32(target_fd[i]);
  1059. }
  1060. cmsg = CMSG_NXTHDR(msgh, cmsg);
  1061. target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
  1062. }
  1063. unlock_user(target_cmsg, target_cmsg_addr, 0);
  1064. the_end:
  1065. msgh->msg_controllen = space;
  1066. return 0;
  1067. }
  1068. static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
  1069. struct msghdr *msgh)
  1070. {
  1071. struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
  1072. abi_long msg_controllen;
  1073. abi_ulong target_cmsg_addr;
  1074. struct target_cmsghdr *target_cmsg;
  1075. socklen_t space = 0;
  1076. msg_controllen = tswapal(target_msgh->msg_controllen);
  1077. if (msg_controllen < sizeof (struct target_cmsghdr))
  1078. goto the_end;
  1079. target_cmsg_addr = tswapal(target_msgh->msg_control);
  1080. target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
  1081. if (!target_cmsg)
  1082. return -TARGET_EFAULT;
  1083. while (cmsg && target_cmsg) {
  1084. void *data = CMSG_DATA(cmsg);
  1085. void *target_data = TARGET_CMSG_DATA(target_cmsg);
  1086. int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
  1087. space += TARGET_CMSG_SPACE(len);
  1088. if (space > msg_controllen) {
  1089. space -= TARGET_CMSG_SPACE(len);
  1090. gemu_log("Target cmsg overflow\n");
  1091. break;
  1092. }
  1093. if (cmsg->cmsg_level == SOL_SOCKET) {
  1094. target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
  1095. } else {
  1096. target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
  1097. }
  1098. target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
  1099. target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(len));
  1100. if ((cmsg->cmsg_level == SOL_SOCKET) &&
  1101. (cmsg->cmsg_type == SCM_RIGHTS)) {
  1102. int *fd = (int *)data;
  1103. int *target_fd = (int *)target_data;
  1104. int i, numfds = len / sizeof(int);
  1105. for (i = 0; i < numfds; i++)
  1106. target_fd[i] = tswap32(fd[i]);
  1107. } else if ((cmsg->cmsg_level == SOL_SOCKET) &&
  1108. (cmsg->cmsg_type == SO_TIMESTAMP) &&
  1109. (len == sizeof(struct timeval))) {
  1110. /* copy struct timeval to target */
  1111. struct timeval *tv = (struct timeval *)data;
  1112. struct target_timeval *target_tv =
  1113. (struct target_timeval *)target_data;
  1114. target_tv->tv_sec = tswapal(tv->tv_sec);
  1115. target_tv->tv_usec = tswapal(tv->tv_usec);
  1116. } else {
  1117. gemu_log("Unsupported ancillary data: %d/%d\n",
  1118. cmsg->cmsg_level, cmsg->cmsg_type);
  1119. memcpy(target_data, data, len);
  1120. }
  1121. cmsg = CMSG_NXTHDR(msgh, cmsg);
  1122. target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
  1123. }
  1124. unlock_user(target_cmsg, target_cmsg_addr, space);
  1125. the_end:
  1126. target_msgh->msg_controllen = tswapal(space);
  1127. return 0;
  1128. }
  1129. /* do_setsockopt() Must return target values and target errnos. */
  1130. static abi_long do_setsockopt(int sockfd, int level, int optname,
  1131. abi_ulong optval_addr, socklen_t optlen)
  1132. {
  1133. abi_long ret;
  1134. int val;
  1135. struct ip_mreqn *ip_mreq;
  1136. struct ip_mreq_source *ip_mreq_source;
  1137. switch(level) {
  1138. case SOL_TCP:
  1139. /* TCP options all take an 'int' value. */
  1140. if (optlen < sizeof(uint32_t))
  1141. return -TARGET_EINVAL;
  1142. if (get_user_u32(val, optval_addr))
  1143. return -TARGET_EFAULT;
  1144. ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
  1145. break;
  1146. case SOL_IP:
  1147. switch(optname) {
  1148. case IP_TOS:
  1149. case IP_TTL:
  1150. case IP_HDRINCL:
  1151. case IP_ROUTER_ALERT:
  1152. case IP_RECVOPTS:
  1153. case IP_RETOPTS:
  1154. case IP_PKTINFO:
  1155. case IP_MTU_DISCOVER:
  1156. case IP_RECVERR:
  1157. case IP_RECVTOS:
  1158. #ifdef IP_FREEBIND
  1159. case IP_FREEBIND:
  1160. #endif
  1161. case IP_MULTICAST_TTL:
  1162. case IP_MULTICAST_LOOP:
  1163. val = 0;
  1164. if (optlen >= sizeof(uint32_t)) {
  1165. if (get_user_u32(val, optval_addr))
  1166. return -TARGET_EFAULT;
  1167. } else if (optlen >= 1) {
  1168. if (get_user_u8(val, optval_addr))
  1169. return -TARGET_EFAULT;
  1170. }
  1171. ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
  1172. break;
  1173. case IP_ADD_MEMBERSHIP:
  1174. case IP_DROP_MEMBERSHIP:
  1175. if (optlen < sizeof (struct target_ip_mreq) ||
  1176. optlen > sizeof (struct target_ip_mreqn))
  1177. return -TARGET_EINVAL;
  1178. ip_mreq = (struct ip_mreqn *) alloca(optlen);
  1179. target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
  1180. ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
  1181. break;
  1182. case IP_BLOCK_SOURCE:
  1183. case IP_UNBLOCK_SOURCE:
  1184. case IP_ADD_SOURCE_MEMBERSHIP:
  1185. case IP_DROP_SOURCE_MEMBERSHIP:
  1186. if (optlen != sizeof (struct target_ip_mreq_source))
  1187. return -TARGET_EINVAL;
  1188. ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
  1189. ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
  1190. unlock_user (ip_mreq_source, optval_addr, 0);
  1191. break;
  1192. default:
  1193. goto unimplemented;
  1194. }
  1195. break;
  1196. case SOL_IPV6:
  1197. switch (optname) {
  1198. case IPV6_MTU_DISCOVER:
  1199. case IPV6_MTU:
  1200. case IPV6_V6ONLY:
  1201. case IPV6_RECVPKTINFO:
  1202. val = 0;
  1203. if (optlen < sizeof(uint32_t)) {
  1204. return -TARGET_EINVAL;
  1205. }
  1206. if (get_user_u32(val, optval_addr)) {
  1207. return -TARGET_EFAULT;
  1208. }
  1209. ret = get_errno(setsockopt(sockfd, level, optname,
  1210. &val, sizeof(val)));
  1211. break;
  1212. default:
  1213. goto unimplemented;
  1214. }
  1215. break;
  1216. case SOL_RAW:
  1217. switch (optname) {
  1218. case ICMP_FILTER:
  1219. /* struct icmp_filter takes an u32 value */
  1220. if (optlen < sizeof(uint32_t)) {
  1221. return -TARGET_EINVAL;
  1222. }
  1223. if (get_user_u32(val, optval_addr)) {
  1224. return -TARGET_EFAULT;
  1225. }
  1226. ret = get_errno(setsockopt(sockfd, level, optname,
  1227. &val, sizeof(val)));
  1228. break;
  1229. default:
  1230. goto unimplemented;
  1231. }
  1232. break;
  1233. case TARGET_SOL_SOCKET:
  1234. switch (optname) {
  1235. case TARGET_SO_RCVTIMEO:
  1236. {
  1237. struct timeval tv;
  1238. optname = SO_RCVTIMEO;
  1239. set_timeout:
  1240. if (optlen != sizeof(struct target_timeval)) {
  1241. return -TARGET_EINVAL;
  1242. }
  1243. if (copy_from_user_timeval(&tv, optval_addr)) {
  1244. return -TARGET_EFAULT;
  1245. }
  1246. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
  1247. &tv, sizeof(tv)));
  1248. return ret;
  1249. }
  1250. case TARGET_SO_SNDTIMEO:
  1251. optname = SO_SNDTIMEO;
  1252. goto set_timeout;
  1253. case TARGET_SO_ATTACH_FILTER:
  1254. {
  1255. struct target_sock_fprog *tfprog;
  1256. struct target_sock_filter *tfilter;
  1257. struct sock_fprog fprog;
  1258. struct sock_filter *filter;
  1259. int i;
  1260. if (optlen != sizeof(*tfprog)) {
  1261. return -TARGET_EINVAL;
  1262. }
  1263. if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
  1264. return -TARGET_EFAULT;
  1265. }
  1266. if (!lock_user_struct(VERIFY_READ, tfilter,
  1267. tswapal(tfprog->filter), 0)) {
  1268. unlock_user_struct(tfprog, optval_addr, 1);
  1269. return -TARGET_EFAULT;
  1270. }
  1271. fprog.len = tswap16(tfprog->len);
  1272. filter = malloc(fprog.len * sizeof(*filter));
  1273. if (filter == NULL) {
  1274. unlock_user_struct(tfilter, tfprog->filter, 1);
  1275. unlock_user_struct(tfprog, optval_addr, 1);
  1276. return -TARGET_ENOMEM;
  1277. }
  1278. for (i = 0; i < fprog.len; i++) {
  1279. filter[i].code = tswap16(tfilter[i].code);
  1280. filter[i].jt = tfilter[i].jt;
  1281. filter[i].jf = tfilter[i].jf;
  1282. filter[i].k = tswap32(tfilter[i].k);
  1283. }
  1284. fprog.filter = filter;
  1285. ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
  1286. SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
  1287. free(filter);
  1288. unlock_user_struct(tfilter, tfprog->filter, 1);
  1289. unlock_user_struct(tfprog, optval_addr, 1);
  1290. return ret;
  1291. }
  1292. /* Options with 'int' argument. */
  1293. case TARGET_SO_DEBUG:
  1294. optname = SO_DEBUG;
  1295. break;
  1296. case TARGET_SO_REUSEADDR:
  1297. optname = SO_REUSEADDR;
  1298. break;
  1299. case TARGET_SO_TYPE:
  1300. optname = SO_TYPE;
  1301. break;
  1302. case TARGET_SO_ERROR:
  1303. optname = SO_ERROR;
  1304. break;
  1305. case TARGET_SO_DONTROUTE:
  1306. optname = SO_DONTROUTE;
  1307. break;
  1308. case TARGET_SO_BROADCAST:
  1309. optname = SO_BROADCAST;
  1310. break;
  1311. case TARGET_SO_SNDBUF:
  1312. optname = SO_SNDBUF;
  1313. break;
  1314. case TARGET_SO_RCVBUF:
  1315. optname = SO_RCVBUF;
  1316. break;
  1317. case TARGET_SO_KEEPALIVE:
  1318. optname = SO_KEEPALIVE;
  1319. break;
  1320. case TARGET_SO_OOBINLINE:
  1321. optname = SO_OOBINLINE;
  1322. break;
  1323. case TARGET_SO_NO_CHECK:
  1324. optname = SO_NO_CHECK;
  1325. break;
  1326. case TARGET_SO_PRIORITY:
  1327. optname = SO_PRIORITY;
  1328. break;
  1329. #ifdef SO_BSDCOMPAT
  1330. case TARGET_SO_BSDCOMPAT:
  1331. optname = SO_BSDCOMPAT;
  1332. break;
  1333. #endif
  1334. case TARGET_SO_PASSCRED:
  1335. optname = SO_PASSCRED;
  1336. break;
  1337. case TARGET_SO_TIMESTAMP:
  1338. optname = SO_TIMESTAMP;
  1339. break;
  1340. case TARGET_SO_RCVLOWAT:
  1341. optname = SO_RCVLOWAT;
  1342. break;
  1343. break;
  1344. default:
  1345. goto unimplemented;
  1346. }
  1347. if (optlen < sizeof(uint32_t))
  1348. return -TARGET_EINVAL;
  1349. if (get_user_u32(val, optval_addr))
  1350. return -TARGET_EFAULT;
  1351. ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
  1352. break;
  1353. default:
  1354. unimplemented:
  1355. gemu_log("Unsupported setsockopt level=%d optname=%d\n", level, optname);
  1356. ret = -TARGET_ENOPROTOOPT;
  1357. }
  1358. return ret;
  1359. }
  1360. /* do_getsockopt() Must return target values and target errnos. */
  1361. static abi_long do_getsockopt(int sockfd, int level, int optname,
  1362. abi_ulong optval_addr, abi_ulong optlen)
  1363. {
  1364. abi_long ret;
  1365. int len, val;
  1366. socklen_t lv;
  1367. switch(level) {
  1368. case TARGET_SOL_SOCKET:
  1369. level = SOL_SOCKET;
  1370. switch (optname) {
  1371. /* These don't just return a single integer */
  1372. case TARGET_SO_LINGER:
  1373. case TARGET_SO_RCVTIMEO:
  1374. case TARGET_SO_SNDTIMEO:
  1375. case TARGET_SO_PEERNAME:
  1376. goto unimplemented;
  1377. case TARGET_SO_PEERCRED: {
  1378. struct ucred cr;
  1379. socklen_t crlen;
  1380. struct target_ucred *tcr;
  1381. if (get_user_u32(len, optlen)) {
  1382. return -TARGET_EFAULT;
  1383. }
  1384. if (len < 0) {
  1385. return -TARGET_EINVAL;
  1386. }
  1387. crlen = sizeof(cr);
  1388. ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
  1389. &cr, &crlen));
  1390. if (ret < 0) {
  1391. return ret;
  1392. }
  1393. if (len > crlen) {
  1394. len = crlen;
  1395. }
  1396. if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
  1397. return -TARGET_EFAULT;
  1398. }
  1399. __put_user(cr.pid, &tcr->pid);
  1400. __put_user(cr.uid, &tcr->uid);
  1401. __put_user(cr.gid, &tcr->gid);
  1402. unlock_user_struct(tcr, optval_addr, 1);
  1403. if (put_user_u32(len, optlen)) {
  1404. return -TARGET_EFAULT;
  1405. }
  1406. break;
  1407. }
  1408. /* Options with 'int' argument. */
  1409. case TARGET_SO_DEBUG:
  1410. optname = SO_DEBUG;
  1411. goto int_case;
  1412. case TARGET_SO_REUSEADDR:
  1413. optname = SO_REUSEADDR;
  1414. goto int_case;
  1415. case TARGET_SO_TYPE:
  1416. optname = SO_TYPE;
  1417. goto int_case;
  1418. case TARGET_SO_ERROR:
  1419. optname = SO_ERROR;
  1420. goto int_case;
  1421. case TARGET_SO_DONTROUTE:
  1422. optname = SO_DONTROUTE;
  1423. goto int_case;
  1424. case TARGET_SO_BROADCAST:
  1425. optname = SO_BROADCAST;
  1426. goto int_case;
  1427. case TARGET_SO_SNDBUF:
  1428. optname = SO_SNDBUF;
  1429. goto int_case;
  1430. case TARGET_SO_RCVBUF:
  1431. optname = SO_RCVBUF;
  1432. goto int_case;
  1433. case TARGET_SO_KEEPALIVE:
  1434. optname = SO_KEEPALIVE;
  1435. goto int_case;
  1436. case TARGET_SO_OOBINLINE:
  1437. optname = SO_OOBINLINE;
  1438. goto int_case;
  1439. case TARGET_SO_NO_CHECK:
  1440. optname = SO_NO_CHECK;
  1441. goto int_case;
  1442. case TARGET_SO_PRIORITY:
  1443. optname = SO_PRIORITY;
  1444. goto int_case;
  1445. #ifdef SO_BSDCOMPAT
  1446. case TARGET_SO_BSDCOMPAT:
  1447. optname = SO_BSDCOMPAT;
  1448. goto int_case;
  1449. #endif
  1450. case TARGET_SO_PASSCRED:
  1451. optname = SO_PASSCRED;
  1452. goto int_case;
  1453. case TARGET_SO_TIMESTAMP:
  1454. optname = SO_TIMESTAMP;
  1455. goto int_case;
  1456. case TARGET_SO_RCVLOWAT:
  1457. optname = SO_RCVLOWAT;
  1458. goto int_case;
  1459. default:
  1460. goto int_case;
  1461. }
  1462. break;
  1463. case SOL_TCP:
  1464. /* TCP options all take an 'int' value. */
  1465. int_case:
  1466. if (get_user_u32(len, optlen))
  1467. return -TARGET_EFAULT;
  1468. if (len < 0)
  1469. return -TARGET_EINVAL;
  1470. lv = sizeof(lv);
  1471. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  1472. if (ret < 0)
  1473. return ret;
  1474. if (len > lv)
  1475. len = lv;
  1476. if (len == 4) {
  1477. if (put_user_u32(val, optval_addr))
  1478. return -TARGET_EFAULT;
  1479. } else {
  1480. if (put_user_u8(val, optval_addr))
  1481. return -TARGET_EFAULT;
  1482. }
  1483. if (put_user_u32(len, optlen))
  1484. return -TARGET_EFAULT;
  1485. break;
  1486. case SOL_IP:
  1487. switch(optname) {
  1488. case IP_TOS:
  1489. case IP_TTL:
  1490. case IP_HDRINCL:
  1491. case IP_ROUTER_ALERT:
  1492. case IP_RECVOPTS:
  1493. case IP_RETOPTS:
  1494. case IP_PKTINFO:
  1495. case IP_MTU_DISCOVER:
  1496. case IP_RECVERR:
  1497. case IP_RECVTOS:
  1498. #ifdef IP_FREEBIND
  1499. case IP_FREEBIND:
  1500. #endif
  1501. case IP_MULTICAST_TTL:
  1502. case IP_MULTICAST_LOOP:
  1503. if (get_user_u32(len, optlen))
  1504. return -TARGET_EFAULT;
  1505. if (len < 0)
  1506. return -TARGET_EINVAL;
  1507. lv = sizeof(lv);
  1508. ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
  1509. if (ret < 0)
  1510. return ret;
  1511. if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
  1512. len = 1;
  1513. if (put_user_u32(len, optlen)
  1514. || put_user_u8(val, optval_addr))
  1515. return -TARGET_EFAULT;
  1516. } else {
  1517. if (len > sizeof(int))
  1518. len = sizeof(int);
  1519. if (put_user_u32(len, optlen)
  1520. || put_user_u32(val, optval_addr))
  1521. return -TARGET_EFAULT;
  1522. }
  1523. break;
  1524. default:
  1525. ret = -TARGET_ENOPROTOOPT;
  1526. break;
  1527. }
  1528. break;
  1529. default:
  1530. unimplemented:
  1531. gemu_log("getsockopt level=%d optname=%d not yet supported\n",
  1532. level, optname);
  1533. ret = -TARGET_EOPNOTSUPP;
  1534. break;
  1535. }
  1536. return ret;
  1537. }
  1538. static struct iovec *lock_iovec(int type, abi_ulong target_addr,
  1539. int count, int copy)
  1540. {
  1541. struct target_iovec *target_vec;
  1542. struct iovec *vec;
  1543. abi_ulong total_len, max_len;
  1544. int i;
  1545. int err = 0;
  1546. if (count == 0) {
  1547. errno = 0;
  1548. return NULL;
  1549. }
  1550. if (count < 0 || count > IOV_MAX) {
  1551. errno = EINVAL;
  1552. return NULL;
  1553. }
  1554. vec = calloc(count, sizeof(struct iovec));
  1555. if (vec == NULL) {
  1556. errno = ENOMEM;
  1557. return NULL;
  1558. }
  1559. target_vec = lock_user(VERIFY_READ, target_addr,
  1560. count * sizeof(struct target_iovec), 1);
  1561. if (target_vec == NULL) {
  1562. err = EFAULT;
  1563. goto fail2;
  1564. }
  1565. /* ??? If host page size > target page size, this will result in a
  1566. value larger than what we can actually support. */
  1567. max_len = 0x7fffffff & TARGET_PAGE_MASK;
  1568. total_len = 0;
  1569. for (i = 0; i < count; i++) {
  1570. abi_ulong base = tswapal(target_vec[i].iov_base);
  1571. abi_long len = tswapal(target_vec[i].iov_len);
  1572. if (len < 0) {
  1573. err = EINVAL;
  1574. goto fail;
  1575. } else if (len == 0) {
  1576. /* Zero length pointer is ignored. */
  1577. vec[i].iov_base = 0;
  1578. } else {
  1579. vec[i].iov_base = lock_user(type, base, len, copy);
  1580. if (!vec[i].iov_base) {
  1581. err = EFAULT;
  1582. goto fail;
  1583. }
  1584. if (len > max_len - total_len) {
  1585. len = max_len - total_len;
  1586. }
  1587. }
  1588. vec[i].iov_len = len;
  1589. total_len += len;
  1590. }
  1591. unlock_user(target_vec, target_addr, 0);
  1592. return vec;
  1593. fail:
  1594. unlock_user(target_vec, target_addr, 0);
  1595. fail2:
  1596. free(vec);
  1597. errno = err;
  1598. return NULL;
  1599. }
  1600. static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
  1601. int count, int copy)
  1602. {
  1603. struct target_iovec *target_vec;
  1604. int i;
  1605. target_vec = lock_user(VERIFY_READ, target_addr,
  1606. count * sizeof(struct target_iovec), 1);
  1607. if (target_vec) {
  1608. for (i = 0; i < count; i++) {
  1609. abi_ulong base = tswapal(target_vec[i].iov_base);
  1610. abi_long len = tswapal(target_vec[i].iov_base);
  1611. if (len < 0) {
  1612. break;
  1613. }
  1614. unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
  1615. }
  1616. unlock_user(target_vec, target_addr, 0);
  1617. }
  1618. free(vec);
  1619. }
  1620. static inline int target_to_host_sock_type(int *type)
  1621. {
  1622. int host_type = 0;
  1623. int target_type = *type;
  1624. switch (target_type & TARGET_SOCK_TYPE_MASK) {
  1625. case TARGET_SOCK_DGRAM:
  1626. host_type = SOCK_DGRAM;
  1627. break;
  1628. case TARGET_SOCK_STREAM:
  1629. host_type = SOCK_STREAM;
  1630. break;
  1631. default:
  1632. host_type = target_type & TARGET_SOCK_TYPE_MASK;
  1633. break;
  1634. }
  1635. if (target_type & TARGET_SOCK_CLOEXEC) {
  1636. #if defined(SOCK_CLOEXEC)
  1637. host_type |= SOCK_CLOEXEC;
  1638. #else
  1639. return -TARGET_EINVAL;
  1640. #endif
  1641. }
  1642. if (target_type & TARGET_SOCK_NONBLOCK) {
  1643. #if defined(SOCK_NONBLOCK)
  1644. host_type |= SOCK_NONBLOCK;
  1645. #elif !defined(O_NONBLOCK)
  1646. return -TARGET_EINVAL;
  1647. #endif
  1648. }
  1649. *type = host_type;
  1650. return 0;
  1651. }
  1652. /* Try to emulate socket type flags after socket creation. */
  1653. static int sock_flags_fixup(int fd, int target_type)
  1654. {
  1655. #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
  1656. if (target_type & TARGET_SOCK_NONBLOCK) {
  1657. int flags = fcntl(fd, F_GETFL);
  1658. if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) {
  1659. close(fd);
  1660. return -TARGET_EINVAL;
  1661. }
  1662. }
  1663. #endif
  1664. return fd;
  1665. }
  1666. /* do_socket() Must return target values and target errnos. */
  1667. static abi_long do_socket(int domain, int type, int protocol)
  1668. {
  1669. int target_type = type;
  1670. int ret;
  1671. ret = target_to_host_sock_type(&type);
  1672. if (ret) {
  1673. return ret;
  1674. }
  1675. if (domain == PF_NETLINK)
  1676. return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
  1677. ret = get_errno(socket(domain, type, protocol));
  1678. if (ret >= 0) {
  1679. ret = sock_flags_fixup(ret, target_type);
  1680. }
  1681. return ret;
  1682. }
  1683. /* do_bind() Must return target values and target errnos. */
  1684. static abi_long do_bind(int sockfd, abi_ulong target_addr,
  1685. socklen_t addrlen)
  1686. {
  1687. void *addr;
  1688. abi_long ret;
  1689. if ((int)addrlen < 0) {
  1690. return -TARGET_EINVAL;
  1691. }
  1692. addr = alloca(addrlen+1);
  1693. ret = target_to_host_sockaddr(addr, target_addr, addrlen);
  1694. if (ret)
  1695. return ret;
  1696. return get_errno(bind(sockfd, addr, addrlen));
  1697. }
  1698. /* do_connect() Must return target values and target errnos. */
  1699. static abi_long do_connect(int sockfd, abi_ulong target_addr,
  1700. socklen_t addrlen)
  1701. {
  1702. void *addr;
  1703. abi_long ret;
  1704. if ((int)addrlen < 0) {
  1705. return -TARGET_EINVAL;
  1706. }
  1707. addr = alloca(addrlen);
  1708. ret = target_to_host_sockaddr(addr, target_addr, addrlen);
  1709. if (ret)
  1710. return ret;
  1711. return get_errno(connect(sockfd, addr, addrlen));
  1712. }
  1713. /* do_sendrecvmsg_locked() Must return target values and target errnos. */
  1714. static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
  1715. int flags, int send)
  1716. {
  1717. abi_long ret, len;
  1718. struct msghdr msg;
  1719. int count;
  1720. struct iovec *vec;
  1721. abi_ulong target_vec;
  1722. if (msgp->msg_name) {
  1723. msg.msg_namelen = tswap32(msgp->msg_namelen);
  1724. msg.msg_name = alloca(msg.msg_namelen);
  1725. ret = target_to_host_sockaddr(msg.msg_name, tswapal(msgp->msg_name),
  1726. msg.msg_namelen);
  1727. if (ret) {
  1728. goto out2;
  1729. }
  1730. } else {
  1731. msg.msg_name = NULL;
  1732. msg.msg_namelen = 0;
  1733. }
  1734. msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
  1735. msg.msg_control = alloca(msg.msg_controllen);
  1736. msg.msg_flags = tswap32(msgp->msg_flags);
  1737. count = tswapal(msgp->msg_iovlen);
  1738. target_vec = tswapal(msgp->msg_iov);
  1739. vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
  1740. target_vec, count, send);
  1741. if (vec == NULL) {
  1742. ret = -host_to_target_errno(errno);
  1743. goto out2;
  1744. }
  1745. msg.msg_iovlen = count;
  1746. msg.msg_iov = vec;
  1747. if (send) {
  1748. ret = target_to_host_cmsg(&msg, msgp);
  1749. if (ret == 0)
  1750. ret = get_errno(sendmsg(fd, &msg, flags));
  1751. } else {
  1752. ret = get_errno(recvmsg(fd, &msg, flags));
  1753. if (!is_error(ret)) {
  1754. len = ret;
  1755. ret = host_to_target_cmsg(msgp, &msg);
  1756. if (!is_error(ret)) {
  1757. msgp->msg_namelen = tswap32(msg.msg_namelen);
  1758. if (msg.msg_name != NULL) {
  1759. ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
  1760. msg.msg_name, msg.msg_namelen);
  1761. if (ret) {
  1762. goto out;
  1763. }
  1764. }
  1765. ret = len;
  1766. }
  1767. }
  1768. }
  1769. out:
  1770. unlock_iovec(vec, target_vec, count, !send);
  1771. out2:
  1772. return ret;
  1773. }
  1774. static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
  1775. int flags, int send)
  1776. {
  1777. abi_long ret;
  1778. struct target_msghdr *msgp;
  1779. if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
  1780. msgp,
  1781. target_msg,
  1782. send ? 1 : 0)) {
  1783. return -TARGET_EFAULT;
  1784. }
  1785. ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
  1786. unlock_user_struct(msgp, target_msg, send ? 0 : 1);
  1787. return ret;
  1788. }
  1789. #ifdef TARGET_NR_sendmmsg
  1790. /* We don't rely on the C library to have sendmmsg/recvmmsg support,
  1791. * so it might not have this *mmsg-specific flag either.
  1792. */
  1793. #ifndef MSG_WAITFORONE
  1794. #define MSG_WAITFORONE 0x10000
  1795. #endif
  1796. static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec,
  1797. unsigned int vlen, unsigned int flags,
  1798. int send)
  1799. {
  1800. struct target_mmsghdr *mmsgp;
  1801. abi_long ret = 0;
  1802. int i;
  1803. if (vlen > UIO_MAXIOV) {
  1804. vlen = UIO_MAXIOV;
  1805. }
  1806. mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1);
  1807. if (!mmsgp) {
  1808. return -TARGET_EFAULT;
  1809. }
  1810. for (i = 0; i < vlen; i++) {
  1811. ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send);
  1812. if (is_error(ret)) {
  1813. break;
  1814. }
  1815. mmsgp[i].msg_len = tswap32(ret);
  1816. /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
  1817. if (flags & MSG_WAITFORONE) {
  1818. flags |= MSG_DONTWAIT;
  1819. }
  1820. }
  1821. unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i);
  1822. /* Return number of datagrams sent if we sent any at all;
  1823. * otherwise return the error.
  1824. */
  1825. if (i) {
  1826. return i;
  1827. }
  1828. return ret;
  1829. }
  1830. #endif
  1831. /* If we don't have a system accept4() then just call accept.
  1832. * The callsites to do_accept4() will ensure that they don't
  1833. * pass a non-zero flags argument in this config.
  1834. */
  1835. #ifndef CONFIG_ACCEPT4
  1836. static inline int accept4(int sockfd, struct sockaddr *addr,
  1837. socklen_t *addrlen, int flags)
  1838. {
  1839. assert(flags == 0);
  1840. return accept(sockfd, addr, addrlen);
  1841. }
  1842. #endif
  1843. /* do_accept4() Must return target values and target errnos. */
  1844. static abi_long do_accept4(int fd, abi_ulong target_addr,
  1845. abi_ulong target_addrlen_addr, int flags)
  1846. {
  1847. socklen_t addrlen;
  1848. void *addr;
  1849. abi_long ret;
  1850. int host_flags;
  1851. host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
  1852. if (target_addr == 0) {
  1853. return get_errno(accept4(fd, NULL, NULL, host_flags));
  1854. }
  1855. /* linux returns EINVAL if addrlen pointer is invalid */
  1856. if (get_user_u32(addrlen, target_addrlen_addr))
  1857. return -TARGET_EINVAL;
  1858. if ((int)addrlen < 0) {
  1859. return -TARGET_EINVAL;
  1860. }
  1861. if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
  1862. return -TARGET_EINVAL;
  1863. addr = alloca(addrlen);
  1864. ret = get_errno(accept4(fd, addr, &addrlen, host_flags));
  1865. if (!is_error(ret)) {
  1866. host_to_target_sockaddr(target_addr, addr, addrlen);
  1867. if (put_user_u32(addrlen, target_addrlen_addr))
  1868. ret = -TARGET_EFAULT;
  1869. }
  1870. return ret;
  1871. }
  1872. /* do_getpeername() Must return target values and target errnos. */
  1873. static abi_long do_getpeername(int fd, abi_ulong target_addr,
  1874. abi_ulong target_addrlen_addr)
  1875. {
  1876. socklen_t addrlen;
  1877. void *addr;
  1878. abi_long ret;
  1879. if (get_user_u32(addrlen, target_addrlen_addr))
  1880. return -TARGET_EFAULT;
  1881. if ((int)addrlen < 0) {
  1882. return -TARGET_EINVAL;
  1883. }
  1884. if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
  1885. return -TARGET_EFAULT;
  1886. addr = alloca(addrlen);
  1887. ret = get_errno(getpeername(fd, addr, &addrlen));
  1888. if (!is_error(ret)) {
  1889. host_to_target_sockaddr(target_addr, addr, addrlen);
  1890. if (put_user_u32(addrlen, target_addrlen_addr))
  1891. ret = -TARGET_EFAULT;
  1892. }
  1893. return ret;
  1894. }
  1895. /* do_getsockname() Must return target values and target errnos. */
  1896. static abi_long do_getsockname(int fd, abi_ulong target_addr,
  1897. abi_ulong target_addrlen_addr)
  1898. {
  1899. socklen_t addrlen;
  1900. void *addr;
  1901. abi_long ret;
  1902. if (get_user_u32(addrlen, target_addrlen_addr))
  1903. return -TARGET_EFAULT;
  1904. if ((int)addrlen < 0) {
  1905. return -TARGET_EINVAL;
  1906. }
  1907. if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
  1908. return -TARGET_EFAULT;
  1909. addr = alloca(addrlen);
  1910. ret = get_errno(getsockname(fd, addr, &addrlen));
  1911. if (!is_error(ret)) {
  1912. host_to_target_sockaddr(target_addr, addr, addrlen);
  1913. if (put_user_u32(addrlen, target_addrlen_addr))
  1914. ret = -TARGET_EFAULT;
  1915. }
  1916. return ret;
  1917. }
  1918. /* do_socketpair() Must return target values and target errnos. */
  1919. static abi_long do_socketpair(int domain, int type, int protocol,
  1920. abi_ulong target_tab_addr)
  1921. {
  1922. int tab[2];
  1923. abi_long ret;
  1924. target_to_host_sock_type(&type);
  1925. ret = get_errno(socketpair(domain, type, protocol, tab));
  1926. if (!is_error(ret)) {
  1927. if (put_user_s32(tab[0], target_tab_addr)
  1928. || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
  1929. ret = -TARGET_EFAULT;
  1930. }
  1931. return ret;
  1932. }
  1933. /* do_sendto() Must return target values and target errnos. */
  1934. static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
  1935. abi_ulong target_addr, socklen_t addrlen)
  1936. {
  1937. void *addr;
  1938. void *host_msg;
  1939. abi_long ret;
  1940. if ((int)addrlen < 0) {
  1941. return -TARGET_EINVAL;
  1942. }
  1943. host_msg = lock_user(VERIFY_READ, msg, len, 1);
  1944. if (!host_msg)
  1945. return -TARGET_EFAULT;
  1946. if (target_addr) {
  1947. addr = alloca(addrlen);
  1948. ret = target_to_host_sockaddr(addr, target_addr, addrlen);
  1949. if (ret) {
  1950. unlock_user(host_msg, msg, 0);
  1951. return ret;
  1952. }
  1953. ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
  1954. } else {
  1955. ret = get_errno(send(fd, host_msg, len, flags));
  1956. }
  1957. unlock_user(host_msg, msg, 0);
  1958. return ret;
  1959. }
  1960. /* do_recvfrom() Must return target values and target errnos. */
  1961. static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
  1962. abi_ulong target_addr,
  1963. abi_ulong target_addrlen)
  1964. {
  1965. socklen_t addrlen;
  1966. void *addr;
  1967. void *host_msg;
  1968. abi_long ret;
  1969. host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
  1970. if (!host_msg)
  1971. return -TARGET_EFAULT;
  1972. if (target_addr) {
  1973. if (get_user_u32(addrlen, target_addrlen)) {
  1974. ret = -TARGET_EFAULT;
  1975. goto fail;
  1976. }
  1977. if ((int)addrlen < 0) {
  1978. ret = -TARGET_EINVAL;
  1979. goto fail;
  1980. }
  1981. addr = alloca(addrlen);
  1982. ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
  1983. } else {
  1984. addr = NULL; /* To keep compiler quiet. */
  1985. ret = get_errno(qemu_recv(fd, host_msg, len, flags));
  1986. }
  1987. if (!is_error(ret)) {
  1988. if (target_addr) {
  1989. host_to_target_sockaddr(target_addr, addr, addrlen);
  1990. if (put_user_u32(addrlen, target_addrlen)) {
  1991. ret = -TARGET_EFAULT;
  1992. goto fail;
  1993. }
  1994. }
  1995. unlock_user(host_msg, msg, len);
  1996. } else {
  1997. fail:
  1998. unlock_user(host_msg, msg, 0);
  1999. }
  2000. return ret;
  2001. }
  2002. #ifdef TARGET_NR_socketcall
  2003. /* do_socketcall() Must return target values and target errnos. */
  2004. static abi_long do_socketcall(int num, abi_ulong vptr)
  2005. {
  2006. static const unsigned ac[] = { /* number of arguments per call */
  2007. [SOCKOP_socket] = 3, /* domain, type, protocol */
  2008. [SOCKOP_bind] = 3, /* sockfd, addr, addrlen */
  2009. [SOCKOP_connect] = 3, /* sockfd, addr, addrlen */
  2010. [SOCKOP_listen] = 2, /* sockfd, backlog */
  2011. [SOCKOP_accept] = 3, /* sockfd, addr, addrlen */
  2012. [SOCKOP_accept4] = 4, /* sockfd, addr, addrlen, flags */
  2013. [SOCKOP_getsockname] = 3, /* sockfd, addr, addrlen */
  2014. [SOCKOP_getpeername] = 3, /* sockfd, addr, addrlen */
  2015. [SOCKOP_socketpair] = 4, /* domain, type, protocol, tab */
  2016. [SOCKOP_send] = 4, /* sockfd, msg, len, flags */
  2017. [SOCKOP_recv] = 4, /* sockfd, msg, len, flags */
  2018. [SOCKOP_sendto] = 6, /* sockfd, msg, len, flags, addr, addrlen */
  2019. [SOCKOP_recvfrom] = 6, /* sockfd, msg, len, flags, addr, addrlen */
  2020. [SOCKOP_shutdown] = 2, /* sockfd, how */
  2021. [SOCKOP_sendmsg] = 3, /* sockfd, msg, flags */
  2022. [SOCKOP_recvmsg] = 3, /* sockfd, msg, flags */
  2023. [SOCKOP_setsockopt] = 5, /* sockfd, level, optname, optval, optlen */
  2024. [SOCKOP_getsockopt] = 5, /* sockfd, level, optname, optval, optlen */
  2025. };
  2026. abi_long a[6]; /* max 6 args */
  2027. /* first, collect the arguments in a[] according to ac[] */
  2028. if (num >= 0 && num < ARRAY_SIZE(ac)) {
  2029. unsigned i;
  2030. assert(ARRAY_SIZE(a) >= ac[num]); /* ensure we have space for args */
  2031. for (i = 0; i < ac[num]; ++i) {
  2032. if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) {
  2033. return -TARGET_EFAULT;
  2034. }
  2035. }
  2036. }
  2037. /* now when we have the args, actually handle the call */
  2038. switch (num) {
  2039. case SOCKOP_socket: /* domain, type, protocol */
  2040. return do_socket(a[0], a[1], a[2]);
  2041. case SOCKOP_bind: /* sockfd, addr, addrlen */
  2042. return do_bind(a[0], a[1], a[2]);
  2043. case SOCKOP_connect: /* sockfd, addr, addrlen */
  2044. return do_connect(a[0], a[1], a[2]);
  2045. case SOCKOP_listen: /* sockfd, backlog */
  2046. return get_errno(listen(a[0], a[1]));
  2047. case SOCKOP_accept: /* sockfd, addr, addrlen */
  2048. return do_accept4(a[0], a[1], a[2], 0);
  2049. case SOCKOP_accept4: /* sockfd, addr, addrlen, flags */
  2050. return do_accept4(a[0], a[1], a[2], a[3]);
  2051. case SOCKOP_getsockname: /* sockfd, addr, addrlen */
  2052. return do_getsockname(a[0], a[1], a[2]);
  2053. case SOCKOP_getpeername: /* sockfd, addr, addrlen */
  2054. return do_getpeername(a[0], a[1], a[2]);
  2055. case SOCKOP_socketpair: /* domain, type, protocol, tab */
  2056. return do_socketpair(a[0], a[1], a[2], a[3]);
  2057. case SOCKOP_send: /* sockfd, msg, len, flags */
  2058. return do_sendto(a[0], a[1], a[2], a[3], 0, 0);
  2059. case SOCKOP_recv: /* sockfd, msg, len, flags */
  2060. return do_recvfrom(a[0], a[1], a[2], a[3], 0, 0);
  2061. case SOCKOP_sendto: /* sockfd, msg, len, flags, addr, addrlen */
  2062. return do_sendto(a[0], a[1], a[2], a[3], a[4], a[5]);
  2063. case SOCKOP_recvfrom: /* sockfd, msg, len, flags, addr, addrlen */
  2064. return do_recvfrom(a[0], a[1], a[2], a[3], a[4], a[5]);
  2065. case SOCKOP_shutdown: /* sockfd, how */
  2066. return get_errno(shutdown(a[0], a[1]));
  2067. case SOCKOP_sendmsg: /* sockfd, msg, flags */
  2068. return do_sendrecvmsg(a[0], a[1], a[2], 1);
  2069. case SOCKOP_recvmsg: /* sockfd, msg, flags */
  2070. return do_sendrecvmsg(a[0], a[1], a[2], 0);
  2071. case SOCKOP_setsockopt: /* sockfd, level, optname, optval, optlen */
  2072. return do_setsockopt(a[0], a[1], a[2], a[3], a[4]);
  2073. case SOCKOP_getsockopt: /* sockfd, level, optname, optval, optlen */
  2074. return do_getsockopt(a[0], a[1], a[2], a[3], a[4]);
  2075. default:
  2076. gemu_log("Unsupported socketcall: %d\n", num);
  2077. return -TARGET_ENOSYS;
  2078. }
  2079. }
  2080. #endif
  2081. #define N_SHM_REGIONS 32
  2082. static struct shm_region {
  2083. abi_ulong start;
  2084. abi_ulong size;
  2085. } shm_regions[N_SHM_REGIONS];
  2086. struct target_semid_ds
  2087. {
  2088. struct target_ipc_perm sem_perm;
  2089. abi_ulong sem_otime;
  2090. abi_ulong __unused1;
  2091. abi_ulong sem_ctime;
  2092. abi_ulong __unused2;
  2093. abi_ulong sem_nsems;
  2094. abi_ulong __unused3;
  2095. abi_ulong __unused4;
  2096. };
  2097. static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
  2098. abi_ulong target_addr)
  2099. {
  2100. struct target_ipc_perm *target_ip;
  2101. struct target_semid_ds *target_sd;
  2102. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  2103. return -TARGET_EFAULT;
  2104. target_ip = &(target_sd->sem_perm);
  2105. host_ip->__key = tswap32(target_ip->__key);
  2106. host_ip->uid = tswap32(target_ip->uid);
  2107. host_ip->gid = tswap32(target_ip->gid);
  2108. host_ip->cuid = tswap32(target_ip->cuid);
  2109. host_ip->cgid = tswap32(target_ip->cgid);
  2110. #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
  2111. host_ip->mode = tswap32(target_ip->mode);
  2112. #else
  2113. host_ip->mode = tswap16(target_ip->mode);
  2114. #endif
  2115. #if defined(TARGET_PPC)
  2116. host_ip->__seq = tswap32(target_ip->__seq);
  2117. #else
  2118. host_ip->__seq = tswap16(target_ip->__seq);
  2119. #endif
  2120. unlock_user_struct(target_sd, target_addr, 0);
  2121. return 0;
  2122. }
  2123. static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
  2124. struct ipc_perm *host_ip)
  2125. {
  2126. struct target_ipc_perm *target_ip;
  2127. struct target_semid_ds *target_sd;
  2128. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  2129. return -TARGET_EFAULT;
  2130. target_ip = &(target_sd->sem_perm);
  2131. target_ip->__key = tswap32(host_ip->__key);
  2132. target_ip->uid = tswap32(host_ip->uid);
  2133. target_ip->gid = tswap32(host_ip->gid);
  2134. target_ip->cuid = tswap32(host_ip->cuid);
  2135. target_ip->cgid = tswap32(host_ip->cgid);
  2136. #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
  2137. target_ip->mode = tswap32(host_ip->mode);
  2138. #else
  2139. target_ip->mode = tswap16(host_ip->mode);
  2140. #endif
  2141. #if defined(TARGET_PPC)
  2142. target_ip->__seq = tswap32(host_ip->__seq);
  2143. #else
  2144. target_ip->__seq = tswap16(host_ip->__seq);
  2145. #endif
  2146. unlock_user_struct(target_sd, target_addr, 1);
  2147. return 0;
  2148. }
  2149. static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
  2150. abi_ulong target_addr)
  2151. {
  2152. struct target_semid_ds *target_sd;
  2153. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  2154. return -TARGET_EFAULT;
  2155. if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
  2156. return -TARGET_EFAULT;
  2157. host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
  2158. host_sd->sem_otime = tswapal(target_sd->sem_otime);
  2159. host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
  2160. unlock_user_struct(target_sd, target_addr, 0);
  2161. return 0;
  2162. }
  2163. static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
  2164. struct semid_ds *host_sd)
  2165. {
  2166. struct target_semid_ds *target_sd;
  2167. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  2168. return -TARGET_EFAULT;
  2169. if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
  2170. return -TARGET_EFAULT;
  2171. target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
  2172. target_sd->sem_otime = tswapal(host_sd->sem_otime);
  2173. target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
  2174. unlock_user_struct(target_sd, target_addr, 1);
  2175. return 0;
  2176. }
  2177. struct target_seminfo {
  2178. int semmap;
  2179. int semmni;
  2180. int semmns;
  2181. int semmnu;
  2182. int semmsl;
  2183. int semopm;
  2184. int semume;
  2185. int semusz;
  2186. int semvmx;
  2187. int semaem;
  2188. };
  2189. static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
  2190. struct seminfo *host_seminfo)
  2191. {
  2192. struct target_seminfo *target_seminfo;
  2193. if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
  2194. return -TARGET_EFAULT;
  2195. __put_user(host_seminfo->semmap, &target_seminfo->semmap);
  2196. __put_user(host_seminfo->semmni, &target_seminfo->semmni);
  2197. __put_user(host_seminfo->semmns, &target_seminfo->semmns);
  2198. __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
  2199. __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
  2200. __put_user(host_seminfo->semopm, &target_seminfo->semopm);
  2201. __put_user(host_seminfo->semume, &target_seminfo->semume);
  2202. __put_user(host_seminfo->semusz, &target_seminfo->semusz);
  2203. __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
  2204. __put_user(host_seminfo->semaem, &target_seminfo->semaem);
  2205. unlock_user_struct(target_seminfo, target_addr, 1);
  2206. return 0;
  2207. }
  2208. union semun {
  2209. int val;
  2210. struct semid_ds *buf;
  2211. unsigned short *array;
  2212. struct seminfo *__buf;
  2213. };
  2214. union target_semun {
  2215. int val;
  2216. abi_ulong buf;
  2217. abi_ulong array;
  2218. abi_ulong __buf;
  2219. };
  2220. static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
  2221. abi_ulong target_addr)
  2222. {
  2223. int nsems;
  2224. unsigned short *array;
  2225. union semun semun;
  2226. struct semid_ds semid_ds;
  2227. int i, ret;
  2228. semun.buf = &semid_ds;
  2229. ret = semctl(semid, 0, IPC_STAT, semun);
  2230. if (ret == -1)
  2231. return get_errno(ret);
  2232. nsems = semid_ds.sem_nsems;
  2233. *host_array = malloc(nsems*sizeof(unsigned short));
  2234. if (!*host_array) {
  2235. return -TARGET_ENOMEM;
  2236. }
  2237. array = lock_user(VERIFY_READ, target_addr,
  2238. nsems*sizeof(unsigned short), 1);
  2239. if (!array) {
  2240. free(*host_array);
  2241. return -TARGET_EFAULT;
  2242. }
  2243. for(i=0; i<nsems; i++) {
  2244. __get_user((*host_array)[i], &array[i]);
  2245. }
  2246. unlock_user(array, target_addr, 0);
  2247. return 0;
  2248. }
  2249. static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
  2250. unsigned short **host_array)
  2251. {
  2252. int nsems;
  2253. unsigned short *array;
  2254. union semun semun;
  2255. struct semid_ds semid_ds;
  2256. int i, ret;
  2257. semun.buf = &semid_ds;
  2258. ret = semctl(semid, 0, IPC_STAT, semun);
  2259. if (ret == -1)
  2260. return get_errno(ret);
  2261. nsems = semid_ds.sem_nsems;
  2262. array = lock_user(VERIFY_WRITE, target_addr,
  2263. nsems*sizeof(unsigned short), 0);
  2264. if (!array)
  2265. return -TARGET_EFAULT;
  2266. for(i=0; i<nsems; i++) {
  2267. __put_user((*host_array)[i], &array[i]);
  2268. }
  2269. free(*host_array);
  2270. unlock_user(array, target_addr, 1);
  2271. return 0;
  2272. }
  2273. static inline abi_long do_semctl(int semid, int semnum, int cmd,
  2274. union target_semun target_su)
  2275. {
  2276. union semun arg;
  2277. struct semid_ds dsarg;
  2278. unsigned short *array = NULL;
  2279. struct seminfo seminfo;
  2280. abi_long ret = -TARGET_EINVAL;
  2281. abi_long err;
  2282. cmd &= 0xff;
  2283. switch( cmd ) {
  2284. case GETVAL:
  2285. case SETVAL:
  2286. arg.val = tswap32(target_su.val);
  2287. ret = get_errno(semctl(semid, semnum, cmd, arg));
  2288. target_su.val = tswap32(arg.val);
  2289. break;
  2290. case GETALL:
  2291. case SETALL:
  2292. err = target_to_host_semarray(semid, &array, target_su.array);
  2293. if (err)
  2294. return err;
  2295. arg.array = array;
  2296. ret = get_errno(semctl(semid, semnum, cmd, arg));
  2297. err = host_to_target_semarray(semid, target_su.array, &array);
  2298. if (err)
  2299. return err;
  2300. break;
  2301. case IPC_STAT:
  2302. case IPC_SET:
  2303. case SEM_STAT:
  2304. err = target_to_host_semid_ds(&dsarg, target_su.buf);
  2305. if (err)
  2306. return err;
  2307. arg.buf = &dsarg;
  2308. ret = get_errno(semctl(semid, semnum, cmd, arg));
  2309. err = host_to_target_semid_ds(target_su.buf, &dsarg);
  2310. if (err)
  2311. return err;
  2312. break;
  2313. case IPC_INFO:
  2314. case SEM_INFO:
  2315. arg.__buf = &seminfo;
  2316. ret = get_errno(semctl(semid, semnum, cmd, arg));
  2317. err = host_to_target_seminfo(target_su.__buf, &seminfo);
  2318. if (err)
  2319. return err;
  2320. break;
  2321. case IPC_RMID:
  2322. case GETPID:
  2323. case GETNCNT:
  2324. case GETZCNT:
  2325. ret = get_errno(semctl(semid, semnum, cmd, NULL));
  2326. break;
  2327. }
  2328. return ret;
  2329. }
  2330. struct target_sembuf {
  2331. unsigned short sem_num;
  2332. short sem_op;
  2333. short sem_flg;
  2334. };
  2335. static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
  2336. abi_ulong target_addr,
  2337. unsigned nsops)
  2338. {
  2339. struct target_sembuf *target_sembuf;
  2340. int i;
  2341. target_sembuf = lock_user(VERIFY_READ, target_addr,
  2342. nsops*sizeof(struct target_sembuf), 1);
  2343. if (!target_sembuf)
  2344. return -TARGET_EFAULT;
  2345. for(i=0; i<nsops; i++) {
  2346. __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
  2347. __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
  2348. __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
  2349. }
  2350. unlock_user(target_sembuf, target_addr, 0);
  2351. return 0;
  2352. }
  2353. static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
  2354. {
  2355. struct sembuf sops[nsops];
  2356. if (target_to_host_sembuf(sops, ptr, nsops))
  2357. return -TARGET_EFAULT;
  2358. return get_errno(semop(semid, sops, nsops));
  2359. }
  2360. struct target_msqid_ds
  2361. {
  2362. struct target_ipc_perm msg_perm;
  2363. abi_ulong msg_stime;
  2364. #if TARGET_ABI_BITS == 32
  2365. abi_ulong __unused1;
  2366. #endif
  2367. abi_ulong msg_rtime;
  2368. #if TARGET_ABI_BITS == 32
  2369. abi_ulong __unused2;
  2370. #endif
  2371. abi_ulong msg_ctime;
  2372. #if TARGET_ABI_BITS == 32
  2373. abi_ulong __unused3;
  2374. #endif
  2375. abi_ulong __msg_cbytes;
  2376. abi_ulong msg_qnum;
  2377. abi_ulong msg_qbytes;
  2378. abi_ulong msg_lspid;
  2379. abi_ulong msg_lrpid;
  2380. abi_ulong __unused4;
  2381. abi_ulong __unused5;
  2382. };
  2383. static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
  2384. abi_ulong target_addr)
  2385. {
  2386. struct target_msqid_ds *target_md;
  2387. if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
  2388. return -TARGET_EFAULT;
  2389. if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
  2390. return -TARGET_EFAULT;
  2391. host_md->msg_stime = tswapal(target_md->msg_stime);
  2392. host_md->msg_rtime = tswapal(target_md->msg_rtime);
  2393. host_md->msg_ctime = tswapal(target_md->msg_ctime);
  2394. host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
  2395. host_md->msg_qnum = tswapal(target_md->msg_qnum);
  2396. host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
  2397. host_md->msg_lspid = tswapal(target_md->msg_lspid);
  2398. host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
  2399. unlock_user_struct(target_md, target_addr, 0);
  2400. return 0;
  2401. }
  2402. static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
  2403. struct msqid_ds *host_md)
  2404. {
  2405. struct target_msqid_ds *target_md;
  2406. if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
  2407. return -TARGET_EFAULT;
  2408. if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
  2409. return -TARGET_EFAULT;
  2410. target_md->msg_stime = tswapal(host_md->msg_stime);
  2411. target_md->msg_rtime = tswapal(host_md->msg_rtime);
  2412. target_md->msg_ctime = tswapal(host_md->msg_ctime);
  2413. target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
  2414. target_md->msg_qnum = tswapal(host_md->msg_qnum);
  2415. target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
  2416. target_md->msg_lspid = tswapal(host_md->msg_lspid);
  2417. target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
  2418. unlock_user_struct(target_md, target_addr, 1);
  2419. return 0;
  2420. }
  2421. struct target_msginfo {
  2422. int msgpool;
  2423. int msgmap;
  2424. int msgmax;
  2425. int msgmnb;
  2426. int msgmni;
  2427. int msgssz;
  2428. int msgtql;
  2429. unsigned short int msgseg;
  2430. };
  2431. static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
  2432. struct msginfo *host_msginfo)
  2433. {
  2434. struct target_msginfo *target_msginfo;
  2435. if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
  2436. return -TARGET_EFAULT;
  2437. __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
  2438. __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
  2439. __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
  2440. __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
  2441. __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
  2442. __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
  2443. __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
  2444. __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
  2445. unlock_user_struct(target_msginfo, target_addr, 1);
  2446. return 0;
  2447. }
  2448. static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
  2449. {
  2450. struct msqid_ds dsarg;
  2451. struct msginfo msginfo;
  2452. abi_long ret = -TARGET_EINVAL;
  2453. cmd &= 0xff;
  2454. switch (cmd) {
  2455. case IPC_STAT:
  2456. case IPC_SET:
  2457. case MSG_STAT:
  2458. if (target_to_host_msqid_ds(&dsarg,ptr))
  2459. return -TARGET_EFAULT;
  2460. ret = get_errno(msgctl(msgid, cmd, &dsarg));
  2461. if (host_to_target_msqid_ds(ptr,&dsarg))
  2462. return -TARGET_EFAULT;
  2463. break;
  2464. case IPC_RMID:
  2465. ret = get_errno(msgctl(msgid, cmd, NULL));
  2466. break;
  2467. case IPC_INFO:
  2468. case MSG_INFO:
  2469. ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
  2470. if (host_to_target_msginfo(ptr, &msginfo))
  2471. return -TARGET_EFAULT;
  2472. break;
  2473. }
  2474. return ret;
  2475. }
  2476. struct target_msgbuf {
  2477. abi_long mtype;
  2478. char mtext[1];
  2479. };
  2480. static inline abi_long do_msgsnd(int msqid, abi_long msgp,
  2481. unsigned int msgsz, int msgflg)
  2482. {
  2483. struct target_msgbuf *target_mb;
  2484. struct msgbuf *host_mb;
  2485. abi_long ret = 0;
  2486. if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
  2487. return -TARGET_EFAULT;
  2488. host_mb = malloc(msgsz+sizeof(long));
  2489. host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
  2490. memcpy(host_mb->mtext, target_mb->mtext, msgsz);
  2491. ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
  2492. free(host_mb);
  2493. unlock_user_struct(target_mb, msgp, 0);
  2494. return ret;
  2495. }
  2496. static inline abi_long do_msgrcv(int msqid, abi_long msgp,
  2497. unsigned int msgsz, abi_long msgtyp,
  2498. int msgflg)
  2499. {
  2500. struct target_msgbuf *target_mb;
  2501. char *target_mtext;
  2502. struct msgbuf *host_mb;
  2503. abi_long ret = 0;
  2504. if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
  2505. return -TARGET_EFAULT;
  2506. host_mb = g_malloc(msgsz+sizeof(long));
  2507. ret = get_errno(msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
  2508. if (ret > 0) {
  2509. abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
  2510. target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
  2511. if (!target_mtext) {
  2512. ret = -TARGET_EFAULT;
  2513. goto end;
  2514. }
  2515. memcpy(target_mb->mtext, host_mb->mtext, ret);
  2516. unlock_user(target_mtext, target_mtext_addr, ret);
  2517. }
  2518. target_mb->mtype = tswapal(host_mb->mtype);
  2519. end:
  2520. if (target_mb)
  2521. unlock_user_struct(target_mb, msgp, 1);
  2522. g_free(host_mb);
  2523. return ret;
  2524. }
  2525. static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
  2526. abi_ulong target_addr)
  2527. {
  2528. struct target_shmid_ds *target_sd;
  2529. if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
  2530. return -TARGET_EFAULT;
  2531. if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
  2532. return -TARGET_EFAULT;
  2533. __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
  2534. __get_user(host_sd->shm_atime, &target_sd->shm_atime);
  2535. __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
  2536. __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
  2537. __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
  2538. __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
  2539. __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
  2540. unlock_user_struct(target_sd, target_addr, 0);
  2541. return 0;
  2542. }
  2543. static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
  2544. struct shmid_ds *host_sd)
  2545. {
  2546. struct target_shmid_ds *target_sd;
  2547. if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
  2548. return -TARGET_EFAULT;
  2549. if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
  2550. return -TARGET_EFAULT;
  2551. __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
  2552. __put_user(host_sd->shm_atime, &target_sd->shm_atime);
  2553. __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
  2554. __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
  2555. __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
  2556. __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
  2557. __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
  2558. unlock_user_struct(target_sd, target_addr, 1);
  2559. return 0;
  2560. }
  2561. struct target_shminfo {
  2562. abi_ulong shmmax;
  2563. abi_ulong shmmin;
  2564. abi_ulong shmmni;
  2565. abi_ulong shmseg;
  2566. abi_ulong shmall;
  2567. };
  2568. static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
  2569. struct shminfo *host_shminfo)
  2570. {
  2571. struct target_shminfo *target_shminfo;
  2572. if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
  2573. return -TARGET_EFAULT;
  2574. __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
  2575. __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
  2576. __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
  2577. __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
  2578. __put_user(host_shminfo->shmall, &target_shminfo->shmall);
  2579. unlock_user_struct(target_shminfo, target_addr, 1);
  2580. return 0;
  2581. }
  2582. struct target_shm_info {
  2583. int used_ids;
  2584. abi_ulong shm_tot;
  2585. abi_ulong shm_rss;
  2586. abi_ulong shm_swp;
  2587. abi_ulong swap_attempts;
  2588. abi_ulong swap_successes;
  2589. };
  2590. static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
  2591. struct shm_info *host_shm_info)
  2592. {
  2593. struct target_shm_info *target_shm_info;
  2594. if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
  2595. return -TARGET_EFAULT;
  2596. __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
  2597. __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
  2598. __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
  2599. __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
  2600. __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
  2601. __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
  2602. unlock_user_struct(target_shm_info, target_addr, 1);
  2603. return 0;
  2604. }
  2605. static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
  2606. {
  2607. struct shmid_ds dsarg;
  2608. struct shminfo shminfo;
  2609. struct shm_info shm_info;
  2610. abi_long ret = -TARGET_EINVAL;
  2611. cmd &= 0xff;
  2612. switch(cmd) {
  2613. case IPC_STAT:
  2614. case IPC_SET:
  2615. case SHM_STAT:
  2616. if (target_to_host_shmid_ds(&dsarg, buf))
  2617. return -TARGET_EFAULT;
  2618. ret = get_errno(shmctl(shmid, cmd, &dsarg));
  2619. if (host_to_target_shmid_ds(buf, &dsarg))
  2620. return -TARGET_EFAULT;
  2621. break;
  2622. case IPC_INFO:
  2623. ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
  2624. if (host_to_target_shminfo(buf, &shminfo))
  2625. return -TARGET_EFAULT;
  2626. break;
  2627. case SHM_INFO:
  2628. ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
  2629. if (host_to_target_shm_info(buf, &shm_info))
  2630. return -TARGET_EFAULT;
  2631. break;
  2632. case IPC_RMID:
  2633. case SHM_LOCK:
  2634. case SHM_UNLOCK:
  2635. ret = get_errno(shmctl(shmid, cmd, NULL));
  2636. break;
  2637. }
  2638. return ret;
  2639. }
  2640. static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg)
  2641. {
  2642. abi_long raddr;
  2643. void *host_raddr;
  2644. struct shmid_ds shm_info;
  2645. int i,ret;
  2646. /* find out the length of the shared memory segment */
  2647. ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
  2648. if (is_error(ret)) {
  2649. /* can't get length, bail out */
  2650. return ret;
  2651. }
  2652. mmap_lock();
  2653. if (shmaddr)
  2654. host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
  2655. else {
  2656. abi_ulong mmap_start;
  2657. mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
  2658. if (mmap_start == -1) {
  2659. errno = ENOMEM;
  2660. host_raddr = (void *)-1;
  2661. } else
  2662. host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
  2663. }
  2664. if (host_raddr == (void *)-1) {
  2665. mmap_unlock();
  2666. return get_errno((long)host_raddr);
  2667. }
  2668. raddr=h2g((unsigned long)host_raddr);
  2669. page_set_flags(raddr, raddr + shm_info.shm_segsz,
  2670. PAGE_VALID | PAGE_READ |
  2671. ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
  2672. for (i = 0; i < N_SHM_REGIONS; i++) {
  2673. if (shm_regions[i].start == 0) {
  2674. shm_regions[i].start = raddr;
  2675. shm_regions[i].size = shm_info.shm_segsz;
  2676. break;
  2677. }
  2678. }
  2679. mmap_unlock();
  2680. return raddr;
  2681. }
  2682. static inline abi_long do_shmdt(abi_ulong shmaddr)
  2683. {
  2684. int i;
  2685. for (i = 0; i < N_SHM_REGIONS; ++i) {
  2686. if (shm_regions[i].start == shmaddr) {
  2687. shm_regions[i].start = 0;
  2688. page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
  2689. break;
  2690. }
  2691. }
  2692. return get_errno(shmdt(g2h(shmaddr)));
  2693. }
  2694. #ifdef TARGET_NR_ipc
  2695. /* ??? This only works with linear mappings. */
  2696. /* do_ipc() must return target values and target errnos. */
  2697. static abi_long do_ipc(unsigned int call, int first,
  2698. int second, int third,
  2699. abi_long ptr, abi_long fifth)
  2700. {
  2701. int version;
  2702. abi_long ret = 0;
  2703. version = call >> 16;
  2704. call &= 0xffff;
  2705. switch (call) {
  2706. case IPCOP_semop:
  2707. ret = do_semop(first, ptr, second);
  2708. break;
  2709. case IPCOP_semget:
  2710. ret = get_errno(semget(first, second, third));
  2711. break;
  2712. case IPCOP_semctl:
  2713. ret = do_semctl(first, second, third, (union target_semun)(abi_ulong) ptr);
  2714. break;
  2715. case IPCOP_msgget:
  2716. ret = get_errno(msgget(first, second));
  2717. break;
  2718. case IPCOP_msgsnd:
  2719. ret = do_msgsnd(first, ptr, second, third);
  2720. break;
  2721. case IPCOP_msgctl:
  2722. ret = do_msgctl(first, second, ptr);
  2723. break;
  2724. case IPCOP_msgrcv:
  2725. switch (version) {
  2726. case 0:
  2727. {
  2728. struct target_ipc_kludge {
  2729. abi_long msgp;
  2730. abi_long msgtyp;
  2731. } *tmp;
  2732. if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
  2733. ret = -TARGET_EFAULT;
  2734. break;
  2735. }
  2736. ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
  2737. unlock_user_struct(tmp, ptr, 0);
  2738. break;
  2739. }
  2740. default:
  2741. ret = do_msgrcv(first, ptr, second, fifth, third);
  2742. }
  2743. break;
  2744. case IPCOP_shmat:
  2745. switch (version) {
  2746. default:
  2747. {
  2748. abi_ulong raddr;
  2749. raddr = do_shmat(first, ptr, second);
  2750. if (is_error(raddr))
  2751. return get_errno(raddr);
  2752. if (put_user_ual(raddr, third))
  2753. return -TARGET_EFAULT;
  2754. break;
  2755. }
  2756. case 1:
  2757. ret = -TARGET_EINVAL;
  2758. break;
  2759. }
  2760. break;
  2761. case IPCOP_shmdt:
  2762. ret = do_shmdt(ptr);
  2763. break;
  2764. case IPCOP_shmget:
  2765. /* IPC_* flag values are the same on all linux platforms */
  2766. ret = get_errno(shmget(first, second, third));
  2767. break;
  2768. /* IPC_* and SHM_* command values are the same on all linux platforms */
  2769. case IPCOP_shmctl:
  2770. ret = do_shmctl(first, second, ptr);
  2771. break;
  2772. default:
  2773. gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
  2774. ret = -TARGET_ENOSYS;
  2775. break;
  2776. }
  2777. return ret;
  2778. }
  2779. #endif
  2780. /* kernel structure types definitions */
  2781. #define STRUCT(name, ...) STRUCT_ ## name,
  2782. #define STRUCT_SPECIAL(name) STRUCT_ ## name,
  2783. enum {
  2784. #include "syscall_types.h"
  2785. };
  2786. #undef STRUCT
  2787. #undef STRUCT_SPECIAL
  2788. #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
  2789. #define STRUCT_SPECIAL(name)
  2790. #include "syscall_types.h"
  2791. #undef STRUCT
  2792. #undef STRUCT_SPECIAL
  2793. typedef struct IOCTLEntry IOCTLEntry;
  2794. typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
  2795. int fd, abi_long cmd, abi_long arg);
  2796. struct IOCTLEntry {
  2797. unsigned int target_cmd;
  2798. unsigned int host_cmd;
  2799. const char *name;
  2800. int access;
  2801. do_ioctl_fn *do_ioctl;
  2802. const argtype arg_type[5];
  2803. };
  2804. #define IOC_R 0x0001
  2805. #define IOC_W 0x0002
  2806. #define IOC_RW (IOC_R | IOC_W)
  2807. #define MAX_STRUCT_SIZE 4096
  2808. #ifdef CONFIG_FIEMAP
  2809. /* So fiemap access checks don't overflow on 32 bit systems.
  2810. * This is very slightly smaller than the limit imposed by
  2811. * the underlying kernel.
  2812. */
  2813. #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
  2814. / sizeof(struct fiemap_extent))
  2815. static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
  2816. int fd, abi_long cmd, abi_long arg)
  2817. {
  2818. /* The parameter for this ioctl is a struct fiemap followed
  2819. * by an array of struct fiemap_extent whose size is set
  2820. * in fiemap->fm_extent_count. The array is filled in by the
  2821. * ioctl.
  2822. */
  2823. int target_size_in, target_size_out;
  2824. struct fiemap *fm;
  2825. const argtype *arg_type = ie->arg_type;
  2826. const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
  2827. void *argptr, *p;
  2828. abi_long ret;
  2829. int i, extent_size = thunk_type_size(extent_arg_type, 0);
  2830. uint32_t outbufsz;
  2831. int free_fm = 0;
  2832. assert(arg_type[0] == TYPE_PTR);
  2833. assert(ie->access == IOC_RW);
  2834. arg_type++;
  2835. target_size_in = thunk_type_size(arg_type, 0);
  2836. argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
  2837. if (!argptr) {
  2838. return -TARGET_EFAULT;
  2839. }
  2840. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  2841. unlock_user(argptr, arg, 0);
  2842. fm = (struct fiemap *)buf_temp;
  2843. if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
  2844. return -TARGET_EINVAL;
  2845. }
  2846. outbufsz = sizeof (*fm) +
  2847. (sizeof(struct fiemap_extent) * fm->fm_extent_count);
  2848. if (outbufsz > MAX_STRUCT_SIZE) {
  2849. /* We can't fit all the extents into the fixed size buffer.
  2850. * Allocate one that is large enough and use it instead.
  2851. */
  2852. fm = malloc(outbufsz);
  2853. if (!fm) {
  2854. return -TARGET_ENOMEM;
  2855. }
  2856. memcpy(fm, buf_temp, sizeof(struct fiemap));
  2857. free_fm = 1;
  2858. }
  2859. ret = get_errno(ioctl(fd, ie->host_cmd, fm));
  2860. if (!is_error(ret)) {
  2861. target_size_out = target_size_in;
  2862. /* An extent_count of 0 means we were only counting the extents
  2863. * so there are no structs to copy
  2864. */
  2865. if (fm->fm_extent_count != 0) {
  2866. target_size_out += fm->fm_mapped_extents * extent_size;
  2867. }
  2868. argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
  2869. if (!argptr) {
  2870. ret = -TARGET_EFAULT;
  2871. } else {
  2872. /* Convert the struct fiemap */
  2873. thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
  2874. if (fm->fm_extent_count != 0) {
  2875. p = argptr + target_size_in;
  2876. /* ...and then all the struct fiemap_extents */
  2877. for (i = 0; i < fm->fm_mapped_extents; i++) {
  2878. thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
  2879. THUNK_TARGET);
  2880. p += extent_size;
  2881. }
  2882. }
  2883. unlock_user(argptr, arg, target_size_out);
  2884. }
  2885. }
  2886. if (free_fm) {
  2887. free(fm);
  2888. }
  2889. return ret;
  2890. }
  2891. #endif
  2892. static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
  2893. int fd, abi_long cmd, abi_long arg)
  2894. {
  2895. const argtype *arg_type = ie->arg_type;
  2896. int target_size;
  2897. void *argptr;
  2898. int ret;
  2899. struct ifconf *host_ifconf;
  2900. uint32_t outbufsz;
  2901. const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
  2902. int target_ifreq_size;
  2903. int nb_ifreq;
  2904. int free_buf = 0;
  2905. int i;
  2906. int target_ifc_len;
  2907. abi_long target_ifc_buf;
  2908. int host_ifc_len;
  2909. char *host_ifc_buf;
  2910. assert(arg_type[0] == TYPE_PTR);
  2911. assert(ie->access == IOC_RW);
  2912. arg_type++;
  2913. target_size = thunk_type_size(arg_type, 0);
  2914. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  2915. if (!argptr)
  2916. return -TARGET_EFAULT;
  2917. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  2918. unlock_user(argptr, arg, 0);
  2919. host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
  2920. target_ifc_len = host_ifconf->ifc_len;
  2921. target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
  2922. target_ifreq_size = thunk_type_size(ifreq_arg_type, 0);
  2923. nb_ifreq = target_ifc_len / target_ifreq_size;
  2924. host_ifc_len = nb_ifreq * sizeof(struct ifreq);
  2925. outbufsz = sizeof(*host_ifconf) + host_ifc_len;
  2926. if (outbufsz > MAX_STRUCT_SIZE) {
  2927. /* We can't fit all the extents into the fixed size buffer.
  2928. * Allocate one that is large enough and use it instead.
  2929. */
  2930. host_ifconf = malloc(outbufsz);
  2931. if (!host_ifconf) {
  2932. return -TARGET_ENOMEM;
  2933. }
  2934. memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
  2935. free_buf = 1;
  2936. }
  2937. host_ifc_buf = (char*)host_ifconf + sizeof(*host_ifconf);
  2938. host_ifconf->ifc_len = host_ifc_len;
  2939. host_ifconf->ifc_buf = host_ifc_buf;
  2940. ret = get_errno(ioctl(fd, ie->host_cmd, host_ifconf));
  2941. if (!is_error(ret)) {
  2942. /* convert host ifc_len to target ifc_len */
  2943. nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
  2944. target_ifc_len = nb_ifreq * target_ifreq_size;
  2945. host_ifconf->ifc_len = target_ifc_len;
  2946. /* restore target ifc_buf */
  2947. host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
  2948. /* copy struct ifconf to target user */
  2949. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  2950. if (!argptr)
  2951. return -TARGET_EFAULT;
  2952. thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
  2953. unlock_user(argptr, arg, target_size);
  2954. /* copy ifreq[] to target user */
  2955. argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
  2956. for (i = 0; i < nb_ifreq ; i++) {
  2957. thunk_convert(argptr + i * target_ifreq_size,
  2958. host_ifc_buf + i * sizeof(struct ifreq),
  2959. ifreq_arg_type, THUNK_TARGET);
  2960. }
  2961. unlock_user(argptr, target_ifc_buf, target_ifc_len);
  2962. }
  2963. if (free_buf) {
  2964. free(host_ifconf);
  2965. }
  2966. return ret;
  2967. }
  2968. static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
  2969. abi_long cmd, abi_long arg)
  2970. {
  2971. void *argptr;
  2972. struct dm_ioctl *host_dm;
  2973. abi_long guest_data;
  2974. uint32_t guest_data_size;
  2975. int target_size;
  2976. const argtype *arg_type = ie->arg_type;
  2977. abi_long ret;
  2978. void *big_buf = NULL;
  2979. char *host_data;
  2980. arg_type++;
  2981. target_size = thunk_type_size(arg_type, 0);
  2982. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  2983. if (!argptr) {
  2984. ret = -TARGET_EFAULT;
  2985. goto out;
  2986. }
  2987. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  2988. unlock_user(argptr, arg, 0);
  2989. /* buf_temp is too small, so fetch things into a bigger buffer */
  2990. big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
  2991. memcpy(big_buf, buf_temp, target_size);
  2992. buf_temp = big_buf;
  2993. host_dm = big_buf;
  2994. guest_data = arg + host_dm->data_start;
  2995. if ((guest_data - arg) < 0) {
  2996. ret = -EINVAL;
  2997. goto out;
  2998. }
  2999. guest_data_size = host_dm->data_size - host_dm->data_start;
  3000. host_data = (char*)host_dm + host_dm->data_start;
  3001. argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
  3002. switch (ie->host_cmd) {
  3003. case DM_REMOVE_ALL:
  3004. case DM_LIST_DEVICES:
  3005. case DM_DEV_CREATE:
  3006. case DM_DEV_REMOVE:
  3007. case DM_DEV_SUSPEND:
  3008. case DM_DEV_STATUS:
  3009. case DM_DEV_WAIT:
  3010. case DM_TABLE_STATUS:
  3011. case DM_TABLE_CLEAR:
  3012. case DM_TABLE_DEPS:
  3013. case DM_LIST_VERSIONS:
  3014. /* no input data */
  3015. break;
  3016. case DM_DEV_RENAME:
  3017. case DM_DEV_SET_GEOMETRY:
  3018. /* data contains only strings */
  3019. memcpy(host_data, argptr, guest_data_size);
  3020. break;
  3021. case DM_TARGET_MSG:
  3022. memcpy(host_data, argptr, guest_data_size);
  3023. *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
  3024. break;
  3025. case DM_TABLE_LOAD:
  3026. {
  3027. void *gspec = argptr;
  3028. void *cur_data = host_data;
  3029. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
  3030. int spec_size = thunk_type_size(arg_type, 0);
  3031. int i;
  3032. for (i = 0; i < host_dm->target_count; i++) {
  3033. struct dm_target_spec *spec = cur_data;
  3034. uint32_t next;
  3035. int slen;
  3036. thunk_convert(spec, gspec, arg_type, THUNK_HOST);
  3037. slen = strlen((char*)gspec + spec_size) + 1;
  3038. next = spec->next;
  3039. spec->next = sizeof(*spec) + slen;
  3040. strcpy((char*)&spec[1], gspec + spec_size);
  3041. gspec += next;
  3042. cur_data += spec->next;
  3043. }
  3044. break;
  3045. }
  3046. default:
  3047. ret = -TARGET_EINVAL;
  3048. goto out;
  3049. }
  3050. unlock_user(argptr, guest_data, 0);
  3051. ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
  3052. if (!is_error(ret)) {
  3053. guest_data = arg + host_dm->data_start;
  3054. guest_data_size = host_dm->data_size - host_dm->data_start;
  3055. argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
  3056. switch (ie->host_cmd) {
  3057. case DM_REMOVE_ALL:
  3058. case DM_DEV_CREATE:
  3059. case DM_DEV_REMOVE:
  3060. case DM_DEV_RENAME:
  3061. case DM_DEV_SUSPEND:
  3062. case DM_DEV_STATUS:
  3063. case DM_TABLE_LOAD:
  3064. case DM_TABLE_CLEAR:
  3065. case DM_TARGET_MSG:
  3066. case DM_DEV_SET_GEOMETRY:
  3067. /* no return data */
  3068. break;
  3069. case DM_LIST_DEVICES:
  3070. {
  3071. struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
  3072. uint32_t remaining_data = guest_data_size;
  3073. void *cur_data = argptr;
  3074. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
  3075. int nl_size = 12; /* can't use thunk_size due to alignment */
  3076. while (1) {
  3077. uint32_t next = nl->next;
  3078. if (next) {
  3079. nl->next = nl_size + (strlen(nl->name) + 1);
  3080. }
  3081. if (remaining_data < nl->next) {
  3082. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  3083. break;
  3084. }
  3085. thunk_convert(cur_data, nl, arg_type, THUNK_TARGET);
  3086. strcpy(cur_data + nl_size, nl->name);
  3087. cur_data += nl->next;
  3088. remaining_data -= nl->next;
  3089. if (!next) {
  3090. break;
  3091. }
  3092. nl = (void*)nl + next;
  3093. }
  3094. break;
  3095. }
  3096. case DM_DEV_WAIT:
  3097. case DM_TABLE_STATUS:
  3098. {
  3099. struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
  3100. void *cur_data = argptr;
  3101. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
  3102. int spec_size = thunk_type_size(arg_type, 0);
  3103. int i;
  3104. for (i = 0; i < host_dm->target_count; i++) {
  3105. uint32_t next = spec->next;
  3106. int slen = strlen((char*)&spec[1]) + 1;
  3107. spec->next = (cur_data - argptr) + spec_size + slen;
  3108. if (guest_data_size < spec->next) {
  3109. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  3110. break;
  3111. }
  3112. thunk_convert(cur_data, spec, arg_type, THUNK_TARGET);
  3113. strcpy(cur_data + spec_size, (char*)&spec[1]);
  3114. cur_data = argptr + spec->next;
  3115. spec = (void*)host_dm + host_dm->data_start + next;
  3116. }
  3117. break;
  3118. }
  3119. case DM_TABLE_DEPS:
  3120. {
  3121. void *hdata = (void*)host_dm + host_dm->data_start;
  3122. int count = *(uint32_t*)hdata;
  3123. uint64_t *hdev = hdata + 8;
  3124. uint64_t *gdev = argptr + 8;
  3125. int i;
  3126. *(uint32_t*)argptr = tswap32(count);
  3127. for (i = 0; i < count; i++) {
  3128. *gdev = tswap64(*hdev);
  3129. gdev++;
  3130. hdev++;
  3131. }
  3132. break;
  3133. }
  3134. case DM_LIST_VERSIONS:
  3135. {
  3136. struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
  3137. uint32_t remaining_data = guest_data_size;
  3138. void *cur_data = argptr;
  3139. const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
  3140. int vers_size = thunk_type_size(arg_type, 0);
  3141. while (1) {
  3142. uint32_t next = vers->next;
  3143. if (next) {
  3144. vers->next = vers_size + (strlen(vers->name) + 1);
  3145. }
  3146. if (remaining_data < vers->next) {
  3147. host_dm->flags |= DM_BUFFER_FULL_FLAG;
  3148. break;
  3149. }
  3150. thunk_convert(cur_data, vers, arg_type, THUNK_TARGET);
  3151. strcpy(cur_data + vers_size, vers->name);
  3152. cur_data += vers->next;
  3153. remaining_data -= vers->next;
  3154. if (!next) {
  3155. break;
  3156. }
  3157. vers = (void*)vers + next;
  3158. }
  3159. break;
  3160. }
  3161. default:
  3162. ret = -TARGET_EINVAL;
  3163. goto out;
  3164. }
  3165. unlock_user(argptr, guest_data, guest_data_size);
  3166. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  3167. if (!argptr) {
  3168. ret = -TARGET_EFAULT;
  3169. goto out;
  3170. }
  3171. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  3172. unlock_user(argptr, arg, target_size);
  3173. }
  3174. out:
  3175. g_free(big_buf);
  3176. return ret;
  3177. }
  3178. static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
  3179. int fd, abi_long cmd, abi_long arg)
  3180. {
  3181. const argtype *arg_type = ie->arg_type;
  3182. const StructEntry *se;
  3183. const argtype *field_types;
  3184. const int *dst_offsets, *src_offsets;
  3185. int target_size;
  3186. void *argptr;
  3187. abi_ulong *target_rt_dev_ptr;
  3188. unsigned long *host_rt_dev_ptr;
  3189. abi_long ret;
  3190. int i;
  3191. assert(ie->access == IOC_W);
  3192. assert(*arg_type == TYPE_PTR);
  3193. arg_type++;
  3194. assert(*arg_type == TYPE_STRUCT);
  3195. target_size = thunk_type_size(arg_type, 0);
  3196. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  3197. if (!argptr) {
  3198. return -TARGET_EFAULT;
  3199. }
  3200. arg_type++;
  3201. assert(*arg_type == (int)STRUCT_rtentry);
  3202. se = struct_entries + *arg_type++;
  3203. assert(se->convert[0] == NULL);
  3204. /* convert struct here to be able to catch rt_dev string */
  3205. field_types = se->field_types;
  3206. dst_offsets = se->field_offsets[THUNK_HOST];
  3207. src_offsets = se->field_offsets[THUNK_TARGET];
  3208. for (i = 0; i < se->nb_fields; i++) {
  3209. if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) {
  3210. assert(*field_types == TYPE_PTRVOID);
  3211. target_rt_dev_ptr = (abi_ulong *)(argptr + src_offsets[i]);
  3212. host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]);
  3213. if (*target_rt_dev_ptr != 0) {
  3214. *host_rt_dev_ptr = (unsigned long)lock_user_string(
  3215. tswapal(*target_rt_dev_ptr));
  3216. if (!*host_rt_dev_ptr) {
  3217. unlock_user(argptr, arg, 0);
  3218. return -TARGET_EFAULT;
  3219. }
  3220. } else {
  3221. *host_rt_dev_ptr = 0;
  3222. }
  3223. field_types++;
  3224. continue;
  3225. }
  3226. field_types = thunk_convert(buf_temp + dst_offsets[i],
  3227. argptr + src_offsets[i],
  3228. field_types, THUNK_HOST);
  3229. }
  3230. unlock_user(argptr, arg, 0);
  3231. ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
  3232. if (*host_rt_dev_ptr != 0) {
  3233. unlock_user((void *)*host_rt_dev_ptr,
  3234. *target_rt_dev_ptr, 0);
  3235. }
  3236. return ret;
  3237. }
  3238. static IOCTLEntry ioctl_entries[] = {
  3239. #define IOCTL(cmd, access, ...) \
  3240. { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
  3241. #define IOCTL_SPECIAL(cmd, access, dofn, ...) \
  3242. { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
  3243. #include "ioctls.h"
  3244. { 0, 0, },
  3245. };
  3246. /* ??? Implement proper locking for ioctls. */
  3247. /* do_ioctl() Must return target values and target errnos. */
  3248. static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
  3249. {
  3250. const IOCTLEntry *ie;
  3251. const argtype *arg_type;
  3252. abi_long ret;
  3253. uint8_t buf_temp[MAX_STRUCT_SIZE];
  3254. int target_size;
  3255. void *argptr;
  3256. ie = ioctl_entries;
  3257. for(;;) {
  3258. if (ie->target_cmd == 0) {
  3259. gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
  3260. return -TARGET_ENOSYS;
  3261. }
  3262. if (ie->target_cmd == cmd)
  3263. break;
  3264. ie++;
  3265. }
  3266. arg_type = ie->arg_type;
  3267. #if defined(DEBUG)
  3268. gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
  3269. #endif
  3270. if (ie->do_ioctl) {
  3271. return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
  3272. }
  3273. switch(arg_type[0]) {
  3274. case TYPE_NULL:
  3275. /* no argument */
  3276. ret = get_errno(ioctl(fd, ie->host_cmd));
  3277. break;
  3278. case TYPE_PTRVOID:
  3279. case TYPE_INT:
  3280. /* int argment */
  3281. ret = get_errno(ioctl(fd, ie->host_cmd, arg));
  3282. break;
  3283. case TYPE_PTR:
  3284. arg_type++;
  3285. target_size = thunk_type_size(arg_type, 0);
  3286. switch(ie->access) {
  3287. case IOC_R:
  3288. ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
  3289. if (!is_error(ret)) {
  3290. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  3291. if (!argptr)
  3292. return -TARGET_EFAULT;
  3293. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  3294. unlock_user(argptr, arg, target_size);
  3295. }
  3296. break;
  3297. case IOC_W:
  3298. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  3299. if (!argptr)
  3300. return -TARGET_EFAULT;
  3301. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  3302. unlock_user(argptr, arg, 0);
  3303. ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
  3304. break;
  3305. default:
  3306. case IOC_RW:
  3307. argptr = lock_user(VERIFY_READ, arg, target_size, 1);
  3308. if (!argptr)
  3309. return -TARGET_EFAULT;
  3310. thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
  3311. unlock_user(argptr, arg, 0);
  3312. ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
  3313. if (!is_error(ret)) {
  3314. argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
  3315. if (!argptr)
  3316. return -TARGET_EFAULT;
  3317. thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
  3318. unlock_user(argptr, arg, target_size);
  3319. }
  3320. break;
  3321. }
  3322. break;
  3323. default:
  3324. gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
  3325. (long)cmd, arg_type[0]);
  3326. ret = -TARGET_ENOSYS;
  3327. break;
  3328. }
  3329. return ret;
  3330. }
  3331. static const bitmask_transtbl iflag_tbl[] = {
  3332. { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
  3333. { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
  3334. { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
  3335. { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
  3336. { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
  3337. { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
  3338. { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
  3339. { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
  3340. { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
  3341. { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
  3342. { TARGET_IXON, TARGET_IXON, IXON, IXON },
  3343. { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
  3344. { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
  3345. { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
  3346. { 0, 0, 0, 0 }
  3347. };
  3348. static const bitmask_transtbl oflag_tbl[] = {
  3349. { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
  3350. { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
  3351. { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
  3352. { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
  3353. { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
  3354. { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
  3355. { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
  3356. { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
  3357. { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
  3358. { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
  3359. { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
  3360. { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
  3361. { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
  3362. { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
  3363. { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
  3364. { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
  3365. { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
  3366. { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
  3367. { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
  3368. { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
  3369. { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
  3370. { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
  3371. { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
  3372. { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
  3373. { 0, 0, 0, 0 }
  3374. };
  3375. static const bitmask_transtbl cflag_tbl[] = {
  3376. { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
  3377. { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
  3378. { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
  3379. { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
  3380. { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
  3381. { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
  3382. { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
  3383. { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
  3384. { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
  3385. { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
  3386. { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
  3387. { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
  3388. { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
  3389. { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
  3390. { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
  3391. { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
  3392. { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
  3393. { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
  3394. { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
  3395. { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
  3396. { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
  3397. { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
  3398. { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
  3399. { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
  3400. { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
  3401. { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
  3402. { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
  3403. { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
  3404. { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
  3405. { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
  3406. { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
  3407. { 0, 0, 0, 0 }
  3408. };
  3409. static const bitmask_transtbl lflag_tbl[] = {
  3410. { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
  3411. { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
  3412. { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
  3413. { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
  3414. { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
  3415. { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
  3416. { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
  3417. { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
  3418. { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
  3419. { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
  3420. { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
  3421. { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
  3422. { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
  3423. { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
  3424. { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
  3425. { 0, 0, 0, 0 }
  3426. };
  3427. static void target_to_host_termios (void *dst, const void *src)
  3428. {
  3429. struct host_termios *host = dst;
  3430. const struct target_termios *target = src;
  3431. host->c_iflag =
  3432. target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
  3433. host->c_oflag =
  3434. target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
  3435. host->c_cflag =
  3436. target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
  3437. host->c_lflag =
  3438. target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
  3439. host->c_line = target->c_line;
  3440. memset(host->c_cc, 0, sizeof(host->c_cc));
  3441. host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
  3442. host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
  3443. host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
  3444. host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
  3445. host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
  3446. host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
  3447. host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
  3448. host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
  3449. host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
  3450. host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
  3451. host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
  3452. host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
  3453. host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
  3454. host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
  3455. host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
  3456. host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
  3457. host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
  3458. }
  3459. static void host_to_target_termios (void *dst, const void *src)
  3460. {
  3461. struct target_termios *target = dst;
  3462. const struct host_termios *host = src;
  3463. target->c_iflag =
  3464. tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
  3465. target->c_oflag =
  3466. tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
  3467. target->c_cflag =
  3468. tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
  3469. target->c_lflag =
  3470. tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
  3471. target->c_line = host->c_line;
  3472. memset(target->c_cc, 0, sizeof(target->c_cc));
  3473. target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
  3474. target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
  3475. target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
  3476. target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
  3477. target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
  3478. target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
  3479. target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
  3480. target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
  3481. target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
  3482. target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
  3483. target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
  3484. target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
  3485. target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
  3486. target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
  3487. target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
  3488. target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
  3489. target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
  3490. }
  3491. static const StructEntry struct_termios_def = {
  3492. .convert = { host_to_target_termios, target_to_host_termios },
  3493. .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
  3494. .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
  3495. };
  3496. static bitmask_transtbl mmap_flags_tbl[] = {
  3497. { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
  3498. { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
  3499. { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
  3500. { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
  3501. { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
  3502. { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
  3503. { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
  3504. { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
  3505. { 0, 0, 0, 0 }
  3506. };
  3507. #if defined(TARGET_I386)
  3508. /* NOTE: there is really one LDT for all the threads */
  3509. static uint8_t *ldt_table;
  3510. static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
  3511. {
  3512. int size;
  3513. void *p;
  3514. if (!ldt_table)
  3515. return 0;
  3516. size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
  3517. if (size > bytecount)
  3518. size = bytecount;
  3519. p = lock_user(VERIFY_WRITE, ptr, size, 0);
  3520. if (!p)
  3521. return -TARGET_EFAULT;
  3522. /* ??? Should this by byteswapped? */
  3523. memcpy(p, ldt_table, size);
  3524. unlock_user(p, ptr, size);
  3525. return size;
  3526. }
  3527. /* XXX: add locking support */
  3528. static abi_long write_ldt(CPUX86State *env,
  3529. abi_ulong ptr, unsigned long bytecount, int oldmode)
  3530. {
  3531. struct target_modify_ldt_ldt_s ldt_info;
  3532. struct target_modify_ldt_ldt_s *target_ldt_info;
  3533. int seg_32bit, contents, read_exec_only, limit_in_pages;
  3534. int seg_not_present, useable, lm;
  3535. uint32_t *lp, entry_1, entry_2;
  3536. if (bytecount != sizeof(ldt_info))
  3537. return -TARGET_EINVAL;
  3538. if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
  3539. return -TARGET_EFAULT;
  3540. ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
  3541. ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
  3542. ldt_info.limit = tswap32(target_ldt_info->limit);
  3543. ldt_info.flags = tswap32(target_ldt_info->flags);
  3544. unlock_user_struct(target_ldt_info, ptr, 0);
  3545. if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
  3546. return -TARGET_EINVAL;
  3547. seg_32bit = ldt_info.flags & 1;
  3548. contents = (ldt_info.flags >> 1) & 3;
  3549. read_exec_only = (ldt_info.flags >> 3) & 1;
  3550. limit_in_pages = (ldt_info.flags >> 4) & 1;
  3551. seg_not_present = (ldt_info.flags >> 5) & 1;
  3552. useable = (ldt_info.flags >> 6) & 1;
  3553. #ifdef TARGET_ABI32
  3554. lm = 0;
  3555. #else
  3556. lm = (ldt_info.flags >> 7) & 1;
  3557. #endif
  3558. if (contents == 3) {
  3559. if (oldmode)
  3560. return -TARGET_EINVAL;
  3561. if (seg_not_present == 0)
  3562. return -TARGET_EINVAL;
  3563. }
  3564. /* allocate the LDT */
  3565. if (!ldt_table) {
  3566. env->ldt.base = target_mmap(0,
  3567. TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
  3568. PROT_READ|PROT_WRITE,
  3569. MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
  3570. if (env->ldt.base == -1)
  3571. return -TARGET_ENOMEM;
  3572. memset(g2h(env->ldt.base), 0,
  3573. TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
  3574. env->ldt.limit = 0xffff;
  3575. ldt_table = g2h(env->ldt.base);
  3576. }
  3577. /* NOTE: same code as Linux kernel */
  3578. /* Allow LDTs to be cleared by the user. */
  3579. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  3580. if (oldmode ||
  3581. (contents == 0 &&
  3582. read_exec_only == 1 &&
  3583. seg_32bit == 0 &&
  3584. limit_in_pages == 0 &&
  3585. seg_not_present == 1 &&
  3586. useable == 0 )) {
  3587. entry_1 = 0;
  3588. entry_2 = 0;
  3589. goto install;
  3590. }
  3591. }
  3592. entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
  3593. (ldt_info.limit & 0x0ffff);
  3594. entry_2 = (ldt_info.base_addr & 0xff000000) |
  3595. ((ldt_info.base_addr & 0x00ff0000) >> 16) |
  3596. (ldt_info.limit & 0xf0000) |
  3597. ((read_exec_only ^ 1) << 9) |
  3598. (contents << 10) |
  3599. ((seg_not_present ^ 1) << 15) |
  3600. (seg_32bit << 22) |
  3601. (limit_in_pages << 23) |
  3602. (lm << 21) |
  3603. 0x7000;
  3604. if (!oldmode)
  3605. entry_2 |= (useable << 20);
  3606. /* Install the new entry ... */
  3607. install:
  3608. lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
  3609. lp[0] = tswap32(entry_1);
  3610. lp[1] = tswap32(entry_2);
  3611. return 0;
  3612. }
  3613. /* specific and weird i386 syscalls */
  3614. static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
  3615. unsigned long bytecount)
  3616. {
  3617. abi_long ret;
  3618. switch (func) {
  3619. case 0:
  3620. ret = read_ldt(ptr, bytecount);
  3621. break;
  3622. case 1:
  3623. ret = write_ldt(env, ptr, bytecount, 1);
  3624. break;
  3625. case 0x11:
  3626. ret = write_ldt(env, ptr, bytecount, 0);
  3627. break;
  3628. default:
  3629. ret = -TARGET_ENOSYS;
  3630. break;
  3631. }
  3632. return ret;
  3633. }
  3634. #if defined(TARGET_I386) && defined(TARGET_ABI32)
  3635. abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
  3636. {
  3637. uint64_t *gdt_table = g2h(env->gdt.base);
  3638. struct target_modify_ldt_ldt_s ldt_info;
  3639. struct target_modify_ldt_ldt_s *target_ldt_info;
  3640. int seg_32bit, contents, read_exec_only, limit_in_pages;
  3641. int seg_not_present, useable, lm;
  3642. uint32_t *lp, entry_1, entry_2;
  3643. int i;
  3644. lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
  3645. if (!target_ldt_info)
  3646. return -TARGET_EFAULT;
  3647. ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
  3648. ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
  3649. ldt_info.limit = tswap32(target_ldt_info->limit);
  3650. ldt_info.flags = tswap32(target_ldt_info->flags);
  3651. if (ldt_info.entry_number == -1) {
  3652. for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
  3653. if (gdt_table[i] == 0) {
  3654. ldt_info.entry_number = i;
  3655. target_ldt_info->entry_number = tswap32(i);
  3656. break;
  3657. }
  3658. }
  3659. }
  3660. unlock_user_struct(target_ldt_info, ptr, 1);
  3661. if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
  3662. ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
  3663. return -TARGET_EINVAL;
  3664. seg_32bit = ldt_info.flags & 1;
  3665. contents = (ldt_info.flags >> 1) & 3;
  3666. read_exec_only = (ldt_info.flags >> 3) & 1;
  3667. limit_in_pages = (ldt_info.flags >> 4) & 1;
  3668. seg_not_present = (ldt_info.flags >> 5) & 1;
  3669. useable = (ldt_info.flags >> 6) & 1;
  3670. #ifdef TARGET_ABI32
  3671. lm = 0;
  3672. #else
  3673. lm = (ldt_info.flags >> 7) & 1;
  3674. #endif
  3675. if (contents == 3) {
  3676. if (seg_not_present == 0)
  3677. return -TARGET_EINVAL;
  3678. }
  3679. /* NOTE: same code as Linux kernel */
  3680. /* Allow LDTs to be cleared by the user. */
  3681. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  3682. if ((contents == 0 &&
  3683. read_exec_only == 1 &&
  3684. seg_32bit == 0 &&
  3685. limit_in_pages == 0 &&
  3686. seg_not_present == 1 &&
  3687. useable == 0 )) {
  3688. entry_1 = 0;
  3689. entry_2 = 0;
  3690. goto install;
  3691. }
  3692. }
  3693. entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
  3694. (ldt_info.limit & 0x0ffff);
  3695. entry_2 = (ldt_info.base_addr & 0xff000000) |
  3696. ((ldt_info.base_addr & 0x00ff0000) >> 16) |
  3697. (ldt_info.limit & 0xf0000) |
  3698. ((read_exec_only ^ 1) << 9) |
  3699. (contents << 10) |
  3700. ((seg_not_present ^ 1) << 15) |
  3701. (seg_32bit << 22) |
  3702. (limit_in_pages << 23) |
  3703. (useable << 20) |
  3704. (lm << 21) |
  3705. 0x7000;
  3706. /* Install the new entry ... */
  3707. install:
  3708. lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
  3709. lp[0] = tswap32(entry_1);
  3710. lp[1] = tswap32(entry_2);
  3711. return 0;
  3712. }
  3713. static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
  3714. {
  3715. struct target_modify_ldt_ldt_s *target_ldt_info;
  3716. uint64_t *gdt_table = g2h(env->gdt.base);
  3717. uint32_t base_addr, limit, flags;
  3718. int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
  3719. int seg_not_present, useable, lm;
  3720. uint32_t *lp, entry_1, entry_2;
  3721. lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
  3722. if (!target_ldt_info)
  3723. return -TARGET_EFAULT;
  3724. idx = tswap32(target_ldt_info->entry_number);
  3725. if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
  3726. idx > TARGET_GDT_ENTRY_TLS_MAX) {
  3727. unlock_user_struct(target_ldt_info, ptr, 1);
  3728. return -TARGET_EINVAL;
  3729. }
  3730. lp = (uint32_t *)(gdt_table + idx);
  3731. entry_1 = tswap32(lp[0]);
  3732. entry_2 = tswap32(lp[1]);
  3733. read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
  3734. contents = (entry_2 >> 10) & 3;
  3735. seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
  3736. seg_32bit = (entry_2 >> 22) & 1;
  3737. limit_in_pages = (entry_2 >> 23) & 1;
  3738. useable = (entry_2 >> 20) & 1;
  3739. #ifdef TARGET_ABI32
  3740. lm = 0;
  3741. #else
  3742. lm = (entry_2 >> 21) & 1;
  3743. #endif
  3744. flags = (seg_32bit << 0) | (contents << 1) |
  3745. (read_exec_only << 3) | (limit_in_pages << 4) |
  3746. (seg_not_present << 5) | (useable << 6) | (lm << 7);
  3747. limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
  3748. base_addr = (entry_1 >> 16) |
  3749. (entry_2 & 0xff000000) |
  3750. ((entry_2 & 0xff) << 16);
  3751. target_ldt_info->base_addr = tswapal(base_addr);
  3752. target_ldt_info->limit = tswap32(limit);
  3753. target_ldt_info->flags = tswap32(flags);
  3754. unlock_user_struct(target_ldt_info, ptr, 1);
  3755. return 0;
  3756. }
  3757. #endif /* TARGET_I386 && TARGET_ABI32 */
  3758. #ifndef TARGET_ABI32
  3759. abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
  3760. {
  3761. abi_long ret = 0;
  3762. abi_ulong val;
  3763. int idx;
  3764. switch(code) {
  3765. case TARGET_ARCH_SET_GS:
  3766. case TARGET_ARCH_SET_FS:
  3767. if (code == TARGET_ARCH_SET_GS)
  3768. idx = R_GS;
  3769. else
  3770. idx = R_FS;
  3771. cpu_x86_load_seg(env, idx, 0);
  3772. env->segs[idx].base = addr;
  3773. break;
  3774. case TARGET_ARCH_GET_GS:
  3775. case TARGET_ARCH_GET_FS:
  3776. if (code == TARGET_ARCH_GET_GS)
  3777. idx = R_GS;
  3778. else
  3779. idx = R_FS;
  3780. val = env->segs[idx].base;
  3781. if (put_user(val, addr, abi_ulong))
  3782. ret = -TARGET_EFAULT;
  3783. break;
  3784. default:
  3785. ret = -TARGET_EINVAL;
  3786. break;
  3787. }
  3788. return ret;
  3789. }
  3790. #endif
  3791. #endif /* defined(TARGET_I386) */
  3792. #define NEW_STACK_SIZE 0x40000
  3793. static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
  3794. typedef struct {
  3795. CPUArchState *env;
  3796. pthread_mutex_t mutex;
  3797. pthread_cond_t cond;
  3798. pthread_t thread;
  3799. uint32_t tid;
  3800. abi_ulong child_tidptr;
  3801. abi_ulong parent_tidptr;
  3802. sigset_t sigmask;
  3803. } new_thread_info;
  3804. static void *clone_func(void *arg)
  3805. {
  3806. new_thread_info *info = arg;
  3807. CPUArchState *env;
  3808. CPUState *cpu;
  3809. TaskState *ts;
  3810. env = info->env;
  3811. cpu = ENV_GET_CPU(env);
  3812. thread_cpu = cpu;
  3813. ts = (TaskState *)cpu->opaque;
  3814. info->tid = gettid();
  3815. cpu->host_tid = info->tid;
  3816. task_settid(ts);
  3817. if (info->child_tidptr)
  3818. put_user_u32(info->tid, info->child_tidptr);
  3819. if (info->parent_tidptr)
  3820. put_user_u32(info->tid, info->parent_tidptr);
  3821. /* Enable signals. */
  3822. sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
  3823. /* Signal to the parent that we're ready. */
  3824. pthread_mutex_lock(&info->mutex);
  3825. pthread_cond_broadcast(&info->cond);
  3826. pthread_mutex_unlock(&info->mutex);
  3827. /* Wait until the parent has finshed initializing the tls state. */
  3828. pthread_mutex_lock(&clone_lock);
  3829. pthread_mutex_unlock(&clone_lock);
  3830. cpu_loop(env);
  3831. /* never exits */
  3832. return NULL;
  3833. }
  3834. /* do_fork() Must return host values and target errnos (unlike most
  3835. do_*() functions). */
  3836. static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
  3837. abi_ulong parent_tidptr, target_ulong newtls,
  3838. abi_ulong child_tidptr)
  3839. {
  3840. CPUState *cpu = ENV_GET_CPU(env);
  3841. int ret;
  3842. TaskState *ts;
  3843. CPUState *new_cpu;
  3844. CPUArchState *new_env;
  3845. unsigned int nptl_flags;
  3846. sigset_t sigmask;
  3847. /* Emulate vfork() with fork() */
  3848. if (flags & CLONE_VFORK)
  3849. flags &= ~(CLONE_VFORK | CLONE_VM);
  3850. if (flags & CLONE_VM) {
  3851. TaskState *parent_ts = (TaskState *)cpu->opaque;
  3852. new_thread_info info;
  3853. pthread_attr_t attr;
  3854. ts = g_malloc0(sizeof(TaskState));
  3855. init_task_state(ts);
  3856. /* we create a new CPU instance. */
  3857. new_env = cpu_copy(env);
  3858. /* Init regs that differ from the parent. */
  3859. cpu_clone_regs(new_env, newsp);
  3860. new_cpu = ENV_GET_CPU(new_env);
  3861. new_cpu->opaque = ts;
  3862. ts->bprm = parent_ts->bprm;
  3863. ts->info = parent_ts->info;
  3864. nptl_flags = flags;
  3865. flags &= ~CLONE_NPTL_FLAGS2;
  3866. if (nptl_flags & CLONE_CHILD_CLEARTID) {
  3867. ts->child_tidptr = child_tidptr;
  3868. }
  3869. if (nptl_flags & CLONE_SETTLS)
  3870. cpu_set_tls (new_env, newtls);
  3871. /* Grab a mutex so that thread setup appears atomic. */
  3872. pthread_mutex_lock(&clone_lock);
  3873. memset(&info, 0, sizeof(info));
  3874. pthread_mutex_init(&info.mutex, NULL);
  3875. pthread_mutex_lock(&info.mutex);
  3876. pthread_cond_init(&info.cond, NULL);
  3877. info.env = new_env;
  3878. if (nptl_flags & CLONE_CHILD_SETTID)
  3879. info.child_tidptr = child_tidptr;
  3880. if (nptl_flags & CLONE_PARENT_SETTID)
  3881. info.parent_tidptr = parent_tidptr;
  3882. ret = pthread_attr_init(&attr);
  3883. ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
  3884. ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  3885. /* It is not safe to deliver signals until the child has finished
  3886. initializing, so temporarily block all signals. */
  3887. sigfillset(&sigmask);
  3888. sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
  3889. ret = pthread_create(&info.thread, &attr, clone_func, &info);
  3890. /* TODO: Free new CPU state if thread creation failed. */
  3891. sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
  3892. pthread_attr_destroy(&attr);
  3893. if (ret == 0) {
  3894. /* Wait for the child to initialize. */
  3895. pthread_cond_wait(&info.cond, &info.mutex);
  3896. ret = info.tid;
  3897. if (flags & CLONE_PARENT_SETTID)
  3898. put_user_u32(ret, parent_tidptr);
  3899. } else {
  3900. ret = -1;
  3901. }
  3902. pthread_mutex_unlock(&info.mutex);
  3903. pthread_cond_destroy(&info.cond);
  3904. pthread_mutex_destroy(&info.mutex);
  3905. pthread_mutex_unlock(&clone_lock);
  3906. } else {
  3907. /* if no CLONE_VM, we consider it is a fork */
  3908. if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0)
  3909. return -EINVAL;
  3910. fork_start();
  3911. ret = fork();
  3912. if (ret == 0) {
  3913. /* Child Process. */
  3914. cpu_clone_regs(env, newsp);
  3915. fork_end(1);
  3916. /* There is a race condition here. The parent process could
  3917. theoretically read the TID in the child process before the child
  3918. tid is set. This would require using either ptrace
  3919. (not implemented) or having *_tidptr to point at a shared memory
  3920. mapping. We can't repeat the spinlock hack used above because
  3921. the child process gets its own copy of the lock. */
  3922. if (flags & CLONE_CHILD_SETTID)
  3923. put_user_u32(gettid(), child_tidptr);
  3924. if (flags & CLONE_PARENT_SETTID)
  3925. put_user_u32(gettid(), parent_tidptr);
  3926. ts = (TaskState *)cpu->opaque;
  3927. if (flags & CLONE_SETTLS)
  3928. cpu_set_tls (env, newtls);
  3929. if (flags & CLONE_CHILD_CLEARTID)
  3930. ts->child_tidptr = child_tidptr;
  3931. } else {
  3932. fork_end(0);
  3933. }
  3934. }
  3935. return ret;
  3936. }
  3937. /* warning : doesn't handle linux specific flags... */
  3938. static int target_to_host_fcntl_cmd(int cmd)
  3939. {
  3940. switch(cmd) {
  3941. case TARGET_F_DUPFD:
  3942. case TARGET_F_GETFD:
  3943. case TARGET_F_SETFD:
  3944. case TARGET_F_GETFL:
  3945. case TARGET_F_SETFL:
  3946. return cmd;
  3947. case TARGET_F_GETLK:
  3948. return F_GETLK;
  3949. case TARGET_F_SETLK:
  3950. return F_SETLK;
  3951. case TARGET_F_SETLKW:
  3952. return F_SETLKW;
  3953. case TARGET_F_GETOWN:
  3954. return F_GETOWN;
  3955. case TARGET_F_SETOWN:
  3956. return F_SETOWN;
  3957. case TARGET_F_GETSIG:
  3958. return F_GETSIG;
  3959. case TARGET_F_SETSIG:
  3960. return F_SETSIG;
  3961. #if TARGET_ABI_BITS == 32
  3962. case TARGET_F_GETLK64:
  3963. return F_GETLK64;
  3964. case TARGET_F_SETLK64:
  3965. return F_SETLK64;
  3966. case TARGET_F_SETLKW64:
  3967. return F_SETLKW64;
  3968. #endif
  3969. case TARGET_F_SETLEASE:
  3970. return F_SETLEASE;
  3971. case TARGET_F_GETLEASE:
  3972. return F_GETLEASE;
  3973. #ifdef F_DUPFD_CLOEXEC
  3974. case TARGET_F_DUPFD_CLOEXEC:
  3975. return F_DUPFD_CLOEXEC;
  3976. #endif
  3977. case TARGET_F_NOTIFY:
  3978. return F_NOTIFY;
  3979. #ifdef F_GETOWN_EX
  3980. case TARGET_F_GETOWN_EX:
  3981. return F_GETOWN_EX;
  3982. #endif
  3983. #ifdef F_SETOWN_EX
  3984. case TARGET_F_SETOWN_EX:
  3985. return F_SETOWN_EX;
  3986. #endif
  3987. default:
  3988. return -TARGET_EINVAL;
  3989. }
  3990. return -TARGET_EINVAL;
  3991. }
  3992. #define TRANSTBL_CONVERT(a) { -1, TARGET_##a, -1, a }
  3993. static const bitmask_transtbl flock_tbl[] = {
  3994. TRANSTBL_CONVERT(F_RDLCK),
  3995. TRANSTBL_CONVERT(F_WRLCK),
  3996. TRANSTBL_CONVERT(F_UNLCK),
  3997. TRANSTBL_CONVERT(F_EXLCK),
  3998. TRANSTBL_CONVERT(F_SHLCK),
  3999. { 0, 0, 0, 0 }
  4000. };
  4001. static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
  4002. {
  4003. struct flock fl;
  4004. struct target_flock *target_fl;
  4005. struct flock64 fl64;
  4006. struct target_flock64 *target_fl64;
  4007. #ifdef F_GETOWN_EX
  4008. struct f_owner_ex fox;
  4009. struct target_f_owner_ex *target_fox;
  4010. #endif
  4011. abi_long ret;
  4012. int host_cmd = target_to_host_fcntl_cmd(cmd);
  4013. if (host_cmd == -TARGET_EINVAL)
  4014. return host_cmd;
  4015. switch(cmd) {
  4016. case TARGET_F_GETLK:
  4017. if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
  4018. return -TARGET_EFAULT;
  4019. fl.l_type =
  4020. target_to_host_bitmask(tswap16(target_fl->l_type), flock_tbl);
  4021. fl.l_whence = tswap16(target_fl->l_whence);
  4022. fl.l_start = tswapal(target_fl->l_start);
  4023. fl.l_len = tswapal(target_fl->l_len);
  4024. fl.l_pid = tswap32(target_fl->l_pid);
  4025. unlock_user_struct(target_fl, arg, 0);
  4026. ret = get_errno(fcntl(fd, host_cmd, &fl));
  4027. if (ret == 0) {
  4028. if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
  4029. return -TARGET_EFAULT;
  4030. target_fl->l_type =
  4031. host_to_target_bitmask(tswap16(fl.l_type), flock_tbl);
  4032. target_fl->l_whence = tswap16(fl.l_whence);
  4033. target_fl->l_start = tswapal(fl.l_start);
  4034. target_fl->l_len = tswapal(fl.l_len);
  4035. target_fl->l_pid = tswap32(fl.l_pid);
  4036. unlock_user_struct(target_fl, arg, 1);
  4037. }
  4038. break;
  4039. case TARGET_F_SETLK:
  4040. case TARGET_F_SETLKW:
  4041. if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
  4042. return -TARGET_EFAULT;
  4043. fl.l_type =
  4044. target_to_host_bitmask(tswap16(target_fl->l_type), flock_tbl);
  4045. fl.l_whence = tswap16(target_fl->l_whence);
  4046. fl.l_start = tswapal(target_fl->l_start);
  4047. fl.l_len = tswapal(target_fl->l_len);
  4048. fl.l_pid = tswap32(target_fl->l_pid);
  4049. unlock_user_struct(target_fl, arg, 0);
  4050. ret = get_errno(fcntl(fd, host_cmd, &fl));
  4051. break;
  4052. case TARGET_F_GETLK64:
  4053. if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
  4054. return -TARGET_EFAULT;
  4055. fl64.l_type =
  4056. target_to_host_bitmask(tswap16(target_fl64->l_type), flock_tbl) >> 1;
  4057. fl64.l_whence = tswap16(target_fl64->l_whence);
  4058. fl64.l_start = tswap64(target_fl64->l_start);
  4059. fl64.l_len = tswap64(target_fl64->l_len);
  4060. fl64.l_pid = tswap32(target_fl64->l_pid);
  4061. unlock_user_struct(target_fl64, arg, 0);
  4062. ret = get_errno(fcntl(fd, host_cmd, &fl64));
  4063. if (ret == 0) {
  4064. if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
  4065. return -TARGET_EFAULT;
  4066. target_fl64->l_type =
  4067. host_to_target_bitmask(tswap16(fl64.l_type), flock_tbl) >> 1;
  4068. target_fl64->l_whence = tswap16(fl64.l_whence);
  4069. target_fl64->l_start = tswap64(fl64.l_start);
  4070. target_fl64->l_len = tswap64(fl64.l_len);
  4071. target_fl64->l_pid = tswap32(fl64.l_pid);
  4072. unlock_user_struct(target_fl64, arg, 1);
  4073. }
  4074. break;
  4075. case TARGET_F_SETLK64:
  4076. case TARGET_F_SETLKW64:
  4077. if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
  4078. return -TARGET_EFAULT;
  4079. fl64.l_type =
  4080. target_to_host_bitmask(tswap16(target_fl64->l_type), flock_tbl) >> 1;
  4081. fl64.l_whence = tswap16(target_fl64->l_whence);
  4082. fl64.l_start = tswap64(target_fl64->l_start);
  4083. fl64.l_len = tswap64(target_fl64->l_len);
  4084. fl64.l_pid = tswap32(target_fl64->l_pid);
  4085. unlock_user_struct(target_fl64, arg, 0);
  4086. ret = get_errno(fcntl(fd, host_cmd, &fl64));
  4087. break;
  4088. case TARGET_F_GETFL:
  4089. ret = get_errno(fcntl(fd, host_cmd, arg));
  4090. if (ret >= 0) {
  4091. ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
  4092. }
  4093. break;
  4094. case TARGET_F_SETFL:
  4095. ret = get_errno(fcntl(fd, host_cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
  4096. break;
  4097. #ifdef F_GETOWN_EX
  4098. case TARGET_F_GETOWN_EX:
  4099. ret = get_errno(fcntl(fd, host_cmd, &fox));
  4100. if (ret >= 0) {
  4101. if (!lock_user_struct(VERIFY_WRITE, target_fox, arg, 0))
  4102. return -TARGET_EFAULT;
  4103. target_fox->type = tswap32(fox.type);
  4104. target_fox->pid = tswap32(fox.pid);
  4105. unlock_user_struct(target_fox, arg, 1);
  4106. }
  4107. break;
  4108. #endif
  4109. #ifdef F_SETOWN_EX
  4110. case TARGET_F_SETOWN_EX:
  4111. if (!lock_user_struct(VERIFY_READ, target_fox, arg, 1))
  4112. return -TARGET_EFAULT;
  4113. fox.type = tswap32(target_fox->type);
  4114. fox.pid = tswap32(target_fox->pid);
  4115. unlock_user_struct(target_fox, arg, 0);
  4116. ret = get_errno(fcntl(fd, host_cmd, &fox));
  4117. break;
  4118. #endif
  4119. case TARGET_F_SETOWN:
  4120. case TARGET_F_GETOWN:
  4121. case TARGET_F_SETSIG:
  4122. case TARGET_F_GETSIG:
  4123. case TARGET_F_SETLEASE:
  4124. case TARGET_F_GETLEASE:
  4125. ret = get_errno(fcntl(fd, host_cmd, arg));
  4126. break;
  4127. default:
  4128. ret = get_errno(fcntl(fd, cmd, arg));
  4129. break;
  4130. }
  4131. return ret;
  4132. }
  4133. #ifdef USE_UID16
  4134. static inline int high2lowuid(int uid)
  4135. {
  4136. if (uid > 65535)
  4137. return 65534;
  4138. else
  4139. return uid;
  4140. }
  4141. static inline int high2lowgid(int gid)
  4142. {
  4143. if (gid > 65535)
  4144. return 65534;
  4145. else
  4146. return gid;
  4147. }
  4148. static inline int low2highuid(int uid)
  4149. {
  4150. if ((int16_t)uid == -1)
  4151. return -1;
  4152. else
  4153. return uid;
  4154. }
  4155. static inline int low2highgid(int gid)
  4156. {
  4157. if ((int16_t)gid == -1)
  4158. return -1;
  4159. else
  4160. return gid;
  4161. }
  4162. static inline int tswapid(int id)
  4163. {
  4164. return tswap16(id);
  4165. }
  4166. #define put_user_id(x, gaddr) put_user_u16(x, gaddr)
  4167. #else /* !USE_UID16 */
  4168. static inline int high2lowuid(int uid)
  4169. {
  4170. return uid;
  4171. }
  4172. static inline int high2lowgid(int gid)
  4173. {
  4174. return gid;
  4175. }
  4176. static inline int low2highuid(int uid)
  4177. {
  4178. return uid;
  4179. }
  4180. static inline int low2highgid(int gid)
  4181. {
  4182. return gid;
  4183. }
  4184. static inline int tswapid(int id)
  4185. {
  4186. return tswap32(id);
  4187. }
  4188. #define put_user_id(x, gaddr) put_user_u32(x, gaddr)
  4189. #endif /* USE_UID16 */
  4190. void syscall_init(void)
  4191. {
  4192. IOCTLEntry *ie;
  4193. const argtype *arg_type;
  4194. int size;
  4195. int i;
  4196. #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
  4197. #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
  4198. #include "syscall_types.h"
  4199. #undef STRUCT
  4200. #undef STRUCT_SPECIAL
  4201. /* Build target_to_host_errno_table[] table from
  4202. * host_to_target_errno_table[]. */
  4203. for (i = 0; i < ERRNO_TABLE_SIZE; i++) {
  4204. target_to_host_errno_table[host_to_target_errno_table[i]] = i;
  4205. }
  4206. /* we patch the ioctl size if necessary. We rely on the fact that
  4207. no ioctl has all the bits at '1' in the size field */
  4208. ie = ioctl_entries;
  4209. while (ie->target_cmd != 0) {
  4210. if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
  4211. TARGET_IOC_SIZEMASK) {
  4212. arg_type = ie->arg_type;
  4213. if (arg_type[0] != TYPE_PTR) {
  4214. fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
  4215. ie->target_cmd);
  4216. exit(1);
  4217. }
  4218. arg_type++;
  4219. size = thunk_type_size(arg_type, 0);
  4220. ie->target_cmd = (ie->target_cmd &
  4221. ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
  4222. (size << TARGET_IOC_SIZESHIFT);
  4223. }
  4224. /* automatic consistency check if same arch */
  4225. #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
  4226. (defined(__x86_64__) && defined(TARGET_X86_64))
  4227. if (unlikely(ie->target_cmd != ie->host_cmd)) {
  4228. fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
  4229. ie->name, ie->target_cmd, ie->host_cmd);
  4230. }
  4231. #endif
  4232. ie++;
  4233. }
  4234. }
  4235. #if TARGET_ABI_BITS == 32
  4236. static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
  4237. {
  4238. #ifdef TARGET_WORDS_BIGENDIAN
  4239. return ((uint64_t)word0 << 32) | word1;
  4240. #else
  4241. return ((uint64_t)word1 << 32) | word0;
  4242. #endif
  4243. }
  4244. #else /* TARGET_ABI_BITS == 32 */
  4245. static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
  4246. {
  4247. return word0;
  4248. }
  4249. #endif /* TARGET_ABI_BITS != 32 */
  4250. #ifdef TARGET_NR_truncate64
  4251. static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
  4252. abi_long arg2,
  4253. abi_long arg3,
  4254. abi_long arg4)
  4255. {
  4256. if (regpairs_aligned(cpu_env)) {
  4257. arg2 = arg3;
  4258. arg3 = arg4;
  4259. }
  4260. return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
  4261. }
  4262. #endif
  4263. #ifdef TARGET_NR_ftruncate64
  4264. static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
  4265. abi_long arg2,
  4266. abi_long arg3,
  4267. abi_long arg4)
  4268. {
  4269. if (regpairs_aligned(cpu_env)) {
  4270. arg2 = arg3;
  4271. arg3 = arg4;
  4272. }
  4273. return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
  4274. }
  4275. #endif
  4276. static inline abi_long target_to_host_timespec(struct timespec *host_ts,
  4277. abi_ulong target_addr)
  4278. {
  4279. struct target_timespec *target_ts;
  4280. if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
  4281. return -TARGET_EFAULT;
  4282. host_ts->tv_sec = tswapal(target_ts->tv_sec);
  4283. host_ts->tv_nsec = tswapal(target_ts->tv_nsec);
  4284. unlock_user_struct(target_ts, target_addr, 0);
  4285. return 0;
  4286. }
  4287. static inline abi_long host_to_target_timespec(abi_ulong target_addr,
  4288. struct timespec *host_ts)
  4289. {
  4290. struct target_timespec *target_ts;
  4291. if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
  4292. return -TARGET_EFAULT;
  4293. target_ts->tv_sec = tswapal(host_ts->tv_sec);
  4294. target_ts->tv_nsec = tswapal(host_ts->tv_nsec);
  4295. unlock_user_struct(target_ts, target_addr, 1);
  4296. return 0;
  4297. }
  4298. static inline abi_long target_to_host_itimerspec(struct itimerspec *host_itspec,
  4299. abi_ulong target_addr)
  4300. {
  4301. struct target_itimerspec *target_itspec;
  4302. if (!lock_user_struct(VERIFY_READ, target_itspec, target_addr, 1)) {
  4303. return -TARGET_EFAULT;
  4304. }
  4305. host_itspec->it_interval.tv_sec =
  4306. tswapal(target_itspec->it_interval.tv_sec);
  4307. host_itspec->it_interval.tv_nsec =
  4308. tswapal(target_itspec->it_interval.tv_nsec);
  4309. host_itspec->it_value.tv_sec = tswapal(target_itspec->it_value.tv_sec);
  4310. host_itspec->it_value.tv_nsec = tswapal(target_itspec->it_value.tv_nsec);
  4311. unlock_user_struct(target_itspec, target_addr, 1);
  4312. return 0;
  4313. }
  4314. static inline abi_long host_to_target_itimerspec(abi_ulong target_addr,
  4315. struct itimerspec *host_its)
  4316. {
  4317. struct target_itimerspec *target_itspec;
  4318. if (!lock_user_struct(VERIFY_WRITE, target_itspec, target_addr, 0)) {
  4319. return -TARGET_EFAULT;
  4320. }
  4321. target_itspec->it_interval.tv_sec = tswapal(host_its->it_interval.tv_sec);
  4322. target_itspec->it_interval.tv_nsec = tswapal(host_its->it_interval.tv_nsec);
  4323. target_itspec->it_value.tv_sec = tswapal(host_its->it_value.tv_sec);
  4324. target_itspec->it_value.tv_nsec = tswapal(host_its->it_value.tv_nsec);
  4325. unlock_user_struct(target_itspec, target_addr, 0);
  4326. return 0;
  4327. }
  4328. #if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
  4329. static inline abi_long host_to_target_stat64(void *cpu_env,
  4330. abi_ulong target_addr,
  4331. struct stat *host_st)
  4332. {
  4333. #if defined(TARGET_ARM) && defined(TARGET_ABI32)
  4334. if (((CPUARMState *)cpu_env)->eabi) {
  4335. struct target_eabi_stat64 *target_st;
  4336. if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
  4337. return -TARGET_EFAULT;
  4338. memset(target_st, 0, sizeof(struct target_eabi_stat64));
  4339. __put_user(host_st->st_dev, &target_st->st_dev);
  4340. __put_user(host_st->st_ino, &target_st->st_ino);
  4341. #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
  4342. __put_user(host_st->st_ino, &target_st->__st_ino);
  4343. #endif
  4344. __put_user(host_st->st_mode, &target_st->st_mode);
  4345. __put_user(host_st->st_nlink, &target_st->st_nlink);
  4346. __put_user(host_st->st_uid, &target_st->st_uid);
  4347. __put_user(host_st->st_gid, &target_st->st_gid);
  4348. __put_user(host_st->st_rdev, &target_st->st_rdev);
  4349. __put_user(host_st->st_size, &target_st->st_size);
  4350. __put_user(host_st->st_blksize, &target_st->st_blksize);
  4351. __put_user(host_st->st_blocks, &target_st->st_blocks);
  4352. __put_user(host_st->st_atime, &target_st->target_st_atime);
  4353. __put_user(host_st->st_mtime, &target_st->target_st_mtime);
  4354. __put_user(host_st->st_ctime, &target_st->target_st_ctime);
  4355. unlock_user_struct(target_st, target_addr, 1);
  4356. } else
  4357. #endif
  4358. {
  4359. #if defined(TARGET_HAS_STRUCT_STAT64)
  4360. struct target_stat64 *target_st;
  4361. #else
  4362. struct target_stat *target_st;
  4363. #endif
  4364. if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
  4365. return -TARGET_EFAULT;
  4366. memset(target_st, 0, sizeof(*target_st));
  4367. __put_user(host_st->st_dev, &target_st->st_dev);
  4368. __put_user(host_st->st_ino, &target_st->st_ino);
  4369. #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
  4370. __put_user(host_st->st_ino, &target_st->__st_ino);
  4371. #endif
  4372. __put_user(host_st->st_mode, &target_st->st_mode);
  4373. __put_user(host_st->st_nlink, &target_st->st_nlink);
  4374. __put_user(host_st->st_uid, &target_st->st_uid);
  4375. __put_user(host_st->st_gid, &target_st->st_gid);
  4376. __put_user(host_st->st_rdev, &target_st->st_rdev);
  4377. /* XXX: better use of kernel struct */
  4378. __put_user(host_st->st_size, &target_st->st_size);
  4379. __put_user(host_st->st_blksize, &target_st->st_blksize);
  4380. __put_user(host_st->st_blocks, &target_st->st_blocks);
  4381. __put_user(host_st->st_atime, &target_st->target_st_atime);
  4382. __put_user(host_st->st_mtime, &target_st->target_st_mtime);
  4383. __put_user(host_st->st_ctime, &target_st->target_st_ctime);
  4384. unlock_user_struct(target_st, target_addr, 1);
  4385. }
  4386. return 0;
  4387. }
  4388. #endif
  4389. /* ??? Using host futex calls even when target atomic operations
  4390. are not really atomic probably breaks things. However implementing
  4391. futexes locally would make futexes shared between multiple processes
  4392. tricky. However they're probably useless because guest atomic
  4393. operations won't work either. */
  4394. static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
  4395. target_ulong uaddr2, int val3)
  4396. {
  4397. struct timespec ts, *pts;
  4398. int base_op;
  4399. /* ??? We assume FUTEX_* constants are the same on both host
  4400. and target. */
  4401. #ifdef FUTEX_CMD_MASK
  4402. base_op = op & FUTEX_CMD_MASK;
  4403. #else
  4404. base_op = op;
  4405. #endif
  4406. switch (base_op) {
  4407. case FUTEX_WAIT:
  4408. case FUTEX_WAIT_BITSET:
  4409. if (timeout) {
  4410. pts = &ts;
  4411. target_to_host_timespec(pts, timeout);
  4412. } else {
  4413. pts = NULL;
  4414. }
  4415. return get_errno(sys_futex(g2h(uaddr), op, tswap32(val),
  4416. pts, NULL, val3));
  4417. case FUTEX_WAKE:
  4418. return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
  4419. case FUTEX_FD:
  4420. return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
  4421. case FUTEX_REQUEUE:
  4422. case FUTEX_CMP_REQUEUE:
  4423. case FUTEX_WAKE_OP:
  4424. /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
  4425. TIMEOUT parameter is interpreted as a uint32_t by the kernel.
  4426. But the prototype takes a `struct timespec *'; insert casts
  4427. to satisfy the compiler. We do not need to tswap TIMEOUT
  4428. since it's not compared to guest memory. */
  4429. pts = (struct timespec *)(uintptr_t) timeout;
  4430. return get_errno(sys_futex(g2h(uaddr), op, val, pts,
  4431. g2h(uaddr2),
  4432. (base_op == FUTEX_CMP_REQUEUE
  4433. ? tswap32(val3)
  4434. : val3)));
  4435. default:
  4436. return -TARGET_ENOSYS;
  4437. }
  4438. }
  4439. /* Map host to target signal numbers for the wait family of syscalls.
  4440. Assume all other status bits are the same. */
  4441. int host_to_target_waitstatus(int status)
  4442. {
  4443. if (WIFSIGNALED(status)) {
  4444. return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
  4445. }
  4446. if (WIFSTOPPED(status)) {
  4447. return (host_to_target_signal(WSTOPSIG(status)) << 8)
  4448. | (status & 0xff);
  4449. }
  4450. return status;
  4451. }
  4452. static int relstr_to_int(const char *s)
  4453. {
  4454. /* Convert a uname release string like "2.6.18" to an integer
  4455. * of the form 0x020612. (Beware that 0x020612 is *not* 2.6.12.)
  4456. */
  4457. int i, n, tmp;
  4458. tmp = 0;
  4459. for (i = 0; i < 3; i++) {
  4460. n = 0;
  4461. while (*s >= '0' && *s <= '9') {
  4462. n *= 10;
  4463. n += *s - '0';
  4464. s++;
  4465. }
  4466. tmp = (tmp << 8) + n;
  4467. if (*s == '.') {
  4468. s++;
  4469. }
  4470. }
  4471. return tmp;
  4472. }
  4473. int get_osversion(void)
  4474. {
  4475. static int osversion;
  4476. struct new_utsname buf;
  4477. const char *s;
  4478. if (osversion)
  4479. return osversion;
  4480. if (qemu_uname_release && *qemu_uname_release) {
  4481. s = qemu_uname_release;
  4482. } else {
  4483. if (sys_uname(&buf))
  4484. return 0;
  4485. s = buf.release;
  4486. }
  4487. osversion = relstr_to_int(s);
  4488. return osversion;
  4489. }
  4490. void init_qemu_uname_release(void)
  4491. {
  4492. /* Initialize qemu_uname_release for later use.
  4493. * If the host kernel is too old and the user hasn't asked for
  4494. * a specific fake version number, we might want to fake a minimum
  4495. * target kernel version.
  4496. */
  4497. #ifdef UNAME_MINIMUM_RELEASE
  4498. struct new_utsname buf;
  4499. if (qemu_uname_release && *qemu_uname_release) {
  4500. return;
  4501. }
  4502. if (sys_uname(&buf)) {
  4503. return;
  4504. }
  4505. if (relstr_to_int(buf.release) < relstr_to_int(UNAME_MINIMUM_RELEASE)) {
  4506. qemu_uname_release = UNAME_MINIMUM_RELEASE;
  4507. }
  4508. #endif
  4509. }
  4510. static int open_self_maps(void *cpu_env, int fd)
  4511. {
  4512. #if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)
  4513. CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env);
  4514. TaskState *ts = cpu->opaque;
  4515. #endif
  4516. FILE *fp;
  4517. char *line = NULL;
  4518. size_t len = 0;
  4519. ssize_t read;
  4520. fp = fopen("/proc/self/maps", "r");
  4521. if (fp == NULL) {
  4522. return -EACCES;
  4523. }
  4524. while ((read = getline(&line, &len, fp)) != -1) {
  4525. int fields, dev_maj, dev_min, inode;
  4526. uint64_t min, max, offset;
  4527. char flag_r, flag_w, flag_x, flag_p;
  4528. char path[512] = "";
  4529. fields = sscanf(line, "%"PRIx64"-%"PRIx64" %c%c%c%c %"PRIx64" %x:%x %d"
  4530. " %512s", &min, &max, &flag_r, &flag_w, &flag_x,
  4531. &flag_p, &offset, &dev_maj, &dev_min, &inode, path);
  4532. if ((fields < 10) || (fields > 11)) {
  4533. continue;
  4534. }
  4535. if (!strncmp(path, "[stack]", 7)) {
  4536. continue;
  4537. }
  4538. if (h2g_valid(min) && h2g_valid(max)) {
  4539. dprintf(fd, TARGET_ABI_FMT_lx "-" TARGET_ABI_FMT_lx
  4540. " %c%c%c%c %08" PRIx64 " %02x:%02x %d %s%s\n",
  4541. h2g(min), h2g(max), flag_r, flag_w,
  4542. flag_x, flag_p, offset, dev_maj, dev_min, inode,
  4543. path[0] ? " " : "", path);
  4544. }
  4545. }
  4546. free(line);
  4547. fclose(fp);
  4548. #if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)
  4549. dprintf(fd, "%08llx-%08llx rw-p %08llx 00:00 0 [stack]\n",
  4550. (unsigned long long)ts->info->stack_limit,
  4551. (unsigned long long)(ts->info->start_stack +
  4552. (TARGET_PAGE_SIZE - 1)) & TARGET_PAGE_MASK,
  4553. (unsigned long long)0);
  4554. #endif
  4555. return 0;
  4556. }
  4557. static int open_self_stat(void *cpu_env, int fd)
  4558. {
  4559. CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env);
  4560. TaskState *ts = cpu->opaque;
  4561. abi_ulong start_stack = ts->info->start_stack;
  4562. int i;
  4563. for (i = 0; i < 44; i++) {
  4564. char buf[128];
  4565. int len;
  4566. uint64_t val = 0;
  4567. if (i == 0) {
  4568. /* pid */
  4569. val = getpid();
  4570. snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
  4571. } else if (i == 1) {
  4572. /* app name */
  4573. snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
  4574. } else if (i == 27) {
  4575. /* stack bottom */
  4576. val = start_stack;
  4577. snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
  4578. } else {
  4579. /* for the rest, there is MasterCard */
  4580. snprintf(buf, sizeof(buf), "0%c", i == 43 ? '\n' : ' ');
  4581. }
  4582. len = strlen(buf);
  4583. if (write(fd, buf, len) != len) {
  4584. return -1;
  4585. }
  4586. }
  4587. return 0;
  4588. }
  4589. static int open_self_auxv(void *cpu_env, int fd)
  4590. {
  4591. CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env);
  4592. TaskState *ts = cpu->opaque;
  4593. abi_ulong auxv = ts->info->saved_auxv;
  4594. abi_ulong len = ts->info->auxv_len;
  4595. char *ptr;
  4596. /*
  4597. * Auxiliary vector is stored in target process stack.
  4598. * read in whole auxv vector and copy it to file
  4599. */
  4600. ptr = lock_user(VERIFY_READ, auxv, len, 0);
  4601. if (ptr != NULL) {
  4602. while (len > 0) {
  4603. ssize_t r;
  4604. r = write(fd, ptr, len);
  4605. if (r <= 0) {
  4606. break;
  4607. }
  4608. len -= r;
  4609. ptr += r;
  4610. }
  4611. lseek(fd, 0, SEEK_SET);
  4612. unlock_user(ptr, auxv, len);
  4613. }
  4614. return 0;
  4615. }
  4616. static int is_proc_myself(const char *filename, const char *entry)
  4617. {
  4618. if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
  4619. filename += strlen("/proc/");
  4620. if (!strncmp(filename, "self/", strlen("self/"))) {
  4621. filename += strlen("self/");
  4622. } else if (*filename >= '1' && *filename <= '9') {
  4623. char myself[80];
  4624. snprintf(myself, sizeof(myself), "%d/", getpid());
  4625. if (!strncmp(filename, myself, strlen(myself))) {
  4626. filename += strlen(myself);
  4627. } else {
  4628. return 0;
  4629. }
  4630. } else {
  4631. return 0;
  4632. }
  4633. if (!strcmp(filename, entry)) {
  4634. return 1;
  4635. }
  4636. }
  4637. return 0;
  4638. }
  4639. #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
  4640. static int is_proc(const char *filename, const char *entry)
  4641. {
  4642. return strcmp(filename, entry) == 0;
  4643. }
  4644. static int open_net_route(void *cpu_env, int fd)
  4645. {
  4646. FILE *fp;
  4647. char *line = NULL;
  4648. size_t len = 0;
  4649. ssize_t read;
  4650. fp = fopen("/proc/net/route", "r");
  4651. if (fp == NULL) {
  4652. return -EACCES;
  4653. }
  4654. /* read header */
  4655. read = getline(&line, &len, fp);
  4656. dprintf(fd, "%s", line);
  4657. /* read routes */
  4658. while ((read = getline(&line, &len, fp)) != -1) {
  4659. char iface[16];
  4660. uint32_t dest, gw, mask;
  4661. unsigned int flags, refcnt, use, metric, mtu, window, irtt;
  4662. sscanf(line, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
  4663. iface, &dest, &gw, &flags, &refcnt, &use, &metric,
  4664. &mask, &mtu, &window, &irtt);
  4665. dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
  4666. iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
  4667. metric, tswap32(mask), mtu, window, irtt);
  4668. }
  4669. free(line);
  4670. fclose(fp);
  4671. return 0;
  4672. }
  4673. #endif
  4674. static int do_open(void *cpu_env, const char *pathname, int flags, mode_t mode)
  4675. {
  4676. struct fake_open {
  4677. const char *filename;
  4678. int (*fill)(void *cpu_env, int fd);
  4679. int (*cmp)(const char *s1, const char *s2);
  4680. };
  4681. const struct fake_open *fake_open;
  4682. static const struct fake_open fakes[] = {
  4683. { "maps", open_self_maps, is_proc_myself },
  4684. { "stat", open_self_stat, is_proc_myself },
  4685. { "auxv", open_self_auxv, is_proc_myself },
  4686. #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
  4687. { "/proc/net/route", open_net_route, is_proc },
  4688. #endif
  4689. { NULL, NULL, NULL }
  4690. };
  4691. for (fake_open = fakes; fake_open->filename; fake_open++) {
  4692. if (fake_open->cmp(pathname, fake_open->filename)) {
  4693. break;
  4694. }
  4695. }
  4696. if (fake_open->filename) {
  4697. const char *tmpdir;
  4698. char filename[PATH_MAX];
  4699. int fd, r;
  4700. /* create temporary file to map stat to */
  4701. tmpdir = getenv("TMPDIR");
  4702. if (!tmpdir)
  4703. tmpdir = "/tmp";
  4704. snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
  4705. fd = mkstemp(filename);
  4706. if (fd < 0) {
  4707. return fd;
  4708. }
  4709. unlink(filename);
  4710. if ((r = fake_open->fill(cpu_env, fd))) {
  4711. close(fd);
  4712. return r;
  4713. }
  4714. lseek(fd, 0, SEEK_SET);
  4715. return fd;
  4716. }
  4717. return get_errno(open(path(pathname), flags, mode));
  4718. }
  4719. /* do_syscall() should always have a single exit point at the end so
  4720. that actions, such as logging of syscall results, can be performed.
  4721. All errnos that do_syscall() returns must be -TARGET_<errcode>. */
  4722. abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
  4723. abi_long arg2, abi_long arg3, abi_long arg4,
  4724. abi_long arg5, abi_long arg6, abi_long arg7,
  4725. abi_long arg8)
  4726. {
  4727. CPUState *cpu = ENV_GET_CPU(cpu_env);
  4728. abi_long ret;
  4729. struct stat st;
  4730. struct statfs stfs;
  4731. void *p;
  4732. #ifdef DEBUG
  4733. gemu_log("syscall %d", num);
  4734. #endif
  4735. if(do_strace)
  4736. print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
  4737. switch(num) {
  4738. case TARGET_NR_exit:
  4739. /* In old applications this may be used to implement _exit(2).
  4740. However in threaded applictions it is used for thread termination,
  4741. and _exit_group is used for application termination.
  4742. Do thread termination if we have more then one thread. */
  4743. /* FIXME: This probably breaks if a signal arrives. We should probably
  4744. be disabling signals. */
  4745. if (CPU_NEXT(first_cpu)) {
  4746. TaskState *ts;
  4747. cpu_list_lock();
  4748. /* Remove the CPU from the list. */
  4749. QTAILQ_REMOVE(&cpus, cpu, node);
  4750. cpu_list_unlock();
  4751. ts = cpu->opaque;
  4752. if (ts->child_tidptr) {
  4753. put_user_u32(0, ts->child_tidptr);
  4754. sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
  4755. NULL, NULL, 0);
  4756. }
  4757. thread_cpu = NULL;
  4758. object_unref(OBJECT(cpu));
  4759. g_free(ts);
  4760. pthread_exit(NULL);
  4761. }
  4762. #ifdef TARGET_GPROF
  4763. _mcleanup();
  4764. #endif
  4765. gdb_exit(cpu_env, arg1);
  4766. _exit(arg1);
  4767. ret = 0; /* avoid warning */
  4768. break;
  4769. case TARGET_NR_read:
  4770. if (arg3 == 0)
  4771. ret = 0;
  4772. else {
  4773. if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
  4774. goto efault;
  4775. ret = get_errno(read(arg1, p, arg3));
  4776. unlock_user(p, arg2, ret);
  4777. }
  4778. break;
  4779. case TARGET_NR_write:
  4780. if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
  4781. goto efault;
  4782. ret = get_errno(write(arg1, p, arg3));
  4783. unlock_user(p, arg2, 0);
  4784. break;
  4785. case TARGET_NR_open:
  4786. if (!(p = lock_user_string(arg1)))
  4787. goto efault;
  4788. ret = get_errno(do_open(cpu_env, p,
  4789. target_to_host_bitmask(arg2, fcntl_flags_tbl),
  4790. arg3));
  4791. unlock_user(p, arg1, 0);
  4792. break;
  4793. #if defined(TARGET_NR_openat) && defined(__NR_openat)
  4794. case TARGET_NR_openat:
  4795. if (!(p = lock_user_string(arg2)))
  4796. goto efault;
  4797. ret = get_errno(sys_openat(arg1,
  4798. path(p),
  4799. target_to_host_bitmask(arg3, fcntl_flags_tbl),
  4800. arg4));
  4801. unlock_user(p, arg2, 0);
  4802. break;
  4803. #endif
  4804. case TARGET_NR_close:
  4805. ret = get_errno(close(arg1));
  4806. break;
  4807. case TARGET_NR_brk:
  4808. ret = do_brk(arg1);
  4809. break;
  4810. case TARGET_NR_fork:
  4811. ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
  4812. break;
  4813. #ifdef TARGET_NR_waitpid
  4814. case TARGET_NR_waitpid:
  4815. {
  4816. int status;
  4817. ret = get_errno(waitpid(arg1, &status, arg3));
  4818. if (!is_error(ret) && arg2 && ret
  4819. && put_user_s32(host_to_target_waitstatus(status), arg2))
  4820. goto efault;
  4821. }
  4822. break;
  4823. #endif
  4824. #ifdef TARGET_NR_waitid
  4825. case TARGET_NR_waitid:
  4826. {
  4827. siginfo_t info;
  4828. info.si_pid = 0;
  4829. ret = get_errno(waitid(arg1, arg2, &info, arg4));
  4830. if (!is_error(ret) && arg3 && info.si_pid != 0) {
  4831. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
  4832. goto efault;
  4833. host_to_target_siginfo(p, &info);
  4834. unlock_user(p, arg3, sizeof(target_siginfo_t));
  4835. }
  4836. }
  4837. break;
  4838. #endif
  4839. #ifdef TARGET_NR_creat /* not on alpha */
  4840. case TARGET_NR_creat:
  4841. if (!(p = lock_user_string(arg1)))
  4842. goto efault;
  4843. ret = get_errno(creat(p, arg2));
  4844. unlock_user(p, arg1, 0);
  4845. break;
  4846. #endif
  4847. case TARGET_NR_link:
  4848. {
  4849. void * p2;
  4850. p = lock_user_string(arg1);
  4851. p2 = lock_user_string(arg2);
  4852. if (!p || !p2)
  4853. ret = -TARGET_EFAULT;
  4854. else
  4855. ret = get_errno(link(p, p2));
  4856. unlock_user(p2, arg2, 0);
  4857. unlock_user(p, arg1, 0);
  4858. }
  4859. break;
  4860. #if defined(TARGET_NR_linkat)
  4861. case TARGET_NR_linkat:
  4862. {
  4863. void * p2 = NULL;
  4864. if (!arg2 || !arg4)
  4865. goto efault;
  4866. p = lock_user_string(arg2);
  4867. p2 = lock_user_string(arg4);
  4868. if (!p || !p2)
  4869. ret = -TARGET_EFAULT;
  4870. else
  4871. ret = get_errno(linkat(arg1, p, arg3, p2, arg5));
  4872. unlock_user(p, arg2, 0);
  4873. unlock_user(p2, arg4, 0);
  4874. }
  4875. break;
  4876. #endif
  4877. case TARGET_NR_unlink:
  4878. if (!(p = lock_user_string(arg1)))
  4879. goto efault;
  4880. ret = get_errno(unlink(p));
  4881. unlock_user(p, arg1, 0);
  4882. break;
  4883. #if defined(TARGET_NR_unlinkat)
  4884. case TARGET_NR_unlinkat:
  4885. if (!(p = lock_user_string(arg2)))
  4886. goto efault;
  4887. ret = get_errno(unlinkat(arg1, p, arg3));
  4888. unlock_user(p, arg2, 0);
  4889. break;
  4890. #endif
  4891. case TARGET_NR_execve:
  4892. {
  4893. char **argp, **envp;
  4894. int argc, envc;
  4895. abi_ulong gp;
  4896. abi_ulong guest_argp;
  4897. abi_ulong guest_envp;
  4898. abi_ulong addr;
  4899. char **q;
  4900. int total_size = 0;
  4901. argc = 0;
  4902. guest_argp = arg2;
  4903. for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
  4904. if (get_user_ual(addr, gp))
  4905. goto efault;
  4906. if (!addr)
  4907. break;
  4908. argc++;
  4909. }
  4910. envc = 0;
  4911. guest_envp = arg3;
  4912. for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
  4913. if (get_user_ual(addr, gp))
  4914. goto efault;
  4915. if (!addr)
  4916. break;
  4917. envc++;
  4918. }
  4919. argp = alloca((argc + 1) * sizeof(void *));
  4920. envp = alloca((envc + 1) * sizeof(void *));
  4921. for (gp = guest_argp, q = argp; gp;
  4922. gp += sizeof(abi_ulong), q++) {
  4923. if (get_user_ual(addr, gp))
  4924. goto execve_efault;
  4925. if (!addr)
  4926. break;
  4927. if (!(*q = lock_user_string(addr)))
  4928. goto execve_efault;
  4929. total_size += strlen(*q) + 1;
  4930. }
  4931. *q = NULL;
  4932. for (gp = guest_envp, q = envp; gp;
  4933. gp += sizeof(abi_ulong), q++) {
  4934. if (get_user_ual(addr, gp))
  4935. goto execve_efault;
  4936. if (!addr)
  4937. break;
  4938. if (!(*q = lock_user_string(addr)))
  4939. goto execve_efault;
  4940. total_size += strlen(*q) + 1;
  4941. }
  4942. *q = NULL;
  4943. /* This case will not be caught by the host's execve() if its
  4944. page size is bigger than the target's. */
  4945. if (total_size > MAX_ARG_PAGES * TARGET_PAGE_SIZE) {
  4946. ret = -TARGET_E2BIG;
  4947. goto execve_end;
  4948. }
  4949. if (!(p = lock_user_string(arg1)))
  4950. goto execve_efault;
  4951. ret = get_errno(execve(p, argp, envp));
  4952. unlock_user(p, arg1, 0);
  4953. goto execve_end;
  4954. execve_efault:
  4955. ret = -TARGET_EFAULT;
  4956. execve_end:
  4957. for (gp = guest_argp, q = argp; *q;
  4958. gp += sizeof(abi_ulong), q++) {
  4959. if (get_user_ual(addr, gp)
  4960. || !addr)
  4961. break;
  4962. unlock_user(*q, addr, 0);
  4963. }
  4964. for (gp = guest_envp, q = envp; *q;
  4965. gp += sizeof(abi_ulong), q++) {
  4966. if (get_user_ual(addr, gp)
  4967. || !addr)
  4968. break;
  4969. unlock_user(*q, addr, 0);
  4970. }
  4971. }
  4972. break;
  4973. case TARGET_NR_chdir:
  4974. if (!(p = lock_user_string(arg1)))
  4975. goto efault;
  4976. ret = get_errno(chdir(p));
  4977. unlock_user(p, arg1, 0);
  4978. break;
  4979. #ifdef TARGET_NR_time
  4980. case TARGET_NR_time:
  4981. {
  4982. time_t host_time;
  4983. ret = get_errno(time(&host_time));
  4984. if (!is_error(ret)
  4985. && arg1
  4986. && put_user_sal(host_time, arg1))
  4987. goto efault;
  4988. }
  4989. break;
  4990. #endif
  4991. case TARGET_NR_mknod:
  4992. if (!(p = lock_user_string(arg1)))
  4993. goto efault;
  4994. ret = get_errno(mknod(p, arg2, arg3));
  4995. unlock_user(p, arg1, 0);
  4996. break;
  4997. #if defined(TARGET_NR_mknodat)
  4998. case TARGET_NR_mknodat:
  4999. if (!(p = lock_user_string(arg2)))
  5000. goto efault;
  5001. ret = get_errno(mknodat(arg1, p, arg3, arg4));
  5002. unlock_user(p, arg2, 0);
  5003. break;
  5004. #endif
  5005. case TARGET_NR_chmod:
  5006. if (!(p = lock_user_string(arg1)))
  5007. goto efault;
  5008. ret = get_errno(chmod(p, arg2));
  5009. unlock_user(p, arg1, 0);
  5010. break;
  5011. #ifdef TARGET_NR_break
  5012. case TARGET_NR_break:
  5013. goto unimplemented;
  5014. #endif
  5015. #ifdef TARGET_NR_oldstat
  5016. case TARGET_NR_oldstat:
  5017. goto unimplemented;
  5018. #endif
  5019. case TARGET_NR_lseek:
  5020. ret = get_errno(lseek(arg1, arg2, arg3));
  5021. break;
  5022. #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
  5023. /* Alpha specific */
  5024. case TARGET_NR_getxpid:
  5025. ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
  5026. ret = get_errno(getpid());
  5027. break;
  5028. #endif
  5029. #ifdef TARGET_NR_getpid
  5030. case TARGET_NR_getpid:
  5031. ret = get_errno(getpid());
  5032. break;
  5033. #endif
  5034. case TARGET_NR_mount:
  5035. {
  5036. /* need to look at the data field */
  5037. void *p2, *p3;
  5038. p = lock_user_string(arg1);
  5039. p2 = lock_user_string(arg2);
  5040. p3 = lock_user_string(arg3);
  5041. if (!p || !p2 || !p3)
  5042. ret = -TARGET_EFAULT;
  5043. else {
  5044. /* FIXME - arg5 should be locked, but it isn't clear how to
  5045. * do that since it's not guaranteed to be a NULL-terminated
  5046. * string.
  5047. */
  5048. if ( ! arg5 )
  5049. ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, NULL));
  5050. else
  5051. ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
  5052. }
  5053. unlock_user(p, arg1, 0);
  5054. unlock_user(p2, arg2, 0);
  5055. unlock_user(p3, arg3, 0);
  5056. break;
  5057. }
  5058. #ifdef TARGET_NR_umount
  5059. case TARGET_NR_umount:
  5060. if (!(p = lock_user_string(arg1)))
  5061. goto efault;
  5062. ret = get_errno(umount(p));
  5063. unlock_user(p, arg1, 0);
  5064. break;
  5065. #endif
  5066. #ifdef TARGET_NR_stime /* not on alpha */
  5067. case TARGET_NR_stime:
  5068. {
  5069. time_t host_time;
  5070. if (get_user_sal(host_time, arg1))
  5071. goto efault;
  5072. ret = get_errno(stime(&host_time));
  5073. }
  5074. break;
  5075. #endif
  5076. case TARGET_NR_ptrace:
  5077. goto unimplemented;
  5078. #ifdef TARGET_NR_alarm /* not on alpha */
  5079. case TARGET_NR_alarm:
  5080. ret = alarm(arg1);
  5081. break;
  5082. #endif
  5083. #ifdef TARGET_NR_oldfstat
  5084. case TARGET_NR_oldfstat:
  5085. goto unimplemented;
  5086. #endif
  5087. #ifdef TARGET_NR_pause /* not on alpha */
  5088. case TARGET_NR_pause:
  5089. ret = get_errno(pause());
  5090. break;
  5091. #endif
  5092. #ifdef TARGET_NR_utime
  5093. case TARGET_NR_utime:
  5094. {
  5095. struct utimbuf tbuf, *host_tbuf;
  5096. struct target_utimbuf *target_tbuf;
  5097. if (arg2) {
  5098. if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
  5099. goto efault;
  5100. tbuf.actime = tswapal(target_tbuf->actime);
  5101. tbuf.modtime = tswapal(target_tbuf->modtime);
  5102. unlock_user_struct(target_tbuf, arg2, 0);
  5103. host_tbuf = &tbuf;
  5104. } else {
  5105. host_tbuf = NULL;
  5106. }
  5107. if (!(p = lock_user_string(arg1)))
  5108. goto efault;
  5109. ret = get_errno(utime(p, host_tbuf));
  5110. unlock_user(p, arg1, 0);
  5111. }
  5112. break;
  5113. #endif
  5114. case TARGET_NR_utimes:
  5115. {
  5116. struct timeval *tvp, tv[2];
  5117. if (arg2) {
  5118. if (copy_from_user_timeval(&tv[0], arg2)
  5119. || copy_from_user_timeval(&tv[1],
  5120. arg2 + sizeof(struct target_timeval)))
  5121. goto efault;
  5122. tvp = tv;
  5123. } else {
  5124. tvp = NULL;
  5125. }
  5126. if (!(p = lock_user_string(arg1)))
  5127. goto efault;
  5128. ret = get_errno(utimes(p, tvp));
  5129. unlock_user(p, arg1, 0);
  5130. }
  5131. break;
  5132. #if defined(TARGET_NR_futimesat)
  5133. case TARGET_NR_futimesat:
  5134. {
  5135. struct timeval *tvp, tv[2];
  5136. if (arg3) {
  5137. if (copy_from_user_timeval(&tv[0], arg3)
  5138. || copy_from_user_timeval(&tv[1],
  5139. arg3 + sizeof(struct target_timeval)))
  5140. goto efault;
  5141. tvp = tv;
  5142. } else {
  5143. tvp = NULL;
  5144. }
  5145. if (!(p = lock_user_string(arg2)))
  5146. goto efault;
  5147. ret = get_errno(futimesat(arg1, path(p), tvp));
  5148. unlock_user(p, arg2, 0);
  5149. }
  5150. break;
  5151. #endif
  5152. #ifdef TARGET_NR_stty
  5153. case TARGET_NR_stty:
  5154. goto unimplemented;
  5155. #endif
  5156. #ifdef TARGET_NR_gtty
  5157. case TARGET_NR_gtty:
  5158. goto unimplemented;
  5159. #endif
  5160. case TARGET_NR_access:
  5161. if (!(p = lock_user_string(arg1)))
  5162. goto efault;
  5163. ret = get_errno(access(path(p), arg2));
  5164. unlock_user(p, arg1, 0);
  5165. break;
  5166. #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
  5167. case TARGET_NR_faccessat:
  5168. if (!(p = lock_user_string(arg2)))
  5169. goto efault;
  5170. ret = get_errno(faccessat(arg1, p, arg3, 0));
  5171. unlock_user(p, arg2, 0);
  5172. break;
  5173. #endif
  5174. #ifdef TARGET_NR_nice /* not on alpha */
  5175. case TARGET_NR_nice:
  5176. ret = get_errno(nice(arg1));
  5177. break;
  5178. #endif
  5179. #ifdef TARGET_NR_ftime
  5180. case TARGET_NR_ftime:
  5181. goto unimplemented;
  5182. #endif
  5183. case TARGET_NR_sync:
  5184. sync();
  5185. ret = 0;
  5186. break;
  5187. case TARGET_NR_kill:
  5188. ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
  5189. break;
  5190. case TARGET_NR_rename:
  5191. {
  5192. void *p2;
  5193. p = lock_user_string(arg1);
  5194. p2 = lock_user_string(arg2);
  5195. if (!p || !p2)
  5196. ret = -TARGET_EFAULT;
  5197. else
  5198. ret = get_errno(rename(p, p2));
  5199. unlock_user(p2, arg2, 0);
  5200. unlock_user(p, arg1, 0);
  5201. }
  5202. break;
  5203. #if defined(TARGET_NR_renameat)
  5204. case TARGET_NR_renameat:
  5205. {
  5206. void *p2;
  5207. p = lock_user_string(arg2);
  5208. p2 = lock_user_string(arg4);
  5209. if (!p || !p2)
  5210. ret = -TARGET_EFAULT;
  5211. else
  5212. ret = get_errno(renameat(arg1, p, arg3, p2));
  5213. unlock_user(p2, arg4, 0);
  5214. unlock_user(p, arg2, 0);
  5215. }
  5216. break;
  5217. #endif
  5218. case TARGET_NR_mkdir:
  5219. if (!(p = lock_user_string(arg1)))
  5220. goto efault;
  5221. ret = get_errno(mkdir(p, arg2));
  5222. unlock_user(p, arg1, 0);
  5223. break;
  5224. #if defined(TARGET_NR_mkdirat)
  5225. case TARGET_NR_mkdirat:
  5226. if (!(p = lock_user_string(arg2)))
  5227. goto efault;
  5228. ret = get_errno(mkdirat(arg1, p, arg3));
  5229. unlock_user(p, arg2, 0);
  5230. break;
  5231. #endif
  5232. case TARGET_NR_rmdir:
  5233. if (!(p = lock_user_string(arg1)))
  5234. goto efault;
  5235. ret = get_errno(rmdir(p));
  5236. unlock_user(p, arg1, 0);
  5237. break;
  5238. case TARGET_NR_dup:
  5239. ret = get_errno(dup(arg1));
  5240. break;
  5241. case TARGET_NR_pipe:
  5242. ret = do_pipe(cpu_env, arg1, 0, 0);
  5243. break;
  5244. #ifdef TARGET_NR_pipe2
  5245. case TARGET_NR_pipe2:
  5246. ret = do_pipe(cpu_env, arg1,
  5247. target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
  5248. break;
  5249. #endif
  5250. case TARGET_NR_times:
  5251. {
  5252. struct target_tms *tmsp;
  5253. struct tms tms;
  5254. ret = get_errno(times(&tms));
  5255. if (arg1) {
  5256. tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
  5257. if (!tmsp)
  5258. goto efault;
  5259. tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
  5260. tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
  5261. tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
  5262. tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
  5263. }
  5264. if (!is_error(ret))
  5265. ret = host_to_target_clock_t(ret);
  5266. }
  5267. break;
  5268. #ifdef TARGET_NR_prof
  5269. case TARGET_NR_prof:
  5270. goto unimplemented;
  5271. #endif
  5272. #ifdef TARGET_NR_signal
  5273. case TARGET_NR_signal:
  5274. goto unimplemented;
  5275. #endif
  5276. case TARGET_NR_acct:
  5277. if (arg1 == 0) {
  5278. ret = get_errno(acct(NULL));
  5279. } else {
  5280. if (!(p = lock_user_string(arg1)))
  5281. goto efault;
  5282. ret = get_errno(acct(path(p)));
  5283. unlock_user(p, arg1, 0);
  5284. }
  5285. break;
  5286. #ifdef TARGET_NR_umount2
  5287. case TARGET_NR_umount2:
  5288. if (!(p = lock_user_string(arg1)))
  5289. goto efault;
  5290. ret = get_errno(umount2(p, arg2));
  5291. unlock_user(p, arg1, 0);
  5292. break;
  5293. #endif
  5294. #ifdef TARGET_NR_lock
  5295. case TARGET_NR_lock:
  5296. goto unimplemented;
  5297. #endif
  5298. case TARGET_NR_ioctl:
  5299. ret = do_ioctl(arg1, arg2, arg3);
  5300. break;
  5301. case TARGET_NR_fcntl:
  5302. ret = do_fcntl(arg1, arg2, arg3);
  5303. break;
  5304. #ifdef TARGET_NR_mpx
  5305. case TARGET_NR_mpx:
  5306. goto unimplemented;
  5307. #endif
  5308. case TARGET_NR_setpgid:
  5309. ret = get_errno(setpgid(arg1, arg2));
  5310. break;
  5311. #ifdef TARGET_NR_ulimit
  5312. case TARGET_NR_ulimit:
  5313. goto unimplemented;
  5314. #endif
  5315. #ifdef TARGET_NR_oldolduname
  5316. case TARGET_NR_oldolduname:
  5317. goto unimplemented;
  5318. #endif
  5319. case TARGET_NR_umask:
  5320. ret = get_errno(umask(arg1));
  5321. break;
  5322. case TARGET_NR_chroot:
  5323. if (!(p = lock_user_string(arg1)))
  5324. goto efault;
  5325. ret = get_errno(chroot(p));
  5326. unlock_user(p, arg1, 0);
  5327. break;
  5328. case TARGET_NR_ustat:
  5329. goto unimplemented;
  5330. case TARGET_NR_dup2:
  5331. ret = get_errno(dup2(arg1, arg2));
  5332. break;
  5333. #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
  5334. case TARGET_NR_dup3:
  5335. ret = get_errno(dup3(arg1, arg2, arg3));
  5336. break;
  5337. #endif
  5338. #ifdef TARGET_NR_getppid /* not on alpha */
  5339. case TARGET_NR_getppid:
  5340. ret = get_errno(getppid());
  5341. break;
  5342. #endif
  5343. case TARGET_NR_getpgrp:
  5344. ret = get_errno(getpgrp());
  5345. break;
  5346. case TARGET_NR_setsid:
  5347. ret = get_errno(setsid());
  5348. break;
  5349. #ifdef TARGET_NR_sigaction
  5350. case TARGET_NR_sigaction:
  5351. {
  5352. #if defined(TARGET_ALPHA)
  5353. struct target_sigaction act, oact, *pact = 0;
  5354. struct target_old_sigaction *old_act;
  5355. if (arg2) {
  5356. if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
  5357. goto efault;
  5358. act._sa_handler = old_act->_sa_handler;
  5359. target_siginitset(&act.sa_mask, old_act->sa_mask);
  5360. act.sa_flags = old_act->sa_flags;
  5361. act.sa_restorer = 0;
  5362. unlock_user_struct(old_act, arg2, 0);
  5363. pact = &act;
  5364. }
  5365. ret = get_errno(do_sigaction(arg1, pact, &oact));
  5366. if (!is_error(ret) && arg3) {
  5367. if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
  5368. goto efault;
  5369. old_act->_sa_handler = oact._sa_handler;
  5370. old_act->sa_mask = oact.sa_mask.sig[0];
  5371. old_act->sa_flags = oact.sa_flags;
  5372. unlock_user_struct(old_act, arg3, 1);
  5373. }
  5374. #elif defined(TARGET_MIPS)
  5375. struct target_sigaction act, oact, *pact, *old_act;
  5376. if (arg2) {
  5377. if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
  5378. goto efault;
  5379. act._sa_handler = old_act->_sa_handler;
  5380. target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
  5381. act.sa_flags = old_act->sa_flags;
  5382. unlock_user_struct(old_act, arg2, 0);
  5383. pact = &act;
  5384. } else {
  5385. pact = NULL;
  5386. }
  5387. ret = get_errno(do_sigaction(arg1, pact, &oact));
  5388. if (!is_error(ret) && arg3) {
  5389. if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
  5390. goto efault;
  5391. old_act->_sa_handler = oact._sa_handler;
  5392. old_act->sa_flags = oact.sa_flags;
  5393. old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
  5394. old_act->sa_mask.sig[1] = 0;
  5395. old_act->sa_mask.sig[2] = 0;
  5396. old_act->sa_mask.sig[3] = 0;
  5397. unlock_user_struct(old_act, arg3, 1);
  5398. }
  5399. #else
  5400. struct target_old_sigaction *old_act;
  5401. struct target_sigaction act, oact, *pact;
  5402. if (arg2) {
  5403. if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
  5404. goto efault;
  5405. act._sa_handler = old_act->_sa_handler;
  5406. target_siginitset(&act.sa_mask, old_act->sa_mask);
  5407. act.sa_flags = old_act->sa_flags;
  5408. act.sa_restorer = old_act->sa_restorer;
  5409. unlock_user_struct(old_act, arg2, 0);
  5410. pact = &act;
  5411. } else {
  5412. pact = NULL;
  5413. }
  5414. ret = get_errno(do_sigaction(arg1, pact, &oact));
  5415. if (!is_error(ret) && arg3) {
  5416. if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
  5417. goto efault;
  5418. old_act->_sa_handler = oact._sa_handler;
  5419. old_act->sa_mask = oact.sa_mask.sig[0];
  5420. old_act->sa_flags = oact.sa_flags;
  5421. old_act->sa_restorer = oact.sa_restorer;
  5422. unlock_user_struct(old_act, arg3, 1);
  5423. }
  5424. #endif
  5425. }
  5426. break;
  5427. #endif
  5428. case TARGET_NR_rt_sigaction:
  5429. {
  5430. #if defined(TARGET_ALPHA)
  5431. struct target_sigaction act, oact, *pact = 0;
  5432. struct target_rt_sigaction *rt_act;
  5433. /* ??? arg4 == sizeof(sigset_t). */
  5434. if (arg2) {
  5435. if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
  5436. goto efault;
  5437. act._sa_handler = rt_act->_sa_handler;
  5438. act.sa_mask = rt_act->sa_mask;
  5439. act.sa_flags = rt_act->sa_flags;
  5440. act.sa_restorer = arg5;
  5441. unlock_user_struct(rt_act, arg2, 0);
  5442. pact = &act;
  5443. }
  5444. ret = get_errno(do_sigaction(arg1, pact, &oact));
  5445. if (!is_error(ret) && arg3) {
  5446. if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
  5447. goto efault;
  5448. rt_act->_sa_handler = oact._sa_handler;
  5449. rt_act->sa_mask = oact.sa_mask;
  5450. rt_act->sa_flags = oact.sa_flags;
  5451. unlock_user_struct(rt_act, arg3, 1);
  5452. }
  5453. #else
  5454. struct target_sigaction *act;
  5455. struct target_sigaction *oact;
  5456. if (arg2) {
  5457. if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
  5458. goto efault;
  5459. } else
  5460. act = NULL;
  5461. if (arg3) {
  5462. if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
  5463. ret = -TARGET_EFAULT;
  5464. goto rt_sigaction_fail;
  5465. }
  5466. } else
  5467. oact = NULL;
  5468. ret = get_errno(do_sigaction(arg1, act, oact));
  5469. rt_sigaction_fail:
  5470. if (act)
  5471. unlock_user_struct(act, arg2, 0);
  5472. if (oact)
  5473. unlock_user_struct(oact, arg3, 1);
  5474. #endif
  5475. }
  5476. break;
  5477. #ifdef TARGET_NR_sgetmask /* not on alpha */
  5478. case TARGET_NR_sgetmask:
  5479. {
  5480. sigset_t cur_set;
  5481. abi_ulong target_set;
  5482. do_sigprocmask(0, NULL, &cur_set);
  5483. host_to_target_old_sigset(&target_set, &cur_set);
  5484. ret = target_set;
  5485. }
  5486. break;
  5487. #endif
  5488. #ifdef TARGET_NR_ssetmask /* not on alpha */
  5489. case TARGET_NR_ssetmask:
  5490. {
  5491. sigset_t set, oset, cur_set;
  5492. abi_ulong target_set = arg1;
  5493. do_sigprocmask(0, NULL, &cur_set);
  5494. target_to_host_old_sigset(&set, &target_set);
  5495. sigorset(&set, &set, &cur_set);
  5496. do_sigprocmask(SIG_SETMASK, &set, &oset);
  5497. host_to_target_old_sigset(&target_set, &oset);
  5498. ret = target_set;
  5499. }
  5500. break;
  5501. #endif
  5502. #ifdef TARGET_NR_sigprocmask
  5503. case TARGET_NR_sigprocmask:
  5504. {
  5505. #if defined(TARGET_ALPHA)
  5506. sigset_t set, oldset;
  5507. abi_ulong mask;
  5508. int how;
  5509. switch (arg1) {
  5510. case TARGET_SIG_BLOCK:
  5511. how = SIG_BLOCK;
  5512. break;
  5513. case TARGET_SIG_UNBLOCK:
  5514. how = SIG_UNBLOCK;
  5515. break;
  5516. case TARGET_SIG_SETMASK:
  5517. how = SIG_SETMASK;
  5518. break;
  5519. default:
  5520. ret = -TARGET_EINVAL;
  5521. goto fail;
  5522. }
  5523. mask = arg2;
  5524. target_to_host_old_sigset(&set, &mask);
  5525. ret = get_errno(do_sigprocmask(how, &set, &oldset));
  5526. if (!is_error(ret)) {
  5527. host_to_target_old_sigset(&mask, &oldset);
  5528. ret = mask;
  5529. ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0; /* force no error */
  5530. }
  5531. #else
  5532. sigset_t set, oldset, *set_ptr;
  5533. int how;
  5534. if (arg2) {
  5535. switch (arg1) {
  5536. case TARGET_SIG_BLOCK:
  5537. how = SIG_BLOCK;
  5538. break;
  5539. case TARGET_SIG_UNBLOCK:
  5540. how = SIG_UNBLOCK;
  5541. break;
  5542. case TARGET_SIG_SETMASK:
  5543. how = SIG_SETMASK;
  5544. break;
  5545. default:
  5546. ret = -TARGET_EINVAL;
  5547. goto fail;
  5548. }
  5549. if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
  5550. goto efault;
  5551. target_to_host_old_sigset(&set, p);
  5552. unlock_user(p, arg2, 0);
  5553. set_ptr = &set;
  5554. } else {
  5555. how = 0;
  5556. set_ptr = NULL;
  5557. }
  5558. ret = get_errno(do_sigprocmask(how, set_ptr, &oldset));
  5559. if (!is_error(ret) && arg3) {
  5560. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
  5561. goto efault;
  5562. host_to_target_old_sigset(p, &oldset);
  5563. unlock_user(p, arg3, sizeof(target_sigset_t));
  5564. }
  5565. #endif
  5566. }
  5567. break;
  5568. #endif
  5569. case TARGET_NR_rt_sigprocmask:
  5570. {
  5571. int how = arg1;
  5572. sigset_t set, oldset, *set_ptr;
  5573. if (arg2) {
  5574. switch(how) {
  5575. case TARGET_SIG_BLOCK:
  5576. how = SIG_BLOCK;
  5577. break;
  5578. case TARGET_SIG_UNBLOCK:
  5579. how = SIG_UNBLOCK;
  5580. break;
  5581. case TARGET_SIG_SETMASK:
  5582. how = SIG_SETMASK;
  5583. break;
  5584. default:
  5585. ret = -TARGET_EINVAL;
  5586. goto fail;
  5587. }
  5588. if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
  5589. goto efault;
  5590. target_to_host_sigset(&set, p);
  5591. unlock_user(p, arg2, 0);
  5592. set_ptr = &set;
  5593. } else {
  5594. how = 0;
  5595. set_ptr = NULL;
  5596. }
  5597. ret = get_errno(do_sigprocmask(how, set_ptr, &oldset));
  5598. if (!is_error(ret) && arg3) {
  5599. if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
  5600. goto efault;
  5601. host_to_target_sigset(p, &oldset);
  5602. unlock_user(p, arg3, sizeof(target_sigset_t));
  5603. }
  5604. }
  5605. break;
  5606. #ifdef TARGET_NR_sigpending
  5607. case TARGET_NR_sigpending:
  5608. {
  5609. sigset_t set;
  5610. ret = get_errno(sigpending(&set));
  5611. if (!is_error(ret)) {
  5612. if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
  5613. goto efault;
  5614. host_to_target_old_sigset(p, &set);
  5615. unlock_user(p, arg1, sizeof(target_sigset_t));
  5616. }
  5617. }
  5618. break;
  5619. #endif
  5620. case TARGET_NR_rt_sigpending:
  5621. {
  5622. sigset_t set;
  5623. ret = get_errno(sigpending(&set));
  5624. if (!is_error(ret)) {
  5625. if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
  5626. goto efault;
  5627. host_to_target_sigset(p, &set);
  5628. unlock_user(p, arg1, sizeof(target_sigset_t));
  5629. }
  5630. }
  5631. break;
  5632. #ifdef TARGET_NR_sigsuspend
  5633. case TARGET_NR_sigsuspend:
  5634. {
  5635. sigset_t set;
  5636. #if defined(TARGET_ALPHA)
  5637. abi_ulong mask = arg1;
  5638. target_to_host_old_sigset(&set, &mask);
  5639. #else
  5640. if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
  5641. goto efault;
  5642. target_to_host_old_sigset(&set, p);
  5643. unlock_user(p, arg1, 0);
  5644. #endif
  5645. ret = get_errno(sigsuspend(&set));
  5646. }
  5647. break;
  5648. #endif
  5649. case TARGET_NR_rt_sigsuspend:
  5650. {
  5651. sigset_t set;
  5652. if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
  5653. goto efault;
  5654. target_to_host_sigset(&set, p);
  5655. unlock_user(p, arg1, 0);
  5656. ret = get_errno(sigsuspend(&set));
  5657. }
  5658. break;
  5659. case TARGET_NR_rt_sigtimedwait:
  5660. {
  5661. sigset_t set;
  5662. struct timespec uts, *puts;
  5663. siginfo_t uinfo;
  5664. if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
  5665. goto efault;
  5666. target_to_host_sigset(&set, p);
  5667. unlock_user(p, arg1, 0);
  5668. if (arg3) {
  5669. puts = &uts;
  5670. target_to_host_timespec(puts, arg3);
  5671. } else {
  5672. puts = NULL;
  5673. }
  5674. ret = get_errno(sigtimedwait(&set, &uinfo, puts));
  5675. if (!is_error(ret)) {
  5676. if (arg2) {
  5677. p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t),
  5678. 0);
  5679. if (!p) {
  5680. goto efault;
  5681. }
  5682. host_to_target_siginfo(p, &uinfo);
  5683. unlock_user(p, arg2, sizeof(target_siginfo_t));
  5684. }
  5685. ret = host_to_target_signal(ret);
  5686. }
  5687. }
  5688. break;
  5689. case TARGET_NR_rt_sigqueueinfo:
  5690. {
  5691. siginfo_t uinfo;
  5692. if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
  5693. goto efault;
  5694. target_to_host_siginfo(&uinfo, p);
  5695. unlock_user(p, arg1, 0);
  5696. ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
  5697. }
  5698. break;
  5699. #ifdef TARGET_NR_sigreturn
  5700. case TARGET_NR_sigreturn:
  5701. /* NOTE: ret is eax, so not transcoding must be done */
  5702. ret = do_sigreturn(cpu_env);
  5703. break;
  5704. #endif
  5705. case TARGET_NR_rt_sigreturn:
  5706. /* NOTE: ret is eax, so not transcoding must be done */
  5707. ret = do_rt_sigreturn(cpu_env);
  5708. break;
  5709. case TARGET_NR_sethostname:
  5710. if (!(p = lock_user_string(arg1)))
  5711. goto efault;
  5712. ret = get_errno(sethostname(p, arg2));
  5713. unlock_user(p, arg1, 0);
  5714. break;
  5715. case TARGET_NR_setrlimit:
  5716. {
  5717. int resource = target_to_host_resource(arg1);
  5718. struct target_rlimit *target_rlim;
  5719. struct rlimit rlim;
  5720. if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
  5721. goto efault;
  5722. rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
  5723. rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
  5724. unlock_user_struct(target_rlim, arg2, 0);
  5725. ret = get_errno(setrlimit(resource, &rlim));
  5726. }
  5727. break;
  5728. case TARGET_NR_getrlimit:
  5729. {
  5730. int resource = target_to_host_resource(arg1);
  5731. struct target_rlimit *target_rlim;
  5732. struct rlimit rlim;
  5733. ret = get_errno(getrlimit(resource, &rlim));
  5734. if (!is_error(ret)) {
  5735. if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
  5736. goto efault;
  5737. target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
  5738. target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
  5739. unlock_user_struct(target_rlim, arg2, 1);
  5740. }
  5741. }
  5742. break;
  5743. case TARGET_NR_getrusage:
  5744. {
  5745. struct rusage rusage;
  5746. ret = get_errno(getrusage(arg1, &rusage));
  5747. if (!is_error(ret)) {
  5748. host_to_target_rusage(arg2, &rusage);
  5749. }
  5750. }
  5751. break;
  5752. case TARGET_NR_gettimeofday:
  5753. {
  5754. struct timeval tv;
  5755. ret = get_errno(gettimeofday(&tv, NULL));
  5756. if (!is_error(ret)) {
  5757. if (copy_to_user_timeval(arg1, &tv))
  5758. goto efault;
  5759. }
  5760. }
  5761. break;
  5762. case TARGET_NR_settimeofday:
  5763. {
  5764. struct timeval tv;
  5765. if (copy_from_user_timeval(&tv, arg1))
  5766. goto efault;
  5767. ret = get_errno(settimeofday(&tv, NULL));
  5768. }
  5769. break;
  5770. #if defined(TARGET_NR_select)
  5771. case TARGET_NR_select:
  5772. #if defined(TARGET_S390X) || defined(TARGET_ALPHA)
  5773. ret = do_select(arg1, arg2, arg3, arg4, arg5);
  5774. #else
  5775. {
  5776. struct target_sel_arg_struct *sel;
  5777. abi_ulong inp, outp, exp, tvp;
  5778. long nsel;
  5779. if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
  5780. goto efault;
  5781. nsel = tswapal(sel->n);
  5782. inp = tswapal(sel->inp);
  5783. outp = tswapal(sel->outp);
  5784. exp = tswapal(sel->exp);
  5785. tvp = tswapal(sel->tvp);
  5786. unlock_user_struct(sel, arg1, 0);
  5787. ret = do_select(nsel, inp, outp, exp, tvp);
  5788. }
  5789. #endif
  5790. break;
  5791. #endif
  5792. #ifdef TARGET_NR_pselect6
  5793. case TARGET_NR_pselect6:
  5794. {
  5795. abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
  5796. fd_set rfds, wfds, efds;
  5797. fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
  5798. struct timespec ts, *ts_ptr;
  5799. /*
  5800. * The 6th arg is actually two args smashed together,
  5801. * so we cannot use the C library.
  5802. */
  5803. sigset_t set;
  5804. struct {
  5805. sigset_t *set;
  5806. size_t size;
  5807. } sig, *sig_ptr;
  5808. abi_ulong arg_sigset, arg_sigsize, *arg7;
  5809. target_sigset_t *target_sigset;
  5810. n = arg1;
  5811. rfd_addr = arg2;
  5812. wfd_addr = arg3;
  5813. efd_addr = arg4;
  5814. ts_addr = arg5;
  5815. ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
  5816. if (ret) {
  5817. goto fail;
  5818. }
  5819. ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
  5820. if (ret) {
  5821. goto fail;
  5822. }
  5823. ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
  5824. if (ret) {
  5825. goto fail;
  5826. }
  5827. /*
  5828. * This takes a timespec, and not a timeval, so we cannot
  5829. * use the do_select() helper ...
  5830. */
  5831. if (ts_addr) {
  5832. if (target_to_host_timespec(&ts, ts_addr)) {
  5833. goto efault;
  5834. }
  5835. ts_ptr = &ts;
  5836. } else {
  5837. ts_ptr = NULL;
  5838. }
  5839. /* Extract the two packed args for the sigset */
  5840. if (arg6) {
  5841. sig_ptr = &sig;
  5842. sig.size = _NSIG / 8;
  5843. arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
  5844. if (!arg7) {
  5845. goto efault;
  5846. }
  5847. arg_sigset = tswapal(arg7[0]);
  5848. arg_sigsize = tswapal(arg7[1]);
  5849. unlock_user(arg7, arg6, 0);
  5850. if (arg_sigset) {
  5851. sig.set = &set;
  5852. if (arg_sigsize != sizeof(*target_sigset)) {
  5853. /* Like the kernel, we enforce correct size sigsets */
  5854. ret = -TARGET_EINVAL;
  5855. goto fail;
  5856. }
  5857. target_sigset = lock_user(VERIFY_READ, arg_sigset,
  5858. sizeof(*target_sigset), 1);
  5859. if (!target_sigset) {
  5860. goto efault;
  5861. }
  5862. target_to_host_sigset(&set, target_sigset);
  5863. unlock_user(target_sigset, arg_sigset, 0);
  5864. } else {
  5865. sig.set = NULL;
  5866. }
  5867. } else {
  5868. sig_ptr = NULL;
  5869. }
  5870. ret = get_errno(sys_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
  5871. ts_ptr, sig_ptr));
  5872. if (!is_error(ret)) {
  5873. if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
  5874. goto efault;
  5875. if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
  5876. goto efault;
  5877. if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
  5878. goto efault;
  5879. if (ts_addr && host_to_target_timespec(ts_addr, &ts))
  5880. goto efault;
  5881. }
  5882. }
  5883. break;
  5884. #endif
  5885. case TARGET_NR_symlink:
  5886. {
  5887. void *p2;
  5888. p = lock_user_string(arg1);
  5889. p2 = lock_user_string(arg2);
  5890. if (!p || !p2)
  5891. ret = -TARGET_EFAULT;
  5892. else
  5893. ret = get_errno(symlink(p, p2));
  5894. unlock_user(p2, arg2, 0);
  5895. unlock_user(p, arg1, 0);
  5896. }
  5897. break;
  5898. #if defined(TARGET_NR_symlinkat)
  5899. case TARGET_NR_symlinkat:
  5900. {
  5901. void *p2;
  5902. p = lock_user_string(arg1);
  5903. p2 = lock_user_string(arg3);
  5904. if (!p || !p2)
  5905. ret = -TARGET_EFAULT;
  5906. else
  5907. ret = get_errno(symlinkat(p, arg2, p2));
  5908. unlock_user(p2, arg3, 0);
  5909. unlock_user(p, arg1, 0);
  5910. }
  5911. break;
  5912. #endif
  5913. #ifdef TARGET_NR_oldlstat
  5914. case TARGET_NR_oldlstat:
  5915. goto unimplemented;
  5916. #endif
  5917. case TARGET_NR_readlink:
  5918. {
  5919. void *p2;
  5920. p = lock_user_string(arg1);
  5921. p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  5922. if (!p || !p2) {
  5923. ret = -TARGET_EFAULT;
  5924. } else if (is_proc_myself((const char *)p, "exe")) {
  5925. char real[PATH_MAX], *temp;
  5926. temp = realpath(exec_path, real);
  5927. ret = temp == NULL ? get_errno(-1) : strlen(real) ;
  5928. snprintf((char *)p2, arg3, "%s", real);
  5929. } else {
  5930. ret = get_errno(readlink(path(p), p2, arg3));
  5931. }
  5932. unlock_user(p2, arg2, ret);
  5933. unlock_user(p, arg1, 0);
  5934. }
  5935. break;
  5936. #if defined(TARGET_NR_readlinkat)
  5937. case TARGET_NR_readlinkat:
  5938. {
  5939. void *p2;
  5940. p = lock_user_string(arg2);
  5941. p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  5942. if (!p || !p2) {
  5943. ret = -TARGET_EFAULT;
  5944. } else if (is_proc_myself((const char *)p, "exe")) {
  5945. char real[PATH_MAX], *temp;
  5946. temp = realpath(exec_path, real);
  5947. ret = temp == NULL ? get_errno(-1) : strlen(real) ;
  5948. snprintf((char *)p2, arg4, "%s", real);
  5949. } else {
  5950. ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
  5951. }
  5952. unlock_user(p2, arg3, ret);
  5953. unlock_user(p, arg2, 0);
  5954. }
  5955. break;
  5956. #endif
  5957. #ifdef TARGET_NR_uselib
  5958. case TARGET_NR_uselib:
  5959. goto unimplemented;
  5960. #endif
  5961. #ifdef TARGET_NR_swapon
  5962. case TARGET_NR_swapon:
  5963. if (!(p = lock_user_string(arg1)))
  5964. goto efault;
  5965. ret = get_errno(swapon(p, arg2));
  5966. unlock_user(p, arg1, 0);
  5967. break;
  5968. #endif
  5969. case TARGET_NR_reboot:
  5970. if (arg3 == LINUX_REBOOT_CMD_RESTART2) {
  5971. /* arg4 must be ignored in all other cases */
  5972. p = lock_user_string(arg4);
  5973. if (!p) {
  5974. goto efault;
  5975. }
  5976. ret = get_errno(reboot(arg1, arg2, arg3, p));
  5977. unlock_user(p, arg4, 0);
  5978. } else {
  5979. ret = get_errno(reboot(arg1, arg2, arg3, NULL));
  5980. }
  5981. break;
  5982. #ifdef TARGET_NR_readdir
  5983. case TARGET_NR_readdir:
  5984. goto unimplemented;
  5985. #endif
  5986. #ifdef TARGET_NR_mmap
  5987. case TARGET_NR_mmap:
  5988. #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
  5989. (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
  5990. defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
  5991. || defined(TARGET_S390X)
  5992. {
  5993. abi_ulong *v;
  5994. abi_ulong v1, v2, v3, v4, v5, v6;
  5995. if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
  5996. goto efault;
  5997. v1 = tswapal(v[0]);
  5998. v2 = tswapal(v[1]);
  5999. v3 = tswapal(v[2]);
  6000. v4 = tswapal(v[3]);
  6001. v5 = tswapal(v[4]);
  6002. v6 = tswapal(v[5]);
  6003. unlock_user(v, arg1, 0);
  6004. ret = get_errno(target_mmap(v1, v2, v3,
  6005. target_to_host_bitmask(v4, mmap_flags_tbl),
  6006. v5, v6));
  6007. }
  6008. #else
  6009. ret = get_errno(target_mmap(arg1, arg2, arg3,
  6010. target_to_host_bitmask(arg4, mmap_flags_tbl),
  6011. arg5,
  6012. arg6));
  6013. #endif
  6014. break;
  6015. #endif
  6016. #ifdef TARGET_NR_mmap2
  6017. case TARGET_NR_mmap2:
  6018. #ifndef MMAP_SHIFT
  6019. #define MMAP_SHIFT 12
  6020. #endif
  6021. ret = get_errno(target_mmap(arg1, arg2, arg3,
  6022. target_to_host_bitmask(arg4, mmap_flags_tbl),
  6023. arg5,
  6024. arg6 << MMAP_SHIFT));
  6025. break;
  6026. #endif
  6027. case TARGET_NR_munmap:
  6028. ret = get_errno(target_munmap(arg1, arg2));
  6029. break;
  6030. case TARGET_NR_mprotect:
  6031. {
  6032. TaskState *ts = cpu->opaque;
  6033. /* Special hack to detect libc making the stack executable. */
  6034. if ((arg3 & PROT_GROWSDOWN)
  6035. && arg1 >= ts->info->stack_limit
  6036. && arg1 <= ts->info->start_stack) {
  6037. arg3 &= ~PROT_GROWSDOWN;
  6038. arg2 = arg2 + arg1 - ts->info->stack_limit;
  6039. arg1 = ts->info->stack_limit;
  6040. }
  6041. }
  6042. ret = get_errno(target_mprotect(arg1, arg2, arg3));
  6043. break;
  6044. #ifdef TARGET_NR_mremap
  6045. case TARGET_NR_mremap:
  6046. ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
  6047. break;
  6048. #endif
  6049. /* ??? msync/mlock/munlock are broken for softmmu. */
  6050. #ifdef TARGET_NR_msync
  6051. case TARGET_NR_msync:
  6052. ret = get_errno(msync(g2h(arg1), arg2, arg3));
  6053. break;
  6054. #endif
  6055. #ifdef TARGET_NR_mlock
  6056. case TARGET_NR_mlock:
  6057. ret = get_errno(mlock(g2h(arg1), arg2));
  6058. break;
  6059. #endif
  6060. #ifdef TARGET_NR_munlock
  6061. case TARGET_NR_munlock:
  6062. ret = get_errno(munlock(g2h(arg1), arg2));
  6063. break;
  6064. #endif
  6065. #ifdef TARGET_NR_mlockall
  6066. case TARGET_NR_mlockall:
  6067. ret = get_errno(mlockall(arg1));
  6068. break;
  6069. #endif
  6070. #ifdef TARGET_NR_munlockall
  6071. case TARGET_NR_munlockall:
  6072. ret = get_errno(munlockall());
  6073. break;
  6074. #endif
  6075. case TARGET_NR_truncate:
  6076. if (!(p = lock_user_string(arg1)))
  6077. goto efault;
  6078. ret = get_errno(truncate(p, arg2));
  6079. unlock_user(p, arg1, 0);
  6080. break;
  6081. case TARGET_NR_ftruncate:
  6082. ret = get_errno(ftruncate(arg1, arg2));
  6083. break;
  6084. case TARGET_NR_fchmod:
  6085. ret = get_errno(fchmod(arg1, arg2));
  6086. break;
  6087. #if defined(TARGET_NR_fchmodat)
  6088. case TARGET_NR_fchmodat:
  6089. if (!(p = lock_user_string(arg2)))
  6090. goto efault;
  6091. ret = get_errno(fchmodat(arg1, p, arg3, 0));
  6092. unlock_user(p, arg2, 0);
  6093. break;
  6094. #endif
  6095. case TARGET_NR_getpriority:
  6096. /* Note that negative values are valid for getpriority, so we must
  6097. differentiate based on errno settings. */
  6098. errno = 0;
  6099. ret = getpriority(arg1, arg2);
  6100. if (ret == -1 && errno != 0) {
  6101. ret = -host_to_target_errno(errno);
  6102. break;
  6103. }
  6104. #ifdef TARGET_ALPHA
  6105. /* Return value is the unbiased priority. Signal no error. */
  6106. ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0;
  6107. #else
  6108. /* Return value is a biased priority to avoid negative numbers. */
  6109. ret = 20 - ret;
  6110. #endif
  6111. break;
  6112. case TARGET_NR_setpriority:
  6113. ret = get_errno(setpriority(arg1, arg2, arg3));
  6114. break;
  6115. #ifdef TARGET_NR_profil
  6116. case TARGET_NR_profil:
  6117. goto unimplemented;
  6118. #endif
  6119. case TARGET_NR_statfs:
  6120. if (!(p = lock_user_string(arg1)))
  6121. goto efault;
  6122. ret = get_errno(statfs(path(p), &stfs));
  6123. unlock_user(p, arg1, 0);
  6124. convert_statfs:
  6125. if (!is_error(ret)) {
  6126. struct target_statfs *target_stfs;
  6127. if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
  6128. goto efault;
  6129. __put_user(stfs.f_type, &target_stfs->f_type);
  6130. __put_user(stfs.f_bsize, &target_stfs->f_bsize);
  6131. __put_user(stfs.f_blocks, &target_stfs->f_blocks);
  6132. __put_user(stfs.f_bfree, &target_stfs->f_bfree);
  6133. __put_user(stfs.f_bavail, &target_stfs->f_bavail);
  6134. __put_user(stfs.f_files, &target_stfs->f_files);
  6135. __put_user(stfs.f_ffree, &target_stfs->f_ffree);
  6136. __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
  6137. __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
  6138. __put_user(stfs.f_namelen, &target_stfs->f_namelen);
  6139. __put_user(stfs.f_frsize, &target_stfs->f_frsize);
  6140. memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
  6141. unlock_user_struct(target_stfs, arg2, 1);
  6142. }
  6143. break;
  6144. case TARGET_NR_fstatfs:
  6145. ret = get_errno(fstatfs(arg1, &stfs));
  6146. goto convert_statfs;
  6147. #ifdef TARGET_NR_statfs64
  6148. case TARGET_NR_statfs64:
  6149. if (!(p = lock_user_string(arg1)))
  6150. goto efault;
  6151. ret = get_errno(statfs(path(p), &stfs));
  6152. unlock_user(p, arg1, 0);
  6153. convert_statfs64:
  6154. if (!is_error(ret)) {
  6155. struct target_statfs64 *target_stfs;
  6156. if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
  6157. goto efault;
  6158. __put_user(stfs.f_type, &target_stfs->f_type);
  6159. __put_user(stfs.f_bsize, &target_stfs->f_bsize);
  6160. __put_user(stfs.f_blocks, &target_stfs->f_blocks);
  6161. __put_user(stfs.f_bfree, &target_stfs->f_bfree);
  6162. __put_user(stfs.f_bavail, &target_stfs->f_bavail);
  6163. __put_user(stfs.f_files, &target_stfs->f_files);
  6164. __put_user(stfs.f_ffree, &target_stfs->f_ffree);
  6165. __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
  6166. __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
  6167. __put_user(stfs.f_namelen, &target_stfs->f_namelen);
  6168. __put_user(stfs.f_frsize, &target_stfs->f_frsize);
  6169. memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
  6170. unlock_user_struct(target_stfs, arg3, 1);
  6171. }
  6172. break;
  6173. case TARGET_NR_fstatfs64:
  6174. ret = get_errno(fstatfs(arg1, &stfs));
  6175. goto convert_statfs64;
  6176. #endif
  6177. #ifdef TARGET_NR_ioperm
  6178. case TARGET_NR_ioperm:
  6179. goto unimplemented;
  6180. #endif
  6181. #ifdef TARGET_NR_socketcall
  6182. case TARGET_NR_socketcall:
  6183. ret = do_socketcall(arg1, arg2);
  6184. break;
  6185. #endif
  6186. #ifdef TARGET_NR_accept
  6187. case TARGET_NR_accept:
  6188. ret = do_accept4(arg1, arg2, arg3, 0);
  6189. break;
  6190. #endif
  6191. #ifdef TARGET_NR_accept4
  6192. case TARGET_NR_accept4:
  6193. #ifdef CONFIG_ACCEPT4
  6194. ret = do_accept4(arg1, arg2, arg3, arg4);
  6195. #else
  6196. goto unimplemented;
  6197. #endif
  6198. break;
  6199. #endif
  6200. #ifdef TARGET_NR_bind
  6201. case TARGET_NR_bind:
  6202. ret = do_bind(arg1, arg2, arg3);
  6203. break;
  6204. #endif
  6205. #ifdef TARGET_NR_connect
  6206. case TARGET_NR_connect:
  6207. ret = do_connect(arg1, arg2, arg3);
  6208. break;
  6209. #endif
  6210. #ifdef TARGET_NR_getpeername
  6211. case TARGET_NR_getpeername:
  6212. ret = do_getpeername(arg1, arg2, arg3);
  6213. break;
  6214. #endif
  6215. #ifdef TARGET_NR_getsockname
  6216. case TARGET_NR_getsockname:
  6217. ret = do_getsockname(arg1, arg2, arg3);
  6218. break;
  6219. #endif
  6220. #ifdef TARGET_NR_getsockopt
  6221. case TARGET_NR_getsockopt:
  6222. ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
  6223. break;
  6224. #endif
  6225. #ifdef TARGET_NR_listen
  6226. case TARGET_NR_listen:
  6227. ret = get_errno(listen(arg1, arg2));
  6228. break;
  6229. #endif
  6230. #ifdef TARGET_NR_recv
  6231. case TARGET_NR_recv:
  6232. ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
  6233. break;
  6234. #endif
  6235. #ifdef TARGET_NR_recvfrom
  6236. case TARGET_NR_recvfrom:
  6237. ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
  6238. break;
  6239. #endif
  6240. #ifdef TARGET_NR_recvmsg
  6241. case TARGET_NR_recvmsg:
  6242. ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
  6243. break;
  6244. #endif
  6245. #ifdef TARGET_NR_send
  6246. case TARGET_NR_send:
  6247. ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
  6248. break;
  6249. #endif
  6250. #ifdef TARGET_NR_sendmsg
  6251. case TARGET_NR_sendmsg:
  6252. ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
  6253. break;
  6254. #endif
  6255. #ifdef TARGET_NR_sendmmsg
  6256. case TARGET_NR_sendmmsg:
  6257. ret = do_sendrecvmmsg(arg1, arg2, arg3, arg4, 1);
  6258. break;
  6259. case TARGET_NR_recvmmsg:
  6260. ret = do_sendrecvmmsg(arg1, arg2, arg3, arg4, 0);
  6261. break;
  6262. #endif
  6263. #ifdef TARGET_NR_sendto
  6264. case TARGET_NR_sendto:
  6265. ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
  6266. break;
  6267. #endif
  6268. #ifdef TARGET_NR_shutdown
  6269. case TARGET_NR_shutdown:
  6270. ret = get_errno(shutdown(arg1, arg2));
  6271. break;
  6272. #endif
  6273. #ifdef TARGET_NR_socket
  6274. case TARGET_NR_socket:
  6275. ret = do_socket(arg1, arg2, arg3);
  6276. break;
  6277. #endif
  6278. #ifdef TARGET_NR_socketpair
  6279. case TARGET_NR_socketpair:
  6280. ret = do_socketpair(arg1, arg2, arg3, arg4);
  6281. break;
  6282. #endif
  6283. #ifdef TARGET_NR_setsockopt
  6284. case TARGET_NR_setsockopt:
  6285. ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
  6286. break;
  6287. #endif
  6288. case TARGET_NR_syslog:
  6289. if (!(p = lock_user_string(arg2)))
  6290. goto efault;
  6291. ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
  6292. unlock_user(p, arg2, 0);
  6293. break;
  6294. case TARGET_NR_setitimer:
  6295. {
  6296. struct itimerval value, ovalue, *pvalue;
  6297. if (arg2) {
  6298. pvalue = &value;
  6299. if (copy_from_user_timeval(&pvalue->it_interval, arg2)
  6300. || copy_from_user_timeval(&pvalue->it_value,
  6301. arg2 + sizeof(struct target_timeval)))
  6302. goto efault;
  6303. } else {
  6304. pvalue = NULL;
  6305. }
  6306. ret = get_errno(setitimer(arg1, pvalue, &ovalue));
  6307. if (!is_error(ret) && arg3) {
  6308. if (copy_to_user_timeval(arg3,
  6309. &ovalue.it_interval)
  6310. || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
  6311. &ovalue.it_value))
  6312. goto efault;
  6313. }
  6314. }
  6315. break;
  6316. case TARGET_NR_getitimer:
  6317. {
  6318. struct itimerval value;
  6319. ret = get_errno(getitimer(arg1, &value));
  6320. if (!is_error(ret) && arg2) {
  6321. if (copy_to_user_timeval(arg2,
  6322. &value.it_interval)
  6323. || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
  6324. &value.it_value))
  6325. goto efault;
  6326. }
  6327. }
  6328. break;
  6329. case TARGET_NR_stat:
  6330. if (!(p = lock_user_string(arg1)))
  6331. goto efault;
  6332. ret = get_errno(stat(path(p), &st));
  6333. unlock_user(p, arg1, 0);
  6334. goto do_stat;
  6335. case TARGET_NR_lstat:
  6336. if (!(p = lock_user_string(arg1)))
  6337. goto efault;
  6338. ret = get_errno(lstat(path(p), &st));
  6339. unlock_user(p, arg1, 0);
  6340. goto do_stat;
  6341. case TARGET_NR_fstat:
  6342. {
  6343. ret = get_errno(fstat(arg1, &st));
  6344. do_stat:
  6345. if (!is_error(ret)) {
  6346. struct target_stat *target_st;
  6347. if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
  6348. goto efault;
  6349. memset(target_st, 0, sizeof(*target_st));
  6350. __put_user(st.st_dev, &target_st->st_dev);
  6351. __put_user(st.st_ino, &target_st->st_ino);
  6352. __put_user(st.st_mode, &target_st->st_mode);
  6353. __put_user(st.st_uid, &target_st->st_uid);
  6354. __put_user(st.st_gid, &target_st->st_gid);
  6355. __put_user(st.st_nlink, &target_st->st_nlink);
  6356. __put_user(st.st_rdev, &target_st->st_rdev);
  6357. __put_user(st.st_size, &target_st->st_size);
  6358. __put_user(st.st_blksize, &target_st->st_blksize);
  6359. __put_user(st.st_blocks, &target_st->st_blocks);
  6360. __put_user(st.st_atime, &target_st->target_st_atime);
  6361. __put_user(st.st_mtime, &target_st->target_st_mtime);
  6362. __put_user(st.st_ctime, &target_st->target_st_ctime);
  6363. unlock_user_struct(target_st, arg2, 1);
  6364. }
  6365. }
  6366. break;
  6367. #ifdef TARGET_NR_olduname
  6368. case TARGET_NR_olduname:
  6369. goto unimplemented;
  6370. #endif
  6371. #ifdef TARGET_NR_iopl
  6372. case TARGET_NR_iopl:
  6373. goto unimplemented;
  6374. #endif
  6375. case TARGET_NR_vhangup:
  6376. ret = get_errno(vhangup());
  6377. break;
  6378. #ifdef TARGET_NR_idle
  6379. case TARGET_NR_idle:
  6380. goto unimplemented;
  6381. #endif
  6382. #ifdef TARGET_NR_syscall
  6383. case TARGET_NR_syscall:
  6384. ret = do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
  6385. arg6, arg7, arg8, 0);
  6386. break;
  6387. #endif
  6388. case TARGET_NR_wait4:
  6389. {
  6390. int status;
  6391. abi_long status_ptr = arg2;
  6392. struct rusage rusage, *rusage_ptr;
  6393. abi_ulong target_rusage = arg4;
  6394. if (target_rusage)
  6395. rusage_ptr = &rusage;
  6396. else
  6397. rusage_ptr = NULL;
  6398. ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
  6399. if (!is_error(ret)) {
  6400. if (status_ptr && ret) {
  6401. status = host_to_target_waitstatus(status);
  6402. if (put_user_s32(status, status_ptr))
  6403. goto efault;
  6404. }
  6405. if (target_rusage)
  6406. host_to_target_rusage(target_rusage, &rusage);
  6407. }
  6408. }
  6409. break;
  6410. #ifdef TARGET_NR_swapoff
  6411. case TARGET_NR_swapoff:
  6412. if (!(p = lock_user_string(arg1)))
  6413. goto efault;
  6414. ret = get_errno(swapoff(p));
  6415. unlock_user(p, arg1, 0);
  6416. break;
  6417. #endif
  6418. case TARGET_NR_sysinfo:
  6419. {
  6420. struct target_sysinfo *target_value;
  6421. struct sysinfo value;
  6422. ret = get_errno(sysinfo(&value));
  6423. if (!is_error(ret) && arg1)
  6424. {
  6425. if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
  6426. goto efault;
  6427. __put_user(value.uptime, &target_value->uptime);
  6428. __put_user(value.loads[0], &target_value->loads[0]);
  6429. __put_user(value.loads[1], &target_value->loads[1]);
  6430. __put_user(value.loads[2], &target_value->loads[2]);
  6431. __put_user(value.totalram, &target_value->totalram);
  6432. __put_user(value.freeram, &target_value->freeram);
  6433. __put_user(value.sharedram, &target_value->sharedram);
  6434. __put_user(value.bufferram, &target_value->bufferram);
  6435. __put_user(value.totalswap, &target_value->totalswap);
  6436. __put_user(value.freeswap, &target_value->freeswap);
  6437. __put_user(value.procs, &target_value->procs);
  6438. __put_user(value.totalhigh, &target_value->totalhigh);
  6439. __put_user(value.freehigh, &target_value->freehigh);
  6440. __put_user(value.mem_unit, &target_value->mem_unit);
  6441. unlock_user_struct(target_value, arg1, 1);
  6442. }
  6443. }
  6444. break;
  6445. #ifdef TARGET_NR_ipc
  6446. case TARGET_NR_ipc:
  6447. ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
  6448. break;
  6449. #endif
  6450. #ifdef TARGET_NR_semget
  6451. case TARGET_NR_semget:
  6452. ret = get_errno(semget(arg1, arg2, arg3));
  6453. break;
  6454. #endif
  6455. #ifdef TARGET_NR_semop
  6456. case TARGET_NR_semop:
  6457. ret = do_semop(arg1, arg2, arg3);
  6458. break;
  6459. #endif
  6460. #ifdef TARGET_NR_semctl
  6461. case TARGET_NR_semctl:
  6462. ret = do_semctl(arg1, arg2, arg3, (union target_semun)(abi_ulong)arg4);
  6463. break;
  6464. #endif
  6465. #ifdef TARGET_NR_msgctl
  6466. case TARGET_NR_msgctl:
  6467. ret = do_msgctl(arg1, arg2, arg3);
  6468. break;
  6469. #endif
  6470. #ifdef TARGET_NR_msgget
  6471. case TARGET_NR_msgget:
  6472. ret = get_errno(msgget(arg1, arg2));
  6473. break;
  6474. #endif
  6475. #ifdef TARGET_NR_msgrcv
  6476. case TARGET_NR_msgrcv:
  6477. ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
  6478. break;
  6479. #endif
  6480. #ifdef TARGET_NR_msgsnd
  6481. case TARGET_NR_msgsnd:
  6482. ret = do_msgsnd(arg1, arg2, arg3, arg4);
  6483. break;
  6484. #endif
  6485. #ifdef TARGET_NR_shmget
  6486. case TARGET_NR_shmget:
  6487. ret = get_errno(shmget(arg1, arg2, arg3));
  6488. break;
  6489. #endif
  6490. #ifdef TARGET_NR_shmctl
  6491. case TARGET_NR_shmctl:
  6492. ret = do_shmctl(arg1, arg2, arg3);
  6493. break;
  6494. #endif
  6495. #ifdef TARGET_NR_shmat
  6496. case TARGET_NR_shmat:
  6497. ret = do_shmat(arg1, arg2, arg3);
  6498. break;
  6499. #endif
  6500. #ifdef TARGET_NR_shmdt
  6501. case TARGET_NR_shmdt:
  6502. ret = do_shmdt(arg1);
  6503. break;
  6504. #endif
  6505. case TARGET_NR_fsync:
  6506. ret = get_errno(fsync(arg1));
  6507. break;
  6508. case TARGET_NR_clone:
  6509. /* Linux manages to have three different orderings for its
  6510. * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
  6511. * match the kernel's CONFIG_CLONE_* settings.
  6512. * Microblaze is further special in that it uses a sixth
  6513. * implicit argument to clone for the TLS pointer.
  6514. */
  6515. #if defined(TARGET_MICROBLAZE)
  6516. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5));
  6517. #elif defined(TARGET_CLONE_BACKWARDS)
  6518. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
  6519. #elif defined(TARGET_CLONE_BACKWARDS2)
  6520. ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
  6521. #else
  6522. ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
  6523. #endif
  6524. break;
  6525. #ifdef __NR_exit_group
  6526. /* new thread calls */
  6527. case TARGET_NR_exit_group:
  6528. #ifdef TARGET_GPROF
  6529. _mcleanup();
  6530. #endif
  6531. gdb_exit(cpu_env, arg1);
  6532. ret = get_errno(exit_group(arg1));
  6533. break;
  6534. #endif
  6535. case TARGET_NR_setdomainname:
  6536. if (!(p = lock_user_string(arg1)))
  6537. goto efault;
  6538. ret = get_errno(setdomainname(p, arg2));
  6539. unlock_user(p, arg1, 0);
  6540. break;
  6541. case TARGET_NR_uname:
  6542. /* no need to transcode because we use the linux syscall */
  6543. {
  6544. struct new_utsname * buf;
  6545. if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
  6546. goto efault;
  6547. ret = get_errno(sys_uname(buf));
  6548. if (!is_error(ret)) {
  6549. /* Overrite the native machine name with whatever is being
  6550. emulated. */
  6551. strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
  6552. /* Allow the user to override the reported release. */
  6553. if (qemu_uname_release && *qemu_uname_release)
  6554. strcpy (buf->release, qemu_uname_release);
  6555. }
  6556. unlock_user_struct(buf, arg1, 1);
  6557. }
  6558. break;
  6559. #ifdef TARGET_I386
  6560. case TARGET_NR_modify_ldt:
  6561. ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
  6562. break;
  6563. #if !defined(TARGET_X86_64)
  6564. case TARGET_NR_vm86old:
  6565. goto unimplemented;
  6566. case TARGET_NR_vm86:
  6567. ret = do_vm86(cpu_env, arg1, arg2);
  6568. break;
  6569. #endif
  6570. #endif
  6571. case TARGET_NR_adjtimex:
  6572. goto unimplemented;
  6573. #ifdef TARGET_NR_create_module
  6574. case TARGET_NR_create_module:
  6575. #endif
  6576. case TARGET_NR_init_module:
  6577. case TARGET_NR_delete_module:
  6578. #ifdef TARGET_NR_get_kernel_syms
  6579. case TARGET_NR_get_kernel_syms:
  6580. #endif
  6581. goto unimplemented;
  6582. case TARGET_NR_quotactl:
  6583. goto unimplemented;
  6584. case TARGET_NR_getpgid:
  6585. ret = get_errno(getpgid(arg1));
  6586. break;
  6587. case TARGET_NR_fchdir:
  6588. ret = get_errno(fchdir(arg1));
  6589. break;
  6590. #ifdef TARGET_NR_bdflush /* not on x86_64 */
  6591. case TARGET_NR_bdflush:
  6592. goto unimplemented;
  6593. #endif
  6594. #ifdef TARGET_NR_sysfs
  6595. case TARGET_NR_sysfs:
  6596. goto unimplemented;
  6597. #endif
  6598. case TARGET_NR_personality:
  6599. ret = get_errno(personality(arg1));
  6600. break;
  6601. #ifdef TARGET_NR_afs_syscall
  6602. case TARGET_NR_afs_syscall:
  6603. goto unimplemented;
  6604. #endif
  6605. #ifdef TARGET_NR__llseek /* Not on alpha */
  6606. case TARGET_NR__llseek:
  6607. {
  6608. int64_t res;
  6609. #if !defined(__NR_llseek)
  6610. res = lseek(arg1, ((uint64_t)arg2 << 32) | arg3, arg5);
  6611. if (res == -1) {
  6612. ret = get_errno(res);
  6613. } else {
  6614. ret = 0;
  6615. }
  6616. #else
  6617. ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
  6618. #endif
  6619. if ((ret == 0) && put_user_s64(res, arg4)) {
  6620. goto efault;
  6621. }
  6622. }
  6623. break;
  6624. #endif
  6625. case TARGET_NR_getdents:
  6626. #ifdef __NR_getdents
  6627. #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
  6628. {
  6629. struct target_dirent *target_dirp;
  6630. struct linux_dirent *dirp;
  6631. abi_long count = arg3;
  6632. dirp = malloc(count);
  6633. if (!dirp) {
  6634. ret = -TARGET_ENOMEM;
  6635. goto fail;
  6636. }
  6637. ret = get_errno(sys_getdents(arg1, dirp, count));
  6638. if (!is_error(ret)) {
  6639. struct linux_dirent *de;
  6640. struct target_dirent *tde;
  6641. int len = ret;
  6642. int reclen, treclen;
  6643. int count1, tnamelen;
  6644. count1 = 0;
  6645. de = dirp;
  6646. if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
  6647. goto efault;
  6648. tde = target_dirp;
  6649. while (len > 0) {
  6650. reclen = de->d_reclen;
  6651. tnamelen = reclen - offsetof(struct linux_dirent, d_name);
  6652. assert(tnamelen >= 0);
  6653. treclen = tnamelen + offsetof(struct target_dirent, d_name);
  6654. assert(count1 + treclen <= count);
  6655. tde->d_reclen = tswap16(treclen);
  6656. tde->d_ino = tswapal(de->d_ino);
  6657. tde->d_off = tswapal(de->d_off);
  6658. memcpy(tde->d_name, de->d_name, tnamelen);
  6659. de = (struct linux_dirent *)((char *)de + reclen);
  6660. len -= reclen;
  6661. tde = (struct target_dirent *)((char *)tde + treclen);
  6662. count1 += treclen;
  6663. }
  6664. ret = count1;
  6665. unlock_user(target_dirp, arg2, ret);
  6666. }
  6667. free(dirp);
  6668. }
  6669. #else
  6670. {
  6671. struct linux_dirent *dirp;
  6672. abi_long count = arg3;
  6673. if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
  6674. goto efault;
  6675. ret = get_errno(sys_getdents(arg1, dirp, count));
  6676. if (!is_error(ret)) {
  6677. struct linux_dirent *de;
  6678. int len = ret;
  6679. int reclen;
  6680. de = dirp;
  6681. while (len > 0) {
  6682. reclen = de->d_reclen;
  6683. if (reclen > len)
  6684. break;
  6685. de->d_reclen = tswap16(reclen);
  6686. tswapls(&de->d_ino);
  6687. tswapls(&de->d_off);
  6688. de = (struct linux_dirent *)((char *)de + reclen);
  6689. len -= reclen;
  6690. }
  6691. }
  6692. unlock_user(dirp, arg2, ret);
  6693. }
  6694. #endif
  6695. #else
  6696. /* Implement getdents in terms of getdents64 */
  6697. {
  6698. struct linux_dirent64 *dirp;
  6699. abi_long count = arg3;
  6700. dirp = lock_user(VERIFY_WRITE, arg2, count, 0);
  6701. if (!dirp) {
  6702. goto efault;
  6703. }
  6704. ret = get_errno(sys_getdents64(arg1, dirp, count));
  6705. if (!is_error(ret)) {
  6706. /* Convert the dirent64 structs to target dirent. We do this
  6707. * in-place, since we can guarantee that a target_dirent is no
  6708. * larger than a dirent64; however this means we have to be
  6709. * careful to read everything before writing in the new format.
  6710. */
  6711. struct linux_dirent64 *de;
  6712. struct target_dirent *tde;
  6713. int len = ret;
  6714. int tlen = 0;
  6715. de = dirp;
  6716. tde = (struct target_dirent *)dirp;
  6717. while (len > 0) {
  6718. int namelen, treclen;
  6719. int reclen = de->d_reclen;
  6720. uint64_t ino = de->d_ino;
  6721. int64_t off = de->d_off;
  6722. uint8_t type = de->d_type;
  6723. namelen = strlen(de->d_name);
  6724. treclen = offsetof(struct target_dirent, d_name)
  6725. + namelen + 2;
  6726. treclen = QEMU_ALIGN_UP(treclen, sizeof(abi_long));
  6727. memmove(tde->d_name, de->d_name, namelen + 1);
  6728. tde->d_ino = tswapal(ino);
  6729. tde->d_off = tswapal(off);
  6730. tde->d_reclen = tswap16(treclen);
  6731. /* The target_dirent type is in what was formerly a padding
  6732. * byte at the end of the structure:
  6733. */
  6734. *(((char *)tde) + treclen - 1) = type;
  6735. de = (struct linux_dirent64 *)((char *)de + reclen);
  6736. tde = (struct target_dirent *)((char *)tde + treclen);
  6737. len -= reclen;
  6738. tlen += treclen;
  6739. }
  6740. ret = tlen;
  6741. }
  6742. unlock_user(dirp, arg2, ret);
  6743. }
  6744. #endif
  6745. break;
  6746. #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
  6747. case TARGET_NR_getdents64:
  6748. {
  6749. struct linux_dirent64 *dirp;
  6750. abi_long count = arg3;
  6751. if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
  6752. goto efault;
  6753. ret = get_errno(sys_getdents64(arg1, dirp, count));
  6754. if (!is_error(ret)) {
  6755. struct linux_dirent64 *de;
  6756. int len = ret;
  6757. int reclen;
  6758. de = dirp;
  6759. while (len > 0) {
  6760. reclen = de->d_reclen;
  6761. if (reclen > len)
  6762. break;
  6763. de->d_reclen = tswap16(reclen);
  6764. tswap64s((uint64_t *)&de->d_ino);
  6765. tswap64s((uint64_t *)&de->d_off);
  6766. de = (struct linux_dirent64 *)((char *)de + reclen);
  6767. len -= reclen;
  6768. }
  6769. }
  6770. unlock_user(dirp, arg2, ret);
  6771. }
  6772. break;
  6773. #endif /* TARGET_NR_getdents64 */
  6774. #if defined(TARGET_NR__newselect)
  6775. case TARGET_NR__newselect:
  6776. ret = do_select(arg1, arg2, arg3, arg4, arg5);
  6777. break;
  6778. #endif
  6779. #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
  6780. # ifdef TARGET_NR_poll
  6781. case TARGET_NR_poll:
  6782. # endif
  6783. # ifdef TARGET_NR_ppoll
  6784. case TARGET_NR_ppoll:
  6785. # endif
  6786. {
  6787. struct target_pollfd *target_pfd;
  6788. unsigned int nfds = arg2;
  6789. int timeout = arg3;
  6790. struct pollfd *pfd;
  6791. unsigned int i;
  6792. target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
  6793. if (!target_pfd)
  6794. goto efault;
  6795. pfd = alloca(sizeof(struct pollfd) * nfds);
  6796. for(i = 0; i < nfds; i++) {
  6797. pfd[i].fd = tswap32(target_pfd[i].fd);
  6798. pfd[i].events = tswap16(target_pfd[i].events);
  6799. }
  6800. # ifdef TARGET_NR_ppoll
  6801. if (num == TARGET_NR_ppoll) {
  6802. struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
  6803. target_sigset_t *target_set;
  6804. sigset_t _set, *set = &_set;
  6805. if (arg3) {
  6806. if (target_to_host_timespec(timeout_ts, arg3)) {
  6807. unlock_user(target_pfd, arg1, 0);
  6808. goto efault;
  6809. }
  6810. } else {
  6811. timeout_ts = NULL;
  6812. }
  6813. if (arg4) {
  6814. target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1);
  6815. if (!target_set) {
  6816. unlock_user(target_pfd, arg1, 0);
  6817. goto efault;
  6818. }
  6819. target_to_host_sigset(set, target_set);
  6820. } else {
  6821. set = NULL;
  6822. }
  6823. ret = get_errno(sys_ppoll(pfd, nfds, timeout_ts, set, _NSIG/8));
  6824. if (!is_error(ret) && arg3) {
  6825. host_to_target_timespec(arg3, timeout_ts);
  6826. }
  6827. if (arg4) {
  6828. unlock_user(target_set, arg4, 0);
  6829. }
  6830. } else
  6831. # endif
  6832. ret = get_errno(poll(pfd, nfds, timeout));
  6833. if (!is_error(ret)) {
  6834. for(i = 0; i < nfds; i++) {
  6835. target_pfd[i].revents = tswap16(pfd[i].revents);
  6836. }
  6837. }
  6838. unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
  6839. }
  6840. break;
  6841. #endif
  6842. case TARGET_NR_flock:
  6843. /* NOTE: the flock constant seems to be the same for every
  6844. Linux platform */
  6845. ret = get_errno(flock(arg1, arg2));
  6846. break;
  6847. case TARGET_NR_readv:
  6848. {
  6849. struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
  6850. if (vec != NULL) {
  6851. ret = get_errno(readv(arg1, vec, arg3));
  6852. unlock_iovec(vec, arg2, arg3, 1);
  6853. } else {
  6854. ret = -host_to_target_errno(errno);
  6855. }
  6856. }
  6857. break;
  6858. case TARGET_NR_writev:
  6859. {
  6860. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  6861. if (vec != NULL) {
  6862. ret = get_errno(writev(arg1, vec, arg3));
  6863. unlock_iovec(vec, arg2, arg3, 0);
  6864. } else {
  6865. ret = -host_to_target_errno(errno);
  6866. }
  6867. }
  6868. break;
  6869. case TARGET_NR_getsid:
  6870. ret = get_errno(getsid(arg1));
  6871. break;
  6872. #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
  6873. case TARGET_NR_fdatasync:
  6874. ret = get_errno(fdatasync(arg1));
  6875. break;
  6876. #endif
  6877. case TARGET_NR__sysctl:
  6878. /* We don't implement this, but ENOTDIR is always a safe
  6879. return value. */
  6880. ret = -TARGET_ENOTDIR;
  6881. break;
  6882. case TARGET_NR_sched_getaffinity:
  6883. {
  6884. unsigned int mask_size;
  6885. unsigned long *mask;
  6886. /*
  6887. * sched_getaffinity needs multiples of ulong, so need to take
  6888. * care of mismatches between target ulong and host ulong sizes.
  6889. */
  6890. if (arg2 & (sizeof(abi_ulong) - 1)) {
  6891. ret = -TARGET_EINVAL;
  6892. break;
  6893. }
  6894. mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
  6895. mask = alloca(mask_size);
  6896. ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
  6897. if (!is_error(ret)) {
  6898. if (ret > arg2) {
  6899. /* More data returned than the caller's buffer will fit.
  6900. * This only happens if sizeof(abi_long) < sizeof(long)
  6901. * and the caller passed us a buffer holding an odd number
  6902. * of abi_longs. If the host kernel is actually using the
  6903. * extra 4 bytes then fail EINVAL; otherwise we can just
  6904. * ignore them and only copy the interesting part.
  6905. */
  6906. int numcpus = sysconf(_SC_NPROCESSORS_CONF);
  6907. if (numcpus > arg2 * 8) {
  6908. ret = -TARGET_EINVAL;
  6909. break;
  6910. }
  6911. ret = arg2;
  6912. }
  6913. if (copy_to_user(arg3, mask, ret)) {
  6914. goto efault;
  6915. }
  6916. }
  6917. }
  6918. break;
  6919. case TARGET_NR_sched_setaffinity:
  6920. {
  6921. unsigned int mask_size;
  6922. unsigned long *mask;
  6923. /*
  6924. * sched_setaffinity needs multiples of ulong, so need to take
  6925. * care of mismatches between target ulong and host ulong sizes.
  6926. */
  6927. if (arg2 & (sizeof(abi_ulong) - 1)) {
  6928. ret = -TARGET_EINVAL;
  6929. break;
  6930. }
  6931. mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
  6932. mask = alloca(mask_size);
  6933. if (!lock_user_struct(VERIFY_READ, p, arg3, 1)) {
  6934. goto efault;
  6935. }
  6936. memcpy(mask, p, arg2);
  6937. unlock_user_struct(p, arg2, 0);
  6938. ret = get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
  6939. }
  6940. break;
  6941. case TARGET_NR_sched_setparam:
  6942. {
  6943. struct sched_param *target_schp;
  6944. struct sched_param schp;
  6945. if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
  6946. goto efault;
  6947. schp.sched_priority = tswap32(target_schp->sched_priority);
  6948. unlock_user_struct(target_schp, arg2, 0);
  6949. ret = get_errno(sched_setparam(arg1, &schp));
  6950. }
  6951. break;
  6952. case TARGET_NR_sched_getparam:
  6953. {
  6954. struct sched_param *target_schp;
  6955. struct sched_param schp;
  6956. ret = get_errno(sched_getparam(arg1, &schp));
  6957. if (!is_error(ret)) {
  6958. if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
  6959. goto efault;
  6960. target_schp->sched_priority = tswap32(schp.sched_priority);
  6961. unlock_user_struct(target_schp, arg2, 1);
  6962. }
  6963. }
  6964. break;
  6965. case TARGET_NR_sched_setscheduler:
  6966. {
  6967. struct sched_param *target_schp;
  6968. struct sched_param schp;
  6969. if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
  6970. goto efault;
  6971. schp.sched_priority = tswap32(target_schp->sched_priority);
  6972. unlock_user_struct(target_schp, arg3, 0);
  6973. ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
  6974. }
  6975. break;
  6976. case TARGET_NR_sched_getscheduler:
  6977. ret = get_errno(sched_getscheduler(arg1));
  6978. break;
  6979. case TARGET_NR_sched_yield:
  6980. ret = get_errno(sched_yield());
  6981. break;
  6982. case TARGET_NR_sched_get_priority_max:
  6983. ret = get_errno(sched_get_priority_max(arg1));
  6984. break;
  6985. case TARGET_NR_sched_get_priority_min:
  6986. ret = get_errno(sched_get_priority_min(arg1));
  6987. break;
  6988. case TARGET_NR_sched_rr_get_interval:
  6989. {
  6990. struct timespec ts;
  6991. ret = get_errno(sched_rr_get_interval(arg1, &ts));
  6992. if (!is_error(ret)) {
  6993. host_to_target_timespec(arg2, &ts);
  6994. }
  6995. }
  6996. break;
  6997. case TARGET_NR_nanosleep:
  6998. {
  6999. struct timespec req, rem;
  7000. target_to_host_timespec(&req, arg1);
  7001. ret = get_errno(nanosleep(&req, &rem));
  7002. if (is_error(ret) && arg2) {
  7003. host_to_target_timespec(arg2, &rem);
  7004. }
  7005. }
  7006. break;
  7007. #ifdef TARGET_NR_query_module
  7008. case TARGET_NR_query_module:
  7009. goto unimplemented;
  7010. #endif
  7011. #ifdef TARGET_NR_nfsservctl
  7012. case TARGET_NR_nfsservctl:
  7013. goto unimplemented;
  7014. #endif
  7015. case TARGET_NR_prctl:
  7016. switch (arg1) {
  7017. case PR_GET_PDEATHSIG:
  7018. {
  7019. int deathsig;
  7020. ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
  7021. if (!is_error(ret) && arg2
  7022. && put_user_ual(deathsig, arg2)) {
  7023. goto efault;
  7024. }
  7025. break;
  7026. }
  7027. #ifdef PR_GET_NAME
  7028. case PR_GET_NAME:
  7029. {
  7030. void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
  7031. if (!name) {
  7032. goto efault;
  7033. }
  7034. ret = get_errno(prctl(arg1, (unsigned long)name,
  7035. arg3, arg4, arg5));
  7036. unlock_user(name, arg2, 16);
  7037. break;
  7038. }
  7039. case PR_SET_NAME:
  7040. {
  7041. void *name = lock_user(VERIFY_READ, arg2, 16, 1);
  7042. if (!name) {
  7043. goto efault;
  7044. }
  7045. ret = get_errno(prctl(arg1, (unsigned long)name,
  7046. arg3, arg4, arg5));
  7047. unlock_user(name, arg2, 0);
  7048. break;
  7049. }
  7050. #endif
  7051. default:
  7052. /* Most prctl options have no pointer arguments */
  7053. ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
  7054. break;
  7055. }
  7056. break;
  7057. #ifdef TARGET_NR_arch_prctl
  7058. case TARGET_NR_arch_prctl:
  7059. #if defined(TARGET_I386) && !defined(TARGET_ABI32)
  7060. ret = do_arch_prctl(cpu_env, arg1, arg2);
  7061. break;
  7062. #else
  7063. goto unimplemented;
  7064. #endif
  7065. #endif
  7066. #ifdef TARGET_NR_pread64
  7067. case TARGET_NR_pread64:
  7068. if (regpairs_aligned(cpu_env)) {
  7069. arg4 = arg5;
  7070. arg5 = arg6;
  7071. }
  7072. if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
  7073. goto efault;
  7074. ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
  7075. unlock_user(p, arg2, ret);
  7076. break;
  7077. case TARGET_NR_pwrite64:
  7078. if (regpairs_aligned(cpu_env)) {
  7079. arg4 = arg5;
  7080. arg5 = arg6;
  7081. }
  7082. if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
  7083. goto efault;
  7084. ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
  7085. unlock_user(p, arg2, 0);
  7086. break;
  7087. #endif
  7088. case TARGET_NR_getcwd:
  7089. if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
  7090. goto efault;
  7091. ret = get_errno(sys_getcwd1(p, arg2));
  7092. unlock_user(p, arg1, ret);
  7093. break;
  7094. case TARGET_NR_capget:
  7095. case TARGET_NR_capset:
  7096. {
  7097. struct target_user_cap_header *target_header;
  7098. struct target_user_cap_data *target_data = NULL;
  7099. struct __user_cap_header_struct header;
  7100. struct __user_cap_data_struct data[2];
  7101. struct __user_cap_data_struct *dataptr = NULL;
  7102. int i, target_datalen;
  7103. int data_items = 1;
  7104. if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) {
  7105. goto efault;
  7106. }
  7107. header.version = tswap32(target_header->version);
  7108. header.pid = tswap32(target_header->pid);
  7109. if (header.version != _LINUX_CAPABILITY_VERSION) {
  7110. /* Version 2 and up takes pointer to two user_data structs */
  7111. data_items = 2;
  7112. }
  7113. target_datalen = sizeof(*target_data) * data_items;
  7114. if (arg2) {
  7115. if (num == TARGET_NR_capget) {
  7116. target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0);
  7117. } else {
  7118. target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1);
  7119. }
  7120. if (!target_data) {
  7121. unlock_user_struct(target_header, arg1, 0);
  7122. goto efault;
  7123. }
  7124. if (num == TARGET_NR_capset) {
  7125. for (i = 0; i < data_items; i++) {
  7126. data[i].effective = tswap32(target_data[i].effective);
  7127. data[i].permitted = tswap32(target_data[i].permitted);
  7128. data[i].inheritable = tswap32(target_data[i].inheritable);
  7129. }
  7130. }
  7131. dataptr = data;
  7132. }
  7133. if (num == TARGET_NR_capget) {
  7134. ret = get_errno(capget(&header, dataptr));
  7135. } else {
  7136. ret = get_errno(capset(&header, dataptr));
  7137. }
  7138. /* The kernel always updates version for both capget and capset */
  7139. target_header->version = tswap32(header.version);
  7140. unlock_user_struct(target_header, arg1, 1);
  7141. if (arg2) {
  7142. if (num == TARGET_NR_capget) {
  7143. for (i = 0; i < data_items; i++) {
  7144. target_data[i].effective = tswap32(data[i].effective);
  7145. target_data[i].permitted = tswap32(data[i].permitted);
  7146. target_data[i].inheritable = tswap32(data[i].inheritable);
  7147. }
  7148. unlock_user(target_data, arg2, target_datalen);
  7149. } else {
  7150. unlock_user(target_data, arg2, 0);
  7151. }
  7152. }
  7153. break;
  7154. }
  7155. case TARGET_NR_sigaltstack:
  7156. #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
  7157. defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
  7158. defined(TARGET_M68K) || defined(TARGET_S390X) || defined(TARGET_OPENRISC)
  7159. ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUArchState *)cpu_env));
  7160. break;
  7161. #else
  7162. goto unimplemented;
  7163. #endif
  7164. #ifdef CONFIG_SENDFILE
  7165. case TARGET_NR_sendfile:
  7166. {
  7167. off_t *offp = NULL;
  7168. off_t off;
  7169. if (arg3) {
  7170. ret = get_user_sal(off, arg3);
  7171. if (is_error(ret)) {
  7172. break;
  7173. }
  7174. offp = &off;
  7175. }
  7176. ret = get_errno(sendfile(arg1, arg2, offp, arg4));
  7177. if (!is_error(ret) && arg3) {
  7178. abi_long ret2 = put_user_sal(off, arg3);
  7179. if (is_error(ret2)) {
  7180. ret = ret2;
  7181. }
  7182. }
  7183. break;
  7184. }
  7185. #ifdef TARGET_NR_sendfile64
  7186. case TARGET_NR_sendfile64:
  7187. {
  7188. off_t *offp = NULL;
  7189. off_t off;
  7190. if (arg3) {
  7191. ret = get_user_s64(off, arg3);
  7192. if (is_error(ret)) {
  7193. break;
  7194. }
  7195. offp = &off;
  7196. }
  7197. ret = get_errno(sendfile(arg1, arg2, offp, arg4));
  7198. if (!is_error(ret) && arg3) {
  7199. abi_long ret2 = put_user_s64(off, arg3);
  7200. if (is_error(ret2)) {
  7201. ret = ret2;
  7202. }
  7203. }
  7204. break;
  7205. }
  7206. #endif
  7207. #else
  7208. case TARGET_NR_sendfile:
  7209. #ifdef TARGET_NR_sendfile64
  7210. case TARGET_NR_sendfile64:
  7211. #endif
  7212. goto unimplemented;
  7213. #endif
  7214. #ifdef TARGET_NR_getpmsg
  7215. case TARGET_NR_getpmsg:
  7216. goto unimplemented;
  7217. #endif
  7218. #ifdef TARGET_NR_putpmsg
  7219. case TARGET_NR_putpmsg:
  7220. goto unimplemented;
  7221. #endif
  7222. #ifdef TARGET_NR_vfork
  7223. case TARGET_NR_vfork:
  7224. ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
  7225. 0, 0, 0, 0));
  7226. break;
  7227. #endif
  7228. #ifdef TARGET_NR_ugetrlimit
  7229. case TARGET_NR_ugetrlimit:
  7230. {
  7231. struct rlimit rlim;
  7232. int resource = target_to_host_resource(arg1);
  7233. ret = get_errno(getrlimit(resource, &rlim));
  7234. if (!is_error(ret)) {
  7235. struct target_rlimit *target_rlim;
  7236. if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
  7237. goto efault;
  7238. target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
  7239. target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
  7240. unlock_user_struct(target_rlim, arg2, 1);
  7241. }
  7242. break;
  7243. }
  7244. #endif
  7245. #ifdef TARGET_NR_truncate64
  7246. case TARGET_NR_truncate64:
  7247. if (!(p = lock_user_string(arg1)))
  7248. goto efault;
  7249. ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
  7250. unlock_user(p, arg1, 0);
  7251. break;
  7252. #endif
  7253. #ifdef TARGET_NR_ftruncate64
  7254. case TARGET_NR_ftruncate64:
  7255. ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
  7256. break;
  7257. #endif
  7258. #ifdef TARGET_NR_stat64
  7259. case TARGET_NR_stat64:
  7260. if (!(p = lock_user_string(arg1)))
  7261. goto efault;
  7262. ret = get_errno(stat(path(p), &st));
  7263. unlock_user(p, arg1, 0);
  7264. if (!is_error(ret))
  7265. ret = host_to_target_stat64(cpu_env, arg2, &st);
  7266. break;
  7267. #endif
  7268. #ifdef TARGET_NR_lstat64
  7269. case TARGET_NR_lstat64:
  7270. if (!(p = lock_user_string(arg1)))
  7271. goto efault;
  7272. ret = get_errno(lstat(path(p), &st));
  7273. unlock_user(p, arg1, 0);
  7274. if (!is_error(ret))
  7275. ret = host_to_target_stat64(cpu_env, arg2, &st);
  7276. break;
  7277. #endif
  7278. #ifdef TARGET_NR_fstat64
  7279. case TARGET_NR_fstat64:
  7280. ret = get_errno(fstat(arg1, &st));
  7281. if (!is_error(ret))
  7282. ret = host_to_target_stat64(cpu_env, arg2, &st);
  7283. break;
  7284. #endif
  7285. #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
  7286. #ifdef TARGET_NR_fstatat64
  7287. case TARGET_NR_fstatat64:
  7288. #endif
  7289. #ifdef TARGET_NR_newfstatat
  7290. case TARGET_NR_newfstatat:
  7291. #endif
  7292. if (!(p = lock_user_string(arg2)))
  7293. goto efault;
  7294. ret = get_errno(fstatat(arg1, path(p), &st, arg4));
  7295. if (!is_error(ret))
  7296. ret = host_to_target_stat64(cpu_env, arg3, &st);
  7297. break;
  7298. #endif
  7299. case TARGET_NR_lchown:
  7300. if (!(p = lock_user_string(arg1)))
  7301. goto efault;
  7302. ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
  7303. unlock_user(p, arg1, 0);
  7304. break;
  7305. #ifdef TARGET_NR_getuid
  7306. case TARGET_NR_getuid:
  7307. ret = get_errno(high2lowuid(getuid()));
  7308. break;
  7309. #endif
  7310. #ifdef TARGET_NR_getgid
  7311. case TARGET_NR_getgid:
  7312. ret = get_errno(high2lowgid(getgid()));
  7313. break;
  7314. #endif
  7315. #ifdef TARGET_NR_geteuid
  7316. case TARGET_NR_geteuid:
  7317. ret = get_errno(high2lowuid(geteuid()));
  7318. break;
  7319. #endif
  7320. #ifdef TARGET_NR_getegid
  7321. case TARGET_NR_getegid:
  7322. ret = get_errno(high2lowgid(getegid()));
  7323. break;
  7324. #endif
  7325. case TARGET_NR_setreuid:
  7326. ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
  7327. break;
  7328. case TARGET_NR_setregid:
  7329. ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
  7330. break;
  7331. case TARGET_NR_getgroups:
  7332. {
  7333. int gidsetsize = arg1;
  7334. target_id *target_grouplist;
  7335. gid_t *grouplist;
  7336. int i;
  7337. grouplist = alloca(gidsetsize * sizeof(gid_t));
  7338. ret = get_errno(getgroups(gidsetsize, grouplist));
  7339. if (gidsetsize == 0)
  7340. break;
  7341. if (!is_error(ret)) {
  7342. target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * sizeof(target_id), 0);
  7343. if (!target_grouplist)
  7344. goto efault;
  7345. for(i = 0;i < ret; i++)
  7346. target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
  7347. unlock_user(target_grouplist, arg2, gidsetsize * sizeof(target_id));
  7348. }
  7349. }
  7350. break;
  7351. case TARGET_NR_setgroups:
  7352. {
  7353. int gidsetsize = arg1;
  7354. target_id *target_grouplist;
  7355. gid_t *grouplist = NULL;
  7356. int i;
  7357. if (gidsetsize) {
  7358. grouplist = alloca(gidsetsize * sizeof(gid_t));
  7359. target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * sizeof(target_id), 1);
  7360. if (!target_grouplist) {
  7361. ret = -TARGET_EFAULT;
  7362. goto fail;
  7363. }
  7364. for (i = 0; i < gidsetsize; i++) {
  7365. grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
  7366. }
  7367. unlock_user(target_grouplist, arg2, 0);
  7368. }
  7369. ret = get_errno(setgroups(gidsetsize, grouplist));
  7370. }
  7371. break;
  7372. case TARGET_NR_fchown:
  7373. ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
  7374. break;
  7375. #if defined(TARGET_NR_fchownat)
  7376. case TARGET_NR_fchownat:
  7377. if (!(p = lock_user_string(arg2)))
  7378. goto efault;
  7379. ret = get_errno(fchownat(arg1, p, low2highuid(arg3),
  7380. low2highgid(arg4), arg5));
  7381. unlock_user(p, arg2, 0);
  7382. break;
  7383. #endif
  7384. #ifdef TARGET_NR_setresuid
  7385. case TARGET_NR_setresuid:
  7386. ret = get_errno(setresuid(low2highuid(arg1),
  7387. low2highuid(arg2),
  7388. low2highuid(arg3)));
  7389. break;
  7390. #endif
  7391. #ifdef TARGET_NR_getresuid
  7392. case TARGET_NR_getresuid:
  7393. {
  7394. uid_t ruid, euid, suid;
  7395. ret = get_errno(getresuid(&ruid, &euid, &suid));
  7396. if (!is_error(ret)) {
  7397. if (put_user_id(high2lowuid(ruid), arg1)
  7398. || put_user_id(high2lowuid(euid), arg2)
  7399. || put_user_id(high2lowuid(suid), arg3))
  7400. goto efault;
  7401. }
  7402. }
  7403. break;
  7404. #endif
  7405. #ifdef TARGET_NR_getresgid
  7406. case TARGET_NR_setresgid:
  7407. ret = get_errno(setresgid(low2highgid(arg1),
  7408. low2highgid(arg2),
  7409. low2highgid(arg3)));
  7410. break;
  7411. #endif
  7412. #ifdef TARGET_NR_getresgid
  7413. case TARGET_NR_getresgid:
  7414. {
  7415. gid_t rgid, egid, sgid;
  7416. ret = get_errno(getresgid(&rgid, &egid, &sgid));
  7417. if (!is_error(ret)) {
  7418. if (put_user_id(high2lowgid(rgid), arg1)
  7419. || put_user_id(high2lowgid(egid), arg2)
  7420. || put_user_id(high2lowgid(sgid), arg3))
  7421. goto efault;
  7422. }
  7423. }
  7424. break;
  7425. #endif
  7426. case TARGET_NR_chown:
  7427. if (!(p = lock_user_string(arg1)))
  7428. goto efault;
  7429. ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
  7430. unlock_user(p, arg1, 0);
  7431. break;
  7432. case TARGET_NR_setuid:
  7433. ret = get_errno(setuid(low2highuid(arg1)));
  7434. break;
  7435. case TARGET_NR_setgid:
  7436. ret = get_errno(setgid(low2highgid(arg1)));
  7437. break;
  7438. case TARGET_NR_setfsuid:
  7439. ret = get_errno(setfsuid(arg1));
  7440. break;
  7441. case TARGET_NR_setfsgid:
  7442. ret = get_errno(setfsgid(arg1));
  7443. break;
  7444. #ifdef TARGET_NR_lchown32
  7445. case TARGET_NR_lchown32:
  7446. if (!(p = lock_user_string(arg1)))
  7447. goto efault;
  7448. ret = get_errno(lchown(p, arg2, arg3));
  7449. unlock_user(p, arg1, 0);
  7450. break;
  7451. #endif
  7452. #ifdef TARGET_NR_getuid32
  7453. case TARGET_NR_getuid32:
  7454. ret = get_errno(getuid());
  7455. break;
  7456. #endif
  7457. #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
  7458. /* Alpha specific */
  7459. case TARGET_NR_getxuid:
  7460. {
  7461. uid_t euid;
  7462. euid=geteuid();
  7463. ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
  7464. }
  7465. ret = get_errno(getuid());
  7466. break;
  7467. #endif
  7468. #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
  7469. /* Alpha specific */
  7470. case TARGET_NR_getxgid:
  7471. {
  7472. uid_t egid;
  7473. egid=getegid();
  7474. ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
  7475. }
  7476. ret = get_errno(getgid());
  7477. break;
  7478. #endif
  7479. #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
  7480. /* Alpha specific */
  7481. case TARGET_NR_osf_getsysinfo:
  7482. ret = -TARGET_EOPNOTSUPP;
  7483. switch (arg1) {
  7484. case TARGET_GSI_IEEE_FP_CONTROL:
  7485. {
  7486. uint64_t swcr, fpcr = cpu_alpha_load_fpcr (cpu_env);
  7487. /* Copied from linux ieee_fpcr_to_swcr. */
  7488. swcr = (fpcr >> 35) & SWCR_STATUS_MASK;
  7489. swcr |= (fpcr >> 36) & SWCR_MAP_DMZ;
  7490. swcr |= (~fpcr >> 48) & (SWCR_TRAP_ENABLE_INV
  7491. | SWCR_TRAP_ENABLE_DZE
  7492. | SWCR_TRAP_ENABLE_OVF);
  7493. swcr |= (~fpcr >> 57) & (SWCR_TRAP_ENABLE_UNF
  7494. | SWCR_TRAP_ENABLE_INE);
  7495. swcr |= (fpcr >> 47) & SWCR_MAP_UMZ;
  7496. swcr |= (~fpcr >> 41) & SWCR_TRAP_ENABLE_DNO;
  7497. if (put_user_u64 (swcr, arg2))
  7498. goto efault;
  7499. ret = 0;
  7500. }
  7501. break;
  7502. /* case GSI_IEEE_STATE_AT_SIGNAL:
  7503. -- Not implemented in linux kernel.
  7504. case GSI_UACPROC:
  7505. -- Retrieves current unaligned access state; not much used.
  7506. case GSI_PROC_TYPE:
  7507. -- Retrieves implver information; surely not used.
  7508. case GSI_GET_HWRPB:
  7509. -- Grabs a copy of the HWRPB; surely not used.
  7510. */
  7511. }
  7512. break;
  7513. #endif
  7514. #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
  7515. /* Alpha specific */
  7516. case TARGET_NR_osf_setsysinfo:
  7517. ret = -TARGET_EOPNOTSUPP;
  7518. switch (arg1) {
  7519. case TARGET_SSI_IEEE_FP_CONTROL:
  7520. {
  7521. uint64_t swcr, fpcr, orig_fpcr;
  7522. if (get_user_u64 (swcr, arg2)) {
  7523. goto efault;
  7524. }
  7525. orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
  7526. fpcr = orig_fpcr & FPCR_DYN_MASK;
  7527. /* Copied from linux ieee_swcr_to_fpcr. */
  7528. fpcr |= (swcr & SWCR_STATUS_MASK) << 35;
  7529. fpcr |= (swcr & SWCR_MAP_DMZ) << 36;
  7530. fpcr |= (~swcr & (SWCR_TRAP_ENABLE_INV
  7531. | SWCR_TRAP_ENABLE_DZE
  7532. | SWCR_TRAP_ENABLE_OVF)) << 48;
  7533. fpcr |= (~swcr & (SWCR_TRAP_ENABLE_UNF
  7534. | SWCR_TRAP_ENABLE_INE)) << 57;
  7535. fpcr |= (swcr & SWCR_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0);
  7536. fpcr |= (~swcr & SWCR_TRAP_ENABLE_DNO) << 41;
  7537. cpu_alpha_store_fpcr(cpu_env, fpcr);
  7538. ret = 0;
  7539. }
  7540. break;
  7541. case TARGET_SSI_IEEE_RAISE_EXCEPTION:
  7542. {
  7543. uint64_t exc, fpcr, orig_fpcr;
  7544. int si_code;
  7545. if (get_user_u64(exc, arg2)) {
  7546. goto efault;
  7547. }
  7548. orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
  7549. /* We only add to the exception status here. */
  7550. fpcr = orig_fpcr | ((exc & SWCR_STATUS_MASK) << 35);
  7551. cpu_alpha_store_fpcr(cpu_env, fpcr);
  7552. ret = 0;
  7553. /* Old exceptions are not signaled. */
  7554. fpcr &= ~(orig_fpcr & FPCR_STATUS_MASK);
  7555. /* If any exceptions set by this call,
  7556. and are unmasked, send a signal. */
  7557. si_code = 0;
  7558. if ((fpcr & (FPCR_INE | FPCR_INED)) == FPCR_INE) {
  7559. si_code = TARGET_FPE_FLTRES;
  7560. }
  7561. if ((fpcr & (FPCR_UNF | FPCR_UNFD)) == FPCR_UNF) {
  7562. si_code = TARGET_FPE_FLTUND;
  7563. }
  7564. if ((fpcr & (FPCR_OVF | FPCR_OVFD)) == FPCR_OVF) {
  7565. si_code = TARGET_FPE_FLTOVF;
  7566. }
  7567. if ((fpcr & (FPCR_DZE | FPCR_DZED)) == FPCR_DZE) {
  7568. si_code = TARGET_FPE_FLTDIV;
  7569. }
  7570. if ((fpcr & (FPCR_INV | FPCR_INVD)) == FPCR_INV) {
  7571. si_code = TARGET_FPE_FLTINV;
  7572. }
  7573. if (si_code != 0) {
  7574. target_siginfo_t info;
  7575. info.si_signo = SIGFPE;
  7576. info.si_errno = 0;
  7577. info.si_code = si_code;
  7578. info._sifields._sigfault._addr
  7579. = ((CPUArchState *)cpu_env)->pc;
  7580. queue_signal((CPUArchState *)cpu_env, info.si_signo, &info);
  7581. }
  7582. }
  7583. break;
  7584. /* case SSI_NVPAIRS:
  7585. -- Used with SSIN_UACPROC to enable unaligned accesses.
  7586. case SSI_IEEE_STATE_AT_SIGNAL:
  7587. case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
  7588. -- Not implemented in linux kernel
  7589. */
  7590. }
  7591. break;
  7592. #endif
  7593. #ifdef TARGET_NR_osf_sigprocmask
  7594. /* Alpha specific. */
  7595. case TARGET_NR_osf_sigprocmask:
  7596. {
  7597. abi_ulong mask;
  7598. int how;
  7599. sigset_t set, oldset;
  7600. switch(arg1) {
  7601. case TARGET_SIG_BLOCK:
  7602. how = SIG_BLOCK;
  7603. break;
  7604. case TARGET_SIG_UNBLOCK:
  7605. how = SIG_UNBLOCK;
  7606. break;
  7607. case TARGET_SIG_SETMASK:
  7608. how = SIG_SETMASK;
  7609. break;
  7610. default:
  7611. ret = -TARGET_EINVAL;
  7612. goto fail;
  7613. }
  7614. mask = arg2;
  7615. target_to_host_old_sigset(&set, &mask);
  7616. do_sigprocmask(how, &set, &oldset);
  7617. host_to_target_old_sigset(&mask, &oldset);
  7618. ret = mask;
  7619. }
  7620. break;
  7621. #endif
  7622. #ifdef TARGET_NR_getgid32
  7623. case TARGET_NR_getgid32:
  7624. ret = get_errno(getgid());
  7625. break;
  7626. #endif
  7627. #ifdef TARGET_NR_geteuid32
  7628. case TARGET_NR_geteuid32:
  7629. ret = get_errno(geteuid());
  7630. break;
  7631. #endif
  7632. #ifdef TARGET_NR_getegid32
  7633. case TARGET_NR_getegid32:
  7634. ret = get_errno(getegid());
  7635. break;
  7636. #endif
  7637. #ifdef TARGET_NR_setreuid32
  7638. case TARGET_NR_setreuid32:
  7639. ret = get_errno(setreuid(arg1, arg2));
  7640. break;
  7641. #endif
  7642. #ifdef TARGET_NR_setregid32
  7643. case TARGET_NR_setregid32:
  7644. ret = get_errno(setregid(arg1, arg2));
  7645. break;
  7646. #endif
  7647. #ifdef TARGET_NR_getgroups32
  7648. case TARGET_NR_getgroups32:
  7649. {
  7650. int gidsetsize = arg1;
  7651. uint32_t *target_grouplist;
  7652. gid_t *grouplist;
  7653. int i;
  7654. grouplist = alloca(gidsetsize * sizeof(gid_t));
  7655. ret = get_errno(getgroups(gidsetsize, grouplist));
  7656. if (gidsetsize == 0)
  7657. break;
  7658. if (!is_error(ret)) {
  7659. target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
  7660. if (!target_grouplist) {
  7661. ret = -TARGET_EFAULT;
  7662. goto fail;
  7663. }
  7664. for(i = 0;i < ret; i++)
  7665. target_grouplist[i] = tswap32(grouplist[i]);
  7666. unlock_user(target_grouplist, arg2, gidsetsize * 4);
  7667. }
  7668. }
  7669. break;
  7670. #endif
  7671. #ifdef TARGET_NR_setgroups32
  7672. case TARGET_NR_setgroups32:
  7673. {
  7674. int gidsetsize = arg1;
  7675. uint32_t *target_grouplist;
  7676. gid_t *grouplist;
  7677. int i;
  7678. grouplist = alloca(gidsetsize * sizeof(gid_t));
  7679. target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
  7680. if (!target_grouplist) {
  7681. ret = -TARGET_EFAULT;
  7682. goto fail;
  7683. }
  7684. for(i = 0;i < gidsetsize; i++)
  7685. grouplist[i] = tswap32(target_grouplist[i]);
  7686. unlock_user(target_grouplist, arg2, 0);
  7687. ret = get_errno(setgroups(gidsetsize, grouplist));
  7688. }
  7689. break;
  7690. #endif
  7691. #ifdef TARGET_NR_fchown32
  7692. case TARGET_NR_fchown32:
  7693. ret = get_errno(fchown(arg1, arg2, arg3));
  7694. break;
  7695. #endif
  7696. #ifdef TARGET_NR_setresuid32
  7697. case TARGET_NR_setresuid32:
  7698. ret = get_errno(setresuid(arg1, arg2, arg3));
  7699. break;
  7700. #endif
  7701. #ifdef TARGET_NR_getresuid32
  7702. case TARGET_NR_getresuid32:
  7703. {
  7704. uid_t ruid, euid, suid;
  7705. ret = get_errno(getresuid(&ruid, &euid, &suid));
  7706. if (!is_error(ret)) {
  7707. if (put_user_u32(ruid, arg1)
  7708. || put_user_u32(euid, arg2)
  7709. || put_user_u32(suid, arg3))
  7710. goto efault;
  7711. }
  7712. }
  7713. break;
  7714. #endif
  7715. #ifdef TARGET_NR_setresgid32
  7716. case TARGET_NR_setresgid32:
  7717. ret = get_errno(setresgid(arg1, arg2, arg3));
  7718. break;
  7719. #endif
  7720. #ifdef TARGET_NR_getresgid32
  7721. case TARGET_NR_getresgid32:
  7722. {
  7723. gid_t rgid, egid, sgid;
  7724. ret = get_errno(getresgid(&rgid, &egid, &sgid));
  7725. if (!is_error(ret)) {
  7726. if (put_user_u32(rgid, arg1)
  7727. || put_user_u32(egid, arg2)
  7728. || put_user_u32(sgid, arg3))
  7729. goto efault;
  7730. }
  7731. }
  7732. break;
  7733. #endif
  7734. #ifdef TARGET_NR_chown32
  7735. case TARGET_NR_chown32:
  7736. if (!(p = lock_user_string(arg1)))
  7737. goto efault;
  7738. ret = get_errno(chown(p, arg2, arg3));
  7739. unlock_user(p, arg1, 0);
  7740. break;
  7741. #endif
  7742. #ifdef TARGET_NR_setuid32
  7743. case TARGET_NR_setuid32:
  7744. ret = get_errno(setuid(arg1));
  7745. break;
  7746. #endif
  7747. #ifdef TARGET_NR_setgid32
  7748. case TARGET_NR_setgid32:
  7749. ret = get_errno(setgid(arg1));
  7750. break;
  7751. #endif
  7752. #ifdef TARGET_NR_setfsuid32
  7753. case TARGET_NR_setfsuid32:
  7754. ret = get_errno(setfsuid(arg1));
  7755. break;
  7756. #endif
  7757. #ifdef TARGET_NR_setfsgid32
  7758. case TARGET_NR_setfsgid32:
  7759. ret = get_errno(setfsgid(arg1));
  7760. break;
  7761. #endif
  7762. case TARGET_NR_pivot_root:
  7763. goto unimplemented;
  7764. #ifdef TARGET_NR_mincore
  7765. case TARGET_NR_mincore:
  7766. {
  7767. void *a;
  7768. ret = -TARGET_EFAULT;
  7769. if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
  7770. goto efault;
  7771. if (!(p = lock_user_string(arg3)))
  7772. goto mincore_fail;
  7773. ret = get_errno(mincore(a, arg2, p));
  7774. unlock_user(p, arg3, ret);
  7775. mincore_fail:
  7776. unlock_user(a, arg1, 0);
  7777. }
  7778. break;
  7779. #endif
  7780. #ifdef TARGET_NR_arm_fadvise64_64
  7781. case TARGET_NR_arm_fadvise64_64:
  7782. {
  7783. /*
  7784. * arm_fadvise64_64 looks like fadvise64_64 but
  7785. * with different argument order
  7786. */
  7787. abi_long temp;
  7788. temp = arg3;
  7789. arg3 = arg4;
  7790. arg4 = temp;
  7791. }
  7792. #endif
  7793. #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
  7794. #ifdef TARGET_NR_fadvise64_64
  7795. case TARGET_NR_fadvise64_64:
  7796. #endif
  7797. #ifdef TARGET_NR_fadvise64
  7798. case TARGET_NR_fadvise64:
  7799. #endif
  7800. #ifdef TARGET_S390X
  7801. switch (arg4) {
  7802. case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
  7803. case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
  7804. case 6: arg4 = POSIX_FADV_DONTNEED; break;
  7805. case 7: arg4 = POSIX_FADV_NOREUSE; break;
  7806. default: break;
  7807. }
  7808. #endif
  7809. ret = -posix_fadvise(arg1, arg2, arg3, arg4);
  7810. break;
  7811. #endif
  7812. #ifdef TARGET_NR_madvise
  7813. case TARGET_NR_madvise:
  7814. /* A straight passthrough may not be safe because qemu sometimes
  7815. turns private file-backed mappings into anonymous mappings.
  7816. This will break MADV_DONTNEED.
  7817. This is a hint, so ignoring and returning success is ok. */
  7818. ret = get_errno(0);
  7819. break;
  7820. #endif
  7821. #if TARGET_ABI_BITS == 32
  7822. case TARGET_NR_fcntl64:
  7823. {
  7824. int cmd;
  7825. struct flock64 fl;
  7826. struct target_flock64 *target_fl;
  7827. #ifdef TARGET_ARM
  7828. struct target_eabi_flock64 *target_efl;
  7829. #endif
  7830. cmd = target_to_host_fcntl_cmd(arg2);
  7831. if (cmd == -TARGET_EINVAL) {
  7832. ret = cmd;
  7833. break;
  7834. }
  7835. switch(arg2) {
  7836. case TARGET_F_GETLK64:
  7837. #ifdef TARGET_ARM
  7838. if (((CPUARMState *)cpu_env)->eabi) {
  7839. if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
  7840. goto efault;
  7841. fl.l_type = tswap16(target_efl->l_type);
  7842. fl.l_whence = tswap16(target_efl->l_whence);
  7843. fl.l_start = tswap64(target_efl->l_start);
  7844. fl.l_len = tswap64(target_efl->l_len);
  7845. fl.l_pid = tswap32(target_efl->l_pid);
  7846. unlock_user_struct(target_efl, arg3, 0);
  7847. } else
  7848. #endif
  7849. {
  7850. if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
  7851. goto efault;
  7852. fl.l_type = tswap16(target_fl->l_type);
  7853. fl.l_whence = tswap16(target_fl->l_whence);
  7854. fl.l_start = tswap64(target_fl->l_start);
  7855. fl.l_len = tswap64(target_fl->l_len);
  7856. fl.l_pid = tswap32(target_fl->l_pid);
  7857. unlock_user_struct(target_fl, arg3, 0);
  7858. }
  7859. ret = get_errno(fcntl(arg1, cmd, &fl));
  7860. if (ret == 0) {
  7861. #ifdef TARGET_ARM
  7862. if (((CPUARMState *)cpu_env)->eabi) {
  7863. if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
  7864. goto efault;
  7865. target_efl->l_type = tswap16(fl.l_type);
  7866. target_efl->l_whence = tswap16(fl.l_whence);
  7867. target_efl->l_start = tswap64(fl.l_start);
  7868. target_efl->l_len = tswap64(fl.l_len);
  7869. target_efl->l_pid = tswap32(fl.l_pid);
  7870. unlock_user_struct(target_efl, arg3, 1);
  7871. } else
  7872. #endif
  7873. {
  7874. if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
  7875. goto efault;
  7876. target_fl->l_type = tswap16(fl.l_type);
  7877. target_fl->l_whence = tswap16(fl.l_whence);
  7878. target_fl->l_start = tswap64(fl.l_start);
  7879. target_fl->l_len = tswap64(fl.l_len);
  7880. target_fl->l_pid = tswap32(fl.l_pid);
  7881. unlock_user_struct(target_fl, arg3, 1);
  7882. }
  7883. }
  7884. break;
  7885. case TARGET_F_SETLK64:
  7886. case TARGET_F_SETLKW64:
  7887. #ifdef TARGET_ARM
  7888. if (((CPUARMState *)cpu_env)->eabi) {
  7889. if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
  7890. goto efault;
  7891. fl.l_type = tswap16(target_efl->l_type);
  7892. fl.l_whence = tswap16(target_efl->l_whence);
  7893. fl.l_start = tswap64(target_efl->l_start);
  7894. fl.l_len = tswap64(target_efl->l_len);
  7895. fl.l_pid = tswap32(target_efl->l_pid);
  7896. unlock_user_struct(target_efl, arg3, 0);
  7897. } else
  7898. #endif
  7899. {
  7900. if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
  7901. goto efault;
  7902. fl.l_type = tswap16(target_fl->l_type);
  7903. fl.l_whence = tswap16(target_fl->l_whence);
  7904. fl.l_start = tswap64(target_fl->l_start);
  7905. fl.l_len = tswap64(target_fl->l_len);
  7906. fl.l_pid = tswap32(target_fl->l_pid);
  7907. unlock_user_struct(target_fl, arg3, 0);
  7908. }
  7909. ret = get_errno(fcntl(arg1, cmd, &fl));
  7910. break;
  7911. default:
  7912. ret = do_fcntl(arg1, arg2, arg3);
  7913. break;
  7914. }
  7915. break;
  7916. }
  7917. #endif
  7918. #ifdef TARGET_NR_cacheflush
  7919. case TARGET_NR_cacheflush:
  7920. /* self-modifying code is handled automatically, so nothing needed */
  7921. ret = 0;
  7922. break;
  7923. #endif
  7924. #ifdef TARGET_NR_security
  7925. case TARGET_NR_security:
  7926. goto unimplemented;
  7927. #endif
  7928. #ifdef TARGET_NR_getpagesize
  7929. case TARGET_NR_getpagesize:
  7930. ret = TARGET_PAGE_SIZE;
  7931. break;
  7932. #endif
  7933. case TARGET_NR_gettid:
  7934. ret = get_errno(gettid());
  7935. break;
  7936. #ifdef TARGET_NR_readahead
  7937. case TARGET_NR_readahead:
  7938. #if TARGET_ABI_BITS == 32
  7939. if (regpairs_aligned(cpu_env)) {
  7940. arg2 = arg3;
  7941. arg3 = arg4;
  7942. arg4 = arg5;
  7943. }
  7944. ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
  7945. #else
  7946. ret = get_errno(readahead(arg1, arg2, arg3));
  7947. #endif
  7948. break;
  7949. #endif
  7950. #ifdef CONFIG_ATTR
  7951. #ifdef TARGET_NR_setxattr
  7952. case TARGET_NR_listxattr:
  7953. case TARGET_NR_llistxattr:
  7954. {
  7955. void *p, *b = 0;
  7956. if (arg2) {
  7957. b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  7958. if (!b) {
  7959. ret = -TARGET_EFAULT;
  7960. break;
  7961. }
  7962. }
  7963. p = lock_user_string(arg1);
  7964. if (p) {
  7965. if (num == TARGET_NR_listxattr) {
  7966. ret = get_errno(listxattr(p, b, arg3));
  7967. } else {
  7968. ret = get_errno(llistxattr(p, b, arg3));
  7969. }
  7970. } else {
  7971. ret = -TARGET_EFAULT;
  7972. }
  7973. unlock_user(p, arg1, 0);
  7974. unlock_user(b, arg2, arg3);
  7975. break;
  7976. }
  7977. case TARGET_NR_flistxattr:
  7978. {
  7979. void *b = 0;
  7980. if (arg2) {
  7981. b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
  7982. if (!b) {
  7983. ret = -TARGET_EFAULT;
  7984. break;
  7985. }
  7986. }
  7987. ret = get_errno(flistxattr(arg1, b, arg3));
  7988. unlock_user(b, arg2, arg3);
  7989. break;
  7990. }
  7991. case TARGET_NR_setxattr:
  7992. case TARGET_NR_lsetxattr:
  7993. {
  7994. void *p, *n, *v = 0;
  7995. if (arg3) {
  7996. v = lock_user(VERIFY_READ, arg3, arg4, 1);
  7997. if (!v) {
  7998. ret = -TARGET_EFAULT;
  7999. break;
  8000. }
  8001. }
  8002. p = lock_user_string(arg1);
  8003. n = lock_user_string(arg2);
  8004. if (p && n) {
  8005. if (num == TARGET_NR_setxattr) {
  8006. ret = get_errno(setxattr(p, n, v, arg4, arg5));
  8007. } else {
  8008. ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
  8009. }
  8010. } else {
  8011. ret = -TARGET_EFAULT;
  8012. }
  8013. unlock_user(p, arg1, 0);
  8014. unlock_user(n, arg2, 0);
  8015. unlock_user(v, arg3, 0);
  8016. }
  8017. break;
  8018. case TARGET_NR_fsetxattr:
  8019. {
  8020. void *n, *v = 0;
  8021. if (arg3) {
  8022. v = lock_user(VERIFY_READ, arg3, arg4, 1);
  8023. if (!v) {
  8024. ret = -TARGET_EFAULT;
  8025. break;
  8026. }
  8027. }
  8028. n = lock_user_string(arg2);
  8029. if (n) {
  8030. ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
  8031. } else {
  8032. ret = -TARGET_EFAULT;
  8033. }
  8034. unlock_user(n, arg2, 0);
  8035. unlock_user(v, arg3, 0);
  8036. }
  8037. break;
  8038. case TARGET_NR_getxattr:
  8039. case TARGET_NR_lgetxattr:
  8040. {
  8041. void *p, *n, *v = 0;
  8042. if (arg3) {
  8043. v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  8044. if (!v) {
  8045. ret = -TARGET_EFAULT;
  8046. break;
  8047. }
  8048. }
  8049. p = lock_user_string(arg1);
  8050. n = lock_user_string(arg2);
  8051. if (p && n) {
  8052. if (num == TARGET_NR_getxattr) {
  8053. ret = get_errno(getxattr(p, n, v, arg4));
  8054. } else {
  8055. ret = get_errno(lgetxattr(p, n, v, arg4));
  8056. }
  8057. } else {
  8058. ret = -TARGET_EFAULT;
  8059. }
  8060. unlock_user(p, arg1, 0);
  8061. unlock_user(n, arg2, 0);
  8062. unlock_user(v, arg3, arg4);
  8063. }
  8064. break;
  8065. case TARGET_NR_fgetxattr:
  8066. {
  8067. void *n, *v = 0;
  8068. if (arg3) {
  8069. v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
  8070. if (!v) {
  8071. ret = -TARGET_EFAULT;
  8072. break;
  8073. }
  8074. }
  8075. n = lock_user_string(arg2);
  8076. if (n) {
  8077. ret = get_errno(fgetxattr(arg1, n, v, arg4));
  8078. } else {
  8079. ret = -TARGET_EFAULT;
  8080. }
  8081. unlock_user(n, arg2, 0);
  8082. unlock_user(v, arg3, arg4);
  8083. }
  8084. break;
  8085. case TARGET_NR_removexattr:
  8086. case TARGET_NR_lremovexattr:
  8087. {
  8088. void *p, *n;
  8089. p = lock_user_string(arg1);
  8090. n = lock_user_string(arg2);
  8091. if (p && n) {
  8092. if (num == TARGET_NR_removexattr) {
  8093. ret = get_errno(removexattr(p, n));
  8094. } else {
  8095. ret = get_errno(lremovexattr(p, n));
  8096. }
  8097. } else {
  8098. ret = -TARGET_EFAULT;
  8099. }
  8100. unlock_user(p, arg1, 0);
  8101. unlock_user(n, arg2, 0);
  8102. }
  8103. break;
  8104. case TARGET_NR_fremovexattr:
  8105. {
  8106. void *n;
  8107. n = lock_user_string(arg2);
  8108. if (n) {
  8109. ret = get_errno(fremovexattr(arg1, n));
  8110. } else {
  8111. ret = -TARGET_EFAULT;
  8112. }
  8113. unlock_user(n, arg2, 0);
  8114. }
  8115. break;
  8116. #endif
  8117. #endif /* CONFIG_ATTR */
  8118. #ifdef TARGET_NR_set_thread_area
  8119. case TARGET_NR_set_thread_area:
  8120. #if defined(TARGET_MIPS)
  8121. ((CPUMIPSState *) cpu_env)->tls_value = arg1;
  8122. ret = 0;
  8123. break;
  8124. #elif defined(TARGET_CRIS)
  8125. if (arg1 & 0xff)
  8126. ret = -TARGET_EINVAL;
  8127. else {
  8128. ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
  8129. ret = 0;
  8130. }
  8131. break;
  8132. #elif defined(TARGET_I386) && defined(TARGET_ABI32)
  8133. ret = do_set_thread_area(cpu_env, arg1);
  8134. break;
  8135. #elif defined(TARGET_M68K)
  8136. {
  8137. TaskState *ts = cpu->opaque;
  8138. ts->tp_value = arg1;
  8139. ret = 0;
  8140. break;
  8141. }
  8142. #else
  8143. goto unimplemented_nowarn;
  8144. #endif
  8145. #endif
  8146. #ifdef TARGET_NR_get_thread_area
  8147. case TARGET_NR_get_thread_area:
  8148. #if defined(TARGET_I386) && defined(TARGET_ABI32)
  8149. ret = do_get_thread_area(cpu_env, arg1);
  8150. break;
  8151. #elif defined(TARGET_M68K)
  8152. {
  8153. TaskState *ts = cpu->opaque;
  8154. ret = ts->tp_value;
  8155. break;
  8156. }
  8157. #else
  8158. goto unimplemented_nowarn;
  8159. #endif
  8160. #endif
  8161. #ifdef TARGET_NR_getdomainname
  8162. case TARGET_NR_getdomainname:
  8163. goto unimplemented_nowarn;
  8164. #endif
  8165. #ifdef TARGET_NR_clock_gettime
  8166. case TARGET_NR_clock_gettime:
  8167. {
  8168. struct timespec ts;
  8169. ret = get_errno(clock_gettime(arg1, &ts));
  8170. if (!is_error(ret)) {
  8171. host_to_target_timespec(arg2, &ts);
  8172. }
  8173. break;
  8174. }
  8175. #endif
  8176. #ifdef TARGET_NR_clock_getres
  8177. case TARGET_NR_clock_getres:
  8178. {
  8179. struct timespec ts;
  8180. ret = get_errno(clock_getres(arg1, &ts));
  8181. if (!is_error(ret)) {
  8182. host_to_target_timespec(arg2, &ts);
  8183. }
  8184. break;
  8185. }
  8186. #endif
  8187. #ifdef TARGET_NR_clock_nanosleep
  8188. case TARGET_NR_clock_nanosleep:
  8189. {
  8190. struct timespec ts;
  8191. target_to_host_timespec(&ts, arg3);
  8192. ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
  8193. if (arg4)
  8194. host_to_target_timespec(arg4, &ts);
  8195. break;
  8196. }
  8197. #endif
  8198. #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
  8199. case TARGET_NR_set_tid_address:
  8200. ret = get_errno(set_tid_address((int *)g2h(arg1)));
  8201. break;
  8202. #endif
  8203. #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
  8204. case TARGET_NR_tkill:
  8205. ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
  8206. break;
  8207. #endif
  8208. #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
  8209. case TARGET_NR_tgkill:
  8210. ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
  8211. target_to_host_signal(arg3)));
  8212. break;
  8213. #endif
  8214. #ifdef TARGET_NR_set_robust_list
  8215. case TARGET_NR_set_robust_list:
  8216. case TARGET_NR_get_robust_list:
  8217. /* The ABI for supporting robust futexes has userspace pass
  8218. * the kernel a pointer to a linked list which is updated by
  8219. * userspace after the syscall; the list is walked by the kernel
  8220. * when the thread exits. Since the linked list in QEMU guest
  8221. * memory isn't a valid linked list for the host and we have
  8222. * no way to reliably intercept the thread-death event, we can't
  8223. * support these. Silently return ENOSYS so that guest userspace
  8224. * falls back to a non-robust futex implementation (which should
  8225. * be OK except in the corner case of the guest crashing while
  8226. * holding a mutex that is shared with another process via
  8227. * shared memory).
  8228. */
  8229. goto unimplemented_nowarn;
  8230. #endif
  8231. #if defined(TARGET_NR_utimensat)
  8232. case TARGET_NR_utimensat:
  8233. {
  8234. struct timespec *tsp, ts[2];
  8235. if (!arg3) {
  8236. tsp = NULL;
  8237. } else {
  8238. target_to_host_timespec(ts, arg3);
  8239. target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
  8240. tsp = ts;
  8241. }
  8242. if (!arg2)
  8243. ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
  8244. else {
  8245. if (!(p = lock_user_string(arg2))) {
  8246. ret = -TARGET_EFAULT;
  8247. goto fail;
  8248. }
  8249. ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
  8250. unlock_user(p, arg2, 0);
  8251. }
  8252. }
  8253. break;
  8254. #endif
  8255. case TARGET_NR_futex:
  8256. ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
  8257. break;
  8258. #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
  8259. case TARGET_NR_inotify_init:
  8260. ret = get_errno(sys_inotify_init());
  8261. break;
  8262. #endif
  8263. #ifdef CONFIG_INOTIFY1
  8264. #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
  8265. case TARGET_NR_inotify_init1:
  8266. ret = get_errno(sys_inotify_init1(arg1));
  8267. break;
  8268. #endif
  8269. #endif
  8270. #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
  8271. case TARGET_NR_inotify_add_watch:
  8272. p = lock_user_string(arg2);
  8273. ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
  8274. unlock_user(p, arg2, 0);
  8275. break;
  8276. #endif
  8277. #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
  8278. case TARGET_NR_inotify_rm_watch:
  8279. ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
  8280. break;
  8281. #endif
  8282. #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
  8283. case TARGET_NR_mq_open:
  8284. {
  8285. struct mq_attr posix_mq_attr;
  8286. p = lock_user_string(arg1 - 1);
  8287. if (arg4 != 0)
  8288. copy_from_user_mq_attr (&posix_mq_attr, arg4);
  8289. ret = get_errno(mq_open(p, arg2, arg3, &posix_mq_attr));
  8290. unlock_user (p, arg1, 0);
  8291. }
  8292. break;
  8293. case TARGET_NR_mq_unlink:
  8294. p = lock_user_string(arg1 - 1);
  8295. ret = get_errno(mq_unlink(p));
  8296. unlock_user (p, arg1, 0);
  8297. break;
  8298. case TARGET_NR_mq_timedsend:
  8299. {
  8300. struct timespec ts;
  8301. p = lock_user (VERIFY_READ, arg2, arg3, 1);
  8302. if (arg5 != 0) {
  8303. target_to_host_timespec(&ts, arg5);
  8304. ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
  8305. host_to_target_timespec(arg5, &ts);
  8306. }
  8307. else
  8308. ret = get_errno(mq_send(arg1, p, arg3, arg4));
  8309. unlock_user (p, arg2, arg3);
  8310. }
  8311. break;
  8312. case TARGET_NR_mq_timedreceive:
  8313. {
  8314. struct timespec ts;
  8315. unsigned int prio;
  8316. p = lock_user (VERIFY_READ, arg2, arg3, 1);
  8317. if (arg5 != 0) {
  8318. target_to_host_timespec(&ts, arg5);
  8319. ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
  8320. host_to_target_timespec(arg5, &ts);
  8321. }
  8322. else
  8323. ret = get_errno(mq_receive(arg1, p, arg3, &prio));
  8324. unlock_user (p, arg2, arg3);
  8325. if (arg4 != 0)
  8326. put_user_u32(prio, arg4);
  8327. }
  8328. break;
  8329. /* Not implemented for now... */
  8330. /* case TARGET_NR_mq_notify: */
  8331. /* break; */
  8332. case TARGET_NR_mq_getsetattr:
  8333. {
  8334. struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
  8335. ret = 0;
  8336. if (arg3 != 0) {
  8337. ret = mq_getattr(arg1, &posix_mq_attr_out);
  8338. copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
  8339. }
  8340. if (arg2 != 0) {
  8341. copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
  8342. ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
  8343. }
  8344. }
  8345. break;
  8346. #endif
  8347. #ifdef CONFIG_SPLICE
  8348. #ifdef TARGET_NR_tee
  8349. case TARGET_NR_tee:
  8350. {
  8351. ret = get_errno(tee(arg1,arg2,arg3,arg4));
  8352. }
  8353. break;
  8354. #endif
  8355. #ifdef TARGET_NR_splice
  8356. case TARGET_NR_splice:
  8357. {
  8358. loff_t loff_in, loff_out;
  8359. loff_t *ploff_in = NULL, *ploff_out = NULL;
  8360. if(arg2) {
  8361. get_user_u64(loff_in, arg2);
  8362. ploff_in = &loff_in;
  8363. }
  8364. if(arg4) {
  8365. get_user_u64(loff_out, arg2);
  8366. ploff_out = &loff_out;
  8367. }
  8368. ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
  8369. }
  8370. break;
  8371. #endif
  8372. #ifdef TARGET_NR_vmsplice
  8373. case TARGET_NR_vmsplice:
  8374. {
  8375. struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
  8376. if (vec != NULL) {
  8377. ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
  8378. unlock_iovec(vec, arg2, arg3, 0);
  8379. } else {
  8380. ret = -host_to_target_errno(errno);
  8381. }
  8382. }
  8383. break;
  8384. #endif
  8385. #endif /* CONFIG_SPLICE */
  8386. #ifdef CONFIG_EVENTFD
  8387. #if defined(TARGET_NR_eventfd)
  8388. case TARGET_NR_eventfd:
  8389. ret = get_errno(eventfd(arg1, 0));
  8390. break;
  8391. #endif
  8392. #if defined(TARGET_NR_eventfd2)
  8393. case TARGET_NR_eventfd2:
  8394. {
  8395. int host_flags = arg2 & (~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC));
  8396. if (arg2 & TARGET_O_NONBLOCK) {
  8397. host_flags |= O_NONBLOCK;
  8398. }
  8399. if (arg2 & TARGET_O_CLOEXEC) {
  8400. host_flags |= O_CLOEXEC;
  8401. }
  8402. ret = get_errno(eventfd(arg1, host_flags));
  8403. break;
  8404. }
  8405. #endif
  8406. #endif /* CONFIG_EVENTFD */
  8407. #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
  8408. case TARGET_NR_fallocate:
  8409. #if TARGET_ABI_BITS == 32
  8410. ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
  8411. target_offset64(arg5, arg6)));
  8412. #else
  8413. ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
  8414. #endif
  8415. break;
  8416. #endif
  8417. #if defined(CONFIG_SYNC_FILE_RANGE)
  8418. #if defined(TARGET_NR_sync_file_range)
  8419. case TARGET_NR_sync_file_range:
  8420. #if TARGET_ABI_BITS == 32
  8421. #if defined(TARGET_MIPS)
  8422. ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
  8423. target_offset64(arg5, arg6), arg7));
  8424. #else
  8425. ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
  8426. target_offset64(arg4, arg5), arg6));
  8427. #endif /* !TARGET_MIPS */
  8428. #else
  8429. ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
  8430. #endif
  8431. break;
  8432. #endif
  8433. #if defined(TARGET_NR_sync_file_range2)
  8434. case TARGET_NR_sync_file_range2:
  8435. /* This is like sync_file_range but the arguments are reordered */
  8436. #if TARGET_ABI_BITS == 32
  8437. ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
  8438. target_offset64(arg5, arg6), arg2));
  8439. #else
  8440. ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
  8441. #endif
  8442. break;
  8443. #endif
  8444. #endif
  8445. #if defined(CONFIG_EPOLL)
  8446. #if defined(TARGET_NR_epoll_create)
  8447. case TARGET_NR_epoll_create:
  8448. ret = get_errno(epoll_create(arg1));
  8449. break;
  8450. #endif
  8451. #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
  8452. case TARGET_NR_epoll_create1:
  8453. ret = get_errno(epoll_create1(arg1));
  8454. break;
  8455. #endif
  8456. #if defined(TARGET_NR_epoll_ctl)
  8457. case TARGET_NR_epoll_ctl:
  8458. {
  8459. struct epoll_event ep;
  8460. struct epoll_event *epp = 0;
  8461. if (arg4) {
  8462. struct target_epoll_event *target_ep;
  8463. if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
  8464. goto efault;
  8465. }
  8466. ep.events = tswap32(target_ep->events);
  8467. /* The epoll_data_t union is just opaque data to the kernel,
  8468. * so we transfer all 64 bits across and need not worry what
  8469. * actual data type it is.
  8470. */
  8471. ep.data.u64 = tswap64(target_ep->data.u64);
  8472. unlock_user_struct(target_ep, arg4, 0);
  8473. epp = &ep;
  8474. }
  8475. ret = get_errno(epoll_ctl(arg1, arg2, arg3, epp));
  8476. break;
  8477. }
  8478. #endif
  8479. #if defined(TARGET_NR_epoll_pwait) && defined(CONFIG_EPOLL_PWAIT)
  8480. #define IMPLEMENT_EPOLL_PWAIT
  8481. #endif
  8482. #if defined(TARGET_NR_epoll_wait) || defined(IMPLEMENT_EPOLL_PWAIT)
  8483. #if defined(TARGET_NR_epoll_wait)
  8484. case TARGET_NR_epoll_wait:
  8485. #endif
  8486. #if defined(IMPLEMENT_EPOLL_PWAIT)
  8487. case TARGET_NR_epoll_pwait:
  8488. #endif
  8489. {
  8490. struct target_epoll_event *target_ep;
  8491. struct epoll_event *ep;
  8492. int epfd = arg1;
  8493. int maxevents = arg3;
  8494. int timeout = arg4;
  8495. target_ep = lock_user(VERIFY_WRITE, arg2,
  8496. maxevents * sizeof(struct target_epoll_event), 1);
  8497. if (!target_ep) {
  8498. goto efault;
  8499. }
  8500. ep = alloca(maxevents * sizeof(struct epoll_event));
  8501. switch (num) {
  8502. #if defined(IMPLEMENT_EPOLL_PWAIT)
  8503. case TARGET_NR_epoll_pwait:
  8504. {
  8505. target_sigset_t *target_set;
  8506. sigset_t _set, *set = &_set;
  8507. if (arg5) {
  8508. target_set = lock_user(VERIFY_READ, arg5,
  8509. sizeof(target_sigset_t), 1);
  8510. if (!target_set) {
  8511. unlock_user(target_ep, arg2, 0);
  8512. goto efault;
  8513. }
  8514. target_to_host_sigset(set, target_set);
  8515. unlock_user(target_set, arg5, 0);
  8516. } else {
  8517. set = NULL;
  8518. }
  8519. ret = get_errno(epoll_pwait(epfd, ep, maxevents, timeout, set));
  8520. break;
  8521. }
  8522. #endif
  8523. #if defined(TARGET_NR_epoll_wait)
  8524. case TARGET_NR_epoll_wait:
  8525. ret = get_errno(epoll_wait(epfd, ep, maxevents, timeout));
  8526. break;
  8527. #endif
  8528. default:
  8529. ret = -TARGET_ENOSYS;
  8530. }
  8531. if (!is_error(ret)) {
  8532. int i;
  8533. for (i = 0; i < ret; i++) {
  8534. target_ep[i].events = tswap32(ep[i].events);
  8535. target_ep[i].data.u64 = tswap64(ep[i].data.u64);
  8536. }
  8537. }
  8538. unlock_user(target_ep, arg2, ret * sizeof(struct target_epoll_event));
  8539. break;
  8540. }
  8541. #endif
  8542. #endif
  8543. #ifdef TARGET_NR_prlimit64
  8544. case TARGET_NR_prlimit64:
  8545. {
  8546. /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
  8547. struct target_rlimit64 *target_rnew, *target_rold;
  8548. struct host_rlimit64 rnew, rold, *rnewp = 0;
  8549. if (arg3) {
  8550. if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
  8551. goto efault;
  8552. }
  8553. rnew.rlim_cur = tswap64(target_rnew->rlim_cur);
  8554. rnew.rlim_max = tswap64(target_rnew->rlim_max);
  8555. unlock_user_struct(target_rnew, arg3, 0);
  8556. rnewp = &rnew;
  8557. }
  8558. ret = get_errno(sys_prlimit64(arg1, arg2, rnewp, arg4 ? &rold : 0));
  8559. if (!is_error(ret) && arg4) {
  8560. if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
  8561. goto efault;
  8562. }
  8563. target_rold->rlim_cur = tswap64(rold.rlim_cur);
  8564. target_rold->rlim_max = tswap64(rold.rlim_max);
  8565. unlock_user_struct(target_rold, arg4, 1);
  8566. }
  8567. break;
  8568. }
  8569. #endif
  8570. #ifdef TARGET_NR_gethostname
  8571. case TARGET_NR_gethostname:
  8572. {
  8573. char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
  8574. if (name) {
  8575. ret = get_errno(gethostname(name, arg2));
  8576. unlock_user(name, arg1, arg2);
  8577. } else {
  8578. ret = -TARGET_EFAULT;
  8579. }
  8580. break;
  8581. }
  8582. #endif
  8583. #ifdef TARGET_NR_atomic_cmpxchg_32
  8584. case TARGET_NR_atomic_cmpxchg_32:
  8585. {
  8586. /* should use start_exclusive from main.c */
  8587. abi_ulong mem_value;
  8588. if (get_user_u32(mem_value, arg6)) {
  8589. target_siginfo_t info;
  8590. info.si_signo = SIGSEGV;
  8591. info.si_errno = 0;
  8592. info.si_code = TARGET_SEGV_MAPERR;
  8593. info._sifields._sigfault._addr = arg6;
  8594. queue_signal((CPUArchState *)cpu_env, info.si_signo, &info);
  8595. ret = 0xdeadbeef;
  8596. }
  8597. if (mem_value == arg2)
  8598. put_user_u32(arg1, arg6);
  8599. ret = mem_value;
  8600. break;
  8601. }
  8602. #endif
  8603. #ifdef TARGET_NR_atomic_barrier
  8604. case TARGET_NR_atomic_barrier:
  8605. {
  8606. /* Like the kernel implementation and the qemu arm barrier, no-op this? */
  8607. ret = 0;
  8608. break;
  8609. }
  8610. #endif
  8611. #ifdef TARGET_NR_timer_create
  8612. case TARGET_NR_timer_create:
  8613. {
  8614. /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
  8615. struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL;
  8616. struct target_sigevent *ptarget_sevp;
  8617. struct target_timer_t *ptarget_timer;
  8618. int clkid = arg1;
  8619. int timer_index = next_free_host_timer();
  8620. if (timer_index < 0) {
  8621. ret = -TARGET_EAGAIN;
  8622. } else {
  8623. timer_t *phtimer = g_posix_timers + timer_index;
  8624. if (arg2) {
  8625. if (!lock_user_struct(VERIFY_READ, ptarget_sevp, arg2, 1)) {
  8626. goto efault;
  8627. }
  8628. host_sevp.sigev_signo = tswap32(ptarget_sevp->sigev_signo);
  8629. host_sevp.sigev_notify = tswap32(ptarget_sevp->sigev_notify);
  8630. phost_sevp = &host_sevp;
  8631. }
  8632. ret = get_errno(timer_create(clkid, phost_sevp, phtimer));
  8633. if (ret) {
  8634. phtimer = NULL;
  8635. } else {
  8636. if (!lock_user_struct(VERIFY_WRITE, ptarget_timer, arg3, 1)) {
  8637. goto efault;
  8638. }
  8639. ptarget_timer->ptr = tswap32(0xcafe0000 | timer_index);
  8640. unlock_user_struct(ptarget_timer, arg3, 1);
  8641. }
  8642. }
  8643. break;
  8644. }
  8645. #endif
  8646. #ifdef TARGET_NR_timer_settime
  8647. case TARGET_NR_timer_settime:
  8648. {
  8649. /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
  8650. * struct itimerspec * old_value */
  8651. arg1 &= 0xffff;
  8652. if (arg3 == 0 || arg1 < 0 || arg1 >= ARRAY_SIZE(g_posix_timers)) {
  8653. ret = -TARGET_EINVAL;
  8654. } else {
  8655. timer_t htimer = g_posix_timers[arg1];
  8656. struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
  8657. target_to_host_itimerspec(&hspec_new, arg3);
  8658. ret = get_errno(
  8659. timer_settime(htimer, arg2, &hspec_new, &hspec_old));
  8660. host_to_target_itimerspec(arg2, &hspec_old);
  8661. }
  8662. break;
  8663. }
  8664. #endif
  8665. #ifdef TARGET_NR_timer_gettime
  8666. case TARGET_NR_timer_gettime:
  8667. {
  8668. /* args: timer_t timerid, struct itimerspec *curr_value */
  8669. arg1 &= 0xffff;
  8670. if (!arg2) {
  8671. return -TARGET_EFAULT;
  8672. } else if (arg1 < 0 || arg1 >= ARRAY_SIZE(g_posix_timers)) {
  8673. ret = -TARGET_EINVAL;
  8674. } else {
  8675. timer_t htimer = g_posix_timers[arg1];
  8676. struct itimerspec hspec;
  8677. ret = get_errno(timer_gettime(htimer, &hspec));
  8678. if (host_to_target_itimerspec(arg2, &hspec)) {
  8679. ret = -TARGET_EFAULT;
  8680. }
  8681. }
  8682. break;
  8683. }
  8684. #endif
  8685. #ifdef TARGET_NR_timer_getoverrun
  8686. case TARGET_NR_timer_getoverrun:
  8687. {
  8688. /* args: timer_t timerid */
  8689. arg1 &= 0xffff;
  8690. if (arg1 < 0 || arg1 >= ARRAY_SIZE(g_posix_timers)) {
  8691. ret = -TARGET_EINVAL;
  8692. } else {
  8693. timer_t htimer = g_posix_timers[arg1];
  8694. ret = get_errno(timer_getoverrun(htimer));
  8695. }
  8696. break;
  8697. }
  8698. #endif
  8699. #ifdef TARGET_NR_timer_delete
  8700. case TARGET_NR_timer_delete:
  8701. {
  8702. /* args: timer_t timerid */
  8703. arg1 &= 0xffff;
  8704. if (arg1 < 0 || arg1 >= ARRAY_SIZE(g_posix_timers)) {
  8705. ret = -TARGET_EINVAL;
  8706. } else {
  8707. timer_t htimer = g_posix_timers[arg1];
  8708. ret = get_errno(timer_delete(htimer));
  8709. g_posix_timers[arg1] = 0;
  8710. }
  8711. break;
  8712. }
  8713. #endif
  8714. default:
  8715. unimplemented:
  8716. gemu_log("qemu: Unsupported syscall: %d\n", num);
  8717. #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
  8718. unimplemented_nowarn:
  8719. #endif
  8720. ret = -TARGET_ENOSYS;
  8721. break;
  8722. }
  8723. fail:
  8724. #ifdef DEBUG
  8725. gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
  8726. #endif
  8727. if(do_strace)
  8728. print_syscall_ret(num, ret);
  8729. return ret;
  8730. efault:
  8731. ret = -TARGET_EFAULT;
  8732. goto fail;
  8733. }