From 6fcb6211866f3ed10dbf8f471ae1722aeb7e70d9 Mon Sep 17 00:00:00 2001 From: Andrea Barisani Date: Tue, 21 Jan 2025 09:14:06 +0100 Subject: [PATCH] tidying --- go.mod | 2 ++ go.sum | 2 ++ internal/reg/port_amd64.s | 8 ++++---- internal/reg/reg32_amd64.s | 12 +++++------ kvm/virtio/descriptor.go | 42 +++++++++++++++++++++----------------- soc/nxp/i2c/i2c.go | 4 ++-- 6 files changed, 39 insertions(+), 31 deletions(-) diff --git a/go.mod b/go.mod index 2fca83ff..23af5369 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/usbarmory/tamago go 1.23.0 + +require golang.org/x/sync v0.10.0 diff --git a/go.sum b/go.sum index e69de29b..cf16d914 100644 --- a/go.sum +++ b/go.sum @@ -0,0 +1,2 @@ +golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= +golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= diff --git a/internal/reg/port_amd64.s b/internal/reg/port_amd64.s index fe22229e..9d67a4b3 100644 --- a/internal/reg/port_amd64.s +++ b/internal/reg/port_amd64.s @@ -7,11 +7,11 @@ // that can be found in the LICENSE file. // func In8(port uint16) (val uint8) -TEXT ·In8(SB),$0-3 +TEXT ·In8(SB),$0-9 MOVW port+0(FP), DX // in al, dx BYTE $0xec - MOVB AL, ret+8(FP) + MOVB AL, val+8(FP) RET // func Out8(port uint16, val uint8) @@ -23,11 +23,11 @@ TEXT ·Out8(SB),$0-3 RET // func In32(port uint32) (val uint32) -TEXT ·In32(SB),$0-8 +TEXT ·In32(SB),$0-12 MOVL port+0(FP), DX // in eax, dx BYTE $0xed - MOVL AX, ret+8(FP) + MOVL AX, val+8(FP) RET // func Out32(port uint32, val uint32) diff --git a/internal/reg/reg32_amd64.s b/internal/reg/reg32_amd64.s index 9c8a2489..d6284d94 100644 --- a/internal/reg/reg32_amd64.s +++ b/internal/reg/reg32_amd64.s @@ -8,15 +8,15 @@ // func Move(dst uint32, src uint32) TEXT ·Move(SB),$0-8 - MOVW dst+0(FP), AX - MOVW src+4(FP), BX + MOVL dst+0(FP), AX + MOVL src+4(FP), BX // copy src to dst - MOVW (AX), CX - MOVW CX, (AX) + MOVL (AX), CX + MOVL CX, (AX) // zero out src - MOVW $0, CX - MOVW CX, (BX) + MOVL $0, CX + MOVL CX, (BX) RET diff --git a/kvm/virtio/descriptor.go b/kvm/virtio/descriptor.go index 857d09b3..7d868d31 100644 --- a/kvm/virtio/descriptor.go +++ b/kvm/virtio/descriptor.go @@ -50,14 +50,6 @@ func (d *Descriptor) Bytes() []byte { return buf.Bytes() } -// SetLength updates the descriptor length field. -func (d *Descriptor) SetLength(length uint32) { - off := 8 - binary.LittleEndian.PutUint32(d.buf[off:], length) - - d.length = length -} - // Init initializes a virtual queue descriptor the given buffer length. func (d *Descriptor) Init(length int, flags uint16) { addr, buf := dma.Reserve(length, 0) @@ -74,6 +66,22 @@ func (d *Descriptor) Destroy() { dma.Release(uint(d.Address)) } +// Read copies the contents of the descriptor buffer to b. +func (d *Descriptor) Read(b []byte) { + copy(b, d.buf) +} + +// Write copies the contents of b to the descriptor buffer, updating its length +// field accordingly. +func (d *Descriptor) Write(b []byte) { + off := 8 + length := uint32(len(b)) + binary.LittleEndian.PutUint32(d.buf[off:], length) + + d.length = length + copy(d.buf, b) +} + // Available represents a VirtIO virtual queue Available ring buffer. // // All exported fields are used one-time at initialization, fields requiring @@ -287,9 +295,9 @@ func (d *VirtualQueue) Pop() (buf []byte) { } avail := d.Used.Ring(d.Used.last % d.size) - buf = make([]byte, avail.Length) - copy(buf, d.Descriptors[avail.Index].buf) + + d.Descriptors[avail.Index].Read(buf) d.Available.index += 1 d.Available.SetRingIndex(d.Available.index%d.size, uint16(avail.Index)) @@ -305,21 +313,17 @@ func (d *VirtualQueue) Push(buf []byte) { d.Lock() defer d.Unlock() - length := len(buf) index := d.Available.Ring(d.Available.index % d.size) + used := d.Used.Index() - d.Used.last - d.Descriptors[index].SetLength(uint32(length)) - copy(d.Descriptors[index].buf, buf) - + d.Descriptors[index].Write(buf) d.Available.SetIndex(d.Available.index + 1) - for used := d.Used.Index() - d.Used.last; used > 0; used-- { - index = used - 1 - avail := d.Used.Ring(used) + for i := used; i > 0; i-- { + avail := d.Used.Ring(i - 1) d.Available.SetRingIndex(d.Available.index%d.size, uint16(avail.Index)) - d.Used.last += 1 } - return + d.Used.last += used } diff --git a/soc/nxp/i2c/i2c.go b/soc/nxp/i2c/i2c.go index 7ab81814..2eca7e3e 100644 --- a/soc/nxp/i2c/i2c.go +++ b/soc/nxp/i2c/i2c.go @@ -276,7 +276,7 @@ func (hw *I2C) tx(buf []byte) (err error) { func (hw *I2C) start(repeat bool) (err error) { var pos int - if repeat == false { + if !repeat { // wait for bus to be free if !reg.WaitFor16(hw.Timeout, hw.i2sr, I2SR_IBB, 1, 0) { return errors.New("timeout waiting bus to be free") @@ -296,7 +296,7 @@ func (hw *I2C) start(repeat bool) (err error) { return errors.New("timeout waiting bus to be busy") } - if repeat == false { + if !repeat { // set Master Transmit mode reg.Set16(hw.i2cr, I2CR_MTX) }