xmon.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926
  1. /*
  2. * Routines providing a simple monitor for use on the PowerMac.
  3. *
  4. * Copyright (C) 1996-2005 Paul Mackerras.
  5. * Copyright (C) 2001 PPC64 Team, IBM Corp
  6. * Copyrignt (C) 2006 Michael Ellerman, IBM Corp
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. #include <linux/errno.h>
  14. #include <linux/sched.h>
  15. #include <linux/smp.h>
  16. #include <linux/mm.h>
  17. #include <linux/reboot.h>
  18. #include <linux/delay.h>
  19. #include <linux/kallsyms.h>
  20. #include <linux/cpumask.h>
  21. #include <linux/module.h>
  22. #include <linux/sysrq.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/irq.h>
  25. #include <asm/ptrace.h>
  26. #include <asm/string.h>
  27. #include <asm/prom.h>
  28. #include <asm/machdep.h>
  29. #include <asm/xmon.h>
  30. #include <asm/processor.h>
  31. #include <asm/pgtable.h>
  32. #include <asm/mmu.h>
  33. #include <asm/mmu_context.h>
  34. #include <asm/cputable.h>
  35. #include <asm/rtas.h>
  36. #include <asm/sstep.h>
  37. #include <asm/bug.h>
  38. #include <asm/irq_regs.h>
  39. #include <asm/spu.h>
  40. #include <asm/spu_priv1.h>
  41. #include <asm/firmware.h>
  42. #ifdef CONFIG_PPC64
  43. #include <asm/hvcall.h>
  44. #include <asm/paca.h>
  45. #endif
  46. #include "nonstdio.h"
  47. #define scanhex xmon_scanhex
  48. #define skipbl xmon_skipbl
  49. #ifdef CONFIG_SMP
  50. cpumask_t cpus_in_xmon = CPU_MASK_NONE;
  51. static unsigned long xmon_taken = 1;
  52. static int xmon_owner;
  53. static int xmon_gate;
  54. #endif /* CONFIG_SMP */
  55. static unsigned long in_xmon = 0;
  56. static unsigned long adrs;
  57. static int size = 1;
  58. #define MAX_DUMP (128 * 1024)
  59. static unsigned long ndump = 64;
  60. static unsigned long nidump = 16;
  61. static unsigned long ncsum = 4096;
  62. static int termch;
  63. static char tmpstr[128];
  64. #define JMP_BUF_LEN 23
  65. static long bus_error_jmp[JMP_BUF_LEN];
  66. static int catch_memory_errors;
  67. static long *xmon_fault_jmp[NR_CPUS];
  68. #define setjmp xmon_setjmp
  69. #define longjmp xmon_longjmp
  70. /* Breakpoint stuff */
  71. struct bpt {
  72. unsigned long address;
  73. unsigned int instr[2];
  74. atomic_t ref_count;
  75. int enabled;
  76. unsigned long pad;
  77. };
  78. /* Bits in bpt.enabled */
  79. #define BP_IABR_TE 1 /* IABR translation enabled */
  80. #define BP_IABR 2
  81. #define BP_TRAP 8
  82. #define BP_DABR 0x10
  83. #define NBPTS 256
  84. static struct bpt bpts[NBPTS];
  85. static struct bpt dabr;
  86. static struct bpt *iabr;
  87. static unsigned bpinstr = 0x7fe00008; /* trap */
  88. #define BP_NUM(bp) ((bp) - bpts + 1)
  89. /* Prototypes */
  90. static int cmds(struct pt_regs *);
  91. static int mread(unsigned long, void *, int);
  92. static int mwrite(unsigned long, void *, int);
  93. static int handle_fault(struct pt_regs *);
  94. static void byterev(unsigned char *, int);
  95. static void memex(void);
  96. static int bsesc(void);
  97. static void dump(void);
  98. static void prdump(unsigned long, long);
  99. static int ppc_inst_dump(unsigned long, long, int);
  100. void print_address(unsigned long);
  101. static void backtrace(struct pt_regs *);
  102. static void excprint(struct pt_regs *);
  103. static void prregs(struct pt_regs *);
  104. static void memops(int);
  105. static void memlocate(void);
  106. static void memzcan(void);
  107. static void memdiffs(unsigned char *, unsigned char *, unsigned, unsigned);
  108. int skipbl(void);
  109. int scanhex(unsigned long *valp);
  110. static void scannl(void);
  111. static int hexdigit(int);
  112. void getstring(char *, int);
  113. static void flush_input(void);
  114. static int inchar(void);
  115. static void take_input(char *);
  116. static unsigned long read_spr(int);
  117. static void write_spr(int, unsigned long);
  118. static void super_regs(void);
  119. static void remove_bpts(void);
  120. static void insert_bpts(void);
  121. static void remove_cpu_bpts(void);
  122. static void insert_cpu_bpts(void);
  123. static struct bpt *at_breakpoint(unsigned long pc);
  124. static struct bpt *in_breakpoint_table(unsigned long pc, unsigned long *offp);
  125. static int do_step(struct pt_regs *);
  126. static void bpt_cmds(void);
  127. static void cacheflush(void);
  128. static int cpu_cmd(void);
  129. static void csum(void);
  130. static void bootcmds(void);
  131. static void proccall(void);
  132. void dump_segments(void);
  133. static void symbol_lookup(void);
  134. static void xmon_show_stack(unsigned long sp, unsigned long lr,
  135. unsigned long pc);
  136. static void xmon_print_symbol(unsigned long address, const char *mid,
  137. const char *after);
  138. static const char *getvecname(unsigned long vec);
  139. static int do_spu_cmd(void);
  140. int xmon_no_auto_backtrace;
  141. extern int print_insn_powerpc(unsigned long insn, unsigned long memaddr);
  142. extern int print_insn_spu(unsigned long insn, unsigned long memaddr);
  143. extern void xmon_enter(void);
  144. extern void xmon_leave(void);
  145. extern long setjmp(long *);
  146. extern void longjmp(long *, long);
  147. extern void xmon_save_regs(struct pt_regs *);
  148. #ifdef CONFIG_PPC64
  149. #define REG "%.16lx"
  150. #define REGS_PER_LINE 4
  151. #define LAST_VOLATILE 13
  152. #else
  153. #define REG "%.8lx"
  154. #define REGS_PER_LINE 8
  155. #define LAST_VOLATILE 12
  156. #endif
  157. #define GETWORD(v) (((v)[0] << 24) + ((v)[1] << 16) + ((v)[2] << 8) + (v)[3])
  158. #define isxdigit(c) (('0' <= (c) && (c) <= '9') \
  159. || ('a' <= (c) && (c) <= 'f') \
  160. || ('A' <= (c) && (c) <= 'F'))
  161. #define isalnum(c) (('0' <= (c) && (c) <= '9') \
  162. || ('a' <= (c) && (c) <= 'z') \
  163. || ('A' <= (c) && (c) <= 'Z'))
  164. #define isspace(c) (c == ' ' || c == '\t' || c == 10 || c == 13 || c == 0)
  165. static char *help_string = "\
  166. Commands:\n\
  167. b show breakpoints\n\
  168. bd set data breakpoint\n\
  169. bi set instruction breakpoint\n\
  170. bc clear breakpoint\n"
  171. #ifdef CONFIG_SMP
  172. "\
  173. c print cpus stopped in xmon\n\
  174. c# try to switch to cpu number h (in hex)\n"
  175. #endif
  176. "\
  177. C checksum\n\
  178. d dump bytes\n\
  179. di dump instructions\n\
  180. df dump float values\n\
  181. dd dump double values\n\
  182. dr dump stream of raw bytes\n\
  183. e print exception information\n\
  184. f flush cache\n\
  185. la lookup symbol+offset of specified address\n\
  186. ls lookup address of specified symbol\n\
  187. m examine/change memory\n\
  188. mm move a block of memory\n\
  189. ms set a block of memory\n\
  190. md compare two blocks of memory\n\
  191. ml locate a block of memory\n\
  192. mz zero a block of memory\n\
  193. mi show information about memory allocation\n\
  194. p call a procedure\n\
  195. r print registers\n\
  196. s single step\n"
  197. #ifdef CONFIG_PPC_CELL
  198. " ss stop execution on all spus\n\
  199. sr restore execution on stopped spus\n\
  200. sf # dump spu fields for spu # (in hex)\n\
  201. sd # dump spu local store for spu # (in hex)\
  202. sdi # disassemble spu local store for spu # (in hex)\n"
  203. #endif
  204. " S print special registers\n\
  205. t print backtrace\n\
  206. x exit monitor and recover\n\
  207. X exit monitor and dont recover\n"
  208. #ifdef CONFIG_PPC64
  209. " u dump segment table or SLB\n"
  210. #endif
  211. #ifdef CONFIG_PPC_STD_MMU_32
  212. " u dump segment registers\n"
  213. #endif
  214. " ? help\n"
  215. " zr reboot\n\
  216. zh halt\n"
  217. ;
  218. static struct pt_regs *xmon_regs;
  219. static inline void sync(void)
  220. {
  221. asm volatile("sync; isync");
  222. }
  223. static inline void store_inst(void *p)
  224. {
  225. asm volatile ("dcbst 0,%0; sync; icbi 0,%0; isync" : : "r" (p));
  226. }
  227. static inline void cflush(void *p)
  228. {
  229. asm volatile ("dcbf 0,%0; icbi 0,%0" : : "r" (p));
  230. }
  231. static inline void cinval(void *p)
  232. {
  233. asm volatile ("dcbi 0,%0; icbi 0,%0" : : "r" (p));
  234. }
  235. /*
  236. * Disable surveillance (the service processor watchdog function)
  237. * while we are in xmon.
  238. * XXX we should re-enable it when we leave. :)
  239. */
  240. #define SURVEILLANCE_TOKEN 9000
  241. static inline void disable_surveillance(void)
  242. {
  243. #ifdef CONFIG_PPC_PSERIES
  244. /* Since this can't be a module, args should end up below 4GB. */
  245. static struct rtas_args args;
  246. /*
  247. * At this point we have got all the cpus we can into
  248. * xmon, so there is hopefully no other cpu calling RTAS
  249. * at the moment, even though we don't take rtas.lock.
  250. * If we did try to take rtas.lock there would be a
  251. * real possibility of deadlock.
  252. */
  253. args.token = rtas_token("set-indicator");
  254. if (args.token == RTAS_UNKNOWN_SERVICE)
  255. return;
  256. args.nargs = 3;
  257. args.nret = 1;
  258. args.rets = &args.args[3];
  259. args.args[0] = SURVEILLANCE_TOKEN;
  260. args.args[1] = 0;
  261. args.args[2] = 0;
  262. enter_rtas(__pa(&args));
  263. #endif /* CONFIG_PPC_PSERIES */
  264. }
  265. #ifdef CONFIG_SMP
  266. static int xmon_speaker;
  267. static void get_output_lock(void)
  268. {
  269. int me = smp_processor_id() + 0x100;
  270. int last_speaker = 0, prev;
  271. long timeout;
  272. if (xmon_speaker == me)
  273. return;
  274. for (;;) {
  275. if (xmon_speaker == 0) {
  276. last_speaker = cmpxchg(&xmon_speaker, 0, me);
  277. if (last_speaker == 0)
  278. return;
  279. }
  280. timeout = 10000000;
  281. while (xmon_speaker == last_speaker) {
  282. if (--timeout > 0)
  283. continue;
  284. /* hostile takeover */
  285. prev = cmpxchg(&xmon_speaker, last_speaker, me);
  286. if (prev == last_speaker)
  287. return;
  288. break;
  289. }
  290. }
  291. }
  292. static void release_output_lock(void)
  293. {
  294. xmon_speaker = 0;
  295. }
  296. #endif
  297. static int xmon_core(struct pt_regs *regs, int fromipi)
  298. {
  299. int cmd = 0;
  300. unsigned long msr;
  301. struct bpt *bp;
  302. long recurse_jmp[JMP_BUF_LEN];
  303. unsigned long offset;
  304. #ifdef CONFIG_SMP
  305. int cpu;
  306. int secondary;
  307. unsigned long timeout;
  308. #endif
  309. msr = mfmsr();
  310. mtmsr(msr & ~MSR_EE); /* disable interrupts */
  311. bp = in_breakpoint_table(regs->nip, &offset);
  312. if (bp != NULL) {
  313. regs->nip = bp->address + offset;
  314. atomic_dec(&bp->ref_count);
  315. }
  316. remove_cpu_bpts();
  317. #ifdef CONFIG_SMP
  318. cpu = smp_processor_id();
  319. if (cpu_isset(cpu, cpus_in_xmon)) {
  320. get_output_lock();
  321. excprint(regs);
  322. printf("cpu 0x%x: Exception %lx %s in xmon, "
  323. "returning to main loop\n",
  324. cpu, regs->trap, getvecname(TRAP(regs)));
  325. release_output_lock();
  326. longjmp(xmon_fault_jmp[cpu], 1);
  327. }
  328. if (setjmp(recurse_jmp) != 0) {
  329. if (!in_xmon || !xmon_gate) {
  330. get_output_lock();
  331. printf("xmon: WARNING: bad recursive fault "
  332. "on cpu 0x%x\n", cpu);
  333. release_output_lock();
  334. goto waiting;
  335. }
  336. secondary = !(xmon_taken && cpu == xmon_owner);
  337. goto cmdloop;
  338. }
  339. xmon_fault_jmp[cpu] = recurse_jmp;
  340. cpu_set(cpu, cpus_in_xmon);
  341. bp = NULL;
  342. if ((regs->msr & (MSR_IR|MSR_PR|MSR_SF)) == (MSR_IR|MSR_SF))
  343. bp = at_breakpoint(regs->nip);
  344. if (bp || (regs->msr & MSR_RI) == 0)
  345. fromipi = 0;
  346. if (!fromipi) {
  347. get_output_lock();
  348. excprint(regs);
  349. if (bp) {
  350. printf("cpu 0x%x stopped at breakpoint 0x%x (",
  351. cpu, BP_NUM(bp));
  352. xmon_print_symbol(regs->nip, " ", ")\n");
  353. }
  354. if ((regs->msr & MSR_RI) == 0)
  355. printf("WARNING: exception is not recoverable, "
  356. "can't continue\n");
  357. release_output_lock();
  358. }
  359. waiting:
  360. secondary = 1;
  361. while (secondary && !xmon_gate) {
  362. if (in_xmon == 0) {
  363. if (fromipi)
  364. goto leave;
  365. secondary = test_and_set_bit(0, &in_xmon);
  366. }
  367. barrier();
  368. }
  369. if (!secondary && !xmon_gate) {
  370. /* we are the first cpu to come in */
  371. /* interrupt other cpu(s) */
  372. int ncpus = num_online_cpus();
  373. xmon_owner = cpu;
  374. mb();
  375. if (ncpus > 1) {
  376. smp_send_debugger_break(MSG_ALL_BUT_SELF);
  377. /* wait for other cpus to come in */
  378. for (timeout = 100000000; timeout != 0; --timeout) {
  379. if (cpus_weight(cpus_in_xmon) >= ncpus)
  380. break;
  381. barrier();
  382. }
  383. }
  384. remove_bpts();
  385. disable_surveillance();
  386. /* for breakpoint or single step, print the current instr. */
  387. if (bp || TRAP(regs) == 0xd00)
  388. ppc_inst_dump(regs->nip, 1, 0);
  389. printf("enter ? for help\n");
  390. mb();
  391. xmon_gate = 1;
  392. barrier();
  393. }
  394. cmdloop:
  395. while (in_xmon) {
  396. if (secondary) {
  397. if (cpu == xmon_owner) {
  398. if (!test_and_set_bit(0, &xmon_taken)) {
  399. secondary = 0;
  400. continue;
  401. }
  402. /* missed it */
  403. while (cpu == xmon_owner)
  404. barrier();
  405. }
  406. barrier();
  407. } else {
  408. cmd = cmds(regs);
  409. if (cmd != 0) {
  410. /* exiting xmon */
  411. insert_bpts();
  412. xmon_gate = 0;
  413. wmb();
  414. in_xmon = 0;
  415. break;
  416. }
  417. /* have switched to some other cpu */
  418. secondary = 1;
  419. }
  420. }
  421. leave:
  422. cpu_clear(cpu, cpus_in_xmon);
  423. xmon_fault_jmp[cpu] = NULL;
  424. #else
  425. /* UP is simple... */
  426. if (in_xmon) {
  427. printf("Exception %lx %s in xmon, returning to main loop\n",
  428. regs->trap, getvecname(TRAP(regs)));
  429. longjmp(xmon_fault_jmp[0], 1);
  430. }
  431. if (setjmp(recurse_jmp) == 0) {
  432. xmon_fault_jmp[0] = recurse_jmp;
  433. in_xmon = 1;
  434. excprint(regs);
  435. bp = at_breakpoint(regs->nip);
  436. if (bp) {
  437. printf("Stopped at breakpoint %x (", BP_NUM(bp));
  438. xmon_print_symbol(regs->nip, " ", ")\n");
  439. }
  440. if ((regs->msr & MSR_RI) == 0)
  441. printf("WARNING: exception is not recoverable, "
  442. "can't continue\n");
  443. remove_bpts();
  444. disable_surveillance();
  445. /* for breakpoint or single step, print the current instr. */
  446. if (bp || TRAP(regs) == 0xd00)
  447. ppc_inst_dump(regs->nip, 1, 0);
  448. printf("enter ? for help\n");
  449. }
  450. cmd = cmds(regs);
  451. insert_bpts();
  452. in_xmon = 0;
  453. #endif
  454. if ((regs->msr & (MSR_IR|MSR_PR|MSR_SF)) == (MSR_IR|MSR_SF)) {
  455. bp = at_breakpoint(regs->nip);
  456. if (bp != NULL) {
  457. int stepped = emulate_step(regs, bp->instr[0]);
  458. if (stepped == 0) {
  459. regs->nip = (unsigned long) &bp->instr[0];
  460. atomic_inc(&bp->ref_count);
  461. } else if (stepped < 0) {
  462. printf("Couldn't single-step %s instruction\n",
  463. (IS_RFID(bp->instr[0])? "rfid": "mtmsrd"));
  464. }
  465. }
  466. }
  467. insert_cpu_bpts();
  468. mtmsr(msr); /* restore interrupt enable */
  469. return cmd != 'X' && cmd != EOF;
  470. }
  471. int xmon(struct pt_regs *excp)
  472. {
  473. struct pt_regs regs;
  474. if (excp == NULL) {
  475. xmon_save_regs(&regs);
  476. excp = &regs;
  477. }
  478. return xmon_core(excp, 0);
  479. }
  480. EXPORT_SYMBOL(xmon);
  481. irqreturn_t xmon_irq(int irq, void *d)
  482. {
  483. unsigned long flags;
  484. local_irq_save(flags);
  485. printf("Keyboard interrupt\n");
  486. xmon(get_irq_regs());
  487. local_irq_restore(flags);
  488. return IRQ_HANDLED;
  489. }
  490. static int xmon_bpt(struct pt_regs *regs)
  491. {
  492. struct bpt *bp;
  493. unsigned long offset;
  494. if ((regs->msr & (MSR_IR|MSR_PR|MSR_SF)) != (MSR_IR|MSR_SF))
  495. return 0;
  496. /* Are we at the trap at bp->instr[1] for some bp? */
  497. bp = in_breakpoint_table(regs->nip, &offset);
  498. if (bp != NULL && offset == 4) {
  499. regs->nip = bp->address + 4;
  500. atomic_dec(&bp->ref_count);
  501. return 1;
  502. }
  503. /* Are we at a breakpoint? */
  504. bp = at_breakpoint(regs->nip);
  505. if (!bp)
  506. return 0;
  507. xmon_core(regs, 0);
  508. return 1;
  509. }
  510. static int xmon_sstep(struct pt_regs *regs)
  511. {
  512. if (user_mode(regs))
  513. return 0;
  514. xmon_core(regs, 0);
  515. return 1;
  516. }
  517. static int xmon_dabr_match(struct pt_regs *regs)
  518. {
  519. if ((regs->msr & (MSR_IR|MSR_PR|MSR_SF)) != (MSR_IR|MSR_SF))
  520. return 0;
  521. if (dabr.enabled == 0)
  522. return 0;
  523. xmon_core(regs, 0);
  524. return 1;
  525. }
  526. static int xmon_iabr_match(struct pt_regs *regs)
  527. {
  528. if ((regs->msr & (MSR_IR|MSR_PR|MSR_SF)) != (MSR_IR|MSR_SF))
  529. return 0;
  530. if (iabr == 0)
  531. return 0;
  532. xmon_core(regs, 0);
  533. return 1;
  534. }
  535. static int xmon_ipi(struct pt_regs *regs)
  536. {
  537. #ifdef CONFIG_SMP
  538. if (in_xmon && !cpu_isset(smp_processor_id(), cpus_in_xmon))
  539. xmon_core(regs, 1);
  540. #endif
  541. return 0;
  542. }
  543. static int xmon_fault_handler(struct pt_regs *regs)
  544. {
  545. struct bpt *bp;
  546. unsigned long offset;
  547. if (in_xmon && catch_memory_errors)
  548. handle_fault(regs); /* doesn't return */
  549. if ((regs->msr & (MSR_IR|MSR_PR|MSR_SF)) == (MSR_IR|MSR_SF)) {
  550. bp = in_breakpoint_table(regs->nip, &offset);
  551. if (bp != NULL) {
  552. regs->nip = bp->address + offset;
  553. atomic_dec(&bp->ref_count);
  554. }
  555. }
  556. return 0;
  557. }
  558. static struct bpt *at_breakpoint(unsigned long pc)
  559. {
  560. int i;
  561. struct bpt *bp;
  562. bp = bpts;
  563. for (i = 0; i < NBPTS; ++i, ++bp)
  564. if (bp->enabled && pc == bp->address)
  565. return bp;
  566. return NULL;
  567. }
  568. static struct bpt *in_breakpoint_table(unsigned long nip, unsigned long *offp)
  569. {
  570. unsigned long off;
  571. off = nip - (unsigned long) bpts;
  572. if (off >= sizeof(bpts))
  573. return NULL;
  574. off %= sizeof(struct bpt);
  575. if (off != offsetof(struct bpt, instr[0])
  576. && off != offsetof(struct bpt, instr[1]))
  577. return NULL;
  578. *offp = off - offsetof(struct bpt, instr[0]);
  579. return (struct bpt *) (nip - off);
  580. }
  581. static struct bpt *new_breakpoint(unsigned long a)
  582. {
  583. struct bpt *bp;
  584. a &= ~3UL;
  585. bp = at_breakpoint(a);
  586. if (bp)
  587. return bp;
  588. for (bp = bpts; bp < &bpts[NBPTS]; ++bp) {
  589. if (!bp->enabled && atomic_read(&bp->ref_count) == 0) {
  590. bp->address = a;
  591. bp->instr[1] = bpinstr;
  592. store_inst(&bp->instr[1]);
  593. return bp;
  594. }
  595. }
  596. printf("Sorry, no free breakpoints. Please clear one first.\n");
  597. return NULL;
  598. }
  599. static void insert_bpts(void)
  600. {
  601. int i;
  602. struct bpt *bp;
  603. bp = bpts;
  604. for (i = 0; i < NBPTS; ++i, ++bp) {
  605. if ((bp->enabled & (BP_TRAP|BP_IABR)) == 0)
  606. continue;
  607. if (mread(bp->address, &bp->instr[0], 4) != 4) {
  608. printf("Couldn't read instruction at %lx, "
  609. "disabling breakpoint there\n", bp->address);
  610. bp->enabled = 0;
  611. continue;
  612. }
  613. if (IS_MTMSRD(bp->instr[0]) || IS_RFID(bp->instr[0])) {
  614. printf("Breakpoint at %lx is on an mtmsrd or rfid "
  615. "instruction, disabling it\n", bp->address);
  616. bp->enabled = 0;
  617. continue;
  618. }
  619. store_inst(&bp->instr[0]);
  620. if (bp->enabled & BP_IABR)
  621. continue;
  622. if (mwrite(bp->address, &bpinstr, 4) != 4) {
  623. printf("Couldn't write instruction at %lx, "
  624. "disabling breakpoint there\n", bp->address);
  625. bp->enabled &= ~BP_TRAP;
  626. continue;
  627. }
  628. store_inst((void *)bp->address);
  629. }
  630. }
  631. static void insert_cpu_bpts(void)
  632. {
  633. if (dabr.enabled)
  634. set_dabr(dabr.address | (dabr.enabled & 7));
  635. if (iabr && cpu_has_feature(CPU_FTR_IABR))
  636. mtspr(SPRN_IABR, iabr->address
  637. | (iabr->enabled & (BP_IABR|BP_IABR_TE)));
  638. }
  639. static void remove_bpts(void)
  640. {
  641. int i;
  642. struct bpt *bp;
  643. unsigned instr;
  644. bp = bpts;
  645. for (i = 0; i < NBPTS; ++i, ++bp) {
  646. if ((bp->enabled & (BP_TRAP|BP_IABR)) != BP_TRAP)
  647. continue;
  648. if (mread(bp->address, &instr, 4) == 4
  649. && instr == bpinstr
  650. && mwrite(bp->address, &bp->instr, 4) != 4)
  651. printf("Couldn't remove breakpoint at %lx\n",
  652. bp->address);
  653. else
  654. store_inst((void *)bp->address);
  655. }
  656. }
  657. static void remove_cpu_bpts(void)
  658. {
  659. set_dabr(0);
  660. if (cpu_has_feature(CPU_FTR_IABR))
  661. mtspr(SPRN_IABR, 0);
  662. }
  663. /* Command interpreting routine */
  664. static char *last_cmd;
  665. static int
  666. cmds(struct pt_regs *excp)
  667. {
  668. int cmd = 0;
  669. last_cmd = NULL;
  670. xmon_regs = excp;
  671. if (!xmon_no_auto_backtrace) {
  672. xmon_no_auto_backtrace = 1;
  673. xmon_show_stack(excp->gpr[1], excp->link, excp->nip);
  674. }
  675. for(;;) {
  676. #ifdef CONFIG_SMP
  677. printf("%x:", smp_processor_id());
  678. #endif /* CONFIG_SMP */
  679. printf("mon> ");
  680. flush_input();
  681. termch = 0;
  682. cmd = skipbl();
  683. if( cmd == '\n' ) {
  684. if (last_cmd == NULL)
  685. continue;
  686. take_input(last_cmd);
  687. last_cmd = NULL;
  688. cmd = inchar();
  689. }
  690. switch (cmd) {
  691. case 'm':
  692. cmd = inchar();
  693. switch (cmd) {
  694. case 'm':
  695. case 's':
  696. case 'd':
  697. memops(cmd);
  698. break;
  699. case 'l':
  700. memlocate();
  701. break;
  702. case 'z':
  703. memzcan();
  704. break;
  705. case 'i':
  706. show_mem();
  707. break;
  708. default:
  709. termch = cmd;
  710. memex();
  711. }
  712. break;
  713. case 'd':
  714. dump();
  715. break;
  716. case 'l':
  717. symbol_lookup();
  718. break;
  719. case 'r':
  720. prregs(excp); /* print regs */
  721. break;
  722. case 'e':
  723. excprint(excp);
  724. break;
  725. case 'S':
  726. super_regs();
  727. break;
  728. case 't':
  729. backtrace(excp);
  730. break;
  731. case 'f':
  732. cacheflush();
  733. break;
  734. case 's':
  735. if (do_spu_cmd() == 0)
  736. break;
  737. if (do_step(excp))
  738. return cmd;
  739. break;
  740. case 'x':
  741. case 'X':
  742. return cmd;
  743. case EOF:
  744. printf(" <no input ...>\n");
  745. mdelay(2000);
  746. return cmd;
  747. case '?':
  748. printf(help_string);
  749. break;
  750. case 'b':
  751. bpt_cmds();
  752. break;
  753. case 'C':
  754. csum();
  755. break;
  756. case 'c':
  757. if (cpu_cmd())
  758. return 0;
  759. break;
  760. case 'z':
  761. bootcmds();
  762. break;
  763. case 'p':
  764. proccall();
  765. break;
  766. #ifdef CONFIG_PPC_STD_MMU
  767. case 'u':
  768. dump_segments();
  769. break;
  770. #endif
  771. default:
  772. printf("Unrecognized command: ");
  773. do {
  774. if (' ' < cmd && cmd <= '~')
  775. putchar(cmd);
  776. else
  777. printf("\\x%x", cmd);
  778. cmd = inchar();
  779. } while (cmd != '\n');
  780. printf(" (type ? for help)\n");
  781. break;
  782. }
  783. }
  784. }
  785. /*
  786. * Step a single instruction.
  787. * Some instructions we emulate, others we execute with MSR_SE set.
  788. */
  789. static int do_step(struct pt_regs *regs)
  790. {
  791. unsigned int instr;
  792. int stepped;
  793. /* check we are in 64-bit kernel mode, translation enabled */
  794. if ((regs->msr & (MSR_SF|MSR_PR|MSR_IR)) == (MSR_SF|MSR_IR)) {
  795. if (mread(regs->nip, &instr, 4) == 4) {
  796. stepped = emulate_step(regs, instr);
  797. if (stepped < 0) {
  798. printf("Couldn't single-step %s instruction\n",
  799. (IS_RFID(instr)? "rfid": "mtmsrd"));
  800. return 0;
  801. }
  802. if (stepped > 0) {
  803. regs->trap = 0xd00 | (regs->trap & 1);
  804. printf("stepped to ");
  805. xmon_print_symbol(regs->nip, " ", "\n");
  806. ppc_inst_dump(regs->nip, 1, 0);
  807. return 0;
  808. }
  809. }
  810. }
  811. regs->msr |= MSR_SE;
  812. return 1;
  813. }
  814. static void bootcmds(void)
  815. {
  816. int cmd;
  817. cmd = inchar();
  818. if (cmd == 'r')
  819. ppc_md.restart(NULL);
  820. else if (cmd == 'h')
  821. ppc_md.halt();
  822. else if (cmd == 'p')
  823. ppc_md.power_off();
  824. }
  825. static int cpu_cmd(void)
  826. {
  827. #ifdef CONFIG_SMP
  828. unsigned long cpu;
  829. int timeout;
  830. int count;
  831. if (!scanhex(&cpu)) {
  832. /* print cpus waiting or in xmon */
  833. printf("cpus stopped:");
  834. count = 0;
  835. for (cpu = 0; cpu < NR_CPUS; ++cpu) {
  836. if (cpu_isset(cpu, cpus_in_xmon)) {
  837. if (count == 0)
  838. printf(" %x", cpu);
  839. ++count;
  840. } else {
  841. if (count > 1)
  842. printf("-%x", cpu - 1);
  843. count = 0;
  844. }
  845. }
  846. if (count > 1)
  847. printf("-%x", NR_CPUS - 1);
  848. printf("\n");
  849. return 0;
  850. }
  851. /* try to switch to cpu specified */
  852. if (!cpu_isset(cpu, cpus_in_xmon)) {
  853. printf("cpu 0x%x isn't in xmon\n", cpu);
  854. return 0;
  855. }
  856. xmon_taken = 0;
  857. mb();
  858. xmon_owner = cpu;
  859. timeout = 10000000;
  860. while (!xmon_taken) {
  861. if (--timeout == 0) {
  862. if (test_and_set_bit(0, &xmon_taken))
  863. break;
  864. /* take control back */
  865. mb();
  866. xmon_owner = smp_processor_id();
  867. printf("cpu %u didn't take control\n", cpu);
  868. return 0;
  869. }
  870. barrier();
  871. }
  872. return 1;
  873. #else
  874. return 0;
  875. #endif /* CONFIG_SMP */
  876. }
  877. static unsigned short fcstab[256] = {
  878. 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
  879. 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
  880. 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
  881. 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
  882. 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
  883. 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
  884. 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
  885. 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
  886. 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
  887. 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
  888. 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
  889. 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
  890. 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
  891. 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
  892. 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
  893. 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
  894. 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
  895. 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
  896. 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
  897. 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
  898. 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
  899. 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
  900. 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
  901. 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
  902. 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
  903. 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
  904. 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
  905. 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
  906. 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
  907. 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
  908. 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
  909. 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
  910. };
  911. #define FCS(fcs, c) (((fcs) >> 8) ^ fcstab[((fcs) ^ (c)) & 0xff])
  912. static void
  913. csum(void)
  914. {
  915. unsigned int i;
  916. unsigned short fcs;
  917. unsigned char v;
  918. if (!scanhex(&adrs))
  919. return;
  920. if (!scanhex(&ncsum))
  921. return;
  922. fcs = 0xffff;
  923. for (i = 0; i < ncsum; ++i) {
  924. if (mread(adrs+i, &v, 1) == 0) {
  925. printf("csum stopped at %x\n", adrs+i);
  926. break;
  927. }
  928. fcs = FCS(fcs, v);
  929. }
  930. printf("%x\n", fcs);
  931. }
  932. /*
  933. * Check if this is a suitable place to put a breakpoint.
  934. */
  935. static long check_bp_loc(unsigned long addr)
  936. {
  937. unsigned int instr;
  938. addr &= ~3;
  939. if (!is_kernel_addr(addr)) {
  940. printf("Breakpoints may only be placed at kernel addresses\n");
  941. return 0;
  942. }
  943. if (!mread(addr, &instr, sizeof(instr))) {
  944. printf("Can't read instruction at address %lx\n", addr);
  945. return 0;
  946. }
  947. if (IS_MTMSRD(instr) || IS_RFID(instr)) {
  948. printf("Breakpoints may not be placed on mtmsrd or rfid "
  949. "instructions\n");
  950. return 0;
  951. }
  952. return 1;
  953. }
  954. static char *breakpoint_help_string =
  955. "Breakpoint command usage:\n"
  956. "b show breakpoints\n"
  957. "b <addr> [cnt] set breakpoint at given instr addr\n"
  958. "bc clear all breakpoints\n"
  959. "bc <n/addr> clear breakpoint number n or at addr\n"
  960. "bi <addr> [cnt] set hardware instr breakpoint (POWER3/RS64 only)\n"
  961. "bd <addr> [cnt] set hardware data breakpoint\n"
  962. "";
  963. static void
  964. bpt_cmds(void)
  965. {
  966. int cmd;
  967. unsigned long a;
  968. int mode, i;
  969. struct bpt *bp;
  970. const char badaddr[] = "Only kernel addresses are permitted "
  971. "for breakpoints\n";
  972. cmd = inchar();
  973. switch (cmd) {
  974. #ifndef CONFIG_8xx
  975. case 'd': /* bd - hardware data breakpoint */
  976. mode = 7;
  977. cmd = inchar();
  978. if (cmd == 'r')
  979. mode = 5;
  980. else if (cmd == 'w')
  981. mode = 6;
  982. else
  983. termch = cmd;
  984. dabr.address = 0;
  985. dabr.enabled = 0;
  986. if (scanhex(&dabr.address)) {
  987. if (!is_kernel_addr(dabr.address)) {
  988. printf(badaddr);
  989. break;
  990. }
  991. dabr.address &= ~7;
  992. dabr.enabled = mode | BP_DABR;
  993. }
  994. break;
  995. case 'i': /* bi - hardware instr breakpoint */
  996. if (!cpu_has_feature(CPU_FTR_IABR)) {
  997. printf("Hardware instruction breakpoint "
  998. "not supported on this cpu\n");
  999. break;
  1000. }
  1001. if (iabr) {
  1002. iabr->enabled &= ~(BP_IABR | BP_IABR_TE);
  1003. iabr = NULL;
  1004. }
  1005. if (!scanhex(&a))
  1006. break;
  1007. if (!check_bp_loc(a))
  1008. break;
  1009. bp = new_breakpoint(a);
  1010. if (bp != NULL) {
  1011. bp->enabled |= BP_IABR | BP_IABR_TE;
  1012. iabr = bp;
  1013. }
  1014. break;
  1015. #endif
  1016. case 'c':
  1017. if (!scanhex(&a)) {
  1018. /* clear all breakpoints */
  1019. for (i = 0; i < NBPTS; ++i)
  1020. bpts[i].enabled = 0;
  1021. iabr = NULL;
  1022. dabr.enabled = 0;
  1023. printf("All breakpoints cleared\n");
  1024. break;
  1025. }
  1026. if (a <= NBPTS && a >= 1) {
  1027. /* assume a breakpoint number */
  1028. bp = &bpts[a-1]; /* bp nums are 1 based */
  1029. } else {
  1030. /* assume a breakpoint address */
  1031. bp = at_breakpoint(a);
  1032. if (bp == 0) {
  1033. printf("No breakpoint at %x\n", a);
  1034. break;
  1035. }
  1036. }
  1037. printf("Cleared breakpoint %x (", BP_NUM(bp));
  1038. xmon_print_symbol(bp->address, " ", ")\n");
  1039. bp->enabled = 0;
  1040. break;
  1041. default:
  1042. termch = cmd;
  1043. cmd = skipbl();
  1044. if (cmd == '?') {
  1045. printf(breakpoint_help_string);
  1046. break;
  1047. }
  1048. termch = cmd;
  1049. if (!scanhex(&a)) {
  1050. /* print all breakpoints */
  1051. printf(" type address\n");
  1052. if (dabr.enabled) {
  1053. printf(" data "REG" [", dabr.address);
  1054. if (dabr.enabled & 1)
  1055. printf("r");
  1056. if (dabr.enabled & 2)
  1057. printf("w");
  1058. printf("]\n");
  1059. }
  1060. for (bp = bpts; bp < &bpts[NBPTS]; ++bp) {
  1061. if (!bp->enabled)
  1062. continue;
  1063. printf("%2x %s ", BP_NUM(bp),
  1064. (bp->enabled & BP_IABR)? "inst": "trap");
  1065. xmon_print_symbol(bp->address, " ", "\n");
  1066. }
  1067. break;
  1068. }
  1069. if (!check_bp_loc(a))
  1070. break;
  1071. bp = new_breakpoint(a);
  1072. if (bp != NULL)
  1073. bp->enabled |= BP_TRAP;
  1074. break;
  1075. }
  1076. }
  1077. /* Very cheap human name for vector lookup. */
  1078. static
  1079. const char *getvecname(unsigned long vec)
  1080. {
  1081. char *ret;
  1082. switch (vec) {
  1083. case 0x100: ret = "(System Reset)"; break;
  1084. case 0x200: ret = "(Machine Check)"; break;
  1085. case 0x300: ret = "(Data Access)"; break;
  1086. case 0x380: ret = "(Data SLB Access)"; break;
  1087. case 0x400: ret = "(Instruction Access)"; break;
  1088. case 0x480: ret = "(Instruction SLB Access)"; break;
  1089. case 0x500: ret = "(Hardware Interrupt)"; break;
  1090. case 0x600: ret = "(Alignment)"; break;
  1091. case 0x700: ret = "(Program Check)"; break;
  1092. case 0x800: ret = "(FPU Unavailable)"; break;
  1093. case 0x900: ret = "(Decrementer)"; break;
  1094. case 0xc00: ret = "(System Call)"; break;
  1095. case 0xd00: ret = "(Single Step)"; break;
  1096. case 0xf00: ret = "(Performance Monitor)"; break;
  1097. case 0xf20: ret = "(Altivec Unavailable)"; break;
  1098. case 0x1300: ret = "(Instruction Breakpoint)"; break;
  1099. default: ret = "";
  1100. }
  1101. return ret;
  1102. }
  1103. static void get_function_bounds(unsigned long pc, unsigned long *startp,
  1104. unsigned long *endp)
  1105. {
  1106. unsigned long size, offset;
  1107. const char *name;
  1108. char *modname;
  1109. *startp = *endp = 0;
  1110. if (pc == 0)
  1111. return;
  1112. if (setjmp(bus_error_jmp) == 0) {
  1113. catch_memory_errors = 1;
  1114. sync();
  1115. name = kallsyms_lookup(pc, &size, &offset, &modname, tmpstr);
  1116. if (name != NULL) {
  1117. *startp = pc - offset;
  1118. *endp = pc - offset + size;
  1119. }
  1120. sync();
  1121. }
  1122. catch_memory_errors = 0;
  1123. }
  1124. static int xmon_depth_to_print = 64;
  1125. #ifdef CONFIG_PPC64
  1126. #define LRSAVE_OFFSET 0x10
  1127. #define REG_FRAME_MARKER 0x7265677368657265ul /* "regshere" */
  1128. #define MARKER_OFFSET 0x60
  1129. #define REGS_OFFSET 0x70
  1130. #else
  1131. #define LRSAVE_OFFSET 4
  1132. #define REG_FRAME_MARKER 0x72656773
  1133. #define MARKER_OFFSET 8
  1134. #define REGS_OFFSET 16
  1135. #endif
  1136. static void xmon_show_stack(unsigned long sp, unsigned long lr,
  1137. unsigned long pc)
  1138. {
  1139. unsigned long ip;
  1140. unsigned long newsp;
  1141. unsigned long marker;
  1142. int count = 0;
  1143. struct pt_regs regs;
  1144. do {
  1145. if (sp < PAGE_OFFSET) {
  1146. if (sp != 0)
  1147. printf("SP (%lx) is in userspace\n", sp);
  1148. break;
  1149. }
  1150. if (!mread(sp + LRSAVE_OFFSET, &ip, sizeof(unsigned long))
  1151. || !mread(sp, &newsp, sizeof(unsigned long))) {
  1152. printf("Couldn't read stack frame at %lx\n", sp);
  1153. break;
  1154. }
  1155. /*
  1156. * For the first stack frame, try to work out if
  1157. * LR and/or the saved LR value in the bottommost
  1158. * stack frame are valid.
  1159. */
  1160. if ((pc | lr) != 0) {
  1161. unsigned long fnstart, fnend;
  1162. unsigned long nextip;
  1163. int printip = 1;
  1164. get_function_bounds(pc, &fnstart, &fnend);
  1165. nextip = 0;
  1166. if (newsp > sp)
  1167. mread(newsp + LRSAVE_OFFSET, &nextip,
  1168. sizeof(unsigned long));
  1169. if (lr == ip) {
  1170. if (lr < PAGE_OFFSET
  1171. || (fnstart <= lr && lr < fnend))
  1172. printip = 0;
  1173. } else if (lr == nextip) {
  1174. printip = 0;
  1175. } else if (lr >= PAGE_OFFSET
  1176. && !(fnstart <= lr && lr < fnend)) {
  1177. printf("[link register ] ");
  1178. xmon_print_symbol(lr, " ", "\n");
  1179. }
  1180. if (printip) {
  1181. printf("["REG"] ", sp);
  1182. xmon_print_symbol(ip, " ", " (unreliable)\n");
  1183. }
  1184. pc = lr = 0;
  1185. } else {
  1186. printf("["REG"] ", sp);
  1187. xmon_print_symbol(ip, " ", "\n");
  1188. }
  1189. /* Look for "regshere" marker to see if this is
  1190. an exception frame. */
  1191. if (mread(sp + MARKER_OFFSET, &marker, sizeof(unsigned long))
  1192. && marker == REG_FRAME_MARKER) {
  1193. if (mread(sp + REGS_OFFSET, &regs, sizeof(regs))
  1194. != sizeof(regs)) {
  1195. printf("Couldn't read registers at %lx\n",
  1196. sp + REGS_OFFSET);
  1197. break;
  1198. }
  1199. printf("--- Exception: %lx %s at ", regs.trap,
  1200. getvecname(TRAP(&regs)));
  1201. pc = regs.nip;
  1202. lr = regs.link;
  1203. xmon_print_symbol(pc, " ", "\n");
  1204. }
  1205. if (newsp == 0)
  1206. break;
  1207. sp = newsp;
  1208. } while (count++ < xmon_depth_to_print);
  1209. }
  1210. static void backtrace(struct pt_regs *excp)
  1211. {
  1212. unsigned long sp;
  1213. if (scanhex(&sp))
  1214. xmon_show_stack(sp, 0, 0);
  1215. else
  1216. xmon_show_stack(excp->gpr[1], excp->link, excp->nip);
  1217. scannl();
  1218. }
  1219. static void print_bug_trap(struct pt_regs *regs)
  1220. {
  1221. struct bug_entry *bug;
  1222. unsigned long addr;
  1223. if (regs->msr & MSR_PR)
  1224. return; /* not in kernel */
  1225. addr = regs->nip; /* address of trap instruction */
  1226. if (addr < PAGE_OFFSET)
  1227. return;
  1228. bug = find_bug(regs->nip);
  1229. if (bug == NULL)
  1230. return;
  1231. if (bug->line & BUG_WARNING_TRAP)
  1232. return;
  1233. printf("kernel BUG in %s at %s:%d!\n",
  1234. bug->function, bug->file, (unsigned int)bug->line);
  1235. }
  1236. void excprint(struct pt_regs *fp)
  1237. {
  1238. unsigned long trap;
  1239. #ifdef CONFIG_SMP
  1240. printf("cpu 0x%x: ", smp_processor_id());
  1241. #endif /* CONFIG_SMP */
  1242. trap = TRAP(fp);
  1243. printf("Vector: %lx %s at [%lx]\n", fp->trap, getvecname(trap), fp);
  1244. printf(" pc: ");
  1245. xmon_print_symbol(fp->nip, ": ", "\n");
  1246. printf(" lr: ", fp->link);
  1247. xmon_print_symbol(fp->link, ": ", "\n");
  1248. printf(" sp: %lx\n", fp->gpr[1]);
  1249. printf(" msr: %lx\n", fp->msr);
  1250. if (trap == 0x300 || trap == 0x380 || trap == 0x600) {
  1251. printf(" dar: %lx\n", fp->dar);
  1252. if (trap != 0x380)
  1253. printf(" dsisr: %lx\n", fp->dsisr);
  1254. }
  1255. printf(" current = 0x%lx\n", current);
  1256. #ifdef CONFIG_PPC64
  1257. printf(" paca = 0x%lx\n", get_paca());
  1258. #endif
  1259. if (current) {
  1260. printf(" pid = %ld, comm = %s\n",
  1261. current->pid, current->comm);
  1262. }
  1263. if (trap == 0x700)
  1264. print_bug_trap(fp);
  1265. }
  1266. void prregs(struct pt_regs *fp)
  1267. {
  1268. int n, trap;
  1269. unsigned long base;
  1270. struct pt_regs regs;
  1271. if (scanhex(&base)) {
  1272. if (setjmp(bus_error_jmp) == 0) {
  1273. catch_memory_errors = 1;
  1274. sync();
  1275. regs = *(struct pt_regs *)base;
  1276. sync();
  1277. __delay(200);
  1278. } else {
  1279. catch_memory_errors = 0;
  1280. printf("*** Error reading registers from "REG"\n",
  1281. base);
  1282. return;
  1283. }
  1284. catch_memory_errors = 0;
  1285. fp = &regs;
  1286. }
  1287. #ifdef CONFIG_PPC64
  1288. if (FULL_REGS(fp)) {
  1289. for (n = 0; n < 16; ++n)
  1290. printf("R%.2ld = "REG" R%.2ld = "REG"\n",
  1291. n, fp->gpr[n], n+16, fp->gpr[n+16]);
  1292. } else {
  1293. for (n = 0; n < 7; ++n)
  1294. printf("R%.2ld = "REG" R%.2ld = "REG"\n",
  1295. n, fp->gpr[n], n+7, fp->gpr[n+7]);
  1296. }
  1297. #else
  1298. for (n = 0; n < 32; ++n) {
  1299. printf("R%.2d = %.8x%s", n, fp->gpr[n],
  1300. (n & 3) == 3? "\n": " ");
  1301. if (n == 12 && !FULL_REGS(fp)) {
  1302. printf("\n");
  1303. break;
  1304. }
  1305. }
  1306. #endif
  1307. printf("pc = ");
  1308. xmon_print_symbol(fp->nip, " ", "\n");
  1309. printf("lr = ");
  1310. xmon_print_symbol(fp->link, " ", "\n");
  1311. printf("msr = "REG" cr = %.8lx\n", fp->msr, fp->ccr);
  1312. printf("ctr = "REG" xer = "REG" trap = %4lx\n",
  1313. fp->ctr, fp->xer, fp->trap);
  1314. trap = TRAP(fp);
  1315. if (trap == 0x300 || trap == 0x380 || trap == 0x600)
  1316. printf("dar = "REG" dsisr = %.8lx\n", fp->dar, fp->dsisr);
  1317. }
  1318. void cacheflush(void)
  1319. {
  1320. int cmd;
  1321. unsigned long nflush;
  1322. cmd = inchar();
  1323. if (cmd != 'i')
  1324. termch = cmd;
  1325. scanhex((void *)&adrs);
  1326. if (termch != '\n')
  1327. termch = 0;
  1328. nflush = 1;
  1329. scanhex(&nflush);
  1330. nflush = (nflush + L1_CACHE_BYTES - 1) / L1_CACHE_BYTES;
  1331. if (setjmp(bus_error_jmp) == 0) {
  1332. catch_memory_errors = 1;
  1333. sync();
  1334. if (cmd != 'i') {
  1335. for (; nflush > 0; --nflush, adrs += L1_CACHE_BYTES)
  1336. cflush((void *) adrs);
  1337. } else {
  1338. for (; nflush > 0; --nflush, adrs += L1_CACHE_BYTES)
  1339. cinval((void *) adrs);
  1340. }
  1341. sync();
  1342. /* wait a little while to see if we get a machine check */
  1343. __delay(200);
  1344. }
  1345. catch_memory_errors = 0;
  1346. }
  1347. unsigned long
  1348. read_spr(int n)
  1349. {
  1350. unsigned int instrs[2];
  1351. unsigned long (*code)(void);
  1352. unsigned long ret = -1UL;
  1353. #ifdef CONFIG_PPC64
  1354. unsigned long opd[3];
  1355. opd[0] = (unsigned long)instrs;
  1356. opd[1] = 0;
  1357. opd[2] = 0;
  1358. code = (unsigned long (*)(void)) opd;
  1359. #else
  1360. code = (unsigned long (*)(void)) instrs;
  1361. #endif
  1362. /* mfspr r3,n; blr */
  1363. instrs[0] = 0x7c6002a6 + ((n & 0x1F) << 16) + ((n & 0x3e0) << 6);
  1364. instrs[1] = 0x4e800020;
  1365. store_inst(instrs);
  1366. store_inst(instrs+1);
  1367. if (setjmp(bus_error_jmp) == 0) {
  1368. catch_memory_errors = 1;
  1369. sync();
  1370. ret = code();
  1371. sync();
  1372. /* wait a little while to see if we get a machine check */
  1373. __delay(200);
  1374. n = size;
  1375. }
  1376. return ret;
  1377. }
  1378. void
  1379. write_spr(int n, unsigned long val)
  1380. {
  1381. unsigned int instrs[2];
  1382. unsigned long (*code)(unsigned long);
  1383. #ifdef CONFIG_PPC64
  1384. unsigned long opd[3];
  1385. opd[0] = (unsigned long)instrs;
  1386. opd[1] = 0;
  1387. opd[2] = 0;
  1388. code = (unsigned long (*)(unsigned long)) opd;
  1389. #else
  1390. code = (unsigned long (*)(unsigned long)) instrs;
  1391. #endif
  1392. instrs[0] = 0x7c6003a6 + ((n & 0x1F) << 16) + ((n & 0x3e0) << 6);
  1393. instrs[1] = 0x4e800020;
  1394. store_inst(instrs);
  1395. store_inst(instrs+1);
  1396. if (setjmp(bus_error_jmp) == 0) {
  1397. catch_memory_errors = 1;
  1398. sync();
  1399. code(val);
  1400. sync();
  1401. /* wait a little while to see if we get a machine check */
  1402. __delay(200);
  1403. n = size;
  1404. }
  1405. }
  1406. static unsigned long regno;
  1407. extern char exc_prolog;
  1408. extern char dec_exc;
  1409. void super_regs(void)
  1410. {
  1411. int cmd;
  1412. unsigned long val;
  1413. cmd = skipbl();
  1414. if (cmd == '\n') {
  1415. unsigned long sp, toc;
  1416. asm("mr %0,1" : "=r" (sp) :);
  1417. asm("mr %0,2" : "=r" (toc) :);
  1418. printf("msr = "REG" sprg0= "REG"\n",
  1419. mfmsr(), mfspr(SPRN_SPRG0));
  1420. printf("pvr = "REG" sprg1= "REG"\n",
  1421. mfspr(SPRN_PVR), mfspr(SPRN_SPRG1));
  1422. printf("dec = "REG" sprg2= "REG"\n",
  1423. mfspr(SPRN_DEC), mfspr(SPRN_SPRG2));
  1424. printf("sp = "REG" sprg3= "REG"\n", sp, mfspr(SPRN_SPRG3));
  1425. printf("toc = "REG" dar = "REG"\n", toc, mfspr(SPRN_DAR));
  1426. #ifdef CONFIG_PPC_ISERIES
  1427. if (firmware_has_feature(FW_FEATURE_ISERIES)) {
  1428. struct paca_struct *ptrPaca;
  1429. struct lppaca *ptrLpPaca;
  1430. struct ItLpRegSave *ptrLpRegSave;
  1431. /* Dump out relevant Paca data areas. */
  1432. printf("Paca: \n");
  1433. ptrPaca = get_paca();
  1434. printf(" Local Processor Control Area (LpPaca): \n");
  1435. ptrLpPaca = ptrPaca->lppaca_ptr;
  1436. printf(" Saved Srr0=%.16lx Saved Srr1=%.16lx \n",
  1437. ptrLpPaca->saved_srr0, ptrLpPaca->saved_srr1);
  1438. printf(" Saved Gpr3=%.16lx Saved Gpr4=%.16lx \n",
  1439. ptrLpPaca->saved_gpr3, ptrLpPaca->saved_gpr4);
  1440. printf(" Saved Gpr5=%.16lx \n", ptrLpPaca->saved_gpr5);
  1441. printf(" Local Processor Register Save Area (LpRegSave): \n");
  1442. ptrLpRegSave = ptrPaca->reg_save_ptr;
  1443. printf(" Saved Sprg0=%.16lx Saved Sprg1=%.16lx \n",
  1444. ptrLpRegSave->xSPRG0, ptrLpRegSave->xSPRG0);
  1445. printf(" Saved Sprg2=%.16lx Saved Sprg3=%.16lx \n",
  1446. ptrLpRegSave->xSPRG2, ptrLpRegSave->xSPRG3);
  1447. printf(" Saved Msr =%.16lx Saved Nia =%.16lx \n",
  1448. ptrLpRegSave->xMSR, ptrLpRegSave->xNIA);
  1449. }
  1450. #endif
  1451. return;
  1452. }
  1453. scanhex(&regno);
  1454. switch (cmd) {
  1455. case 'w':
  1456. val = read_spr(regno);
  1457. scanhex(&val);
  1458. write_spr(regno, val);
  1459. /* fall through */
  1460. case 'r':
  1461. printf("spr %lx = %lx\n", regno, read_spr(regno));
  1462. break;
  1463. }
  1464. scannl();
  1465. }
  1466. /*
  1467. * Stuff for reading and writing memory safely
  1468. */
  1469. int
  1470. mread(unsigned long adrs, void *buf, int size)
  1471. {
  1472. volatile int n;
  1473. char *p, *q;
  1474. n = 0;
  1475. if (setjmp(bus_error_jmp) == 0) {
  1476. catch_memory_errors = 1;
  1477. sync();
  1478. p = (char *)adrs;
  1479. q = (char *)buf;
  1480. switch (size) {
  1481. case 2:
  1482. *(u16 *)q = *(u16 *)p;
  1483. break;
  1484. case 4:
  1485. *(u32 *)q = *(u32 *)p;
  1486. break;
  1487. case 8:
  1488. *(u64 *)q = *(u64 *)p;
  1489. break;
  1490. default:
  1491. for( ; n < size; ++n) {
  1492. *q++ = *p++;
  1493. sync();
  1494. }
  1495. }
  1496. sync();
  1497. /* wait a little while to see if we get a machine check */
  1498. __delay(200);
  1499. n = size;
  1500. }
  1501. catch_memory_errors = 0;
  1502. return n;
  1503. }
  1504. int
  1505. mwrite(unsigned long adrs, void *buf, int size)
  1506. {
  1507. volatile int n;
  1508. char *p, *q;
  1509. n = 0;
  1510. if (setjmp(bus_error_jmp) == 0) {
  1511. catch_memory_errors = 1;
  1512. sync();
  1513. p = (char *) adrs;
  1514. q = (char *) buf;
  1515. switch (size) {
  1516. case 2:
  1517. *(u16 *)p = *(u16 *)q;
  1518. break;
  1519. case 4:
  1520. *(u32 *)p = *(u32 *)q;
  1521. break;
  1522. case 8:
  1523. *(u64 *)p = *(u64 *)q;
  1524. break;
  1525. default:
  1526. for ( ; n < size; ++n) {
  1527. *p++ = *q++;
  1528. sync();
  1529. }
  1530. }
  1531. sync();
  1532. /* wait a little while to see if we get a machine check */
  1533. __delay(200);
  1534. n = size;
  1535. } else {
  1536. printf("*** Error writing address %x\n", adrs + n);
  1537. }
  1538. catch_memory_errors = 0;
  1539. return n;
  1540. }
  1541. static int fault_type;
  1542. static int fault_except;
  1543. static char *fault_chars[] = { "--", "**", "##" };
  1544. static int handle_fault(struct pt_regs *regs)
  1545. {
  1546. fault_except = TRAP(regs);
  1547. switch (TRAP(regs)) {
  1548. case 0x200:
  1549. fault_type = 0;
  1550. break;
  1551. case 0x300:
  1552. case 0x380:
  1553. fault_type = 1;
  1554. break;
  1555. default:
  1556. fault_type = 2;
  1557. }
  1558. longjmp(bus_error_jmp, 1);
  1559. return 0;
  1560. }
  1561. #define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t))
  1562. void
  1563. byterev(unsigned char *val, int size)
  1564. {
  1565. int t;
  1566. switch (size) {
  1567. case 2:
  1568. SWAP(val[0], val[1], t);
  1569. break;
  1570. case 4:
  1571. SWAP(val[0], val[3], t);
  1572. SWAP(val[1], val[2], t);
  1573. break;
  1574. case 8: /* is there really any use for this? */
  1575. SWAP(val[0], val[7], t);
  1576. SWAP(val[1], val[6], t);
  1577. SWAP(val[2], val[5], t);
  1578. SWAP(val[3], val[4], t);
  1579. break;
  1580. }
  1581. }
  1582. static int brev;
  1583. static int mnoread;
  1584. static char *memex_help_string =
  1585. "Memory examine command usage:\n"
  1586. "m [addr] [flags] examine/change memory\n"
  1587. " addr is optional. will start where left off.\n"
  1588. " flags may include chars from this set:\n"
  1589. " b modify by bytes (default)\n"
  1590. " w modify by words (2 byte)\n"
  1591. " l modify by longs (4 byte)\n"
  1592. " d modify by doubleword (8 byte)\n"
  1593. " r toggle reverse byte order mode\n"
  1594. " n do not read memory (for i/o spaces)\n"
  1595. " . ok to read (default)\n"
  1596. "NOTE: flags are saved as defaults\n"
  1597. "";
  1598. static char *memex_subcmd_help_string =
  1599. "Memory examine subcommands:\n"
  1600. " hexval write this val to current location\n"
  1601. " 'string' write chars from string to this location\n"
  1602. " ' increment address\n"
  1603. " ^ decrement address\n"
  1604. " / increment addr by 0x10. //=0x100, ///=0x1000, etc\n"
  1605. " \\ decrement addr by 0x10. \\\\=0x100, \\\\\\=0x1000, etc\n"
  1606. " ` clear no-read flag\n"
  1607. " ; stay at this addr\n"
  1608. " v change to byte mode\n"
  1609. " w change to word (2 byte) mode\n"
  1610. " l change to long (4 byte) mode\n"
  1611. " u change to doubleword (8 byte) mode\n"
  1612. " m addr change current addr\n"
  1613. " n toggle no-read flag\n"
  1614. " r toggle byte reverse flag\n"
  1615. " < count back up count bytes\n"
  1616. " > count skip forward count bytes\n"
  1617. " x exit this mode\n"
  1618. "";
  1619. void
  1620. memex(void)
  1621. {
  1622. int cmd, inc, i, nslash;
  1623. unsigned long n;
  1624. unsigned char val[16];
  1625. scanhex((void *)&adrs);
  1626. cmd = skipbl();
  1627. if (cmd == '?') {
  1628. printf(memex_help_string);
  1629. return;
  1630. } else {
  1631. termch = cmd;
  1632. }
  1633. last_cmd = "m\n";
  1634. while ((cmd = skipbl()) != '\n') {
  1635. switch( cmd ){
  1636. case 'b': size = 1; break;
  1637. case 'w': size = 2; break;
  1638. case 'l': size = 4; break;
  1639. case 'd': size = 8; break;
  1640. case 'r': brev = !brev; break;
  1641. case 'n': mnoread = 1; break;
  1642. case '.': mnoread = 0; break;
  1643. }
  1644. }
  1645. if( size <= 0 )
  1646. size = 1;
  1647. else if( size > 8 )
  1648. size = 8;
  1649. for(;;){
  1650. if (!mnoread)
  1651. n = mread(adrs, val, size);
  1652. printf(REG"%c", adrs, brev? 'r': ' ');
  1653. if (!mnoread) {
  1654. if (brev)
  1655. byterev(val, size);
  1656. putchar(' ');
  1657. for (i = 0; i < n; ++i)
  1658. printf("%.2x", val[i]);
  1659. for (; i < size; ++i)
  1660. printf("%s", fault_chars[fault_type]);
  1661. }
  1662. putchar(' ');
  1663. inc = size;
  1664. nslash = 0;
  1665. for(;;){
  1666. if( scanhex(&n) ){
  1667. for (i = 0; i < size; ++i)
  1668. val[i] = n >> (i * 8);
  1669. if (!brev)
  1670. byterev(val, size);
  1671. mwrite(adrs, val, size);
  1672. inc = size;
  1673. }
  1674. cmd = skipbl();
  1675. if (cmd == '\n')
  1676. break;
  1677. inc = 0;
  1678. switch (cmd) {
  1679. case '\'':
  1680. for(;;){
  1681. n = inchar();
  1682. if( n == '\\' )
  1683. n = bsesc();
  1684. else if( n == '\'' )
  1685. break;
  1686. for (i = 0; i < size; ++i)
  1687. val[i] = n >> (i * 8);
  1688. if (!brev)
  1689. byterev(val, size);
  1690. mwrite(adrs, val, size);
  1691. adrs += size;
  1692. }
  1693. adrs -= size;
  1694. inc = size;
  1695. break;
  1696. case ',':
  1697. adrs += size;
  1698. break;
  1699. case '.':
  1700. mnoread = 0;
  1701. break;
  1702. case ';':
  1703. break;
  1704. case 'x':
  1705. case EOF:
  1706. scannl();
  1707. return;
  1708. case 'b':
  1709. case 'v':
  1710. size = 1;
  1711. break;
  1712. case 'w':
  1713. size = 2;
  1714. break;
  1715. case 'l':
  1716. size = 4;
  1717. break;
  1718. case 'u':
  1719. size = 8;
  1720. break;
  1721. case '^':
  1722. adrs -= size;
  1723. break;
  1724. break;
  1725. case '/':
  1726. if (nslash > 0)
  1727. adrs -= 1 << nslash;
  1728. else
  1729. nslash = 0;
  1730. nslash += 4;
  1731. adrs += 1 << nslash;
  1732. break;
  1733. case '\\':
  1734. if (nslash < 0)
  1735. adrs += 1 << -nslash;
  1736. else
  1737. nslash = 0;
  1738. nslash -= 4;
  1739. adrs -= 1 << -nslash;
  1740. break;
  1741. case 'm':
  1742. scanhex((void *)&adrs);
  1743. break;
  1744. case 'n':
  1745. mnoread = 1;
  1746. break;
  1747. case 'r':
  1748. brev = !brev;
  1749. break;
  1750. case '<':
  1751. n = size;
  1752. scanhex(&n);
  1753. adrs -= n;
  1754. break;
  1755. case '>':
  1756. n = size;
  1757. scanhex(&n);
  1758. adrs += n;
  1759. break;
  1760. case '?':
  1761. printf(memex_subcmd_help_string);
  1762. break;
  1763. }
  1764. }
  1765. adrs += inc;
  1766. }
  1767. }
  1768. int
  1769. bsesc(void)
  1770. {
  1771. int c;
  1772. c = inchar();
  1773. switch( c ){
  1774. case 'n': c = '\n'; break;
  1775. case 'r': c = '\r'; break;
  1776. case 'b': c = '\b'; break;
  1777. case 't': c = '\t'; break;
  1778. }
  1779. return c;
  1780. }
  1781. static void xmon_rawdump (unsigned long adrs, long ndump)
  1782. {
  1783. long n, m, r, nr;
  1784. unsigned char temp[16];
  1785. for (n = ndump; n > 0;) {
  1786. r = n < 16? n: 16;
  1787. nr = mread(adrs, temp, r);
  1788. adrs += nr;
  1789. for (m = 0; m < r; ++m) {
  1790. if (m < nr)
  1791. printf("%.2x", temp[m]);
  1792. else
  1793. printf("%s", fault_chars[fault_type]);
  1794. }
  1795. n -= r;
  1796. if (nr < r)
  1797. break;
  1798. }
  1799. printf("\n");
  1800. }
  1801. #define isxdigit(c) (('0' <= (c) && (c) <= '9') \
  1802. || ('a' <= (c) && (c) <= 'f') \
  1803. || ('A' <= (c) && (c) <= 'F'))
  1804. void
  1805. dump(void)
  1806. {
  1807. int c;
  1808. c = inchar();
  1809. if ((isxdigit(c) && c != 'f' && c != 'd') || c == '\n')
  1810. termch = c;
  1811. scanhex((void *)&adrs);
  1812. if (termch != '\n')
  1813. termch = 0;
  1814. if (c == 'i') {
  1815. scanhex(&nidump);
  1816. if (nidump == 0)
  1817. nidump = 16;
  1818. else if (nidump > MAX_DUMP)
  1819. nidump = MAX_DUMP;
  1820. adrs += ppc_inst_dump(adrs, nidump, 1);
  1821. last_cmd = "di\n";
  1822. } else if (c == 'r') {
  1823. scanhex(&ndump);
  1824. if (ndump == 0)
  1825. ndump = 64;
  1826. xmon_rawdump(adrs, ndump);
  1827. adrs += ndump;
  1828. last_cmd = "dr\n";
  1829. } else {
  1830. scanhex(&ndump);
  1831. if (ndump == 0)
  1832. ndump = 64;
  1833. else if (ndump > MAX_DUMP)
  1834. ndump = MAX_DUMP;
  1835. prdump(adrs, ndump);
  1836. adrs += ndump;
  1837. last_cmd = "d\n";
  1838. }
  1839. }
  1840. void
  1841. prdump(unsigned long adrs, long ndump)
  1842. {
  1843. long n, m, c, r, nr;
  1844. unsigned char temp[16];
  1845. for (n = ndump; n > 0;) {
  1846. printf(REG, adrs);
  1847. putchar(' ');
  1848. r = n < 16? n: 16;
  1849. nr = mread(adrs, temp, r);
  1850. adrs += nr;
  1851. for (m = 0; m < r; ++m) {
  1852. if ((m & (sizeof(long) - 1)) == 0 && m > 0)
  1853. putchar(' ');
  1854. if (m < nr)
  1855. printf("%.2x", temp[m]);
  1856. else
  1857. printf("%s", fault_chars[fault_type]);
  1858. }
  1859. for (; m < 16; ++m) {
  1860. if ((m & (sizeof(long) - 1)) == 0)
  1861. putchar(' ');
  1862. printf(" ");
  1863. }
  1864. printf(" |");
  1865. for (m = 0; m < r; ++m) {
  1866. if (m < nr) {
  1867. c = temp[m];
  1868. putchar(' ' <= c && c <= '~'? c: '.');
  1869. } else
  1870. putchar(' ');
  1871. }
  1872. n -= r;
  1873. for (; m < 16; ++m)
  1874. putchar(' ');
  1875. printf("|\n");
  1876. if (nr < r)
  1877. break;
  1878. }
  1879. }
  1880. typedef int (*instruction_dump_func)(unsigned long inst, unsigned long addr);
  1881. int
  1882. generic_inst_dump(unsigned long adr, long count, int praddr,
  1883. instruction_dump_func dump_func)
  1884. {
  1885. int nr, dotted;
  1886. unsigned long first_adr;
  1887. unsigned long inst, last_inst = 0;
  1888. unsigned char val[4];
  1889. dotted = 0;
  1890. for (first_adr = adr; count > 0; --count, adr += 4) {
  1891. nr = mread(adr, val, 4);
  1892. if (nr == 0) {
  1893. if (praddr) {
  1894. const char *x = fault_chars[fault_type];
  1895. printf(REG" %s%s%s%s\n", adr, x, x, x, x);
  1896. }
  1897. break;
  1898. }
  1899. inst = GETWORD(val);
  1900. if (adr > first_adr && inst == last_inst) {
  1901. if (!dotted) {
  1902. printf(" ...\n");
  1903. dotted = 1;
  1904. }
  1905. continue;
  1906. }
  1907. dotted = 0;
  1908. last_inst = inst;
  1909. if (praddr)
  1910. printf(REG" %.8x", adr, inst);
  1911. printf("\t");
  1912. dump_func(inst, adr);
  1913. printf("\n");
  1914. }
  1915. return adr - first_adr;
  1916. }
  1917. int
  1918. ppc_inst_dump(unsigned long adr, long count, int praddr)
  1919. {
  1920. return generic_inst_dump(adr, count, praddr, print_insn_powerpc);
  1921. }
  1922. void
  1923. print_address(unsigned long addr)
  1924. {
  1925. xmon_print_symbol(addr, "\t# ", "");
  1926. }
  1927. /*
  1928. * Memory operations - move, set, print differences
  1929. */
  1930. static unsigned long mdest; /* destination address */
  1931. static unsigned long msrc; /* source address */
  1932. static unsigned long mval; /* byte value to set memory to */
  1933. static unsigned long mcount; /* # bytes to affect */
  1934. static unsigned long mdiffs; /* max # differences to print */
  1935. void
  1936. memops(int cmd)
  1937. {
  1938. scanhex((void *)&mdest);
  1939. if( termch != '\n' )
  1940. termch = 0;
  1941. scanhex((void *)(cmd == 's'? &mval: &msrc));
  1942. if( termch != '\n' )
  1943. termch = 0;
  1944. scanhex((void *)&mcount);
  1945. switch( cmd ){
  1946. case 'm':
  1947. memmove((void *)mdest, (void *)msrc, mcount);
  1948. break;
  1949. case 's':
  1950. memset((void *)mdest, mval, mcount);
  1951. break;
  1952. case 'd':
  1953. if( termch != '\n' )
  1954. termch = 0;
  1955. scanhex((void *)&mdiffs);
  1956. memdiffs((unsigned char *)mdest, (unsigned char *)msrc, mcount, mdiffs);
  1957. break;
  1958. }
  1959. }
  1960. void
  1961. memdiffs(unsigned char *p1, unsigned char *p2, unsigned nb, unsigned maxpr)
  1962. {
  1963. unsigned n, prt;
  1964. prt = 0;
  1965. for( n = nb; n > 0; --n )
  1966. if( *p1++ != *p2++ )
  1967. if( ++prt <= maxpr )
  1968. printf("%.16x %.2x # %.16x %.2x\n", p1 - 1,
  1969. p1[-1], p2 - 1, p2[-1]);
  1970. if( prt > maxpr )
  1971. printf("Total of %d differences\n", prt);
  1972. }
  1973. static unsigned mend;
  1974. static unsigned mask;
  1975. void
  1976. memlocate(void)
  1977. {
  1978. unsigned a, n;
  1979. unsigned char val[4];
  1980. last_cmd = "ml";
  1981. scanhex((void *)&mdest);
  1982. if (termch != '\n') {
  1983. termch = 0;
  1984. scanhex((void *)&mend);
  1985. if (termch != '\n') {
  1986. termch = 0;
  1987. scanhex((void *)&mval);
  1988. mask = ~0;
  1989. if (termch != '\n') termch = 0;
  1990. scanhex((void *)&mask);
  1991. }
  1992. }
  1993. n = 0;
  1994. for (a = mdest; a < mend; a += 4) {
  1995. if (mread(a, val, 4) == 4
  1996. && ((GETWORD(val) ^ mval) & mask) == 0) {
  1997. printf("%.16x: %.16x\n", a, GETWORD(val));
  1998. if (++n >= 10)
  1999. break;
  2000. }
  2001. }
  2002. }
  2003. static unsigned long mskip = 0x1000;
  2004. static unsigned long mlim = 0xffffffff;
  2005. void
  2006. memzcan(void)
  2007. {
  2008. unsigned char v;
  2009. unsigned a;
  2010. int ok, ook;
  2011. scanhex(&mdest);
  2012. if (termch != '\n') termch = 0;
  2013. scanhex(&mskip);
  2014. if (termch != '\n') termch = 0;
  2015. scanhex(&mlim);
  2016. ook = 0;
  2017. for (a = mdest; a < mlim; a += mskip) {
  2018. ok = mread(a, &v, 1);
  2019. if (ok && !ook) {
  2020. printf("%.8x .. ", a);
  2021. } else if (!ok && ook)
  2022. printf("%.8x\n", a - mskip);
  2023. ook = ok;
  2024. if (a + mskip < a)
  2025. break;
  2026. }
  2027. if (ook)
  2028. printf("%.8x\n", a - mskip);
  2029. }
  2030. void proccall(void)
  2031. {
  2032. unsigned long args[8];
  2033. unsigned long ret;
  2034. int i;
  2035. typedef unsigned long (*callfunc_t)(unsigned long, unsigned long,
  2036. unsigned long, unsigned long, unsigned long,
  2037. unsigned long, unsigned long, unsigned long);
  2038. callfunc_t func;
  2039. if (!scanhex(&adrs))
  2040. return;
  2041. if (termch != '\n')
  2042. termch = 0;
  2043. for (i = 0; i < 8; ++i)
  2044. args[i] = 0;
  2045. for (i = 0; i < 8; ++i) {
  2046. if (!scanhex(&args[i]) || termch == '\n')
  2047. break;
  2048. termch = 0;
  2049. }
  2050. func = (callfunc_t) adrs;
  2051. ret = 0;
  2052. if (setjmp(bus_error_jmp) == 0) {
  2053. catch_memory_errors = 1;
  2054. sync();
  2055. ret = func(args[0], args[1], args[2], args[3],
  2056. args[4], args[5], args[6], args[7]);
  2057. sync();
  2058. printf("return value is %x\n", ret);
  2059. } else {
  2060. printf("*** %x exception occurred\n", fault_except);
  2061. }
  2062. catch_memory_errors = 0;
  2063. }
  2064. /* Input scanning routines */
  2065. int
  2066. skipbl(void)
  2067. {
  2068. int c;
  2069. if( termch != 0 ){
  2070. c = termch;
  2071. termch = 0;
  2072. } else
  2073. c = inchar();
  2074. while( c == ' ' || c == '\t' )
  2075. c = inchar();
  2076. return c;
  2077. }
  2078. #define N_PTREGS 44
  2079. static char *regnames[N_PTREGS] = {
  2080. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  2081. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  2082. "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  2083. "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
  2084. "pc", "msr", "or3", "ctr", "lr", "xer", "ccr",
  2085. #ifdef CONFIG_PPC64
  2086. "softe",
  2087. #else
  2088. "mq",
  2089. #endif
  2090. "trap", "dar", "dsisr", "res"
  2091. };
  2092. int
  2093. scanhex(unsigned long *vp)
  2094. {
  2095. int c, d;
  2096. unsigned long v;
  2097. c = skipbl();
  2098. if (c == '%') {
  2099. /* parse register name */
  2100. char regname[8];
  2101. int i;
  2102. for (i = 0; i < sizeof(regname) - 1; ++i) {
  2103. c = inchar();
  2104. if (!isalnum(c)) {
  2105. termch = c;
  2106. break;
  2107. }
  2108. regname[i] = c;
  2109. }
  2110. regname[i] = 0;
  2111. for (i = 0; i < N_PTREGS; ++i) {
  2112. if (strcmp(regnames[i], regname) == 0) {
  2113. if (xmon_regs == NULL) {
  2114. printf("regs not available\n");
  2115. return 0;
  2116. }
  2117. *vp = ((unsigned long *)xmon_regs)[i];
  2118. return 1;
  2119. }
  2120. }
  2121. printf("invalid register name '%%%s'\n", regname);
  2122. return 0;
  2123. }
  2124. /* skip leading "0x" if any */
  2125. if (c == '0') {
  2126. c = inchar();
  2127. if (c == 'x') {
  2128. c = inchar();
  2129. } else {
  2130. d = hexdigit(c);
  2131. if (d == EOF) {
  2132. termch = c;
  2133. *vp = 0;
  2134. return 1;
  2135. }
  2136. }
  2137. } else if (c == '$') {
  2138. int i;
  2139. for (i=0; i<63; i++) {
  2140. c = inchar();
  2141. if (isspace(c)) {
  2142. termch = c;
  2143. break;
  2144. }
  2145. tmpstr[i] = c;
  2146. }
  2147. tmpstr[i++] = 0;
  2148. *vp = 0;
  2149. if (setjmp(bus_error_jmp) == 0) {
  2150. catch_memory_errors = 1;
  2151. sync();
  2152. *vp = kallsyms_lookup_name(tmpstr);
  2153. sync();
  2154. }
  2155. catch_memory_errors = 0;
  2156. if (!(*vp)) {
  2157. printf("unknown symbol '%s'\n", tmpstr);
  2158. return 0;
  2159. }
  2160. return 1;
  2161. }
  2162. d = hexdigit(c);
  2163. if (d == EOF) {
  2164. termch = c;
  2165. return 0;
  2166. }
  2167. v = 0;
  2168. do {
  2169. v = (v << 4) + d;
  2170. c = inchar();
  2171. d = hexdigit(c);
  2172. } while (d != EOF);
  2173. termch = c;
  2174. *vp = v;
  2175. return 1;
  2176. }
  2177. void
  2178. scannl(void)
  2179. {
  2180. int c;
  2181. c = termch;
  2182. termch = 0;
  2183. while( c != '\n' )
  2184. c = inchar();
  2185. }
  2186. int hexdigit(int c)
  2187. {
  2188. if( '0' <= c && c <= '9' )
  2189. return c - '0';
  2190. if( 'A' <= c && c <= 'F' )
  2191. return c - ('A' - 10);
  2192. if( 'a' <= c && c <= 'f' )
  2193. return c - ('a' - 10);
  2194. return EOF;
  2195. }
  2196. void
  2197. getstring(char *s, int size)
  2198. {
  2199. int c;
  2200. c = skipbl();
  2201. do {
  2202. if( size > 1 ){
  2203. *s++ = c;
  2204. --size;
  2205. }
  2206. c = inchar();
  2207. } while( c != ' ' && c != '\t' && c != '\n' );
  2208. termch = c;
  2209. *s = 0;
  2210. }
  2211. static char line[256];
  2212. static char *lineptr;
  2213. void
  2214. flush_input(void)
  2215. {
  2216. lineptr = NULL;
  2217. }
  2218. int
  2219. inchar(void)
  2220. {
  2221. if (lineptr == NULL || *lineptr == 0) {
  2222. if (xmon_gets(line, sizeof(line)) == NULL) {
  2223. lineptr = NULL;
  2224. return EOF;
  2225. }
  2226. lineptr = line;
  2227. }
  2228. return *lineptr++;
  2229. }
  2230. void
  2231. take_input(char *str)
  2232. {
  2233. lineptr = str;
  2234. }
  2235. static void
  2236. symbol_lookup(void)
  2237. {
  2238. int type = inchar();
  2239. unsigned long addr;
  2240. static char tmp[64];
  2241. switch (type) {
  2242. case 'a':
  2243. if (scanhex(&addr))
  2244. xmon_print_symbol(addr, ": ", "\n");
  2245. termch = 0;
  2246. break;
  2247. case 's':
  2248. getstring(tmp, 64);
  2249. if (setjmp(bus_error_jmp) == 0) {
  2250. catch_memory_errors = 1;
  2251. sync();
  2252. addr = kallsyms_lookup_name(tmp);
  2253. if (addr)
  2254. printf("%s: %lx\n", tmp, addr);
  2255. else
  2256. printf("Symbol '%s' not found.\n", tmp);
  2257. sync();
  2258. }
  2259. catch_memory_errors = 0;
  2260. termch = 0;
  2261. break;
  2262. }
  2263. }
  2264. /* Print an address in numeric and symbolic form (if possible) */
  2265. static void xmon_print_symbol(unsigned long address, const char *mid,
  2266. const char *after)
  2267. {
  2268. char *modname;
  2269. const char *name = NULL;
  2270. unsigned long offset, size;
  2271. printf(REG, address);
  2272. if (setjmp(bus_error_jmp) == 0) {
  2273. catch_memory_errors = 1;
  2274. sync();
  2275. name = kallsyms_lookup(address, &size, &offset, &modname,
  2276. tmpstr);
  2277. sync();
  2278. /* wait a little while to see if we get a machine check */
  2279. __delay(200);
  2280. }
  2281. catch_memory_errors = 0;
  2282. if (name) {
  2283. printf("%s%s+%#lx/%#lx", mid, name, offset, size);
  2284. if (modname)
  2285. printf(" [%s]", modname);
  2286. }
  2287. printf("%s", after);
  2288. }
  2289. #ifdef CONFIG_PPC64
  2290. static void dump_slb(void)
  2291. {
  2292. int i;
  2293. unsigned long tmp;
  2294. printf("SLB contents of cpu %x\n", smp_processor_id());
  2295. for (i = 0; i < SLB_NUM_ENTRIES; i++) {
  2296. asm volatile("slbmfee %0,%1" : "=r" (tmp) : "r" (i));
  2297. printf("%02d %016lx ", i, tmp);
  2298. asm volatile("slbmfev %0,%1" : "=r" (tmp) : "r" (i));
  2299. printf("%016lx\n", tmp);
  2300. }
  2301. }
  2302. static void dump_stab(void)
  2303. {
  2304. int i;
  2305. unsigned long *tmp = (unsigned long *)get_paca()->stab_addr;
  2306. printf("Segment table contents of cpu %x\n", smp_processor_id());
  2307. for (i = 0; i < PAGE_SIZE/16; i++) {
  2308. unsigned long a, b;
  2309. a = *tmp++;
  2310. b = *tmp++;
  2311. if (a || b) {
  2312. printf("%03d %016lx ", i, a);
  2313. printf("%016lx\n", b);
  2314. }
  2315. }
  2316. }
  2317. void dump_segments(void)
  2318. {
  2319. if (cpu_has_feature(CPU_FTR_SLB))
  2320. dump_slb();
  2321. else
  2322. dump_stab();
  2323. }
  2324. #endif
  2325. #ifdef CONFIG_PPC_STD_MMU_32
  2326. void dump_segments(void)
  2327. {
  2328. int i;
  2329. printf("sr0-15 =");
  2330. for (i = 0; i < 16; ++i)
  2331. printf(" %x", mfsrin(i));
  2332. printf("\n");
  2333. }
  2334. #endif
  2335. void xmon_init(int enable)
  2336. {
  2337. if (enable) {
  2338. __debugger = xmon;
  2339. __debugger_ipi = xmon_ipi;
  2340. __debugger_bpt = xmon_bpt;
  2341. __debugger_sstep = xmon_sstep;
  2342. __debugger_iabr_match = xmon_iabr_match;
  2343. __debugger_dabr_match = xmon_dabr_match;
  2344. __debugger_fault_handler = xmon_fault_handler;
  2345. } else {
  2346. __debugger = NULL;
  2347. __debugger_ipi = NULL;
  2348. __debugger_bpt = NULL;
  2349. __debugger_sstep = NULL;
  2350. __debugger_iabr_match = NULL;
  2351. __debugger_dabr_match = NULL;
  2352. __debugger_fault_handler = NULL;
  2353. }
  2354. xmon_map_scc();
  2355. }
  2356. #ifdef CONFIG_MAGIC_SYSRQ
  2357. static void sysrq_handle_xmon(int key, struct tty_struct *tty)
  2358. {
  2359. /* ensure xmon is enabled */
  2360. xmon_init(1);
  2361. debugger(get_irq_regs());
  2362. }
  2363. static struct sysrq_key_op sysrq_xmon_op =
  2364. {
  2365. .handler = sysrq_handle_xmon,
  2366. .help_msg = "Xmon",
  2367. .action_msg = "Entering xmon",
  2368. };
  2369. static int __init setup_xmon_sysrq(void)
  2370. {
  2371. register_sysrq_key('x', &sysrq_xmon_op);
  2372. return 0;
  2373. }
  2374. __initcall(setup_xmon_sysrq);
  2375. #endif /* CONFIG_MAGIC_SYSRQ */
  2376. int __initdata xmon_early, xmon_off;
  2377. static int __init early_parse_xmon(char *p)
  2378. {
  2379. if (!p || strncmp(p, "early", 5) == 0) {
  2380. /* just "xmon" is equivalent to "xmon=early" */
  2381. xmon_init(1);
  2382. xmon_early = 1;
  2383. } else if (strncmp(p, "on", 2) == 0)
  2384. xmon_init(1);
  2385. else if (strncmp(p, "off", 3) == 0)
  2386. xmon_off = 1;
  2387. else if (strncmp(p, "nobt", 4) == 0)
  2388. xmon_no_auto_backtrace = 1;
  2389. else
  2390. return 1;
  2391. return 0;
  2392. }
  2393. early_param("xmon", early_parse_xmon);
  2394. void __init xmon_setup(void)
  2395. {
  2396. #ifdef CONFIG_XMON_DEFAULT
  2397. if (!xmon_off)
  2398. xmon_init(1);
  2399. #endif
  2400. if (xmon_early)
  2401. debugger(NULL);
  2402. }
  2403. #ifdef CONFIG_PPC_CELL
  2404. struct spu_info {
  2405. struct spu *spu;
  2406. u64 saved_mfc_sr1_RW;
  2407. u32 saved_spu_runcntl_RW;
  2408. unsigned long dump_addr;
  2409. u8 stopped_ok;
  2410. };
  2411. #define XMON_NUM_SPUS 16 /* Enough for current hardware */
  2412. static struct spu_info spu_info[XMON_NUM_SPUS];
  2413. void xmon_register_spus(struct list_head *list)
  2414. {
  2415. struct spu *spu;
  2416. list_for_each_entry(spu, list, full_list) {
  2417. if (spu->number >= XMON_NUM_SPUS) {
  2418. WARN_ON(1);
  2419. continue;
  2420. }
  2421. spu_info[spu->number].spu = spu;
  2422. spu_info[spu->number].stopped_ok = 0;
  2423. spu_info[spu->number].dump_addr = (unsigned long)
  2424. spu_info[spu->number].spu->local_store;
  2425. }
  2426. }
  2427. static void stop_spus(void)
  2428. {
  2429. struct spu *spu;
  2430. int i;
  2431. u64 tmp;
  2432. for (i = 0; i < XMON_NUM_SPUS; i++) {
  2433. if (!spu_info[i].spu)
  2434. continue;
  2435. if (setjmp(bus_error_jmp) == 0) {
  2436. catch_memory_errors = 1;
  2437. sync();
  2438. spu = spu_info[i].spu;
  2439. spu_info[i].saved_spu_runcntl_RW =
  2440. in_be32(&spu->problem->spu_runcntl_RW);
  2441. tmp = spu_mfc_sr1_get(spu);
  2442. spu_info[i].saved_mfc_sr1_RW = tmp;
  2443. tmp &= ~MFC_STATE1_MASTER_RUN_CONTROL_MASK;
  2444. spu_mfc_sr1_set(spu, tmp);
  2445. sync();
  2446. __delay(200);
  2447. spu_info[i].stopped_ok = 1;
  2448. printf("Stopped spu %.2d (was %s)\n", i,
  2449. spu_info[i].saved_spu_runcntl_RW ?
  2450. "running" : "stopped");
  2451. } else {
  2452. catch_memory_errors = 0;
  2453. printf("*** Error stopping spu %.2d\n", i);
  2454. }
  2455. catch_memory_errors = 0;
  2456. }
  2457. }
  2458. static void restart_spus(void)
  2459. {
  2460. struct spu *spu;
  2461. int i;
  2462. for (i = 0; i < XMON_NUM_SPUS; i++) {
  2463. if (!spu_info[i].spu)
  2464. continue;
  2465. if (!spu_info[i].stopped_ok) {
  2466. printf("*** Error, spu %d was not successfully stopped"
  2467. ", not restarting\n", i);
  2468. continue;
  2469. }
  2470. if (setjmp(bus_error_jmp) == 0) {
  2471. catch_memory_errors = 1;
  2472. sync();
  2473. spu = spu_info[i].spu;
  2474. spu_mfc_sr1_set(spu, spu_info[i].saved_mfc_sr1_RW);
  2475. out_be32(&spu->problem->spu_runcntl_RW,
  2476. spu_info[i].saved_spu_runcntl_RW);
  2477. sync();
  2478. __delay(200);
  2479. printf("Restarted spu %.2d\n", i);
  2480. } else {
  2481. catch_memory_errors = 0;
  2482. printf("*** Error restarting spu %.2d\n", i);
  2483. }
  2484. catch_memory_errors = 0;
  2485. }
  2486. }
  2487. #define DUMP_WIDTH 23
  2488. #define DUMP_VALUE(format, field, value) \
  2489. do { \
  2490. if (setjmp(bus_error_jmp) == 0) { \
  2491. catch_memory_errors = 1; \
  2492. sync(); \
  2493. printf(" %-*s = "format"\n", DUMP_WIDTH, \
  2494. #field, value); \
  2495. sync(); \
  2496. __delay(200); \
  2497. } else { \
  2498. catch_memory_errors = 0; \
  2499. printf(" %-*s = *** Error reading field.\n", \
  2500. DUMP_WIDTH, #field); \
  2501. } \
  2502. catch_memory_errors = 0; \
  2503. } while (0)
  2504. #define DUMP_FIELD(obj, format, field) \
  2505. DUMP_VALUE(format, field, obj->field)
  2506. static void dump_spu_fields(struct spu *spu)
  2507. {
  2508. printf("Dumping spu fields at address %p:\n", spu);
  2509. DUMP_FIELD(spu, "0x%x", number);
  2510. DUMP_FIELD(spu, "%s", name);
  2511. DUMP_FIELD(spu, "%s", devnode->full_name);
  2512. DUMP_FIELD(spu, "0x%x", nid);
  2513. DUMP_FIELD(spu, "0x%lx", local_store_phys);
  2514. DUMP_FIELD(spu, "0x%p", local_store);
  2515. DUMP_FIELD(spu, "0x%lx", ls_size);
  2516. DUMP_FIELD(spu, "0x%x", node);
  2517. DUMP_FIELD(spu, "0x%lx", flags);
  2518. DUMP_FIELD(spu, "0x%lx", dar);
  2519. DUMP_FIELD(spu, "0x%lx", dsisr);
  2520. DUMP_FIELD(spu, "%d", class_0_pending);
  2521. DUMP_FIELD(spu, "0x%lx", irqs[0]);
  2522. DUMP_FIELD(spu, "0x%lx", irqs[1]);
  2523. DUMP_FIELD(spu, "0x%lx", irqs[2]);
  2524. DUMP_FIELD(spu, "0x%x", slb_replace);
  2525. DUMP_FIELD(spu, "%d", pid);
  2526. DUMP_FIELD(spu, "%d", prio);
  2527. DUMP_FIELD(spu, "0x%p", mm);
  2528. DUMP_FIELD(spu, "0x%p", ctx);
  2529. DUMP_FIELD(spu, "0x%p", rq);
  2530. DUMP_FIELD(spu, "0x%p", timestamp);
  2531. DUMP_FIELD(spu, "0x%lx", problem_phys);
  2532. DUMP_FIELD(spu, "0x%p", problem);
  2533. DUMP_VALUE("0x%x", problem->spu_runcntl_RW,
  2534. in_be32(&spu->problem->spu_runcntl_RW));
  2535. DUMP_VALUE("0x%x", problem->spu_status_R,
  2536. in_be32(&spu->problem->spu_status_R));
  2537. DUMP_VALUE("0x%x", problem->spu_npc_RW,
  2538. in_be32(&spu->problem->spu_npc_RW));
  2539. DUMP_FIELD(spu, "0x%p", priv1);
  2540. if (spu->priv1) {
  2541. DUMP_VALUE("0x%lx", priv1->mfc_sr1_RW,
  2542. in_be64(&spu->priv1->mfc_sr1_RW));
  2543. }
  2544. DUMP_FIELD(spu, "0x%p", priv2);
  2545. }
  2546. int
  2547. spu_inst_dump(unsigned long adr, long count, int praddr)
  2548. {
  2549. return generic_inst_dump(adr, count, praddr, print_insn_spu);
  2550. }
  2551. static void dump_spu_ls(unsigned long num, int subcmd)
  2552. {
  2553. unsigned long offset, addr, ls_addr;
  2554. if (setjmp(bus_error_jmp) == 0) {
  2555. catch_memory_errors = 1;
  2556. sync();
  2557. ls_addr = (unsigned long)spu_info[num].spu->local_store;
  2558. sync();
  2559. __delay(200);
  2560. } else {
  2561. catch_memory_errors = 0;
  2562. printf("*** Error: accessing spu info for spu %d\n", num);
  2563. return;
  2564. }
  2565. catch_memory_errors = 0;
  2566. if (scanhex(&offset))
  2567. addr = ls_addr + offset;
  2568. else
  2569. addr = spu_info[num].dump_addr;
  2570. if (addr >= ls_addr + LS_SIZE) {
  2571. printf("*** Error: address outside of local store\n");
  2572. return;
  2573. }
  2574. switch (subcmd) {
  2575. case 'i':
  2576. addr += spu_inst_dump(addr, 16, 1);
  2577. last_cmd = "sdi\n";
  2578. break;
  2579. default:
  2580. prdump(addr, 64);
  2581. addr += 64;
  2582. last_cmd = "sd\n";
  2583. break;
  2584. }
  2585. spu_info[num].dump_addr = addr;
  2586. }
  2587. static int do_spu_cmd(void)
  2588. {
  2589. static unsigned long num = 0;
  2590. int cmd, subcmd = 0;
  2591. cmd = inchar();
  2592. switch (cmd) {
  2593. case 's':
  2594. stop_spus();
  2595. break;
  2596. case 'r':
  2597. restart_spus();
  2598. break;
  2599. case 'd':
  2600. subcmd = inchar();
  2601. if (isxdigit(subcmd) || subcmd == '\n')
  2602. termch = subcmd;
  2603. case 'f':
  2604. scanhex(&num);
  2605. if (num >= XMON_NUM_SPUS || !spu_info[num].spu) {
  2606. printf("*** Error: invalid spu number\n");
  2607. return 0;
  2608. }
  2609. switch (cmd) {
  2610. case 'f':
  2611. dump_spu_fields(spu_info[num].spu);
  2612. break;
  2613. default:
  2614. dump_spu_ls(num, subcmd);
  2615. break;
  2616. }
  2617. break;
  2618. default:
  2619. return -1;
  2620. }
  2621. return 0;
  2622. }
  2623. #else /* ! CONFIG_PPC_CELL */
  2624. static int do_spu_cmd(void)
  2625. {
  2626. return -1;
  2627. }
  2628. #endif