Skip to content

Commit

Permalink
kernel: change unsafe math functions to their operator counterparts
Browse files Browse the repository at this point in the history
  • Loading branch information
ffwff committed Sep 6, 2019
1 parent fb2dc9c commit 11a7243
Show file tree
Hide file tree
Showing 21 changed files with 89 additions and 73 deletions.
2 changes: 1 addition & 1 deletion pkgs/libcanvas/src
Submodule src updated 2 files
+6 −0 README.md
+1 −1 canvas.h
2 changes: 1 addition & 1 deletion pkgs/wm/wm.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ int main(int argc, char **argv) {
ioctl(fb_fd, TIOCGWINSZ, &ws);

// wallpaper
#if 1
#if 0
struct fbdev_bitblit pape_spr = {
.target_buffer = GFX_BACK_BUFFER,
.source = NULL,
Expand Down
2 changes: 1 addition & 1 deletion src/alloc/alloc.cr
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private struct Pool

# how many blocks can this pool store
def capacity
(POOL_SIZE - HEADER_SIZE).unsafe_div block_size
(POOL_SIZE - HEADER_SIZE) / block_size
end

# first free block in linked list
Expand Down
4 changes: 2 additions & 2 deletions src/alloc/gc.cr
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ module Gc
unless addr >= KernelArena.start_addr && addr <= KernelArena.placement_addr
# must be a nil union, skip
pos += 1
offsets = offsets.unsafe_shr 1
offsets >>= 1
next
end
# mark the header as gray
Expand All @@ -209,7 +209,7 @@ module Gc
end
end
pos += 1
offsets = offsets.unsafe_shr 1
offsets >>= 1
end
node = node.value.next_node
end
Expand Down
5 changes: 2 additions & 3 deletions src/alloc/gc/array.cr
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ class GcArray(T)
end

private def recalculate_capacity
@capacity = (KernelArena.block_size_for_ptr(@ptr) -
(GC_ARRAY_HEADER_SIZE + sizeof(Kernel::GcNode)))
.unsafe_div(sizeof(Void*)).to_isize
@capacity = ((KernelArena.block_size_for_ptr(@ptr) -
(GC_ARRAY_HEADER_SIZE + sizeof(Kernel::GcNode))) / sizeof(Void*)).to_isize
end

# getter/setter
Expand Down
4 changes: 2 additions & 2 deletions src/arch/frame_allocator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module FrameAllocator
property next_region

def _initialize(@base_addr : UInt64, @length : UInt64)
nframes = @length.unsafe_div(0x1000).to_i32
nframes = (@length / 0x1000).to_i32
@frames = PBitArray.new nframes
end

Expand All @@ -28,7 +28,7 @@ module FrameAllocator
end

private def index_for_address(addr : UInt64)
(addr - @base_addr).unsafe_div(0x1000).to_i32
((addr - @base_addr) / 0x1000).to_i32
end

def initial_claim(addr : UInt64)
Expand Down
14 changes: 7 additions & 7 deletions src/arch/gdt.cr
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ module Gdt
entry = Kernel::GdtEntry.new

entry.base_low = (base & 0xFFFF).to_u16
entry.base_middle = (base.unsafe_shr(16) & 0xFF).to_u8
entry.base_high = (base.unsafe_shr(24) & 0xFF).to_u8
entry.base_middle = ((base >> 16) & 0xFF).to_u8
entry.base_high = ((base >> 24) & 0xFF).to_u8

entry.limit_low = (limit & 0xFFFF).to_u16
entry.granularity = (limit.unsafe_shr(16) & 0x0F).to_u8
entry.granularity = ((limit >> 16) & 0x0F).to_u8

entry.granularity |= gran
entry.access = access.to_u8
Expand All @@ -105,12 +105,12 @@ module Gdt
entry = Kernel::GdtSystemEntry.new

entry.base_low = (base & 0xFFFF).to_u16
entry.base_middle = (base.unsafe_shr(16) & 0xFF).to_u8
entry.base_high = (base.unsafe_shr(24) & 0xFF).to_u8
entry.base_higher = base.unsafe_shr(32).to_u32
entry.base_middle = ((base >> 16) & 0xFF).to_u8
entry.base_high = ((base >> 24) & 0xFF).to_u8
entry.base_higher = (base >> 32).to_u32

entry.limit_low = (limit & 0xFFFF).to_u16
entry.granularity = (limit.unsafe_shr(16) & 0x0F).to_u8
entry.granularity = ((limit >> 16) & 0x0F).to_u8

entry.granularity |= gran
entry.access = access.to_u8
Expand Down
4 changes: 2 additions & 2 deletions src/arch/idt.cr
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ module Idt
idt.ist = 0
idt.selector = selector
idt.type_attr = type
idt.offset_2 = offset.unsafe_shr(16) & 0xFFFF
idt.offset_3 = offset.unsafe_shr(32)
idt.offset_2 = (offset >> 16) & 0xFFFF
idt.offset_3 = (offset >> 32)
idt.zero = 0
@@idt[num] = idt
end
Expand Down
8 changes: 4 additions & 4 deletions src/arch/paging.cr
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ module Paging
end

def page_layer_indexes(addr : UInt64)
pdpt_idx = addr.unsafe_shr(39) & (0x200 - 1)
dir_idx = addr.unsafe_shr(30) & (0x200 - 1)
table_idx = addr.unsafe_shr(21) & (0x200 - 1)
page_idx = addr.unsafe_shr(12) & (0x200 - 1)
pdpt_idx = (addr >> 39) & (0x200 - 1)
dir_idx = (addr >> 30) & (0x200 - 1)
table_idx = (addr >> 21) & (0x200 - 1)
page_idx = (addr >> 12) & (0x200 - 1)
Tuple.new(pdpt_idx.to_i32, dir_idx.to_i32, table_idx.to_i32, page_idx.to_i32)
end

Expand Down
37 changes: 27 additions & 10 deletions src/core/int.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ struct Int
end
end

# unsafe math
def /(other)
self.unsafe_div other
end

def %(other)
self.unsafe_mod other
end

def <<(other)
self.unsafe_shl other
end

def >>(other)
self.unsafe_shr other
end

# math
def ~
self ^ -1
Expand All @@ -27,7 +44,7 @@ struct Int
end

def div_ceil(other : Int)
(self + (other - 1)).unsafe_div other
(self + (other - 1)) / other
end

# bit manips
Expand All @@ -40,17 +57,17 @@ struct Int
while (n & (n - 1)) != 0
n = n & (n - 1)
end
n.unsafe_shl 1
n << 1
end

def lowest_power_of_2
x = self
x = x | x.unsafe_shr(1)
x = x | x.unsafe_shr(2)
x = x | x.unsafe_shr(4)
x = x | x.unsafe_shr(8)
x = x | x.unsafe_shr(16)
x - x.unsafe_shr(1)
x = x | (x >> 1)
x = x | (x >> 2)
x = x | (x >> 4)
x = x | (x >> 8)
x = x | (x >> 16)
x - (x >> 1)
end

# format
Expand All @@ -62,9 +79,9 @@ struct Int
n = self.abs
i = 0
while i < 128
s[i] = BASE.bytes[n.unsafe_mod(base)]
s[i] = BASE.bytes[n % base]
i += 1
break if (n = n.unsafe_div(base)) == 0
break if (n /= base) == 0
end
if sign
yield '-'.ord.to_u8
Expand Down
10 changes: 5 additions & 5 deletions src/core/pbit_array.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ struct PBitArray
def []=(k : Int, value : Bool)
panic "pbitarray: out of range" if k > size || k < 0
if value
@pointer[index_position k] |= 1.unsafe_shl(bit_position k)
@pointer[index_position k] |= 1 << bit_position k
else
@pointer[index_position k] &= ~1.unsafe_shl(bit_position k)
@pointer[index_position k] &= ~(1 << bit_position k)
end
end

def [](k : Int) : Bool
panic "pbitarray: out of range" if k > size || k < 0
if (@pointer[index_position k] & 1.unsafe_shl(bit_position k)) != 0
if (@pointer[index_position k] & (1 << bit_position k)) != 0
true
else
false
Expand Down Expand Up @@ -96,10 +96,10 @@ struct PBitArray

# position
private def index_position(k : Int)
k.unsafe_div 32
k / 32
end

private def bit_position(k : Int)
k.unsafe_mod 32
k % 32
end
end
4 changes: 2 additions & 2 deletions src/drivers/arch/cpumsr.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ module X86
lo = 0u32
hi = 0u32
asm("rdmsr" : "={eax}"(lo), "={edx}"(hi) : "{ecx}"(msr) :: "volatile")
hi.to_u64.unsafe_shr(32) | lo.to_u64
(hi.to_u64 >> 32) | lo.to_u64
end

def wrmsr(msr : UInt32, val : UInt64)
lo = (val & 0xFFFF_FFFF).to_u32
hi = (val.unsafe_shr(8) & 0xFFFF_FFFF).to_u32
hi = ((val >> 8) & 0xFFFF_FFFF).to_u32
asm("wrmsr" :: "{eax}"(lo), "{edx}"(hi), "{ecx}"(msr) :: "volatile")
end
end
4 changes: 2 additions & 2 deletions src/drivers/arch/pci.cr
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ module PCI
PCI_NONE = 0xFFFFu16

private def config_address(bus : UInt32, slot : UInt32, func : UInt32, offset : UInt32)
address = bus.unsafe_shl(16) | slot.unsafe_shl(11) |
func.unsafe_shl(8) | (offset & 0xfc) | 0x80000000
address = (bus << 16) | (slot << 11) |
(func << 8) | (offset & 0xfc) | 0x80000000
X86.outl PCI_ADDRESS_PORT, address
end

Expand Down
6 changes: 3 additions & 3 deletions src/drivers/arch/pit_timer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ module Pit

private PIT_CONST = 1193180
FREQUENCY = 100 # Hz
USECS_PER_TICK = 1_000_000.unsafe_div(FREQUENCY)
USECS_PER_TICK = 1_000_000 / FREQUENCY

def init_device
X86.outb(0x43, 0x36)
divisor = PIT_CONST.unsafe_div(FREQUENCY)
divisor = PIT_CONST / FREQUENCY
l = (divisor & 0xFF).to_u8
h = (divisor.unsafe_shr(8) & 0xFF).to_u8
h = ((divisor >> 8) & 0xFF).to_u8
X86.outb(0x40, l)
X86.outb(0x40, h)
end
Expand Down
8 changes: 4 additions & 4 deletions src/drivers/graphics/fbdev.cr
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ extend self
end

def init_device(@@width, @@height, ptr)
@@cwidth = @@width.unsafe_div(FB_ASCII_FONT_WIDTH) - 1
@@cheight = @@height.unsafe_div(FB_ASCII_FONT_HEIGHT) - 1
@@cwidth = (@@width / FB_ASCII_FONT_WIDTH) - 1
@@cheight = (@@height / FB_ASCII_FONT_HEIGHT) - 1
@@buffer = Slice(UInt32).new(ptr, @@width * @@height)
memset(@@buffer.to_unsafe.as(UInt8*), 0u64,
@@width.to_usize * @@height.to_usize * sizeof(UInt32).to_usize)

npages = (@@width.to_usize * @@height.to_usize * sizeof(UInt32).to_usize).unsafe_div 0x1000
npages = (@@width.to_usize * @@height.to_usize * sizeof(UInt32).to_usize) / 0x1000
back_ptr = Paging.alloc_page_pg(FB_BACK_BUFFER_POINTER, true, false, npages)
@@back_buffer = Slice(UInt32).new(Pointer(UInt32).new(back_ptr), @@width * @@height)
memset(@@back_buffer.to_unsafe.as(UInt8*), 0u64,
Expand All @@ -175,7 +175,7 @@ extend self
FB_ASCII_FONT_HEIGHT.times do |cy|
dx = x * FB_ASCII_FONT_WIDTH + cx
dy = y * FB_ASCII_FONT_HEIGHT + cy
if (bitmap[cy] & 1.unsafe_shl(cx)) != 0
if (bitmap[cy] & (1 << cx)) != 0
@@buffer[offset dx, dy] = 0x00FFFFFF
else
@@buffer[offset dx, dy] = 0x0
Expand Down
6 changes: 3 additions & 3 deletions src/drivers/graphics/vga.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ end

private struct VgaInstance < OutputDriver
private def color_code(fg : VgaColor, bg : VgaColor, char : UInt8) : UInt16
attrib = (bg.value.unsafe_shl(4)) | fg.value
attrib.unsafe_shl(8) | char.to_u8
attrib = (bg.value << 4) | fg.value
(attrib << 8) | char.to_u8
end

private def offset(x : Int, y : Int)
Expand Down Expand Up @@ -140,7 +140,7 @@ private struct VgaInstance < OutputDriver
X86.outb(0x3D4, 0x0F)
X86.outb(0x3D5, (pos & 0xFF).to_u8)
X86.outb(0x3D4, 0x0E)
X86.outb(0x3D5, (pos.unsafe_shr(8) & 0xFF).to_u8)
X86.outb(0x3D5, ((pos >> 8) & 0xFF).to_u8)
end
end

Expand Down
22 changes: 11 additions & 11 deletions src/drivers/hdd/ide.cr
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private module Ata
# drive = 0 => primary
# drive = 1 => secondary
def select(bus, slave = 0u8)
X86.outb(bus + REG_HDDEVSEL, 0xA0.to_u8 | slave.unsafe_shl(4))
X86.outb(bus + REG_HDDEVSEL, 0xA0.to_u8 | (slave << 4))
end

def identify(bus)
Expand Down Expand Up @@ -176,13 +176,13 @@ private module Ata
def read(sector, bus, slave)
wait_ready bus

X86.outb(bus + REG_HDDEVSEL, (0xe0 | slave.unsafe_shl(4) |
(sector & 0x0f000000).unsafe_shr(24)).to_u8)
X86.outb(bus + REG_HDDEVSEL, (0xe0 | (slave << 4) |
((sector & 0x0f000000) >> 24)).to_u8)
X86.outb(bus + REG_FEATURES, 0x00)
X86.outb(bus + REG_SECCOUNT0, 1)
X86.outb(bus + REG_LBA0, (sector & 0x000000ff).to_u8)
X86.outb(bus + REG_LBA1, (sector & 0x0000ff00).unsafe_shr(8).to_u8)
X86.outb(bus + REG_LBA2, (sector & 0x00ff0000).unsafe_shr(16).to_u8)
X86.outb(bus + REG_LBA1, ((sector & 0x0000ff00) >> 8).to_u8)
X86.outb(bus + REG_LBA2, ((sector & 0x00ff0000) >> 16).to_u8)
X86.outb(bus + REG_COMMAND, CMD_READ_PIO)
end

Expand All @@ -194,10 +194,10 @@ private module Ata
packet = uninitialized UInt8[SCSI_PACKET_SIZE]
packet[0] = ATAPI_CMD_READ
packet[1] = 0x0u8
packet[2] = (sector.unsafe_shr(24) & 0xFF).to_u8
packet[3] = (sector.unsafe_shr(16) & 0xFF).to_u8
packet[4] = (sector.unsafe_shr(8) & 0xFF).to_u8
packet[5] = (sector.unsafe_shr(0) & 0xFF).to_u8
packet[2] = ((sector >> 24) & 0xFF).to_u8
packet[3] = ((sector >> 16) & 0xFF).to_u8
packet[4] = ((sector >> 8) & 0xFF).to_u8
packet[5] = ((sector >> 0) & 0xFF).to_u8
packet[6] = 0x0u8
packet[7] = 0x0u8
packet[8] = 0x0u8
Expand All @@ -207,12 +207,12 @@ private module Ata

wait_ready bus

X86.outb(bus + REG_HDDEVSEL, slave.unsafe_shl(4).to_u8)
X86.outb(bus + REG_HDDEVSEL, (slave << 4).to_u8)
wait_io bus

X86.outb(bus + REG_FEATURES, 0x00)
X86.outb(bus + REG_LBA0, (sector_size & 0xFF).to_u8)
X86.outb(bus + REG_LBA1, sector_size.unsafe_shr(8).to_u8)
X86.outb(bus + REG_LBA1, (sector_size >> 8).to_u8)
X86.outb(bus + REG_COMMAND, CMD_PACKET)

return unless wait(bus, true)
Expand Down

0 comments on commit 11a7243

Please sign in to comment.