xmon.c 68 KB

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