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