xmon.c 68 KB

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