xmon.c 53 KB

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