1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1993, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, Joyent, Inc. All rights reserved.
*/
#include <sys/types.h>
#include <sys/kstat.h>
#include <sys/param.h>
#include <sys/stack.h>
#include <sys/regset.h>
#include <sys/thread.h>
#include <sys/proc.h>
#include <sys/procfs_isa.h>
#include <sys/kmem.h>
#include <sys/cpuvar.h>
#include <sys/systm.h>
#include <sys/machpcb.h>
#include <sys/machasi.h>
#include <sys/vis.h>
#include <sys/fpu/fpusystm.h>
#include <sys/cpu_module.h>
#include <sys/privregs.h>
#include <sys/archsystm.h>
#include <sys/atomic.h>
#include <sys/cmn_err.h>
#include <sys/time.h>
#include <sys/clock.h>
#include <sys/cmp.h>
#include <sys/platform_module.h>
#include <sys/bl.h>
#include <sys/nvpair.h>
#include <sys/kdi_impl.h>
#include <sys/machsystm.h>
#include <sys/sysmacros.h>
#include <sys/promif.h>
#include <sys/pool_pset.h>
#include <sys/mem.h>
#include <sys/dumphdr.h>
#include <vm/seg_kmem.h>
#include <sys/hold_page.h>
#include <sys/cpu.h>
#include <sys/ivintr.h>
#include <sys/clock_impl.h>
#include <sys/machclock.h>
int maxphys = MMU_PAGESIZE * 16; /* 128k */
int klustsize = MMU_PAGESIZE * 16; /* 128k */
/*
* Initialize kernel thread's stack.
*/
caddr_t
thread_stk_init(caddr_t stk)
{
kfpu_t *fp;
ulong_t align;
/* allocate extra space for floating point state */
stk -= SA(sizeof (kfpu_t) + GSR_SIZE);
align = (uintptr_t)stk & 0x3f;
stk -= align; /* force v9_fpu to be 16 byte aligned */
fp = (kfpu_t *)stk;
fp->fpu_fprs = 0;
stk -= SA(MINFRAME);
return (stk);
}
#define WIN32_SIZE (MAXWIN * sizeof (struct rwindow32))
#define WIN64_SIZE (MAXWIN * sizeof (struct rwindow64))
kmem_cache_t *wbuf32_cache;
kmem_cache_t *wbuf64_cache;
void
lwp_stk_cache_init(void)
{
/*
* Window buffers are allocated from the static arena
* because they are accessed at TL>0. We also must use
* KMC_NOHASH to prevent them from straddling page
* boundaries as they are accessed by physical address.
*/
wbuf32_cache = kmem_cache_create("wbuf32_cache", WIN32_SIZE,
0, NULL, NULL, NULL, NULL, static_arena, KMC_NOHASH);
wbuf64_cache = kmem_cache_create("wbuf64_cache", WIN64_SIZE,
0, NULL, NULL, NULL, NULL, static_arena, KMC_NOHASH);
}
/*
* Initialize lwp's kernel stack.
* Note that now that the floating point register save area (kfpu_t)
* has been broken out from machpcb and aligned on a 64 byte boundary so that
* we can do block load/stores to/from it, there are a couple of potential
* optimizations to save stack space. 1. The floating point register save
* area could be aligned on a 16 byte boundary, and the floating point code
* changed to (a) check the alignment and (b) use different save/restore
* macros depending upon the alignment. 2. The lwp_stk_init code below
* could be changed to calculate if less space would be wasted if machpcb
* was first instead of second. However there is a REGOFF macro used in
* locore, syscall_trap, machdep and mlsetup that assumes that the saved
* register area is a fixed distance from the %sp, and would have to be
* changed to a pointer or something...JJ said later.
*/
caddr_t
lwp_stk_init(klwp_t *lwp, caddr_t stk)
{
struct machpcb *mpcb;
kfpu_t *fp;
uintptr_t aln;
stk -= SA(sizeof (kfpu_t) + GSR_SIZE);
aln = (uintptr_t)stk & 0x3F;
stk -= aln;
fp = (kfpu_t *)stk;
stk -= SA(sizeof (struct machpcb));
mpcb = (struct machpcb *)stk;
bzero(mpcb, sizeof (struct machpcb));
bzero(fp, sizeof (kfpu_t) + GSR_SIZE);
lwp->lwp_regs = (void *)&mpcb->mpcb_regs;
lwp->lwp_fpu = (void *)fp;
mpcb->mpcb_fpu = fp;
mpcb->mpcb_fpu->fpu_q = mpcb->mpcb_fpu_q;
mpcb->mpcb_thread = lwp->lwp_thread;
mpcb->mpcb_wbcnt = 0;
if (lwp->lwp_procp->p_model == DATAMODEL_ILP32) {
mpcb->mpcb_wstate = WSTATE_USER32;
mpcb->mpcb_wbuf = kmem_cache_alloc(wbuf32_cache, KM_SLEEP);
} else {
mpcb->mpcb_wstate = WSTATE_USER64;
mpcb->mpcb_wbuf = kmem_cache_alloc(wbuf64_cache, KM_SLEEP);
}
ASSERT(((uintptr_t)mpcb->mpcb_wbuf & 7) == 0);
mpcb->mpcb_wbuf_pa = va_to_pa(mpcb->mpcb_wbuf);
mpcb->mpcb_pa = va_to_pa(mpcb);
return (stk);
}
void
lwp_stk_fini(klwp_t *lwp)
{
struct machpcb *mpcb = lwptompcb(lwp);
/*
* there might be windows still in the wbuf due to unmapped
* stack, misaligned stack pointer, etc. We just free it.
*/
mpcb->mpcb_wbcnt = 0;
if (mpcb->mpcb_wstate == WSTATE_USER32)
kmem_cache_free(wbuf32_cache, mpcb->mpcb_wbuf);
else
kmem_cache_free(wbuf64_cache, mpcb->mpcb_wbuf);
mpcb->mpcb_wbuf = NULL;
mpcb->mpcb_wbuf_pa = -1;
}
/*ARGSUSED*/
void
lwp_fp_init(klwp_t *lwp)
{
}
/*
* Copy regs from parent to child.
*/
void
lwp_forkregs(klwp_t *lwp, klwp_t *clwp)
{
kthread_t *t, *pt = lwptot(lwp);
struct machpcb *mpcb = lwptompcb(clwp);
struct machpcb *pmpcb = lwptompcb(lwp);
kfpu_t *fp, *pfp = lwptofpu(lwp);
caddr_t wbuf;
uint_t wstate;
t = mpcb->mpcb_thread;
/*
* remember child's fp and wbuf since they will get erased during
* the bcopy.
*/
fp = mpcb->mpcb_fpu;
wbuf = mpcb->mpcb_wbuf;
wstate = mpcb->mpcb_wstate;
/*
* Don't copy mpcb_frame since we hand-crafted it
* in thread_load().
*/
bcopy(lwp->lwp_regs, clwp->lwp_regs, sizeof (struct machpcb) - REGOFF);
mpcb->mpcb_thread = t;
mpcb->mpcb_fpu = fp;
fp->fpu_q = mpcb->mpcb_fpu_q;
/*
* It is theoretically possibly for the lwp's wstate to
* be different from its value assigned in lwp_stk_init,
* since lwp_stk_init assumed the data model of the process.
* Here, we took on the data model of the cloned lwp.
*/
if (mpcb->mpcb_wstate != wstate) {
if (wstate == WSTATE_USER32) {
kmem_cache_free(wbuf32_cache, wbuf);
wbuf = kmem_cache_alloc(wbuf64_cache, KM_SLEEP);
wstate = WSTATE_USER64;
} else {
kmem_cache_free(wbuf64_cache, wbuf);
wbuf = kmem_cache_alloc(wbuf32_cache, KM_SLEEP);
wstate = WSTATE_USER32;
}
}
mpcb->mpcb_pa = va_to_pa(mpcb);
mpcb->mpcb_wbuf = wbuf;
mpcb->mpcb_wbuf_pa = va_to_pa(wbuf);
ASSERT(mpcb->mpcb_wstate == wstate);
if (mpcb->mpcb_wbcnt != 0) {
bcopy(pmpcb->mpcb_wbuf, mpcb->mpcb_wbuf,
mpcb->mpcb_wbcnt * ((mpcb->mpcb_wstate == WSTATE_USER32) ?
sizeof (struct rwindow32) : sizeof (struct rwindow64)));
}
if (pt == curthread)
pfp->fpu_fprs = _fp_read_fprs();
if ((pfp->fpu_en) || (pfp->fpu_fprs & FPRS_FEF)) {
if (pt == curthread && fpu_exists) {
save_gsr(clwp->lwp_fpu);
} else {
uint64_t gsr;
gsr = get_gsr(lwp->lwp_fpu);
set_gsr(gsr, clwp->lwp_fpu);
}
fp_fork(lwp, clwp);
}
}
/*
* Free lwp fpu regs.
*/
void
lwp_freeregs(klwp_t *lwp, int isexec)
{
kfpu_t *fp = lwptofpu(lwp);
if (lwptot(lwp) == curthread)
fp->fpu_fprs = _fp_read_fprs();
if ((fp->fpu_en) || (fp->fpu_fprs & FPRS_FEF))
fp_free(fp, isexec);
}
/*
* These function are currently unused on sparc.
*/
/*ARGSUSED*/
void
lwp_attach_brand_hdlrs(klwp_t *lwp)
{}
/*ARGSUSED*/
void
lwp_detach_brand_hdlrs(klwp_t *lwp)
{}
/*
* fill in the extra register state area specified with the
* specified lwp's platform-dependent non-floating-point extra
* register state information
*/
/* ARGSUSED */
void
xregs_getgfiller(klwp_id_t lwp, caddr_t xrp)
{
/* for sun4u nothing to do here, added for symmetry */
}
/*
* fill in the extra register state area specified with the specified lwp's
* platform-dependent floating-point extra register state information.
* NOTE: 'lwp' might not correspond to 'curthread' since this is
* called from code in /proc to get the registers of another lwp.
*/
void
xregs_getfpfiller(klwp_id_t lwp, caddr_t xrp)
{
prxregset_t *xregs = (prxregset_t *)xrp;
kfpu_t *fp = lwptofpu(lwp);
uint32_t fprs = (FPRS_FEF|FPRS_DU|FPRS_DL);
uint64_t gsr;
/*
* fp_fksave() does not flush the GSR register into
* the lwp area, so do it now
*/
kpreempt_disable();
if (ttolwp(curthread) == lwp && fpu_exists) {
fp->fpu_fprs = _fp_read_fprs();
if ((fp->fpu_fprs & FPRS_FEF) != FPRS_FEF) {
_fp_write_fprs(fprs);
fp->fpu_fprs = (V9_FPU_FPRS_TYPE)fprs;
}
save_gsr(fp);
}
gsr = get_gsr(fp);
kpreempt_enable();
PRXREG_GSR(xregs) = gsr;
}
/*
* set the specified lwp's platform-dependent non-floating-point
* extra register state based on the specified input
*/
/* ARGSUSED */
void
xregs_setgfiller(klwp_id_t lwp, caddr_t xrp)
{
/* for sun4u nothing to do here, added for symmetry */
}
/*
* set the specified lwp's platform-dependent floating-point
* extra register state based on the specified input
*/
void
xregs_setfpfiller(klwp_id_t lwp, caddr_t xrp)
{
prxregset_t *xregs = (prxregset_t *)xrp;
kfpu_t *fp = lwptofpu(lwp);
uint32_t fprs = (FPRS_FEF|FPRS_DU|FPRS_DL);
uint64_t gsr = PRXREG_GSR(xregs);
kpreempt_disable();
set_gsr(gsr, lwptofpu(lwp));
if ((lwp == ttolwp(curthread)) && fpu_exists) {
fp->fpu_fprs = _fp_read_fprs();
if ((fp->fpu_fprs & FPRS_FEF) != FPRS_FEF) {
_fp_write_fprs(fprs);
fp->fpu_fprs = (V9_FPU_FPRS_TYPE)fprs;
}
restore_gsr(lwptofpu(lwp));
}
kpreempt_enable();
}
/*
* fill in the sun4u asrs, ie, the lwp's platform-dependent
* non-floating-point extra register state information
*/
/* ARGSUSED */
void
getasrs(klwp_t *lwp, asrset_t asr)
{
/* for sun4u nothing to do here, added for symmetry */
}
/*
* fill in the sun4u asrs, ie, the lwp's platform-dependent
* floating-point extra register state information
*/
void
getfpasrs(klwp_t *lwp, asrset_t asr)
{
kfpu_t *fp = lwptofpu(lwp);
uint32_t fprs = (FPRS_FEF|FPRS_DU|FPRS_DL);
kpreempt_disable();
if (ttolwp(curthread) == lwp)
fp->fpu_fprs = _fp_read_fprs();
if ((fp->fpu_en) || (fp->fpu_fprs & FPRS_FEF)) {
if (fpu_exists && ttolwp(curthread) == lwp) {
if ((fp->fpu_fprs & FPRS_FEF) != FPRS_FEF) {
_fp_write_fprs(fprs);
fp->fpu_fprs = (V9_FPU_FPRS_TYPE)fprs;
}
save_gsr(fp);
}
asr[ASR_GSR] = (int64_t)get_gsr(fp);
}
kpreempt_enable();
}
/*
* set the sun4u asrs, ie, the lwp's platform-dependent
* non-floating-point extra register state information
*/
/* ARGSUSED */
void
setasrs(klwp_t *lwp, asrset_t asr)
{
/* for sun4u nothing to do here, added for symmetry */
}
void
setfpasrs(klwp_t *lwp, asrset_t asr)
{
kfpu_t *fp = lwptofpu(lwp);
uint32_t fprs = (FPRS_FEF|FPRS_DU|FPRS_DL);
kpreempt_disable();
if (ttolwp(curthread) == lwp)
fp->fpu_fprs = _fp_read_fprs();
if ((fp->fpu_en) || (fp->fpu_fprs & FPRS_FEF)) {
set_gsr(asr[ASR_GSR], fp);
if (fpu_exists && ttolwp(curthread) == lwp) {
if ((fp->fpu_fprs & FPRS_FEF) != FPRS_FEF) {
_fp_write_fprs(fprs);
fp->fpu_fprs = (V9_FPU_FPRS_TYPE)fprs;
}
restore_gsr(fp);
}
}
kpreempt_enable();
}
/*
* Create interrupt kstats for this CPU.
*/
void
cpu_create_intrstat(cpu_t *cp)
{
int i;
kstat_t *intr_ksp;
kstat_named_t *knp;
char name[KSTAT_STRLEN];
zoneid_t zoneid;
ASSERT(MUTEX_HELD(&cpu_lock));
if (pool_pset_enabled())
zoneid = GLOBAL_ZONEID;
else
zoneid = ALL_ZONES;
intr_ksp = kstat_create_zone("cpu", cp->cpu_id, "intrstat", "misc",
KSTAT_TYPE_NAMED, PIL_MAX * 2, 0, zoneid);
/*
* Initialize each PIL's named kstat
*/
if (intr_ksp != NULL) {
intr_ksp->ks_update = cpu_kstat_intrstat_update;
knp = (kstat_named_t *)intr_ksp->ks_data;
intr_ksp->ks_private = cp;
for (i = 0; i < PIL_MAX; i++) {
(void) snprintf(name, KSTAT_STRLEN, "level-%d-time",
i + 1);
kstat_named_init(&knp[i * 2], name, KSTAT_DATA_UINT64);
(void) snprintf(name, KSTAT_STRLEN, "level-%d-count",
i + 1);
kstat_named_init(&knp[(i * 2) + 1], name,
KSTAT_DATA_UINT64);
}
kstat_install(intr_ksp);
}
}
/*
* Delete interrupt kstats for this CPU.
*/
void
cpu_delete_intrstat(cpu_t *cp)
{
kstat_delete_byname_zone("cpu", cp->cpu_id, "intrstat", ALL_ZONES);
}
/*
* Convert interrupt statistics from CPU ticks to nanoseconds and
* update kstat.
*/
int
cpu_kstat_intrstat_update(kstat_t *ksp, int rw)
{
kstat_named_t *knp = ksp->ks_data;
cpu_t *cpup = (cpu_t *)ksp->ks_private;
int i;
if (rw == KSTAT_WRITE)
return (EACCES);
/*
* We use separate passes to copy and convert the statistics to
* nanoseconds. This assures that the snapshot of the data is as
* self-consistent as possible.
*/
for (i = 0; i < PIL_MAX; i++) {
knp[i * 2].value.ui64 = cpup->cpu_m.intrstat[i + 1][0];
knp[(i * 2) + 1].value.ui64 = cpup->cpu_stats.sys.intr[i];
}
for (i = 0; i < PIL_MAX; i++) {
knp[i * 2].value.ui64 =
(uint64_t)tick2ns((hrtime_t)knp[i * 2].value.ui64,
cpup->cpu_id);
}
return (0);
}
/*
* Called by common/os/cpu.c for psrinfo(8) kstats
*/
char *
cpu_fru_fmri(cpu_t *cp)
{
return (cpunodes[cp->cpu_id].fru_fmri);
}
/*
* An interrupt thread is ending a time slice, so compute the interval it
* ran for and update the statistic for its PIL.
*/
void
cpu_intr_swtch_enter(kthread_id_t t)
{
uint64_t interval;
uint64_t start;
cpu_t *cpu;
ASSERT((t->t_flag & T_INTR_THREAD) != 0);
ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
/*
* We could be here with a zero timestamp. This could happen if:
* an interrupt thread which no longer has a pinned thread underneath
* it (i.e. it blocked at some point in its past) has finished running
* its handler. intr_thread() updated the interrupt statistic for its
* PIL and zeroed its timestamp. Since there was no pinned thread to
* return to, swtch() gets called and we end up here.
*
* It can also happen if an interrupt thread in intr_thread() calls
* preempt. It will have already taken care of updating stats. In
* this event, the interrupt thread will be runnable.
*/
if (t->t_intr_start) {
do {
start = t->t_intr_start;
interval = CLOCK_TICK_COUNTER() - start;
} while (atomic_cas_64(&t->t_intr_start, start, 0) != start);
cpu = CPU;
if (cpu->cpu_m.divisor > 1)
interval *= cpu->cpu_m.divisor;
cpu->cpu_m.intrstat[t->t_pil][0] += interval;
atomic_add_64((uint64_t *)&cpu->cpu_intracct[cpu->cpu_mstate],
interval);
} else
ASSERT(t->t_intr == NULL || t->t_state == TS_RUN);
}
/*
* An interrupt thread is returning from swtch(). Place a starting timestamp
* in its thread structure.
*/
void
cpu_intr_swtch_exit(kthread_id_t t)
{
uint64_t ts;
ASSERT((t->t_flag & T_INTR_THREAD) != 0);
ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
do {
ts = t->t_intr_start;
} while (atomic_cas_64(&t->t_intr_start, ts, CLOCK_TICK_COUNTER()) !=
ts);
}
int
blacklist(int cmd, const char *scheme, nvlist_t *fmri, const char *class)
{
if (&plat_blacklist)
return (plat_blacklist(cmd, scheme, fmri, class));
return (ENOTSUP);
}
int
kdi_pread(caddr_t buf, size_t nbytes, uint64_t addr, size_t *ncopiedp)
{
extern void kdi_flush_caches(void);
size_t nread = 0;
uint32_t word;
int slop, i;
kdi_flush_caches();
membar_enter();
/* We might not begin on a word boundary. */
if ((slop = addr & 3) != 0) {
word = ldphys(addr & ~3);
for (i = slop; i < 4 && nbytes > 0; i++, nbytes--, nread++)
*buf++ = ((uchar_t *)&word)[i];
addr = roundup(addr, 4);
}
while (nbytes > 0) {
word = ldphys(addr);
for (i = 0; i < 4 && nbytes > 0; i++, nbytes--, nread++, addr++)
*buf++ = ((uchar_t *)&word)[i];
}
kdi_flush_caches();
*ncopiedp = nread;
return (0);
}
int
kdi_pwrite(caddr_t buf, size_t nbytes, uint64_t addr, size_t *ncopiedp)
{
extern void kdi_flush_caches(void);
size_t nwritten = 0;
uint32_t word;
int slop, i;
kdi_flush_caches();
/* We might not begin on a word boundary. */
if ((slop = addr & 3) != 0) {
word = ldphys(addr & ~3);
for (i = slop; i < 4 && nbytes > 0; i++, nbytes--, nwritten++)
((uchar_t *)&word)[i] = *buf++;
stphys(addr & ~3, word);
addr = roundup(addr, 4);
}
while (nbytes > 3) {
for (word = 0, i = 0; i < 4; i++, nbytes--, nwritten++)
((uchar_t *)&word)[i] = *buf++;
stphys(addr, word);
addr += 4;
}
/* We might not end with a whole word. */
if (nbytes > 0) {
word = ldphys(addr);
for (i = 0; nbytes > 0; i++, nbytes--, nwritten++)
((uchar_t *)&word)[i] = *buf++;
stphys(addr, word);
}
membar_enter();
kdi_flush_caches();
*ncopiedp = nwritten;
return (0);
}
static void
kdi_kernpanic(struct regs *regs, uint_t tt)
{
sync_reg_buf = *regs;
sync_tt = tt;
sync_handler();
}
static void
kdi_plat_call(void (*platfn)(void))
{
if (platfn != NULL) {
prom_suspend_prepost();
platfn();
prom_resume_prepost();
}
}
/*
* kdi_system_claim and release are defined here for all sun4 platforms and
* pointed to by mach_kdi_init() to provide default callbacks for such systems.
* Specific sun4u or sun4v platforms may implement their own claim and release
* routines, at which point their respective callbacks will be updated.
*/
static void
kdi_system_claim(void)
{
lbolt_debug_entry();
}
static void
kdi_system_release(void)
{
lbolt_debug_return();
}
void
mach_kdi_init(kdi_t *kdi)
{
kdi->kdi_plat_call = kdi_plat_call;
kdi->kdi_kmdb_enter = kmdb_enter;
kdi->pkdi_system_claim = kdi_system_claim;
kdi->pkdi_system_release = kdi_system_release;
kdi->mkdi_cpu_index = kdi_cpu_index;
kdi->mkdi_trap_vatotte = kdi_trap_vatotte;
kdi->mkdi_kernpanic = kdi_kernpanic;
}
/*
* get_cpu_mstate() is passed an array of timestamps, NCMSTATES
* long, and it fills in the array with the time spent on cpu in
* each of the mstates, where time is returned in nsec.
*
* No guarantee is made that the returned values in times[] will
* monotonically increase on sequential calls, although this will
* be true in the long run. Any such guarantee must be handled by
* the caller, if needed. This can happen if we fail to account
* for elapsed time due to a generation counter conflict, yet we
* did account for it on a prior call (see below).
*
* The complication is that the cpu in question may be updating
* its microstate at the same time that we are reading it.
* Because the microstate is only updated when the CPU's state
* changes, the values in cpu_intracct[] can be indefinitely out
* of date. To determine true current values, it is necessary to
* compare the current time with cpu_mstate_start, and add the
* difference to times[cpu_mstate].
*
* This can be a problem if those values are changing out from
* under us. Because the code path in new_cpu_mstate() is
* performance critical, we have not added a lock to it. Instead,
* we have added a generation counter. Before beginning
* modifications, the counter is set to 0. After modifications,
* it is set to the old value plus one.
*
* get_cpu_mstate() will not consider the values of cpu_mstate
* and cpu_mstate_start to be usable unless the value of
* cpu_mstate_gen is both non-zero and unchanged, both before and
* after reading the mstate information. Note that we must
* protect against out-of-order loads around accesses to the
* generation counter. Also, this is a best effort approach in
* that we do not retry should the counter be found to have
* changed.
*
* cpu_intracct[] is used to identify time spent in each CPU
* mstate while handling interrupts. Such time should be reported
* against system time, and so is subtracted out from its
* corresponding cpu_acct[] time and added to
* cpu_acct[CMS_SYSTEM]. Additionally, intracct time is stored in
* %ticks, but acct time may be stored as %sticks, thus requiring
* different conversions before they can be compared.
*/
void
get_cpu_mstate(cpu_t *cpu, hrtime_t *times)
{
int i;
hrtime_t now, start;
uint16_t gen;
uint16_t state;
hrtime_t intracct[NCMSTATES];
/*
* Load all volatile state under the protection of membar.
* cpu_acct[cpu_mstate] must be loaded to avoid double counting
* of (now - cpu_mstate_start) by a change in CPU mstate that
* arrives after we make our last check of cpu_mstate_gen.
*/
now = gethrtime_unscaled();
gen = cpu->cpu_mstate_gen;
membar_consumer(); /* guarantee load ordering */
start = cpu->cpu_mstate_start;
state = cpu->cpu_mstate;
for (i = 0; i < NCMSTATES; i++) {
intracct[i] = cpu->cpu_intracct[i];
times[i] = cpu->cpu_acct[i];
}
membar_consumer(); /* guarantee load ordering */
if (gen != 0 && gen == cpu->cpu_mstate_gen && now > start)
times[state] += now - start;
for (i = 0; i < NCMSTATES; i++) {
scalehrtime(×[i]);
intracct[i] = tick2ns((hrtime_t)intracct[i], cpu->cpu_id);
}
for (i = 0; i < NCMSTATES; i++) {
if (i == CMS_SYSTEM)
continue;
times[i] -= intracct[i];
if (times[i] < 0) {
intracct[i] += times[i];
times[i] = 0;
}
times[CMS_SYSTEM] += intracct[i];
}
}
void
mach_cpu_pause(volatile char *safe)
{
/*
* This cpu is now safe.
*/
*safe = PAUSE_WAIT;
membar_enter(); /* make sure stores are flushed */
/*
* Now we wait. When we are allowed to continue, safe
* will be set to PAUSE_IDLE.
*/
while (*safe != PAUSE_IDLE)
SMT_PAUSE();
}
/*ARGSUSED*/
int
plat_mem_do_mmio(struct uio *uio, enum uio_rw rw)
{
return (ENOTSUP);
}
/* cpu threshold for compressed dumps */
#ifdef sun4v
uint_t dump_plat_mincpu_default = DUMP_PLAT_SUN4V_MINCPU;
#else
uint_t dump_plat_mincpu_default = DUMP_PLAT_SUN4U_MINCPU;
#endif
int
dump_plat_addr()
{
return (0);
}
void
dump_plat_pfn()
{
}
/* ARGSUSED */
int
dump_plat_data(void *dump_cdata)
{
return (0);
}
/* ARGSUSED */
int
plat_hold_page(pfn_t pfn, int lock, page_t **pp_ret)
{
return (PLAT_HOLD_OK);
}
/* ARGSUSED */
void
plat_release_page(page_t *pp)
{
}
/* ARGSUSED */
void
progressbar_key_abort(ldi_ident_t li)
{
}
/*
* We need to post a soft interrupt to reprogram the lbolt cyclic when
* switching from event to cyclic driven lbolt. The following code adds
* and posts the softint for sun4 platforms.
*/
static uint64_t lbolt_softint_inum;
void
lbolt_softint_add(void)
{
lbolt_softint_inum = add_softintr(LOCK_LEVEL,
(softintrfunc)lbolt_ev_to_cyclic, NULL, SOFTINT_MT);
}
void
lbolt_softint_post(void)
{
setsoftint(lbolt_softint_inum);
}
void
do_hotinlines(struct module *mp __unused)
{
}
|