ptrace.c 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381
  1. /*
  2. * PowerPC version
  3. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  4. *
  5. * Derived from "arch/m68k/kernel/ptrace.c"
  6. * Copyright (C) 1994 by Hamish Macdonald
  7. * Taken from linux/kernel/ptrace.c and modified for M680x0.
  8. * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
  9. *
  10. * Modified by Cort Dougan (cort@hq.fsmlabs.com)
  11. * and Paul Mackerras (paulus@samba.org).
  12. *
  13. * This file is subject to the terms and conditions of the GNU General
  14. * Public License. See the file README.legal in the main directory of
  15. * this archive for more details.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/sched.h>
  19. #include <linux/mm.h>
  20. #include <linux/smp.h>
  21. #include <linux/errno.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/regset.h>
  24. #include <linux/tracehook.h>
  25. #include <linux/elf.h>
  26. #include <linux/user.h>
  27. #include <linux/security.h>
  28. #include <linux/signal.h>
  29. #include <linux/seccomp.h>
  30. #include <linux/audit.h>
  31. #include <trace/syscall.h>
  32. #include <linux/hw_breakpoint.h>
  33. #include <linux/perf_event.h>
  34. #include <linux/context_tracking.h>
  35. #include <linux/uaccess.h>
  36. #include <linux/pkeys.h>
  37. #include <asm/page.h>
  38. #include <asm/pgtable.h>
  39. #include <asm/switch_to.h>
  40. #include <asm/tm.h>
  41. #include <asm/asm-prototypes.h>
  42. #include <asm/debug.h>
  43. #define CREATE_TRACE_POINTS
  44. #include <trace/events/syscalls.h>
  45. /*
  46. * The parameter save area on the stack is used to store arguments being passed
  47. * to callee function and is located at fixed offset from stack pointer.
  48. */
  49. #ifdef CONFIG_PPC32
  50. #define PARAMETER_SAVE_AREA_OFFSET 24 /* bytes */
  51. #else /* CONFIG_PPC32 */
  52. #define PARAMETER_SAVE_AREA_OFFSET 48 /* bytes */
  53. #endif
  54. struct pt_regs_offset {
  55. const char *name;
  56. int offset;
  57. };
  58. #define STR(s) #s /* convert to string */
  59. #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
  60. #define GPR_OFFSET_NAME(num) \
  61. {.name = STR(r##num), .offset = offsetof(struct pt_regs, gpr[num])}, \
  62. {.name = STR(gpr##num), .offset = offsetof(struct pt_regs, gpr[num])}
  63. #define REG_OFFSET_END {.name = NULL, .offset = 0}
  64. #define TVSO(f) (offsetof(struct thread_vr_state, f))
  65. #define TFSO(f) (offsetof(struct thread_fp_state, f))
  66. #define TSO(f) (offsetof(struct thread_struct, f))
  67. static const struct pt_regs_offset regoffset_table[] = {
  68. GPR_OFFSET_NAME(0),
  69. GPR_OFFSET_NAME(1),
  70. GPR_OFFSET_NAME(2),
  71. GPR_OFFSET_NAME(3),
  72. GPR_OFFSET_NAME(4),
  73. GPR_OFFSET_NAME(5),
  74. GPR_OFFSET_NAME(6),
  75. GPR_OFFSET_NAME(7),
  76. GPR_OFFSET_NAME(8),
  77. GPR_OFFSET_NAME(9),
  78. GPR_OFFSET_NAME(10),
  79. GPR_OFFSET_NAME(11),
  80. GPR_OFFSET_NAME(12),
  81. GPR_OFFSET_NAME(13),
  82. GPR_OFFSET_NAME(14),
  83. GPR_OFFSET_NAME(15),
  84. GPR_OFFSET_NAME(16),
  85. GPR_OFFSET_NAME(17),
  86. GPR_OFFSET_NAME(18),
  87. GPR_OFFSET_NAME(19),
  88. GPR_OFFSET_NAME(20),
  89. GPR_OFFSET_NAME(21),
  90. GPR_OFFSET_NAME(22),
  91. GPR_OFFSET_NAME(23),
  92. GPR_OFFSET_NAME(24),
  93. GPR_OFFSET_NAME(25),
  94. GPR_OFFSET_NAME(26),
  95. GPR_OFFSET_NAME(27),
  96. GPR_OFFSET_NAME(28),
  97. GPR_OFFSET_NAME(29),
  98. GPR_OFFSET_NAME(30),
  99. GPR_OFFSET_NAME(31),
  100. REG_OFFSET_NAME(nip),
  101. REG_OFFSET_NAME(msr),
  102. REG_OFFSET_NAME(ctr),
  103. REG_OFFSET_NAME(link),
  104. REG_OFFSET_NAME(xer),
  105. REG_OFFSET_NAME(ccr),
  106. #ifdef CONFIG_PPC64
  107. REG_OFFSET_NAME(softe),
  108. #else
  109. REG_OFFSET_NAME(mq),
  110. #endif
  111. REG_OFFSET_NAME(trap),
  112. REG_OFFSET_NAME(dar),
  113. REG_OFFSET_NAME(dsisr),
  114. REG_OFFSET_END,
  115. };
  116. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  117. static void flush_tmregs_to_thread(struct task_struct *tsk)
  118. {
  119. /*
  120. * If task is not current, it will have been flushed already to
  121. * it's thread_struct during __switch_to().
  122. *
  123. * A reclaim flushes ALL the state or if not in TM save TM SPRs
  124. * in the appropriate thread structures from live.
  125. */
  126. if ((!cpu_has_feature(CPU_FTR_TM)) || (tsk != current))
  127. return;
  128. if (MSR_TM_SUSPENDED(mfmsr())) {
  129. tm_reclaim_current(TM_CAUSE_SIGNAL);
  130. } else {
  131. tm_enable();
  132. tm_save_sprs(&(tsk->thread));
  133. }
  134. }
  135. #else
  136. static inline void flush_tmregs_to_thread(struct task_struct *tsk) { }
  137. #endif
  138. /**
  139. * regs_query_register_offset() - query register offset from its name
  140. * @name: the name of a register
  141. *
  142. * regs_query_register_offset() returns the offset of a register in struct
  143. * pt_regs from its name. If the name is invalid, this returns -EINVAL;
  144. */
  145. int regs_query_register_offset(const char *name)
  146. {
  147. const struct pt_regs_offset *roff;
  148. for (roff = regoffset_table; roff->name != NULL; roff++)
  149. if (!strcmp(roff->name, name))
  150. return roff->offset;
  151. return -EINVAL;
  152. }
  153. /**
  154. * regs_query_register_name() - query register name from its offset
  155. * @offset: the offset of a register in struct pt_regs.
  156. *
  157. * regs_query_register_name() returns the name of a register from its
  158. * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
  159. */
  160. const char *regs_query_register_name(unsigned int offset)
  161. {
  162. const struct pt_regs_offset *roff;
  163. for (roff = regoffset_table; roff->name != NULL; roff++)
  164. if (roff->offset == offset)
  165. return roff->name;
  166. return NULL;
  167. }
  168. /*
  169. * does not yet catch signals sent when the child dies.
  170. * in exit.c or in signal.c.
  171. */
  172. /*
  173. * Set of msr bits that gdb can change on behalf of a process.
  174. */
  175. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  176. #define MSR_DEBUGCHANGE 0
  177. #else
  178. #define MSR_DEBUGCHANGE (MSR_SE | MSR_BE)
  179. #endif
  180. /*
  181. * Max register writeable via put_reg
  182. */
  183. #ifdef CONFIG_PPC32
  184. #define PT_MAX_PUT_REG PT_MQ
  185. #else
  186. #define PT_MAX_PUT_REG PT_CCR
  187. #endif
  188. static unsigned long get_user_msr(struct task_struct *task)
  189. {
  190. return task->thread.regs->msr | task->thread.fpexc_mode;
  191. }
  192. static int set_user_msr(struct task_struct *task, unsigned long msr)
  193. {
  194. task->thread.regs->msr &= ~MSR_DEBUGCHANGE;
  195. task->thread.regs->msr |= msr & MSR_DEBUGCHANGE;
  196. return 0;
  197. }
  198. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  199. static unsigned long get_user_ckpt_msr(struct task_struct *task)
  200. {
  201. return task->thread.ckpt_regs.msr | task->thread.fpexc_mode;
  202. }
  203. static int set_user_ckpt_msr(struct task_struct *task, unsigned long msr)
  204. {
  205. task->thread.ckpt_regs.msr &= ~MSR_DEBUGCHANGE;
  206. task->thread.ckpt_regs.msr |= msr & MSR_DEBUGCHANGE;
  207. return 0;
  208. }
  209. static int set_user_ckpt_trap(struct task_struct *task, unsigned long trap)
  210. {
  211. task->thread.ckpt_regs.trap = trap & 0xfff0;
  212. return 0;
  213. }
  214. #endif
  215. #ifdef CONFIG_PPC64
  216. static int get_user_dscr(struct task_struct *task, unsigned long *data)
  217. {
  218. *data = task->thread.dscr;
  219. return 0;
  220. }
  221. static int set_user_dscr(struct task_struct *task, unsigned long dscr)
  222. {
  223. task->thread.dscr = dscr;
  224. task->thread.dscr_inherit = 1;
  225. return 0;
  226. }
  227. #else
  228. static int get_user_dscr(struct task_struct *task, unsigned long *data)
  229. {
  230. return -EIO;
  231. }
  232. static int set_user_dscr(struct task_struct *task, unsigned long dscr)
  233. {
  234. return -EIO;
  235. }
  236. #endif
  237. /*
  238. * We prevent mucking around with the reserved area of trap
  239. * which are used internally by the kernel.
  240. */
  241. static int set_user_trap(struct task_struct *task, unsigned long trap)
  242. {
  243. task->thread.regs->trap = trap & 0xfff0;
  244. return 0;
  245. }
  246. /*
  247. * Get contents of register REGNO in task TASK.
  248. */
  249. int ptrace_get_reg(struct task_struct *task, int regno, unsigned long *data)
  250. {
  251. if ((task->thread.regs == NULL) || !data)
  252. return -EIO;
  253. if (regno == PT_MSR) {
  254. *data = get_user_msr(task);
  255. return 0;
  256. }
  257. if (regno == PT_DSCR)
  258. return get_user_dscr(task, data);
  259. #ifdef CONFIG_PPC64
  260. /*
  261. * softe copies paca->irq_soft_mask variable state. Since irq_soft_mask is
  262. * no more used as a flag, lets force usr to alway see the softe value as 1
  263. * which means interrupts are not soft disabled.
  264. */
  265. if (regno == PT_SOFTE) {
  266. *data = 1;
  267. return 0;
  268. }
  269. #endif
  270. if (regno < (sizeof(struct user_pt_regs) / sizeof(unsigned long))) {
  271. *data = ((unsigned long *)task->thread.regs)[regno];
  272. return 0;
  273. }
  274. return -EIO;
  275. }
  276. /*
  277. * Write contents of register REGNO in task TASK.
  278. */
  279. int ptrace_put_reg(struct task_struct *task, int regno, unsigned long data)
  280. {
  281. if (task->thread.regs == NULL)
  282. return -EIO;
  283. if (regno == PT_MSR)
  284. return set_user_msr(task, data);
  285. if (regno == PT_TRAP)
  286. return set_user_trap(task, data);
  287. if (regno == PT_DSCR)
  288. return set_user_dscr(task, data);
  289. if (regno <= PT_MAX_PUT_REG) {
  290. ((unsigned long *)task->thread.regs)[regno] = data;
  291. return 0;
  292. }
  293. return -EIO;
  294. }
  295. static int gpr_get(struct task_struct *target, const struct user_regset *regset,
  296. unsigned int pos, unsigned int count,
  297. void *kbuf, void __user *ubuf)
  298. {
  299. int i, ret;
  300. if (target->thread.regs == NULL)
  301. return -EIO;
  302. if (!FULL_REGS(target->thread.regs)) {
  303. /* We have a partial register set. Fill 14-31 with bogus values */
  304. for (i = 14; i < 32; i++)
  305. target->thread.regs->gpr[i] = NV_REG_POISON;
  306. }
  307. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  308. target->thread.regs,
  309. 0, offsetof(struct pt_regs, msr));
  310. if (!ret) {
  311. unsigned long msr = get_user_msr(target);
  312. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &msr,
  313. offsetof(struct pt_regs, msr),
  314. offsetof(struct pt_regs, msr) +
  315. sizeof(msr));
  316. }
  317. BUILD_BUG_ON(offsetof(struct pt_regs, orig_gpr3) !=
  318. offsetof(struct pt_regs, msr) + sizeof(long));
  319. if (!ret)
  320. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  321. &target->thread.regs->orig_gpr3,
  322. offsetof(struct pt_regs, orig_gpr3),
  323. sizeof(struct user_pt_regs));
  324. if (!ret)
  325. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  326. sizeof(struct user_pt_regs), -1);
  327. return ret;
  328. }
  329. static int gpr_set(struct task_struct *target, const struct user_regset *regset,
  330. unsigned int pos, unsigned int count,
  331. const void *kbuf, const void __user *ubuf)
  332. {
  333. unsigned long reg;
  334. int ret;
  335. if (target->thread.regs == NULL)
  336. return -EIO;
  337. CHECK_FULL_REGS(target->thread.regs);
  338. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  339. target->thread.regs,
  340. 0, PT_MSR * sizeof(reg));
  341. if (!ret && count > 0) {
  342. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &reg,
  343. PT_MSR * sizeof(reg),
  344. (PT_MSR + 1) * sizeof(reg));
  345. if (!ret)
  346. ret = set_user_msr(target, reg);
  347. }
  348. BUILD_BUG_ON(offsetof(struct pt_regs, orig_gpr3) !=
  349. offsetof(struct pt_regs, msr) + sizeof(long));
  350. if (!ret)
  351. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  352. &target->thread.regs->orig_gpr3,
  353. PT_ORIG_R3 * sizeof(reg),
  354. (PT_MAX_PUT_REG + 1) * sizeof(reg));
  355. if (PT_MAX_PUT_REG + 1 < PT_TRAP && !ret)
  356. ret = user_regset_copyin_ignore(
  357. &pos, &count, &kbuf, &ubuf,
  358. (PT_MAX_PUT_REG + 1) * sizeof(reg),
  359. PT_TRAP * sizeof(reg));
  360. if (!ret && count > 0) {
  361. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &reg,
  362. PT_TRAP * sizeof(reg),
  363. (PT_TRAP + 1) * sizeof(reg));
  364. if (!ret)
  365. ret = set_user_trap(target, reg);
  366. }
  367. if (!ret)
  368. ret = user_regset_copyin_ignore(
  369. &pos, &count, &kbuf, &ubuf,
  370. (PT_TRAP + 1) * sizeof(reg), -1);
  371. return ret;
  372. }
  373. /*
  374. * Regardless of transactions, 'fp_state' holds the current running
  375. * value of all FPR registers and 'ckfp_state' holds the last checkpointed
  376. * value of all FPR registers for the current transaction.
  377. *
  378. * Userspace interface buffer layout:
  379. *
  380. * struct data {
  381. * u64 fpr[32];
  382. * u64 fpscr;
  383. * };
  384. */
  385. static int fpr_get(struct task_struct *target, const struct user_regset *regset,
  386. unsigned int pos, unsigned int count,
  387. void *kbuf, void __user *ubuf)
  388. {
  389. #ifdef CONFIG_VSX
  390. u64 buf[33];
  391. int i;
  392. flush_fp_to_thread(target);
  393. /* copy to local buffer then write that out */
  394. for (i = 0; i < 32 ; i++)
  395. buf[i] = target->thread.TS_FPR(i);
  396. buf[32] = target->thread.fp_state.fpscr;
  397. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, buf, 0, -1);
  398. #else
  399. BUILD_BUG_ON(offsetof(struct thread_fp_state, fpscr) !=
  400. offsetof(struct thread_fp_state, fpr[32]));
  401. flush_fp_to_thread(target);
  402. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  403. &target->thread.fp_state, 0, -1);
  404. #endif
  405. }
  406. /*
  407. * Regardless of transactions, 'fp_state' holds the current running
  408. * value of all FPR registers and 'ckfp_state' holds the last checkpointed
  409. * value of all FPR registers for the current transaction.
  410. *
  411. * Userspace interface buffer layout:
  412. *
  413. * struct data {
  414. * u64 fpr[32];
  415. * u64 fpscr;
  416. * };
  417. *
  418. */
  419. static int fpr_set(struct task_struct *target, const struct user_regset *regset,
  420. unsigned int pos, unsigned int count,
  421. const void *kbuf, const void __user *ubuf)
  422. {
  423. #ifdef CONFIG_VSX
  424. u64 buf[33];
  425. int i;
  426. flush_fp_to_thread(target);
  427. for (i = 0; i < 32 ; i++)
  428. buf[i] = target->thread.TS_FPR(i);
  429. buf[32] = target->thread.fp_state.fpscr;
  430. /* copy to local buffer then write that out */
  431. i = user_regset_copyin(&pos, &count, &kbuf, &ubuf, buf, 0, -1);
  432. if (i)
  433. return i;
  434. for (i = 0; i < 32 ; i++)
  435. target->thread.TS_FPR(i) = buf[i];
  436. target->thread.fp_state.fpscr = buf[32];
  437. return 0;
  438. #else
  439. BUILD_BUG_ON(offsetof(struct thread_fp_state, fpscr) !=
  440. offsetof(struct thread_fp_state, fpr[32]));
  441. flush_fp_to_thread(target);
  442. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  443. &target->thread.fp_state, 0, -1);
  444. #endif
  445. }
  446. #ifdef CONFIG_ALTIVEC
  447. /*
  448. * Get/set all the altivec registers vr0..vr31, vscr, vrsave, in one go.
  449. * The transfer totals 34 quadword. Quadwords 0-31 contain the
  450. * corresponding vector registers. Quadword 32 contains the vscr as the
  451. * last word (offset 12) within that quadword. Quadword 33 contains the
  452. * vrsave as the first word (offset 0) within the quadword.
  453. *
  454. * This definition of the VMX state is compatible with the current PPC32
  455. * ptrace interface. This allows signal handling and ptrace to use the
  456. * same structures. This also simplifies the implementation of a bi-arch
  457. * (combined (32- and 64-bit) gdb.
  458. */
  459. static int vr_active(struct task_struct *target,
  460. const struct user_regset *regset)
  461. {
  462. flush_altivec_to_thread(target);
  463. return target->thread.used_vr ? regset->n : 0;
  464. }
  465. /*
  466. * Regardless of transactions, 'vr_state' holds the current running
  467. * value of all the VMX registers and 'ckvr_state' holds the last
  468. * checkpointed value of all the VMX registers for the current
  469. * transaction to fall back on in case it aborts.
  470. *
  471. * Userspace interface buffer layout:
  472. *
  473. * struct data {
  474. * vector128 vr[32];
  475. * vector128 vscr;
  476. * vector128 vrsave;
  477. * };
  478. */
  479. static int vr_get(struct task_struct *target, const struct user_regset *regset,
  480. unsigned int pos, unsigned int count,
  481. void *kbuf, void __user *ubuf)
  482. {
  483. int ret;
  484. flush_altivec_to_thread(target);
  485. BUILD_BUG_ON(offsetof(struct thread_vr_state, vscr) !=
  486. offsetof(struct thread_vr_state, vr[32]));
  487. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  488. &target->thread.vr_state, 0,
  489. 33 * sizeof(vector128));
  490. if (!ret) {
  491. /*
  492. * Copy out only the low-order word of vrsave.
  493. */
  494. union {
  495. elf_vrreg_t reg;
  496. u32 word;
  497. } vrsave;
  498. memset(&vrsave, 0, sizeof(vrsave));
  499. vrsave.word = target->thread.vrsave;
  500. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &vrsave,
  501. 33 * sizeof(vector128), -1);
  502. }
  503. return ret;
  504. }
  505. /*
  506. * Regardless of transactions, 'vr_state' holds the current running
  507. * value of all the VMX registers and 'ckvr_state' holds the last
  508. * checkpointed value of all the VMX registers for the current
  509. * transaction to fall back on in case it aborts.
  510. *
  511. * Userspace interface buffer layout:
  512. *
  513. * struct data {
  514. * vector128 vr[32];
  515. * vector128 vscr;
  516. * vector128 vrsave;
  517. * };
  518. */
  519. static int vr_set(struct task_struct *target, const struct user_regset *regset,
  520. unsigned int pos, unsigned int count,
  521. const void *kbuf, const void __user *ubuf)
  522. {
  523. int ret;
  524. flush_altivec_to_thread(target);
  525. BUILD_BUG_ON(offsetof(struct thread_vr_state, vscr) !=
  526. offsetof(struct thread_vr_state, vr[32]));
  527. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  528. &target->thread.vr_state, 0,
  529. 33 * sizeof(vector128));
  530. if (!ret && count > 0) {
  531. /*
  532. * We use only the first word of vrsave.
  533. */
  534. union {
  535. elf_vrreg_t reg;
  536. u32 word;
  537. } vrsave;
  538. memset(&vrsave, 0, sizeof(vrsave));
  539. vrsave.word = target->thread.vrsave;
  540. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &vrsave,
  541. 33 * sizeof(vector128), -1);
  542. if (!ret)
  543. target->thread.vrsave = vrsave.word;
  544. }
  545. return ret;
  546. }
  547. #endif /* CONFIG_ALTIVEC */
  548. #ifdef CONFIG_VSX
  549. /*
  550. * Currently to set and and get all the vsx state, you need to call
  551. * the fp and VMX calls as well. This only get/sets the lower 32
  552. * 128bit VSX registers.
  553. */
  554. static int vsr_active(struct task_struct *target,
  555. const struct user_regset *regset)
  556. {
  557. flush_vsx_to_thread(target);
  558. return target->thread.used_vsr ? regset->n : 0;
  559. }
  560. /*
  561. * Regardless of transactions, 'fp_state' holds the current running
  562. * value of all FPR registers and 'ckfp_state' holds the last
  563. * checkpointed value of all FPR registers for the current
  564. * transaction.
  565. *
  566. * Userspace interface buffer layout:
  567. *
  568. * struct data {
  569. * u64 vsx[32];
  570. * };
  571. */
  572. static int vsr_get(struct task_struct *target, const struct user_regset *regset,
  573. unsigned int pos, unsigned int count,
  574. void *kbuf, void __user *ubuf)
  575. {
  576. u64 buf[32];
  577. int ret, i;
  578. flush_tmregs_to_thread(target);
  579. flush_fp_to_thread(target);
  580. flush_altivec_to_thread(target);
  581. flush_vsx_to_thread(target);
  582. for (i = 0; i < 32 ; i++)
  583. buf[i] = target->thread.fp_state.fpr[i][TS_VSRLOWOFFSET];
  584. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  585. buf, 0, 32 * sizeof(double));
  586. return ret;
  587. }
  588. /*
  589. * Regardless of transactions, 'fp_state' holds the current running
  590. * value of all FPR registers and 'ckfp_state' holds the last
  591. * checkpointed value of all FPR registers for the current
  592. * transaction.
  593. *
  594. * Userspace interface buffer layout:
  595. *
  596. * struct data {
  597. * u64 vsx[32];
  598. * };
  599. */
  600. static int vsr_set(struct task_struct *target, const struct user_regset *regset,
  601. unsigned int pos, unsigned int count,
  602. const void *kbuf, const void __user *ubuf)
  603. {
  604. u64 buf[32];
  605. int ret,i;
  606. flush_tmregs_to_thread(target);
  607. flush_fp_to_thread(target);
  608. flush_altivec_to_thread(target);
  609. flush_vsx_to_thread(target);
  610. for (i = 0; i < 32 ; i++)
  611. buf[i] = target->thread.fp_state.fpr[i][TS_VSRLOWOFFSET];
  612. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  613. buf, 0, 32 * sizeof(double));
  614. if (!ret)
  615. for (i = 0; i < 32 ; i++)
  616. target->thread.fp_state.fpr[i][TS_VSRLOWOFFSET] = buf[i];
  617. return ret;
  618. }
  619. #endif /* CONFIG_VSX */
  620. #ifdef CONFIG_SPE
  621. /*
  622. * For get_evrregs/set_evrregs functions 'data' has the following layout:
  623. *
  624. * struct {
  625. * u32 evr[32];
  626. * u64 acc;
  627. * u32 spefscr;
  628. * }
  629. */
  630. static int evr_active(struct task_struct *target,
  631. const struct user_regset *regset)
  632. {
  633. flush_spe_to_thread(target);
  634. return target->thread.used_spe ? regset->n : 0;
  635. }
  636. static int evr_get(struct task_struct *target, const struct user_regset *regset,
  637. unsigned int pos, unsigned int count,
  638. void *kbuf, void __user *ubuf)
  639. {
  640. int ret;
  641. flush_spe_to_thread(target);
  642. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  643. &target->thread.evr,
  644. 0, sizeof(target->thread.evr));
  645. BUILD_BUG_ON(offsetof(struct thread_struct, acc) + sizeof(u64) !=
  646. offsetof(struct thread_struct, spefscr));
  647. if (!ret)
  648. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  649. &target->thread.acc,
  650. sizeof(target->thread.evr), -1);
  651. return ret;
  652. }
  653. static int evr_set(struct task_struct *target, const struct user_regset *regset,
  654. unsigned int pos, unsigned int count,
  655. const void *kbuf, const void __user *ubuf)
  656. {
  657. int ret;
  658. flush_spe_to_thread(target);
  659. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  660. &target->thread.evr,
  661. 0, sizeof(target->thread.evr));
  662. BUILD_BUG_ON(offsetof(struct thread_struct, acc) + sizeof(u64) !=
  663. offsetof(struct thread_struct, spefscr));
  664. if (!ret)
  665. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  666. &target->thread.acc,
  667. sizeof(target->thread.evr), -1);
  668. return ret;
  669. }
  670. #endif /* CONFIG_SPE */
  671. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  672. /**
  673. * tm_cgpr_active - get active number of registers in CGPR
  674. * @target: The target task.
  675. * @regset: The user regset structure.
  676. *
  677. * This function checks for the active number of available
  678. * regisers in transaction checkpointed GPR category.
  679. */
  680. static int tm_cgpr_active(struct task_struct *target,
  681. const struct user_regset *regset)
  682. {
  683. if (!cpu_has_feature(CPU_FTR_TM))
  684. return -ENODEV;
  685. if (!MSR_TM_ACTIVE(target->thread.regs->msr))
  686. return 0;
  687. return regset->n;
  688. }
  689. /**
  690. * tm_cgpr_get - get CGPR registers
  691. * @target: The target task.
  692. * @regset: The user regset structure.
  693. * @pos: The buffer position.
  694. * @count: Number of bytes to copy.
  695. * @kbuf: Kernel buffer to copy from.
  696. * @ubuf: User buffer to copy into.
  697. *
  698. * This function gets transaction checkpointed GPR registers.
  699. *
  700. * When the transaction is active, 'ckpt_regs' holds all the checkpointed
  701. * GPR register values for the current transaction to fall back on if it
  702. * aborts in between. This function gets those checkpointed GPR registers.
  703. * The userspace interface buffer layout is as follows.
  704. *
  705. * struct data {
  706. * struct pt_regs ckpt_regs;
  707. * };
  708. */
  709. static int tm_cgpr_get(struct task_struct *target,
  710. const struct user_regset *regset,
  711. unsigned int pos, unsigned int count,
  712. void *kbuf, void __user *ubuf)
  713. {
  714. int ret;
  715. if (!cpu_has_feature(CPU_FTR_TM))
  716. return -ENODEV;
  717. if (!MSR_TM_ACTIVE(target->thread.regs->msr))
  718. return -ENODATA;
  719. flush_tmregs_to_thread(target);
  720. flush_fp_to_thread(target);
  721. flush_altivec_to_thread(target);
  722. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  723. &target->thread.ckpt_regs,
  724. 0, offsetof(struct pt_regs, msr));
  725. if (!ret) {
  726. unsigned long msr = get_user_ckpt_msr(target);
  727. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &msr,
  728. offsetof(struct pt_regs, msr),
  729. offsetof(struct pt_regs, msr) +
  730. sizeof(msr));
  731. }
  732. BUILD_BUG_ON(offsetof(struct pt_regs, orig_gpr3) !=
  733. offsetof(struct pt_regs, msr) + sizeof(long));
  734. if (!ret)
  735. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  736. &target->thread.ckpt_regs.orig_gpr3,
  737. offsetof(struct pt_regs, orig_gpr3),
  738. sizeof(struct user_pt_regs));
  739. if (!ret)
  740. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  741. sizeof(struct user_pt_regs), -1);
  742. return ret;
  743. }
  744. /*
  745. * tm_cgpr_set - set the CGPR registers
  746. * @target: The target task.
  747. * @regset: The user regset structure.
  748. * @pos: The buffer position.
  749. * @count: Number of bytes to copy.
  750. * @kbuf: Kernel buffer to copy into.
  751. * @ubuf: User buffer to copy from.
  752. *
  753. * This function sets in transaction checkpointed GPR registers.
  754. *
  755. * When the transaction is active, 'ckpt_regs' holds the checkpointed
  756. * GPR register values for the current transaction to fall back on if it
  757. * aborts in between. This function sets those checkpointed GPR registers.
  758. * The userspace interface buffer layout is as follows.
  759. *
  760. * struct data {
  761. * struct pt_regs ckpt_regs;
  762. * };
  763. */
  764. static int tm_cgpr_set(struct task_struct *target,
  765. const struct user_regset *regset,
  766. unsigned int pos, unsigned int count,
  767. const void *kbuf, const void __user *ubuf)
  768. {
  769. unsigned long reg;
  770. int ret;
  771. if (!cpu_has_feature(CPU_FTR_TM))
  772. return -ENODEV;
  773. if (!MSR_TM_ACTIVE(target->thread.regs->msr))
  774. return -ENODATA;
  775. flush_tmregs_to_thread(target);
  776. flush_fp_to_thread(target);
  777. flush_altivec_to_thread(target);
  778. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  779. &target->thread.ckpt_regs,
  780. 0, PT_MSR * sizeof(reg));
  781. if (!ret && count > 0) {
  782. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &reg,
  783. PT_MSR * sizeof(reg),
  784. (PT_MSR + 1) * sizeof(reg));
  785. if (!ret)
  786. ret = set_user_ckpt_msr(target, reg);
  787. }
  788. BUILD_BUG_ON(offsetof(struct pt_regs, orig_gpr3) !=
  789. offsetof(struct pt_regs, msr) + sizeof(long));
  790. if (!ret)
  791. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  792. &target->thread.ckpt_regs.orig_gpr3,
  793. PT_ORIG_R3 * sizeof(reg),
  794. (PT_MAX_PUT_REG + 1) * sizeof(reg));
  795. if (PT_MAX_PUT_REG + 1 < PT_TRAP && !ret)
  796. ret = user_regset_copyin_ignore(
  797. &pos, &count, &kbuf, &ubuf,
  798. (PT_MAX_PUT_REG + 1) * sizeof(reg),
  799. PT_TRAP * sizeof(reg));
  800. if (!ret && count > 0) {
  801. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &reg,
  802. PT_TRAP * sizeof(reg),
  803. (PT_TRAP + 1) * sizeof(reg));
  804. if (!ret)
  805. ret = set_user_ckpt_trap(target, reg);
  806. }
  807. if (!ret)
  808. ret = user_regset_copyin_ignore(
  809. &pos, &count, &kbuf, &ubuf,
  810. (PT_TRAP + 1) * sizeof(reg), -1);
  811. return ret;
  812. }
  813. /**
  814. * tm_cfpr_active - get active number of registers in CFPR
  815. * @target: The target task.
  816. * @regset: The user regset structure.
  817. *
  818. * This function checks for the active number of available
  819. * regisers in transaction checkpointed FPR category.
  820. */
  821. static int tm_cfpr_active(struct task_struct *target,
  822. const struct user_regset *regset)
  823. {
  824. if (!cpu_has_feature(CPU_FTR_TM))
  825. return -ENODEV;
  826. if (!MSR_TM_ACTIVE(target->thread.regs->msr))
  827. return 0;
  828. return regset->n;
  829. }
  830. /**
  831. * tm_cfpr_get - get CFPR registers
  832. * @target: The target task.
  833. * @regset: The user regset structure.
  834. * @pos: The buffer position.
  835. * @count: Number of bytes to copy.
  836. * @kbuf: Kernel buffer to copy from.
  837. * @ubuf: User buffer to copy into.
  838. *
  839. * This function gets in transaction checkpointed FPR registers.
  840. *
  841. * When the transaction is active 'ckfp_state' holds the checkpointed
  842. * values for the current transaction to fall back on if it aborts
  843. * in between. This function gets those checkpointed FPR registers.
  844. * The userspace interface buffer layout is as follows.
  845. *
  846. * struct data {
  847. * u64 fpr[32];
  848. * u64 fpscr;
  849. *};
  850. */
  851. static int tm_cfpr_get(struct task_struct *target,
  852. const struct user_regset *regset,
  853. unsigned int pos, unsigned int count,
  854. void *kbuf, void __user *ubuf)
  855. {
  856. u64 buf[33];
  857. int i;
  858. if (!cpu_has_feature(CPU_FTR_TM))
  859. return -ENODEV;
  860. if (!MSR_TM_ACTIVE(target->thread.regs->msr))
  861. return -ENODATA;
  862. flush_tmregs_to_thread(target);
  863. flush_fp_to_thread(target);
  864. flush_altivec_to_thread(target);
  865. /* copy to local buffer then write that out */
  866. for (i = 0; i < 32 ; i++)
  867. buf[i] = target->thread.TS_CKFPR(i);
  868. buf[32] = target->thread.ckfp_state.fpscr;
  869. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, buf, 0, -1);
  870. }
  871. /**
  872. * tm_cfpr_set - set CFPR registers
  873. * @target: The target task.
  874. * @regset: The user regset structure.
  875. * @pos: The buffer position.
  876. * @count: Number of bytes to copy.
  877. * @kbuf: Kernel buffer to copy into.
  878. * @ubuf: User buffer to copy from.
  879. *
  880. * This function sets in transaction checkpointed FPR registers.
  881. *
  882. * When the transaction is active 'ckfp_state' holds the checkpointed
  883. * FPR register values for the current transaction to fall back on
  884. * if it aborts in between. This function sets these checkpointed
  885. * FPR registers. The userspace interface buffer layout is as follows.
  886. *
  887. * struct data {
  888. * u64 fpr[32];
  889. * u64 fpscr;
  890. *};
  891. */
  892. static int tm_cfpr_set(struct task_struct *target,
  893. const struct user_regset *regset,
  894. unsigned int pos, unsigned int count,
  895. const void *kbuf, const void __user *ubuf)
  896. {
  897. u64 buf[33];
  898. int i;
  899. if (!cpu_has_feature(CPU_FTR_TM))
  900. return -ENODEV;
  901. if (!MSR_TM_ACTIVE(target->thread.regs->msr))
  902. return -ENODATA;
  903. flush_tmregs_to_thread(target);
  904. flush_fp_to_thread(target);
  905. flush_altivec_to_thread(target);
  906. for (i = 0; i < 32; i++)
  907. buf[i] = target->thread.TS_CKFPR(i);
  908. buf[32] = target->thread.ckfp_state.fpscr;
  909. /* copy to local buffer then write that out */
  910. i = user_regset_copyin(&pos, &count, &kbuf, &ubuf, buf, 0, -1);
  911. if (i)
  912. return i;
  913. for (i = 0; i < 32 ; i++)
  914. target->thread.TS_CKFPR(i) = buf[i];
  915. target->thread.ckfp_state.fpscr = buf[32];
  916. return 0;
  917. }
  918. /**
  919. * tm_cvmx_active - get active number of registers in CVMX
  920. * @target: The target task.
  921. * @regset: The user regset structure.
  922. *
  923. * This function checks for the active number of available
  924. * regisers in checkpointed VMX category.
  925. */
  926. static int tm_cvmx_active(struct task_struct *target,
  927. const struct user_regset *regset)
  928. {
  929. if (!cpu_has_feature(CPU_FTR_TM))
  930. return -ENODEV;
  931. if (!MSR_TM_ACTIVE(target->thread.regs->msr))
  932. return 0;
  933. return regset->n;
  934. }
  935. /**
  936. * tm_cvmx_get - get CMVX registers
  937. * @target: The target task.
  938. * @regset: The user regset structure.
  939. * @pos: The buffer position.
  940. * @count: Number of bytes to copy.
  941. * @kbuf: Kernel buffer to copy from.
  942. * @ubuf: User buffer to copy into.
  943. *
  944. * This function gets in transaction checkpointed VMX registers.
  945. *
  946. * When the transaction is active 'ckvr_state' and 'ckvrsave' hold
  947. * the checkpointed values for the current transaction to fall
  948. * back on if it aborts in between. The userspace interface buffer
  949. * layout is as follows.
  950. *
  951. * struct data {
  952. * vector128 vr[32];
  953. * vector128 vscr;
  954. * vector128 vrsave;
  955. *};
  956. */
  957. static int tm_cvmx_get(struct task_struct *target,
  958. const struct user_regset *regset,
  959. unsigned int pos, unsigned int count,
  960. void *kbuf, void __user *ubuf)
  961. {
  962. int ret;
  963. BUILD_BUG_ON(TVSO(vscr) != TVSO(vr[32]));
  964. if (!cpu_has_feature(CPU_FTR_TM))
  965. return -ENODEV;
  966. if (!MSR_TM_ACTIVE(target->thread.regs->msr))
  967. return -ENODATA;
  968. /* Flush the state */
  969. flush_tmregs_to_thread(target);
  970. flush_fp_to_thread(target);
  971. flush_altivec_to_thread(target);
  972. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  973. &target->thread.ckvr_state, 0,
  974. 33 * sizeof(vector128));
  975. if (!ret) {
  976. /*
  977. * Copy out only the low-order word of vrsave.
  978. */
  979. union {
  980. elf_vrreg_t reg;
  981. u32 word;
  982. } vrsave;
  983. memset(&vrsave, 0, sizeof(vrsave));
  984. vrsave.word = target->thread.ckvrsave;
  985. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &vrsave,
  986. 33 * sizeof(vector128), -1);
  987. }
  988. return ret;
  989. }
  990. /**
  991. * tm_cvmx_set - set CMVX registers
  992. * @target: The target task.
  993. * @regset: The user regset structure.
  994. * @pos: The buffer position.
  995. * @count: Number of bytes to copy.
  996. * @kbuf: Kernel buffer to copy into.
  997. * @ubuf: User buffer to copy from.
  998. *
  999. * This function sets in transaction checkpointed VMX registers.
  1000. *
  1001. * When the transaction is active 'ckvr_state' and 'ckvrsave' hold
  1002. * the checkpointed values for the current transaction to fall
  1003. * back on if it aborts in between. The userspace interface buffer
  1004. * layout is as follows.
  1005. *
  1006. * struct data {
  1007. * vector128 vr[32];
  1008. * vector128 vscr;
  1009. * vector128 vrsave;
  1010. *};
  1011. */
  1012. static int tm_cvmx_set(struct task_struct *target,
  1013. const struct user_regset *regset,
  1014. unsigned int pos, unsigned int count,
  1015. const void *kbuf, const void __user *ubuf)
  1016. {
  1017. int ret;
  1018. BUILD_BUG_ON(TVSO(vscr) != TVSO(vr[32]));
  1019. if (!cpu_has_feature(CPU_FTR_TM))
  1020. return -ENODEV;
  1021. if (!MSR_TM_ACTIVE(target->thread.regs->msr))
  1022. return -ENODATA;
  1023. flush_tmregs_to_thread(target);
  1024. flush_fp_to_thread(target);
  1025. flush_altivec_to_thread(target);
  1026. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  1027. &target->thread.ckvr_state, 0,
  1028. 33 * sizeof(vector128));
  1029. if (!ret && count > 0) {
  1030. /*
  1031. * We use only the low-order word of vrsave.
  1032. */
  1033. union {
  1034. elf_vrreg_t reg;
  1035. u32 word;
  1036. } vrsave;
  1037. memset(&vrsave, 0, sizeof(vrsave));
  1038. vrsave.word = target->thread.ckvrsave;
  1039. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &vrsave,
  1040. 33 * sizeof(vector128), -1);
  1041. if (!ret)
  1042. target->thread.ckvrsave = vrsave.word;
  1043. }
  1044. return ret;
  1045. }
  1046. /**
  1047. * tm_cvsx_active - get active number of registers in CVSX
  1048. * @target: The target task.
  1049. * @regset: The user regset structure.
  1050. *
  1051. * This function checks for the active number of available
  1052. * regisers in transaction checkpointed VSX category.
  1053. */
  1054. static int tm_cvsx_active(struct task_struct *target,
  1055. const struct user_regset *regset)
  1056. {
  1057. if (!cpu_has_feature(CPU_FTR_TM))
  1058. return -ENODEV;
  1059. if (!MSR_TM_ACTIVE(target->thread.regs->msr))
  1060. return 0;
  1061. flush_vsx_to_thread(target);
  1062. return target->thread.used_vsr ? regset->n : 0;
  1063. }
  1064. /**
  1065. * tm_cvsx_get - get CVSX registers
  1066. * @target: The target task.
  1067. * @regset: The user regset structure.
  1068. * @pos: The buffer position.
  1069. * @count: Number of bytes to copy.
  1070. * @kbuf: Kernel buffer to copy from.
  1071. * @ubuf: User buffer to copy into.
  1072. *
  1073. * This function gets in transaction checkpointed VSX registers.
  1074. *
  1075. * When the transaction is active 'ckfp_state' holds the checkpointed
  1076. * values for the current transaction to fall back on if it aborts
  1077. * in between. This function gets those checkpointed VSX registers.
  1078. * The userspace interface buffer layout is as follows.
  1079. *
  1080. * struct data {
  1081. * u64 vsx[32];
  1082. *};
  1083. */
  1084. static int tm_cvsx_get(struct task_struct *target,
  1085. const struct user_regset *regset,
  1086. unsigned int pos, unsigned int count,
  1087. void *kbuf, void __user *ubuf)
  1088. {
  1089. u64 buf[32];
  1090. int ret, i;
  1091. if (!cpu_has_feature(CPU_FTR_TM))
  1092. return -ENODEV;
  1093. if (!MSR_TM_ACTIVE(target->thread.regs->msr))
  1094. return -ENODATA;
  1095. /* Flush the state */
  1096. flush_tmregs_to_thread(target);
  1097. flush_fp_to_thread(target);
  1098. flush_altivec_to_thread(target);
  1099. flush_vsx_to_thread(target);
  1100. for (i = 0; i < 32 ; i++)
  1101. buf[i] = target->thread.ckfp_state.fpr[i][TS_VSRLOWOFFSET];
  1102. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  1103. buf, 0, 32 * sizeof(double));
  1104. return ret;
  1105. }
  1106. /**
  1107. * tm_cvsx_set - set CFPR registers
  1108. * @target: The target task.
  1109. * @regset: The user regset structure.
  1110. * @pos: The buffer position.
  1111. * @count: Number of bytes to copy.
  1112. * @kbuf: Kernel buffer to copy into.
  1113. * @ubuf: User buffer to copy from.
  1114. *
  1115. * This function sets in transaction checkpointed VSX registers.
  1116. *
  1117. * When the transaction is active 'ckfp_state' holds the checkpointed
  1118. * VSX register values for the current transaction to fall back on
  1119. * if it aborts in between. This function sets these checkpointed
  1120. * FPR registers. The userspace interface buffer layout is as follows.
  1121. *
  1122. * struct data {
  1123. * u64 vsx[32];
  1124. *};
  1125. */
  1126. static int tm_cvsx_set(struct task_struct *target,
  1127. const struct user_regset *regset,
  1128. unsigned int pos, unsigned int count,
  1129. const void *kbuf, const void __user *ubuf)
  1130. {
  1131. u64 buf[32];
  1132. int ret, i;
  1133. if (!cpu_has_feature(CPU_FTR_TM))
  1134. return -ENODEV;
  1135. if (!MSR_TM_ACTIVE(target->thread.regs->msr))
  1136. return -ENODATA;
  1137. /* Flush the state */
  1138. flush_tmregs_to_thread(target);
  1139. flush_fp_to_thread(target);
  1140. flush_altivec_to_thread(target);
  1141. flush_vsx_to_thread(target);
  1142. for (i = 0; i < 32 ; i++)
  1143. buf[i] = target->thread.ckfp_state.fpr[i][TS_VSRLOWOFFSET];
  1144. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  1145. buf, 0, 32 * sizeof(double));
  1146. if (!ret)
  1147. for (i = 0; i < 32 ; i++)
  1148. target->thread.ckfp_state.fpr[i][TS_VSRLOWOFFSET] = buf[i];
  1149. return ret;
  1150. }
  1151. /**
  1152. * tm_spr_active - get active number of registers in TM SPR
  1153. * @target: The target task.
  1154. * @regset: The user regset structure.
  1155. *
  1156. * This function checks the active number of available
  1157. * regisers in the transactional memory SPR category.
  1158. */
  1159. static int tm_spr_active(struct task_struct *target,
  1160. const struct user_regset *regset)
  1161. {
  1162. if (!cpu_has_feature(CPU_FTR_TM))
  1163. return -ENODEV;
  1164. return regset->n;
  1165. }
  1166. /**
  1167. * tm_spr_get - get the TM related SPR registers
  1168. * @target: The target task.
  1169. * @regset: The user regset structure.
  1170. * @pos: The buffer position.
  1171. * @count: Number of bytes to copy.
  1172. * @kbuf: Kernel buffer to copy from.
  1173. * @ubuf: User buffer to copy into.
  1174. *
  1175. * This function gets transactional memory related SPR registers.
  1176. * The userspace interface buffer layout is as follows.
  1177. *
  1178. * struct {
  1179. * u64 tm_tfhar;
  1180. * u64 tm_texasr;
  1181. * u64 tm_tfiar;
  1182. * };
  1183. */
  1184. static int tm_spr_get(struct task_struct *target,
  1185. const struct user_regset *regset,
  1186. unsigned int pos, unsigned int count,
  1187. void *kbuf, void __user *ubuf)
  1188. {
  1189. int ret;
  1190. /* Build tests */
  1191. BUILD_BUG_ON(TSO(tm_tfhar) + sizeof(u64) != TSO(tm_texasr));
  1192. BUILD_BUG_ON(TSO(tm_texasr) + sizeof(u64) != TSO(tm_tfiar));
  1193. BUILD_BUG_ON(TSO(tm_tfiar) + sizeof(u64) != TSO(ckpt_regs));
  1194. if (!cpu_has_feature(CPU_FTR_TM))
  1195. return -ENODEV;
  1196. /* Flush the states */
  1197. flush_tmregs_to_thread(target);
  1198. flush_fp_to_thread(target);
  1199. flush_altivec_to_thread(target);
  1200. /* TFHAR register */
  1201. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  1202. &target->thread.tm_tfhar, 0, sizeof(u64));
  1203. /* TEXASR register */
  1204. if (!ret)
  1205. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  1206. &target->thread.tm_texasr, sizeof(u64),
  1207. 2 * sizeof(u64));
  1208. /* TFIAR register */
  1209. if (!ret)
  1210. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  1211. &target->thread.tm_tfiar,
  1212. 2 * sizeof(u64), 3 * sizeof(u64));
  1213. return ret;
  1214. }
  1215. /**
  1216. * tm_spr_set - set the TM related SPR registers
  1217. * @target: The target task.
  1218. * @regset: The user regset structure.
  1219. * @pos: The buffer position.
  1220. * @count: Number of bytes to copy.
  1221. * @kbuf: Kernel buffer to copy into.
  1222. * @ubuf: User buffer to copy from.
  1223. *
  1224. * This function sets transactional memory related SPR registers.
  1225. * The userspace interface buffer layout is as follows.
  1226. *
  1227. * struct {
  1228. * u64 tm_tfhar;
  1229. * u64 tm_texasr;
  1230. * u64 tm_tfiar;
  1231. * };
  1232. */
  1233. static int tm_spr_set(struct task_struct *target,
  1234. const struct user_regset *regset,
  1235. unsigned int pos, unsigned int count,
  1236. const void *kbuf, const void __user *ubuf)
  1237. {
  1238. int ret;
  1239. /* Build tests */
  1240. BUILD_BUG_ON(TSO(tm_tfhar) + sizeof(u64) != TSO(tm_texasr));
  1241. BUILD_BUG_ON(TSO(tm_texasr) + sizeof(u64) != TSO(tm_tfiar));
  1242. BUILD_BUG_ON(TSO(tm_tfiar) + sizeof(u64) != TSO(ckpt_regs));
  1243. if (!cpu_has_feature(CPU_FTR_TM))
  1244. return -ENODEV;
  1245. /* Flush the states */
  1246. flush_tmregs_to_thread(target);
  1247. flush_fp_to_thread(target);
  1248. flush_altivec_to_thread(target);
  1249. /* TFHAR register */
  1250. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  1251. &target->thread.tm_tfhar, 0, sizeof(u64));
  1252. /* TEXASR register */
  1253. if (!ret)
  1254. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  1255. &target->thread.tm_texasr, sizeof(u64),
  1256. 2 * sizeof(u64));
  1257. /* TFIAR register */
  1258. if (!ret)
  1259. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  1260. &target->thread.tm_tfiar,
  1261. 2 * sizeof(u64), 3 * sizeof(u64));
  1262. return ret;
  1263. }
  1264. static int tm_tar_active(struct task_struct *target,
  1265. const struct user_regset *regset)
  1266. {
  1267. if (!cpu_has_feature(CPU_FTR_TM))
  1268. return -ENODEV;
  1269. if (MSR_TM_ACTIVE(target->thread.regs->msr))
  1270. return regset->n;
  1271. return 0;
  1272. }
  1273. static int tm_tar_get(struct task_struct *target,
  1274. const struct user_regset *regset,
  1275. unsigned int pos, unsigned int count,
  1276. void *kbuf, void __user *ubuf)
  1277. {
  1278. int ret;
  1279. if (!cpu_has_feature(CPU_FTR_TM))
  1280. return -ENODEV;
  1281. if (!MSR_TM_ACTIVE(target->thread.regs->msr))
  1282. return -ENODATA;
  1283. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  1284. &target->thread.tm_tar, 0, sizeof(u64));
  1285. return ret;
  1286. }
  1287. static int tm_tar_set(struct task_struct *target,
  1288. const struct user_regset *regset,
  1289. unsigned int pos, unsigned int count,
  1290. const void *kbuf, const void __user *ubuf)
  1291. {
  1292. int ret;
  1293. if (!cpu_has_feature(CPU_FTR_TM))
  1294. return -ENODEV;
  1295. if (!MSR_TM_ACTIVE(target->thread.regs->msr))
  1296. return -ENODATA;
  1297. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  1298. &target->thread.tm_tar, 0, sizeof(u64));
  1299. return ret;
  1300. }
  1301. static int tm_ppr_active(struct task_struct *target,
  1302. const struct user_regset *regset)
  1303. {
  1304. if (!cpu_has_feature(CPU_FTR_TM))
  1305. return -ENODEV;
  1306. if (MSR_TM_ACTIVE(target->thread.regs->msr))
  1307. return regset->n;
  1308. return 0;
  1309. }
  1310. static int tm_ppr_get(struct task_struct *target,
  1311. const struct user_regset *regset,
  1312. unsigned int pos, unsigned int count,
  1313. void *kbuf, void __user *ubuf)
  1314. {
  1315. int ret;
  1316. if (!cpu_has_feature(CPU_FTR_TM))
  1317. return -ENODEV;
  1318. if (!MSR_TM_ACTIVE(target->thread.regs->msr))
  1319. return -ENODATA;
  1320. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  1321. &target->thread.tm_ppr, 0, sizeof(u64));
  1322. return ret;
  1323. }
  1324. static int tm_ppr_set(struct task_struct *target,
  1325. const struct user_regset *regset,
  1326. unsigned int pos, unsigned int count,
  1327. const void *kbuf, const void __user *ubuf)
  1328. {
  1329. int ret;
  1330. if (!cpu_has_feature(CPU_FTR_TM))
  1331. return -ENODEV;
  1332. if (!MSR_TM_ACTIVE(target->thread.regs->msr))
  1333. return -ENODATA;
  1334. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  1335. &target->thread.tm_ppr, 0, sizeof(u64));
  1336. return ret;
  1337. }
  1338. static int tm_dscr_active(struct task_struct *target,
  1339. const struct user_regset *regset)
  1340. {
  1341. if (!cpu_has_feature(CPU_FTR_TM))
  1342. return -ENODEV;
  1343. if (MSR_TM_ACTIVE(target->thread.regs->msr))
  1344. return regset->n;
  1345. return 0;
  1346. }
  1347. static int tm_dscr_get(struct task_struct *target,
  1348. const struct user_regset *regset,
  1349. unsigned int pos, unsigned int count,
  1350. void *kbuf, void __user *ubuf)
  1351. {
  1352. int ret;
  1353. if (!cpu_has_feature(CPU_FTR_TM))
  1354. return -ENODEV;
  1355. if (!MSR_TM_ACTIVE(target->thread.regs->msr))
  1356. return -ENODATA;
  1357. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  1358. &target->thread.tm_dscr, 0, sizeof(u64));
  1359. return ret;
  1360. }
  1361. static int tm_dscr_set(struct task_struct *target,
  1362. const struct user_regset *regset,
  1363. unsigned int pos, unsigned int count,
  1364. const void *kbuf, const void __user *ubuf)
  1365. {
  1366. int ret;
  1367. if (!cpu_has_feature(CPU_FTR_TM))
  1368. return -ENODEV;
  1369. if (!MSR_TM_ACTIVE(target->thread.regs->msr))
  1370. return -ENODATA;
  1371. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  1372. &target->thread.tm_dscr, 0, sizeof(u64));
  1373. return ret;
  1374. }
  1375. #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
  1376. #ifdef CONFIG_PPC64
  1377. static int ppr_get(struct task_struct *target,
  1378. const struct user_regset *regset,
  1379. unsigned int pos, unsigned int count,
  1380. void *kbuf, void __user *ubuf)
  1381. {
  1382. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  1383. &target->thread.regs->ppr, 0, sizeof(u64));
  1384. }
  1385. static int ppr_set(struct task_struct *target,
  1386. const struct user_regset *regset,
  1387. unsigned int pos, unsigned int count,
  1388. const void *kbuf, const void __user *ubuf)
  1389. {
  1390. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  1391. &target->thread.regs->ppr, 0, sizeof(u64));
  1392. }
  1393. static int dscr_get(struct task_struct *target,
  1394. const struct user_regset *regset,
  1395. unsigned int pos, unsigned int count,
  1396. void *kbuf, void __user *ubuf)
  1397. {
  1398. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  1399. &target->thread.dscr, 0, sizeof(u64));
  1400. }
  1401. static int dscr_set(struct task_struct *target,
  1402. const struct user_regset *regset,
  1403. unsigned int pos, unsigned int count,
  1404. const void *kbuf, const void __user *ubuf)
  1405. {
  1406. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  1407. &target->thread.dscr, 0, sizeof(u64));
  1408. }
  1409. #endif
  1410. #ifdef CONFIG_PPC_BOOK3S_64
  1411. static int tar_get(struct task_struct *target,
  1412. const struct user_regset *regset,
  1413. unsigned int pos, unsigned int count,
  1414. void *kbuf, void __user *ubuf)
  1415. {
  1416. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  1417. &target->thread.tar, 0, sizeof(u64));
  1418. }
  1419. static int tar_set(struct task_struct *target,
  1420. const struct user_regset *regset,
  1421. unsigned int pos, unsigned int count,
  1422. const void *kbuf, const void __user *ubuf)
  1423. {
  1424. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  1425. &target->thread.tar, 0, sizeof(u64));
  1426. }
  1427. static int ebb_active(struct task_struct *target,
  1428. const struct user_regset *regset)
  1429. {
  1430. if (!cpu_has_feature(CPU_FTR_ARCH_207S))
  1431. return -ENODEV;
  1432. if (target->thread.used_ebb)
  1433. return regset->n;
  1434. return 0;
  1435. }
  1436. static int ebb_get(struct task_struct *target,
  1437. const struct user_regset *regset,
  1438. unsigned int pos, unsigned int count,
  1439. void *kbuf, void __user *ubuf)
  1440. {
  1441. /* Build tests */
  1442. BUILD_BUG_ON(TSO(ebbrr) + sizeof(unsigned long) != TSO(ebbhr));
  1443. BUILD_BUG_ON(TSO(ebbhr) + sizeof(unsigned long) != TSO(bescr));
  1444. if (!cpu_has_feature(CPU_FTR_ARCH_207S))
  1445. return -ENODEV;
  1446. if (!target->thread.used_ebb)
  1447. return -ENODATA;
  1448. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  1449. &target->thread.ebbrr, 0, 3 * sizeof(unsigned long));
  1450. }
  1451. static int ebb_set(struct task_struct *target,
  1452. const struct user_regset *regset,
  1453. unsigned int pos, unsigned int count,
  1454. const void *kbuf, const void __user *ubuf)
  1455. {
  1456. int ret = 0;
  1457. /* Build tests */
  1458. BUILD_BUG_ON(TSO(ebbrr) + sizeof(unsigned long) != TSO(ebbhr));
  1459. BUILD_BUG_ON(TSO(ebbhr) + sizeof(unsigned long) != TSO(bescr));
  1460. if (!cpu_has_feature(CPU_FTR_ARCH_207S))
  1461. return -ENODEV;
  1462. if (target->thread.used_ebb)
  1463. return -ENODATA;
  1464. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  1465. &target->thread.ebbrr, 0, sizeof(unsigned long));
  1466. if (!ret)
  1467. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  1468. &target->thread.ebbhr, sizeof(unsigned long),
  1469. 2 * sizeof(unsigned long));
  1470. if (!ret)
  1471. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  1472. &target->thread.bescr,
  1473. 2 * sizeof(unsigned long), 3 * sizeof(unsigned long));
  1474. return ret;
  1475. }
  1476. static int pmu_active(struct task_struct *target,
  1477. const struct user_regset *regset)
  1478. {
  1479. if (!cpu_has_feature(CPU_FTR_ARCH_207S))
  1480. return -ENODEV;
  1481. return regset->n;
  1482. }
  1483. static int pmu_get(struct task_struct *target,
  1484. const struct user_regset *regset,
  1485. unsigned int pos, unsigned int count,
  1486. void *kbuf, void __user *ubuf)
  1487. {
  1488. /* Build tests */
  1489. BUILD_BUG_ON(TSO(siar) + sizeof(unsigned long) != TSO(sdar));
  1490. BUILD_BUG_ON(TSO(sdar) + sizeof(unsigned long) != TSO(sier));
  1491. BUILD_BUG_ON(TSO(sier) + sizeof(unsigned long) != TSO(mmcr2));
  1492. BUILD_BUG_ON(TSO(mmcr2) + sizeof(unsigned long) != TSO(mmcr0));
  1493. if (!cpu_has_feature(CPU_FTR_ARCH_207S))
  1494. return -ENODEV;
  1495. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  1496. &target->thread.siar, 0,
  1497. 5 * sizeof(unsigned long));
  1498. }
  1499. static int pmu_set(struct task_struct *target,
  1500. const struct user_regset *regset,
  1501. unsigned int pos, unsigned int count,
  1502. const void *kbuf, const void __user *ubuf)
  1503. {
  1504. int ret = 0;
  1505. /* Build tests */
  1506. BUILD_BUG_ON(TSO(siar) + sizeof(unsigned long) != TSO(sdar));
  1507. BUILD_BUG_ON(TSO(sdar) + sizeof(unsigned long) != TSO(sier));
  1508. BUILD_BUG_ON(TSO(sier) + sizeof(unsigned long) != TSO(mmcr2));
  1509. BUILD_BUG_ON(TSO(mmcr2) + sizeof(unsigned long) != TSO(mmcr0));
  1510. if (!cpu_has_feature(CPU_FTR_ARCH_207S))
  1511. return -ENODEV;
  1512. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  1513. &target->thread.siar, 0,
  1514. sizeof(unsigned long));
  1515. if (!ret)
  1516. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  1517. &target->thread.sdar, sizeof(unsigned long),
  1518. 2 * sizeof(unsigned long));
  1519. if (!ret)
  1520. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  1521. &target->thread.sier, 2 * sizeof(unsigned long),
  1522. 3 * sizeof(unsigned long));
  1523. if (!ret)
  1524. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  1525. &target->thread.mmcr2, 3 * sizeof(unsigned long),
  1526. 4 * sizeof(unsigned long));
  1527. if (!ret)
  1528. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  1529. &target->thread.mmcr0, 4 * sizeof(unsigned long),
  1530. 5 * sizeof(unsigned long));
  1531. return ret;
  1532. }
  1533. #endif
  1534. #ifdef CONFIG_PPC_MEM_KEYS
  1535. static int pkey_active(struct task_struct *target,
  1536. const struct user_regset *regset)
  1537. {
  1538. if (!arch_pkeys_enabled())
  1539. return -ENODEV;
  1540. return regset->n;
  1541. }
  1542. static int pkey_get(struct task_struct *target,
  1543. const struct user_regset *regset,
  1544. unsigned int pos, unsigned int count,
  1545. void *kbuf, void __user *ubuf)
  1546. {
  1547. BUILD_BUG_ON(TSO(amr) + sizeof(unsigned long) != TSO(iamr));
  1548. BUILD_BUG_ON(TSO(iamr) + sizeof(unsigned long) != TSO(uamor));
  1549. if (!arch_pkeys_enabled())
  1550. return -ENODEV;
  1551. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  1552. &target->thread.amr, 0,
  1553. ELF_NPKEY * sizeof(unsigned long));
  1554. }
  1555. static int pkey_set(struct task_struct *target,
  1556. const struct user_regset *regset,
  1557. unsigned int pos, unsigned int count,
  1558. const void *kbuf, const void __user *ubuf)
  1559. {
  1560. u64 new_amr;
  1561. int ret;
  1562. if (!arch_pkeys_enabled())
  1563. return -ENODEV;
  1564. /* Only the AMR can be set from userspace */
  1565. if (pos != 0 || count != sizeof(new_amr))
  1566. return -EINVAL;
  1567. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  1568. &new_amr, 0, sizeof(new_amr));
  1569. if (ret)
  1570. return ret;
  1571. /* UAMOR determines which bits of the AMR can be set from userspace. */
  1572. target->thread.amr = (new_amr & target->thread.uamor) |
  1573. (target->thread.amr & ~target->thread.uamor);
  1574. return 0;
  1575. }
  1576. #endif /* CONFIG_PPC_MEM_KEYS */
  1577. /*
  1578. * These are our native regset flavors.
  1579. */
  1580. enum powerpc_regset {
  1581. REGSET_GPR,
  1582. REGSET_FPR,
  1583. #ifdef CONFIG_ALTIVEC
  1584. REGSET_VMX,
  1585. #endif
  1586. #ifdef CONFIG_VSX
  1587. REGSET_VSX,
  1588. #endif
  1589. #ifdef CONFIG_SPE
  1590. REGSET_SPE,
  1591. #endif
  1592. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1593. REGSET_TM_CGPR, /* TM checkpointed GPR registers */
  1594. REGSET_TM_CFPR, /* TM checkpointed FPR registers */
  1595. REGSET_TM_CVMX, /* TM checkpointed VMX registers */
  1596. REGSET_TM_CVSX, /* TM checkpointed VSX registers */
  1597. REGSET_TM_SPR, /* TM specific SPR registers */
  1598. REGSET_TM_CTAR, /* TM checkpointed TAR register */
  1599. REGSET_TM_CPPR, /* TM checkpointed PPR register */
  1600. REGSET_TM_CDSCR, /* TM checkpointed DSCR register */
  1601. #endif
  1602. #ifdef CONFIG_PPC64
  1603. REGSET_PPR, /* PPR register */
  1604. REGSET_DSCR, /* DSCR register */
  1605. #endif
  1606. #ifdef CONFIG_PPC_BOOK3S_64
  1607. REGSET_TAR, /* TAR register */
  1608. REGSET_EBB, /* EBB registers */
  1609. REGSET_PMR, /* Performance Monitor Registers */
  1610. #endif
  1611. #ifdef CONFIG_PPC_MEM_KEYS
  1612. REGSET_PKEY, /* AMR register */
  1613. #endif
  1614. };
  1615. static const struct user_regset native_regsets[] = {
  1616. [REGSET_GPR] = {
  1617. .core_note_type = NT_PRSTATUS, .n = ELF_NGREG,
  1618. .size = sizeof(long), .align = sizeof(long),
  1619. .get = gpr_get, .set = gpr_set
  1620. },
  1621. [REGSET_FPR] = {
  1622. .core_note_type = NT_PRFPREG, .n = ELF_NFPREG,
  1623. .size = sizeof(double), .align = sizeof(double),
  1624. .get = fpr_get, .set = fpr_set
  1625. },
  1626. #ifdef CONFIG_ALTIVEC
  1627. [REGSET_VMX] = {
  1628. .core_note_type = NT_PPC_VMX, .n = 34,
  1629. .size = sizeof(vector128), .align = sizeof(vector128),
  1630. .active = vr_active, .get = vr_get, .set = vr_set
  1631. },
  1632. #endif
  1633. #ifdef CONFIG_VSX
  1634. [REGSET_VSX] = {
  1635. .core_note_type = NT_PPC_VSX, .n = 32,
  1636. .size = sizeof(double), .align = sizeof(double),
  1637. .active = vsr_active, .get = vsr_get, .set = vsr_set
  1638. },
  1639. #endif
  1640. #ifdef CONFIG_SPE
  1641. [REGSET_SPE] = {
  1642. .core_note_type = NT_PPC_SPE, .n = 35,
  1643. .size = sizeof(u32), .align = sizeof(u32),
  1644. .active = evr_active, .get = evr_get, .set = evr_set
  1645. },
  1646. #endif
  1647. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1648. [REGSET_TM_CGPR] = {
  1649. .core_note_type = NT_PPC_TM_CGPR, .n = ELF_NGREG,
  1650. .size = sizeof(long), .align = sizeof(long),
  1651. .active = tm_cgpr_active, .get = tm_cgpr_get, .set = tm_cgpr_set
  1652. },
  1653. [REGSET_TM_CFPR] = {
  1654. .core_note_type = NT_PPC_TM_CFPR, .n = ELF_NFPREG,
  1655. .size = sizeof(double), .align = sizeof(double),
  1656. .active = tm_cfpr_active, .get = tm_cfpr_get, .set = tm_cfpr_set
  1657. },
  1658. [REGSET_TM_CVMX] = {
  1659. .core_note_type = NT_PPC_TM_CVMX, .n = ELF_NVMX,
  1660. .size = sizeof(vector128), .align = sizeof(vector128),
  1661. .active = tm_cvmx_active, .get = tm_cvmx_get, .set = tm_cvmx_set
  1662. },
  1663. [REGSET_TM_CVSX] = {
  1664. .core_note_type = NT_PPC_TM_CVSX, .n = ELF_NVSX,
  1665. .size = sizeof(double), .align = sizeof(double),
  1666. .active = tm_cvsx_active, .get = tm_cvsx_get, .set = tm_cvsx_set
  1667. },
  1668. [REGSET_TM_SPR] = {
  1669. .core_note_type = NT_PPC_TM_SPR, .n = ELF_NTMSPRREG,
  1670. .size = sizeof(u64), .align = sizeof(u64),
  1671. .active = tm_spr_active, .get = tm_spr_get, .set = tm_spr_set
  1672. },
  1673. [REGSET_TM_CTAR] = {
  1674. .core_note_type = NT_PPC_TM_CTAR, .n = 1,
  1675. .size = sizeof(u64), .align = sizeof(u64),
  1676. .active = tm_tar_active, .get = tm_tar_get, .set = tm_tar_set
  1677. },
  1678. [REGSET_TM_CPPR] = {
  1679. .core_note_type = NT_PPC_TM_CPPR, .n = 1,
  1680. .size = sizeof(u64), .align = sizeof(u64),
  1681. .active = tm_ppr_active, .get = tm_ppr_get, .set = tm_ppr_set
  1682. },
  1683. [REGSET_TM_CDSCR] = {
  1684. .core_note_type = NT_PPC_TM_CDSCR, .n = 1,
  1685. .size = sizeof(u64), .align = sizeof(u64),
  1686. .active = tm_dscr_active, .get = tm_dscr_get, .set = tm_dscr_set
  1687. },
  1688. #endif
  1689. #ifdef CONFIG_PPC64
  1690. [REGSET_PPR] = {
  1691. .core_note_type = NT_PPC_PPR, .n = 1,
  1692. .size = sizeof(u64), .align = sizeof(u64),
  1693. .get = ppr_get, .set = ppr_set
  1694. },
  1695. [REGSET_DSCR] = {
  1696. .core_note_type = NT_PPC_DSCR, .n = 1,
  1697. .size = sizeof(u64), .align = sizeof(u64),
  1698. .get = dscr_get, .set = dscr_set
  1699. },
  1700. #endif
  1701. #ifdef CONFIG_PPC_BOOK3S_64
  1702. [REGSET_TAR] = {
  1703. .core_note_type = NT_PPC_TAR, .n = 1,
  1704. .size = sizeof(u64), .align = sizeof(u64),
  1705. .get = tar_get, .set = tar_set
  1706. },
  1707. [REGSET_EBB] = {
  1708. .core_note_type = NT_PPC_EBB, .n = ELF_NEBB,
  1709. .size = sizeof(u64), .align = sizeof(u64),
  1710. .active = ebb_active, .get = ebb_get, .set = ebb_set
  1711. },
  1712. [REGSET_PMR] = {
  1713. .core_note_type = NT_PPC_PMU, .n = ELF_NPMU,
  1714. .size = sizeof(u64), .align = sizeof(u64),
  1715. .active = pmu_active, .get = pmu_get, .set = pmu_set
  1716. },
  1717. #endif
  1718. #ifdef CONFIG_PPC_MEM_KEYS
  1719. [REGSET_PKEY] = {
  1720. .core_note_type = NT_PPC_PKEY, .n = ELF_NPKEY,
  1721. .size = sizeof(u64), .align = sizeof(u64),
  1722. .active = pkey_active, .get = pkey_get, .set = pkey_set
  1723. },
  1724. #endif
  1725. };
  1726. static const struct user_regset_view user_ppc_native_view = {
  1727. .name = UTS_MACHINE, .e_machine = ELF_ARCH, .ei_osabi = ELF_OSABI,
  1728. .regsets = native_regsets, .n = ARRAY_SIZE(native_regsets)
  1729. };
  1730. #ifdef CONFIG_PPC64
  1731. #include <linux/compat.h>
  1732. static int gpr32_get_common(struct task_struct *target,
  1733. const struct user_regset *regset,
  1734. unsigned int pos, unsigned int count,
  1735. void *kbuf, void __user *ubuf,
  1736. unsigned long *regs)
  1737. {
  1738. compat_ulong_t *k = kbuf;
  1739. compat_ulong_t __user *u = ubuf;
  1740. compat_ulong_t reg;
  1741. pos /= sizeof(reg);
  1742. count /= sizeof(reg);
  1743. if (kbuf)
  1744. for (; count > 0 && pos < PT_MSR; --count)
  1745. *k++ = regs[pos++];
  1746. else
  1747. for (; count > 0 && pos < PT_MSR; --count)
  1748. if (__put_user((compat_ulong_t) regs[pos++], u++))
  1749. return -EFAULT;
  1750. if (count > 0 && pos == PT_MSR) {
  1751. reg = get_user_msr(target);
  1752. if (kbuf)
  1753. *k++ = reg;
  1754. else if (__put_user(reg, u++))
  1755. return -EFAULT;
  1756. ++pos;
  1757. --count;
  1758. }
  1759. if (kbuf)
  1760. for (; count > 0 && pos < PT_REGS_COUNT; --count)
  1761. *k++ = regs[pos++];
  1762. else
  1763. for (; count > 0 && pos < PT_REGS_COUNT; --count)
  1764. if (__put_user((compat_ulong_t) regs[pos++], u++))
  1765. return -EFAULT;
  1766. kbuf = k;
  1767. ubuf = u;
  1768. pos *= sizeof(reg);
  1769. count *= sizeof(reg);
  1770. return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  1771. PT_REGS_COUNT * sizeof(reg), -1);
  1772. }
  1773. static int gpr32_set_common(struct task_struct *target,
  1774. const struct user_regset *regset,
  1775. unsigned int pos, unsigned int count,
  1776. const void *kbuf, const void __user *ubuf,
  1777. unsigned long *regs)
  1778. {
  1779. const compat_ulong_t *k = kbuf;
  1780. const compat_ulong_t __user *u = ubuf;
  1781. compat_ulong_t reg;
  1782. pos /= sizeof(reg);
  1783. count /= sizeof(reg);
  1784. if (kbuf)
  1785. for (; count > 0 && pos < PT_MSR; --count)
  1786. regs[pos++] = *k++;
  1787. else
  1788. for (; count > 0 && pos < PT_MSR; --count) {
  1789. if (__get_user(reg, u++))
  1790. return -EFAULT;
  1791. regs[pos++] = reg;
  1792. }
  1793. if (count > 0 && pos == PT_MSR) {
  1794. if (kbuf)
  1795. reg = *k++;
  1796. else if (__get_user(reg, u++))
  1797. return -EFAULT;
  1798. set_user_msr(target, reg);
  1799. ++pos;
  1800. --count;
  1801. }
  1802. if (kbuf) {
  1803. for (; count > 0 && pos <= PT_MAX_PUT_REG; --count)
  1804. regs[pos++] = *k++;
  1805. for (; count > 0 && pos < PT_TRAP; --count, ++pos)
  1806. ++k;
  1807. } else {
  1808. for (; count > 0 && pos <= PT_MAX_PUT_REG; --count) {
  1809. if (__get_user(reg, u++))
  1810. return -EFAULT;
  1811. regs[pos++] = reg;
  1812. }
  1813. for (; count > 0 && pos < PT_TRAP; --count, ++pos)
  1814. if (__get_user(reg, u++))
  1815. return -EFAULT;
  1816. }
  1817. if (count > 0 && pos == PT_TRAP) {
  1818. if (kbuf)
  1819. reg = *k++;
  1820. else if (__get_user(reg, u++))
  1821. return -EFAULT;
  1822. set_user_trap(target, reg);
  1823. ++pos;
  1824. --count;
  1825. }
  1826. kbuf = k;
  1827. ubuf = u;
  1828. pos *= sizeof(reg);
  1829. count *= sizeof(reg);
  1830. return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  1831. (PT_TRAP + 1) * sizeof(reg), -1);
  1832. }
  1833. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1834. static int tm_cgpr32_get(struct task_struct *target,
  1835. const struct user_regset *regset,
  1836. unsigned int pos, unsigned int count,
  1837. void *kbuf, void __user *ubuf)
  1838. {
  1839. return gpr32_get_common(target, regset, pos, count, kbuf, ubuf,
  1840. &target->thread.ckpt_regs.gpr[0]);
  1841. }
  1842. static int tm_cgpr32_set(struct task_struct *target,
  1843. const struct user_regset *regset,
  1844. unsigned int pos, unsigned int count,
  1845. const void *kbuf, const void __user *ubuf)
  1846. {
  1847. return gpr32_set_common(target, regset, pos, count, kbuf, ubuf,
  1848. &target->thread.ckpt_regs.gpr[0]);
  1849. }
  1850. #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
  1851. static int gpr32_get(struct task_struct *target,
  1852. const struct user_regset *regset,
  1853. unsigned int pos, unsigned int count,
  1854. void *kbuf, void __user *ubuf)
  1855. {
  1856. int i;
  1857. if (target->thread.regs == NULL)
  1858. return -EIO;
  1859. if (!FULL_REGS(target->thread.regs)) {
  1860. /*
  1861. * We have a partial register set.
  1862. * Fill 14-31 with bogus values.
  1863. */
  1864. for (i = 14; i < 32; i++)
  1865. target->thread.regs->gpr[i] = NV_REG_POISON;
  1866. }
  1867. return gpr32_get_common(target, regset, pos, count, kbuf, ubuf,
  1868. &target->thread.regs->gpr[0]);
  1869. }
  1870. static int gpr32_set(struct task_struct *target,
  1871. const struct user_regset *regset,
  1872. unsigned int pos, unsigned int count,
  1873. const void *kbuf, const void __user *ubuf)
  1874. {
  1875. if (target->thread.regs == NULL)
  1876. return -EIO;
  1877. CHECK_FULL_REGS(target->thread.regs);
  1878. return gpr32_set_common(target, regset, pos, count, kbuf, ubuf,
  1879. &target->thread.regs->gpr[0]);
  1880. }
  1881. /*
  1882. * These are the regset flavors matching the CONFIG_PPC32 native set.
  1883. */
  1884. static const struct user_regset compat_regsets[] = {
  1885. [REGSET_GPR] = {
  1886. .core_note_type = NT_PRSTATUS, .n = ELF_NGREG,
  1887. .size = sizeof(compat_long_t), .align = sizeof(compat_long_t),
  1888. .get = gpr32_get, .set = gpr32_set
  1889. },
  1890. [REGSET_FPR] = {
  1891. .core_note_type = NT_PRFPREG, .n = ELF_NFPREG,
  1892. .size = sizeof(double), .align = sizeof(double),
  1893. .get = fpr_get, .set = fpr_set
  1894. },
  1895. #ifdef CONFIG_ALTIVEC
  1896. [REGSET_VMX] = {
  1897. .core_note_type = NT_PPC_VMX, .n = 34,
  1898. .size = sizeof(vector128), .align = sizeof(vector128),
  1899. .active = vr_active, .get = vr_get, .set = vr_set
  1900. },
  1901. #endif
  1902. #ifdef CONFIG_SPE
  1903. [REGSET_SPE] = {
  1904. .core_note_type = NT_PPC_SPE, .n = 35,
  1905. .size = sizeof(u32), .align = sizeof(u32),
  1906. .active = evr_active, .get = evr_get, .set = evr_set
  1907. },
  1908. #endif
  1909. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1910. [REGSET_TM_CGPR] = {
  1911. .core_note_type = NT_PPC_TM_CGPR, .n = ELF_NGREG,
  1912. .size = sizeof(long), .align = sizeof(long),
  1913. .active = tm_cgpr_active,
  1914. .get = tm_cgpr32_get, .set = tm_cgpr32_set
  1915. },
  1916. [REGSET_TM_CFPR] = {
  1917. .core_note_type = NT_PPC_TM_CFPR, .n = ELF_NFPREG,
  1918. .size = sizeof(double), .align = sizeof(double),
  1919. .active = tm_cfpr_active, .get = tm_cfpr_get, .set = tm_cfpr_set
  1920. },
  1921. [REGSET_TM_CVMX] = {
  1922. .core_note_type = NT_PPC_TM_CVMX, .n = ELF_NVMX,
  1923. .size = sizeof(vector128), .align = sizeof(vector128),
  1924. .active = tm_cvmx_active, .get = tm_cvmx_get, .set = tm_cvmx_set
  1925. },
  1926. [REGSET_TM_CVSX] = {
  1927. .core_note_type = NT_PPC_TM_CVSX, .n = ELF_NVSX,
  1928. .size = sizeof(double), .align = sizeof(double),
  1929. .active = tm_cvsx_active, .get = tm_cvsx_get, .set = tm_cvsx_set
  1930. },
  1931. [REGSET_TM_SPR] = {
  1932. .core_note_type = NT_PPC_TM_SPR, .n = ELF_NTMSPRREG,
  1933. .size = sizeof(u64), .align = sizeof(u64),
  1934. .active = tm_spr_active, .get = tm_spr_get, .set = tm_spr_set
  1935. },
  1936. [REGSET_TM_CTAR] = {
  1937. .core_note_type = NT_PPC_TM_CTAR, .n = 1,
  1938. .size = sizeof(u64), .align = sizeof(u64),
  1939. .active = tm_tar_active, .get = tm_tar_get, .set = tm_tar_set
  1940. },
  1941. [REGSET_TM_CPPR] = {
  1942. .core_note_type = NT_PPC_TM_CPPR, .n = 1,
  1943. .size = sizeof(u64), .align = sizeof(u64),
  1944. .active = tm_ppr_active, .get = tm_ppr_get, .set = tm_ppr_set
  1945. },
  1946. [REGSET_TM_CDSCR] = {
  1947. .core_note_type = NT_PPC_TM_CDSCR, .n = 1,
  1948. .size = sizeof(u64), .align = sizeof(u64),
  1949. .active = tm_dscr_active, .get = tm_dscr_get, .set = tm_dscr_set
  1950. },
  1951. #endif
  1952. #ifdef CONFIG_PPC64
  1953. [REGSET_PPR] = {
  1954. .core_note_type = NT_PPC_PPR, .n = 1,
  1955. .size = sizeof(u64), .align = sizeof(u64),
  1956. .get = ppr_get, .set = ppr_set
  1957. },
  1958. [REGSET_DSCR] = {
  1959. .core_note_type = NT_PPC_DSCR, .n = 1,
  1960. .size = sizeof(u64), .align = sizeof(u64),
  1961. .get = dscr_get, .set = dscr_set
  1962. },
  1963. #endif
  1964. #ifdef CONFIG_PPC_BOOK3S_64
  1965. [REGSET_TAR] = {
  1966. .core_note_type = NT_PPC_TAR, .n = 1,
  1967. .size = sizeof(u64), .align = sizeof(u64),
  1968. .get = tar_get, .set = tar_set
  1969. },
  1970. [REGSET_EBB] = {
  1971. .core_note_type = NT_PPC_EBB, .n = ELF_NEBB,
  1972. .size = sizeof(u64), .align = sizeof(u64),
  1973. .active = ebb_active, .get = ebb_get, .set = ebb_set
  1974. },
  1975. #endif
  1976. };
  1977. static const struct user_regset_view user_ppc_compat_view = {
  1978. .name = "ppc", .e_machine = EM_PPC, .ei_osabi = ELF_OSABI,
  1979. .regsets = compat_regsets, .n = ARRAY_SIZE(compat_regsets)
  1980. };
  1981. #endif /* CONFIG_PPC64 */
  1982. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  1983. {
  1984. #ifdef CONFIG_PPC64
  1985. if (test_tsk_thread_flag(task, TIF_32BIT))
  1986. return &user_ppc_compat_view;
  1987. #endif
  1988. return &user_ppc_native_view;
  1989. }
  1990. void user_enable_single_step(struct task_struct *task)
  1991. {
  1992. struct pt_regs *regs = task->thread.regs;
  1993. if (regs != NULL) {
  1994. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  1995. task->thread.debug.dbcr0 &= ~DBCR0_BT;
  1996. task->thread.debug.dbcr0 |= DBCR0_IDM | DBCR0_IC;
  1997. regs->msr |= MSR_DE;
  1998. #else
  1999. regs->msr &= ~MSR_BE;
  2000. regs->msr |= MSR_SE;
  2001. #endif
  2002. }
  2003. set_tsk_thread_flag(task, TIF_SINGLESTEP);
  2004. }
  2005. void user_enable_block_step(struct task_struct *task)
  2006. {
  2007. struct pt_regs *regs = task->thread.regs;
  2008. if (regs != NULL) {
  2009. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  2010. task->thread.debug.dbcr0 &= ~DBCR0_IC;
  2011. task->thread.debug.dbcr0 = DBCR0_IDM | DBCR0_BT;
  2012. regs->msr |= MSR_DE;
  2013. #else
  2014. regs->msr &= ~MSR_SE;
  2015. regs->msr |= MSR_BE;
  2016. #endif
  2017. }
  2018. set_tsk_thread_flag(task, TIF_SINGLESTEP);
  2019. }
  2020. void user_disable_single_step(struct task_struct *task)
  2021. {
  2022. struct pt_regs *regs = task->thread.regs;
  2023. if (regs != NULL) {
  2024. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  2025. /*
  2026. * The logic to disable single stepping should be as
  2027. * simple as turning off the Instruction Complete flag.
  2028. * And, after doing so, if all debug flags are off, turn
  2029. * off DBCR0(IDM) and MSR(DE) .... Torez
  2030. */
  2031. task->thread.debug.dbcr0 &= ~(DBCR0_IC|DBCR0_BT);
  2032. /*
  2033. * Test to see if any of the DBCR_ACTIVE_EVENTS bits are set.
  2034. */
  2035. if (!DBCR_ACTIVE_EVENTS(task->thread.debug.dbcr0,
  2036. task->thread.debug.dbcr1)) {
  2037. /*
  2038. * All debug events were off.....
  2039. */
  2040. task->thread.debug.dbcr0 &= ~DBCR0_IDM;
  2041. regs->msr &= ~MSR_DE;
  2042. }
  2043. #else
  2044. regs->msr &= ~(MSR_SE | MSR_BE);
  2045. #endif
  2046. }
  2047. clear_tsk_thread_flag(task, TIF_SINGLESTEP);
  2048. }
  2049. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  2050. void ptrace_triggered(struct perf_event *bp,
  2051. struct perf_sample_data *data, struct pt_regs *regs)
  2052. {
  2053. struct perf_event_attr attr;
  2054. /*
  2055. * Disable the breakpoint request here since ptrace has defined a
  2056. * one-shot behaviour for breakpoint exceptions in PPC64.
  2057. * The SIGTRAP signal is generated automatically for us in do_dabr().
  2058. * We don't have to do anything about that here
  2059. */
  2060. attr = bp->attr;
  2061. attr.disabled = true;
  2062. modify_user_hw_breakpoint(bp, &attr);
  2063. }
  2064. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  2065. static int ptrace_set_debugreg(struct task_struct *task, unsigned long addr,
  2066. unsigned long data)
  2067. {
  2068. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  2069. int ret;
  2070. struct thread_struct *thread = &(task->thread);
  2071. struct perf_event *bp;
  2072. struct perf_event_attr attr;
  2073. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  2074. #ifndef CONFIG_PPC_ADV_DEBUG_REGS
  2075. bool set_bp = true;
  2076. struct arch_hw_breakpoint hw_brk;
  2077. #endif
  2078. /* For ppc64 we support one DABR and no IABR's at the moment (ppc64).
  2079. * For embedded processors we support one DAC and no IAC's at the
  2080. * moment.
  2081. */
  2082. if (addr > 0)
  2083. return -EINVAL;
  2084. /* The bottom 3 bits in dabr are flags */
  2085. if ((data & ~0x7UL) >= TASK_SIZE)
  2086. return -EIO;
  2087. #ifndef CONFIG_PPC_ADV_DEBUG_REGS
  2088. /* For processors using DABR (i.e. 970), the bottom 3 bits are flags.
  2089. * It was assumed, on previous implementations, that 3 bits were
  2090. * passed together with the data address, fitting the design of the
  2091. * DABR register, as follows:
  2092. *
  2093. * bit 0: Read flag
  2094. * bit 1: Write flag
  2095. * bit 2: Breakpoint translation
  2096. *
  2097. * Thus, we use them here as so.
  2098. */
  2099. /* Ensure breakpoint translation bit is set */
  2100. if (data && !(data & HW_BRK_TYPE_TRANSLATE))
  2101. return -EIO;
  2102. hw_brk.address = data & (~HW_BRK_TYPE_DABR);
  2103. hw_brk.type = (data & HW_BRK_TYPE_DABR) | HW_BRK_TYPE_PRIV_ALL;
  2104. hw_brk.len = 8;
  2105. set_bp = (data) && (hw_brk.type & HW_BRK_TYPE_RDWR);
  2106. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  2107. bp = thread->ptrace_bps[0];
  2108. if (!set_bp) {
  2109. if (bp) {
  2110. unregister_hw_breakpoint(bp);
  2111. thread->ptrace_bps[0] = NULL;
  2112. }
  2113. return 0;
  2114. }
  2115. if (bp) {
  2116. attr = bp->attr;
  2117. attr.bp_addr = hw_brk.address;
  2118. arch_bp_generic_fields(hw_brk.type, &attr.bp_type);
  2119. /* Enable breakpoint */
  2120. attr.disabled = false;
  2121. ret = modify_user_hw_breakpoint(bp, &attr);
  2122. if (ret) {
  2123. return ret;
  2124. }
  2125. thread->ptrace_bps[0] = bp;
  2126. thread->hw_brk = hw_brk;
  2127. return 0;
  2128. }
  2129. /* Create a new breakpoint request if one doesn't exist already */
  2130. hw_breakpoint_init(&attr);
  2131. attr.bp_addr = hw_brk.address;
  2132. attr.bp_len = 8;
  2133. arch_bp_generic_fields(hw_brk.type,
  2134. &attr.bp_type);
  2135. thread->ptrace_bps[0] = bp = register_user_hw_breakpoint(&attr,
  2136. ptrace_triggered, NULL, task);
  2137. if (IS_ERR(bp)) {
  2138. thread->ptrace_bps[0] = NULL;
  2139. return PTR_ERR(bp);
  2140. }
  2141. #else /* !CONFIG_HAVE_HW_BREAKPOINT */
  2142. if (set_bp && (!ppc_breakpoint_available()))
  2143. return -ENODEV;
  2144. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  2145. task->thread.hw_brk = hw_brk;
  2146. #else /* CONFIG_PPC_ADV_DEBUG_REGS */
  2147. /* As described above, it was assumed 3 bits were passed with the data
  2148. * address, but we will assume only the mode bits will be passed
  2149. * as to not cause alignment restrictions for DAC-based processors.
  2150. */
  2151. /* DAC's hold the whole address without any mode flags */
  2152. task->thread.debug.dac1 = data & ~0x3UL;
  2153. if (task->thread.debug.dac1 == 0) {
  2154. dbcr_dac(task) &= ~(DBCR_DAC1R | DBCR_DAC1W);
  2155. if (!DBCR_ACTIVE_EVENTS(task->thread.debug.dbcr0,
  2156. task->thread.debug.dbcr1)) {
  2157. task->thread.regs->msr &= ~MSR_DE;
  2158. task->thread.debug.dbcr0 &= ~DBCR0_IDM;
  2159. }
  2160. return 0;
  2161. }
  2162. /* Read or Write bits must be set */
  2163. if (!(data & 0x3UL))
  2164. return -EINVAL;
  2165. /* Set the Internal Debugging flag (IDM bit 1) for the DBCR0
  2166. register */
  2167. task->thread.debug.dbcr0 |= DBCR0_IDM;
  2168. /* Check for write and read flags and set DBCR0
  2169. accordingly */
  2170. dbcr_dac(task) &= ~(DBCR_DAC1R|DBCR_DAC1W);
  2171. if (data & 0x1UL)
  2172. dbcr_dac(task) |= DBCR_DAC1R;
  2173. if (data & 0x2UL)
  2174. dbcr_dac(task) |= DBCR_DAC1W;
  2175. task->thread.regs->msr |= MSR_DE;
  2176. #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
  2177. return 0;
  2178. }
  2179. /*
  2180. * Called by kernel/ptrace.c when detaching..
  2181. *
  2182. * Make sure single step bits etc are not set.
  2183. */
  2184. void ptrace_disable(struct task_struct *child)
  2185. {
  2186. /* make sure the single step bit is not set. */
  2187. user_disable_single_step(child);
  2188. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  2189. }
  2190. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  2191. static long set_instruction_bp(struct task_struct *child,
  2192. struct ppc_hw_breakpoint *bp_info)
  2193. {
  2194. int slot;
  2195. int slot1_in_use = ((child->thread.debug.dbcr0 & DBCR0_IAC1) != 0);
  2196. int slot2_in_use = ((child->thread.debug.dbcr0 & DBCR0_IAC2) != 0);
  2197. int slot3_in_use = ((child->thread.debug.dbcr0 & DBCR0_IAC3) != 0);
  2198. int slot4_in_use = ((child->thread.debug.dbcr0 & DBCR0_IAC4) != 0);
  2199. if (dbcr_iac_range(child) & DBCR_IAC12MODE)
  2200. slot2_in_use = 1;
  2201. if (dbcr_iac_range(child) & DBCR_IAC34MODE)
  2202. slot4_in_use = 1;
  2203. if (bp_info->addr >= TASK_SIZE)
  2204. return -EIO;
  2205. if (bp_info->addr_mode != PPC_BREAKPOINT_MODE_EXACT) {
  2206. /* Make sure range is valid. */
  2207. if (bp_info->addr2 >= TASK_SIZE)
  2208. return -EIO;
  2209. /* We need a pair of IAC regsisters */
  2210. if ((!slot1_in_use) && (!slot2_in_use)) {
  2211. slot = 1;
  2212. child->thread.debug.iac1 = bp_info->addr;
  2213. child->thread.debug.iac2 = bp_info->addr2;
  2214. child->thread.debug.dbcr0 |= DBCR0_IAC1;
  2215. if (bp_info->addr_mode ==
  2216. PPC_BREAKPOINT_MODE_RANGE_EXCLUSIVE)
  2217. dbcr_iac_range(child) |= DBCR_IAC12X;
  2218. else
  2219. dbcr_iac_range(child) |= DBCR_IAC12I;
  2220. #if CONFIG_PPC_ADV_DEBUG_IACS > 2
  2221. } else if ((!slot3_in_use) && (!slot4_in_use)) {
  2222. slot = 3;
  2223. child->thread.debug.iac3 = bp_info->addr;
  2224. child->thread.debug.iac4 = bp_info->addr2;
  2225. child->thread.debug.dbcr0 |= DBCR0_IAC3;
  2226. if (bp_info->addr_mode ==
  2227. PPC_BREAKPOINT_MODE_RANGE_EXCLUSIVE)
  2228. dbcr_iac_range(child) |= DBCR_IAC34X;
  2229. else
  2230. dbcr_iac_range(child) |= DBCR_IAC34I;
  2231. #endif
  2232. } else
  2233. return -ENOSPC;
  2234. } else {
  2235. /* We only need one. If possible leave a pair free in
  2236. * case a range is needed later
  2237. */
  2238. if (!slot1_in_use) {
  2239. /*
  2240. * Don't use iac1 if iac1-iac2 are free and either
  2241. * iac3 or iac4 (but not both) are free
  2242. */
  2243. if (slot2_in_use || (slot3_in_use == slot4_in_use)) {
  2244. slot = 1;
  2245. child->thread.debug.iac1 = bp_info->addr;
  2246. child->thread.debug.dbcr0 |= DBCR0_IAC1;
  2247. goto out;
  2248. }
  2249. }
  2250. if (!slot2_in_use) {
  2251. slot = 2;
  2252. child->thread.debug.iac2 = bp_info->addr;
  2253. child->thread.debug.dbcr0 |= DBCR0_IAC2;
  2254. #if CONFIG_PPC_ADV_DEBUG_IACS > 2
  2255. } else if (!slot3_in_use) {
  2256. slot = 3;
  2257. child->thread.debug.iac3 = bp_info->addr;
  2258. child->thread.debug.dbcr0 |= DBCR0_IAC3;
  2259. } else if (!slot4_in_use) {
  2260. slot = 4;
  2261. child->thread.debug.iac4 = bp_info->addr;
  2262. child->thread.debug.dbcr0 |= DBCR0_IAC4;
  2263. #endif
  2264. } else
  2265. return -ENOSPC;
  2266. }
  2267. out:
  2268. child->thread.debug.dbcr0 |= DBCR0_IDM;
  2269. child->thread.regs->msr |= MSR_DE;
  2270. return slot;
  2271. }
  2272. static int del_instruction_bp(struct task_struct *child, int slot)
  2273. {
  2274. switch (slot) {
  2275. case 1:
  2276. if ((child->thread.debug.dbcr0 & DBCR0_IAC1) == 0)
  2277. return -ENOENT;
  2278. if (dbcr_iac_range(child) & DBCR_IAC12MODE) {
  2279. /* address range - clear slots 1 & 2 */
  2280. child->thread.debug.iac2 = 0;
  2281. dbcr_iac_range(child) &= ~DBCR_IAC12MODE;
  2282. }
  2283. child->thread.debug.iac1 = 0;
  2284. child->thread.debug.dbcr0 &= ~DBCR0_IAC1;
  2285. break;
  2286. case 2:
  2287. if ((child->thread.debug.dbcr0 & DBCR0_IAC2) == 0)
  2288. return -ENOENT;
  2289. if (dbcr_iac_range(child) & DBCR_IAC12MODE)
  2290. /* used in a range */
  2291. return -EINVAL;
  2292. child->thread.debug.iac2 = 0;
  2293. child->thread.debug.dbcr0 &= ~DBCR0_IAC2;
  2294. break;
  2295. #if CONFIG_PPC_ADV_DEBUG_IACS > 2
  2296. case 3:
  2297. if ((child->thread.debug.dbcr0 & DBCR0_IAC3) == 0)
  2298. return -ENOENT;
  2299. if (dbcr_iac_range(child) & DBCR_IAC34MODE) {
  2300. /* address range - clear slots 3 & 4 */
  2301. child->thread.debug.iac4 = 0;
  2302. dbcr_iac_range(child) &= ~DBCR_IAC34MODE;
  2303. }
  2304. child->thread.debug.iac3 = 0;
  2305. child->thread.debug.dbcr0 &= ~DBCR0_IAC3;
  2306. break;
  2307. case 4:
  2308. if ((child->thread.debug.dbcr0 & DBCR0_IAC4) == 0)
  2309. return -ENOENT;
  2310. if (dbcr_iac_range(child) & DBCR_IAC34MODE)
  2311. /* Used in a range */
  2312. return -EINVAL;
  2313. child->thread.debug.iac4 = 0;
  2314. child->thread.debug.dbcr0 &= ~DBCR0_IAC4;
  2315. break;
  2316. #endif
  2317. default:
  2318. return -EINVAL;
  2319. }
  2320. return 0;
  2321. }
  2322. static int set_dac(struct task_struct *child, struct ppc_hw_breakpoint *bp_info)
  2323. {
  2324. int byte_enable =
  2325. (bp_info->condition_mode >> PPC_BREAKPOINT_CONDITION_BE_SHIFT)
  2326. & 0xf;
  2327. int condition_mode =
  2328. bp_info->condition_mode & PPC_BREAKPOINT_CONDITION_MODE;
  2329. int slot;
  2330. if (byte_enable && (condition_mode == 0))
  2331. return -EINVAL;
  2332. if (bp_info->addr >= TASK_SIZE)
  2333. return -EIO;
  2334. if ((dbcr_dac(child) & (DBCR_DAC1R | DBCR_DAC1W)) == 0) {
  2335. slot = 1;
  2336. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_READ)
  2337. dbcr_dac(child) |= DBCR_DAC1R;
  2338. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_WRITE)
  2339. dbcr_dac(child) |= DBCR_DAC1W;
  2340. child->thread.debug.dac1 = (unsigned long)bp_info->addr;
  2341. #if CONFIG_PPC_ADV_DEBUG_DVCS > 0
  2342. if (byte_enable) {
  2343. child->thread.debug.dvc1 =
  2344. (unsigned long)bp_info->condition_value;
  2345. child->thread.debug.dbcr2 |=
  2346. ((byte_enable << DBCR2_DVC1BE_SHIFT) |
  2347. (condition_mode << DBCR2_DVC1M_SHIFT));
  2348. }
  2349. #endif
  2350. #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
  2351. } else if (child->thread.debug.dbcr2 & DBCR2_DAC12MODE) {
  2352. /* Both dac1 and dac2 are part of a range */
  2353. return -ENOSPC;
  2354. #endif
  2355. } else if ((dbcr_dac(child) & (DBCR_DAC2R | DBCR_DAC2W)) == 0) {
  2356. slot = 2;
  2357. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_READ)
  2358. dbcr_dac(child) |= DBCR_DAC2R;
  2359. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_WRITE)
  2360. dbcr_dac(child) |= DBCR_DAC2W;
  2361. child->thread.debug.dac2 = (unsigned long)bp_info->addr;
  2362. #if CONFIG_PPC_ADV_DEBUG_DVCS > 0
  2363. if (byte_enable) {
  2364. child->thread.debug.dvc2 =
  2365. (unsigned long)bp_info->condition_value;
  2366. child->thread.debug.dbcr2 |=
  2367. ((byte_enable << DBCR2_DVC2BE_SHIFT) |
  2368. (condition_mode << DBCR2_DVC2M_SHIFT));
  2369. }
  2370. #endif
  2371. } else
  2372. return -ENOSPC;
  2373. child->thread.debug.dbcr0 |= DBCR0_IDM;
  2374. child->thread.regs->msr |= MSR_DE;
  2375. return slot + 4;
  2376. }
  2377. static int del_dac(struct task_struct *child, int slot)
  2378. {
  2379. if (slot == 1) {
  2380. if ((dbcr_dac(child) & (DBCR_DAC1R | DBCR_DAC1W)) == 0)
  2381. return -ENOENT;
  2382. child->thread.debug.dac1 = 0;
  2383. dbcr_dac(child) &= ~(DBCR_DAC1R | DBCR_DAC1W);
  2384. #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
  2385. if (child->thread.debug.dbcr2 & DBCR2_DAC12MODE) {
  2386. child->thread.debug.dac2 = 0;
  2387. child->thread.debug.dbcr2 &= ~DBCR2_DAC12MODE;
  2388. }
  2389. child->thread.debug.dbcr2 &= ~(DBCR2_DVC1M | DBCR2_DVC1BE);
  2390. #endif
  2391. #if CONFIG_PPC_ADV_DEBUG_DVCS > 0
  2392. child->thread.debug.dvc1 = 0;
  2393. #endif
  2394. } else if (slot == 2) {
  2395. if ((dbcr_dac(child) & (DBCR_DAC2R | DBCR_DAC2W)) == 0)
  2396. return -ENOENT;
  2397. #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
  2398. if (child->thread.debug.dbcr2 & DBCR2_DAC12MODE)
  2399. /* Part of a range */
  2400. return -EINVAL;
  2401. child->thread.debug.dbcr2 &= ~(DBCR2_DVC2M | DBCR2_DVC2BE);
  2402. #endif
  2403. #if CONFIG_PPC_ADV_DEBUG_DVCS > 0
  2404. child->thread.debug.dvc2 = 0;
  2405. #endif
  2406. child->thread.debug.dac2 = 0;
  2407. dbcr_dac(child) &= ~(DBCR_DAC2R | DBCR_DAC2W);
  2408. } else
  2409. return -EINVAL;
  2410. return 0;
  2411. }
  2412. #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
  2413. #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
  2414. static int set_dac_range(struct task_struct *child,
  2415. struct ppc_hw_breakpoint *bp_info)
  2416. {
  2417. int mode = bp_info->addr_mode & PPC_BREAKPOINT_MODE_MASK;
  2418. /* We don't allow range watchpoints to be used with DVC */
  2419. if (bp_info->condition_mode)
  2420. return -EINVAL;
  2421. /*
  2422. * Best effort to verify the address range. The user/supervisor bits
  2423. * prevent trapping in kernel space, but let's fail on an obvious bad
  2424. * range. The simple test on the mask is not fool-proof, and any
  2425. * exclusive range will spill over into kernel space.
  2426. */
  2427. if (bp_info->addr >= TASK_SIZE)
  2428. return -EIO;
  2429. if (mode == PPC_BREAKPOINT_MODE_MASK) {
  2430. /*
  2431. * dac2 is a bitmask. Don't allow a mask that makes a
  2432. * kernel space address from a valid dac1 value
  2433. */
  2434. if (~((unsigned long)bp_info->addr2) >= TASK_SIZE)
  2435. return -EIO;
  2436. } else {
  2437. /*
  2438. * For range breakpoints, addr2 must also be a valid address
  2439. */
  2440. if (bp_info->addr2 >= TASK_SIZE)
  2441. return -EIO;
  2442. }
  2443. if (child->thread.debug.dbcr0 &
  2444. (DBCR0_DAC1R | DBCR0_DAC1W | DBCR0_DAC2R | DBCR0_DAC2W))
  2445. return -ENOSPC;
  2446. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_READ)
  2447. child->thread.debug.dbcr0 |= (DBCR0_DAC1R | DBCR0_IDM);
  2448. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_WRITE)
  2449. child->thread.debug.dbcr0 |= (DBCR0_DAC1W | DBCR0_IDM);
  2450. child->thread.debug.dac1 = bp_info->addr;
  2451. child->thread.debug.dac2 = bp_info->addr2;
  2452. if (mode == PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE)
  2453. child->thread.debug.dbcr2 |= DBCR2_DAC12M;
  2454. else if (mode == PPC_BREAKPOINT_MODE_RANGE_EXCLUSIVE)
  2455. child->thread.debug.dbcr2 |= DBCR2_DAC12MX;
  2456. else /* PPC_BREAKPOINT_MODE_MASK */
  2457. child->thread.debug.dbcr2 |= DBCR2_DAC12MM;
  2458. child->thread.regs->msr |= MSR_DE;
  2459. return 5;
  2460. }
  2461. #endif /* CONFIG_PPC_ADV_DEBUG_DAC_RANGE */
  2462. static long ppc_set_hwdebug(struct task_struct *child,
  2463. struct ppc_hw_breakpoint *bp_info)
  2464. {
  2465. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  2466. int len = 0;
  2467. struct thread_struct *thread = &(child->thread);
  2468. struct perf_event *bp;
  2469. struct perf_event_attr attr;
  2470. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  2471. #ifndef CONFIG_PPC_ADV_DEBUG_REGS
  2472. struct arch_hw_breakpoint brk;
  2473. #endif
  2474. if (bp_info->version != 1)
  2475. return -ENOTSUPP;
  2476. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  2477. /*
  2478. * Check for invalid flags and combinations
  2479. */
  2480. if ((bp_info->trigger_type == 0) ||
  2481. (bp_info->trigger_type & ~(PPC_BREAKPOINT_TRIGGER_EXECUTE |
  2482. PPC_BREAKPOINT_TRIGGER_RW)) ||
  2483. (bp_info->addr_mode & ~PPC_BREAKPOINT_MODE_MASK) ||
  2484. (bp_info->condition_mode &
  2485. ~(PPC_BREAKPOINT_CONDITION_MODE |
  2486. PPC_BREAKPOINT_CONDITION_BE_ALL)))
  2487. return -EINVAL;
  2488. #if CONFIG_PPC_ADV_DEBUG_DVCS == 0
  2489. if (bp_info->condition_mode != PPC_BREAKPOINT_CONDITION_NONE)
  2490. return -EINVAL;
  2491. #endif
  2492. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_EXECUTE) {
  2493. if ((bp_info->trigger_type != PPC_BREAKPOINT_TRIGGER_EXECUTE) ||
  2494. (bp_info->condition_mode != PPC_BREAKPOINT_CONDITION_NONE))
  2495. return -EINVAL;
  2496. return set_instruction_bp(child, bp_info);
  2497. }
  2498. if (bp_info->addr_mode == PPC_BREAKPOINT_MODE_EXACT)
  2499. return set_dac(child, bp_info);
  2500. #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
  2501. return set_dac_range(child, bp_info);
  2502. #else
  2503. return -EINVAL;
  2504. #endif
  2505. #else /* !CONFIG_PPC_ADV_DEBUG_DVCS */
  2506. /*
  2507. * We only support one data breakpoint
  2508. */
  2509. if ((bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_RW) == 0 ||
  2510. (bp_info->trigger_type & ~PPC_BREAKPOINT_TRIGGER_RW) != 0 ||
  2511. bp_info->condition_mode != PPC_BREAKPOINT_CONDITION_NONE)
  2512. return -EINVAL;
  2513. if ((unsigned long)bp_info->addr >= TASK_SIZE)
  2514. return -EIO;
  2515. brk.address = bp_info->addr & ~7UL;
  2516. brk.type = HW_BRK_TYPE_TRANSLATE;
  2517. brk.len = 8;
  2518. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_READ)
  2519. brk.type |= HW_BRK_TYPE_READ;
  2520. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_WRITE)
  2521. brk.type |= HW_BRK_TYPE_WRITE;
  2522. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  2523. /*
  2524. * Check if the request is for 'range' breakpoints. We can
  2525. * support it if range < 8 bytes.
  2526. */
  2527. if (bp_info->addr_mode == PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE)
  2528. len = bp_info->addr2 - bp_info->addr;
  2529. else if (bp_info->addr_mode == PPC_BREAKPOINT_MODE_EXACT)
  2530. len = 1;
  2531. else
  2532. return -EINVAL;
  2533. bp = thread->ptrace_bps[0];
  2534. if (bp)
  2535. return -ENOSPC;
  2536. /* Create a new breakpoint request if one doesn't exist already */
  2537. hw_breakpoint_init(&attr);
  2538. attr.bp_addr = (unsigned long)bp_info->addr & ~HW_BREAKPOINT_ALIGN;
  2539. attr.bp_len = len;
  2540. arch_bp_generic_fields(brk.type, &attr.bp_type);
  2541. thread->ptrace_bps[0] = bp = register_user_hw_breakpoint(&attr,
  2542. ptrace_triggered, NULL, child);
  2543. if (IS_ERR(bp)) {
  2544. thread->ptrace_bps[0] = NULL;
  2545. return PTR_ERR(bp);
  2546. }
  2547. return 1;
  2548. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  2549. if (bp_info->addr_mode != PPC_BREAKPOINT_MODE_EXACT)
  2550. return -EINVAL;
  2551. if (child->thread.hw_brk.address)
  2552. return -ENOSPC;
  2553. if (!ppc_breakpoint_available())
  2554. return -ENODEV;
  2555. child->thread.hw_brk = brk;
  2556. return 1;
  2557. #endif /* !CONFIG_PPC_ADV_DEBUG_DVCS */
  2558. }
  2559. static long ppc_del_hwdebug(struct task_struct *child, long data)
  2560. {
  2561. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  2562. int ret = 0;
  2563. struct thread_struct *thread = &(child->thread);
  2564. struct perf_event *bp;
  2565. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  2566. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  2567. int rc;
  2568. if (data <= 4)
  2569. rc = del_instruction_bp(child, (int)data);
  2570. else
  2571. rc = del_dac(child, (int)data - 4);
  2572. if (!rc) {
  2573. if (!DBCR_ACTIVE_EVENTS(child->thread.debug.dbcr0,
  2574. child->thread.debug.dbcr1)) {
  2575. child->thread.debug.dbcr0 &= ~DBCR0_IDM;
  2576. child->thread.regs->msr &= ~MSR_DE;
  2577. }
  2578. }
  2579. return rc;
  2580. #else
  2581. if (data != 1)
  2582. return -EINVAL;
  2583. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  2584. bp = thread->ptrace_bps[0];
  2585. if (bp) {
  2586. unregister_hw_breakpoint(bp);
  2587. thread->ptrace_bps[0] = NULL;
  2588. } else
  2589. ret = -ENOENT;
  2590. return ret;
  2591. #else /* CONFIG_HAVE_HW_BREAKPOINT */
  2592. if (child->thread.hw_brk.address == 0)
  2593. return -ENOENT;
  2594. child->thread.hw_brk.address = 0;
  2595. child->thread.hw_brk.type = 0;
  2596. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  2597. return 0;
  2598. #endif
  2599. }
  2600. long arch_ptrace(struct task_struct *child, long request,
  2601. unsigned long addr, unsigned long data)
  2602. {
  2603. int ret = -EPERM;
  2604. void __user *datavp = (void __user *) data;
  2605. unsigned long __user *datalp = datavp;
  2606. switch (request) {
  2607. /* read the word at location addr in the USER area. */
  2608. case PTRACE_PEEKUSR: {
  2609. unsigned long index, tmp;
  2610. ret = -EIO;
  2611. /* convert to index and check */
  2612. #ifdef CONFIG_PPC32
  2613. index = addr >> 2;
  2614. if ((addr & 3) || (index > PT_FPSCR)
  2615. || (child->thread.regs == NULL))
  2616. #else
  2617. index = addr >> 3;
  2618. if ((addr & 7) || (index > PT_FPSCR))
  2619. #endif
  2620. break;
  2621. CHECK_FULL_REGS(child->thread.regs);
  2622. if (index < PT_FPR0) {
  2623. ret = ptrace_get_reg(child, (int) index, &tmp);
  2624. if (ret)
  2625. break;
  2626. } else {
  2627. unsigned int fpidx = index - PT_FPR0;
  2628. flush_fp_to_thread(child);
  2629. if (fpidx < (PT_FPSCR - PT_FPR0))
  2630. memcpy(&tmp, &child->thread.TS_FPR(fpidx),
  2631. sizeof(long));
  2632. else
  2633. tmp = child->thread.fp_state.fpscr;
  2634. }
  2635. ret = put_user(tmp, datalp);
  2636. break;
  2637. }
  2638. /* write the word at location addr in the USER area */
  2639. case PTRACE_POKEUSR: {
  2640. unsigned long index;
  2641. ret = -EIO;
  2642. /* convert to index and check */
  2643. #ifdef CONFIG_PPC32
  2644. index = addr >> 2;
  2645. if ((addr & 3) || (index > PT_FPSCR)
  2646. || (child->thread.regs == NULL))
  2647. #else
  2648. index = addr >> 3;
  2649. if ((addr & 7) || (index > PT_FPSCR))
  2650. #endif
  2651. break;
  2652. CHECK_FULL_REGS(child->thread.regs);
  2653. if (index < PT_FPR0) {
  2654. ret = ptrace_put_reg(child, index, data);
  2655. } else {
  2656. unsigned int fpidx = index - PT_FPR0;
  2657. flush_fp_to_thread(child);
  2658. if (fpidx < (PT_FPSCR - PT_FPR0))
  2659. memcpy(&child->thread.TS_FPR(fpidx), &data,
  2660. sizeof(long));
  2661. else
  2662. child->thread.fp_state.fpscr = data;
  2663. ret = 0;
  2664. }
  2665. break;
  2666. }
  2667. case PPC_PTRACE_GETHWDBGINFO: {
  2668. struct ppc_debug_info dbginfo;
  2669. dbginfo.version = 1;
  2670. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  2671. dbginfo.num_instruction_bps = CONFIG_PPC_ADV_DEBUG_IACS;
  2672. dbginfo.num_data_bps = CONFIG_PPC_ADV_DEBUG_DACS;
  2673. dbginfo.num_condition_regs = CONFIG_PPC_ADV_DEBUG_DVCS;
  2674. dbginfo.data_bp_alignment = 4;
  2675. dbginfo.sizeof_condition = 4;
  2676. dbginfo.features = PPC_DEBUG_FEATURE_INSN_BP_RANGE |
  2677. PPC_DEBUG_FEATURE_INSN_BP_MASK;
  2678. #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
  2679. dbginfo.features |=
  2680. PPC_DEBUG_FEATURE_DATA_BP_RANGE |
  2681. PPC_DEBUG_FEATURE_DATA_BP_MASK;
  2682. #endif
  2683. #else /* !CONFIG_PPC_ADV_DEBUG_REGS */
  2684. dbginfo.num_instruction_bps = 0;
  2685. if (ppc_breakpoint_available())
  2686. dbginfo.num_data_bps = 1;
  2687. else
  2688. dbginfo.num_data_bps = 0;
  2689. dbginfo.num_condition_regs = 0;
  2690. #ifdef CONFIG_PPC64
  2691. dbginfo.data_bp_alignment = 8;
  2692. #else
  2693. dbginfo.data_bp_alignment = 4;
  2694. #endif
  2695. dbginfo.sizeof_condition = 0;
  2696. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  2697. dbginfo.features = PPC_DEBUG_FEATURE_DATA_BP_RANGE;
  2698. if (cpu_has_feature(CPU_FTR_DAWR))
  2699. dbginfo.features |= PPC_DEBUG_FEATURE_DATA_BP_DAWR;
  2700. #else
  2701. dbginfo.features = 0;
  2702. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  2703. #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
  2704. if (copy_to_user(datavp, &dbginfo,
  2705. sizeof(struct ppc_debug_info)))
  2706. return -EFAULT;
  2707. return 0;
  2708. }
  2709. case PPC_PTRACE_SETHWDEBUG: {
  2710. struct ppc_hw_breakpoint bp_info;
  2711. if (copy_from_user(&bp_info, datavp,
  2712. sizeof(struct ppc_hw_breakpoint)))
  2713. return -EFAULT;
  2714. return ppc_set_hwdebug(child, &bp_info);
  2715. }
  2716. case PPC_PTRACE_DELHWDEBUG: {
  2717. ret = ppc_del_hwdebug(child, data);
  2718. break;
  2719. }
  2720. case PTRACE_GET_DEBUGREG: {
  2721. #ifndef CONFIG_PPC_ADV_DEBUG_REGS
  2722. unsigned long dabr_fake;
  2723. #endif
  2724. ret = -EINVAL;
  2725. /* We only support one DABR and no IABRS at the moment */
  2726. if (addr > 0)
  2727. break;
  2728. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  2729. ret = put_user(child->thread.debug.dac1, datalp);
  2730. #else
  2731. dabr_fake = ((child->thread.hw_brk.address & (~HW_BRK_TYPE_DABR)) |
  2732. (child->thread.hw_brk.type & HW_BRK_TYPE_DABR));
  2733. ret = put_user(dabr_fake, datalp);
  2734. #endif
  2735. break;
  2736. }
  2737. case PTRACE_SET_DEBUGREG:
  2738. ret = ptrace_set_debugreg(child, addr, data);
  2739. break;
  2740. #ifdef CONFIG_PPC64
  2741. case PTRACE_GETREGS64:
  2742. #endif
  2743. case PTRACE_GETREGS: /* Get all pt_regs from the child. */
  2744. return copy_regset_to_user(child, &user_ppc_native_view,
  2745. REGSET_GPR,
  2746. 0, sizeof(struct user_pt_regs),
  2747. datavp);
  2748. #ifdef CONFIG_PPC64
  2749. case PTRACE_SETREGS64:
  2750. #endif
  2751. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  2752. return copy_regset_from_user(child, &user_ppc_native_view,
  2753. REGSET_GPR,
  2754. 0, sizeof(struct user_pt_regs),
  2755. datavp);
  2756. case PTRACE_GETFPREGS: /* Get the child FPU state (FPR0...31 + FPSCR) */
  2757. return copy_regset_to_user(child, &user_ppc_native_view,
  2758. REGSET_FPR,
  2759. 0, sizeof(elf_fpregset_t),
  2760. datavp);
  2761. case PTRACE_SETFPREGS: /* Set the child FPU state (FPR0...31 + FPSCR) */
  2762. return copy_regset_from_user(child, &user_ppc_native_view,
  2763. REGSET_FPR,
  2764. 0, sizeof(elf_fpregset_t),
  2765. datavp);
  2766. #ifdef CONFIG_ALTIVEC
  2767. case PTRACE_GETVRREGS:
  2768. return copy_regset_to_user(child, &user_ppc_native_view,
  2769. REGSET_VMX,
  2770. 0, (33 * sizeof(vector128) +
  2771. sizeof(u32)),
  2772. datavp);
  2773. case PTRACE_SETVRREGS:
  2774. return copy_regset_from_user(child, &user_ppc_native_view,
  2775. REGSET_VMX,
  2776. 0, (33 * sizeof(vector128) +
  2777. sizeof(u32)),
  2778. datavp);
  2779. #endif
  2780. #ifdef CONFIG_VSX
  2781. case PTRACE_GETVSRREGS:
  2782. return copy_regset_to_user(child, &user_ppc_native_view,
  2783. REGSET_VSX,
  2784. 0, 32 * sizeof(double),
  2785. datavp);
  2786. case PTRACE_SETVSRREGS:
  2787. return copy_regset_from_user(child, &user_ppc_native_view,
  2788. REGSET_VSX,
  2789. 0, 32 * sizeof(double),
  2790. datavp);
  2791. #endif
  2792. #ifdef CONFIG_SPE
  2793. case PTRACE_GETEVRREGS:
  2794. /* Get the child spe register state. */
  2795. return copy_regset_to_user(child, &user_ppc_native_view,
  2796. REGSET_SPE, 0, 35 * sizeof(u32),
  2797. datavp);
  2798. case PTRACE_SETEVRREGS:
  2799. /* Set the child spe register state. */
  2800. return copy_regset_from_user(child, &user_ppc_native_view,
  2801. REGSET_SPE, 0, 35 * sizeof(u32),
  2802. datavp);
  2803. #endif
  2804. default:
  2805. ret = ptrace_request(child, request, addr, data);
  2806. break;
  2807. }
  2808. return ret;
  2809. }
  2810. #ifdef CONFIG_SECCOMP
  2811. static int do_seccomp(struct pt_regs *regs)
  2812. {
  2813. if (!test_thread_flag(TIF_SECCOMP))
  2814. return 0;
  2815. /*
  2816. * The ABI we present to seccomp tracers is that r3 contains
  2817. * the syscall return value and orig_gpr3 contains the first
  2818. * syscall parameter. This is different to the ptrace ABI where
  2819. * both r3 and orig_gpr3 contain the first syscall parameter.
  2820. */
  2821. regs->gpr[3] = -ENOSYS;
  2822. /*
  2823. * We use the __ version here because we have already checked
  2824. * TIF_SECCOMP. If this fails, there is nothing left to do, we
  2825. * have already loaded -ENOSYS into r3, or seccomp has put
  2826. * something else in r3 (via SECCOMP_RET_ERRNO/TRACE).
  2827. */
  2828. if (__secure_computing(NULL))
  2829. return -1;
  2830. /*
  2831. * The syscall was allowed by seccomp, restore the register
  2832. * state to what audit expects.
  2833. * Note that we use orig_gpr3, which means a seccomp tracer can
  2834. * modify the first syscall parameter (in orig_gpr3) and also
  2835. * allow the syscall to proceed.
  2836. */
  2837. regs->gpr[3] = regs->orig_gpr3;
  2838. return 0;
  2839. }
  2840. #else
  2841. static inline int do_seccomp(struct pt_regs *regs) { return 0; }
  2842. #endif /* CONFIG_SECCOMP */
  2843. /**
  2844. * do_syscall_trace_enter() - Do syscall tracing on kernel entry.
  2845. * @regs: the pt_regs of the task to trace (current)
  2846. *
  2847. * Performs various types of tracing on syscall entry. This includes seccomp,
  2848. * ptrace, syscall tracepoints and audit.
  2849. *
  2850. * The pt_regs are potentially visible to userspace via ptrace, so their
  2851. * contents is ABI.
  2852. *
  2853. * One or more of the tracers may modify the contents of pt_regs, in particular
  2854. * to modify arguments or even the syscall number itself.
  2855. *
  2856. * It's also possible that a tracer can choose to reject the system call. In
  2857. * that case this function will return an illegal syscall number, and will put
  2858. * an appropriate return value in regs->r3.
  2859. *
  2860. * Return: the (possibly changed) syscall number.
  2861. */
  2862. long do_syscall_trace_enter(struct pt_regs *regs)
  2863. {
  2864. user_exit();
  2865. if (test_thread_flag(TIF_SYSCALL_EMU)) {
  2866. /*
  2867. * A nonzero return code from tracehook_report_syscall_entry()
  2868. * tells us to prevent the syscall execution, but we are not
  2869. * going to execute it anyway.
  2870. *
  2871. * Returning -1 will skip the syscall execution. We want to
  2872. * avoid clobbering any register also, thus, not 'gotoing'
  2873. * skip label.
  2874. */
  2875. if (tracehook_report_syscall_entry(regs))
  2876. ;
  2877. return -1;
  2878. }
  2879. /*
  2880. * The tracer may decide to abort the syscall, if so tracehook
  2881. * will return !0. Note that the tracer may also just change
  2882. * regs->gpr[0] to an invalid syscall number, that is handled
  2883. * below on the exit path.
  2884. */
  2885. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  2886. tracehook_report_syscall_entry(regs))
  2887. goto skip;
  2888. /* Run seccomp after ptrace; allow it to set gpr[3]. */
  2889. if (do_seccomp(regs))
  2890. return -1;
  2891. /* Avoid trace and audit when syscall is invalid. */
  2892. if (regs->gpr[0] >= NR_syscalls)
  2893. goto skip;
  2894. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  2895. trace_sys_enter(regs, regs->gpr[0]);
  2896. #ifdef CONFIG_PPC64
  2897. if (!is_32bit_task())
  2898. audit_syscall_entry(regs->gpr[0], regs->gpr[3], regs->gpr[4],
  2899. regs->gpr[5], regs->gpr[6]);
  2900. else
  2901. #endif
  2902. audit_syscall_entry(regs->gpr[0],
  2903. regs->gpr[3] & 0xffffffff,
  2904. regs->gpr[4] & 0xffffffff,
  2905. regs->gpr[5] & 0xffffffff,
  2906. regs->gpr[6] & 0xffffffff);
  2907. /* Return the possibly modified but valid syscall number */
  2908. return regs->gpr[0];
  2909. skip:
  2910. /*
  2911. * If we are aborting explicitly, or if the syscall number is
  2912. * now invalid, set the return value to -ENOSYS.
  2913. */
  2914. regs->gpr[3] = -ENOSYS;
  2915. return -1;
  2916. }
  2917. void do_syscall_trace_leave(struct pt_regs *regs)
  2918. {
  2919. int step;
  2920. audit_syscall_exit(regs);
  2921. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  2922. trace_sys_exit(regs, regs->result);
  2923. step = test_thread_flag(TIF_SINGLESTEP);
  2924. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  2925. tracehook_report_syscall_exit(regs, step);
  2926. user_enter();
  2927. }
  2928. void __init pt_regs_check(void)
  2929. {
  2930. BUILD_BUG_ON(offsetof(struct pt_regs, gpr) !=
  2931. offsetof(struct user_pt_regs, gpr));
  2932. BUILD_BUG_ON(offsetof(struct pt_regs, nip) !=
  2933. offsetof(struct user_pt_regs, nip));
  2934. BUILD_BUG_ON(offsetof(struct pt_regs, msr) !=
  2935. offsetof(struct user_pt_regs, msr));
  2936. BUILD_BUG_ON(offsetof(struct pt_regs, msr) !=
  2937. offsetof(struct user_pt_regs, msr));
  2938. BUILD_BUG_ON(offsetof(struct pt_regs, orig_gpr3) !=
  2939. offsetof(struct user_pt_regs, orig_gpr3));
  2940. BUILD_BUG_ON(offsetof(struct pt_regs, ctr) !=
  2941. offsetof(struct user_pt_regs, ctr));
  2942. BUILD_BUG_ON(offsetof(struct pt_regs, link) !=
  2943. offsetof(struct user_pt_regs, link));
  2944. BUILD_BUG_ON(offsetof(struct pt_regs, xer) !=
  2945. offsetof(struct user_pt_regs, xer));
  2946. BUILD_BUG_ON(offsetof(struct pt_regs, ccr) !=
  2947. offsetof(struct user_pt_regs, ccr));
  2948. #ifdef __powerpc64__
  2949. BUILD_BUG_ON(offsetof(struct pt_regs, softe) !=
  2950. offsetof(struct user_pt_regs, softe));
  2951. #else
  2952. BUILD_BUG_ON(offsetof(struct pt_regs, mq) !=
  2953. offsetof(struct user_pt_regs, mq));
  2954. #endif
  2955. BUILD_BUG_ON(offsetof(struct pt_regs, trap) !=
  2956. offsetof(struct user_pt_regs, trap));
  2957. BUILD_BUG_ON(offsetof(struct pt_regs, dar) !=
  2958. offsetof(struct user_pt_regs, dar));
  2959. BUILD_BUG_ON(offsetof(struct pt_regs, dsisr) !=
  2960. offsetof(struct user_pt_regs, dsisr));
  2961. BUILD_BUG_ON(offsetof(struct pt_regs, result) !=
  2962. offsetof(struct user_pt_regs, result));
  2963. BUILD_BUG_ON(sizeof(struct user_pt_regs) > sizeof(struct pt_regs));
  2964. }