These are charged particles (protons and fully ionized heavier nuclei), so electromagnetism is a much more efficient way to reach high energies.
All currently proposed mechanisms for cosmic ray acceleration involve turbulent plasmas and shock fronts that reflect particles magnetically, giving them a small bump in energy each time [1] or rotating magnetic fields [2] in sufficiently extreme environments.
[1] https://en.wikipedia.org/wiki/Fermi_acceleration
[2] https://en.wikipedia.org/wiki/Centrifugal_acceleration_(astr...
Gravity just doesn't play a direct role for accelerating charged particles, it's much too weak compared to the electromagnetic force for a charged particle.