Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memtransport, move debug prints to utils #68

Merged
merged 9 commits into from
Aug 22, 2024

Conversation

billphipps
Copy link
Contributor

First stage of a sequence of fixes. This one adds cache flush/mem fencing support to mem transport, stolen from the Bernina private repo, which corrects the surprisingly relaxed memory consistency on Apple silicon when running the sim.

@billphipps billphipps requested a review from bigbrett August 20, 2024 17:49
@billphipps billphipps self-assigned this Aug 20, 2024
}

/* Ensure the memcpy is complete */
XMEMFENCE();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are always fencing after a memcpy, should we just pull this into wh_Utils_memcpy_flush()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bug. The fence needed to be after the memset/cpy but before the flush. Updated.

Comment on lines +91 to +113
const void* wh_Utils_CacheInvalidate(const void* p, size_t n)
{
int len = (int)n;
const uint8_t* ptr = (const uint8_t*)p;
do {
XCACHEINVLD(ptr);
ptr += XCACHELINE;
len -= XCACHELINE;
} while (len > 0);
return p;
}

void* wh_Utils_CacheFlush(void* p, size_t n)
{
int len = (int)n;
uint8_t* ptr = (uint8_t*)p;
do {
XCACHEFLUSH(ptr);
ptr += XCACHELINE;
len -= XCACHELINE;
} while (len > 0);
return p;
}
Copy link
Contributor

@bigbrett bigbrett Aug 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this should be a callback function left up to the port.... For example TC3xx lets you flush/invalidate the entire cache, a wordline, or a block, so that should be selectable based on the length passed in. But the HSM cache is totally different, so would use a different implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call. updated.

src/wh_utils.c Outdated

void* wh_Utils_memcpy_invalidate(void* dst, const void* src, size_t n)
{
return memcpy(dst,wh_Utils_CacheInvalidate(src,n),n);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space separate args

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

src/wh_utils.c Outdated

void* wh_Utils_memcpy_flush(void* dst, const void* src , size_t n)
{
return wh_Utils_CacheFlush(memcpy(dst,src,n),n);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space separate args

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

src/wh_utils.c Outdated
#endif

#ifdef DEBUG_CRYPTOCB_VERBOSE
void _hexdump(const char* initial, uint8_t* ptr, size_t size)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no longer static, so might as well rename to wh_Utils_Hexdump

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

/** Cache flushing and memory fencing synchronization primitives */
/* Create a full sequential memory fence to ensure compiler memory ordering */
#ifndef XMEMFENCE
#define XMEMFENCE() __atomic_thread_fence(__ATOMIC_SEQ_CST)
Copy link
Contributor

@bigbrett bigbrett Aug 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is GCC specific - either needs to have a tree of compiler-specific #ifdefs. Also consider moving to wh_settings.h with a non-compiler-specific default value with the expectation it is overridden by ports in wolfhsm_config.h?

Copy link
Contributor Author

@billphipps billphipps Aug 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. This one is good for gcc's and clang and probably others too. Moved to settings.h

Comment on lines 51 to 66
#define XCACHELINE (32)
#endif

/* Flush the cache line at _p. Used after writing to ensure the memory is
* consistent. */
#ifndef XCACHEFLUSH
#define XCACHEFLUSH(_p) (void)(_p)
/* PPC32: __asm__ volatile ("dcbf 0, %0" : : "r" (_p): "memory") */
#endif

/* Invalidate the cache line at _p. Used prior to reading to ensure
* freshness. */
#ifndef XCACHEINVLD
#define XCACHEINVLD(_p) (void)(_p)
/* PPC32: __asm__ volatile ("dcbi 0, %0" : : "r" (_p): "memory") */
#endif
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same thing as above for all these - we should discuss if they should live in wh_settings.h and document the best way for ports to define

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed. Moved already.

@billphipps billphipps requested a review from bigbrett August 20, 2024 19:54
bigbrett
bigbrett previously approved these changes Aug 20, 2024
Copy link
Contributor

@bigbrett bigbrett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, @billphipps has this been tested on SPC?

@billphipps
Copy link
Contributor Author

I did not test on the SPC. Maybe I'll fire that up and make sure I didn't break anything. I'll undo the custom transport and use this instead.

@bigbrett bigbrett self-requested a review August 22, 2024 16:51
Copy link
Contributor

@bigbrett bigbrett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested on TC3xx and works.

@bigbrett bigbrett merged commit d46da52 into wolfSSL:main Aug 22, 2024
1 check passed
@billphipps billphipps deleted the update_memtransport branch September 8, 2024 15:48
jefferyq2 pushed a commit to jefferyq2/wolfHSM that referenced this pull request Nov 10, 2024
Fix memtransport, move debug prints to utils
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants