@@ -7,12 +7,6 @@ import (
77 "machine/uefi"
88)
99
10- //go:linkname systemTable machine/uefi.systemTable
11- var systemTable * uefi.EFI_SYSTEM_TABLE
12-
13- //go:linkname imageHandle machine/uefi.imageHandle
14- var imageHandle uintptr
15-
1610func machineTicks () uint64 {
1711 return uefi .Ticks ()
1812}
@@ -33,40 +27,18 @@ func nanosecondsToTicks(ns int64) timeUnit {
3327 return timeUnit (x86 .ConvertTicksFromNanoSeconds (uefi .TicksFrequency (), uint64 (ns )))
3428}
3529
36- // timeOffset
37- // TODO: use rtc
3830var timeOffset int64
3931
4032//go:linkname now time.now
4133func now () (sec int64 , nsec int32 , mono int64 ) {
4234 mono = nanotime ()
4335 efiTime , status := uefi .GetTime ()
4436 if status != uefi .EFI_SUCCESS {
45- uefi .DebugPrint ("WHAT THE " , uint64 (status ))
37+ uefi .DebugPrint ("gettime error " , uint64 (status ))
4638 sec = (mono + timeOffset ) / (1000 * 1000 * 1000 )
4739 nsec = int32 ((mono + timeOffset ) - sec * (1000 * 1000 * 1000 ))
4840 } else {
49- year := int (efiTime .Year )
50- month := int (efiTime .Month ) - 1
51-
52- // Compute days since the absolute epoch.
53- d := daysSinceEpoch (year )
54-
55- // Add in days before this month.
56- d += uint64 (daysBefore [month - 1 ])
57- if isLeap (year ) && month >= March {
58- d ++ // February 29
59- }
60-
61- // Add in days before today.
62- d += uint64 (efiTime .Day - 1 )
63-
64- // Add in time elapsed today.
65- abs := d * secondsPerDay
66- abs += uint64 (uint64 (efiTime .Hour )* uint64 (secondsPerHour ) + uint64 (efiTime .Minute )* uint64 (secondsPerMinute ) + uint64 (efiTime .Second ))
67-
68- sec = int64 (abs ) + (absoluteToInternal + internalToUnix )
69- nsec = int32 (efiTime .Nanosecond )
41+ sec , nsec = efiTime .GetEpoch ()
7042 }
7143 return
7244}
@@ -79,22 +51,22 @@ func sleepTicks(d timeUnit) {
7951 // Busy loop
8052 sleepUntil := ticks () + d
8153 for ticks () < sleepUntil {
54+ x86 .AsmPause ()
8255 }
8356}
8457
8558func putchar (c byte ) {
8659 buf := [2 ]uefi.CHAR16 {uefi .CHAR16 (c ), 0 }
8760 st := uefi .ST ()
88- st .ConOut .OutputString (st . ConOut , & buf [0 ])
61+ st .ConOut .OutputString (& buf [0 ])
8962}
9063
9164func exit (code int ) {
65+ uefi .BS ().Exit (uefi .GetImageHandle (), uefi .EFI_STATUS (code ), 0 , nil )
9266}
9367
9468func abort () {
95- for {
96- // empty
97- }
69+ uefi .BS ().Exit (uefi .GetImageHandle (), uefi .EFI_ABORTED , 0 , nil )
9870}
9971
10072//go:linkname procPin sync/atomic.runtime_procPin
@@ -115,9 +87,10 @@ var stackTop uintptr
11587var allocatePagesAddress uefi.EFI_PHYSICAL_ADDRESS
11688
11789func preinit () {
90+ // status is must be register
11891 var status uefi.EFI_STATUS
11992
120- heapMaxSize = 128 * 1024 * 1024 // 1G for the entire heap
93+ heapMaxSize = 1024 * 1024 * 1024 // 1G for the entire heap
12194
12295 bs := uefi .BS ()
12396 for heapMaxSize > 16 * 1024 * 1024 {
@@ -152,6 +125,29 @@ func growHeap() bool {
152125 return true
153126}
154127
128+ func init () {
129+ var efiTime uefi.EFI_TIME
130+
131+ efiTime , status := uefi .GetTime ()
132+ if status == uefi .EFI_SUCCESS {
133+ sec , nsec := efiTime .GetEpoch ()
134+ timeOffset = sec * 1000000000 + int64 (nsec )
135+ }
136+
137+ go func () {
138+ // Event Loop
139+ uefi .DebugPrint ("HELLO WORLD" , 0 )
140+ for {
141+ uefi .EventLoop ()
142+ Gosched ()
143+ }
144+ }()
145+ }
146+
147+ func waitForEvents () {
148+ uefi .DebugPrint ("waitForEvents" , 0 )
149+ }
150+
155151// Must be a separate function to get the correct stack pointer.
156152//
157153//go:noinline
@@ -174,100 +170,3 @@ func main(imageHandle uintptr, systemTable uintptr) uintptr {
174170 // For libc compatibility.
175171 return 0
176172}
177-
178- // region: time utils
179- const (
180- secondsPerMinute = 60
181- secondsPerHour = 60 * secondsPerMinute
182- secondsPerDay = 24 * secondsPerHour
183- secondsPerWeek = 7 * secondsPerDay
184- daysPer400Years = 365 * 400 + 97
185- daysPer100Years = 365 * 100 + 24
186- daysPer4Years = 365 * 4 + 1
187-
188- // The unsigned zero year for internal calculations.
189- // Must be 1 mod 400, and times before it will not compute correctly,
190- // but otherwise can be changed at will.
191- absoluteZeroYear = - 292277022399
192-
193- // The year of the zero Time.
194- // Assumed by the unixToInternal computation below.
195- internalYear = 1
196-
197- // Offsets to convert between internal and absolute or Unix times.
198- absoluteToInternal int64 = (absoluteZeroYear - internalYear ) * 365.2425 * secondsPerDay
199- internalToAbsolute = - absoluteToInternal
200-
201- unixToInternal int64 = (1969 * 365 + 1969 / 4 - 1969 / 100 + 1969 / 400 ) * secondsPerDay
202- internalToUnix int64 = - unixToInternal
203-
204- wallToInternal int64 = (1884 * 365 + 1884 / 4 - 1884 / 100 + 1884 / 400 ) * secondsPerDay
205- )
206-
207- const (
208- January = 1 + iota
209- February
210- March
211- April
212- May
213- June
214- July
215- August
216- September
217- October
218- November
219- December
220- )
221-
222- // daysBefore[m] counts the number of days in a non-leap year
223- // before month m begins. There is an entry for m=12, counting
224- // the number of days before January of next year (365).
225- var daysBefore = [... ]int32 {
226- 0 ,
227- 31 ,
228- 31 + 28 ,
229- 31 + 28 + 31 ,
230- 31 + 28 + 31 + 30 ,
231- 31 + 28 + 31 + 30 + 31 ,
232- 31 + 28 + 31 + 30 + 31 + 30 ,
233- 31 + 28 + 31 + 30 + 31 + 30 + 31 ,
234- 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 ,
235- 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 ,
236- 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 ,
237- 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 ,
238- 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 ,
239- }
240-
241- // daysSinceEpoch takes a year and returns the number of days from
242- // the absolute epoch to the start of that year.
243- // This is basically (year - zeroYear) * 365, but accounting for leap days.
244- func daysSinceEpoch (year int ) uint64 {
245- y := uint64 (int64 (year ) - absoluteZeroYear )
246-
247- // Add in days from 400-year cycles.
248- n := y / 400
249- y -= 400 * n
250- d := daysPer400Years * n
251-
252- // Add in 100-year cycles.
253- n = y / 100
254- y -= 100 * n
255- d += daysPer100Years * n
256-
257- // Add in 4-year cycles.
258- n = y / 4
259- y -= 4 * n
260- d += daysPer4Years * n
261-
262- // Add in non-leap years.
263- n = y
264- d += 365 * n
265-
266- return d
267- }
268-
269- func isLeap (year int ) bool {
270- return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0 )
271- }
272-
273- //endregion
0 commit comments