ptrace.c 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326
  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 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 pt_regs));
  324. if (!ret)
  325. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  326. sizeof(struct 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 pt_regs));
  739. if (!ret)
  740. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  741. sizeof(struct 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.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.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. }
  2189. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  2190. static long set_instruction_bp(struct task_struct *child,
  2191. struct ppc_hw_breakpoint *bp_info)
  2192. {
  2193. int slot;
  2194. int slot1_in_use = ((child->thread.debug.dbcr0 & DBCR0_IAC1) != 0);
  2195. int slot2_in_use = ((child->thread.debug.dbcr0 & DBCR0_IAC2) != 0);
  2196. int slot3_in_use = ((child->thread.debug.dbcr0 & DBCR0_IAC3) != 0);
  2197. int slot4_in_use = ((child->thread.debug.dbcr0 & DBCR0_IAC4) != 0);
  2198. if (dbcr_iac_range(child) & DBCR_IAC12MODE)
  2199. slot2_in_use = 1;
  2200. if (dbcr_iac_range(child) & DBCR_IAC34MODE)
  2201. slot4_in_use = 1;
  2202. if (bp_info->addr >= TASK_SIZE)
  2203. return -EIO;
  2204. if (bp_info->addr_mode != PPC_BREAKPOINT_MODE_EXACT) {
  2205. /* Make sure range is valid. */
  2206. if (bp_info->addr2 >= TASK_SIZE)
  2207. return -EIO;
  2208. /* We need a pair of IAC regsisters */
  2209. if ((!slot1_in_use) && (!slot2_in_use)) {
  2210. slot = 1;
  2211. child->thread.debug.iac1 = bp_info->addr;
  2212. child->thread.debug.iac2 = bp_info->addr2;
  2213. child->thread.debug.dbcr0 |= DBCR0_IAC1;
  2214. if (bp_info->addr_mode ==
  2215. PPC_BREAKPOINT_MODE_RANGE_EXCLUSIVE)
  2216. dbcr_iac_range(child) |= DBCR_IAC12X;
  2217. else
  2218. dbcr_iac_range(child) |= DBCR_IAC12I;
  2219. #if CONFIG_PPC_ADV_DEBUG_IACS > 2
  2220. } else if ((!slot3_in_use) && (!slot4_in_use)) {
  2221. slot = 3;
  2222. child->thread.debug.iac3 = bp_info->addr;
  2223. child->thread.debug.iac4 = bp_info->addr2;
  2224. child->thread.debug.dbcr0 |= DBCR0_IAC3;
  2225. if (bp_info->addr_mode ==
  2226. PPC_BREAKPOINT_MODE_RANGE_EXCLUSIVE)
  2227. dbcr_iac_range(child) |= DBCR_IAC34X;
  2228. else
  2229. dbcr_iac_range(child) |= DBCR_IAC34I;
  2230. #endif
  2231. } else
  2232. return -ENOSPC;
  2233. } else {
  2234. /* We only need one. If possible leave a pair free in
  2235. * case a range is needed later
  2236. */
  2237. if (!slot1_in_use) {
  2238. /*
  2239. * Don't use iac1 if iac1-iac2 are free and either
  2240. * iac3 or iac4 (but not both) are free
  2241. */
  2242. if (slot2_in_use || (slot3_in_use == slot4_in_use)) {
  2243. slot = 1;
  2244. child->thread.debug.iac1 = bp_info->addr;
  2245. child->thread.debug.dbcr0 |= DBCR0_IAC1;
  2246. goto out;
  2247. }
  2248. }
  2249. if (!slot2_in_use) {
  2250. slot = 2;
  2251. child->thread.debug.iac2 = bp_info->addr;
  2252. child->thread.debug.dbcr0 |= DBCR0_IAC2;
  2253. #if CONFIG_PPC_ADV_DEBUG_IACS > 2
  2254. } else if (!slot3_in_use) {
  2255. slot = 3;
  2256. child->thread.debug.iac3 = bp_info->addr;
  2257. child->thread.debug.dbcr0 |= DBCR0_IAC3;
  2258. } else if (!slot4_in_use) {
  2259. slot = 4;
  2260. child->thread.debug.iac4 = bp_info->addr;
  2261. child->thread.debug.dbcr0 |= DBCR0_IAC4;
  2262. #endif
  2263. } else
  2264. return -ENOSPC;
  2265. }
  2266. out:
  2267. child->thread.debug.dbcr0 |= DBCR0_IDM;
  2268. child->thread.regs->msr |= MSR_DE;
  2269. return slot;
  2270. }
  2271. static int del_instruction_bp(struct task_struct *child, int slot)
  2272. {
  2273. switch (slot) {
  2274. case 1:
  2275. if ((child->thread.debug.dbcr0 & DBCR0_IAC1) == 0)
  2276. return -ENOENT;
  2277. if (dbcr_iac_range(child) & DBCR_IAC12MODE) {
  2278. /* address range - clear slots 1 & 2 */
  2279. child->thread.debug.iac2 = 0;
  2280. dbcr_iac_range(child) &= ~DBCR_IAC12MODE;
  2281. }
  2282. child->thread.debug.iac1 = 0;
  2283. child->thread.debug.dbcr0 &= ~DBCR0_IAC1;
  2284. break;
  2285. case 2:
  2286. if ((child->thread.debug.dbcr0 & DBCR0_IAC2) == 0)
  2287. return -ENOENT;
  2288. if (dbcr_iac_range(child) & DBCR_IAC12MODE)
  2289. /* used in a range */
  2290. return -EINVAL;
  2291. child->thread.debug.iac2 = 0;
  2292. child->thread.debug.dbcr0 &= ~DBCR0_IAC2;
  2293. break;
  2294. #if CONFIG_PPC_ADV_DEBUG_IACS > 2
  2295. case 3:
  2296. if ((child->thread.debug.dbcr0 & DBCR0_IAC3) == 0)
  2297. return -ENOENT;
  2298. if (dbcr_iac_range(child) & DBCR_IAC34MODE) {
  2299. /* address range - clear slots 3 & 4 */
  2300. child->thread.debug.iac4 = 0;
  2301. dbcr_iac_range(child) &= ~DBCR_IAC34MODE;
  2302. }
  2303. child->thread.debug.iac3 = 0;
  2304. child->thread.debug.dbcr0 &= ~DBCR0_IAC3;
  2305. break;
  2306. case 4:
  2307. if ((child->thread.debug.dbcr0 & DBCR0_IAC4) == 0)
  2308. return -ENOENT;
  2309. if (dbcr_iac_range(child) & DBCR_IAC34MODE)
  2310. /* Used in a range */
  2311. return -EINVAL;
  2312. child->thread.debug.iac4 = 0;
  2313. child->thread.debug.dbcr0 &= ~DBCR0_IAC4;
  2314. break;
  2315. #endif
  2316. default:
  2317. return -EINVAL;
  2318. }
  2319. return 0;
  2320. }
  2321. static int set_dac(struct task_struct *child, struct ppc_hw_breakpoint *bp_info)
  2322. {
  2323. int byte_enable =
  2324. (bp_info->condition_mode >> PPC_BREAKPOINT_CONDITION_BE_SHIFT)
  2325. & 0xf;
  2326. int condition_mode =
  2327. bp_info->condition_mode & PPC_BREAKPOINT_CONDITION_MODE;
  2328. int slot;
  2329. if (byte_enable && (condition_mode == 0))
  2330. return -EINVAL;
  2331. if (bp_info->addr >= TASK_SIZE)
  2332. return -EIO;
  2333. if ((dbcr_dac(child) & (DBCR_DAC1R | DBCR_DAC1W)) == 0) {
  2334. slot = 1;
  2335. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_READ)
  2336. dbcr_dac(child) |= DBCR_DAC1R;
  2337. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_WRITE)
  2338. dbcr_dac(child) |= DBCR_DAC1W;
  2339. child->thread.debug.dac1 = (unsigned long)bp_info->addr;
  2340. #if CONFIG_PPC_ADV_DEBUG_DVCS > 0
  2341. if (byte_enable) {
  2342. child->thread.debug.dvc1 =
  2343. (unsigned long)bp_info->condition_value;
  2344. child->thread.debug.dbcr2 |=
  2345. ((byte_enable << DBCR2_DVC1BE_SHIFT) |
  2346. (condition_mode << DBCR2_DVC1M_SHIFT));
  2347. }
  2348. #endif
  2349. #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
  2350. } else if (child->thread.debug.dbcr2 & DBCR2_DAC12MODE) {
  2351. /* Both dac1 and dac2 are part of a range */
  2352. return -ENOSPC;
  2353. #endif
  2354. } else if ((dbcr_dac(child) & (DBCR_DAC2R | DBCR_DAC2W)) == 0) {
  2355. slot = 2;
  2356. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_READ)
  2357. dbcr_dac(child) |= DBCR_DAC2R;
  2358. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_WRITE)
  2359. dbcr_dac(child) |= DBCR_DAC2W;
  2360. child->thread.debug.dac2 = (unsigned long)bp_info->addr;
  2361. #if CONFIG_PPC_ADV_DEBUG_DVCS > 0
  2362. if (byte_enable) {
  2363. child->thread.debug.dvc2 =
  2364. (unsigned long)bp_info->condition_value;
  2365. child->thread.debug.dbcr2 |=
  2366. ((byte_enable << DBCR2_DVC2BE_SHIFT) |
  2367. (condition_mode << DBCR2_DVC2M_SHIFT));
  2368. }
  2369. #endif
  2370. } else
  2371. return -ENOSPC;
  2372. child->thread.debug.dbcr0 |= DBCR0_IDM;
  2373. child->thread.regs->msr |= MSR_DE;
  2374. return slot + 4;
  2375. }
  2376. static int del_dac(struct task_struct *child, int slot)
  2377. {
  2378. if (slot == 1) {
  2379. if ((dbcr_dac(child) & (DBCR_DAC1R | DBCR_DAC1W)) == 0)
  2380. return -ENOENT;
  2381. child->thread.debug.dac1 = 0;
  2382. dbcr_dac(child) &= ~(DBCR_DAC1R | DBCR_DAC1W);
  2383. #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
  2384. if (child->thread.debug.dbcr2 & DBCR2_DAC12MODE) {
  2385. child->thread.debug.dac2 = 0;
  2386. child->thread.debug.dbcr2 &= ~DBCR2_DAC12MODE;
  2387. }
  2388. child->thread.debug.dbcr2 &= ~(DBCR2_DVC1M | DBCR2_DVC1BE);
  2389. #endif
  2390. #if CONFIG_PPC_ADV_DEBUG_DVCS > 0
  2391. child->thread.debug.dvc1 = 0;
  2392. #endif
  2393. } else if (slot == 2) {
  2394. if ((dbcr_dac(child) & (DBCR_DAC2R | DBCR_DAC2W)) == 0)
  2395. return -ENOENT;
  2396. #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
  2397. if (child->thread.debug.dbcr2 & DBCR2_DAC12MODE)
  2398. /* Part of a range */
  2399. return -EINVAL;
  2400. child->thread.debug.dbcr2 &= ~(DBCR2_DVC2M | DBCR2_DVC2BE);
  2401. #endif
  2402. #if CONFIG_PPC_ADV_DEBUG_DVCS > 0
  2403. child->thread.debug.dvc2 = 0;
  2404. #endif
  2405. child->thread.debug.dac2 = 0;
  2406. dbcr_dac(child) &= ~(DBCR_DAC2R | DBCR_DAC2W);
  2407. } else
  2408. return -EINVAL;
  2409. return 0;
  2410. }
  2411. #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
  2412. #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
  2413. static int set_dac_range(struct task_struct *child,
  2414. struct ppc_hw_breakpoint *bp_info)
  2415. {
  2416. int mode = bp_info->addr_mode & PPC_BREAKPOINT_MODE_MASK;
  2417. /* We don't allow range watchpoints to be used with DVC */
  2418. if (bp_info->condition_mode)
  2419. return -EINVAL;
  2420. /*
  2421. * Best effort to verify the address range. The user/supervisor bits
  2422. * prevent trapping in kernel space, but let's fail on an obvious bad
  2423. * range. The simple test on the mask is not fool-proof, and any
  2424. * exclusive range will spill over into kernel space.
  2425. */
  2426. if (bp_info->addr >= TASK_SIZE)
  2427. return -EIO;
  2428. if (mode == PPC_BREAKPOINT_MODE_MASK) {
  2429. /*
  2430. * dac2 is a bitmask. Don't allow a mask that makes a
  2431. * kernel space address from a valid dac1 value
  2432. */
  2433. if (~((unsigned long)bp_info->addr2) >= TASK_SIZE)
  2434. return -EIO;
  2435. } else {
  2436. /*
  2437. * For range breakpoints, addr2 must also be a valid address
  2438. */
  2439. if (bp_info->addr2 >= TASK_SIZE)
  2440. return -EIO;
  2441. }
  2442. if (child->thread.debug.dbcr0 &
  2443. (DBCR0_DAC1R | DBCR0_DAC1W | DBCR0_DAC2R | DBCR0_DAC2W))
  2444. return -ENOSPC;
  2445. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_READ)
  2446. child->thread.debug.dbcr0 |= (DBCR0_DAC1R | DBCR0_IDM);
  2447. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_WRITE)
  2448. child->thread.debug.dbcr0 |= (DBCR0_DAC1W | DBCR0_IDM);
  2449. child->thread.debug.dac1 = bp_info->addr;
  2450. child->thread.debug.dac2 = bp_info->addr2;
  2451. if (mode == PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE)
  2452. child->thread.debug.dbcr2 |= DBCR2_DAC12M;
  2453. else if (mode == PPC_BREAKPOINT_MODE_RANGE_EXCLUSIVE)
  2454. child->thread.debug.dbcr2 |= DBCR2_DAC12MX;
  2455. else /* PPC_BREAKPOINT_MODE_MASK */
  2456. child->thread.debug.dbcr2 |= DBCR2_DAC12MM;
  2457. child->thread.regs->msr |= MSR_DE;
  2458. return 5;
  2459. }
  2460. #endif /* CONFIG_PPC_ADV_DEBUG_DAC_RANGE */
  2461. static long ppc_set_hwdebug(struct task_struct *child,
  2462. struct ppc_hw_breakpoint *bp_info)
  2463. {
  2464. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  2465. int len = 0;
  2466. struct thread_struct *thread = &(child->thread);
  2467. struct perf_event *bp;
  2468. struct perf_event_attr attr;
  2469. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  2470. #ifndef CONFIG_PPC_ADV_DEBUG_REGS
  2471. struct arch_hw_breakpoint brk;
  2472. #endif
  2473. if (bp_info->version != 1)
  2474. return -ENOTSUPP;
  2475. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  2476. /*
  2477. * Check for invalid flags and combinations
  2478. */
  2479. if ((bp_info->trigger_type == 0) ||
  2480. (bp_info->trigger_type & ~(PPC_BREAKPOINT_TRIGGER_EXECUTE |
  2481. PPC_BREAKPOINT_TRIGGER_RW)) ||
  2482. (bp_info->addr_mode & ~PPC_BREAKPOINT_MODE_MASK) ||
  2483. (bp_info->condition_mode &
  2484. ~(PPC_BREAKPOINT_CONDITION_MODE |
  2485. PPC_BREAKPOINT_CONDITION_BE_ALL)))
  2486. return -EINVAL;
  2487. #if CONFIG_PPC_ADV_DEBUG_DVCS == 0
  2488. if (bp_info->condition_mode != PPC_BREAKPOINT_CONDITION_NONE)
  2489. return -EINVAL;
  2490. #endif
  2491. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_EXECUTE) {
  2492. if ((bp_info->trigger_type != PPC_BREAKPOINT_TRIGGER_EXECUTE) ||
  2493. (bp_info->condition_mode != PPC_BREAKPOINT_CONDITION_NONE))
  2494. return -EINVAL;
  2495. return set_instruction_bp(child, bp_info);
  2496. }
  2497. if (bp_info->addr_mode == PPC_BREAKPOINT_MODE_EXACT)
  2498. return set_dac(child, bp_info);
  2499. #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
  2500. return set_dac_range(child, bp_info);
  2501. #else
  2502. return -EINVAL;
  2503. #endif
  2504. #else /* !CONFIG_PPC_ADV_DEBUG_DVCS */
  2505. /*
  2506. * We only support one data breakpoint
  2507. */
  2508. if ((bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_RW) == 0 ||
  2509. (bp_info->trigger_type & ~PPC_BREAKPOINT_TRIGGER_RW) != 0 ||
  2510. bp_info->condition_mode != PPC_BREAKPOINT_CONDITION_NONE)
  2511. return -EINVAL;
  2512. if ((unsigned long)bp_info->addr >= TASK_SIZE)
  2513. return -EIO;
  2514. brk.address = bp_info->addr & ~7UL;
  2515. brk.type = HW_BRK_TYPE_TRANSLATE;
  2516. brk.len = 8;
  2517. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_READ)
  2518. brk.type |= HW_BRK_TYPE_READ;
  2519. if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_WRITE)
  2520. brk.type |= HW_BRK_TYPE_WRITE;
  2521. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  2522. /*
  2523. * Check if the request is for 'range' breakpoints. We can
  2524. * support it if range < 8 bytes.
  2525. */
  2526. if (bp_info->addr_mode == PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE)
  2527. len = bp_info->addr2 - bp_info->addr;
  2528. else if (bp_info->addr_mode == PPC_BREAKPOINT_MODE_EXACT)
  2529. len = 1;
  2530. else
  2531. return -EINVAL;
  2532. bp = thread->ptrace_bps[0];
  2533. if (bp)
  2534. return -ENOSPC;
  2535. /* Create a new breakpoint request if one doesn't exist already */
  2536. hw_breakpoint_init(&attr);
  2537. attr.bp_addr = (unsigned long)bp_info->addr & ~HW_BREAKPOINT_ALIGN;
  2538. attr.bp_len = len;
  2539. arch_bp_generic_fields(brk.type, &attr.bp_type);
  2540. thread->ptrace_bps[0] = bp = register_user_hw_breakpoint(&attr,
  2541. ptrace_triggered, NULL, child);
  2542. if (IS_ERR(bp)) {
  2543. thread->ptrace_bps[0] = NULL;
  2544. return PTR_ERR(bp);
  2545. }
  2546. return 1;
  2547. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  2548. if (bp_info->addr_mode != PPC_BREAKPOINT_MODE_EXACT)
  2549. return -EINVAL;
  2550. if (child->thread.hw_brk.address)
  2551. return -ENOSPC;
  2552. if (!ppc_breakpoint_available())
  2553. return -ENODEV;
  2554. child->thread.hw_brk = brk;
  2555. return 1;
  2556. #endif /* !CONFIG_PPC_ADV_DEBUG_DVCS */
  2557. }
  2558. static long ppc_del_hwdebug(struct task_struct *child, long data)
  2559. {
  2560. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  2561. int ret = 0;
  2562. struct thread_struct *thread = &(child->thread);
  2563. struct perf_event *bp;
  2564. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  2565. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  2566. int rc;
  2567. if (data <= 4)
  2568. rc = del_instruction_bp(child, (int)data);
  2569. else
  2570. rc = del_dac(child, (int)data - 4);
  2571. if (!rc) {
  2572. if (!DBCR_ACTIVE_EVENTS(child->thread.debug.dbcr0,
  2573. child->thread.debug.dbcr1)) {
  2574. child->thread.debug.dbcr0 &= ~DBCR0_IDM;
  2575. child->thread.regs->msr &= ~MSR_DE;
  2576. }
  2577. }
  2578. return rc;
  2579. #else
  2580. if (data != 1)
  2581. return -EINVAL;
  2582. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  2583. bp = thread->ptrace_bps[0];
  2584. if (bp) {
  2585. unregister_hw_breakpoint(bp);
  2586. thread->ptrace_bps[0] = NULL;
  2587. } else
  2588. ret = -ENOENT;
  2589. return ret;
  2590. #else /* CONFIG_HAVE_HW_BREAKPOINT */
  2591. if (child->thread.hw_brk.address == 0)
  2592. return -ENOENT;
  2593. child->thread.hw_brk.address = 0;
  2594. child->thread.hw_brk.type = 0;
  2595. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  2596. return 0;
  2597. #endif
  2598. }
  2599. long arch_ptrace(struct task_struct *child, long request,
  2600. unsigned long addr, unsigned long data)
  2601. {
  2602. int ret = -EPERM;
  2603. void __user *datavp = (void __user *) data;
  2604. unsigned long __user *datalp = datavp;
  2605. switch (request) {
  2606. /* read the word at location addr in the USER area. */
  2607. case PTRACE_PEEKUSR: {
  2608. unsigned long index, tmp;
  2609. ret = -EIO;
  2610. /* convert to index and check */
  2611. #ifdef CONFIG_PPC32
  2612. index = addr >> 2;
  2613. if ((addr & 3) || (index > PT_FPSCR)
  2614. || (child->thread.regs == NULL))
  2615. #else
  2616. index = addr >> 3;
  2617. if ((addr & 7) || (index > PT_FPSCR))
  2618. #endif
  2619. break;
  2620. CHECK_FULL_REGS(child->thread.regs);
  2621. if (index < PT_FPR0) {
  2622. ret = ptrace_get_reg(child, (int) index, &tmp);
  2623. if (ret)
  2624. break;
  2625. } else {
  2626. unsigned int fpidx = index - PT_FPR0;
  2627. flush_fp_to_thread(child);
  2628. if (fpidx < (PT_FPSCR - PT_FPR0))
  2629. memcpy(&tmp, &child->thread.TS_FPR(fpidx),
  2630. sizeof(long));
  2631. else
  2632. tmp = child->thread.fp_state.fpscr;
  2633. }
  2634. ret = put_user(tmp, datalp);
  2635. break;
  2636. }
  2637. /* write the word at location addr in the USER area */
  2638. case PTRACE_POKEUSR: {
  2639. unsigned long index;
  2640. ret = -EIO;
  2641. /* convert to index and check */
  2642. #ifdef CONFIG_PPC32
  2643. index = addr >> 2;
  2644. if ((addr & 3) || (index > PT_FPSCR)
  2645. || (child->thread.regs == NULL))
  2646. #else
  2647. index = addr >> 3;
  2648. if ((addr & 7) || (index > PT_FPSCR))
  2649. #endif
  2650. break;
  2651. CHECK_FULL_REGS(child->thread.regs);
  2652. if (index < PT_FPR0) {
  2653. ret = ptrace_put_reg(child, index, data);
  2654. } else {
  2655. unsigned int fpidx = index - PT_FPR0;
  2656. flush_fp_to_thread(child);
  2657. if (fpidx < (PT_FPSCR - PT_FPR0))
  2658. memcpy(&child->thread.TS_FPR(fpidx), &data,
  2659. sizeof(long));
  2660. else
  2661. child->thread.fp_state.fpscr = data;
  2662. ret = 0;
  2663. }
  2664. break;
  2665. }
  2666. case PPC_PTRACE_GETHWDBGINFO: {
  2667. struct ppc_debug_info dbginfo;
  2668. dbginfo.version = 1;
  2669. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  2670. dbginfo.num_instruction_bps = CONFIG_PPC_ADV_DEBUG_IACS;
  2671. dbginfo.num_data_bps = CONFIG_PPC_ADV_DEBUG_DACS;
  2672. dbginfo.num_condition_regs = CONFIG_PPC_ADV_DEBUG_DVCS;
  2673. dbginfo.data_bp_alignment = 4;
  2674. dbginfo.sizeof_condition = 4;
  2675. dbginfo.features = PPC_DEBUG_FEATURE_INSN_BP_RANGE |
  2676. PPC_DEBUG_FEATURE_INSN_BP_MASK;
  2677. #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
  2678. dbginfo.features |=
  2679. PPC_DEBUG_FEATURE_DATA_BP_RANGE |
  2680. PPC_DEBUG_FEATURE_DATA_BP_MASK;
  2681. #endif
  2682. #else /* !CONFIG_PPC_ADV_DEBUG_REGS */
  2683. dbginfo.num_instruction_bps = 0;
  2684. if (ppc_breakpoint_available())
  2685. dbginfo.num_data_bps = 1;
  2686. else
  2687. dbginfo.num_data_bps = 0;
  2688. dbginfo.num_condition_regs = 0;
  2689. #ifdef CONFIG_PPC64
  2690. dbginfo.data_bp_alignment = 8;
  2691. #else
  2692. dbginfo.data_bp_alignment = 4;
  2693. #endif
  2694. dbginfo.sizeof_condition = 0;
  2695. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  2696. dbginfo.features = PPC_DEBUG_FEATURE_DATA_BP_RANGE;
  2697. if (cpu_has_feature(CPU_FTR_DAWR))
  2698. dbginfo.features |= PPC_DEBUG_FEATURE_DATA_BP_DAWR;
  2699. #else
  2700. dbginfo.features = 0;
  2701. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  2702. #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
  2703. if (copy_to_user(datavp, &dbginfo,
  2704. sizeof(struct ppc_debug_info)))
  2705. return -EFAULT;
  2706. return 0;
  2707. }
  2708. case PPC_PTRACE_SETHWDEBUG: {
  2709. struct ppc_hw_breakpoint bp_info;
  2710. if (copy_from_user(&bp_info, datavp,
  2711. sizeof(struct ppc_hw_breakpoint)))
  2712. return -EFAULT;
  2713. return ppc_set_hwdebug(child, &bp_info);
  2714. }
  2715. case PPC_PTRACE_DELHWDEBUG: {
  2716. ret = ppc_del_hwdebug(child, data);
  2717. break;
  2718. }
  2719. case PTRACE_GET_DEBUGREG: {
  2720. #ifndef CONFIG_PPC_ADV_DEBUG_REGS
  2721. unsigned long dabr_fake;
  2722. #endif
  2723. ret = -EINVAL;
  2724. /* We only support one DABR and no IABRS at the moment */
  2725. if (addr > 0)
  2726. break;
  2727. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  2728. ret = put_user(child->thread.debug.dac1, datalp);
  2729. #else
  2730. dabr_fake = ((child->thread.hw_brk.address & (~HW_BRK_TYPE_DABR)) |
  2731. (child->thread.hw_brk.type & HW_BRK_TYPE_DABR));
  2732. ret = put_user(dabr_fake, datalp);
  2733. #endif
  2734. break;
  2735. }
  2736. case PTRACE_SET_DEBUGREG:
  2737. ret = ptrace_set_debugreg(child, addr, data);
  2738. break;
  2739. #ifdef CONFIG_PPC64
  2740. case PTRACE_GETREGS64:
  2741. #endif
  2742. case PTRACE_GETREGS: /* Get all pt_regs from the child. */
  2743. return copy_regset_to_user(child, &user_ppc_native_view,
  2744. REGSET_GPR,
  2745. 0, sizeof(struct pt_regs),
  2746. datavp);
  2747. #ifdef CONFIG_PPC64
  2748. case PTRACE_SETREGS64:
  2749. #endif
  2750. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  2751. return copy_regset_from_user(child, &user_ppc_native_view,
  2752. REGSET_GPR,
  2753. 0, sizeof(struct pt_regs),
  2754. datavp);
  2755. case PTRACE_GETFPREGS: /* Get the child FPU state (FPR0...31 + FPSCR) */
  2756. return copy_regset_to_user(child, &user_ppc_native_view,
  2757. REGSET_FPR,
  2758. 0, sizeof(elf_fpregset_t),
  2759. datavp);
  2760. case PTRACE_SETFPREGS: /* Set the child FPU state (FPR0...31 + FPSCR) */
  2761. return copy_regset_from_user(child, &user_ppc_native_view,
  2762. REGSET_FPR,
  2763. 0, sizeof(elf_fpregset_t),
  2764. datavp);
  2765. #ifdef CONFIG_ALTIVEC
  2766. case PTRACE_GETVRREGS:
  2767. return copy_regset_to_user(child, &user_ppc_native_view,
  2768. REGSET_VMX,
  2769. 0, (33 * sizeof(vector128) +
  2770. sizeof(u32)),
  2771. datavp);
  2772. case PTRACE_SETVRREGS:
  2773. return copy_regset_from_user(child, &user_ppc_native_view,
  2774. REGSET_VMX,
  2775. 0, (33 * sizeof(vector128) +
  2776. sizeof(u32)),
  2777. datavp);
  2778. #endif
  2779. #ifdef CONFIG_VSX
  2780. case PTRACE_GETVSRREGS:
  2781. return copy_regset_to_user(child, &user_ppc_native_view,
  2782. REGSET_VSX,
  2783. 0, 32 * sizeof(double),
  2784. datavp);
  2785. case PTRACE_SETVSRREGS:
  2786. return copy_regset_from_user(child, &user_ppc_native_view,
  2787. REGSET_VSX,
  2788. 0, 32 * sizeof(double),
  2789. datavp);
  2790. #endif
  2791. #ifdef CONFIG_SPE
  2792. case PTRACE_GETEVRREGS:
  2793. /* Get the child spe register state. */
  2794. return copy_regset_to_user(child, &user_ppc_native_view,
  2795. REGSET_SPE, 0, 35 * sizeof(u32),
  2796. datavp);
  2797. case PTRACE_SETEVRREGS:
  2798. /* Set the child spe register state. */
  2799. return copy_regset_from_user(child, &user_ppc_native_view,
  2800. REGSET_SPE, 0, 35 * sizeof(u32),
  2801. datavp);
  2802. #endif
  2803. default:
  2804. ret = ptrace_request(child, request, addr, data);
  2805. break;
  2806. }
  2807. return ret;
  2808. }
  2809. #ifdef CONFIG_SECCOMP
  2810. static int do_seccomp(struct pt_regs *regs)
  2811. {
  2812. if (!test_thread_flag(TIF_SECCOMP))
  2813. return 0;
  2814. /*
  2815. * The ABI we present to seccomp tracers is that r3 contains
  2816. * the syscall return value and orig_gpr3 contains the first
  2817. * syscall parameter. This is different to the ptrace ABI where
  2818. * both r3 and orig_gpr3 contain the first syscall parameter.
  2819. */
  2820. regs->gpr[3] = -ENOSYS;
  2821. /*
  2822. * We use the __ version here because we have already checked
  2823. * TIF_SECCOMP. If this fails, there is nothing left to do, we
  2824. * have already loaded -ENOSYS into r3, or seccomp has put
  2825. * something else in r3 (via SECCOMP_RET_ERRNO/TRACE).
  2826. */
  2827. if (__secure_computing(NULL))
  2828. return -1;
  2829. /*
  2830. * The syscall was allowed by seccomp, restore the register
  2831. * state to what audit expects.
  2832. * Note that we use orig_gpr3, which means a seccomp tracer can
  2833. * modify the first syscall parameter (in orig_gpr3) and also
  2834. * allow the syscall to proceed.
  2835. */
  2836. regs->gpr[3] = regs->orig_gpr3;
  2837. return 0;
  2838. }
  2839. #else
  2840. static inline int do_seccomp(struct pt_regs *regs) { return 0; }
  2841. #endif /* CONFIG_SECCOMP */
  2842. /**
  2843. * do_syscall_trace_enter() - Do syscall tracing on kernel entry.
  2844. * @regs: the pt_regs of the task to trace (current)
  2845. *
  2846. * Performs various types of tracing on syscall entry. This includes seccomp,
  2847. * ptrace, syscall tracepoints and audit.
  2848. *
  2849. * The pt_regs are potentially visible to userspace via ptrace, so their
  2850. * contents is ABI.
  2851. *
  2852. * One or more of the tracers may modify the contents of pt_regs, in particular
  2853. * to modify arguments or even the syscall number itself.
  2854. *
  2855. * It's also possible that a tracer can choose to reject the system call. In
  2856. * that case this function will return an illegal syscall number, and will put
  2857. * an appropriate return value in regs->r3.
  2858. *
  2859. * Return: the (possibly changed) syscall number.
  2860. */
  2861. long do_syscall_trace_enter(struct pt_regs *regs)
  2862. {
  2863. user_exit();
  2864. /*
  2865. * The tracer may decide to abort the syscall, if so tracehook
  2866. * will return !0. Note that the tracer may also just change
  2867. * regs->gpr[0] to an invalid syscall number, that is handled
  2868. * below on the exit path.
  2869. */
  2870. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  2871. tracehook_report_syscall_entry(regs))
  2872. goto skip;
  2873. /* Run seccomp after ptrace; allow it to set gpr[3]. */
  2874. if (do_seccomp(regs))
  2875. return -1;
  2876. /* Avoid trace and audit when syscall is invalid. */
  2877. if (regs->gpr[0] >= NR_syscalls)
  2878. goto skip;
  2879. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  2880. trace_sys_enter(regs, regs->gpr[0]);
  2881. #ifdef CONFIG_PPC64
  2882. if (!is_32bit_task())
  2883. audit_syscall_entry(regs->gpr[0], regs->gpr[3], regs->gpr[4],
  2884. regs->gpr[5], regs->gpr[6]);
  2885. else
  2886. #endif
  2887. audit_syscall_entry(regs->gpr[0],
  2888. regs->gpr[3] & 0xffffffff,
  2889. regs->gpr[4] & 0xffffffff,
  2890. regs->gpr[5] & 0xffffffff,
  2891. regs->gpr[6] & 0xffffffff);
  2892. /* Return the possibly modified but valid syscall number */
  2893. return regs->gpr[0];
  2894. skip:
  2895. /*
  2896. * If we are aborting explicitly, or if the syscall number is
  2897. * now invalid, set the return value to -ENOSYS.
  2898. */
  2899. regs->gpr[3] = -ENOSYS;
  2900. return -1;
  2901. }
  2902. void do_syscall_trace_leave(struct pt_regs *regs)
  2903. {
  2904. int step;
  2905. audit_syscall_exit(regs);
  2906. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  2907. trace_sys_exit(regs, regs->result);
  2908. step = test_thread_flag(TIF_SINGLESTEP);
  2909. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  2910. tracehook_report_syscall_exit(regs, step);
  2911. user_enter();
  2912. }