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
|
/*
* 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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/types.h>
#include <sys/cpr.h>
#include <sys/ddi.h>
#include "cprboot.h"
/*
* check if any cpd_t pages clash with the statefile buffer and shuffle
* buf pages to free space; since kpages are saved in ascending order,
* any buf pages preceding the current statefile buffer offset can be
* written because those pages have already been read and restored
*/
static void
shuffle_pages(cpd_t *descp)
{
pfn_t low_src_ppn, dst_ppn, tail_ppn, new_ppn;
size_t dst_off;
/*
* set the lowest source buf ppn for the (precede) comparison
* below; the ORIG macro is used for the case where the src buf
* page had already been moved - and would confuse the compare
*/
low_src_ppn = SF_ORIG_PPN(sfile.buf_offset);
tail_ppn = descp->cpd_pfn + descp->cpd_pages;
for (dst_ppn = descp->cpd_pfn; dst_ppn < tail_ppn; dst_ppn++) {
/*
* if the dst page is outside the range of statefile
* buffer phys pages, it's OK to write that page;
* buf pages may have been moved outside the range,
* but only to locations isolated from any dst page
*/
if (dst_ppn < sfile.low_ppn || dst_ppn > sfile.high_ppn) {
SF_STAT_INC(outside);
continue;
}
/*
* the dst page is inside the range of buf ppns;
* dont need to move the buf page if the dst page
* precedes the lowest src buf page
*/
if (dst_ppn < low_src_ppn) {
SF_STAT_INC(precede);
continue;
}
/*
* the dst page clashes with the statefile buffer;
* move the buf page to a free location and update
* the buffer map
*/
new_ppn = find_apage();
phys_xcopy(PN_TO_ADDR(dst_ppn), PN_TO_ADDR(new_ppn),
MMU_PAGESIZE);
dst_off = mmu_ptob(dst_ppn - sfile.low_ppn);
SF_BUF_PPN(dst_off) = new_ppn;
SF_STAT_INC(move);
}
}
/*
* map-in source statefile buffer pages (read-only) at CB_SRC_VIRT;
* sets the starting source vaddr with correct page offset
*/
static void
mapin_buf_pages(size_t datalen, caddr_t *srcp)
{
int dtlb_index, pg_off;
caddr_t vaddr, tail;
size_t off, bytes;
pfn_t src_ppn;
dtlb_index = cb_dents - CB_MAX_KPAGES - 1;
off = sfile.buf_offset;
pg_off = off & MMU_PAGEOFFSET;
bytes = PAGE_ROUNDUP(pg_off + datalen);
vaddr = (caddr_t)CB_SRC_VIRT;
*srcp = vaddr + pg_off;
for (tail = vaddr + bytes; vaddr < tail; vaddr += MMU_PAGESIZE) {
src_ppn = SF_BUF_PPN(off);
cb_mapin(vaddr, src_ppn, TTE8K, 0, dtlb_index);
dtlb_index--;
off += MMU_PAGESIZE;
}
}
/*
* map-in destination kernel pages (read/write) at CB_DST_VIRT
*/
static void
mapin_dst_pages(cpd_t *descp)
{
int dtlb_index, pages;
caddr_t vaddr;
pfn_t dst_ppn;
dtlb_index = cb_dents - 1;
vaddr = (caddr_t)CB_DST_VIRT;
dst_ppn = descp->cpd_pfn;
for (pages = 0; pages < descp->cpd_pages; pages++) {
cb_mapin(vaddr, dst_ppn, TTE8K, TTE_HWWR_INT, dtlb_index);
dtlb_index--;
vaddr += MMU_PAGESIZE;
dst_ppn++;
}
}
/*
* run a checksum on un/compressed data when flag is set
*/
static int
kdata_cksum(void *data, cpd_t *descp, uint_t flag)
{
uint_t sum, expect;
size_t len;
if ((descp->cpd_flag & flag) == 0)
return (0);
else if (flag == CPD_CSUM) {
expect = descp->cpd_csum;
len = descp->cpd_length;
} else {
expect = descp->cpd_usum;
len = mmu_ptob(descp->cpd_pages);
}
sum = checksum32(data, len);
if (sum != expect) {
prom_printf("\n%scompressed data checksum error, "
"expect 0x%x, got 0x%x\n", (flag == CPD_USUM) ? "un" : "",
expect, sum);
return (ERR);
}
return (0);
}
/*
* primary kpage restoration routine
*/
static int
restore_page_group(cpd_t *descp)
{
caddr_t dst, datap;
size_t size, len;
caddr_t src;
int raw;
#if defined(lint)
(void) compress(0, 0, 0);
#endif
/*
* move any source buf pages that clash with dst kernel pages;
* create tlb entries for the orig/new source buf pages and
* the dst kpages
*/
shuffle_pages(descp);
mapin_buf_pages(descp->cpd_length, &src);
mapin_dst_pages(descp);
/*
* for compressed pages, run a checksum at the src vaddr and
* decompress to the mapped-in dst kpages; for uncompressed pages,
* just copy direct; uncompressed checksums are used for either
* uncompressed src data or decompressed result data
*/
dst = (caddr_t)CB_DST_VIRT;
if (descp->cpd_flag & CPD_COMPRESS) {
if (kdata_cksum(src, descp, CPD_CSUM))
return (ERR);
size = mmu_ptob(descp->cpd_pages);
len = decompress(src, dst, descp->cpd_length, size);
if (len != size) {
prom_printf("\nbad decompressed len %lu, size %lu\n",
len, size);
return (ERR);
}
raw = 0;
datap = dst;
} else {
raw = 1;
datap = src;
}
if (kdata_cksum(datap, descp, CPD_USUM))
return (ERR);
if (raw)
bcopy(src, dst, descp->cpd_length);
/*
* advance past the kdata for this cpd_t
*/
SF_ADV(descp->cpd_length);
return (0);
}
/*
* mapin part of the statefile buffer, copy to the virt destination,
* and advance the statefile buffer offset. this is used primarily
* to copy thousands of tiny cpd_t into aligned struct space.
*/
static void
get_phys_data(void *vdst, size_t size)
{
caddr_t src;
mapin_buf_pages(size, &src);
bcopy(src, vdst, size);
SF_ADV(size);
}
/*
* clear leftover locked dtlb entries
*/
static void
dtlb_cleanup(void)
{
int dtlb_index;
caddr_t vaddr;
tte_t tte;
CB_VENTRY(dtlb_cleanup);
dtlb_index = cb_dents - CB_MAX_KPAGES - CB_MAX_BPAGES - 1;
for (; dtlb_index < cb_dents; dtlb_index++) {
get_dtlb_entry(dtlb_index, &vaddr, &tte);
if (TTE_IS_LOCKED(&tte)) {
tte.ll = 0;
set_dtlb_entry(dtlb_index, (caddr_t)0, &tte);
CB_VPRINTF((" cleared dtlb entry %x\n", dtlb_index));
}
}
}
/*
* before calling this routine, all cprboot phys pages
* are isolated from kernel pages; now we can restore
* kpages from the statefile buffer
*/
int
cb_restore_kpages(void)
{
int npages, compressed, regular;
cpd_t desc;
char *str;
str = "cb_restore_kpages";
CB_VPRINTF((ent_fmt, str, entry));
CPR_DEBUG(CPR_DEBUG1, "%s: restoring kpages... ", prog);
npages = compressed = regular = 0;
while (npages < sfile.kpages) {
get_phys_data(&desc, sizeof (desc));
if (desc.cpd_magic != CPR_PAGE_MAGIC) {
prom_printf("\nbad page magic 0x%x, expect 0x%x\n",
desc.cpd_magic, CPR_PAGE_MAGIC);
return (ERR);
}
if (restore_page_group(&desc))
return (ERR);
npages += desc.cpd_pages;
if (desc.cpd_flag & CPD_COMPRESS)
compressed += desc.cpd_pages;
else
regular += desc.cpd_pages;
/*
* display a spin char for every 32 page groups
* (a full spin <= each MB restored)
*/
if ((sfile.ngroups++ & 0x1f) == 0)
cb_spin();
}
CPR_DEBUG(CPR_DEBUG1, " \b\n");
dtlb_cleanup();
if (verbose) {
prom_printf("\npage stats: total %d, outside %d, "
"move %d, precede %d\n", sfile.kpages, sfile.outside,
sfile.move, sfile.precede);
prom_printf("page stats: ngroups %d, recycle %d\n",
sfile.ngroups, sfile.recycle);
}
CPR_DEBUG(CPR_DEBUG4,
"%s: total=%d, npages=%d, compressed=%d, regular=%d\n",
str, sfile.kpages, npages, compressed, regular);
/*
* sanity check
*/
if (npages != sfile.kpages) {
prom_printf("\n%s: page count mismatch, expect %d, got %d\n",
str, sfile.kpages, npages);
return (ERR);
}
return (0);
}
/*
* check and update the statefile terminator;
* on exit there will be a leftover tlb entry,
* but it will soon get replaced by restore_tlb()
*/
int
cb_terminator(void)
{
ctrm_t cterm;
CB_VENTRY(cb_terminator);
get_phys_data(&cterm, sizeof (cterm));
if (cterm.magic != CPR_TERM_MAGIC) {
prom_printf("\nbad term magic 0x%x, expect 0x%x\n",
cterm.magic, CPR_TERM_MAGIC);
return (ERR);
}
cterm.tm_cprboot_start.tv_sec = cb_msec / 1000;
cb_mapin((caddr_t)CB_DST_VIRT, cterm.pfn,
TTE8K, TTE_HWWR_INT, cb_dents - 1);
cpr_update_terminator(&cterm, (caddr_t)CB_DST_VIRT);
return (0);
}
|