syscall.c 269 KB

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