VirtualBox

source: vbox/trunk/src/VBox/Devices/PC/DevRTC.cpp@ 1245

Last change on this file since 1245 was 1245, checked in by vboxsync, 18 years ago

fixed win.amd64 alignemnt issue.

  • Property svn:eol-style set to native
File size: 29.8 KB
Line 
1/** @file
2 *
3 * VBox basic PC devices:
4 * Motorola MC146818 RTC/CMOS Device.
5 */
6
7/*
8 * Copyright (C) 2006 InnoTek Systemberatung GmbH
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License as published by the Free Software Foundation,
14 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
15 * distribution. VirtualBox OSE is distributed in the hope that it will
16 * be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * If you received this file as part of a commercial VirtualBox
19 * distribution, then only the terms of your commercial VirtualBox
20 * license agreement apply instead of the previous paragraph.
21 *
22 * --------------------------------------------------------------------
23 *
24 * This code is based on:
25 *
26 * QEMU MC146818 RTC emulation
27 *
28 * Copyright (c) 2003-2004 Fabrice Bellard
29 *
30 * Permission is hereby granted, free of charge, to any person obtaining a copy
31 * of this software and associated documentation files (the "Software"), to deal
32 * in the Software without restriction, including without limitation the rights
33 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
34 * copies of the Software, and to permit persons to whom the Software is
35 * furnished to do so, subject to the following conditions:
36 *
37 * The above copyright notice and this permission notice shall be included in
38 * all copies or substantial portions of the Software.
39 *
40 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
41 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
42 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
43 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
44 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
45 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
46 * THE SOFTWARE.
47 */
48
49/*******************************************************************************
50* Header Files *
51*******************************************************************************/
52#define LOG_GROUP LOG_GROUP_DEV_RTC
53#include <VBox/pdm.h>
54
55#include <VBox/log.h>
56#include <iprt/assert.h>
57
58#include "vl_vbox.h"
59
60/** @todo Implement time/localtime/gmtime replacements in Runtime! */
61#include <time.h>
62
63struct RTCState;
64typedef struct RTCState RTCState;
65
66#define RTC_CRC_START 0x10
67#define RTC_CRC_LAST 0x2d
68#define RTC_CRC_HIGH 0x2e
69#define RTC_CRC_LOW 0x2f
70
71#ifndef VBOX_DEVICE_STRUCT_TESTCASE
72/*******************************************************************************
73* Internal Functions *
74*******************************************************************************/
75__BEGIN_DECLS
76PDMBOTHCBDECL(int) rtcIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb);
77PDMBOTHCBDECL(int) rtcIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb);
78PDMBOTHCBDECL(void) rtcTimerPeriodic(PPDMDEVINS pDevIns, PTMTIMER pTimer);
79PDMBOTHCBDECL(void) rtcTimerSecond(PPDMDEVINS pDevIns, PTMTIMER pTimer);
80PDMBOTHCBDECL(void) rtcTimerSecond2(PPDMDEVINS pDevIns, PTMTIMER pTimer);
81__END_DECLS
82#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
83
84/*#define DEBUG_CMOS*/
85
86#define RTC_SECONDS 0
87#define RTC_SECONDS_ALARM 1
88#define RTC_MINUTES 2
89#define RTC_MINUTES_ALARM 3
90#define RTC_HOURS 4
91#define RTC_HOURS_ALARM 5
92#define RTC_ALARM_DONT_CARE 0xC0
93
94#define RTC_DAY_OF_WEEK 6
95#define RTC_DAY_OF_MONTH 7
96#define RTC_MONTH 8
97#define RTC_YEAR 9
98
99#define RTC_REG_A 10
100#define RTC_REG_B 11
101#define RTC_REG_C 12
102#define RTC_REG_D 13
103
104#define REG_A_UIP 0x80
105
106#define REG_B_SET 0x80
107#define REG_B_PIE 0x40
108#define REG_B_AIE 0x20
109#define REG_B_UIE 0x10
110
111struct RTCState {
112 uint8_t cmos_data[128];
113 uint8_t cmos_index;
114 uint8_t Alignment0[7];
115 struct tm current_tm;
116#if HC_ARCH_BITS == 64 && GC_ARCH_BITS == 32 && IN_GC
117# if !defined(__WIN__)
118 uint32_t Alignment1[3];
119# endif
120#endif
121 int32_t irq;
122 /* periodic timer */
123 PTMTIMERGC pPeriodicTimerGC;
124 PTMTIMERHC pPeriodicTimerHC;
125 int64_t next_periodic_time;
126 /* second update */
127 int64_t next_second_time;
128 PTMTIMERHC pSecondTimerHC;
129 PTMTIMERGC pSecondTimerGC;
130 PTMTIMERGC pSecondTimer2GC;
131 PTMTIMERHC pSecondTimer2HC;
132 /** Pointer to the device instance - HC Ptr. */
133 PPDMDEVINSHC pDevInsHC;
134 /** Pointer to the device instance - GC Ptr. */
135 PPDMDEVINSGC pDevInsGC;
136 /** Use UCT or local time initially. */
137 bool fUCT;
138 /** The RTC registration structure. */
139 PDMRTCREG RtcReg;
140 /** The RTC device helpers. */
141 HCPTRTYPE(PCPDMRTCHLP) pRtcHlpHC;
142};
143
144#ifndef VBOX_DEVICE_STRUCT_TESTCASE
145static void rtc_set_time(RTCState *s);
146static void rtc_copy_date(RTCState *s);
147
148static void rtc_timer_update(RTCState *s, int64_t current_time)
149{
150 int period_code, period;
151 uint64_t cur_clock, next_irq_clock, now, quarter_period_time;
152 int64_t delta;
153 uint32_t freq;
154
155 period_code = s->cmos_data[RTC_REG_A] & 0x0f;
156 if (period_code != 0 &&
157 (s->cmos_data[RTC_REG_B] & REG_B_PIE)) {
158 if (period_code <= 2)
159 period_code += 7;
160 /* period in 32 kHz cycles */
161 period = 1 << (period_code - 1);
162 /* compute 32 kHz clock */
163 freq = TMTimerGetFreq(s->CTXSUFF(pPeriodicTimer));
164
165 cur_clock = muldiv64(current_time, 32768, freq);
166 next_irq_clock = (cur_clock & ~(uint64_t)(period - 1)) + period;
167 s->next_periodic_time = muldiv64(next_irq_clock, freq, 32768) + 1;
168
169 /* fiddly bits for dealing with running to keep up and losing interrupts. */
170 quarter_period_time = muldiv64(period, freq, 32768 * 4);
171 now = TMTimerGet(s->CTXSUFF(pPeriodicTimer));
172 delta = s->next_periodic_time - now;
173 if (delta >= (int64_t)quarter_period_time)
174 {
175 TMTimerSet(s->CTXSUFF(pPeriodicTimer), s->next_periodic_time);
176 Log2(("period=%d current_time=%RU64 next=%RU64 delta=%-10RI64\n", period, current_time, s->next_periodic_time, delta));
177 }
178 else
179 {
180 uint64_t next = now + quarter_period_time; /* 4x speed */
181 TMTimerSet(s->CTXSUFF(pPeriodicTimer), next);
182 Log2(("period=%d current_time=%RU64 next=%RU64 delta=%-10RI64 now=%RU64 real_next=%RU64\n", period, current_time,
183 s->next_periodic_time, delta, now, next));
184 }
185 } else {
186 TMTimerStop(s->CTXSUFF(pPeriodicTimer));
187 }
188}
189
190static void rtc_periodic_timer(void *opaque)
191{
192 RTCState *s = (RTCState*)opaque;
193
194 rtc_timer_update(s, s->next_periodic_time);
195 s->cmos_data[RTC_REG_C] |= 0xc0;
196 PDMDevHlpISASetIrq(s->CTXSUFF(pDevIns), s->irq, 1);
197}
198
199static void cmos_ioport_write(void *opaque, uint32_t addr, uint32_t data)
200{
201 RTCState *s = (RTCState*)opaque;
202
203 if ((addr & 1) == 0) {
204 s->cmos_index = data & 0x7f;
205 } else {
206 Log(("CMOS: Write idx %#04x: %#04x (old %#04x)\n", s->cmos_index, data, s->cmos_data[s->cmos_index]));
207 switch(s->cmos_index) {
208 case RTC_SECONDS_ALARM:
209 case RTC_MINUTES_ALARM:
210 case RTC_HOURS_ALARM:
211 /* XXX: not supported */
212 s->cmos_data[s->cmos_index] = data;
213 break;
214 case RTC_SECONDS:
215 case RTC_MINUTES:
216 case RTC_HOURS:
217 case RTC_DAY_OF_WEEK:
218 case RTC_DAY_OF_MONTH:
219 case RTC_MONTH:
220 case RTC_YEAR:
221 s->cmos_data[s->cmos_index] = data;
222 /* if in set mode, do not update the time */
223 if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
224 rtc_set_time(s);
225 }
226 break;
227 case RTC_REG_A:
228 /* UIP bit is read only */
229 s->cmos_data[RTC_REG_A] = (data & ~REG_A_UIP) |
230 (s->cmos_data[RTC_REG_A] & REG_A_UIP);
231 rtc_timer_update(s, TMTimerGet(s->CTXSUFF(pPeriodicTimer)));
232 break;
233 case RTC_REG_B:
234 if (data & REG_B_SET) {
235 /* set mode: reset UIP mode */
236 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
237 data &= ~REG_B_UIE;
238 } else {
239 /* if disabling set mode, update the time */
240 if (s->cmos_data[RTC_REG_B] & REG_B_SET) {
241 rtc_set_time(s);
242 }
243 }
244 s->cmos_data[RTC_REG_B] = data;
245 rtc_timer_update(s, TMTimerGet(s->CTXSUFF(pPeriodicTimer)));
246 break;
247 case RTC_REG_C:
248 case RTC_REG_D:
249 /* cannot write to them */
250 break;
251 default:
252 s->cmos_data[s->cmos_index] = data;
253 break;
254 }
255 }
256}
257
258static inline int to_bcd(RTCState *s, int a)
259{
260 if (s->cmos_data[RTC_REG_B] & 0x04) {
261 return a;
262 } else {
263 return ((a / 10) << 4) | (a % 10);
264 }
265}
266
267static inline int from_bcd(RTCState *s, int a)
268{
269 if (s->cmos_data[RTC_REG_B] & 0x04) {
270 return a;
271 } else {
272 return ((a >> 4) * 10) + (a & 0x0f);
273 }
274}
275
276static void rtc_set_time(RTCState *s)
277{
278 struct tm *tm = &s->current_tm;
279
280 tm->tm_sec = from_bcd(s, s->cmos_data[RTC_SECONDS]);
281 tm->tm_min = from_bcd(s, s->cmos_data[RTC_MINUTES]);
282 tm->tm_hour = from_bcd(s, s->cmos_data[RTC_HOURS] & 0x7f);
283 if (!(s->cmos_data[RTC_REG_B] & 0x02) &&
284 (s->cmos_data[RTC_HOURS] & 0x80)) {
285 tm->tm_hour += 12;
286 }
287 tm->tm_wday = from_bcd(s, s->cmos_data[RTC_DAY_OF_WEEK]);
288 tm->tm_mday = from_bcd(s, s->cmos_data[RTC_DAY_OF_MONTH]);
289 tm->tm_mon = from_bcd(s, s->cmos_data[RTC_MONTH]) - 1;
290 tm->tm_year = from_bcd(s, s->cmos_data[RTC_YEAR]) + 100;
291}
292
293static void rtc_copy_date(RTCState *s)
294{
295 const struct tm *tm = &s->current_tm;
296
297 s->cmos_data[RTC_SECONDS] = to_bcd(s, tm->tm_sec);
298 s->cmos_data[RTC_MINUTES] = to_bcd(s, tm->tm_min);
299 if (s->cmos_data[RTC_REG_B] & 0x02) {
300 /* 24 hour format */
301 s->cmos_data[RTC_HOURS] = to_bcd(s, tm->tm_hour);
302 } else {
303 /* 12 hour format */
304 s->cmos_data[RTC_HOURS] = to_bcd(s, tm->tm_hour % 12);
305 if (tm->tm_hour >= 12)
306 s->cmos_data[RTC_HOURS] |= 0x80;
307 }
308 s->cmos_data[RTC_DAY_OF_WEEK] = to_bcd(s, tm->tm_wday);
309 s->cmos_data[RTC_DAY_OF_MONTH] = to_bcd(s, tm->tm_mday);
310 s->cmos_data[RTC_MONTH] = to_bcd(s, tm->tm_mon + 1);
311 s->cmos_data[RTC_YEAR] = to_bcd(s, tm->tm_year % 100);
312}
313
314/* month is between 0 and 11. */
315static int get_days_in_month(int month, int year)
316{
317 static const int days_tab[12] = {
318 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
319 };
320 int d;
321 if ((unsigned )month >= 12)
322 return 31;
323 d = days_tab[month];
324 if (month == 1) {
325 if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0))
326 d++;
327 }
328 return d;
329}
330
331/* update 'tm' to the next second */
332static void rtc_next_second(struct tm *tm)
333{
334 int days_in_month;
335
336 tm->tm_sec++;
337 if ((unsigned)tm->tm_sec >= 60) {
338 tm->tm_sec = 0;
339 tm->tm_min++;
340 if ((unsigned)tm->tm_min >= 60) {
341 tm->tm_min = 0;
342 tm->tm_hour++;
343 if ((unsigned)tm->tm_hour >= 24) {
344 tm->tm_hour = 0;
345 /* next day */
346 tm->tm_wday++;
347 if ((unsigned)tm->tm_wday >= 7)
348 tm->tm_wday = 0;
349 days_in_month = get_days_in_month(tm->tm_mon,
350 tm->tm_year + 1900);
351 tm->tm_mday++;
352 if (tm->tm_mday < 1) {
353 tm->tm_mday = 1;
354 } else if (tm->tm_mday > days_in_month) {
355 tm->tm_mday = 1;
356 tm->tm_mon++;
357 if (tm->tm_mon >= 12) {
358 tm->tm_mon = 0;
359 tm->tm_year++;
360 }
361 }
362 }
363 }
364 }
365}
366
367
368static void rtc_update_second(void *opaque)
369{
370 RTCState *s = (RTCState*)opaque;
371 int64_t delay;
372
373 /* if the oscillator is not in normal operation, we do not update */
374 if ((s->cmos_data[RTC_REG_A] & 0x70) != 0x20) {
375 s->next_second_time += TMTimerGetFreq(s->CTXSUFF(pSecondTimer));
376 TMTimerSet(s->CTXSUFF(pSecondTimer), s->next_second_time);
377 } else {
378 rtc_next_second(&s->current_tm);
379
380 if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
381 /* update in progress bit */
382 s->cmos_data[RTC_REG_A] |= REG_A_UIP;
383 }
384 /* should be 244 us = 8 / 32768 seconds, but currently the
385 timers do not have the necessary resolution. */
386 delay = (TMTimerGetFreq(s->CTXSUFF(pSecondTimer2)) * 1) / 100;
387 if (delay < 1)
388 delay = 1;
389 TMTimerSet(s->CTXSUFF(pSecondTimer2), s->next_second_time + delay);
390 }
391}
392
393static void rtc_update_second2(void *opaque)
394{
395 RTCState *s = (RTCState*)opaque;
396
397 if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
398 rtc_copy_date(s);
399 }
400
401 /* check alarm */
402 if (s->cmos_data[RTC_REG_B] & REG_B_AIE) {
403 if (((s->cmos_data[RTC_SECONDS_ALARM] & 0xc0) == 0xc0 ||
404 s->cmos_data[RTC_SECONDS_ALARM] == s->current_tm.tm_sec) &&
405 ((s->cmos_data[RTC_MINUTES_ALARM] & 0xc0) == 0xc0 ||
406 s->cmos_data[RTC_MINUTES_ALARM] == s->current_tm.tm_mon) &&
407 ((s->cmos_data[RTC_HOURS_ALARM] & 0xc0) == 0xc0 ||
408 s->cmos_data[RTC_HOURS_ALARM] == s->current_tm.tm_hour)) {
409
410 s->cmos_data[RTC_REG_C] |= 0xa0;
411 PDMDevHlpISASetIrq(s->CTXSUFF(pDevIns), s->irq, 1);
412 }
413 }
414
415 /* update ended interrupt */
416 if (s->cmos_data[RTC_REG_B] & REG_B_UIE) {
417 s->cmos_data[RTC_REG_C] |= 0x90;
418 PDMDevHlpISASetIrq(s->CTXSUFF(pDevIns), s->irq, 1);
419 }
420
421 /* clear update in progress bit */
422 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
423
424 s->next_second_time += TMTimerGetFreq(s->CTXSUFF(pSecondTimer));
425 TMTimerSet(s->CTXSUFF(pSecondTimer), s->next_second_time);
426}
427
428static uint32_t cmos_ioport_read(void *opaque, uint32_t addr)
429{
430 RTCState *s = (RTCState*)opaque;
431 int ret;
432 if ((addr & 1) == 0) {
433 return 0xff;
434 } else {
435 switch(s->cmos_index) {
436 case RTC_SECONDS:
437 case RTC_MINUTES:
438 case RTC_HOURS:
439 case RTC_DAY_OF_WEEK:
440 case RTC_DAY_OF_MONTH:
441 case RTC_MONTH:
442 case RTC_YEAR:
443 ret = s->cmos_data[s->cmos_index];
444 break;
445 case RTC_REG_A:
446 ret = s->cmos_data[s->cmos_index];
447 break;
448 case RTC_REG_C:
449 ret = s->cmos_data[s->cmos_index];
450 PDMDevHlpISASetIrq(s->CTXSUFF(pDevIns), s->irq, 0);
451 s->cmos_data[RTC_REG_C] = 0x00;
452 break;
453 default:
454 ret = s->cmos_data[s->cmos_index];
455 break;
456 }
457 Log(("CMOS: Read idx %#04x: %#04x\n", s->cmos_index, ret));
458 return ret;
459 }
460}
461
462#ifdef IN_RING3
463static void rtc_set_memory(RTCState *s, int addr, int val)
464{
465 if (addr >= 0 && addr <= 127)
466 s->cmos_data[addr] = val;
467}
468
469static void rtc_set_date(RTCState *s, const struct tm *tm)
470{
471 s->current_tm = *tm;
472 rtc_copy_date(s);
473}
474
475static void rtc_save(QEMUFile *f, void *opaque)
476{
477 RTCState *s = (RTCState*)opaque;
478
479 qemu_put_buffer(f, s->cmos_data, 128);
480 qemu_put_8s(f, &s->cmos_index);
481
482 qemu_put_be32s(f, &s->current_tm.tm_sec);
483 qemu_put_be32s(f, &s->current_tm.tm_min);
484 qemu_put_be32s(f, &s->current_tm.tm_hour);
485 qemu_put_be32s(f, &s->current_tm.tm_wday);
486 qemu_put_be32s(f, &s->current_tm.tm_mday);
487 qemu_put_be32s(f, &s->current_tm.tm_mon);
488 qemu_put_be32s(f, &s->current_tm.tm_year);
489
490 qemu_put_timer(f, s->CTXSUFF(pPeriodicTimer));
491 qemu_put_be64s(f, &s->next_periodic_time);
492
493 qemu_put_be64s(f, &s->next_second_time);
494 qemu_put_timer(f, s->CTXSUFF(pSecondTimer));
495 qemu_put_timer(f, s->CTXSUFF(pSecondTimer2));
496}
497
498static int rtc_load(QEMUFile *f, void *opaque, int version_id)
499{
500 RTCState *s = (RTCState*)opaque;
501
502 if (version_id != 1)
503 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
504
505 qemu_get_buffer(f, s->cmos_data, 128);
506 qemu_get_8s(f, &s->cmos_index);
507
508 qemu_get_be32s(f, (uint32_t *)&s->current_tm.tm_sec);
509 qemu_get_be32s(f, (uint32_t *)&s->current_tm.tm_min);
510 qemu_get_be32s(f, (uint32_t *)&s->current_tm.tm_hour);
511 qemu_get_be32s(f, (uint32_t *)&s->current_tm.tm_wday);
512 qemu_get_be32s(f, (uint32_t *)&s->current_tm.tm_mday);
513 qemu_get_be32s(f, (uint32_t *)&s->current_tm.tm_mon);
514 qemu_get_be32s(f, (uint32_t *)&s->current_tm.tm_year);
515
516 qemu_get_timer(f, s->CTXSUFF(pPeriodicTimer));
517
518 qemu_get_be64s(f, (uint64_t *)&s->next_periodic_time);
519
520 qemu_get_be64s(f, (uint64_t *)&s->next_second_time);
521 qemu_get_timer(f, s->CTXSUFF(pSecondTimer));
522 qemu_get_timer(f, s->CTXSUFF(pSecondTimer2));
523 return 0;
524}
525#endif /* IN_RING3 */
526
527/* -=-=-=-=-=- wrappers -=-=-=-=-=- */
528
529/**
530 * Port I/O Handler for IN operations.
531 *
532 * @returns VBox status code.
533 *
534 * @param pDevIns The device instance.
535 * @param pvUser User argument - ignored.
536 * @param uPort Port number used for the IN operation.
537 * @param pu32 Where to store the result.
538 * @param cb Number of bytes read.
539 */
540PDMBOTHCBDECL(int) rtcIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
541{
542 NOREF(pvUser);
543 if (cb == 1)
544 {
545 *pu32 = cmos_ioport_read(PDMINS2DATA(pDevIns, RTCState *), Port);
546 return VINF_SUCCESS;
547 }
548 return VERR_IOM_IOPORT_UNUSED;
549}
550
551
552/**
553 * Port I/O Handler for OUT operations.
554 *
555 * @returns VBox status code.
556 *
557 * @param pDevIns The device instance.
558 * @param pvUser User argument - ignored.
559 * @param uPort Port number used for the IN operation.
560 * @param u32 The value to output.
561 * @param cb The value size in bytes.
562 */
563PDMBOTHCBDECL(int) rtcIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
564{
565 NOREF(pvUser);
566 if (cb == 1)
567 cmos_ioport_write(PDMINS2DATA(pDevIns, RTCState *), Port, u32);
568 return VINF_SUCCESS;
569}
570
571
572/**
573 * Device timer callback function, periodic.
574 *
575 * @param pDevIns Device instance of the device which registered the timer.
576 * @param pTimer The timer handle.
577 */
578PDMBOTHCBDECL(void) rtcTimerPeriodic(PPDMDEVINS pDevIns, PTMTIMER pTimer)
579{
580 rtc_periodic_timer(PDMINS2DATA(pDevIns, RTCState *));
581}
582
583
584/**
585 * Device timer callback function, second.
586 *
587 * @param pDevIns Device instance of the device which registered the timer.
588 * @param pTimer The timer handle.
589 */
590PDMBOTHCBDECL(void) rtcTimerSecond(PPDMDEVINS pDevIns, PTMTIMER pTimer)
591{
592 rtc_update_second(PDMINS2DATA(pDevIns, RTCState *));
593}
594
595
596/**
597 * Device timer callback function, second2.
598 *
599 * @param pDevIns Device instance of the device which registered the timer.
600 * @param pTimer The timer handle.
601 */
602PDMBOTHCBDECL(void) rtcTimerSecond2(PPDMDEVINS pDevIns, PTMTIMER pTimer)
603{
604 rtc_update_second2(PDMINS2DATA(pDevIns, RTCState *));
605}
606
607
608#ifdef IN_RING3
609/**
610 * Saves a state of the programmable interval timer device.
611 *
612 * @returns VBox status code.
613 * @param pDevIns The device instance.
614 * @param pSSMHandle The handle to save the state to.
615 */
616static DECLCALLBACK(int) rtcSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle)
617{
618 RTCState *pData = PDMINS2DATA(pDevIns, RTCState *);
619 rtc_save(pSSMHandle, pData);
620 return VINF_SUCCESS;
621}
622
623
624/**
625 * Loads a saved programmable interval timer device state.
626 *
627 * @returns VBox status code.
628 * @param pDevIns The device instance.
629 * @param pSSMHandle The handle to the saved state.
630 * @param u32Version The data unit version number.
631 */
632static DECLCALLBACK(int) rtcLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle, uint32_t u32Version)
633{
634 RTCState *pData = PDMINS2DATA(pDevIns, RTCState *);
635 return rtc_load(pSSMHandle, pData, u32Version);
636}
637
638
639/* -=-=-=-=-=- PDM Interface provided by the RTC device -=-=-=-=-=- */
640
641/**
642 * Calculate and update the standard CMOS checksum.
643 *
644 * @param pData Pointer to the RTC state data.
645 */
646static void rtcCalcCRC(RTCState *pData)
647{
648 uint16_t u16;
649 unsigned i;
650
651 for (i = RTC_CRC_START, u16 = 0; i <= RTC_CRC_LAST; i++)
652 u16 += pData->cmos_data[i];
653 pData->cmos_data[RTC_CRC_LOW] = u16 & 0xff;
654 pData->cmos_data[RTC_CRC_HIGH] = (u16 >> 8) & 0xff;
655}
656
657
658/**
659 * Write to a CMOS register and update the checksum if necessary.
660 *
661 * @returns VBox status code.
662 * @param pDevIns Device instance of the RTC.
663 * @param iReg The CMOS register index.
664 * @param u8Value The CMOS register value.
665 */
666static DECLCALLBACK(int) rtcCMOSWrite(PPDMDEVINS pDevIns, unsigned iReg, uint8_t u8Value)
667{
668 RTCState *pData = PDMINS2DATA(pDevIns, RTCState *);
669 if (iReg < ELEMENTS(pData->cmos_data))
670 {
671 pData->cmos_data[iReg] = u8Value;
672
673 /* does it require checksum update? */
674 if ( iReg >= RTC_CRC_START
675 && iReg <= RTC_CRC_LAST)
676 rtcCalcCRC(pData);
677
678 return VINF_SUCCESS;
679 }
680 AssertMsgFailed(("iReg=%d\n", iReg));
681 return VERR_INVALID_PARAMETER;
682}
683
684
685/**
686 * Read a CMOS register.
687 *
688 * @returns VBox status code.
689 * @param pDevIns Device instance of the RTC.
690 * @param iReg The CMOS register index.
691 * @param pu8Value Where to store the CMOS register value.
692 */
693static DECLCALLBACK(int) rtcCMOSRead(PPDMDEVINS pDevIns, unsigned iReg, uint8_t *pu8Value)
694{
695 RTCState *pData = PDMINS2DATA(pDevIns, RTCState *);
696 if (iReg < ELEMENTS(pData->cmos_data))
697 {
698 *pu8Value = pData->cmos_data[iReg];
699 return VINF_SUCCESS;
700 }
701 AssertMsgFailed(("iReg=%d\n", iReg));
702 return VERR_INVALID_PARAMETER;
703}
704
705
706/* -=-=-=-=-=- based on bits from pc.c -=-=-=-=-=- */
707
708/** @copydoc FNPDMDEVINITCOMPLETE */
709static DECLCALLBACK(int) rtcInitComplete(PPDMDEVINS pDevIns)
710{
711 RTCState *pData = PDMINS2DATA(pDevIns, RTCState *);
712 time_t Ti;
713 struct tm Tm;
714 struct tm *pTm;
715 int iYear;
716
717 /*
718 * Set the CMOS date/time.
719 */
720#if 0 /* later */
721 RTTIMESPEC Now;
722 RTTIME Time;
723 RTTimeNow(&Now);
724 if (pData->fUCT)
725 RTTimeExplode(&Time, &Now);
726 else
727 RTTimeLocalExplode(&Time, &Now);
728 pTm = RTTimeToPosixTm(&Tm, &Time);
729#else
730 time(&Ti);
731#ifndef __WIN__
732 if (pData->fUCT)
733 pTm = gmtime_r(&Ti, &Tm);
734 else
735 pTm = localtime_r(&Ti, &Tm);
736 Assert(pTm);
737#else
738 /* Win32 doesn't have thread safe stuff, let's just hope this works out fine :/ */
739 if (pData->fUCT)
740 pTm = gmtime(&Ti);
741 else
742 pTm = localtime(&Ti);
743 Assert(pTm);
744 Tm = *pTm;
745 pTm = &Tm;
746#endif
747#endif
748 rtc_set_date(pData, pTm);
749
750 iYear = to_bcd(pData, (Tm.tm_year / 100) + 19); /* tm_year is 1900 based (stupid) */
751 rtc_set_memory(pData, 0x32, iYear); /* 32h - Century Byte (BCD value for the century */
752 rtc_set_memory(pData, 0x37, iYear); /* 37h - (IBM PS/2) Date Century Byte */
753
754 /*
755 * Recalculate the checksum just in case.
756 */
757 rtcCalcCRC(pData);
758
759 Log(("CMOS: \n%16.128Vhxd\n", pData->cmos_data));
760 return VINF_SUCCESS;
761}
762
763
764/* -=-=-=-=-=- real code -=-=-=-=-=- */
765
766/**
767 * @copydoc
768 */
769static DECLCALLBACK(void) rtcRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
770{
771 RTCState *pThis = PDMINS2DATA(pDevIns, RTCState *);
772
773 pThis->pDevInsGC = PDMDEVINS_2_GCPTR(pDevIns);
774 pThis->pPeriodicTimerGC = TMTimerGCPtr(pThis->pPeriodicTimerHC);
775 pThis->pSecondTimerGC = TMTimerGCPtr(pThis->pSecondTimerHC);
776 pThis->pSecondTimer2GC = TMTimerGCPtr(pThis->pSecondTimer2HC);
777}
778
779
780/**
781 * Construct a device instance for a VM.
782 *
783 * @returns VBox status.
784 * @param pDevIns The device instance data.
785 * If the registration structure is needed, pDevIns->pDevReg points to it.
786 * @param iInstance Instance number. Use this to figure out which registers and such to use.
787 * The device number is also found in pDevIns->iInstance, but since it's
788 * likely to be freqently used PDM passes it as parameter.
789 * @param pCfgHandle Configuration node handle for the device. Use this to obtain the configuration
790 * of the device instance. It's also found in pDevIns->pCfgHandle, but like
791 * iInstance it's expected to be used a bit in this function.
792 */
793static DECLCALLBACK(int) rtcConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfgHandle)
794{
795 RTCState *pData = PDMINS2DATA(pDevIns, RTCState *);
796 int rc;
797 uint8_t u8Irq;
798 uint16_t u16Base;
799 bool fGCEnabled;
800 bool fR0Enabled;
801 Assert(iInstance == 0);
802
803 /*
804 * Validate configuration.
805 */
806 if (!CFGMR3AreValuesValid(pCfgHandle, "Irq\0Base\0GCEnabled\0fR0Enabled\0"))
807 return VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES;
808
809 /*
810 * Init the data.
811 */
812 rc = CFGMR3QueryU8(pCfgHandle, "Irq", &u8Irq);
813 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
814 u8Irq = 8;
815 else if (VBOX_FAILURE(rc))
816 return PDMDEV_SET_ERROR(pDevIns, rc,
817 N_("Configuration error: Querying \"Irq\" as a uint8_t failed"));
818
819 rc = CFGMR3QueryU16(pCfgHandle, "Base", &u16Base);
820 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
821 u16Base = 0x70;
822 else if (VBOX_FAILURE(rc))
823 return PDMDEV_SET_ERROR(pDevIns, rc,
824 N_("Configuration error: Querying \"Base\" as a uint16_t failed"));
825
826 rc = CFGMR3QueryBool(pCfgHandle, "GCEnabled", &fGCEnabled);
827 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
828 fGCEnabled = false/*true*/; /** @todo later when we've got more than 15-30 switches to save. */
829 else if (VBOX_FAILURE(rc))
830 return PDMDEV_SET_ERROR(pDevIns, rc,
831 N_("Configuration error: failed to read GCEnabled as boolean"));
832
833 rc = CFGMR3QueryBool(pCfgHandle, "R0Enabled", &fR0Enabled);
834 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
835 fR0Enabled = true;
836 else if (VBOX_FAILURE(rc))
837 return PDMDEV_SET_ERROR(pDevIns, rc,
838 N_("Configuration error: failed to read R0Enabled as boolean"));
839
840 Log(("CMOS: fGCEnabled=%d fR0Enabled=%d\n", fGCEnabled, fR0Enabled));
841
842
843 pData->pDevInsHC = pDevIns;
844 pData->irq = u8Irq;
845 pData->cmos_data[RTC_REG_A] = 0x26;
846 pData->cmos_data[RTC_REG_B] = 0x02;
847 pData->cmos_data[RTC_REG_C] = 0x00;
848 pData->cmos_data[RTC_REG_D] = 0x80;
849 pData->RtcReg.u32Version = PDM_RTCREG_VERSION;
850 pData->RtcReg.pfnRead = rtcCMOSRead;
851 pData->RtcReg.pfnWrite = rtcCMOSWrite;
852
853 /*
854 * Create timers, arm them, register I/O Ports and save state.
855 */
856 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, rtcTimerPeriodic, "MC146818 RTC/CMOS - Periodic", &pData->pPeriodicTimerHC);
857 if (VBOX_FAILURE(rc))
858 {
859 AssertMsgFailed(("pfnTMTimerCreate -> %Vrc\n", rc));
860 return rc;
861 }
862 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, rtcTimerSecond, "MC146818 RTC/CMOS - Second", &pData->pSecondTimerHC);
863 if (VBOX_FAILURE(rc))
864 {
865 AssertMsgFailed(("pfnTMTimerCreate -> %Vrc\n", rc));
866 return rc;
867 }
868 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, rtcTimerSecond2, "MC146818 RTC/CMOS - Second2", &pData->pSecondTimer2HC);
869 if (VBOX_FAILURE(rc))
870 {
871 AssertMsgFailed(("pfnTMTimerCreate -> %Vrc\n", rc));
872 return rc;
873 }
874 pData->next_second_time = TMTimerGet(pData->CTXSUFF(pSecondTimer2)) + (TMTimerGetFreq(pData->CTXSUFF(pSecondTimer2)) * 99) / 100;
875 TMTimerSet(pData->CTXSUFF(pSecondTimer2), pData->next_second_time);
876
877 rc = PDMDevHlpIOPortRegister(pDevIns, u16Base, 2, NULL, rtcIOPortWrite, rtcIOPortRead, NULL, NULL, "MC146818 RTC/CMOS");
878 if (VBOX_FAILURE(rc))
879 return rc;
880 if (fGCEnabled)
881 {
882 rc = PDMDevHlpIOPortRegisterGC(pDevIns, u16Base, 2, 0, "rtcIOPortWrite", "rtcIOPortRead", NULL, NULL, "MC146818 RTC/CMOS");
883 if (VBOX_FAILURE(rc))
884 return rc;
885 }
886 if (fR0Enabled)
887 {
888 rc = PDMDevHlpIOPortRegisterR0(pDevIns, u16Base, 2, 0, "rtcIOPortWrite", "rtcIOPortRead", NULL, NULL, "MC146818 RTC/CMOS");
889 if (VBOX_FAILURE(rc))
890 return rc;
891 }
892
893 rc = PDMDevHlpSSMRegister(pDevIns, pDevIns->pDevReg->szDeviceName, iInstance, 1 /* version */, sizeof(*pData),
894 NULL, rtcSaveExec, NULL,
895 NULL, rtcLoadExec, NULL);
896 if (VBOX_FAILURE(rc))
897 return rc;
898
899 /*
900 * Register ourselves as the RTC with PDM.
901 */
902 rc = pDevIns->pDevHlp->pfnRTCRegister(pDevIns, &pData->RtcReg, &pData->pRtcHlpHC);
903 if (VBOX_FAILURE(rc))
904 return rc;
905
906 return VINF_SUCCESS;
907}
908
909
910/**
911 * The device registration structure.
912 */
913const PDMDEVREG g_DeviceMC146818 =
914{
915 /* u32Version */
916 PDM_DEVREG_VERSION,
917 /* szDeviceName */
918 "mc146818",
919 /* szGCMod */
920 "VBoxDDGC.gc",
921 /* szR0Mod */
922 "VBoxDDR0.r0",
923 /* pszDescription */
924 "Motorola MC146818 RTC/CMOS Device.",
925 /* fFlags */
926 PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GUEST_BITS_32_64 | PDM_DEVREG_FLAGS_PAE36 | PDM_DEVREG_FLAGS_GC | PDM_DEVREG_FLAGS_R0,
927 /* fClass */
928 PDM_DEVREG_CLASS_RTC,
929 /* cMaxInstances */
930 1,
931 /* cbInstance */
932 sizeof(RTCState),
933 /* pfnConstruct */
934 rtcConstruct,
935 /* pfnDestruct */
936 NULL,
937 /* pfnRelocate */
938 rtcRelocate,
939 /* pfnIOCtl */
940 NULL,
941 /* pfnPowerOn */
942 NULL,
943 /* pfnReset */
944 NULL,
945 /* pfnSuspend */
946 NULL,
947 /* pfnResume */
948 NULL,
949 /* pfnAttach */
950 NULL,
951 /* pfnDetach */
952 NULL,
953 /* pfnQueryInterface */
954 NULL,
955 /* pfnInitComplete */
956 rtcInitComplete
957};
958#endif /* IN_RING3 */
959#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
960
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette