arch.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * EPISODE 12B
  11. * How to recognise different types of trees from quite a long way away.
  12. * NO. 1
  13. * THE LARCH
  14. */
  15. #include "u.h"
  16. #include "../port/lib.h"
  17. #include "mem.h"
  18. #include "dat.h"
  19. #include "fns.h"
  20. #include "../port/error.h"
  21. #include "ureg.h"
  22. /* the rules are different for different compilers. We need to define up. */
  23. // Initialize it to force it into data.
  24. // That way, if we set them in assembly, they won't get zero'd by the bss init in main
  25. // N.B. There was an interesting hack in plan 9 c. You could grab up to two registers for your
  26. // program. In the case of Plan 9, m was r15, and up was r14. Very slick, and if there is a way to do
  27. // this in gcc or clang I don't know it. This also nicely handled per cpu info; R15/14 were always right for
  28. // your core and context.
  29. //Mach *m = (void *)0;
  30. int
  31. incref(Ref *r)
  32. {
  33. int x;
  34. lock(&r->l);
  35. x = ++r->ref;
  36. unlock(&r->l);
  37. return x;
  38. }
  39. int
  40. decref(Ref *r)
  41. {
  42. int x;
  43. lock(&r->l);
  44. x = --r->ref;
  45. unlock(&r->l);
  46. if(x < 0)
  47. panic("decref pc=%#p", getcallerpc(&r));
  48. return x;
  49. }
  50. void
  51. procrestore(Proc *p)
  52. {
  53. uint64_t t;
  54. if(p->kp)
  55. return;
  56. cycles(&t);
  57. p->pcycles -= t;
  58. fpuprocrestore(p);
  59. }
  60. /*
  61. * Save the mach dependent part of the process state.
  62. * NB: the caller should mmuflushtlb after procsave().
  63. * procsave/procrestore don't touch the mmu, they
  64. * care about fpu, mostly.
  65. */
  66. void
  67. procsave(Proc *p)
  68. {
  69. uint64_t t;
  70. cycles(&t);
  71. p->pcycles += t;
  72. fpuprocsave(p);
  73. }
  74. static void
  75. linkproc(void)
  76. {
  77. Proc *up = externup();
  78. spllo();
  79. up->kpfun(up->kparg);
  80. pexit("kproc dying", 0);
  81. }
  82. void
  83. kprocchild(Proc* p, void (*func)(void*), void* arg)
  84. {
  85. /*
  86. * gotolabel() needs a word on the stack in
  87. * which to place the return PC used to jump
  88. * to linkproc().
  89. */
  90. p->sched.pc = PTR2UINT(linkproc);
  91. p->sched.sp = PTR2UINT(p->kstack+KSTACK-BY2SE);
  92. p->sched.sp = STACKALIGN(p->sched.sp);
  93. p->kpfun = func;
  94. p->kparg = arg;
  95. }
  96. /*
  97. * put the processor in the halt state if we've no processes to run.
  98. * an interrupt will get us going again.
  99. * The boot TC in nix can't halt, because it must stay alert in
  100. * case an AC makes a handler process ready.
  101. * We should probably use mwait in that case.
  102. */
  103. void
  104. idlehands(void)
  105. {
  106. if(machp()->NIX.nixtype != NIXAC)
  107. halt();
  108. }
  109. void
  110. ureg2gdb(Ureg *u, uintptr_t *g)
  111. {
  112. g[GDB_AX] = u->ax;
  113. g[GDB_BX] = u->bx;
  114. g[GDB_CX] = u->cx;
  115. g[GDB_DX] = u->dx;
  116. g[GDB_SI] = u->si;
  117. g[GDB_DI] = u->di;
  118. g[GDB_BP] = u->bp;
  119. g[GDB_SP] = u->sp;
  120. g[GDB_R8] = u->r8;
  121. g[GDB_R9] = u->r9;
  122. g[GDB_R10] = u->r10;
  123. g[GDB_R11] = u->r11;
  124. g[GDB_R12] = u->r12;
  125. g[GDB_R13] = u->r13;
  126. g[GDB_R14] = u->r14;
  127. g[GDB_R15] = u->r15;
  128. g[GDB_PC] = u->ip;
  129. /* it's weird, docs say 5 32-bit fields
  130. * but I count 4 if we pack these. Fix me
  131. */
  132. g[GDB_PS] = 0; // u->PS;
  133. g[GDB_CS] = 0; // u->CS;
  134. g[GDB_SS] = 0; // u->SS;
  135. g[GDB_DS] = 0; // u->DS;
  136. g[GDB_ES] = 0; // u->ES;
  137. g[GDB_FS] = 0; // u->FS;
  138. g[GDB_GS] = 0; // u->GS;
  139. }
  140. void
  141. gdb2ureg(uintptr_t *g, Ureg *u)
  142. {
  143. u->ax = g[GDB_AX];
  144. u->bx = g[GDB_BX];
  145. u->cx = g[GDB_CX];
  146. u->dx = g[GDB_DX];
  147. u->si = g[GDB_SI];
  148. u->di = g[GDB_DI];
  149. u->bp = g[GDB_BP];
  150. u->sp = g[GDB_SP];
  151. u->r8 = g[GDB_R8];
  152. u->r9 = g[GDB_R9];
  153. u->r10 = g[GDB_R10];
  154. u->r11 = g[GDB_R11];
  155. u->r12 = g[GDB_R12];
  156. u->r13 = g[GDB_R13];
  157. u->r14 = g[GDB_R14];
  158. u->r15 = g[GDB_R15];
  159. u->ip = g[GDB_PC];
  160. /* it's weird but gdb seems to have no way to
  161. * express the sp. Hmm.
  162. */
  163. u->flags = g[GDB_PS];
  164. /* is there any point to this? */
  165. u->cs = g[GDB_CS];
  166. u->ss = g[GDB_SS];
  167. }