xmon.c 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Routines providing a simple monitor for use on the PowerMac.
  4. *
  5. * Copyright (C) 1996-2005 Paul Mackerras.
  6. * Copyright (C) 2001 PPC64 Team, IBM Corp
  7. * Copyrignt (C) 2006 Michael Ellerman, IBM Corp
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/errno.h>
  11. #include <linux/sched/signal.h>
  12. #include <linux/smp.h>
  13. #include <linux/mm.h>
  14. #include <linux/reboot.h>
  15. #include <linux/delay.h>
  16. #include <linux/kallsyms.h>
  17. #include <linux/kmsg_dump.h>
  18. #include <linux/cpumask.h>
  19. #include <linux/export.h>
  20. #include <linux/sysrq.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/irq.h>
  23. #include <linux/bug.h>
  24. #include <linux/nmi.h>
  25. #include <linux/ctype.h>
  26. #include <linux/highmem.h>
  27. #include <linux/security.h>
  28. #include <linux/debugfs.h>
  29. #include <asm/ptrace.h>
  30. #include <asm/smp.h>
  31. #include <asm/string.h>
  32. #include <asm/machdep.h>
  33. #include <asm/xmon.h>
  34. #include <asm/processor.h>
  35. #include <asm/mmu.h>
  36. #include <asm/mmu_context.h>
  37. #include <asm/plpar_wrappers.h>
  38. #include <asm/cputable.h>
  39. #include <asm/rtas.h>
  40. #include <asm/sstep.h>
  41. #include <asm/irq_regs.h>
  42. #include <asm/spu.h>
  43. #include <asm/spu_priv1.h>
  44. #include <asm/setjmp.h>
  45. #include <asm/reg.h>
  46. #include <asm/debug.h>
  47. #include <asm/hw_breakpoint.h>
  48. #include <asm/xive.h>
  49. #include <asm/opal.h>
  50. #include <asm/firmware.h>
  51. #include <asm/code-patching.h>
  52. #include <asm/sections.h>
  53. #include <asm/inst.h>
  54. #include <asm/interrupt.h>
  55. #ifdef CONFIG_PPC64
  56. #include <asm/hvcall.h>
  57. #include <asm/paca.h>
  58. #include <asm/lppaca.h>
  59. #endif
  60. #include "nonstdio.h"
  61. #include "dis-asm.h"
  62. #include "xmon_bpts.h"
  63. #ifdef CONFIG_SMP
  64. static cpumask_t cpus_in_xmon = CPU_MASK_NONE;
  65. static unsigned long xmon_taken = 1;
  66. static int xmon_owner;
  67. static int xmon_gate;
  68. static int xmon_batch;
  69. static unsigned long xmon_batch_start_cpu;
  70. static cpumask_t xmon_batch_cpus = CPU_MASK_NONE;
  71. #else
  72. #define xmon_owner 0
  73. #endif /* CONFIG_SMP */
  74. #ifdef CONFIG_PPC_PSERIES
  75. static int set_indicator_token = RTAS_UNKNOWN_SERVICE;
  76. #endif
  77. static unsigned long in_xmon __read_mostly = 0;
  78. static int xmon_on = IS_ENABLED(CONFIG_XMON_DEFAULT);
  79. static bool xmon_is_ro = IS_ENABLED(CONFIG_XMON_DEFAULT_RO_MODE);
  80. static unsigned long adrs;
  81. static int size = 1;
  82. #define MAX_DUMP (64 * 1024)
  83. static unsigned long ndump = 64;
  84. #define MAX_IDUMP (MAX_DUMP >> 2)
  85. static unsigned long nidump = 16;
  86. static unsigned long ncsum = 4096;
  87. static int termch;
  88. static char tmpstr[KSYM_NAME_LEN];
  89. static int tracing_enabled;
  90. static long bus_error_jmp[JMP_BUF_LEN];
  91. static int catch_memory_errors;
  92. static int catch_spr_faults;
  93. static long *xmon_fault_jmp[NR_CPUS];
  94. /* Breakpoint stuff */
  95. struct bpt {
  96. unsigned long address;
  97. u32 *instr;
  98. atomic_t ref_count;
  99. int enabled;
  100. unsigned long pad;
  101. };
  102. /* Bits in bpt.enabled */
  103. #define BP_CIABR 1
  104. #define BP_TRAP 2
  105. #define BP_DABR 4
  106. static struct bpt bpts[NBPTS];
  107. static struct bpt dabr[HBP_NUM_MAX];
  108. static struct bpt *iabr;
  109. static unsigned int bpinstr = PPC_RAW_TRAP();
  110. #define BP_NUM(bp) ((bp) - bpts + 1)
  111. /* Prototypes */
  112. static int cmds(struct pt_regs *);
  113. static int mread(unsigned long, void *, int);
  114. static int mwrite(unsigned long, void *, int);
  115. static int mread_instr(unsigned long, ppc_inst_t *);
  116. static int handle_fault(struct pt_regs *);
  117. static void byterev(unsigned char *, int);
  118. static void memex(void);
  119. static int bsesc(void);
  120. static void dump(void);
  121. static void show_pte(unsigned long);
  122. static void prdump(unsigned long, long);
  123. static int ppc_inst_dump(unsigned long, long, int);
  124. static void dump_log_buf(void);
  125. #ifdef CONFIG_SMP
  126. static int xmon_switch_cpu(unsigned long);
  127. static int xmon_batch_next_cpu(void);
  128. static int batch_cmds(struct pt_regs *);
  129. #endif
  130. #ifdef CONFIG_PPC_POWERNV
  131. static void dump_opal_msglog(void);
  132. #else
  133. static inline void dump_opal_msglog(void)
  134. {
  135. printf("Machine is not running OPAL firmware.\n");
  136. }
  137. #endif
  138. static void backtrace(struct pt_regs *);
  139. static void excprint(struct pt_regs *);
  140. static void prregs(struct pt_regs *);
  141. static void memops(int);
  142. static void memlocate(void);
  143. static void memzcan(void);
  144. static void memdiffs(unsigned char *, unsigned char *, unsigned, unsigned);
  145. int skipbl(void);
  146. int scanhex(unsigned long *valp);
  147. static void scannl(void);
  148. static int hexdigit(int);
  149. void getstring(char *, int);
  150. static void flush_input(void);
  151. static int inchar(void);
  152. static void take_input(char *);
  153. static int read_spr(int, unsigned long *);
  154. static void write_spr(int, unsigned long);
  155. static void super_regs(void);
  156. static void remove_bpts(void);
  157. static void insert_bpts(void);
  158. static void remove_cpu_bpts(void);
  159. static void insert_cpu_bpts(void);
  160. static struct bpt *at_breakpoint(unsigned long pc);
  161. static struct bpt *in_breakpoint_table(unsigned long pc, unsigned long *offp);
  162. static int do_step(struct pt_regs *);
  163. static void bpt_cmds(void);
  164. static void cacheflush(void);
  165. static int cpu_cmd(void);
  166. static void csum(void);
  167. static void bootcmds(void);
  168. static void proccall(void);
  169. static void show_tasks(void);
  170. void dump_segments(void);
  171. static void symbol_lookup(void);
  172. static void xmon_show_stack(unsigned long sp, unsigned long lr,
  173. unsigned long pc);
  174. static void xmon_print_symbol(unsigned long address, const char *mid,
  175. const char *after);
  176. static const char *getvecname(unsigned long vec);
  177. static int do_spu_cmd(void);
  178. #ifdef CONFIG_44x
  179. static void dump_tlb_44x(void);
  180. #endif
  181. #ifdef CONFIG_PPC_BOOK3E_64
  182. static void dump_tlb_book3e(void);
  183. #endif
  184. static void clear_all_bpt(void);
  185. #ifdef CONFIG_PPC64
  186. #define REG "%.16lx"
  187. #else
  188. #define REG "%.8lx"
  189. #endif
  190. #ifdef __LITTLE_ENDIAN__
  191. #define GETWORD(v) (((v)[3] << 24) + ((v)[2] << 16) + ((v)[1] << 8) + (v)[0])
  192. #else
  193. #define GETWORD(v) (((v)[0] << 24) + ((v)[1] << 16) + ((v)[2] << 8) + (v)[3])
  194. #endif
  195. static const char *xmon_ro_msg = "Operation disabled: xmon in read-only mode\n";
  196. static char *help_string = "\
  197. Commands:\n\
  198. b show breakpoints\n\
  199. bd set data breakpoint\n\
  200. bi set instruction breakpoint\n\
  201. bc clear breakpoint\n"
  202. #ifdef CONFIG_SMP
  203. "\
  204. c print cpus stopped in xmon\n\
  205. c# try to switch to cpu number h (in hex)\n\
  206. c# $ run command '$' (one of 'r','S' or 't') on all cpus in xmon\n"
  207. #endif
  208. "\
  209. C checksum\n\
  210. d dump bytes\n\
  211. d1 dump 1 byte values\n\
  212. d2 dump 2 byte values\n\
  213. d4 dump 4 byte values\n\
  214. d8 dump 8 byte values\n\
  215. di dump instructions\n\
  216. df dump float values\n\
  217. dd dump double values\n\
  218. dl dump the kernel log buffer\n"
  219. #ifdef CONFIG_PPC_POWERNV
  220. "\
  221. do dump the OPAL message log\n"
  222. #endif
  223. #ifdef CONFIG_PPC64
  224. "\
  225. dp[#] dump paca for current cpu, or cpu #\n\
  226. dpa dump paca for all possible cpus\n"
  227. #endif
  228. "\
  229. dr dump stream of raw bytes\n\
  230. dv dump virtual address translation \n\
  231. dt dump the tracing buffers (uses printk)\n\
  232. dtc dump the tracing buffers for current CPU (uses printk)\n\
  233. "
  234. #ifdef CONFIG_PPC_POWERNV
  235. " dx# dump xive on CPU #\n\
  236. dxi# dump xive irq state #\n\
  237. dxa dump xive on all CPUs\n"
  238. #endif
  239. " e print exception information\n\
  240. f flush cache\n\
  241. la lookup symbol+offset of specified address\n\
  242. ls lookup address of specified symbol\n\
  243. lp s [#] lookup address of percpu symbol s for current cpu, or cpu #\n\
  244. m examine/change memory\n\
  245. mm move a block of memory\n\
  246. ms set a block of memory\n\
  247. md compare two blocks of memory\n\
  248. ml locate a block of memory\n\
  249. mz zero a block of memory\n\
  250. mi show information about memory allocation\n\
  251. p call a procedure\n\
  252. P list processes/tasks\n\
  253. r print registers\n\
  254. s single step\n"
  255. #ifdef CONFIG_SPU_BASE
  256. " ss stop execution on all spus\n\
  257. sr restore execution on stopped spus\n\
  258. sf # dump spu fields for spu # (in hex)\n\
  259. sd # dump spu local store for spu # (in hex)\n\
  260. sdi # disassemble spu local store for spu # (in hex)\n"
  261. #endif
  262. " S print special registers\n\
  263. Sa print all SPRs\n\
  264. Sr # read SPR #\n\
  265. Sw #v write v to SPR #\n\
  266. t print backtrace\n\
  267. x exit monitor and recover\n\
  268. X exit monitor and don't recover\n"
  269. #if defined(CONFIG_PPC_BOOK3S_64)
  270. " u dump segment table or SLB\n"
  271. #elif defined(CONFIG_PPC_BOOK3S_32)
  272. " u dump segment registers\n"
  273. #elif defined(CONFIG_44x) || defined(CONFIG_PPC_BOOK3E_64)
  274. " u dump TLB\n"
  275. #endif
  276. " U show uptime information\n"
  277. " ? help\n"
  278. " # n limit output to n lines per page (for dp, dpa, dl)\n"
  279. " zr reboot\n"
  280. " zh halt\n"
  281. ;
  282. #ifdef CONFIG_SECURITY
  283. static bool xmon_is_locked_down(void)
  284. {
  285. static bool lockdown;
  286. if (!lockdown) {
  287. lockdown = !!security_locked_down(LOCKDOWN_XMON_RW);
  288. if (lockdown) {
  289. printf("xmon: Disabled due to kernel lockdown\n");
  290. xmon_is_ro = true;
  291. }
  292. }
  293. if (!xmon_is_ro) {
  294. xmon_is_ro = !!security_locked_down(LOCKDOWN_XMON_WR);
  295. if (xmon_is_ro)
  296. printf("xmon: Read-only due to kernel lockdown\n");
  297. }
  298. return lockdown;
  299. }
  300. #else /* CONFIG_SECURITY */
  301. static inline bool xmon_is_locked_down(void)
  302. {
  303. return false;
  304. }
  305. #endif
  306. static struct pt_regs *xmon_regs;
  307. static inline void sync(void)
  308. {
  309. asm volatile("sync; isync");
  310. }
  311. static inline void cflush(void *p)
  312. {
  313. asm volatile ("dcbf 0,%0; icbi 0,%0" : : "r" (p));
  314. }
  315. static inline void cinval(void *p)
  316. {
  317. asm volatile ("dcbi 0,%0; icbi 0,%0" : : "r" (p));
  318. }
  319. /**
  320. * write_ciabr() - write the CIABR SPR
  321. * @ciabr: The value to write.
  322. *
  323. * This function writes a value to the CIARB register either directly
  324. * through mtspr instruction if the kernel is in HV privilege mode or
  325. * call a hypervisor function to achieve the same in case the kernel
  326. * is in supervisor privilege mode.
  327. */
  328. static void write_ciabr(unsigned long ciabr)
  329. {
  330. if (!cpu_has_feature(CPU_FTR_ARCH_207S))
  331. return;
  332. if (cpu_has_feature(CPU_FTR_HVMODE)) {
  333. mtspr(SPRN_CIABR, ciabr);
  334. return;
  335. }
  336. plpar_set_ciabr(ciabr);
  337. }
  338. /**
  339. * set_ciabr() - set the CIABR
  340. * @addr: The value to set.
  341. *
  342. * This function sets the correct privilege value into the HW
  343. * breakpoint address before writing it up in the CIABR register.
  344. */
  345. static void set_ciabr(unsigned long addr)
  346. {
  347. addr &= ~CIABR_PRIV;
  348. if (cpu_has_feature(CPU_FTR_HVMODE))
  349. addr |= CIABR_PRIV_HYPER;
  350. else
  351. addr |= CIABR_PRIV_SUPER;
  352. write_ciabr(addr);
  353. }
  354. /*
  355. * Disable surveillance (the service processor watchdog function)
  356. * while we are in xmon.
  357. * XXX we should re-enable it when we leave. :)
  358. */
  359. #define SURVEILLANCE_TOKEN 9000
  360. static inline void disable_surveillance(void)
  361. {
  362. #ifdef CONFIG_PPC_PSERIES
  363. /* Since this can't be a module, args should end up below 4GB. */
  364. static struct rtas_args args;
  365. /*
  366. * At this point we have got all the cpus we can into
  367. * xmon, so there is hopefully no other cpu calling RTAS
  368. * at the moment, even though we don't take rtas.lock.
  369. * If we did try to take rtas.lock there would be a
  370. * real possibility of deadlock.
  371. */
  372. if (set_indicator_token == RTAS_UNKNOWN_SERVICE)
  373. return;
  374. rtas_call_unlocked(&args, set_indicator_token, 3, 1, NULL,
  375. SURVEILLANCE_TOKEN, 0, 0);
  376. #endif /* CONFIG_PPC_PSERIES */
  377. }
  378. #ifdef CONFIG_SMP
  379. static int xmon_speaker;
  380. static void get_output_lock(void)
  381. {
  382. int me = smp_processor_id() + 0x100;
  383. int last_speaker = 0, prev;
  384. long timeout;
  385. if (xmon_speaker == me)
  386. return;
  387. for (;;) {
  388. last_speaker = cmpxchg(&xmon_speaker, 0, me);
  389. if (last_speaker == 0)
  390. return;
  391. /*
  392. * Wait a full second for the lock, we might be on a slow
  393. * console, but check every 100us.
  394. */
  395. timeout = 10000;
  396. while (xmon_speaker == last_speaker) {
  397. if (--timeout > 0) {
  398. udelay(100);
  399. continue;
  400. }
  401. /* hostile takeover */
  402. prev = cmpxchg(&xmon_speaker, last_speaker, me);
  403. if (prev == last_speaker)
  404. return;
  405. break;
  406. }
  407. }
  408. }
  409. static void release_output_lock(void)
  410. {
  411. xmon_speaker = 0;
  412. }
  413. int cpus_are_in_xmon(void)
  414. {
  415. return !cpumask_empty(&cpus_in_xmon);
  416. }
  417. static bool wait_for_other_cpus(int ncpus)
  418. {
  419. unsigned long timeout;
  420. /* We wait for 2s, which is a metric "little while" */
  421. for (timeout = 20000; timeout != 0; --timeout) {
  422. if (cpumask_weight(&cpus_in_xmon) >= ncpus)
  423. return true;
  424. udelay(100);
  425. barrier();
  426. }
  427. return false;
  428. }
  429. #else /* CONFIG_SMP */
  430. static inline void get_output_lock(void) {}
  431. static inline void release_output_lock(void) {}
  432. #endif
  433. static void xmon_touch_watchdogs(void)
  434. {
  435. touch_softlockup_watchdog_sync();
  436. rcu_cpu_stall_reset();
  437. touch_nmi_watchdog();
  438. }
  439. static int xmon_core(struct pt_regs *regs, volatile int fromipi)
  440. {
  441. volatile int cmd = 0;
  442. struct bpt *volatile bp;
  443. long recurse_jmp[JMP_BUF_LEN];
  444. bool locked_down;
  445. unsigned long offset;
  446. unsigned long flags;
  447. #ifdef CONFIG_SMP
  448. int cpu;
  449. int secondary;
  450. #endif
  451. local_irq_save(flags);
  452. hard_irq_disable();
  453. locked_down = xmon_is_locked_down();
  454. if (!fromipi) {
  455. tracing_enabled = tracing_is_on();
  456. tracing_off();
  457. }
  458. bp = in_breakpoint_table(regs->nip, &offset);
  459. if (bp != NULL) {
  460. regs_set_return_ip(regs, bp->address + offset);
  461. atomic_dec(&bp->ref_count);
  462. }
  463. remove_cpu_bpts();
  464. #ifdef CONFIG_SMP
  465. cpu = smp_processor_id();
  466. if (cpumask_test_cpu(cpu, &cpus_in_xmon)) {
  467. /*
  468. * We catch SPR read/write faults here because the 0x700, 0xf60
  469. * etc. handlers don't call debugger_fault_handler().
  470. */
  471. if (catch_spr_faults)
  472. longjmp(bus_error_jmp, 1);
  473. get_output_lock();
  474. excprint(regs);
  475. printf("cpu 0x%x: Exception %lx %s in xmon, "
  476. "returning to main loop\n",
  477. cpu, regs->trap, getvecname(TRAP(regs)));
  478. release_output_lock();
  479. longjmp(xmon_fault_jmp[cpu], 1);
  480. }
  481. if (setjmp(recurse_jmp) != 0) {
  482. if (!in_xmon || !xmon_gate) {
  483. get_output_lock();
  484. printf("xmon: WARNING: bad recursive fault "
  485. "on cpu 0x%x\n", cpu);
  486. release_output_lock();
  487. goto waiting;
  488. }
  489. secondary = !(xmon_taken && cpu == xmon_owner);
  490. goto cmdloop;
  491. }
  492. xmon_fault_jmp[cpu] = recurse_jmp;
  493. bp = NULL;
  494. if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) == (MSR_IR|MSR_64BIT))
  495. bp = at_breakpoint(regs->nip);
  496. if (bp || regs_is_unrecoverable(regs))
  497. fromipi = 0;
  498. if (!fromipi) {
  499. get_output_lock();
  500. if (!locked_down)
  501. excprint(regs);
  502. if (bp) {
  503. printf("cpu 0x%x stopped at breakpoint 0x%tx (",
  504. cpu, BP_NUM(bp));
  505. xmon_print_symbol(regs->nip, " ", ")\n");
  506. }
  507. if (regs_is_unrecoverable(regs))
  508. printf("WARNING: exception is not recoverable, "
  509. "can't continue\n");
  510. release_output_lock();
  511. }
  512. cpumask_set_cpu(cpu, &cpus_in_xmon);
  513. waiting:
  514. secondary = 1;
  515. spin_begin();
  516. while (secondary && !xmon_gate) {
  517. if (in_xmon == 0) {
  518. if (fromipi) {
  519. spin_end();
  520. goto leave;
  521. }
  522. secondary = test_and_set_bit(0, &in_xmon);
  523. }
  524. spin_cpu_relax();
  525. touch_nmi_watchdog();
  526. }
  527. spin_end();
  528. if (!secondary && !xmon_gate) {
  529. /* we are the first cpu to come in */
  530. /* interrupt other cpu(s) */
  531. int ncpus = num_online_cpus();
  532. xmon_owner = cpu;
  533. mb();
  534. if (ncpus > 1) {
  535. /*
  536. * A system reset (trap == 0x100) can be triggered on
  537. * all CPUs, so when we come in via 0x100 try waiting
  538. * for the other CPUs to come in before we send the
  539. * debugger break (IPI). This is similar to
  540. * crash_kexec_secondary().
  541. */
  542. if (TRAP(regs) != INTERRUPT_SYSTEM_RESET || !wait_for_other_cpus(ncpus))
  543. smp_send_debugger_break();
  544. wait_for_other_cpus(ncpus);
  545. }
  546. remove_bpts();
  547. disable_surveillance();
  548. if (!locked_down) {
  549. /* for breakpoint or single step, print curr insn */
  550. if (bp || TRAP(regs) == INTERRUPT_TRACE)
  551. ppc_inst_dump(regs->nip, 1, 0);
  552. printf("enter ? for help\n");
  553. }
  554. mb();
  555. xmon_gate = 1;
  556. barrier();
  557. touch_nmi_watchdog();
  558. }
  559. cmdloop:
  560. while (in_xmon) {
  561. if (secondary) {
  562. spin_begin();
  563. if (cpu == xmon_owner) {
  564. if (!test_and_set_bit(0, &xmon_taken)) {
  565. secondary = 0;
  566. spin_end();
  567. continue;
  568. }
  569. /* missed it */
  570. while (cpu == xmon_owner)
  571. spin_cpu_relax();
  572. }
  573. spin_cpu_relax();
  574. touch_nmi_watchdog();
  575. } else {
  576. cmd = 1;
  577. #ifdef CONFIG_SMP
  578. if (xmon_batch)
  579. cmd = batch_cmds(regs);
  580. #endif
  581. if (!locked_down && cmd)
  582. cmd = cmds(regs);
  583. if (locked_down || cmd != 0) {
  584. /* exiting xmon */
  585. insert_bpts();
  586. xmon_gate = 0;
  587. wmb();
  588. in_xmon = 0;
  589. break;
  590. }
  591. /* have switched to some other cpu */
  592. secondary = 1;
  593. }
  594. }
  595. leave:
  596. cpumask_clear_cpu(cpu, &cpus_in_xmon);
  597. xmon_fault_jmp[cpu] = NULL;
  598. #else
  599. /* UP is simple... */
  600. if (in_xmon) {
  601. printf("Exception %lx %s in xmon, returning to main loop\n",
  602. regs->trap, getvecname(TRAP(regs)));
  603. longjmp(xmon_fault_jmp[0], 1);
  604. }
  605. if (setjmp(recurse_jmp) == 0) {
  606. xmon_fault_jmp[0] = recurse_jmp;
  607. in_xmon = 1;
  608. excprint(regs);
  609. bp = at_breakpoint(regs->nip);
  610. if (bp) {
  611. printf("Stopped at breakpoint %tx (", BP_NUM(bp));
  612. xmon_print_symbol(regs->nip, " ", ")\n");
  613. }
  614. if (regs_is_unrecoverable(regs))
  615. printf("WARNING: exception is not recoverable, "
  616. "can't continue\n");
  617. remove_bpts();
  618. disable_surveillance();
  619. if (!locked_down) {
  620. /* for breakpoint or single step, print current insn */
  621. if (bp || TRAP(regs) == INTERRUPT_TRACE)
  622. ppc_inst_dump(regs->nip, 1, 0);
  623. printf("enter ? for help\n");
  624. }
  625. }
  626. if (!locked_down)
  627. cmd = cmds(regs);
  628. insert_bpts();
  629. in_xmon = 0;
  630. #endif
  631. #ifdef CONFIG_BOOKE
  632. if (regs->msr & MSR_DE) {
  633. bp = at_breakpoint(regs->nip);
  634. if (bp != NULL) {
  635. regs_set_return_ip(regs, (unsigned long) &bp->instr[0]);
  636. atomic_inc(&bp->ref_count);
  637. }
  638. }
  639. #else
  640. if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) == (MSR_IR|MSR_64BIT)) {
  641. bp = at_breakpoint(regs->nip);
  642. if (bp != NULL) {
  643. int stepped = emulate_step(regs, ppc_inst_read(bp->instr));
  644. if (stepped == 0) {
  645. regs_set_return_ip(regs, (unsigned long) &bp->instr[0]);
  646. atomic_inc(&bp->ref_count);
  647. } else if (stepped < 0) {
  648. printf("Couldn't single-step %s instruction\n",
  649. IS_RFID(ppc_inst_read(bp->instr))? "rfid": "mtmsrd");
  650. }
  651. }
  652. }
  653. #endif
  654. if (locked_down)
  655. clear_all_bpt();
  656. else
  657. insert_cpu_bpts();
  658. xmon_touch_watchdogs();
  659. local_irq_restore(flags);
  660. return cmd != 'X' && cmd != EOF;
  661. }
  662. int xmon(struct pt_regs *excp)
  663. {
  664. struct pt_regs regs;
  665. if (excp == NULL) {
  666. ppc_save_regs(&regs);
  667. excp = &regs;
  668. }
  669. return xmon_core(excp, 0);
  670. }
  671. EXPORT_SYMBOL(xmon);
  672. irqreturn_t xmon_irq(int irq, void *d)
  673. {
  674. unsigned long flags;
  675. local_irq_save(flags);
  676. printf("Keyboard interrupt\n");
  677. xmon(get_irq_regs());
  678. local_irq_restore(flags);
  679. return IRQ_HANDLED;
  680. }
  681. static int xmon_bpt(struct pt_regs *regs)
  682. {
  683. struct bpt *bp;
  684. unsigned long offset;
  685. if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) != (MSR_IR|MSR_64BIT))
  686. return 0;
  687. /* Are we at the trap at bp->instr[1] for some bp? */
  688. bp = in_breakpoint_table(regs->nip, &offset);
  689. if (bp != NULL && (offset == 4 || offset == 8)) {
  690. regs_set_return_ip(regs, bp->address + offset);
  691. atomic_dec(&bp->ref_count);
  692. return 1;
  693. }
  694. /* Are we at a breakpoint? */
  695. bp = at_breakpoint(regs->nip);
  696. if (!bp)
  697. return 0;
  698. xmon_core(regs, 0);
  699. return 1;
  700. }
  701. static int xmon_sstep(struct pt_regs *regs)
  702. {
  703. if (user_mode(regs))
  704. return 0;
  705. xmon_core(regs, 0);
  706. return 1;
  707. }
  708. static int xmon_break_match(struct pt_regs *regs)
  709. {
  710. int i;
  711. if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) != (MSR_IR|MSR_64BIT))
  712. return 0;
  713. for (i = 0; i < nr_wp_slots(); i++) {
  714. if (dabr[i].enabled)
  715. goto found;
  716. }
  717. return 0;
  718. found:
  719. xmon_core(regs, 0);
  720. return 1;
  721. }
  722. static int xmon_iabr_match(struct pt_regs *regs)
  723. {
  724. if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) != (MSR_IR|MSR_64BIT))
  725. return 0;
  726. if (iabr == NULL)
  727. return 0;
  728. xmon_core(regs, 0);
  729. return 1;
  730. }
  731. static int xmon_ipi(struct pt_regs *regs)
  732. {
  733. #ifdef CONFIG_SMP
  734. if (in_xmon && !cpumask_test_cpu(smp_processor_id(), &cpus_in_xmon))
  735. xmon_core(regs, 1);
  736. #endif
  737. return 0;
  738. }
  739. static int xmon_fault_handler(struct pt_regs *regs)
  740. {
  741. struct bpt *bp;
  742. unsigned long offset;
  743. if (in_xmon && catch_memory_errors)
  744. handle_fault(regs); /* doesn't return */
  745. if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) == (MSR_IR|MSR_64BIT)) {
  746. bp = in_breakpoint_table(regs->nip, &offset);
  747. if (bp != NULL) {
  748. regs_set_return_ip(regs, bp->address + offset);
  749. atomic_dec(&bp->ref_count);
  750. }
  751. }
  752. return 0;
  753. }
  754. /* Force enable xmon if not already enabled */
  755. static inline void force_enable_xmon(void)
  756. {
  757. /* Enable xmon hooks if needed */
  758. if (!xmon_on) {
  759. printf("xmon: Enabling debugger hooks\n");
  760. xmon_on = 1;
  761. }
  762. }
  763. static struct bpt *at_breakpoint(unsigned long pc)
  764. {
  765. int i;
  766. struct bpt *volatile bp;
  767. bp = bpts;
  768. for (i = 0; i < NBPTS; ++i, ++bp)
  769. if (bp->enabled && pc == bp->address)
  770. return bp;
  771. return NULL;
  772. }
  773. static struct bpt *in_breakpoint_table(unsigned long nip, unsigned long *offp)
  774. {
  775. unsigned long off;
  776. off = nip - (unsigned long)bpt_table;
  777. if (off >= sizeof(bpt_table))
  778. return NULL;
  779. *offp = off & (BPT_SIZE - 1);
  780. if (off & 3)
  781. return NULL;
  782. return bpts + (off / BPT_SIZE);
  783. }
  784. static struct bpt *new_breakpoint(unsigned long a)
  785. {
  786. struct bpt *bp;
  787. a &= ~3UL;
  788. bp = at_breakpoint(a);
  789. if (bp)
  790. return bp;
  791. for (bp = bpts; bp < &bpts[NBPTS]; ++bp) {
  792. if (!bp->enabled && atomic_read(&bp->ref_count) == 0) {
  793. bp->address = a;
  794. bp->instr = (void *)(bpt_table + ((bp - bpts) * BPT_WORDS));
  795. return bp;
  796. }
  797. }
  798. printf("Sorry, no free breakpoints. Please clear one first.\n");
  799. return NULL;
  800. }
  801. static void insert_bpts(void)
  802. {
  803. int i;
  804. ppc_inst_t instr, instr2;
  805. struct bpt *bp, *bp2;
  806. bp = bpts;
  807. for (i = 0; i < NBPTS; ++i, ++bp) {
  808. if ((bp->enabled & (BP_TRAP|BP_CIABR)) == 0)
  809. continue;
  810. if (!mread_instr(bp->address, &instr)) {
  811. printf("Couldn't read instruction at %lx, "
  812. "disabling breakpoint there\n", bp->address);
  813. bp->enabled = 0;
  814. continue;
  815. }
  816. if (!can_single_step(ppc_inst_val(instr))) {
  817. printf("Breakpoint at %lx is on an instruction that can't be single stepped, disabling it\n",
  818. bp->address);
  819. bp->enabled = 0;
  820. continue;
  821. }
  822. /*
  823. * Check the address is not a suffix by looking for a prefix in
  824. * front of it.
  825. */
  826. if (mread_instr(bp->address - 4, &instr2) == 8) {
  827. printf("Breakpoint at %lx is on the second word of a prefixed instruction, disabling it\n",
  828. bp->address);
  829. bp->enabled = 0;
  830. continue;
  831. }
  832. /*
  833. * We might still be a suffix - if the prefix has already been
  834. * replaced by a breakpoint we won't catch it with the above
  835. * test.
  836. */
  837. bp2 = at_breakpoint(bp->address - 4);
  838. if (bp2 && ppc_inst_prefixed(ppc_inst_read(bp2->instr))) {
  839. printf("Breakpoint at %lx is on the second word of a prefixed instruction, disabling it\n",
  840. bp->address);
  841. bp->enabled = 0;
  842. continue;
  843. }
  844. patch_instruction(bp->instr, instr);
  845. patch_instruction(ppc_inst_next(bp->instr, bp->instr),
  846. ppc_inst(bpinstr));
  847. if (bp->enabled & BP_CIABR)
  848. continue;
  849. if (patch_instruction((u32 *)bp->address,
  850. ppc_inst(bpinstr)) != 0) {
  851. printf("Couldn't write instruction at %lx, "
  852. "disabling breakpoint there\n", bp->address);
  853. bp->enabled &= ~BP_TRAP;
  854. continue;
  855. }
  856. }
  857. }
  858. static void insert_cpu_bpts(void)
  859. {
  860. int i;
  861. struct arch_hw_breakpoint brk;
  862. for (i = 0; i < nr_wp_slots(); i++) {
  863. if (dabr[i].enabled) {
  864. brk.address = dabr[i].address;
  865. brk.type = (dabr[i].enabled & HW_BRK_TYPE_DABR) | HW_BRK_TYPE_PRIV_ALL;
  866. brk.len = 8;
  867. brk.hw_len = 8;
  868. __set_breakpoint(i, &brk);
  869. }
  870. }
  871. if (iabr)
  872. set_ciabr(iabr->address);
  873. }
  874. static void remove_bpts(void)
  875. {
  876. int i;
  877. struct bpt *bp;
  878. ppc_inst_t instr;
  879. bp = bpts;
  880. for (i = 0; i < NBPTS; ++i, ++bp) {
  881. if ((bp->enabled & (BP_TRAP|BP_CIABR)) != BP_TRAP)
  882. continue;
  883. if (mread_instr(bp->address, &instr)
  884. && ppc_inst_equal(instr, ppc_inst(bpinstr))
  885. && patch_instruction(
  886. (u32 *)bp->address, ppc_inst_read(bp->instr)) != 0)
  887. printf("Couldn't remove breakpoint at %lx\n",
  888. bp->address);
  889. }
  890. }
  891. static void remove_cpu_bpts(void)
  892. {
  893. hw_breakpoint_disable();
  894. write_ciabr(0);
  895. }
  896. /* Based on uptime_proc_show(). */
  897. static void
  898. show_uptime(void)
  899. {
  900. struct timespec64 uptime;
  901. if (setjmp(bus_error_jmp) == 0) {
  902. catch_memory_errors = 1;
  903. sync();
  904. ktime_get_coarse_boottime_ts64(&uptime);
  905. printf("Uptime: %lu.%.2lu seconds\n", (unsigned long)uptime.tv_sec,
  906. ((unsigned long)uptime.tv_nsec / (NSEC_PER_SEC/100)));
  907. sync();
  908. __delay(200); \
  909. }
  910. catch_memory_errors = 0;
  911. }
  912. static void set_lpp_cmd(void)
  913. {
  914. unsigned long lpp;
  915. if (!scanhex(&lpp)) {
  916. printf("Invalid number.\n");
  917. lpp = 0;
  918. }
  919. xmon_set_pagination_lpp(lpp);
  920. }
  921. /* Command interpreting routine */
  922. static char *last_cmd;
  923. static int
  924. cmds(struct pt_regs *excp)
  925. {
  926. int cmd = 0;
  927. last_cmd = NULL;
  928. xmon_regs = excp;
  929. xmon_show_stack(excp->gpr[1], excp->link, excp->nip);
  930. for(;;) {
  931. #ifdef CONFIG_SMP
  932. printf("%x:", smp_processor_id());
  933. #endif /* CONFIG_SMP */
  934. printf("mon> ");
  935. flush_input();
  936. termch = 0;
  937. cmd = skipbl();
  938. if( cmd == '\n' ) {
  939. if (last_cmd == NULL)
  940. continue;
  941. take_input(last_cmd);
  942. last_cmd = NULL;
  943. cmd = inchar();
  944. }
  945. switch (cmd) {
  946. case 'm':
  947. cmd = inchar();
  948. switch (cmd) {
  949. case 'm':
  950. case 's':
  951. case 'd':
  952. memops(cmd);
  953. break;
  954. case 'l':
  955. memlocate();
  956. break;
  957. case 'z':
  958. if (xmon_is_ro) {
  959. printf(xmon_ro_msg);
  960. break;
  961. }
  962. memzcan();
  963. break;
  964. case 'i':
  965. show_mem(0, NULL);
  966. break;
  967. default:
  968. termch = cmd;
  969. memex();
  970. }
  971. break;
  972. case 'd':
  973. dump();
  974. break;
  975. case 'l':
  976. symbol_lookup();
  977. break;
  978. case 'r':
  979. prregs(excp); /* print regs */
  980. break;
  981. case 'e':
  982. excprint(excp);
  983. break;
  984. case 'S':
  985. super_regs();
  986. break;
  987. case 't':
  988. backtrace(excp);
  989. break;
  990. case 'f':
  991. cacheflush();
  992. break;
  993. case 's':
  994. if (do_spu_cmd() == 0)
  995. break;
  996. if (do_step(excp))
  997. return cmd;
  998. break;
  999. case 'x':
  1000. case 'X':
  1001. if (tracing_enabled)
  1002. tracing_on();
  1003. return cmd;
  1004. case EOF:
  1005. printf(" <no input ...>\n");
  1006. mdelay(2000);
  1007. return cmd;
  1008. case '?':
  1009. xmon_puts(help_string);
  1010. break;
  1011. case '#':
  1012. set_lpp_cmd();
  1013. break;
  1014. case 'b':
  1015. bpt_cmds();
  1016. break;
  1017. case 'C':
  1018. csum();
  1019. break;
  1020. case 'c':
  1021. if (cpu_cmd())
  1022. return 0;
  1023. break;
  1024. case 'z':
  1025. bootcmds();
  1026. break;
  1027. case 'p':
  1028. if (xmon_is_ro) {
  1029. printf(xmon_ro_msg);
  1030. break;
  1031. }
  1032. proccall();
  1033. break;
  1034. case 'P':
  1035. show_tasks();
  1036. break;
  1037. #if defined(CONFIG_PPC_BOOK3S_32) || defined(CONFIG_PPC_64S_HASH_MMU)
  1038. case 'u':
  1039. dump_segments();
  1040. break;
  1041. #elif defined(CONFIG_44x)
  1042. case 'u':
  1043. dump_tlb_44x();
  1044. break;
  1045. #elif defined(CONFIG_PPC_BOOK3E_64)
  1046. case 'u':
  1047. dump_tlb_book3e();
  1048. break;
  1049. #endif
  1050. case 'U':
  1051. show_uptime();
  1052. break;
  1053. default:
  1054. printf("Unrecognized command: ");
  1055. do {
  1056. if (' ' < cmd && cmd <= '~')
  1057. putchar(cmd);
  1058. else
  1059. printf("\\x%x", cmd);
  1060. cmd = inchar();
  1061. } while (cmd != '\n');
  1062. printf(" (type ? for help)\n");
  1063. break;
  1064. }
  1065. }
  1066. }
  1067. #ifdef CONFIG_BOOKE
  1068. static int do_step(struct pt_regs *regs)
  1069. {
  1070. regs_set_return_msr(regs, regs->msr | MSR_DE);
  1071. mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) | DBCR0_IC | DBCR0_IDM);
  1072. return 1;
  1073. }
  1074. #else
  1075. /*
  1076. * Step a single instruction.
  1077. * Some instructions we emulate, others we execute with MSR_SE set.
  1078. */
  1079. static int do_step(struct pt_regs *regs)
  1080. {
  1081. ppc_inst_t instr;
  1082. int stepped;
  1083. force_enable_xmon();
  1084. /* check we are in 64-bit kernel mode, translation enabled */
  1085. if ((regs->msr & (MSR_64BIT|MSR_PR|MSR_IR)) == (MSR_64BIT|MSR_IR)) {
  1086. if (mread_instr(regs->nip, &instr)) {
  1087. stepped = emulate_step(regs, instr);
  1088. if (stepped < 0) {
  1089. printf("Couldn't single-step %s instruction\n",
  1090. (IS_RFID(instr)? "rfid": "mtmsrd"));
  1091. return 0;
  1092. }
  1093. if (stepped > 0) {
  1094. set_trap(regs, 0xd00);
  1095. printf("stepped to ");
  1096. xmon_print_symbol(regs->nip, " ", "\n");
  1097. ppc_inst_dump(regs->nip, 1, 0);
  1098. return 0;
  1099. }
  1100. }
  1101. }
  1102. regs_set_return_msr(regs, regs->msr | MSR_SE);
  1103. return 1;
  1104. }
  1105. #endif
  1106. static void bootcmds(void)
  1107. {
  1108. char tmp[64];
  1109. int cmd;
  1110. cmd = inchar();
  1111. if (cmd == 'r') {
  1112. getstring(tmp, 64);
  1113. ppc_md.restart(tmp);
  1114. } else if (cmd == 'h') {
  1115. ppc_md.halt();
  1116. } else if (cmd == 'p') {
  1117. do_kernel_power_off();
  1118. }
  1119. }
  1120. #ifdef CONFIG_SMP
  1121. static int xmon_switch_cpu(unsigned long cpu)
  1122. {
  1123. int timeout;
  1124. xmon_taken = 0;
  1125. mb();
  1126. xmon_owner = cpu;
  1127. timeout = 10000000;
  1128. while (!xmon_taken) {
  1129. if (--timeout == 0) {
  1130. if (test_and_set_bit(0, &xmon_taken))
  1131. break;
  1132. /* take control back */
  1133. mb();
  1134. xmon_owner = smp_processor_id();
  1135. printf("cpu 0x%lx didn't take control\n", cpu);
  1136. return 0;
  1137. }
  1138. barrier();
  1139. }
  1140. return 1;
  1141. }
  1142. static int xmon_batch_next_cpu(void)
  1143. {
  1144. unsigned long cpu;
  1145. while (!cpumask_empty(&xmon_batch_cpus)) {
  1146. cpu = cpumask_next_wrap(smp_processor_id(), &xmon_batch_cpus,
  1147. xmon_batch_start_cpu, true);
  1148. if (cpu == nr_cpumask_bits)
  1149. break;
  1150. if (xmon_batch_start_cpu == -1)
  1151. xmon_batch_start_cpu = cpu;
  1152. if (xmon_switch_cpu(cpu))
  1153. return 0;
  1154. cpumask_clear_cpu(cpu, &xmon_batch_cpus);
  1155. }
  1156. xmon_batch = 0;
  1157. printf("%x:mon> \n", smp_processor_id());
  1158. return 1;
  1159. }
  1160. static int batch_cmds(struct pt_regs *excp)
  1161. {
  1162. int cmd;
  1163. /* simulate command entry */
  1164. cmd = xmon_batch;
  1165. termch = '\n';
  1166. last_cmd = NULL;
  1167. xmon_regs = excp;
  1168. printf("%x:", smp_processor_id());
  1169. printf("mon> ");
  1170. printf("%c\n", (char)cmd);
  1171. switch (cmd) {
  1172. case 'r':
  1173. prregs(excp); /* print regs */
  1174. break;
  1175. case 'S':
  1176. super_regs();
  1177. break;
  1178. case 't':
  1179. backtrace(excp);
  1180. break;
  1181. }
  1182. cpumask_clear_cpu(smp_processor_id(), &xmon_batch_cpus);
  1183. return xmon_batch_next_cpu();
  1184. }
  1185. static int cpu_cmd(void)
  1186. {
  1187. unsigned long cpu, first_cpu, last_cpu;
  1188. cpu = skipbl();
  1189. if (cpu == '#') {
  1190. xmon_batch = skipbl();
  1191. if (xmon_batch) {
  1192. switch (xmon_batch) {
  1193. case 'r':
  1194. case 'S':
  1195. case 't':
  1196. cpumask_copy(&xmon_batch_cpus, &cpus_in_xmon);
  1197. if (cpumask_weight(&xmon_batch_cpus) <= 1) {
  1198. printf("There are no other cpus in xmon\n");
  1199. break;
  1200. }
  1201. xmon_batch_start_cpu = -1;
  1202. if (!xmon_batch_next_cpu())
  1203. return 1;
  1204. break;
  1205. default:
  1206. printf("c# only supports 'r', 'S' and 't' commands\n");
  1207. }
  1208. xmon_batch = 0;
  1209. return 0;
  1210. }
  1211. }
  1212. termch = cpu;
  1213. if (!scanhex(&cpu)) {
  1214. /* print cpus waiting or in xmon */
  1215. printf("cpus stopped:");
  1216. last_cpu = first_cpu = NR_CPUS;
  1217. for_each_possible_cpu(cpu) {
  1218. if (cpumask_test_cpu(cpu, &cpus_in_xmon)) {
  1219. if (cpu == last_cpu + 1) {
  1220. last_cpu = cpu;
  1221. } else {
  1222. if (last_cpu != first_cpu)
  1223. printf("-0x%lx", last_cpu);
  1224. last_cpu = first_cpu = cpu;
  1225. printf(" 0x%lx", cpu);
  1226. }
  1227. }
  1228. }
  1229. if (last_cpu != first_cpu)
  1230. printf("-0x%lx", last_cpu);
  1231. printf("\n");
  1232. return 0;
  1233. }
  1234. /* try to switch to cpu specified */
  1235. if (!cpumask_test_cpu(cpu, &cpus_in_xmon)) {
  1236. printf("cpu 0x%lx isn't in xmon\n", cpu);
  1237. #ifdef CONFIG_PPC64
  1238. printf("backtrace of paca[0x%lx].saved_r1 (possibly stale):\n", cpu);
  1239. xmon_show_stack(paca_ptrs[cpu]->saved_r1, 0, 0);
  1240. #endif
  1241. return 0;
  1242. }
  1243. return xmon_switch_cpu(cpu);
  1244. }
  1245. #else
  1246. static int cpu_cmd(void)
  1247. {
  1248. return 0;
  1249. }
  1250. #endif /* CONFIG_SMP */
  1251. static unsigned short fcstab[256] = {
  1252. 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
  1253. 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
  1254. 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
  1255. 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
  1256. 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
  1257. 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
  1258. 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
  1259. 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
  1260. 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
  1261. 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
  1262. 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
  1263. 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
  1264. 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
  1265. 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
  1266. 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
  1267. 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
  1268. 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
  1269. 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
  1270. 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
  1271. 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
  1272. 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
  1273. 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
  1274. 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
  1275. 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
  1276. 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
  1277. 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
  1278. 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
  1279. 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
  1280. 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
  1281. 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
  1282. 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
  1283. 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
  1284. };
  1285. #define FCS(fcs, c) (((fcs) >> 8) ^ fcstab[((fcs) ^ (c)) & 0xff])
  1286. static void
  1287. csum(void)
  1288. {
  1289. unsigned int i;
  1290. unsigned short fcs;
  1291. unsigned char v;
  1292. if (!scanhex(&adrs))
  1293. return;
  1294. if (!scanhex(&ncsum))
  1295. return;
  1296. fcs = 0xffff;
  1297. for (i = 0; i < ncsum; ++i) {
  1298. if (mread(adrs+i, &v, 1) == 0) {
  1299. printf("csum stopped at "REG"\n", adrs+i);
  1300. break;
  1301. }
  1302. fcs = FCS(fcs, v);
  1303. }
  1304. printf("%x\n", fcs);
  1305. }
  1306. /*
  1307. * Check if this is a suitable place to put a breakpoint.
  1308. */
  1309. static long check_bp_loc(unsigned long addr)
  1310. {
  1311. ppc_inst_t instr;
  1312. addr &= ~3;
  1313. if (!is_kernel_addr(addr)) {
  1314. printf("Breakpoints may only be placed at kernel addresses\n");
  1315. return 0;
  1316. }
  1317. if (!mread_instr(addr, &instr)) {
  1318. printf("Can't read instruction at address %lx\n", addr);
  1319. return 0;
  1320. }
  1321. if (!can_single_step(ppc_inst_val(instr))) {
  1322. printf("Breakpoints may not be placed on instructions that can't be single stepped\n");
  1323. return 0;
  1324. }
  1325. return 1;
  1326. }
  1327. static int find_free_data_bpt(void)
  1328. {
  1329. int i;
  1330. for (i = 0; i < nr_wp_slots(); i++) {
  1331. if (!dabr[i].enabled)
  1332. return i;
  1333. }
  1334. printf("Couldn't find free breakpoint register\n");
  1335. return -1;
  1336. }
  1337. static void print_data_bpts(void)
  1338. {
  1339. int i;
  1340. for (i = 0; i < nr_wp_slots(); i++) {
  1341. if (!dabr[i].enabled)
  1342. continue;
  1343. printf(" data "REG" [", dabr[i].address);
  1344. if (dabr[i].enabled & 1)
  1345. printf("r");
  1346. if (dabr[i].enabled & 2)
  1347. printf("w");
  1348. printf("]\n");
  1349. }
  1350. }
  1351. static char *breakpoint_help_string =
  1352. "Breakpoint command usage:\n"
  1353. "b show breakpoints\n"
  1354. "b <addr> [cnt] set breakpoint at given instr addr\n"
  1355. "bc clear all breakpoints\n"
  1356. "bc <n/addr> clear breakpoint number n or at addr\n"
  1357. "bi <addr> [cnt] set hardware instr breakpoint (POWER8 only)\n"
  1358. "bd <addr> [cnt] set hardware data breakpoint\n"
  1359. "";
  1360. static void
  1361. bpt_cmds(void)
  1362. {
  1363. int cmd;
  1364. unsigned long a;
  1365. int i;
  1366. struct bpt *bp;
  1367. cmd = inchar();
  1368. switch (cmd) {
  1369. case 'd': { /* bd - hardware data breakpoint */
  1370. static const char badaddr[] = "Only kernel addresses are permitted for breakpoints\n";
  1371. int mode;
  1372. if (xmon_is_ro) {
  1373. printf(xmon_ro_msg);
  1374. break;
  1375. }
  1376. if (!ppc_breakpoint_available()) {
  1377. printf("Hardware data breakpoint not supported on this cpu\n");
  1378. break;
  1379. }
  1380. i = find_free_data_bpt();
  1381. if (i < 0)
  1382. break;
  1383. mode = 7;
  1384. cmd = inchar();
  1385. if (cmd == 'r')
  1386. mode = 5;
  1387. else if (cmd == 'w')
  1388. mode = 6;
  1389. else
  1390. termch = cmd;
  1391. dabr[i].address = 0;
  1392. dabr[i].enabled = 0;
  1393. if (scanhex(&dabr[i].address)) {
  1394. if (!is_kernel_addr(dabr[i].address)) {
  1395. printf(badaddr);
  1396. break;
  1397. }
  1398. dabr[i].address &= ~HW_BRK_TYPE_DABR;
  1399. dabr[i].enabled = mode | BP_DABR;
  1400. }
  1401. force_enable_xmon();
  1402. break;
  1403. }
  1404. case 'i': /* bi - hardware instr breakpoint */
  1405. if (xmon_is_ro) {
  1406. printf(xmon_ro_msg);
  1407. break;
  1408. }
  1409. if (!cpu_has_feature(CPU_FTR_ARCH_207S)) {
  1410. printf("Hardware instruction breakpoint "
  1411. "not supported on this cpu\n");
  1412. break;
  1413. }
  1414. if (iabr) {
  1415. iabr->enabled &= ~BP_CIABR;
  1416. iabr = NULL;
  1417. }
  1418. if (!scanhex(&a))
  1419. break;
  1420. if (!check_bp_loc(a))
  1421. break;
  1422. bp = new_breakpoint(a);
  1423. if (bp != NULL) {
  1424. bp->enabled |= BP_CIABR;
  1425. iabr = bp;
  1426. force_enable_xmon();
  1427. }
  1428. break;
  1429. case 'c':
  1430. if (!scanhex(&a)) {
  1431. /* clear all breakpoints */
  1432. for (i = 0; i < NBPTS; ++i)
  1433. bpts[i].enabled = 0;
  1434. iabr = NULL;
  1435. for (i = 0; i < nr_wp_slots(); i++)
  1436. dabr[i].enabled = 0;
  1437. printf("All breakpoints cleared\n");
  1438. break;
  1439. }
  1440. if (a <= NBPTS && a >= 1) {
  1441. /* assume a breakpoint number */
  1442. bp = &bpts[a-1]; /* bp nums are 1 based */
  1443. } else {
  1444. /* assume a breakpoint address */
  1445. bp = at_breakpoint(a);
  1446. if (bp == NULL) {
  1447. printf("No breakpoint at %lx\n", a);
  1448. break;
  1449. }
  1450. }
  1451. printf("Cleared breakpoint %tx (", BP_NUM(bp));
  1452. xmon_print_symbol(bp->address, " ", ")\n");
  1453. bp->enabled = 0;
  1454. break;
  1455. default:
  1456. termch = cmd;
  1457. cmd = skipbl();
  1458. if (cmd == '?') {
  1459. printf(breakpoint_help_string);
  1460. break;
  1461. }
  1462. termch = cmd;
  1463. if (xmon_is_ro || !scanhex(&a)) {
  1464. /* print all breakpoints */
  1465. printf(" type address\n");
  1466. print_data_bpts();
  1467. for (bp = bpts; bp < &bpts[NBPTS]; ++bp) {
  1468. if (!bp->enabled)
  1469. continue;
  1470. printf("%tx %s ", BP_NUM(bp),
  1471. (bp->enabled & BP_CIABR) ? "inst": "trap");
  1472. xmon_print_symbol(bp->address, " ", "\n");
  1473. }
  1474. break;
  1475. }
  1476. if (!check_bp_loc(a))
  1477. break;
  1478. bp = new_breakpoint(a);
  1479. if (bp != NULL) {
  1480. bp->enabled |= BP_TRAP;
  1481. force_enable_xmon();
  1482. }
  1483. break;
  1484. }
  1485. }
  1486. /* Very cheap human name for vector lookup. */
  1487. static
  1488. const char *getvecname(unsigned long vec)
  1489. {
  1490. char *ret;
  1491. switch (vec) {
  1492. case 0x100: ret = "(System Reset)"; break;
  1493. case 0x200: ret = "(Machine Check)"; break;
  1494. case 0x300: ret = "(Data Access)"; break;
  1495. case 0x380:
  1496. if (radix_enabled())
  1497. ret = "(Data Access Out of Range)";
  1498. else
  1499. ret = "(Data SLB Access)";
  1500. break;
  1501. case 0x400: ret = "(Instruction Access)"; break;
  1502. case 0x480:
  1503. if (radix_enabled())
  1504. ret = "(Instruction Access Out of Range)";
  1505. else
  1506. ret = "(Instruction SLB Access)";
  1507. break;
  1508. case 0x500: ret = "(Hardware Interrupt)"; break;
  1509. case 0x600: ret = "(Alignment)"; break;
  1510. case 0x700: ret = "(Program Check)"; break;
  1511. case 0x800: ret = "(FPU Unavailable)"; break;
  1512. case 0x900: ret = "(Decrementer)"; break;
  1513. case 0x980: ret = "(Hypervisor Decrementer)"; break;
  1514. case 0xa00: ret = "(Doorbell)"; break;
  1515. case 0xc00: ret = "(System Call)"; break;
  1516. case 0xd00: ret = "(Single Step)"; break;
  1517. case 0xe40: ret = "(Emulation Assist)"; break;
  1518. case 0xe60: ret = "(HMI)"; break;
  1519. case 0xe80: ret = "(Hypervisor Doorbell)"; break;
  1520. case 0xf00: ret = "(Performance Monitor)"; break;
  1521. case 0xf20: ret = "(Altivec Unavailable)"; break;
  1522. case 0x1300: ret = "(Instruction Breakpoint)"; break;
  1523. case 0x1500: ret = "(Denormalisation)"; break;
  1524. case 0x1700: ret = "(Altivec Assist)"; break;
  1525. case 0x3000: ret = "(System Call Vectored)"; break;
  1526. default: ret = "";
  1527. }
  1528. return ret;
  1529. }
  1530. static void get_function_bounds(unsigned long pc, unsigned long *startp,
  1531. unsigned long *endp)
  1532. {
  1533. unsigned long size, offset;
  1534. const char *name;
  1535. *startp = *endp = 0;
  1536. if (pc == 0)
  1537. return;
  1538. if (setjmp(bus_error_jmp) == 0) {
  1539. catch_memory_errors = 1;
  1540. sync();
  1541. name = kallsyms_lookup(pc, &size, &offset, NULL, tmpstr);
  1542. if (name != NULL) {
  1543. *startp = pc - offset;
  1544. *endp = pc - offset + size;
  1545. }
  1546. sync();
  1547. }
  1548. catch_memory_errors = 0;
  1549. }
  1550. #define LRSAVE_OFFSET (STACK_FRAME_LR_SAVE * sizeof(unsigned long))
  1551. #define MARKER_OFFSET (STACK_FRAME_MARKER * sizeof(unsigned long))
  1552. static void xmon_show_stack(unsigned long sp, unsigned long lr,
  1553. unsigned long pc)
  1554. {
  1555. int max_to_print = 64;
  1556. unsigned long ip;
  1557. unsigned long newsp;
  1558. unsigned long marker;
  1559. struct pt_regs regs;
  1560. while (max_to_print--) {
  1561. if (!is_kernel_addr(sp)) {
  1562. if (sp != 0)
  1563. printf("SP (%lx) is in userspace\n", sp);
  1564. break;
  1565. }
  1566. if (!mread(sp + LRSAVE_OFFSET, &ip, sizeof(unsigned long))
  1567. || !mread(sp, &newsp, sizeof(unsigned long))) {
  1568. printf("Couldn't read stack frame at %lx\n", sp);
  1569. break;
  1570. }
  1571. /*
  1572. * For the first stack frame, try to work out if
  1573. * LR and/or the saved LR value in the bottommost
  1574. * stack frame are valid.
  1575. */
  1576. if ((pc | lr) != 0) {
  1577. unsigned long fnstart, fnend;
  1578. unsigned long nextip;
  1579. int printip = 1;
  1580. get_function_bounds(pc, &fnstart, &fnend);
  1581. nextip = 0;
  1582. if (newsp > sp)
  1583. mread(newsp + LRSAVE_OFFSET, &nextip,
  1584. sizeof(unsigned long));
  1585. if (lr == ip) {
  1586. if (!is_kernel_addr(lr)
  1587. || (fnstart <= lr && lr < fnend))
  1588. printip = 0;
  1589. } else if (lr == nextip) {
  1590. printip = 0;
  1591. } else if (is_kernel_addr(lr)
  1592. && !(fnstart <= lr && lr < fnend)) {
  1593. printf("[link register ] ");
  1594. xmon_print_symbol(lr, " ", "\n");
  1595. }
  1596. if (printip) {
  1597. printf("["REG"] ", sp);
  1598. xmon_print_symbol(ip, " ", " (unreliable)\n");
  1599. }
  1600. pc = lr = 0;
  1601. } else {
  1602. printf("["REG"] ", sp);
  1603. xmon_print_symbol(ip, " ", "\n");
  1604. }
  1605. /* Look for "regshere" marker to see if this is
  1606. an exception frame. */
  1607. if (mread(sp + MARKER_OFFSET, &marker, sizeof(unsigned long))
  1608. && marker == STACK_FRAME_REGS_MARKER) {
  1609. if (mread(sp + STACK_FRAME_OVERHEAD, &regs, sizeof(regs))
  1610. != sizeof(regs)) {
  1611. printf("Couldn't read registers at %lx\n",
  1612. sp + STACK_FRAME_OVERHEAD);
  1613. break;
  1614. }
  1615. printf("--- Exception: %lx %s at ", regs.trap,
  1616. getvecname(TRAP(&regs)));
  1617. pc = regs.nip;
  1618. lr = regs.link;
  1619. xmon_print_symbol(pc, " ", "\n");
  1620. }
  1621. if (newsp == 0)
  1622. break;
  1623. sp = newsp;
  1624. }
  1625. }
  1626. static void backtrace(struct pt_regs *excp)
  1627. {
  1628. unsigned long sp;
  1629. if (scanhex(&sp))
  1630. xmon_show_stack(sp, 0, 0);
  1631. else
  1632. xmon_show_stack(excp->gpr[1], excp->link, excp->nip);
  1633. scannl();
  1634. }
  1635. static void print_bug_trap(struct pt_regs *regs)
  1636. {
  1637. #ifdef CONFIG_BUG
  1638. const struct bug_entry *bug;
  1639. unsigned long addr;
  1640. if (regs->msr & MSR_PR)
  1641. return; /* not in kernel */
  1642. addr = regs->nip; /* address of trap instruction */
  1643. if (!is_kernel_addr(addr))
  1644. return;
  1645. bug = find_bug(regs->nip);
  1646. if (bug == NULL)
  1647. return;
  1648. if (is_warning_bug(bug))
  1649. return;
  1650. #ifdef CONFIG_DEBUG_BUGVERBOSE
  1651. printf("kernel BUG at %s:%u!\n",
  1652. (char *)bug + bug->file_disp, bug->line);
  1653. #else
  1654. printf("kernel BUG at %px!\n", (void *)bug + bug->bug_addr_disp);
  1655. #endif
  1656. #endif /* CONFIG_BUG */
  1657. }
  1658. static void excprint(struct pt_regs *fp)
  1659. {
  1660. unsigned long trap;
  1661. #ifdef CONFIG_SMP
  1662. printf("cpu 0x%x: ", smp_processor_id());
  1663. #endif /* CONFIG_SMP */
  1664. trap = TRAP(fp);
  1665. printf("Vector: %lx %s at [%px]\n", fp->trap, getvecname(trap), fp);
  1666. printf(" pc: ");
  1667. xmon_print_symbol(fp->nip, ": ", "\n");
  1668. printf(" lr: ");
  1669. xmon_print_symbol(fp->link, ": ", "\n");
  1670. printf(" sp: %lx\n", fp->gpr[1]);
  1671. printf(" msr: %lx\n", fp->msr);
  1672. if (trap == INTERRUPT_DATA_STORAGE ||
  1673. trap == INTERRUPT_DATA_SEGMENT ||
  1674. trap == INTERRUPT_ALIGNMENT ||
  1675. trap == INTERRUPT_MACHINE_CHECK) {
  1676. printf(" dar: %lx\n", fp->dar);
  1677. if (trap != INTERRUPT_DATA_SEGMENT)
  1678. printf(" dsisr: %lx\n", fp->dsisr);
  1679. }
  1680. printf(" current = 0x%px\n", current);
  1681. #ifdef CONFIG_PPC64
  1682. printf(" paca = 0x%px\t irqmask: 0x%02x\t irq_happened: 0x%02x\n",
  1683. local_paca, local_paca->irq_soft_mask, local_paca->irq_happened);
  1684. #endif
  1685. if (current) {
  1686. printf(" pid = %d, comm = %s\n",
  1687. current->pid, current->comm);
  1688. }
  1689. if (trap == INTERRUPT_PROGRAM)
  1690. print_bug_trap(fp);
  1691. printf(linux_banner);
  1692. }
  1693. static void prregs(struct pt_regs *fp)
  1694. {
  1695. int n, trap;
  1696. unsigned long base;
  1697. struct pt_regs regs;
  1698. if (scanhex(&base)) {
  1699. if (setjmp(bus_error_jmp) == 0) {
  1700. catch_memory_errors = 1;
  1701. sync();
  1702. regs = *(struct pt_regs *)base;
  1703. sync();
  1704. __delay(200);
  1705. } else {
  1706. catch_memory_errors = 0;
  1707. printf("*** Error reading registers from "REG"\n",
  1708. base);
  1709. return;
  1710. }
  1711. catch_memory_errors = 0;
  1712. fp = &regs;
  1713. }
  1714. #ifdef CONFIG_PPC64
  1715. #define R_PER_LINE 2
  1716. #else
  1717. #define R_PER_LINE 4
  1718. #endif
  1719. for (n = 0; n < 32; ++n) {
  1720. printf("R%.2d = "REG"%s", n, fp->gpr[n],
  1721. (n % R_PER_LINE) == R_PER_LINE - 1 ? "\n" : " ");
  1722. }
  1723. printf("pc = ");
  1724. xmon_print_symbol(fp->nip, " ", "\n");
  1725. if (!trap_is_syscall(fp) && cpu_has_feature(CPU_FTR_CFAR)) {
  1726. printf("cfar= ");
  1727. xmon_print_symbol(fp->orig_gpr3, " ", "\n");
  1728. }
  1729. printf("lr = ");
  1730. xmon_print_symbol(fp->link, " ", "\n");
  1731. printf("msr = "REG" cr = %.8lx\n", fp->msr, fp->ccr);
  1732. printf("ctr = "REG" xer = "REG" trap = %4lx\n",
  1733. fp->ctr, fp->xer, fp->trap);
  1734. trap = TRAP(fp);
  1735. if (trap == INTERRUPT_DATA_STORAGE ||
  1736. trap == INTERRUPT_DATA_SEGMENT ||
  1737. trap == INTERRUPT_ALIGNMENT)
  1738. printf("dar = "REG" dsisr = %.8lx\n", fp->dar, fp->dsisr);
  1739. }
  1740. static void cacheflush(void)
  1741. {
  1742. int cmd;
  1743. unsigned long nflush;
  1744. cmd = inchar();
  1745. if (cmd != 'i')
  1746. termch = cmd;
  1747. scanhex((void *)&adrs);
  1748. if (termch != '\n')
  1749. termch = 0;
  1750. nflush = 1;
  1751. scanhex(&nflush);
  1752. nflush = (nflush + L1_CACHE_BYTES - 1) / L1_CACHE_BYTES;
  1753. if (setjmp(bus_error_jmp) == 0) {
  1754. catch_memory_errors = 1;
  1755. sync();
  1756. if (cmd != 'i' || IS_ENABLED(CONFIG_PPC_BOOK3S_64)) {
  1757. for (; nflush > 0; --nflush, adrs += L1_CACHE_BYTES)
  1758. cflush((void *) adrs);
  1759. } else {
  1760. for (; nflush > 0; --nflush, adrs += L1_CACHE_BYTES)
  1761. cinval((void *) adrs);
  1762. }
  1763. sync();
  1764. /* wait a little while to see if we get a machine check */
  1765. __delay(200);
  1766. }
  1767. catch_memory_errors = 0;
  1768. }
  1769. extern unsigned long xmon_mfspr(int spr, unsigned long default_value);
  1770. extern void xmon_mtspr(int spr, unsigned long value);
  1771. static int
  1772. read_spr(int n, unsigned long *vp)
  1773. {
  1774. unsigned long ret = -1UL;
  1775. int ok = 0;
  1776. if (setjmp(bus_error_jmp) == 0) {
  1777. catch_spr_faults = 1;
  1778. sync();
  1779. ret = xmon_mfspr(n, *vp);
  1780. sync();
  1781. *vp = ret;
  1782. ok = 1;
  1783. }
  1784. catch_spr_faults = 0;
  1785. return ok;
  1786. }
  1787. static void
  1788. write_spr(int n, unsigned long val)
  1789. {
  1790. if (xmon_is_ro) {
  1791. printf(xmon_ro_msg);
  1792. return;
  1793. }
  1794. if (setjmp(bus_error_jmp) == 0) {
  1795. catch_spr_faults = 1;
  1796. sync();
  1797. xmon_mtspr(n, val);
  1798. sync();
  1799. } else {
  1800. printf("SPR 0x%03x (%4d) Faulted during write\n", n, n);
  1801. }
  1802. catch_spr_faults = 0;
  1803. }
  1804. static void dump_206_sprs(void)
  1805. {
  1806. #ifdef CONFIG_PPC64
  1807. if (!cpu_has_feature(CPU_FTR_ARCH_206))
  1808. return;
  1809. /* Actually some of these pre-date 2.06, but whatever */
  1810. printf("srr0 = %.16lx srr1 = %.16lx dsisr = %.8lx\n",
  1811. mfspr(SPRN_SRR0), mfspr(SPRN_SRR1), mfspr(SPRN_DSISR));
  1812. printf("dscr = %.16lx ppr = %.16lx pir = %.8lx\n",
  1813. mfspr(SPRN_DSCR), mfspr(SPRN_PPR), mfspr(SPRN_PIR));
  1814. printf("amr = %.16lx uamor = %.16lx\n",
  1815. mfspr(SPRN_AMR), mfspr(SPRN_UAMOR));
  1816. if (!(mfmsr() & MSR_HV))
  1817. return;
  1818. printf("sdr1 = %.16lx hdar = %.16lx hdsisr = %.8lx\n",
  1819. mfspr(SPRN_SDR1), mfspr(SPRN_HDAR), mfspr(SPRN_HDSISR));
  1820. printf("hsrr0 = %.16lx hsrr1 = %.16lx hdec = %.16lx\n",
  1821. mfspr(SPRN_HSRR0), mfspr(SPRN_HSRR1), mfspr(SPRN_HDEC));
  1822. printf("lpcr = %.16lx pcr = %.16lx lpidr = %.8lx\n",
  1823. mfspr(SPRN_LPCR), mfspr(SPRN_PCR), mfspr(SPRN_LPID));
  1824. printf("hsprg0 = %.16lx hsprg1 = %.16lx amor = %.16lx\n",
  1825. mfspr(SPRN_HSPRG0), mfspr(SPRN_HSPRG1), mfspr(SPRN_AMOR));
  1826. printf("dabr = %.16lx dabrx = %.16lx\n",
  1827. mfspr(SPRN_DABR), mfspr(SPRN_DABRX));
  1828. #endif
  1829. }
  1830. static void dump_207_sprs(void)
  1831. {
  1832. #ifdef CONFIG_PPC64
  1833. unsigned long msr;
  1834. if (!cpu_has_feature(CPU_FTR_ARCH_207S))
  1835. return;
  1836. printf("dpdes = %.16lx tir = %.16lx cir = %.8lx\n",
  1837. mfspr(SPRN_DPDES), mfspr(SPRN_TIR), mfspr(SPRN_CIR));
  1838. printf("fscr = %.16lx tar = %.16lx pspb = %.8lx\n",
  1839. mfspr(SPRN_FSCR), mfspr(SPRN_TAR), mfspr(SPRN_PSPB));
  1840. msr = mfmsr();
  1841. if (msr & MSR_TM) {
  1842. /* Only if TM has been enabled in the kernel */
  1843. printf("tfhar = %.16lx tfiar = %.16lx texasr = %.16lx\n",
  1844. mfspr(SPRN_TFHAR), mfspr(SPRN_TFIAR),
  1845. mfspr(SPRN_TEXASR));
  1846. }
  1847. printf("mmcr0 = %.16lx mmcr1 = %.16lx mmcr2 = %.16lx\n",
  1848. mfspr(SPRN_MMCR0), mfspr(SPRN_MMCR1), mfspr(SPRN_MMCR2));
  1849. printf("pmc1 = %.8lx pmc2 = %.8lx pmc3 = %.8lx pmc4 = %.8lx\n",
  1850. mfspr(SPRN_PMC1), mfspr(SPRN_PMC2),
  1851. mfspr(SPRN_PMC3), mfspr(SPRN_PMC4));
  1852. printf("mmcra = %.16lx siar = %.16lx pmc5 = %.8lx\n",
  1853. mfspr(SPRN_MMCRA), mfspr(SPRN_SIAR), mfspr(SPRN_PMC5));
  1854. printf("sdar = %.16lx sier = %.16lx pmc6 = %.8lx\n",
  1855. mfspr(SPRN_SDAR), mfspr(SPRN_SIER), mfspr(SPRN_PMC6));
  1856. printf("ebbhr = %.16lx ebbrr = %.16lx bescr = %.16lx\n",
  1857. mfspr(SPRN_EBBHR), mfspr(SPRN_EBBRR), mfspr(SPRN_BESCR));
  1858. printf("iamr = %.16lx\n", mfspr(SPRN_IAMR));
  1859. if (!(msr & MSR_HV))
  1860. return;
  1861. printf("hfscr = %.16lx dhdes = %.16lx rpr = %.16lx\n",
  1862. mfspr(SPRN_HFSCR), mfspr(SPRN_DHDES), mfspr(SPRN_RPR));
  1863. printf("dawr0 = %.16lx dawrx0 = %.16lx\n",
  1864. mfspr(SPRN_DAWR0), mfspr(SPRN_DAWRX0));
  1865. if (nr_wp_slots() > 1) {
  1866. printf("dawr1 = %.16lx dawrx1 = %.16lx\n",
  1867. mfspr(SPRN_DAWR1), mfspr(SPRN_DAWRX1));
  1868. }
  1869. printf("ciabr = %.16lx\n", mfspr(SPRN_CIABR));
  1870. #endif
  1871. }
  1872. static void dump_300_sprs(void)
  1873. {
  1874. #ifdef CONFIG_PPC64
  1875. bool hv = mfmsr() & MSR_HV;
  1876. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  1877. return;
  1878. if (cpu_has_feature(CPU_FTR_P9_TIDR)) {
  1879. printf("pidr = %.16lx tidr = %.16lx\n",
  1880. mfspr(SPRN_PID), mfspr(SPRN_TIDR));
  1881. } else {
  1882. printf("pidr = %.16lx\n",
  1883. mfspr(SPRN_PID));
  1884. }
  1885. printf("psscr = %.16lx\n",
  1886. hv ? mfspr(SPRN_PSSCR) : mfspr(SPRN_PSSCR_PR));
  1887. if (!hv)
  1888. return;
  1889. printf("ptcr = %.16lx asdr = %.16lx\n",
  1890. mfspr(SPRN_PTCR), mfspr(SPRN_ASDR));
  1891. #endif
  1892. }
  1893. static void dump_310_sprs(void)
  1894. {
  1895. #ifdef CONFIG_PPC64
  1896. if (!cpu_has_feature(CPU_FTR_ARCH_31))
  1897. return;
  1898. printf("mmcr3 = %.16lx, sier2 = %.16lx, sier3 = %.16lx\n",
  1899. mfspr(SPRN_MMCR3), mfspr(SPRN_SIER2), mfspr(SPRN_SIER3));
  1900. #endif
  1901. }
  1902. static void dump_one_spr(int spr, bool show_unimplemented)
  1903. {
  1904. unsigned long val;
  1905. val = 0xdeadbeef;
  1906. if (!read_spr(spr, &val)) {
  1907. printf("SPR 0x%03x (%4d) Faulted during read\n", spr, spr);
  1908. return;
  1909. }
  1910. if (val == 0xdeadbeef) {
  1911. /* Looks like read was a nop, confirm */
  1912. val = 0x0badcafe;
  1913. if (!read_spr(spr, &val)) {
  1914. printf("SPR 0x%03x (%4d) Faulted during read\n", spr, spr);
  1915. return;
  1916. }
  1917. if (val == 0x0badcafe) {
  1918. if (show_unimplemented)
  1919. printf("SPR 0x%03x (%4d) Unimplemented\n", spr, spr);
  1920. return;
  1921. }
  1922. }
  1923. printf("SPR 0x%03x (%4d) = 0x%lx\n", spr, spr, val);
  1924. }
  1925. static void super_regs(void)
  1926. {
  1927. static unsigned long regno;
  1928. int cmd;
  1929. int spr;
  1930. cmd = skipbl();
  1931. switch (cmd) {
  1932. case '\n': {
  1933. unsigned long sp, toc;
  1934. asm("mr %0,1" : "=r" (sp) :);
  1935. asm("mr %0,2" : "=r" (toc) :);
  1936. printf("msr = "REG" sprg0 = "REG"\n",
  1937. mfmsr(), mfspr(SPRN_SPRG0));
  1938. printf("pvr = "REG" sprg1 = "REG"\n",
  1939. mfspr(SPRN_PVR), mfspr(SPRN_SPRG1));
  1940. printf("dec = "REG" sprg2 = "REG"\n",
  1941. mfspr(SPRN_DEC), mfspr(SPRN_SPRG2));
  1942. printf("sp = "REG" sprg3 = "REG"\n", sp, mfspr(SPRN_SPRG3));
  1943. printf("toc = "REG" dar = "REG"\n", toc, mfspr(SPRN_DAR));
  1944. dump_206_sprs();
  1945. dump_207_sprs();
  1946. dump_300_sprs();
  1947. dump_310_sprs();
  1948. return;
  1949. }
  1950. case 'w': {
  1951. unsigned long val;
  1952. scanhex(&regno);
  1953. val = 0;
  1954. read_spr(regno, &val);
  1955. scanhex(&val);
  1956. write_spr(regno, val);
  1957. dump_one_spr(regno, true);
  1958. break;
  1959. }
  1960. case 'r':
  1961. scanhex(&regno);
  1962. dump_one_spr(regno, true);
  1963. break;
  1964. case 'a':
  1965. /* dump ALL SPRs */
  1966. for (spr = 1; spr < 1024; ++spr)
  1967. dump_one_spr(spr, false);
  1968. break;
  1969. }
  1970. scannl();
  1971. }
  1972. /*
  1973. * Stuff for reading and writing memory safely
  1974. */
  1975. static int
  1976. mread(unsigned long adrs, void *buf, int size)
  1977. {
  1978. volatile int n;
  1979. char *p, *q;
  1980. n = 0;
  1981. if (setjmp(bus_error_jmp) == 0) {
  1982. catch_memory_errors = 1;
  1983. sync();
  1984. p = (char *)adrs;
  1985. q = (char *)buf;
  1986. switch (size) {
  1987. case 2:
  1988. *(u16 *)q = *(u16 *)p;
  1989. break;
  1990. case 4:
  1991. *(u32 *)q = *(u32 *)p;
  1992. break;
  1993. case 8:
  1994. *(u64 *)q = *(u64 *)p;
  1995. break;
  1996. default:
  1997. for( ; n < size; ++n) {
  1998. *q++ = *p++;
  1999. sync();
  2000. }
  2001. }
  2002. sync();
  2003. /* wait a little while to see if we get a machine check */
  2004. __delay(200);
  2005. n = size;
  2006. }
  2007. catch_memory_errors = 0;
  2008. return n;
  2009. }
  2010. static int
  2011. mwrite(unsigned long adrs, void *buf, int size)
  2012. {
  2013. volatile int n;
  2014. char *p, *q;
  2015. n = 0;
  2016. if (xmon_is_ro) {
  2017. printf(xmon_ro_msg);
  2018. return n;
  2019. }
  2020. if (setjmp(bus_error_jmp) == 0) {
  2021. catch_memory_errors = 1;
  2022. sync();
  2023. p = (char *) adrs;
  2024. q = (char *) buf;
  2025. switch (size) {
  2026. case 2:
  2027. *(u16 *)p = *(u16 *)q;
  2028. break;
  2029. case 4:
  2030. *(u32 *)p = *(u32 *)q;
  2031. break;
  2032. case 8:
  2033. *(u64 *)p = *(u64 *)q;
  2034. break;
  2035. default:
  2036. for ( ; n < size; ++n) {
  2037. *p++ = *q++;
  2038. sync();
  2039. }
  2040. }
  2041. sync();
  2042. /* wait a little while to see if we get a machine check */
  2043. __delay(200);
  2044. n = size;
  2045. } else {
  2046. printf("*** Error writing address "REG"\n", adrs + n);
  2047. }
  2048. catch_memory_errors = 0;
  2049. return n;
  2050. }
  2051. static int
  2052. mread_instr(unsigned long adrs, ppc_inst_t *instr)
  2053. {
  2054. volatile int n;
  2055. n = 0;
  2056. if (setjmp(bus_error_jmp) == 0) {
  2057. catch_memory_errors = 1;
  2058. sync();
  2059. *instr = ppc_inst_read((u32 *)adrs);
  2060. sync();
  2061. /* wait a little while to see if we get a machine check */
  2062. __delay(200);
  2063. n = ppc_inst_len(*instr);
  2064. }
  2065. catch_memory_errors = 0;
  2066. return n;
  2067. }
  2068. static int fault_type;
  2069. static int fault_except;
  2070. static char *fault_chars[] = { "--", "**", "##" };
  2071. static int handle_fault(struct pt_regs *regs)
  2072. {
  2073. fault_except = TRAP(regs);
  2074. switch (TRAP(regs)) {
  2075. case 0x200:
  2076. fault_type = 0;
  2077. break;
  2078. case 0x300:
  2079. case 0x380:
  2080. fault_type = 1;
  2081. break;
  2082. default:
  2083. fault_type = 2;
  2084. }
  2085. longjmp(bus_error_jmp, 1);
  2086. return 0;
  2087. }
  2088. #define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t))
  2089. static void
  2090. byterev(unsigned char *val, int size)
  2091. {
  2092. int t;
  2093. switch (size) {
  2094. case 2:
  2095. SWAP(val[0], val[1], t);
  2096. break;
  2097. case 4:
  2098. SWAP(val[0], val[3], t);
  2099. SWAP(val[1], val[2], t);
  2100. break;
  2101. case 8: /* is there really any use for this? */
  2102. SWAP(val[0], val[7], t);
  2103. SWAP(val[1], val[6], t);
  2104. SWAP(val[2], val[5], t);
  2105. SWAP(val[3], val[4], t);
  2106. break;
  2107. }
  2108. }
  2109. static int brev;
  2110. static int mnoread;
  2111. static char *memex_help_string =
  2112. "Memory examine command usage:\n"
  2113. "m [addr] [flags] examine/change memory\n"
  2114. " addr is optional. will start where left off.\n"
  2115. " flags may include chars from this set:\n"
  2116. " b modify by bytes (default)\n"
  2117. " w modify by words (2 byte)\n"
  2118. " l modify by longs (4 byte)\n"
  2119. " d modify by doubleword (8 byte)\n"
  2120. " r toggle reverse byte order mode\n"
  2121. " n do not read memory (for i/o spaces)\n"
  2122. " . ok to read (default)\n"
  2123. "NOTE: flags are saved as defaults\n"
  2124. "";
  2125. static char *memex_subcmd_help_string =
  2126. "Memory examine subcommands:\n"
  2127. " hexval write this val to current location\n"
  2128. " 'string' write chars from string to this location\n"
  2129. " ' increment address\n"
  2130. " ^ decrement address\n"
  2131. " / increment addr by 0x10. //=0x100, ///=0x1000, etc\n"
  2132. " \\ decrement addr by 0x10. \\\\=0x100, \\\\\\=0x1000, etc\n"
  2133. " ` clear no-read flag\n"
  2134. " ; stay at this addr\n"
  2135. " v change to byte mode\n"
  2136. " w change to word (2 byte) mode\n"
  2137. " l change to long (4 byte) mode\n"
  2138. " u change to doubleword (8 byte) mode\n"
  2139. " m addr change current addr\n"
  2140. " n toggle no-read flag\n"
  2141. " r toggle byte reverse flag\n"
  2142. " < count back up count bytes\n"
  2143. " > count skip forward count bytes\n"
  2144. " x exit this mode\n"
  2145. "";
  2146. static void
  2147. memex(void)
  2148. {
  2149. int cmd, inc, i, nslash;
  2150. unsigned long n;
  2151. unsigned char val[16];
  2152. scanhex((void *)&adrs);
  2153. cmd = skipbl();
  2154. if (cmd == '?') {
  2155. printf(memex_help_string);
  2156. return;
  2157. } else {
  2158. termch = cmd;
  2159. }
  2160. last_cmd = "m\n";
  2161. while ((cmd = skipbl()) != '\n') {
  2162. switch( cmd ){
  2163. case 'b': size = 1; break;
  2164. case 'w': size = 2; break;
  2165. case 'l': size = 4; break;
  2166. case 'd': size = 8; break;
  2167. case 'r': brev = !brev; break;
  2168. case 'n': mnoread = 1; break;
  2169. case '.': mnoread = 0; break;
  2170. }
  2171. }
  2172. if( size <= 0 )
  2173. size = 1;
  2174. else if( size > 8 )
  2175. size = 8;
  2176. for(;;){
  2177. if (!mnoread)
  2178. n = mread(adrs, val, size);
  2179. printf(REG"%c", adrs, brev? 'r': ' ');
  2180. if (!mnoread) {
  2181. if (brev)
  2182. byterev(val, size);
  2183. putchar(' ');
  2184. for (i = 0; i < n; ++i)
  2185. printf("%.2x", val[i]);
  2186. for (; i < size; ++i)
  2187. printf("%s", fault_chars[fault_type]);
  2188. }
  2189. putchar(' ');
  2190. inc = size;
  2191. nslash = 0;
  2192. for(;;){
  2193. if( scanhex(&n) ){
  2194. for (i = 0; i < size; ++i)
  2195. val[i] = n >> (i * 8);
  2196. if (!brev)
  2197. byterev(val, size);
  2198. mwrite(adrs, val, size);
  2199. inc = size;
  2200. }
  2201. cmd = skipbl();
  2202. if (cmd == '\n')
  2203. break;
  2204. inc = 0;
  2205. switch (cmd) {
  2206. case '\'':
  2207. for(;;){
  2208. n = inchar();
  2209. if( n == '\\' )
  2210. n = bsesc();
  2211. else if( n == '\'' )
  2212. break;
  2213. for (i = 0; i < size; ++i)
  2214. val[i] = n >> (i * 8);
  2215. if (!brev)
  2216. byterev(val, size);
  2217. mwrite(adrs, val, size);
  2218. adrs += size;
  2219. }
  2220. adrs -= size;
  2221. inc = size;
  2222. break;
  2223. case ',':
  2224. adrs += size;
  2225. break;
  2226. case '.':
  2227. mnoread = 0;
  2228. break;
  2229. case ';':
  2230. break;
  2231. case 'x':
  2232. case EOF:
  2233. scannl();
  2234. return;
  2235. case 'b':
  2236. case 'v':
  2237. size = 1;
  2238. break;
  2239. case 'w':
  2240. size = 2;
  2241. break;
  2242. case 'l':
  2243. size = 4;
  2244. break;
  2245. case 'u':
  2246. size = 8;
  2247. break;
  2248. case '^':
  2249. adrs -= size;
  2250. break;
  2251. case '/':
  2252. if (nslash > 0)
  2253. adrs -= 1 << nslash;
  2254. else
  2255. nslash = 0;
  2256. nslash += 4;
  2257. adrs += 1 << nslash;
  2258. break;
  2259. case '\\':
  2260. if (nslash < 0)
  2261. adrs += 1 << -nslash;
  2262. else
  2263. nslash = 0;
  2264. nslash -= 4;
  2265. adrs -= 1 << -nslash;
  2266. break;
  2267. case 'm':
  2268. scanhex((void *)&adrs);
  2269. break;
  2270. case 'n':
  2271. mnoread = 1;
  2272. break;
  2273. case 'r':
  2274. brev = !brev;
  2275. break;
  2276. case '<':
  2277. n = size;
  2278. scanhex(&n);
  2279. adrs -= n;
  2280. break;
  2281. case '>':
  2282. n = size;
  2283. scanhex(&n);
  2284. adrs += n;
  2285. break;
  2286. case '?':
  2287. printf(memex_subcmd_help_string);
  2288. break;
  2289. }
  2290. }
  2291. adrs += inc;
  2292. }
  2293. }
  2294. static int
  2295. bsesc(void)
  2296. {
  2297. int c;
  2298. c = inchar();
  2299. switch( c ){
  2300. case 'n': c = '\n'; break;
  2301. case 'r': c = '\r'; break;
  2302. case 'b': c = '\b'; break;
  2303. case 't': c = '\t'; break;
  2304. }
  2305. return c;
  2306. }
  2307. static void xmon_rawdump (unsigned long adrs, long ndump)
  2308. {
  2309. long n, m, r, nr;
  2310. unsigned char temp[16];
  2311. for (n = ndump; n > 0;) {
  2312. r = n < 16? n: 16;
  2313. nr = mread(adrs, temp, r);
  2314. adrs += nr;
  2315. for (m = 0; m < r; ++m) {
  2316. if (m < nr)
  2317. printf("%.2x", temp[m]);
  2318. else
  2319. printf("%s", fault_chars[fault_type]);
  2320. }
  2321. n -= r;
  2322. if (nr < r)
  2323. break;
  2324. }
  2325. printf("\n");
  2326. }
  2327. static void dump_tracing(void)
  2328. {
  2329. int c;
  2330. c = inchar();
  2331. if (c == 'c')
  2332. ftrace_dump(DUMP_ORIG);
  2333. else
  2334. ftrace_dump(DUMP_ALL);
  2335. }
  2336. #ifdef CONFIG_PPC64
  2337. static void dump_one_paca(int cpu)
  2338. {
  2339. struct paca_struct *p;
  2340. #ifdef CONFIG_PPC_64S_HASH_MMU
  2341. int i = 0;
  2342. #endif
  2343. if (setjmp(bus_error_jmp) != 0) {
  2344. printf("*** Error dumping paca for cpu 0x%x!\n", cpu);
  2345. return;
  2346. }
  2347. catch_memory_errors = 1;
  2348. sync();
  2349. p = paca_ptrs[cpu];
  2350. printf("paca for cpu 0x%x @ %px:\n", cpu, p);
  2351. printf(" %-*s = %s\n", 25, "possible", cpu_possible(cpu) ? "yes" : "no");
  2352. printf(" %-*s = %s\n", 25, "present", cpu_present(cpu) ? "yes" : "no");
  2353. printf(" %-*s = %s\n", 25, "online", cpu_online(cpu) ? "yes" : "no");
  2354. #define DUMP(paca, name, format) \
  2355. printf(" %-*s = "format"\t(0x%lx)\n", 25, #name, 18, paca->name, \
  2356. offsetof(struct paca_struct, name));
  2357. DUMP(p, lock_token, "%#-*x");
  2358. DUMP(p, paca_index, "%#-*x");
  2359. DUMP(p, kernel_toc, "%#-*llx");
  2360. DUMP(p, kernelbase, "%#-*llx");
  2361. DUMP(p, kernel_msr, "%#-*llx");
  2362. DUMP(p, emergency_sp, "%-*px");
  2363. #ifdef CONFIG_PPC_BOOK3S_64
  2364. DUMP(p, nmi_emergency_sp, "%-*px");
  2365. DUMP(p, mc_emergency_sp, "%-*px");
  2366. DUMP(p, in_nmi, "%#-*x");
  2367. DUMP(p, in_mce, "%#-*x");
  2368. DUMP(p, hmi_event_available, "%#-*x");
  2369. #endif
  2370. DUMP(p, data_offset, "%#-*llx");
  2371. DUMP(p, hw_cpu_id, "%#-*x");
  2372. DUMP(p, cpu_start, "%#-*x");
  2373. DUMP(p, kexec_state, "%#-*x");
  2374. #ifdef CONFIG_PPC_BOOK3S_64
  2375. #ifdef CONFIG_PPC_64S_HASH_MMU
  2376. if (!early_radix_enabled()) {
  2377. for (i = 0; i < SLB_NUM_BOLTED; i++) {
  2378. u64 esid, vsid;
  2379. if (!p->slb_shadow_ptr)
  2380. continue;
  2381. esid = be64_to_cpu(p->slb_shadow_ptr->save_area[i].esid);
  2382. vsid = be64_to_cpu(p->slb_shadow_ptr->save_area[i].vsid);
  2383. if (esid || vsid) {
  2384. printf(" %-*s[%d] = 0x%016llx 0x%016llx\n",
  2385. 22, "slb_shadow", i, esid, vsid);
  2386. }
  2387. }
  2388. DUMP(p, vmalloc_sllp, "%#-*x");
  2389. DUMP(p, stab_rr, "%#-*x");
  2390. DUMP(p, slb_used_bitmap, "%#-*x");
  2391. DUMP(p, slb_kern_bitmap, "%#-*x");
  2392. if (!early_cpu_has_feature(CPU_FTR_ARCH_300)) {
  2393. DUMP(p, slb_cache_ptr, "%#-*x");
  2394. for (i = 0; i < SLB_CACHE_ENTRIES; i++)
  2395. printf(" %-*s[%d] = 0x%016x\n",
  2396. 22, "slb_cache", i, p->slb_cache[i]);
  2397. }
  2398. }
  2399. #endif
  2400. DUMP(p, rfi_flush_fallback_area, "%-*px");
  2401. #endif
  2402. DUMP(p, dscr_default, "%#-*llx");
  2403. #ifdef CONFIG_PPC_BOOK3E_64
  2404. DUMP(p, pgd, "%-*px");
  2405. DUMP(p, kernel_pgd, "%-*px");
  2406. DUMP(p, tcd_ptr, "%-*px");
  2407. DUMP(p, mc_kstack, "%-*px");
  2408. DUMP(p, crit_kstack, "%-*px");
  2409. DUMP(p, dbg_kstack, "%-*px");
  2410. #endif
  2411. DUMP(p, __current, "%-*px");
  2412. DUMP(p, kstack, "%#-*llx");
  2413. printf(" %-*s = 0x%016llx\n", 25, "kstack_base", p->kstack & ~(THREAD_SIZE - 1));
  2414. #ifdef CONFIG_STACKPROTECTOR
  2415. DUMP(p, canary, "%#-*lx");
  2416. #endif
  2417. DUMP(p, saved_r1, "%#-*llx");
  2418. #ifdef CONFIG_PPC_BOOK3E_64
  2419. DUMP(p, trap_save, "%#-*x");
  2420. #endif
  2421. DUMP(p, irq_soft_mask, "%#-*x");
  2422. DUMP(p, irq_happened, "%#-*x");
  2423. #ifdef CONFIG_MMIOWB
  2424. DUMP(p, mmiowb_state.nesting_count, "%#-*x");
  2425. DUMP(p, mmiowb_state.mmiowb_pending, "%#-*x");
  2426. #endif
  2427. DUMP(p, irq_work_pending, "%#-*x");
  2428. DUMP(p, sprg_vdso, "%#-*llx");
  2429. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  2430. DUMP(p, tm_scratch, "%#-*llx");
  2431. #endif
  2432. #ifdef CONFIG_PPC_POWERNV
  2433. DUMP(p, idle_state, "%#-*lx");
  2434. if (!early_cpu_has_feature(CPU_FTR_ARCH_300)) {
  2435. DUMP(p, thread_idle_state, "%#-*x");
  2436. DUMP(p, subcore_sibling_mask, "%#-*x");
  2437. } else {
  2438. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  2439. DUMP(p, requested_psscr, "%#-*llx");
  2440. DUMP(p, dont_stop.counter, "%#-*x");
  2441. #endif
  2442. }
  2443. #endif
  2444. DUMP(p, accounting.utime, "%#-*lx");
  2445. DUMP(p, accounting.stime, "%#-*lx");
  2446. #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
  2447. DUMP(p, accounting.utime_scaled, "%#-*lx");
  2448. #endif
  2449. DUMP(p, accounting.starttime, "%#-*lx");
  2450. DUMP(p, accounting.starttime_user, "%#-*lx");
  2451. #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
  2452. DUMP(p, accounting.startspurr, "%#-*lx");
  2453. DUMP(p, accounting.utime_sspurr, "%#-*lx");
  2454. #endif
  2455. DUMP(p, accounting.steal_time, "%#-*lx");
  2456. #undef DUMP
  2457. catch_memory_errors = 0;
  2458. sync();
  2459. }
  2460. static void dump_all_pacas(void)
  2461. {
  2462. int cpu;
  2463. if (num_possible_cpus() == 0) {
  2464. printf("No possible cpus, use 'dp #' to dump individual cpus\n");
  2465. return;
  2466. }
  2467. for_each_possible_cpu(cpu)
  2468. dump_one_paca(cpu);
  2469. }
  2470. static void dump_pacas(void)
  2471. {
  2472. unsigned long num;
  2473. int c;
  2474. c = inchar();
  2475. if (c == 'a') {
  2476. dump_all_pacas();
  2477. return;
  2478. }
  2479. termch = c; /* Put c back, it wasn't 'a' */
  2480. if (scanhex(&num))
  2481. dump_one_paca(num);
  2482. else
  2483. dump_one_paca(xmon_owner);
  2484. }
  2485. #endif
  2486. #ifdef CONFIG_PPC_POWERNV
  2487. static void dump_one_xive(int cpu)
  2488. {
  2489. unsigned int hwid = get_hard_smp_processor_id(cpu);
  2490. bool hv = cpu_has_feature(CPU_FTR_HVMODE);
  2491. if (hv) {
  2492. opal_xive_dump(XIVE_DUMP_TM_HYP, hwid);
  2493. opal_xive_dump(XIVE_DUMP_TM_POOL, hwid);
  2494. opal_xive_dump(XIVE_DUMP_TM_OS, hwid);
  2495. opal_xive_dump(XIVE_DUMP_TM_USER, hwid);
  2496. opal_xive_dump(XIVE_DUMP_VP, hwid);
  2497. opal_xive_dump(XIVE_DUMP_EMU_STATE, hwid);
  2498. }
  2499. if (setjmp(bus_error_jmp) != 0) {
  2500. catch_memory_errors = 0;
  2501. printf("*** Error dumping xive on cpu %d\n", cpu);
  2502. return;
  2503. }
  2504. catch_memory_errors = 1;
  2505. sync();
  2506. xmon_xive_do_dump(cpu);
  2507. sync();
  2508. __delay(200);
  2509. catch_memory_errors = 0;
  2510. }
  2511. static void dump_all_xives(void)
  2512. {
  2513. int cpu;
  2514. if (num_online_cpus() == 0) {
  2515. printf("No possible cpus, use 'dx #' to dump individual cpus\n");
  2516. return;
  2517. }
  2518. for_each_online_cpu(cpu)
  2519. dump_one_xive(cpu);
  2520. }
  2521. static void dump_xives(void)
  2522. {
  2523. unsigned long num;
  2524. int c;
  2525. if (!xive_enabled()) {
  2526. printf("Xive disabled on this system\n");
  2527. return;
  2528. }
  2529. c = inchar();
  2530. if (c == 'a') {
  2531. dump_all_xives();
  2532. return;
  2533. } else if (c == 'i') {
  2534. if (scanhex(&num))
  2535. xmon_xive_get_irq_config(num, NULL);
  2536. else
  2537. xmon_xive_get_irq_all();
  2538. return;
  2539. }
  2540. termch = c; /* Put c back, it wasn't 'a' */
  2541. if (scanhex(&num))
  2542. dump_one_xive(num);
  2543. else
  2544. dump_one_xive(xmon_owner);
  2545. }
  2546. #endif /* CONFIG_PPC_POWERNV */
  2547. static void dump_by_size(unsigned long addr, long count, int size)
  2548. {
  2549. unsigned char temp[16];
  2550. int i, j;
  2551. u64 val;
  2552. count = ALIGN(count, 16);
  2553. for (i = 0; i < count; i += 16, addr += 16) {
  2554. printf(REG, addr);
  2555. if (mread(addr, temp, 16) != 16) {
  2556. printf("\nFaulted reading %d bytes from 0x"REG"\n", 16, addr);
  2557. return;
  2558. }
  2559. for (j = 0; j < 16; j += size) {
  2560. putchar(' ');
  2561. switch (size) {
  2562. case 1: val = temp[j]; break;
  2563. case 2: val = *(u16 *)&temp[j]; break;
  2564. case 4: val = *(u32 *)&temp[j]; break;
  2565. case 8: val = *(u64 *)&temp[j]; break;
  2566. default: val = 0;
  2567. }
  2568. printf("%0*llx", size * 2, val);
  2569. }
  2570. printf(" |");
  2571. for (j = 0; j < 16; ++j) {
  2572. val = temp[j];
  2573. putchar(' ' <= val && val <= '~' ? val : '.');
  2574. }
  2575. printf("|\n");
  2576. }
  2577. }
  2578. static void
  2579. dump(void)
  2580. {
  2581. static char last[] = { "d?\n" };
  2582. int c;
  2583. c = inchar();
  2584. #ifdef CONFIG_PPC64
  2585. if (c == 'p') {
  2586. xmon_start_pagination();
  2587. dump_pacas();
  2588. xmon_end_pagination();
  2589. return;
  2590. }
  2591. #endif
  2592. #ifdef CONFIG_PPC_POWERNV
  2593. if (c == 'x') {
  2594. xmon_start_pagination();
  2595. dump_xives();
  2596. xmon_end_pagination();
  2597. return;
  2598. }
  2599. #endif
  2600. if (c == 't') {
  2601. dump_tracing();
  2602. return;
  2603. }
  2604. if (c == '\n')
  2605. termch = c;
  2606. scanhex((void *)&adrs);
  2607. if (termch != '\n')
  2608. termch = 0;
  2609. if (c == 'i') {
  2610. scanhex(&nidump);
  2611. if (nidump == 0)
  2612. nidump = 16;
  2613. else if (nidump > MAX_IDUMP)
  2614. nidump = MAX_IDUMP;
  2615. adrs += ppc_inst_dump(adrs, nidump, 1);
  2616. last_cmd = "di\n";
  2617. } else if (c == 'l') {
  2618. dump_log_buf();
  2619. } else if (c == 'o') {
  2620. dump_opal_msglog();
  2621. } else if (c == 'v') {
  2622. /* dump virtual to physical translation */
  2623. show_pte(adrs);
  2624. } else if (c == 'r') {
  2625. scanhex(&ndump);
  2626. if (ndump == 0)
  2627. ndump = 64;
  2628. xmon_rawdump(adrs, ndump);
  2629. adrs += ndump;
  2630. last_cmd = "dr\n";
  2631. } else {
  2632. scanhex(&ndump);
  2633. if (ndump == 0)
  2634. ndump = 64;
  2635. else if (ndump > MAX_DUMP)
  2636. ndump = MAX_DUMP;
  2637. switch (c) {
  2638. case '8':
  2639. case '4':
  2640. case '2':
  2641. case '1':
  2642. ndump = ALIGN(ndump, 16);
  2643. dump_by_size(adrs, ndump, c - '0');
  2644. last[1] = c;
  2645. last_cmd = last;
  2646. break;
  2647. default:
  2648. prdump(adrs, ndump);
  2649. last_cmd = "d\n";
  2650. }
  2651. adrs += ndump;
  2652. }
  2653. }
  2654. static void
  2655. prdump(unsigned long adrs, long ndump)
  2656. {
  2657. long n, m, c, r, nr;
  2658. unsigned char temp[16];
  2659. for (n = ndump; n > 0;) {
  2660. printf(REG, adrs);
  2661. putchar(' ');
  2662. r = n < 16? n: 16;
  2663. nr = mread(adrs, temp, r);
  2664. adrs += nr;
  2665. for (m = 0; m < r; ++m) {
  2666. if ((m & (sizeof(long) - 1)) == 0 && m > 0)
  2667. putchar(' ');
  2668. if (m < nr)
  2669. printf("%.2x", temp[m]);
  2670. else
  2671. printf("%s", fault_chars[fault_type]);
  2672. }
  2673. for (; m < 16; ++m) {
  2674. if ((m & (sizeof(long) - 1)) == 0)
  2675. putchar(' ');
  2676. printf(" ");
  2677. }
  2678. printf(" |");
  2679. for (m = 0; m < r; ++m) {
  2680. if (m < nr) {
  2681. c = temp[m];
  2682. putchar(' ' <= c && c <= '~'? c: '.');
  2683. } else
  2684. putchar(' ');
  2685. }
  2686. n -= r;
  2687. for (; m < 16; ++m)
  2688. putchar(' ');
  2689. printf("|\n");
  2690. if (nr < r)
  2691. break;
  2692. }
  2693. }
  2694. typedef int (*instruction_dump_func)(unsigned long inst, unsigned long addr);
  2695. static int
  2696. generic_inst_dump(unsigned long adr, long count, int praddr,
  2697. instruction_dump_func dump_func)
  2698. {
  2699. int nr, dotted;
  2700. unsigned long first_adr;
  2701. ppc_inst_t inst, last_inst = ppc_inst(0);
  2702. dotted = 0;
  2703. for (first_adr = adr; count > 0; --count, adr += ppc_inst_len(inst)) {
  2704. nr = mread_instr(adr, &inst);
  2705. if (nr == 0) {
  2706. if (praddr) {
  2707. const char *x = fault_chars[fault_type];
  2708. printf(REG" %s%s%s%s\n", adr, x, x, x, x);
  2709. }
  2710. break;
  2711. }
  2712. if (adr > first_adr && ppc_inst_equal(inst, last_inst)) {
  2713. if (!dotted) {
  2714. printf(" ...\n");
  2715. dotted = 1;
  2716. }
  2717. continue;
  2718. }
  2719. dotted = 0;
  2720. last_inst = inst;
  2721. if (praddr)
  2722. printf(REG" %08lx", adr, ppc_inst_as_ulong(inst));
  2723. printf("\t");
  2724. if (!ppc_inst_prefixed(inst))
  2725. dump_func(ppc_inst_val(inst), adr);
  2726. else
  2727. dump_func(ppc_inst_as_ulong(inst), adr);
  2728. printf("\n");
  2729. }
  2730. return adr - first_adr;
  2731. }
  2732. static int
  2733. ppc_inst_dump(unsigned long adr, long count, int praddr)
  2734. {
  2735. return generic_inst_dump(adr, count, praddr, print_insn_powerpc);
  2736. }
  2737. void
  2738. print_address(unsigned long addr)
  2739. {
  2740. xmon_print_symbol(addr, "\t# ", "");
  2741. }
  2742. static void
  2743. dump_log_buf(void)
  2744. {
  2745. struct kmsg_dump_iter iter;
  2746. static unsigned char buf[1024];
  2747. size_t len;
  2748. if (setjmp(bus_error_jmp) != 0) {
  2749. printf("Error dumping printk buffer!\n");
  2750. return;
  2751. }
  2752. catch_memory_errors = 1;
  2753. sync();
  2754. kmsg_dump_rewind(&iter);
  2755. xmon_start_pagination();
  2756. while (kmsg_dump_get_line(&iter, false, buf, sizeof(buf), &len)) {
  2757. buf[len] = '\0';
  2758. printf("%s", buf);
  2759. }
  2760. xmon_end_pagination();
  2761. sync();
  2762. /* wait a little while to see if we get a machine check */
  2763. __delay(200);
  2764. catch_memory_errors = 0;
  2765. }
  2766. #ifdef CONFIG_PPC_POWERNV
  2767. static void dump_opal_msglog(void)
  2768. {
  2769. unsigned char buf[128];
  2770. ssize_t res;
  2771. volatile loff_t pos = 0;
  2772. if (!firmware_has_feature(FW_FEATURE_OPAL)) {
  2773. printf("Machine is not running OPAL firmware.\n");
  2774. return;
  2775. }
  2776. if (setjmp(bus_error_jmp) != 0) {
  2777. printf("Error dumping OPAL msglog!\n");
  2778. return;
  2779. }
  2780. catch_memory_errors = 1;
  2781. sync();
  2782. xmon_start_pagination();
  2783. while ((res = opal_msglog_copy(buf, pos, sizeof(buf) - 1))) {
  2784. if (res < 0) {
  2785. printf("Error dumping OPAL msglog! Error: %zd\n", res);
  2786. break;
  2787. }
  2788. buf[res] = '\0';
  2789. printf("%s", buf);
  2790. pos += res;
  2791. }
  2792. xmon_end_pagination();
  2793. sync();
  2794. /* wait a little while to see if we get a machine check */
  2795. __delay(200);
  2796. catch_memory_errors = 0;
  2797. }
  2798. #endif
  2799. /*
  2800. * Memory operations - move, set, print differences
  2801. */
  2802. static unsigned long mdest; /* destination address */
  2803. static unsigned long msrc; /* source address */
  2804. static unsigned long mval; /* byte value to set memory to */
  2805. static unsigned long mcount; /* # bytes to affect */
  2806. static unsigned long mdiffs; /* max # differences to print */
  2807. static void
  2808. memops(int cmd)
  2809. {
  2810. scanhex((void *)&mdest);
  2811. if( termch != '\n' )
  2812. termch = 0;
  2813. scanhex((void *)(cmd == 's'? &mval: &msrc));
  2814. if( termch != '\n' )
  2815. termch = 0;
  2816. scanhex((void *)&mcount);
  2817. switch( cmd ){
  2818. case 'm':
  2819. if (xmon_is_ro) {
  2820. printf(xmon_ro_msg);
  2821. break;
  2822. }
  2823. memmove((void *)mdest, (void *)msrc, mcount);
  2824. break;
  2825. case 's':
  2826. if (xmon_is_ro) {
  2827. printf(xmon_ro_msg);
  2828. break;
  2829. }
  2830. memset((void *)mdest, mval, mcount);
  2831. break;
  2832. case 'd':
  2833. if( termch != '\n' )
  2834. termch = 0;
  2835. scanhex((void *)&mdiffs);
  2836. memdiffs((unsigned char *)mdest, (unsigned char *)msrc, mcount, mdiffs);
  2837. break;
  2838. }
  2839. }
  2840. static void
  2841. memdiffs(unsigned char *p1, unsigned char *p2, unsigned nb, unsigned maxpr)
  2842. {
  2843. unsigned n, prt;
  2844. prt = 0;
  2845. for( n = nb; n > 0; --n )
  2846. if( *p1++ != *p2++ )
  2847. if( ++prt <= maxpr )
  2848. printf("%px %.2x # %px %.2x\n", p1 - 1,
  2849. p1[-1], p2 - 1, p2[-1]);
  2850. if( prt > maxpr )
  2851. printf("Total of %d differences\n", prt);
  2852. }
  2853. static unsigned mend;
  2854. static unsigned mask;
  2855. static void
  2856. memlocate(void)
  2857. {
  2858. unsigned a, n;
  2859. unsigned char val[4];
  2860. last_cmd = "ml";
  2861. scanhex((void *)&mdest);
  2862. if (termch != '\n') {
  2863. termch = 0;
  2864. scanhex((void *)&mend);
  2865. if (termch != '\n') {
  2866. termch = 0;
  2867. scanhex((void *)&mval);
  2868. mask = ~0;
  2869. if (termch != '\n') termch = 0;
  2870. scanhex((void *)&mask);
  2871. }
  2872. }
  2873. n = 0;
  2874. for (a = mdest; a < mend; a += 4) {
  2875. if (mread(a, val, 4) == 4
  2876. && ((GETWORD(val) ^ mval) & mask) == 0) {
  2877. printf("%.16x: %.16x\n", a, GETWORD(val));
  2878. if (++n >= 10)
  2879. break;
  2880. }
  2881. }
  2882. }
  2883. static unsigned long mskip = 0x1000;
  2884. static unsigned long mlim = 0xffffffff;
  2885. static void
  2886. memzcan(void)
  2887. {
  2888. unsigned char v;
  2889. unsigned a;
  2890. int ok, ook;
  2891. scanhex(&mdest);
  2892. if (termch != '\n') termch = 0;
  2893. scanhex(&mskip);
  2894. if (termch != '\n') termch = 0;
  2895. scanhex(&mlim);
  2896. ook = 0;
  2897. for (a = mdest; a < mlim; a += mskip) {
  2898. ok = mread(a, &v, 1);
  2899. if (ok && !ook) {
  2900. printf("%.8x .. ", a);
  2901. } else if (!ok && ook)
  2902. printf("%.8lx\n", a - mskip);
  2903. ook = ok;
  2904. if (a + mskip < a)
  2905. break;
  2906. }
  2907. if (ook)
  2908. printf("%.8lx\n", a - mskip);
  2909. }
  2910. static void show_task(struct task_struct *volatile tsk)
  2911. {
  2912. unsigned int p_state = READ_ONCE(tsk->__state);
  2913. char state;
  2914. /*
  2915. * Cloned from kdb_task_state_char(), which is not entirely
  2916. * appropriate for calling from xmon. This could be moved
  2917. * to a common, generic, routine used by both.
  2918. */
  2919. state = (p_state == TASK_RUNNING) ? 'R' :
  2920. (p_state & TASK_UNINTERRUPTIBLE) ? 'D' :
  2921. (p_state & TASK_STOPPED) ? 'T' :
  2922. (p_state & TASK_TRACED) ? 'C' :
  2923. (tsk->exit_state & EXIT_ZOMBIE) ? 'Z' :
  2924. (tsk->exit_state & EXIT_DEAD) ? 'E' :
  2925. (p_state & TASK_INTERRUPTIBLE) ? 'S' : '?';
  2926. printf("%16px %16lx %16px %6d %6d %c %2d %s\n", tsk,
  2927. tsk->thread.ksp, tsk->thread.regs,
  2928. tsk->pid, rcu_dereference(tsk->parent)->pid,
  2929. state, task_cpu(tsk),
  2930. tsk->comm);
  2931. }
  2932. #ifdef CONFIG_PPC_BOOK3S_64
  2933. static void format_pte(void *ptep, unsigned long pte)
  2934. {
  2935. pte_t entry = __pte(pte);
  2936. printf("ptep @ 0x%016lx = 0x%016lx\n", (unsigned long)ptep, pte);
  2937. printf("Maps physical address = 0x%016lx\n", pte & PTE_RPN_MASK);
  2938. printf("Flags = %s%s%s%s%s\n",
  2939. pte_young(entry) ? "Accessed " : "",
  2940. pte_dirty(entry) ? "Dirty " : "",
  2941. pte_read(entry) ? "Read " : "",
  2942. pte_write(entry) ? "Write " : "",
  2943. pte_exec(entry) ? "Exec " : "");
  2944. }
  2945. static void show_pte(unsigned long addr)
  2946. {
  2947. unsigned long tskv = 0;
  2948. struct task_struct *volatile tsk = NULL;
  2949. struct mm_struct *mm;
  2950. pgd_t *pgdp;
  2951. p4d_t *p4dp;
  2952. pud_t *pudp;
  2953. pmd_t *pmdp;
  2954. pte_t *ptep;
  2955. if (!scanhex(&tskv))
  2956. mm = &init_mm;
  2957. else
  2958. tsk = (struct task_struct *)tskv;
  2959. if (tsk == NULL)
  2960. mm = &init_mm;
  2961. else
  2962. mm = tsk->active_mm;
  2963. if (setjmp(bus_error_jmp) != 0) {
  2964. catch_memory_errors = 0;
  2965. printf("*** Error dumping pte for task %px\n", tsk);
  2966. return;
  2967. }
  2968. catch_memory_errors = 1;
  2969. sync();
  2970. if (mm == &init_mm)
  2971. pgdp = pgd_offset_k(addr);
  2972. else
  2973. pgdp = pgd_offset(mm, addr);
  2974. p4dp = p4d_offset(pgdp, addr);
  2975. if (p4d_none(*p4dp)) {
  2976. printf("No valid P4D\n");
  2977. return;
  2978. }
  2979. if (p4d_is_leaf(*p4dp)) {
  2980. format_pte(p4dp, p4d_val(*p4dp));
  2981. return;
  2982. }
  2983. printf("p4dp @ 0x%px = 0x%016lx\n", p4dp, p4d_val(*p4dp));
  2984. pudp = pud_offset(p4dp, addr);
  2985. if (pud_none(*pudp)) {
  2986. printf("No valid PUD\n");
  2987. return;
  2988. }
  2989. if (pud_is_leaf(*pudp)) {
  2990. format_pte(pudp, pud_val(*pudp));
  2991. return;
  2992. }
  2993. printf("pudp @ 0x%px = 0x%016lx\n", pudp, pud_val(*pudp));
  2994. pmdp = pmd_offset(pudp, addr);
  2995. if (pmd_none(*pmdp)) {
  2996. printf("No valid PMD\n");
  2997. return;
  2998. }
  2999. if (pmd_is_leaf(*pmdp)) {
  3000. format_pte(pmdp, pmd_val(*pmdp));
  3001. return;
  3002. }
  3003. printf("pmdp @ 0x%px = 0x%016lx\n", pmdp, pmd_val(*pmdp));
  3004. ptep = pte_offset_map(pmdp, addr);
  3005. if (pte_none(*ptep)) {
  3006. printf("no valid PTE\n");
  3007. return;
  3008. }
  3009. format_pte(ptep, pte_val(*ptep));
  3010. sync();
  3011. __delay(200);
  3012. catch_memory_errors = 0;
  3013. }
  3014. #else
  3015. static void show_pte(unsigned long addr)
  3016. {
  3017. printf("show_pte not yet implemented\n");
  3018. }
  3019. #endif /* CONFIG_PPC_BOOK3S_64 */
  3020. static void show_tasks(void)
  3021. {
  3022. unsigned long tskv;
  3023. struct task_struct *volatile tsk = NULL;
  3024. printf(" task_struct ->thread.ksp ->thread.regs PID PPID S P CMD\n");
  3025. if (scanhex(&tskv))
  3026. tsk = (struct task_struct *)tskv;
  3027. if (setjmp(bus_error_jmp) != 0) {
  3028. catch_memory_errors = 0;
  3029. printf("*** Error dumping task %px\n", tsk);
  3030. return;
  3031. }
  3032. catch_memory_errors = 1;
  3033. sync();
  3034. if (tsk)
  3035. show_task(tsk);
  3036. else
  3037. for_each_process(tsk)
  3038. show_task(tsk);
  3039. sync();
  3040. __delay(200);
  3041. catch_memory_errors = 0;
  3042. }
  3043. static void proccall(void)
  3044. {
  3045. unsigned long args[8];
  3046. unsigned long ret;
  3047. int i;
  3048. typedef unsigned long (*callfunc_t)(unsigned long, unsigned long,
  3049. unsigned long, unsigned long, unsigned long,
  3050. unsigned long, unsigned long, unsigned long);
  3051. callfunc_t func;
  3052. if (!scanhex(&adrs))
  3053. return;
  3054. if (termch != '\n')
  3055. termch = 0;
  3056. for (i = 0; i < 8; ++i)
  3057. args[i] = 0;
  3058. for (i = 0; i < 8; ++i) {
  3059. if (!scanhex(&args[i]) || termch == '\n')
  3060. break;
  3061. termch = 0;
  3062. }
  3063. func = (callfunc_t) adrs;
  3064. ret = 0;
  3065. if (setjmp(bus_error_jmp) == 0) {
  3066. catch_memory_errors = 1;
  3067. sync();
  3068. ret = func(args[0], args[1], args[2], args[3],
  3069. args[4], args[5], args[6], args[7]);
  3070. sync();
  3071. printf("return value is 0x%lx\n", ret);
  3072. } else {
  3073. printf("*** %x exception occurred\n", fault_except);
  3074. }
  3075. catch_memory_errors = 0;
  3076. }
  3077. /* Input scanning routines */
  3078. int
  3079. skipbl(void)
  3080. {
  3081. int c;
  3082. if( termch != 0 ){
  3083. c = termch;
  3084. termch = 0;
  3085. } else
  3086. c = inchar();
  3087. while( c == ' ' || c == '\t' )
  3088. c = inchar();
  3089. return c;
  3090. }
  3091. #define N_PTREGS 44
  3092. static const char *regnames[N_PTREGS] = {
  3093. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  3094. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  3095. "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  3096. "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
  3097. "pc", "msr", "or3", "ctr", "lr", "xer", "ccr",
  3098. #ifdef CONFIG_PPC64
  3099. "softe",
  3100. #else
  3101. "mq",
  3102. #endif
  3103. "trap", "dar", "dsisr", "res"
  3104. };
  3105. int
  3106. scanhex(unsigned long *vp)
  3107. {
  3108. int c, d;
  3109. unsigned long v;
  3110. c = skipbl();
  3111. if (c == '%') {
  3112. /* parse register name */
  3113. char regname[8];
  3114. int i;
  3115. for (i = 0; i < sizeof(regname) - 1; ++i) {
  3116. c = inchar();
  3117. if (!isalnum(c)) {
  3118. termch = c;
  3119. break;
  3120. }
  3121. regname[i] = c;
  3122. }
  3123. regname[i] = 0;
  3124. i = match_string(regnames, N_PTREGS, regname);
  3125. if (i < 0) {
  3126. printf("invalid register name '%%%s'\n", regname);
  3127. return 0;
  3128. }
  3129. if (xmon_regs == NULL) {
  3130. printf("regs not available\n");
  3131. return 0;
  3132. }
  3133. *vp = ((unsigned long *)xmon_regs)[i];
  3134. return 1;
  3135. }
  3136. /* skip leading "0x" if any */
  3137. if (c == '0') {
  3138. c = inchar();
  3139. if (c == 'x') {
  3140. c = inchar();
  3141. } else {
  3142. d = hexdigit(c);
  3143. if (d == EOF) {
  3144. termch = c;
  3145. *vp = 0;
  3146. return 1;
  3147. }
  3148. }
  3149. } else if (c == '$') {
  3150. int i;
  3151. for (i=0; i<63; i++) {
  3152. c = inchar();
  3153. if (isspace(c) || c == '\0') {
  3154. termch = c;
  3155. break;
  3156. }
  3157. tmpstr[i] = c;
  3158. }
  3159. tmpstr[i++] = 0;
  3160. *vp = 0;
  3161. if (setjmp(bus_error_jmp) == 0) {
  3162. catch_memory_errors = 1;
  3163. sync();
  3164. *vp = kallsyms_lookup_name(tmpstr);
  3165. sync();
  3166. }
  3167. catch_memory_errors = 0;
  3168. if (!(*vp)) {
  3169. printf("unknown symbol '%s'\n", tmpstr);
  3170. return 0;
  3171. }
  3172. return 1;
  3173. }
  3174. d = hexdigit(c);
  3175. if (d == EOF) {
  3176. termch = c;
  3177. return 0;
  3178. }
  3179. v = 0;
  3180. do {
  3181. v = (v << 4) + d;
  3182. c = inchar();
  3183. d = hexdigit(c);
  3184. } while (d != EOF);
  3185. termch = c;
  3186. *vp = v;
  3187. return 1;
  3188. }
  3189. static void
  3190. scannl(void)
  3191. {
  3192. int c;
  3193. c = termch;
  3194. termch = 0;
  3195. while( c != '\n' )
  3196. c = inchar();
  3197. }
  3198. static int hexdigit(int c)
  3199. {
  3200. if( '0' <= c && c <= '9' )
  3201. return c - '0';
  3202. if( 'A' <= c && c <= 'F' )
  3203. return c - ('A' - 10);
  3204. if( 'a' <= c && c <= 'f' )
  3205. return c - ('a' - 10);
  3206. return EOF;
  3207. }
  3208. void
  3209. getstring(char *s, int size)
  3210. {
  3211. int c;
  3212. c = skipbl();
  3213. if (c == '\n') {
  3214. *s = 0;
  3215. return;
  3216. }
  3217. do {
  3218. if( size > 1 ){
  3219. *s++ = c;
  3220. --size;
  3221. }
  3222. c = inchar();
  3223. } while( c != ' ' && c != '\t' && c != '\n' );
  3224. termch = c;
  3225. *s = 0;
  3226. }
  3227. static char line[256];
  3228. static char *lineptr;
  3229. static void
  3230. flush_input(void)
  3231. {
  3232. lineptr = NULL;
  3233. }
  3234. static int
  3235. inchar(void)
  3236. {
  3237. if (lineptr == NULL || *lineptr == 0) {
  3238. if (xmon_gets(line, sizeof(line)) == NULL) {
  3239. lineptr = NULL;
  3240. return EOF;
  3241. }
  3242. lineptr = line;
  3243. }
  3244. return *lineptr++;
  3245. }
  3246. static void
  3247. take_input(char *str)
  3248. {
  3249. lineptr = str;
  3250. }
  3251. static void
  3252. symbol_lookup(void)
  3253. {
  3254. int type = inchar();
  3255. unsigned long addr, cpu;
  3256. void __percpu *ptr = NULL;
  3257. static char tmp[64];
  3258. switch (type) {
  3259. case 'a':
  3260. if (scanhex(&addr))
  3261. xmon_print_symbol(addr, ": ", "\n");
  3262. termch = 0;
  3263. break;
  3264. case 's':
  3265. getstring(tmp, 64);
  3266. if (setjmp(bus_error_jmp) == 0) {
  3267. catch_memory_errors = 1;
  3268. sync();
  3269. addr = kallsyms_lookup_name(tmp);
  3270. if (addr)
  3271. printf("%s: %lx\n", tmp, addr);
  3272. else
  3273. printf("Symbol '%s' not found.\n", tmp);
  3274. sync();
  3275. }
  3276. catch_memory_errors = 0;
  3277. termch = 0;
  3278. break;
  3279. case 'p':
  3280. getstring(tmp, 64);
  3281. if (setjmp(bus_error_jmp) == 0) {
  3282. catch_memory_errors = 1;
  3283. sync();
  3284. ptr = (void __percpu *)kallsyms_lookup_name(tmp);
  3285. sync();
  3286. }
  3287. if (ptr &&
  3288. ptr >= (void __percpu *)__per_cpu_start &&
  3289. ptr < (void __percpu *)__per_cpu_end)
  3290. {
  3291. if (scanhex(&cpu) && cpu < num_possible_cpus()) {
  3292. addr = (unsigned long)per_cpu_ptr(ptr, cpu);
  3293. } else {
  3294. cpu = raw_smp_processor_id();
  3295. addr = (unsigned long)this_cpu_ptr(ptr);
  3296. }
  3297. printf("%s for cpu 0x%lx: %lx\n", tmp, cpu, addr);
  3298. } else {
  3299. printf("Percpu symbol '%s' not found.\n", tmp);
  3300. }
  3301. catch_memory_errors = 0;
  3302. termch = 0;
  3303. break;
  3304. }
  3305. }
  3306. /* Print an address in numeric and symbolic form (if possible) */
  3307. static void xmon_print_symbol(unsigned long address, const char *mid,
  3308. const char *after)
  3309. {
  3310. char *modname;
  3311. const char *volatile name = NULL;
  3312. unsigned long offset, size;
  3313. printf(REG, address);
  3314. if (setjmp(bus_error_jmp) == 0) {
  3315. catch_memory_errors = 1;
  3316. sync();
  3317. name = kallsyms_lookup(address, &size, &offset, &modname,
  3318. tmpstr);
  3319. sync();
  3320. /* wait a little while to see if we get a machine check */
  3321. __delay(200);
  3322. }
  3323. catch_memory_errors = 0;
  3324. if (name) {
  3325. printf("%s%s+%#lx/%#lx", mid, name, offset, size);
  3326. if (modname)
  3327. printf(" [%s]", modname);
  3328. }
  3329. printf("%s", after);
  3330. }
  3331. #ifdef CONFIG_PPC_64S_HASH_MMU
  3332. void dump_segments(void)
  3333. {
  3334. int i;
  3335. unsigned long esid,vsid;
  3336. unsigned long llp;
  3337. printf("SLB contents of cpu 0x%x\n", smp_processor_id());
  3338. for (i = 0; i < mmu_slb_size; i++) {
  3339. asm volatile("slbmfee %0,%1" : "=r" (esid) : "r" (i));
  3340. asm volatile("slbmfev %0,%1" : "=r" (vsid) : "r" (i));
  3341. if (!esid && !vsid)
  3342. continue;
  3343. printf("%02d %016lx %016lx", i, esid, vsid);
  3344. if (!(esid & SLB_ESID_V)) {
  3345. printf("\n");
  3346. continue;
  3347. }
  3348. llp = vsid & SLB_VSID_LLP;
  3349. if (vsid & SLB_VSID_B_1T) {
  3350. printf(" 1T ESID=%9lx VSID=%13lx LLP:%3lx \n",
  3351. GET_ESID_1T(esid),
  3352. (vsid & ~SLB_VSID_B) >> SLB_VSID_SHIFT_1T,
  3353. llp);
  3354. } else {
  3355. printf(" 256M ESID=%9lx VSID=%13lx LLP:%3lx \n",
  3356. GET_ESID(esid),
  3357. (vsid & ~SLB_VSID_B) >> SLB_VSID_SHIFT,
  3358. llp);
  3359. }
  3360. }
  3361. }
  3362. #endif
  3363. #ifdef CONFIG_PPC_BOOK3S_32
  3364. void dump_segments(void)
  3365. {
  3366. int i;
  3367. printf("sr0-15 =");
  3368. for (i = 0; i < 16; ++i)
  3369. printf(" %x", mfsr(i << 28));
  3370. printf("\n");
  3371. }
  3372. #endif
  3373. #ifdef CONFIG_44x
  3374. static void dump_tlb_44x(void)
  3375. {
  3376. int i;
  3377. for (i = 0; i < PPC44x_TLB_SIZE; i++) {
  3378. unsigned long w0,w1,w2;
  3379. asm volatile("tlbre %0,%1,0" : "=r" (w0) : "r" (i));
  3380. asm volatile("tlbre %0,%1,1" : "=r" (w1) : "r" (i));
  3381. asm volatile("tlbre %0,%1,2" : "=r" (w2) : "r" (i));
  3382. printf("[%02x] %08lx %08lx %08lx ", i, w0, w1, w2);
  3383. if (w0 & PPC44x_TLB_VALID) {
  3384. printf("V %08lx -> %01lx%08lx %c%c%c%c%c",
  3385. w0 & PPC44x_TLB_EPN_MASK,
  3386. w1 & PPC44x_TLB_ERPN_MASK,
  3387. w1 & PPC44x_TLB_RPN_MASK,
  3388. (w2 & PPC44x_TLB_W) ? 'W' : 'w',
  3389. (w2 & PPC44x_TLB_I) ? 'I' : 'i',
  3390. (w2 & PPC44x_TLB_M) ? 'M' : 'm',
  3391. (w2 & PPC44x_TLB_G) ? 'G' : 'g',
  3392. (w2 & PPC44x_TLB_E) ? 'E' : 'e');
  3393. }
  3394. printf("\n");
  3395. }
  3396. }
  3397. #endif /* CONFIG_44x */
  3398. #ifdef CONFIG_PPC_BOOK3E_64
  3399. static void dump_tlb_book3e(void)
  3400. {
  3401. u32 mmucfg, pidmask, lpidmask;
  3402. u64 ramask;
  3403. int i, tlb, ntlbs, pidsz, lpidsz, rasz, lrat = 0;
  3404. int mmu_version;
  3405. static const char *pgsz_names[] = {
  3406. " 1K",
  3407. " 2K",
  3408. " 4K",
  3409. " 8K",
  3410. " 16K",
  3411. " 32K",
  3412. " 64K",
  3413. "128K",
  3414. "256K",
  3415. "512K",
  3416. " 1M",
  3417. " 2M",
  3418. " 4M",
  3419. " 8M",
  3420. " 16M",
  3421. " 32M",
  3422. " 64M",
  3423. "128M",
  3424. "256M",
  3425. "512M",
  3426. " 1G",
  3427. " 2G",
  3428. " 4G",
  3429. " 8G",
  3430. " 16G",
  3431. " 32G",
  3432. " 64G",
  3433. "128G",
  3434. "256G",
  3435. "512G",
  3436. " 1T",
  3437. " 2T",
  3438. };
  3439. /* Gather some infos about the MMU */
  3440. mmucfg = mfspr(SPRN_MMUCFG);
  3441. mmu_version = (mmucfg & 3) + 1;
  3442. ntlbs = ((mmucfg >> 2) & 3) + 1;
  3443. pidsz = ((mmucfg >> 6) & 0x1f) + 1;
  3444. lpidsz = (mmucfg >> 24) & 0xf;
  3445. rasz = (mmucfg >> 16) & 0x7f;
  3446. if ((mmu_version > 1) && (mmucfg & 0x10000))
  3447. lrat = 1;
  3448. printf("Book3E MMU MAV=%d.0,%d TLBs,%d-bit PID,%d-bit LPID,%d-bit RA\n",
  3449. mmu_version, ntlbs, pidsz, lpidsz, rasz);
  3450. pidmask = (1ul << pidsz) - 1;
  3451. lpidmask = (1ul << lpidsz) - 1;
  3452. ramask = (1ull << rasz) - 1;
  3453. for (tlb = 0; tlb < ntlbs; tlb++) {
  3454. u32 tlbcfg;
  3455. int nent, assoc, new_cc = 1;
  3456. printf("TLB %d:\n------\n", tlb);
  3457. switch(tlb) {
  3458. case 0:
  3459. tlbcfg = mfspr(SPRN_TLB0CFG);
  3460. break;
  3461. case 1:
  3462. tlbcfg = mfspr(SPRN_TLB1CFG);
  3463. break;
  3464. case 2:
  3465. tlbcfg = mfspr(SPRN_TLB2CFG);
  3466. break;
  3467. case 3:
  3468. tlbcfg = mfspr(SPRN_TLB3CFG);
  3469. break;
  3470. default:
  3471. printf("Unsupported TLB number !\n");
  3472. continue;
  3473. }
  3474. nent = tlbcfg & 0xfff;
  3475. assoc = (tlbcfg >> 24) & 0xff;
  3476. for (i = 0; i < nent; i++) {
  3477. u32 mas0 = MAS0_TLBSEL(tlb);
  3478. u32 mas1 = MAS1_TSIZE(BOOK3E_PAGESZ_4K);
  3479. u64 mas2 = 0;
  3480. u64 mas7_mas3;
  3481. int esel = i, cc = i;
  3482. if (assoc != 0) {
  3483. cc = i / assoc;
  3484. esel = i % assoc;
  3485. mas2 = cc * 0x1000;
  3486. }
  3487. mas0 |= MAS0_ESEL(esel);
  3488. mtspr(SPRN_MAS0, mas0);
  3489. mtspr(SPRN_MAS1, mas1);
  3490. mtspr(SPRN_MAS2, mas2);
  3491. asm volatile("tlbre 0,0,0" : : : "memory");
  3492. mas1 = mfspr(SPRN_MAS1);
  3493. mas2 = mfspr(SPRN_MAS2);
  3494. mas7_mas3 = mfspr(SPRN_MAS7_MAS3);
  3495. if (assoc && (i % assoc) == 0)
  3496. new_cc = 1;
  3497. if (!(mas1 & MAS1_VALID))
  3498. continue;
  3499. if (assoc == 0)
  3500. printf("%04x- ", i);
  3501. else if (new_cc)
  3502. printf("%04x-%c", cc, 'A' + esel);
  3503. else
  3504. printf(" |%c", 'A' + esel);
  3505. new_cc = 0;
  3506. printf(" %016llx %04x %s %c%c AS%c",
  3507. mas2 & ~0x3ffull,
  3508. (mas1 >> 16) & 0x3fff,
  3509. pgsz_names[(mas1 >> 7) & 0x1f],
  3510. mas1 & MAS1_IND ? 'I' : ' ',
  3511. mas1 & MAS1_IPROT ? 'P' : ' ',
  3512. mas1 & MAS1_TS ? '1' : '0');
  3513. printf(" %c%c%c%c%c%c%c",
  3514. mas2 & MAS2_X0 ? 'a' : ' ',
  3515. mas2 & MAS2_X1 ? 'v' : ' ',
  3516. mas2 & MAS2_W ? 'w' : ' ',
  3517. mas2 & MAS2_I ? 'i' : ' ',
  3518. mas2 & MAS2_M ? 'm' : ' ',
  3519. mas2 & MAS2_G ? 'g' : ' ',
  3520. mas2 & MAS2_E ? 'e' : ' ');
  3521. printf(" %016llx", mas7_mas3 & ramask & ~0x7ffull);
  3522. if (mas1 & MAS1_IND)
  3523. printf(" %s\n",
  3524. pgsz_names[(mas7_mas3 >> 1) & 0x1f]);
  3525. else
  3526. printf(" U%c%c%c S%c%c%c\n",
  3527. mas7_mas3 & MAS3_UX ? 'x' : ' ',
  3528. mas7_mas3 & MAS3_UW ? 'w' : ' ',
  3529. mas7_mas3 & MAS3_UR ? 'r' : ' ',
  3530. mas7_mas3 & MAS3_SX ? 'x' : ' ',
  3531. mas7_mas3 & MAS3_SW ? 'w' : ' ',
  3532. mas7_mas3 & MAS3_SR ? 'r' : ' ');
  3533. }
  3534. }
  3535. }
  3536. #endif /* CONFIG_PPC_BOOK3E_64 */
  3537. static void xmon_init(int enable)
  3538. {
  3539. if (enable) {
  3540. __debugger = xmon;
  3541. __debugger_ipi = xmon_ipi;
  3542. __debugger_bpt = xmon_bpt;
  3543. __debugger_sstep = xmon_sstep;
  3544. __debugger_iabr_match = xmon_iabr_match;
  3545. __debugger_break_match = xmon_break_match;
  3546. __debugger_fault_handler = xmon_fault_handler;
  3547. #ifdef CONFIG_PPC_PSERIES
  3548. /*
  3549. * Get the token here to avoid trying to get a lock
  3550. * during the crash, causing a deadlock.
  3551. */
  3552. set_indicator_token = rtas_token("set-indicator");
  3553. #endif
  3554. } else {
  3555. __debugger = NULL;
  3556. __debugger_ipi = NULL;
  3557. __debugger_bpt = NULL;
  3558. __debugger_sstep = NULL;
  3559. __debugger_iabr_match = NULL;
  3560. __debugger_break_match = NULL;
  3561. __debugger_fault_handler = NULL;
  3562. }
  3563. }
  3564. #ifdef CONFIG_MAGIC_SYSRQ
  3565. static void sysrq_handle_xmon(int key)
  3566. {
  3567. if (xmon_is_locked_down()) {
  3568. clear_all_bpt();
  3569. xmon_init(0);
  3570. return;
  3571. }
  3572. /* ensure xmon is enabled */
  3573. xmon_init(1);
  3574. debugger(get_irq_regs());
  3575. if (!xmon_on)
  3576. xmon_init(0);
  3577. }
  3578. static const struct sysrq_key_op sysrq_xmon_op = {
  3579. .handler = sysrq_handle_xmon,
  3580. .help_msg = "xmon(x)",
  3581. .action_msg = "Entering xmon",
  3582. };
  3583. static int __init setup_xmon_sysrq(void)
  3584. {
  3585. register_sysrq_key('x', &sysrq_xmon_op);
  3586. return 0;
  3587. }
  3588. device_initcall(setup_xmon_sysrq);
  3589. #endif /* CONFIG_MAGIC_SYSRQ */
  3590. static void clear_all_bpt(void)
  3591. {
  3592. int i;
  3593. /* clear/unpatch all breakpoints */
  3594. remove_bpts();
  3595. remove_cpu_bpts();
  3596. /* Disable all breakpoints */
  3597. for (i = 0; i < NBPTS; ++i)
  3598. bpts[i].enabled = 0;
  3599. /* Clear any data or iabr breakpoints */
  3600. iabr = NULL;
  3601. for (i = 0; i < nr_wp_slots(); i++)
  3602. dabr[i].enabled = 0;
  3603. }
  3604. #ifdef CONFIG_DEBUG_FS
  3605. static int xmon_dbgfs_set(void *data, u64 val)
  3606. {
  3607. xmon_on = !!val;
  3608. xmon_init(xmon_on);
  3609. /* make sure all breakpoints removed when disabling */
  3610. if (!xmon_on) {
  3611. clear_all_bpt();
  3612. get_output_lock();
  3613. printf("xmon: All breakpoints cleared\n");
  3614. release_output_lock();
  3615. }
  3616. return 0;
  3617. }
  3618. static int xmon_dbgfs_get(void *data, u64 *val)
  3619. {
  3620. *val = xmon_on;
  3621. return 0;
  3622. }
  3623. DEFINE_SIMPLE_ATTRIBUTE(xmon_dbgfs_ops, xmon_dbgfs_get,
  3624. xmon_dbgfs_set, "%llu\n");
  3625. static int __init setup_xmon_dbgfs(void)
  3626. {
  3627. debugfs_create_file("xmon", 0600, arch_debugfs_dir, NULL,
  3628. &xmon_dbgfs_ops);
  3629. return 0;
  3630. }
  3631. device_initcall(setup_xmon_dbgfs);
  3632. #endif /* CONFIG_DEBUG_FS */
  3633. static int xmon_early __initdata;
  3634. static int __init early_parse_xmon(char *p)
  3635. {
  3636. if (xmon_is_locked_down()) {
  3637. xmon_init(0);
  3638. xmon_early = 0;
  3639. xmon_on = 0;
  3640. } else if (!p || strncmp(p, "early", 5) == 0) {
  3641. /* just "xmon" is equivalent to "xmon=early" */
  3642. xmon_init(1);
  3643. xmon_early = 1;
  3644. xmon_on = 1;
  3645. } else if (strncmp(p, "on", 2) == 0) {
  3646. xmon_init(1);
  3647. xmon_on = 1;
  3648. } else if (strncmp(p, "rw", 2) == 0) {
  3649. xmon_init(1);
  3650. xmon_on = 1;
  3651. xmon_is_ro = false;
  3652. } else if (strncmp(p, "ro", 2) == 0) {
  3653. xmon_init(1);
  3654. xmon_on = 1;
  3655. xmon_is_ro = true;
  3656. } else if (strncmp(p, "off", 3) == 0)
  3657. xmon_on = 0;
  3658. else
  3659. return 1;
  3660. return 0;
  3661. }
  3662. early_param("xmon", early_parse_xmon);
  3663. void __init xmon_setup(void)
  3664. {
  3665. if (xmon_on)
  3666. xmon_init(1);
  3667. if (xmon_early)
  3668. debugger(NULL);
  3669. }
  3670. #ifdef CONFIG_SPU_BASE
  3671. struct spu_info {
  3672. struct spu *spu;
  3673. u64 saved_mfc_sr1_RW;
  3674. u32 saved_spu_runcntl_RW;
  3675. unsigned long dump_addr;
  3676. u8 stopped_ok;
  3677. };
  3678. #define XMON_NUM_SPUS 16 /* Enough for current hardware */
  3679. static struct spu_info spu_info[XMON_NUM_SPUS];
  3680. void __init xmon_register_spus(struct list_head *list)
  3681. {
  3682. struct spu *spu;
  3683. list_for_each_entry(spu, list, full_list) {
  3684. if (spu->number >= XMON_NUM_SPUS) {
  3685. WARN_ON(1);
  3686. continue;
  3687. }
  3688. spu_info[spu->number].spu = spu;
  3689. spu_info[spu->number].stopped_ok = 0;
  3690. spu_info[spu->number].dump_addr = (unsigned long)
  3691. spu_info[spu->number].spu->local_store;
  3692. }
  3693. }
  3694. static void stop_spus(void)
  3695. {
  3696. struct spu *spu;
  3697. volatile int i;
  3698. u64 tmp;
  3699. for (i = 0; i < XMON_NUM_SPUS; i++) {
  3700. if (!spu_info[i].spu)
  3701. continue;
  3702. if (setjmp(bus_error_jmp) == 0) {
  3703. catch_memory_errors = 1;
  3704. sync();
  3705. spu = spu_info[i].spu;
  3706. spu_info[i].saved_spu_runcntl_RW =
  3707. in_be32(&spu->problem->spu_runcntl_RW);
  3708. tmp = spu_mfc_sr1_get(spu);
  3709. spu_info[i].saved_mfc_sr1_RW = tmp;
  3710. tmp &= ~MFC_STATE1_MASTER_RUN_CONTROL_MASK;
  3711. spu_mfc_sr1_set(spu, tmp);
  3712. sync();
  3713. __delay(200);
  3714. spu_info[i].stopped_ok = 1;
  3715. printf("Stopped spu %.2d (was %s)\n", i,
  3716. spu_info[i].saved_spu_runcntl_RW ?
  3717. "running" : "stopped");
  3718. } else {
  3719. catch_memory_errors = 0;
  3720. printf("*** Error stopping spu %.2d\n", i);
  3721. }
  3722. catch_memory_errors = 0;
  3723. }
  3724. }
  3725. static void restart_spus(void)
  3726. {
  3727. struct spu *spu;
  3728. volatile int i;
  3729. for (i = 0; i < XMON_NUM_SPUS; i++) {
  3730. if (!spu_info[i].spu)
  3731. continue;
  3732. if (!spu_info[i].stopped_ok) {
  3733. printf("*** Error, spu %d was not successfully stopped"
  3734. ", not restarting\n", i);
  3735. continue;
  3736. }
  3737. if (setjmp(bus_error_jmp) == 0) {
  3738. catch_memory_errors = 1;
  3739. sync();
  3740. spu = spu_info[i].spu;
  3741. spu_mfc_sr1_set(spu, spu_info[i].saved_mfc_sr1_RW);
  3742. out_be32(&spu->problem->spu_runcntl_RW,
  3743. spu_info[i].saved_spu_runcntl_RW);
  3744. sync();
  3745. __delay(200);
  3746. printf("Restarted spu %.2d\n", i);
  3747. } else {
  3748. catch_memory_errors = 0;
  3749. printf("*** Error restarting spu %.2d\n", i);
  3750. }
  3751. catch_memory_errors = 0;
  3752. }
  3753. }
  3754. #define DUMP_WIDTH 23
  3755. #define DUMP_VALUE(format, field, value) \
  3756. do { \
  3757. if (setjmp(bus_error_jmp) == 0) { \
  3758. catch_memory_errors = 1; \
  3759. sync(); \
  3760. printf(" %-*s = "format"\n", DUMP_WIDTH, \
  3761. #field, value); \
  3762. sync(); \
  3763. __delay(200); \
  3764. } else { \
  3765. catch_memory_errors = 0; \
  3766. printf(" %-*s = *** Error reading field.\n", \
  3767. DUMP_WIDTH, #field); \
  3768. } \
  3769. catch_memory_errors = 0; \
  3770. } while (0)
  3771. #define DUMP_FIELD(obj, format, field) \
  3772. DUMP_VALUE(format, field, obj->field)
  3773. static void dump_spu_fields(struct spu *spu)
  3774. {
  3775. printf("Dumping spu fields at address %p:\n", spu);
  3776. DUMP_FIELD(spu, "0x%x", number);
  3777. DUMP_FIELD(spu, "%s", name);
  3778. DUMP_FIELD(spu, "0x%lx", local_store_phys);
  3779. DUMP_FIELD(spu, "0x%p", local_store);
  3780. DUMP_FIELD(spu, "0x%lx", ls_size);
  3781. DUMP_FIELD(spu, "0x%x", node);
  3782. DUMP_FIELD(spu, "0x%lx", flags);
  3783. DUMP_FIELD(spu, "%llu", class_0_pending);
  3784. DUMP_FIELD(spu, "0x%llx", class_0_dar);
  3785. DUMP_FIELD(spu, "0x%llx", class_1_dar);
  3786. DUMP_FIELD(spu, "0x%llx", class_1_dsisr);
  3787. DUMP_FIELD(spu, "0x%x", irqs[0]);
  3788. DUMP_FIELD(spu, "0x%x", irqs[1]);
  3789. DUMP_FIELD(spu, "0x%x", irqs[2]);
  3790. DUMP_FIELD(spu, "0x%x", slb_replace);
  3791. DUMP_FIELD(spu, "%d", pid);
  3792. DUMP_FIELD(spu, "0x%p", mm);
  3793. DUMP_FIELD(spu, "0x%p", ctx);
  3794. DUMP_FIELD(spu, "0x%p", rq);
  3795. DUMP_FIELD(spu, "0x%llx", timestamp);
  3796. DUMP_FIELD(spu, "0x%lx", problem_phys);
  3797. DUMP_FIELD(spu, "0x%p", problem);
  3798. DUMP_VALUE("0x%x", problem->spu_runcntl_RW,
  3799. in_be32(&spu->problem->spu_runcntl_RW));
  3800. DUMP_VALUE("0x%x", problem->spu_status_R,
  3801. in_be32(&spu->problem->spu_status_R));
  3802. DUMP_VALUE("0x%x", problem->spu_npc_RW,
  3803. in_be32(&spu->problem->spu_npc_RW));
  3804. DUMP_FIELD(spu, "0x%p", priv2);
  3805. DUMP_FIELD(spu, "0x%p", pdata);
  3806. }
  3807. static int spu_inst_dump(unsigned long adr, long count, int praddr)
  3808. {
  3809. return generic_inst_dump(adr, count, praddr, print_insn_spu);
  3810. }
  3811. static void dump_spu_ls(unsigned long num, int subcmd)
  3812. {
  3813. unsigned long offset, addr, ls_addr;
  3814. if (setjmp(bus_error_jmp) == 0) {
  3815. catch_memory_errors = 1;
  3816. sync();
  3817. ls_addr = (unsigned long)spu_info[num].spu->local_store;
  3818. sync();
  3819. __delay(200);
  3820. } else {
  3821. catch_memory_errors = 0;
  3822. printf("*** Error: accessing spu info for spu %ld\n", num);
  3823. return;
  3824. }
  3825. catch_memory_errors = 0;
  3826. if (scanhex(&offset))
  3827. addr = ls_addr + offset;
  3828. else
  3829. addr = spu_info[num].dump_addr;
  3830. if (addr >= ls_addr + LS_SIZE) {
  3831. printf("*** Error: address outside of local store\n");
  3832. return;
  3833. }
  3834. switch (subcmd) {
  3835. case 'i':
  3836. addr += spu_inst_dump(addr, 16, 1);
  3837. last_cmd = "sdi\n";
  3838. break;
  3839. default:
  3840. prdump(addr, 64);
  3841. addr += 64;
  3842. last_cmd = "sd\n";
  3843. break;
  3844. }
  3845. spu_info[num].dump_addr = addr;
  3846. }
  3847. static int do_spu_cmd(void)
  3848. {
  3849. static unsigned long num = 0;
  3850. int cmd, subcmd = 0;
  3851. cmd = inchar();
  3852. switch (cmd) {
  3853. case 's':
  3854. stop_spus();
  3855. break;
  3856. case 'r':
  3857. restart_spus();
  3858. break;
  3859. case 'd':
  3860. subcmd = inchar();
  3861. if (isxdigit(subcmd) || subcmd == '\n')
  3862. termch = subcmd;
  3863. fallthrough;
  3864. case 'f':
  3865. scanhex(&num);
  3866. if (num >= XMON_NUM_SPUS || !spu_info[num].spu) {
  3867. printf("*** Error: invalid spu number\n");
  3868. return 0;
  3869. }
  3870. switch (cmd) {
  3871. case 'f':
  3872. dump_spu_fields(spu_info[num].spu);
  3873. break;
  3874. default:
  3875. dump_spu_ls(num, subcmd);
  3876. break;
  3877. }
  3878. break;
  3879. default:
  3880. return -1;
  3881. }
  3882. return 0;
  3883. }
  3884. #else /* ! CONFIG_SPU_BASE */
  3885. static int do_spu_cmd(void)
  3886. {
  3887. return -1;
  3888. }
  3889. #endif